123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- const url_1 = require("url");
- const degenerator_1 = require("degenerator");
- const dateRange_1 = __importDefault(require("./dateRange"));
- const dnsDomainIs_1 = __importDefault(require("./dnsDomainIs"));
- const dnsDomainLevels_1 = __importDefault(require("./dnsDomainLevels"));
- const dnsResolve_1 = __importDefault(require("./dnsResolve"));
- const isInNet_1 = __importDefault(require("./isInNet"));
- const isPlainHostName_1 = __importDefault(require("./isPlainHostName"));
- const isResolvable_1 = __importDefault(require("./isResolvable"));
- const localHostOrDomainIs_1 = __importDefault(require("./localHostOrDomainIs"));
- const myIpAddress_1 = __importDefault(require("./myIpAddress"));
- const shExpMatch_1 = __importDefault(require("./shExpMatch"));
- const timeRange_1 = __importDefault(require("./timeRange"));
- const weekdayRange_1 = __importDefault(require("./weekdayRange"));
- function createPacResolver(_str, _opts = {}) {
- const str = Buffer.isBuffer(_str) ? _str.toString('utf8') : _str;
-
- const sandbox = Object.assign(Object.assign({}, createPacResolver.sandbox), _opts.sandbox);
- const opts = Object.assign(Object.assign({ filename: 'proxy.pac' }, _opts), { sandbox });
-
- const names = Object.keys(sandbox).filter((k) => isAsyncFunction(sandbox[k]));
-
- const resolver = degenerator_1.compile(str, 'FindProxyForURL', names, opts);
- function FindProxyForURL(url, _host, _callback) {
- let host = null;
- let callback = null;
- if (typeof _callback === 'function') {
- callback = _callback;
- }
- if (typeof _host === 'string') {
- host = _host;
- }
- else if (typeof _host === 'function') {
- callback = _host;
- }
- if (!host) {
- host = url_1.parse(url).hostname;
- }
- if (!host) {
- throw new TypeError('Could not determine `host`');
- }
- const promise = resolver(url, host);
- if (typeof callback === 'function') {
- toCallback(promise, callback);
- }
- else {
- return promise;
- }
- }
- Object.defineProperty(FindProxyForURL, 'toString', {
- value: () => resolver.toString(),
- enumerable: false,
- });
- return FindProxyForURL;
- }
- (function (createPacResolver) {
- createPacResolver.sandbox = Object.freeze({
- alert: (message = '') => console.log('%s', message),
- dateRange: dateRange_1.default,
- dnsDomainIs: dnsDomainIs_1.default,
- dnsDomainLevels: dnsDomainLevels_1.default,
- dnsResolve: dnsResolve_1.default,
- isInNet: isInNet_1.default,
- isPlainHostName: isPlainHostName_1.default,
- isResolvable: isResolvable_1.default,
- localHostOrDomainIs: localHostOrDomainIs_1.default,
- myIpAddress: myIpAddress_1.default,
- shExpMatch: shExpMatch_1.default,
- timeRange: timeRange_1.default,
- weekdayRange: weekdayRange_1.default,
- });
- })(createPacResolver || (createPacResolver = {}));
- function toCallback(promise, callback) {
- promise.then((rtn) => callback(null, rtn), callback);
- }
- function isAsyncFunction(v) {
- if (typeof v !== 'function')
- return false;
-
- if (v.constructor.name === 'AsyncFunction')
- return true;
-
- if (String(v).indexOf('__awaiter(') !== -1)
- return true;
-
- return Boolean(v.async);
- }
- module.exports = createPacResolver;
|