Chore: replace tar w/ tar-stream

This commit is contained in:
SukkaW
2023-12-13 12:02:10 +08:00
parent dec8952226
commit a2db9ded8b
3 changed files with 24 additions and 34 deletions

View File

@@ -1,4 +1,3 @@
import tar from 'tar';
import fs from 'fs'; import fs from 'fs';
import fsp from 'fs/promises'; import fsp from 'fs/promises';
import path from 'path'; import path from 'path';
@@ -9,6 +8,8 @@ import { readFileByLine } from './lib/fetch-text-by-line';
import { isCI } from 'ci-info'; import { isCI } from 'ci-info';
import { task, traceAsync } from './lib/trace-runner'; import { task, traceAsync } from './lib/trace-runner';
import { defaultRequestInit, fetchWithRetry } from './lib/fetch-retry'; import { defaultRequestInit, fetchWithRetry } from './lib/fetch-retry';
import tarStream from 'tar-stream';
import zlib from 'zlib';
const IS_READING_BUILD_OUTPUT = 1 << 2; const IS_READING_BUILD_OUTPUT = 1 << 2;
const ALL_FILES_EXISTS = 1 << 3; const ALL_FILES_EXISTS = 1 << 3;
@@ -56,41 +57,30 @@ export const downloadPreviousBuild = task(import.meta.path, async () => {
fetchWithRetry('https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master', defaultRequestInit), fetchWithRetry('https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master', defaultRequestInit),
fsp.mkdir(extractedPath, { recursive: true }) fsp.mkdir(extractedPath, { recursive: true })
]); ]);
await pipeline(
Readable.fromWeb(resp.body!), const extract = tarStream.extract();
tar.t({ Readable.fromWeb(resp.body!).pipe(zlib.createGunzip()).pipe(extract);
filter(p) { for await (const entry of extract) {
return filesList.some(f => p.startsWith(f)); if (entry.header.type !== 'file') {
}, entry.resume(); // Drain the entry
// onentry is async, so we close entry manually after consumed continue;
noResume: true, }
async onentry(entry) { // filter entry
if (entry.type !== 'File') { if (!filesList.some(f => entry.header.name.startsWith(f))) {
// not a file, throw away entry.resume(); // Drain the entry
entry.resume(); continue;
return;
} }
const relativeEntryPath = entry.path.replace(`ruleset.skk.moe-master${path.sep}`, ''); const relativeEntryPath = entry.header.name.replace(`ruleset.skk.moe-master${path.sep}`, '');
const targetPath = path.join(import.meta.dir, '..', relativeEntryPath); const targetPath = path.join(import.meta.dir, '..', relativeEntryPath);
await fsp.mkdir(path.dirname(targetPath), { recursive: true }); await fsp.mkdir(path.dirname(targetPath), { recursive: true });
await pipeline(
const targetFileSink = Bun.file(targetPath).writer(); entry,
const onData = (chunk: Buffer) => targetFileSink.write(chunk); fs.createWriteStream(targetPath)
// I don't know, but for some reason it is impossible to consume entry with AsyncIterator
await new Promise<void>((resolve, reject) => {
entry.on('data', onData);
entry.on('end', resolve);
entry.on('error', reject);
});
await targetFileSink.end();
}
})
); );
} }
}
); );
}); });

BIN
bun.lockb

Binary file not shown.

View File

@@ -26,14 +26,14 @@
"picocolors": "^1.0.0", "picocolors": "^1.0.0",
"punycode": "^2.3.1", "punycode": "^2.3.1",
"table": "^6.8.1", "table": "^6.8.1",
"tar": "^6.2.0", "tar-stream": "^3.1.6",
"tldts": "^6.0.22" "tldts": "^6.0.22"
}, },
"devDependencies": { "devDependencies": {
"@eslint-sukka/node": "4.1.10-beta.2", "@eslint-sukka/node": "4.1.10-beta.2",
"@eslint-sukka/ts": "4.1.10-beta.2", "@eslint-sukka/ts": "4.1.10-beta.2",
"@types/async-retry": "^1.4.8", "@types/async-retry": "^1.4.8",
"@types/tar": "^6.1.9", "@types/tar-stream": "^3.1.3",
"bun-types": "^1.0.11", "bun-types": "^1.0.11",
"eslint": "^8.55.0", "eslint": "^8.55.0",
"eslint-config-sukka": "4.1.10-beta.2", "eslint-config-sukka": "4.1.10-beta.2",