@alexgyver/bson 2.1.0 → 2.1.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/README.md +22 -6
- package/bson.js +16 -11
- package/bson.min.js +1 -1
- package/package.json +1 -1
- package/test.html +5 -7
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# bson.js
|
|
2
|
-
Распаковщик бинарного JSON для библиотеки [BSON](https://github.com/GyverLibs/BSON).
|
|
2
|
+
Распаковщик и запаковщик бинарного JSON для библиотеки [BSON](https://github.com/GyverLibs/BSON).
|
|
3
3
|
|
|
4
4
|
> npm i @alexgyver/bson
|
|
5
5
|
|
|
@@ -10,10 +10,26 @@ const codes = [
|
|
|
10
10
|
'constants',
|
|
11
11
|
];
|
|
12
12
|
|
|
13
|
-
let
|
|
14
|
-
|
|
13
|
+
let test = {
|
|
14
|
+
int: 123,
|
|
15
|
+
float: 3.14,
|
|
16
|
+
arr: [
|
|
17
|
+
"str",
|
|
18
|
+
true,
|
|
19
|
+
1234,
|
|
20
|
+
new Uint8Array([1, 2, 3]),
|
|
21
|
+
],
|
|
22
|
+
obj: {
|
|
23
|
+
str2: "str2",
|
|
24
|
+
true: true,
|
|
25
|
+
},
|
|
26
|
+
str3: "str3",
|
|
27
|
+
nul: null,
|
|
28
|
+
[getCode('constants', codes)]: getCode('string', codes),
|
|
29
|
+
bins: new Uint8Array([1, 2, 3]),
|
|
30
|
+
};
|
|
15
31
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
32
|
+
let enc = encodeBson(test);
|
|
33
|
+
let json = decodeBson(enc, codes);
|
|
34
|
+
console.log(json);
|
|
19
35
|
```
|
package/bson.js
CHANGED
|
@@ -156,16 +156,16 @@ function _makeBins(obj, bins) {
|
|
|
156
156
|
* @param {*} val
|
|
157
157
|
* @returns {Uint8Array}
|
|
158
158
|
*/
|
|
159
|
-
export function encodeBson(val) {
|
|
159
|
+
export function encodeBson(val, codes = []) {
|
|
160
160
|
let arr = [];
|
|
161
|
-
_encode(val, arr);
|
|
161
|
+
_encode(val, arr, codes);
|
|
162
162
|
return Uint8Array.from(arr);
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
-
function _encode(val, arr) {
|
|
165
|
+
function _encode(val, arr, codes) {
|
|
166
166
|
if (Array.isArray(val)) {
|
|
167
167
|
arr.push(BS_CONTAINER | BS_CONT_ARR | BS_CONT_OPEN);
|
|
168
|
-
val.forEach(v => _encode(v, arr));
|
|
168
|
+
val.forEach(v => _encode(v, arr, codes));
|
|
169
169
|
arr.push(BS_CONTAINER | BS_CONT_ARR | BS_CONT_CLOSE);
|
|
170
170
|
return;
|
|
171
171
|
}
|
|
@@ -182,8 +182,8 @@ function _encode(val, arr) {
|
|
|
182
182
|
} else {
|
|
183
183
|
arr.push(BS_CONTAINER | BS_CONT_OBJ | BS_CONT_OPEN);
|
|
184
184
|
for (const [key, value] of Object.entries(val)) {
|
|
185
|
-
_encode(key, arr);
|
|
186
|
-
_encode(value, arr);
|
|
185
|
+
_encode(key, arr, codes);
|
|
186
|
+
_encode(value, arr, codes);
|
|
187
187
|
}
|
|
188
188
|
arr.push(BS_CONTAINER | BS_CONT_OBJ | BS_CONT_CLOSE);
|
|
189
189
|
}
|
|
@@ -212,9 +212,14 @@ function _encode(val, arr) {
|
|
|
212
212
|
|
|
213
213
|
case 'string':
|
|
214
214
|
if (val.startsWith(BS_CODE_PREFIX)) {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
215
|
+
val = val.slice(BS_CODE_PREFIX.length);
|
|
216
|
+
let code = codes.indexOf(val);
|
|
217
|
+
if (code >= 0) {
|
|
218
|
+
arr.push(BS_CODE | BS_D16_MSB(code));
|
|
219
|
+
arr.push(BS_D16_LSB(code));
|
|
220
|
+
} else {
|
|
221
|
+
_encode(val, arr, codes);
|
|
222
|
+
}
|
|
218
223
|
} else {
|
|
219
224
|
const bytes = new TextEncoder().encode(val);
|
|
220
225
|
const len = Math.min(bytes.length, BS_MAX_LEN);
|
|
@@ -234,6 +239,6 @@ function _encode(val, arr) {
|
|
|
234
239
|
}
|
|
235
240
|
}
|
|
236
241
|
|
|
237
|
-
export function getCode(name
|
|
238
|
-
return BS_CODE_PREFIX +
|
|
242
|
+
export function getCode(name) {
|
|
243
|
+
return BS_CODE_PREFIX + name;
|
|
239
244
|
}
|
package/bson.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{$R:()=>U,QC:()=>C,lo:()=>F});const r=0,n=32,s=64,o=96,a=128,c=160,i=192,l=224,u=16,h=8,f=4,p=2,b=8191,g=e=>224&e,y=e=>31&e,d=e=>1&e,w=16,O=e=>e&w,N=e=>15&e,k=4,A=e=>15&e,B=e=>e>>8&31,
|
|
1
|
+
var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{$R:()=>U,QC:()=>C,lo:()=>F});const r=0,n=32,s=64,o=96,a=128,c=160,i=192,l=224,u=16,h=8,f=4,p=2,b=8191,g=e=>224&e,y=e=>31&e,d=e=>1&e,w=16,O=e=>e&w,N=e=>15&e,k=4,A=e=>15&e,B=e=>e>>8&31,_=e=>255&e,m=(e,t)=>(e<<8|t)>>>0,j="__BSON_BIN_",x="__BSON_CODE_";function F(e,t=[]){if(!(e&&e instanceof Uint8Array))return null;if(!e.length)return{};let f=[],p=[],b=!0,w="";try{for(let B=0;B<e.length;B++){const _=g(e[B]),x=y(e[B]);switch(_){case i:if(x&h){let e=x&u?"{":"[";w+=e,p.push(e)}else","==w[w.length-1]&&(w=w.slice(0,-1)),w+=(x&u?"}":"]")+",",p.pop();b=!0;continue;case a:w+='"'+t[m(x,e[++B])]+'"';break;case r:{let t=m(x,e[++B]);w+=JSON.stringify((new TextDecoder).decode(e.slice(B+1,B+1+t))),B+=t}break;case s:{O(x)&&(w+="-");let t=N(x),r=new Uint8Array(8);r.set(e.slice(B+1,B+1+t)),w+=new BigUint64Array(r.buffer)[0],B+=t}break;case n:w+=d(x)?"true":"false";break;case l:w+="null";break;case o:{let t=new DataView(e.buffer,e.byteOffset+B+1,k).getFloat32(0,!0);w+=isNaN(t)||!isFinite(t)?"null":t.toFixed(A(x)),B+=k}break;case c:{let t=m(x,e[++B]);B++,w+='"'+j+f.length+'"',f.push(e.slice(B,B+t)),B+=t-1}}"{"===p[p.length-1]?(w+=b?":":",",b=!b):w+=","}}catch(e){throw console.error(e,w),new Error("BSON decode error")}","==w[w.length-1]&&(w=w.slice(0,-1));try{let e=JSON.parse(w);return f.length&&S(e,f),e}catch(e){throw console.error(e,w),new Error("JSON parse error")}}function S(e,t){if("object"==typeof e)for(let r in e)"object"==typeof e[r]&&null!==e[r]?S(e[r],t):"string"==typeof e[r]&&e[r].startsWith(j)&&(e[r]=t[e[r].slice(j.length)])}function U(e,t=[]){let r=[];return E(e,r,t),Uint8Array.from(r)}function E(e,t,g){if(Array.isArray(e))return t.push(i|f|h),e.forEach(e=>E(e,t,g)),void t.push(i|f|p);switch(typeof e){case"object":if(null===e)t.push(l);else if(e instanceof Uint8Array){const r=Math.min(e.length,b);t.push(c|B(r)),t.push(_(r)),t.push(...e.slice(0,r))}else{t.push(i|u|h);for(const[r,n]of Object.entries(e))E(r,t,g),E(n,t,g);t.push(i|u|p)}break;case"bigint":case"number":if(Number.isInteger(e)){const r=(e=BigInt(e))<0;r&&(e=-e);let n=[];for(;e;)n.push(Number(0xFFn&e)),e>>=8n;t.push(s|(r?w:0)|n.length),t.push(...n)}else{const r=new ArrayBuffer(k);new DataView(r).setFloat32(0,e,!0),t.push(4|o),t.push(...new Uint8Array(r))}break;case"string":if(e.startsWith(x)){e=e.slice(x.length);let r=g.indexOf(e);r>=0?(t.push(a|B(r)),t.push(_(r))):E(e,t,g)}else{const n=(new TextEncoder).encode(e),s=Math.min(n.length,b);t.push(r|B(s)),t.push(_(s)),t.push(...n.slice(0,s))}break;case"boolean":t.push(n|(e?1:0));break;default:t.push(l)}}function C(e){return x+e}const D=t.lo,v=t.$R,I=t.QC;export{D as decodeBson,v as encodeBson,I as getCode};
|
package/package.json
CHANGED
package/test.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
|
-
|
|
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
|
|
|
@@ -16,9 +16,6 @@
|
|
|
16
16
|
'constants',
|
|
17
17
|
];
|
|
18
18
|
|
|
19
|
-
// =========== getCode ===========
|
|
20
|
-
console.log(getCode('constants', codes));
|
|
21
|
-
|
|
22
19
|
// =========== encode ===========
|
|
23
20
|
let test = {
|
|
24
21
|
int: 123,
|
|
@@ -35,11 +32,12 @@
|
|
|
35
32
|
},
|
|
36
33
|
str3: "str3",
|
|
37
34
|
nul: null,
|
|
38
|
-
[getCode('
|
|
35
|
+
[getCode('constants1')]: getCode('string1'),
|
|
36
|
+
[getCode('constants')]: getCode('string'),
|
|
39
37
|
bins: new Uint8Array([1, 2, 3]),
|
|
40
38
|
};
|
|
41
39
|
|
|
42
|
-
let enc = encodeBson(test);
|
|
40
|
+
let enc = encodeBson(test, codes);
|
|
43
41
|
let json = decodeBson(enc, codes);
|
|
44
42
|
console.log(json);
|
|
45
43
|
|