better-lookup
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -183,19 +183,33 @@ "use strict"; | ||
function install(agent, family = 0) { | ||
if (_createConnection in agent) | ||
return agent; | ||
else if (typeof agent["createConnection"] !== "function") | ||
throw new TypeError("Cannot install lookup function on the given agent"); | ||
agent[_createConnection] = agent["createConnection"]; | ||
agent["createConnection"] = function (options, callback) { | ||
let tryAttach = (options) => { | ||
if (!("lookup" in options)) { | ||
options["lookup"] = function (hostname, options, callback) { | ||
options["lookup"] = function (hostname, options, cb) { | ||
var _a; | ||
return lookup(hostname, (_a = options["family"]) !== null && _a !== void 0 ? _a : family, callback); | ||
return lookup(hostname, (_a = options["family"]) !== null && _a !== void 0 ? _a : family, cb); | ||
}; | ||
} | ||
return agent[_createConnection](options, callback); | ||
}; | ||
return agent; | ||
if (typeof agent.createConnection === "function") { | ||
if (!(_createConnection in agent)) { | ||
agent[_createConnection] = agent.createConnection; | ||
agent.createConnection = function (options, callback) { | ||
tryAttach(options); | ||
return agent[_createConnection](options, callback); | ||
}; | ||
} | ||
return agent; | ||
} | ||
else if (isHttpsProxyAgent(agent)) { | ||
tryAttach(agent.proxy); | ||
return agent; | ||
} | ||
else { | ||
throw new TypeError("Cannot install lookup function on the given agent"); | ||
} | ||
} | ||
exports.install = install; | ||
function isHttpsProxyAgent(agent) { | ||
return agent.constructor.name === "HttpsProxyAgent" | ||
&& typeof agent.proxy === "object"; | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "better-lookup", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A better async DNS lookup function for Node.js that implements atomic cache operation.", | ||
@@ -30,2 +30,3 @@ "main": "dist/index.js", | ||
"@types/node": "^14.0.26", | ||
"https-proxy-agent": "^5.0.0", | ||
"mocha": "^8.0.1" | ||
@@ -41,2 +42,2 @@ }, | ||
} | ||
} | ||
} |
import * as dns from "dns"; | ||
import * as fs from "fs-extra"; | ||
import { isIP } from "net"; | ||
import { isIP, Socket } from "net"; | ||
import { Agent as HttpAgent } from "http"; | ||
@@ -241,12 +241,8 @@ import { Agent as HttpsAgent } from "https"; | ||
export function install<T extends HttpAgent | HttpsAgent>( | ||
agent: T, | ||
agent: T & { | ||
createConnection?: (options: any, callback: Function) => Socket | ||
}, | ||
family: 0 | 4 | 6 = 0 | ||
): T { | ||
if (_createConnection in agent) | ||
return agent; | ||
else if (typeof agent["createConnection"] !== "function") | ||
throw new TypeError("Cannot install lookup function on the given agent"); | ||
agent[_createConnection] = agent["createConnection"]; | ||
agent["createConnection"] = function (options: any, callback: Function) { | ||
let tryAttach = (options: Record<string, any>) => { | ||
if (!("lookup" in options)) { | ||
@@ -256,12 +252,30 @@ options["lookup"] = function ( | ||
options: any, | ||
callback: LookupCallback<string> | ||
cb: LookupCallback<string> | ||
) { | ||
return lookup(hostname, options["family"] ?? family, callback); | ||
return lookup(hostname, options["family"] ?? family, cb); | ||
}; | ||
} | ||
return agent[_createConnection](options, callback); | ||
}; | ||
return agent; | ||
if (typeof agent.createConnection === "function") { | ||
if (!(_createConnection in agent)) { | ||
agent[_createConnection] = agent.createConnection; | ||
agent.createConnection = function (options, callback) { | ||
tryAttach(options); | ||
return agent[_createConnection](options, callback); | ||
}; | ||
} | ||
return agent; | ||
} else if (isHttpsProxyAgent(agent)) { | ||
tryAttach(agent.proxy); | ||
return agent; | ||
} else { | ||
throw new TypeError("Cannot install lookup function on the given agent"); | ||
} | ||
} | ||
function isHttpsProxyAgent(agent: any): agent is { proxy: Record<string, any> } { | ||
return agent.constructor.name === "HttpsProxyAgent" | ||
&& typeof agent.proxy === "object"; | ||
} |
Sorry, the diff of this file is not supported yet
28079
489
3