@alexgyver/bson 2.0.0 → 2.0.2

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 CHANGED
@@ -66,25 +66,17 @@ export default function decodeBson(b, codes = []) {
66
66
 
67
67
  case BS_STRING: {
68
68
  let len = unpack5(data, b[++i]);
69
- i++;
70
- let txt = new TextDecoder().decode(b.slice(i, i + len));
71
- txt = txt.replaceAll(/([^\\])\\([^\"\\nrt])/ig, "$1\\\\$2")
72
- .replaceAll(/\t/ig, "\\t")
73
- .replaceAll(/\n/ig, "\\n")
74
- .replaceAll(/\r/ig, "\\r")
75
- .replaceAll(/([^\\])(")/ig, '$1\\"');
76
- s += '"' + txt + '"';
77
- i += len - 1;
69
+ s += JSON.stringify(new TextDecoder().decode(b.slice(i + 1, i + 1 + len)));
70
+ i += len;
78
71
  } break;
79
72
 
80
73
  case BS_INTEGER: {
81
74
  if (data & BS_NEGATIVE) s += '-';
82
75
  let len = data & 0b1111;
83
- let v = BigInt(0);
84
- for (let j = 0; j < len; j++) {
85
- v |= BigInt(b[++i]) << BigInt(j * 8);
86
- }
87
- s += v;
76
+ let u8 = new Uint8Array(8);
77
+ u8.set(b.slice(i + 1, i + 1 + len));
78
+ s += new BigUint64Array(u8.buffer)[0];
79
+ i += len;
88
80
  } break;
89
81
 
90
82
  case BS_BOOLEAN:
@@ -92,12 +84,9 @@ export default function decodeBson(b, codes = []) {
92
84
  break;
93
85
 
94
86
  case BS_FLOAT: {
95
- let v = 0;
96
- for (let j = 0; j < 4; j++) {
97
- v |= b[++i] << (j * 8);
98
- }
99
- let f = new Float32Array(new Uint32Array([v]).buffer)[0];
87
+ let f = new Float32Array(b.slice(i + 1, i + 1 + 4).buffer)[0];
100
88
  s += (isNaN(f) || !isFinite(f)) ? 'null' : f.toFixed(data);
89
+ i += 4;
101
90
  } break;
102
91
 
103
92
  case BS_BINARY: {
@@ -118,7 +107,7 @@ export default function decodeBson(b, codes = []) {
118
107
  }
119
108
  }
120
109
  } catch (e) {
121
- console.log(s);
110
+ console.error(e, s);
122
111
  throw new Error("BSON decode error");
123
112
  }
124
113
 
@@ -129,7 +118,7 @@ export default function decodeBson(b, codes = []) {
129
118
  if (bins.length) makeBins(obj, bins);
130
119
  return obj;
131
120
  } catch (e) {
132
- console.log(s);
121
+ console.error(e, s);
133
122
  throw new Error("JSON parse error");
134
123
  }
135
124
  }
package/bson.min.js ADDED
@@ -0,0 +1 @@
1
+ var e={d:(r,t)=>{for(var n in t)e.o(t,n)&&!e.o(r,n)&&Object.defineProperty(r,n,{enumerable:!0,get:t[n]})},o:(e,r)=>Object.prototype.hasOwnProperty.call(e,r)},r={};function t(e,r=[]){if(!(e&&e instanceof Uint8Array))return null;if(!e.length)return{};const t="__BSON_BINARY";function n(e,r){return(e<<8|r)>>>0}let o=[],l=[],c=!0,s="";try{for(let i=0;i<e.length;i++){const a=224&e[i],f=31&e[i];switch(a){case 192:if(8&f){let e=16&f?"{":"[";s+=e,l.push(e)}else","==s[s.length-1]&&(s=s.slice(0,-1)),s+=(16&f?"}":"]")+",",l.pop();c=!0;continue;case 128:s+='"'+r[n(f,e[++i])]+'"';break;case 0:{let r=n(f,e[++i]);s+=JSON.stringify((new TextDecoder).decode(e.slice(i+1,i+1+r))),i+=r}break;case 64:{16&f&&(s+="-");let r=15&f,t=new Uint8Array(8);t.set(e.slice(i+1,i+1+r)),s+=new BigUint64Array(t.buffer)[0],i+=r}break;case 32:s+=1&f?"true":"false";break;case 96:{let r=new Float32Array(e.slice(i+1,i+1+4).buffer)[0];s+=isNaN(r)||!isFinite(r)?"null":r.toFixed(f),i+=4}break;case 160:{let r=n(f,e[++i]);i++,s+='"'+t+"#"+o.length+'"',o.push(e.slice(i,i+r)),i+=r-1}}"{"===l[l.length-1]?(s+=c?":":",",c=!c):s+=","}}catch(e){throw console.error(e,s),new Error("BSON decode error")}","==s[s.length-1]&&(s=s.slice(0,-1));try{let e=JSON.parse(s);return o.length&&function e(r,n){if("object"==typeof r)for(let o in r)"object"==typeof r[o]&&null!==r[o]?e(r[o],n):"string"==typeof r[o]&&r[o].startsWith(t)&&(r[o]=n[r[o].split("#")[1]])}(e,o),e}catch(e){throw console.error(e,s),new Error("JSON parse error")}}e.d(r,{A:()=>t});const n=r.A;export{n as default};
package/package.json CHANGED
@@ -1,14 +1,21 @@
1
1
  {
2
2
  "name": "@alexgyver/bson",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Decode BSON from BSON Arduino library",
5
5
  "main": "./bson.js",
6
6
  "module": "./bson.js",
7
7
  "types": "./bson.js",
8
+ "scripts": {
9
+ "build": "webpack --config ./webpack.config.js"
10
+ },
8
11
  "repository": {
9
12
  "type": "git",
10
13
  "url": "git+https://github.com/GyverLibs/bson.js.git"
11
14
  },
15
+ "devDependencies": {
16
+ "webpack": "^5.98.0",
17
+ "webpack-cli": "^6.0.1"
18
+ },
12
19
  "author": "AlexGyver <alex@alexgyver.ru>",
13
20
  "license": "MIT"
14
21
  }
package/test.html ADDED
@@ -0,0 +1,35 @@
1
+ <!DOCTYPE html>
2
+
3
+ <body>
4
+ <button id="req">REQUEST</button>
5
+ <p id="out" style="white-space: break-spaces;"></p>
6
+
7
+ <script type="module">
8
+ import decodeBson from 'https://gyverlibs.github.io/bson.js/dist/bson.js';
9
+
10
+ const url = 'http://192.168.1.54';
11
+
12
+ const codes = [
13
+ 'some',
14
+ 'string',
15
+ 'constants',
16
+ ];
17
+
18
+ req.onclick = async () => {
19
+ let res = await fetch(url + '/bson');
20
+ try {
21
+ let arr = new Uint8Array(await res.arrayBuffer());
22
+ let json = decodeBson(arr, codes);
23
+ console.log(arr);
24
+ console.log(JSON.stringify(json), JSON.stringify(json).length);
25
+ console.log(json);
26
+ out.innerText = JSON.stringify(json, null, 2);
27
+ } catch (e) {
28
+ console.log(e);
29
+ out.innerText = e;
30
+ }
31
+ }
32
+ </script>
33
+ </body>
34
+
35
+ </html>
@@ -0,0 +1,14 @@
1
+ module.exports = {
2
+ entry: './bson.js',
3
+ output: {
4
+ path: __dirname,
5
+ filename: 'bson.min.js',
6
+ library: {
7
+ type: 'module'
8
+ }
9
+ },
10
+ experiments: {
11
+ outputModule: true
12
+ },
13
+ mode: "production",
14
+ };