Skip to content

Commit 8eb0081

Browse files
authored
fix(NODE-2944): Reintroduce bson-ext support (#2823)
- Update benchmark imports - Add bson-ext test to CI
1 parent eae91b5 commit 8eb0081

File tree

11 files changed

+267
-39
lines changed

11 files changed

+267
-39
lines changed

.evergreen/config.yml

+29
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,16 @@ functions:
516516
rm -f ./prepare_client_encryption.sh
517517
518518
MONGODB_URI="${MONGODB_URI}" bash ${PROJECT_DIRECTORY}/.evergreen/run-custom-csfle-tests.sh
519+
run bson-ext test:
520+
- command: shell.exec
521+
type: test
522+
params:
523+
working_dir: src
524+
timeout_secs: 60
525+
script: |
526+
${PREPARE_SHELL}
527+
528+
MONGODB_URI="${MONGODB_URI}" bash ${PROJECT_DIRECTORY}/.evergreen/run-bson-ext-test.sh
519529
upload test results:
520530
- command: attach.xunit_results
521531
params:
@@ -1353,6 +1363,20 @@ tasks:
13531363
VERSION: '4.4'
13541364
TOPOLOGY: server
13551365
- func: run custom csfle tests
1366+
- name: run-bson-ext-test
1367+
tags:
1368+
- run-bson-ext-test
1369+
commands:
1370+
- func: install dependencies
1371+
vars:
1372+
NODE_LTS_NAME: fermium
1373+
- func: bootstrap mongo-orchestration
1374+
vars:
1375+
VERSION: '4.4'
1376+
TOPOLOGY: server
1377+
- func: run bson-ext test
1378+
vars:
1379+
NODE_LTS_NAME: fermium
13561380
buildvariants:
13571381
- name: macos-1014-dubnium
13581382
display_name: macOS 10.14 Node Dubnium
@@ -1685,6 +1709,11 @@ buildvariants:
16851709
run_on: ubuntu1804-test
16861710
tasks:
16871711
- run-custom-csfle-tests
1712+
- name: ubuntu1804-run-bson-ext-test
1713+
display_name: BSON EXT Test
1714+
run_on: ubuntu1804-test
1715+
tasks:
1716+
- run-bson-ext-test
16881717
- name: mongosh_integration_tests
16891718
display_name: mongosh integration tests
16901719
run_on: ubuntu1804-test

.evergreen/config.yml.in

+11
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,17 @@ functions:
561561

562562
MONGODB_URI="${MONGODB_URI}" bash ${PROJECT_DIRECTORY}/.evergreen/run-custom-csfle-tests.sh
563563

564+
"run bson-ext test":
565+
- command: shell.exec
566+
type: test
567+
params:
568+
working_dir: "src"
569+
timeout_secs: 60
570+
script: |
571+
${PREPARE_SHELL}
572+
573+
MONGODB_URI="${MONGODB_URI}" bash ${PROJECT_DIRECTORY}/.evergreen/run-bson-ext-test.sh
574+
564575
"upload test results":
565576
# Upload the xunit-format test results.
566577
- command: attach.xunit_results

.evergreen/generate_evergreen_tasks.js

+31
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,11 @@ BUILD_VARIANTS.push({
533533
display_name: 'Custom FLE Version Test',
534534
run_on: 'ubuntu1804-test',
535535
tasks: ['run-custom-csfle-tests']
536+
},{
537+
name: 'ubuntu1804-run-bson-ext-test',
538+
display_name: 'BSON EXT Test',
539+
run_on: 'ubuntu1804-test',
540+
tasks: ['run-bson-ext-test']
536541
});
537542

538543
// singleton build variant for mongosh integration tests
@@ -591,6 +596,32 @@ SINGLETON_TASKS.push({
591596
]
592597
});
593598

599+
// special case for custom BSON-ext test
600+
SINGLETON_TASKS.push({
601+
name: 'run-bson-ext-test',
602+
tags: ['run-bson-ext-test'],
603+
commands: [
604+
{
605+
func: 'install dependencies',
606+
vars: {
607+
NODE_LTS_NAME: 'fermium',
608+
},
609+
},
610+
{
611+
func: 'bootstrap mongo-orchestration',
612+
vars: {
613+
VERSION: '4.4',
614+
TOPOLOGY: 'server'
615+
}
616+
},
617+
{ func: 'run bson-ext test',
618+
vars: {
619+
NODE_LTS_NAME: 'fermium',
620+
}
621+
}
622+
]
623+
});
624+
594625
const fileData = yaml.safeLoad(fs.readFileSync(`${__dirname}/config.yml.in`, 'utf8'));
595626
fileData.tasks = (fileData.tasks || []).concat(BASE_TASKS).concat(TASKS).concat(SINGLETON_TASKS);
596627
fileData.buildvariants = (fileData.buildvariants || []).concat(BUILD_VARIANTS);

