Skip to content

Commit

Permalink
feat!(NODE-4712): remove unused Map polyfill (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Nov 28, 2022
1 parent d705d75 commit 1fb6dc6
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 260 deletions.
5 changes: 5 additions & 0 deletions docs/upgrade-to-v5.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,8 @@ We have set our typescript compilation target to `es2020` which aligns with our
> **TL;DR**: TODO
TODO(NODE-4771): serializeFunctions bug fix makes function names outside the ascii range get serialized correctly
> 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.
### Remove `Map` export

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.
2 changes: 0 additions & 2 deletions src/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Double } from './double';
import { EJSON } from './extended_json';
import { Int32 } from './int_32';
import { Long } from './long';
import { Map } from './map';
import { MaxKey } from './max_key';
import { MinKey } from './min_key';
import { ObjectId } from './objectid';
Expand Down Expand Up @@ -74,7 +73,6 @@ export { LongWithoutOverridesClass } from './timestamp';
export type { SerializeOptions, DeserializeOptions };
export {
Code,
Map,
BSONSymbol,
DBRef,
Binary,
Expand Down
119 changes: 0 additions & 119 deletions src/map.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/parser/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { BSONError, BSONTypeError } from '../error';
import { isBSONType } from '../extended_json';
import type { Int32 } from '../int_32';
import { Long } from '../long';
import { Map } from '../map';
import type { MinKey } from '../min_key';
import type { ObjectId } from '../objectid';
import type { BSONRegExp } from '../regexp';
Expand Down
82 changes: 82 additions & 0 deletions test/node/exports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as BSON from '../register-bson';
import { sorted, byStrings } from './tools/utils';

const EXPECTED_EXPORTS = [
// This is our added web indicator not a real export but a small exception for this test.
'__isWeb__',

'BSON_BINARY_SUBTYPE_BYTE_ARRAY',
'BSON_BINARY_SUBTYPE_DEFAULT',
'BSON_BINARY_SUBTYPE_FUNCTION',
'BSON_BINARY_SUBTYPE_MD5',
'BSON_BINARY_SUBTYPE_USER_DEFINED',
'BSON_BINARY_SUBTYPE_UUID',
'BSON_BINARY_SUBTYPE_UUID_NEW',
'BSON_BINARY_SUBTYPE_ENCRYPTED',
'BSON_BINARY_SUBTYPE_COLUMN',
'BSON_DATA_ARRAY',
'BSON_DATA_BINARY',
'BSON_DATA_BOOLEAN',
'BSON_DATA_CODE',
'BSON_DATA_CODE_W_SCOPE',
'BSON_DATA_DATE',
'BSON_DATA_DBPOINTER',
'BSON_DATA_DECIMAL128',
'BSON_DATA_INT',
'BSON_DATA_LONG',
'BSON_DATA_MAX_KEY',
'BSON_DATA_MIN_KEY',
'BSON_DATA_NULL',
'BSON_DATA_NUMBER',
'BSON_DATA_OBJECT',
'BSON_DATA_OID',
'BSON_DATA_REGEXP',
'BSON_DATA_STRING',
'BSON_DATA_SYMBOL',
'BSON_DATA_TIMESTAMP',
'BSON_DATA_UNDEFINED',
'BSON_INT32_MAX',
'BSON_INT32_MIN',
'BSON_INT64_MAX',
'BSON_INT64_MIN',
'EJSON',
'Code',
'BSONSymbol',
'DBRef',
'Binary',
'ObjectId',
'UUID',
'Long',
'LongWithoutOverridesClass',
'Timestamp',
'Double',
'Int32',
'MinKey',
'MaxKey',
'BSONRegExp',
'Decimal128',
'ObjectID',
'BSONError',
'BSONTypeError',
'setInternalBufferSize',
'serialize',
'serializeWithBufferAndIndex',
'deserialize',
'calculateObjectSize',
'deserializeStream',
'default'
];

const EXPECTED_EJSON_EXPORTS = ['parse', 'stringify', 'serialize', 'deserialize'];

describe('bson entrypoint', () => {
it('should export all and only the expected keys in expected_exports', () => {
expect(sorted(Object.keys(BSON), byStrings)).to.deep.equal(sorted(EXPECTED_EXPORTS, byStrings));
});

it('should export all and only the expected keys in expected_ejson_exports', () => {
expect(sorted(Object.keys(BSON.EJSON), byStrings)).to.deep.equal(
sorted(EXPECTED_EJSON_EXPORTS, byStrings)
);
});
});
66 changes: 66 additions & 0 deletions test/node/map.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { expect } from 'chai';
import * as BSON from '../register-bson';
import { bufferFromHexArray } from './tools/utils';

describe('ES Map support in serialize()', () => {
it('serialize a empty Map to BSON document - { }', () => {
const map = new Map();
const emptyBSON = bufferFromHexArray(['']);
const result = BSON.serialize(map);
expect(result).to.have.property('byteLength', 5);
expect(result, 'byteLength must be 5 followed by null terminator').to.deep.equal(emptyBSON);
});

it('serialize a Map with one key to BSON document - { a: 2 }', () => {
const map = new Map([['a', new BSON.Int32(2)]]);
const expected = bufferFromHexArray([
'10', // int32 type
'6100', // 'a' & null
'02000000' // LE 32bit 2
]);
const result = BSON.serialize(map);
expect(result).to.have.property('byteLength', 12);
expect(result).to.deep.equal(expected);
});

it('serialize a nested Map to a BSON document - { a: { b: 2 } }', () => {
// { a: { b: 2 } }
const map = new Map([['a', new Map([['b', new BSON.Int32(2)]])]]);
const expected = bufferFromHexArray([
'03', // doc type
'6100', // 'a' & null
// nested document
bufferFromHexArray([
'10', // int32 type
'6200', // 'b' & null
'02000000' // LE 32bit 2
]).toString('hex')
]);
const result = BSON.serialize(map);
expect(result).to.have.property('byteLength', 20);
expect(result).to.deep.equal(expected);
});

it('keep chronological Map key order despite keys being numeric', () => {
const map = new Map([
['2', new BSON.Int32(2)],
['1', new BSON.Int32(1)]
]);

// meta assertions: demonstrating that keys are not reordered like objects would
expect(Array.from(map.keys())).to.deep.equal(['2', '1']);
expect(Object.keys({ [2]: 2, [1]: 1 })).to.deep.equal(['1', '2']);

const expected = bufferFromHexArray([
'10', // int32 type
'3200', // '2' & null
'02000000', // LE 32bit 2
'10', // int32 type
'3100', // '1' & null
'01000000' // LE 32bit 1
]);
const result = BSON.serialize(map);
expect(result).to.have.property('byteLength', 19);
expect(result).to.deep.equal(expected);
});
});
Loading

0 comments on commit 1fb6dc6

Please sign in to comment.