fetch-text-by-line.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import fs from 'node:fs';
  2. import { Readable } from 'node:stream';
  3. import type { FileHandle } from 'node:fs/promises';
  4. import readline from 'node:readline';
  5. import { TextLineStream } from './text-line-transform-stream';
  6. import type { ReadableStream } from 'node:stream/web';
  7. import { TextDecoderStream } from 'node:stream/web';
  8. import { processLine, ProcessLineStream } from './process-line';
  9. import { $fetch } from './make-fetch-happen';
  10. import type { NodeFetchResponse } from './make-fetch-happen';
  11. import type { UndiciResponseData } from './fetch-retry';
  12. import type { Response as UnidiciWebResponse } from 'undici';
  13. function getReadableStream(file: string | FileHandle): ReadableStream {
  14. if (typeof file === 'string') {
  15. // return fs.openAsBlob(file).then(blob => blob.stream())
  16. return Readable.toWeb(fs.createReadStream(file/* , { encoding: 'utf-8' } */));
  17. }
  18. return file.readableWebStream();
  19. }
  20. // TODO: use FileHandle.readLine()
  21. export const readFileByLineLegacy: ((file: string /* | FileHandle */) => AsyncIterable<string>) = (file: string | FileHandle) => getReadableStream(file)
  22. .pipeThrough(new TextDecoderStream())
  23. .pipeThrough(new TextLineStream());
  24. export const readFileByLine: ((file: string /* | FileHandle */) => AsyncIterable<string>) = (file: string) => readline.createInterface({
  25. input: fs.createReadStream(file/* , { encoding: 'utf-8' } */),
  26. crlfDelay: Infinity
  27. });
  28. function ensureResponseBody<T extends NodeFetchResponse | UndiciResponseData | UnidiciWebResponse>(resp: T): NonNullable<T['body']> {
  29. if (resp.body == null) {
  30. throw new Error('Failed to fetch remote text');
  31. }
  32. if ('bodyUsed' in resp && resp.bodyUsed) {
  33. throw new Error('Body has already been consumed.');
  34. }
  35. return resp.body;
  36. }
  37. export const createReadlineInterfaceFromResponse: ((resp: NodeFetchResponse | UndiciResponseData | UnidiciWebResponse, processLine?: boolean) => ReadableStream<string>) = (resp, processLine = false) => {
  38. const stream = ensureResponseBody(resp);
  39. const webStream: ReadableStream<Uint8Array> = 'getReader' in stream
  40. ? stream
  41. : (
  42. 'text' in stream
  43. ? stream.body as any
  44. : Readable.toWeb(new Readable().wrap(stream))
  45. );
  46. const resultStream = webStream
  47. .pipeThrough(new TextDecoderStream())
  48. .pipeThrough(new TextLineStream());
  49. if (processLine) {
  50. return resultStream.pipeThrough(new ProcessLineStream());
  51. }
  52. return resultStream;
  53. };
  54. export function fetchRemoteTextByLine(url: string, processLine = false): Promise<AsyncIterable<string>> {
  55. return $fetch(url).then(resp => createReadlineInterfaceFromResponse(resp, processLine));
  56. }
  57. export async function readFileIntoProcessedArray(file: string /* | FileHandle */) {
  58. const results = [];
  59. for await (const line of readFileByLine(file)) {
  60. if (processLine(line)) {
  61. results.push(line);
  62. }
  63. }
  64. return results;
  65. }