Skip to content
4 min read · 861 words

Class: InMemorySpoolStore

Defined in: batteries/storage/in_memory/index.ts:134

In-memory "give bytes, get a reader" persistence layer keyed by callId.

Remarks

Stores each value byte-faithfully as a Uint8Array. string inputs are encoded as UTF-8; Uint8Array inputs are held verbatim (no lossy text round-trip, so binary payloads survive intact); ReadableStream<Uint8Array> inputs are drained fully into a buffer — in-memory storage cannot stream to disk, so the stream form resolves asynchronously and is the documented trade-off for this battery.

Each write() and each read() returns a fresh InMemorySpoolReader — the store owns the bytes, the reader is a view. Mutating the store after handing out a reader does not invalidate the reader.

Implements @nhtio/adk!SpoolStore (i.e. ByteStore<SpoolReader>).

Example

ts
const store = new InMemorySpoolStore();
const bytes = await tool.executor(ctx)(args);
const reader = await store.write(callId, bytes);
const Ctor = tool.artifactConstructor?.() ?? SpooledArtifact;
const artifact = new Ctor(reader);

Implements

Constructors

Constructor

ts
new InMemorySpoolStore(): InMemorySpoolStore;

Returns

InMemorySpoolStore

Accessors

size

Get Signature

ts
get size(): number;

Defined in: batteries/storage/in_memory/index.ts:210

Returns the number of entries currently in the store.

Returns

number

Methods

clear()

ts
clear(): void;

Defined in: batteries/storage/in_memory/index.ts:203

Removes every entry from the store.

Returns

void

Remarks

Existing readers handed out by prior write() / read() calls remain valid — they hold their own snapshot.


delete()

ts
delete(callId: string): boolean;

Defined in: batteries/storage/in_memory/index.ts:192

Removes the entry under callId.

Parameters

ParameterTypeDescription
callIdstringIdentifier whose entry should be removed.

Returns

boolean

true if an entry existed and was removed; false otherwise.

Implementation of

ts
SpoolStore.delete;

read()

ts
read(callId: string): InMemorySpoolReader | undefined;

Defined in: batteries/storage/in_memory/index.ts:180

Returns a reader over the bytes previously written under callId, or undefined if the entry has not been written or has been deleted.

Parameters

ParameterTypeDescription
callIdstringIdentifier supplied to a prior InMemorySpoolStore.write call.

Returns

InMemorySpoolReader | undefined

A fresh InMemorySpoolReader bound to the stored bytes, or undefined.

Implementation of

ts
SpoolStore.read;

write()

Call Signature

ts
write(callId: string, bytes: string): InMemorySpoolReader;

Defined in: batteries/storage/in_memory/index.ts:151

Persists bytes under callId and returns a reader over them.

Parameters
ParameterTypeDescription
callIdstringIdentifier used to retrieve the bytes via InMemorySpoolStore.read.
bytesstringThe bytes to store, as a string, Uint8Array, or ReadableStream<Uint8Array>.
Returns

InMemorySpoolReader

A fresh InMemorySpoolReader bound to the stored bytes — a Promise for stream input, synchronous otherwise.

Remarks

string input is encoded as UTF-8; Uint8Array is stored byte-faithfully; ReadableStream<Uint8Array> is drained fully (and write returns a Promise). Re-writing the same callId replaces the prior entry; readers handed out before the rewrite continue to view the old bytes (they hold their own snapshot via the InMemorySpoolReader constructor).

Implementation of
ts
SpoolStore.write;

Call Signature

ts
write(callId: string, bytes: Uint8Array): InMemorySpoolReader;

Defined in: batteries/storage/in_memory/index.ts:152

Persists bytes under callId and returns a reader over them.

Parameters
ParameterTypeDescription
callIdstringIdentifier used to retrieve the bytes via InMemorySpoolStore.read.
bytesUint8ArrayThe bytes to store, as a string, Uint8Array, or ReadableStream<Uint8Array>.
Returns

InMemorySpoolReader

A fresh InMemorySpoolReader bound to the stored bytes — a Promise for stream input, synchronous otherwise.

Remarks

string input is encoded as UTF-8; Uint8Array is stored byte-faithfully; ReadableStream<Uint8Array> is drained fully (and write returns a Promise). Re-writing the same callId replaces the prior entry; readers handed out before the rewrite continue to view the old bytes (they hold their own snapshot via the InMemorySpoolReader constructor).

Implementation of
ts
SpoolStore.write;

Call Signature

ts
write(callId: string, bytes: ReadableStream<Uint8Array<ArrayBufferLike>>): Promise<InMemorySpoolReader>;

Defined in: batteries/storage/in_memory/index.ts:153

Persists bytes under callId and returns a reader over them.

Parameters
ParameterTypeDescription
callIdstringIdentifier used to retrieve the bytes via InMemorySpoolStore.read.
bytesReadableStream<Uint8Array<ArrayBufferLike>>The bytes to store, as a string, Uint8Array, or ReadableStream<Uint8Array>.
Returns

Promise<InMemorySpoolReader>

A fresh InMemorySpoolReader bound to the stored bytes — a Promise for stream input, synchronous otherwise.

Remarks

string input is encoded as UTF-8; Uint8Array is stored byte-faithfully; ReadableStream<Uint8Array> is drained fully (and write returns a Promise). Re-writing the same callId replaces the prior entry; readers handed out before the rewrite continue to view the old bytes (they hold their own snapshot via the InMemorySpoolReader constructor).

Implementation of
ts
SpoolStore.write;

Call Signature

ts
write(callId: string, bytes:
  | string
  | Uint8Array<ArrayBufferLike>
  | ReadableStream<Uint8Array<ArrayBufferLike>>):
  | InMemorySpoolReader
| Promise<InMemorySpoolReader>;

Defined in: batteries/storage/in_memory/index.ts:154

Persists bytes under callId and returns a reader over them.

Parameters
ParameterTypeDescription
callIdstringIdentifier used to retrieve the bytes via InMemorySpoolStore.read.
bytes| string | Uint8Array<ArrayBufferLike> | ReadableStream<Uint8Array<ArrayBufferLike>>The bytes to store, as a string, Uint8Array, or ReadableStream<Uint8Array>.
Returns

| InMemorySpoolReader | Promise<InMemorySpoolReader>

A fresh InMemorySpoolReader bound to the stored bytes — a Promise for stream input, synchronous otherwise.

Remarks

string input is encoded as UTF-8; Uint8Array is stored byte-faithfully; ReadableStream<Uint8Array> is drained fully (and write returns a Promise). Re-writing the same callId replaces the prior entry; readers handed out before the rewrite continue to view the old bytes (they hold their own snapshot via the InMemorySpoolReader constructor).

Implementation of
ts
SpoolStore.write;