Skip to content

UUID convenience class #425

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

Merged
merged 21 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"ts-node": "^9.0.0",
"typedoc": "^0.18.0",
"typescript": "^4.0.2",
"typescript-cached-transpile": "0.0.6"
"typescript-cached-transpile": "0.0.6",
"uuid": "^8.3.2"
},
"config": {
"native": false
Expand Down
5 changes: 3 additions & 2 deletions src/binary.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Buffer } from 'buffer';
import { ensureBuffer } from './ensure_buffer';
import { uuidHexStringToBuffer } from './uuid_utils';
import type { EJSONOptions } from './extended_json';
import { parseUUID, UUIDExtended } from './uuid';
import type { UUIDExtended } from './uuid';

/** @public */
export type BinarySequence = Uint8Array | Buffer | number[];
Expand Down Expand Up @@ -248,7 +249,7 @@ export class Binary {
}
} else if ('$uuid' in doc) {
type = 4;
data = Buffer.from(parseUUID(doc.$uuid));
data = uuidHexStringToBuffer(doc.$uuid);
}
if (!data) {
throw new TypeError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
Expand Down
2 changes: 2 additions & 0 deletions src/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Map } from './map';
import { MaxKey } from './max_key';
import { MinKey } from './min_key';
import { ObjectId } from './objectid';
import { UUID } from './uuid';
import { calculateObjectSize as internalCalculateObjectSize } from './parser/calculate_size';
// Parts of the parser
import { deserialize as internalDeserialize, DeserializeOptions } from './parser/deserializer';
Expand Down Expand Up @@ -81,6 +82,7 @@ export {
DBRef,
Binary,
ObjectId,
UUID,
Long,
Timestamp,
Double,
Expand Down
4 changes: 2 additions & 2 deletions src/objectid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export class ObjectId {
typeof otherId === 'string' &&
ObjectId.isValid(otherId) &&
otherId.length === 12 &&
this.id instanceof Buffer
Buffer.isBuffer(this.id)
) {
return otherId === this.id.toString('binary');
}
Expand Down Expand Up @@ -313,7 +313,7 @@ export class ObjectId {
return true;
}

if (id instanceof Buffer && id.length === 12) {
if (Buffer.isBuffer(id) && id.length === 12) {
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions src/parser/calculate_size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ function calculateElement(
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
} else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (12 + 1);
} else if (value['_bsontype'] === 'UUID') {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 4 + 1) + 16;
} else if (value instanceof Date || isDate(value)) {
return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
} else if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) {
Expand Down
7 changes: 7 additions & 0 deletions src/parser/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Long } from '../long';
import { MaxKey } from '../max_key';
import { MinKey } from '../min_key';
import { ObjectId } from '../objectid';
import { UUID } from '../uuid';
import { BSONRegExp } from '../regexp';
import { BSONSymbol } from '../symbol';
import { Timestamp } from '../timestamp';
Expand Down Expand Up @@ -334,6 +335,9 @@ function deserializeObject(

if (promoteBuffers && promoteValues) {
object[name] = buffer.slice(index, index + binarySize);
} else if (subType === Binary.SUBTYPE_UUID) {
// NOTE: This is breaking. What previously would return an instance of Binary, now returns a UUID instance.
object[name] = new UUID(buffer.slice(index, index + binarySize));
} else {
object[name] = new Binary(buffer.slice(index, index + binarySize), subType);
}
Expand Down Expand Up @@ -361,6 +365,9 @@ function deserializeObject(

if (promoteBuffers && promoteValues) {
object[name] = _buffer;
} else if (subType === Binary.SUBTYPE_UUID) {
// NOTE: This is breaking. What previously would return an instance of Binary, now returns a UUID instance.
object[name] = new UUID(_buffer);
} else {
object[name] = new Binary(_buffer, subType);
}
Expand Down
24 changes: 24 additions & 0 deletions src/parser/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,14 @@ export function serializeInto(
index = serializeNull(buffer, key, value, index, true);
} else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
index = serializeObjectId(buffer, key, value, index, true);
} else if (value['_bsontype'] === 'UUID') {
index = serializeBinary(
buffer,
key,
new Binary(value.id, Binary.SUBTYPE_UUID),
index,
true
);
} else if (isBuffer(value) || isUint8Array(value)) {
index = serializeBuffer(buffer, key, value, index, true);
} else if (value instanceof RegExp || isRegExp(value)) {
Expand Down Expand Up @@ -892,6 +900,14 @@ export function serializeInto(
index = serializeNull(buffer, key, value, index);
} else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
index = serializeObjectId(buffer, key, value, index);
} else if (value['_bsontype'] === 'UUID') {
index = serializeBinary(
buffer,
key,
new Binary(value.id, Binary.SUBTYPE_UUID),
index,
true
);
} else if (isBuffer(value) || isUint8Array(value)) {
index = serializeBuffer(buffer, key, value, index);
} else if (value instanceof RegExp || isRegExp(value)) {
Expand Down Expand Up @@ -998,6 +1014,14 @@ export function serializeInto(
index = serializeNull(buffer, key, value, index);
} else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
index = serializeObjectId(buffer, key, value, index);
} else if (value['_bsontype'] === 'UUID') {
index = serializeBinary(
buffer,
key,
new Binary(value.id, Binary.SUBTYPE_UUID),
index,
true
);
} else if (isBuffer(value) || isUint8Array(value)) {
index = serializeBuffer(buffer, key, value, index);
} else if (value instanceof RegExp || isRegExp(value)) {
Expand Down
11 changes: 9 additions & 2 deletions src/parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ declare let global: any;
declare const self: unknown;

export let randomBytes = insecureRandomBytes;
if (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues) {
randomBytes = size => window.crypto.getRandomValues(Buffer.alloc(size));
if (typeof window !== 'undefined') {
// browser crypto implementation(s)
const target = window.crypto || window.msCrypto; // allow for IE11
if (target && target.getRandomValues) {
randomBytes = size => target.getRandomValues(Buffer.alloc(size));
}
} else if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) {
// allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global
randomBytes = size => global.crypto.getRandomValues(Buffer.alloc(size));
} else {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down
Loading