@alexgyver/bson 3.0.1 → 3.0.3
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 +1 -1
- package/bson.js +31 -14
- package/bson.min.js +1 -1
- package/package.json +1 -1
- package/test/index.html +7 -5
package/README.md
CHANGED
package/bson.js
CHANGED
|
@@ -37,8 +37,7 @@ const BS_CONT_CLOSE = 0 << 1;
|
|
|
37
37
|
const BS_CONT_OPEN = 1 << 1;
|
|
38
38
|
|
|
39
39
|
// subtype float
|
|
40
|
-
const
|
|
41
|
-
const BS_FLOAT_F32 = 2;
|
|
40
|
+
const BS_FLOAT_SIZE = 4;
|
|
42
41
|
|
|
43
42
|
// subtype bin
|
|
44
43
|
const BS_GET_BIN_LEN = x => x & 0b111;
|
|
@@ -54,6 +53,9 @@ export class BSDecoder {
|
|
|
54
53
|
this.codes = codes;
|
|
55
54
|
}
|
|
56
55
|
|
|
56
|
+
/**
|
|
57
|
+
* @returns {Object}
|
|
58
|
+
*/
|
|
57
59
|
decode() {
|
|
58
60
|
if (!(this.buf instanceof Uint8Array) || !this.buf.length) return undefined;
|
|
59
61
|
|
|
@@ -76,7 +78,7 @@ export class BSDecoder {
|
|
|
76
78
|
let val = 0n;
|
|
77
79
|
let bytes = this.read(len);
|
|
78
80
|
while (len--) val = (val << 8n) | BigInt(bytes[len]);
|
|
79
|
-
return Number(val);
|
|
81
|
+
return val <= Number.MAX_SAFE_INTEGER ? Number(val) : val;
|
|
80
82
|
}
|
|
81
83
|
_ext(hdr) {
|
|
82
84
|
return BS_GET_LSB(hdr) | ((hdr & BS_EXT_FLAG) ? (this.readB() << BS_EXT_BITS) : 0);
|
|
@@ -142,10 +144,8 @@ export class BSDecoder {
|
|
|
142
144
|
return !!(header & 1);
|
|
143
145
|
|
|
144
146
|
case BS_FLOAT: {
|
|
145
|
-
|
|
146
|
-
|
|
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);
|
|
147
|
+
const b = this.read(BS_FLOAT_SIZE);
|
|
148
|
+
return new DataView(b.buffer, b.byteOffset, BS_FLOAT_SIZE).getFloat32(0, true);
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
case BS_BIN:
|
|
@@ -163,10 +163,24 @@ export class BSEncoder {
|
|
|
163
163
|
this.codes = codes;
|
|
164
164
|
}
|
|
165
165
|
|
|
166
|
+
/**
|
|
167
|
+
* @returns {Uint8Array}
|
|
168
|
+
*/
|
|
166
169
|
getArray() {
|
|
167
170
|
return Uint8Array.from(this.arr);
|
|
168
171
|
}
|
|
169
172
|
|
|
173
|
+
/**
|
|
174
|
+
* @returns {Uint8Array}
|
|
175
|
+
*/
|
|
176
|
+
encodeSingle(val) {
|
|
177
|
+
return this.encode(val).getArray();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* @param {*} val
|
|
182
|
+
* @returns {BSEncoder}
|
|
183
|
+
*/
|
|
170
184
|
encode(val) {
|
|
171
185
|
let a = this.arr;
|
|
172
186
|
|
|
@@ -186,16 +200,18 @@ export class BSEncoder {
|
|
|
186
200
|
a.push(...val);
|
|
187
201
|
} else {
|
|
188
202
|
a.push(BS_SUBT | BS_CONT | BS_CONT_OBJ | BS_CONT_OPEN);
|
|
189
|
-
for (
|
|
203
|
+
for (let key in val) {
|
|
190
204
|
this.encode(key);
|
|
191
|
-
this.encode(
|
|
205
|
+
this.encode(val[key]);
|
|
192
206
|
}
|
|
193
207
|
a.push(BS_SUBT | BS_CONT | BS_CONT_OBJ | BS_CONT_CLOSE);
|
|
194
208
|
}
|
|
195
209
|
break;
|
|
196
210
|
|
|
197
|
-
case 'number':
|
|
198
211
|
case 'bigint':
|
|
212
|
+
val = Number(val);
|
|
213
|
+
|
|
214
|
+
case 'number':
|
|
199
215
|
if (Number.isInteger(val)) {
|
|
200
216
|
if (val >= 0 && val < BS_INT_SMALL) {
|
|
201
217
|
a.push(BS_INT | BS_INT_SMALL | val);
|
|
@@ -207,9 +223,10 @@ export class BSEncoder {
|
|
|
207
223
|
a.push(...bytes);
|
|
208
224
|
}
|
|
209
225
|
} else {
|
|
210
|
-
|
|
226
|
+
// 0.0 is integer
|
|
227
|
+
const buffer = new ArrayBuffer(BS_FLOAT_SIZE);
|
|
211
228
|
new DataView(buffer).setFloat32(0, val, true);
|
|
212
|
-
a.push(BS_SUBT | BS_FLOAT
|
|
229
|
+
a.push(BS_SUBT | BS_FLOAT);
|
|
213
230
|
a.push(...new Uint8Array(buffer));
|
|
214
231
|
}
|
|
215
232
|
break;
|
|
@@ -279,9 +296,9 @@ export function decodeBson(buf, codes = []) {
|
|
|
279
296
|
* @returns {Uint8Array}
|
|
280
297
|
*/
|
|
281
298
|
export function encodeBson(val, codes = []) {
|
|
282
|
-
return new BSEncoder(codes).
|
|
299
|
+
return new BSEncoder(codes).encodeSingle(val);
|
|
283
300
|
}
|
|
284
301
|
|
|
285
|
-
export function
|
|
302
|
+
export function BSCode(name) {
|
|
286
303
|
return BS_CODE_PREFIX + name;
|
|
287
304
|
}
|
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,
|
|
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
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,
|
|
9
|
-
|
|
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,10 +18,12 @@
|
|
|
18
18
|
|
|
19
19
|
// =========== encode ===========
|
|
20
20
|
let test = {
|
|
21
|
-
sint:
|
|
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",
|
|
@@ -37,8 +39,8 @@
|
|
|
37
39
|
},
|
|
38
40
|
str3: "str3",
|
|
39
41
|
nul: null,
|
|
40
|
-
[
|
|
41
|
-
[
|
|
42
|
+
[BSCode('constants1')]: BSCode('string1'),
|
|
43
|
+
[BSCode('constants')]: BSCode('string'),
|
|
42
44
|
bins: new Uint8Array([1, 2, 3]),
|
|
43
45
|
};
|
|
44
46
|
|