Skip to content
2 min read · 377 words

Class: InMemorySpoolReader

Sync in-memory @nhtio/adk!SpoolReader over a string body.

Remarks

Splits the supplied content on \n at construction time and caches the resulting line array plus the UTF-8 byte length. All three SpoolReader methods resolve synchronously from the cache — no I/O happens after construction.

Empty input yields a reader with lineCount() === 0 and byteLength() === 0. A trailing newline produces a final empty line: "a\nb\n".split('\n') === ['a', 'b', '']. This matches the JavaScript String.prototype.split contract and lets a lineCount() consumer distinguish "two lines, no trailing newline" from "two lines, trailing newline".

Implements

Constructors

Constructor

ts
new InMemorySpoolReader(content: string): InMemorySpoolReader;

Parameters

ParameterType
contentstring

Returns

InMemorySpoolReader

Methods

byteLength()

ts
byteLength(): number;

Returns the total number of bytes in the underlying data.

Returns

number

The byte length of the underlying data.

Remarks

Used for reporting and token-estimation purposes. Byte length is distinct from character length for multi-byte encodings.

Implementation of

SpoolReader.byteLength


line()

ts
line(index: number): string | undefined;

Returns the line at the given 0-based index, or undefined when out of range.

Parameters

ParameterTypeDescription
indexnumber0-based line index.

Returns

string | undefined

The raw line string (without trailing newline), or undefined.

Implementation of

SpoolReader.line


lineCount()

ts
lineCount(): number;

Returns the total number of lines in the underlying data.

Returns

number

The total line count.

Remarks

Required so consumers know when to stop iterating; the line count must remain stable for the lifetime of the reader.

Implementation of

SpoolReader.lineCount


readAll()

ts
readAll(): string;

Returns the full underlying content as a single decoded string, byte-faithful to the source.

Returns

string

The full underlying content as a single string.

Remarks

Unlike SpoolReader.line, this method preserves trailing newlines and any non-\n line terminators (e.g. \r\n) present in the original bytes. It is the primitive that powers SpooledArtifact.asString() — the round-trip-faithful alternative to assembling the artifact body from per-line reads.

Implementations should make this O(n) in the size of the underlying data and may cache the result if the read source is durable. Streaming implementations may choose not to cache.

Implementation of

SpoolReader.readAll