Skip to content

Commit e653c77

Browse files
committed
chore(shell-bson-parser): clean bson import
1 parent 338432f commit e653c77

1 file changed

Lines changed: 30 additions & 33 deletions

File tree

packages/shell-bson-parser/src/scope.ts

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as bson from 'bson';
1+
import { Binary, BSONSymbol, Code, DBRef, Decimal128, Double, Int32, Long, MaxKey, MinKey, ObjectId, Timestamp, UUID } from 'bson';
22

33
// Returns the same object but frozen and with a null prototype.
44
function lookupMap<T extends object>(input: T): Readonly<T> {
@@ -9,9 +9,9 @@ function lookupMap<T extends object>(input: T): Readonly<T> {
99

1010
function NumberLong(v: any) {
1111
if (typeof v === 'string') {
12-
return bson.Long.fromString(v);
12+
return Long.fromString(v);
1313
} else {
14-
return bson.Long.fromNumber(v);
14+
return Long.fromNumber(v);
1515
}
1616
}
1717

@@ -39,15 +39,15 @@ const SCOPE_NEW: { [x: string]: Function } = lookupMap({
3939
const SCOPE_ANY: { [x: string]: Function } = lookupMap({
4040
RegExp: RegExp,
4141
Binary: function (buffer: any, subType: any) {
42-
return new bson.Binary(buffer, subType);
42+
return new Binary(buffer, subType);
4343
},
4444

4545
// Legacy UUID functions from
4646
// https://github.com/mongodb/mongo-csharp-driver/blob/ac2b2a61c6b7a193cf0266dfb8c65f86c2bf7572/uuidhelpers.js
4747
LegacyJavaUUID: function (u: any) {
4848
if (u === undefined) {
4949
// Generate a new UUID and format it.
50-
u = new bson.UUID().toHexString();
50+
u = new UUID().toHexString();
5151
}
5252

5353
let hex: string = String.prototype.replace.call(u, /[{}-]/g, () => '');
@@ -74,12 +74,12 @@ const SCOPE_ANY: { [x: string]: Function } = lookupMap({
7474
hex = msb + lsb;
7575

7676
const hexBuffer = Buffer.from(hex, 'hex');
77-
return new bson.Binary(hexBuffer, 3);
77+
return new Binary(hexBuffer, 3);
7878
},
7979
LegacyCSharpUUID: function (u: any) {
8080
if (u === undefined) {
8181
// Generate a new UUID and format it.
82-
u = new bson.UUID().toHexString();
82+
u = new UUID().toHexString();
8383
}
8484

8585
let hex: string = String.prototype.replace.call(u, /[{}-]/g, () => '');
@@ -98,14 +98,14 @@ const SCOPE_ANY: { [x: string]: Function } = lookupMap({
9898
hex = a + b + c + d;
9999

100100
const hexBuffer = Buffer.from(hex, 'hex');
101-
return new bson.Binary(hexBuffer, 3);
101+
return new Binary(hexBuffer, 3);
102102
},
103103
LegacyPythonUUID: function (u: any) {
104104
if (u === undefined) {
105-
return new bson.Binary(new bson.UUID().toBinary().buffer, 3);
105+
return new Binary(new UUID().toBinary().buffer, 3);
106106
}
107107

108-
return new bson.Binary(
108+
return new Binary(
109109
Buffer.from(
110110
String.prototype.replace.call(u, /[{}-]/g, () => ''),
111111
'hex',
@@ -114,57 +114,54 @@ const SCOPE_ANY: { [x: string]: Function } = lookupMap({
114114
);
115115
},
116116
BinData: function (t: any, d: any) {
117-
return new bson.Binary(Buffer.from(d, 'base64'), t);
117+
return new Binary(Buffer.from(d, 'base64'), t);
118118
},
119119
UUID: function (u: any) {
120120
if (u === undefined) {
121-
return new bson.UUID().toBinary();
121+
return new UUID().toBinary();
122122
}
123-
return new bson.Binary(Buffer.from(u.replace(/-/g, ''), 'hex'), 4);
123+
return new Binary(Buffer.from(u.replace(/-/g, ''), 'hex'), 4);
124124
},
125125
Code: function (c: any, s: any) {
126-
return new bson.Code(c, s);
126+
return new Code(c, s);
127127
},
128128
DBRef: function (namespace: any, oid: any, db: any, fields: any) {
129-
return new (bson as any).DBRef(namespace, oid, db, fields);
129+
return new DBRef(namespace, oid, db, fields);
130130
},
131131
Decimal128: function (s: any) {
132-
return bson.Decimal128.fromString(s);
132+
return Decimal128.fromString(s);
133133
},
134134
NumberDecimal: function (s: any) {
135-
return bson.Decimal128.fromString(s);
135+
return Decimal128.fromString(s);
136136
},
137137
Double: function (s: any) {
138-
return new bson.Double(s);
138+
return new Double(s);
139139
},
140140
Int32: function (i: any) {
141-
return new bson.Int32(i);
141+
return new Int32(i);
142142
},
143143
NumberInt: function (s: any) {
144-
return new bson.Int32(s);
144+
return new Int32(s);
145145
},
146146
Long: function (low: any, high: any) {
147-
return new bson.Long(low, high);
147+
return new Long(low, high);
148148
},
149149
NumberLong: NumberLong,
150150
Int64: NumberLong,
151151
Map: function (arr: any) {
152-
return new ((bson as any).Map ?? Map)(arr);
152+
return new Map(arr);
153153
},
154154
MaxKey: function () {
155-
return new bson.MaxKey();
155+
return new MaxKey();
156156
},
157157
MinKey: function () {
158-
return new bson.MinKey();
159-
},
160-
ObjectID: function (i: any) {
161-
return new bson.ObjectId(i);
158+
return new MinKey();
162159
},
163160
ObjectId: function (i: any) {
164-
return new bson.ObjectId(i);
161+
return new ObjectId(i);
165162
},
166163
Symbol: function (i: any) {
167-
return new (bson as any).BSONSymbol(i);
164+
return new BSONSymbol(i);
168165
},
169166
Timestamp: function (low: any, high: any) {
170167
if (
@@ -173,10 +170,10 @@ const SCOPE_ANY: { [x: string]: Function } = lookupMap({
173170
) {
174171
// https://www.mongodb.com/docs/manual/reference/bson-types/#timestamps
175172
// reverse the order to match the legacy shell
176-
return new bson.Timestamp({ t: low, i: high });
173+
return new Timestamp({ t: low, i: high });
177174
}
178175

179-
return new bson.Timestamp(low);
176+
return new Timestamp(low);
180177
},
181178
ISODate: function (input?: string): Date {
182179
if (input === undefined) return new Date();
@@ -220,7 +217,7 @@ type AllowedMethods = { [methodName: string]: boolean };
220217
*/
221218
type ClassExpressions = {
222219
[member: string]: {
223-
class: typeof Math | typeof Date | typeof bson.Binary;
220+
class: typeof Math | typeof Date | typeof Binary;
224221
allowedMethods: AllowedMethods | string;
225222
};
226223
};
@@ -313,7 +310,7 @@ const ALLOWED_CLASS_EXPRESSIONS: ClassExpressions = lookupMap({
313310
allowedMethods: 'Date',
314311
}),
315312
Binary: lookupMap({
316-
class: bson.Binary,
313+
class: Binary,
317314
allowedMethods: {
318315
createFromHexString: true,
319316
createFromBase64: true,

0 commit comments

Comments
 (0)