-
Notifications
You must be signed in to change notification settings - Fork 253
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
UUID convenience class #425
UUID convenience class #425
Conversation
} | ||
|
||
/** | ||
* Generate a 16 byte uuid v4 buffer used in UUIDs |
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.
Is this a good place to discuss how random data is generated?
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.
Can we rely on the 'uuid'
dependency here? will it correctly find the the right crypto function across platforms? If not I believe it allows you to provide the correct function as an argument.
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've added a warning if/when the current implementation falls back to using insecureRandomBytes
.
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.
There's some changes needed here to bring us in line with spec and I don't see why we can't use the uuid library as a dependency, hopefully that'll make life a bit easier here. Thanks for taking a crack at this!
} | ||
|
||
/** | ||
* Generate a 16 byte uuid v4 buffer used in UUIDs |
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.
Can we rely on the 'uuid'
dependency here? will it correctly find the the right crypto function across platforms? If not I believe it allows you to provide the correct function as an argument.
…dded to UUID & Binary for convertion between the two.
it('should correctly generate a valid UUID v4 from empty constructor', () => { | ||
const uuid = new UUID(); | ||
const uuidHexStr = uuid.toHexString(); | ||
expect(uuidStringValidate(uuidHexStr)).to.be.true; | ||
expect(uuidStringVersion(uuidHexStr)).to.equal(Binary.SUBTYPE_UUID); | ||
}); |
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.
This is a nondeterministic test (as it tests auto-generation). Is this welcome as a test case?
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.
One minor nit, but this LGTM, thanks for the contribution! 👍
: 'BSON: No cryptographic implementation for random bytes present, falling back to a less secure implementation.'; | ||
|
||
const insecureRandomBytes: RandomBytesFunction = function insecureRandomBytes(size: number) { | ||
console.warn(insecureWarning); |
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 think it'd be better to use process.emitWarning
here.
console.warn(insecureWarning); | |
process.emitWarning(insecureWarning); |
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.
Thank you, but wouldn't this throw in a browser context without crypto?
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.
LGTM 👍
Thanks for all the work done to get this in! A question though: The js-bson/src/parser/serializer.ts Lines 774 to 846 in 210fc11
I was wondering if this is on purpose, or an oversight. And would a PR be accepted to add this type to the list? |
For anyone having the same issue. This seems to solve it for me: Object.defineProperty(UUID.prototype, 'toBSON', {
get() {
return this.toBinary;
},
}); The Or was to intended behaviour to call it manually for each value on each save? That get's problematic quickly, especially with nested UUIDs. Edit: There is still the issue of deserializing though. |
UUID convenience class
Convenience class making uuid creations easier & typed, allowing:
I've kept this PR as a draft, as there are still left in comments as
// NOTE:
that should be addressed.Changes/approaches:
UUID
convenience class.UUID
andBinary
to convert between eachother.'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
).crypto
detection to includeglobal
, for allowing packages such as react-native-get-random-values to populate implementglobal.crypto. getRandomValues
in the context of React Native.randomBytes
detection falls back toinsecureRandomBytes
(with added guidance for React Native).[value] instanceof Buffer
has been replaced withBuffer.isBuffer(value)
, asinstanceof
yields problems when multiple packages depends onBuffer
implementations, in browser builds. (as each package is scoped)