Skip to content

Commit

Permalink
feat!(NODE-4862): add BSONType enum and remove internal constants fro…
Browse files Browse the repository at this point in the history
…m export (#532)

Co-authored-by: Durran Jordan <durran@gmail.com>
nbbeeken and durran authored Dec 8, 2022
1 parent 19b0654 commit 196f9f8
Showing 8 changed files with 221 additions and 224 deletions.
37 changes: 1 addition & 36 deletions src/bson.ts
Original file line number Diff line number Diff line change
@@ -20,42 +20,6 @@ import { Timestamp } from './timestamp';
import { ByteUtils } from './utils/byte_utils';
export type { UUIDExtended, BinaryExtended, BinaryExtendedLegacy, BinarySequence } from './binary';
export type { CodeExtended } from './code';
export {
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
} from './constants';
export type { DBRefLike } from './db_ref';
export type { Decimal128Extended } from './decimal128';
export type { DoubleExtended } from './double';
@@ -92,6 +56,7 @@ export {
ObjectId as ObjectID
};
export { BSONError, BSONTypeError } from './error';
export { BSONType } from './constants';

/** @public */
export interface Document {
28 changes: 28 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -108,3 +108,31 @@ export const BSON_BINARY_SUBTYPE_COLUMN = 7;

/** Binary User Defined Type @internal */
export const BSON_BINARY_SUBTYPE_USER_DEFINED = 128;

/** @public */
export const BSONType = Object.freeze({
double: 1,
string: 2,
object: 3,
array: 4,
binData: 5,
undefined: 6,
objectId: 7,
bool: 8,
date: 9,
null: 10,
regex: 11,
dbPointer: 12,
javascript: 13,
symbol: 14,
javascriptWithScope: 15,
int: 16,
timestamp: 17,
long: 18,
decimal: 19,
minKey: -1,
maxKey: 127
} as const);

/** @public */
export type BSONType = typeof BSONType[keyof typeof BSONType];
9 changes: 5 additions & 4 deletions test/node/bson_test.js
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

const { Buffer } = require('buffer');
const BSON = require('../register-bson');
const { BSON_INT32_MAX, BSON_BINARY_SUBTYPE_USER_DEFINED } = require('../../src/constants');
const Code = BSON.Code;
const BSONRegExp = BSON.BSONRegExp;
const Binary = BSON.Binary;
@@ -434,7 +435,7 @@ describe('BSON', function () {
* @ignore
*/
it('Should Correctly Serialize and Deserialize Number 4', function (done) {
var doc = { doc: BSON.BSON_INT32_MAX + 10 };
var doc = { doc: BSON_INT32_MAX + 10 };
var serialized_data = BSON.serialize(doc);

var serialized_data2 = Buffer.alloc(BSON.calculateObjectSize(doc));
@@ -443,7 +444,7 @@ describe('BSON', function () {

var deserialized = BSON.deserialize(serialized_data);
// expect(deserialized.doc instanceof Binary).to.be.ok;
expect(BSON.BSON_INT32_MAX + 10).to.equal(deserialized.doc);
expect(BSON_INT32_MAX + 10).to.equal(deserialized.doc);
done();
});

@@ -853,7 +854,7 @@ describe('BSON', function () {
*/
it('Should Correctly Serialize and Deserialize a User defined Binary object', function (done) {
var bin = new Binary();
bin.sub_type = BSON.BSON_BINARY_SUBTYPE_USER_DEFINED;
bin.sub_type = BSON_BINARY_SUBTYPE_USER_DEFINED;
var string = 'binstring';
for (var index = 0; index < string.length; index++) {
bin.put(string.charAt(index));
@@ -867,7 +868,7 @@ describe('BSON', function () {
assertBuffersEqual(done, serialized_data, serialized_data2, 0);
var deserialized_data = BSON.deserialize(serialized_data);

expect(deserialized_data.doc.sub_type).to.deep.equal(BSON.BSON_BINARY_SUBTYPE_USER_DEFINED);
expect(deserialized_data.doc.sub_type).to.deep.equal(BSON_BINARY_SUBTYPE_USER_DEFINED);
expect(doc.doc.value()).to.deep.equal(deserialized_data.doc.value());
done();
});
142 changes: 0 additions & 142 deletions test/node/check_constants.js

This file was deleted.

180 changes: 180 additions & 0 deletions test/node/constants.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import { expect } from 'chai';
import { Binary } from '../register-bson';
import * as constants from '../../src/constants';

describe('BSON Constants', () => {
context('Binary Subtype', () => {
/*
subtype ::=
| "\x00" Generic binary subtype
| "\x01" Function
| "\x02" Binary (Old)
| "\x03" UUID (Old)
| "\x04" UUID
| "\x05" MD5
| "\x06" Encrypted BSON value
| "\x80" User defined
*/
it('Default should be 0', () => {
expect(constants.BSON_BINARY_SUBTYPE_DEFAULT).to.equal(0);
expect(Binary.SUBTYPE_DEFAULT).to.equal(0);
});
it('Function should be 1', () => {
expect(constants.BSON_BINARY_SUBTYPE_FUNCTION).to.equal(1);
expect(Binary.SUBTYPE_FUNCTION).to.equal(1);
});
it('Binary (Old) should be 2', () => {
expect(constants.BSON_BINARY_SUBTYPE_BYTE_ARRAY).to.equal(2);
expect(Binary.SUBTYPE_BYTE_ARRAY).to.equal(2);
});
it('UUID (Old) should be 3', () => {
expect(constants.BSON_BINARY_SUBTYPE_UUID).to.equal(3);
expect(Binary.SUBTYPE_UUID_OLD).to.equal(3);
});
it('UUID should be 4', () => {
expect(constants.BSON_BINARY_SUBTYPE_UUID_NEW).to.equal(4);
expect(Binary.SUBTYPE_UUID).to.equal(4);
});
it('MD5 should be 5', () => {
expect(constants.BSON_BINARY_SUBTYPE_MD5).to.equal(5);
expect(Binary.SUBTYPE_MD5).to.equal(5);
});

it('Encrypted should be 6', () => {
expect(constants.BSON_BINARY_SUBTYPE_ENCRYPTED).to.equal(6);
expect(Binary.SUBTYPE_ENCRYPTED).to.equal(6);
});

it('Column should be 7', () => {
expect(constants.BSON_BINARY_SUBTYPE_COLUMN).to.equal(7);
expect(Binary.SUBTYPE_COLUMN).to.equal(7);
});
});
context('BSON Type indicators', () => {
/*
| "\x01" 64-bit binary floating point
| "\x02" UTF-8 string
| "\x03" Embedded document
| "\x04" Array
| "\x05" Binary data
| "\x06" Undefined (value) — Deprecated
| "\x07" ObjectId
| "\x08" Boolean
| "\x09" UTC date time
| "\x0A" Null value
| "\x0B" Regular expression
| "\x0C" DBPointer — Deprecated
| "\x0D" JavaScript code
| "\x0E" Symbol. — Deprecated
| "\x0F" JavaScript code w/ scope — Deprecated
| "\x10" 32-bit integer
| "\x11" Timestamp
| "\x12" 64-bit integer
| "\x13" 128-bit decimal floating point
| "\xFF" Min key
| "\x7F" Max key
*/

it('64-bit binary floating point should be 0x01', () => {
expect(constants.BSON_DATA_NUMBER).to.equal(0x01);
});
it('UTF-8 string should be 0x02', () => {
expect(constants.BSON_DATA_STRING).to.equal(0x02);
});
it('Embedded document should be 0x03', () => {
expect(constants.BSON_DATA_OBJECT).to.equal(0x03);
});
it('Array should be 0x04', () => {
expect(constants.BSON_DATA_ARRAY).to.equal(0x04);
});
it('Binary data should be 0x05', () => {
expect(constants.BSON_DATA_BINARY).to.equal(0x05);
});
it('Undefined (value) — Deprecated should be 0x06', () => {
expect(constants.BSON_DATA_UNDEFINED).to.equal(0x06);
});
it('ObjectId should be 0x07', () => {
expect(constants.BSON_DATA_OID).to.equal(0x07);
});
it('Boolean should be 0x08', () => {
expect(constants.BSON_DATA_BOOLEAN).to.equal(0x08);
});
it('UTC date time should be 0x09', () => {
expect(constants.BSON_DATA_DATE).to.equal(0x09);
});
it('Null value should be 0x0A', () => {
expect(constants.BSON_DATA_NULL).to.equal(0x0a);
});
it('Regular expression should be 0x0B', () => {
expect(constants.BSON_DATA_REGEXP).to.equal(0x0b);
});
it('DBPointer — Deprecated should be 0x0C', () => {
expect(constants.BSON_DATA_DBPOINTER).to.equal(0x0c);
});
it('JavaScript code should be 0x0D', () => {
expect(constants.BSON_DATA_CODE).to.equal(0x0d);
});
it('Symbol. — Deprecated should be 0x0E', () => {
expect(constants.BSON_DATA_SYMBOL).to.equal(0x0e);
});
it('JavaScript code w/ scope — Deprecated should be 0x0F', () => {
expect(constants.BSON_DATA_CODE_W_SCOPE).to.equal(0x0f);
});
it('32-bit integer should be 0x10', () => {
expect(constants.BSON_DATA_INT).to.equal(0x10);
});
it('Timestamp should be 0x11', () => {
expect(constants.BSON_DATA_TIMESTAMP).to.equal(0x11);
});
it('64-bit integer should be 0x12', () => {
expect(constants.BSON_DATA_LONG).to.equal(0x12);
});
it('128-bit decimal floating point should be 0x13', () => {
expect(constants.BSON_DATA_DECIMAL128).to.equal(0x13);
});
it('Min key should be 0xFF', () => {
expect(constants.BSON_DATA_MIN_KEY).to.equal(0xff);
});
it('Max key should be 0x7F', () => {
expect(constants.BSON_DATA_MAX_KEY).to.equal(0x7f);
});
});

describe('BSONType enum', () => {
it('double equals 1', () => expect(constants.BSONType.double).to.equal(1));
it('string equals 2', () => expect(constants.BSONType.string).to.equal(2));
it('object equals 3', () => expect(constants.BSONType.object).to.equal(3));
it('array equals 4', () => expect(constants.BSONType.array).to.equal(4));
it('binData equals 5', () => expect(constants.BSONType.binData).to.equal(5));
it('undefined equals 6', () => expect(constants.BSONType.undefined).to.equal(6));
it('objectId equals 7', () => expect(constants.BSONType.objectId).to.equal(7));
it('bool equals 8', () => expect(constants.BSONType.bool).to.equal(8));
it('date equals 9', () => expect(constants.BSONType.date).to.equal(9));
it('null equals 10', () => expect(constants.BSONType.null).to.equal(10));
it('regex equals 11', () => expect(constants.BSONType.regex).to.equal(11));
it('dbPointer equals 12', () => expect(constants.BSONType.dbPointer).to.equal(12));
it('javascript equals 13', () => expect(constants.BSONType.javascript).to.equal(13));
it('symbol equals 14', () => expect(constants.BSONType.symbol).to.equal(14));
it('javascriptWithScope equals 15', () =>
expect(constants.BSONType.javascriptWithScope).to.equal(15));
it('int equals 16', () => expect(constants.BSONType.int).to.equal(16));
it('timestamp equals 17', () => expect(constants.BSONType.timestamp).to.equal(17));
it('long equals 18', () => expect(constants.BSONType.long).to.equal(18));
it('decimal equals 19', () => expect(constants.BSONType.decimal).to.equal(19));
it('minKey equals -1', () => expect(constants.BSONType.minKey).to.equal(-1));
it('maxKey equals 27', () => expect(constants.BSONType.maxKey).to.equal(127));

it('minKey equals 255 when used in Uint8Array', () => {
const byte = new Uint8Array(1);
byte[0] = constants.BSONType.minKey;
expect(byte[0]).to.equal(255);
});

it('minKey equals 255 when used in DataView in unsigned way', () => {
const dv = new DataView(new ArrayBuffer(1));
dv.setUint8(0, constants.BSONType.minKey);
expect(dv.getUint8(0)).to.equal(255);
expect(new Uint8Array(dv.buffer, 0, 1)[0]).to.equal(255);
});
});
});
10 changes: 4 additions & 6 deletions test/node/double.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as BSON from '../register-bson';
const Double = BSON.Double;

const BSON_DOUBLE_TYPE_INDICATOR = 0x01;
const BSON_INT_TYPE_INDICATOR = 0x10;
import { BSON_DATA_NUMBER, BSON_DATA_INT } from '../../src/constants';

describe('BSON Double Precision', function () {
context('class Double', function () {
@@ -82,11 +81,10 @@ describe('BSON Double Precision', function () {
});

it('does preserve -0 in serialize as a double', function () {
const value = -0;
const serializedDouble = BSON.serialize({ d: value });
const serializedDouble = BSON.serialize({ d: -0 });
const type = serializedDouble[4];
expect(type).to.equal(BSON_DOUBLE_TYPE_INDICATOR);
expect(type).to.not.equal(BSON_INT_TYPE_INDICATOR);
expect(type).to.equal(BSON_DATA_NUMBER);
expect(type).to.not.equal(BSON_DATA_INT);
expect(serializedDouble.subarray(7, 15)).to.deep.equal(
new Uint8Array(new Float64Array([-0]).buffer)
);
35 changes: 1 addition & 34 deletions test/node/exports.test.ts
Original file line number Diff line number Diff line change
@@ -5,40 +5,7 @@ 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',
'BSONType',
'EJSON',
'Code',
'BSONSymbol',
4 changes: 2 additions & 2 deletions test/node/uuid_tests.js
Original file line number Diff line number Diff line change
@@ -6,8 +6,8 @@ const { inspect } = require('util');
const { validate: uuidStringValidate, version: uuidStringVersion } = require('uuid');
const BSON = require('../register-bson');
const BSONTypeError = BSON.BSONTypeError;
const BSON_DATA_BINARY = BSON.BSON_DATA_BINARY;
const BSON_BINARY_SUBTYPE_UUID_NEW = BSON.BSON_BINARY_SUBTYPE_UUID_NEW;
const BSON_DATA_BINARY = BSON.BSONType.binData;
const { BSON_BINARY_SUBTYPE_UUID_NEW } = require('../../src/constants');

// Test values
const UPPERCASE_DASH_SEPARATED_UUID_STRING = 'AAAAAAAA-AAAA-4AAA-AAAA-AAAAAAAAAAAA';

0 comments on commit 196f9f8

Please sign in to comment.