mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 09:10:35 +08:00
Refactor: drop all Bun.write usage
This commit is contained in:
parent
b947f9f818
commit
eed0d58697
@ -5,6 +5,8 @@ import { exclude, merge } from 'fast-cidr-tools';
|
|||||||
import { getChnCidrPromise } from './build-chn-cidr';
|
import { getChnCidrPromise } from './build-chn-cidr';
|
||||||
import { NON_CN_CIDR_INCLUDED_IN_CHNROUTE, RESERVED_IPV4_CIDR } from './constants/cidr';
|
import { NON_CN_CIDR_INCLUDED_IN_CHNROUTE, RESERVED_IPV4_CIDR } from './constants/cidr';
|
||||||
|
|
||||||
|
import fsp from 'fs/promises';
|
||||||
|
|
||||||
export const buildInternalReverseChnCIDR = task(import.meta.main, import.meta.path)(async () => {
|
export const buildInternalReverseChnCIDR = task(import.meta.main, import.meta.path)(async () => {
|
||||||
const cidr = await getChnCidrPromise();
|
const cidr = await getChnCidrPromise();
|
||||||
|
|
||||||
@ -19,5 +21,9 @@ export const buildInternalReverseChnCIDR = task(import.meta.main, import.meta.pa
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
return Bun.write(path.resolve(import.meta.dir, '../Internal/reversed-chn-cidr.txt'), `${reversedCidr.join('\n')}\n`);
|
return fsp.writeFile(
|
||||||
|
path.resolve(import.meta.dir, '../Internal/reversed-chn-cidr.txt'),
|
||||||
|
reversedCidr.join('\n') + '\n',
|
||||||
|
{ encoding: 'utf-8' }
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import fsp from 'fs/promises';
|
||||||
import { task } from './trace';
|
import { task } from './trace';
|
||||||
import { treeDir } from './lib/tree-dir';
|
import { treeDir } from './lib/tree-dir';
|
||||||
import type { TreeType, TreeTypeArray } from './lib/tree-dir';
|
import type { TreeType, TreeTypeArray } from './lib/tree-dir';
|
||||||
@ -41,7 +42,7 @@ export const buildPublic = task(import.meta.main, import.meta.path)(async (span)
|
|||||||
const src = path.join(rootPath, file);
|
const src = path.join(rootPath, file);
|
||||||
const dest = path.join(publicPath, file);
|
const dest = path.join(publicPath, file);
|
||||||
|
|
||||||
return Bun.write(dest, Bun.file(src));
|
return fsp.copyFile(src, dest);
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -49,7 +50,7 @@ export const buildPublic = task(import.meta.main, import.meta.path)(async (span)
|
|||||||
.traceChild('generate index.html')
|
.traceChild('generate index.html')
|
||||||
.traceAsyncFn(() => treeDir(publicPath).then(generateHtml));
|
.traceAsyncFn(() => treeDir(publicPath).then(generateHtml));
|
||||||
|
|
||||||
return Bun.write(path.join(publicPath, 'index.html'), html);
|
return fsp.writeFile(path.join(publicPath, 'index.html'), html);
|
||||||
});
|
});
|
||||||
|
|
||||||
const priorityOrder: Record<'default' | string & {}, number> = {
|
const priorityOrder: Record<'default' | string & {}, number> = {
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import fsp from 'fs/promises';
|
||||||
import { task } from './trace';
|
import { task } from './trace';
|
||||||
import { compareAndWriteFile } from './lib/create-file';
|
import { compareAndWriteFile } from './lib/create-file';
|
||||||
import { DIRECTS, LANS } from '../Source/non_ip/direct';
|
import { DIRECTS, LANS } from '../Source/non_ip/direct';
|
||||||
@ -60,7 +61,7 @@ export const buildAlwaysRealIPModule = task(import.meta.main, import.meta.path)(
|
|||||||
],
|
],
|
||||||
path.resolve(import.meta.dir, '../Modules/sukka_common_always_realip.sgmodule')
|
path.resolve(import.meta.dir, '../Modules/sukka_common_always_realip.sgmodule')
|
||||||
),
|
),
|
||||||
Bun.write(
|
fsp.writeFile(
|
||||||
path.resolve(import.meta.dir, '../Internal/clash_fake_ip_filter.yaml'),
|
path.resolve(import.meta.dir, '../Internal/clash_fake_ip_filter.yaml'),
|
||||||
yaml.stringify(
|
yaml.stringify(
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
import { task } from './trace';
|
import { task } from './trace';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import fs from 'fs';
|
||||||
|
import { Readable } from 'stream';
|
||||||
|
import { pipeline } from 'stream/promises';
|
||||||
import { fetchWithRetry } from './lib/fetch-retry';
|
import { fetchWithRetry } from './lib/fetch-retry';
|
||||||
|
|
||||||
const ASSETS_LIST = {
|
const ASSETS_LIST = {
|
||||||
@ -15,5 +18,16 @@ const mockDir = path.resolve(import.meta.dir, '../Mock');
|
|||||||
export const downloadMockAssets = task(import.meta.main, import.meta.path)((span) => Promise.all(Object.entries(ASSETS_LIST).map(
|
export const downloadMockAssets = task(import.meta.main, import.meta.path)((span) => Promise.all(Object.entries(ASSETS_LIST).map(
|
||||||
([filename, url]) => span
|
([filename, url]) => span
|
||||||
.traceChild(url)
|
.traceChild(url)
|
||||||
.traceAsyncFn(() => fetchWithRetry(url).then(res => Bun.write(path.join(mockDir, filename), res)))
|
.traceAsyncFn(() => fetchWithRetry(url).then(res => {
|
||||||
|
const src = path.join(mockDir, filename);
|
||||||
|
if (!res.body) {
|
||||||
|
throw new Error(`Empty body from ${url}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const writeStream = fs.createWriteStream(src, { encoding: 'utf-8' });
|
||||||
|
return pipeline(
|
||||||
|
Readable.fromWeb(res.body),
|
||||||
|
writeStream
|
||||||
|
);
|
||||||
|
}))
|
||||||
)));
|
)));
|
||||||
|
|||||||
@ -4,6 +4,8 @@ import { surgeDomainsetToClashDomainset, surgeRulesetToClashClassicalTextRuleset
|
|||||||
import picocolors from 'picocolors';
|
import picocolors from 'picocolors';
|
||||||
import type { Span } from '../trace';
|
import type { Span } from '../trace';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import fs from 'fs';
|
||||||
|
import fsp from 'fs/promises';
|
||||||
import { sort } from './timsort';
|
import { sort } from './timsort';
|
||||||
import { fastStringArrayJoin } from './misc';
|
import { fastStringArrayJoin } from './misc';
|
||||||
|
|
||||||
@ -13,7 +15,7 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath
|
|||||||
|
|
||||||
const linesALen = linesA.length;
|
const linesALen = linesA.length;
|
||||||
|
|
||||||
if (!(await file.exists())) {
|
if (!fs.existsSync(filePath)) {
|
||||||
console.log(`${filePath} does not exists, writing...`);
|
console.log(`${filePath} does not exists, writing...`);
|
||||||
isEqual = false;
|
isEqual = false;
|
||||||
} else if (linesALen === 0) {
|
} else if (linesALen === 0) {
|
||||||
@ -70,7 +72,7 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath
|
|||||||
|
|
||||||
await span.traceChildAsync(`writing ${filePath}`, async () => {
|
await span.traceChildAsync(`writing ${filePath}`, async () => {
|
||||||
// if (linesALen < 10000) {
|
// if (linesALen < 10000) {
|
||||||
return Bun.write(file, fastStringArrayJoin(linesA, '\n') + '\n');
|
return fsp.writeFile(filePath, fastStringArrayJoin(linesA, '\n') + '\n');
|
||||||
// }
|
// }
|
||||||
// const writer = file.writer();
|
// const writer = file.writer();
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import fsp from 'fs/promises';
|
||||||
import { fdir as Fdir } from 'fdir';
|
import { fdir as Fdir } from 'fdir';
|
||||||
import { readFileByLine } from './lib/fetch-text-by-line';
|
import { readFileByLine } from './lib/fetch-text-by-line';
|
||||||
|
|
||||||
@ -43,5 +44,5 @@ async function trimFileLines(file: string) {
|
|||||||
result += line.trim() + '\n';
|
result += line.trim() + '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
return Bun.write(file, result);
|
return fsp.writeFile(file, result);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user