Chore: replace tar-stream w/ tar-fs

This commit is contained in:
SukkaW 2024-09-09 06:15:32 +08:00
parent c1fa3c7b96
commit 385147f784
3 changed files with 81 additions and 29 deletions

View File

@ -1,10 +1,8 @@
import { createWriteStream } from 'node:fs';
import { mkdir } from 'node:fs/promises';
import path from 'node:path'; import path from 'node:path';
import { pipeline } from 'node:stream/promises'; import { pipeline } from 'node:stream/promises';
import { task } from './trace'; import { task } from './trace';
import { defaultRequestInit, fetchWithRetry } from './lib/fetch-retry'; import { defaultRequestInit, fetchWithRetry } from './lib/fetch-retry';
import tarStream from 'tar-stream'; import { extract as tarExtract } from 'tar-fs';
import zlib from 'node:zlib'; import zlib from 'node:zlib';
import { Readable } from 'node:stream'; import { Readable } from 'node:stream';
@ -58,28 +56,26 @@ export const downloadPreviousBuild = task(require.main === module, __filename)(a
throw new Error('Download previous build failed! No body found'); throw new Error('Download previous build failed! No body found');
} }
const gunzip = zlib.createGunzip(); const pathPrefix = 'ruleset.skk.moe-master/';
const extract = tarStream.extract();
pipeline( const gunzip = zlib.createGunzip();
const extract = tarExtract(
publicDir,
{
ignore(_, header) {
return header?.type !== 'file' && header?.type !== 'directory';
},
map(header) {
header.name = header.name.replace(pathPrefix, '');
return header;
}
}
);
return pipeline(
Readable.fromWeb(resp.body), Readable.fromWeb(resp.body),
gunzip, gunzip,
extract extract
); );
const pathPrefix = 'ruleset.skk.moe-master/';
for await (const entry of extract) {
if (entry.header.type !== 'file') {
entry.resume(); // Drain the entry
continue;
}
const relativeEntryPath = entry.header.name.replace(pathPrefix, '');
const targetPath = path.join(publicDir, relativeEntryPath);
await mkdir(path.dirname(targetPath), { recursive: true });
await pipeline(entry, createWriteStream(targetPath));
}
}); });
}); });

View File

@ -35,7 +35,7 @@
"picocolors": "^1.1.0", "picocolors": "^1.1.0",
"punycode": "^2.3.1", "punycode": "^2.3.1",
"table": "^6.8.2", "table": "^6.8.2",
"tar-stream": "^3.1.7", "tar-fs": "^3.0.6",
"tldts": "^6.1.43", "tldts": "^6.1.43",
"tldts-experimental": "^6.1.43", "tldts-experimental": "^6.1.43",
"yaml": "^2.5.1" "yaml": "^2.5.1"
@ -49,6 +49,7 @@
"@types/chai": "^4.3.19", "@types/chai": "^4.3.19",
"@types/mocha": "^10.0.7", "@types/mocha": "^10.0.7",
"@types/punycode": "^2.1.4", "@types/punycode": "^2.1.4",
"@types/tar-fs": "^2.0.4",
"@types/tar-stream": "^3.1.3", "@types/tar-stream": "^3.1.3",
"chai": "4", "chai": "4",
"eslint": "^9.10.0", "eslint": "^9.10.0",

69
pnpm-lock.yaml generated
View File

