Skip to content

Commit 1fb6dc6

Browse files
authored
feat!(NODE-4712): remove unused Map polyfill (#523)
1 parent d705d75 commit 1fb6dc6

File tree

8 files changed

+167
-260
lines changed

8 files changed

+167
-260
lines changed

docs/upgrade-to-v5.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,8 @@ 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.
83+
84+
### Remove `Map` export
85+
86+
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.

src/bson.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { Double } from './double';
66
import { EJSON } from './extended_json';
77
import { Int32 } from './int_32';
88
import { Long } from './long';
9-
import { Map } from './map';
109
import { MaxKey } from './max_key';
1110
import { MinKey } from './min_key';
1211
import { ObjectId } from './objectid';
@@ -74,7 +73,6 @@ export { LongWithoutOverridesClass } from './timestamp';
7473
export type { SerializeOptions, DeserializeOptions };
7574
export {
7675
Code,
77-
Map,
7876
BSONSymbol,
7977
DBRef,
8078
Binary,

src/map.ts

Lines changed: 0 additions & 119 deletions
This file was deleted.

src/parser/serializer.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { BSONError, BSONTypeError } from '../error';
99
import { isBSONType } from '../extended_json';
1010
import type { Int32 } from '../int_32';
1111
import { Long } from '../long';
12-
import { Map } from '../map';
1312
import type { MinKey } from '../min_key';
1413
import type { ObjectId } from '../objectid';
1514
import type { BSONRegExp } from '../regexp';

test/node/exports.test.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import * as BSON from '../register-bson';
2+
import { sorted, byStrings } from './tools/utils';
3+
4+
const EXPECTED_EXPORTS = [
5+
// This is our added web indicator not a real export but a small exception for this test.
6+
'__isWeb__',
7+
8+
'BSON_BINARY_SUBTYPE_BYTE_ARRAY',
9+
'BSON_BINARY_SUBTYPE_DEFAULT',
10+
'BSON_BINARY_SUBTYPE_FUNCTION',
11+
'BSON_BINARY_SUBTYPE_MD5',
12+
'BSON_BINARY_SUBTYPE_USER_DEFINED',
13+
'BSON_BINARY_SUBTYPE_UUID',
14+
'BSON_BINARY_SUBTYPE_UUID_NEW',
15+
'BSON_BINARY_SUBTYPE_ENCRYPTED',
16+
'BSON_BINARY_SUBTYPE_COLUMN',
17+
'BSON_DATA_ARRAY',
18+
'BSON_DATA_BINARY',
19+
'BSON_DATA_BOOLEAN',
20+
'BSON_DATA_CODE',
21+
'BSON_DATA_CODE_W_SCOPE',
22+
'BSON_DATA_DATE',
23+
'BSON_DATA_DBPOINTER',
24+
'BSON_DATA_DECIMAL128',
25+
'BSON_DATA_INT',
26+
'BSON_DATA_LONG',
27+
'BSON_DATA_MAX_KEY',
28+
'BSON_DATA_MIN_KEY',
29+
'BSON_DATA_NULL',
30+
'BSON_DATA_NUMBER',
31+
'BSON_DATA_OBJECT',
32+
'BSON_DATA_OID',
33+
'BSON_DATA_REGEXP',
34+
'BSON_DATA_STRING',
35+
'BSON_DATA_SYMBOL',
36+
'BSON_DATA_TIMESTAMP',
37+
'BSON_DATA_UNDEFINED',
38+
'BSON_INT32_MAX',
39+
'BSON_INT32_MIN',
40+
'BSON_INT64_MAX',
41+
'BSON_INT64_MIN',
42+
'EJSON',
43+
'Code',
44+
'BSONSymbol',
45+
'DBRef',
46+
'Binary',
47+
'ObjectId',
48+
'UUID',
49+
'Long',
50+
'LongWithoutOverridesClass',
51+
'Timestamp',
52+
'Double',
53+
'Int32',
54+
'MinKey',
55+
'MaxKey',
56+
'BSONRegExp',
57+
'Decimal128',
58+
'ObjectID',
59+
'BSONError',
60+
'BSONTypeError',
61+
'setInternalBufferSize',
62+
'serialize',
63+
'serializeWithBufferAndIndex',
64+
'deserialize',
65+
'calculateObjectSize',
66+
'deserializeStream',
67+
'default'
68+
];
69+
70+
const EXPECTED_EJSON_EXPORTS = ['parse', 'stringify', 'serialize', 'deserialize'];
71+
72+
describe('bson entrypoint', () => {
73+
it('should export all and only the expected keys in expected_exports', () => {
74+
expect(sorted(Object.keys(BSON), byStrings)).to.deep.equal(sorted(EXPECTED_EXPORTS, byStrings));
75+
});
76+
77+
it('should export all and only the expected keys in expected_ejson_exports', () => {
78+
expect(sorted(Object.keys(BSON.EJSON), byStrings)).to.deep.equal(
79+
sorted(EXPECTED_EJSON_EXPORTS, byStrings)
80+
);
81+
});
82+
});

test/node/map.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { expect } from 'chai';
2+
import * as BSON from '../register-bson';
3+
import { bufferFromHexArray } from './tools/utils';
4+
5+
describe('ES Map support in serialize()', () => {
6+
it('serialize a empty Map to BSON document - { }', () => {
7+
const map = new Map();
8+
const emptyBSON = bufferFromHexArray(['']);
9+
const result = BSON.serialize(map);
10+
expect(result).to.have.property('byteLength', 5);
11+
expect(result, 'byteLength must be 5 followed by null terminator').to.deep.equal(emptyBSON);
12+
});
13+
14+
it('serialize a Map with one key to BSON document - { a: 2 }', () => {
15+
const map = new Map([['a', new BSON.Int32(2)]]);
16+
const expected = bufferFromHexArray([
17+
'10', // int32 type
18+
'6100', // 'a' & null
19+
'02000000' // LE 32bit 2
20+
]);
21+
const result = BSON.serialize(map);
22+
expect(result).to.have.property('byteLength', 12);
23+
expect(result).to.deep.equal(expected);
24+
});
25+
26+
it('serialize a nested Map to a BSON document - { a: { b: 2 } }', () => {
27+
// { a: { b: 2 } }
28+
const map = new Map([['a', new Map([['b', new BSON.Int32(2)]])]]);
29+
const expected = bufferFromHexArray([
30+
'03', // doc type
31+
'6100', // 'a' & null
32+
// nested document
33+
bufferFromHexArray([
34+
'10', // int32 type
35+
'6200', // 'b' & null
36+
'02000000' // LE 32bit 2
37+
]).toString('hex')
38+
]);
39+
const result = BSON.serialize(map);
40+
expect(result).to.have.property('byteLength', 20);
41+
expect(result).to.deep.equal(expected);
42+
});
43+
44+
it('keep chronological Map key order despite keys being numeric', () => {
45+
const map = new Map([
46+
['2', new BSON.Int32(2)],
47+
['1', new BSON.Int32(1)]
48+
]);
49+
50+
// meta assertions: demonstrating that keys are not reordered like objects would
51+
expect(Array.from(map.keys())).to.deep.equal(['2', '1']);
52+
expect(Object.keys({ [2]: 2, [1]: 1 })).to.deep.equal(['1', '2']);
53+
54+
const expected = bufferFromHexArray([
55+
'10', // int32 type
56+
'3200', // '2' & null
57+
'02000000', // LE 32bit 2
58+
'10', // int32 type
59+
'3100', // '1' & null
60+
'01000000' // LE 32bit 1
61+
]);
62+
const result = BSON.serialize(map);
63+
expect(result).to.have.property('byteLength', 19);
64+
expect(result).to.deep.equal(expected);
65+
});
66+
});

0 commit comments

Comments
 (0)