@alexgyver/bson 3.0.3 → 3.0.4

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.
Files changed (3) hide show
  1. package/bson.js +24 -4
  2. package/bson.min.js +1 -1
  3. package/package.json +1 -1
package/bson.js CHANGED
@@ -36,6 +36,11 @@ const BS_CONT_OBJ = 1 << 0;
36
36
  const BS_CONT_CLOSE = 0 << 1;
37
37
  const BS_CONT_OPEN = 1 << 1;
38
38
 
39
+ const BS_ARR_OPEN = BS_SUBT | BS_CONT | BS_CONT_ARR | BS_CONT_OPEN;
40
+ const BS_ARR_CLOSE = BS_SUBT | BS_CONT | BS_CONT_ARR | BS_CONT_CLOSE;
41
+ const BS_OBJ_OPEN = BS_SUBT | BS_CONT | BS_CONT_OBJ | BS_CONT_OPEN;
42
+ const BS_OBJ_CLOSE = BS_SUBT | BS_CONT | BS_CONT_OBJ | BS_CONT_CLOSE;
43
+
39
44
  // subtype float
40
45
  const BS_FLOAT_SIZE = 4;
41
46
 
@@ -53,6 +58,13 @@ export class BSDecoder {
53
58
  this.codes = codes;
54
59
  }
55
60
 
61
+ wrapObject() {
62
+ this._wrap(true);
63
+ }
64
+ wrapArray() {
65
+ this._wrap(false);
66
+ }
67
+
56
68
  /**
57
69
  * @returns {Object}
58
70
  */
@@ -63,6 +75,7 @@ export class BSDecoder {
63
75
  if (this.offset != this.buf.length) throw this._err("Broken packet");
64
76
  return res;
65
77
  }
78
+
66
79
  read(len) {
67
80
  if (this.offset + len > this.buf.length) {
68
81
  throw this._err("Overflow");
@@ -80,6 +93,13 @@ export class BSDecoder {
80
93
  while (len--) val = (val << 8n) | BigInt(bytes[len]);
81
94
  return val <= Number.MAX_SAFE_INTEGER ? Number(val) : val;
82
95
  }
96
+ _wrap(obj) {
97
+ const t = new Uint8Array(this.buf.length + 2);
98
+ t[0] = obj ? BS_OBJ_OPEN : BS_ARR_OPEN;
99
+ t.set(this.buf, 1);
100
+ t[t.length - 1] = obj ? BS_OBJ_CLOSE : BS_ARR_CLOSE;
101
+ this.buf = t;
102
+ }
83
103
  _ext(hdr) {
84
104
  return BS_GET_LSB(hdr) | ((hdr & BS_EXT_FLAG) ? (this.readB() << BS_EXT_BITS) : 0);
85
105
  }
@@ -185,9 +205,9 @@ export class BSEncoder {
185
205
  let a = this.arr;
186
206
 
187
207
  if (Array.isArray(val)) {
188
- a.push(BS_SUBT | BS_CONT | BS_CONT_ARR | BS_CONT_OPEN);
208
+ a.push(BS_ARR_OPEN);
189
209
  val.forEach(v => this.encode(v));
190
- a.push(BS_SUBT | BS_CONT | BS_CONT_ARR | BS_CONT_CLOSE);
210
+ a.push(BS_ARR_CLOSE);
191
211
  } else {
192
212
  switch (typeof val) {
193
213
  case 'object':
@@ -199,12 +219,12 @@ export class BSEncoder {
199
219
  a.push(...lenb);
200
220
  a.push(...val);
201
221
  } else {
202
- a.push(BS_SUBT | BS_CONT | BS_CONT_OBJ | BS_CONT_OPEN);
222
+ a.push(BS_OBJ_OPEN);
203
223
  for (let key in val) {
204
224
  this.encode(key);
205
225
  this.encode(val[key]);
206
226
  }
207
- a.push(BS_SUBT | BS_CONT | BS_CONT_OBJ | BS_CONT_CLOSE);
227
+ a.push(BS_OBJ_CLOSE);
208
228
  }
209
229
  break;
210
230
 
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,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};
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:()=>o,GF:()=>i,JV:()=>c,lo:()=>h,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}wrapObject(){this._wrap(!0)}wrapArray(){this._wrap(!1)}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}_wrap(e){const t=new Uint8Array(this.buf.length+2);t[0]=e?19:18,t.set(this.buf,1),t[t.length-1]=e?17:16,this.buf=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:{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 h(e,t=[]){return new n(e,t).decode()}function o(e,t=[]){return new i(t).encodeSingle(e)}function c(e){return s+e}const a=t.JV,u=t.mp,l=t.GF,f=t.lo,d=t.$R;export{a as BSCode,u 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.3",
3
+ "version": "3.0.4",
4
4
  "description": "Decode BSON from BSON Arduino library",
5
5
  "main": "./bson.js",
6
6
  "module": "./bson.js",