mirror of
https://github.com/tenrok/axios.git
synced 2026-06-14 18:42:33 +03:00
feat(dns): added support for a custom lookup function; (#5339)
This commit is contained in:
+15
-1
@@ -23,6 +23,7 @@ import EventEmitter from 'events';
|
||||
import formDataToStream from "../helpers/formDataToStream.js";
|
||||
import readBlob from "../helpers/readBlob.js";
|
||||
import ZlibHeaderTransformStream from '../helpers/ZlibHeaderTransformStream.js';
|
||||
import callbackify from "../helpers/callbackify.js";
|
||||
|
||||
const zlibOptions = {
|
||||
flush: zlib.constants.Z_SYNC_FLUSH,
|
||||
@@ -146,13 +147,24 @@ const wrapAsync = (asyncExecutor) => {
|
||||
/*eslint consistent-return:0*/
|
||||
export default isHttpAdapterSupported && function httpAdapter(config) {
|
||||
return wrapAsync(async function dispatchHttpRequest(resolve, reject, onDone) {
|
||||
let {data} = config;
|
||||
let {data, lookup, family} = config;
|
||||
const {responseType, responseEncoding} = config;
|
||||
const method = config.method.toUpperCase();
|
||||
let isDone;
|
||||
let rejected = false;
|
||||
let req;
|
||||
|
||||
if (lookup && utils.isAsyncFn(lookup)) {
|
||||
lookup = callbackify(lookup, (entry) => {
|
||||
if(utils.isString(entry)) {
|
||||
entry = [entry, entry.indexOf('.') < 0 ? 6 : 4]
|
||||
} else if (!utils.isArray(entry)) {
|
||||
throw new TypeError('lookup async function must return an array [ip: string, family: number]]')
|
||||
}
|
||||
return entry;
|
||||
})
|
||||
}
|
||||
|
||||
// temporary internal emitter until the AxiosRequest class will be implemented
|
||||
const emitter = new EventEmitter();
|
||||
|
||||
@@ -378,6 +390,8 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
|
||||
agents: { http: config.httpAgent, https: config.httpsAgent },
|
||||
auth,
|
||||
protocol,
|
||||
family,
|
||||
lookup,
|
||||
beforeRedirect: dispatchBeforeRedirect,
|
||||
beforeRedirects: {}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import utils from "../utils.js";
|
||||
|
||||
const callbackify = (fn, reducer) => {
|
||||
return utils.isAsyncFn(fn) ? function (...args) {
|
||||
const cb = args.pop();
|
||||
fn.apply(this, args).then((value) => {
|
||||
try {
|
||||
reducer ? cb(null, ...reducer(value)) : cb(null, value);
|
||||
} catch (err) {
|
||||
cb(err);
|
||||
}
|
||||
}, cb);
|
||||
} : fn;
|
||||
}
|
||||
|
||||
export default callbackify;
|
||||
+15
-1
@@ -662,6 +662,15 @@ const toJSONObject = (obj) => {
|
||||
return visit(obj, 0);
|
||||
}
|
||||
|
||||
const [isPlainFunction, isAsyncFn, isGeneratorFn, isAsyncGeneratorFn] = (
|
||||
(...fns) => fns.map(
|
||||
({constructor})=> (thing) => thing && typeof thing === 'function' && thing.constructor === constructor
|
||||
)
|
||||
)(()=> {}, async()=>{}, function*(){}, async function*(){});
|
||||
|
||||
const isThenable = (thing) =>
|
||||
thing && (isObject(thing) || isFunction(thing)) && isFunction(thing.then) && isFunction(thing.catch);
|
||||
|
||||
export default {
|
||||
isArray,
|
||||
isArrayBuffer,
|
||||
@@ -711,5 +720,10 @@ export default {
|
||||
ALPHABET,
|
||||
generateString,
|
||||
isSpecCompliantForm,
|
||||
toJSONObject
|
||||
toJSONObject,
|
||||
isAsyncFn,
|
||||
isGeneratorFn,
|
||||
isAsyncGeneratorFn,
|
||||
isPlainFunction,
|
||||
isThenable
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user