Skip to content

Commit 384d3c7

Browse files
committed
Improvements
1 parent 5d01f60 commit 384d3c7

File tree

6 files changed

+47
-59
lines changed

6 files changed

+47
-59
lines changed

src/objectid.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ export class ObjectId extends BSONValue {
6969
}
7070

7171
static set poolSize(size: number) {
72-
const iSize = Math.ceil(size); // Ensure new pool size is an integer
73-
if (iSize <= 0) {
74-
throw new BSONError('poolSize must be a positive integer greater than 0');
75-
}
76-
poolSize = iSize;
72+
poolSize = Math.max(Math.abs(Number(size)) >>> 0, 1);
7773
}
7874

7975
/** ObjectId buffer pool pointer @internal */
@@ -241,6 +237,21 @@ export class ObjectId extends BSONValue {
241237
return (ObjectId.index = (ObjectId.index + 1) % 0xffffff);
242238
}
243239

240+
/**
241+
* Generate a 12 byte id buffer used in ObjectId's
242+
*
243+
* @param time - pass in a second based timestamp.
244+
*/
245+
static generate(time?: number): Uint8Array;
246+
/**
247+
* Generate a 12 byte id buffer used in ObjectId's and write to the provided buffer at offset.
248+
* @internal
249+
*
250+
* @param time - pass in a second based timestamp.
251+
* @param buffer - Optionally pass in a buffer instance.
252+
* @param offset - Optionally pass in a buffer offset.
253+
*/
254+
static generate(time?: number, buffer?: Uint8Array, offset?: number): Uint8Array;
244255
/**
245256
* Generate a 12 byte id buffer used in ObjectId's
246257
*

test/node/bson_test.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ const MaxKey = BSON.MaxKey;
2020
const BSONError = BSON.BSONError;
2121
const { BinaryParser } = require('./tools/binary_parser');
2222
const vm = require('vm');
23-
const {
24-
assertBuffersEqual,
25-
isBufferOrUint8Array,
26-
assertDeepEqualsWithObjectId
27-
} = require('./tools/utils');
23+
const { assertBuffersEqual, isBufferOrUint8Array } = require('./tools/utils');
2824
const { inspect } = require('util');
2925

3026
/**
@@ -711,7 +707,7 @@ describe('BSON', function () {
711707
expect(serialized_data).to.deep.equal(serialized_data2);
712708

713709
var doc2 = b.deserialize(serialized_data);
714-
assertDeepEqualsWithObjectId(doc, doc2);
710+
expect(b.serialize(doc)).to.deep.equal(b.serialize(doc2));
715711
expect(doc2.dbref.oid.toHexString()).to.deep.equal(oid.toHexString());
716712
done();
717713
});
@@ -1005,7 +1001,7 @@ describe('BSON', function () {
10051001

10061002
var deserialized_data = BSON.deserialize(serialized_data);
10071003
expect(doc.b).to.deep.equal(deserialized_data.b);
1008-
assertDeepEqualsWithObjectId(doc, deserialized_data);
1004+
expect(BSON.serialize(doc)).to.deep.equal(BSON.serialize(deserialized_data));
10091005
done();
10101006
});
10111007

@@ -1217,7 +1213,7 @@ describe('BSON', function () {
12171213

12181214
var doc2 = BSON.deserialize(serialized_data);
12191215

1220-
assertDeepEqualsWithObjectId(doc, doc2);
1216+
expect(BSON.serialize(doc)).to.deep.equal(BSON.serialize(doc2));
12211217
done();
12221218
});
12231219

test/node/bson_type_classes.test.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import {
1616
ObjectId,
1717
Timestamp,
1818
UUID,
19-
BSONValue
19+
BSONValue,
20+
BSON
2021
} from '../register-bson';
21-
import { assertDeepEqualsWithObjectId } from './tools/utils';
2222
import * as vm from 'node:vm';
2323

2424
const BSONTypeClasses = [
@@ -130,12 +130,9 @@ describe('BSON Type classes common interfaces', () => {
130130
}
131131
vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx);
132132

133-
if (ctx.ObjectId) {
134-
// Since ObjectId uses a pool we expect offset to be different
135-
assertDeepEqualsWithObjectId(ctx.module.exports.result, bsonValue);
136-
} else {
137-
expect(ctx.module.exports.result).to.deep.equal(bsonValue);
138-
}
133+
expect(BSON.serialize({ result: ctx.module.exports.result })).to.deep.equal(
134+
BSON.serialize({ result: bsonValue })
135+
);
139136
});
140137
}
141138
});

test/node/extended_json.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as vm from 'node:vm';
44
import { expect } from 'chai';
55
import { BSONVersionError, BSONRuntimeError } from '../../src';
66
import { BSONError } from '../register-bson';
7-
import { assertDeepEqualsWithObjectId } from './tools/utils';
87

98
// BSON types
109
const Binary = BSON.Binary;
@@ -145,10 +144,11 @@ describe('Extended JSON', function () {
145144
const input = '{"result":[{"_id":{"$oid":"591801a468f9e7024b623939"},"emptyField":null}]}';
146145
const parsed = EJSON.parse(input);
147146

148-
const expected = {
149-
result: [{ _id: new ObjectId('591801a468f9e7024b623939'), emptyField: null }]
150-
};
151-
assertDeepEqualsWithObjectId(parsed, expected);
147+
expect(EJSON.serialize(parsed)).to.deep.equal(
148+
EJSON.serialize({
149+
result: [{ _id: new ObjectId('591801a468f9e7024b623939'), emptyField: null }]
150+
})
151+
);
152152
});
153153

154154
it('should correctly throw when passed a non-string to parse', function () {

test/node/object_id.test.ts

+17-11
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ import { expect } from 'chai';
55
import { bufferFromHexArray } from './tools/utils';
66
import { isBufferOrUint8Array } from './tools/utils';
77

8-
declare module '../register-bson' {
9-
interface ObjectId {
10-
pool: Uint8Array;
11-
offset: number;
12-
}
13-
}
14-
158
describe('ObjectId', function () {
169
describe('static createFromTime()', () => {
1710
it('creates an objectId with user defined value in the timestamp field', function () {
@@ -325,11 +318,24 @@ describe('ObjectId', function () {
325318
ObjectId.poolSize = oldPoolSize;
326319
});
327320

328-
it('should not allow 0 poolSize', function () {
321+
it('should default to poolSize = 1 when invalid poolSize set', function () {
329322
const oldPoolSize = ObjectId.poolSize;
330-
expect(() => {
331-
ObjectId.poolSize = 0;
332-
}).to.throw(BSONError);
323+
324+
ObjectId.poolSize = 0;
325+
expect(ObjectId.poolSize).to.equal(1);
326+
ObjectId.poolSize = -1;
327+
expect(ObjectId.poolSize).to.equal(1);
328+
ObjectId.poolSize = 0n;
329+
expect(ObjectId.poolSize).to.equal(1);
330+
ObjectId.poolSize = '';
331+
expect(ObjectId.poolSize).to.equal(1);
332+
ObjectId.poolSize = NaN;
333+
expect(ObjectId.poolSize).to.equal(1);
334+
ObjectId.poolSize = {};
335+
expect(ObjectId.poolSize).to.equal(1);
336+
ObjectId.poolSize = false;
337+
expect(ObjectId.poolSize).to.equal(1);
338+
ObjectId.poolSize = '1';
333339

334340
ObjectId.poolSize = oldPoolSize;
335341
});

test/node/tools/utils.js

-22
Original file line numberDiff line numberDiff line change
@@ -218,25 +218,3 @@ module.exports.sorted = (iterable, how) => {
218218
items.sort(how);
219219
return items;
220220
};
221-
222-
/** Deeply converts all ObjectIds into their hex value representation */
223-
const deepOidValue = obj => {
224-
if (obj?._bsontype === 'ObjectId') {
225-
return obj.toHexString();
226-
}
227-
if (Array.isArray(obj)) {
228-
return obj.map(item => deepOidValue(item));
229-
} else if (obj !== null && typeof obj === 'object' && !Buffer.isBuffer(obj)) {
230-
return Object.entries(obj).reduce((acc, [key, value]) => {
231-
acc[key] = deepOidValue(value);
232-
return acc;
233-
}, {});
234-
}
235-
return obj;
236-
};
237-
238-
module.exports.assertDeepEqualsWithObjectId = (actual, expected) => {
239-
const a = deepOidValue(actual);
240-
const b = deepOidValue(expected);
241-
expect(a).to.deep.equal(b);
242-
};

0 commit comments

Comments
 (0)