| 90 | } |
| 91 | |
| 92 | export class SyncMemoryWriter implements SyncWriter { |
| 93 | private chunks: Uint8Array[] = []; |
| 94 | private _closed = false; |
| 95 | private _byteCount = 0; |
| 96 | private enc = new TextEncoder(); |
| 97 | |
| 98 | get desiredSize(): number | null { |
| 99 | return this._closed ? null : 100; |
| 100 | } |
| 101 | |
| 102 | write(chunk: Uint8Array | string): boolean { |
| 103 | if (this._closed) throw new Error('Writer is closed'); |
| 104 | const bytes = typeof chunk === 'string' ? this.enc.encode(chunk) : chunk; |
| 105 | this.chunks.push(bytes); |
| 106 | this._byteCount += bytes.byteLength; |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | writev(chunks: (Uint8Array | string)[]): boolean { |
| 111 | for (const chunk of chunks) { |
| 112 | this.write(chunk); |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | end(): number { |
| 118 | this._closed = true; |
| 119 | return this._byteCount; |
| 120 | } |
| 121 | |
| 122 | fail(reason?: any): void { |
| 123 | this._closed = true; |
| 124 | } |
| 125 | |
| 126 | getText(): string { |
| 127 | const dec = new TextDecoder(); |
| 128 | return this.chunks.map((c) => dec.decode(c, { stream: true })).join('') + dec.decode(); |
| 129 | } |
| 130 | } |
nothing calls this directly
no outgoing calls
no test coverage detected