@alexgyver/bson 3.0.0 → 3.0.2

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
@@ -29,7 +29,7 @@ let test = {
29
29
  },
30
30
  str3: "str3",
31
31
  nul: null,
32
- [getCode('constants', codes)]: getCode('string', codes),
32
+ [BSCode('constants', codes)]: BSCode('string', codes),
33
33
  bins: new Uint8Array([1, 2, 3]),
34
34
  };
35
35
 
package/bson.js CHANGED
@@ -37,8 +37,8 @@ const BS_CONT_CLOSE = 0 << 1;
37
37
  const BS_CONT_OPEN = 1 << 1;
38
38
 
39
39
  // subtype float
40
- const BS_GET_FLOAT_LEN = x => 1 << ((x) & 0b111);
41
- const BS_FLOAT_F32 = 2;
40
+ const BS_FLOAT_ZERO = 1;
41
+ const BS_FLOAT_SIZE = 4;
42
42
 
43
43
  // subtype bin
44
44
  const BS_GET_BIN_LEN = x => x & 0b111;
@@ -54,6 +54,9 @@ export class BSDecoder {
54
54
  this.codes = codes;
55
55
  }
56
56
 
57
+ /**
58
+ * @returns {Object}
59
+ */
57
60
  decode() {
58
61
  if (!(this.buf instanceof Uint8Array) || !this.buf.length) return undefined;
59
62
 
@@ -73,10 +76,10 @@ export class BSDecoder {
73
76
  return this.read(1)[0];
74
77
  }
75
78
  readInt(len) {
76
- let val = 0;
79
+ let val = 0n;
77
80
  let bytes = this.read(len);
78
- while (len--) val = (val << 8) | bytes[len];
79
- return val;
81
+ while (len--) val = (val << 8n) | BigInt(bytes[len]);
82
+ return val <= Number.MAX_SAFE_INTEGER ? Number(val) : val;
80
83
  }
81
84
  _ext(hdr) {
82
85
  return BS_GET_LSB(hdr) | ((hdr & BS_EXT_FLAG) ? (this.readB() << BS_EXT_BITS) : 0);
@@ -141,12 +144,12 @@ export class BSDecoder {
141
144
  case BS_BOOL:
142
145
  return !!(header & 1);
143
146
 
144
- case BS_FLOAT: {
145
- let len = BS_GET_FLOAT_LEN(header);
146
- const b = this.read(len);
147
- let view = new DataView(b.buffer, b.byteOffset, len);
148
- return (len == 4) ? view.getFloat32(0, true) : ((len == 8) ? view.getFloat64(0, true) : 0);
149
- }
147
+ case BS_FLOAT:
148
+ if (header & BS_FLOAT_ZERO) return 0.0;
149
+ else {
150
+ const b = this.read(BS_FLOAT_SIZE);
151
+ return new DataView(b.buffer, b.byteOffset, BS_FLOAT_SIZE).getFloat32(0, true);
152
+ }
150
153
 
151
154
  case BS_BIN:
152
155
  return this.read(this.readInt(BS_GET_BIN_LEN(header))).slice();
@@ -163,10 +166,24 @@ export class BSEncoder {
163
166
  this.codes = codes;
164
167
  }
165
168
 
169
+ /**
170
+ * @returns {Uint8Array}
171
+ */
166
172
  getArray() {
167
173
  return Uint8Array.from(this.arr);
168
174
  }
169
175
 
176
+ /**
177
+ * @returns {Uint8Array}
178
+ */
179
+ encodeSingle(val) {
180
+ return this.encode(val).getArray();
181
+ }
182
+
183
+ /**
184
+ * @param {*} val
185
+ * @returns {BSEncoder}
186
+ */
170
187
  encode(val) {
171
188
  let a = this.arr;
172
189
 
@@ -186,14 +203,17 @@ export class BSEncoder {
186
203
  a.push(...val);
187
204
  } else {
188
205
  a.push(BS_SUBT | BS_CONT | BS_CONT_OBJ | BS_CONT_OPEN);
189
- for (const [key, value] of Object.entries(val)) {
206
+ for (let key in val) {
190
207
  this.encode(key);
191
- this.encode(value);
208
+ this.encode(val[key]);
192
209
  }
193
210
  a.push(BS_SUBT | BS_CONT | BS_CONT_OBJ | BS_CONT_CLOSE);
194
211
  }
195
212
  break;
196
213
 
214
+ case 'bigint':
215
+ val = Number(val);
216
+
197
217
  case 'number':
198
218
  if (Number.isInteger(val)) {
199
219
  if (val >= 0 && val < BS_INT_SMALL) {
@@ -206,9 +226,10 @@ export class BSEncoder {
206
226
  a.push(...bytes);
207
227
  }
208
228
  } else {
209
- const buffer = new ArrayBuffer(4);
229
+ // 0.0 is integer
230
+ const buffer = new ArrayBuffer(BS_FLOAT_SIZE);
210
231
  new DataView(buffer).setFloat32(0, val, true);
211
- a.push(BS_SUBT | BS_FLOAT | BS_FLOAT_F32);
232
+ a.push(BS_SUBT | BS_FLOAT);
212
233
  a.push(...new Uint8Array(buffer));
213
234
  }
214
235
  break;
@@ -242,9 +263,10 @@ export class BSEncoder {
242
263
 
243
264
  _int(v) {
244
265
  let t = [];
266
+ v = BigInt(v);
245
267
  while (v) {
246
- t.push(v & 0xFF);
247
- v >>= 8;
268
+ t.push(Number(v & 0xFFn));
269
+ v >>= 8n;
248
270
  }
249
271
  return t;
250
272
  }
@@ -277,9 +299,9 @@ export function decodeBson(buf, codes = []) {
277
299
  * @returns {Uint8Array}
278
300
  */
279
301
  export function encodeBson(val, codes = []) {
280
- return new BSEncoder(codes).encode(val).getArray();
302
+ return new BSEncoder(codes).encodeSingle(val);
281
303
  }
282
304
 
283
- export function getCode(name) {
305
+ export function BSCode(name) {
284
306
  return BS_CODE_PREFIX + name;
285
307
  }
package/bson.min.js CHANGED
@@ -1 +1 @@
1
- var e={d:(t,r)=>{for(var s in r)e.o(r,s)&&!e.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:r[s]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{$R:()=>h,GF:()=>i,QC:()=>c,lo:()=>o,mp:()=>n});const r=e=>31&e,s="__BS#";class n{offset=0;cont="root";tdec=new TextDecoder;constructor(e,t){this.buf=e,this.codes=t}decode(){if(!(this.buf instanceof Uint8Array&&this.buf.length))return;let e=this._decode();if(this.offset!=this.buf.length)throw this._err("Broken packet");return e}read(e){if(this.offset+e>this.buf.length)throw this._err("Overflow");let t=this.buf.subarray(this.offset,this.offset+e);return this.offset+=e,t}readB(){return this.read(1)[0]}readInt(e){let t=0,r=this.read(e);for(;e--;)t=t<<8|r[e];return t}_ext(e){return r(e)|(32&e?this.readB()<<5:0)}_close(e,t){if(e&&void 0!==e.__close){if(t===e.__close)return!0;throw this._err("Wrong close")}return!1}_err(e){return new Error(e+" in "+JSON.stringify(this.cont))}_decode(){const e=this.readB();let t=192&e;switch(0==t&&(t=56&e),t){case 16:if(2&e){let t=!!(1&e),r=t?{}:[];for(;;){this.cont=r;let e=this._decode();if(this._close(e,t))return r;if(t){let s=this._decode();if(this._close(s,t))throw this._err("Missed value");r[String(e)]=s}else r.push(e)}}return{__close:!!(1&e)};case 64:if(32&e)return 31&e;{let t=this.readInt(15&e);return 16&e?-t:t}case 128:return this.tdec.decode(this.read(this._ext(e)));case 192:return this.codes[this._ext(e)];case 0:return null;case 8:return!!(1&e);case 24:{let t=(e=>1<<(7&e))(e);const r=this.read(t);let s=new DataView(r.buffer,r.byteOffset,t);return 4==t?s.getFloat32(0,!0):8==t?s.getFloat64(0,!0):0}case 32:return this.read(this.readInt((e=>7&e)(e))).slice()}}}class i{arr=[];tenc=new TextEncoder;constructor(e=[]){this.codes=e}getArray(){return Uint8Array.from(this.arr)}encode(e){let t=this.arr;if(Array.isArray(e))t.push(18),e.forEach(e=>this.encode(e)),t.push(16);else switch(typeof e){case"object":if(null===e)t.push(0);else if(e instanceof Uint8Array){let r=this._int(e.length);t.push(32|r.length),t.push(...r),t.push(...e)}else{t.push(19);for(const[t,r]of Object.entries(e))this.encode(t),this.encode(r);t.push(17)}break;case"number":if(Number.isInteger(e))if(e>=0&&e<32)t.push(96|e);else{let r=e<0;r&&(e=-e);let s=this._int(e);t.push(64|(r?16:0)|s.length),t.push(...s)}else{const r=new ArrayBuffer(4);new DataView(r).setFloat32(0,e,!0),t.push(26),t.push(...new Uint8Array(r))}break;case"string":if(e.startsWith(s)){e=e.slice(s.length);let t=this.codes.indexOf(e);t>=0?this._ext(192,t):this.encode(e)}else{const r=this.tenc.encode(e),s=Math.min(r.length,8191);this._ext(128,s),t.push(...r.slice(0,s))}break;case"boolean":t.push(8|!!e);break;default:t.push(0)}return this}_int(e){let t=[];for(;e;)t.push(255&e),e>>=8;return t}_ext(e,t){let s=this.arr;t<32?s.push(e|t):(s.push(32|e|r(t)),s.push(t>>5))}}function o(e,t=[]){return new n(e,t).decode()}function h(e,t=[]){return new i(t).encode(e).getArray()}function c(e){return s+e}const a=t.mp,u=t.GF,l=t.lo,f=t.$R,d=t.QC;export{a as BSDecoder,u as BSEncoder,l as decodeBson,f as encodeBson,d as getCode};
1
+ var e={d:(t,r)=>{for(var s in r)e.o(r,s)&&!e.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:r[s]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{$R:()=>h,GF:()=>i,JV:()=>c,lo:()=>o,mp:()=>n});const r=e=>31&e,s="__BS#";class n{offset=0;cont="root";tdec=new TextDecoder;constructor(e,t){this.buf=e,this.codes=t}decode(){if(!(this.buf instanceof Uint8Array&&this.buf.length))return;let e=this._decode();if(this.offset!=this.buf.length)throw this._err("Broken packet");return e}read(e){if(this.offset+e>this.buf.length)throw this._err("Overflow");let t=this.buf.subarray(this.offset,this.offset+e);return this.offset+=e,t}readB(){return this.read(1)[0]}readInt(e){let t=0n,r=this.read(e);for(;e--;)t=t<<8n|BigInt(r[e]);return t<=Number.MAX_SAFE_INTEGER?Number(t):t}_ext(e){return r(e)|(32&e?this.readB()<<5:0)}_close(e,t){if(e&&void 0!==e.__close){if(t===e.__close)return!0;throw this._err("Wrong close")}return!1}_err(e){return new Error(e+" in "+JSON.stringify(this.cont))}_decode(){const e=this.readB();let t=192&e;switch(0==t&&(t=56&e),t){case 16:if(2&e){let t=!!(1&e),r=t?{}:[];for(;;){this.cont=r;let e=this._decode();if(this._close(e,t))return r;if(t){let s=this._decode();if(this._close(s,t))throw this._err("Missed value");r[String(e)]=s}else r.push(e)}}return{__close:!!(1&e)};case 64:if(32&e)return 31&e;{let t=this.readInt(15&e);return 16&e?-t:t}case 128:return this.tdec.decode(this.read(this._ext(e)));case 192:return this.codes[this._ext(e)];case 0:return null;case 8:return!!(1&e);case 24:if(1&e)return 0;{const e=this.read(4);return new DataView(e.buffer,e.byteOffset,4).getFloat32(0,!0)}case 32:return this.read(this.readInt((e=>7&e)(e))).slice()}}}class i{arr=[];tenc=new TextEncoder;constructor(e=[]){this.codes=e}getArray(){return Uint8Array.from(this.arr)}encodeSingle(e){return this.encode(e).getArray()}encode(e){let t=this.arr;if(Array.isArray(e))t.push(18),e.forEach(e=>this.encode(e)),t.push(16);else switch(typeof e){case"object":if(null===e)t.push(0);else if(e instanceof Uint8Array){let r=this._int(e.length);t.push(32|r.length),t.push(...r),t.push(...e)}else{t.push(19);for(let t in e)this.encode(t),this.encode(e[t]);t.push(17)}break;case"bigint":e=Number(e);case"number":if(Number.isInteger(e))if(e>=0&&e<32)t.push(96|e);else{let r=e<0;r&&(e=-e);let s=this._int(e);t.push(64|(r?16:0)|s.length),t.push(...s)}else{const r=new ArrayBuffer(4);new DataView(r).setFloat32(0,e,!0),t.push(24),t.push(...new Uint8Array(r))}break;case"string":if(e.startsWith(s)){e=e.slice(s.length);let t=this.codes.indexOf(e);t>=0?this._ext(192,t):this.encode(e)}else{const r=this.tenc.encode(e),s=Math.min(r.length,8191);this._ext(128,s),t.push(...r.slice(0,s))}break;case"boolean":t.push(8|!!e);break;default:t.push(0)}return this}_int(e){let t=[];for(e=BigInt(e);e;)t.push(Number(0xFFn&e)),e>>=8n;return t}_ext(e,t){let s=this.arr;t<32?s.push(e|t):(s.push(32|e|r(t)),s.push(t>>5))}}function o(e,t=[]){return new n(e,t).decode()}function h(e,t=[]){return new i(t).encodeSingle(e)}function c(e){return s+e}const u=t.JV,a=t.mp,l=t.GF,f=t.lo,d=t.$R;export{u as BSCode,a as BSDecoder,l as BSEncoder,f as decodeBson,d as encodeBson};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexgyver/bson",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
4
4
  "description": "Decode BSON from BSON Arduino library",
5
5
  "main": "./bson.js",
6
6
  "module": "./bson.js",
package/test/index.html CHANGED
@@ -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, BSCode } from 'https://gyverlibs.github.io/bson.js/dist/bson.js';
9
+ import { decodeBson, encodeBson, BSCode } from '/bson.js';
10
10
 
11
11
  const url = 'http://192.168.1.54';
12
12
 
@@ -18,15 +18,18 @@
18
18
 
19
19
  // =========== encode ===========
20
20
  let test = {
21
- sint: 20,
21
+ sint: 20n,
22
+ bint: 2000n,
22
23
  int: 123,
23
24
  nint: -123,
24
25
  float: 3.14,
26
+ float0: 0.0,
25
27
  arr: [
26
28
  "str",
27
29
  "0123456789012345678901234567890123456789",
28
30
  true,
29
- 1234,
31
+ 123456789876543,
32
+ -123456789876543,
30
33
  new Uint8Array([1, 2, 3]),
31
34
  new Uint8Array([]),
32
35
  ],
@@ -36,8 +39,8 @@
36
39
  },
37
40
  str3: "str3",
38
41
  nul: null,
39
- [getCode('constants1')]: getCode('string1'),
40
- [getCode('constants')]: getCode('string'),
42
+ [BSCode('constants1')]: BSCode('string1'),
43
+ [BSCode('constants')]: BSCode('string'),
41
44
  bins: new Uint8Array([1, 2, 3]),
42
45
  };
43
46