mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-15 02:30:37 +08:00
Refactor: use undici.pipeline & undici.request
This commit is contained in:
parent
0dd7150709
commit
e6f7a98ee9
@ -1,13 +1,10 @@
|
|||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import fs from 'node:fs';
|
|
||||||
import { pipeline } from 'node:stream/promises';
|
import { pipeline } from 'node:stream/promises';
|
||||||
import picocolors from 'picocolors';
|
|
||||||
import { task } from './trace';
|
import { task } from './trace';
|
||||||
import { extract as tarExtract } from 'tar-fs';
|
import { extract as tarExtract } from 'tar-fs';
|
||||||
import type { Headers as TarEntryHeaders } from 'tar-fs';
|
import type { Headers as TarEntryHeaders } from 'tar-fs';
|
||||||
import zlib from 'node:zlib';
|
import zlib from 'node:zlib';
|
||||||
import { fetchWithLog } from './lib/fetch-retry';
|
import undici from 'undici';
|
||||||
import { Readable } from 'node:stream';
|
|
||||||
|
|
||||||
const GITHUB_CODELOAD_URL = 'https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master';
|
const GITHUB_CODELOAD_URL = 'https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master';
|
||||||
const GITLAB_CODELOAD_URL = 'https://gitlab.com/SukkaW/ruleset.skk.moe/-/archive/master/ruleset.skk.moe-master.tar.gz';
|
const GITLAB_CODELOAD_URL = 'https://gitlab.com/SukkaW/ruleset.skk.moe/-/archive/master/ruleset.skk.moe-master.tar.gz';
|
||||||
@ -15,15 +12,15 @@ const GITLAB_CODELOAD_URL = 'https://gitlab.com/SukkaW/ruleset.skk.moe/-/archive
|
|||||||
export const downloadPreviousBuild = task(require.main === module, __filename)(async (span) => {
|
export const downloadPreviousBuild = task(require.main === module, __filename)(async (span) => {
|
||||||
const publicDir = path.resolve(__dirname, '..', 'public');
|
const publicDir = path.resolve(__dirname, '..', 'public');
|
||||||
|
|
||||||
if (fs.existsSync(publicDir)) {
|
// if (fs.existsSync(publicDir)) {
|
||||||
console.log(picocolors.blue('Public directory exists, skip downloading previous build'));
|
// console.log(picocolors.blue('Public directory exists, skip downloading previous build'));
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
const tarGzUrl = await span.traceChildAsync('get tar.gz url', async () => {
|
const tarGzUrl = await span.traceChildAsync('get tar.gz url', async () => {
|
||||||
const resp = await fetchWithLog(GITHUB_CODELOAD_URL, { method: 'HEAD' });
|
const resp = await undici.request(GITHUB_CODELOAD_URL, { method: 'HEAD' });
|
||||||
if (resp.status !== 200) {
|
if (resp.statusCode !== 200) {
|
||||||
console.warn('Download previous build from GitHub failed! Status:', resp.status);
|
console.warn('Download previous build from GitHub failed! Status:', resp.statusCode);
|
||||||
console.warn('Switch to GitLab');
|
console.warn('Switch to GitLab');
|
||||||
return GITLAB_CODELOAD_URL;
|
return GITLAB_CODELOAD_URL;
|
||||||
}
|
}
|
||||||
@ -31,26 +28,32 @@ export const downloadPreviousBuild = task(require.main === module, __filename)(a
|
|||||||
});
|
});
|
||||||
|
|
||||||
return span.traceChildAsync('download & extract previoud build', async () => {
|
return span.traceChildAsync('download & extract previoud build', async () => {
|
||||||
const resp = await fetchWithLog(tarGzUrl, {
|
const respBody = undici.pipeline(
|
||||||
headers: {
|
tarGzUrl,
|
||||||
'User-Agent': 'curl/8.9.1',
|
{
|
||||||
// https://github.com/unjs/giget/issues/97
|
method: 'GET',
|
||||||
// https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
|
headers: {
|
||||||
'sec-fetch-mode': 'same-origin'
|
'User-Agent': 'curl/8.9.1',
|
||||||
|
// https://github.com/unjs/giget/issues/97
|
||||||
|
// https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
|
||||||
|
'sec-fetch-mode': 'same-origin'
|
||||||
|
},
|
||||||
|
// Allow redirects by default
|
||||||
|
maxRedirections: 5
|
||||||
},
|
},
|
||||||
mode: 'same-origin'
|
({ statusCode, body }) => {
|
||||||
});
|
if (statusCode !== 200) {
|
||||||
|
console.warn('Download previous build failed! Status:', statusCode);
|
||||||
|
if (statusCode === 404) {
|
||||||
|
throw new Error('Download previous build failed! 404');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (resp.status !== 200) {
|
return body;
|
||||||
console.warn('Download previous build failed! Status:', resp.status);
|
|
||||||
if (resp.status === 404) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
// by default, undici.pipeline returns a duplex stream (for POST/PUT)
|
||||||
|
// Since we are using GET, we need to end the write immediately
|
||||||
if (!resp.body) {
|
).end();
|
||||||
throw new Error('Download previous build failed! No body found');
|
|
||||||
}
|
|
||||||
|
|
||||||
const pathPrefix = 'ruleset.skk.moe-master/';
|
const pathPrefix = 'ruleset.skk.moe-master/';
|
||||||
|
|
||||||
@ -67,7 +70,7 @@ export const downloadPreviousBuild = task(require.main === module, __filename)(a
|
|||||||
);
|
);
|
||||||
|
|
||||||
return pipeline(
|
return pipeline(
|
||||||
Readable.fromWeb(resp.body),
|
respBody,
|
||||||
gunzip,
|
gunzip,
|
||||||
extract
|
extract
|
||||||
);
|
);
|
||||||
|
|||||||
@ -41,7 +41,7 @@
|
|||||||
"tar-fs": "^3.0.6",
|
"tar-fs": "^3.0.6",
|
||||||
"tldts": "^6.1.51",
|
"tldts": "^6.1.51",
|
||||||
"tldts-experimental": "^6.1.51",
|
"tldts-experimental": "^6.1.51",
|
||||||
"undici": "^6.20.0",
|
"undici": "7.0.0-alpha.2",
|
||||||
"wtfnode": "^0.9.3",
|
"wtfnode": "^0.9.3",
|
||||||
"yaml": "^2.5.1"
|
"yaml": "^2.5.1"
|
||||||
},
|
},
|
||||||
|
|||||||
10
pnpm-lock.yaml
generated
10
pnpm-lock.yaml
generated
@ -75,8 +75,8 @@ importers:
|
|||||||
specifier: ^6.1.51
|
specifier: ^6.1.51
|
||||||
version: 6.1.51
|
version: 6.1.51
|
||||||
undici:
|
undici:
|
||||||
specifier: ^6.20.0
|
specifier: 7.0.0-alpha.2
|
||||||
version: 6.20.0
|
version: 7.0.0-alpha.2
|
||||||
wtfnode:
|
wtfnode:
|
||||||
specifier: ^0.9.3
|
specifier: ^0.9.3
|
||||||
version: 0.9.3
|
version: 0.9.3
|
||||||
@ -1771,8 +1771,8 @@ packages:
|
|||||||
undici-types@6.19.8:
|
undici-types@6.19.8:
|
||||||
resolution: {integrity: sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==}
|
resolution: {integrity: sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==}
|
||||||
|
|
||||||
undici@6.20.0:
|
undici@7.0.0-alpha.2:
|
||||||
resolution: {integrity: sha512-AITZfPuxubm31Sx0vr8bteSalEbs9wQb/BOBi9FPlD9Qpd6HxZ4Q0+hI742jBhkPb4RT2v5MQzaW5VhRVyj+9A==}
|
resolution: {integrity: sha512-4Xn6pwsp6U8F9U7qdDhDwcHPvjcfwt3gbswRg30xgmkQ4Bpfbi6J1OrjTe8WIUVecbcGWOZMPAt6crf0PLliUw==}
|
||||||
engines: {node: '>=18.17'}
|
engines: {node: '>=18.17'}
|
||||||
|
|
||||||
unique-filename@4.0.0:
|
unique-filename@4.0.0:
|
||||||
@ -3613,7 +3613,7 @@ snapshots:
|
|||||||
|
|
||||||
undici-types@6.19.8: {}
|
undici-types@6.19.8: {}
|
||||||
|
|
||||||
undici@6.20.0: {}
|
undici@7.0.0-alpha.2: {}
|
||||||
|
|
||||||
unique-filename@4.0.0:
|
unique-filename@4.0.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user