download-mock-assets.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { task } from './trace';
  2. import path from 'node:path';
  3. import fs from 'node:fs';
  4. import fsp from 'node:fs/promises';
  5. import { Readable } from 'node:stream';
  6. import { pipeline } from 'node:stream/promises';
  7. import { fetchWithRetry } from './lib/fetch-retry';
  8. import { OUTPUT_MOCK_DIR } from './constants/dir';
  9. const ASSETS_LIST = {
  10. 'www-google-analytics-com_ga.js': 'https://raw.githubusercontent.com/AdguardTeam/Scriptlets/master/dist/redirect-files/google-analytics-ga.js',
  11. 'www-googletagservices-com_gpt.js': 'https://raw.githubusercontent.com/AdguardTeam/Scriptlets/master/dist/redirect-files/googletagservices-gpt.js',
  12. 'www-google-analytics-com_analytics.js': 'https://raw.githubusercontent.com/AdguardTeam/Scriptlets/master/dist/redirect-files/google-analytics.js',
  13. 'www-googlesyndication-com_adsbygoogle.js': 'https://raw.githubusercontent.com/AdguardTeam/Scriptlets/master/dist/redirect-files/googlesyndication-adsbygoogle.js',
  14. 'amazon-adsystem-com_amazon-apstag.js': 'https://raw.githubusercontent.com/AdguardTeam/Scriptlets/master/dist/redirect-files/amazon-apstag.js'
  15. } as const;
  16. export const downloadMockAssets = task(require.main === module, __filename)((span) => Promise.all(Object.entries(ASSETS_LIST).map(
  17. ([filename, url]) => span
  18. .traceChildAsync(url, async () => {
  19. const res = await fetchWithRetry(url);
  20. const src = path.join(OUTPUT_MOCK_DIR, filename);
  21. if (!res.body) {
  22. throw new Error(`Empty body from ${url}`);
  23. }
  24. await fsp.mkdir(OUTPUT_MOCK_DIR, { recursive: true });
  25. return pipeline(
  26. Readable.fromWeb(res.body),
  27. fs.createWriteStream(src, 'utf-8')
  28. );
  29. })
  30. )));