tcollinsworth / axios-cached-dns-resolve Goto Github PK
View Code? Open in Web Editor NEWUses async dns.resolve instead of synchronous dns.lookup, optional cache, and periodic background refreshing
License: MIT License
Uses async dns.resolve instead of synchronous dns.lookup, optional cache, and periodic background refreshing
License: MIT License
Hi,
Would you be open to supporting linux host files as part of this? It would be great to extend this to support host files which is missing from dns.resolve
.
Cheers,
Andrew
Always getting empty object for getDnsCacheEntries()
Versions
Node: 18.4
axios: "^0.27.2"
axios-cached-dns-resolve: "^3.1.0"
Code:
import 'esm'
const express = require('express')
import axios from 'axios'
import { registerInterceptor, getStats, getDnsCacheEntries } from 'axios-cached-dns-resolve'
const app = express()
const port = 3000
const config = {
disabled: false,
dnsTtlMs: 50000,
cacheGraceExpireMultiplier: 2,
dnsIdleTtlMs: 3600000,
backgroundScanMs: 2400,
dnsCacheSize: 100,
logging: {
name: 'axios-cache-dns-resolve',
// enabled: true,
level: 'info',
// timestamp: true,
prettyPrint: false,
useLevelLabels: true,
},
}
const axiosClient = axios.create(config)
registerInterceptor(axiosClient)
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.get('/get_google', function (req, res) {
const gUrl = 'https://google.com'
axiosClient.get(gUrl).then(gRes=> {
return res.json(gRes.data)
}).catch(err => res.json({error: err}))
})
app.get('/as', getAxiosDnsCacheStatistics)
function getAxiosDnsCacheStatistics(req, resp) {
var stat = {
axiosDnsCacheStatistics: getStats(),
axiosDnsCacheEntries: getDnsCacheEntries()
}
resp.json(stat)
}
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Result after hitting lvh.me:3000/get_google URL
{"axiosDnsCacheStatistics":{"dnsEntries":1,"refreshed":0,"hits":0,"misses":1,"idleExpired":0,"errors":0,"lastError":0,"lastErrorTs":0},"axiosDnsCacheEntries":{}}
when used in electron, emits error TypeError: logger.isLevelEnabled is not a function, seems pino logger in electron not has this method
may be should remove : if (log.isLevelEnabled('debug')) , because logger can handle the log.debug for itself
or check the level without use isLevelEnabled
This error shows up when including this package in my project:
Error: Unable to resolve module dns from (...)\axios-cached-dns-resolve.js: dns could not be found within the project or in these directories:
node_modules\axios-cached-dns-resolve\node_modules
node_modules
I'm guessing the required modules are only present server-side, but I'm using Axios client-side. So I was wondering if it's possible to adapt this package to make it more environment-agnostic.
Of course feel free to close this if you think that doesn't make sense, or if that's not the purpose of this package.
I noticed the default cache size is 100. What's the rationale there?
Shouldn't this be able to potentially hold thousands of entries without any notable impact on memory?
Separately, I'd like an ability to know whenever a cache eviction happens due to the cache filling up
I am already using require('dnscache') in my process. As stated in https://www.npmjs.com/package/dnscache, once that module is enabled, all the subsequent calls to require('dns') are wrapped too.
So, does that mean that I don't need to use axios-cached-dns-resolve? I don't see any calls to require('dns') in axios, so it might be that I still have a problem and need to use axios-cached-dns-resolve instead (or in addition) to 'npm install dnscache'
Thanks for this really useful library! However to use it in most applications it needs to be exported as a CommonJS module. Most bundlers do not process the node_modules
folder so you get errors like this:
export * from './axios-cached-dns-resolve'
^^^^^^
SyntaxError: Unexpected token 'export'
at wrapSafe (internal/modules/cjs/loader.js:1053:16)
at Module._compile (internal/modules/cjs/loader.js:1101:27)
Ideas from this article which Bryan Paulus shared with me.
My typescript project fails to compile, spitting out the following error:
C:\...\node_modules\axios-cached-dns-resolve\index.js:1
export {
^^^^^^
SyntaxError: Unexpected token 'export'
at wrapSafe (internal/modules/cjs/loader.js:1001:16)
at Module._compile (internal/modules/cjs/loader.js:1049:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:101:18)
at Object.<anonymous> (C:\...\build\lib\helpers\network-manager.js:65:34)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
From what I've seen in the past, this was a bug and has been resolved in past versions, however I am using v3.0.6 (latest as of writing), and still encounter this error.
Attempting to add "type": "module"
to my package.json let to unexpected behavior, and isn't a solution for me either.
Uptime Kuma was recently added axios-cached-dns-resolve and was released yesterday. Unfortunately, it seems that it is not working well in local environment like Tailscale/Wireguard. After changing back to the normal axios client without axios-cached-dns-resolve, it is back to normal.
The code changes: louislam/uptime-kuma#1598
It does not resolve local domain names correctly.
louislam/uptime-kuma#1821
It does not respect /etc/hosts
louislam/uptime-kuma#1822
It should be working like a normal axios.
In my case, it is connection timeout. Someone got ECONNREFUSED
error.
axios-cached-dns-resolve: 3.1.0
Node.js 16
I have the type module in package.json, node 14.15.5
import { registerInterceptor } from 'axios-cached-dns-resolve'
^^^^^^^^^^^^^^^^^^^
SyntaxError: Named export 'registerInterceptor' not found. The requested module 'axios-cached-dns-resolve' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:...
I think I know why, it looks like npm is pulling version 3.0.0 while in your package.json I saw 2.0.3 and I set it to 2.0.3 and it now works
This appears to be the same as #37; I am receiving the error:
import { registerInterceptor } from 'axios-cached-dns-resolve';
^^^^^^^^^^^^^^^^^^^
SyntaxError: Named export 'registerInterceptor' not found. The requested module 'axios-cached-dns-resolve' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'axios-cached-dns-resolve';
const { registerInterceptor } = pkg;
However, that advice does not work as this isn't a CJS module. Manually editing the package.json
of axios-cached-dns-resolve
to include "type": "module"
fixes the issue.
Is there a way to use this library without ESM, which has been abandoned for a long time?
hello,
I built an electron and web isomorphic project, and I want to introduced as needed according to the current environment.
like follow:
import axios from 'axios'
const axiosIns= axios.create(config);
if (window.process && window.process.platform) {
const dnsResolve = require('axios-cached-dns-resolve');
dnsResolve.registerInterceptor(axiosIns);
}
axiosIns.get('xxx');
Can this usage be supported?
Hi guys,
I'm looking into using you library, but I'm having issues running 1.x.x versions.
const axios = require('axios');
import { registerInterceptor } from 'axios-cached-dns-resolve'
import config from '../config';
export default (baseURL: string) => {
const axiosClient = axios.create({ ...{ baseURL: baseURL }, ...config.axios, ...config.axiosDNS });
registerInterceptor(axiosClient);
return axiosClient;
};
In package.json
define "axios-cached-dns-resolve": "^0.5.2"
, run npm install
and npm start
.
In package.json
define "axios-cached-dns-resolve": "^1.2.1"
, run npm install
and npm start
.
/Users/filipbozic/workspace/infra.sauron.eye/node_modules/axios-cached-dns-resolve/index.js:1
export * from './axios-cached-dns-resolve'
^^^^^^
SyntaxError: Unexpected token 'export'
at wrapSafe (node:internal/modules/cjs/loader:1024:16)
at Module._compile (node:internal/modules/cjs/loader:1072:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Object.require.extensions.<computed> [as .js] (/Users/filipbozic/workspace/infra.sauron.eye/node_modules/ts-node/src/index.ts:1045:43)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:997:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.<anonymous> (/Users/filipbozic/workspace/infra.sauron.eye/src/httpclient/index.ts:2:1)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
Hello
I have a script that makes many axios requests and it causes errors like AxiosError: getaddrinfo EAI_AGAIN
due to many DNS requests.
Then I saw on the internet that I could use this axios-cached-dns-resolve
module to cache DNS in axios.
But whenever I implement the code and run my script it returns the error:
\axios-cached-dns-resolve\index.js:1
export {
^^^^^^
SyntaxError: Unexpected token 'export'
I do not understand where is the fault.
It looks like the official help page is confusing with import examples.
My script does not accept importation because it is a single script like myscript.js
where I put all the code to be executed.
In myscript.js
I avoided using the //import
as my execution returns the error:
import { registerInterceptor } from 'axios-cached-dns-resolve'
^^^^^^
SyntaxError: Cannot use import statement outside a module
Instead of import
I used const
:
const { registerInterceptor } = require("axios-cached-dns-resolve");
Still I get the error:
\axios-cached-dns-resolve\index.js:1
export {
^^^^^^
SyntaxError: Unexpected token 'export'
It seems to me that the module is broken or I am doing something wrong.
I need help.
My example code: myscript.js
//import { registerInterceptor } from 'axios-cached-dns-resolve'
const { registerInterceptor } = require("axios-cached-dns-resolve");
const axiosClient = axios.create(config);
function func_01(a, b, c) {
url = `https://domain.com`;
//axios
registerInterceptor(axiosClient)
.get(url)
// ... mais código aqui ... //
}
axios-cached-dns-resolve
.node_modules
.Please someone help me to solve this.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.