download-mock-assets.ts 1.5 KB

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