]> git.kaiwu.me - njs.git/commitdiff
Types: refactored working with TypeScript API description.
authorJakub Jirutka <jakub@jirutka.cz>
Thu, 29 Oct 2020 12:50:05 +0000 (12:50 +0000)
committerJakub Jirutka <jakub@jirutka.cz>
Thu, 29 Oct 2020 12:50:05 +0000 (12:50 +0000)
auto/make
auto/sources
ts/README.md [new file with mode: 0644]
ts/index.d.ts [new file with mode: 0644]
ts/ngx_http_js_module.d.ts
ts/ngx_stream_js_module.d.ts
ts/njs_shell.d.ts
ts/package.json

index c085e95dc0e46a3d10c08beb52fed1330718992f..5c910a7fb003f34ded60952be07a7cf8cac6104a 100644 (file)
--- a/auto/make
+++ b/auto/make
@@ -22,6 +22,9 @@ 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)
+
+NPM = npm
 
 default: $NJS_DEFAULT_TARGET
 
@@ -234,15 +237,6 @@ 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/njs_modules/* $NJS_BUILD_DIR/ts/njs_modules/
-
-ts_test: ts
-       tsc ./test/ts/test.ts
-
 dist:
        rm -rf njs-\$(NJS_VER) \\
        && hg archive njs-\$(NJS_VER).tar.gz \\
@@ -251,6 +245,41 @@ dist:
        && echo njs-\$(NJS_VER).tar.gz done
 END
 
+njs_ts_deps=`echo $NJS_TS_SRCS \
+        | sed -e "s# *\([^ ][^ ]*\)#\1$njs_regex_cont#g"`
+
+cat << END >> $NJS_MAKEFILE
+
+$NJS_BUILD_DIR/ts/package.json: $njs_ts_deps
+       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
+
+$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
+       hg id -i > $NJS_BUILD_DIR/ts/COMMITHASH || true
+       cd $NJS_BUILD_DIR && \$(NPM) pack ./ts
+
+.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_publish: ts_clean $NJS_BUILD_DIR/njs-types-\$(NJS_TYPES_VER).tgz
+       cd $NJS_BUILD_DIR/ && \$(NPM) publish njs-types-\$(NJS_TYPES_VER).tgz
+
+ts_clean:
+       rm -rf $NJS_BUILD_DIR/ts
+END
+
 
 # Makefile.
 
index 418a4d61136af0c7a73c45ffc5de675318168262..b7c0a46594de17fb094f4c3445c64ef7190ac839 100644 (file)
@@ -71,3 +71,5 @@ 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")
diff --git a/ts/README.md b/ts/README.md
new file mode 100644 (file)
index 0000000..774fd46
--- /dev/null
@@ -0,0 +1,79 @@
+# 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
+/// <reference path="./node_modules/njs-types/ngx_http_js_module.d.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",
+      "ES2016.Array.Include",
+      "ES2017.Object",
+      "ES2017.String"
+    ],
+    "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).
diff --git a/ts/index.d.ts b/ts/index.d.ts
new file mode 100644 (file)
index 0000000..8a324cc
--- /dev/null
@@ -0,0 +1,4 @@
+/// <reference path="njs_core.d.ts" />
+/// <reference path="njs_modules/crypto.d.ts" />
+/// <reference path="njs_modules/fs.d.ts" />
+/// <reference path="njs_modules/querystring.d.ts" />
index 0440b7f4f325a2b15c6b4a387dd4be52ef57ecd6..c6b94e4f68e5afb889c5236e6100959d2b403174 100644 (file)
@@ -1,4 +1,4 @@
-/// <reference path="njs_core.d.ts" />
+/// <reference path="index.d.ts" />
 
 interface NginxHTTPArgs {
     readonly [prop: string]: NjsByteString;
index 67e24dfa7853e85c2aaf9726a1dc93fb5114ffbb..46aa76dd6bdcbbfa66558a42b5d2fa038ccf34eb 100644 (file)
@@ -1,4 +1,4 @@
-/// <reference path="njs_core.d.ts" />
+/// <reference path="index.d.ts" />
 
 interface NginxStreamVariables {
     readonly 'binary_remote_addr'?: NjsByteString;
index ce3e3148305ebdc6a1f179d087e750a7f6e42fa0..e818025848148db6b9874828ad5eced28393d650 100644 (file)
@@ -1,4 +1,4 @@
-/// <reference path="njs_core.d.ts" />
+/// <reference path="index.d.ts" />
 
 interface Console {
     log(...args: any[]): void;
index 5bc34d31fb17f1d1c16dae633c9f0883dfd23e1b..e7b2eb77934a4429cc6ee7cadba719672b22e3b9 100644 (file)
@@ -6,7 +6,8 @@
     "lint": "tsc"
   },
   "files": [
-    "**/*.d.ts"
+    "**/*.d.ts",
+    "COMMITHASH"
   ],
   "types": "index.d.ts",
   "repository": {