@alexgyver/bson 1.0.0
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/.gitattributes +2 -0
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/bson.js +124 -0
- package/package.json +14 -0
package/.gitattributes
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 AlexGyver
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/bson.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {Uint8Array} b
|
|
3
|
+
* @param {Array} codes
|
|
4
|
+
* @returns {Object}
|
|
5
|
+
*/
|
|
6
|
+
export default function decodeBson(b, codes = []) {
|
|
7
|
+
if (!b || !(b instanceof Uint8Array)) return null;
|
|
8
|
+
if (!b.length) return {};
|
|
9
|
+
let bins = [];
|
|
10
|
+
|
|
11
|
+
const BS_KEY_CODE = (0 << 5);
|
|
12
|
+
const BS_KEY_STR = (1 << 5);
|
|
13
|
+
const BS_VAL_CODE = (2 << 5);
|
|
14
|
+
const BS_VAL_STR = (3 << 5);
|
|
15
|
+
const BS_VAL_INT = (4 << 5);
|
|
16
|
+
const BS_VAL_FLOAT = (5 << 5);
|
|
17
|
+
const BS_CONTAINER = (6 << 5);
|
|
18
|
+
const BS_BINARY = (7 << 5);
|
|
19
|
+
const BS_BIN_PREFIX = "__BSON_BINARY";
|
|
20
|
+
|
|
21
|
+
const BS_CONT_OBJ = (1 << 4);
|
|
22
|
+
const BS_CONT_OPEN = (1 << 3);
|
|
23
|
+
|
|
24
|
+
function u32toFloat(u32) {
|
|
25
|
+
return new Float32Array(new Uint32Array([u32]).buffer)[0];
|
|
26
|
+
}
|
|
27
|
+
function unpack5(msb5, lsb) {
|
|
28
|
+
return ((msb5 << 8) | lsb) >>> 0;
|
|
29
|
+
}
|
|
30
|
+
function makeBins(obj) {
|
|
31
|
+
if (typeof obj !== 'object') return;
|
|
32
|
+
for (let k in obj) {
|
|
33
|
+
if (typeof obj[k] === "object" && obj[k] !== null) {
|
|
34
|
+
makeBins(obj[k]);
|
|
35
|
+
} else if (typeof obj[k] === "string" && obj[k].startsWith(BS_BIN_PREFIX)) {
|
|
36
|
+
obj[k] = bins[obj[k].split('#')[1]];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let s = '';
|
|
42
|
+
for (let i = 0; i < b.length; i++) {
|
|
43
|
+
const type = b[i] & 0b11100000;
|
|
44
|
+
const data = b[i] & 0b00011111;
|
|
45
|
+
|
|
46
|
+
switch (type) {
|
|
47
|
+
case BS_CONTAINER:
|
|
48
|
+
if (data & BS_CONT_OPEN) {
|
|
49
|
+
s += (data & BS_CONT_OBJ) ? '{' : '[';
|
|
50
|
+
} else {
|
|
51
|
+
if (s[s.length - 1] == ',') s = s.slice(0, -1);
|
|
52
|
+
s += (data & BS_CONT_OBJ) ? '}' : ']';
|
|
53
|
+
s += ',';
|
|
54
|
+
}
|
|
55
|
+
break;
|
|
56
|
+
|
|
57
|
+
case BS_KEY_CODE:
|
|
58
|
+
case BS_VAL_CODE:
|
|
59
|
+
s += '"' + codes[unpack5(data, b[++i])] + '"';
|
|
60
|
+
s += (type == BS_KEY_CODE) ? ':' : ',';
|
|
61
|
+
break;
|
|
62
|
+
|
|
63
|
+
case BS_KEY_STR:
|
|
64
|
+
case BS_VAL_STR: {
|
|
65
|
+
let len = unpack5(data, b[++i]);
|
|
66
|
+
i++;
|
|
67
|
+
let txt = new TextDecoder().decode(b.slice(i, i + len));
|
|
68
|
+
txt = txt.replaceAll(/([^\\])\\([^\"\\nrt])/ig, "$1\\\\$2")
|
|
69
|
+
.replaceAll(/\t/ig, "\\t")
|
|
70
|
+
.replaceAll(/\n/ig, "\\n")
|
|
71
|
+
.replaceAll(/\r/ig, "\\r")
|
|
72
|
+
.replaceAll(/([^\\])(")/ig, '$1\\"');
|
|
73
|
+
s += '"' + txt + '"';
|
|
74
|
+
s += (type == BS_KEY_STR) ? ':' : ',';
|
|
75
|
+
i += len - 1;
|
|
76
|
+
} break;
|
|
77
|
+
|
|
78
|
+
case BS_VAL_INT: {
|
|
79
|
+
if (data & 0b10000) s += '-';
|
|
80
|
+
let len = data & 0b01111;
|
|
81
|
+
let v = BigInt(0);
|
|
82
|
+
for (let j = 0; j < len; j++) {
|
|
83
|
+
v |= BigInt(b[++i]) << BigInt(j * 8);
|
|
84
|
+
}
|
|
85
|
+
s += v;
|
|
86
|
+
s += ',';
|
|
87
|
+
} break;
|
|
88
|
+
|
|
89
|
+
case BS_VAL_FLOAT: {
|
|
90
|
+
let v = 0;
|
|
91
|
+
for (let j = 0; j < 4; j++) {
|
|
92
|
+
v |= b[++i] << (j * 8);
|
|
93
|
+
}
|
|
94
|
+
let f = u32toFloat(v);
|
|
95
|
+
if (isNaN(f)) {
|
|
96
|
+
s += '"NaN"';
|
|
97
|
+
} else if (!isFinite(f)) {
|
|
98
|
+
s += '"Infinity"';
|
|
99
|
+
} else {
|
|
100
|
+
s += f.toFixed(data);
|
|
101
|
+
}
|
|
102
|
+
s += ',';
|
|
103
|
+
} break;
|
|
104
|
+
|
|
105
|
+
case BS_BINARY: {
|
|
106
|
+
let len = unpack5(data, b[++i]);
|
|
107
|
+
i++;
|
|
108
|
+
s += '"' + BS_BIN_PREFIX + '#' + bins.length + '",';
|
|
109
|
+
bins.push(b.slice(i, i + len));
|
|
110
|
+
i += len - 1;
|
|
111
|
+
} break;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (s[s.length - 1] == ',') s = s.slice(0, -1);
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
let obj = JSON.parse(s);
|
|
118
|
+
if (bins.length) makeBins(obj);
|
|
119
|
+
return obj;
|
|
120
|
+
} catch (e) {
|
|
121
|
+
console.log(s);
|
|
122
|
+
throw new Error("JSON error")
|
|
123
|
+
}
|
|
124
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alexgyver/bson",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Decode BSON from GSON Arduino library",
|
|
5
|
+
"main": "./bson.js",
|
|
6
|
+
"module": "./bson.js",
|
|
7
|
+
"types": "./bson.js",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/GyverLibs/bson.js.git"
|
|
11
|
+
},
|
|
12
|
+
"author": "AlexGyver <alex@alexgyver.ru>",
|
|
13
|
+
"license": "MIT"
|
|
14
|
+
}
|