fetch-remote-text-by-line.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import type { BunFile } from 'bun';
  2. import { fetchWithRetry, defaultRequestInit } from './fetch-retry';
  3. // import { TextLineStream } from './text-line-transform-stream';
  4. // import { PolyfillTextDecoderStream } from './text-decoder-stream';
  5. // export function readFileByLine(file: string | BunFile) {
  6. // if (typeof file === 'string') {
  7. // file = Bun.file(file);
  8. // }
  9. // return file.stream().pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream());
  10. // }
  11. // export function createReadlineInterfaceFromResponse(resp: Response) {
  12. // if (!resp.body) {
  13. // throw new Error('Failed to fetch remote text');
  14. // }
  15. // if (resp.bodyUsed) {
  16. // throw new Error('Body has already been consumed.');
  17. // }
  18. // return (resp.body as ReadableStream<Uint8Array>).pipeThrough(new PolyfillTextDecoderStream()).pipeThrough(new TextLineStream());
  19. // }
  20. const decoder = new TextDecoder('utf-8');
  21. export async function *readFileByLine(file: string | BunFile): AsyncGenerator<string> {
  22. if (typeof file === 'string') {
  23. file = Bun.file(file);
  24. }
  25. let buf = '';
  26. for await (const chunk of file.stream()) {
  27. const chunkStr = decoder.decode(chunk).replaceAll('\r\n', '\n');
  28. for (let i = 0, len = chunkStr.length; i < len; i++) {
  29. const char = chunkStr[i];
  30. if (char === '\n') {
  31. yield buf;
  32. buf = '';
  33. } else {
  34. buf += char;
  35. }
  36. }
  37. }
  38. if (buf) {
  39. yield buf;
  40. }
  41. }
  42. export async function *createReadlineInterfaceFromResponse(resp: Response): AsyncGenerator<string> {
  43. if (!resp.body) {
  44. throw new Error('Failed to fetch remote text');
  45. }
  46. if (resp.bodyUsed) {
  47. throw new Error('Body has already been consumed.');
  48. }
  49. let buf = '';
  50. for await (const chunk of resp.body) {
  51. const chunkStr = decoder.decode(chunk).replaceAll('\r\n', '\n');
  52. for (let i = 0, len = chunkStr.length; i < len; i++) {
  53. const char = chunkStr[i];
  54. if (char === '\n') {
  55. yield buf;
  56. buf = '';
  57. } else {
  58. buf += char;
  59. }
  60. }
  61. }
  62. if (buf) {
  63. yield buf;
  64. }
  65. }
  66. export function fetchRemoteTextAndCreateReadlineInterface(url: string | URL) {
  67. return fetchWithRetry(url, defaultRequestInit).then(res => createReadlineInterfaceFromResponse(res as Response));
  68. }