build-internal-reverse-chn-cidr.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import path from 'node:path';
  2. import { task } from './trace';
  3. import { getChnCidrPromise } from './build-chn-cidr';
  4. // import { RESERVED_IPV4_CIDR, NON_CN_CIDR_INCLUDED_IN_CHNROUTE } from './constants/cidr';
  5. import fs from 'node:fs';
  6. import { OUTPUT_INTERNAL_DIR } from './constants/dir';
  7. import { asyncWriteToStream } from 'foxts/async-write-to-stream';
  8. import { mkdirp } from './lib/misc';
  9. // import { appendArrayInPlace } from './lib/append-array-in-place';
  10. import Worktank from 'worktank';
  11. const pool = new Worktank({
  12. name: 'build-internal-reverse-chn-cidr',
  13. size: 1,
  14. timeout: 10000, // The maximum number of milliseconds to wait for the result from the worker, if exceeded the worker is terminated and the execution promise rejects
  15. warmup: true,
  16. autoterminate: 30000, // The interval of milliseconds at which to check if the pool can be automatically terminated, to free up resources, workers will be spawned up again if needed
  17. env: {},
  18. methods: {
  19. // eslint-disable-next-line object-shorthand -- workertank
  20. getreversedCidr: async function (cidr: string[], importMetaUrl: string): Promise<string[]> {
  21. // TODO: createRequire is a temporary workaround for https://github.com/nodejs/node/issues/51956
  22. const { default: module } = await import('node:module');
  23. const __require = module.createRequire(importMetaUrl);
  24. const { exclude, merge } = __require('fast-cidr-tools');
  25. const { RESERVED_IPV4_CIDR, NON_CN_CIDR_INCLUDED_IN_CHNROUTE } = __require('./constants/cidr');
  26. const { appendArrayInPlace } = __require('./lib/append-array-in-place');
  27. return merge(
  28. appendArrayInPlace(
  29. exclude(
  30. ['0.0.0.0/0'],
  31. RESERVED_IPV4_CIDR.concat(cidr),
  32. true
  33. ),
  34. // https://github.com/misakaio/chnroutes2/issues/25
  35. NON_CN_CIDR_INCLUDED_IN_CHNROUTE
  36. ),
  37. true
  38. );
  39. }
  40. }
  41. });
  42. export const buildInternalReverseChnCIDR = task(require.main === module, __filename)(async (span) => {
  43. const [cidr] = await span.traceChildPromise('download chnroutes2', getChnCidrPromise());
  44. const reversedCidr = await span.traceChildAsync('build reversed chn cidr', async () => {
  45. const reversedCidr = await pool.exec(
  46. 'getreversedCidr',
  47. [cidr, import.meta.url]
  48. );
  49. pool.terminate();
  50. return reversedCidr;
  51. });
  52. const outputFile = path.join(OUTPUT_INTERNAL_DIR, 'reversed-chn-cidr.txt');
  53. await mkdirp(OUTPUT_INTERNAL_DIR);
  54. const writeStream = fs.createWriteStream(outputFile);
  55. for (const line of reversedCidr) {
  56. const p = asyncWriteToStream(writeStream, line + '\n');
  57. if (p) {
  58. // eslint-disable-next-line no-await-in-loop -- stream high water mark
  59. await p;
  60. }
  61. }
  62. await asyncWriteToStream(writeStream, '\n');
  63. writeStream.end();
  64. });