@altronix/webtobin 0.2.0
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/package.json +23 -0
- package/webtobin.d.ts +6 -0
- package/webtobin.mjs +81 -0
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@altronix/webtobin",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "",
|
|
6
|
+
"main": "./webtobin.mjs",
|
|
7
|
+
"types": "./webtobin.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"webtobin.d.ts",
|
|
10
|
+
"webtobin.mjs",
|
|
11
|
+
"package.json"
|
|
12
|
+
],
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"cross-env": "^10.0.0",
|
|
18
|
+
"jest": "^30.1.3"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules npx jest"
|
|
22
|
+
}
|
|
23
|
+
}
|
package/webtobin.d.ts
ADDED
package/webtobin.mjs
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
//! webtobin.mjs
|
|
2
|
+
|
|
3
|
+
/** Class holding Webtobin state */
|
|
4
|
+
export default class Webtobin {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.items = [];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Parse a binary
|
|
11
|
+
*/
|
|
12
|
+
static from(bin) {
|
|
13
|
+
let c = 0;
|
|
14
|
+
let w2b = new Webtobin();
|
|
15
|
+
let view = new DataView(bin.buffer);
|
|
16
|
+
while (c < bin.length) {
|
|
17
|
+
let tlen = view.getUint32(c);
|
|
18
|
+
if (tlen == 0xffffffff) break;
|
|
19
|
+
c += 4;
|
|
20
|
+
let decoder = new TextDecoder("utf-8");
|
|
21
|
+
let path = decoder.decode(view.buffer.slice(c)).split("\0")[0];
|
|
22
|
+
let dlen = tlen - path.length - 1;
|
|
23
|
+
c += path.length + 1;
|
|
24
|
+
let data = new Uint8Array(view.buffer.slice(c, c + dlen));
|
|
25
|
+
c += dlen;
|
|
26
|
+
w2b.append(path, data);
|
|
27
|
+
}
|
|
28
|
+
return w2b;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Append a binary available by path
|
|
33
|
+
* @param {string} path
|
|
34
|
+
* @param {string|Uint8Array} data
|
|
35
|
+
* @return {Webtobin}
|
|
36
|
+
*/
|
|
37
|
+
append(path, data) {
|
|
38
|
+
let arr;
|
|
39
|
+
if (typeof data == "string") {
|
|
40
|
+
const encoder = new TextEncoder();
|
|
41
|
+
arr = encoder.encode(data);
|
|
42
|
+
} else {
|
|
43
|
+
arr = data;
|
|
44
|
+
}
|
|
45
|
+
this.items.push({ path, data: arr });
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Sort and build binary
|
|
51
|
+
* @return {Uint8Array}
|
|
52
|
+
*/
|
|
53
|
+
finish() {
|
|
54
|
+
this.items.sort((a, b) => a.path.localeCompare(b.path));
|
|
55
|
+
let arr;
|
|
56
|
+
for (const item of this.items) {
|
|
57
|
+
const encoder = new TextEncoder();
|
|
58
|
+
const path = encoder.encode(item.path);
|
|
59
|
+
const len = path.length + item.data.length + 1;
|
|
60
|
+
const buff = new ArrayBuffer(len + 4);
|
|
61
|
+
const view = new DataView(buff);
|
|
62
|
+
|
|
63
|
+
view.setUint32(0, len, false);
|
|
64
|
+
path.forEach((v, idx) => view.setUint8(idx + 4, v));
|
|
65
|
+
view.setUint8(path.length + 4, 0);
|
|
66
|
+
item.data.forEach((v, idx) => view.setUint8(idx + path.length + 5, v));
|
|
67
|
+
if (!arr) {
|
|
68
|
+
arr = new Uint8Array(view.buffer);
|
|
69
|
+
} else {
|
|
70
|
+
const next = new Uint8Array(arr.length + view.byteLength);
|
|
71
|
+
next.set(arr, 0);
|
|
72
|
+
next.set(new Uint8Array(view.buffer), arr.length);
|
|
73
|
+
arr = next;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
let ret = new Uint8Array(arr.length + 4);
|
|
77
|
+
ret.set(arr, 0);
|
|
78
|
+
ret.set(new Uint8Array([0xff, 0xff, 0xff, 0xff]), arr.length);
|
|
79
|
+
return ret;
|
|
80
|
+
}
|
|
81
|
+
}
|