bson 4.0.4 → 4.2.2
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/HISTORY.md +56 -1
- package/README.md +7 -9
- package/bower.json +1 -1
- package/bson.d.ts +983 -0
- package/dist/bson.browser.esm.js +7261 -5004
- package/dist/bson.browser.esm.js.map +1 -0
- package/dist/bson.browser.umd.js +7319 -5099
- package/dist/bson.browser.umd.js.map +1 -0
- package/dist/bson.bundle.js +8168 -9216
- package/dist/bson.bundle.js.map +1 -0
- package/dist/bson.esm.js +5643 -5409
- package/dist/bson.esm.js.map +1 -0
- package/etc/prepare.js +19 -0
- package/lib/binary.js +194 -377
- package/lib/binary.js.map +1 -0
- package/lib/bson.js +200 -243
- package/lib/bson.js.map +1 -0
- package/lib/code.js +36 -39
- package/lib/code.js.map +1 -0
- package/lib/constants.js +78 -203
- package/lib/constants.js.map +1 -0
- package/lib/db_ref.js +79 -79
- package/lib/db_ref.js.map +1 -0
- package/lib/decimal128.js +647 -760
- package/lib/decimal128.js.map +1 -0
- package/lib/double.js +61 -58
- package/lib/double.js.map +1 -0
- package/lib/ensure_buffer.js +22 -18
- package/lib/ensure_buffer.js.map +1 -0
- package/lib/extended_json.js +305 -322
- package/lib/extended_json.js.map +1 -0
- package/lib/float_parser.js +98 -104
- package/lib/float_parser.js.map +1 -0
- package/lib/int_32.js +45 -47
- package/lib/int_32.js.map +1 -0
- package/lib/long.js +876 -16
- package/lib/long.js.map +1 -0
- package/lib/map.js +123 -124
- package/lib/map.js.map +1 -0
- package/lib/max_key.js +21 -23
- package/lib/max_key.js.map +1 -0
- package/lib/min_key.js +21 -23
- package/lib/min_key.js.map +1 -0
- package/lib/objectid.js +264 -382
- package/lib/objectid.js.map +1 -0
- package/lib/parser/calculate_size.js +185 -224
- package/lib/parser/calculate_size.js.map +1 -0
- package/lib/parser/deserializer.js +543 -620
- package/lib/parser/deserializer.js.map +1 -0
- package/lib/parser/serializer.js +774 -918
- package/lib/parser/serializer.js.map +1 -0
- package/lib/parser/utils.js +81 -30
- package/lib/parser/utils.js.map +1 -0
- package/lib/regexp.js +54 -70
- package/lib/regexp.js.map +1 -0
- package/lib/symbol.js +40 -56
- package/lib/symbol.js.map +1 -0
- package/lib/timestamp.js +70 -95
- package/lib/timestamp.js.map +1 -0
- package/lib/uuid.js +48 -0
- package/lib/uuid.js.map +1 -0
- package/lib/validate_utf8.js +32 -33
- package/lib/validate_utf8.js.map +1 -0
- package/package.json +53 -31
- package/src/binary.ts +270 -0
- package/src/bson.ts +326 -0
- package/src/code.ts +57 -0
- package/src/constants.ts +104 -0
- package/src/db_ref.ts +115 -0
- package/src/decimal128.ts +801 -0
- package/src/double.ts +85 -0
- package/src/ensure_buffer.ts +26 -0
- package/src/extended_json.ts +395 -0
- package/src/float_parser.ts +152 -0
- package/src/int_32.ts +64 -0
- package/src/long.ts +1000 -0
- package/src/map.ts +139 -0
- package/src/max_key.ts +33 -0
- package/src/min_key.ts +33 -0
- package/src/objectid.ts +377 -0
- package/src/parser/calculate_size.ts +230 -0
- package/src/parser/deserializer.ts +655 -0
- package/src/parser/serializer.ts +1069 -0
- package/src/parser/utils.ts +93 -0
- package/src/regexp.ts +92 -0
- package/src/symbol.ts +57 -0
- package/src/timestamp.ts +103 -0
- package/src/uuid.ts +57 -0
- package/src/validate_utf8.ts +47 -0
- package/lib/fnv1a.js +0 -48
package/lib/uuid.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseUUID = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* UUID regular expression pattern copied from `uuid` npm module.
|
|
6
|
+
* @see https://github.com/uuidjs/uuid/blob/master/src/regex.js
|
|
7
|
+
*/
|
|
8
|
+
const UUID_RX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
|
|
9
|
+
/**
|
|
10
|
+
* Parser function copied from `uuid` npm module.
|
|
11
|
+
* @see https://github.com/uuidjs/uuid/blob/master/src/parse.js
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
function parseUUID(uuid) {
|
|
15
|
+
if (typeof uuid !== 'string') {
|
|
16
|
+
throw new TypeError('Invalid type for UUID, expected string but got ' + typeof uuid);
|
|
17
|
+
}
|
|
18
|
+
if (!UUID_RX.test(uuid)) {
|
|
19
|
+
throw new TypeError('Invalid format for UUID: ' + uuid);
|
|
20
|
+
}
|
|
21
|
+
let v;
|
|
22
|
+
const arr = new Uint8Array(16);
|
|
23
|
+
// Parse ########-....-....-....-............
|
|
24
|
+
arr[0] = (v = parseInt(uuid.slice(0, 8), 16)) >>> 24;
|
|
25
|
+
arr[1] = (v >>> 16) & 0xff;
|
|
26
|
+
arr[2] = (v >>> 8) & 0xff;
|
|
27
|
+
arr[3] = v & 0xff;
|
|
28
|
+
// Parse ........-####-....-....-............
|
|
29
|
+
arr[4] = (v = parseInt(uuid.slice(9, 13), 16)) >>> 8;
|
|
30
|
+
arr[5] = v & 0xff;
|
|
31
|
+
// Parse ........-....-####-....-............
|
|
32
|
+
arr[6] = (v = parseInt(uuid.slice(14, 18), 16)) >>> 8;
|
|
33
|
+
arr[7] = v & 0xff;
|
|
34
|
+
// Parse ........-....-....-####-............
|
|
35
|
+
arr[8] = (v = parseInt(uuid.slice(19, 23), 16)) >>> 8;
|
|
36
|
+
arr[9] = v & 0xff;
|
|
37
|
+
// Parse ........-....-....-....-############
|
|
38
|
+
// (Use "/" to avoid 32-bit truncation when bit-shifting high-order bytes)
|
|
39
|
+
arr[10] = ((v = parseInt(uuid.slice(24, 36), 16)) / 0x10000000000) & 0xff;
|
|
40
|
+
arr[11] = (v / 0x100000000) & 0xff;
|
|
41
|
+
arr[12] = (v >>> 24) & 0xff;
|
|
42
|
+
arr[13] = (v >>> 16) & 0xff;
|
|
43
|
+
arr[14] = (v >>> 8) & 0xff;
|
|
44
|
+
arr[15] = v & 0xff;
|
|
45
|
+
return arr;
|
|
46
|
+
}
|
|
47
|
+
exports.parseUUID = parseUUID;
|
|
48
|
+
//# sourceMappingURL=uuid.js.map
|
package/lib/uuid.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"uuid.js","sourceRoot":"","sources":["../src/uuid.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACH,MAAM,OAAO,GAAG,qHAAqH,CAAC;AAOtI;;;;GAIG;AACH,SAAgB,SAAS,CAAC,IAAY;IACpC,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;QAC5B,MAAM,IAAI,SAAS,CAAC,iDAAiD,GAAG,OAAO,IAAI,CAAC,CAAC;KACtF;IAED,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;QACvB,MAAM,IAAI,SAAS,CAAC,2BAA2B,GAAG,IAAI,CAAC,CAAC;KACzD;IAED,IAAI,CAAC,CAAC;IACN,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IAE/B,6CAA6C;IAC7C,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC;IACrD,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IAC3B,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,IAAI,CAAC;IAC1B,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IAElB,6CAA6C;IAC7C,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;IACrD,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IAElB,6CAA6C;IAC7C,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;IACtD,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IAElB,6CAA6C;IAC7C,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;IACtD,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IAElB,6CAA6C;IAC7C,0EAA0E;IAC1E,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,aAAa,CAAC,GAAG,IAAI,CAAC;IAC1E,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,GAAG,WAAW,CAAC,GAAG,IAAI,CAAC;IACnC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IAC5B,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IAC5B,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,IAAI,CAAC;IAC3B,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IAEnB,OAAO,GAAG,CAAC;AACb,CAAC;AAxCD,8BAwCC"}
|
package/lib/validate_utf8.js
CHANGED
|
@@ -1,48 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateUtf8 = void 0;
|
|
3
4
|
const FIRST_BIT = 0x80;
|
|
4
5
|
const FIRST_TWO_BITS = 0xc0;
|
|
5
6
|
const FIRST_THREE_BITS = 0xe0;
|
|
6
7
|
const FIRST_FOUR_BITS = 0xf0;
|
|
7
8
|
const FIRST_FIVE_BITS = 0xf8;
|
|
8
|
-
|
|
9
9
|
const TWO_BIT_CHAR = 0xc0;
|
|
10
10
|
const THREE_BIT_CHAR = 0xe0;
|
|
11
11
|
const FOUR_BIT_CHAR = 0xf0;
|
|
12
12
|
const CONTINUING_CHAR = 0x80;
|
|
13
|
-
|
|
14
13
|
/**
|
|
15
14
|
* Determines if the passed in bytes are valid utf8
|
|
16
|
-
* @param
|
|
17
|
-
* @param
|
|
18
|
-
* @param
|
|
19
|
-
* @returns {boolean} True if valid utf8
|
|
15
|
+
* @param bytes - An array of 8-bit bytes. Must be indexable and have length property
|
|
16
|
+
* @param start - The index to start validating
|
|
17
|
+
* @param end - The index to end validating
|
|
20
18
|
*/
|
|
21
19
|
function validateUtf8(bytes, start, end) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
20
|
+
let continuation = 0;
|
|
21
|
+
for (let i = start; i < end; i += 1) {
|
|
22
|
+
const byte = bytes[i];
|
|
23
|
+
if (continuation) {
|
|
24
|
+
if ((byte & FIRST_TWO_BITS) !== CONTINUING_CHAR) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
continuation -= 1;
|
|
28
|
+
}
|
|
29
|
+
else if (byte & FIRST_BIT) {
|
|
30
|
+
if ((byte & FIRST_THREE_BITS) === TWO_BIT_CHAR) {
|
|
31
|
+
continuation = 1;
|
|
32
|
+
}
|
|
33
|
+
else if ((byte & FIRST_FOUR_BITS) === THREE_BIT_CHAR) {
|
|
34
|
+
continuation = 2;
|
|
35
|
+
}
|
|
36
|
+
else if ((byte & FIRST_FIVE_BITS) === FOUR_BIT_CHAR) {
|
|
37
|
+
continuation = 3;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
42
43
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return !continuation;
|
|
44
|
+
return !continuation;
|
|
46
45
|
}
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
exports.validateUtf8 = validateUtf8;
|
|
47
|
+
//# sourceMappingURL=validate_utf8.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate_utf8.js","sourceRoot":"","sources":["../src/validate_utf8.ts"],"names":[],"mappings":";;;AAAA,MAAM,SAAS,GAAG,IAAI,CAAC;AACvB,MAAM,cAAc,GAAG,IAAI,CAAC;AAC5B,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAC9B,MAAM,eAAe,GAAG,IAAI,CAAC;AAC7B,MAAM,eAAe,GAAG,IAAI,CAAC;AAE7B,MAAM,YAAY,GAAG,IAAI,CAAC;AAC1B,MAAM,cAAc,GAAG,IAAI,CAAC;AAC5B,MAAM,aAAa,GAAG,IAAI,CAAC;AAC3B,MAAM,eAAe,GAAG,IAAI,CAAC;AAE7B;;;;;GAKG;AACH,SAAgB,YAAY,CAC1B,KAAkC,EAClC,KAAa,EACb,GAAW;IAEX,IAAI,YAAY,GAAG,CAAC,CAAC;IAErB,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE;QACnC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QAEtB,IAAI,YAAY,EAAE;YAChB,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC,KAAK,eAAe,EAAE;gBAC/C,OAAO,KAAK,CAAC;aACd;YACD,YAAY,IAAI,CAAC,CAAC;SACnB;aAAM,IAAI,IAAI,GAAG,SAAS,EAAE;YAC3B,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC,KAAK,YAAY,EAAE;gBAC9C,YAAY,GAAG,CAAC,CAAC;aAClB;iBAAM,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC,KAAK,cAAc,EAAE;gBACtD,YAAY,GAAG,CAAC,CAAC;aAClB;iBAAM,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC,KAAK,aAAa,EAAE;gBACrD,YAAY,GAAG,CAAC,CAAC;aAClB;iBAAM;gBACL,OAAO,KAAK,CAAC;aACd;SACF;KACF;IAED,OAAO,CAAC,YAAY,CAAC;AACvB,CAAC;AA7BD,oCA6BC"}
|
package/package.json
CHANGED
|
@@ -8,10 +8,14 @@
|
|
|
8
8
|
],
|
|
9
9
|
"files": [
|
|
10
10
|
"lib",
|
|
11
|
+
"src",
|
|
11
12
|
"dist",
|
|
13
|
+
"bson.d.ts",
|
|
14
|
+
"etc/prepare.js",
|
|
12
15
|
"bower.json"
|
|
13
16
|
],
|
|
14
|
-
"
|
|
17
|
+
"types": "bson.d.ts",
|
|
18
|
+
"version": "4.2.2",
|
|
15
19
|
"author": "Christian Amor Kvalheim <christkv@gmail.com>",
|
|
16
20
|
"license": "Apache-2.0",
|
|
17
21
|
"contributors": [],
|
|
@@ -21,30 +25,45 @@
|
|
|
21
25
|
"url": "https://github.com/mongodb/js-bson/issues"
|
|
22
26
|
},
|
|
23
27
|
"devDependencies": {
|
|
24
|
-
"@babel/
|
|
25
|
-
"@babel/preset-env": "^7.
|
|
28
|
+
"@babel/plugin-external-helpers": "^7.10.4",
|
|
29
|
+
"@babel/preset-env": "^7.11.0",
|
|
30
|
+
"@istanbuljs/nyc-config-typescript": "^1.0.1",
|
|
31
|
+
"@microsoft/api-extractor": "^7.11.2",
|
|
32
|
+
"@rollup/plugin-babel": "^5.2.0",
|
|
33
|
+
"@rollup/plugin-commonjs": "^15.0.0",
|
|
34
|
+
"@rollup/plugin-json": "^4.1.0",
|
|
35
|
+
"@rollup/plugin-node-resolve": "^9.0.0",
|
|
36
|
+
"@rollup/plugin-typescript": "^6.0.0",
|
|
37
|
+
"@typescript-eslint/eslint-plugin": "^3.10.1",
|
|
38
|
+
"@typescript-eslint/parser": "^3.10.1",
|
|
26
39
|
"benchmark": "^2.1.4",
|
|
27
40
|
"chai": "^4.2.0",
|
|
28
|
-
"
|
|
29
|
-
"eslint": "^
|
|
30
|
-
"eslint-
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"karma": "^
|
|
34
|
-
"karma-
|
|
35
|
-
"karma-
|
|
41
|
+
"downlevel-dts": "^0.7.0",
|
|
42
|
+
"eslint": "^7.7.0",
|
|
43
|
+
"eslint-config-prettier": "^6.11.0",
|
|
44
|
+
"eslint-plugin-prettier": "^3.1.4",
|
|
45
|
+
"eslint-plugin-tsdoc": "^0.2.6",
|
|
46
|
+
"karma": "^5.1.1",
|
|
47
|
+
"karma-chai": "^0.1.0",
|
|
48
|
+
"karma-chrome-launcher": "^3.1.0",
|
|
49
|
+
"karma-mocha": "^2.0.1",
|
|
36
50
|
"karma-mocha-reporter": "^2.2.5",
|
|
37
|
-
"karma-rollup-preprocessor": "^
|
|
38
|
-
"mocha": "
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"rollup
|
|
51
|
+
"karma-rollup-preprocessor": "^7.0.5",
|
|
52
|
+
"mocha": "5.2.0",
|
|
53
|
+
"node-fetch": "^2.6.1",
|
|
54
|
+
"nyc": "^15.1.0",
|
|
55
|
+
"prettier": "^2.1.1",
|
|
56
|
+
"rimraf": "^3.0.2",
|
|
57
|
+
"rollup": "^2.26.5",
|
|
58
|
+
"rollup-plugin-commonjs": "^10.1.0",
|
|
44
59
|
"rollup-plugin-node-builtins": "^2.1.2",
|
|
45
60
|
"rollup-plugin-node-globals": "^1.4.0",
|
|
46
|
-
"rollup-plugin-node-
|
|
47
|
-
"standard-version": "^
|
|
61
|
+
"rollup-plugin-node-polyfills": "^0.2.1",
|
|
62
|
+
"standard-version": "^8.0.1",
|
|
63
|
+
"ts-node": "^9.0.0",
|
|
64
|
+
"typedoc": "^0.18.0",
|
|
65
|
+
"typescript": "^4.0.2",
|
|
66
|
+
"typescript-cached-transpile": "0.0.6"
|
|
48
67
|
},
|
|
49
68
|
"config": {
|
|
50
69
|
"native": false
|
|
@@ -59,19 +78,22 @@
|
|
|
59
78
|
"node": ">=6.9.0"
|
|
60
79
|
},
|
|
61
80
|
"scripts": {
|
|
62
|
-
"docs": "
|
|
63
|
-
"test": "npm run
|
|
64
|
-
"test-node": "
|
|
65
|
-
"test-browser": "
|
|
66
|
-
"build": "
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
81
|
+
"docs": "typedoc",
|
|
82
|
+
"test": "npm run build && npm run test-node && npm run test-browser",
|
|
83
|
+
"test-node": "mocha test/node test/*_tests.js",
|
|
84
|
+
"test-browser": "karma start karma.conf.js",
|
|
85
|
+
"build:ts": "tsc",
|
|
86
|
+
"build:dts": "npm run build:ts && api-extractor run --typescript-compiler-folder node_modules/typescript --local && rimraf 'lib/**/*.d.ts*' && downlevel-dts bson.d.ts bson.d.ts",
|
|
87
|
+
"build:bundle": "rollup -c rollup.config.js",
|
|
88
|
+
"build": "npm run build:dts && npm run build:bundle",
|
|
89
|
+
"lint": "eslint -v && eslint --ext '.js,.ts' --max-warnings=0 src test && tsc -v && tsc --noEmit",
|
|
90
|
+
"format": "eslint --ext '.js,.ts' src test --fix",
|
|
91
|
+
"coverage": "nyc npm run test-node",
|
|
92
|
+
"coverage:html": "npm run coverage && open ./coverage/index.html",
|
|
93
|
+
"prepare": "node etc/prepare.js",
|
|
71
94
|
"release": "standard-version -i HISTORY.md"
|
|
72
95
|
},
|
|
73
96
|
"dependencies": {
|
|
74
|
-
"buffer": "^5.
|
|
75
|
-
"long": "^4.0.0"
|
|
97
|
+
"buffer": "^5.6.0"
|
|
76
98
|
}
|
|
77
99
|
}
|
package/src/binary.ts
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
2
|
+
import { ensureBuffer } from './ensure_buffer';
|
|
3
|
+
import type { EJSONOptions } from './extended_json';
|
|
4
|
+
import { parseUUID, UUIDExtended } from './uuid';
|
|
5
|
+
|
|
6
|
+
/** @public */
|
|
7
|
+
export type BinarySequence = Uint8Array | Buffer | number[];
|
|
8
|
+
|
|
9
|
+
/** @public */
|
|
10
|
+
export interface BinaryExtendedLegacy {
|
|
11
|
+
$type: string;
|
|
12
|
+
$binary: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** @public */
|
|
16
|
+
export interface BinaryExtended {
|
|
17
|
+
$binary: {
|
|
18
|
+
subType: string;
|
|
19
|
+
base64: string;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A class representation of the BSON Binary type.
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export class Binary {
|
|
28
|
+
_bsontype!: 'Binary';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Binary default subtype
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
private static readonly BSON_BINARY_SUBTYPE_DEFAULT = 0;
|
|
35
|
+
|
|
36
|
+
/** Initial buffer default size */
|
|
37
|
+
static readonly BUFFER_SIZE = 256;
|
|
38
|
+
/** Default BSON type */
|
|
39
|
+
static readonly SUBTYPE_DEFAULT = 0;
|
|
40
|
+
/** Function BSON type */
|
|
41
|
+
static readonly SUBTYPE_FUNCTION = 1;
|
|
42
|
+
/** Byte Array BSON type */
|
|
43
|
+
static readonly SUBTYPE_BYTE_ARRAY = 2;
|
|
44
|
+
/** Deprecated UUID BSON type @deprecated Please use SUBTYPE_UUID */
|
|
45
|
+
static readonly SUBTYPE_UUID_OLD = 3;
|
|
46
|
+
/** UUID BSON type */
|
|
47
|
+
static readonly SUBTYPE_UUID = 4;
|
|
48
|
+
/** MD5 BSON type */
|
|
49
|
+
static readonly SUBTYPE_MD5 = 5;
|
|
50
|
+
/** User BSON type */
|
|
51
|
+
static readonly SUBTYPE_USER_DEFINED = 128;
|
|
52
|
+
|
|
53
|
+
buffer: Buffer;
|
|
54
|
+
sub_type: number;
|
|
55
|
+
position: number;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param buffer - a buffer object containing the binary data.
|
|
59
|
+
* @param subType - the option binary type.
|
|
60
|
+
*/
|
|
61
|
+
constructor(buffer?: string | BinarySequence, subType?: number) {
|
|
62
|
+
if (
|
|
63
|
+
!(buffer == null) &&
|
|
64
|
+
!(typeof buffer === 'string') &&
|
|
65
|
+
!ArrayBuffer.isView(buffer) &&
|
|
66
|
+
!(buffer instanceof ArrayBuffer) &&
|
|
67
|
+
!Array.isArray(buffer)
|
|
68
|
+
) {
|
|
69
|
+
throw new TypeError(
|
|
70
|
+
'Binary can only be constructed from string, Buffer, TypedArray, or Array<number>'
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
|
|
75
|
+
|
|
76
|
+
if (buffer == null) {
|
|
77
|
+
// create an empty binary buffer
|
|
78
|
+
this.buffer = Buffer.alloc(Binary.BUFFER_SIZE);
|
|
79
|
+
this.position = 0;
|
|
80
|
+
} else {
|
|
81
|
+
if (typeof buffer === 'string') {
|
|
82
|
+
// string
|
|
83
|
+
this.buffer = Buffer.from(buffer, 'binary');
|
|
84
|
+
} else if (Array.isArray(buffer)) {
|
|
85
|
+
// number[]
|
|
86
|
+
this.buffer = Buffer.from(buffer);
|
|
87
|
+
} else {
|
|
88
|
+
// Buffer | TypedArray | ArrayBuffer
|
|
89
|
+
this.buffer = ensureBuffer(buffer);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
this.position = this.buffer.byteLength;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Updates this binary with byte_value.
|
|
98
|
+
*
|
|
99
|
+
* @param byteValue - a single byte we wish to write.
|
|
100
|
+
*/
|
|
101
|
+
put(byteValue: string | number | Uint8Array | Buffer | number[]): void {
|
|
102
|
+
// If it's a string and a has more than one character throw an error
|
|
103
|
+
if (typeof byteValue === 'string' && byteValue.length !== 1) {
|
|
104
|
+
throw new TypeError('only accepts single character String');
|
|
105
|
+
} else if (typeof byteValue !== 'number' && byteValue.length !== 1)
|
|
106
|
+
throw new TypeError('only accepts single character Uint8Array or Array');
|
|
107
|
+
|
|
108
|
+
// Decode the byte value once
|
|
109
|
+
let decodedByte: number;
|
|
110
|
+
if (typeof byteValue === 'string') {
|
|
111
|
+
decodedByte = byteValue.charCodeAt(0);
|
|
112
|
+
} else if (typeof byteValue === 'number') {
|
|
113
|
+
decodedByte = byteValue;
|
|
114
|
+
} else {
|
|
115
|
+
decodedByte = byteValue[0];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (decodedByte < 0 || decodedByte > 255) {
|
|
119
|
+
throw new TypeError('only accepts number in a valid unsigned byte range 0-255');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (this.buffer.length > this.position) {
|
|
123
|
+
this.buffer[this.position++] = decodedByte;
|
|
124
|
+
} else {
|
|
125
|
+
const buffer = Buffer.alloc(Binary.BUFFER_SIZE + this.buffer.length);
|
|
126
|
+
// Combine the two buffers together
|
|
127
|
+
this.buffer.copy(buffer, 0, 0, this.buffer.length);
|
|
128
|
+
this.buffer = buffer;
|
|
129
|
+
this.buffer[this.position++] = decodedByte;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Writes a buffer or string to the binary.
|
|
135
|
+
*
|
|
136
|
+
* @param sequence - a string or buffer to be written to the Binary BSON object.
|
|
137
|
+
* @param offset - specify the binary of where to write the content.
|
|
138
|
+
*/
|
|
139
|
+
write(sequence: string | BinarySequence, offset: number): void {
|
|
140
|
+
offset = typeof offset === 'number' ? offset : this.position;
|
|
141
|
+
|
|
142
|
+
// If the buffer is to small let's extend the buffer
|
|
143
|
+
if (this.buffer.length < offset + sequence.length) {
|
|
144
|
+
const buffer = Buffer.alloc(this.buffer.length + sequence.length);
|
|
145
|
+
this.buffer.copy(buffer, 0, 0, this.buffer.length);
|
|
146
|
+
|
|
147
|
+
// Assign the new buffer
|
|
148
|
+
this.buffer = buffer;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (ArrayBuffer.isView(sequence)) {
|
|
152
|
+
this.buffer.set(ensureBuffer(sequence), offset);
|
|
153
|
+
this.position =
|
|
154
|
+
offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
|
|
155
|
+
} else if (typeof sequence === 'string') {
|
|
156
|
+
this.buffer.write(sequence, offset, sequence.length, 'binary');
|
|
157
|
+
this.position =
|
|
158
|
+
offset + sequence.length > this.position ? offset + sequence.length : this.position;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Reads **length** bytes starting at **position**.
|
|
164
|
+
*
|
|
165
|
+
* @param position - read from the given position in the Binary.
|
|
166
|
+
* @param length - the number of bytes to read.
|
|
167
|
+
*/
|
|
168
|
+
read(position: number, length: number): BinarySequence {
|
|
169
|
+
length = length && length > 0 ? length : this.position;
|
|
170
|
+
|
|
171
|
+
// Let's return the data based on the type we have
|
|
172
|
+
return this.buffer.slice(position, position + length);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Returns the value of this binary as a string.
|
|
177
|
+
* @param asRaw - Will skip converting to a string
|
|
178
|
+
* @remarks
|
|
179
|
+
* This is handy when calling this function conditionally for some key value pairs and not others
|
|
180
|
+
*/
|
|
181
|
+
value(asRaw?: boolean): string | BinarySequence {
|
|
182
|
+
asRaw = !!asRaw;
|
|
183
|
+
|
|
184
|
+
// Optimize to serialize for the situation where the data == size of buffer
|
|
185
|
+
if (asRaw && this.buffer.length === this.position) {
|
|
186
|
+
return this.buffer;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// If it's a node.js buffer object
|
|
190
|
+
if (asRaw) {
|
|
191
|
+
return this.buffer.slice(0, this.position);
|
|
192
|
+
}
|
|
193
|
+
return this.buffer.toString('binary', 0, this.position);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** the length of the binary sequence */
|
|
197
|
+
length(): number {
|
|
198
|
+
return this.position;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** @internal */
|
|
202
|
+
toJSON(): string {
|
|
203
|
+
return this.buffer.toString('base64');
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** @internal */
|
|
207
|
+
toString(format: string): string {
|
|
208
|
+
return this.buffer.toString(format);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** @internal */
|
|
212
|
+
toExtendedJSON(options?: EJSONOptions): BinaryExtendedLegacy | BinaryExtended {
|
|
213
|
+
options = options || {};
|
|
214
|
+
const base64String = this.buffer.toString('base64');
|
|
215
|
+
|
|
216
|
+
const subType = Number(this.sub_type).toString(16);
|
|
217
|
+
if (options.legacy) {
|
|
218
|
+
return {
|
|
219
|
+
$binary: base64String,
|
|
220
|
+
$type: subType.length === 1 ? '0' + subType : subType
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
return {
|
|
224
|
+
$binary: {
|
|
225
|
+
base64: base64String,
|
|
226
|
+
subType: subType.length === 1 ? '0' + subType : subType
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** @internal */
|
|
232
|
+
static fromExtendedJSON(
|
|
233
|
+
doc: BinaryExtendedLegacy | BinaryExtended | UUIDExtended,
|
|
234
|
+
options?: EJSONOptions
|
|
235
|
+
): Binary {
|
|
236
|
+
options = options || {};
|
|
237
|
+
let data: Buffer | undefined;
|
|
238
|
+
let type;
|
|
239
|
+
if ('$binary' in doc) {
|
|
240
|
+
if (options.legacy && typeof doc.$binary === 'string' && '$type' in doc) {
|
|
241
|
+
type = doc.$type ? parseInt(doc.$type, 16) : 0;
|
|
242
|
+
data = Buffer.from(doc.$binary, 'base64');
|
|
243
|
+
} else {
|
|
244
|
+
if (typeof doc.$binary !== 'string') {
|
|
245
|
+
type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0;
|
|
246
|
+
data = Buffer.from(doc.$binary.base64, 'base64');
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
} else if ('$uuid' in doc) {
|
|
250
|
+
type = 4;
|
|
251
|
+
data = Buffer.from(parseUUID(doc.$uuid));
|
|
252
|
+
}
|
|
253
|
+
if (!data) {
|
|
254
|
+
throw new TypeError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
|
|
255
|
+
}
|
|
256
|
+
return new Binary(data, type);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** @internal */
|
|
260
|
+
[Symbol.for('nodejs.util.inspect.custom')](): string {
|
|
261
|
+
return this.inspect();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
inspect(): string {
|
|
265
|
+
const asBuffer = this.value(true);
|
|
266
|
+
return `Binary("${asBuffer.toString('hex')}", ${this.sub_type})`;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
Object.defineProperty(Binary.prototype, '_bsontype', { value: 'Binary' });
|