Initial commit

This commit is contained in:
2025-03-07 19:22:02 +01:00
commit 4a98255d83
55743 changed files with 5280367 additions and 0 deletions
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2024 jsonjoy.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+93
View File
@@ -0,0 +1,93 @@
# Base64
Fast Base64 encoder and decoder for browser and Node.js.
## Encoder
- Implements Base64 encoding algorithm compatible with Node's Buffer.
- Isomorphic—it can be used in, both, Node and the browser.
- Faster than the Node's implementation for short blobs, smaller than 40 bytes.
- Uses Node's implementation for long blobs, if available. Hence, it also works
in browser, but in Node environment will perform faster for short strings.
- Can encode into Base64 text or Base64 `Uint8Array`.
### Usage
Use encoder compatible with Node's Buffer:
```ts
import {toBase64} from '@jsonjoy.com/base64';
toBase64(new Uint8Array([1, 2, 3]));
```
Create your custom encoder:
```ts
import {createToBase64} from '@jsonjoy.com/base64';
const encode = createToBase64('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_');
encode(new Uint8Array([1, 2, 3]));
```
### Benchmark
Below benchmark encodes random binary blobs of sizes 8, 16, 32, 64, 128, 256, 512, and 1024 byes.
`@jsonjoy.com/base64` is faster, because for short strings (less than 40 chars) it uses a
native JavaScript implementation, which is faster and also works in browsers. For blobs larger
than 40 chars, it falls back to Node `Buffer` implementation, if available.
Encoding:
```
node src/__bench__/encode.js
util/base64 toBase64(uint8) x 1,531,283 ops/sec ±0.30% (92 runs sampled), 653 ns/op
util/base64 createToBase64()(uint8) x 946,364 ops/sec ±0.76% (100 runs sampled), 1057 ns/op
js-base64 x 1,103,190 ops/sec ±1.27% (96 runs sampled), 906 ns/op
fast-base64-encode x 500,225 ops/sec ±0.64% (96 runs sampled), 1999 ns/op
base64-js x 328,368 ops/sec ±0.25% (95 runs sampled), 3045 ns/op
Buffer.from(uint8).toString('base64'); x 1,099,420 ops/sec ±0.20% (100 runs sampled), 910 ns/op
Fastest is util/base64 toBase64(uint8)
```
Decoding:
```
node src/__bench__/decode.js
@jsonjoy.com/base64 fromBase64(str) x 756,989 ops/sec ±0.46% (97 runs sampled), 1321 ns/op
@jsonjoy.com/base64 createFromBase64()(str) x 475,591 ops/sec ±0.37% (96 runs sampled), 2103 ns/op
Buffer.from(str, 'base64') x 545,012 ops/sec ±0.33% (101 runs sampled), 1835 ns/op
base64-js x 487,015 ops/sec ±1.19% (94 runs sampled), 2053 ns/op
js-base64 x 173,049 ops/sec ±0.20% (99 runs sampled), 5779 ns/op
Fastest is @jsonjoy.com/base64 fromBase64(str)
```
## Decoder
- Uses Node.js built-in `Buffer`, if available.
- When `Buffer` is not available, uses JavaScript implementation.
### Usage
Use decoder compatible with Node's Buffer:
```ts
import {toBase64, fromBase64} from '@jsonjoy.com/base64';
fromBase64(toBase64(new Uint8Array([1, 2, 3])));
```
Create your custom encoder:
```ts
import {createFromBase64} from '@jsonjoy.com/base64';
const decoder = createFromBase64('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_');
decoder(toBase64(new Uint8Array([1, 2, 3])));
```
+2
View File
@@ -0,0 +1,2 @@
export declare const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
export declare const hasBuffer: boolean;
+6
View File
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasBuffer = exports.alphabet = void 0;
exports.alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
exports.hasBuffer = typeof Buffer === 'function' && typeof Buffer.from === 'function';
//# sourceMappingURL=constants.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,QAAQ,GAAG,kEAAkE,CAAC;AAC9E,QAAA,SAAS,GAAG,OAAO,MAAM,KAAK,UAAU,IAAI,OAAO,MAAM,CAAC,IAAI,KAAK,UAAU,CAAC"}
+1
View File
@@ -0,0 +1 @@
export declare const createFromBase64: (chars?: string, noPadding?: boolean) => (encoded: string) => Uint8Array;
+80
View File
@@ -0,0 +1,80 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFromBase64 = void 0;
const constants_1 = require("./constants");
const E = '=';
const createFromBase64 = (chars = constants_1.alphabet, noPadding = false) => {
if (chars.length !== 64)
throw new Error('chars must be 64 characters long');
let max = 0;
for (let i = 0; i < chars.length; i++)
max = Math.max(max, chars.charCodeAt(i));
const table = [];
for (let i = 0; i <= max; i += 1)
table[i] = -1;
for (let i = 0; i < chars.length; i++)
table[chars.charCodeAt(i)] = i;
return (encoded) => {
if (!encoded)
return new Uint8Array(0);
let length = encoded.length;
if (noPadding) {
const mod = length % 4;
if (mod === 2) {
encoded += '==';
length += 2;
}
else if (mod === 3) {
encoded += '=';
length += 1;
}
}
if (length % 4 !== 0)
throw new Error('Base64 string length must be a multiple of 4');
const mainLength = encoded[length - 1] !== E ? length : length - 4;
let bufferLength = (length >> 2) * 3;
let padding = 0;
if (encoded[length - 2] === E) {
padding = 2;
bufferLength -= 2;
}
else if (encoded[length - 1] === E) {
padding = 1;
bufferLength -= 1;
}
const buf = new Uint8Array(bufferLength);
let j = 0;
let i = 0;
for (; i < mainLength; i += 4) {
const sextet0 = table[encoded.charCodeAt(i)];
const sextet1 = table[encoded.charCodeAt(i + 1)];
const sextet2 = table[encoded.charCodeAt(i + 2)];
const sextet3 = table[encoded.charCodeAt(i + 3)];
if (sextet0 < 0 || sextet1 < 0 || sextet2 < 0 || sextet3 < 0)
throw new Error('INVALID_BASE64_STRING');
buf[j] = (sextet0 << 2) | (sextet1 >> 4);
buf[j + 1] = (sextet1 << 4) | (sextet2 >> 2);
buf[j + 2] = (sextet2 << 6) | sextet3;
j += 3;
}
if (padding === 2) {
const sextet0 = table[encoded.charCodeAt(mainLength)];
const sextet1 = table[encoded.charCodeAt(mainLength + 1)];
if (sextet0 < 0 || sextet1 < 0)
throw new Error('INVALID_BASE64_STRING');
buf[j] = (sextet0 << 2) | (sextet1 >> 4);
}
else if (padding === 1) {
const sextet0 = table[encoded.charCodeAt(mainLength)];
const sextet1 = table[encoded.charCodeAt(mainLength + 1)];
const sextet2 = table[encoded.charCodeAt(mainLength + 2)];
if (sextet0 < 0 || sextet1 < 0 || sextet2 < 0)
throw new Error('INVALID_BASE64_STRING');
buf[j] = (sextet0 << 2) | (sextet1 >> 4);
buf[j + 1] = (sextet1 << 4) | (sextet2 >> 2);
}
return buf;
};
};
exports.createFromBase64 = createFromBase64;
//# sourceMappingURL=createFromBase64.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"createFromBase64.js","sourceRoot":"","sources":["../src/createFromBase64.ts"],"names":[],"mappings":";;;AAAA,2CAAqC;AAErC,MAAM,CAAC,GAAG,GAAG,CAAC;AAEP,MAAM,gBAAgB,GAAG,CAAC,QAAgB,oBAAQ,EAAE,YAAqB,KAAK,EAAE,EAAE;IACvF,IAAI,KAAK,CAAC,MAAM,KAAK,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;IAC7E,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE;QAAE,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;IAChF,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3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AC;AA7DW,QAAA,gBAAgB,oBA6D3B"}
+1
View File
@@ -0,0 +1 @@
export declare const createFromBase64Bin: (chars?: string, pad?: string) => (view: DataView, offset: number, length: number) => Uint8Array;
+87
View File
@@ -0,0 +1,87 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFromBase64Bin = void 0;
const constants_1 = require("./constants");
const createFromBase64Bin = (chars = constants_1.alphabet, pad = '=') => {
if (chars.length !== 64)
throw new Error('chars must be 64 characters long');
let max = 0;
for (let i = 0; i < chars.length; i++)
max = Math.max(max, chars.charCodeAt(i));
const table = [];
for (let i = 0; i <= max; i += 1)
table[i] = -1;
for (let i = 0; i < chars.length; i++)
table[chars.charCodeAt(i)] = i;
const doExpectPadding = pad.length === 1;
const PAD = doExpectPadding ? pad.charCodeAt(0) : 0;
return (view, offset, length) => {
if (!length)
return new Uint8Array(0);
let padding = 0;
if (length % 4 !== 0) {
padding = 4 - (length % 4);
length += padding;
}
else {
const end = offset + length;
const last = end - 1;
if (view.getUint8(last) === PAD) {
padding = 1;
if (length > 1 && view.getUint8(last - 1) === PAD)
padding = 2;
}
}
if (length % 4 !== 0)
throw new Error('Base64 string length must be a multiple of 4');
const mainEnd = offset + length - (padding ? 4 : 0);
const bufferLength = (length >> 2) * 3 - padding;
const buf = new Uint8Array(bufferLength);
let j = 0;
let i = offset;
for (; i < mainEnd; i += 4) {
const word = view.getUint32(i);
const octet0 = word >>> 24;
const octet1 = (word >>> 16) & 0xff;
const octet2 = (word >>> 8) & 0xff;
const octet3 = word & 0xff;
const sextet0 = table[octet0];
const sextet1 = table[octet1];
const sextet2 = table[octet2];
const sextet3 = table[octet3];
if (sextet0 < 0 || sextet1 < 0 || sextet2 < 0 || sextet3 < 0)
throw new Error('INVALID_BASE64_SEQ');
buf[j] = (sextet0 << 2) | (sextet1 >> 4);
buf[j + 1] = (sextet1 << 4) | (sextet2 >> 2);
buf[j + 2] = (sextet2 << 6) | sextet3;
j += 3;
}
if (!padding)
return buf;
if (padding === 1) {
const word = view.getUint16(mainEnd);
const octet0 = word >> 8;
const octet1 = word & 0xff;
const octet2 = view.getUint8(mainEnd + 2);
const sextet0 = table[octet0];
const sextet1 = table[octet1];
const sextet2 = table[octet2];
if (sextet0 < 0 || sextet1 < 0 || sextet2 < 0)
throw new Error('INVALID_BASE64_SEQ');
buf[j] = (sextet0 << 2) | (sextet1 >> 4);
buf[j + 1] = (sextet1 << 4) | (sextet2 >> 2);
return buf;
}
const word = view.getUint16(mainEnd);
const octet0 = word >> 8;
const octet1 = word & 0xff;
const sextet0 = table[octet0];
const sextet1 = table[octet1];
if (sextet0 < 0 || sextet1 < 0)
throw new Error('INVALID_BASE64_SEQ');
buf[j] = (sextet0 << 2) | (sextet1 >> 4);
return buf;
};
};
exports.createFromBase64Bin = createFromBase64Bin;
//# sourceMappingURL=createFromBase64Bin.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"createFromBase64Bin.js","sourceRoot":"","sources":["../src/createFromBase64Bin.ts"],"names":[],"mappings":";;;AAAA,2CAAqC;AAE9B,MAAM,mBAAmB,GAAG,CAAC,QAAgB,oBAAQ,EAAE,MAAc,GAAG,EAAE,EAAE;IACjF,IAAI,KAAK,CAAC,MAAM,KAAK,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;IAC7E,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE;QAAE,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;IAChF,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,IAAI,CAAC;QAAE,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IAChD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AC9B,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC;QAC9B,IAAI,OAAO,GAAG,CAAC,IAAI,OAAO,GAAG,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;QACtE,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,IAAI,CAAC,CAAC,GAAG,CAAC,OAAO,IAAI,CAAC,CAAC,CAAC;QACzC,OAAO,GAAG,CAAC;IACb,CAAC,CAAC;AACJ,CAAC,CAAC;AAtEW,QAAA,mBAAmB,uBAsE9B"}
+1
View File
@@ -0,0 +1 @@
export declare const createToBase64: (chars?: string, pad?: string) => (uint8: Uint8Array, length: number) => string;
+48
View File
@@ -0,0 +1,48 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createToBase64 = void 0;
const flatstr_1 = require("./util/strings/flatstr");
const constants_1 = require("./constants");
const createToBase64 = (chars = constants_1.alphabet, pad = '=') => {
if (chars.length !== 64)
throw new Error('chars must be 64 characters long');
const table = chars.split('');
const table2 = [];
for (const c1 of table) {
for (const c2 of table) {
const two = (0, flatstr_1.flatstr)(c1 + c2);
table2.push(two);
}
}
const E = pad;
const EE = (0, flatstr_1.flatstr)(pad + pad);
return (uint8, length) => {
let out = '';
const extraLength = length % 3;
const baseLength = length - extraLength;
for (let i = 0; i < baseLength; i += 3) {
const o1 = uint8[i];
const o2 = uint8[i + 1];
const o3 = uint8[i + 2];
const v1 = (o1 << 4) | (o2 >> 4);
const v2 = ((o2 & 0b1111) << 8) | o3;
out += table2[v1] + table2[v2];
}
if (!extraLength)
return out;
if (extraLength === 1) {
const o1 = uint8[baseLength];
out += table2[o1 << 4] + EE;
}
else {
const o1 = uint8[baseLength];
const o2 = uint8[baseLength + 1];
const v1 = (o1 << 4) | (o2 >> 4);
const v2 = (o2 & 0b1111) << 2;
out += table2[v1] + table[v2] + E;
}
return out;
};
};
exports.createToBase64 = createToBase64;
//# sourceMappingURL=createToBase64.js.map
+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GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;YACjC,MAAM,EAAE,GAAG,CAAC,CAAC,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC;YACrC,GAAG,IAAI,MAAM,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;QACjC,CAAC;QACD,IAAI,CAAC,WAAW;YAAE,OAAO,GAAG,CAAC;QAC7B,IAAI,WAAW,KAAK,CAAC,EAAE,CAAC;YACtB,MAAM,EAAE,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC;YAC7B,GAAG,IAAI,MAAM,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC;QAC9B,CAAC;aAAM,CAAC;YACN,MAAM,EAAE,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC;YAC7B,MAAM,EAAE,GAAG,KAAK,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC;YACjC,MAAM,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;YACjC,MAAM,EAAE,GAAG,CAAC,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;YAC9B,GAAG,IAAI,MAAM,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC;QACpC,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC,CAAC;AACJ,CAAC,CAAC;AAzCW,QAAA,cAAc,kBAyCzB"}
+1
View File
@@ -0,0 +1 @@
export declare const createToBase64Bin: (chars?: string, pad?: string) => (uint8: Uint8Array, start: number, length: number, dest: DataView, offset: number) => number;
+62
View File
@@ -0,0 +1,62 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createToBase64Bin = void 0;
const constants_1 = require("./constants");
const createToBase64Bin = (chars = constants_1.alphabet, pad = '=') => {
if (chars.length !== 64)
throw new Error('chars must be 64 characters long');
const table = chars.split('').map((c) => c.charCodeAt(0));
const table2 = [];
for (const c1 of table) {
for (const c2 of table) {
const two = (c1 << 8) + c2;
table2.push(two);
}
}
const doAddPadding = pad.length === 1;
const E = doAddPadding ? pad.charCodeAt(0) : 0;
const EE = doAddPadding ? (E << 8) | E : 0;
return (uint8, start, length, dest, offset) => {
const extraLength = length % 3;
const baseLength = length - extraLength;
for (; start < baseLength; start += 3) {
const o1 = uint8[start];
const o2 = uint8[start + 1];
const o3 = uint8[start + 2];
const v1 = (o1 << 4) | (o2 >> 4);
const v2 = ((o2 & 0b1111) << 8) | o3;
dest.setInt32(offset, (table2[v1] << 16) + table2[v2]);
offset += 4;
}
if (extraLength === 1) {
const o1 = uint8[baseLength];
if (doAddPadding) {
dest.setInt32(offset, (table2[o1 << 4] << 16) + EE);
offset += 4;
}
else {
dest.setInt16(offset, table2[o1 << 4]);
offset += 2;
}
}
else if (extraLength) {
const o1 = uint8[baseLength];
const o2 = uint8[baseLength + 1];
const v1 = (o1 << 4) | (o2 >> 4);
const v2 = (o2 & 0b1111) << 2;
if (doAddPadding) {
dest.setInt32(offset, (table2[v1] << 16) + (table[v2] << 8) + E);
offset += 4;
}
else {
dest.setInt16(offset, table2[v1]);
offset += 2;
dest.setInt8(offset, table[v2]);
offset += 1;
}
}
return offset;
};
};
exports.createToBase64Bin = createToBase64Bin;
//# sourceMappingURL=createToBase64Bin.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"createToBase64Bin.js","sourceRoot":"","sources":["../src/createToBase64Bin.ts"],"names":[],"mappings":";;;AAAA,2CAAqC;AAE9B,MAAM,iBAAiB,GAAG,CAAC,QAAgB,oBAAQ,EAAE,MAAc,GAAG,EAAE,EAAE;IAC/E,IAAI,KAAK,CAAC,MAAM,KAAK,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;IAE7E,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,C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AAC;gBACjE,MAAM,IAAI,CAAC,CAAC;YACd,CAAC;iBAAM,CAAC;gBACN,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC;gBAClC,MAAM,IAAI,CAAC,CAAC;gBACZ,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;gBAChC,MAAM,IAAI,CAAC,CAAC;YACd,CAAC;QACH,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC,CAAC;AAvDW,QAAA,iBAAiB,qBAuD5B"}
+1
View File
@@ -0,0 +1 @@
export declare const createToBase64BinUint8: (chars?: string, pad?: string) => (uint8: Uint8Array, start: number, length: number, dest: Uint8Array, offset: number) => number;
+59
View File
@@ -0,0 +1,59 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createToBase64BinUint8 = void 0;
const constants_1 = require("./constants");
const createToBase64BinUint8 = (chars = constants_1.alphabet, pad = '=') => {
if (chars.length !== 64)
throw new Error('chars must be 64 characters long');
const table = chars.split('').map((c) => c.charCodeAt(0));
const table2 = [];
for (const c1 of table) {
for (const c2 of table) {
const two = (c1 << 8) + c2;
table2.push(two);
}
}
const PAD = pad.length === 1 ? pad.charCodeAt(0) : 0;
return (uint8, start, length, dest, offset) => {
const extraLength = length % 3;
const baseLength = length - extraLength;
for (; start < baseLength; start += 3) {
const o1 = uint8[start];
const o2 = uint8[start + 1];
const o3 = uint8[start + 2];
const v1 = (o1 << 4) | (o2 >> 4);
const v2 = ((o2 & 0b1111) << 8) | o3;
let u16 = table2[v1];
dest[offset++] = u16 >> 8;
dest[offset++] = u16;
u16 = table2[v2];
dest[offset++] = u16 >> 8;
dest[offset++] = u16;
}
if (extraLength === 1) {
const o1 = uint8[baseLength];
const u16 = table2[o1 << 4];
dest[offset++] = u16 >> 8;
dest[offset++] = u16;
if (PAD) {
dest[offset++] = PAD;
dest[offset++] = PAD;
}
}
else if (extraLength) {
const o1 = uint8[baseLength];
const o2 = uint8[baseLength + 1];
const v1 = (o1 << 4) | (o2 >> 4);
const v2 = (o2 & 0b1111) << 2;
const u16 = table2[v1];
dest[offset++] = u16 >> 8;
dest[offset++] = u16;
dest[offset++] = table[v2];
if (PAD)
dest[offset++] = PAD;
}
return offset;
};
};
exports.createToBase64BinUint8 = createToBase64BinUint8;
//# sourceMappingURL=createToBase64BinUint8.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"createToBase64BinUint8.js","sourceRoot":"","sources":["../src/createToBase64BinUint8.ts"],"names":[],"mappings":";;;AAAA,2CAAqC;AAE9B,MAAM,sBAAsB,GAAG,CAAC,QAAgB,oBAAQ,EAAE,MAAc,GAAG,EAAE,EAAE;IACpF,IAAI,KAAK,CAAC,MAAM,KAAK,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;IAE7E,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1D,MAAM,MAAM,GAAa,EAAE,CAAC;IAE5B,KAAK,MAAM,EAAE,IAAI,KAAK,EAAE,CAAC;QACvB,KAAK,MAAM,EAAE,IAAI,KAAK,EAAE,CAAC;YACvB,MAAM,GAAG,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC;YAC3B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACnB,CAAC;IACH,CAAC;IAED,MAAM,GAAG,GAAW,GAAG,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAE7D,OAAO,CAAC,KAAiB,EAAE,KAAa,EAAE,MAAc,EAAE,IAAgB,EAAE,MAAc,EAAU,EA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,CAAC,MAAM,EAAE,CAAC,GAAG,GAAG,CAAC;gBACrB,IAAI,CAAC,MAAM,EAAE,CAAC,GAAG,GAAG,CAAC;YACvB,CAAC;QACH,CAAC;aAAM,IAAI,WAAW,EAAE,CAAC;YACvB,MAAM,EAAE,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC;YAC7B,MAAM,EAAE,GAAG,KAAK,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC;YACjC,MAAM,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;YACjC,MAAM,EAAE,GAAG,CAAC,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;YAC9B,MAAM,GAAG,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC;YACvB,IAAI,CAAC,MAAM,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC;YAC1B,IAAI,CAAC,MAAM,EAAE,CAAC,GAAG,GAAG,CAAC;YACrB,IAAI,CAAC,MAAM,EAAE,CAAC,GAAG,KAAK,CAAC,EAAE,CAAC,CAAC;YAC3B,IAAI,GAAG;gBAAE,IAAI,CAAC,MAAM,EAAE,CAAC,GAAG,GAAG,CAAC;QAChC,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC,CAAC;AArDW,QAAA,sBAAsB,0BAqDjC"}
+1
View File
@@ -0,0 +1 @@
export declare const fromBase64: (encoded: string) => Uint8Array;
+12
View File
@@ -0,0 +1,12 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fromBase64 = void 0;
const bufferToUint8Array_1 = require("./util/buffers/bufferToUint8Array");
const constants_1 = require("./constants");
const createFromBase64_1 = require("./createFromBase64");
const fromBase64Cpp = constants_1.hasBuffer ? (encoded) => (0, bufferToUint8Array_1.bufferToUint8Array)(Buffer.from(encoded, 'base64')) : null;
const fromBase64Native = (0, createFromBase64_1.createFromBase64)();
exports.fromBase64 = !fromBase64Cpp
? fromBase64Native
: (encoded) => (encoded.length > 48 ? fromBase64Cpp(encoded) : fromBase64Native(encoded));
//# sourceMappingURL=fromBase64.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"fromBase64.js","sourceRoot":"","sources":["../src/fromBase64.ts"],"names":[],"mappings":";;;AAAA,0EAAqE;AACrE,2CAAsC;AACtC,yDAAoD;AAEpD,MAAM,aAAa,GAAG,qBAAS,CAAC,CAAC,CAAC,CAAC,OAAe,EAAE,EAAE,CAAC,IAAA,uCAAkB,EAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AACjH,MAAM,gBAAgB,GAAG,IAAA,mCAAgB,GAAE,CAAC;AAE/B,QAAA,UAAU,GAAG,CAAC,aAAa;IACtC,CAAC,CAAC,gBAAgB;IAClB,CAAC,CAAC,CAAC,OAAe,EAAc,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC"}
+1
View File
@@ -0,0 +1 @@
export declare const fromBase64Bin: (view: DataView, offset: number, length: number) => Uint8Array;
+6
View File
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fromBase64Bin = void 0;
const createFromBase64Bin_1 = require("./createFromBase64Bin");
exports.fromBase64Bin = (0, createFromBase64Bin_1.createFromBase64Bin)();
//# sourceMappingURL=fromBase64Bin.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"fromBase64Bin.js","sourceRoot":"","sources":["../src/fromBase64Bin.ts"],"names":[],"mappings":";;;AAAA,+DAA0D;AAE7C,QAAA,aAAa,GAAG,IAAA,yCAAmB,GAAE,CAAC"}
+1
View File
@@ -0,0 +1 @@
export declare const fromBase64Url: (encoded: string) => Uint8Array;
+6
View File
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fromBase64Url = void 0;
const createFromBase64_1 = require("./createFromBase64");
exports.fromBase64Url = (0, createFromBase64_1.createFromBase64)('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_', true);
//# sourceMappingURL=fromBase64Url.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"fromBase64Url.js","sourceRoot":"","sources":["../src/fromBase64Url.ts"],"names":[],"mappings":";;;AAAA,yDAAoD;AAEvC,QAAA,aAAa,GAAG,IAAA,mCAAgB,EAAC,kEAAkE,EAAE,IAAI,CAAC,CAAC"}
+7
View File
@@ -0,0 +1,7 @@
export * from './createToBase64';
export * from './createToBase64Bin';
export * from './createFromBase64';
export * from './toBase64';
export * from './toBase64Bin';
export * from './fromBase64';
export * from './fromBase64Bin';
+11
View File
@@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
tslib_1.__exportStar(require("./createToBase64"), exports);
tslib_1.__exportStar(require("./createToBase64Bin"), exports);
tslib_1.__exportStar(require("./createFromBase64"), exports);
tslib_1.__exportStar(require("./toBase64"), exports);
tslib_1.__exportStar(require("./toBase64Bin"), exports);
tslib_1.__exportStar(require("./fromBase64"), exports);
tslib_1.__exportStar(require("./fromBase64Bin"), exports);
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,2DAAiC;AACjC,8DAAoC;AACpC,6DAAmC;AACnC,qDAA2B;AAC3B,wDAA8B;AAC9B,uDAA6B;AAC7B,0DAAgC"}
+1
View File
@@ -0,0 +1 @@
export declare const toBase64: (uint8: Uint8Array) => string;
+15
View File
@@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toBase64 = void 0;
const constants_1 = require("./constants");
const createToBase64_1 = require("./createToBase64");
const encodeSmall = (0, createToBase64_1.createToBase64)();
exports.toBase64 = !constants_1.hasBuffer
? (uint8) => encodeSmall(uint8, uint8.length)
: (uint8) => {
const length = uint8.length;
if (length <= 48)
return encodeSmall(uint8, length);
return Buffer.from(uint8).toString('base64');
};
//# sourceMappingURL=toBase64.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"toBase64.js","sourceRoot":"","sources":["../src/toBase64.ts"],"names":[],"mappings":";;;AAAA,2CAAsC;AACtC,qDAAgD;AAEhD,MAAM,WAAW,GAAG,IAAA,+BAAc,GAAE,CAAC;AAExB,QAAA,QAAQ,GAAG,CAAC,qBAAS;IAChC,CAAC,CAAC,CAAC,KAAiB,EAAE,EAAE,CAAC,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC;IACzD,CAAC,CAAC,CAAC,KAAiB,EAAU,EAAE;QAC5B,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;QAC5B,IAAI,MAAM,IAAI,EAAE;YAAE,OAAO,WAAW,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QACpD,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAC/C,CAAC,CAAC"}
+1
View File
@@ -0,0 +1 @@
export declare const toBase64Bin: (uint8: Uint8Array, start: number, length: number, dest: DataView, offset: number) => number;
+6
View File
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toBase64Bin = void 0;
const createToBase64Bin_1 = require("./createToBase64Bin");
exports.toBase64Bin = (0, createToBase64Bin_1.createToBase64Bin)();
//# sourceMappingURL=toBase64Bin.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"toBase64Bin.js","sourceRoot":"","sources":["../src/toBase64Bin.ts"],"names":[],"mappings":";;;AAAA,2DAAsD;AAEzC,QAAA,WAAW,GAAG,IAAA,qCAAiB,GAAE,CAAC"}
+1
View File
@@ -0,0 +1 @@
export declare const toBase64Url: (uint8: Uint8Array, length: number) => string;
+6
View File
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toBase64Url = void 0;
const createToBase64_1 = require("./createToBase64");
exports.toBase64Url = (0, createToBase64_1.createToBase64)('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_', '');
//# sourceMappingURL=toBase64Url.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"toBase64Url.js","sourceRoot":"","sources":["../src/toBase64Url.ts"],"names":[],"mappings":";;;AAAA,qDAAgD;AAEnC,QAAA,WAAW,GAAG,IAAA,+BAAc,EAAC,kEAAkE,EAAE,EAAE,CAAC,CAAC"}
+1
View File
@@ -0,0 +1 @@
export declare const b: (...args: number[]) => Uint8Array;
+6
View File
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.b = void 0;
const b = (...args) => new Uint8Array(args);
exports.b = b;
//# sourceMappingURL=b.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"b.js","sourceRoot":"","sources":["../../../src/util/buffers/b.ts"],"names":[],"mappings":";;;AAAO,MAAM,CAAC,GAAG,CAAC,GAAG,IAAc,EAAE,EAAE,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;AAAhD,QAAA,CAAC,KAA+C"}
@@ -0,0 +1,2 @@
/// <reference types="node" />
export declare const bufferToUint8Array: (buf: Buffer) => Uint8Array;
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bufferToUint8Array = void 0;
const bufferToUint8Array = (buf) => new Uint8Array(buf.buffer, buf.byteOffset, buf.length);
exports.bufferToUint8Array = bufferToUint8Array;
//# sourceMappingURL=bufferToUint8Array.js.map
@@ -0,0 +1 @@
{"version":3,"file":"bufferToUint8Array.js","sourceRoot":"","sources":["../../../src/util/buffers/bufferToUint8Array.ts"],"names":[],"mappings":";;;AAAO,MAAM,kBAAkB,GAAG,CAAC,GAAW,EAAc,EAAE,CAAC,IAAI,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;AAAzG,QAAA,kBAAkB,sBAAuF"}
+1
View File
@@ -0,0 +1 @@
export declare const copy: <T extends Uint8Array>(arr: T) => T;
+10
View File
@@ -0,0 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.copy = void 0;
const copy = (arr) => {
const dupe = new Uint8Array(arr.length);
dupe.set(arr);
return dupe;
};
exports.copy = copy;
//# sourceMappingURL=copy.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"copy.js","sourceRoot":"","sources":["../../../src/util/buffers/copy.ts"],"names":[],"mappings":";;;AAAO,MAAM,IAAI,GAAG,CAAuB,GAAM,EAAK,EAAE;IACtD,MAAM,IAAI,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,MAAM,CAAM,CAAC;IAC7C,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACd,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAJW,QAAA,IAAI,QAIf"}
+1
View File
@@ -0,0 +1 @@
export declare const flatstr: (s: string) => string;
+10
View File
@@ -0,0 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.flatstr = void 0;
const flatstr = (s) => {
s | 0;
Number(s);
return s;
};
exports.flatstr = flatstr;
//# sourceMappingURL=flatstr.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"flatstr.js","sourceRoot":"","sources":["../../../src/util/strings/flatstr.ts"],"names":[],"mappings":";;;AAAO,MAAM,OAAO,GAAG,CAAC,CAAS,EAAU,EAAE;IACrC,CAAE,GAAG,CAAC,CAAC;IACb,MAAM,CAAC,CAAC,CAAC,CAAC;IACV,OAAO,CAAC,CAAC;AACX,CAAC,CAAC;AAJW,QAAA,OAAO,WAIlB"}
+109
View File
@@ -0,0 +1,109 @@
{
"name": "@jsonjoy.com/base64",
"private": false,
"publishConfig": {
"access": "public"
},
"version": "1.1.2",
"description": "High-performance Base64 encoder and decoder",
"author": {
"name": "streamich",
"url": "https://github.com/streamich"
},
"homepage": "https://github.com/jsonjoy-com/base64",
"repository": "jsonjoy-com/base64",
"license": "Apache-2.0",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/streamich"
},
"keywords": [
"base64",
"base64url",
"base64-url",
"base64urlsafe",
"base64url-safe"
],
"engines": {
"node": ">=10.0"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
"typings": "lib/index.d.ts",
"files": [
"LICENSE",
"lib/"
],
"scripts": {
"prettier": "prettier --ignore-path .gitignore --write \"src/**/*.{ts,tsx,js,jsx}\"",
"prettier:check": "prettier --ignore-path .gitignore --list-different 'src/**/*.{ts,tsx,js,jsx}'",
"lint": "yarn tslint",
"tslint": "tslint 'src/**/*.{js,jsx,ts,tsx}' -t verbose --project .",
"clean": "rimraf lib typedocs coverage gh-pages yarn-error.log",
"build": "tsc --project tsconfig.build.json --module commonjs --target es2020 --outDir lib",
"jest": "node -r ts-node/register ./node_modules/.bin/jest",
"test": "jest --maxWorkers 7",
"test:ci": "yarn jest --maxWorkers 3 --no-cache",
"coverage": "yarn test --collectCoverage",
"typedoc": "typedoc",
"build:pages": "rimraf gh-pages && mkdir -p gh-pages && cp -r typedocs/* gh-pages && cp -r coverage gh-pages/coverage",
"deploy:pages": "gh-pages -d gh-pages",
"publish-coverage-and-typedocs": "yarn typedoc && yarn coverage && yarn build:pages && yarn deploy:pages"
},
"peerDependencies": {
"tslib": "2"
},
"dependencies": {},
"devDependencies": {
"@types/benchmark": "^2.1.2",
"@types/jest": "^29.5.12",
"base64-js": "^1.5.1",
"benchmark": "^2.1.4",
"jest": "^29.7.0",
"js-base64": "^3.7.2",
"prettier": "^3.2.5",
"pretty-quick": "^3.1.3",
"rimraf": "^5.0.0",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"tslib": "^2.6.2",
"tslint": "^6.1.3",
"tslint-config-common": "^1.6.2",
"typedoc": "^0.25.12",
"typescript": "^5.4.4"
},
"jest": {
"verbose": true,
"testEnvironmentOptions": {
"url": "http://localhost/"
},
"setupFiles": [
"<rootDir>/src/__tests__/setup.js"
],
"moduleFileExtensions": [
"ts",
"js"
],
"transform": {
"^.+\\.ts$": "ts-jest"
},
"transformIgnorePatterns": [],
"testRegex": ".*/(__tests__|__jest__|demo)/.*\\.(test|spec)\\.ts$"
},
"prettier": {
"arrowParens": "always",
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": false
},
"release": {
"branches": [
"master",
"next"
]
}
}
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2024 jsonjoy.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+432
View File
@@ -0,0 +1,432 @@
# json-pack
High performance JSON serialization and deserialization library for JavaScript, Node.js, browser.
`json-pack` contains implementations of various JSON codecs into binary,
formats, such as MessagePack, CBOR and other formats.
- [__MessagePack__](./src/msgpack/README.md)
- [__CBOR__](./src/cbor/README.md)
- DAG-CBOR
- JSON
- DAG-JSON
- UBJSON
- Amazon Ion
- RESP3
- Bencode
## Benchmarks
Encoding:
```
npx ts-node benchmarks/json-pack/bench.encoding.ts
=============================================================================== Benchmark: Encoding
Warmup: 1000x , Node.js: v20.0.0 , Arch: arm64 , CPU: Apple M1
---------------------------------------------------------------------------- Small object, 44 bytes
🤞 json-pack JsonEncoder x 5,385,617 ops/sec ±0.53% (100 runs sampled)
🤞 Buffer.from(JSON.stringify()) x 2,254,954 ops/sec ±0.83% (97 runs sampled)
🤞 json-pack CborEncoderFast x 5,953,159 ops/sec ±1.12% (92 runs sampled)
🤞 json-pack CborEncoder x 6,248,036 ops/sec ±0.29% (98 runs sampled)
🤞 json-pack MsgPackEncoderFast x 3,121,940 ops/sec ±0.16% (99 runs sampled)
🤞 JSON.stringify() x 3,866,116 ops/sec ±0.11% (101 runs sampled)
🤞 @msgpack/msgpack x 1,406,546 ops/sec ±0.94% (93 runs sampled)
🤞 msgpackr x 2,404,916 ops/sec ±3.22% (86 runs sampled)
🤞 cbor-x x 4,737,433 ops/sec ±1.00% (97 runs sampled)
🤞 msgpack-lite x 987,201 ops/sec ±2.84% (91 runs sampled)
🤞 msgpack5 x 197,867 ops/sec ±3.65% (84 runs sampled)
🤞 messagepack x 171,865 ops/sec ±4.44% (74 runs sampled)
Fastest is 🤞 json-pack CborEncoder
------------------------------------------------------------------------- Typical object, 993 bytes
🤞 json-pack JsonEncoder x 299,970 ops/sec ±0.30% (97 runs sampled)
🤞 Buffer.from(JSON.stringify()) x 211,651 ops/sec ±0.18% (100 runs sampled)
🤞 json-pack CborEncoderFast x 429,535 ops/sec ±3.38% (93 runs sampled)
🤞 json-pack CborEncoder x 428,848 ops/sec ±0.71% (97 runs sampled)
🤞 json-pack MsgPackEncoderFast x 322,982 ops/sec ±0.67% (97 runs sampled)
🤞 JSON.stringify() x 306,828 ops/sec ±1.94% (90 runs sampled)
🤞 @msgpack/msgpack x 199,937 ops/sec ±5.52% (93 runs sampled)
🤞 msgpackr x 317,457 ops/sec ±2.18% (90 runs sampled)
🤞 cbor-x x 401,854 ops/sec ±3.20% (92 runs sampled)
🤞 msgpack-lite x 135,110 ops/sec ±1.29% (94 runs sampled)
🤞 msgpack5 x 15,217 ops/sec ±3.72% (85 runs sampled)
🤞 messagepack x 13,853 ops/sec ±4.73% (71 runs sampled)
Fastest is 🤞 json-pack CborEncoder
-------------------------------------------------------------------------- Large object, 3741 bytes
🤞 json-pack JsonEncoder x 87,312 ops/sec ±1.10% (96 runs sampled)
🤞 Buffer.from(JSON.stringify()) x 64,718 ops/sec ±0.45% (96 runs sampled)
🤞 json-pack CborEncoderFast x 134,615 ops/sec ±0.19% (97 runs sampled)
🤞 json-pack CborEncoder x 128,975 ops/sec ±0.20% (98 runs sampled)
🤞 json-pack MsgPackEncoderFast x 103,325 ops/sec ±1.62% (98 runs sampled)
🤞 JSON.stringify() x 101,067 ops/sec ±1.36% (95 runs sampled)
🤞 @msgpack/msgpack x 61,715 ops/sec ±0.22% (98 runs sampled)
🤞 msgpackr x 95,175 ops/sec ±3.84% (95 runs sampled)
🤞 cbor-x x 111,658 ops/sec ±1.34% (95 runs sampled)
🤞 msgpack-lite x 41,364 ops/sec ±0.28% (100 runs sampled)
🤞 msgpack5 x 3,262 ops/sec ±4.32% (71 runs sampled)
🤞 messagepack x 4,167 ops/sec ±7.29% (65 runs sampled)
Fastest is 🤞 json-pack CborEncoderFast
-------------------------------------------------------------------- Very large object, 45750 bytes
🤞 json-pack JsonEncoder x 5,687 ops/sec ±1.92% (94 runs sampled)
🤞 Buffer.from(JSON.stringify()) x 5,813 ops/sec ±2.51% (97 runs sampled)
🤞 json-pack CborEncoderFast x 5,749 ops/sec ±0.67% (98 runs sampled)
🤞 json-pack CborEncoder x 5,515 ops/sec ±0.70% (98 runs sampled)
🤞 json-pack MsgPackEncoderFast x 5,027 ops/sec ±0.19% (100 runs sampled)
🤞 JSON.stringify() x 7,687 ops/sec ±0.87% (99 runs sampled)
🤞 @msgpack/msgpack x 3,379 ops/sec ±2.20% (97 runs sampled)
🤞 msgpackr x 5,929 ops/sec ±15.26% (96 runs sampled)
🤞 cbor-x x 5,032 ops/sec ±5.17% (90 runs sampled)
🤞 msgpack-lite x 2,173 ops/sec ±1.17% (97 runs sampled)
🤞 msgpack5 x 179 ops/sec ±2.95% (68 runs sampled)
🤞 messagepack x 167 ops/sec ±1.09% (79 runs sampled)
Fastest is 🤞 JSON.stringify()
------------------------------------------------------------------ Object with many keys, 969 bytes
🤞 json-pack JsonEncoder x 213,447 ops/sec ±3.31% (95 runs sampled)
🤞 Buffer.from(JSON.stringify()) x 168,303 ops/sec ±2.13% (95 runs sampled)
🤞 json-pack CborEncoderFast x 275,511 ops/sec ±0.40% (95 runs sampled)
🤞 json-pack CborEncoder x 270,949 ops/sec ±0.32% (97 runs sampled)
🤞 json-pack MsgPackEncoderFast x 210,525 ops/sec ±0.66% (99 runs sampled)
🤞 JSON.stringify() x 200,767 ops/sec ±0.19% (101 runs sampled)
🤞 @msgpack/msgpack x 163,665 ops/sec ±0.81% (98 runs sampled)
🤞 msgpackr x 151,889 ops/sec ±0.27% (96 runs sampled)
🤞 cbor-x x 191,010 ops/sec ±0.44% (96 runs sampled)
🤞 msgpack-lite x 93,537 ops/sec ±0.68% (99 runs sampled)
🤞 msgpack5 x 28,581 ops/sec ±1.74% (93 runs sampled)
🤞 messagepack x 8,330 ops/sec ±5.00% (61 runs sampled)
Fastest is 🤞 json-pack CborEncoderFast
------------------------------------------------------------------------- String ladder, 3398 bytes
🤞 json-pack JsonEncoder x 147,755 ops/sec ±0.23% (97 runs sampled)
🤞 Buffer.from(JSON.stringify()) x 128,378 ops/sec ±0.15% (96 runs sampled)
🤞 json-pack CborEncoderFast x 298,037 ops/sec ±0.73% (98 runs sampled)
🤞 json-pack CborEncoder x 293,608 ops/sec ±0.22% (97 runs sampled)
🤞 json-pack MsgPackEncoderFast x 244,864 ops/sec ±3.92% (92 runs sampled)
🤞 JSON.stringify() x 165,819 ops/sec ±1.72% (94 runs sampled)
🤞 @msgpack/msgpack x 79,127 ops/sec ±1.43% (93 runs sampled)
🤞 msgpackr x 236,254 ops/sec ±1.45% (94 runs sampled)
🤞 cbor-x x 206,835 ops/sec ±1.26% (92 runs sampled)
🤞 msgpack-lite x 157,499 ops/sec ±0.39% (98 runs sampled)
🤞 msgpack5 x 55,363 ops/sec ±2.75% (88 runs sampled)
🤞 messagepack x 8,261 ops/sec ±2.97% (72 runs sampled)
Fastest is 🤞 json-pack CborEncoderFast
-------------------------------------------------------------------------- Long strings, 7011 bytes
🤞 json-pack JsonEncoder x 51,334 ops/sec ±0.16% (99 runs sampled)
🤞 Buffer.from(JSON.stringify()) x 27,108 ops/sec ±4.89% (90 runs sampled)
🤞 json-pack CborEncoderFast x 374,042 ops/sec ±6.39% (91 runs sampled)
🤞 json-pack CborEncoder x 424,864 ops/sec ±0.35% (97 runs sampled)
🤞 json-pack MsgPackEncoderFast x 363,465 ops/sec ±1.91% (85 runs sampled)
🤞 JSON.stringify() x 59,793 ops/sec ±0.14% (100 runs sampled)
🤞 @msgpack/msgpack x 57,373 ops/sec ±0.13% (98 runs sampled)
🤞 msgpackr x 372,751 ops/sec ±2.17% (90 runs sampled)
🤞 cbor-x x 389,277 ops/sec ±1.60% (93 runs sampled)
🤞 msgpack-lite x 170,279 ops/sec ±0.82% (97 runs sampled)
🤞 msgpack5 x 83,809 ops/sec ±2.80% (83 runs sampled)
🤞 messagepack x 20,076 ops/sec ±1.45% (87 runs sampled)
Fastest is 🤞 json-pack CborEncoder
-------------------------------------------------------------------------- Short strings, 170 bytes
🤞 json-pack JsonEncoder x 1,577,757 ops/sec ±0.16% (98 runs sampled)
🤞 Buffer.from(JSON.stringify()) x 1,057,420 ops/sec ±0.38% (100 runs sampled)
🤞 json-pack CborEncoderFast x 1,844,775 ops/sec ±0.20% (100 runs sampled)
🤞 json-pack CborEncoder x 1,468,011 ops/sec ±0.23% (98 runs sampled)
🤞 json-pack MsgPackEncoderFast x 1,240,577 ops/sec ±0.19% (98 runs sampled)
🤞 JSON.stringify() x 1,852,916 ops/sec ±0.20% (100 runs sampled)
🤞 @msgpack/msgpack x 781,414 ops/sec ±0.42% (92 runs sampled)
🤞 msgpackr x 1,672,474 ops/sec ±0.23% (99 runs sampled)
🤞 cbor-x x 1,351,338 ops/sec ±0.20% (97 runs sampled)
🤞 msgpack-lite x 416,300 ops/sec ±0.76% (96 runs sampled)
🤞 msgpack5 x 151,657 ops/sec ±1.97% (91 runs sampled)
🤞 messagepack x 35,124 ops/sec ±5.60% (61 runs sampled)
Fastest is 🤞 JSON.stringify()
-------------------------------------------------------------------------------- Numbers, 136 bytes
🤞 json-pack JsonEncoder x 1,708,133 ops/sec ±1.09% (98 runs sampled)
🤞 Buffer.from(JSON.stringify()) x 1,135,630 ops/sec ±1.67% (95 runs sampled)
🤞 json-pack CborEncoderFast x 2,658,037 ops/sec ±1.33% (97 runs sampled)
🤞 json-pack CborEncoder x 3,084,914 ops/sec ±0.24% (101 runs sampled)
🤞 json-pack MsgPackEncoderFast x 1,620,958 ops/sec ±2.15% (94 runs sampled)
🤞 JSON.stringify() x 1,602,303 ops/sec ±0.24% (98 runs sampled)
🤞 @msgpack/msgpack x 997,885 ops/sec ±1.70% (97 runs sampled)
🤞 msgpackr x 2,659,862 ops/sec ±0.51% (96 runs sampled)
🤞 cbor-x x 3,116,954 ops/sec ±0.89% (95 runs sampled)
🤞 msgpack-lite x 892,281 ops/sec ±2.19% (92 runs sampled)
🤞 msgpack5 x 144,567 ops/sec ±3.06% (88 runs sampled)
🤞 messagepack x 383,134 ops/sec ±2.95% (74 runs sampled)
Fastest is 🤞 cbor-x
--------------------------------------------------------------------------------- Tokens, 308 bytes
🤞 json-pack JsonEncoder x 1,370,517 ops/sec ±0.52% (98 runs sampled)
🤞 Buffer.from(JSON.stringify()) x 1,016,856 ops/sec ±0.16% (93 runs sampled)
🤞 json-pack CborEncoderFast x 1,347,193 ops/sec ±0.20% (96 runs sampled)
🤞 json-pack CborEncoder x 1,353,358 ops/sec ±0.20% (101 runs sampled)
🤞 json-pack MsgPackEncoderFast x 1,130,418 ops/sec ±0.14% (96 runs sampled)
🤞 JSON.stringify() x 1,549,669 ops/sec ±0.49% (97 runs sampled)
🤞 @msgpack/msgpack x 871,477 ops/sec ±0.92% (98 runs sampled)
🤞 msgpackr x 1,716,378 ops/sec ±0.20% (99 runs sampled)
🤞 cbor-x x 1,951,639 ops/sec ±0.16% (100 runs sampled)
🤞 msgpack-lite x 622,495 ops/sec ±1.03% (96 runs sampled)
🤞 msgpack5 x 81,727 ops/sec ±2.04% (91 runs sampled)
🤞 messagepack x 609,651 ops/sec ±1.64% (89 runs sampled)
Fastest is 🤞 cbor-x
```
Decoding:
```
node benchmarks/json-pack/bench.decoding.js
=============================================================================== Benchmark: Decoding
Warmup: 1000x , Node.js: v16.14.2 , Arch: arm64 , CPU: Apple M1
-------------------------------------------------------------------- Very large object, 45750 bytes
👍 JSON.parse() x 3,506 ops/sec ±0.19% (100 runs sampled)
👍 sjson.parse() x 3,336 ops/sec ±0.11% (99 runs sampled)
👍 json-pack CborDecoderBase x 4,915 ops/sec ±0.18% (100 runs sampled)
👍 cbor-x x 4,747 ops/sec ±0.15% (100 runs sampled)
👍 cbor x 260 ops/sec ±0.29% (90 runs sampled)
👍 json-pack MsgPackDecoderFast x 5,506 ops/sec ±0.48% (100 runs sampled)
👍 msgpackr x 4,729 ops/sec ±0.23% (101 runs sampled)
👍 @msgpack/msgpack x 4,096 ops/sec ±0.25% (100 runs sampled)
👍 msgpack5 x 920 ops/sec ±0.34% (99 runs sampled)
👍 msgpack-lite x 1,223 ops/sec ±0.10% (100 runs sampled)
👍 messagepack x 194 ops/sec ±1.93% (73 runs sampled)
Fastest is 👍 json-pack MsgPackDecoderFast
-------------------------------------------------------------------------- Large object, 3741 bytes
👍 JSON.parse() x 91,582 ops/sec ±0.30% (100 runs sampled)
👍 sjson.parse() x 84,411 ops/sec ±0.16% (99 runs sampled)
👍 json-pack CborDecoderBase x 94,618 ops/sec ±0.27% (97 runs sampled)
👍 cbor-x x 108,102 ops/sec ±0.37% (101 runs sampled)
👍 cbor x 4,845 ops/sec ±0.79% (95 runs sampled)
👍 json-pack MsgPackDecoderFast x 102,544 ops/sec ±0.39% (99 runs sampled)
👍 msgpackr x 111,668 ops/sec ±0.16% (101 runs sampled)
👍 @msgpack/msgpack x 56,952 ops/sec ±0.51% (97 runs sampled)
👍 msgpack5 x 17,420 ops/sec ±0.60% (101 runs sampled)
👍 msgpack-lite x 20,536 ops/sec ±0.23% (98 runs sampled)
👍 messagepack x 3,247 ops/sec ±2.30% (87 runs sampled)
Fastest is 👍 msgpackr
------------------------------------------------------------------------- Typical object, 993 bytes
👍 JSON.parse() x 304,670 ops/sec ±0.98% (97 runs sampled)
👍 sjson.parse() x 283,259 ops/sec ±0.20% (98 runs sampled)
👍 json-pack CborDecoderBase x 298,666 ops/sec ±0.19% (100 runs sampled)
👍 cbor-x x 322,995 ops/sec ±0.71% (97 runs sampled)
👍 cbor x 14,391 ops/sec ±0.88% (95 runs sampled)
👍 json-pack MsgPackDecoderFast x 321,984 ops/sec ±0.23% (100 runs sampled)
👍 msgpackr x 328,671 ops/sec ±0.31% (99 runs sampled)
👍 @msgpack/msgpack x 198,604 ops/sec ±0.85% (96 runs sampled)
👍 msgpack5 x 51,549 ops/sec ±0.32% (99 runs sampled)
👍 msgpack-lite x 67,171 ops/sec ±0.19% (99 runs sampled)
👍 messagepack x 9,464 ops/sec ±1.95% (92 runs sampled)
Fastest is 👍 msgpackr
---------------------------------------------------------------------------- Small object, 44 bytes
👍 JSON.parse() x 2,654,389 ops/sec ±0.28% (98 runs sampled)
👍 sjson.parse() x 2,325,941 ops/sec ±0.21% (98 runs sampled)
👍 json-pack CborDecoderBase x 3,357,402 ops/sec ±0.31% (99 runs sampled)
👍 cbor-x x 4,133,737 ops/sec ±0.29% (101 runs sampled)
👍 cbor x 112,776 ops/sec ±5.79% (88 runs sampled)
👍 json-pack MsgPackDecoderFast x 3,359,127 ops/sec ±0.56% (98 runs sampled)
👍 msgpackr x 3,436,592 ops/sec ±0.35% (97 runs sampled)
👍 @msgpack/msgpack x 2,288,251 ops/sec ±0.52% (94 runs sampled)
👍 msgpack5 x 377,061 ops/sec ±0.67% (96 runs sampled)
👍 msgpack-lite x 872,569 ops/sec ±0.31% (100 runs sampled)
👍 messagepack x 116,422 ops/sec ±1.84% (86 runs sampled)
Fastest is 👍 cbor-x
------------------------------------------------------------------ Object with many keys, 969 bytes
👍 JSON.parse() x 270,312 ops/sec ±0.57% (98 runs sampled)
👍 sjson.parse() x 242,328 ops/sec ±3.10% (97 runs sampled)
👍 json-pack CborDecoderBase x 81,403 ops/sec ±0.42% (96 runs sampled)
👍 cbor-x x 93,131 ops/sec ±0.48% (99 runs sampled)
👍 cbor x 8,760 ops/sec ±0.93% (95 runs sampled)
👍 json-pack MsgPackDecoderFast x 84,014 ops/sec ±0.31% (96 runs sampled)
👍 msgpackr x 91,477 ops/sec ±0.77% (90 runs sampled)
👍 @msgpack/msgpack x 73,089 ops/sec ±0.56% (89 runs sampled)
👍 msgpack5 x 23,468 ops/sec ±0.72% (97 runs sampled)
👍 msgpack-lite x 34,630 ops/sec ±0.48% (100 runs sampled)
👍 messagepack x 6,161 ops/sec ±1.77% (86 runs sampled)
Fastest is 👍 JSON.parse()
------------------------------------------------------------------------- String ladder, 3398 bytes
👍 JSON.parse() x 287,387 ops/sec ±0.36% (99 runs sampled)
👍 sjson.parse() x 192,836 ops/sec ±0.40% (95 runs sampled)
👍 json-pack CborDecoderBase x 177,787 ops/sec ±0.48% (98 runs sampled)
👍 cbor-x x 320,303 ops/sec ±0.51% (94 runs sampled)
👍 cbor x 15,416 ops/sec ±0.61% (94 runs sampled)
👍 json-pack MsgPackDecoderFast x 179,625 ops/sec ±0.59% (100 runs sampled)
👍 msgpackr x 375,452 ops/sec ±0.69% (94 runs sampled)
👍 @msgpack/msgpack x 36,544 ops/sec ±0.75% (84 runs sampled)
👍 msgpack5 x 54,428 ops/sec ±0.46% (98 runs sampled)
👍 msgpack-lite x 25,309 ops/sec ±0.81% (75 runs sampled)
👍 messagepack x 10,117 ops/sec ±3.99% (82 runs sampled)
Fastest is 👍 msgpackr
-------------------------------------------------------------------------- Long strings, 7011 bytes
👍 JSON.parse() x 117,335 ops/sec ±3.32% (89 runs sampled)
👍 sjson.parse() x 103,275 ops/sec ±0.64% (94 runs sampled)
👍 json-pack CborDecoderBase x 74,140 ops/sec ±7.50% (81 runs sampled)
👍 cbor-x x 92,753 ops/sec ±0.78% (96 runs sampled)
👍 cbor x 24,292 ops/sec ±27.70% (75 runs sampled)
👍 json-pack MsgPackDecoderFast x 88,124 ops/sec ±1.65% (90 runs sampled)
👍 msgpackr x 94,352 ops/sec ±0.91% (94 runs sampled)
👍 @msgpack/msgpack x 33,256 ops/sec ±30.68% (71 runs sampled)
👍 msgpack5 x 68,367 ops/sec ±0.70% (95 runs sampled)
👍 msgpack-lite x 14,764 ops/sec ±2.04% (63 runs sampled)
👍 messagepack x 17,522 ops/sec ±28.57% (66 runs sampled)
Fastest is 👍 JSON.parse()
-------------------------------------------------------------------------- Short strings, 170 bytes
👍 JSON.parse() x 1,077,084 ops/sec ±6.88% (77 runs sampled)
👍 sjson.parse() x 837,130 ops/sec ±2.70% (80 runs sampled)
👍 json-pack CborDecoderBase x 698,901 ops/sec ±4.69% (88 runs sampled)
👍 cbor-x x 1,182,303 ops/sec ±0.39% (94 runs sampled)
👍 cbor x 26,810 ops/sec ±14.70% (73 runs sampled)
👍 json-pack MsgPackDecoderFast x 742,562 ops/sec ±5.06% (88 runs sampled)
👍 msgpackr x 1,041,143 ops/sec ±2.66% (85 runs sampled)
👍 @msgpack/msgpack x 440,652 ops/sec ±1.38% (92 runs sampled)
👍 msgpack5 x 133,387 ops/sec ±1.14% (96 runs sampled)
👍 msgpack-lite x 206,844 ops/sec ±0.63% (97 runs sampled)
👍 messagepack x 23,818 ops/sec ±2.13% (94 runs sampled)
Fastest is 👍 cbor-x,👍 JSON.parse()
-------------------------------------------------------------------------------- Numbers, 136 bytes
👍 JSON.parse() x 1,747,460 ops/sec ±0.61% (95 runs sampled)
👍 sjson.parse() x 1,553,635 ops/sec ±1.04% (93 runs sampled)
👍 json-pack CborDecoderBase x 2,289,002 ops/sec ±0.93% (87 runs sampled)
👍 cbor-x x 3,775,727 ops/sec ±2.86% (82 runs sampled)
👍 cbor x 77,650 ops/sec ±4.32% (83 runs sampled)
👍 json-pack MsgPackDecoderFast x 2,287,682 ops/sec ±1.54% (80 runs sampled)
👍 msgpackr x 3,391,489 ops/sec ±0.59% (80 runs sampled)
👍 @msgpack/msgpack x 2,297,255 ops/sec ±1.54% (78 runs sampled)
👍 msgpack5 x 112,373 ops/sec ±1.19% (91 runs sampled)
👍 msgpack-lite x 1,378,387 ops/sec ±0.84% (95 runs sampled)
👍 messagepack x 1,174,740 ops/sec ±0.97% (89 runs sampled)
Fastest is 👍 cbor-x
--------------------------------------------------------------------------------- Tokens, 308 bytes
👍 JSON.parse() x 1,303,300 ops/sec ±2.26% (92 runs sampled)
👍 sjson.parse() x 1,091,921 ops/sec ±2.85% (86 runs sampled)
👍 json-pack CborDecoderBase x 1,203,319 ops/sec ±2.12% (90 runs sampled)
👍 cbor-x x 1,787,591 ops/sec ±2.94% (74 runs sampled)
👍 cbor x 45,127 ops/sec ±24.11% (64 runs sampled)
👍 json-pack MsgPackDecoderFast x 1,283,322 ops/sec ±1.93% (94 runs sampled)
👍 msgpackr x 1,890,533 ops/sec ±2.66% (90 runs sampled)
👍 @msgpack/msgpack x 1,364,025 ops/sec ±3.78% (67 runs sampled)
👍 msgpack5 x 117,205 ops/sec ±2.72% (90 runs sampled)
👍 msgpack-lite x 1,316,133 ops/sec ±0.74% (99 runs sampled)
👍 messagepack x 733,566 ops/sec ±1.55% (87 runs sampled)
Fastest is 👍 msgpackr
```
Encoder comparison:
```
npx ts-node benchmarks/json-pack/bench.encoders.ts
=============================================================================== Benchmark: Encoding
Warmup: 1000x , Node.js: v20.2.0 , Arch: arm64 , CPU: Apple M1 Max
---------------------------------------------------------------------------- Small object, 44 bytes
👍 CborEncoderFast x 6,319,117 ops/sec ±0.11% (101 runs sampled)
👍 CborEncoder x 6,001,443 ops/sec ±0.15% (101 runs sampled)
👎 MsgPackEncoderFast x 6,047,466 ops/sec ±0.20% (99 runs sampled)
👎 MsgPackEncoder x 5,493,093 ops/sec ±0.10% (101 runs sampled)
👎 JsonEncoder x 6,018,890 ops/sec ±0.11% (97 runs sampled)
👎 UbjsonEncoder x 6,545,118 ops/sec ±0.10% (97 runs sampled)
👎 IonEncoderFast x 1,032,434 ops/sec ±0.14% (99 runs sampled)
👎 Buffer.from(JSON.stringify()) x 2,300,069 ops/sec ±0.15% (100 runs sampled)
Fastest is 👎 UbjsonEncoder
------------------------------------------------------------------------- Typical object, 993 bytes
👍 CborEncoderFast x 460,125 ops/sec ±0.14% (98 runs sampled)
👍 CborEncoder x 439,506 ops/sec ±0.18% (98 runs sampled)
👎 MsgPackEncoderFast x 458,530 ops/sec ±0.15% (99 runs sampled)
👎 MsgPackEncoder x 449,540 ops/sec ±0.16% (100 runs sampled)
👎 JsonEncoder x 303,410 ops/sec ±0.12% (101 runs sampled)
👎 UbjsonEncoder x 479,450 ops/sec ±0.13% (99 runs sampled)
👎 IonEncoderFast x 68,000 ops/sec ±0.11% (102 runs sampled)
👎 Buffer.from(JSON.stringify()) x 207,747 ops/sec ±0.11% (98 runs sampled)
Fastest is 👎 UbjsonEncoder
-------------------------------------------------------------------------- Large object, 3741 bytes
👍 CborEncoderFast x 133,608 ops/sec ±0.15% (100 runs sampled)
👍 CborEncoder x 128,019 ops/sec ±0.13% (97 runs sampled)
👎 MsgPackEncoderFast x 133,863 ops/sec ±0.14% (99 runs sampled)
👎 MsgPackEncoder x 131,521 ops/sec ±0.18% (99 runs sampled)
👎 JsonEncoder x 93,018 ops/sec ±0.13% (98 runs sampled)
👎 UbjsonEncoder x 140,969 ops/sec ±0.15% (101 runs sampled)
👎 IonEncoderFast x 11,523 ops/sec ±0.15% (101 runs sampled)
👎 Buffer.from(JSON.stringify()) x 63,389 ops/sec ±0.13% (101 runs sampled)
Fastest is 👎 UbjsonEncoder
-------------------------------------------------------------------- Very large object, 45750 bytes
👍 CborEncoderFast x 5,790 ops/sec ±0.15% (100 runs sampled)
👍 CborEncoder x 5,579 ops/sec ±0.14% (100 runs sampled)
👎 MsgPackEncoderFast x 6,005 ops/sec ±0.13% (100 runs sampled)
👎 MsgPackEncoder x 5,670 ops/sec ±0.18% (99 runs sampled)
👎 JsonEncoder x 6,351 ops/sec ±0.16% (101 runs sampled)
👎 UbjsonEncoder x 6,248 ops/sec ±0.18% (99 runs sampled)
👎 IonEncoderFast x 1,868 ops/sec ±0.21% (98 runs sampled)
👎 Buffer.from(JSON.stringify()) x 7,240 ops/sec ±0.19% (99 runs sampled)
Fastest is 👎 Buffer.from(JSON.stringify())
------------------------------------------------------------------ Object with many keys, 969 bytes
👍 CborEncoderFast x 283,371 ops/sec ±0.18% (99 runs sampled)
👍 CborEncoder x 268,056 ops/sec ±0.17% (96 runs sampled)
👎 MsgPackEncoderFast x 285,224 ops/sec ±0.17% (96 runs sampled)
👎 MsgPackEncoder x 272,416 ops/sec ±0.21% (98 runs sampled)
👎 JsonEncoder x 234,921 ops/sec ±0.21% (98 runs sampled)
👎 UbjsonEncoder x 292,228 ops/sec ±0.19% (95 runs sampled)
👎 IonEncoderFast x 63,456 ops/sec ±0.14% (98 runs sampled)
👎 Buffer.from(JSON.stringify()) x 175,341 ops/sec ±0.86% (93 runs sampled)
Fastest is 👎 UbjsonEncoder
------------------------------------------------------------------------- String ladder, 3398 bytes
👍 CborEncoderFast x 280,167 ops/sec ±0.20% (100 runs sampled)
👍 CborEncoder x 283,404 ops/sec ±0.20% (97 runs sampled)
👎 MsgPackEncoderFast x 272,800 ops/sec ±0.18% (99 runs sampled)
👎 MsgPackEncoder x 283,433 ops/sec ±0.23% (98 runs sampled)
👎 JsonEncoder x 147,390 ops/sec ±0.16% (98 runs sampled)
👎 UbjsonEncoder x 290,624 ops/sec ±0.21% (98 runs sampled)
👎 IonEncoderFast x 25,452 ops/sec ±0.17% (101 runs sampled)
👎 Buffer.from(JSON.stringify()) x 145,352 ops/sec ±0.23% (99 runs sampled)
Fastest is 👎 UbjsonEncoder
-------------------------------------------------------------------------- Long strings, 7011 bytes
👍 CborEncoderFast x 394,386 ops/sec ±0.53% (95 runs sampled)
👍 CborEncoder x 394,442 ops/sec ±0.49% (94 runs sampled)
👎 MsgPackEncoderFast x 386,894 ops/sec ±0.54% (95 runs sampled)
👎 MsgPackEncoder x 394,019 ops/sec ±0.50% (95 runs sampled)
👎 JsonEncoder x 50,781 ops/sec ±0.13% (97 runs sampled)
👎 UbjsonEncoder x 396,184 ops/sec ±0.57% (95 runs sampled)
👎 IonEncoderFast x 11,799 ops/sec ±0.22% (99 runs sampled)
👎 Buffer.from(JSON.stringify()) x 28,742 ops/sec ±0.11% (102 runs sampled)
Fastest is 👎 UbjsonEncoder,👍 CborEncoder,👍 CborEncoderFast,👎 MsgPackEncoder
-------------------------------------------------------------------------- Short strings, 170 bytes
👍 CborEncoderFast x 1,816,742 ops/sec ±0.16% (100 runs sampled)
👍 CborEncoder x 1,831,503 ops/sec ±0.22% (97 runs sampled)
👎 MsgPackEncoderFast x 1,641,743 ops/sec ±0.17% (101 runs sampled)
👎 MsgPackEncoder x 1,694,803 ops/sec ±0.17% (97 runs sampled)
👎 JsonEncoder x 1,595,041 ops/sec ±0.12% (99 runs sampled)
👎 UbjsonEncoder x 1,779,112 ops/sec ±0.24% (98 runs sampled)
👎 IonEncoderFast x 422,031 ops/sec ±0.10% (101 runs sampled)
👎 Buffer.from(JSON.stringify()) x 1,001,976 ops/sec ±0.24% (98 runs sampled)
Fastest is 👍 CborEncoder
-------------------------------------------------------------------------------- Numbers, 136 bytes
👍 CborEncoderFast x 2,822,683 ops/sec ±0.14% (99 runs sampled)
👍 CborEncoder x 3,111,311 ops/sec ±0.20% (97 runs sampled)
👎 MsgPackEncoderFast x 2,918,971 ops/sec ±0.14% (100 runs sampled)
👎 MsgPackEncoder x 2,862,193 ops/sec ±0.13% (100 runs sampled)
👎 JsonEncoder x 1,706,584 ops/sec ±0.18% (96 runs sampled)
👎 UbjsonEncoder x 3,238,810 ops/sec ±0.15% (97 runs sampled)
👎 IonEncoderFast x 545,885 ops/sec ±0.16% (98 runs sampled)
👎 Buffer.from(JSON.stringify()) x 1,216,907 ops/sec ±0.20% (98 runs sampled)
Fastest is 👎 UbjsonEncoder
--------------------------------------------------------------------------------- Tokens, 308 bytes
👍 CborEncoderFast x 1,360,976 ops/sec ±0.20% (96 runs sampled)
👍 CborEncoder x 1,367,625 ops/sec ±0.16% (101 runs sampled)
👎 MsgPackEncoderFast x 1,753,202 ops/sec ±0.19% (99 runs sampled)
👎 MsgPackEncoder x 1,733,298 ops/sec ±0.16% (100 runs sampled)
👎 JsonEncoder x 1,411,382 ops/sec ±0.27% (98 runs sampled)
👎 UbjsonEncoder x 1,734,304 ops/sec ±0.17% (101 runs sampled)
👎 IonEncoderFast x 369,161 ops/sec ±0.21% (97 runs sampled)
👎 Buffer.from(JSON.stringify()) x 1,092,623 ops/sec ±0.15% (101 runs sampled)
Fastest is 👎 MsgPackEncoderFast
```
### Shallow reading
```
node benchmarks/json-pack/bench.shallow-read.js
=============================================================================== Benchmark: Encoding
Warmup: 10000x , Node.js: v16.14.2 , Arch: arm64 , CPU: Apple M1
------------------------------------------------------------------------- Typical object, 993 bytes
👍 JSON.parse() x 314,451 ops/sec ±0.24% (94 runs sampled)
👍 msgpackr x 332,628 ops/sec ±0.09% (99 runs sampled)
👍 cbor-x x 326,509 ops/sec ±0.05% (101 runs sampled)
👍 MsgPackDecoder x 368,088 ops/sec ±0.15% (100 runs sampled)
👍 CborDecoder x 327,286 ops/sec ±0.15% (101 runs sampled)
👍 MsgPackDecoder.{findKey,findIndex}() x 1,815,090 ops/sec ±0.07% (99 runs sampled)
👍 MsgPackDecoder.find() x 1,797,098 ops/sec ±0.15% (98 runs sampled)
👍 genShallowReader()(MsgPackDecoder) x 2,085,260 ops/sec ±0.19% (99 runs sampled)
Fastest is 👍 genShallowReader()(MsgPackDecoder)
```
+5
View File
@@ -0,0 +1,5 @@
export declare class JsonPackExtension<T = Uint8Array> {
readonly tag: number;
readonly val: T;
constructor(tag: number, val: T);
}
+11
View File
@@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonPackExtension = void 0;
class JsonPackExtension {
constructor(tag, val) {
this.tag = tag;
this.val = val;
}
}
exports.JsonPackExtension = JsonPackExtension;
//# sourceMappingURL=JsonPackExtension.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"JsonPackExtension.js","sourceRoot":"","sources":["../src/JsonPackExtension.ts"],"names":[],"mappings":";;;AAQA,MAAa,iBAAiB;IAC5B,YACkB,GAAW,EACX,GAAM;QADN,QAAG,GAAH,GAAG,CAAQ;QACX,QAAG,GAAH,GAAG,CAAG;IACrB,CAAC;CACL;AALD,8CAKC"}
+4
View File
@@ -0,0 +1,4 @@
export declare class JsonPackValue<T = Uint8Array> {
readonly val: T;
constructor(val: T);
}
+10
View File
@@ -0,0 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonPackValue = void 0;
class JsonPackValue {
constructor(val) {
this.val = val;
}
}
exports.JsonPackValue = JsonPackValue;
//# sourceMappingURL=JsonPackValue.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"JsonPackValue.js","sourceRoot":"","sources":["../src/JsonPackValue.ts"],"names":[],"mappings":";;;AAUA,MAAa,aAAa;IACxB,YAA4B,GAAM;QAAN,QAAG,GAAH,GAAG,CAAG;IAAG,CAAC;CACvC;AAFD,sCAEC"}
+18
View File
@@ -0,0 +1,18 @@
import { Reader } from '@jsonjoy.com/util/lib/buffers/Reader';
import type { BinaryJsonDecoder, PackValue } from '../types';
export declare class BencodeDecoder implements BinaryJsonDecoder {
reader: Reader;
read(uint8: Uint8Array): unknown;
decode(uint8: Uint8Array): unknown;
readAny(): unknown;
readNull(): null;
readUndef(): undefined;
readTrue(): true;
readFalse(): false;
readBool(): unknown;
readNum(): number;
readStr(): string;
readBin(): Uint8Array;
readArr(): unknown[];
readObj(): PackValue | Record<string, unknown> | unknown;
}
+156
View File
@@ -0,0 +1,156 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BencodeDecoder = void 0;
const Reader_1 = require("@jsonjoy.com/util/lib/buffers/Reader");
class BencodeDecoder {
constructor() {
this.reader = new Reader_1.Reader();
}
read(uint8) {
this.reader.reset(uint8);
return this.readAny();
}
decode(uint8) {
this.reader.reset(uint8);
return this.readAny();
}
readAny() {
const reader = this.reader;
const x = reader.x;
const uint8 = reader.uint8;
const char = uint8[x];
switch (char) {
case 0x69:
return this.readNum();
case 0x64:
return this.readObj();
case 0x6c:
return this.readArr();
case 0x66:
return this.readFalse();
case 0x74:
return this.readTrue();
case 110:
return this.readNull();
case 117:
return this.readUndef();
default:
if (char >= 48 && char <= 57)
return this.readBin();
}
throw new Error('INVALID_BENCODE');
}
readNull() {
if (this.reader.u8() !== 0x6e)
throw new Error('INVALID_BENCODE');
return null;
}
readUndef() {
if (this.reader.u8() !== 117)
throw new Error('INVALID_BENCODE');
return undefined;
}
readTrue() {
if (this.reader.u8() !== 0x74)
throw new Error('INVALID_BENCODE');
return true;
}
readFalse() {
if (this.reader.u8() !== 0x66)
throw new Error('INVALID_BENCODE');
return false;
}
readBool() {
const reader = this.reader;
switch (reader.uint8[reader.x]) {
case 0x66:
return this.readFalse();
case 0x74:
return this.readTrue();
default:
throw new Error('INVALID_BENCODE');
}
}
readNum() {
const reader = this.reader;
const startChar = reader.u8();
if (startChar !== 0x69)
throw new Error('INVALID_BENCODE');
const u8 = reader.uint8;
let x = reader.x;
let numStr = '';
let c = u8[x++];
let i = 0;
while (c !== 0x65) {
numStr += String.fromCharCode(c);
c = u8[x++];
if (i > 25)
throw new Error('INVALID_BENCODE');
i++;
}
if (!numStr)
throw new Error('INVALID_BENCODE');
reader.x = x;
return +numStr;
}
readStr() {
const bin = this.readBin();
return new TextDecoder().decode(bin);
}
readBin() {
const reader = this.reader;
const u8 = reader.uint8;
let lenStr = '';
let x = reader.x;
let c = u8[x++];
let i = 0;
while (c !== 0x3a) {
if (c < 48 || c > 57)
throw new Error('INVALID_BENCODE');
lenStr += String.fromCharCode(c);
c = u8[x++];
if (i > 10)
throw new Error('INVALID_BENCODE');
i++;
}
reader.x = x;
const len = +lenStr;
const bin = reader.buf(len);
return bin;
}
readArr() {
const reader = this.reader;
if (reader.u8() !== 0x6c)
throw new Error('INVALID_BENCODE');
const arr = [];
const uint8 = reader.uint8;
while (true) {
const char = uint8[reader.x];
if (char === 0x65) {
reader.x++;
return arr;
}
arr.push(this.readAny());
}
}
readObj() {
const reader = this.reader;
if (reader.u8() !== 0x64)
throw new Error('INVALID_BENCODE');
const obj = {};
const uint8 = reader.uint8;
while (true) {
const char = uint8[reader.x];
if (char === 0x65) {
reader.x++;
return obj;
}
const key = this.readStr();
if (key === '__proto__')
throw new Error('INVALID_KEY');
obj[key] = this.readAny();
}
}
}
exports.BencodeDecoder = BencodeDecoder;
//# sourceMappingURL=BencodeDecoder.js.map
@@ -0,0 +1 @@
{"version":3,"file":"BencodeDecoder.js","sourceRoot":"","sources":["../../src/bencode/BencodeDecoder.ts"],"names":[],"mappings":";;;AAAA,iEAA4D;AAG5D,MAAa,cAAc;IAA3B;QACS,WAAM,GAAG,IAAI,eAAM,EAAE,CAAC;IAkJ/B,CAAC;IAhJQ,IAAI,CAAC,KAAiB;QAC3B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACzB,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAEM,MAAM,CAAC,KAAiB;QAC7B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACzB,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAEM,OAAO;QACZ,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC3B,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;QACnB,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QAC3B,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACtB,QAAQ,IAA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O,IAAI,EAAE,CAAC;YACZ,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;YAC7B,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;gBAClB,MAAM,CAAC,CAAC,EAAE,CAAC;gBACX,OAAO,GAAG,CAAC;YACb,CAAC;YACD,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;QAC3B,CAAC;IACH,CAAC;IAEM,OAAO;QACZ,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC3B,IAAI,MAAM,CAAC,EAAE,EAAE,KAAK,IAAI;YAAE,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;QAC7D,MAAM,GAAG,GAA4B,EAAE,CAAC;QACxC,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QAC3B,OAAO,IAAI,EAAE,CAAC;YACZ,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;YAC7B,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;gBAClB,MAAM,CAAC,CAAC,EAAE,CAAC;gBACX,OAAO,GAAG,CAAC;YACb,CAAC;YACD,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;YAC3B,IAAI,GAAG,KAAK,WAAW;gBAAE,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC;YACxD,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;QAC5B,CAAC;IACH,CAAC;CACF;AAnJD,wCAmJC"}
+24
View File
@@ -0,0 +1,24 @@
import type { IWriter, IWriterGrowable } from '@jsonjoy.com/util/lib/buffers';
import type { BinaryJsonEncoder } from '../types';
export declare class BencodeEncoder implements BinaryJsonEncoder {
readonly writer: IWriter & IWriterGrowable;
constructor(writer: IWriter & IWriterGrowable);
encode(value: unknown): Uint8Array;
writeUnknown(value: unknown): void;
writeAny(value: unknown): void;
writeNull(): void;
writeUndef(): void;
writeBoolean(bool: boolean): void;
writeNumber(num: number): void;
writeInteger(int: number): void;
writeUInteger(uint: number): void;
writeFloat(float: number): void;
writeBigint(int: bigint): void;
writeBin(buf: Uint8Array): void;
writeStr(str: string): void;
writeAsciiStr(str: string): void;
writeArr(arr: unknown[]): void;
writeObj(obj: Record<string, unknown>): void;
writeMap(obj: Map<unknown, unknown>): void;
writeSet(set: Set<unknown>): void;
}
+147
View File
@@ -0,0 +1,147 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BencodeEncoder = void 0;
const utf8_1 = require("@jsonjoy.com/util/lib/strings/utf8");
const insertion_1 = require("@jsonjoy.com/util/lib/sort/insertion");
class BencodeEncoder {
constructor(writer) {
this.writer = writer;
}
encode(value) {
const writer = this.writer;
writer.reset();
this.writeAny(value);
return writer.flush();
}
writeUnknown(value) {
this.writeNull();
}
writeAny(value) {
switch (typeof value) {
case 'boolean':
return this.writeBoolean(value);
case 'number':
return this.writeNumber(value);
case 'string':
return this.writeStr(value);
case 'object': {
if (value === null)
return this.writeNull();
const constructor = value.constructor;
switch (constructor) {
case Object:
return this.writeObj(value);
case Array:
return this.writeArr(value);
case Uint8Array:
return this.writeBin(value);
case Map:
return this.writeMap(value);
case Set:
return this.writeSet(value);
default:
return this.writeUnknown(value);
}
}
case 'bigint': {
return this.writeBigint(value);
}
case 'undefined': {
return this.writeUndef();
}
default:
return this.writeUnknown(value);
}
}
writeNull() {
this.writer.u8(110);
}
writeUndef() {
this.writer.u8(117);
}
writeBoolean(bool) {
this.writer.u8(bool ? 0x74 : 0x66);
}
writeNumber(num) {
const writer = this.writer;
writer.u8(0x69);
writer.ascii(Math.round(num) + '');
writer.u8(0x65);
}
writeInteger(int) {
const writer = this.writer;
writer.u8(0x69);
writer.ascii(int + '');
writer.u8(0x65);
}
writeUInteger(uint) {
this.writeInteger(uint);
}
writeFloat(float) {
this.writeNumber(float);
}
writeBigint(int) {
const writer = this.writer;
writer.u8(0x69);
writer.ascii(int + '');
writer.u8(0x65);
}
writeBin(buf) {
const writer = this.writer;
const length = buf.length;
writer.ascii(length + '');
writer.u8(0x3a);
writer.buf(buf, length);
}
writeStr(str) {
const writer = this.writer;
const length = (0, utf8_1.utf8Size)(str);
writer.ascii(length + '');
writer.u8(0x3a);
writer.ensureCapacity(length);
writer.utf8(str);
}
writeAsciiStr(str) {
const writer = this.writer;
writer.ascii(str.length + '');
writer.u8(0x3a);
writer.ascii(str);
}
writeArr(arr) {
const writer = this.writer;
writer.u8(0x6c);
const length = arr.length;
for (let i = 0; i < length; i++)
this.writeAny(arr[i]);
writer.u8(0x65);
}
writeObj(obj) {
const writer = this.writer;
writer.u8(0x64);
const keys = (0, insertion_1.sort)(Object.keys(obj));
const length = keys.length;
for (let i = 0; i < length; i++) {
const key = keys[i];
this.writeStr(key);
this.writeAny(obj[key]);
}
writer.u8(0x65);
}
writeMap(obj) {
const writer = this.writer;
writer.u8(0x64);
const keys = (0, insertion_1.sort)([...obj.keys()]);
const length = keys.length;
for (let i = 0; i < length; i++) {
const key = keys[i];
this.writeStr(key + '');
this.writeAny(obj.get(key));
}
writer.u8(0x65);
}
writeSet(set) {
this.writeArr([...set.values()]);
}
}
exports.BencodeEncoder = BencodeEncoder;
//# sourceMappingURL=BencodeEncoder.js.map
@@ -0,0 +1 @@
{"version":3,"file":"BencodeEncoder.js","sourceRoot":"","sources":["../../src/bencode/BencodeEncoder.ts"],"names":[],"mappings":";;;AAAA,6DAA4D;AAC5D,oEAA0D;AAI1D,MAAa,cAAc;IACzB,YAA4B,MAAiC;QAAjC,WAAM,GAAN,MAAM,CAA2B;IAAG,CAAC;IAE1D,MAAM,CAAC,KAAc;QAC1B,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC3B,MAAM,CAAC,KAAK,EAAE,CAAC;QACf,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QACrB,OAAO,MAAM,CAAC,KAAK,EAAE,CAAC;IACx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B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;QAC9B,CAAC;QACD,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;IAClB,CAAC;IAEM,QAAQ,CAAC,GAAiB;QAC/B,IAAI,CAAC,QAAQ,CAAC,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IACnC,CAAC;CACF;AA9JD,wCA8JC"}
+3
View File
@@ -0,0 +1,3 @@
export * from './types';
export * from './BencodeEncoder';
export * from './BencodeDecoder';
+7
View File
@@ -0,0 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
tslib_1.__exportStar(require("./types"), exports);
tslib_1.__exportStar(require("./BencodeEncoder"), exports);
tslib_1.__exportStar(require("./BencodeDecoder"), exports);
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/bencode/index.ts"],"names":[],"mappings":";;;AAAA,kDAAwB;AACxB,2DAAiC;AACjC,2DAAiC"}
+4
View File
@@ -0,0 +1,4 @@
export type BencodeUint8Array<T> = Uint8Array & {
__BRAND__: 'bencode';
__TYPE__: T;
};
+3
View File
@@ -0,0 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=types.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/bencode/types.ts"],"names":[],"mappings":""}
+27
View File
@@ -0,0 +1,27 @@
import { BsonObjectId } from './values';
import type { IWriter, IWriterGrowable } from '@jsonjoy.com/util/lib/buffers';
import type { BinaryJsonEncoder } from '../types';
export declare class BsonEncoder implements BinaryJsonEncoder {
readonly writer: IWriter & IWriterGrowable;
constructor(writer: IWriter & IWriterGrowable);
encode(value: unknown): Uint8Array;
writeAny(value: unknown): void;
writeNull(): void;
writeUndef(): void;
writeBoolean(bool: boolean): void;
writeNumber(num: number): void;
writeInteger(int: number): void;
writeUInteger(uint: number): void;
writeInt32(int: number): void;
writeInt64(int: number | bigint): void;
writeFloat(float: number): void;
writeBigInt(int: bigint): void;
writeBin(buf: Uint8Array): void;
writeStr(str: string): void;
writeAsciiStr(str: string): void;
writeArr(arr: unknown[]): void;
writeObj(obj: Record<string, unknown>): void;
writeCString(str: string): void;
writeObjectId(id: BsonObjectId): void;
writeKey(key: string, value: unknown): void;
}
+364
View File
@@ -0,0 +1,364 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BsonEncoder = void 0;
const values_1 = require("./values");
class BsonEncoder {
constructor(writer) {
this.writer = writer;
}
encode(value) {
const writer = this.writer;
writer.reset();
this.writeAny(value);
return writer.flush();
}
writeAny(value) {
switch (typeof value) {
case 'object': {
if (value === null)
throw new Error('NOT_OBJ');
return this.writeObj(value);
}
}
throw new Error('NOT_OBJ');
}
writeNull() {
throw new Error('Method not implemented.');
}
writeUndef() {
throw new Error('Method not implemented.');
}
writeBoolean(bool) {
throw new Error('Method not implemented.');
}
writeNumber(num) {
throw new Error('Method not implemented.');
}
writeInteger(int) {
throw new Error('Method not implemented.');
}
writeUInteger(uint) {
throw new Error('Method not implemented.');
}
writeInt32(int) {
const writer = this.writer;
writer.ensureCapacity(4);
writer.view.setInt32(writer.x, int, true);
writer.x += 4;
}
writeInt64(int) {
const writer = this.writer;
writer.ensureCapacity(8);
writer.view.setBigInt64(writer.x, BigInt(int), true);
writer.x += 8;
}
writeFloat(float) {
const writer = this.writer;
writer.ensureCapacity(4);
writer.view.setFloat64(writer.x, float, true);
writer.x += 8;
}
writeBigInt(int) {
throw new Error('Method not implemented.');
}
writeBin(buf) {
const length = buf.length;
this.writeInt32(length);
const writer = this.writer;
writer.u8(0);
writer.buf(buf, length);
}
writeStr(str) {
const writer = this.writer;
const length = str.length;
const maxSize = 4 + 1 + 4 * length;
writer.ensureCapacity(maxSize);
const x = writer.x;
this.writeInt32(length + 1);
const bytesWritten = writer.utf8(str);
writer.u8(0);
if (bytesWritten !== length) {
writer.view.setInt32(x, bytesWritten + 1, true);
}
}
writeAsciiStr(str) {
throw new Error('Method not implemented.');
}
writeArr(arr) {
this.writeObj(arr);
}
writeObj(obj) {
const writer = this.writer;
writer.ensureCapacity(8);
const x0 = writer.x0;
const dx = writer.x - x0;
writer.x += 4;
const keys = Object.keys(obj);
const length = keys.length;
for (let i = 0; i < length; i++) {
const key = keys[i];
const value = obj[key];
this.writeKey(key, value);
}
writer.u8(0);
const x = writer.x0 + dx;
const size = writer.x - x;
writer.view.setUint32(x, size, true);
}
writeCString(str) {
const writer = this.writer;
const length = str.length;
writer.ensureCapacity(1 + 4 * length);
const uint8 = writer.uint8;
let x = writer.x;
let pos = 0;
while (pos < length) {
let value = str.charCodeAt(pos++);
if ((value & 0xffffff80) === 0) {
if (!value)
break;
uint8[x++] = value;
continue;
}
else if ((value & 0xfffff800) === 0) {
const octet = ((value >> 6) & 0x1f) | 0xc0;
if (!octet)
break;
uint8[x++] = octet;
}
else {
if (value >= 0xd800 && value <= 0xdbff) {
if (pos < length) {
const extra = str.charCodeAt(pos);
if ((extra & 0xfc00) === 0xdc00) {
pos++;
value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000;
}
}
}
if ((value & 0xffff0000) === 0) {
const octet1 = ((value >> 12) & 0x0f) | 0xe0;
const octet2 = ((value >> 6) & 0x3f) | 0x80;
if (!octet1 || !octet2)
throw new Error('INVALID_CSTRING');
uint8[x++] = octet1;
uint8[x++] = octet2;
}
else {
const octet1 = ((value >> 18) & 0x07) | 0xf0;
const octet2 = ((value >> 12) & 0x3f) | 0x80;
const octet3 = ((value >> 6) & 0x3f) | 0x80;
if (!octet1 || !octet2 || !octet3)
throw new Error('INVALID_CSTRING');
uint8[x++] = octet1;
uint8[x++] = octet2;
uint8[x++] = octet3;
}
}
const octet = (value & 0x3f) | 0x80;
if (!octet)
break;
uint8[x++] = octet;
}
uint8[x++] = 0;
writer.x = x;
}
writeObjectId(id) {
const writer = this.writer;
writer.ensureCapacity(12);
const uint8 = writer.uint8;
const x = writer.x;
const { timestamp, process, counter } = id;
uint8[x + 0] = timestamp >>> 24;
uint8[x + 1] = (timestamp >>> 16) & 0xff;
uint8[x + 2] = (timestamp >>> 8) & 0xff;
uint8[x + 3] = timestamp & 0xff;
uint8[x + 4] = process & 0xff;
uint8[x + 5] = (process >>> 8) & 0xff;
uint8[x + 6] = (process >>> 16) & 0xff;
uint8[x + 7] = (process >>> 24) & 0xff;
let lo32 = process | 0;
if (lo32 < 0)
lo32 += 4294967296;
const hi32 = (process - lo32) / 4294967296;
uint8[x + 8] = hi32 & 0xff;
uint8[x + 9] = counter >>> 16;
uint8[x + 10] = (counter >>> 8) & 0xff;
uint8[x + 11] = counter & 0xff;
writer.x += 12;
}
writeKey(key, value) {
const writer = this.writer;
switch (typeof value) {
case 'number': {
const isFloat = Math.floor(value) !== value;
if (isFloat) {
writer.u8(0x01);
this.writeCString(key);
this.writeFloat(value);
break;
}
if (value <= 2147483647 && value >= -2147483648) {
writer.u8(0x10);
this.writeCString(key);
this.writeInt32(value);
break;
}
writer.u8(0x12);
this.writeCString(key);
this.writeInt64(value);
break;
}
case 'string': {
writer.u8(0x02);
this.writeCString(key);
this.writeStr(value);
break;
}
case 'object': {
if (value === null) {
writer.u8(0x0a);
this.writeCString(key);
break;
}
const constructor = value.constructor;
switch (constructor) {
case Object: {
writer.u8(0x03);
this.writeCString(key);
this.writeObj(value);
break;
}
case Array: {
writer.u8(0x04);
this.writeCString(key);
this.writeObj(value);
break;
}
case Uint8Array: {
writer.u8(0x05);
this.writeCString(key);
this.writeBin(value);
break;
}
case values_1.BsonObjectId: {
writer.u8(0x07);
this.writeCString(key);
this.writeObjectId(value);
break;
}
case Date: {
writer.u8(0x09);
this.writeCString(key);
writer.ensureCapacity(8);
writer.view.setBigUint64(writer.x, BigInt(value.getTime()), true);
writer.x += 8;
break;
}
case RegExp: {
writer.u8(0x0b);
this.writeCString(key);
this.writeCString(value.source);
this.writeCString(value.flags);
break;
}
case values_1.BsonDbPointer: {
writer.u8(0x0c);
this.writeCString(key);
const pointer = value;
this.writeStr(pointer.name);
this.writeObjectId(pointer.id);
break;
}
case values_1.BsonJavascriptCode: {
writer.u8(0x0d);
this.writeCString(key);
this.writeStr(value.code);
break;
}
case values_1.BsonInt32: {
writer.u8(0x10);
this.writeCString(key);
this.writeInt32(value.value);
break;
}
case values_1.BsonInt64: {
writer.u8(0x12);
this.writeCString(key);
this.writeInt64(value.value);
break;
}
case values_1.BsonFloat: {
writer.u8(0x01);
this.writeCString(key);
this.writeFloat(value.value);
break;
}
case values_1.BsonTimestamp: {
writer.u8(0x11);
this.writeCString(key);
const ts = value;
this.writeInt32(ts.increment);
this.writeInt32(ts.timestamp);
break;
}
case values_1.BsonDecimal128: {
writer.u8(0x13);
this.writeCString(key);
const dec = value;
if (dec.data.length !== 16)
throw new Error('INVALID_DECIMAL128');
writer.buf(dec.data, 16);
break;
}
case values_1.BsonMinKey: {
writer.u8(0xff);
this.writeCString(key);
break;
}
case values_1.BsonMaxKey: {
writer.u8(0x7f);
this.writeCString(key);
break;
}
case values_1.BsonBinary: {
writer.u8(0x05);
this.writeCString(key);
const bin = value;
const length = bin.data.length;
this.writeInt32(length);
writer.u8(bin.subtype);
writer.buf(bin.data, length);
break;
}
default: {
writer.u8(0x03);
this.writeCString(key);
this.writeObj(value);
break;
}
}
break;
}
case 'boolean': {
writer.u8(0x08);
this.writeCString(key);
writer.u8(+value);
break;
}
case 'undefined': {
writer.u8(0x06);
this.writeCString(key);
break;
}
case 'symbol': {
writer.u8(0x0e);
this.writeCString(key);
this.writeStr(value.description || '');
break;
}
}
}
}
exports.BsonEncoder = BsonEncoder;
//# sourceMappingURL=BsonEncoder.js.map
File diff suppressed because one or more lines are too long
+2
View File
@@ -0,0 +1,2 @@
export * from './values';
export * from './BsonEncoder';
+6
View File
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
tslib_1.__exportStar(require("./values"), exports);
tslib_1.__exportStar(require("./BsonEncoder"), exports);
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/bson/index.ts"],"names":[],"mappings":";;;AAAA,mDAAyB;AACzB,wDAA8B"}
+54
View File
@@ -0,0 +1,54 @@
export declare class BsonObjectId {
timestamp: number;
process: number;
counter: number;
constructor(timestamp: number, process: number, counter: number);
}
export declare class BsonDbPointer {
name: string;
id: BsonObjectId;
constructor(name: string, id: BsonObjectId);
}
export declare class BsonJavascriptCode {
code: string;
constructor(code: string);
}
export declare class BsonSymbol {
symbol: string;
constructor(symbol: string);
}
export declare class BsonJavascriptCodeWithScope {
code: string;
scope: Record<string, unknown>;
constructor(code: string, scope: Record<string, unknown>);
}
export declare class BsonInt32 {
value: number;
constructor(value: number);
}
export declare class BsonInt64 {
value: number;
constructor(value: number);
}
export declare class BsonFloat {
value: number;
constructor(value: number);
}
export declare class BsonTimestamp {
increment: number;
timestamp: number;
constructor(increment: number, timestamp: number);
}
export declare class BsonDecimal128 {
data: Uint8Array;
constructor(data: Uint8Array);
}
export declare class BsonMinKey {
}
export declare class BsonMaxKey {
}
export declare class BsonBinary {
subtype: number;
data: Uint8Array;
constructor(subtype: number, data: Uint8Array);
}
+82
View File
@@ -0,0 +1,82 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BsonBinary = exports.BsonMaxKey = exports.BsonMinKey = exports.BsonDecimal128 = exports.BsonTimestamp = exports.BsonFloat = exports.BsonInt64 = exports.BsonInt32 = exports.BsonJavascriptCodeWithScope = exports.BsonSymbol = exports.BsonJavascriptCode = exports.BsonDbPointer = exports.BsonObjectId = void 0;
class BsonObjectId {
constructor(timestamp, process, counter) {
this.timestamp = timestamp;
this.process = process;
this.counter = counter;
}
}
exports.BsonObjectId = BsonObjectId;
class BsonDbPointer {
constructor(name, id) {
this.name = name;
this.id = id;
}
}
exports.BsonDbPointer = BsonDbPointer;
class BsonJavascriptCode {
constructor(code) {
this.code = code;
}
}
exports.BsonJavascriptCode = BsonJavascriptCode;
class BsonSymbol {
constructor(symbol) {
this.symbol = symbol;
}
}
exports.BsonSymbol = BsonSymbol;
class BsonJavascriptCodeWithScope {
constructor(code, scope) {
this.code = code;
this.scope = scope;
}
}
exports.BsonJavascriptCodeWithScope = BsonJavascriptCodeWithScope;
class BsonInt32 {
constructor(value) {
this.value = value;
}
}
exports.BsonInt32 = BsonInt32;
class BsonInt64 {
constructor(value) {
this.value = value;
}
}
exports.BsonInt64 = BsonInt64;
class BsonFloat {
constructor(value) {
this.value = value;
}
}
exports.BsonFloat = BsonFloat;
class BsonTimestamp {
constructor(increment, timestamp) {
this.increment = increment;
this.timestamp = timestamp;
}
}
exports.BsonTimestamp = BsonTimestamp;
class BsonDecimal128 {
constructor(data) {
this.data = data;
}
}
exports.BsonDecimal128 = BsonDecimal128;
class BsonMinKey {
}
exports.BsonMinKey = BsonMinKey;
class BsonMaxKey {
}
exports.BsonMaxKey = BsonMaxKey;
class BsonBinary {
constructor(subtype, data) {
this.subtype = subtype;
this.data = data;
}
}
exports.BsonBinary = BsonBinary;
//# sourceMappingURL=values.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"values.js","sourceRoot":"","sources":["../../src/bson/values.ts"],"names":[],"mappings":";;;AAAA,MAAa,YAAY;IACvB,YACS,SAAiB,EACjB,OAAe,EACf,OAAe;QAFf,cAAS,GAAT,SAAS,CAAQ;QACjB,YAAO,GAAP,OAAO,CAAQ;QACf,YAAO,GAAP,OAAO,CAAQ;IACrB,CAAC;CACL;AAND,oCAMC;AAED,MAAa,aAAa;IACxB,YACS,IAAY,EACZ,EAAgB;QADhB,SAAI,GAAJ,IAAI,CAAQ;QACZ,OAAE,GAAF,EAAE,CAAc;IACtB,CAAC;CACL;AALD,sCAKC;AAED,MAAa,kBAAkB;IAC7B,YAA0B,IAAY;QAAZ,SAAI,GAAJ,IAAI,CAAQ;IAAG,CAAC;CAC3C;AAFD,gDAEC;AAED,MAAa,UAAU;IACrB,YAA0B,MAAc;QAAd,WAAM,GAAN,MAAM,CAAQ;IAAG,CAAC;CAC7C;AAFD,gCAEC;AAED,MAAa,2BAA2B;IACtC,YACS,IAAY,EACZ,KAA8B;QAD9B,SAAI,GAAJ,IAAI,CAAQ;QACZ,UAAK,GAAL,KAAK,CAAyB;IACpC,CAAC;CACL;AALD,kEAKC;AAED,MAAa,SAAS;IACpB,YAA0B,KAAa;QAAb,UAAK,GAAL,KAAK,CAAQ;IAAG,CAAC;CAC5C;AAFD,8BAEC;AAED,MAAa,SAAS;IACpB,YAA0B,KAAa;QAAb,UAAK,GAAL,KAAK,CAAQ;IAAG,CAAC;CAC5C;AAFD,8BAEC;AAED,MAAa,SAAS;IACpB,YAA0B,KAAa;QAAb,UAAK,GAAL,KAAK,CAAQ;IAAG,CAAC;CAC5C;AAFD,8BAEC;AAED,MAAa,aAAa;IACxB,YACS,SAAiB,EACjB,SAAiB;QADjB,cAAS,GAAT,SAAS,CAAQ;QACjB,cAAS,GAAT,SAAS,CAAQ;IACvB,CAAC;CACL;AALD,sCAKC;AAED,MAAa,cAAc;IACzB,YAA0B,IAAgB;QAAhB,SAAI,GAAJ,IAAI,CAAY;IAAG,CAAC;CAC/C;AAFD,wCAEC;AAED,MAAa,UAAU;CAAG;AAA1B,gCAA0B;AAE1B,MAAa,UAAU;CAAG;AAA1B,gCAA0B;AAE1B,MAAa,UAAU;IACrB,YACS,OAAe,EACf,IAAgB;QADhB,YAAO,GAAP,OAAO,CAAQ;QACf,SAAI,GAAJ,IAAI,CAAY;IACtB,CAAC;CACL;AALD,gCAKC"}
+41
View File
@@ -0,0 +1,41 @@
import { CborDecoderBase } from './CborDecoderBase';
import { JsonPackValue } from '../JsonPackValue';
import type { Path } from '../json-pointer';
import type { IReader, IReaderResettable } from '@jsonjoy.com/util/lib/buffers';
export declare class CborDecoder<R extends IReader & IReaderResettable = IReader & IReaderResettable> extends CborDecoderBase<R> {
readAsMap(): Map<unknown, unknown>;
readMap(minor: number): Map<unknown, unknown>;
readMapRaw(length: number): Map<unknown, unknown>;
readMapIndef(): Map<unknown, unknown>;
skipN(n: number): void;
skipAny(): void;
skipAnyRaw(octet: number): void;
skipMinorLen(minor: number): number;
skipUNint(minor: number): void;
skipBin(minor: number): void;
skipBinChunk(): void;
skipStr(minor: number): void;
skipStrChunk(): void;
skipArr(minor: number): void;
skipObj(minor: number): void;
skipTag(minor: number): void;
skipTkn(minor: number): void;
validate(value: Uint8Array, offset?: number, size?: number): void;
decodeLevel(value: Uint8Array): unknown;
readLevel(): unknown;
readPrimitiveOrVal(): unknown | JsonPackValue;
readAsValue(): JsonPackValue;
readObjLevel(minor: number): Record<string, unknown>;
readObjRawLevel(length: number): Record<string, unknown>;
readObjIndefLevel(): Record<string, unknown>;
readArrLevel(minor: number): unknown[];
readArrRawLevel(length: number): unknown[];
readArrIndefLevel(): unknown[];
readHdr(expectedMajor: number): number;
readStrHdr(): number;
readObjHdr(): number;
readArrHdr(): number;
findKey(key: string): this;
findIndex(index: number): this;
find(path: Path): this;
}
+356
View File
@@ -0,0 +1,356 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CborDecoder = void 0;
const CborDecoderBase_1 = require("./CborDecoderBase");
const JsonPackValue_1 = require("../JsonPackValue");
class CborDecoder extends CborDecoderBase_1.CborDecoderBase {
readAsMap() {
const octet = this.reader.u8();
const major = octet >> 5;
const minor = octet & 31;
switch (major) {
case 5:
return this.readMap(minor);
default:
throw 0;
}
}
readMap(minor) {
const length = this.readMinorLen(minor);
if (length >= 0)
return this.readMapRaw(length);
else
return this.readMapIndef();
}
readMapRaw(length) {
const map = new Map();
for (let i = 0; i < length; i++) {
const key = this.val();
const value = this.val();
map.set(key, value);
}
return map;
}
readMapIndef() {
const map = new Map();
while (this.reader.peak() !== 255) {
const key = this.val();
if (this.reader.peak() === 255)
throw 7;
const value = this.val();
map.set(key, value);
}
this.reader.x++;
return map;
}
skipN(n) {
for (let i = 0; i < n; i++)
this.skipAny();
}
skipAny() {
this.skipAnyRaw(this.reader.u8());
}
skipAnyRaw(octet) {
const major = octet >> 5;
const minor = octet & 31;
switch (major) {
case 0:
case 1:
this.skipUNint(minor);
break;
case 2:
this.skipBin(minor);
break;
case 3:
this.skipStr(minor);
break;
case 4:
this.skipArr(minor);
break;
case 5:
this.skipObj(minor);
break;
case 7:
this.skipTkn(minor);
break;
case 6:
this.skipTag(minor);
break;
}
}
skipMinorLen(minor) {
if (minor <= 23)
return minor;
switch (minor) {
case 24:
return this.reader.u8();
case 25:
return this.reader.u16();
case 26:
return this.reader.u32();
case 27:
return Number(this.reader.u64());
case 31:
return -1;
default:
throw 1;
}
}
skipUNint(minor) {
if (minor <= 23)
return;
switch (minor) {
case 24:
return this.reader.skip(1);
case 25:
return this.reader.skip(2);
case 26:
return this.reader.skip(4);
case 27:
return this.reader.skip(8);
default:
throw 1;
}
}
skipBin(minor) {
const length = this.skipMinorLen(minor);
if (length >= 0)
this.reader.skip(length);
else {
while (this.reader.peak() !== 255)
this.skipBinChunk();
this.reader.x++;
}
}
skipBinChunk() {
const octet = this.reader.u8();
const major = octet >> 5;
const minor = octet & 31;
if (major !== 2)
throw 2;
if (minor > 27)
throw 3;
this.skipBin(minor);
}
skipStr(minor) {
const length = this.skipMinorLen(minor);
if (length >= 0)
this.reader.skip(length);
else {
while (this.reader.peak() !== 255)
this.skipStrChunk();
this.reader.x++;
}
}
skipStrChunk() {
const octet = this.reader.u8();
const major = octet >> 5;
const minor = octet & 31;
if (major !== 3)
throw 4;
if (minor > 27)
throw 5;
this.skipStr(minor);
}
skipArr(minor) {
const length = this.skipMinorLen(minor);
if (length >= 0)
this.skipN(length);
else {
while (this.reader.peak() !== 255)
this.skipAny();
this.reader.x++;
}
}
skipObj(minor) {
const length = this.readMinorLen(minor);
if (length >= 0)
return this.skipN(length * 2);
else {
while (this.reader.peak() !== 255) {
this.skipAny();
if (this.reader.peak() === 255)
throw 7;
this.skipAny();
}
this.reader.x++;
}
}
skipTag(minor) {
const length = this.skipMinorLen(minor);
if (length < 0)
throw 1;
this.skipAny();
}
skipTkn(minor) {
switch (minor) {
case 0xf8 & 31:
this.reader.skip(1);
return;
case 0xf9 & 31:
this.reader.skip(2);
return;
case 0xfa & 31:
this.reader.skip(4);
return;
case 0xfb & 31:
this.reader.skip(8);
return;
}
if (minor <= 23)
return;
throw 1;
}
validate(value, offset = 0, size = value.length) {
this.reader.reset(value);
this.reader.x = offset;
const start = offset;
this.skipAny();
const end = this.reader.x;
if (end - start !== size)
throw 8;
}
decodeLevel(value) {
this.reader.reset(value);
return this.readLevel();
}
readLevel() {
const octet = this.reader.u8();
const major = octet >> 5;
const minor = octet & 31;
switch (major) {
case 4:
return this.readArrLevel(minor);
case 5:
return this.readObjLevel(minor);
default:
return super.readAnyRaw(octet);
}
}
readPrimitiveOrVal() {
const octet = this.reader.peak();
const major = octet >> 5;
switch (major) {
case 4:
case 5:
return this.readAsValue();
default:
return this.val();
}
}
readAsValue() {
const reader = this.reader;
const start = reader.x;
this.skipAny();
const end = reader.x;
return new JsonPackValue_1.JsonPackValue(reader.uint8.subarray(start, end));
}
readObjLevel(minor) {
const length = this.readMinorLen(minor);
if (length >= 0)
return this.readObjRawLevel(length);
else
return this.readObjIndefLevel();
}
readObjRawLevel(length) {
const obj = {};
for (let i = 0; i < length; i++) {
const key = this.key();
const value = this.readPrimitiveOrVal();
obj[key] = value;
}
return obj;
}
readObjIndefLevel() {
const obj = {};
while (this.reader.peak() !== 255) {
const key = this.key();
if (this.reader.peak() === 255)
throw 7;
const value = this.readPrimitiveOrVal();
obj[key] = value;
}
this.reader.x++;
return obj;
}
readArrLevel(minor) {
const length = this.readMinorLen(minor);
if (length >= 0)
return this.readArrRawLevel(length);
return this.readArrIndefLevel();
}
readArrRawLevel(length) {
const arr = [];
for (let i = 0; i < length; i++)
arr.push(this.readPrimitiveOrVal());
return arr;
}
readArrIndefLevel() {
const arr = [];
while (this.reader.peak() !== 255)
arr.push(this.readPrimitiveOrVal());
this.reader.x++;
return arr;
}
readHdr(expectedMajor) {
const octet = this.reader.u8();
const major = octet >> 5;
if (major !== expectedMajor)
throw 0;
const minor = octet & 31;
if (minor < 24)
return minor;
switch (minor) {
case 24:
return this.reader.u8();
case 25:
return this.reader.u16();
case 26:
return this.reader.u32();
case 27:
return Number(this.reader.u64());
case 31:
return -1;
}
throw 1;
}
readStrHdr() {
return this.readHdr(3);
}
readObjHdr() {
return this.readHdr(5);
}
readArrHdr() {
return this.readHdr(4);
}
findKey(key) {
const size = this.readObjHdr();
for (let i = 0; i < size; i++) {
const k = this.key();
if (k === key)
return this;
this.skipAny();
}
throw 9;
}
findIndex(index) {
const size = this.readArrHdr();
if (index >= size)
throw 10;
for (let i = 0; i < index; i++)
this.skipAny();
return this;
}
find(path) {
for (let i = 0; i < path.length; i++) {
const segment = path[i];
if (typeof segment === 'string')
this.findKey(segment);
else
this.findIndex(segment);
}
return this;
}
}
exports.CborDecoder = CborDecoder;
//# sourceMappingURL=CborDecoder.js.map
File diff suppressed because one or more lines are too long
+34
View File
@@ -0,0 +1,34 @@
import { JsonPackExtension } from '../JsonPackExtension';
import { JsonPackValue } from '../JsonPackValue';
import type { CachedUtf8Decoder } from '@jsonjoy.com/util/lib/buffers/utf8/CachedUtf8Decoder';
import type { IReader, IReaderResettable } from '@jsonjoy.com/util/lib/buffers';
import type { BinaryJsonDecoder, PackValue } from '../types';
export declare class CborDecoderBase<R extends IReader & IReaderResettable = IReader & IReaderResettable> implements BinaryJsonDecoder {
reader: R;
readonly keyDecoder: CachedUtf8Decoder;
constructor(reader?: R, keyDecoder?: CachedUtf8Decoder);
read(uint8: Uint8Array): PackValue;
decode(uint8: Uint8Array): unknown;
val(): unknown;
readAnyRaw(octet: number): unknown;
readMinorLen(minor: number): number;
readUint(minor: number): number | bigint;
readNint(minor: number): number | bigint;
readBin(minor: number): Uint8Array;
readBinChunk(): Uint8Array;
readAsStr(): string;
readStr(minor: number): string;
readStrLen(minor: number): number;
readStrChunk(): string;
readArr(minor: number): unknown[];
readArrRaw(length: number): unknown[];
readArrIndef(): unknown[];
readObj(minor: number): Record<string, unknown>;
readObjRaw(length: number): Record<string, unknown>;
readObjIndef(): Record<string, unknown>;
key(): string;
readTag(minor: number): JsonPackExtension<unknown> | unknown;
readTagRaw(tag: number): JsonPackExtension<unknown> | unknown;
readTkn(minor: number): number | true | false | null | undefined | JsonPackValue<number>;
f16(): number;
}
+340
View File
@@ -0,0 +1,340 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CborDecoderBase = void 0;
const tslib_1 = require("tslib");
const f16_1 = require("@jsonjoy.com/util/lib/buffers/f16");
const JsonPackExtension_1 = require("../JsonPackExtension");
const JsonPackValue_1 = require("../JsonPackValue");
const Reader_1 = require("@jsonjoy.com/util/lib/buffers/Reader");
const sharedCachedUtf8Decoder_1 = tslib_1.__importDefault(require("@jsonjoy.com/util/lib/buffers/utf8/sharedCachedUtf8Decoder"));
class CborDecoderBase {
constructor(reader = new Reader_1.Reader(), keyDecoder = sharedCachedUtf8Decoder_1.default) {
this.reader = reader;
this.keyDecoder = keyDecoder;
}
read(uint8) {
this.reader.reset(uint8);
return this.val();
}
decode(uint8) {
this.reader.reset(uint8);
return this.val();
}
val() {
const reader = this.reader;
const octet = reader.u8();
const major = octet >> 5;
const minor = octet & 31;
if (major < 4) {
if (major < 2)
return major === 0 ? this.readUint(minor) : this.readNint(minor);
else
return major === 2 ? this.readBin(minor) : this.readStr(minor);
}
else {
if (major < 6)
return major === 4 ? this.readArr(minor) : this.readObj(minor);
else
return major === 6 ? this.readTag(minor) : this.readTkn(minor);
}
}
readAnyRaw(octet) {
const major = octet >> 5;
const minor = octet & 31;
if (major < 4) {
if (major < 2)
return major === 0 ? this.readUint(minor) : this.readNint(minor);
else
return major === 2 ? this.readBin(minor) : this.readStr(minor);
}
else {
if (major < 6)
return major === 4 ? this.readArr(minor) : this.readObj(minor);
else
return major === 6 ? this.readTag(minor) : this.readTkn(minor);
}
}
readMinorLen(minor) {
if (minor < 24)
return minor;
switch (minor) {
case 24:
return this.reader.u8();
case 25:
return this.reader.u16();
case 26:
return this.reader.u32();
case 27:
return Number(this.reader.u64());
case 31:
return -1;
default:
throw 1;
}
}
readUint(minor) {
if (minor < 25) {
return minor === 24 ? this.reader.u8() : minor;
}
else {
if (minor < 27) {
return minor === 25 ? this.reader.u16() : this.reader.u32();
}
else {
const num = this.reader.u64();
return num > 9007199254740991 ? num : Number(num);
}
}
}
readNint(minor) {
if (minor < 25) {
return minor === 24 ? -this.reader.u8() - 1 : -minor - 1;
}
else {
if (minor < 27) {
return minor === 25 ? -this.reader.u16() - 1 : -this.reader.u32() - 1;
}
else {
const num = this.reader.u64();
return num > 9007199254740991 - 1 ? -num - BigInt(1) : -Number(num) - 1;
}
}
}
readBin(minor) {
const reader = this.reader;
if (minor <= 23)
return reader.buf(minor);
switch (minor) {
case 24:
return reader.buf(reader.u8());
case 25:
return reader.buf(reader.u16());
case 26:
return reader.buf(reader.u32());
case 27:
return reader.buf(Number(reader.u64()));
case 31: {
let size = 0;
const list = [];
while (this.reader.peak() !== 255) {
const uint8 = this.readBinChunk();
size += uint8.length;
list.push(uint8);
}
this.reader.x++;
const res = new Uint8Array(size);
let offset = 0;
const length = list.length;
for (let i = 0; i < length; i++) {
const arr = list[i];
res.set(arr, offset);
offset += arr.length;
}
return res;
}
default:
throw 1;
}
}
readBinChunk() {
const octet = this.reader.u8();
const major = octet >> 5;
const minor = octet & 31;
if (major !== 2)
throw 2;
if (minor > 27)
throw 3;
return this.readBin(minor);
}
readAsStr() {
const reader = this.reader;
const octet = reader.u8();
const major = octet >> 5;
const minor = octet & 31;
if (major !== 3)
throw 11;
return this.readStr(minor);
}
readStr(minor) {
const reader = this.reader;
if (minor <= 23)
return reader.utf8(minor);
switch (minor) {
case 24:
return reader.utf8(reader.u8());
case 25:
return reader.utf8(reader.u16());
case 26:
return reader.utf8(reader.u32());
case 27:
return reader.utf8(Number(reader.u64()));
case 31: {
let str = '';
while (reader.peak() !== 255)
str += this.readStrChunk();
this.reader.x++;
return str;
}
default:
throw 1;
}
}
readStrLen(minor) {
if (minor <= 23)
return minor;
switch (minor) {
case 24:
return this.reader.u8();
case 25:
return this.reader.u16();
case 26:
return this.reader.u32();
case 27:
return Number(this.reader.u64());
default:
throw 1;
}
}
readStrChunk() {
const octet = this.reader.u8();
const major = octet >> 5;
const minor = octet & 31;
if (major !== 3)
throw 4;
if (minor > 27)
throw 5;
return this.readStr(minor);
}
readArr(minor) {
const length = this.readMinorLen(minor);
if (length >= 0)
return this.readArrRaw(length);
return this.readArrIndef();
}
readArrRaw(length) {
const arr = [];
for (let i = 0; i < length; i++)
arr.push(this.val());
return arr;
}
readArrIndef() {
const arr = [];
while (this.reader.peak() !== 255)
arr.push(this.val());
this.reader.x++;
return arr;
}
readObj(minor) {
if (minor < 28) {
let length = minor;
switch (minor) {
case 24:
length = this.reader.u8();
break;
case 25:
length = this.reader.u16();
break;
case 26:
length = this.reader.u32();
break;
case 27:
length = Number(this.reader.u64());
break;
}
const obj = {};
for (let i = 0; i < length; i++) {
const key = this.key();
if (key === '__proto__')
throw 6;
const value = this.val();
obj[key] = value;
}
return obj;
}
else if (minor === 31)
return this.readObjIndef();
else
throw 1;
}
readObjRaw(length) {
const obj = {};
for (let i = 0; i < length; i++) {
const key = this.key();
const value = this.val();
obj[key] = value;
}
return obj;
}
readObjIndef() {
const obj = {};
while (this.reader.peak() !== 255) {
const key = this.key();
if (this.reader.peak() === 255)
throw 7;
const value = this.val();
obj[key] = value;
}
this.reader.x++;
return obj;
}
key() {
const octet = this.reader.u8();
const major = octet >> 5;
const minor = octet & 31;
if (major !== 3)
return String(this.readAnyRaw(octet));
const length = this.readStrLen(minor);
if (length > 31)
return this.reader.utf8(length);
const key = this.keyDecoder.decode(this.reader.uint8, this.reader.x, length);
this.reader.skip(length);
return key;
}
readTag(minor) {
if (minor <= 23)
return this.readTagRaw(minor);
switch (minor) {
case 24:
return this.readTagRaw(this.reader.u8());
case 25:
return this.readTagRaw(this.reader.u16());
case 26:
return this.readTagRaw(this.reader.u32());
case 27:
return this.readTagRaw(Number(this.reader.u64()));
default:
throw 1;
}
}
readTagRaw(tag) {
return new JsonPackExtension_1.JsonPackExtension(tag, this.val());
}
readTkn(minor) {
switch (minor) {
case 0xf4 & 31:
return false;
case 0xf5 & 31:
return true;
case 0xf6 & 31:
return null;
case 0xf7 & 31:
return undefined;
case 0xf8 & 31:
return new JsonPackValue_1.JsonPackValue(this.reader.u8());
case 0xf9 & 31:
return this.f16();
case 0xfa & 31:
return this.reader.f32();
case 0xfb & 31:
return this.reader.f64();
}
if (minor <= 23)
return new JsonPackValue_1.JsonPackValue(minor);
throw 1;
}
f16() {
return (0, f16_1.decodeF16)(this.reader.u16());
}
}
exports.CborDecoderBase = CborDecoderBase;
//# sourceMappingURL=CborDecoderBase.js.map
File diff suppressed because one or more lines are too long
+5
View File
@@ -0,0 +1,5 @@
import { JsonPackExtension } from '../JsonPackExtension';
import { CborDecoder } from './CborDecoder';
export declare class CborDecoderDag extends CborDecoder {
readTagRaw(tag: number): JsonPackExtension<unknown> | unknown;
}
+13
View File
@@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CborDecoderDag = void 0;
const JsonPackExtension_1 = require("../JsonPackExtension");
const CborDecoder_1 = require("./CborDecoder");
class CborDecoderDag extends CborDecoder_1.CborDecoder {
readTagRaw(tag) {
const value = this.val();
return tag === 42 ? new JsonPackExtension_1.JsonPackExtension(tag, value) : value;
}
}
exports.CborDecoderDag = CborDecoderDag;
//# sourceMappingURL=CborDecoderDag.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"CborDecoderDag.js","sourceRoot":"","sources":["../../src/cbor/CborDecoderDag.ts"],"names":[],"mappings":";;;AAAA,4DAAuD;AACvD,+CAA0C;AAE1C,MAAa,cAAe,SAAQ,yBAAW;IACtC,UAAU,CAAC,GAAW;QAC3B,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACzB,OAAO,GAAG,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,qCAAiB,CAAU,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IACzE,CAAC;CACF;AALD,wCAKC"}
+9
View File
@@ -0,0 +1,9 @@
import { CborEncoderFast } from './CborEncoderFast';
import type { IWriter, IWriterGrowable } from '@jsonjoy.com/util/lib/buffers';
export declare class CborEncoder<W extends IWriter & IWriterGrowable = IWriter & IWriterGrowable> extends CborEncoderFast<W> {
writeUnknown(value: unknown): void;
writeAny(value: unknown): void;
writeFloat(float: number): void;
writeMap(map: Map<unknown, unknown>): void;
writeUndef(): void;
}
+74
View File
@@ -0,0 +1,74 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CborEncoder = void 0;
const isFloat32_1 = require("@jsonjoy.com/util/lib/buffers/isFloat32");
const JsonPackExtension_1 = require("../JsonPackExtension");
const CborEncoderFast_1 = require("./CborEncoderFast");
const JsonPackValue_1 = require("../JsonPackValue");
class CborEncoder extends CborEncoderFast_1.CborEncoderFast {
writeUnknown(value) {
this.writeNull();
}
writeAny(value) {
switch (typeof value) {
case 'number':
return this.writeNumber(value);
case 'string':
return this.writeStr(value);
case 'boolean':
return this.writer.u8(0xf4 + +value);
case 'object': {
if (!value)
return this.writer.u8(0xf6);
const constructor = value.constructor;
switch (constructor) {
case Object:
return this.writeObj(value);
case Array:
return this.writeArr(value);
case Uint8Array:
return this.writeBin(value);
case Map:
return this.writeMap(value);
case JsonPackExtension_1.JsonPackExtension:
return this.writeTag(value.tag, value.val);
case JsonPackValue_1.JsonPackValue:
const buf = value.val;
return this.writer.buf(buf, buf.length);
default:
if (value instanceof Uint8Array)
return this.writeBin(value);
if (Array.isArray(value))
return this.writeArr(value);
if (value instanceof Map)
return this.writeMap(value);
return this.writeUnknown(value);
}
}
case 'undefined':
return this.writeUndef();
case 'bigint':
return this.writeBigInt(value);
default:
return this.writeUnknown(value);
}
}
writeFloat(float) {
if ((0, isFloat32_1.isFloat32)(float))
this.writer.u8f32(0xfa, float);
else
this.writer.u8f64(0xfb, float);
}
writeMap(map) {
this.writeMapHdr(map.size);
map.forEach((value, key) => {
this.writeAny(key);
this.writeAny(value);
});
}
writeUndef() {
this.writer.u8(0xf7);
}
}
exports.CborEncoder = CborEncoder;
//# sourceMappingURL=CborEncoder.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"CborEncoder.js","sourceRoot":"","sources":["../../src/cbor/CborEncoder.ts"],"names":[],"mappings":";;;AAAA,uEAAkE;AAClE,4DAAuD;AACvD,uDAAkD;AAClD,oDAA+C;AAG/C,MAAa,WAA6E,SAAQ,iCAAkB;IAM3G,YAAY,CAAC,KAAc;QAChC,IAAI,CAAC,SAAS,EAAE,CAAC;IACnB,CAAC;IAEM,QAAQ,CAAC,KAAc;QAC5B,QAAQ,OAAO,KAAK,EAAE,CAAC;YACrB,KAAK,QAAQ;gBACX,OAAO,IAAI,CAAC,WAAW,CAAC,KAAe,CAAC,CAAC;YAC3C,KAAK,QAAQ;gBACX,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;YAC9B,KAAK,SAAS;gBACZ,OAAO,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;YACvC,KAAK,QAAQ,CAAC,CAAC,CAAC;gBACd,IAAI,CAAC,KAAK;oBAAE,OAAO,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;gBACxC,MAAM,WAAW,GAAG,KAAK,CAAC,WAAW,CAAC;gBACtC,QAAQ,WAAW,EAAE,CAAC;oBACpB,KAAK,MAAM;wBACT,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAgC,CAAC,CAAC;oBACzD,KAAK,KAAK;wBACR,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAkB,C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,CAAC,CAAC;IACtC,CAAC;IAEM,QAAQ,CAAC,GAA0B;QACxC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC3B,GAAG,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;YACzB,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;YACnB,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QACvB,CAAC,CAAC,CAAC;IACL,CAAC;IAEM,UAAU;QACf,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;IACvB,CAAC;CACF;AAnED,kCAmEC"}
+6
View File
@@ -0,0 +1,6 @@
import { CborEncoderStable } from './CborEncoderStable';
export declare class CborEncoderDag extends CborEncoderStable {
writeUndef(): void;
writeFloat(float: number): void;
writeTag(tag: number, value: unknown): void;
}
+23
View File
@@ -0,0 +1,23 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CborEncoderDag = void 0;
const CborEncoderStable_1 = require("./CborEncoderStable");
class CborEncoderDag extends CborEncoderStable_1.CborEncoderStable {
writeUndef() {
this.writeNull();
}
writeFloat(float) {
if (float !== float)
return this.writeNull();
if (!Number.isFinite(float))
return this.writeNull();
this.writer.u8f64(0xfb, float);
}
writeTag(tag, value) {
if (tag === 42)
this.writeTagHdr(tag);
this.writeAny(value);
}
}
exports.CborEncoderDag = CborEncoderDag;
//# sourceMappingURL=CborEncoderDag.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"CborEncoderDag.js","sourceRoot":"","sources":["../../src/cbor/CborEncoderDag.ts"],"names":[],"mappings":";;;AAAA,2DAAsD;AAEtD,MAAa,cAAe,SAAQ,qCAAiB;IAC5C,UAAU;QACf,IAAI,CAAC,SAAS,EAAE,CAAC;IACnB,CAAC;IAEM,UAAU,CAAC,KAAa;QAC7B,IAAI,KAAK,KAAK,KAAK;YAAE,OAAO,IAAI,CAAC,SAAS,EAAE,CAAC;QAC7C,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC,SAAS,EAAE,CAAC;QACrD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IACjC,CAAC;IAEM,QAAQ,CAAC,GAAW,EAAE,KAAc;QACzC,IAAI,GAAG,KAAK,EAAE;YAAE,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;QACtC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;CACF;AAfD,wCAeC"}
+51
View File
@@ -0,0 +1,51 @@
import type { IWriter, IWriterGrowable } from '@jsonjoy.com/util/lib/buffers';
import type { BinaryJsonEncoder, StreamingBinaryJsonEncoder, TlvBinaryJsonEncoder } from '../types';
import type { Slice } from '@jsonjoy.com/util/lib/buffers/Slice';
export declare class CborEncoderFast<W extends IWriter & IWriterGrowable = IWriter & IWriterGrowable> implements BinaryJsonEncoder, StreamingBinaryJsonEncoder, TlvBinaryJsonEncoder {
readonly writer: W;
constructor(writer?: W);
encode(value: unknown): Uint8Array;
encodeToSlice(value: unknown): Slice;
writeAny(value: unknown): void;
writeCbor(): void;
writeEnd(): void;
writeNull(): void;
writeBoolean(bool: boolean): void;
writeNumber(num: number): void;
writeBigInt(int: bigint): void;
writeBigUint(uint: bigint): void;
writeBigSint(int: bigint): void;
writeInteger(int: number): void;
writeUInteger(uint: number): void;
encodeNumber(num: number): void;
encodeInteger(int: number): void;
encodeUint(uint: number): void;
encodeNint(int: number): void;
writeFloat(float: number): void;
writeBin(buf: Uint8Array): void;
writeBinHdr(length: number): void;
writeStr(str: string): void;
writeStrHdr(length: number): void;
writeAsciiStr(str: string): void;
writeArr(arr: unknown[]): void;
writeArrHdr(length: number): void;
writeObj(obj: Record<string, unknown>): void;
writeObjHdr(length: number): void;
writeMapHdr(length: number): void;
writeStartMap(): void;
writeTag(tag: number, value: unknown): void;
writeTagHdr(tag: number): void;
writeTkn(value: number): void;
writeStartStr(): void;
writeStrChunk(str: string): void;
writeEndStr(): void;
writeStartBin(): void;
writeBinChunk(buf: Uint8Array): void;
writeEndBin(): void;
writeStartArr(): void;
writeArrChunk(item: unknown): void;
writeEndArr(): void;
writeStartObj(): void;
writeObjChunk(key: string, value: unknown): void;
writeEndObj(): void;
}
+333
View File
@@ -0,0 +1,333 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CborEncoderFast = void 0;
const Writer_1 = require("@jsonjoy.com/util/lib/buffers/Writer");
const isSafeInteger = Number.isSafeInteger;
class CborEncoderFast {
constructor(writer = new Writer_1.Writer()) {
this.writer = writer;
}
encode(value) {
this.writeAny(value);
return this.writer.flush();
}
encodeToSlice(value) {
this.writeAny(value);
return this.writer.flushSlice();
}
writeAny(value) {
switch (typeof value) {
case 'number':
return this.writeNumber(value);
case 'string':
return this.writeStr(value);
case 'boolean':
return this.writer.u8(0xf4 + +value);
case 'object': {
if (!value)
return this.writer.u8(0xf6);
const constructor = value.constructor;
switch (constructor) {
case Array:
return this.writeArr(value);
default:
return this.writeObj(value);
}
}
}
}
writeCbor() {
this.writer.u8u16(0xd9, 0xd9f7);
}
writeEnd() {
this.writer.u8(255);
}
writeNull() {
this.writer.u8(0xf6);
}
writeBoolean(bool) {
if (bool)
this.writer.u8(0xf5);
else
this.writer.u8(0xf4);
}
writeNumber(num) {
if (isSafeInteger(num))
this.writeInteger(num);
else if (typeof num === 'bigint')
this.writeBigInt(num);
else
this.writeFloat(num);
}
writeBigInt(int) {
if (int >= 0)
this.writeBigUint(int);
else
this.writeBigSint(int);
}
writeBigUint(uint) {
if (uint <= Number.MAX_SAFE_INTEGER)
return this.writeUInteger(Number(uint));
this.writer.u8u64(0x1b, uint);
}
writeBigSint(int) {
if (int >= Number.MIN_SAFE_INTEGER)
return this.encodeNint(Number(int));
const uint = -BigInt(1) - int;
this.writer.u8u64(0x3b, uint);
}
writeInteger(int) {
if (int >= 0)
this.writeUInteger(int);
else
this.encodeNint(int);
}
writeUInteger(uint) {
const writer = this.writer;
writer.ensureCapacity(9);
const uint8 = writer.uint8;
let x = writer.x;
if (uint <= 23) {
uint8[x++] = 0 + uint;
}
else if (uint <= 0xff) {
uint8[x++] = 0x18;
uint8[x++] = uint;
}
else if (uint <= 0xffff) {
uint8[x++] = 0x19;
writer.view.setUint16(x, uint);
x += 2;
}
else if (uint <= 0xffffffff) {
uint8[x++] = 0x1a;
writer.view.setUint32(x, uint);
x += 4;
}
else {
uint8[x++] = 0x1b;
writer.view.setBigUint64(x, BigInt(uint));
x += 8;
}
writer.x = x;
}
encodeNumber(num) {
this.writeNumber(num);
}
encodeInteger(int) {
this.writeInteger(int);
}
encodeUint(uint) {
this.writeUInteger(uint);
}
encodeNint(int) {
const uint = -1 - int;
const writer = this.writer;
writer.ensureCapacity(9);
const uint8 = writer.uint8;
let x = writer.x;
if (uint < 24) {
uint8[x++] = 32 + uint;
}
else if (uint <= 0xff) {
uint8[x++] = 0x38;
uint8[x++] = uint;
}
else if (uint <= 0xffff) {
uint8[x++] = 0x39;
writer.view.setUint16(x, uint);
x += 2;
}
else if (uint <= 0xffffffff) {
uint8[x++] = 0x3a;
writer.view.setUint32(x, uint);
x += 4;
}
else {
uint8[x++] = 0x3b;
writer.view.setBigUint64(x, BigInt(uint));
x += 8;
}
writer.x = x;
}
writeFloat(float) {
this.writer.u8f64(0xfb, float);
}
writeBin(buf) {
const length = buf.length;
this.writeBinHdr(length);
this.writer.buf(buf, length);
}
writeBinHdr(length) {
const writer = this.writer;
if (length <= 23)
writer.u8(64 + length);
else if (length <= 0xff)
writer.u16((0x58 << 8) + length);
else if (length <= 0xffff)
writer.u8u16(0x59, length);
else if (length <= 0xffffffff)
writer.u8u32(0x5a, length);
else
writer.u8u64(0x5b, length);
}
writeStr(str) {
const writer = this.writer;
const length = str.length;
const maxSize = length * 4;
writer.ensureCapacity(5 + maxSize);
const uint8 = writer.uint8;
let lengthOffset = writer.x;
if (maxSize <= 23)
writer.x++;
else if (maxSize <= 0xff) {
uint8[writer.x++] = 0x78;
lengthOffset = writer.x;
writer.x++;
}
else if (maxSize <= 0xffff) {
uint8[writer.x++] = 0x79;
lengthOffset = writer.x;
writer.x += 2;
}
else {
uint8[writer.x++] = 0x7a;
lengthOffset = writer.x;
writer.x += 4;
}
const bytesWritten = writer.utf8(str);
if (maxSize <= 23)
uint8[lengthOffset] = 96 + bytesWritten;
else if (maxSize <= 0xff)
uint8[lengthOffset] = bytesWritten;
else if (maxSize <= 0xffff)
writer.view.setUint16(lengthOffset, bytesWritten);
else
writer.view.setUint32(lengthOffset, bytesWritten);
}
writeStrHdr(length) {
const writer = this.writer;
if (length <= 23)
writer.u8(96 + length);
else if (length <= 0xff)
writer.u16((0x78 << 8) + length);
else if (length <= 0xffff)
writer.u8u16(0x79, length);
else
writer.u8u32(0x7a, length);
}
writeAsciiStr(str) {
this.writeStrHdr(str.length);
this.writer.ascii(str);
}
writeArr(arr) {
const length = arr.length;
this.writeArrHdr(length);
for (let i = 0; i < length; i++)
this.writeAny(arr[i]);
}
writeArrHdr(length) {
const writer = this.writer;
if (length <= 23)
writer.u8(128 + length);
else if (length <= 0xff)
writer.u16((0x98 << 8) + length);
else if (length <= 0xffff)
writer.u8u16(0x99, length);
else if (length <= 0xffffffff)
writer.u8u32(0x9a, length);
else
writer.u8u64(0x9b, length);
}
writeObj(obj) {
const keys = Object.keys(obj);
const length = keys.length;
this.writeObjHdr(length);
for (let i = 0; i < length; i++) {
const key = keys[i];
this.writeStr(key);
this.writeAny(obj[key]);
}
}
writeObjHdr(length) {
const writer = this.writer;
if (length <= 23)
writer.u8(160 + length);
else if (length <= 0xff)
writer.u16((0xb8 << 8) + length);
else if (length <= 0xffff)
writer.u8u16(0xb9, length);
else if (length <= 0xffffffff)
writer.u8u32(0xba, length);
else
writer.u8u64(0xbb, length);
}
writeMapHdr(length) {
this.writeObjHdr(length);
}
writeStartMap() {
this.writer.u8(0xbf);
}
writeTag(tag, value) {
this.writeTagHdr(tag);
this.writeAny(value);
}
writeTagHdr(tag) {
const writer = this.writer;
if (tag <= 23)
writer.u8(192 + tag);
else if (tag <= 0xff)
writer.u16((0xd8 << 8) + tag);
else if (tag <= 0xffff)
writer.u8u16(0xd9, tag);
else if (tag <= 0xffffffff)
writer.u8u32(0xda, tag);
else
writer.u8u64(0xdb, tag);
}
writeTkn(value) {
const writer = this.writer;
if (value <= 23)
writer.u8(224 + value);
else if (value <= 0xff)
writer.u16((0xf8 << 8) + value);
}
writeStartStr() {
this.writer.u8(0x7f);
}
writeStrChunk(str) {
throw new Error('Not implemented');
}
writeEndStr() {
throw new Error('Not implemented');
}
writeStartBin() {
this.writer.u8(0x5f);
}
writeBinChunk(buf) {
throw new Error('Not implemented');
}
writeEndBin() {
throw new Error('Not implemented');
}
writeStartArr() {
this.writer.u8(0x9f);
}
writeArrChunk(item) {
throw new Error('Not implemented');
}
writeEndArr() {
this.writer.u8(255);
}
writeStartObj() {
this.writer.u8(0xbf);
}
writeObjChunk(key, value) {
throw new Error('Not implemented');
}
writeEndObj() {
this.writer.u8(255);
}
}
exports.CborEncoderFast = CborEncoderFast;
//# sourceMappingURL=CborEncoderFast.js.map

Some files were not shown because too many files have changed in this diff Show More