Compare commits
2 commits
b94aeb2df2
...
83bce62672
Author | SHA1 | Date | |
---|---|---|---|
83bce62672 | |||
6fd422f2b0 |
1 changed files with 29 additions and 8 deletions
|
@ -1,6 +1,7 @@
|
|||
import * as http from 'node:http';
|
||||
import * as https from 'node:https';
|
||||
import { URL } from 'node:url';
|
||||
import * as dns from 'node:dns';
|
||||
import CacheableLookup from 'cacheable-lookup';
|
||||
import fetch from 'node-fetch';
|
||||
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
|
||||
|
@ -49,11 +50,6 @@ export async function getResponse(_args: {
|
|||
..._args,
|
||||
};
|
||||
|
||||
const controller = new AbortController();
|
||||
setTimeout(() => {
|
||||
controller.abort();
|
||||
}, args.timeout);
|
||||
|
||||
const res = await fetch(args.url, {
|
||||
method: args.method,
|
||||
headers: Object.assign({
|
||||
|
@ -63,7 +59,7 @@ export async function getResponse(_args: {
|
|||
redirect: args.redirect,
|
||||
size: args.size,
|
||||
agent: getAgentByUrl,
|
||||
signal: controller.signal,
|
||||
signal: AbortSignal.timeout(args.timeout),
|
||||
});
|
||||
|
||||
if (
|
||||
|
@ -83,13 +79,38 @@ const cache = new CacheableLookup({
|
|||
lookup: false, // nativeのdns.lookupにfallbackしない
|
||||
});
|
||||
|
||||
// because `cacheable-lookup` is annoying and prefers IPv4 by default,
|
||||
// we need a little wrapper setting some extra options
|
||||
const cacheLookup = (host, _2, _3) => {
|
||||
// do all the weird parameter shenanigans that nodejs's dns.lookup does.
|
||||
let options = {}, callback;
|
||||
if (_3) {
|
||||
options = _2;
|
||||
if (typeof options === 'number') {
|
||||
options = { family: options };
|
||||
}
|
||||
callback = _3;
|
||||
} else {
|
||||
callback = _2;
|
||||
}
|
||||
|
||||
// here come the shenanigans, trying to be careful not to mess up
|
||||
// intentionally different behaviour
|
||||
if (options.family == null && (options.hints ?? 0) === 0) {
|
||||
options.family = 6;
|
||||
options.hints = dns.V4MAPPED;
|
||||
}
|
||||
|
||||
return cache.lookup(host, options, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get http non-proxy agent
|
||||
*/
|
||||
const _http = new http.Agent({
|
||||
keepAlive: true,
|
||||
keepAliveMsecs: 30 * SECOND,
|
||||
lookup: cache.lookup,
|
||||
lookup: cacheLookup,
|
||||
} as http.AgentOptions);
|
||||
|
||||
/**
|
||||
|
@ -98,7 +119,7 @@ const _http = new http.Agent({
|
|||
const _https = new https.Agent({
|
||||
keepAlive: true,
|
||||
keepAliveMsecs: 30 * SECOND,
|
||||
lookup: cache.lookup,
|
||||
lookup: cacheLookup,
|
||||
} as https.AgentOptions);
|
||||
|
||||
const maxSockets = Math.max(256, config.deliverJobConcurrency);
|
||||
|
|
Loading…
Add table
Reference in a new issue