@alexgyver/bson 1.0.2 → 2.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/bson.js +10 -15
- package/package.json +1 -1
- package/test.html +35 -0
package/bson.js
CHANGED
|
@@ -66,25 +66,23 @@ export default function decodeBson(b, codes = []) {
|
|
|
66
66
|
|
|
67
67
|
case BS_STRING: {
|
|
68
68
|
let len = unpack5(data, b[++i]);
|
|
69
|
-
i
|
|
70
|
-
let txt = new TextDecoder().decode(b.slice(i, i + len));
|
|
69
|
+
let txt = new TextDecoder().decode(b.slice(i + 1, i + 1 + len));
|
|
71
70
|
txt = txt.replaceAll(/([^\\])\\([^\"\\nrt])/ig, "$1\\\\$2")
|
|
72
71
|
.replaceAll(/\t/ig, "\\t")
|
|
73
72
|
.replaceAll(/\n/ig, "\\n")
|
|
74
73
|
.replaceAll(/\r/ig, "\\r")
|
|
75
74
|
.replaceAll(/([^\\])(")/ig, '$1\\"');
|
|
76
75
|
s += '"' + txt + '"';
|
|
77
|
-
i += len
|
|
76
|
+
i += len;
|
|
78
77
|
} break;
|
|
79
78
|
|
|
80
79
|
case BS_INTEGER: {
|
|
81
80
|
if (data & BS_NEGATIVE) s += '-';
|
|
82
81
|
let len = data & 0b1111;
|
|
83
|
-
let
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
s += v;
|
|
82
|
+
let u8 = new Uint8Array(8);
|
|
83
|
+
u8.set(b.slice(i + 1, i + 1 + len));
|
|
84
|
+
s += new BigUint64Array(u8.buffer)[0];
|
|
85
|
+
i += len;
|
|
88
86
|
} break;
|
|
89
87
|
|
|
90
88
|
case BS_BOOLEAN:
|
|
@@ -92,12 +90,9 @@ export default function decodeBson(b, codes = []) {
|
|
|
92
90
|
break;
|
|
93
91
|
|
|
94
92
|
case BS_FLOAT: {
|
|
95
|
-
let
|
|
96
|
-
for (let j = 0; j < 4; j++) {
|
|
97
|
-
v |= b[++i] << (j * 8);
|
|
98
|
-
}
|
|
99
|
-
let f = new Float32Array(new Uint32Array([v]).buffer)[0];
|
|
93
|
+
let f = new Float32Array(b.slice(i + 1, i + 1 + 4).buffer)[0];
|
|
100
94
|
s += (isNaN(f) || !isFinite(f)) ? 'null' : f.toFixed(data);
|
|
95
|
+
i += 4;
|
|
101
96
|
} break;
|
|
102
97
|
|
|
103
98
|
case BS_BINARY: {
|
|
@@ -118,7 +113,7 @@ export default function decodeBson(b, codes = []) {
|
|
|
118
113
|
}
|
|
119
114
|
}
|
|
120
115
|
} catch (e) {
|
|
121
|
-
console.log(s);
|
|
116
|
+
console.log(e, s);
|
|
122
117
|
throw new Error("BSON decode error");
|
|
123
118
|
}
|
|
124
119
|
|
|
@@ -129,7 +124,7 @@ export default function decodeBson(b, codes = []) {
|
|
|
129
124
|
if (bins.length) makeBins(obj, bins);
|
|
130
125
|
return obj;
|
|
131
126
|
} catch (e) {
|
|
132
|
-
console.log(s);
|
|
127
|
+
console.log(e, s);
|
|
133
128
|
throw new Error("JSON parse error");
|
|
134
129
|
}
|
|
135
130
|
}
|
package/package.json
CHANGED
package/test.html
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
|
|
3
|
+
<body>
|
|
4
|
+
<button id="req">REQUEST</button>
|
|
5
|
+
<p id="out" style="white-space: break-spaces;"></p>
|
|
6
|
+
|
|
7
|
+
<script type="module">
|
|
8
|
+
import decodeBson from '/bson.js';
|
|
9
|
+
|
|
10
|
+
const url = 'http://192.168.1.54';
|
|
11
|
+
|
|
12
|
+
const codes = [
|
|
13
|
+
'some',
|
|
14
|
+
'string',
|
|
15
|
+
'constants',
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
req.onclick = async () => {
|
|
19
|
+
let res = await fetch(url + '/bson');
|
|
20
|
+
try {
|
|
21
|
+
let arr = new Uint8Array(await res.arrayBuffer());
|
|
22
|
+
let json = decodeBson(arr, codes);
|
|
23
|
+
console.log(arr);
|
|
24
|
+
console.log(JSON.stringify(json), JSON.stringify(json).length);
|
|
25
|
+
console.log(json);
|
|
26
|
+
out.innerText = JSON.stringify(json, null, 2);
|
|
27
|
+
} catch (e) {
|
|
28
|
+
console.log(e);
|
|
29
|
+
out.innerText = e;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
</script>
|
|
33
|
+
</body>
|
|
34
|
+
|
|
35
|
+
</html>
|