index.js 4.1 KB

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