index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 = 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 = 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. (function (createPacResolver) {
  72. createPacResolver.sandbox = Object.freeze({
  73. alert: (message = '') => console.log('%s', message),
  74. dateRange: dateRange_1.default,
  75. dnsDomainIs: dnsDomainIs_1.default,
  76. dnsDomainLevels: dnsDomainLevels_1.default,
  77. dnsResolve: dnsResolve_1.default,
  78. isInNet: isInNet_1.default,
  79. isPlainHostName: isPlainHostName_1.default,
  80. isResolvable: isResolvable_1.default,
  81. localHostOrDomainIs: localHostOrDomainIs_1.default,
  82. myIpAddress: myIpAddress_1.default,
  83. shExpMatch: shExpMatch_1.default,
  84. timeRange: timeRange_1.default,
  85. weekdayRange: weekdayRange_1.default,
  86. });
  87. })(createPacResolver || (createPacResolver = {}));
  88. function toCallback(promise, callback) {
  89. promise.then((rtn) => callback(null, rtn), callback);
  90. }
  91. function isAsyncFunction(v) {
  92. if (typeof v !== 'function')
  93. return false;
  94. // Native `AsyncFunction`
  95. if (v.constructor.name === 'AsyncFunction')
  96. return true;
  97. // TypeScript compiled
  98. if (String(v).indexOf('__awaiter(') !== -1)
  99. return true;
  100. // Legacy behavior - set `async` property on the function
  101. return Boolean(v.async);
  102. }
  103. module.exports = createPacResolver;
  104. //# sourceMappingURL=index.js.map