-
-
Notifications
You must be signed in to change notification settings - Fork 913
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: preserve types of passthrough buffers in v1-v7 #865
feat: preserve types of passthrough buffers in v1-v7 #865
Conversation
This looks interesting. I don't have time to dig into this in detail at the moment, but I do have a question ... When I did the TS rewrite, I rather deliberately decided to do away with nodejs-specific types such as So I guess the question here is, "Is this really a good idea"? I suspect I know the answer, but I'd like to hear the high-level argument for this from someone other than myself. Or maybe another way to frame this question is, "Does accepting this change require revisiting that high-level decision (of standardizing on |
This has absolutely been the right choice as
No, it does not. Note how we never reference anything nodejs specific. We only take care to not perform unnecessary type erasure. This really helps in cases like this.db
.prepare('INSERT INTO entry VALUES (?)')
// takes a dependency on a documented implementation property
// not expressed in the type system
.run(v4(undefined, Buffer.alloc(16)) as Buffer);
this.db
.prepare('INSERT INTO entry VALUES (?)')
// unnecessary copy
.run(Buffer.from(v4(undefined, Buffer.alloc(16))));
// technically correct, but eww
const uuid = Buffer.alloc(16);
v4(undefined,uuid);
this.db
.prepare('INSERT INTO entry VALUES (?)')
.run(uuid); This change enables the intuitive to work: this.db
.prepare('INSERT INTO entry VALUES (?)')
.run(v4(undefined, Buffer.alloc(16)));
// this also still works
v4(undefined, new Uint8Array(16)) |
I'd like to have a unit test for this. Can you add the following file: examples/typescript/buffer.test.ts import { v1 } from 'uuid';
// eslint-disable-next-line no-unused-vars
const ret: Buffer = v1(null, Buffer.alloc(16)); With that file in place,
If I understand your use case correctly, this error should go away inyour branch, right? |
That's correct. I went ahead and added a buffer accepting overload to EDIT: If the design is acceptable, I'd continue and adapt the documentation. |
Please revert the API changes to Without the |
5b80863
to
4c176be
Compare
done
Just a clarification in the uuid@11 TL;DR paragraph: 104713f |
Published as |
My pleasure! |
The generator APIs accept a pre-allocated buffer and return said instance, but the function type signature didn't encode this information. Therefore we just need to sprinkle a few generics onto them to make it work.
This is mainly useful for nodejs users where
Buffer
accepting APIs are prevalent. Btw. the overload signatures ensure that the following does not compile:It would be nice if
parse()
could optionally accept a buffer constructor, but that requires a bit more thought.