Skip to content

Commit

Permalink
feat(bson): export new wrapObjectId/wrapUUId for faster any serializa…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
marcj committed Feb 13, 2024
1 parent 690927c commit 718839b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
24 changes: 23 additions & 1 deletion packages/bson/src/bson-serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
isUUIDType,
JitStack,
memberNameToString,
MongoId,
mongoIdAnnotation,
NamingStrategy,
ReceiveType,
Expand All @@ -57,10 +58,12 @@ import {
TypeIndexSignature,
TypeLiteral,
TypeObjectLiteral,
typeOf,
typeSettings,
TypeTuple,
UnpopulatedCheck,
unpopulatedSymbol,
UUID,
uuidAnnotation,
} from '@deepkit/type';
import {
Expand Down Expand Up @@ -90,7 +93,14 @@ import {
} from './bson-deserializer-templates.js';
import { seekElementSize } from './continuation.js';
import { BSONError } from './model.js';
import { BSON_BINARY_SUBTYPE_DEFAULT, BSON_BINARY_SUBTYPE_UUID, BSONType, digitByteSize, isSerializable, TWO_PWR_32_DBL_N } from './utils.js';
import {
BSON_BINARY_SUBTYPE_DEFAULT,
BSON_BINARY_SUBTYPE_UUID,
BSONType,
digitByteSize,
isSerializable,
TWO_PWR_32_DBL_N,
} from './utils.js';

export function createBuffer(size: number): Uint8Array {
return 'undefined' !== typeof Buffer && 'function' === typeof Buffer.allocUnsafe ? Buffer.allocUnsafe(size) : new Uint8Array(size);
Expand Down Expand Up @@ -244,6 +254,18 @@ export function wrapValue<T>(value: any, type?: ReceiveType<T>) {
return new ValueWithBSONSerializer(value, resolveReceiveType(type));
}

const mongoIdType = typeOf<MongoId>();

export function wrapObjectId(value: string) {
return new ValueWithBSONSerializer(value, mongoIdType);
}

const uuidIdType = typeOf<UUID>();

export function wrapUUID(value: string) {
return new ValueWithBSONSerializer(value, uuidIdType);
}

export class Writer {
public dataView: DataView;
public typeOffset: number = 0;
Expand Down
53 changes: 32 additions & 21 deletions packages/bson/tests/bson-serialize.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
import { expect, test } from '@jest/globals';
import { getBSONSerializer, getBSONSizer, getValueSize, hexToByte, serializeBSONWithoutOptimiser, uuidStringToByte, wrapValue } from '../src/bson-serializer.js';
import {
BinaryBigInt,
createReference,
Excluded,
hasCircularReference,
MongoId,
nodeBufferToArrayBuffer,
PrimaryKey,
Reference,
SignedBinaryBigInt,
typeOf,
uuid,
UUID,
} from '@deepkit/type';
import { getBSONSerializer, getBSONSizer, getValueSize, hexToByte, serializeBSONWithoutOptimiser, uuidStringToByte, wrapObjectId, wrapUUID, wrapValue } from '../src/bson-serializer.js';
import { BinaryBigInt, createReference, Excluded, hasCircularReference, MongoId, nodeBufferToArrayBuffer, PrimaryKey, Reference, SignedBinaryBigInt, typeOf, uuid, UUID } from '@deepkit/type';
import bson from 'bson';
import { randomBytes } from 'crypto';
import { BSON_BINARY_SUBTYPE_DEFAULT, BSONType } from '../src/utils.js';
import { deserializeBSONWithoutOptimiser } from '../src/bson-parser.js';
import { deserializeBSON, getBSONDeserializer } from '../src/bson-deserializer.js';

const { Binary, calculateObjectSize, deserialize, Long, ObjectId: OfficialObjectId, serialize } = bson;
const { Binary, calculateObjectSize, deserialize, Long, ObjectId: OfficialObjectId, UUID: OfficialUUID, serialize } = bson;

test('hexToByte', () => {
expect(hexToByte('00')).toBe(0);
Expand Down Expand Up @@ -1430,9 +1417,33 @@ test('undefined for required object', () => {
});

test('wrapValue', () => {
const objectId = wrapValue<MongoId>('507f191e810c19729de860ea');
const bson = serializeBSONWithoutOptimiser({v: objectId});
const back = deserialize(bson);
expect(back.v).toBeInstanceOf(OfficialObjectId);
expect(back.v.toHexString()).toBe('507f191e810c19729de860ea');
{
const objectId = wrapValue<MongoId>('507f191e810c19729de860ea');
const bson = serializeBSONWithoutOptimiser({ v: objectId });
const back = deserialize(bson);
expect(back.v).toBeInstanceOf(OfficialObjectId);
expect(back.v.toHexString()).toBe('507f191e810c19729de860ea');
}
{
const objectId = wrapValue<MongoId>('507f191e810c19729de860ea');
const serialize = getBSONSerializer<{v: any }>();
const bson = serialize({ v: objectId });
const back = deserialize(bson);
expect(back.v).toBeInstanceOf(OfficialObjectId);
expect(back.v.toHexString()).toBe('507f191e810c19729de860ea');
}
{
const objectId = wrapObjectId('507f191e810c19729de860ea');
const bson = serializeBSONWithoutOptimiser({ v: objectId });
const back = deserialize(bson);
expect(back.v).toBeInstanceOf(OfficialObjectId);
expect(back.v.toHexString()).toBe('507f191e810c19729de860ea');
}
{
const uuid1 = wrapUUID(uuid());
const bson = serializeBSONWithoutOptimiser({ v: uuid1 });
const back = deserialize(bson);
expect(back.v).toBeInstanceOf(OfficialUUID);
expect(back.v.toHexString()).toBe(uuid1.value);
}
});

0 comments on commit 718839b

Please sign in to comment.