@tgsnake/bytesio 1.0.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/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/BytesIO.d.ts +33 -0
- package/dist/BytesIO.js +145 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -0
- package/package.json +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 tgsnake
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# BytesIO 🚀
|
|
2
|
+
|
|
3
|
+
A simple Buffer implementation inspired by Python's `BytesIO`.
|
|
4
|
+
This project was originally based on the source code from [`@tgsnake/core`](https://github.com/tgsnake/core). 🐍
|
|
5
|
+
|
|
6
|
+
## Features ✨
|
|
7
|
+
|
|
8
|
+
- In-memory byte stream operations 💾
|
|
9
|
+
- Read and write bytes efficiently ⚡
|
|
10
|
+
- Python-like API for ease of use 🐍
|
|
11
|
+
|
|
12
|
+
## Installation 📦
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
# Example installation command
|
|
16
|
+
npm install @tgsnake/bytesio
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage 🛠️
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { BytesIO } from '@tgsnake/bytesio';
|
|
23
|
+
|
|
24
|
+
const stream = new BytesIO();
|
|
25
|
+
stream.write(Buffer.from([1, 2, 3]));
|
|
26
|
+
stream.seek(0);
|
|
27
|
+
console.log(stream.read(3)); // Output: <Buffer 01 02 03>
|
|
28
|
+
```
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
export declare class BytesIO {
|
|
3
|
+
private _buffer;
|
|
4
|
+
private _post;
|
|
5
|
+
constructor(buffer?: Buffer);
|
|
6
|
+
seek(offset: number, whence?: number): number;
|
|
7
|
+
slice(...args: Array<any>): BytesIO;
|
|
8
|
+
toJSON(): {
|
|
9
|
+
type: "Buffer";
|
|
10
|
+
data: number[];
|
|
11
|
+
};
|
|
12
|
+
toString(...args: Array<any>): string;
|
|
13
|
+
read(length?: number): Buffer;
|
|
14
|
+
readInt32LE(size?: number): number;
|
|
15
|
+
readInt32BE(size?: number): number;
|
|
16
|
+
readUInt32LE(size?: number): number;
|
|
17
|
+
readUInt32BE(size?: number): number;
|
|
18
|
+
readBigInt64LE(size?: number): bigint;
|
|
19
|
+
readBigInt64BE(size?: number): bigint;
|
|
20
|
+
readBigUInt64LE(size?: number): bigint;
|
|
21
|
+
readBigUInt64BE(size?: number): bigint;
|
|
22
|
+
readDoubleLE(size?: number): number;
|
|
23
|
+
readDoubleBE(size?: number): number;
|
|
24
|
+
readFloatLE(size?: number): number;
|
|
25
|
+
readFloatBE(size?: number): number;
|
|
26
|
+
write(data: Buffer): BytesIO;
|
|
27
|
+
static alloc(size: number): BytesIO;
|
|
28
|
+
static from(input: any, encode?: any): BytesIO;
|
|
29
|
+
static concat(data: Array<Buffer>): BytesIO;
|
|
30
|
+
get length(): number;
|
|
31
|
+
get buffer(): Buffer;
|
|
32
|
+
get post(): number;
|
|
33
|
+
}
|
package/dist/BytesIO.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BytesIO = void 0;
|
|
4
|
+
const node_buffer_1 = require("node:buffer");
|
|
5
|
+
class BytesIO {
|
|
6
|
+
_buffer;
|
|
7
|
+
_post;
|
|
8
|
+
constructor(buffer = node_buffer_1.Buffer.alloc(0)) {
|
|
9
|
+
this._post = 0;
|
|
10
|
+
this._buffer = buffer;
|
|
11
|
+
return this;
|
|
12
|
+
}
|
|
13
|
+
seek(offset, whence = 0) {
|
|
14
|
+
if (whence === 0) {
|
|
15
|
+
if (offset < 0) {
|
|
16
|
+
throw new Error(`offset of BytesIO.seek must be zero or positive value when whence is 0`);
|
|
17
|
+
}
|
|
18
|
+
this._post = whence;
|
|
19
|
+
}
|
|
20
|
+
else if (whence === 1) {
|
|
21
|
+
this._post += offset;
|
|
22
|
+
}
|
|
23
|
+
else if (whence === 2) {
|
|
24
|
+
if (offset >= 0) {
|
|
25
|
+
throw new Error(`offset of BytesIO.seek must be less than zero or negative value when whence is 2`);
|
|
26
|
+
}
|
|
27
|
+
if (node_buffer_1.Buffer.byteLength(this._buffer) + offset < 0) {
|
|
28
|
+
throw new Error(`offset out of range, offset ${offset} is less than the available buffer length.`);
|
|
29
|
+
}
|
|
30
|
+
this._post = node_buffer_1.Buffer.byteLength(this._buffer) + offset;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
throw new Error(`whence must be 0 or 1 or 2, but receive ${whence}`);
|
|
34
|
+
}
|
|
35
|
+
return this._post;
|
|
36
|
+
}
|
|
37
|
+
slice(...args) {
|
|
38
|
+
return new BytesIO(this._buffer.subarray(...args));
|
|
39
|
+
}
|
|
40
|
+
toJSON() {
|
|
41
|
+
return this._buffer.toJSON();
|
|
42
|
+
}
|
|
43
|
+
toString(...args) {
|
|
44
|
+
return this._buffer.toString(...args);
|
|
45
|
+
}
|
|
46
|
+
read(length) {
|
|
47
|
+
if (length === undefined) {
|
|
48
|
+
const results = this._buffer.subarray(this._post);
|
|
49
|
+
this.seek(node_buffer_1.Buffer.byteLength(results), 1);
|
|
50
|
+
return results;
|
|
51
|
+
}
|
|
52
|
+
if (length >= 1 && this._post <= node_buffer_1.Buffer.byteLength(this._buffer)) {
|
|
53
|
+
const results = this._buffer.subarray(this._post, this._post + length);
|
|
54
|
+
this.seek(length, 1);
|
|
55
|
+
return results;
|
|
56
|
+
}
|
|
57
|
+
return node_buffer_1.Buffer.alloc(0);
|
|
58
|
+
}
|
|
59
|
+
readInt32LE(size = 4) {
|
|
60
|
+
const results = this._buffer.readInt32LE(this._post);
|
|
61
|
+
this.seek(size, 1);
|
|
62
|
+
return results;
|
|
63
|
+
}
|
|
64
|
+
readInt32BE(size = 4) {
|
|
65
|
+
const results = this._buffer.readInt32BE(this._post);
|
|
66
|
+
this.seek(size, 1);
|
|
67
|
+
return results;
|
|
68
|
+
}
|
|
69
|
+
readUInt32LE(size = 4) {
|
|
70
|
+
const results = this._buffer.readUInt32LE(this._post);
|
|
71
|
+
this.seek(size, 1);
|
|
72
|
+
return results;
|
|
73
|
+
}
|
|
74
|
+
readUInt32BE(size = 4) {
|
|
75
|
+
const results = this._buffer.readInt32BE(this._post);
|
|
76
|
+
this.seek(size, 1);
|
|
77
|
+
return results;
|
|
78
|
+
}
|
|
79
|
+
readBigInt64LE(size = 8) {
|
|
80
|
+
const results = this._buffer.readBigInt64LE(this._post);
|
|
81
|
+
this.seek(size, 1);
|
|
82
|
+
return results;
|
|
83
|
+
}
|
|
84
|
+
readBigInt64BE(size = 8) {
|
|
85
|
+
const results = this._buffer.readBigInt64BE(this._post);
|
|
86
|
+
this.seek(size, 1);
|
|
87
|
+
return results;
|
|
88
|
+
}
|
|
89
|
+
readBigUInt64LE(size = 8) {
|
|
90
|
+
const results = this._buffer.readBigUInt64LE(this._post);
|
|
91
|
+
this.seek(size, 1);
|
|
92
|
+
return results;
|
|
93
|
+
}
|
|
94
|
+
readBigUInt64BE(size = 8) {
|
|
95
|
+
const results = this._buffer.readBigUInt64BE(this._post);
|
|
96
|
+
this.seek(size, 1);
|
|
97
|
+
return results;
|
|
98
|
+
}
|
|
99
|
+
readDoubleLE(size = 8) {
|
|
100
|
+
const results = this._buffer.readDoubleLE(this._post);
|
|
101
|
+
this.seek(size, 1);
|
|
102
|
+
return results;
|
|
103
|
+
}
|
|
104
|
+
readDoubleBE(size = 8) {
|
|
105
|
+
const results = this._buffer.readDoubleBE(this._post);
|
|
106
|
+
this.seek(size, 1);
|
|
107
|
+
return results;
|
|
108
|
+
}
|
|
109
|
+
readFloatLE(size = 4) {
|
|
110
|
+
const results = this._buffer.readFloatLE(this._post);
|
|
111
|
+
this.seek(size, 1);
|
|
112
|
+
return results;
|
|
113
|
+
}
|
|
114
|
+
readFloatBE(size = 4) {
|
|
115
|
+
const results = this._buffer.readFloatBE(this._post);
|
|
116
|
+
this.seek(size, 1);
|
|
117
|
+
return results;
|
|
118
|
+
}
|
|
119
|
+
write(data) {
|
|
120
|
+
this._buffer = node_buffer_1.Buffer.concat([
|
|
121
|
+
this._buffer,
|
|
122
|
+
data,
|
|
123
|
+
]);
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
static alloc(size) {
|
|
127
|
+
return new BytesIO(node_buffer_1.Buffer.alloc(size));
|
|
128
|
+
}
|
|
129
|
+
static from(input, encode) {
|
|
130
|
+
return new BytesIO(node_buffer_1.Buffer.from(input, encode));
|
|
131
|
+
}
|
|
132
|
+
static concat(data) {
|
|
133
|
+
return new BytesIO(node_buffer_1.Buffer.concat(data));
|
|
134
|
+
}
|
|
135
|
+
get length() {
|
|
136
|
+
return node_buffer_1.Buffer.byteLength(this._buffer);
|
|
137
|
+
}
|
|
138
|
+
get buffer() {
|
|
139
|
+
return this._buffer;
|
|
140
|
+
}
|
|
141
|
+
get post() {
|
|
142
|
+
return this._post;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
exports.BytesIO = BytesIO;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = exports.BytesIO = void 0;
|
|
4
|
+
var BytesIO_js_1 = require("./BytesIO.js");
|
|
5
|
+
Object.defineProperty(exports, "BytesIO", { enumerable: true, get: function () { return BytesIO_js_1.BytesIO; } });
|
|
6
|
+
var BytesIO_js_2 = require("./BytesIO.js");
|
|
7
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return BytesIO_js_2.BytesIO; } });
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tgsnake/bytesio",
|
|
3
|
+
"packageManager": "yarn@4.9.2",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "A simple BytesIO implementation for Typescript",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"clean": "rm -rf dist",
|
|
11
|
+
"prepare": "yarn build",
|
|
12
|
+
"prettier": "NODE_OPTIONS=--max_old_space_size=4096 prettier -w .",
|
|
13
|
+
"docs": "typedoc --out docs src/BytesIO.ts --excludePrivate --excludeProtected --excludeInternal --theme default --readme none"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://tgsnake.js.org",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"tgsnake",
|
|
18
|
+
"BytesIO",
|
|
19
|
+
"Buffer"
|
|
20
|
+
],
|
|
21
|
+
"bugs": "https://github.com/tgsnake/bytesio/issues",
|
|
22
|
+
"repository": "https://github.com/tgsnake/bytesio",
|
|
23
|
+
"author": "butthx",
|
|
24
|
+
"files": [
|
|
25
|
+
"dist/**/**"
|
|
26
|
+
],
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/bun": "^1.2.18",
|
|
30
|
+
"@types/node": "^24.0.13",
|
|
31
|
+
"prettier": "^3.6.2",
|
|
32
|
+
"typedoc": "^0.28.7",
|
|
33
|
+
"typescript": "^5.8.3"
|
|
34
|
+
}
|
|
35
|
+
}
|