@tgsnake/bytesio 2.0.0-browser.0 → 2.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/dist/mod.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software : you can redistribute it and/or modify
8
+ * it under the terms of the MIT License as published.
9
+ */
10
+ export * from './src/index.js';
package/dist/mod.js ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software : you can redistribute it and/or modify
8
+ * it under the terms of the MIT License as published.
9
+ */
10
+ export * from './src/index.js';
@@ -0,0 +1,274 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software : you can redistribute it and/or modify
8
+ * it under the terms of the MIT License as published.
9
+ */
10
+ import { Buffer } from './deps.js';
11
+ /**
12
+ * A versatile in-memory binary stream class utilizing Node.js `Buffer`s.
13
+ * Mimics the behavior of Python's built-in `io.BytesIO` module, providing a stream-like
14
+ * interface for reading and writing binary data in memory.
15
+ *
16
+ * Supports sequential and random-access operations using a seekable internal pointer, and provides
17
+ * convenient helper methods to read and write integers, floats, doubles, and raw buffers.
18
+ *
19
+ * @remarks
20
+ * This class is highly optimized for performance and is particularly useful in environments
21
+ * requiring low-level binary manipulation, such as network protocols, file parsers, and custom serializers.
22
+ *
23
+ * @example
24
+ * **Writing and Reading Data:**
25
+ * ```typescript
26
+ * import { BytesIO } from '@tgsnake/bytesio';
27
+ *
28
+ * // Create a BytesIO instance from existing data
29
+ * const stream = BytesIO.from([0x01, 0x02, 0x03, 0x04]);
30
+ *
31
+ * // Read a 32-bit signed integer in little-endian format
32
+ * const intVal = stream.readInt32LE(); // Reads 4 bytes and advances pointer
33
+ * console.log(intVal); // Output: 67305985
34
+ * ```
35
+ *
36
+ * @example
37
+ * **Seeking the Stream Pointer:**
38
+ * ```typescript
39
+ * const stream = BytesIO.from('Hello, World!', 'utf-8');
40
+ *
41
+ * // Seek to position 7 (from the beginning of the buffer)
42
+ * stream.seek(7, 0);
43
+ *
44
+ * // Read remaining bytes
45
+ * const msg = stream.read().toString('utf-8');
46
+ * console.log(msg); // Output: 'World!'
47
+ * ```
48
+ */
49
+ export declare class BytesIO {
50
+ /**
51
+ * The raw internal Node.js Buffer instance where binary data is stored.
52
+ * @private
53
+ */
54
+ private _buffer;
55
+ /**
56
+ * The current internal read/write pointer position (byte offset).
57
+ * @private
58
+ */
59
+ private _post;
60
+ /**
61
+ * Initializes a new instance of the `BytesIO` class.
62
+ *
63
+ * @param buffer - The initial binary buffer to populate the `BytesIO` instance with. Defaults to an empty buffer.
64
+ */
65
+ constructor(buffer?: Buffer);
66
+ /**
67
+ * Adjusts the position of the internal read/write pointer (offset cursor).
68
+ *
69
+ * @param offset - The byte offset relative to the reference point defined by `whence`.
70
+ * - If `whence` is `0`, must be 0 or positive.
71
+ * - If `whence` is `2`, must be less than 0.
72
+ * @param whence - The reference point for the seek operation:
73
+ * - `0` (default): Absolute positioning from the start of the buffer.
74
+ * - `1`: Relative positioning from the current pointer position.
75
+ * - `2`: Relative positioning from the end of the buffer (expects a negative `offset`).
76
+ * @returns The new pointer position (relative to the start of the buffer).
77
+ * @throws {Error} If `whence` is `0` and `offset` is negative.
78
+ * @throws {Error} If `whence` is `2` and `offset` is non-negative.
79
+ * @throws {Error} If `whence` is `2` and the absolute seek location would be less than 0.
80
+ * @throws {Error} If `whence` is not `0`, `1`, or `2`.
81
+ */
82
+ seek(offset: number, whence?: number): number;
83
+ /**
84
+ * Returns a new `BytesIO` instance populated with a subarray/slice of the current buffer.
85
+ * The slice is created using the standard `Buffer.subarray` method, meaning it shares the same
86
+ * allocated memory as the original buffer.
87
+ *
88
+ * @param args - Arguments passed directly to the underlying `Buffer.subarray` method.
89
+ * Typically `[start[, end]]`, where `start` is the starting index (default 0) and
90
+ * `end` is the ending index (exclusive, default buffer length).
91
+ * @returns A new `BytesIO` instance containing the sliced view of the buffer.
92
+ */
93
+ slice(...args: Array<any>): BytesIO;
94
+ /**
95
+ * Returns a JSON representation of the underlying buffer.
96
+ * This is useful for serializing the binary data, representing it as an object
97
+ * containing a `type` property (typically `"Buffer"`) and a `data` array of byte values.
98
+ *
99
+ * @returns An object representing the buffer in JSON format.
100
+ */
101
+ toJSON(): {
102
+ type: 'Buffer';
103
+ data: number[];
104
+ };
105
+ /**
106
+ * Decodes the underlying buffer to a string according to the specified encoding.
107
+ *
108
+ * @param args - Arguments passed directly to the underlying `Buffer.toString` method.
109
+ * Typically `[encoding[, start[, end]]]`, where:
110
+ * - `encoding` is the character encoding to use (e.g., `'utf8'`, `'hex'`, `'base64'`).
111
+ * - `start` is the byte offset to start decoding (default 0).
112
+ * - `end` is the byte offset to stop decoding (exclusive, default buffer length).
113
+ * @returns The decoded string representation of the buffer.
114
+ */
115
+ toString(...args: Array<any>): string;
116
+ /**
117
+ * Reads binary data from the current pointer position.
118
+ * Advances the internal pointer by the number of bytes successfully read.
119
+ *
120
+ * @param length - The number of bytes to read.
121
+ * - If `undefined` or not provided, reads all remaining bytes from the current pointer to the end of the buffer.
122
+ * - If provided, reads at most `length` bytes from the current pointer position.
123
+ * @returns A new `Buffer` containing the read bytes. If the pointer is out of bounds or `length` is less than 1, returns an empty buffer.
124
+ */
125
+ read(length?: number): Buffer;
126
+ /**
127
+ * Reads a signed 32-bit integer in little-endian format (`Int32LE`) from the current pointer position.
128
+ * Automatically advances the pointer by the specified `size` of bytes.
129
+ *
130
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
131
+ * @returns The read signed 32-bit integer (value ranges from -2,147,483,648 to 2,147,483,647).
132
+ */
133
+ readInt32LE(size?: number): number;
134
+ /**
135
+ * Reads a signed 32-bit integer in big-endian format (`Int32BE`) from the current pointer position.
136
+ * Automatically advances the pointer by the specified `size` of bytes.
137
+ *
138
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
139
+ * @returns The read signed 32-bit integer (value ranges from -2,147,483,648 to 2,147,483,647).
140
+ */
141
+ readInt32BE(size?: number): number;
142
+ /**
143
+ * Reads an unsigned 32-bit integer in little-endian format (`UInt32LE`) from the current pointer position.
144
+ * Automatically advances the pointer by the specified `size` of bytes.
145
+ *
146
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
147
+ * @returns The read unsigned 32-bit integer (value ranges from 0 to 4,294,967,295).
148
+ */
149
+ readUInt32LE(size?: number): number;
150
+ /**
151
+ * Reads an unsigned 32-bit integer in big-endian format (`UInt32BE`) from the current pointer position.
152
+ * Automatically advances the pointer by the specified `size` of bytes.
153
+ *
154
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
155
+ * @returns The read unsigned 32-bit integer.
156
+ */
157
+ readUInt32BE(size?: number): number;
158
+ /**
159
+ * Reads a signed 64-bit BigInt in little-endian format (`BigInt64LE`) from the current pointer position.
160
+ * Automatically advances the pointer by the specified `size` of bytes.
161
+ *
162
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
163
+ * @returns The read signed 64-bit BigInt (value ranges from -9,223,372,036,854,775,808n to 9,223,372,036,854,775,807n).
164
+ */
165
+ readBigInt64LE(size?: number): bigint;
166
+ /**
167
+ * Reads a signed 64-bit BigInt in big-endian format (`BigInt64BE`) from the current pointer position.
168
+ * Automatically advances the pointer by the specified `size` of bytes.
169
+ *
170
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
171
+ * @returns The read signed 64-bit BigInt (value ranges from -9,223,372,036,854,775,808n to 9,223,372,036,854,775,807n).
172
+ */
173
+ readBigInt64BE(size?: number): bigint;
174
+ /**
175
+ * Reads an unsigned 64-bit BigInt in little-endian format (`BigUInt64LE`) from the current pointer position.
176
+ * Automatically advances the pointer by the specified `size` of bytes.
177
+ *
178
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
179
+ * @returns The read unsigned 64-bit BigInt (value ranges from 0n to 18,446,744,073,709,551,615n).
180
+ */
181
+ readBigUInt64LE(size?: number): bigint;
182
+ /**
183
+ * Reads an unsigned 64-bit BigInt in big-endian format (`BigUInt64BE`) from the current pointer position.
184
+ * Automatically advances the pointer by the specified `size` of bytes.
185
+ *
186
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
187
+ * @returns The read unsigned 64-bit BigInt (value ranges from 0n to 18,446,744,073,709,551,615n).
188
+ */
189
+ readBigUInt64BE(size?: number): bigint;
190
+ /**
191
+ * Reads a 64-bit double-precision floating-point number in little-endian format (`DoubleLE`) from the current pointer position.
192
+ * Automatically advances the pointer by the specified `size` of bytes.
193
+ *
194
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
195
+ * @returns The read double-precision floating-point number.
196
+ */
197
+ readDoubleLE(size?: number): number;
198
+ /**
199
+ * Reads a 64-bit double-precision floating-point number in big-endian format (`DoubleBE`) from the current pointer position.
200
+ * Automatically advances the pointer by the specified `size` of bytes.
201
+ *
202
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
203
+ * @returns The read double-precision floating-point number.
204
+ */
205
+ readDoubleBE(size?: number): number;
206
+ /**
207
+ * Reads a 32-bit single-precision floating-point number in little-endian format (`FloatLE`) from the current pointer position.
208
+ * Automatically advances the pointer by the specified `size` of bytes.
209
+ *
210
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
211
+ * @returns The read single-precision floating-point number.
212
+ */
213
+ readFloatLE(size?: number): number;
214
+ /**
215
+ * Reads a 32-bit single-precision floating-point number in big-endian format (`FloatBE`) from the current pointer position.
216
+ * Automatically advances the pointer by the specified `size` of bytes.
217
+ *
218
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
219
+ * @returns The read single-precision floating-point number.
220
+ */
221
+ readFloatBE(size?: number): number;
222
+ /**
223
+ * Appends the provided binary data to the absolute end of the underlying buffer.
224
+ *
225
+ * @remarks
226
+ * This operation concatenates the new data to the existing buffer, increasing its total length.
227
+ * Note that this method does not modify or advance the current pointer position (`post`).
228
+ *
229
+ * @param data - The `Buffer` containing the binary data to append.
230
+ * @returns The current `BytesIO` instance, enabling method chaining.
231
+ */
232
+ write(data: Buffer): BytesIO;
233
+ /**
234
+ * Allocates a new `BytesIO` instance populated with a zero-filled buffer of the specified size.
235
+ *
236
+ * @param size - The number of bytes to allocate for the initial buffer.
237
+ * @returns A new `BytesIO` instance with the allocated buffer.
238
+ */
239
+ static alloc(size: number): BytesIO;
240
+ /**
241
+ * Creates a new `BytesIO` instance from the given input data, delegating to `Buffer.from`.
242
+ *
243
+ * @param input - The input data. Can be a `Buffer`, `Uint8Array`, `ArrayBuffer`, `SharedArrayBuffer`,
244
+ * an `Array` of numbers, a `string`, or an object with a `valueOf` or `Symbol.toPrimitive` method.
245
+ * @param encode - The character encoding to use if the `input` is a string (e.g., `'utf8'`, `'hex'`, `'base64'`).
246
+ * @returns A new `BytesIO` instance initialized with the parsed input data.
247
+ */
248
+ static from(input: any, encode?: any): BytesIO;
249
+ /**
250
+ * Concatenates an array of `Buffer` instances into a single `BytesIO` instance.
251
+ *
252
+ * @param data - An array of `Buffer` objects to be joined together in order.
253
+ * @returns A new `BytesIO` instance containing the concatenated binary data.
254
+ */
255
+ static concat(data: Array<Buffer>): BytesIO;
256
+ /**
257
+ * Gets the total size of the underlying buffer in bytes.
258
+ *
259
+ * @returns The byte length of the buffer.
260
+ */
261
+ get length(): number;
262
+ /**
263
+ * Gets the raw, underlying `Buffer` instance managed by this `BytesIO` object.
264
+ *
265
+ * @returns The underlying `Buffer` instance.
266
+ */
267
+ get buffer(): Buffer;
268
+ /**
269
+ * Gets the current read/write pointer (offset index) within the buffer.
270
+ *
271
+ * @returns The current pointer position.
272
+ */
273
+ get post(): number;
274
+ }
@@ -0,0 +1,382 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software : you can redistribute it and/or modify
8
+ * it under the terms of the MIT License as published.
9
+ */
10
+ import { Buffer } from './deps.js';
11
+ /**
12
+ * A versatile in-memory binary stream class utilizing Node.js `Buffer`s.
13
+ * Mimics the behavior of Python's built-in `io.BytesIO` module, providing a stream-like
14
+ * interface for reading and writing binary data in memory.
15
+ *
16
+ * Supports sequential and random-access operations using a seekable internal pointer, and provides
17
+ * convenient helper methods to read and write integers, floats, doubles, and raw buffers.
18
+ *
19
+ * @remarks
20
+ * This class is highly optimized for performance and is particularly useful in environments
21
+ * requiring low-level binary manipulation, such as network protocols, file parsers, and custom serializers.
22
+ *
23
+ * @example
24
+ * **Writing and Reading Data:**
25
+ * ```typescript
26
+ * import { BytesIO } from '@tgsnake/bytesio';
27
+ *
28
+ * // Create a BytesIO instance from existing data
29
+ * const stream = BytesIO.from([0x01, 0x02, 0x03, 0x04]);
30
+ *
31
+ * // Read a 32-bit signed integer in little-endian format
32
+ * const intVal = stream.readInt32LE(); // Reads 4 bytes and advances pointer
33
+ * console.log(intVal); // Output: 67305985
34
+ * ```
35
+ *
36
+ * @example
37
+ * **Seeking the Stream Pointer:**
38
+ * ```typescript
39
+ * const stream = BytesIO.from('Hello, World!', 'utf-8');
40
+ *
41
+ * // Seek to position 7 (from the beginning of the buffer)
42
+ * stream.seek(7, 0);
43
+ *
44
+ * // Read remaining bytes
45
+ * const msg = stream.read().toString('utf-8');
46
+ * console.log(msg); // Output: 'World!'
47
+ * ```
48
+ */
49
+ export class BytesIO {
50
+ /**
51
+ * The raw internal Node.js Buffer instance where binary data is stored.
52
+ * @private
53
+ */
54
+ _buffer;
55
+ /**
56
+ * The current internal read/write pointer position (byte offset).
57
+ * @private
58
+ */
59
+ _post;
60
+ /**
61
+ * Initializes a new instance of the `BytesIO` class.
62
+ *
63
+ * @param buffer - The initial binary buffer to populate the `BytesIO` instance with. Defaults to an empty buffer.
64
+ */
65
+ constructor(buffer = Buffer.alloc(0)) {
66
+ this._post = 0;
67
+ this._buffer = buffer;
68
+ return this;
69
+ }
70
+ /**
71
+ * Adjusts the position of the internal read/write pointer (offset cursor).
72
+ *
73
+ * @param offset - The byte offset relative to the reference point defined by `whence`.
74
+ * - If `whence` is `0`, must be 0 or positive.
75
+ * - If `whence` is `2`, must be less than 0.
76
+ * @param whence - The reference point for the seek operation:
77
+ * - `0` (default): Absolute positioning from the start of the buffer.
78
+ * - `1`: Relative positioning from the current pointer position.
79
+ * - `2`: Relative positioning from the end of the buffer (expects a negative `offset`).
80
+ * @returns The new pointer position (relative to the start of the buffer).
81
+ * @throws {Error} If `whence` is `0` and `offset` is negative.
82
+ * @throws {Error} If `whence` is `2` and `offset` is non-negative.
83
+ * @throws {Error} If `whence` is `2` and the absolute seek location would be less than 0.
84
+ * @throws {Error} If `whence` is not `0`, `1`, or `2`.
85
+ */
86
+ seek(offset, whence = 0) {
87
+ if (whence === 0) {
88
+ if (offset < 0) {
89
+ throw new Error(`offset of BytesIO.seek must be zero or positive value when whence is 0`);
90
+ }
91
+ this._post = whence;
92
+ }
93
+ else if (whence === 1) {
94
+ this._post += offset;
95
+ }
96
+ else if (whence === 2) {
97
+ if (offset >= 0) {
98
+ throw new Error(`offset of BytesIO.seek must be less than zero or negative value when whence is 2`);
99
+ }
100
+ if (Buffer.byteLength(this._buffer) + offset < 0) {
101
+ throw new Error(`offset out of range, offset ${offset} is less than the available buffer length.`);
102
+ }
103
+ this._post = Buffer.byteLength(this._buffer) + offset;
104
+ }
105
+ else {
106
+ throw new Error(`whence must be 0 or 1 or 2, but receive ${whence}`);
107
+ }
108
+ return this._post;
109
+ }
110
+ /**
111
+ * Returns a new `BytesIO` instance populated with a subarray/slice of the current buffer.
112
+ * The slice is created using the standard `Buffer.subarray` method, meaning it shares the same
113
+ * allocated memory as the original buffer.
114
+ *
115
+ * @param args - Arguments passed directly to the underlying `Buffer.subarray` method.
116
+ * Typically `[start[, end]]`, where `start` is the starting index (default 0) and
117
+ * `end` is the ending index (exclusive, default buffer length).
118
+ * @returns A new `BytesIO` instance containing the sliced view of the buffer.
119
+ */
120
+ slice(...args) {
121
+ return new BytesIO(this._buffer.subarray(...args));
122
+ }
123
+ /**
124
+ * Returns a JSON representation of the underlying buffer.
125
+ * This is useful for serializing the binary data, representing it as an object
126
+ * containing a `type` property (typically `"Buffer"`) and a `data` array of byte values.
127
+ *
128
+ * @returns An object representing the buffer in JSON format.
129
+ */
130
+ toJSON() {
131
+ return this._buffer.toJSON();
132
+ }
133
+ /**
134
+ * Decodes the underlying buffer to a string according to the specified encoding.
135
+ *
136
+ * @param args - Arguments passed directly to the underlying `Buffer.toString` method.
137
+ * Typically `[encoding[, start[, end]]]`, where:
138
+ * - `encoding` is the character encoding to use (e.g., `'utf8'`, `'hex'`, `'base64'`).
139
+ * - `start` is the byte offset to start decoding (default 0).
140
+ * - `end` is the byte offset to stop decoding (exclusive, default buffer length).
141
+ * @returns The decoded string representation of the buffer.
142
+ */
143
+ toString(...args) {
144
+ return this._buffer.toString(...args);
145
+ }
146
+ /**
147
+ * Reads binary data from the current pointer position.
148
+ * Advances the internal pointer by the number of bytes successfully read.
149
+ *
150
+ * @param length - The number of bytes to read.
151
+ * - If `undefined` or not provided, reads all remaining bytes from the current pointer to the end of the buffer.
152
+ * - If provided, reads at most `length` bytes from the current pointer position.
153
+ * @returns A new `Buffer` containing the read bytes. If the pointer is out of bounds or `length` is less than 1, returns an empty buffer.
154
+ */
155
+ read(length) {
156
+ if (length === undefined) {
157
+ const results = this._buffer.subarray(this._post);
158
+ this.seek(Buffer.byteLength(results), 1);
159
+ return results;
160
+ }
161
+ if (length >= 1 && this._post <= Buffer.byteLength(this._buffer)) {
162
+ const results = this._buffer.subarray(this._post, this._post + length);
163
+ this.seek(length, 1);
164
+ return results;
165
+ }
166
+ return Buffer.alloc(0);
167
+ }
168
+ /**
169
+ * Reads a signed 32-bit integer in little-endian format (`Int32LE`) from the current pointer position.
170
+ * Automatically advances the pointer by the specified `size` of bytes.
171
+ *
172
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
173
+ * @returns The read signed 32-bit integer (value ranges from -2,147,483,648 to 2,147,483,647).
174
+ */
175
+ readInt32LE(size = 4) {
176
+ const results = this._buffer.readInt32LE(this._post);
177
+ this.seek(size, 1);
178
+ return results;
179
+ }
180
+ /**
181
+ * Reads a signed 32-bit integer in big-endian format (`Int32BE`) from the current pointer position.
182
+ * Automatically advances the pointer by the specified `size` of bytes.
183
+ *
184
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
185
+ * @returns The read signed 32-bit integer (value ranges from -2,147,483,648 to 2,147,483,647).
186
+ */
187
+ readInt32BE(size = 4) {
188
+ const results = this._buffer.readInt32BE(this._post);
189
+ this.seek(size, 1);
190
+ return results;
191
+ }
192
+ /**
193
+ * Reads an unsigned 32-bit integer in little-endian format (`UInt32LE`) from the current pointer position.
194
+ * Automatically advances the pointer by the specified `size` of bytes.
195
+ *
196
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
197
+ * @returns The read unsigned 32-bit integer (value ranges from 0 to 4,294,967,295).
198
+ */
199
+ readUInt32LE(size = 4) {
200
+ const results = this._buffer.readUInt32LE(this._post);
201
+ this.seek(size, 1);
202
+ return results;
203
+ }
204
+ /**
205
+ * Reads an unsigned 32-bit integer in big-endian format (`UInt32BE`) from the current pointer position.
206
+ * Automatically advances the pointer by the specified `size` of bytes.
207
+ *
208
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
209
+ * @returns The read unsigned 32-bit integer.
210
+ */
211
+ readUInt32BE(size = 4) {
212
+ const results = this._buffer.readInt32BE(this._post);
213
+ this.seek(size, 1);
214
+ return results;
215
+ }
216
+ /**
217
+ * Reads a signed 64-bit BigInt in little-endian format (`BigInt64LE`) from the current pointer position.
218
+ * Automatically advances the pointer by the specified `size` of bytes.
219
+ *
220
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
221
+ * @returns The read signed 64-bit BigInt (value ranges from -9,223,372,036,854,775,808n to 9,223,372,036,854,775,807n).
222
+ */
223
+ readBigInt64LE(size = 8) {
224
+ const results = this._buffer.readBigInt64LE(this._post);
225
+ this.seek(size, 1);
226
+ return results;
227
+ }
228
+ /**
229
+ * Reads a signed 64-bit BigInt in big-endian format (`BigInt64BE`) from the current pointer position.
230
+ * Automatically advances the pointer by the specified `size` of bytes.
231
+ *
232
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
233
+ * @returns The read signed 64-bit BigInt (value ranges from -9,223,372,036,854,775,808n to 9,223,372,036,854,775,807n).
234
+ */
235
+ readBigInt64BE(size = 8) {
236
+ const results = this._buffer.readBigInt64BE(this._post);
237
+ this.seek(size, 1);
238
+ return results;
239
+ }
240
+ /**
241
+ * Reads an unsigned 64-bit BigInt in little-endian format (`BigUInt64LE`) from the current pointer position.
242
+ * Automatically advances the pointer by the specified `size` of bytes.
243
+ *
244
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
245
+ * @returns The read unsigned 64-bit BigInt (value ranges from 0n to 18,446,744,073,709,551,615n).
246
+ */
247
+ readBigUInt64LE(size = 8) {
248
+ const results = this._buffer.readBigUInt64LE(this._post);
249
+ this.seek(size, 1);
250
+ return results;
251
+ }
252
+ /**
253
+ * Reads an unsigned 64-bit BigInt in big-endian format (`BigUInt64BE`) from the current pointer position.
254
+ * Automatically advances the pointer by the specified `size` of bytes.
255
+ *
256
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
257
+ * @returns The read unsigned 64-bit BigInt (value ranges from 0n to 18,446,744,073,709,551,615n).
258
+ */
259
+ readBigUInt64BE(size = 8) {
260
+ const results = this._buffer.readBigUInt64BE(this._post);
261
+ this.seek(size, 1);
262
+ return results;
263
+ }
264
+ /**
265
+ * Reads a 64-bit double-precision floating-point number in little-endian format (`DoubleLE`) from the current pointer position.
266
+ * Automatically advances the pointer by the specified `size` of bytes.
267
+ *
268
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
269
+ * @returns The read double-precision floating-point number.
270
+ */
271
+ readDoubleLE(size = 8) {
272
+ const results = this._buffer.readDoubleLE(this._post);
273
+ this.seek(size, 1);
274
+ return results;
275
+ }
276
+ /**
277
+ * Reads a 64-bit double-precision floating-point number in big-endian format (`DoubleBE`) from the current pointer position.
278
+ * Automatically advances the pointer by the specified `size` of bytes.
279
+ *
280
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
281
+ * @returns The read double-precision floating-point number.
282
+ */
283
+ readDoubleBE(size = 8) {
284
+ const results = this._buffer.readDoubleBE(this._post);
285
+ this.seek(size, 1);
286
+ return results;
287
+ }
288
+ /**
289
+ * Reads a 32-bit single-precision floating-point number in little-endian format (`FloatLE`) from the current pointer position.
290
+ * Automatically advances the pointer by the specified `size` of bytes.
291
+ *
292
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
293
+ * @returns The read single-precision floating-point number.
294
+ */
295
+ readFloatLE(size = 4) {
296
+ const results = this._buffer.readFloatLE(this._post);
297
+ this.seek(size, 1);
298
+ return results;
299
+ }
300
+ /**
301
+ * Reads a 32-bit single-precision floating-point number in big-endian format (`FloatBE`) from the current pointer position.
302
+ * Automatically advances the pointer by the specified `size` of bytes.
303
+ *
304
+ * @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
305
+ * @returns The read single-precision floating-point number.
306
+ */
307
+ readFloatBE(size = 4) {
308
+ const results = this._buffer.readFloatBE(this._post);
309
+ this.seek(size, 1);
310
+ return results;
311
+ }
312
+ /**
313
+ * Appends the provided binary data to the absolute end of the underlying buffer.
314
+ *
315
+ * @remarks
316
+ * This operation concatenates the new data to the existing buffer, increasing its total length.
317
+ * Note that this method does not modify or advance the current pointer position (`post`).
318
+ *
319
+ * @param data - The `Buffer` containing the binary data to append.
320
+ * @returns The current `BytesIO` instance, enabling method chaining.
321
+ */
322
+ write(data) {
323
+ this._buffer = Buffer.concat([
324
+ this._buffer,
325
+ data,
326
+ ]);
327
+ return this;
328
+ }
329
+ /**
330
+ * Allocates a new `BytesIO` instance populated with a zero-filled buffer of the specified size.
331
+ *
332
+ * @param size - The number of bytes to allocate for the initial buffer.
333
+ * @returns A new `BytesIO` instance with the allocated buffer.
334
+ */
335
+ static alloc(size) {
336
+ return new BytesIO(Buffer.alloc(size));
337
+ }
338
+ /**
339
+ * Creates a new `BytesIO` instance from the given input data, delegating to `Buffer.from`.
340
+ *
341
+ * @param input - The input data. Can be a `Buffer`, `Uint8Array`, `ArrayBuffer`, `SharedArrayBuffer`,
342
+ * an `Array` of numbers, a `string`, or an object with a `valueOf` or `Symbol.toPrimitive` method.
343
+ * @param encode - The character encoding to use if the `input` is a string (e.g., `'utf8'`, `'hex'`, `'base64'`).
344
+ * @returns A new `BytesIO` instance initialized with the parsed input data.
345
+ */
346
+ static from(input, encode) {
347
+ return new BytesIO(Buffer.from(input, encode));
348
+ }
349
+ /**
350
+ * Concatenates an array of `Buffer` instances into a single `BytesIO` instance.
351
+ *
352
+ * @param data - An array of `Buffer` objects to be joined together in order.
353
+ * @returns A new `BytesIO` instance containing the concatenated binary data.
354
+ */
355
+ static concat(data) {
356
+ return new BytesIO(Buffer.concat(data));
357
+ }
358
+ /**
359
+ * Gets the total size of the underlying buffer in bytes.
360
+ *
361
+ * @returns The byte length of the buffer.
362
+ */
363
+ get length() {
364
+ return Buffer.byteLength(this._buffer);
365
+ }
366
+ /**
367
+ * Gets the raw, underlying `Buffer` instance managed by this `BytesIO` object.
368
+ *
369
+ * @returns The underlying `Buffer` instance.
370
+ */
371
+ get buffer() {
372
+ return this._buffer;
373
+ }
374
+ /**
375
+ * Gets the current read/write pointer (offset index) within the buffer.
376
+ *
377
+ * @returns The current pointer position.
378
+ */
379
+ get post() {
380
+ return this._post;
381
+ }
382
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software : you can redistribute it and/or modify
8
+ * it under the terms of the MIT License as published.
9
+ */
10
+ /**
11
+ * This file is responsible for providing the necessary dependencies for the tgsnake library in browser environments.
12
+ * It imports and exports various modules and libraries that are used throughout the codebase,
13
+ * ensuring compatibility in the browser. By centralizing these imports and exports, we can maintain a clean and organized codebase while also making it easier to manage dependencies and ensure that the library works seamlessly.
14
+ */
15
+ export { Buffer } from 'buffer';
@@ -0,0 +1,15 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software : you can redistribute it and/or modify
8
+ * it under the terms of the MIT License as published.
9
+ */
10
+ /**
11
+ * This file is responsible for providing the necessary dependencies for the tgsnake library in browser environments.
12
+ * It imports and exports various modules and libraries that are used throughout the codebase,
13
+ * ensuring compatibility in the browser. By centralizing these imports and exports, we can maintain a clean and organized codebase while also making it easier to manage dependencies and ensure that the library works seamlessly.
14
+ */
15
+ export { Buffer } from 'buffer';
@@ -0,0 +1,18 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software : you can redistribute it and/or modify
8
+ * it under the terms of the GPL v3 License as published.
9
+ */
10
+ /**
11
+ * This file is responsible for providing the necessary dependencies for the tgsnake library.
12
+ * It imports and exports various modules and libraries that are used throughout the codebase,
13
+ * ensuring compatibility across different platforms such as Node.js, Deno, Bun, and browsers.
14
+ * By centralizing these imports and exports, we can maintain a clean and organized codebase
15
+ * while also making it easier to manage dependencies and ensure that the library works seamlessly
16
+ * in various environments.
17
+ */
18
+ export { Buffer } from 'node:buffer';
@@ -0,0 +1,18 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software : you can redistribute it and/or modify
8
+ * it under the terms of the GPL v3 License as published.
9
+ */
10
+ /**
11
+ * This file is responsible for providing the necessary dependencies for the tgsnake library.
12
+ * It imports and exports various modules and libraries that are used throughout the codebase,
13
+ * ensuring compatibility across different platforms such as Node.js, Deno, Bun, and browsers.
14
+ * By centralizing these imports and exports, we can maintain a clean and organized codebase
15
+ * while also making it easier to manage dependencies and ensure that the library works seamlessly
16
+ * in various environments.
17
+ */
18
+ export { Buffer } from 'node:buffer';
@@ -0,0 +1,11 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software : you can redistribute it and/or modify
8
+ * it under the terms of the MIT License as published.
9
+ */
10
+ export { BytesIO } from './BytesIO.js';
11
+ export { BytesIO as default } from './BytesIO.js';
@@ -0,0 +1,11 @@
1
+ /**
2
+ * tgsnake - Telegram MTProto library for javascript or typescript.
3
+ * Copyright (C) 2026 tgsnake <https://github.com/tgsnake>
4
+ *
5
+ * THIS FILE IS PART OF TGSNAKE
6
+ *
7
+ * tgsnake is a free software : you can redistribute it and/or modify
8
+ * it under the terms of the MIT License as published.
9
+ */
10
+ export { BytesIO } from './BytesIO.js';
11
+ export { BytesIO as default } from './BytesIO.js';
package/package.json CHANGED
@@ -1,48 +1,45 @@
1
- {
2
- "name": "@tgsnake/bytesio",
3
- "packageManager": "yarn@4.9.2",
4
- "version": "2.0.0-browser.0",
5
- "description": "A simple BytesIO implementation for Typescript",
6
- "main": "./browser/mod.js",
7
- "types": "./browser/mod.d.ts",
8
- "type": "module",
9
- "scripts": {
10
- "build": "tsc",
11
- "build:jsr": "node ./generator/jsr_version/index.js",
12
- "clean": "rm -rf dist",
13
- "prepare": "yarn build",
14
- "prettier": "NODE_OPTIONS=--max_old_space_size=4096 prettier -w .",
15
- "docs": "typedoc --out docs src/BytesIO.ts --excludePrivate --excludeProtected --excludeInternal --theme default --readme none"
16
- },
17
- "homepage": "https://tgsnake.js.org",
18
- "keywords": [
19
- "tgsnake",
20
- "BytesIO",
21
- "Buffer"
22
- ],
23
- "bugs": "https://github.com/tgsnake/bytesio/issues",
24
- "repository": "https://github.com/tgsnake/bytesio",
25
- "author": "butthx",
26
- "files": [
27
- "browser/**/**"
28
- ],
29
- "license": "MIT",
30
- "devDependencies": {
31
- "@types/bun": "^1.3.14",
32
- "@types/node": "^25.9.1",
33
- "prettier": "^3.8.3",
34
- "typedoc": "^0.28.19",
35
- "typescript": "^6.0.3"
36
- },
37
- "engineStrict": true,
38
- "engines": {
39
- "node": ">=22.0.0",
40
- "npm": ">=6.0.0",
41
- "yarn": ">=1.0.0",
42
- "deno": ">=1.0.0",
43
- "bun": ">=1.0.0"
44
- },
45
- "dependencies": {
46
- "buffer": "^6.0.3"
47
- }
48
- }
1
+ {
2
+ "name": "@tgsnake/bytesio",
3
+ "packageManager": "yarn@4.9.2",
4
+ "version": "2.0.0",
5
+ "description": "A simple BytesIO implementation for Typescript",
6
+ "main": "dist/mod.js",
7
+ "types": "dist/mod.d.ts",
8
+ "type": "module",
9
+ "scripts": {
10
+ "build": "tsc",
11
+ "build:jsr": "node ./generator/jsr_version/index.js",
12
+ "clean": "rm -rf dist",
13
+ "prepare": "yarn build",
14
+ "prettier": "NODE_OPTIONS=--max_old_space_size=4096 prettier -w .",
15
+ "docs": "typedoc --out docs src/BytesIO.ts --excludePrivate --excludeProtected --excludeInternal --theme default --readme none"
16
+ },
17
+ "homepage": "https://tgsnake.js.org",
18
+ "keywords": [
19
+ "tgsnake",
20
+ "BytesIO",
21
+ "Buffer"
22
+ ],
23
+ "bugs": "https://github.com/tgsnake/bytesio/issues",
24
+ "repository": "https://github.com/tgsnake/bytesio",
25
+ "author": "butthx",
26
+ "files": [
27
+ "dist/**/**"
28
+ ],
29
+ "license": "MIT",
30
+ "devDependencies": {
31
+ "@types/bun": "^1.3.14",
32
+ "@types/node": "^25.9.1",
33
+ "prettier": "^3.8.3",
34
+ "typedoc": "^0.28.19",
35
+ "typescript": "^6.0.3"
36
+ },
37
+ "engineStrict": true,
38
+ "engines": {
39
+ "node": ">=22.0.0",
40
+ "npm": ">=6.0.0",
41
+ "yarn": ">=1.0.0",
42
+ "deno": ">=1.0.0",
43
+ "bun": ">=1.0.0"
44
+ }
45
+ }