mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-28 17:41:54 +08:00
Chore: say hello to bun
This commit is contained in:
@@ -44,35 +44,21 @@ async function compareAndWriteFile(linesA, filePath) {
|
||||
}
|
||||
|
||||
if (!isEqual) {
|
||||
const stream = fs.createWriteStream(filePath, { encoding: 'utf-8' });
|
||||
const file = Bun.file(filePath);
|
||||
const writer = file.writer();
|
||||
|
||||
for (let i = 0, len = linesA.length; i < len; i++) {
|
||||
const p = writeToStream(stream, `${linesA[i]}\n`);
|
||||
if (p) {
|
||||
// eslint-disable-next-line no-await-in-loop -- backpressure, besides we only wait for drain
|
||||
await p;
|
||||
}
|
||||
writer.write(`${linesA[i]}\n`);
|
||||
}
|
||||
stream.end();
|
||||
} else {
|
||||
console.log(`Same Content, bail out writing: ${filePath}`);
|
||||
|
||||
await writer.end();
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Same Content, bail out writing: ${filePath}`);
|
||||
}
|
||||
module.exports.compareAndWriteFile = compareAndWriteFile;
|
||||
|
||||
/**
|
||||
* @param {import('fs').WriteStream} stream
|
||||
* @param {string} data
|
||||
*/
|
||||
function writeToStream(stream, data) {
|
||||
if (!stream.write(data)) {
|
||||
return /** @type {Promise<void>} */(new Promise((resolve) => {
|
||||
stream.once('drain', resolve);
|
||||
}));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} title
|
||||
* @param {string[]} description
|
||||
|
||||
@@ -1,33 +1,60 @@
|
||||
// @ts-check
|
||||
const fs = require('fs');
|
||||
const { fetchWithRetry } = require('./fetch-retry');
|
||||
const readline = require('readline');
|
||||
const { Readable } = require('stream');
|
||||
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
/**
|
||||
* @param {string} path
|
||||
*/
|
||||
module.exports.readFileByLine = (path) => {
|
||||
return readline.createInterface({
|
||||
input: fs.createReadStream(path, { encoding: 'utf-8' }),
|
||||
crlfDelay: Infinity
|
||||
});
|
||||
module.exports.readFileByLine = async function *(path) {
|
||||
let buf = '';
|
||||
|
||||
for await (const chunk of Bun.file(path).stream()) {
|
||||
const chunkStr = decoder.decode(chunk).replaceAll('\r\n', '\n');
|
||||
for (let i = 0, len = chunkStr.length; i < len; i++) {
|
||||
const char = chunkStr[i];
|
||||
if (char === '\n') {
|
||||
yield buf;
|
||||
buf = '';
|
||||
} else {
|
||||
buf += char;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (buf) {
|
||||
yield buf;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {import('undici').Response} resp
|
||||
*/
|
||||
const createReadlineInterfaceFromResponse = (resp) => {
|
||||
const createReadlineInterfaceFromResponse = async function *(resp) {
|
||||
if (!resp.body) {
|
||||
throw new Error('Failed to fetch remote text');
|
||||
}
|
||||
if (resp.bodyUsed) {
|
||||
throw new Error('Body has already been consumed.');
|
||||
}
|
||||
return readline.createInterface({
|
||||
input: Readable.fromWeb(resp.body),
|
||||
crlfDelay: Infinity
|
||||
});
|
||||
|
||||
let buf = '';
|
||||
|
||||
for await (const chunk of resp.body) {
|
||||
const chunkStr = decoder.decode(chunk).replaceAll('\r\n', '\n');
|
||||
for (let i = 0, len = chunkStr.length; i < len; i++) {
|
||||
const char = chunkStr[i];
|
||||
if (char === '\n') {
|
||||
yield buf;
|
||||
buf = '';
|
||||
} else {
|
||||
buf += char;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (buf) {
|
||||
yield buf;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.createReadlineInterfaceFromResponse = createReadlineInterfaceFromResponse;
|
||||
|
||||
Reference in New Issue
Block a user