GithubHelp home page GithubHelp logo

tcollinsworth / axios-cached-dns-resolve Goto Github PK

View Code? Open in Web Editor NEW
37.0 37.0 32.0 378 KB

Uses async dns.resolve instead of synchronous dns.lookup, optional cache, and periodic background refreshing

License: MIT License

JavaScript 100.00%

axios-cached-dns-resolve's People

Contributors

dependabot[bot] avatar jbrunken avatar matrec4 avatar mf-pherlihy avatar nikhilverma avatar seankarson avatar tcollinsworth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

axios-cached-dns-resolve's Issues

Support for hostfile

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

getDnsCacheEntries always returning empty object

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":{}}

image

image

Error: Unable to resolve module dns from axios-cached-dns-resolve.js

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.

Can I get similar functionality from https://www.npmjs.com/package/dnscache?

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'

Export the library as a CommonJS module

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)

SyntaxError: Unexpected token 'export'

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.

[Transfer issues from Uptime Kuma] axios-cached-dns-resolve does not work well in some local networks like Tailscale/Wireguard.

Description

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

Related issues

  1. It does not resolve local domain names correctly.
    louislam/uptime-kuma#1821

  2. It does not respect /etc/hosts
    louislam/uptime-kuma#1822

👟 Reproduction steps

  1. Prepare a tailscale/wireguard network
  2. Make a request using axios-cached-dns-resolve and using a url with custom domain such as http://my-server:8096

👀 Expected behavior

It should be working like a normal axios.

😓 Actual Behavior

In my case, it is connection timeout. Someone got ECONNREFUSED error.

Version

axios-cached-dns-resolve: 3.1.0
Node.js 16

my file is .mjs but I get the following

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

Unable to use in ESM project (Node 20)

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.

When use in electron/web isomorphic code,it need to introduce on demand

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?

Syntax error with version 1.x.x

Hi guys,
I'm looking into using you library, but I'm having issues running 1.x.x versions.

Source code

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;
};

How to reproduce

In package.json define "axios-cached-dns-resolve": "^0.5.2", run npm install and npm start.

  • It works fine.

In package.json define "axios-cached-dns-resolve": "^1.2.1", run npm install and npm start.

  • It fails with following stack trace
/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)

SyntaxError: Unexpected token 'export'

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 ... //
	}
  • Note: This is just an abbreviated piece of code.
    Because it's fine and it's functional and there's no need to post all the code.
  • The only problem and the implementation of axios-cached-dns-resolve.
  • All necessary dependencies are in node_modules.

Please someone help me to solve this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.