Skip to content
2 min read · 391 words

Class: OpfsSpoolReader

Reads an OPFS-backed file as a @nhtio/adk!SpoolReader.

Remarks

Constructor is not async — but the first method call awaits a private readiness promise that fetches the underlying File (and in eager mode, its contents). Subsequent calls reuse the cached state. This keeps construction call sites synchronous while still doing real I/O lazily.

All four SpoolReader methods on this reader return promises. The SpoolReader contract supports both sync and async return; consumers of SpooledArtifact handle either.

Implements

Constructors

Constructor

ts
new OpfsSpoolReader(handle: OpfsFileHandle, opts?: OpfsSpoolReaderOptions): OpfsSpoolReader;

Parameters

ParameterType
handleOpfsFileHandle
optsOpfsSpoolReaderOptions

Returns

OpfsSpoolReader

Methods

byteLength()

ts
byteLength(): Promise<number>;

Returns the total number of bytes in the underlying data.

Returns

Promise<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): Promise<string | undefined>;

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

Parameters

ParameterTypeDescription
indexnumber0-based line index.

Returns

Promise<string | undefined>

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

Implementation of

SpoolReader.line


lineCount()

ts
lineCount(): Promise<number>;

Returns the total number of lines in the underlying data.

Returns

Promise<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(): Promise<string>;

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

Returns

Promise<string>

Remarks

In eager mode the content is already cached at first-call load and this method is effectively a property access. In streaming mode there is no cache: the file is re-read (as a single File.text() call) on every invocation. Use SpooledArtifact.asString() judiciously on large streaming-mode artifacts.

Implementation of

SpoolReader.readAll


isOpfsSpoolReader()

ts
static isOpfsSpoolReader(value: unknown): value is OpfsSpoolReader;

Returns true if value is an OpfsSpoolReader instance.

Parameters

ParameterTypeDescription
valueunknownThe value to test.

Returns

value is OpfsSpoolReader

true when value is an OpfsSpoolReader instance.

Remarks

Uses @nhtio/adk!isInstanceOf for cross-realm safety.