urllib
Version:
Help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more. Base undici fetch API.
62 lines • 2.66 kB
JavaScript
;
var _HttpAgent_checkAddress;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpAgent = void 0;
const tslib_1 = require("tslib");
const dns_1 = tslib_1.__importDefault(require("dns"));
const net_1 = require("net");
const undici_1 = require("undici");
class IllegalAddressError extends Error {
constructor(hostname, ip, family) {
const message = 'illegal address';
super(message);
this.name = this.constructor.name;
this.hostname = hostname;
this.ip = ip;
this.family = family;
Error.captureStackTrace(this, this.constructor);
}
}
class HttpAgent extends undici_1.Agent {
constructor(options) {
var _a;
/* eslint node/prefer-promises/dns: off*/
const _lookup = (_a = options.lookup) !== null && _a !== void 0 ? _a : dns_1.default.lookup;
const lookup = (hostname, dnsOptions, callback) => {
_lookup(hostname, dnsOptions, (err, address, family) => {
if (err)
return callback(err, address, family);
if (options.checkAddress && !options.checkAddress(address, family)) {
err = new IllegalAddressError(hostname, address, family);
}
callback(err, address, family);
});
};
super({
connect: { ...options.connect, lookup },
});
_HttpAgent_checkAddress.set(this, void 0);
tslib_1.__classPrivateFieldSet(this, _HttpAgent_checkAddress, options.checkAddress, "f");
}
dispatch(options, handler) {
if (tslib_1.__classPrivateFieldGet(this, _HttpAgent_checkAddress, "f") && options.origin) {
const originUrl = typeof options.origin === 'string' ? new URL(options.origin) : options.origin;
let hostname = originUrl.hostname;
// [2001:db8:2de::e13] => 2001:db8:2de::e13
if (hostname.startsWith('[') && hostname.endsWith(']')) {
hostname = hostname.substring(1, hostname.length - 1);
}
const family = (0, net_1.isIP)(hostname);
if (family === 4 || family === 6) {
// if request hostname is ip, custom lookup won't excute
if (!tslib_1.__classPrivateFieldGet(this, _HttpAgent_checkAddress, "f").call(this, hostname, family)) {
throw new IllegalAddressError(hostname, hostname, family);
}
}
}
return super.dispatch(options, handler);
}
}
exports.HttpAgent = HttpAgent;
_HttpAgent_checkAddress = new WeakMap();
//# sourceMappingURL=HttpAgent.js.map