@alexgyver/bson 2.1.0 → 2.2.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/README.md +27 -7
- package/bson.js +231 -238
- package/bson.min.js +1 -1
- package/package.json +24 -20
- package/{test.html → test/index.html} +3 -5
- package/webpack.dev.config.js +24 -0
package/README.md
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
# bson.js
|
|
2
|
-
Распаковщик бинарного JSON для библиотеки [BSON](https://github.com/GyverLibs/BSON).
|
|
2
|
+
Распаковщик и запаковщик бинарного JSON для библиотеки [BSON](https://github.com/GyverLibs/BSON).
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
[demo](https://gyverlibs.github.io/bson.js/test/)
|
|
5
|
+
|
|
6
|
+
> **Browser**: https://gyverlibs.github.io/bson.js/bson.min.js
|
|
7
|
+
|
|
8
|
+
> **Node**: npm i @alexgyver/bson
|
|
5
9
|
|
|
6
10
|
```js
|
|
7
11
|
const codes = [
|
|
@@ -10,10 +14,26 @@ const codes = [
|
|
|
10
14
|
'constants',
|
|
11
15
|
];
|
|
12
16
|
|
|
13
|
-
let
|
|
14
|
-
|
|
17
|
+
let test = {
|
|
18
|
+
int: 123,
|
|
19
|
+
float: 3.14,
|
|
20
|
+
arr: [
|
|
21
|
+
"str",
|
|
22
|
+
true,
|
|
23
|
+
1234,
|
|
24
|
+
new Uint8Array([1, 2, 3]),
|
|
25
|
+
],
|
|
26
|
+
obj: {
|
|
27
|
+
str2: "str2",
|
|
28
|
+
true: true,
|
|
29
|
+
},
|
|
30
|
+
str3: "str3",
|
|
31
|
+
nul: null,
|
|
32
|
+
[getCode('constants', codes)]: getCode('string', codes),
|
|
33
|
+
bins: new Uint8Array([1, 2, 3]),
|
|
34
|
+
};
|
|
15
35
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
36
|
+
let enc = encodeBson(test);
|
|
37
|
+
let json = decodeBson(enc, codes);
|
|
38
|
+
console.log(json);
|
|
19
39
|
```
|
package/bson.js
CHANGED
|
@@ -1,239 +1,232 @@
|
|
|
1
|
-
const BS_STRING = (0 << 5);
|
|
2
|
-
const BS_BOOLEAN = (1 << 5);
|
|
3
|
-
const BS_INTEGER = (2 << 5);
|
|
4
|
-
const BS_FLOAT = (3 << 5);
|
|
5
|
-
const BS_CODE = (4 << 5);
|
|
6
|
-
const BS_BINARY = (5 << 5);
|
|
7
|
-
const BS_CONTAINER = (6 << 5);
|
|
8
|
-
const BS_NULL = (7 << 5);
|
|
9
|
-
|
|
10
|
-
const BS_CONT_OBJ = (1 << 4);
|
|
11
|
-
const BS_CONT_OPEN = (1 << 3);
|
|
12
|
-
const BS_CONT_ARR = (1 << 2);
|
|
13
|
-
const BS_CONT_CLOSE = (1 << 1);
|
|
14
|
-
|
|
15
|
-
const BS_MAX_LEN = 0b0001111111111111;
|
|
16
|
-
|
|
17
|
-
const BS_TYPE_MASK = 0b11100000;
|
|
18
|
-
const BS_TYPE = x => x & BS_TYPE_MASK;
|
|
19
|
-
const BS_DATA_MASK = 0b00011111;
|
|
20
|
-
const BS_DATA = x => x & BS_DATA_MASK;
|
|
21
|
-
|
|
22
|
-
const BS_BOOLV_MASK = 0b1;
|
|
23
|
-
const BS_BOOLV = x => x & BS_BOOLV_MASK;
|
|
24
|
-
|
|
25
|
-
const BS_NEG_MASK = 0b10000;
|
|
26
|
-
const BS_NEGATIVE = x => x & BS_NEG_MASK;
|
|
27
|
-
const BS_SIZE_MASK = 0b01111;
|
|
28
|
-
const BS_SIZE = x => x & BS_SIZE_MASK;
|
|
29
|
-
|
|
30
|
-
const BS_FLOAT_SIZE = 4;
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
arr.push(
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
arr.push(
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
arr.push(BS_NULL);
|
|
233
|
-
break;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
export function getCode(name, codes) {
|
|
238
|
-
return BS_CODE_PREFIX + codes.indexOf(name);
|
|
1
|
+
const BS_STRING = (0 << 5);
|
|
2
|
+
const BS_BOOLEAN = (1 << 5);
|
|
3
|
+
const BS_INTEGER = (2 << 5);
|
|
4
|
+
const BS_FLOAT = (3 << 5);
|
|
5
|
+
const BS_CODE = (4 << 5);
|
|
6
|
+
const BS_BINARY = (5 << 5);
|
|
7
|
+
const BS_CONTAINER = (6 << 5);
|
|
8
|
+
const BS_NULL = (7 << 5);
|
|
9
|
+
|
|
10
|
+
const BS_CONT_OBJ = (1 << 4);
|
|
11
|
+
const BS_CONT_OPEN = (1 << 3);
|
|
12
|
+
const BS_CONT_ARR = (1 << 2);
|
|
13
|
+
const BS_CONT_CLOSE = (1 << 1);
|
|
14
|
+
|
|
15
|
+
const BS_MAX_LEN = 0b0001111111111111;
|
|
16
|
+
|
|
17
|
+
const BS_TYPE_MASK = 0b11100000;
|
|
18
|
+
const BS_TYPE = x => x & BS_TYPE_MASK;
|
|
19
|
+
const BS_DATA_MASK = 0b00011111;
|
|
20
|
+
const BS_DATA = x => x & BS_DATA_MASK;
|
|
21
|
+
|
|
22
|
+
const BS_BOOLV_MASK = 0b1;
|
|
23
|
+
const BS_BOOLV = x => x & BS_BOOLV_MASK;
|
|
24
|
+
|
|
25
|
+
const BS_NEG_MASK = 0b10000;
|
|
26
|
+
const BS_NEGATIVE = x => x & BS_NEG_MASK;
|
|
27
|
+
const BS_SIZE_MASK = 0b01111;
|
|
28
|
+
const BS_SIZE = x => x & BS_SIZE_MASK;
|
|
29
|
+
|
|
30
|
+
const BS_FLOAT_SIZE = 4;
|
|
31
|
+
|
|
32
|
+
const BS_D16_MSB = x => (x >> 8) & BS_DATA_MASK;
|
|
33
|
+
const BS_D16_LSB = x => x & 0xff;
|
|
34
|
+
const BS_D16_MERGE = (msb5, lsb) => ((msb5 << 8) | lsb) >>> 0;
|
|
35
|
+
|
|
36
|
+
const BS_CODE_PREFIX = "__BS#";
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {Uint8Array} b
|
|
40
|
+
* @param {Array} codes
|
|
41
|
+
* @returns {Object|Array|*}
|
|
42
|
+
*/
|
|
43
|
+
export function decodeBson(buf, codes = []) {
|
|
44
|
+
if (!(buf instanceof Uint8Array)) return undefined;
|
|
45
|
+
|
|
46
|
+
const reader = {
|
|
47
|
+
buf,
|
|
48
|
+
offset: 0,
|
|
49
|
+
decoder: new TextDecoder(),
|
|
50
|
+
read(len) {
|
|
51
|
+
if (this.offset + len > buf.length) {
|
|
52
|
+
throw new Error("Overflow");
|
|
53
|
+
}
|
|
54
|
+
const res = this.buf.subarray(this.offset, this.offset + len);
|
|
55
|
+
this.offset += len;
|
|
56
|
+
return res;
|
|
57
|
+
},
|
|
58
|
+
readB() {
|
|
59
|
+
return this.read(1)[0];
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return _decode(reader, codes);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function _decode(r, codes) {
|
|
67
|
+
const header = r.readB();
|
|
68
|
+
const data = BS_DATA(header);
|
|
69
|
+
|
|
70
|
+
switch (BS_TYPE(header)) {
|
|
71
|
+
case BS_CONTAINER:
|
|
72
|
+
if (data & BS_CONT_OPEN) {
|
|
73
|
+
let isArr = !!(data & BS_CONT_ARR);
|
|
74
|
+
let cont = isArr ? [] : {};
|
|
75
|
+
let expect = false;
|
|
76
|
+
let key;
|
|
77
|
+
|
|
78
|
+
while (true) {
|
|
79
|
+
let res = _decode(r, codes);
|
|
80
|
+
|
|
81
|
+
if (res && res.__close !== undefined) {
|
|
82
|
+
if (isArr === res.__close) {
|
|
83
|
+
if (!isArr && expect) throw new Error("Missed value: " + JSON.stringify(cont));
|
|
84
|
+
return cont;
|
|
85
|
+
} else {
|
|
86
|
+
throw new Error("Wrong close: " + JSON.stringify(cont));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (isArr) {
|
|
91
|
+
cont.push(res);
|
|
92
|
+
} else {
|
|
93
|
+
if (expect) cont[key] = res;
|
|
94
|
+
else key = res;
|
|
95
|
+
expect = !expect;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
} else if (data & BS_CONT_CLOSE) {
|
|
99
|
+
return { __close: !!(data & BS_CONT_ARR) };
|
|
100
|
+
} else {
|
|
101
|
+
throw new Error("Unknown cont: " + JSON.stringify(cont));
|
|
102
|
+
}
|
|
103
|
+
// break;
|
|
104
|
+
|
|
105
|
+
case BS_CODE:
|
|
106
|
+
return codes[BS_D16_MERGE(data, r.readB())];
|
|
107
|
+
|
|
108
|
+
case BS_STRING: {
|
|
109
|
+
let len = BS_D16_MERGE(data, r.readB());
|
|
110
|
+
return r.decoder.decode(r.read(len));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
case BS_INTEGER: {
|
|
114
|
+
let size = BS_SIZE(data);
|
|
115
|
+
if (!size) return 0;
|
|
116
|
+
|
|
117
|
+
let value = 0n;
|
|
118
|
+
const bytes = r.read(size);
|
|
119
|
+
while (size--) value = (value << 8n) | BigInt(bytes[size]);
|
|
120
|
+
|
|
121
|
+
if (BS_NEGATIVE(data)) value = -value;
|
|
122
|
+
return (value >= Number.MIN_SAFE_INTEGER && value <= Number.MAX_SAFE_INTEGER) ? Number(value) : value;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
case BS_BOOLEAN:
|
|
126
|
+
return !!BS_BOOLV(data);
|
|
127
|
+
|
|
128
|
+
case BS_NULL:
|
|
129
|
+
return null;
|
|
130
|
+
|
|
131
|
+
case BS_FLOAT: {
|
|
132
|
+
const b = r.read(4);
|
|
133
|
+
return new DataView(b.buffer, b.byteOffset, 4).getFloat32(0, true);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
case BS_BINARY: {
|
|
137
|
+
let len = BS_D16_MERGE(data, r.readB());
|
|
138
|
+
return r.read(len).slice();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @param {*} val
|
|
145
|
+
* @returns {Uint8Array}
|
|
146
|
+
*/
|
|
147
|
+
export function encodeBson(val, codes = []) {
|
|
148
|
+
let arr = [];
|
|
149
|
+
_encode(val, arr, codes);
|
|
150
|
+
return Uint8Array.from(arr);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function _encode(val, arr, codes) {
|
|
154
|
+
if (Array.isArray(val)) {
|
|
155
|
+
arr.push(BS_CONTAINER | BS_CONT_ARR | BS_CONT_OPEN);
|
|
156
|
+
val.forEach(v => _encode(v, arr, codes));
|
|
157
|
+
arr.push(BS_CONTAINER | BS_CONT_ARR | BS_CONT_CLOSE);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
switch (typeof val) {
|
|
162
|
+
case 'object':
|
|
163
|
+
if (val === null) {
|
|
164
|
+
arr.push(BS_NULL);
|
|
165
|
+
} else if (val instanceof Uint8Array) {
|
|
166
|
+
const len = Math.min(val.length, BS_MAX_LEN);
|
|
167
|
+
arr.push(BS_BINARY | BS_D16_MSB(len));
|
|
168
|
+
arr.push(BS_D16_LSB(len));
|
|
169
|
+
arr.push(...val.slice(0, len));
|
|
170
|
+
} else {
|
|
171
|
+
arr.push(BS_CONTAINER | BS_CONT_OBJ | BS_CONT_OPEN);
|
|
172
|
+
for (const [key, value] of Object.entries(val)) {
|
|
173
|
+
_encode(key, arr, codes);
|
|
174
|
+
_encode(value, arr, codes);
|
|
175
|
+
}
|
|
176
|
+
arr.push(BS_CONTAINER | BS_CONT_OBJ | BS_CONT_CLOSE);
|
|
177
|
+
}
|
|
178
|
+
break;
|
|
179
|
+
|
|
180
|
+
case 'bigint':
|
|
181
|
+
case 'number':
|
|
182
|
+
if (Number.isInteger(val)) {
|
|
183
|
+
val = BigInt(val);
|
|
184
|
+
const neg = val < 0;
|
|
185
|
+
if (neg) val = -val;
|
|
186
|
+
let bytes = [];
|
|
187
|
+
while (val) {
|
|
188
|
+
bytes.push(Number(val & 0xFFn));
|
|
189
|
+
val >>= 8n;
|
|
190
|
+
}
|
|
191
|
+
arr.push(BS_INTEGER | (neg ? BS_NEG_MASK : 0) | bytes.length);
|
|
192
|
+
arr.push(...bytes);
|
|
193
|
+
} else {
|
|
194
|
+
const buffer = new ArrayBuffer(BS_FLOAT_SIZE);
|
|
195
|
+
new DataView(buffer).setFloat32(0, val, true);
|
|
196
|
+
arr.push(BS_FLOAT);
|
|
197
|
+
arr.push(...new Uint8Array(buffer));
|
|
198
|
+
}
|
|
199
|
+
break;
|
|
200
|
+
|
|
201
|
+
case 'string':
|
|
202
|
+
if (val.startsWith(BS_CODE_PREFIX)) {
|
|
203
|
+
val = val.slice(BS_CODE_PREFIX.length);
|
|
204
|
+
let code = codes.indexOf(val);
|
|
205
|
+
if (code >= 0) {
|
|
206
|
+
arr.push(BS_CODE | BS_D16_MSB(code));
|
|
207
|
+
arr.push(BS_D16_LSB(code));
|
|
208
|
+
} else {
|
|
209
|
+
_encode(val, arr, codes);
|
|
210
|
+
}
|
|
211
|
+
} else {
|
|
212
|
+
const bytes = new TextEncoder().encode(val);
|
|
213
|
+
const len = Math.min(bytes.length, BS_MAX_LEN);
|
|
214
|
+
arr.push(BS_STRING | BS_D16_MSB(len));
|
|
215
|
+
arr.push(BS_D16_LSB(len));
|
|
216
|
+
arr.push(...bytes.slice(0, len));
|
|
217
|
+
}
|
|
218
|
+
break;
|
|
219
|
+
|
|
220
|
+
case 'boolean':
|
|
221
|
+
arr.push(BS_BOOLEAN | !!val);
|
|
222
|
+
break;
|
|
223
|
+
|
|
224
|
+
default:
|
|
225
|
+
arr.push(BS_NULL);
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export function getCode(name) {
|
|
231
|
+
return BS_CODE_PREFIX + name;
|
|
239
232
|
}
|
package/bson.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e={d:(t
|
|
1
|
+
var e={d:(r,t)=>{for(var n in t)e.o(t,n)&&!e.o(r,n)&&Object.defineProperty(r,n,{enumerable:!0,get:t[n]})},o:(e,r)=>Object.prototype.hasOwnProperty.call(e,r)},r={};e.d(r,{$R:()=>I,QC:()=>k,lo:()=>v});const t=0,n=32,s=64,o=96,i=128,a=160,u=192,c=224,f=16,h=8,l=4,d=2,p=8191,b=e=>224&e,w=e=>31&e,g=e=>1&e,y=16,E=e=>e&y,_=e=>15&e,B=4,N=e=>e>>8&31,m=e=>255&e,A=(e,r)=>(e<<8|r)>>>0,O="__BS#";function v(e,r=[]){if(e instanceof Uint8Array)return F({buf:e,offset:0,decoder:new TextDecoder,read(r){if(this.offset+r>e.length)throw new Error("Overflow");const t=this.buf.subarray(this.offset,this.offset+r);return this.offset+=r,t},readB(){return this.read(1)[0]}},r)}function F(e,r){const f=e.readB(),p=w(f);switch(b(f)){case u:if(!(p&h)){if(p&d)return{__close:!!(p&l)};throw new Error("Unknown cont: "+JSON.stringify(cont))}{let t,n=!!(p&l),s=n?[]:{},o=!1;for(;;){let i=F(e,r);if(i&&void 0!==i.__close){if(n===i.__close){if(!n&&o)throw new Error("Missed value: "+JSON.stringify(s));return s}throw new Error("Wrong close: "+JSON.stringify(s))}n?s.push(i):(o?s[t]=i:t=i,o=!o)}}case i:return r[A(p,e.readB())];case t:{let r=A(p,e.readB());return e.decoder.decode(e.read(r))}case s:{let r=_(p);if(!r)return 0;let t=0n;const n=e.read(r);for(;r--;)t=t<<8n|BigInt(n[r]);return E(p)&&(t=-t),t>=Number.MIN_SAFE_INTEGER&&t<=Number.MAX_SAFE_INTEGER?Number(t):t}case n:return!!g(p);case c:return null;case o:{const r=e.read(4);return new DataView(r.buffer,r.byteOffset,4).getFloat32(0,!0)}case a:{let r=A(p,e.readB());return e.read(r).slice()}}}function I(e,r=[]){let t=[];return S(e,t,r),Uint8Array.from(t)}function S(e,r,b){if(Array.isArray(e))return r.push(u|l|h),e.forEach(e=>S(e,r,b)),void r.push(u|l|d);switch(typeof e){case"object":if(null===e)r.push(c);else if(e instanceof Uint8Array){const t=Math.min(e.length,p);r.push(a|N(t)),r.push(m(t)),r.push(...e.slice(0,t))}else{r.push(u|f|h);for(const[t,n]of Object.entries(e))S(t,r,b),S(n,r,b);r.push(u|f|d)}break;case"bigint":case"number":if(Number.isInteger(e)){const t=(e=BigInt(e))<0;t&&(e=-e);let n=[];for(;e;)n.push(Number(0xFFn&e)),e>>=8n;r.push(s|(t?y:0)|n.length),r.push(...n)}else{const t=new ArrayBuffer(B);new DataView(t).setFloat32(0,e,!0),r.push(o),r.push(...new Uint8Array(t))}break;case"string":if(e.startsWith(O)){e=e.slice(O.length);let t=b.indexOf(e);t>=0?(r.push(i|N(t)),r.push(m(t))):S(e,r,b)}else{const n=(new TextEncoder).encode(e),s=Math.min(n.length,p);r.push(t|N(s)),r.push(m(s)),r.push(...n.slice(0,s))}break;case"boolean":r.push(n|!!e);break;default:r.push(c)}}function k(e){return O+e}const x=r.lo,M=r.$R,U=r.QC;export{x as decodeBson,M as encodeBson,U as getCode};
|
package/package.json
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@alexgyver/bson",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "Decode BSON from BSON Arduino library",
|
|
5
|
-
"main": "./bson.js",
|
|
6
|
-
"module": "./bson.js",
|
|
7
|
-
"types": "./bson.js",
|
|
8
|
-
"scripts": {
|
|
9
|
-
"build": "webpack --config ./webpack.config.js"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"webpack
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@alexgyver/bson",
|
|
3
|
+
"version": "2.2.0",
|
|
4
|
+
"description": "Decode BSON from BSON Arduino library",
|
|
5
|
+
"main": "./bson.js",
|
|
6
|
+
"module": "./bson.js",
|
|
7
|
+
"types": "./bson.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "webpack --config ./webpack.config.js",
|
|
10
|
+
"dev": "webpack serve --config ./webpack.dev.config.js & webpack --config ./webpack.dev.config.js"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/GyverLibs/bson.js.git"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"webpack": "^5.105.4",
|
|
18
|
+
"webpack-cli": "^6.0.1",
|
|
19
|
+
"webpack-dev-server": "^5.2.3",
|
|
20
|
+
"style-loader": "^4.0.0",
|
|
21
|
+
"css-loader": "^7.1.4"
|
|
22
|
+
},
|
|
23
|
+
"author": "AlexGyver <alex@alexgyver.ru>",
|
|
24
|
+
"license": "MIT"
|
|
21
25
|
}
|
|
@@ -16,9 +16,6 @@
|
|
|
16
16
|
'constants',
|
|
17
17
|
];
|
|
18
18
|
|
|
19
|
-
// =========== getCode ===========
|
|
20
|
-
console.log(getCode('constants', codes));
|
|
21
|
-
|
|
22
19
|
// =========== encode ===========
|
|
23
20
|
let test = {
|
|
24
21
|
int: 123,
|
|
@@ -35,11 +32,12 @@
|
|
|
35
32
|
},
|
|
36
33
|
str3: "str3",
|
|
37
34
|
nul: null,
|
|
38
|
-
[getCode('
|
|
35
|
+
[getCode('constants1')]: getCode('string1'),
|
|
36
|
+
[getCode('constants')]: getCode('string'),
|
|
39
37
|
bins: new Uint8Array([1, 2, 3]),
|
|
40
38
|
};
|
|
41
39
|
|
|
42
|
-
let enc = encodeBson(test);
|
|
40
|
+
let enc = encodeBson(test, codes);
|
|
43
41
|
let json = decodeBson(enc, codes);
|
|
44
42
|
console.log(json);
|
|
45
43
|
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
entry: './test/script.js',
|
|
5
|
+
output: {
|
|
6
|
+
filename: 'script.js',
|
|
7
|
+
path: path.resolve(__dirname, 'test'),
|
|
8
|
+
clean: false,
|
|
9
|
+
},
|
|
10
|
+
module: {
|
|
11
|
+
rules: [
|
|
12
|
+
{
|
|
13
|
+
test: /\.css$/,
|
|
14
|
+
use: ['style-loader', 'css-loader'],
|
|
15
|
+
}
|
|
16
|
+
]
|
|
17
|
+
},
|
|
18
|
+
devServer: {
|
|
19
|
+
static: path.resolve(__dirname, 'test'),
|
|
20
|
+
hot: true,
|
|
21
|
+
open: true,
|
|
22
|
+
},
|
|
23
|
+
mode: 'development',
|
|
24
|
+
};
|