MCPcopy
hub / github.com/SukkaW/Surge / createReadlineInterfaceFromResponse

Function createReadlineInterfaceFromResponse

Build/lib/fetch-text-by-line.ts:20–41  ·  view source on GitHub ↗
(resp, processLine = false)

Source from the content-addressed store, hash-verified

18}
19
20export 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
43export function fetchRemoteTextByLine(url: string, processLine = false): Promise<AsyncIterable<string>> {
44 return $$fetch(url).then(resp => createReadlineInterfaceFromResponse(resp, processLine));

Callers 4

fetchRemoteTextByLineFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected