@fuman/io 0.0.2 → 0.0.3

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/bits/reader.cjs CHANGED
@@ -6,6 +6,7 @@ class BitReader {
6
6
  #readable;
7
7
  #currentByte = 0;
8
8
  #currentBitIdx = 0;
9
+ /** @param readable fuman readable stream */
9
10
  constructor(readable) {
10
11
  this.#readable = readable;
11
12
  }
@@ -41,6 +42,7 @@ class BitReader {
41
42
  }
42
43
  return result;
43
44
  }
45
+ /** read a number of bits from the stream, and return them as a number */
44
46
  readBits(size) {
45
47
  let result = 0;
46
48
  if (this.#currentBitIdx !== 0) {
@@ -71,6 +73,7 @@ class BitReader {
71
73
  }
72
74
  return result;
73
75
  }
76
+ /** read a number of bits from the stream, and return them as a bigint */
74
77
  readBitsBig(size) {
75
78
  let result = 0n;
76
79
  if (this.#currentBitIdx !== 0) {
@@ -102,6 +105,7 @@ class BitReader {
102
105
  }
103
106
  return result;
104
107
  }
108
+ /** skip a number of bits from the stream */
105
109
  skipBits(size) {
106
110
  if (size % 8 === 0) {
107
111
  const buf = strings.exactly(this.#readable, size / 8);
package/bits/reader.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import { ISyncReadable } from '../types.js';
2
+ /** a bit reader that reads bits from a byte-aligned stream */
2
3
  export declare class BitReader implements ISyncReadable {
3
4
  #private;
5
+ /** @param readable fuman readable stream */
4
6
  constructor(readable: ISyncReadable);
5
7
  /** Whether the reader is currently aligned on a byte boundary */
6
8
  get isAligned(): boolean;
@@ -9,7 +11,10 @@ export declare class BitReader implements ISyncReadable {
9
11
  /** The current bit position within the last consumed byte */
10
12
  get bitPosition(): number;
11
13
  readSync(bytes: number): Uint8Array;
14
+ /** read a number of bits from the stream, and return them as a number */
12
15
  readBits(size: number): number;
16
+ /** read a number of bits from the stream, and return them as a bigint */
13
17
  readBitsBig(size: number): bigint;
18
+ /** skip a number of bits from the stream */
14
19
  skipBits(size: number): void;
15
20
  }
package/bits/reader.js CHANGED
@@ -4,6 +4,7 @@ class BitReader {
4
4
  #readable;
5
5
  #currentByte = 0;
6
6
  #currentBitIdx = 0;
7
+ /** @param readable fuman readable stream */
7
8
  constructor(readable) {
8
9
  this.#readable = readable;
9
10
  }
@@ -39,6 +40,7 @@ class BitReader {
39
40
  }
40
41
  return result;
41
42
  }
43
+ /** read a number of bits from the stream, and return them as a number */
42
44
  readBits(size) {
43
45
  let result = 0;
44
46
  if (this.#currentBitIdx !== 0) {
@@ -69,6 +71,7 @@ class BitReader {
69
71
  }
70
72
  return result;
71
73
  }
74
+ /** read a number of bits from the stream, and return them as a bigint */
72
75
  readBitsBig(size) {
73
76
  let result = 0n;
74
77
  if (this.#currentBitIdx !== 0) {
@@ -100,6 +103,7 @@ class BitReader {
100
103
  }
101
104
  return result;
102
105
  }
106
+ /** skip a number of bits from the stream */
103
107
  skipBits(size) {
104
108
  if (size % 8 === 0) {
105
109
  const buf = exactly(this.#readable, size / 8);
package/buf-reader.cjs CHANGED
@@ -9,9 +9,6 @@ class BufReader {
9
9
  #readPos = 0;
10
10
  #writePos = 0;
11
11
  #eof = false;
12
- static create(readable, size = DEFAULT_BUF_SIZE) {
13
- return new BufReader(readable, size);
14
- }
15
12
  constructor(readable, size = DEFAULT_BUF_SIZE) {
16
13
  if (size < MIN_BUF_SIZE) {
17
14
  size = MIN_BUF_SIZE;
@@ -19,9 +16,11 @@ class BufReader {
19
16
  this.#buffer = utils.u8.alloc(size);
20
17
  this.#readable = readable;
21
18
  }
19
+ /** the size of the buffer */
22
20
  get bufferSize() {
23
21
  return this.#buffer.byteLength;
24
22
  }
23
+ /** the number of bytes that are currently buffered */
25
24
  get buffered() {
26
25
  return this.#writePos - this.#readPos;
27
26
  }
package/buf-reader.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { IReadable } from './types.js';
2
2
  export declare class BufReader implements IReadable {
3
3
  #private;
4
- static create(readable: IReadable, size?: number): BufReader;
5
4
  constructor(readable: IReadable, size?: number);
5
+ /** the size of the buffer */
6
6
  get bufferSize(): number;
7
+ /** the number of bytes that are currently buffered */
7
8
  get buffered(): number;
8
9
  read(into: Uint8Array): Promise<number>;
9
10
  }
package/buf-reader.js CHANGED
@@ -7,9 +7,6 @@ class BufReader {
7
7
  #readPos = 0;
8
8
  #writePos = 0;
9
9
  #eof = false;
10
- static create(readable, size = DEFAULT_BUF_SIZE) {
11
- return new BufReader(readable, size);
12
- }
13
10
  constructor(readable, size = DEFAULT_BUF_SIZE) {
14
11
  if (size < MIN_BUF_SIZE) {
15
12
  size = MIN_BUF_SIZE;
@@ -17,9 +14,11 @@ class BufReader {
17
14
  this.#buffer = u8.alloc(size);
18
15
  this.#readable = readable;
19
16
  }
17
+ /** the size of the buffer */
20
18
  get bufferSize() {
21
19
  return this.#buffer.byteLength;
22
20
  }
21
+ /** the number of bytes that are currently buffered */
23
22
  get buffered() {
24
23
  return this.#writePos - this.#readPos;
25
24
  }
package/bytes.cjs CHANGED
@@ -3,11 +3,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const utils = require("@fuman/utils");
4
4
  const _utils = require("./_utils.cjs");
5
5
  class Bytes {
6
- /** Underlying buffer */
7
6
  #buffer;
8
- /** Position of the write cursor (should always be >= {@link #readPos}) */
9
7
  #writePos = 0;
10
- /** Position of the read cursor */
11
8
  #readPos = 0;
12
9
  #preferredCapacity;
13
10
  constructor(buf) {
@@ -80,8 +77,14 @@ class Bytes {
80
77
  this.writeSync(bytes.length).set(bytes);
81
78
  this.disposeWriteSync();
82
79
  }
80
+ /**
81
+ * get the "result" of the buffer, i.e. everything that has been written so far,
82
+ * but not yet read
83
+ *
84
+ * **Note**: this method returns a view into the underlying buffer, and does advance the read cursor
85
+ */
83
86
  result() {
84
- return this.#buffer.subarray(0, this.#writePos);
87
+ return this.#buffer.subarray(this.#readPos, this.#writePos);
85
88
  }
86
89
  /** Reclaim memory by only keeping the yet-unread data */
87
90
  reclaim() {
@@ -106,6 +109,7 @@ class Bytes {
106
109
  }
107
110
  this.#readPos -= n;
108
111
  }
112
+ /** reset the read/write cursors */
109
113
  reset() {
110
114
  this.#readPos = 0;
111
115
  this.#writePos = 0;
package/bytes.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { IReadable, ISyncReadable, ISyncWritable, IWritable } from './types.js';
2
+ /** a byte buffer implementing fuman readable/writable streams */
2
3
  export declare class Bytes implements IReadable, IWritable, ISyncReadable, ISyncWritable {
3
4
  #private;
4
5
  constructor(buf: Uint8Array);
@@ -15,10 +16,17 @@ export declare class Bytes implements IReadable, IWritable, ISyncReadable, ISync
15
16
  writeSync(size: number): Uint8Array;
16
17
  disposeWriteSync(written?: number): void;
17
18
  write(bytes: Uint8Array): Promise<void>;
19
+ /**
20
+ * get the "result" of the buffer, i.e. everything that has been written so far,
21
+ * but not yet read
22
+ *
23
+ * **Note**: this method returns a view into the underlying buffer, and does advance the read cursor
24
+ */
18
25
  result(): Uint8Array;
19
26
  /** Reclaim memory by only keeping the yet-unread data */
20
27
  reclaim(): void;
21
28
  /** Mark last n bytes as unread */
22
29
  rewind(n: number): void;
30
+ /** reset the read/write cursors */
23
31
  reset(): void;
24
32
  }
package/bytes.js CHANGED
@@ -1,11 +1,8 @@
1
1
  import { u8 } from "@fuman/utils";
2
2
  import { nextPowerOfTwo } from "./_utils.js";
3
3
  class Bytes {
4
- /** Underlying buffer */
5
4
  #buffer;
6
- /** Position of the write cursor (should always be >= {@link #readPos}) */
7
5
  #writePos = 0;
8
- /** Position of the read cursor */
9
6
  #readPos = 0;
10
7
  #preferredCapacity;
11
8
  constructor(buf) {
@@ -78,8 +75,14 @@ class Bytes {
78
75
  this.writeSync(bytes.length).set(bytes);
79
76
  this.disposeWriteSync();
80
77
  }
78
+ /**
79
+ * get the "result" of the buffer, i.e. everything that has been written so far,
80
+ * but not yet read
81
+ *
82
+ * **Note**: this method returns a view into the underlying buffer, and does advance the read cursor
83
+ */
81
84
  result() {
82
- return this.#buffer.subarray(0, this.#writePos);
85
+ return this.#buffer.subarray(this.#readPos, this.#writePos);
83
86
  }
84
87
  /** Reclaim memory by only keeping the yet-unread data */
85
88
  reclaim() {
@@ -104,6 +107,7 @@ class Bytes {
104
107
  }
105
108
  this.#readPos -= n;
106
109
  }
110
+ /** reset the read/write cursors */
107
111
  reset() {
108
112
  this.#readPos = 0;
109
113
  this.#writePos = 0;
@@ -2,6 +2,10 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const utils = require("@fuman/utils");
4
4
  class DelimiterCodec {
5
+ /**
6
+ * @param delimiter delimiter to use
7
+ * @param options options
8
+ */
5
9
  constructor(delimiter, options) {
6
10
  this.delimiter = delimiter;
7
11
  this.options = options;
@@ -1,6 +1,7 @@
1
1
  import { Bytes } from '../bytes.js';
2
2
  import { ISyncWritable } from '../types.js';
3
3
  import { IFrameDecoder, IFrameEncoder } from './types.js';
4
+ /** options for {@link DelimiterCodec} */
4
5
  export interface DelimiterCodecOptions {
5
6
  /**
6
7
  * Strategy for handling delimiter.
@@ -8,13 +9,20 @@ export interface DelimiterCodecOptions {
8
9
  * - `discard` - delimiter is discarded
9
10
  *
10
11
  * Ignored for encoding (delimiter is always appended after the frame)
12
+ *
13
+ * @default 'discard'
11
14
  */
12
15
  strategy?: 'keep' | 'discard';
13
16
  }
17
+ /** a simple frame codec that uses a delimiter to separate frames */
14
18
  export declare class DelimiterCodec implements IFrameDecoder, IFrameEncoder {
15
19
  #private;
16
20
  readonly delimiter: Uint8Array;
17
21
  readonly options?: DelimiterCodecOptions | undefined;
22
+ /**
23
+ * @param delimiter delimiter to use
24
+ * @param options options
25
+ */
18
26
  constructor(delimiter: Uint8Array, options?: DelimiterCodecOptions | undefined);
19
27
  decode(buf: Bytes, eof: boolean): Uint8Array | null;
20
28
  encode(data: Uint8Array, into: ISyncWritable): void;
@@ -1,5 +1,9 @@
1
1
  import { typed } from "@fuman/utils";
2
2
  class DelimiterCodec {
3
+ /**
4
+ * @param delimiter delimiter to use
5
+ * @param options options
6
+ */
3
7
  constructor(delimiter, options) {
4
8
  this.delimiter = delimiter;
5
9
  this.options = options;
@@ -1,10 +1,22 @@
1
1
  import { Bytes } from '../bytes.js';
2
2
  import { ISyncWritable } from '../types.js';
3
3
  import { IFrameDecoder, IFrameEncoder } from './types.js';
4
+ /** options for {@link LengthDelimitedCodec} */
4
5
  export interface LengthDelimitedCodecOptions {
6
+ /**
7
+ * function that will be called to read the length of the frame
8
+ *
9
+ * @example `read.uint32le`
10
+ */
5
11
  read?: (r: Bytes) => number | null;
12
+ /**
13
+ * function that will be called to write the length of the frame
14
+ *
15
+ * @example `write.uint32le`
16
+ */
6
17
  write?: (w: ISyncWritable, n: number) => void;
7
18
  }
19
+ /** a simple frame codec that uses a length prefix to separate frames */
8
20
  export declare class LengthDelimitedCodec implements IFrameDecoder, IFrameEncoder {
9
21
  #private;
10
22
  constructor(options: LengthDelimitedCodecOptions);
package/codec/reader.cjs CHANGED
@@ -8,12 +8,18 @@ class FramedReader {
8
8
  #readChunkSize;
9
9
  #eof = false;
10
10
  #canDecode = false;
11
+ /**
12
+ * @param readable fuman readable stream to read from
13
+ * @param decoder frame decoder
14
+ * @param options extra options
15
+ */
11
16
  constructor(readable, decoder, options) {
12
17
  this.#readable = readable;
13
18
  this.#decoder = decoder;
14
19
  this.#buffer = bytes.Bytes.alloc(options?.initialBufferSize ?? 1024 * 16);
15
20
  this.#readChunkSize = options?.readChunkSize ?? 1024 * 16;
16
21
  }
22
+ /** read a next frame from the stream, or `null` if the stream ended */
17
23
  async read() {
18
24
  while (true) {
19
25
  if (this.#canDecode) {
@@ -36,6 +42,7 @@ class FramedReader {
36
42
  }
37
43
  }
38
44
  }
45
+ /** create an async iterator that yields frames */
39
46
  [Symbol.asyncIterator]() {
40
47
  return {
41
48
  next: async () => {
package/codec/reader.d.ts CHANGED
@@ -1,12 +1,21 @@
1
1
  import { IReadable } from '../types.js';
2
2
  import { IFrameDecoder } from './types.js';
3
+ /** options for {@link FramedReader} */
3
4
  export interface FramedReaderOptions {
4
5
  initialBufferSize?: number;
5
6
  readChunkSize?: number;
6
7
  }
8
+ /** a reader that decodes frames one by one from a readable stream */
7
9
  export declare class FramedReader<Frame> {
8
10
  #private;
11
+ /**
12
+ * @param readable fuman readable stream to read from
13
+ * @param decoder frame decoder
14
+ * @param options extra options
15
+ */
9
16
  constructor(readable: IReadable, decoder: IFrameDecoder<Frame>, options?: FramedReaderOptions);
17
+ /** read a next frame from the stream, or `null` if the stream ended */
10
18
  read(): Promise<Frame | null>;
19
+ /** create an async iterator that yields frames */
11
20
  [Symbol.asyncIterator](): AsyncIterator<Frame>;
12
21
  }
package/codec/reader.js CHANGED
@@ -6,12 +6,18 @@ class FramedReader {
6
6
  #readChunkSize;
7
7
  #eof = false;
8
8
  #canDecode = false;
9
+ /**
10
+ * @param readable fuman readable stream to read from
11
+ * @param decoder frame decoder
12
+ * @param options extra options
13
+ */
9
14
  constructor(readable, decoder, options) {
10
15
  this.#readable = readable;
11
16
  this.#decoder = decoder;
12
17
  this.#buffer = Bytes.alloc(options?.initialBufferSize ?? 1024 * 16);
13
18
  this.#readChunkSize = options?.readChunkSize ?? 1024 * 16;
14
19
  }
20
+ /** read a next frame from the stream, or `null` if the stream ended */
15
21
  async read() {
16
22
  while (true) {
17
23
  if (this.#canDecode) {
@@ -34,6 +40,7 @@ class FramedReader {
34
40
  }
35
41
  }
36
42
  }
43
+ /** create an async iterator that yields frames */
37
44
  [Symbol.asyncIterator]() {
38
45
  return {
39
46
  next: async () => {
@@ -2,6 +2,7 @@ import { Bytes } from '../bytes.js';
2
2
  import { ISyncWritable } from '../types.js';
3
3
  import { DelimiterCodecOptions } from './delimiter.js';
4
4
  import { IFrameDecoder, IFrameEncoder } from './types.js';
5
+ /** wrapper over {@link DelimiterCodec} that handles text frames */
5
6
  export declare class TextDelimiterCodec implements IFrameDecoder<string>, IFrameEncoder<string> {
6
7
  #private;
7
8
  constructor(delimiter: Uint8Array | string, options?: DelimiterCodecOptions);
package/codec/types.d.ts CHANGED
@@ -5,12 +5,14 @@ export interface IFrameDecoder<Frame = Uint8Array> {
5
5
  /**
6
6
  * Decode a frame from a buffer
7
7
  *
8
- * > **Important**: When returning byte arrays, make sure that the returned array is **not**
8
+ * > **Important implementation notice**: When returning byte arrays, make sure that the returned array is **not**
9
9
  * > a view into the original buffer, as the underlying buffer may get invalidated
10
10
  */
11
11
  decode: (buf: Bytes, eof: boolean) => MaybePromise<Frame | null>;
12
12
  }
13
13
  export interface IFrameEncoder<Frame = Uint8Array> {
14
+ /** Encode a frame into a writable stream */
14
15
  encode: (frame: Frame, into: ISyncWritable) => MaybePromise<void>;
16
+ /** Reset the encoder, should it have any internal state */
15
17
  reset: () => void;
16
18
  }
package/codec/writer.cjs CHANGED
@@ -6,12 +6,18 @@ class FramedWriter {
6
6
  #encoder;
7
7
  #buffer;
8
8
  #highWaterMark;
9
+ /**
10
+ * @param writable fuman writable stream to write to
11
+ * @param encoder frame encoder
12
+ * @param options extra options
13
+ */
9
14
  constructor(writable, encoder, options) {
10
15
  this.#writable = writable;
11
16
  this.#encoder = encoder;
12
17
  this.#highWaterMark = options?.initialBufferSize ?? 1024;
13
18
  this.#buffer = bytes.Bytes.alloc(this.#highWaterMark);
14
19
  }
20
+ /** write a frame to the stream */
15
21
  async write(frame) {
16
22
  await this.#encoder.encode(frame, this.#buffer);
17
23
  const buffer = this.#buffer.result();
package/codec/writer.d.ts CHANGED
@@ -1,10 +1,18 @@
1
1
  import { IWritable } from '../types.js';
2
2
  import { IFrameEncoder } from './types.js';
3
+ /** options for {@link FramedWriter} */
3
4
  export interface FramedWriterOptions {
4
5
  initialBufferSize?: number;
5
6
  }
7
+ /** a writer that encodes frames one by one into a writable stream */
6
8
  export declare class FramedWriter<Frame = Uint8Array> {
7
9
  #private;
10
+ /**
11
+ * @param writable fuman writable stream to write to
12
+ * @param encoder frame encoder
13
+ * @param options extra options
14
+ */
8
15
  constructor(writable: IWritable, encoder: IFrameEncoder<Frame>, options?: FramedWriterOptions);
16
+ /** write a frame to the stream */
9
17
  write(frame: Frame): Promise<void>;
10
18
  }
package/codec/writer.js CHANGED
@@ -4,12 +4,18 @@ class FramedWriter {
4
4
  #encoder;
5
5
  #buffer;
6
6
  #highWaterMark;
7
+ /**
8
+ * @param writable fuman writable stream to write to
9
+ * @param encoder frame encoder
10
+ * @param options extra options
11
+ */
7
12
  constructor(writable, encoder, options) {
8
13
  this.#writable = writable;
9
14
  this.#encoder = encoder;
10
15
  this.#highWaterMark = options?.initialBufferSize ?? 1024;
11
16
  this.#buffer = Bytes.alloc(this.#highWaterMark);
12
17
  }
18
+ /** write a frame to the stream */
13
19
  async write(frame) {
14
20
  await this.#encoder.encode(frame, this.#buffer);
15
21
  const buffer = this.#buffer.result();
package/errors.d.ts CHANGED
@@ -4,6 +4,9 @@
4
4
  * The part that was read is available in the `part` property.
5
5
  */
6
6
  export declare class PartialReadError extends RangeError {
7
+ /** the part that was read */
7
8
  readonly part: Uint8Array;
8
- constructor(part: Uint8Array, expectedLength: number);
9
+ constructor(
10
+ /** the part that was read */
11
+ part: Uint8Array, expectedLength: number);
9
12
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@fuman/io",
3
3
  "type": "module",
4
- "version": "0.0.2",
4
+ "version": "0.0.3",
5
5
  "description": "experimental i/o abstractions",
6
6
  "license": "MIT",
7
7
  "scripts": {},
8
8
  "dependencies": {
9
- "@fuman/utils": "^0.0.1"
9
+ "@fuman/utils": "^0.0.3"
10
10
  },
11
11
  "exports": {
12
12
  ".": {
@@ -1,4 +1,7 @@
1
1
  import { IClosable, IReadable, ISyncReadable } from '../types.js';
2
+ /** create an async readable stream from a sync readable stream */
2
3
  export declare function fumanSyncReadableToAsync(readable: ISyncReadable): IReadable;
4
+ /** convert a web ReadableStream to a fuman readable stream */
3
5
  export declare function webReadableToFuman(readable: ReadableStream<Uint8Array>): IReadable & IClosable;
6
+ /** convert a fuman readable stream to a web ReadableStream */
4
7
  export declare function fumanReadableToWeb(readable: IReadable): ReadableStream<Uint8Array>;
@@ -1,3 +1,18 @@
1
1
  import { IReadable } from '../../types.js';
2
+ /**
3
+ * read exactly N bytes from the source
4
+ *
5
+ * @param readable fuman readable stream
6
+ * @param length length of the buffer to read, or a buffer to read into (when a number is passed, a new buffer will be allocated)
7
+ * @param onEof what to do when the end of the stream is reached
8
+ * - `error` - throw an {@link PartialReadError}
9
+ * - `truncate` - truncate the buffer to the number of bytes that were read. note that this might return 0 bytes
10
+ */
2
11
  export declare function exactly(readable: IReadable, length: number | Uint8Array, onEof?: 'error' | 'truncate'): Promise<Uint8Array>;
12
+ /**
13
+ * read the source until it ends, and return the buffer
14
+ *
15
+ * @param readable fuman readable stream
16
+ * @param chunkSize size of the chunks to read
17
+ */
3
18
  export declare function untilEnd(readable: IReadable, chunkSize?: number): Promise<Uint8Array>;
package/read/numbers.d.ts CHANGED
@@ -1,27 +1,53 @@
1
1
  import { ISyncReadable } from '../types.js';
2
+ /** read a uint8 from the source */
2
3
  export declare function uint8(readable: ISyncReadable): number;
4
+ /** read an int8 from the source (fuman readable stream or a buffer) */
3
5
  export declare function int8(readable: ISyncReadable | Uint8Array): number;
6
+ /** read a uint16 (little endian) from the source (fuman readable stream or a buffer) */
4
7
  export declare function uint16le(readable: ISyncReadable | Uint8Array): number;
8
+ /** read a uint16 (big endian) from the source (fuman readable stream or a buffer) */
5
9
  export declare function uint16be(readable: ISyncReadable | Uint8Array): number;
10
+ /** read a uint24 (little endian) from the source (fuman readable stream or a buffer) */
6
11
  export declare function uint24le(readable: ISyncReadable | Uint8Array): number;
12
+ /** read a uint24 (big endian) from the source (fuman readable stream or a buffer) */
7
13
  export declare function uint24be(readable: ISyncReadable | Uint8Array): number;
14
+ /** read a uint32 (little endian) from the source (fuman readable stream or a buffer) */
8
15
  export declare function uint32le(readable: ISyncReadable | Uint8Array): number;
16
+ /** read a uint32 (big endian) from the source (fuman readable stream or a buffer) */
9
17
  export declare function uint32be(readable: ISyncReadable | Uint8Array): number;
18
+ /** read a uint64 (little endian) from the source (fuman readable stream or a buffer) */
10
19
  export declare function uint64le(readable: ISyncReadable | Uint8Array): bigint;
20
+ /** read a uint64 (big endian) from the source (fuman readable stream or a buffer) */
11
21
  export declare function uint64be(readable: ISyncReadable | Uint8Array): bigint;
22
+ /** read an int16 (little endian) from the source (fuman readable stream or a buffer) */
12
23
  export declare function int16le(readable: ISyncReadable | Uint8Array): number;
24
+ /** read an int16 (big endian) from the source (fuman readable stream or a buffer) */
13
25
  export declare function int16be(readable: ISyncReadable | Uint8Array): number;
26
+ /** read an int24 (little endian) from the source (fuman readable stream or a buffer) */
14
27
  export declare function int24le(readable: ISyncReadable | Uint8Array): number;
28
+ /** read an int24 (big endian) from the source (fuman readable stream or a buffer) */
15
29
  export declare function int24be(readable: ISyncReadable | Uint8Array): number;
30
+ /** read an int32 (little endian) from the source (fuman readable stream or a buffer) */
16
31
  export declare function int32le(readable: ISyncReadable | Uint8Array): number;
32
+ /** read an int32 (big endian) from the source (fuman readable stream or a buffer) */
17
33
  export declare function int32be(readable: ISyncReadable | Uint8Array): number;
34
+ /** read an int64 (little endian) from the source (fuman readable stream or a buffer) */
18
35
  export declare function int64le(readable: ISyncReadable | Uint8Array): bigint;
36
+ /** read an int64 (big endian) from the source (fuman readable stream or a buffer) */
19
37
  export declare function int64be(readable: ISyncReadable | Uint8Array): bigint;
38
+ /** read a variable-size uint (little endian) from the source (fuman readable stream or a buffer) */
20
39
  export declare function uintle(readable: ISyncReadable | Uint8Array, size: number): bigint;
40
+ /** read a variable-size uint (big endian) from the source (fuman readable stream or a buffer) */
21
41
  export declare function uintbe(readable: ISyncReadable | Uint8Array, size: number): bigint;
42
+ /** read a variable-size int (big endian) from the source (fuman readable stream or a buffer) */
22
43
  export declare function intbe(readable: ISyncReadable | Uint8Array, size: number): bigint;
44
+ /** read a variable-size int (little endian) from the source (fuman readable stream or a buffer) */
23
45
  export declare function intle(readable: ISyncReadable | Uint8Array, size: number): bigint;
46
+ /** read a float32 (little endian) from the source (fuman readable stream or a buffer) */
24
47
  export declare function float32le(readable: ISyncReadable | Uint8Array): number;
48
+ /** read a float32 (big endian) from the source (fuman readable stream or a buffer) */
25
49
  export declare function float32be(readable: ISyncReadable | Uint8Array): number;
50
+ /** read a float64 (little endian) from the source (fuman readable stream or a buffer) */
26
51
  export declare function float64le(readable: ISyncReadable | Uint8Array): number;
52
+ /** read a float64 (big endian) from the source (fuman readable stream or a buffer) */
27
53
  export declare function float64be(readable: ISyncReadable | Uint8Array): number;
@@ -17,6 +17,7 @@ class ReaderWithFinal {
17
17
  this.#buf1 = this.#buf2;
18
18
  this.#buf2 = tmp;
19
19
  }
20
+ /** read a chunk of data, and whether it is the last one */
20
21
  async readWithFinal(into) {
21
22
  if (this.#ended) {
22
23
  return {
@@ -1,13 +1,21 @@
1
1
  import { IReadable } from './types.js';
2
+ /** result of {@link ReaderWithFinal#readWithFinal} */
2
3
  export interface ReaderWithFinalResult {
4
+ /** number of bytes read */
3
5
  readonly nread: number;
6
+ /** whether this was the last chunk */
4
7
  readonly final: boolean;
5
8
  }
9
+ /**
10
+ * a reader that reads one read ahead, allowing the caller to know
11
+ * whether the chunk being read is the last one
12
+ */
6
13
  export declare class ReaderWithFinal implements IReadable {
7
14
  #private;
8
15
  constructor(readable: IReadable, params?: {
9
16
  internalBufferSize?: number;
10
17
  });
18
+ /** read a chunk of data, and whether it is the last one */
11
19
  readWithFinal(into: Uint8Array): Promise<ReaderWithFinalResult>;
12
20
  read(into: Uint8Array): Promise<number>;
13
21
  }
@@ -15,6 +15,7 @@ class ReaderWithFinal {
15
15
  this.#buf1 = this.#buf2;
16
16
  this.#buf2 = tmp;
17
17
  }
18
+ /** read a chunk of data, and whether it is the last one */
18
19
  async readWithFinal(into) {
19
20
  if (this.#ended) {
20
21
  return {
package/types.d.ts CHANGED
@@ -1,6 +1,4 @@
1
- /**
2
- * An interface for reading bytes synchronously from somewhere.
3
- */
1
+ /** A synchronous readable stream */
4
2
  export interface ISyncReadable {
5
3
  /**
6
4
  * Read the specified number of bytes from the source
@@ -17,6 +15,7 @@ export interface ISyncReadable {
17
15
  */
18
16
  readSync: (bytes: number) => Uint8Array;
19
17
  }
18
+ /** A readable stream */
20
19
  export interface IReadable {
21
20
  /**
22
21
  * Read data from the underlying source into the provided Uint8Array,
@@ -31,12 +30,14 @@ export interface IReadable {
31
30
  */
32
31
  read: (into: Uint8Array) => Promise<number>;
33
32
  }
33
+ /** Something that can be closed */
34
34
  export interface IClosable {
35
35
  /**
36
36
  * Close the underlying source.
37
37
  */
38
38
  close: () => void;
39
39
  }
40
+ /** A synchronous writable stream */
40
41
  export interface ISyncWritable {
41
42
  /**
42
43
  * Write the specified number of bytes to the underlying source.
@@ -61,6 +62,8 @@ export interface ISyncWritable {
61
62
  */
62
63
  disposeWriteSync: (written?: number) => void;
63
64
  }
65
+ /** A writable stream */
64
66
  export interface IWritable {
67
+ /** Write bytes to the underlying stream, resolving once the write is complete */
65
68
  write: (bytes: Uint8Array) => Promise<void>;
66
69
  }
@@ -1,27 +1,53 @@
1
1
  import { ISyncWritable } from '../types.js';
2
+ /** write a uint8 to the target (fuman writable stream or a buffer) */
2
3
  export declare function uint8(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
4
+ /** write an int8 to the target (fuman writable stream or a buffer) */
3
5
  export declare function int8(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
6
+ /** write a uint16 (little endian) to the target (fuman writable stream or a buffer) */
4
7
  export declare function uint16le(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
8
+ /** write a uint16 (big endian) to the target (fuman writable stream or a buffer) */
5
9
  export declare function uint16be(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
10
+ /** write an int16 (little endian) to the target (fuman writable stream or a buffer) */
6
11
  export declare function int16le(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
12
+ /** write an int16 (big endian) to the target (fuman writable stream or a buffer) */
7
13
  export declare function int16be(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
14
+ /** write a uint24 (little endian) to the target (fuman writable stream or a buffer) */
8
15
  export declare function uint24le(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
16
+ /** write a uint24 (big endian) to the target (fuman writable stream or a buffer) */
9
17
  export declare function uint24be(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
18
+ /** write an int24 (little endian) to the target (fuman writable stream or a buffer) */
10
19
  export declare function int24le(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
20
+ /** write an int24 (big endian) to the target (fuman writable stream or a buffer) */
11
21
  export declare function int24be(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
22
+ /** write a uint32 (little endian) to the target (fuman writable stream or a buffer) */
12
23
  export declare function uint32le(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
24
+ /** write a uint32 (big endian) to the target (fuman writable stream or a buffer) */
13
25
  export declare function uint32be(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
26
+ /** write an int32 (little endian) to the target (fuman writable stream or a buffer) */
14
27
  export declare function int32le(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
28
+ /** write an int32 (big endian) to the target (fuman writable stream or a buffer) */
15
29
  export declare function int32be(writable: ISyncWritable | Uint8Array, value: number, noAssert?: boolean): void;
30
+ /** write a uint64 (little endian) to the target (fuman writable stream or a buffer) */
16
31
  export declare function uint64le(writable: ISyncWritable | Uint8Array, value: bigint, noAssert?: boolean): void;
32
+ /** write a uint64 (big endian) to the target (fuman writable stream or a buffer) */
17
33
  export declare function uint64be(writable: ISyncWritable | Uint8Array, value: bigint, noAssert?: boolean): void;
34
+ /** write an int64 (little endian) to the target (fuman writable stream or a buffer) */
18
35
  export declare function int64le(writable: ISyncWritable | Uint8Array, value: bigint, noAssert?: boolean): void;
36
+ /** write an int64 (big endian) to the target (fuman writable stream or a buffer) */
19
37
  export declare function int64be(writable: ISyncWritable | Uint8Array, value: bigint, noAssert?: boolean): void;
38
+ /** write a variable-size uint (little endian) to the target (fuman writable stream or a buffer) */
20
39
  export declare function uintle(writable: ISyncWritable | Uint8Array, size: number, value: bigint, noAssert?: boolean): void;
40
+ /** write a variable-size uint (big endian) to the target (fuman writable stream or a buffer) */
21
41
  export declare function uintbe(writable: ISyncWritable | Uint8Array, size: number, value: bigint, noAssert?: boolean): void;
42
+ /** write a variable-size int (little endian) to the target (fuman writable stream or a buffer) */
22
43
  export declare function intle(writable: ISyncWritable | Uint8Array, size: number, value: bigint, noAssert?: boolean): void;
44
+ /** write a variable-size int (big endian) to the target (fuman writable stream or a buffer) */
23
45
  export declare function intbe(writable: ISyncWritable | Uint8Array, size: number, value: bigint, noAssert?: boolean): void;
46
+ /** write a float32 (little endian) to the target (fuman writable stream or a buffer) */
24
47
  export declare function float32le(writable: ISyncWritable | Uint8Array, value: number): void;
48
+ /** write a float32 (big endian) to the target (fuman writable stream or a buffer) */
25
49
  export declare function float32be(writable: ISyncWritable | Uint8Array, value: number): void;
50
+ /** write a float64 (little endian) to the target (fuman writable stream or a buffer) */
26
51
  export declare function float64le(writable: ISyncWritable | Uint8Array, value: number): void;
52
+ /** write a float64 (big endian) to the target (fuman writable stream or a buffer) */
27
53
  export declare function float64be(writable: ISyncWritable | Uint8Array, value: number): void;
package/write/pipe.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  import { IReadable, IWritable } from '../types.js';
2
+ /** pipe the contents of a readable stream (until it ends) into a writable stream */
2
3
  export declare function pipe(into: IWritable, readable: IReadable): Promise<void>;
@@ -1,6 +1,11 @@
1
1
  import { ISyncWritable } from '../types.js';
2
+ /** write a buffer to the stream */
2
3
  export declare function bytes(writable: ISyncWritable, bytes: Uint8Array): void;
4
+ /** write a buffer to the stream, but in reverse order */
3
5
  export declare function bytesReversed(writable: ISyncWritable, bytes: Uint8Array): void;
6
+ /** write a raw string to the stream (`.charCodeAt` is used to get the codepoints) */
4
7
  export declare function rawString(writable: ISyncWritable, str: string): void;
8
+ /** write a utf8-encoded string to the stream */
5
9
  export declare function utf8String(writable: ISyncWritable, str: string): void;
10
+ /** write a utf8-encoded string to the stream, with a null terminator */
6
11
  export declare function cUtf8String(writable: ISyncWritable, str: string): void;