download-previous-build.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import path from 'node:path';
  2. import fs from 'node:fs';
  3. import { pipeline } from 'node:stream/promises';
  4. import { task } from './trace';
  5. import { extract as tarExtract } from 'tar-fs';
  6. import type { Headers as TarEntryHeaders } from 'tar-fs';
  7. import zlib from 'node:zlib';
  8. import undici from 'undici';
  9. import picocolors from 'picocolors';
  10. import { PUBLIC_DIR } from './constants/dir';
  11. import { requestWithLog } from './lib/fetch-retry';
  12. import { isDirectoryEmptySync } from './lib/misc';
  13. const GITHUB_CODELOAD_URL = 'https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master';
  14. const GITLAB_CODELOAD_URL = 'https://gitlab.com/SukkaW/ruleset.skk.moe/-/archive/master/ruleset.skk.moe-master.tar.gz';
  15. export const downloadPreviousBuild = task(require.main === module, __filename)(async (span) => {
  16. if (fs.existsSync(PUBLIC_DIR) && !isDirectoryEmptySync(PUBLIC_DIR)) {
  17. console.log(picocolors.blue('Public directory exists, skip downloading previous build'));
  18. return;
  19. }
  20. const tarGzUrl = await span.traceChildAsync('get tar.gz url', async () => {
  21. const resp = await requestWithLog(GITHUB_CODELOAD_URL, { method: 'HEAD' });
  22. if (resp.statusCode !== 200) {
  23. console.warn('Download previous build from GitHub failed! Status:', resp.statusCode);
  24. console.warn('Switch to GitLab');
  25. return GITLAB_CODELOAD_URL;
  26. }
  27. return GITHUB_CODELOAD_URL;
  28. });
  29. return span.traceChildAsync('download & extract previoud build', async () => {
  30. const respBody = undici.pipeline(
  31. tarGzUrl,
  32. {
  33. method: 'GET',
  34. headers: {
  35. 'User-Agent': 'curl/8.9.1',
  36. // https://github.com/unjs/giget/issues/97
  37. // https://gitlab.com/gitlab-org/gitlab/-/commit/50c11f278d18fe1f3fb12eb595067216bb58ade2
  38. 'sec-fetch-mode': 'same-origin'
  39. },
  40. // Allow redirects by default
  41. maxRedirections: 5
  42. },
  43. ({ statusCode, body }) => {
  44. if (statusCode !== 200) {
  45. console.warn('Download previous build failed! Status:', statusCode);
  46. if (statusCode === 404) {
  47. throw new Error('Download previous build failed! 404');
  48. }
  49. }
  50. return body;
  51. }
  52. // by default, undici.pipeline returns a duplex stream (for POST/PUT)
  53. // Since we are using GET, we need to end the write immediately
  54. ).end();
  55. const pathPrefix = 'ruleset.skk.moe-master/';
  56. const gunzip = zlib.createGunzip();
  57. const extract = tarExtract(
  58. PUBLIC_DIR,
  59. {
  60. ignore(_: string, header?: TarEntryHeaders) {
  61. if (header) {
  62. if (header.type !== 'file' && header.type !== 'directory') {
  63. return true;
  64. }
  65. if (header.type === 'file' && path.extname(header.name) === '.ts') {
  66. return true;
  67. }
  68. }
  69. return false;
  70. },
  71. map(header) {
  72. header.name = header.name.replace(pathPrefix, '');
  73. return header;
  74. }
  75. }
  76. );
  77. return pipeline(
  78. respBody,
  79. gunzip,
  80. extract
  81. );
  82. });
  83. });