| 58 | } |
| 59 | |
| 60 | writeString(value: string): void { |
| 61 | const encoded = this.stringEncoder.encode(value); |
| 62 | const len = encoded.length; |
| 63 | |
| 64 | // Write length as u32 first |
| 65 | if (this.cursor + 4 > this.buffer.length) this.expand(); |
| 66 | this.view.setUint32(this.cursor, len, true); |
| 67 | this.cursor += 4; |
| 68 | |
| 69 | // Write string bytes |
| 70 | if (this.cursor + len > this.buffer.length) this.expand(); |
| 71 | this.buffer.set(encoded, this.cursor); |
| 72 | this.cursor += len; |
| 73 | } |
| 74 | |
| 75 | flush(): Uint8Array { |
| 76 | return this.buffer.slice(0, this.cursor); |