fetch-remote-text-by-line.js 570 B

1234567891011121314151617181920
  1. // @ts-check
  2. const { fetchWithRetry } = require('./fetch-retry');
  3. const readline = require('readline');
  4. const { Readable } = require('stream');
  5. /**
  6. * @param {import('undici').RequestInfo} url
  7. * @param {import('undici').RequestInit | undefined} [opt]
  8. */
  9. module.exports.fetchRemoteTextAndCreateReadlineInterface = async (url, opt) => {
  10. const resp = await fetchWithRetry(url, opt);
  11. if (!resp.body) {
  12. throw new Error('Failed to fetch remote text');
  13. }
  14. return readline.createInterface({
  15. input: Readable.fromWeb(resp.body),
  16. crlfDelay: Infinity
  17. });
  18. }