download-previous-build.ts 2.7 KB

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