@ -56,9 +56,9 @@ importers:
table: table:
specifier: ^6.8.2 specifier: ^6.8.2
version: 6.8.2 version: 6.8.2
tar-stream: tar-fs:
specifier: ^3.1.7 specifier: ^3.0.6
version: 3.1.7 version: 3.0.6
tldts: tldts:
specifier: ^6.1.43 specifier: ^6.1.43
version: 6.1.43 version: 6.1.43
@ -93,6 +93,9 @@ importers:
'@types/punycode': '@types/punycode':
specifier: ^2.1.4 specifier: ^2.1.4
version: 2.1.4 version: 2.1.4
'@types/tar-fs':
specifier: ^2.0.4
version: 2.0.4
'@types/tar-stream': '@types/tar-stream':
specifier: ^3.1.3 specifier: ^3.1.3
version: 3.1.3 version: 3.1.3
@ -443,6 +446,9 @@ packages:
'@types/retry@0.12.5': '@types/retry@0.12.5':
resolution: {integrity: sha512-3xSjTp3v03X/lSQLkczaN9UIEwJMoMCA1+Nb5HfbJEQWogdeQIyVtTvxPXDQjZ5zws8rFQfVfRdz03ARihPJgw==} resolution: {integrity: sha512-3xSjTp3v03X/lSQLkczaN9UIEwJMoMCA1+Nb5HfbJEQWogdeQIyVtTvxPXDQjZ5zws8rFQfVfRdz03ARihPJgw==}
'@types/tar-fs@2.0.4':
resolution: {integrity: sha512-ipPec0CjTmVDWE+QKr9cTmIIoTl7dFG/yARCM5MqK8i6CNLIG1P8x4kwDsOQY1ChZOZjH0wO9nvfgBvWl4R3kA==}
'@types/tar-stream@3.1.3': '@types/tar-stream@3.1.3':
resolution: {integrity: sha512-Zbnx4wpkWBMBSu5CytMbrT5ZpMiF55qgM+EpHzR4yIDu7mv52cej8hTkOc6K+LzpkOAbxwn/m7j3iO+/l42YkQ==} resolution: {integrity: sha512-Zbnx4wpkWBMBSu5CytMbrT5ZpMiF55qgM+EpHzR4yIDu7mv52cej8hTkOc6K+LzpkOAbxwn/m7j3iO+/l42YkQ==}
@ -560,6 +566,18 @@ packages:
bare-events@2.4.2: bare-events@2.4.2:
resolution: {integrity: sha512-qMKFd2qG/36aA4GwvKq8MxnPgCQAmBWmSyLWsJcbn8v03wvIPQ/hG1Ms8bPzndZxMDoHpxez5VOS+gC9Yi24/Q==} resolution: {integrity: sha512-qMKFd2qG/36aA4GwvKq8MxnPgCQAmBWmSyLWsJcbn8v03wvIPQ/hG1Ms8bPzndZxMDoHpxez5VOS+gC9Yi24/Q==}
bare-fs@2.3.3:
resolution: {integrity: sha512-7RYKL+vZVCyAsMLi5SPu7QGauGGT8avnP/HO571ndEuV4MYdGXvLhtW67FuLPeEI8EiIY7zbbRR9x7x7HU0kgw==}
bare-os@2.4.2:
resolution: {integrity: sha512-HZoJwzC+rZ9lqEemTMiO0luOePoGYNBgsLLgegKR/cljiJvcDNhDZQkzC+NC5Oh0aHbdBNSOHpghwMuB5tqhjg==}
bare-path@2.1.3:
resolution: {integrity: sha512-lh/eITfU8hrj9Ru5quUp0Io1kJWIk1bTjzo7JH1P5dWmQ2EL4hFUlfI8FonAhSlgIfhn63p84CDY/x+PisgcXA==}
bare-stream@2.2.1:
resolution: {integrity: sha512-YTB47kHwBW9zSG8LD77MIBAAQXjU2WjAkMHeeb7hUplVs6+IoM5I7uEVQNPMB7lj9r8I76UMdoMkGnCodHOLqg==}
base64-js@1.5.1: base64-js@1.5.1:
resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==} resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==}
@ -1353,8 +1371,8 @@ packages:
stable-hash@0.0.4: stable-hash@0.0.4:
resolution: {integrity: sha512-LjdcbuBeLcdETCrPn9i8AYAZ1eCtu4ECAWtP7UleOiZ9LzVxRzzUZEoZ8zB24nhkQnDWyET0I+3sWokSDS3E7g==} resolution: {integrity: sha512-LjdcbuBeLcdETCrPn9i8AYAZ1eCtu4ECAWtP7UleOiZ9LzVxRzzUZEoZ8zB24nhkQnDWyET0I+3sWokSDS3E7g==}
streamx@2.18.0: streamx@2.20.0:
resolution: {integrity: sha512-LLUC1TWdjVdn1weXGcSxyTR3T4+acB6tVGXT95y0nGbca4t4o/ng1wKAGTljm9VicuCVLvRlqFYXYy5GwgM7sQ==} resolution: {integrity: sha512-ZGd1LhDeGFucr1CUCTBOS58ZhEendd0ttpGT3usTvosS4ntIwKN9LJFp+OeCSprsCPL14BXVRZlHGRY1V9PVzQ==}
string-argv@0.3.2: string-argv@0.3.2:
resolution: {integrity: sha512-aqD2Q0144Z+/RqG52NeHEkZauTAUWJO8c6yTftGJKO3Tja5tUgIfmIl6kExvhtxSDP7fXB6DvzkfMpCd/F3G+Q==} resolution: {integrity: sha512-aqD2Q0144Z+/RqG52NeHEkZauTAUWJO8c6yTftGJKO3Tja5tUgIfmIl6kExvhtxSDP7fXB6DvzkfMpCd/F3G+Q==}
@ -1410,6 +1428,9 @@ packages:
tar-fs@2.1.1: tar-fs@2.1.1:
resolution: {integrity: sha512-V0r2Y9scmbDRLCNex/+hYzvp/zyYjvFbHPNgVTKfQvVrb6guiE/fxP+XblDNR011utopbkex2nM4dHNV6GDsng==} resolution: {integrity: sha512-V0r2Y9scmbDRLCNex/+hYzvp/zyYjvFbHPNgVTKfQvVrb6guiE/fxP+XblDNR011utopbkex2nM4dHNV6GDsng==}
tar-fs@3.0.6:
resolution: {integrity: sha512-iokBDQQkUyeXhgPYaZxmczGPhnhXZ0CmrqI+MOb/WFGS9DW5wnfrLgtjUJBvz50vQ3qfRwJ62QVoCFu8mPVu5w==}
tar-stream@2.2.0: tar-stream@2.2.0:
resolution: {integrity: sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==} resolution: {integrity: sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==}
engines: {node: '>=6'} engines: {node: '>=6'}
@ -1865,6 +1886,11 @@ snapshots:
'@types/retry@0.12.5': {} '@types/retry@0.12.5': {}
'@types/tar-fs@2.0.4':
dependencies:
'@types/node': 20.14.11
'@types/tar-stream': 3.1.3
'@types/tar-stream@3.1.3': '@types/tar-stream@3.1.3':
dependencies: dependencies:
'@types/node': 20.14.11 '@types/node': 20.14.11
@ -2002,6 +2028,27 @@ snapshots:
bare-events@2.4.2: bare-events@2.4.2:
optional: true optional: true
bare-fs@2.3.3:
dependencies:
bare-events: 2.4.2
bare-path: 2.1.3
bare-stream: 2.2.1
optional: true
bare-os@2.4.2:
optional: true
bare-path@2.1.3:
dependencies:
bare-os: 2.4.2
optional: true
bare-stream@2.2.1:
dependencies:
b4a: 1.6.6
streamx: 2.20.0
optional: true
base64-js@1.5.1: {} base64-js@1.5.1: {}
better-sqlite3@11.2.1: better-sqlite3@11.2.1:
@ -2850,7 +2897,7 @@ snapshots:
stable-hash@0.0.4: {} stable-hash@0.0.4: {}
streamx@2.18.0: streamx@2.20.0:
dependencies: dependencies:
fast-fifo: 1.3.2 fast-fifo: 1.3.2
queue-tick: 1.0.1 queue-tick: 1.0.1
@ -2911,6 +2958,14 @@ snapshots:
pump: 3.0.0 pump: 3.0.0
tar-stream: 2.2.0 tar-stream: 2.2.0
tar-fs@3.0.6:
dependencies:
pump: 3.0.0
tar-stream: 3.1.7
optionalDependencies:
bare-fs: 2.3.3
bare-path: 2.1.3
tar-stream@2.2.0: tar-stream@2.2.0:
dependencies: dependencies:
bl: 4.1.0 bl: 4.1.0
@ -2923,7 +2978,7 @@ snapshots:
dependencies: dependencies:
b4a: 1.6.6 b4a: 1.6.6
fast-fifo: 1.3.2 fast-fifo: 1.3.2
streamx: 2.18.0 streamx: 2.20.0
text-decoder@1.1.1: text-decoder@1.1.1:
dependencies: dependencies: