Skip to content

Commit 7a50a42

Browse files
committed
fixups
1 parent 1af9177 commit 7a50a42

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

docs/upgrade-to-v5.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ We have set our typescript compilation target to `es2020` which aligns with our
7979
> **TL;DR**: TODO
8080
8181
TODO(NODE-4771): serializeFunctions bug fix makes function names outside the ascii range get serialized correctly
82-
> This will preserve newer ECMAScript 2020 features like optional chaining, nullish coalescing, export * as ns, and dynamic import(...) syntax. It also means bigint literals now have a stable target below esnext.
8382

8483
### Remove `Map` export
8584

8685
This library no longer polyfills [ES Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) and the export "Map" was removed. Users should migrate to using the global Map constructor available in all supported JS environments.
86+
87+
### Remove default export and internal constants
88+
89+
The BSON default export has been removed, it was out of sync with the commonjs and aggregate (`* as`) imports and we now have a way of keeping it consistent. There is now a `BSON` namespace export. We removed the internal constants, but provided the BSONType enum from the driver as a migration for these constants. ... etc TODO...

src/bson.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export {
5454
};
5555
export { BSONError, BSONTypeError } from './error';
5656

57+
export { BSONType } from './constants';
58+
5759
/** @public */
5860
export interface Document {
5961
// eslint-disable-next-line @typescript-eslint/no-explicit-any

src/constants.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,31 @@ export const BSON_BINARY_SUBTYPE_COLUMN = 7;
108108

109109
/** Binary User Defined Type @internal */
110110
export const BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
111+
112+
/** @public */
113+
export const BSONType = Object.freeze({
114+
double: 1,
115+
string: 2,
116+
object: 3,
117+
array: 4,
118+
binData: 5,
119+
undefined: 6,
120+
objectId: 7,
121+
bool: 8,
122+
date: 9,
123+
null: 10,
124+
regex: 11,
125+
dbPointer: 12,
126+
javascript: 13,
127+
symbol: 14,
128+
javascriptWithScope: 15,
129+
int: 16,
130+
timestamp: 17,
131+
long: 18,
132+
decimal: 19,
133+
minKey: -1,
134+
maxKey: 127
135+
} as const);
136+
137+
/** @public */
138+
export type BSONType = typeof BSONType[keyof typeof BSONType];

test/node/check_constants.js renamed to test/node/constants.test.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict';
2-
3-
const constants = require('../../src/constants');
4-
const BSON = require('../register-bson');
1+
import * as constants from '../../src/constants';
2+
import * as BSON from '../register-bson';
53

64
describe('BSON Constants', () => {
75
context('Binary Subtype', () => {
@@ -140,4 +138,42 @@ describe('BSON Constants', () => {
140138
expect(constants.BSON_DATA_MAX_KEY).to.equal(0x7f);
141139
});
142140
});
141+
142+
describe('BSONType enum', () => {
143+
it('double equals 1', () => expect(constants.BSONType.double).to.equal(1));
144+
it('string equals 2', () => expect(constants.BSONType.string).to.equal(2));
145+
it('object equals 3', () => expect(constants.BSONType.object).to.equal(3));
146+
it('array equals 4', () => expect(constants.BSONType.array).to.equal(4));
147+
it('binData equals 5', () => expect(constants.BSONType.binData).to.equal(5));
148+
it('undefined equals 6', () => expect(constants.BSONType.undefined).to.equal(6));
149+
it('objectId equals 7', () => expect(constants.BSONType.objectId).to.equal(7));
150+
it('bool equals 8', () => expect(constants.BSONType.bool).to.equal(8));
151+
it('date equals 9', () => expect(constants.BSONType.date).to.equal(9));
152+
it('null equals 10', () => expect(constants.BSONType.null).to.equal(10));
153+
it('regex equals 11', () => expect(constants.BSONType.regex).to.equal(11));
154+
it('dbPointer equals 12', () => expect(constants.BSONType.dbPointer).to.equal(12));
155+
it('javascript equals 13', () => expect(constants.BSONType.javascript).to.equal(13));
156+
it('symbol equals 14', () => expect(constants.BSONType.symbol).to.equal(14));
157+
it('javascriptWithScope equals 15', () =>
158+
expect(constants.BSONType.javascriptWithScope).to.equal(15));
159+
it('int equals 16', () => expect(constants.BSONType.int).to.equal(16));
160+
it('timestamp equals 17', () => expect(constants.BSONType.timestamp).to.equal(17));
161+
it('long equals 18', () => expect(constants.BSONType.long).to.equal(18));
162+
it('decimal equals 19', () => expect(constants.BSONType.decimal).to.equal(19));
163+
it('minKey equals -1', () => expect(constants.BSONType.minKey).to.equal(-1));
164+
it('maxKey equals 27', () => expect(constants.BSONType.maxKey).to.equal(127));
165+
166+
it('minKey equals 255 when used in Uint8Array', () => {
167+
const byte = new Uint8Array(1);
168+
byte[0] = constants.BSONType.minKey;
169+
expect(byte[0]).to.equal(255);
170+
});
171+
172+
it('minKey equals 255 when used in DataView in unsigned way', () => {
173+
const dv = new DataView(new ArrayBuffer(1));
174+
dv.setUint8(0, constants.BSONType.minKey);
175+
expect(dv.getUint8(0)).to.equal(255);
176+
expect(new Uint8Array(dv.buffer, 0, 1)[0]).to.equal(255);
177+
});
178+
});
143179
});

test/node/exports.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const EXPECTED_EXPORTS = [
2222
'Decimal128',
2323
'ObjectID',
2424
'BSONError',
25+
'BSONType',
2526
'BSONTypeError',
2627
'setInternalBufferSize',
2728
'serialize',

0 commit comments

Comments
 (0)