Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
cacheable-lookup
Advanced tools
The cacheable-lookup npm package is designed to enhance the Node.js DNS module with caching capabilities. It provides a way to cache DNS lookup results in order to improve performance for repeated DNS queries. This is particularly useful for applications making numerous requests to the same domains, as it reduces the number of DNS queries that need to be performed over the network.
Caching DNS lookups
This feature allows you to cache DNS lookups to improve performance. The code sample demonstrates how to perform a DNS lookup for 'example.com' and cache the result.
const CacheableLookup = require('cacheable-lookup');
const cacheable = new CacheableLookup();
cacheable.lookup('example.com', (err, address, family) => {
console.log(address);
});
Integration with http.Agent
This feature demonstrates how cacheable-lookup can be integrated with Node.js http.Agent to automatically use cached DNS lookups for HTTP requests. This can significantly reduce DNS lookup times for repeated requests to the same domain.
const CacheableLookup = require('cacheable-lookup');
const http = require('http');
const cacheable = new CacheableLookup();
const agent = new http.Agent({
lookup: cacheable.lookup
});
http.get('http://example.com', { agent }, (res) => {
// Handle response
});
dns-cache is a simple DNS cache module. It provides basic caching functionalities similar to cacheable-lookup but lacks some of the advanced features and customizability options such as integration with http.Agent.
A cacheable
dns.lookup(…)
that respects TTL :tada:
Making lots of HTTP requests? You can save some time by caching DNS lookups :zap:
lookup
optionconst http = require('http');
const CacheableLookup = require('cacheable-lookup');
const cacheable = new CacheableLookup();
http.get('https://example.com', {lookup: cacheable.lookup}, response => {
// Handle the response here
});
const http = require('http');
const CacheableLookup = require('cacheable-lookup');
const cacheable = new CacheableLookup();
cacheable.install(http.globalAgent);
http.get('https://example.com', response => {
// Handle the response here
});
Returns a new instance of CacheableLookup
.
Type: object
Default: {}
Options used to cache the DNS lookups.
Type: number
Default: Infinity
Limits the cache time (TTL in seconds).
If set to 0
, it will make a new DNS query each time.
Type: dns.Resolver | dns.promises.Resolver
Default: new dns.promises.Resolver()
An instance of DNS Resolver used to make DNS queries.
Type: string
Default: undefined
(OS-specific)
The full path to the hosts
file. Set this to false
to prevent loading entries from the hosts
file.
Type: object
Type: string
The IP address (can be an IPv4 or IPv6 address).
Type: number
The IP family (4
or 6
).
Type: number
Note: This is not present when using the native dns.lookup(...)
!
The timestamp (Date.now() + ttl * 1000
) when the entry expires.
Note: This is not present when using the native dns.lookup(...)
!
The time in seconds for its lifetime.
When options.all
is false
, then callback(error, address, family, expires, ttl)
is called.
When options.all
is true
, then callback(error, entries)
is called.
Type: Array
DNS servers used to make the query. Can be overridden - then the new servers will be used.
The asynchronous version of dns.lookup(…)
.
Returns an entry object.
If options.all
is true, returns an array of entry objects.
Note: If entry(ies) were not found, it will return undefined
by default.
Type: string
Type: object
The same as the dns.lookup(…)
options.
Type: boolean
Default: true
If set to false
and it gets no match, it will return undefined
.
If set to true
and it gets no match, it will throw ENOTFOUND
error.
Note: This option is meant only for the asynchronous implementation! The callback version will always pass an error if no match found.
An asynchronous function which returns cached DNS lookup entries.
This is the base for lookupAsync(hostname, options)
and lookup(hostname, options, callback)
.
Note: This function has no options.
Returns an array of objects with address
, family
, ttl
and expires
properties.
An asynchronous function which makes two DNS queries: A and AAAA. The result is cached.
This is used by query(hostname)
if no entry in the database is present.
Returns an array of objects with address
, family
, ttl
and expires
properties.
Removes outdated entries.
Updates interface info. For example, you need to run this when you plug or unplug your WiFi driver.
Note: Running updateInterfaceInfo()
will also trigger clear()
!
Clears the cache.
See the benchmarks (queries localhost
, performed on i7-7700k):
CacheableLookup#lookupAsync x 4,095,922 ops/sec ±1.01% (84 runs sampled)
CacheableLookup#lookupAsync.all x 4,472,817 ops/sec ±0.67% (88 runs sampled)
CacheableLookup#lookupAsync.all.ADDRCONFIG x 3,713,702 ops/sec ±0.66% (85 runs sampled)
CacheableLookup#lookup x 3,332,170 ops/sec ±0.50% (82 runs sampled)
CacheableLookup#lookup.all x 3,303,159 ops/sec ±0.61% (84 runs sampled)
CacheableLookup#lookup.all.ADDRCONFIG x 2,851,815 ops/sec ±0.82% (84 runs sampled)
CacheableLookup#lookupAsync - zero TTL x 226 ops/sec ±3.55% (56 runs sampled)
CacheableLookup#lookup - zero TTL x 228 ops/sec ±2.48% (62 runs sampled)
dns#resolve4 x 346 ops/sec ±3.58% (55 runs sampled)
dns#lookup x 20,368 ops/sec ±38.31% (53 runs sampled)
dns#lookup.all x 13,529 ops/sec ±31.35% (29 runs sampled)
dns#lookup.all.ADDRCONFIG x 6,211 ops/sec ±22.92% (26 runs sampled)
Fastest is CacheableLookup#lookupAsync.all
The package is based on dns.resolve4(…)
and dns.resolve6(…)
.
It is not possible to use
dns.lookup(…)
because underlying calls like getaddrinfo have no concept of servers or TTL (caching is done on OS level instead).
MIT
FAQs
A cacheable dns.lookup(…) that respects TTL
We found that cacheable-lookup demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.