Chore: read request stream line by line

This commit is contained in:
SukkaW
2023-06-14 23:15:21 +08:00
parent 8a77541ce7
commit bf4c92cc5d
6 changed files with 57 additions and 33 deletions

View File

@@ -0,0 +1,20 @@
// @ts-check
const { fetchWithRetry } = require('./fetch-retry');
const readline = require('readline');
const { Readable } = require('stream');
/**
* @param {import('undici').RequestInfo} url
* @param {import('undici').RequestInit | undefined} [opt]
*/
module.exports.fetchRemoteTextAndCreateReadlineInterface = async (url, opt) => {
const resp = await fetchWithRetry(url, opt);
if (!resp.body) {
throw new Error('Failed to fetch remote text');
}
return readline.createInterface({
input: Readable.fromWeb(resp.body),
crlfDelay: Infinity
});
}