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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 | URL | BunFile): AsyncGenerator<string> {
  22. if (typeof file === 'string') {
  23. file = Bun.file(file);
  24. } else if (!('writer' in file)) {
  25. file = Bun.file(file);
  26. }
  27. let buf = '';
  28. for await (const chunk of file.stream()) {
  29. const chunkStr = decoder.decode(chunk).replaceAll('\r\n', '\n');
  30. for (let i = 0, len = chunkStr.length; i < len; i++) {
  31. const char = chunkStr[i];
  32. if (char === '\n') {
  33. yield buf;
  34. buf = '';
  35. } else {
  36. buf += char;
  37. }
  38. }
  39. }
  40. if (buf) {
  41. yield buf;
  42. }
  43. }
  44. export async function *createReadlineInterfaceFromResponse(resp: Response): AsyncGenerator<string> {
  45. if (!resp.body) {
  46. throw new Error('Failed to fetch remote text');
  47. }
  48. if (resp.bodyUsed) {
  49. throw new Error('Body has already been consumed.');
  50. }
  51. let buf = '';
  52. for await (const chunk of resp.body) {
  53. const chunkStr = decoder.decode(chunk).replaceAll('\r\n', '\n');
  54. for (let i = 0, len = chunkStr.length; i < len; i++) {
  55. const char = chunkStr[i];
  56. if (char === '\n') {
  57. yield buf;
  58. buf = '';
  59. } else {
  60. buf += char;
  61. }
  62. }
  63. }
  64. if (buf) {
  65. yield buf;
  66. }
  67. }
  68. export function fetchRemoteTextAndCreateReadlineInterface(url: string | URL) {
  69. return fetchWithRetry(url, defaultRequestInit).then(res => createReadlineInterfaceFromResponse(res as Response));
  70. }