@alexgyver/bson 2.1.1 → 2.2.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 CHANGED
@@ -1,7 +1,11 @@
1
1
  # bson.js
2
2
  Распаковщик и запаковщик бинарного JSON для библиотеки [BSON](https://github.com/GyverLibs/BSON).
3
3
 
4
- > npm i @alexgyver/bson
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 = [
package/bson.js CHANGED
@@ -1,244 +1,239 @@
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
- const BS_DEC_MASK = 0b1111;
32
- const BS_DECIMAL = x => x & BS_DEC_MASK;
33
-
34
- const BS_D16_MSB = x => (x >> 8) & BS_DATA_MASK;
35
- const BS_D16_LSB = x => x & 0xff;
36
- const BS_D16_MERGE = (msb5, lsb) => ((msb5 << 8) | lsb) >>> 0;
37
-
38
- const BS_BIN_PREFIX = "__BSON_BIN_";
39
- const BS_CODE_PREFIX = "__BSON_CODE_";
40
-
41
- /**
42
- * @param {Uint8Array} b
43
- * @param {Array} codes
44
- * @returns {Object}
45
- */
46
- export function decodeBson(b, codes = []) {
47
- if (!b || !(b instanceof Uint8Array)) return null;
48
- if (!b.length) return {};
49
-
50
- let bins = [];
51
- let stack = [];
52
- let keyf = true;
53
- let s = '';
54
-
55
- try {
56
- for (let i = 0; i < b.length; i++) {
57
- const type = BS_TYPE(b[i]);
58
- const data = BS_DATA(b[i]);
59
-
60
- switch (type) {
61
-
62
- case BS_CONTAINER:
63
- if (data & BS_CONT_OPEN) {
64
- let t = (data & BS_CONT_OBJ) ? '{' : '[';
65
- s += t;
66
- stack.push(t);
67
- } else {
68
- if (s[s.length - 1] == ',') s = s.slice(0, -1);
69
- let t = (data & BS_CONT_OBJ) ? '}' : ']';
70
- s += t + ',';
71
- stack.pop();
72
- }
73
- keyf = true;
74
- continue;
75
-
76
- case BS_CODE:
77
- s += '"' + codes[BS_D16_MERGE(data, b[++i])] + '"';
78
- break;
79
-
80
- case BS_STRING: {
81
- let len = BS_D16_MERGE(data, b[++i]);
82
- s += JSON.stringify(new TextDecoder().decode(b.slice(i + 1, i + 1 + len)));
83
- i += len;
84
- } break;
85
-
86
- case BS_INTEGER: {
87
- if (BS_NEGATIVE(data)) s += '-';
88
- let len = BS_SIZE(data);
89
- let u8 = new Uint8Array(8);
90
- u8.set(b.slice(i + 1, i + 1 + len));
91
- s += new BigUint64Array(u8.buffer)[0];
92
- i += len;
93
- } break;
94
-
95
- case BS_BOOLEAN:
96
- s += BS_BOOLV(data) ? 'true' : 'false';
97
- break;
98
-
99
- case BS_NULL:
100
- s += 'null';
101
- break;
102
-
103
- case BS_FLOAT: {
104
- let f = new DataView(b.buffer, b.byteOffset + i + 1, BS_FLOAT_SIZE).getFloat32(0, true);
105
- s += (isNaN(f) || !isFinite(f)) ? 'null' : f.toFixed(BS_DECIMAL(data));
106
- i += BS_FLOAT_SIZE;
107
-
108
- } break;
109
-
110
- case BS_BINARY: {
111
- let len = BS_D16_MERGE(data, b[++i]);
112
- i++;
113
- s += '"' + BS_BIN_PREFIX + bins.length + '"';
114
- bins.push(b.slice(i, i + len));
115
- i += len - 1;
116
- } break;
117
-
118
- }
119
-
120
- if (stack[stack.length - 1] === '{') {
121
- s += keyf ? ':' : ',';
122
- keyf = !keyf;
123
- } else {
124
- s += ',';
125
- }
126
- }
127
- } catch (e) {
128
- console.error(e, s);
129
- throw new Error("BSON decode error");
130
- }
131
-
132
- if (s[s.length - 1] == ',') s = s.slice(0, -1);
133
-
134
- try {
135
- let obj = JSON.parse(s);
136
- if (bins.length) _makeBins(obj, bins);
137
- return obj;
138
- } catch (e) {
139
- console.error(e, s);
140
- throw new Error("JSON parse error");
141
- }
142
- }
143
-
144
- function _makeBins(obj, bins) {
145
- if (typeof obj !== 'object') return;
146
- for (let k in obj) {
147
- if (typeof obj[k] === "object" && obj[k] !== null) {
148
- _makeBins(obj[k], bins);
149
- } else if (typeof obj[k] === "string" && obj[k].startsWith(BS_BIN_PREFIX)) {
150
- obj[k] = bins[obj[k].slice(BS_BIN_PREFIX.length)];
151
- }
152
- }
153
- }
154
-
155
- /**
156
- * @param {*} val
157
- * @returns {Uint8Array}
158
- */
159
- export function encodeBson(val, codes = []) {
160
- let arr = [];
161
- _encode(val, arr, codes);
162
- return Uint8Array.from(arr);
163
- }
164
-
165
- function _encode(val, arr, codes) {
166
- if (Array.isArray(val)) {
167
- arr.push(BS_CONTAINER | BS_CONT_ARR | BS_CONT_OPEN);
168
- val.forEach(v => _encode(v, arr, codes));
169
- arr.push(BS_CONTAINER | BS_CONT_ARR | BS_CONT_CLOSE);
170
- return;
171
- }
172
-
173
- switch (typeof val) {
174
- case 'object':
175
- if (val === null) {
176
- arr.push(BS_NULL);
177
- } else if (val instanceof Uint8Array) {
178
- const len = Math.min(val.length, BS_MAX_LEN);
179
- arr.push(BS_BINARY | BS_D16_MSB(len));
180
- arr.push(BS_D16_LSB(len));
181
- arr.push(...val.slice(0, len));
182
- } else {
183
- arr.push(BS_CONTAINER | BS_CONT_OBJ | BS_CONT_OPEN);
184
- for (const [key, value] of Object.entries(val)) {
185
- _encode(key, arr, codes);
186
- _encode(value, arr, codes);
187
- }
188
- arr.push(BS_CONTAINER | BS_CONT_OBJ | BS_CONT_CLOSE);
189
- }
190
- break;
191
-
192
- case 'bigint':
193
- case 'number':
194
- if (Number.isInteger(val)) {
195
- val = BigInt(val);
196
- const neg = val < 0;
197
- if (neg) val = -val;
198
- let bytes = [];
199
- while (val) {
200
- bytes.push(Number(val & 0xFFn));
201
- val >>= 8n;
202
- }
203
- arr.push(BS_INTEGER | (neg ? BS_NEG_MASK : 0) | bytes.length);
204
- arr.push(...bytes);
205
- } else {
206
- const buffer = new ArrayBuffer(BS_FLOAT_SIZE);
207
- new DataView(buffer).setFloat32(0, val, true);
208
- arr.push(BS_FLOAT | 4);
209
- arr.push(...new Uint8Array(buffer));
210
- }
211
- break;
212
-
213
- case 'string':
214
- if (val.startsWith(BS_CODE_PREFIX)) {
215
- val = val.slice(BS_CODE_PREFIX.length);
216
- let code = codes.indexOf(val);
217
- if (code >= 0) {
218
- arr.push(BS_CODE | BS_D16_MSB(code));
219
- arr.push(BS_D16_LSB(code));
220
- } else {
221
- _encode(val, arr, codes);
222
- }
223
- } else {
224
- const bytes = new TextEncoder().encode(val);
225
- const len = Math.min(bytes.length, BS_MAX_LEN);
226
- arr.push(BS_STRING | BS_D16_MSB(len));
227
- arr.push(BS_D16_LSB(len));
228
- arr.push(...bytes.slice(0, len));
229
- }
230
- break;
231
-
232
- case 'boolean':
233
- arr.push(BS_BOOLEAN | (val ? 1 : 0));
234
- break;
235
-
236
- default:
237
- arr.push(BS_NULL);
238
- break;
239
- }
240
- }
241
-
242
- export function getCode(name) {
243
- return BS_CODE_PREFIX + 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) || !buf.length) return undefined;
45
+
46
+ const reader = {
47
+ buf,
48
+ offset: 0,
49
+ cont: 'root',
50
+ decoder: new TextDecoder(),
51
+ error(e) {
52
+ return new Error(e + ' in ' + JSON.stringify(this.cont));
53
+ },
54
+ read(len) {
55
+ if (this.offset + len > buf.length) {
56
+ throw this.error("Overflow");
57
+ }
58
+ const res = this.buf.subarray(this.offset, this.offset + len);
59
+ this.offset += len;
60
+ return res;
61
+ },
62
+ readB() {
63
+ return this.read(1)[0];
64
+ }
65
+ };
66
+
67
+ let res = _decode(reader, codes);
68
+ if (reader.offset != reader.buf.length) throw reader.error("Broken packet");
69
+ return res;
70
+ }
71
+
72
+ function _decode(r, codes) {
73
+ const header = r.readB();
74
+ const data = BS_DATA(header);
75
+
76
+ switch (BS_TYPE(header)) {
77
+ case BS_CONTAINER:
78
+ if (data & BS_CONT_OPEN) {
79
+ let isArr = !!(data & BS_CONT_ARR);
80
+ let cont = isArr ? [] : {};
81
+ let expect = false;
82
+ let key;
83
+
84
+ while (true) {
85
+ r.cont = cont;
86
+ let res = _decode(r, codes);
87
+
88
+ if (res && res.__close !== undefined) {
89
+ if (isArr === res.__close) {
90
+ if (!isArr && expect) throw r.error("Missed value");
91
+ return cont;
92
+ } else {
93
+ throw r.error("Wrong close");
94
+ }
95
+ }
96
+
97
+ if (isArr) {
98
+ cont.push(res);
99
+ } else {
100
+ if (expect) cont[key] = res;
101
+ else key = res;
102
+ expect = !expect;
103
+ }
104
+ }
105
+ } else if (data & BS_CONT_CLOSE) {
106
+ return { __close: !!(data & BS_CONT_ARR) };
107
+ } else {
108
+ throw r.error("Unknown cont");
109
+ }
110
+ // break;
111
+
112
+ case BS_CODE:
113
+ return codes[BS_D16_MERGE(data, r.readB())];
114
+
115
+ case BS_STRING: {
116
+ let len = BS_D16_MERGE(data, r.readB());
117
+ return r.decoder.decode(r.read(len));
118
+ }
119
+
120
+ case BS_INTEGER: {
121
+ let size = BS_SIZE(data);
122
+ if (!size) return 0;
123
+
124
+ let value = 0n;
125
+ const bytes = r.read(size);
126
+ while (size--) value = (value << 8n) | BigInt(bytes[size]);
127
+
128
+ if (BS_NEGATIVE(data)) value = -value;
129
+ return (value >= Number.MIN_SAFE_INTEGER && value <= Number.MAX_SAFE_INTEGER) ? Number(value) : value;
130
+ }
131
+
132
+ case BS_BOOLEAN:
133
+ return !!BS_BOOLV(data);
134
+
135
+ case BS_NULL:
136
+ return null;
137
+
138
+ case BS_FLOAT: {
139
+ const b = r.read(4);
140
+ return new DataView(b.buffer, b.byteOffset, 4).getFloat32(0, true);
141
+ }
142
+
143
+ case BS_BINARY: {
144
+ let len = BS_D16_MERGE(data, r.readB());
145
+ return r.read(len).slice();
146
+ }
147
+ }
148
+ }
149
+
150
+ /**
151
+ * @param {*} val
152
+ * @returns {Uint8Array}
153
+ */
154
+ export function encodeBson(val, codes = []) {
155
+ let arr = [];
156
+ _encode(val, arr, codes);
157
+ return Uint8Array.from(arr);
158
+ }
159
+
160
+ function _encode(val, arr, codes) {
161
+ if (Array.isArray(val)) {
162
+ arr.push(BS_CONTAINER | BS_CONT_ARR | BS_CONT_OPEN);
163
+ val.forEach(v => _encode(v, arr, codes));
164
+ arr.push(BS_CONTAINER | BS_CONT_ARR | BS_CONT_CLOSE);
165
+ return;
166
+ }
167
+
168
+ switch (typeof val) {
169
+ case 'object':
170
+ if (val === null) {
171
+ arr.push(BS_NULL);
172
+ } else if (val instanceof Uint8Array) {
173
+ const len = Math.min(val.length, BS_MAX_LEN);
174
+ arr.push(BS_BINARY | BS_D16_MSB(len));
175
+ arr.push(BS_D16_LSB(len));
176
+ arr.push(...val.slice(0, len));
177
+ } else {
178
+ arr.push(BS_CONTAINER | BS_CONT_OBJ | BS_CONT_OPEN);
179
+ for (const [key, value] of Object.entries(val)) {
180
+ _encode(key, arr, codes);
181
+ _encode(value, arr, codes);
182
+ }
183
+ arr.push(BS_CONTAINER | BS_CONT_OBJ | BS_CONT_CLOSE);
184
+ }
185
+ break;
186
+
187
+ case 'bigint':
188
+ case 'number':
189
+ if (Number.isInteger(val)) {
190
+ val = BigInt(val);
191
+ const neg = val < 0;
192
+ if (neg) val = -val;
193
+ let bytes = [];
194
+ while (val) {
195
+ bytes.push(Number(val & 0xFFn));
196
+ val >>= 8n;
197
+ }
198
+ arr.push(BS_INTEGER | (neg ? BS_NEG_MASK : 0) | bytes.length);
199
+ arr.push(...bytes);
200
+ } else {
201
+ const buffer = new ArrayBuffer(BS_FLOAT_SIZE);
202
+ new DataView(buffer).setFloat32(0, val, true);
203
+ arr.push(BS_FLOAT);
204
+ arr.push(...new Uint8Array(buffer));
205
+ }
206
+ break;
207
+
208
+ case 'string':
209
+ if (val.startsWith(BS_CODE_PREFIX)) {
210
+ val = val.slice(BS_CODE_PREFIX.length);
211
+ let code = codes.indexOf(val);
212
+ if (code >= 0) {
213
+ arr.push(BS_CODE | BS_D16_MSB(code));
214
+ arr.push(BS_D16_LSB(code));
215
+ } else {
216
+ _encode(val, arr, codes);
217
+ }
218
+ } else {
219
+ const bytes = new TextEncoder().encode(val);
220
+ const len = Math.min(bytes.length, BS_MAX_LEN);
221
+ arr.push(BS_STRING | BS_D16_MSB(len));
222
+ arr.push(BS_D16_LSB(len));
223
+ arr.push(...bytes.slice(0, len));
224
+ }
225
+ break;
226
+
227
+ case 'boolean':
228
+ arr.push(BS_BOOLEAN | !!val);
229
+ break;
230
+
231
+ default:
232
+ arr.push(BS_NULL);
233
+ break;
234
+ }
235
+ }
236
+
237
+ export function getCode(name) {
238
+ return BS_CODE_PREFIX + name;
244
239
  }
