fetch-text-by-line.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import fs from 'node:fs';
  2. import readline from 'node:readline';
  3. import { TextLineStream } from 'foxts/text-line-stream';
  4. import type { ReadableStream } from 'node:stream/web';
  5. import { TextDecoderStream } from 'node:stream/web';
  6. import { processLine, ProcessLineStream } from './process-line';
  7. import { $$fetch } from './fetch-retry';
  8. import type { UndiciResponseData } from './fetch-retry';
  9. import type { Response as UnidiciWebResponse } from 'undici';
  10. import { invariant } from 'foxts/guard';
  11. export function readFileByLine(file: string): AsyncIterable<string> {
  12. return readline.createInterface({
  13. input: fs.createReadStream(file/* , { encoding: 'utf-8' } */),
  14. crlfDelay: Infinity
  15. });
  16. }
  17. export const createReadlineInterfaceFromResponse: ((resp: UndiciResponseData | UnidiciWebResponse, processLine?: boolean) => ReadableStream<string>) = (resp, processLine = false) => {
  18. invariant(resp.body, 'Failed to fetch remote text');
  19. if ('bodyUsed' in resp && resp.bodyUsed) {
  20. throw new Error('Body has already been consumed.');
  21. }
  22. let webStream: ReadableStream<Uint8Array>;
  23. if ('pipeThrough' in resp.body) {
  24. webStream = resp.body;
  25. } else {
  26. throw new TypeError('Invalid response body!');
  27. }
  28. const resultStream = webStream
  29. .pipeThrough(new TextDecoderStream())
  30. .pipeThrough(new TextLineStream({ skipEmptyLines: processLine }));
  31. if (processLine) {
  32. return resultStream.pipeThrough(new ProcessLineStream());
  33. }
  34. return resultStream;
  35. };
  36. export function fetchRemoteTextByLine(url: string, processLine = false): Promise<AsyncIterable<string>> {
  37. return $$fetch(url).then(resp => createReadlineInterfaceFromResponse(resp, processLine));
  38. }
  39. export async function readFileIntoProcessedArray(file: string /* | FileHandle */) {
  40. const results = [];
  41. let processed: string | null = '';
  42. for await (const line of readFileByLine(file)) {
  43. processed = processLine(line);
  44. if (processed) {
  45. results.push(processed);
  46. }
  47. }
  48. return results;
  49. }