@alexgyver/bson 3.0.2 → 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.
- package/bson.js +28 -11
- package/bson.min.js +1 -1
- package/package.json +1 -1
package/bson.js
CHANGED
|
@@ -36,8 +36,12 @@ 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
|
-
const BS_FLOAT_ZERO = 1;
|
|
41
45
|
const BS_FLOAT_SIZE = 4;
|
|
42
46
|
|
|
43
47
|
// subtype bin
|
|
@@ -54,6 +58,13 @@ export class BSDecoder {
|
|
|
54
58
|
this.codes = codes;
|
|
55
59
|
}
|
|
56
60
|
|
|
61
|
+
wrapObject() {
|
|
62
|
+
this._wrap(true);
|
|
63
|
+
}
|
|
64
|
+
wrapArray() {
|
|
65
|
+
this._wrap(false);
|
|
66
|
+
}
|
|
67
|
+
|
|
57
68
|
/**
|
|
58
69
|
* @returns {Object}
|
|
59
70
|
*/
|
|
@@ -64,6 +75,7 @@ export class BSDecoder {
|
|
|
64
75
|
if (this.offset != this.buf.length) throw this._err("Broken packet");
|
|
65
76
|
return res;
|
|
66
77
|
}
|
|
78
|
+
|
|
67
79
|
read(len) {
|
|
68
80
|
if (this.offset + len > this.buf.length) {
|
|
69
81
|
throw this._err("Overflow");
|
|
@@ -81,6 +93,13 @@ export class BSDecoder {
|
|
|
81
93
|
while (len--) val = (val << 8n) | BigInt(bytes[len]);
|
|
82
94
|
return val <= Number.MAX_SAFE_INTEGER ? Number(val) : val;
|
|
83
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
|
+
}
|
|
84
103
|
_ext(hdr) {
|
|
85
104
|
return BS_GET_LSB(hdr) | ((hdr & BS_EXT_FLAG) ? (this.readB() << BS_EXT_BITS) : 0);
|
|
86
105
|
}
|
|
@@ -144,12 +163,10 @@ export class BSDecoder {
|
|
|
144
163
|
case BS_BOOL:
|
|
145
164
|
return !!(header & 1);
|
|
146
165
|
|
|
147
|
-
case BS_FLOAT:
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
return new DataView(b.buffer, b.byteOffset, BS_FLOAT_SIZE).getFloat32(0, true);
|
|
152
|
-
}
|
|
166
|
+
case BS_FLOAT: {
|
|
167
|
+
const b = this.read(BS_FLOAT_SIZE);
|
|
168
|
+
return new DataView(b.buffer, b.byteOffset, BS_FLOAT_SIZE).getFloat32(0, true);
|
|
169
|
+
}
|
|
153
170
|
|
|
154
171
|
case BS_BIN:
|
|
155
172
|
return this.read(this.readInt(BS_GET_BIN_LEN(header))).slice();
|
|
@@ -188,9 +205,9 @@ export class BSEncoder {
|
|
|
188
205
|
let a = this.arr;
|
|
189
206
|
|
|
190
207
|
if (Array.isArray(val)) {
|
|
191
|
-
a.push(
|
|
208
|
+
a.push(BS_ARR_OPEN);
|
|
192
209
|
val.forEach(v => this.encode(v));
|
|
193
|
-
a.push(
|
|
210
|
+
a.push(BS_ARR_CLOSE);
|
|
194
211
|
} else {
|
|
195
212
|
switch (typeof val) {
|
|
196
213
|
case 'object':
|
|
@@ -202,12 +219,12 @@ export class BSEncoder {
|
|
|
202
219
|
a.push(...lenb);
|
|
203
220
|
a.push(...val);
|
|
204
221
|
} else {
|
|
205
|
-
a.push(
|
|
222
|
+
a.push(BS_OBJ_OPEN);
|
|
206
223
|
for (let key in val) {
|
|
207
224
|
this.encode(key);
|
|
208
225
|
this.encode(val[key]);
|
|
209
226
|
}
|
|
210
|
-
a.push(
|
|
227
|
+
a.push(BS_OBJ_CLOSE);
|
|
211
228
|
}
|
|
212
229
|
break;
|
|
213
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:()=>
|
|
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};
|