.evergreen/run-bson-ext-test.sh

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
[ -s "$PROJECT_DIRECTORY/node-artifacts/nvm/nvm.sh" ] && source "$PROJECT_DIRECTORY"/node-artifacts/nvm/nvm.sh
4+
5+
set -o xtrace # Write all commands first to stderr
6+
set -o errexit # Exit the script with error if any of the commands fail
7+
8+
# Supported/used environment variables:
9+
# SSL Set to enable SSL. Defaults to "nossl"
10+
# MONGODB_URI Set the suggested connection MONGODB_URI (including credentials and topology info)
11+
# TEST_NPM_SCRIPT Script to npm run. Defaults to "check:test"
12+
13+
MONGODB_URI=${MONGODB_URI:-}
14+
TEST_NPM_SCRIPT=${TEST_NPM_SCRIPT:-check:test}
15+
16+
# ssl setup
17+
SSL=${SSL:-nossl}
18+
if [ "$SSL" != "nossl" ]; then
19+
export SSL_KEY_FILE="$DRIVERS_TOOLS/.evergreen/x509gen/client.pem"
20+
export SSL_CA_FILE="$DRIVERS_TOOLS/.evergreen/x509gen/ca.pem"
21+
fi
22+
23+
# run tests
24+
echo "Running $AUTH tests over $SSL, connecting to $MONGODB_URI"
25+
26+
npm install bson-ext
27+
28+
export MONGODB_API_VERSION=${MONGODB_API_VERSION}
29+
export MONGODB_URI=${MONGODB_URI}
30+
31+
npm run "${TEST_NPM_SCRIPT}"

src/bson.ts

+31-31
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
// import type * as _BSON from 'bson';
2-
// let BSON: typeof _BSON = require('bson');
3-
// try {
4-
// BSON = require('bson-ext');
5-
// } catch {} // eslint-disable-line
1+
import type {
2+
serialize as serializeFn,
3+
deserialize as deserializeFn,
4+
calculateObjectSize as calculateObjectSizeFn
5+
} from 'bson';
6+
7+
// eslint-disable-next-line @typescript-eslint/no-var-requires
8+
let BSON = require('bson');
9+
try {
10+
BSON = require('bson-ext');
11+
} catch {} // eslint-disable-line
612

7-
// export = BSON;
13+
/** @internal */
14+
export const deserialize = BSON.deserialize as typeof deserializeFn;
15+
/** @internal */
16+
export const serialize = BSON.serialize as typeof serializeFn;
17+
/** @internal */
18+
export const calculateObjectSize = BSON.calculateObjectSize as typeof calculateObjectSizeFn;
819

