download-previous-build.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { createWriteStream } from 'node:fs';
  2. import { mkdir } from 'node:fs/promises';
  3. import path from 'node:path';
  4. import { pipeline } from 'node:stream/promises';
  5. import { task } from './trace';
  6. import { defaultRequestInit, fetchWithRetry } from './lib/fetch-retry';
  7. import tarStream from 'tar-stream';
  8. import zlib from 'node:zlib';
  9. import { Readable } from 'node:stream';
  10. const GITHUB_CODELOAD_URL = 'https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master';
  11. const GITLAB_CODELOAD_URL = 'https://gitlab.com/SukkaW/ruleset.skk.moe/-/archive/master/ruleset.skk.moe-master.tar.gz';
  12. export const downloadPreviousBuild = task(require.main === module, __filename)(async (span) => {
  13. const tarGzUrl = await span.traceChildAsync('get tar.gz url', async () => {
  14. const resp = await fetchWithRetry(GITHUB_CODELOAD_URL, {
  15. ...defaultRequestInit,
  16. method: 'HEAD',
  17. retry: {
  18. retryOnNon2xx: false
  19. }
  20. });
  21. if (resp.status !== 200) {
  22. console.warn('Download previous build from GitHub failed! Status:', resp.status);
  23. console.warn('Switch to GitLab');
  24. return GITLAB_CODELOAD_URL;
  25. }
  26. return GITHUB_CODELOAD_URL;
  27. });
  28. const publicDir = path.resolve(__dirname, '..', 'public');
  29. return span.traceChildAsync('download & extract previoud build', async () => {
  30. const resp = await fetchWithRetry(tarGzUrl, {
  31. headers: {
  32. 'User-Agent': 'curl/8.9.1',
  33. // https://github.com/unjs/giget/issues/97
  34. // https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
  35. 'sec-fetch-mode': 'same-origin'
  36. },
  37. // https://github.com/unjs/giget/issues/97
  38. // https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
  39. mode: 'same-origin',
  40. retry: {
  41. retryOnNon2xx: false
  42. }
  43. });
  44. if (resp.status !== 200) {
  45. console.warn('Download previous build failed! Status:', resp.status);
  46. if (resp.status === 404) {
  47. return;
  48. }
  49. }
  50. if (!resp.body) {
  51. throw new Error('Download previous build failed! No body found');
  52. }
  53. const gunzip = zlib.createGunzip();
  54. const extract = tarStream.extract();
  55. pipeline(
  56. Readable.fromWeb(resp.body),
  57. gunzip,
  58. extract
  59. );
  60. const pathPrefix = 'ruleset.skk.moe-master/';
  61. for await (const entry of extract) {
  62. if (entry.header.type !== 'file') {
  63. entry.resume(); // Drain the entry
  64. continue;
  65. }
  66. const relativeEntryPath = entry.header.name.replace(pathPrefix, '');
  67. const targetPath = path.join(publicDir, relativeEntryPath);
  68. await mkdir(path.dirname(targetPath), { recursive: true });
  69. await pipeline(entry, createWriteStream(targetPath));
  70. }
  71. });
  72. });