@tgsnake/bytesio 2.0.1 → 2.1.0-browser.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.
@@ -8,6 +8,14 @@
8
8
  * it under the terms of the MIT License as published.
9
9
  */
10
10
  import { Buffer } from './deps.js';
11
+ /**
12
+ * Standard seek origin flags matching POSIX and Python's `io` module.
13
+ */
14
+ export declare enum SeekOrigin {
15
+ SEEK_SET = 0,
16
+ SEEK_CUR = 1,
17
+ SEEK_END = 2
18
+ }
11
19
  /**
12
20
  * A versatile in-memory binary stream class utilizing Node.js `Buffer`s.
13
21
  * Mimics the behavior of Python's built-in `io.BytesIO` module, providing a stream-like
@@ -39,7 +47,7 @@ import { Buffer } from './deps.js';
39
47
  * const stream = BytesIO.from('Hello, World!', 'utf-8');
40
48
  *
41
49
  * // Seek to position 7 (from the beginning of the buffer)
42
- * stream.seek(7, 0);
50
+ * stream.seek(7, BytesIO.SEEK_SET);
43
51
  *
44
52
  * // Read remaining bytes
45
53
  * const msg = stream.read().toString('utf-8');
@@ -47,6 +55,18 @@ import { Buffer } from './deps.js';
47
55
  * ```
48
56
  */
