|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +/** |
| 4 | + * @template T |
| 5 | + * @template U |
| 6 | + * @template V |
| 7 | + * @typedef {import('./stream.js').Stream<T, U, V>} Stream |
| 8 | + */ |
| 9 | + |
| 10 | +const COLON = ':'.charCodeAt(0); |
| 11 | +const COMMA = ','.charCodeAt(0); |
| 12 | + |
| 13 | +const decoder = new TextDecoder(); |
| 14 | +const encoder = new TextEncoder(); |
| 15 | + |
| 16 | +/** |
| 17 | + * @param {AsyncIterable<Uint8Array>} input |
| 18 | + * @param {string=} name |
| 19 | + * @param {number=} capacity |
| 20 | + * @returns {AsyncIterableIterator<Uint8Array>} input |
| 21 | + */ |
| 22 | +export async function* reader(input, name = '<unknown>', capacity = 1024) { |
| 23 | + let length = 0; |
| 24 | + let buffer = new Uint8Array(capacity); |
| 25 | + let offset = 0; |
| 26 | + |
| 27 | + for await (const chunk of input) { |
| 28 | + if (length + chunk.byteLength >= capacity) { |
| 29 | + while (length + chunk.byteLength >= capacity) { |
| 30 | + capacity *= 2; |
| 31 | + } |
| 32 | + const replacement = new Uint8Array(capacity); |
| 33 | + replacement.set(buffer, 0); |
| 34 | + buffer = replacement; |
| 35 | + } |
| 36 | + buffer.set(chunk, length); |
| 37 | + length += chunk.byteLength; |
| 38 | + |
| 39 | + let drained = false; |
| 40 | + while (!drained && length > 0) { |
| 41 | + const colon = buffer.indexOf(COLON); |
| 42 | + if (colon === 0) { |
| 43 | + throw new Error( |
| 44 | + `Expected number before colon at offset ${offset} of ${name}`, |
| 45 | + ); |
| 46 | + } else if (colon > 0) { |
| 47 | + const prefixBytes = buffer.subarray(0, colon); |
| 48 | + const prefixString = decoder.decode(prefixBytes); |
| 49 | + const contentLength = +prefixString; |
| 50 | + if (Number.isNaN(contentLength)) { |
| 51 | + throw new Error( |
| 52 | + `Invalid netstring prefix length ${prefixString} at offset ${offset} of ${name}`, |
| 53 | + ); |
| 54 | + } |
| 55 | + const messageLength = colon + contentLength + 2; |
| 56 | + if (messageLength <= length) { |
| 57 | + yield buffer.subarray(colon + 1, colon + 1 + contentLength); |
| 58 | + buffer.copyWithin(0, messageLength); |
| 59 | + length -= messageLength; |
| 60 | + offset += messageLength; |
| 61 | + } else { |
| 62 | + drained = true; |
| 63 | + } |
| 64 | + } else { |
| 65 | + drained = true; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (length > 0) { |
| 71 | + throw new Error( |
| 72 | + `Unexpected dangling message at offset ${offset} of ${name}`, |
| 73 | + ); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * @param {Stream<void, Uint8Array, void>} output |
| 79 | + * @returns {Stream<void, Uint8Array, void>} |
| 80 | + */ |
| 81 | +export function writer(output) { |
| 82 | + const scratch = new Uint8Array(8); |
| 83 | + |
| 84 | + return { |
| 85 | + async next(message) { |
| 86 | + const { written: length = 0 } = encoder.encodeInto( |
| 87 | + `${message.byteLength}`, |
| 88 | + scratch, |
| 89 | + ); |
| 90 | + scratch[length] = COLON; |
| 91 | + |
| 92 | + const { done: done1 } = await output.next( |
| 93 | + scratch.subarray(0, length + 1), |
| 94 | + ); |
| 95 | + if (done1) { |
| 96 | + return output.return(); |
| 97 | + } |
| 98 | + |
| 99 | + const { done: done2 } = await output.next(message); |
| 100 | + if (done2) { |
| 101 | + return output.return(); |
| 102 | + } |
| 103 | + |
| 104 | + scratch[0] = COMMA; |
| 105 | + return output.next(scratch.subarray(0, 1)); |
| 106 | + }, |
| 107 | + async return() { |
| 108 | + return output.return(); |
| 109 | + }, |
| 110 | + async throw(error) { |
| 111 | + return output.throw(error); |
| 112 | + }, |
| 113 | + [Symbol.asyncIterator]() { |
| 114 | + return this; |
| 115 | + }, |
| 116 | + }; |
| 117 | +} |
0 commit comments