MCPcopy Create free account
hub / github.com/WinterTC55/iter-streams / MemoryWriter

Class MemoryWriter

samples/util.ts:29–90  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27
28// Simple in-memory writer for testing
29export class MemoryWriter implements Writer {
30 private chunks: Uint8Array[] = [];
31 private _closed = false;
32 private _byteCount = 0;
33 private enc = new TextEncoder();
34
35 get desiredSize(): number | null {
36 return this._closed ? null : 100;
37 }
38
39 async write(chunk: Uint8Array | string, _options?: WriteOptions): Promise<void> {
40 if (this._closed) throw new Error('Writer is closed');
41 const bytes = typeof chunk === 'string' ? this.enc.encode(chunk) : chunk;
42 this.chunks.push(bytes);
43 this._byteCount += bytes.byteLength;
44 }
45
46 async writev(chunks: (Uint8Array | string)[], _options?: WriteOptions): Promise<void> {
47 for (const chunk of chunks) {
48 await this.write(chunk);
49 }
50 }
51
52 writeSync(chunk: Uint8Array | string): boolean {
53 if (this._closed) return false;
54 const bytes = typeof chunk === 'string' ? this.enc.encode(chunk) : chunk;
55 this.chunks.push(bytes);
56 this._byteCount += bytes.byteLength;
57 return true;
58 }
59
60 writevSync(chunks: (Uint8Array | string)[]): boolean {
61 for (const chunk of chunks) {
62 if (!this.writeSync(chunk)) return false;
63 }
64 return true;
65 }
66
67 async end(_options?: WriteOptions): Promise<number> {
68 this._closed = true;
69 return this._byteCount;
70 }
71
72 endSync(): number {
73 this._closed = true;
74 return this._byteCount;
75 }
76
77 async fail(reason?: any): Promise<void> {
78 this._closed = true;
79 }
80
81 failSync(reason?: any): boolean {
82 this._closed = true;
83 return true;
84 }
85
86 getText(): string {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected