@mongosh/shell-bson 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.depcheckrc +16 -0
- package/.eslintignore +2 -0
- package/.eslintrc.js +10 -0
- package/.prettierignore +6 -0
- package/.prettierrc.json +1 -0
- package/AUTHORS +2 -0
- package/LICENSE +192 -0
- package/README.md +27 -0
- package/lib/bson-export.d.ts +19 -0
- package/lib/bson-export.js +3 -0
- package/lib/bson-export.js.map +1 -0
- package/lib/helpers.d.ts +11 -0
- package/lib/helpers.js +69 -0
- package/lib/helpers.js.map +1 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +9 -0
- package/lib/index.js.map +1 -0
- package/lib/printable-bson.d.ts +6 -0
- package/lib/printable-bson.js +98 -0
- package/lib/printable-bson.js.map +1 -0
- package/lib/shell-bson.d.ts +58 -0
- package/lib/shell-bson.js +213 -0
- package/lib/shell-bson.js.map +1 -0
- package/package.json +55 -0
- package/src/bson-export.ts +51 -0
- package/src/helpers.ts +123 -0
- package/src/index.ts +3 -0
- package/src/printable-bson.spec.ts +137 -0
- package/src/printable-bson.ts +147 -0
- package/src/shell-bson.spec.ts +820 -0
- package/src/shell-bson.ts +447 -0
- package/tsconfig-lint.json +5 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.constructShellBson = constructShellBson;
|
|
4
|
+
const errors_1 = require("@mongosh/errors");
|
|
5
|
+
const helpers_1 = require("./helpers");
|
|
6
|
+
const crypto_1 = require("crypto");
|
|
7
|
+
function constructShellBson({ bsonLibrary: bson, printWarning, assignMetadata, constructHelp, }) {
|
|
8
|
+
const bsonNames = [
|
|
9
|
+
'Binary',
|
|
10
|
+
'Code',
|
|
11
|
+
'DBRef',
|
|
12
|
+
'Decimal128',
|
|
13
|
+
'Double',
|
|
14
|
+
'Int32',
|
|
15
|
+
'Long',
|
|
16
|
+
'MaxKey',
|
|
17
|
+
'MinKey',
|
|
18
|
+
'ObjectId',
|
|
19
|
+
'Timestamp',
|
|
20
|
+
'BSONSymbol',
|
|
21
|
+
'BSONRegExp',
|
|
22
|
+
];
|
|
23
|
+
const helps = {};
|
|
24
|
+
for (const className of bsonNames) {
|
|
25
|
+
if (!(className in bson)) {
|
|
26
|
+
throw new errors_1.MongoshInternalError(`${className} does not exist in provided BSON package.`);
|
|
27
|
+
}
|
|
28
|
+
const help = constructHelp?.(className);
|
|
29
|
+
helps[className] = help;
|
|
30
|
+
if (!('prototype' in bson[className]))
|
|
31
|
+
continue;
|
|
32
|
+
assignMetadata?.(bson[className].prototype, {
|
|
33
|
+
help: help,
|
|
34
|
+
...(className === 'BSONSymbol'
|
|
35
|
+
? { deprecated: true, maxVersion: '1.6.0' }
|
|
36
|
+
: {}),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
const bsonPkg = {
|
|
40
|
+
DBRef: (0, helpers_1.assignAll)(function DBRef(namespace, oid, db, fields) {
|
|
41
|
+
(0, helpers_1.assertArgsDefinedType)([namespace, oid, db], ['string', true, [undefined, 'string'], [undefined, 'object']], 'DBRef');
|
|
42
|
+
return new bson.DBRef(namespace, oid, db, fields);
|
|
43
|
+
}, (0, helpers_1.pickWithExactKeyMatch)(bson.DBRef, ['prototype'])),
|
|
44
|
+
bsonsize: function bsonsize(object) {
|
|
45
|
+
(0, helpers_1.assertArgsDefinedType)([object], ['object'], 'bsonsize');
|
|
46
|
+
return bson.calculateObjectSize(object);
|
|
47
|
+
},
|
|
48
|
+
MaxKey: (0, helpers_1.assignAll)(function MaxKey() {
|
|
49
|
+
return new bson.MaxKey();
|
|
50
|
+
}, (0, helpers_1.pickWithExactKeyMatch)(bson.MaxKey, ['prototype']), { toBSON: () => new bson.MaxKey() }),
|
|
51
|
+
MinKey: (0, helpers_1.assignAll)(function MinKey() {
|
|
52
|
+
return new bson.MinKey();
|
|
53
|
+
}, (0, helpers_1.pickWithExactKeyMatch)(bson.MinKey, ['prototype']), { toBSON: () => new bson.MinKey() }),
|
|
54
|
+
ObjectId: (0, helpers_1.assignAll)(function ObjectId(id) {
|
|
55
|
+
(0, helpers_1.assertArgsDefinedType)([id], [[undefined, 'string', 'number', 'object']], 'ObjectId');
|
|
56
|
+
return new bson.ObjectId(id);
|
|
57
|
+
}, (0, helpers_1.pickWithExactKeyMatch)(bson.ObjectId, ['prototype', 'cacheHexString', 'generate', 'createFromTime', 'createFromHexString', 'createFromBase64', 'isValid'])),
|
|
58
|
+
Timestamp: (0, helpers_1.assignAll)(function Timestamp(t, i) {
|
|
59
|
+
(0, helpers_1.assertArgsDefinedType)([t, i], [
|
|
60
|
+
['number', 'object', undefined],
|
|
61
|
+
[undefined, 'number'],
|
|
62
|
+
], 'Timestamp');
|
|
63
|
+
if (typeof t === 'object' && t !== null && 't' in t && 'i' in t) {
|
|
64
|
+
return new bson.Timestamp(t);
|
|
65
|
+
}
|
|
66
|
+
else if (i !== undefined || typeof t === 'number') {
|
|
67
|
+
return new bson.Timestamp({ t: t, i: i ?? 0 });
|
|
68
|
+
}
|
|
69
|
+
return new bson.Timestamp(t);
|
|
70
|
+
}, (0, helpers_1.pickWithExactKeyMatch)(bson.Timestamp, ['prototype', 'fromInt', 'fromNumber', 'fromBits', 'fromString', 'MAX_VALUE'])),
|
|
71
|
+
Code: (0, helpers_1.assignAll)(function Code(c = '', s) {
|
|
72
|
+
(0, helpers_1.assertArgsDefinedType)([c, s], [
|
|
73
|
+
[undefined, 'string', 'function'],
|
|
74
|
+
[undefined, 'object'],
|
|
75
|
+
], 'Code');
|
|
76
|
+
return new bson.Code(c, s);
|
|
77
|
+
}, (0, helpers_1.pickWithExactKeyMatch)(bson.Code, ['prototype'])),
|
|
78
|
+
NumberDecimal: (0, helpers_1.assignAll)(function NumberDecimal(s = '0') {
|
|
79
|
+
(0, helpers_1.assertArgsDefinedType)([s], [['string', 'number', 'bson:Long', 'bson:Int32', 'bson:Decimal128']], 'NumberDecimal');
|
|
80
|
+
if (typeof s === 'number') {
|
|
81
|
+
printWarning('NumberDecimal: specifying a number as argument is deprecated and may lead to loss of precision, pass a string instead');
|
|
82
|
+
}
|
|
83
|
+
return bson.Decimal128.fromString(`${s}`);
|
|
84
|
+
}, { prototype: bson.Decimal128.prototype }),
|
|
85
|
+
NumberInt: (0, helpers_1.assignAll)(function NumberInt(v = '0') {
|
|
86
|
+
v ?? (v = '0');
|
|
87
|
+
(0, helpers_1.assertArgsDefinedType)([v], [['string', 'number', 'bson:Long', 'bson:Int32']], 'NumberInt');
|
|
88
|
+
return new bson.Int32(parseInt(`${v}`, 10));
|
|
89
|
+
}, { prototype: bson.Int32.prototype }),
|
|
90
|
+
NumberLong: (0, helpers_1.assignAll)(function NumberLong(s = '0') {
|
|
91
|
+
s ?? (s = '0');
|
|
92
|
+
(0, helpers_1.assertArgsDefinedType)([s], [['string', 'number', 'bson:Long', 'bson:Int32']], 'NumberLong');
|
|
93
|
+
if (typeof s === 'number') {
|
|
94
|
+
printWarning('NumberLong: specifying a number as argument is deprecated and may lead to loss of precision, pass a string instead');
|
|
95
|
+
return bson.Long.fromNumber(s);
|
|
96
|
+
}
|
|
97
|
+
return bson.Long.fromString(`${s}`);
|
|
98
|
+
}, { prototype: bson.Long.prototype }),
|
|
99
|
+
ISODate: function ISODate(input) {
|
|
100
|
+
if (input === undefined)
|
|
101
|
+
return new Date();
|
|
102
|
+
if (typeof input !== 'string')
|
|
103
|
+
return new Date(input);
|
|
104
|
+
const isoDateRegex = /^(?<Y>\d{4})-?(?<M>\d{2})-?(?<D>\d{2})([T ](?<h>\d{2})(:?(?<m>\d{2})(:?((?<s>\d{2})(\.(?<ms>\d+))?))?)?(?<tz>Z|([+-])(\d{2}):?(\d{2})?)?)?$/;
|
|
105
|
+
const match = isoDateRegex.exec(input);
|
|
106
|
+
if (match !== null && match.groups !== undefined) {
|
|
107
|
+
const { Y, M, D, h, m, s, ms, tz } = match.groups;
|
|
108
|
+
const normalized = `${Y}-${M}-${D}T${h || '00'}:${m || '00'}:${s || '00'}.${ms || '000'}${tz || 'Z'}`;
|
|
109
|
+
const date = new Date(normalized);
|
|
110
|
+
if (date.getTime() >= -62167219200000 &&
|
|
111
|
+
date.getTime() <= 253402300799999) {
|
|
112
|
+
return date;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
throw new errors_1.MongoshInvalidInputError(`${JSON.stringify(input)} is not a valid ISODate`, errors_1.CommonErrors.InvalidArgument);
|
|
116
|
+
},
|
|
117
|
+
BinData: (0, helpers_1.assignAll)(function BinData(subtype, b64string) {
|
|
118
|
+
(0, helpers_1.assertArgsDefinedType)([subtype, b64string], ['number', 'string'], 'BinData');
|
|
119
|
+
const buffer = Buffer.from(b64string, 'base64');
|
|
120
|
+
return new bson.Binary(buffer, subtype);
|
|
121
|
+
}, { prototype: bson.Binary.prototype }),
|
|
122
|
+
HexData: (0, helpers_1.assignAll)(function HexData(subtype, hexstr) {
|
|
123
|
+
(0, helpers_1.assertArgsDefinedType)([subtype, hexstr], ['number', 'string'], 'HexData');
|
|
124
|
+
const buffer = Buffer.from(hexstr, 'hex');
|
|
125
|
+
return new bson.Binary(buffer, subtype);
|
|
126
|
+
}, { prototype: bson.Binary.prototype }),
|
|
127
|
+
UUID: (0, helpers_1.assignAll)(function UUID(hexstr) {
|
|
128
|
+
if (hexstr === undefined) {
|
|
129
|
+
const uuid = (0, crypto_1.randomBytes)(16);
|
|
130
|
+
uuid[6] = (uuid[6] & 0x0f) | 0x40;
|
|
131
|
+
uuid[8] = (uuid[8] & 0x3f) | 0x80;
|
|
132
|
+
hexstr = uuid.toString('hex');
|
|
133
|
+
}
|
|
134
|
+
(0, helpers_1.assertArgsDefinedType)([hexstr], ['string'], 'UUID');
|
|
135
|
+
const buffer = Buffer.from(hexstr.replace(/-/g, ''), 'hex');
|
|
136
|
+
return new bson.Binary(buffer, bson.Binary.SUBTYPE_UUID);
|
|
137
|
+
}, { prototype: bson.Binary.prototype }),
|
|
138
|
+
MD5: (0, helpers_1.assignAll)(function MD5(hexstr) {
|
|
139
|
+
(0, helpers_1.assertArgsDefinedType)([hexstr], ['string'], 'MD5');
|
|
140
|
+
const buffer = Buffer.from(hexstr, 'hex');
|
|
141
|
+
return new bson.Binary(buffer, bson.Binary.SUBTYPE_MD5);
|
|
142
|
+
}, { prototype: bson.Binary.prototype }),
|
|
143
|
+
Decimal128: (0, helpers_1.assignAll)((0, helpers_1.functionCtorWithoutProps)(bson.Decimal128), (0, helpers_1.pickWithExactKeyMatch)(bson.Decimal128, [
|
|
144
|
+
'prototype',
|
|
145
|
+
'fromString',
|
|
146
|
+
'fromStringWithRounding',
|
|
147
|
+
])),
|
|
148
|
+
BSONSymbol: (0, helpers_1.assignAll)((0, helpers_1.functionCtorWithoutProps)(bson.BSONSymbol), (0, helpers_1.pickWithExactKeyMatch)(bson.BSONSymbol, ['prototype'])),
|
|
149
|
+
Int32: (0, helpers_1.assignAll)((0, helpers_1.functionCtorWithoutProps)(bson.Int32), (0, helpers_1.pickWithExactKeyMatch)(bson.Int32, ['prototype', 'fromString'])),
|
|
150
|
+
Long: (0, helpers_1.assignAll)((0, helpers_1.functionCtorWithoutProps)(bson.Long), (0, helpers_1.pickWithExactKeyMatch)(bson.Long, [
|
|
151
|
+
'prototype',
|
|
152
|
+
'fromValue',
|
|
153
|
+
'isLong',
|
|
154
|
+
'fromBytesBE',
|
|
155
|
+
'fromBytesLE',
|
|
156
|
+
'fromBytes',
|
|
157
|
+
'fromString',
|
|
158
|
+
'fromStringStrict',
|
|
159
|
+
'fromBigInt',
|
|
160
|
+
'fromNumber',
|
|
161
|
+
'fromInt',
|
|
162
|
+
'fromBits',
|
|
163
|
+
'MIN_VALUE',
|
|
164
|
+
'MAX_VALUE',
|
|
165
|
+
'NEG_ONE',
|
|
166
|
+
'UONE',
|
|
167
|
+
'ONE',
|
|
168
|
+
'UZERO',
|
|
169
|
+
'ZERO',
|
|
170
|
+
'MAX_UNSIGNED_VALUE',
|
|
171
|
+
'TWO_PWR_24',
|
|
172
|
+
])),
|
|
173
|
+
Binary: (0, helpers_1.assignAll)((0, helpers_1.functionCtorWithoutProps)(bson.Binary), (0, helpers_1.pickWithExactKeyMatch)(bson.Binary, [
|
|
174
|
+
'prototype',
|
|
175
|
+
'createFromBase64',
|
|
176
|
+
'createFromHexString',
|
|
177
|
+
'fromInt8Array',
|
|
178
|
+
'fromFloat32Array',
|
|
179
|
+
'fromPackedBits',
|
|
180
|
+
'fromBits',
|
|
181
|
+
'BUFFER_SIZE',
|
|
182
|
+
'SUBTYPE_DEFAULT',
|
|
183
|
+
'SUBTYPE_FUNCTION',
|
|
184
|
+
'SUBTYPE_BYTE_ARRAY',
|
|
185
|
+
'SUBTYPE_UUID_OLD',
|
|
186
|
+
'SUBTYPE_UUID',
|
|
187
|
+
'SUBTYPE_MD5',
|
|
188
|
+
'SUBTYPE_ENCRYPTED',
|
|
189
|
+
'SUBTYPE_COLUMN',
|
|
190
|
+
'SUBTYPE_SENSITIVE',
|
|
191
|
+
'SUBTYPE_VECTOR',
|
|
192
|
+
'SUBTYPE_USER_DEFINED',
|
|
193
|
+
'VECTOR_TYPE',
|
|
194
|
+
])),
|
|
195
|
+
Double: (0, helpers_1.assignAll)((0, helpers_1.functionCtorWithoutProps)(bson.Double), (0, helpers_1.pickWithExactKeyMatch)(bson.Double, ['prototype', 'fromString'])),
|
|
196
|
+
BSONRegExp: (0, helpers_1.assignAll)((0, helpers_1.functionCtorWithoutProps)(bson.BSONRegExp), (0, helpers_1.pickWithExactKeyMatch)(bson.BSONRegExp, ['prototype', 'parseOptions'])),
|
|
197
|
+
EJSON: (0, helpers_1.pickWithExactKeyMatch)(bson.EJSON, [
|
|
198
|
+
'parse',
|
|
199
|
+
'serialize',
|
|
200
|
+
'stringify',
|
|
201
|
+
'deserialize',
|
|
202
|
+
]),
|
|
203
|
+
};
|
|
204
|
+
for (const className of Object.keys(bsonPkg)) {
|
|
205
|
+
const help = helps[className] ?? constructHelp?.(className);
|
|
206
|
+
if (!help)
|
|
207
|
+
continue;
|
|
208
|
+
bsonPkg[className].help = () => help;
|
|
209
|
+
Object.setPrototypeOf(bsonPkg[className].help, help);
|
|
210
|
+
}
|
|
211
|
+
return bsonPkg;
|
|
212
|
+
}
|
|
213
|
+
//# sourceMappingURL=shell-bson.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shell-bson.js","sourceRoot":"","sources":["../src/shell-bson.ts"],"names":[],"mappings":";;AAgGA,gDA8VC;AA7bD,4CAIyB;AACzB,uCAKmB;AACnB,mCAAqC;AAoFrC,SAAgB,kBAAkB,CAGhC,EACA,WAAW,EAAE,IAAI,EACjB,YAAY,EACZ,cAAc,EACd,aAAa,GACmB;IAChC,MAAM,SAAS,GAAyC;QACtD,QAAQ;QACR,MAAM;QACN,OAAO;QACP,YAAY;QACZ,QAAQ;QACR,OAAO;QACP,MAAM;QACN,QAAQ;QACR,QAAQ;QACR,UAAU;QACV,WAAW;QACX,YAAY;QACZ,YAAY;KACb,CAAC;IAEF,MAAM,KAAK,GAA+C,EAAE,CAAC;IAC7D,KAAK,MAAM,SAAS,IAAI,SAAS,EAAE,CAAC;QAClC,IAAI,CAAC,CAAC,SAAS,IAAI,IAAI,CAAC,EAAE,CAAC;YACzB,MAAM,IAAI,6BAAoB,CAC5B,GAAG,SAAS,2CAA2C,CACxD,CAAC;QACJ,CAAC;QACD,MAAM,IAAI,GAAG,aAAa,EAAE,CAAC,SAAS,CAAC,CAAC;QACxC,KAAK,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC;QACxB,IAAI,CAAC,CAAC,WAAW,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC;YAAE,SAAS;QAChD,cAAc,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,SAAS,EAAE;YAC1C,IAAI,EAAE,IAAI;YAEV,GAAG,CAAC,SAAS,KAAK,YAAY;gBAC5B,CAAC,CAAC,EAAE,UA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,OAAO,CAAC,SAAS,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACvD,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mongosh/shell-bson",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "mongosh-style BSON handling",
|
|
5
|
+
"homepage": "https://github.com/mongodb-js/mongosh/tree/main/packages/shell-bson#readme",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"main": "lib/index.js",
|
|
8
|
+
"types": "lib/index.d.ts",
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=14.15.1"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/mongodb-js/mongosh.git"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"check": "npm run lint && npm run depcheck",
|
|
21
|
+
"depcheck": "depcheck",
|
|
22
|
+
"compile": "tsc -p tsconfig.json",
|
|
23
|
+
"eslint": "eslint",
|
|
24
|
+
"lint": "npm run eslint . && npm run prettier -- --check .",
|
|
25
|
+
"prepublish": "npm run compile",
|
|
26
|
+
"test": "mocha --timeout 15000 -r ts-node/register --reporter \"../../configs/mocha-config-mongosh/reporter.ts\" \"./src/*.spec.ts\"",
|
|
27
|
+
"test-ci": "node ../../scripts/run-if-package-requested.js npm test",
|
|
28
|
+
"test-coverage": "nyc --no-clean --cwd ../.. --reporter=none npm run test",
|
|
29
|
+
"test-ci-coverage": "nyc --no-clean --cwd ../.. --reporter=none npm run test-ci",
|
|
30
|
+
"prettier": "prettier",
|
|
31
|
+
"reformat": "npm run prettier -- --write . && npm run eslint -- --fix"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/mongodb-js/mongosh/issues"
|
|
35
|
+
},
|
|
36
|
+
"mongosh": {
|
|
37
|
+
"unitTestsOnly": true
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@mongosh/errors": "^2.4.4"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"bson": "^6.10.4"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@mongodb-js/eslint-config-mongosh": "^1.0.0",
|
|
47
|
+
"@mongodb-js/prettier-config-devtools": "^1.0.1",
|
|
48
|
+
"@mongodb-js/tsconfig-mongosh": "^1.0.0",
|
|
49
|
+
"bson": "^6.10.4",
|
|
50
|
+
"depcheck": "^1.4.7",
|
|
51
|
+
"eslint": "^7.25.0",
|
|
52
|
+
"prettier": "^2.8.8"
|
|
53
|
+
},
|
|
54
|
+
"gitHead": "9f0183e375c5422a0e1ced87c9a620384180abf7"
|
|
55
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ObjectId,
|
|
3
|
+
DBRef,
|
|
4
|
+
MaxKey,
|
|
5
|
+
MinKey,
|
|
6
|
+
Timestamp,
|
|
7
|
+
BSONSymbol,
|
|
8
|
+
Code,
|
|
9
|
+
Decimal128,
|
|
10
|
+
Int32,
|
|
11
|
+
Long,
|
|
12
|
+
Binary,
|
|
13
|
+
calculateObjectSize,
|
|
14
|
+
Double,
|
|
15
|
+
EJSON,
|
|
16
|
+
BSONRegExp,
|
|
17
|
+
} from 'bson';
|
|
18
|
+
export type {
|
|
19
|
+
ObjectId,
|
|
20
|
+
DBRef,
|
|
21
|
+
MaxKey,
|
|
22
|
+
MinKey,
|
|
23
|
+
Timestamp,
|
|
24
|
+
BSONSymbol,
|
|
25
|
+
Code,
|
|
26
|
+
Decimal128,
|
|
27
|
+
Int32,
|
|
28
|
+
Long,
|
|
29
|
+
Binary,
|
|
30
|
+
Double,
|
|
31
|
+
EJSON,
|
|
32
|
+
BSONRegExp,
|
|
33
|
+
calculateObjectSize,
|
|
34
|
+
};
|
|
35
|
+
export type BSON = {
|
|
36
|
+
ObjectId: typeof ObjectId;
|
|
37
|
+
DBRef: typeof DBRef;
|
|
38
|
+
MaxKey: typeof MaxKey;
|
|
39
|
+
MinKey: typeof MinKey;
|
|
40
|
+
Timestamp: typeof Timestamp;
|
|
41
|
+
Code: typeof Code;
|
|
42
|
+
Decimal128: typeof Decimal128;
|
|
43
|
+
Int32: typeof Int32;
|
|
44
|
+
Long: typeof Long;
|
|
45
|
+
Binary: typeof Binary;
|
|
46
|
+
Double: typeof Double;
|
|
47
|
+
EJSON: typeof EJSON;
|
|
48
|
+
BSONRegExp: typeof BSONRegExp;
|
|
49
|
+
BSONSymbol: typeof BSONSymbol;
|
|
50
|
+
calculateObjectSize: typeof calculateObjectSize;
|
|
51
|
+
};
|
package/src/helpers.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { MongoshInvalidInputError, CommonErrors } from '@mongosh/errors';
|
|
2
|
+
|
|
3
|
+
// Create a copy of a class so that it's constructible without `new`, i.e.
|
|
4
|
+
// class A {}; B = functionCtor(A);
|
|
5
|
+
// A() // throws
|
|
6
|
+
// B() // does not throw, returns instance of A
|
|
7
|
+
export function functionCtorWithoutProps<
|
|
8
|
+
T extends Function & { new (...args: any): any }
|
|
9
|
+
>(
|
|
10
|
+
ClassCtor: T
|
|
11
|
+
): {
|
|
12
|
+
new (...args: ConstructorParameters<T>): InstanceType<T>;
|
|
13
|
+
(...args: ConstructorParameters<T>): InstanceType<T>;
|
|
14
|
+
} {
|
|
15
|
+
function fnCtor(...args: any[]) {
|
|
16
|
+
if (new.target) {
|
|
17
|
+
return Reflect.construct(ClassCtor, args, new.target);
|
|
18
|
+
}
|
|
19
|
+
return new ClassCtor(...args);
|
|
20
|
+
}
|
|
21
|
+
Object.setPrototypeOf(fnCtor, Object.getPrototypeOf(ClassCtor));
|
|
22
|
+
const nameDescriptor = Object.getOwnPropertyDescriptor(ClassCtor, 'name');
|
|
23
|
+
if (nameDescriptor) {
|
|
24
|
+
Object.defineProperty(fnCtor, 'name', nameDescriptor);
|
|
25
|
+
}
|
|
26
|
+
return fnCtor as any;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function assignAll<T extends {}, U extends {}>(t: T, u: U): T & U;
|
|
30
|
+
export function assignAll<T extends {}, U extends {}, V extends {}>(
|
|
31
|
+
t: T,
|
|
32
|
+
u: U,
|
|
33
|
+
v: V
|
|
34
|
+
): T & U & V;
|
|
35
|
+
export function assignAll<
|
|
36
|
+
T extends {},
|
|
37
|
+
U extends {},
|
|
38
|
+
V extends {},
|
|
39
|
+
W extends {}
|
|
40
|
+
>(t: T, u: U, v: V, w: W): T & U & V & W;
|
|
41
|
+
export function assignAll(target: {}, ...sources: {}[]): any {
|
|
42
|
+
const newDescriptorList = [];
|
|
43
|
+
for (const source of sources) {
|
|
44
|
+
newDescriptorList.push(
|
|
45
|
+
...Object.entries(Object.getOwnPropertyDescriptors(source))
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
const newDescriptorMap = Object.fromEntries(newDescriptorList);
|
|
49
|
+
for (const key of Object.getOwnPropertyNames(newDescriptorMap)) {
|
|
50
|
+
if (Object.getOwnPropertyDescriptor(target, key)?.configurable === false) {
|
|
51
|
+
// e.g. .prototype can be written to but not re-defined
|
|
52
|
+
(target as any)[key] = newDescriptorMap[key].value;
|
|
53
|
+
delete newDescriptorMap[key];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
Object.defineProperties(target, newDescriptorMap);
|
|
57
|
+
|
|
58
|
+
return target;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// pick() but account for descriptor properties and ensure that the set of passed
|
|
62
|
+
// keys matches the public properties of O exactly
|
|
63
|
+
export function pickWithExactKeyMatch<
|
|
64
|
+
K extends string,
|
|
65
|
+
O extends Record<K, unknown>
|
|
66
|
+
>(o: Record<string, never> extends Omit<O, K> ? O : never, keys: K[]): O {
|
|
67
|
+
return Object.create(
|
|
68
|
+
Object.getPrototypeOf(o),
|
|
69
|
+
Object.fromEntries(
|
|
70
|
+
Object.entries(Object.getOwnPropertyDescriptors(o)).filter(([k]) =>
|
|
71
|
+
(keys as string[]).includes(k)
|
|
72
|
+
)
|
|
73
|
+
)
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function getAssertCaller(caller?: string): string {
|
|
78
|
+
return caller ? ` (${caller})` : '';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// NB: Duplicate of the one in packages/shell-api/src/helpers.ts
|
|
82
|
+
export function assertArgsDefinedType(
|
|
83
|
+
args: any[],
|
|
84
|
+
expectedTypes: Array<true | string | Array<string | undefined>>,
|
|
85
|
+
func?: string
|
|
86
|
+
): void {
|
|
87
|
+
args.forEach((arg, i) => {
|
|
88
|
+
const expected = expectedTypes[i];
|
|
89
|
+
if (arg === undefined) {
|
|
90
|
+
if (
|
|
91
|
+
expected !== true &&
|
|
92
|
+
Array.isArray(expected) &&
|
|
93
|
+
expected.includes(undefined)
|
|
94
|
+
) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
throw new MongoshInvalidInputError(
|
|
98
|
+
`Missing required argument at position ${i}${getAssertCaller(func)}`,
|
|
99
|
+
CommonErrors.InvalidArgument
|
|
100
|
+
);
|
|
101
|
+
} else if (expected === true) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const expectedTypesList: Array<string | undefined> =
|
|
106
|
+
typeof expected === 'string' ? [expected] : expected;
|
|
107
|
+
const isExpectedTypeof = expectedTypesList.includes(typeof arg);
|
|
108
|
+
const isExpectedBson = expectedTypesList.includes(`bson:${arg?._bsontype}`);
|
|
109
|
+
|
|
110
|
+
if (!isExpectedTypeof && !isExpectedBson) {
|
|
111
|
+
const expectedMsg = expectedTypesList
|
|
112
|
+
.filter((e) => e !== undefined)
|
|
113
|
+
.map((e) => e?.replace(/^bson:/, ''))
|
|
114
|
+
.join(' or ');
|
|
115
|
+
throw new MongoshInvalidInputError(
|
|
116
|
+
`Argument at position ${i} must be of type ${expectedMsg}, got ${typeof arg} instead${getAssertCaller(
|
|
117
|
+
func
|
|
118
|
+
)}`,
|
|
119
|
+
CommonErrors.InvalidArgument
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { expect } from 'chai';
|
|
2
|
+
import * as bson from 'bson';
|
|
3
|
+
import { inspect } from 'util';
|
|
4
|
+
import { makePrintableBson } from './printable-bson';
|
|
5
|
+
|
|
6
|
+
describe('BSON printers', function () {
|
|
7
|
+
before('make BSON objects printable', function () {
|
|
8
|
+
makePrintableBson(bson);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('formats ObjectIds correctly', function () {
|
|
12
|
+
expect(inspect(new bson.ObjectId('5fa5694f88211043b23c7f11'))).to.equal(
|
|
13
|
+
"ObjectId('5fa5694f88211043b23c7f11')"
|
|
14
|
+
);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('formats DBRefs correctly', function () {
|
|
18
|
+
expect(
|
|
19
|
+
inspect(
|
|
20
|
+
new bson.DBRef('a', new bson.ObjectId('5f16b8bebe434dc98cdfc9cb'), 'db')
|
|
21
|
+
)
|
|
22
|
+
).to.equal("DBRef('a', ObjectId('5f16b8bebe434dc98cdfc9cb'), 'db')");
|
|
23
|
+
expect(inspect(new bson.DBRef('a', 'foo' as any, 'db'))).to.equal(
|
|
24
|
+
"DBRef('a', 'foo', 'db')"
|
|
25
|
+
);
|
|
26
|
+
expect(inspect(new bson.DBRef('a', { x: 1 } as any, 'db'))).to.equal(
|
|
27
|
+
"DBRef('a', { x: 1 }, 'db')"
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('formats MinKey and MaxKey correctly', function () {
|
|
32
|
+
expect(inspect(new bson.MinKey())).to.equal('MinKey()');
|
|
33
|
+
expect(inspect(new bson.MaxKey())).to.equal('MaxKey()');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('formats NumberInt correctly', function () {
|
|
37
|
+
expect(inspect(new bson.Int32(32))).to.equal('Int32(32)');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('formats NumberLong correctly', function () {
|
|
41
|
+
expect(inspect(bson.Long.fromString('64'))).to.equal("Long('64')");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('formats unsigned NumberLong correctly', function () {
|
|
45
|
+
expect(inspect(bson.Long.fromString('64', true))).to.equal(
|
|
46
|
+
"Long('64', true)"
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('formats NumberDecimal correctly', function () {
|
|
51
|
+
expect(inspect(bson.Decimal128.fromString('1'))).to.equal(
|
|
52
|
+
"Decimal128('1')"
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('formats Timestamp correctly', function () {
|
|
57
|
+
expect(inspect(new bson.Timestamp(new bson.Long(100, 1)))).to.equal(
|
|
58
|
+
'Timestamp({ t: 1, i: 100 })'
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('formats Symbol correctly', function () {
|
|
63
|
+
expect(inspect(new bson.BSONSymbol('abc'))).to.equal("BSONSymbol('abc')");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('formats Code correctly', function () {
|
|
67
|
+
expect(inspect(new bson.Code('abc'))).to.equal("Code('abc')");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('formats BSONRegExp correctly', function () {
|
|
71
|
+
expect(inspect(new bson.BSONRegExp('(?-i)AA_', 'im'))).to.equal(
|
|
72
|
+
"BSONRegExp('(?-i)AA_', 'im')"
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('formats UUIDs correctly', function () {
|
|
77
|
+
expect(
|
|
78
|
+
inspect(
|
|
79
|
+
new bson.Binary(
|
|
80
|
+
Buffer.from('0123456789abcdef0123456789abcdef', 'hex'),
|
|
81
|
+
4
|
|
82
|
+
)
|
|
83
|
+
)
|
|
84
|
+
).to.equal("UUID('01234567-89ab-cdef-0123-456789abcdef')");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('formats MD5s correctly', function () {
|
|
88
|
+
expect(
|
|
89
|
+
inspect(
|
|
90
|
+
new bson.Binary(
|
|
91
|
+
Buffer.from('0123456789abcdef0123456789abcdef', 'hex'),
|
|
92
|
+
5
|
|
93
|
+
)
|
|
94
|
+
)
|
|
95
|
+
).to.equal("MD5('0123456789abcdef0123456789abcdef')");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('with Vector types', function () {
|
|
99
|
+
it('formats Int8Array correctly', function () {
|
|
100
|
+
expect(
|
|
101
|
+
inspect(bson.Binary.fromInt8Array(new Int8Array([1, 2, 3])))
|
|
102
|
+
).to.equal('Binary.fromInt8Array(new Int8Array([ 1, 2, 3 ]))');
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('formats PackedBits correctly', function () {
|
|
106
|
+
expect(
|
|
107
|
+
inspect(bson.Binary.fromPackedBits(new Uint8Array([1, 2, 3])))
|
|
108
|
+
).to.equal('Binary.fromPackedBits(new Uint8Array([ 1, 2, 3 ]))');
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('formats PackedBits correctly with padding', function () {
|
|
112
|
+
expect(
|
|
113
|
+
inspect(bson.Binary.fromPackedBits(new Uint8Array([1, 2, 3]), 7))
|
|
114
|
+
).to.equal('Binary.fromPackedBits(new Uint8Array([ 1, 2, 3 ]), 7)');
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('formats Float32Array correctly', function () {
|
|
118
|
+
expect(
|
|
119
|
+
inspect(
|
|
120
|
+
bson.Binary.fromFloat32Array(
|
|
121
|
+
new Float32Array([1.1111, 2.2222, 3.3333])
|
|
122
|
+
),
|
|
123
|
+
{ compact: true }
|
|
124
|
+
)
|
|
125
|
+
).matches(
|
|
126
|
+
// Precision is lost because of float handling, so we use regex to match
|
|
127
|
+
/Binary.fromFloat32Array\(new Float32Array\(\[ 1\.1\d*, 2\.2\d*, 3\.3\d* \]\)\)/
|
|
128
|
+
);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('formats any other value with the new format using createfromBase64', function () {
|
|
133
|
+
expect(
|
|
134
|
+
inspect(bson.Binary.createFromBase64('SGVsbG8sIFdvcmxkIQo='))
|
|
135
|
+
).to.equal("Binary.createFromBase64('SGVsbG8sIFdvcmxkIQo=', 0)");
|
|
136
|
+
});
|
|
137
|
+
});
|