@alexgyver/bson 3.0.0 → 3.0.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/bson.js +7 -5
- package/bson.min.js +1 -1
- package/package.json +1 -1
- package/test/index.html +4 -3
package/bson.js
CHANGED
|
@@ -73,10 +73,10 @@ export class BSDecoder {
|
|
|
73
73
|
return this.read(1)[0];
|
|
74
74
|
}
|
|
75
75
|
readInt(len) {
|
|
76
|
-
let val =
|
|
76
|
+
let val = 0n;
|
|
77
77
|
let bytes = this.read(len);
|
|
78
|
-
while (len--) val = (val <<
|
|
79
|
-
return val;
|
|
78
|
+
while (len--) val = (val << 8n) | BigInt(bytes[len]);
|
|
79
|
+
return Number(val);
|
|
80
80
|
}
|
|
81
81
|
_ext(hdr) {
|
|
82
82
|
return BS_GET_LSB(hdr) | ((hdr & BS_EXT_FLAG) ? (this.readB() << BS_EXT_BITS) : 0);
|
|
@@ -195,6 +195,7 @@ export class BSEncoder {
|
|
|
195
195
|
break;
|
|
196
196
|
|
|
197
197
|
case 'number':
|
|
198
|
+
case 'bigint':
|
|
198
199
|
if (Number.isInteger(val)) {
|
|
199
200
|
if (val >= 0 && val < BS_INT_SMALL) {
|
|
200
201
|
a.push(BS_INT | BS_INT_SMALL | val);
|
|
@@ -242,9 +243,10 @@ export class BSEncoder {
|
|
|
242
243
|
|
|
243
244
|
_int(v) {
|
|
244
245
|
let t = [];
|
|
246
|
+
v = BigInt(v);
|
|
245
247
|
while (v) {
|
|
246
|
-
t.push(v &
|
|
247
|
-
v >>=
|
|
248
|
+
t.push(Number(v & 0xFFn));
|
|
249
|
+
v >>= 8n;
|
|
248
250
|
}
|
|
249
251
|
return t;
|
|
250
252
|
}
|
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=
|
|
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=0n,r=this.read(e);for(;e--;)t=t<<8n|BigInt(r[e]);return Number(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":case"bigint":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=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).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};
|
package/package.json
CHANGED
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
|
-
|
|
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
|
|
|
@@ -26,7 +26,8 @@
|
|
|
26
26
|
"str",
|
|
27
27
|
"0123456789012345678901234567890123456789",
|
|
28
28
|
true,
|
|
29
|
-
|
|
29
|
+
123456789876543,
|
|
30
|
+
-123456789876543,
|
|
30
31
|
new Uint8Array([1, 2, 3]),
|
|
31
32
|
new Uint8Array([]),
|
|
32
33
|
],
|