index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.sandbox = exports.createPacResolver = void 0;
  7. const degenerator_1 = require("degenerator");
  8. /**
  9. * Built-in PAC functions.
  10. */
  11. const dateRange_1 = __importDefault(require("./dateRange"));
  12. const dnsDomainIs_1 = __importDefault(require("./dnsDomainIs"));
  13. const dnsDomainLevels_1 = __importDefault(require("./dnsDomainLevels"));
  14. const dnsResolve_1 = __importDefault(require("./dnsResolve"));
  15. const isInNet_1 = __importDefault(require("./isInNet"));
  16. const isPlainHostName_1 = __importDefault(require("./isPlainHostName"));
  17. const isResolvable_1 = __importDefault(require("./isResolvable"));
  18. const localHostOrDomainIs_1 = __importDefault(require("./localHostOrDomainIs"));
  19. const myIpAddress_1 = __importDefault(require("./myIpAddress"));
  20. const shExpMatch_1 = __importDefault(require("./shExpMatch"));
  21. const timeRange_1 = __importDefault(require("./timeRange"));
  22. const weekdayRange_1 = __importDefault(require("./weekdayRange"));
  23. /**
  24. * Returns an asynchronous `FindProxyForURL()` function
  25. * from the given JS string (from a PAC file).
  26. */
  27. function createPacResolver(qjs, _str, _opts = {}) {
  28. const str = Buffer.isBuffer(_str) ? _str.toString('utf8') : _str;
  29. // The sandbox to use for the `vm` context.
  30. const context = {
  31. ...exports.sandbox,
  32. ..._opts.sandbox,
  33. };
  34. // Construct the array of async function names to add `await` calls to.
  35. const names = Object.keys(context).filter((k) => isAsyncFunction(context[k]));
  36. const opts = {
  37. filename: 'proxy.pac',
  38. names,
  39. ..._opts,
  40. sandbox: context,
  41. };
  42. // Compile the JS `FindProxyForURL()` function into an async function.
  43. const resolver = (0, degenerator_1.compile)(qjs, str, 'FindProxyForURL', opts);
  44. function FindProxyForURL(url, _host) {
  45. const urlObj = typeof url === 'string' ? new URL(url) : url;
  46. const host = _host || urlObj.hostname;
  47. if (!host) {
  48. throw new TypeError('Could not determine `host`');
  49. }
  50. return resolver(urlObj.href, host);
  51. }
  52. Object.defineProperty(FindProxyForURL, 'toString', {
  53. value: () => resolver.toString(),
  54. enumerable: false,
  55. });
  56. return FindProxyForURL;
  57. }
  58. exports.createPacResolver = createPacResolver;
  59. exports.sandbox = Object.freeze({
  60. alert: (message = '') => console.log('%s', message),
  61. dateRange: dateRange_1.default,
  62. dnsDomainIs: dnsDomainIs_1.default,
  63. dnsDomainLevels: dnsDomainLevels_1.default,
  64. dnsResolve: dnsResolve_1.default,
  65. isInNet: isInNet_1.default,
  66. isPlainHostName: isPlainHostName_1.default,
  67. isResolvable: isResolvable_1.default,
  68. localHostOrDomainIs: localHostOrDomainIs_1.default,
  69. myIpAddress: myIpAddress_1.default,
  70. shExpMatch: shExpMatch_1.default,
  71. timeRange: timeRange_1.default,
  72. weekdayRange: weekdayRange_1.default,
  73. });
  74. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  75. function isAsyncFunction(v) {
  76. if (typeof v !== 'function')
  77. return false;
  78. // Native `AsyncFunction`
  79. if (v.constructor.name === 'AsyncFunction')
  80. return true;
  81. // TypeScript compiled
  82. if (String(v).indexOf('__awaiter(') !== -1)
  83. return true;
  84. // Legacy behavior - set `async` property on the function
  85. return Boolean(v.async);
  86. }
  87. //# sourceMappingURL=index.js.map