920
export {
1021
Long,
@@ -21,38 +32,27 @@ export {
2132
BSONRegExp,
2233
BSONSymbol,
2334
Map,
24-
deserialize,
25-
serialize,
26-
calculateObjectSize
35+
Document
2736
} from 'bson';
2837

29-
/** @public */
30-
export interface Document {
31-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
32-
[key: string]: any;
33-
}
38+
import type { DeserializeOptions, SerializeOptions } from 'bson';
3439

35-
import type { SerializeOptions } from 'bson';
36-
37-
// TODO: Remove me when types from BSON are updated
3840
/**
3941
* BSON Serialization options.
4042
* @public
4143
*/
42-
export interface BSONSerializeOptions extends Omit<SerializeOptions, 'index'> {
43-
/** Return document results as raw BSON buffers */
44-
fieldsAsRaw?: { [key: string]: boolean };
45-
/** Promotes BSON values to native types where possible, set to false to only receive wrapper types */
46-
promoteValues?: boolean;
47-
/** Promotes Binary BSON values to native Node Buffers */
48-
promoteBuffers?: boolean;
49-
/** Promotes long values to number if they fit inside the 53 bits resolution */
50-
promoteLongs?: boolean;
51-
/** Serialize functions on any object */
52-
serializeFunctions?: boolean;
53-
/** Specify if the BSON serializer should ignore undefined fields */
54-
ignoreUndefined?: boolean;
55-
44+
export interface BSONSerializeOptions
45+
extends Omit<SerializeOptions, 'index'>,
46+
Omit<
47+
DeserializeOptions,
48+
| 'evalFunctions'
49+
| 'cacheFunctions'
50+
| 'cacheFunctionsCrc32'
51+
| 'bsonRegExp'
52+
| 'allowObjectSmallerThanBufferSize'
53+
| 'index'
54+
> {
55+
/** Return BSON filled buffers from operations */
5656
raw?: boolean;
5757
}
5858

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,4 @@ export type {
358358
MetaProjectionOperators,
359359
MetaSortOperators
360360
} from './mongo_types';
361+
export type { serialize, deserialize } from './bson';

test/benchmarks/driverBench/common.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
/* eslint-disable no-restricted-modules */
12
'use strict';
23

34
const fs = require('fs');
45
const path = require('path');
5-
const { MongoClient } = require('../../../src/mongo_client');
6-
const { GridFsBucket } = require('../../../src/gridfs-stream');
6+
const { MongoClient } = require('../../..');
7+
const { GridFSBucket } = require('../../..');
78

89
const DB_NAME = 'perftest';
910
const COLLECTION_NAME = 'corpus';
@@ -54,7 +55,7 @@ function dropCollection() {
5455
}
5556

5657
function initBucket() {
57-
this.bucket = new GridFsBucket(this.db);
58+
this.bucket = new GridFSBucket(this.db);
5859
}
5960

6061
function dropBucket() {

test/benchmarks/driverBench/index.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ const MongoBench = require('../mongoBench');
55
const Runner = MongoBench.Runner;
66
const commonHelpers = require('./common');
77

8-
const BSON = require('bson');
8+
let BSON = require('bson');
9+
try {
10+
BSON = require('bson-ext');
11+
} catch (_) {
12+
// do not care
13+
}
14+
915
const { EJSON } = require('bson');
1016

1117
const makeClient = commonHelpers.makeClient;
@@ -359,5 +365,8 @@ benchmarkRunner
359365
driverBench
360366
};
361367
})
362-
.then(data => console.log(data))
368+
.then(data => {
369+
data.bsonType = BSON.serialize.toString().includes('native code') ? 'bson-ext' : 'js-bson';
370+
console.log(data);
371+
})
363372
.catch(err => console.error(err));

test/functional/insert.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1577,7 +1577,7 @@ describe('Insert', function () {
15771577
var collection = db.collection('bson_types_insert_1');
15781578

15791579
var document = {
1580-
symbol: new BSONSymbol('abcdefghijkl'),
1580+
string: 'abcdefghijkl',
15811581
objid: new ObjectId('abcdefghijkl'),
15821582
double: new Double(1),
15831583
binary: new Binary(Buffer.from('hello world')),
@@ -1590,9 +1590,9 @@ describe('Insert', function () {
15901590
expect(err).to.not.exist;
15911591
test.ok(result);
15921592

1593-
collection.findOne({ symbol: new BSONSymbol('abcdefghijkl') }, function (err, doc) {
1593+
collection.findOne({ string: 'abcdefghijkl' }, function (err, doc) {
15941594
expect(err).to.not.exist;
1595-
test.equal('abcdefghijkl', doc.symbol.toString());
1595+
test.equal('abcdefghijkl', doc.string.toString());
15961596

15971597
collection.findOne({ objid: new ObjectId('abcdefghijkl') }, function (err, doc) {
15981598
expect(err).to.not.exist;

test/types/bson.test-d.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { expectType } from 'tsd';
2+
import type { BSONSerializeOptions, Document } from '../../src/bson';
3+
4+
const options: BSONSerializeOptions = {};
5+
6+
expectType<boolean | undefined>(options.checkKeys);
7+
expectType<boolean | undefined>(options.serializeFunctions);
8+
expectType<boolean | undefined>(options.ignoreUndefined);
9+
expectType<boolean | undefined>(options.promoteLongs);
10+
expectType<boolean | undefined>(options.promoteBuffers);
11+
expectType<boolean | undefined>(options.promoteValues);
12+
expectType<Document | undefined>(options.fieldsAsRaw);
13+
14+
type PermittedBSONOptionKeys =
15+
| 'checkKeys'
16+
| 'serializeFunctions'
17+
| 'ignoreUndefined'
18+
| 'promoteLongs'
19+
| 'promoteBuffers'
20+
| 'promoteValues'
21+
| 'fieldsAsRaw'
22+
| 'raw';
23+
24+
const keys = (null as unknown) as PermittedBSONOptionKeys;
25+
// creates an explicit allow list assertion
26+
expectType<keyof BSONSerializeOptions>(keys);

0 commit comments

Comments
 (0)