package/bson.min.js CHANGED
@@ -1 +1 @@
1
- var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{$R:()=>U,QC:()=>C,lo:()=>F});const r=0,n=32,s=64,o=96,a=128,c=160,i=192,l=224,u=16,h=8,f=4,p=2,b=8191,g=e=>224&e,y=e=>31&e,d=e=>1&e,w=16,O=e=>e&w,N=e=>15&e,k=4,A=e=>15&e,B=e=>e>>8&31,_=e=>255&e,m=(e,t)=>(e<<8|t)>>>0,j="__BSON_BIN_",x="__BSON_CODE_";function F(e,t=[]){if(!(e&&e instanceof Uint8Array))return null;if(!e.length)return{};let f=[],p=[],b=!0,w="";try{for(let B=0;B<e.length;B++){const _=g(e[B]),x=y(e[B]);switch(_){case i:if(x&h){let e=x&u?"{":"[";w+=e,p.push(e)}else","==w[w.length-1]&&(w=w.slice(0,-1)),w+=(x&u?"}":"]")+",",p.pop();b=!0;continue;case a:w+='"'+t[m(x,e[++B])]+'"';break;case r:{let t=m(x,e[++B]);w+=JSON.stringify((new TextDecoder).decode(e.slice(B+1,B+1+t))),B+=t}break;case s:{O(x)&&(w+="-");let t=N(x),r=new Uint8Array(8);r.set(e.slice(B+1,B+1+t)),w+=new BigUint64Array(r.buffer)[0],B+=t}break;case n:w+=d(x)?"true":"false";break;case l:w+="null";break;case o:{let t=new DataView(e.buffer,e.byteOffset+B+1,k).getFloat32(0,!0);w+=isNaN(t)||!isFinite(t)?"null":t.toFixed(A(x)),B+=k}break;case c:{let t=m(x,e[++B]);B++,w+='"'+j+f.length+'"',f.push(e.slice(B,B+t)),B+=t-1}}"{"===p[p.length-1]?(w+=b?":":",",b=!b):w+=","}}catch(e){throw console.error(e,w),new Error("BSON decode error")}","==w[w.length-1]&&(w=w.slice(0,-1));try{let e=JSON.parse(w);return f.length&&S(e,f),e}catch(e){throw console.error(e,w),new Error("JSON parse error")}}function S(e,t){if("object"==typeof e)for(let r in e)"object"==typeof e[r]&&null!==e[r]?S(e[r],t):"string"==typeof e[r]&&e[r].startsWith(j)&&(e[r]=t[e[r].slice(j.length)])}function U(e,t=[]){let r=[];return E(e,r,t),Uint8Array.from(r)}function E(e,t,g){if(Array.isArray(e))return t.push(i|f|h),e.forEach(e=>E(e,t,g)),void t.push(i|f|p);switch(typeof e){case"object":if(null===e)t.push(l);else if(e instanceof Uint8Array){const r=Math.min(e.length,b);t.push(c|B(r)),t.push(_(r)),t.push(...e.slice(0,r))}else{t.push(i|u|h);for(const[r,n]of Object.entries(e))E(r,t,g),E(n,t,g);t.push(i|u|p)}break;case"bigint":case"number":if(Number.isInteger(e)){const r=(e=BigInt(e))<0;r&&(e=-e);let n=[];for(;e;)n.push(Number(0xFFn&e)),e>>=8n;t.push(s|(r?w:0)|n.length),t.push(...n)}else{const r=new ArrayBuffer(k);new DataView(r).setFloat32(0,e,!0),t.push(4|o),t.push(...new Uint8Array(r))}break;case"string":if(e.startsWith(x)){e=e.slice(x.length);let r=g.indexOf(e);r>=0?(t.push(a|B(r)),t.push(_(r))):E(e,t,g)}else{const n=(new TextEncoder).encode(e),s=Math.min(n.length,b);t.push(r|B(s)),t.push(_(s)),t.push(...n.slice(0,s))}break;case"boolean":t.push(n|(e?1:0));break;default:t.push(l)}}function C(e){return x+e}const D=t.lo,v=t.$R,I=t.QC;export{D as decodeBson,v as encodeBson,I as getCode};
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.1.1",
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
- "repository": {
12
- "type": "git",
13
- "url": "git+https://github.com/GyverLibs/bson.js.git"
14
- },
15
- "devDependencies": {
16
- "webpack": "^5.98.0",
17
- "webpack-cli": "^6.0.1"
18
- },
19
- "author": "AlexGyver <alex@alexgyver.ru>",
20
- "license": "MIT"
1
+ {
2
+ "name": "@alexgyver/bson",
3
+ "version": "2.2.1",
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
  }
@@ -5,8 +5,8 @@
5
5
  <p id="out" style="white-space: break-spaces;"></p>
6
6
 
7
7
  <script type="module">
8
- // import { decodeBson, encodeBson, getCode } from 'https://gyverlibs.github.io/bson.js/dist/bson.js';
9
- import { decodeBson, encodeBson, getCode } from '/bson.js';
8
+ import { decodeBson, encodeBson, getCode } from 'https://gyverlibs.github.io/bson.js/dist/bson.js';
9
+ // import { decodeBson, encodeBson, getCode } from '/bson.js';
10
10
 
11
11
  const url = 'http://192.168.1.54';
12
12
 
@@ -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
+ };