(resp, processLine = false)
| 18 | } |
| 19 | |
| 20 | export const createReadlineInterfaceFromResponse: ((resp: UndiciResponseData | UnidiciWebResponse, processLine?: boolean) => ReadableStream<string>) = (resp, processLine = false) => { |
| 21 | invariant(resp.body, 'Failed to fetch remote text'); |
| 22 | if ('bodyUsed' in resp && resp.bodyUsed) { |
| 23 | throw new Error('Body has already been consumed.'); |
| 24 | } |
| 25 | let webStream: ReadableStream<Uint8Array>; |
| 26 | if ('pipeThrough' in resp.body) { |
| 27 | webStream = resp.body; |
| 28 | } else { |
| 29 | throw new TypeError('Invalid response body!'); |
| 30 | } |
| 31 | |
| 32 | const resultStream = webStream |
| 33 | // @ts-expect-error -- mismatched Node.js and web types |
| 34 | .pipeThrough(new TextDecoderStream()) |
| 35 | .pipeThrough(new TextLineStream({ skipEmptyLines: processLine })); |
| 36 | |
| 37 | if (processLine) { |
| 38 | return resultStream.pipeThrough(new ProcessLineStream()); |
| 39 | } |
| 40 | return resultStream; |
| 41 | }; |
| 42 | |
| 43 | export function fetchRemoteTextByLine(url: string, processLine = false): Promise<AsyncIterable<string>> { |
| 44 | return $$fetch(url).then(resp => createReadlineInterfaceFromResponse(resp, processLine)); |
no outgoing calls
no test coverage detected