-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add TypeScript types for modules #342
Conversation
@xeioex, could you please give me some feedback and answer the questions above? |
Sorry for delay, I plan to return to your merge request later.
No, it replicates API for node.js Buffer
Basically it is a pseudotype for TS, which says that some function accepts both string and NjsByteString. In the future, we plan to get rid of NjsByteString (as a non-standard type) in favour of Buffer. |
Regarding the modules API, we are trying to follow the original API for them, can't we just copy the existing API description for them? |
Thanks for the quick response!
Okay, I will update the type definitions.
I understand that. I just don’t know how to recognize what functions accept/return
You mean Node.js API? You don’t follow them fully, there are some minor differences (e.g. missing functions, some extra arguments not supported, limited set of encodings etc.). It’s more a subset of them. The TS types must to be accurate to be useful. BTW, I have copied JSDoc comments from http://nginx.org/en/docs/njs/reference.html, not Node.js docs, both due to differences and to avoid potential legal issues (not sure if there are any, just to be sure). |
Node’s Buffer has quite a large API surface – does njs support all of its (non-deprecated) methods with all arguments? What encodings does it support? |
yes, that is the point of deprecation of NjsByteString type. Currently, we basically have two types The only difference is the way they treat characters. String.prototype.toUTF8(), and String.prototype.fromUTF8() are used to convert in both directions. |
We plan to release the documentation for it by the end of this week. With the exception of: buf.keys(), buf.entries(), buf.values(), buf.readBigInt64BE() (and other BigInt methods). The available encodings are: utf8, hex, base64, base64url. |
BTW, I haven’t tested it yet, but it should be possible to generate TS types and HTML documentation from TSDoc comments included directly in the |
It would be great if you share a working POC (using a |
http://nginx.org/en/docs/njs/reference.html#buffer - Buffer docs. |
Okay, I’ve just added the Buffer type to this PR. |
cc5e787
to
2496cce
Compare
If I understand |
Hi @jirutka This is my mistake, I will fix it soon. |
Try this patch: # HG changeset patch
# User Alexander Borisov <alexander.borisov@nginx.com>
# Date 1602919869 -10800
# Sat Oct 17 10:31:09 2020 +0300
# Node ID b4429c95c89a1b54da3450aef85db96d0f650d23
# Parent a82f123409b7c0c8b97417e11dffde523018ed30
Fixed returned value for Buffer.prototype.write* functions.
All Buffer.prototype.write* functions must return offset plus
the number of bytes written.
The issue was introduced in 27bb9caf186c.
diff -r a82f123409b7 -r b4429c95c89a src/njs_buffer.c
--- a/src/njs_buffer.c Tue Oct 13 15:44:33 2020 +0300
+++ b/src/njs_buffer.c Sat Oct 17 10:31:09 2020 +0300
@@ -1331,7 +1331,7 @@ njs_buffer_prototype_write_int(njs_vm_t
break;
}
- njs_set_undefined(&vm->retval);
+ njs_set_number(&vm->retval, index + size);
return NJS_OK;
}
diff -r a82f123409b7 -r b4429c95c89a src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c Tue Oct 13 15:44:33 2020 +0300
+++ b/src/test/njs_unit_test.c Sat Oct 17 10:31:09 2020 +0300
@@ -19191,7 +19191,11 @@ static njs_unit_test_t njs_test[] =
" if (size > 1) { rmethod += endianness; wmethod += endianness; };"
" var v = 0x7abbccddeeff & (size * 8 - 1);"
""
- " buf[wgmethod](v, offset, size);"
+ " var ret = buf[wgmethod](v, offset, size);"
+ " if(ret !== offset + size) {"
+ " throw Error(`${wgmethod} returned ${ret}, need ${offset + size}`);"
+ " }"
+ ""
" var gv = buf[rgmethod](offset, size);"
""
" buf.fill(0);" |
2443e3c
to
fd5c0c7
Compare
I’ve polished it a bit and I think that it’s ready. |
f74ca06
to
033ce0a
Compare
372811f
to
8f4fc6d
Compare
I’ve fixed all the mentioned issues in the It seems that - readFileSync(path: NjsStringLike, options: { encoding?: FileEncoding; flag?: OpenMode; } | FileEncoding): NjsStringLike;
+ readFileSync(path: NjsStringLike, options: { encoding?: FileEncoding; flag?: OpenMode; } | FileEncoding): string; - readdirSync(path: NjsStringLike, options?: { encoding?: "utf8"; withFileTypes?: false; } | "utf8"): NjsStringLike[];
+ readdirSync(path: NjsStringLike, options?: { encoding?: "utf8"; withFileTypes?: false; } | "utf8"): string[]; - realpathSync(path: NjsStringLike, options?: { encoding?: "utf8" } | "utf8"): NjsStringLike;
+ realpathSync(path: NjsStringLike, options?: { encoding?: "utf8" } | "utf8"): string; - readFile(path: NjsStringLike, options: { encoding?: FileEncoding; flag?: OpenMode; } | FileEncoding): Promise<NjsStringLike>;
+ readFile(path: NjsStringLike, options: { encoding?: FileEncoding; flag?: OpenMode; } | FileEncoding): Promise<string>; - readdir(path: NjsStringLike, options?: { encoding?: "utf8"; withFileTypes?: false; } | "utf8"): Promise<NjsStringLike[]>;
+ readdir(path: NjsStringLike, options?: { encoding?: "utf8"; withFileTypes?: false; } | "utf8"): Promise<string[]>; - realpath(path: NjsStringLike, options?: { encoding?: "utf8" } | "utf8"): Promise<NjsStringLike>;
+ realpath(path: NjsStringLike, options?: { encoding?: "utf8" } | "utf8"): Promise<string>; I’ve also changed |
@jirutka yes, since 0.4.4 "fs" method ensures that ordinary string are returned (as a part of the plan to eliminate NjsByteString). But, still NjsByteString can be created with deprecated method String.bytesFrom() (will be removed in the future). |
Created according to http://nginx.org/en/docs/njs/reference.html. Signed-off-by: Jakub Jirutka <jakub@jirutka.cz>
Okay, updated: all the path parameters accept I’ve also tagged |
Created according to http://nginx.org/en/docs/njs/reference.html. Signed-off-by: Jakub Jirutka <jakub@jirutka.cz>
Created according to http://nginx.org/en/docs/njs/reference.html. Signed-off-by: Jakub Jirutka <jakub@jirutka.cz>
Created according to http://nginx.org/en/docs/njs/reference.html. Signed-off-by: Jakub Jirutka <jakub@jirutka.cz>
nginx/ts/ngx_stream_js_module.d.ts
Outdated
@@ -66,7 +66,7 @@ interface NginxStreamVariables { | |||
readonly 'time_iso8601'?: NjsByteString; | |||
readonly 'time_local'?: NjsByteString; | |||
|
|||
[prop: string]: NjsByteString; | |||
[prop: string]: NjsByteString | undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jirutka Can you share the exact code snippet that causes the issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, it’s part of #344:
git clone -b njs-types-pkg git@github.com:jirutka/njs.git
cd njs/ts
npm install
npm test
The problem is that you’re running tsc
with the defaults – backward compatible settings that are very lax and shouldn’t be used for any new projects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for mixing this commit into this PR, I should have created a new PR for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m gonna move this commit into #344 and close this PR, ’cause it has been already merged, except this one.
@jirutka BTW, is there a proper way to reference internal types like Hash? https://github.com/nginx/njs/blob/master/test/ts/test.ts#L87 Something like: var h:Hash;
h = crypto.createHash('sha1'); |
Well, import crypto, { Hash } from 'crypto'
const h: Hash = crypto.createHash('sha1') TypeScript will automatically remove named imports of types from the generated ES2015 code. |
Questions:
Buffer
– ArrayBuffer or some custom type?NjsStringLike
and wherestring
?Patch: https://patch-diff.githubusercontent.com/raw/nginx/njs/pull/342.patch