From 4559ed6910c55db44450cf30c5229383371e38ba Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Mon, 19 Oct 2020 23:51:54 +0200 Subject: [PATCH 01/15] Types: moved all TS type descriptions to ts/ in the repo root. --- auto/make | 7 ++++--- {nginx/ts => ts}/ngx_http_js_module.d.ts | 0 {nginx/ts => ts}/ngx_stream_js_module.d.ts | 0 {src/ts => ts}/njs_core.d.ts | 0 {src/ts => ts/njs_modules}/crypto.d.ts | 2 +- {src/ts => ts/njs_modules}/fs.d.ts | 2 +- {src/ts => ts/njs_modules}/querystring.d.ts | 2 +- {src/ts => ts}/njs_shell.d.ts | 0 8 files changed, 7 insertions(+), 6 deletions(-) rename {nginx/ts => ts}/ngx_http_js_module.d.ts (100%) rename {nginx/ts => ts}/ngx_stream_js_module.d.ts (100%) rename {src/ts => ts}/njs_core.d.ts (100%) rename {src/ts => ts/njs_modules}/crypto.d.ts (98%) rename {src/ts => ts/njs_modules}/fs.d.ts (99%) rename {src/ts => ts/njs_modules}/querystring.d.ts (99%) rename {src/ts => ts}/njs_shell.d.ts (100%) diff --git a/auto/make b/auto/make index 4ec533fee..13f8a2c9c 100644 --- a/auto/make +++ b/auto/make @@ -260,10 +260,11 @@ benchmark: $NJS_BUILD_DIR/njs_auto_config.h \\ $NJS_BUILD_DIR/njs_benchmark +.PHONY: ts ts: - mkdir -p $NJS_BUILD_DIR/ts - cp nginx/ts/*.ts $NJS_BUILD_DIR/ts/ - cp src/ts/*.ts $NJS_BUILD_DIR/ts/ + mkdir -p $NJS_BUILD_DIR/ts/njs_modules + cp ts/*.d.ts $NJS_BUILD_DIR/ts/ + cp ts/njs_modules/* $NJS_BUILD_DIR/ts/njs_modules/ ts_test: ts tsc ./test/ts/test.ts diff --git a/nginx/ts/ngx_http_js_module.d.ts b/ts/ngx_http_js_module.d.ts similarity index 100% rename from nginx/ts/ngx_http_js_module.d.ts rename to ts/ngx_http_js_module.d.ts diff --git a/nginx/ts/ngx_stream_js_module.d.ts b/ts/ngx_stream_js_module.d.ts similarity index 100% rename from nginx/ts/ngx_stream_js_module.d.ts rename to ts/ngx_stream_js_module.d.ts diff --git a/src/ts/njs_core.d.ts b/ts/njs_core.d.ts similarity index 100% rename from src/ts/njs_core.d.ts rename to ts/njs_core.d.ts diff --git a/src/ts/crypto.d.ts b/ts/njs_modules/crypto.d.ts similarity index 98% rename from src/ts/crypto.d.ts rename to ts/njs_modules/crypto.d.ts index 5895b3e74..345a2d1a7 100644 --- a/src/ts/crypto.d.ts +++ b/ts/njs_modules/crypto.d.ts @@ -1,4 +1,4 @@ -/// +/// declare module "crypto" { diff --git a/src/ts/fs.d.ts b/ts/njs_modules/fs.d.ts similarity index 99% rename from src/ts/fs.d.ts rename to ts/njs_modules/fs.d.ts index 682cee490..cc24f243a 100644 --- a/src/ts/fs.d.ts +++ b/ts/njs_modules/fs.d.ts @@ -1,4 +1,4 @@ -/// +/// declare module "fs" { diff --git a/src/ts/querystring.d.ts b/ts/njs_modules/querystring.d.ts similarity index 99% rename from src/ts/querystring.d.ts rename to ts/njs_modules/querystring.d.ts index 0368ec1c4..4058f5a1c 100644 --- a/src/ts/querystring.d.ts +++ b/ts/njs_modules/querystring.d.ts @@ -1,4 +1,4 @@ -/// +/// declare module "querystring" { diff --git a/src/ts/njs_shell.d.ts b/ts/njs_shell.d.ts similarity index 100% rename from src/ts/njs_shell.d.ts rename to ts/njs_shell.d.ts From 453971389056cb64e8b6f8cfc1c5f5c218505b1f Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Tue, 20 Oct 2020 21:16:20 +0200 Subject: [PATCH 02/15] Types: added tsconfig.json and package.json for type descriptions. With strict type checking options enabled (they are recommended to be enabled in all new TypeScript projects). --- ts/index.d.ts | 4 ++++ ts/ngx_http_js_module.d.ts | 2 +- ts/ngx_stream_js_module.d.ts | 2 +- ts/njs_shell.d.ts | 2 +- ts/package.json | 30 ++++++++++++++++++++++++++++++ ts/tsconfig.json | 22 ++++++++++++++++++++++ 6 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 ts/index.d.ts create mode 100644 ts/package.json create mode 100644 ts/tsconfig.json diff --git a/ts/index.d.ts b/ts/index.d.ts new file mode 100644 index 000000000..8a324cc6c --- /dev/null +++ b/ts/index.d.ts @@ -0,0 +1,4 @@ +/// +/// +/// +/// diff --git a/ts/ngx_http_js_module.d.ts b/ts/ngx_http_js_module.d.ts index d29b1385c..7faf1e630 100644 --- a/ts/ngx_http_js_module.d.ts +++ b/ts/ngx_http_js_module.d.ts @@ -1,4 +1,4 @@ -/// +/// interface NginxHTTPArgs { readonly [prop: string]: NjsByteString; diff --git a/ts/ngx_stream_js_module.d.ts b/ts/ngx_stream_js_module.d.ts index 47799ba23..ef903a7e0 100644 --- a/ts/ngx_stream_js_module.d.ts +++ b/ts/ngx_stream_js_module.d.ts @@ -1,4 +1,4 @@ -/// +/// interface NginxStreamVariables { readonly 'binary_remote_addr'?: NjsByteString; diff --git a/ts/njs_shell.d.ts b/ts/njs_shell.d.ts index 3a6aa5ed6..108227ff6 100644 --- a/ts/njs_shell.d.ts +++ b/ts/njs_shell.d.ts @@ -1,4 +1,4 @@ -/// +/// interface Console { log(...args: any[]): void; diff --git a/ts/package.json b/ts/package.json new file mode 100644 index 000000000..5bc34d31f --- /dev/null +++ b/ts/package.json @@ -0,0 +1,30 @@ +{ + "name": "njs-types", + "version": "0.0.0-dev", + "description": "TypeScript definitions for njs scripting language and nginx js modules.", + "scripts": { + "lint": "tsc" + }, + "files": [ + "**/*.d.ts" + ], + "types": "index.d.ts", + "repository": { + "type": "git", + "url": "https://github.com/nginx/njs.git" + }, + "keywords": [ + "nginx", + "njs", + "types" + ], + "author": "NGINX, Inc.", + "license": "BSD-2-Clause", + "bugs": { + "url": "https://github.com/nginx/njs/issues" + }, + "homepage": "https://nginx.org/en/docs/njs/", + "devDependencies": { + "typescript": "^4.0.3" + } +} diff --git a/ts/tsconfig.json b/ts/tsconfig.json new file mode 100644 index 000000000..9184791d6 --- /dev/null +++ b/ts/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "target": "ES5", + "module": "es2015", + "lib": [ + "ES2015" + ], + "noEmit": true, + "downlevelIteration": true, + "strict": true, + "noImplicitAny": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "strictBindCallApply": true, + "strictPropertyInitialization": true, + "noImplicitThis": true, + "alwaysStrict": true, + + "moduleResolution": "node", + "forceConsistentCasingInFileNames": true + } +} From 0b4ca92ba9070f8b55db9615d9b02bd7f876c03b Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Thu, 22 Oct 2020 22:45:00 +0200 Subject: [PATCH 03/15] Added node_modules/ and package-lock.json into .hgignore/.gitignore. To prevent developers from accidentally committing them into the repository. --- .gitignore | 2 ++ .hgignore | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 .gitignore create mode 100644 .hgignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..504afef81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +package-lock.json diff --git a/.hgignore b/.hgignore new file mode 100644 index 000000000..4ef133f11 --- /dev/null +++ b/.hgignore @@ -0,0 +1,2 @@ +/node_modules/ +package-lock.json From 1505741394b33392b2b2a3bbe25d8d1d73e69f64 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Mon, 19 Oct 2020 23:56:57 +0200 Subject: [PATCH 04/15] Make: updated ts task and added ts_lint and ts_publish tasks. --- auto/make | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/auto/make b/auto/make index 13f8a2c9c..1a443c68a 100644 --- a/auto/make +++ b/auto/make @@ -264,11 +264,22 @@ benchmark: $NJS_BUILD_DIR/njs_auto_config.h \\ ts: mkdir -p $NJS_BUILD_DIR/ts/njs_modules cp ts/*.d.ts $NJS_BUILD_DIR/ts/ + cp ts/*.json $NJS_BUILD_DIR/ts/ cp ts/njs_modules/* $NJS_BUILD_DIR/ts/njs_modules/ + cp LICENSE $NJS_BUILD_DIR/ts/ + NJS_VER=`grep NJS_VERSION src/njs.h | sed -e 's/.*"\(.*\)".*/\1/'`; \\ + sed 's/\("version":\s*\)"\([^"]\+\)"/\1"'\$\${NJS_VER}'"/' \\ + ts/package.json > $NJS_BUILD_DIR/ts/package.json + +ts_lint: + tsc -p ./ts ts_test: ts tsc ./test/ts/test.ts +ts_publish: ts + npm publish $NJS_BUILD_DIR/ts + dist: NJS_VER=`grep NJS_VERSION src/njs.h | sed -e 's/.*"\(.*\)".*/\1/'`; \\ rm -rf njs-\$\${NJS_VER} \\ From 65bc79c4dee0e49760e503596b3e9339e9649194 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Thu, 22 Oct 2020 01:51:31 +0200 Subject: [PATCH 05/15] Make: refactored NJV_VER. --- auto/make | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/auto/make b/auto/make index 1a443c68a..3c37519bb 100644 --- a/auto/make +++ b/auto/make @@ -19,6 +19,9 @@ NJS_STATIC_LINK = ${AR} -r -c NJS_LINK = ${CC} ${NJS_LD_OPT} NJS_CFLAGS = ${NJS_CFLAGS} ${NJS_CC_OPT} ${CFLAGS} +NJS_VER = $(grep NJS_VERSION src/njs.h | sed -e 's/.*"\(.*\)".*/\1/') +NJS_TYPES_VER ?= \$(NJS_VER) + default: $NJS_DEFAULT_TARGET END @@ -267,8 +270,7 @@ ts: cp ts/*.json $NJS_BUILD_DIR/ts/ cp ts/njs_modules/* $NJS_BUILD_DIR/ts/njs_modules/ cp LICENSE $NJS_BUILD_DIR/ts/ - NJS_VER=`grep NJS_VERSION src/njs.h | sed -e 's/.*"\(.*\)".*/\1/'`; \\ - sed 's/\("version":\s*\)"\([^"]\+\)"/\1"'\$\${NJS_VER}'"/' \\ + sed 's/\("version":\s*\)"\([^"]\+\)"/\1"\$(NJS_TYPES_VER)"/' \\ ts/package.json > $NJS_BUILD_DIR/ts/package.json ts_lint: @@ -281,12 +283,11 @@ ts_publish: ts npm publish $NJS_BUILD_DIR/ts dist: - NJS_VER=`grep NJS_VERSION src/njs.h | sed -e 's/.*"\(.*\)".*/\1/'`; \\ - rm -rf njs-\$\${NJS_VER} \\ - && hg archive njs-\$\${NJS_VER}.tar.gz \\ - -p njs-\$\${NJS_VER} \\ + rm -rf njs-\$(NJS_VER) \\ + && hg archive njs-\$(NJS_VER).tar.gz \\ + -p njs-\$(NJS_VER) \\ -X ".hg*" \\ - && echo njs-\$\${NJS_VER}.tar.gz done + && echo njs-\$(NJS_VER).tar.gz done END From 83fdea08bea92ed9353ff9cad861505eb05540da Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Tue, 20 Oct 2020 22:29:54 +0200 Subject: [PATCH 06/15] Types: added tsconfig.json and package.json to test/ts. --- test/ts/package.json | 15 +++++++++++++++ test/ts/test.ts | 5 ----- test/ts/tsconfig.json | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 test/ts/package.json create mode 100644 test/ts/tsconfig.json diff --git a/test/ts/package.json b/test/ts/package.json new file mode 100644 index 000000000..3daeea81c --- /dev/null +++ b/test/ts/package.json @@ -0,0 +1,15 @@ +{ + "private": true, + "name": "njs-types-test", + "version": "0.0.0", + "description": "Tests for njs TypeScript type definitions.", + "scripts": { + "test": "tsc" + }, + "author": "NGINX, Inc.", + "license": "BSD-2-Clause", + "devDependencies": { + "njs-types": "file:../../ts", + "typescript": "~4.0.3" + } +} diff --git a/test/ts/test.ts b/test/ts/test.ts index dbb8df7b7..40c378fe0 100644 --- a/test/ts/test.ts +++ b/test/ts/test.ts @@ -1,8 +1,3 @@ -/// -/// -/// -/// - import fs from 'fs'; import qs from 'querystring'; import crypto from 'crypto'; diff --git a/test/ts/tsconfig.json b/test/ts/tsconfig.json new file mode 100644 index 000000000..9f7485b4f --- /dev/null +++ b/test/ts/tsconfig.json @@ -0,0 +1,32 @@ +{ + "compilerOptions": { + "target": "ES5", + "module": "es2015", + "lib": [ + "ES2015" + ], + "noEmit": true, + "downlevelIteration": true, + + "strict": true, + "noImplicitAny": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "strictBindCallApply": true, + "strictPropertyInitialization": true, + "noImplicitThis": true, + "alwaysStrict": true, + + "moduleResolution": "node", + "forceConsistentCasingInFileNames": true + }, + "include": [ + "**/*.ts" + ], + "exclude": [ + "./node_modules/**" + ], + "files": [ + "./node_modules/njs-types/ngx_http_js_module.d.ts" + ] +} From d2a04c49bc2444c8a0b1aa6e1b15235534daf20f Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Thu, 22 Oct 2020 13:39:59 +0200 Subject: [PATCH 07/15] Make: reworked tasks ts_lint and ts_test to utilize npm. ts_test was changed to work as an integration test - it packs njs-types into a package, installs it in test/ts as a dependency, installs typescript from npm registry and then checks types in the test files using the installed tsc. The goal is to verify a typical usage scenario of the njs-types package, including the project's configuration. --- auto/make | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/auto/make b/auto/make index 3c37519bb..baf29d55a 100644 --- a/auto/make +++ b/auto/make @@ -22,6 +22,8 @@ NJS_CFLAGS = ${NJS_CFLAGS} ${NJS_CC_OPT} ${CFLAGS} NJS_VER = $(grep NJS_VERSION src/njs.h | sed -e 's/.*"\(.*\)".*/\1/') NJS_TYPES_VER ?= \$(NJS_VER) +NPM = npm + default: $NJS_DEFAULT_TARGET END @@ -263,24 +265,39 @@ benchmark: $NJS_BUILD_DIR/njs_auto_config.h \\ $NJS_BUILD_DIR/njs_benchmark -.PHONY: ts -ts: - mkdir -p $NJS_BUILD_DIR/ts/njs_modules - cp ts/*.d.ts $NJS_BUILD_DIR/ts/ - cp ts/*.json $NJS_BUILD_DIR/ts/ - cp ts/njs_modules/* $NJS_BUILD_DIR/ts/njs_modules/ +$NJS_BUILD_DIR/ts/package.json: ts/*.json ts/*.d.ts ts/**/*.d.ts + cp -fr ts $NJS_BUILD_DIR/ cp LICENSE $NJS_BUILD_DIR/ts/ sed 's/\("version":\s*\)"\([^"]\+\)"/\1"\$(NJS_TYPES_VER)"/' \\ ts/package.json > $NJS_BUILD_DIR/ts/package.json -ts_lint: - tsc -p ./ts +$NJS_BUILD_DIR/ts/node_modules: $NJS_BUILD_DIR/ts/package.json + cd $NJS_BUILD_DIR/ts && \$(NPM) install + touch $NJS_BUILD_DIR/ts/node_modules + +$NJS_BUILD_DIR/njs-types-\$(NJS_TYPES_VER).tgz: $NJS_BUILD_DIR/ts/package.json + cd $NJS_BUILD_DIR && \$(NPM) pack ./ts + +$NJS_BUILD_DIR/test/ts/package.json: test/ts/*.json test/ts/*.ts + mkdir -p $NJS_BUILD_DIR/test + cp -fr test/ts $NJS_BUILD_DIR/test/ + +$NJS_BUILD_DIR/test/ts/node_modules: $NJS_BUILD_DIR/njs-types-\$(NJS_TYPES_VER).tgz $NJS_BUILD_DIR/test/ts/package.json + cd $NJS_BUILD_DIR/test/ts && \$(NPM) install --save-dev file:../../njs-types-\$(NJS_TYPES_VER).tgz + cd $NJS_BUILD_DIR/test/ts && \$(NPM) install + touch $NJS_BUILD_DIR/test/ts/node_modules + +.PHONY: ts +ts: $NJS_BUILD_DIR/ts/package.json + +ts_lint: $NJS_BUILD_DIR/ts/node_modules + cd $NJS_BUILD_DIR/ts && \$(NPM) run lint -ts_test: ts - tsc ./test/ts/test.ts +ts_test: $NJS_BUILD_DIR/test/ts/node_modules + cd $NJS_BUILD_DIR/test/ts && \$(NPM) test -ts_publish: ts - npm publish $NJS_BUILD_DIR/ts +ts_publish: $NJS_BUILD_DIR/njs-types-\$(NJS_TYPES_VER).tgz + \$(NPM) publish $< dist: rm -rf njs-\$(NJS_VER) \\ From fa9d234045a856661856385fbc4b20facf206608 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sat, 24 Oct 2020 21:47:04 +0200 Subject: [PATCH 08/15] Added COMMITHASH into njs-types package. --- auto/make | 1 + ts/package.json | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/auto/make b/auto/make index baf29d55a..3673d9713 100644 --- a/auto/make +++ b/auto/make @@ -276,6 +276,7 @@ $NJS_BUILD_DIR/ts/node_modules: $NJS_BUILD_DIR/ts/package.json touch $NJS_BUILD_DIR/ts/node_modules $NJS_BUILD_DIR/njs-types-\$(NJS_TYPES_VER).tgz: $NJS_BUILD_DIR/ts/package.json + hg id -i > $NJS_BUILD_DIR/ts/COMMITHASH || true cd $NJS_BUILD_DIR && \$(NPM) pack ./ts $NJS_BUILD_DIR/test/ts/package.json: test/ts/*.json test/ts/*.ts diff --git a/ts/package.json b/ts/package.json index 5bc34d31f..e7b2eb779 100644 --- a/ts/package.json +++ b/ts/package.json @@ -6,7 +6,8 @@ "lint": "tsc" }, "files": [ - "**/*.d.ts" + "**/*.d.ts", + "COMMITHASH" ], "types": "index.d.ts", "repository": { From c5f4825673eeb71c29aefd2c157645772336df7f Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Mon, 19 Oct 2020 23:18:22 +0200 Subject: [PATCH 09/15] Types: fixed index types in ngx_* modules (TS2411). Fixes the following type errors: Property ''xxx'' of type 'NjsByteString | undefined' is not assignable to string index type 'NjsStringLike'.ts(2411) https://www.typescriptlang.org/docs/handbook/interfaces.html#indexable-types: > While string index signatures are a powerful way to describe the > "dictionary" pattern, they also **enforce that all properties match > their return type.** --- ts/ngx_http_js_module.d.ts | 6 +++--- ts/ngx_stream_js_module.d.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ts/ngx_http_js_module.d.ts b/ts/ngx_http_js_module.d.ts index 7faf1e630..c6b94e4f6 100644 --- a/ts/ngx_http_js_module.d.ts +++ b/ts/ngx_http_js_module.d.ts @@ -39,7 +39,7 @@ interface NginxHeadersIn { readonly 'Warning'?: NjsByteString; readonly 'X-Forwarded-For'?: NjsByteString; - readonly [prop: string]: NjsByteString; + readonly [prop: string]: NjsByteString | undefined; } interface NginxHeadersOut { @@ -76,7 +76,7 @@ interface NginxHeadersOut { 'Set-Cookie'?: NjsStringLike[]; - [prop: string]: NjsStringLike | NjsStringLike[]; + [prop: string]: NjsStringLike | NjsStringLike[] | undefined; } interface NginxVariables { @@ -229,7 +229,7 @@ interface NginxVariables { readonly 'upstream_trailer_'?: NjsByteString; readonly 'uri'?: NjsByteString; - [prop: string]: NjsStringLike; + [prop: string]: NjsStringLike | undefined; } interface NginxSubrequestOptions { diff --git a/ts/ngx_stream_js_module.d.ts b/ts/ngx_stream_js_module.d.ts index ef903a7e0..46aa76dd6 100644 --- a/ts/ngx_stream_js_module.d.ts +++ b/ts/ngx_stream_js_module.d.ts @@ -66,7 +66,7 @@ interface NginxStreamVariables { readonly 'time_iso8601'?: NjsByteString; readonly 'time_local'?: NjsByteString; - [prop: string]: NjsByteString; + [prop: string]: NjsByteString | undefined; } interface NginxStreamCallbackFlags { From 15a67d219b793a67d1863e87d2600c98afbe9e93 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Thu, 22 Oct 2020 22:37:06 +0200 Subject: [PATCH 10/15] Types: fixed TS errors in test.ts with strict checks enabled. --- test/ts/test.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/test/ts/test.ts b/test/ts/test.ts index 40c378fe0..ce7940ded 100644 --- a/test/ts/test.ts +++ b/test/ts/test.ts @@ -10,9 +10,9 @@ function http_module(r: NginxHTTPRequest) { s = 'ordinary string'; bs = String.bytesFrom('000000', 'hex'); - bs = s.toBytes(); + var bs2: NjsByteString | null = s.toBytes(); bs = s.toUTF8(); - bs.fromBytes(null, null); + bs.fromBytes(undefined, undefined); s = bs + ''; @@ -25,29 +25,30 @@ function http_module(r: NginxHTTPRequest) { bs = r.args.x; bs = r.args[1]; - s = r.args.x.fromUTF8(); + var s2: string | null = r.args.x.fromUTF8(); s = r.args.x + ''; // r.headersIn - r.headersIn['Accept'].fromBytes() == 'dddd'; + r.headersIn['Accept']?.fromBytes() == 'dddd'; // r.headersOut r.headersOut['Content-Type'] = 'text/plain'; // Warning: r.headersOut['Content-Type'] = ['a', 'b']; r.headersOut['Connection'] = undefined; - r.headersOut['Connection'] = null; + + delete r.headersOut['Bar']; r.headersOut['Set-Cookie'] = ['aaa', 'bbb']; r.headersOut['Foo'] = ['aaa', 'bbb']; - r.subrequest('/uri', reply => r.return(200, reply.headersOut["Location"])); + r.subrequest('/uri', reply => r.return(200, reply.headersOut["Location"] ?? '')); // r.log r.log(bs); - r.log(r.headersOut['Connection']); + r.log(r.headersOut['Connection'] ?? ''); // r.variables @@ -59,7 +60,7 @@ function http_module(r: NginxHTTPRequest) { r.subrequest('/p/sub2', reply => r.return(reply.status)); r.subrequest('/p/sub3', {detached:true}); r.subrequest('/p/sub4', 'a=1&b=2').then(reply => r.return(reply.status, - JSON.stringify(JSON.parse(reply.responseBody)))); + JSON.stringify(JSON.parse(reply.responseBody ?? '')))); } From c3786223f977c051a58c1bed9deb69b9965169ba Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sat, 24 Oct 2020 23:59:18 +0200 Subject: [PATCH 11/15] Types: added missing var "console" into njs_shell. --- ts/njs_shell.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ts/njs_shell.d.ts b/ts/njs_shell.d.ts index 108227ff6..e81802584 100644 --- a/ts/njs_shell.d.ts +++ b/ts/njs_shell.d.ts @@ -7,3 +7,5 @@ interface Console { time(label?: NjsStringLike): void; timeEnd(label?: NjsStringLike): void; } + +declare const console: Console; From 020655580854af946800a38130fdc085b5895964 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sat, 24 Oct 2020 15:02:59 +0200 Subject: [PATCH 12/15] Types: added Readme for npmjs to njs-types. --- ts/README.md | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 ts/README.md diff --git a/ts/README.md b/ts/README.md new file mode 100644 index 000000000..c63115e55 --- /dev/null +++ b/ts/README.md @@ -0,0 +1,76 @@ +# TypeScript definitions for njs + +This package contains type definitions for [njs](https://github.com/nginx/njs) – NGINX JavaScript. + + +## Usage + +Install **njs-types** from the [npm registry](https://www.npmjs.com/) into your project: + +```sh +# using npm: +npm install --save-dev njs-types +# or using yarn: +yarn add --dev njs-types +``` + +njs-types provides three entry points with global declarations for each of njs environments: + +* `njs_shell.d.ts` – njs shell +* `ngx_http_js_module.d.ts` – NGINX JS HTTP Module +* `ngx_stream_js_module.d.ts` – NGINX JS Stream Module + +You can either reference them using [triple-slash directive](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html) at top of your `.ts` files (adjust `path` to point into your project’s node_modules): + +```ts +/// +``` + +or include them using the [files](https://www.typescriptlang.org/tsconfig#files) flag in your `tsconfig.json`, for example: + +```json +{ + "compilerOptions": { + "target": "ES5", + "module": "es2015", + "lib": [ + "ES2015", + ], + "outDir": "./lib", + "downlevelIteration": true, + + "strict": true, + "noImplicitAny": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "strictBindCallApply": true, + "strictPropertyInitialization": true, + "noImplicitThis": true, + "alwaysStrict": true, + + "moduleResolution": "node", + + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + }, + "include": [ + "./src", + ], + "files": [ + "./node_modules/njs-types/ngx_http_js_module.d.ts", + ], +} +``` + + +## Versions + +njs-types is typically being released together with njs. +Their major and minor release numbers (the first two numbers) are always aligned, but the patch version (the third number) may differ. +That's because njs-types may be updated between njs releases and in such case the patch version is incremented. + +It's the same strategy as used in [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped#how-do-definitely-typed-package-versions-relate-to-versions-of-the-corresponding-library). +The reason is that npmjs enforces [SemVer](https://semver.org/) which doesn't allow four-part version number nor provide post-release suffixes. + +You can find from which commit the package was built in file `COMMITHASH` inside the published package. +It contains global revision id in the upstream repository https://hg.nginx.org/njs/ (Mercurial). From 3f407ce2e4d343ad11d1437b2a1d1415fd12f4be Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Tue, 27 Oct 2020 22:16:28 +0100 Subject: [PATCH 13/15] fixup: avoid using globstar bash extention in make --- auto/make | 4 ++-- auto/sources | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/auto/make b/auto/make index 3673d9713..5484dcff9 100644 --- a/auto/make +++ b/auto/make @@ -265,7 +265,7 @@ benchmark: $NJS_BUILD_DIR/njs_auto_config.h \\ $NJS_BUILD_DIR/njs_benchmark -$NJS_BUILD_DIR/ts/package.json: ts/*.json ts/*.d.ts ts/**/*.d.ts +$NJS_BUILD_DIR/ts/package.json: $NJS_TS_SRCS cp -fr ts $NJS_BUILD_DIR/ cp LICENSE $NJS_BUILD_DIR/ts/ sed 's/\("version":\s*\)"\([^"]\+\)"/\1"\$(NJS_TYPES_VER)"/' \\ @@ -279,7 +279,7 @@ $NJS_BUILD_DIR/njs-types-\$(NJS_TYPES_VER).tgz: $NJS_BUILD_DIR/ts/package.json hg id -i > $NJS_BUILD_DIR/ts/COMMITHASH || true cd $NJS_BUILD_DIR && \$(NPM) pack ./ts -$NJS_BUILD_DIR/test/ts/package.json: test/ts/*.json test/ts/*.ts +$NJS_BUILD_DIR/test/ts/package.json: $NJS_TS_TEST_SRCS mkdir -p $NJS_BUILD_DIR/test cp -fr test/ts $NJS_BUILD_DIR/test/ diff --git a/auto/sources b/auto/sources index 418a4d611..22622b00a 100644 --- a/auto/sources +++ b/auto/sources @@ -71,3 +71,7 @@ NJS_TEST_SRCS=" \ src/test/njs_unit_test.c \ src/test/njs_benchmark.c \ " + +NJS_TS_SRCS=$(find ts/ -name "*.d.ts" -o -name "*.json" | xargs) + +NJS_TS_TEST_SRCS=$(find test/ts/ -name "*.ts" -o -name "*.json" | xargs) From 4412267a015d5ecdf309cae004f98db4afd19742 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Tue, 27 Oct 2020 22:57:10 +0100 Subject: [PATCH 14/15] fixup: fixed problem with ts_publish and NJS_TYPES_VER --- auto/make | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/auto/make b/auto/make index 5484dcff9..871354ff5 100644 --- a/auto/make +++ b/auto/make @@ -297,9 +297,12 @@ ts_lint: $NJS_BUILD_DIR/ts/node_modules ts_test: $NJS_BUILD_DIR/test/ts/node_modules cd $NJS_BUILD_DIR/test/ts && \$(NPM) test -ts_publish: $NJS_BUILD_DIR/njs-types-\$(NJS_TYPES_VER).tgz +ts_publish: ts_clean $NJS_BUILD_DIR/njs-types-\$(NJS_TYPES_VER).tgz \$(NPM) publish $< +ts_clean: + rm -rf $NJS_BUILD_DIR/ts $NJS_BUILD_DIR/test/ts + dist: rm -rf njs-\$(NJS_VER) \\ && hg archive njs-\$(NJS_VER).tar.gz \\ From 708c204d57ba79691e1438eeb0cd052600cb5f35 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Thu, 29 Oct 2020 12:35:25 +0100 Subject: [PATCH 15/15] fixup: fixed compilerOptions.libs in tsconfig.json. --- test/ts/tsconfig.json | 5 ++++- ts/README.md | 3 +++ ts/tsconfig.json | 5 ++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/test/ts/tsconfig.json b/test/ts/tsconfig.json index 9f7485b4f..f11f689a5 100644 --- a/test/ts/tsconfig.json +++ b/test/ts/tsconfig.json @@ -3,7 +3,10 @@ "target": "ES5", "module": "es2015", "lib": [ - "ES2015" + "ES2015", + "ES2016.Array.Include", + "ES2017.Object", + "ES2017.String" ], "noEmit": true, "downlevelIteration": true, diff --git a/ts/README.md b/ts/README.md index c63115e55..de28f6d6b 100644 --- a/ts/README.md +++ b/ts/README.md @@ -35,6 +35,9 @@ or include them using the [files](https://www.typescriptlang.org/tsconfig#files) "module": "es2015", "lib": [ "ES2015", + "ES2016.Array.Include", + "ES2017.Object", + "ES2017.String", ], "outDir": "./lib", "downlevelIteration": true, diff --git a/ts/tsconfig.json b/ts/tsconfig.json index 9184791d6..b9a969a9a 100644 --- a/ts/tsconfig.json +++ b/ts/tsconfig.json @@ -3,7 +3,10 @@ "target": "ES5", "module": "es2015", "lib": [ - "ES2015" + "ES2015", + "ES2016.Array.Include", + "ES2017.Object", + "ES2017.String" ], "noEmit": true, "downlevelIteration": true,