Skip to content

Commit 196f9f8

Browse files
nbbeekendurran
andauthored
feat!(NODE-4862): add BSONType enum and remove internal constants from export (#532)
Co-authored-by: Durran Jordan <durran@gmail.com>
1 parent 19b0654 commit 196f9f8

File tree

8 files changed

+221
-224
lines changed

8 files changed

+221
-224
lines changed

src/bson.ts

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,6 @@ import { Timestamp } from './timestamp';
2020
import { ByteUtils } from './utils/byte_utils';
2121
export type { UUIDExtended, BinaryExtended, BinaryExtendedLegacy, BinarySequence } from './binary';
2222
export type { CodeExtended } from './code';
23-
export {
24-
BSON_BINARY_SUBTYPE_BYTE_ARRAY,
25-
BSON_BINARY_SUBTYPE_DEFAULT,
26-
BSON_BINARY_SUBTYPE_FUNCTION,
27-
BSON_BINARY_SUBTYPE_MD5,
28-
BSON_BINARY_SUBTYPE_USER_DEFINED,
29-
BSON_BINARY_SUBTYPE_UUID,
30-
BSON_BINARY_SUBTYPE_UUID_NEW,
31-
BSON_BINARY_SUBTYPE_ENCRYPTED,
32-
BSON_BINARY_SUBTYPE_COLUMN,
33-
BSON_DATA_ARRAY,
34-
BSON_DATA_BINARY,
35-
BSON_DATA_BOOLEAN,
36-
BSON_DATA_CODE,
37-
BSON_DATA_CODE_W_SCOPE,
38-
BSON_DATA_DATE,
39-
BSON_DATA_DBPOINTER,
40-
BSON_DATA_DECIMAL128,
41-
BSON_DATA_INT,
42-
BSON_DATA_LONG,
43-
BSON_DATA_MAX_KEY,
44-
BSON_DATA_MIN_KEY,
45-
BSON_DATA_NULL,
46-
BSON_DATA_NUMBER,
47-
BSON_DATA_OBJECT,
48-
BSON_DATA_OID,
49-
BSON_DATA_REGEXP,
50-
BSON_DATA_STRING,
51-
BSON_DATA_SYMBOL,
52-
BSON_DATA_TIMESTAMP,
53-
BSON_DATA_UNDEFINED,
54-
BSON_INT32_MAX,
55-
BSON_INT32_MIN,
56-
BSON_INT64_MAX,
57-
BSON_INT64_MIN
58-
} from './constants';
5923
export type { DBRefLike } from './db_ref';
6024
export type { Decimal128Extended } from './decimal128';
6125
export type { DoubleExtended } from './double';
@@ -92,6 +56,7 @@ export {
9256
ObjectId as ObjectID
9357
};
9458
export { BSONError, BSONTypeError } from './error';
59+
export { BSONType } from './constants';
9560

9661
/** @public */
9762
export interface Document {

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/bson_test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const { Buffer } = require('buffer');
44
const BSON = require('../register-bson');
5+
const { BSON_INT32_MAX, BSON_BINARY_SUBTYPE_USER_DEFINED } = require('../../src/constants');
56
const Code = BSON.Code;
67
const BSONRegExp = BSON.BSONRegExp;
78
const Binary = BSON.Binary;
@@ -434,7 +435,7 @@ describe('BSON', function () {
434435
* @ignore
435436
*/
436437
it('Should Correctly Serialize and Deserialize Number 4', function (done) {
437-
var doc = { doc: BSON.BSON_INT32_MAX + 10 };
438+
var doc = { doc: BSON_INT32_MAX + 10 };
438439
var serialized_data = BSON.serialize(doc);
439440

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

444445
var deserialized = BSON.deserialize(serialized_data);
445446
// expect(deserialized.doc instanceof Binary).to.be.ok;
446-
expect(BSON.BSON_INT32_MAX + 10).to.equal(deserialized.doc);
447+
expect(BSON_INT32_MAX + 10).to.equal(deserialized.doc);
447448
done();
448449
});
449450

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

870-
expect(deserialized_data.doc.sub_type).to.deep.equal(BSON.BSON_BINARY_SUBTYPE_USER_DEFINED);
871+
expect(deserialized_data.doc.sub_type).to.deep.equal(BSON_BINARY_SUBTYPE_USER_DEFINED);
871872
expect(doc.doc.value()).to.deep.equal(deserialized_data.doc.value());
872873
done();
873874
});

test/node/check_constants.js

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

0 commit comments

Comments
 (0)