@kikuchan/binary-reader 0.1.0-alpha.3 → 0.1.0-alpha.4
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 +71 -0
- package/index.cjs +1 -0
- package/index.d.cts +55 -0
- package/package.json +3 -2
package/README.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# @kikuchan/binary-reader
|
|
2
|
+
|
|
3
|
+
Small binary buffer reader for streaming-style, cursor-based parsing in Node.js and browsers.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -D @kikuchan/binary-reader
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { BinaryReader } from '@kikuchan/binary-reader';
|
|
15
|
+
|
|
16
|
+
const u8 = new Uint8Array([0x12, 0x34, 0x89, 0xab, 0xcd, 0xef]);
|
|
17
|
+
const r = new BinaryReader(u8); // default: big-endian
|
|
18
|
+
|
|
19
|
+
r.readUint16(); // 0x1234 (BE)
|
|
20
|
+
r.readUint32(); // 0x89ABCDEF (BE)
|
|
21
|
+
|
|
22
|
+
// Force endian per read
|
|
23
|
+
r.seek(0);
|
|
24
|
+
r.readUint16le(); // 0x3412
|
|
25
|
+
|
|
26
|
+
// Strings
|
|
27
|
+
const s = new BinaryReader(new TextEncoder().encode('Hi\0A'));
|
|
28
|
+
s.readString(); // 'Hi' (C-string, consumes trailing NUL)
|
|
29
|
+
s.readUint8(); // 0x41 ('A')
|
|
30
|
+
|
|
31
|
+
// Raw bytes
|
|
32
|
+
const r2 = new BinaryReader(new Uint8Array([1,2,3,4]));
|
|
33
|
+
const bytes = r2.peekBytes(2); // Uint8Array [1,2], does not advance
|
|
34
|
+
const next = r2.readBytes(3); // Uint8Array [1,2,3], advances by 3
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## API
|
|
38
|
+
|
|
39
|
+
- Position/state:
|
|
40
|
+
- `position`, `size`, `remain`, `eof()`
|
|
41
|
+
- `seek(n | label)`, `bookmark(label)`, `rewind()`, `skip(n?)`, `align(n)`
|
|
42
|
+
|
|
43
|
+
- Unsigned integers:
|
|
44
|
+
- Base: `readUint8()`, `readUint16(le?)`, `readUint32(le?)`, `readUint64(le?)`
|
|
45
|
+
- Suffix aliases: `readUint16le/be()`, `readUint32le/be()`, `readUint64le/be()`
|
|
46
|
+
|
|
47
|
+
- Signed integers:
|
|
48
|
+
- Base: `readInt8()`, `readInt16(le?)`, `readInt32(le?)`, `readInt64(le?)`
|
|
49
|
+
- Suffix aliases: `readInt16le/be()`, `readInt32le/be()`, `readInt64le/be()`
|
|
50
|
+
|
|
51
|
+
- Floating-point:
|
|
52
|
+
- Base: `readFloat16(le?)`, `readFloat32(le?)`, `readFloat64(le?)`
|
|
53
|
+
- Suffix aliases: `readFloat16le/be()`, `readFloat32le/be()`, `readFloat64le/be()`
|
|
54
|
+
|
|
55
|
+
- Bytes and strings:
|
|
56
|
+
- `peekBytes(n?) -> Uint8Array` — peek without advancing (default: all remaining)
|
|
57
|
+
- `readBytes(n?) -> Uint8Array` — read bytes and advance (default: all remaining)
|
|
58
|
+
- `readString(len?, encoding?) -> string | undefined`
|
|
59
|
+
- `len` omitted: C-string (null-terminated). Missing terminator returns `undefined` and does not advance.
|
|
60
|
+
- `len` provided: reads exactly `len` bytes; decode failure returns `undefined` and does not advance.
|
|
61
|
+
|
|
62
|
+
## Endianness
|
|
63
|
+
|
|
64
|
+
- Default endianness is big-endian.
|
|
65
|
+
- You can set a default via constructor: `new BinaryReader(u8, { littleEndian: true })`.
|
|
66
|
+
- Per-call overrides use either the second param (e.g. `readUint16(true)`) or the `le/be` suffix methods.
|
|
67
|
+
|
|
68
|
+
## Notes
|
|
69
|
+
|
|
70
|
+
- Streaming-focused: the API models a moving cursor over a buffer. Methods read at the cursor and advance.
|
|
71
|
+
- `seek()` and `skip()` clamp positions to `[0, size]`.
|
package/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Object.defineProperty(exports,`__esModule`,{value:!0});var e=class{#e;#t;#n;#r={};constructor(e,t){ArrayBuffer.isView(e)?this.#e=new DataView(e.buffer,e.byteOffset,e.byteLength):this.#e=new DataView(e),this.#t=0,this.#n=t?.littleEndian}get position(){return this.#t}get size(){return this.#e.byteLength}get remain(){return this.size-this.position}eof(){return this.remain<=0}seek(e){if(typeof e==`string`){let t=this.#r[e];if(t===void 0)throw Error(`seek: no such bookmark`);e=t}return typeof e==`number`&&(this.#t=Math.min(Math.max(e,0),this.size)),this}bookmark(e){return this.#r[e]=this.#t,this}rewind(){return this.seek(0)}skip(e){return this.seek(this.#t+(e??1))}align(e){if(!(e>0))throw RangeError(`align(n): n must be > 0`);return this.position%e?this.skip(e-this.position%e):this}peekBytes(e){e===void 0&&(e=this.remain);let t=this.#e.byteOffset+this.position;return new Uint8Array(this.#e.buffer.slice(t,t+e))}readUint8(){let e=this.#e.getUint8(this.position);return this.skip(1),e}readUint16(e){let t=this.#e.getUint16(this.position,e??this.#n);return this.skip(2),t}readUint16le(){return this.readUint16(!0)}readUint16be(){return this.readUint16(!1)}readUint32(e){let t=this.#e.getUint32(this.position,e??this.#n);return this.skip(4),t}readUint32le(){return this.readUint32(!0)}readUint32be(){return this.readUint32(!1)}readUint64(e){let t=this.#e.getBigUint64(this.position,e??this.#n);return this.skip(8),t}readUint64le(){return this.readUint64(!0)}readUint64be(){return this.readUint64(!1)}readInt8(){let e=this.#e.getInt8(this.position);return this.skip(1),e}readInt16(e){let t=this.#e.getInt16(this.position,e??this.#n);return this.skip(2),t}readInt16le(){return this.readInt16(!0)}readInt16be(){return this.readInt16(!1)}readInt32(e){let t=this.#e.getInt32(this.position,e??this.#n);return this.skip(4),t}readInt32le(){return this.readInt32(!0)}readInt32be(){return this.readInt32(!1)}readInt64(e){let t=this.#e.getBigInt64(this.position,e??this.#n);return this.skip(8),t}readInt64le(){return this.readInt64(!0)}readInt64be(){return this.readInt64(!1)}readFloat16(e){let t=this.#e.getFloat16(this.position,e??this.#n);return this.skip(2),t}readFloat16le(){return this.readFloat16(!0)}readFloat16be(){return this.readFloat16(!1)}readFloat32(e){let t=this.#e.getFloat32(this.position,e??this.#n);return this.skip(4),t}readFloat32le(){return this.readFloat32(!0)}readFloat32be(){return this.readFloat32(!1)}readFloat64(e){let t=this.#e.getFloat64(this.position,e??this.#n);return this.skip(8),t}readFloat64le(){return this.readFloat64(!0)}readFloat64be(){return this.readFloat64(!1)}#i(){let e=this.remain;for(let t=0;t<e;t++)if(this.#e.getUint8(this.position+t)===0)return t}readBytes(e){let t=this.peekBytes(e);return t&&this.skip(t.byteLength),t}readString(e,t){let n=this.remain,r=0;if(e===void 0&&(e=this.#i(),r=1),e===void 0||n<e)return;let i=this.peekBytes(e);try{if(typeof t==`function`){let n=t(i);return this.skip(e+r),n}else{let n=new TextDecoder(t??`utf-8`,{fatal:!0}).decode(i);return this.skip(e+r),n}}catch{return}}},t=e;exports.BinaryReader=e,exports.default=t;
|
package/index.d.cts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
type BinaryLike = Uint8Array | Uint8ClampedArray | ArrayBufferLike | DataView;
|
|
3
|
+
type Options = {
|
|
4
|
+
littleEndian?: boolean;
|
|
5
|
+
};
|
|
6
|
+
declare class BinaryReader {
|
|
7
|
+
#private;
|
|
8
|
+
constructor(u8: BinaryLike, opts?: Options);
|
|
9
|
+
get position(): number;
|
|
10
|
+
get size(): number;
|
|
11
|
+
get remain(): number;
|
|
12
|
+
eof(): boolean;
|
|
13
|
+
seek(n: number | string): this;
|
|
14
|
+
bookmark(s: string): this;
|
|
15
|
+
rewind(): this;
|
|
16
|
+
skip(n?: number): this;
|
|
17
|
+
align(n: number): this;
|
|
18
|
+
peekBytes(n?: number): Uint8Array<ArrayBuffer | SharedArrayBuffer>;
|
|
19
|
+
readUint8(): number;
|
|
20
|
+
readUint16(littleEndian?: boolean): number;
|
|
21
|
+
readUint16le(): number;
|
|
22
|
+
readUint16be(): number;
|
|
23
|
+
readUint32(littleEndian?: boolean): number;
|
|
24
|
+
readUint32le(): number;
|
|
25
|
+
readUint32be(): number;
|
|
26
|
+
readUint64(littleEndian?: boolean): bigint;
|
|
27
|
+
readUint64le(): bigint;
|
|
28
|
+
readUint64be(): bigint;
|
|
29
|
+
readInt8(): number;
|
|
30
|
+
readInt16(littleEndian?: boolean): number;
|
|
31
|
+
readInt16le(): number;
|
|
32
|
+
readInt16be(): number;
|
|
33
|
+
readInt32(littleEndian?: boolean): number;
|
|
34
|
+
readInt32le(): number;
|
|
35
|
+
readInt32be(): number;
|
|
36
|
+
readInt64(littleEndian?: boolean): bigint;
|
|
37
|
+
readInt64le(): bigint;
|
|
38
|
+
readInt64be(): bigint;
|
|
39
|
+
readFloat16(littleEndian?: boolean): number;
|
|
40
|
+
readFloat16le(): number;
|
|
41
|
+
readFloat16be(): number;
|
|
42
|
+
readFloat32(littleEndian?: boolean): number;
|
|
43
|
+
readFloat32le(): number;
|
|
44
|
+
readFloat32be(): number;
|
|
45
|
+
readFloat64(littleEndian?: boolean): number;
|
|
46
|
+
readFloat64le(): number;
|
|
47
|
+
readFloat64be(): number;
|
|
48
|
+
readBytes(n?: number): Uint8Array<ArrayBuffer | SharedArrayBuffer>;
|
|
49
|
+
/**
|
|
50
|
+
* @returns string. undefined on oversize or EOF.
|
|
51
|
+
*/
|
|
52
|
+
readString(len?: number, encoding?: string | ((u8: Uint8Array) => string)): string | undefined;
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
export { BinaryLike, BinaryReader, BinaryReader as default };
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"keywords": [
|
|
5
5
|
"binary-reader"
|
|
6
6
|
],
|
|
7
|
-
"version": "0.1.0-alpha.
|
|
7
|
+
"version": "0.1.0-alpha.4",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"main": "./index.js",
|
|
10
10
|
"author": "kikuchan <kikuchan98@gmail.com>",
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
"exports": {
|
|
15
15
|
".": {
|
|
16
16
|
"types": "./index.d.ts",
|
|
17
|
-
"import": "./index.js"
|
|
17
|
+
"import": "./index.js",
|
|
18
|
+
"require": "./index.cjs"
|
|
18
19
|
}
|
|
19
20
|
},
|
|
20
21
|
"repository": {
|