Skip to content

Commit b0fecd8

Browse files
committed
refactor: switch to typescript
1 parent b94ad0a commit b0fecd8

File tree

6 files changed

+64
-93
lines changed

6 files changed

+64
-93
lines changed

lib/binarypack.js renamed to lib/binarypack.ts

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BufferBuilder, binaryFeatures } from "./bufferbuilder";
1+
import { BufferBuilder } from "./bufferbuilder";
22

33
export function unpack(data) {
44
const unpacker = new Unpacker(data);
@@ -7,13 +7,16 @@ export function unpack(data) {
77
export function pack(data) {
88
const packer = new Packer();
99
packer.pack(data);
10-
const buffer = packer.getBuffer();
11-
return buffer;
10+
return packer.getBuffer();
1211
}
1312

1413
export default { pack, unpack };
1514

1615
class Unpacker {
16+
private index: number;
17+
private readonly dataBuffer: any;
18+
private readonly dataView: Uint8Array;
19+
private readonly length: number;
1720
constructor(data) {
1821
// Data is ArrayBuffer
1922
this.index = 0;
@@ -234,8 +237,7 @@ class Unpacker {
234237
const map = {};
235238
for (let i = 0; i < size; i++) {
236239
const key = this.unpack();
237-
const value = this.unpack();
238-
map[key] = value;
240+
map[key] = this.unpack();
239241
}
240242
return map;
241243
}
@@ -269,6 +271,7 @@ class Unpacker {
269271
}
270272

271273
class Packer {
274+
private bufferBuilder: BufferBuilder;
272275
constructor() {
273276
this.bufferBuilder = new BufferBuilder();
274277
}
@@ -310,17 +313,9 @@ class Packer {
310313
) {
311314
this.pack_bin(value);
312315
} else if (constructor == ArrayBuffer) {
313-
if (binaryFeatures.useArrayBufferView) {
314-
this.pack_bin(new Uint8Array(value));
315-
} else {
316-
this.pack_bin(value);
317-
}
316+
this.pack_bin(new Uint8Array(value));
318317
} else if ("BYTES_PER_ELEMENT" in value) {
319-
if (binaryFeatures.useArrayBufferView) {
320-
this.pack_bin(new Uint8Array(value.buffer));
321-
} else {
322-
this.pack_bin(value.buffer);
323-
}
318+
this.pack_bin(new Uint8Array(value.buffer));
324319
} else if (
325320
constructor == Object ||
326321
constructor.toString().startsWith("class")

lib/bufferbuilder.js

-71
This file was deleted.

lib/bufferbuilder.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class BufferBuilder {
2+
private _pieces: any[];
3+
private readonly _parts: any[];
4+
constructor() {
5+
this._pieces = [];
6+
this._parts = [];
7+
}
8+
9+
append(data) {
10+
if (typeof data === "number") {
11+
this._pieces.push(data);
12+
} else {
13+
this.flush();
14+
this._parts.push(data);
15+
}
16+
}
17+
18+
flush() {
19+
if (this._pieces.length > 0) {
20+
let buf = new Uint8Array(this._pieces);
21+
this._parts.push(buf);
22+
this._pieces = [];
23+
}
24+
}
25+
26+
getBuffer() {
27+
this.flush();
28+
return new Blob(this._parts);
29+
}
30+
}
31+
32+
export { BufferBuilder };

lib/exports.js

-6
This file was deleted.

package-lock.json

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"homepage": "https://github.com/peers/js-binarypack",
66
"main": "dist/binarypack.js",
77
"module": "dist/module.mjs",
8-
"source": "lib/binarypack.js",
8+
"source": "lib/binarypack.ts",
99
"scripts": {
1010
"watch": "parcel watch",
1111
"build": "parcel build",
@@ -39,6 +39,7 @@
3939
"jest-environment-jsdom": "^29.4.3",
4040
"parcel": "^2.8.3",
4141
"prettier": "^2.8.4",
42+
"typescript": "^4.9.5",
4243
"uglifyjs": "^2.4.11"
4344
},
4445
"license": "BSD",

0 commit comments

Comments
 (0)