@alexgyver/bson 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -1
- package/bson.js +13 -6
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,2 +1,19 @@
|
|
|
1
1
|
# bson.js
|
|
2
|
-
|
|
2
|
+
Распаковщик бинарного JSON для библиотеки [BSON](https://github.com/GyverLibs/BSON).
|
|
3
|
+
|
|
4
|
+
> npm i @alexgyver/bson
|
|
5
|
+
|
|
6
|
+
```js
|
|
7
|
+
const codes = [
|
|
8
|
+
'some',
|
|
9
|
+
'string',
|
|
10
|
+
'constants',
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
let json;
|
|
14
|
+
const res = await fetch(...);
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
json = decodeBson(new Uint8Array(await res.arrayBuffer()), codes);
|
|
18
|
+
} catch (e) { }
|
|
19
|
+
```
|
package/bson.js
CHANGED
|
@@ -18,6 +18,9 @@ export default function decodeBson(b, codes = []) {
|
|
|
18
18
|
const BS_BINARY = (7 << 5);
|
|
19
19
|
const BS_BIN_PREFIX = "__BSON_BINARY";
|
|
20
20
|
|
|
21
|
+
const BS_NEGATIVE = (1 << 4);
|
|
22
|
+
const BS_BOOLEAN = (1 << 3);
|
|
23
|
+
|
|
21
24
|
const BS_CONT_OBJ = (1 << 4);
|
|
22
25
|
const BS_CONT_OPEN = (1 << 3);
|
|
23
26
|
|
|
@@ -76,13 +79,17 @@ export default function decodeBson(b, codes = []) {
|
|
|
76
79
|
} break;
|
|
77
80
|
|
|
78
81
|
case BS_VAL_INT: {
|
|
79
|
-
if (data &
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
if (data & BS_BOOLEAN) {
|
|
83
|
+
s += (data & 0b1) ? 'true' : 'false';
|
|
84
|
+
} else {
|
|
85
|
+
if (data & BS_NEGATIVE) s += '-';
|
|
86
|
+
let len = data & 0b111;
|
|
87
|
+
let v = BigInt(0);
|
|
88
|
+
for (let j = 0; j < len; j++) {
|
|
89
|
+
v |= BigInt(b[++i]) << BigInt(j * 8);
|
|
90
|
+
}
|
|
91
|
+
s += v;
|
|
84
92
|
}
|
|
85
|
-
s += v;
|
|
86
93
|
s += ',';
|
|
87
94
|
} break;
|
|
88
95
|
|
package/package.json
CHANGED