| 40 | constructor() { |
| 41 | const init = { |
| 42 | async transform( |
| 43 | chunk: unknown, |
| 44 | controller: TransformStreamDefaultController<Uint8Array>, |
| 45 | ) { |
| 46 | chunk = await chunk; |
| 47 | switch (typeof chunk) { |
| 48 | case "object": |
| 49 | if (chunk === null) { |
| 50 | controller.terminate(); |
| 51 | } else if (ArrayBuffer.isView(chunk)) { |
| 52 | controller.enqueue( |
| 53 | new Uint8Array( |
| 54 | chunk.buffer, |
| 55 | chunk.byteOffset, |
| 56 | chunk.byteLength, |
| 57 | ), |
| 58 | ); |
| 59 | } else if ( |
| 60 | Array.isArray(chunk) && |
| 61 | chunk.every((value) => typeof value === "number") |
| 62 | ) { |
| 63 | controller.enqueue(new Uint8Array(chunk)); |
| 64 | } else if ( |
| 65 | typeof chunk.valueOf === "function" && chunk.valueOf() !== chunk |
| 66 | ) { |
| 67 | this.transform(chunk.valueOf(), controller); |
| 68 | } else if ("toJSON" in chunk) { |
| 69 | this.transform(JSON.stringify(chunk), controller); |
| 70 | } |
| 71 | break; |
| 72 | case "symbol": |
| 73 | controller.error( |
| 74 | new TypeError("Cannot transform a symbol to a Uint8Array"), |
| 75 | ); |
| 76 | break; |
| 77 | case "undefined": |
| 78 | controller.error( |
| 79 | new TypeError("Cannot transform undefined to a Uint8Array"), |
| 80 | ); |
| 81 | break; |
| 82 | default: |
| 83 | controller.enqueue(this.encoder.encode(String(chunk))); |
| 84 | } |
| 85 | }, |
| 86 | encoder, |
| 87 | }; |
| 88 | super(init); |