49
57
  export declare class BytesIO {
58
+ /**
59
+ * Seek relative to the beginning of the stream. Offset must be >= 0.
60
+ */
61
+ static readonly SEEK_SET: number;
62
+ /**
63
+ * Seek relative to the current stream position.
64
+ */
65
+ static readonly SEEK_CUR: number;
66
+ /**
67
+ * Seek relative to the end of the stream.
68
+ */
69
+ static readonly SEEK_END: number;
50
70
  /**
51
71
  * The raw internal Node.js Buffer instance where binary data is stored.
52
72
  * @private
@@ -56,45 +76,108 @@ export declare class BytesIO {
56
76
  * The current internal read/write pointer position (byte offset).
57
77
  * @private
58
78
  */
59
- private _post;
79
+ private _pos;
80
+ /**
81
+ * Indicates whether the stream has been closed.
82
+ * @private
83
+ */
84
+ private _closed;
60
85
  /**
61
86
  * Initializes a new instance of the `BytesIO` class.
62
87
  *
63
88
  * @param buffer - The initial binary buffer to populate the `BytesIO` instance with. Defaults to an empty buffer.
64
89
  */
65
- constructor(buffer?: Buffer);
90
+ constructor(buffer?: Buffer | Uint8Array);
91
+ /**
92
+ * Internal helper to verify that the stream has not been closed.
93
+ * @private
94
+ */
95
+ private _checkClosed;
66
96
  /**
67
97
  * Adjusts the position of the internal read/write pointer (offset cursor).
68
98
  *
69
99
  * @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.
100
+ * - If `whence` is `0`, must be 0 or a positive number.
101
+ * - If `whence` is `1`, relative to the current position.
102
+ * - If `whence` is `2`, relative to the end of the buffer (supports 0 for EOF, negative offsets, and clamps underflow to 0).
72
103
  * @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`).
104
+ * - `0` (`SEEK_SET`, default): Absolute positioning from the start of the buffer.
105
+ * - `1` (`SEEK_CUR`): Relative positioning from the current pointer position.
106
+ * - `2` (`SEEK_END`): Relative positioning from the end of the buffer.
76
107
  * @returns The new pointer position (relative to the start of the buffer).
77
108
  * @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
109
  * @throws {Error} If `whence` is not `0`, `1`, or `2`.
81
110
  */
82
111
  seek(offset: number, whence?: number): number;
112
+ /**
113
+ * Returns the current stream position (cursor offset).
114
+ * Parity with Python's `BytesIO.tell()`.
115
+ *
116
+ * @returns Current byte offset.
117
+ */
118
+ tell(): number;
119
+ /**
120
+ * Flushes the stream buffers (no-op for in-memory streams, provided for stream parity).
121
+ */
122
+ flush(): void;
123
+ /**
124
+ * Returns whether this stream is readable.
125
+ */
126
+ readable(): boolean;
127
+ /**
128
+ * Returns whether this stream is writable.
129
+ */
130
+ writable(): boolean;
131
+ /**
132
+ * Returns whether this stream supports random access / seeking.
133
+ */
134
+ seekable(): boolean;
135
+ /**
136
+ * Flushes and closes the stream. Once closed, any further read, write, or seek operations will throw an error.
137
+ */
138
+ close(): void;
139
+ /**
140
+ * Returns a copy of the entire buffer contents.
141
+ * Parity with Python's `BytesIO.getvalue()`.
142
+ *
143
+ * @returns An independent `Buffer` copy containing the full stream contents.
144
+ */
145
+ getvalue(): Buffer;
146
+ /**
147
+ * Returns a direct view of the underlying buffer without copying memory.
148
+ * Parity with Python's `BytesIO.getbuffer()`.
149
+ *
150
+ * @returns The underlying `Buffer` instance.
151
+ */
152
+ getbuffer(): Buffer;
153
+ /**
154
+ * Truncates the stream to the specified size (in bytes).
155
+ * If `size` is omitted, it defaults to the current pointer position.
156
+ * If the new size is larger than the current buffer, it is padded with null bytes (`0x00`).
157
+ * The current stream position is not changed.
158
+ *
159
+ * @param size - The target size in bytes. Defaults to current cursor position.
160
+ * @returns The new size.
161
+ */
162
+ truncate(size?: number): number;
163
+ /**
164
+ * Returns bytes from the current stream position without advancing the pointer.
165
+ *
166
+ * @param size - Number of bytes to peek. If omitted or <= 0, peeks all remaining bytes.
167
+ * @returns A `Buffer` containing the peeked bytes.
168
+ */
169
+ peek(size?: number): Buffer;
83
170
  /**
84
171
  * 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
172
+ * The slice is created using `Buffer.subarray`, meaning it shares the same
86
173
  * allocated memory as the original buffer.
87
174
  *
88
175
  * @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
176
  * @returns A new `BytesIO` instance containing the sliced view of the buffer.
92
177
  */
93
178
  slice(...args: Array<any>): BytesIO;
94
179
  /**
95
180
  * 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
181
  *
99
182
  * @returns An object representing the buffer in JSON format.
100
183
  */
@@ -106,10 +189,6 @@ export declare class BytesIO {
106
189
  * Decodes the underlying buffer to a string according to the specified encoding.
107
190
  *
108
191
  * @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
192
  * @returns The decoded string representation of the buffer.
114
193
  */
115
194
  toString(...args: Array<any>): string;
@@ -118,17 +197,54 @@ export declare class BytesIO {
118
197
  * Advances the internal pointer by the number of bytes successfully read.
119
198
  *
120
199
  * @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.
200
+ * - If `undefined` or negative (e.g. `-1`), reads all remaining bytes to the end of the buffer.
122
201
  * - 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.
202
+ * @returns A new `Buffer` containing the read bytes. If the pointer is at or past EOF, returns an empty buffer.
124
203
  */
125
204
  read(length?: number): Buffer;
205
+ /**
206
+ * Writes binary data at the current stream pointer position.
207
+ *
208
+ * @remarks
209
+ * If the current pointer is before the end of the buffer, existing bytes are overwritten in-place.
210
+ * If the current pointer is beyond the end of the buffer, the buffer is padded with null bytes (`0x00`) up to the pointer.
211
+ * Advances the pointer by the number of bytes written.
212
+ *
213
+ * @param data - The `Buffer`, `Uint8Array`, or `string` containing the data to write.
214
+ * @param encoding - The character encoding if `data` is a string. Defaults to `'utf-8'`.
215
+ * @returns The current `BytesIO` instance, enabling method chaining.
216
+ */
217
+ write(data: Buffer | Uint8Array | string, encoding?: BufferEncoding): this;
218
+ /**
219
+ * Reads a signed 8-bit integer from the current pointer position.
220
+ */
221
+ readInt8(size?: number): number;
222
+ /**
223
+ * Reads an unsigned 8-bit integer from the current pointer position.
224
+ */
225
+ readUInt8(size?: number): number;
226
+ /**
227
+ * Reads a signed 16-bit integer in little-endian format (`Int16LE`).
228
+ */
229
+ readInt16LE(size?: number): number;
230
+ /**
231
+ * Reads a signed 16-bit integer in big-endian format (`Int16BE`).
232
+ */
233
+ readInt16BE(size?: number): number;
234
+ /**
235
+ * Reads an unsigned 16-bit integer in little-endian format (`UInt16LE`).
236
+ */
237
+ readUInt16LE(size?: number): number;
238
+ /**
239
+ * Reads an unsigned 16-bit integer in big-endian format (`UInt16BE`).
240
+ */
241
+ readUInt16BE(size?: number): number;
126
242
  /**
127
243
  * Reads a signed 32-bit integer in little-endian format (`Int32LE`) from the current pointer position.
128
244
  * Automatically advances the pointer by the specified `size` of bytes.
129
245
  *
130
246
  * @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).
247
+ * @returns The read signed 32-bit integer.
132
248
  */
133
249
  readInt32LE(size?: number): number;
134
250
  /**
@@ -136,7 +252,7 @@ export declare class BytesIO {
136
252
  * Automatically advances the pointer by the specified `size` of bytes.
137
253
  *
138
254
  * @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).
255
+ * @returns The read signed 32-bit integer.
140
256
  */
141
257
  readInt32BE(size?: number): number;
142
258
  /**
@@ -144,7 +260,7 @@ export declare class BytesIO {
144
260
  * Automatically advances the pointer by the specified `size` of bytes.
145
261
  *
146
262
  * @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).
263
+ * @returns The read unsigned 32-bit integer.
148
264
  */
149
265
  readUInt32LE(size?: number): number;
150
266
  /**
@@ -160,7 +276,7 @@ export declare class BytesIO {
160
276
  * Automatically advances the pointer by the specified `size` of bytes.
161
277
  *
162
278
  * @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).
279
+ * @returns The read signed 64-bit BigInt.
164
280
  */
165
281
  readBigInt64LE(size?: number): bigint;
166
282
  /**
@@ -168,7 +284,7 @@ export declare class BytesIO {
168
284
  * Automatically advances the pointer by the specified `size` of bytes.
169
285
  *
170
286
  * @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).
287
+ * @returns The read signed 64-bit BigInt.
172
288
  */
173
289
  readBigInt64BE(size?: number): bigint;
174
290
  /**
@@ -176,7 +292,7 @@ export declare class BytesIO {
176
292
  * Automatically advances the pointer by the specified `size` of bytes.
177
293
  *
178
294
  * @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).
295
+ * @returns The read unsigned 64-bit BigInt.
180
296
  */
181
297
  readBigUInt64LE(size?: number): bigint;
182
298
  /**
@@ -184,11 +300,11 @@ export declare class BytesIO {
184
300
  * Automatically advances the pointer by the specified `size` of bytes.
185
301
  *
186
302
  * @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).
303
+ * @returns The read unsigned 64-bit BigInt.
188
304
  */
189
305
  readBigUInt64BE(size?: number): bigint;
190
306
  /**
191
- * Reads a 64-bit double-precision floating-point number in little-endian format (`DoubleLE`) from the current pointer position.
307
+ * Reads a 64-bit double-precision floating-point number in little-endian format (`DoubleLE`).
192
308
  * Automatically advances the pointer by the specified `size` of bytes.
193
309
  *
194
310
  * @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
@@ -196,7 +312,7 @@ export declare class BytesIO {
196
312
  */
197
313
  readDoubleLE(size?: number): number;
198
314
  /**
199
- * Reads a 64-bit double-precision floating-point number in big-endian format (`DoubleBE`) from the current pointer position.
315
+ * Reads a 64-bit double-precision floating-point number in big-endian format (`DoubleBE`).
200
316
  * Automatically advances the pointer by the specified `size` of bytes.
201
317
  *
202
318
  * @param size - The number of bytes to advance the pointer after reading. Defaults to `8`.
@@ -204,7 +320,7 @@ export declare class BytesIO {
204
320
  */
205
321
  readDoubleBE(size?: number): number;
206
322
  /**
207
- * Reads a 32-bit single-precision floating-point number in little-endian format (`FloatLE`) from the current pointer position.
323
+ * Reads a 32-bit single-precision floating-point number in little-endian format (`FloatLE`).
208
324
  * Automatically advances the pointer by the specified `size` of bytes.
209
325
  *
210
326
  * @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
@@ -212,7 +328,7 @@ export declare class BytesIO {
212
328
  */
213
329
  readFloatLE(size?: number): number;
214
330
  /**
215
- * Reads a 32-bit single-precision floating-point number in big-endian format (`FloatBE`) from the current pointer position.
331
+ * Reads a 32-bit single-precision floating-point number in big-endian format (`FloatBE`).
216
332
  * Automatically advances the pointer by the specified `size` of bytes.
217
333
  *
218
334
  * @param size - The number of bytes to advance the pointer after reading. Defaults to `4`.
@@ -220,16 +336,77 @@ export declare class BytesIO {
220
336
  */
221
337
  readFloatBE(size?: number): number;
222
338
  /**
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.
339
+ * Writes a signed 8-bit integer at the current stream pointer position.
340
+ */
341
+ writeInt8(value: number): this;
342
+ /**
343
+ * Writes an unsigned 8-bit integer at the current stream pointer position.
344
+ */
345
+ writeUInt8(value: number): this;
346
+ /**
347
+ * Writes a signed 16-bit integer in little-endian format at the current stream pointer position.
348
+ */
349
+ writeInt16LE(value: number): this;
350
+ /**
351
+ * Writes a signed 16-bit integer in big-endian format at the current stream pointer position.
352
+ */
353
+ writeInt16BE(value: number): this;
354
+ /**
355
+ * Writes an unsigned 16-bit integer in little-endian format at the current stream pointer position.
356
+ */
357
+ writeUInt16LE(value: number): this;
358
+ /**
359
+ * Writes an unsigned 16-bit integer in big-endian format at the current stream pointer position.
360
+ */
361
+ writeUInt16BE(value: number): this;
362
+ /**
363
+ * Writes a signed 32-bit integer in little-endian format at the current stream pointer position.
364
+ */
365
+ writeInt32LE(value: number): this;
366
+ /**
367
+ * Writes a signed 32-bit integer in big-endian format at the current stream pointer position.
368
+ */
369
+ writeInt32BE(value: number): this;
370
+ /**
371
+ * Writes an unsigned 32-bit integer in little-endian format at the current stream pointer position.
372
+ */
373
+ writeUInt32LE(value: number): this;
374
+ /**
375
+ * Writes an unsigned 32-bit integer in big-endian format at the current stream pointer position.
376
+ */
377
+ writeUInt32BE(value: number): this;
378
+ /**
379
+ * Writes a signed 64-bit BigInt in little-endian format at the current stream pointer position.
380
+ */
381
+ writeBigInt64LE(value: bigint): this;
382
+ /**
383
+ * Writes a signed 64-bit BigInt in big-endian format at the current stream pointer position.
384
+ */
385
+ writeBigInt64BE(value: bigint): this;
386
+ /**
387
+ * Writes an unsigned 64-bit BigInt in little-endian format at the current stream pointer position.
231
388
  */
232
- write(data: Buffer): BytesIO;
389
+ writeBigUInt64LE(value: bigint): this;
390
+ /**
391
+ * Writes an unsigned 64-bit BigInt in big-endian format at the current stream pointer position.
392
+ */
393
+ writeBigUInt64BE(value: bigint): this;
394
+ /**
395
+ * Writes a 32-bit single-precision floating-point number in little-endian format at the current stream pointer position.
396
+ */
397
+ writeFloatLE(value: number): this;
398
+ /**
399
+ * Writes a 32-bit single-precision floating-point number in big-endian format at the current stream pointer position.
400
+ */
401
+ writeFloatBE(value: number): this;
402
+ /**
403
+ * Writes a 64-bit double-precision floating-point number in little-endian format at the current stream pointer position.
404
+ */
405
+ writeDoubleLE(value: number): this;
406
+ /**
407
+ * Writes a 64-bit double-precision floating-point number in big-endian format at the current stream pointer position.
408
+ */
409
+ writeDoubleBE(value: number): this;
233
410
  /**
234
411
  * Allocates a new `BytesIO` instance populated with a zero-filled buffer of the specified size.
235
412
  *
@@ -242,17 +419,17 @@ export declare class BytesIO {
242
419
  *
243
420
  * @param input - The input data. Can be a `Buffer`, `Uint8Array`, `ArrayBuffer`, `SharedArrayBuffer`,
244
421
  * 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'`).
422
+ * @param encode - The character encoding to use if the `input` is a string.
246
423
  * @returns A new `BytesIO` instance initialized with the parsed input data.
247
424
  */
248
- static from(input: any, encode?: any): BytesIO;
425
+ static from(input: any, encode?: BufferEncoding): BytesIO;
249
426
  /**
250
- * Concatenates an array of `Buffer` instances into a single `BytesIO` instance.
427
+ * Concatenates an array of `Buffer` or `Uint8Array` instances into a single `BytesIO` instance.
251
428
  *
252
- * @param data - An array of `Buffer` objects to be joined together in order.
429
+ * @param data - An array of `Buffer` or `Uint8Array` objects to be joined together in order.
253
430
  * @returns A new `BytesIO` instance containing the concatenated binary data.
254
431
  */
255
- static concat(data: Array<Buffer>): BytesIO;
432
+ static concat(data: Array<Buffer | Uint8Array>): BytesIO;
256
433
  /**
257
434
  * Gets the total size of the underlying buffer in bytes.
258
435
  *
@@ -270,5 +447,9 @@ export declare class BytesIO {
270
447
  *
271
448
  * @returns The current pointer position.
272
449
  */
273
- get post(): number;
450
+ get pos(): number;
451
+ /**
452
+ * Gets whether the stream has been closed.
453
+ */
454
+ get closed(): boolean;
274
455
  }