Skip to content

Commit

Permalink
Require Node.js 14 and move to ESM (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Sep 27, 2022
1 parent ab8d286 commit 08a6478
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 86 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 13.x]
node-version: [18, 16, 14]
os: [ubuntu-latest, windows-latest]

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
Expand Down
44 changes: 20 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Making lots of HTTP requests? You can save some time by caching DNS lookups :zap
### Using the `lookup` option

```js
const http = require('http');
const CacheableLookup = require('cacheable-lookup');
import http from 'node:http';
import CacheableLookup from 'cacheable-lookup';

const cacheable = new CacheableLookup();

Expand All @@ -27,9 +27,9 @@ http.get('http://example.com', {lookup: cacheable.lookup}, response => {
### Attaching CacheableLookup to an Agent

```js
const http = require('http');
const https = require('https');
const CacheableLookup = require('cacheable-lookup');
import http from 'node:http';
import https from 'node:https';
import CacheableLookup from 'cacheable-lookup';

const cacheable = new CacheableLookup();

Expand All @@ -49,14 +49,14 @@ Returns a new instance of `CacheableLookup`.

#### options

Type: `object`<br>
Type: `object`\
Default: `{}`

Options used to cache the DNS lookups.

##### cache

Type: `Map` | [`Keyv`](https://github.com/lukechilds/keyv/)<br>
Type: `Map` | [`Keyv`](https://github.com/lukechilds/keyv/)\
Default: `new Map()`

Custom cache instance. If `undefined`, it will create a new one.
Expand All @@ -66,9 +66,9 @@ Custom cache instance. If `undefined`, it will create a new one.
**Tip**: [`QuickLRU`](https://github.com/sindresorhus/quick-lru) is fully compatible with the Map API, you can use it to limit the amount of cached entries. Example:

```js
const http = require('http');
const CacheableLookup = require('cacheable-lookup');
const QuickLRU = require('quick-lru');
import http from 'node:http';
import CacheableLookup from 'cacheable-lookup';
import QuickLRU from 'quick-lru';

const cacheable = new CacheableLookup({
cache: new QuickLRU({maxSize: 1000})
Expand All @@ -81,7 +81,7 @@ http.get('http://example.com', {lookup: cacheable.lookup}, response => {

##### options.maxTtl

Type: `number`<br>
Type: `number`\
Default: `Infinity`

The maximum lifetime of the entries received from the specifed DNS server (TTL in seconds).
Expand All @@ -92,7 +92,7 @@ If set to `0`, it will make a new DNS query each time.

##### options.fallbackDuration

Type: `number`<br>
Type: `number`\
Default: `3600` (1 hour)

When the DNS server responds with `ENOTFOUND` or `ENODATA` and the OS reports that the entry is available, it will use `dns.lookup(...)` directly for the requested hostnames for the specified amount of time (in seconds).
Expand All @@ -101,7 +101,7 @@ When the DNS server responds with `ENOTFOUND` or `ENODATA` and the OS reports th

##### options.errorTtl

Type: `number`<br>
Type: `number`\
Default: `0.15`

The time how long it needs to remember queries that threw `ENOTFOUND` or `ENODATA` (TTL in seconds).
Expand All @@ -112,14 +112,14 @@ The time how long it needs to remember queries that threw `ENOTFOUND` or `ENODAT

##### options.resolver

Type: `dns.Resolver | dns.promises.Resolver`<br>
Type: `dns.Resolver | dns.promises.Resolver`\
Default: [`new dns.promises.Resolver()`](https://nodejs.org/api/dns.html#dns_class_dns_resolver)

An instance of [DNS Resolver](https://nodejs.org/api/dns.html#dns_class_dns_resolver) used to make DNS queries.

##### options.lookup

Type: `Function`<br>
Type: `Function`\
Default: [`dns.lookup`](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback)

The fallback function to use when the DNS server responds with `ENOTFOUND` or `ENODATA`.
Expand Down Expand Up @@ -164,7 +164,7 @@ Whether this entry was loaded from the cache or came from a query (`cache` or `q

### Entry object (callback-style)

When `options.all` is `false`, then `callback(error, address, family, expires, ttl)` is called. <br>
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.

### CacheableLookup instance
Expand All @@ -181,7 +181,7 @@ The DNS servers used to make queries. Can be overridden - doing so will clear th

The asynchronous version of `dns.lookup(…)`.

Returns an [entry object](#entry-object).<br>
Returns an [entry object](#entry-object).\
If `options.all` is true, returns an array of entry objects.

##### hostname
Expand All @@ -196,7 +196,7 @@ The same as the [`dns.lookup(…)`](https://nodejs.org/api/dns.html#dns_dns_look

#### query(hostname)

An asynchronous function which returns cached DNS lookup entries.<br>
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.
Expand All @@ -205,7 +205,7 @@ Returns an array of objects with `address`, `family`, `ttl` and `expires` proper

#### queryAndCache(hostname)

An asynchronous function which makes two DNS queries: A and AAAA. The result is cached.<br>
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.
Expand Down Expand Up @@ -242,8 +242,4 @@ Fastest is CacheableLookup#lookupAsync.all

## Related

- [cacheable-request](https://github.com/lukechilds/cacheable-request) - Wrap native HTTP requests with RFC compliant cache support

## License

MIT
- [cacheable-request](https://github.com/lukechilds/cacheable-request) - Wrap native HTTP requests with RFC compliant cache support
7 changes: 3 additions & 4 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const dns = require('dns');
const Benchmark = require('benchmark');
const CacheableLookup = require('.');
import dns from 'node:dns';
import Benchmark from 'benchmark';
import CacheableLookup from './source/index.js';

const cacheable = new CacheableLookup();
const suite = new Benchmark.Suite();
Expand Down
30 changes: 23 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
"version": "6.1.0",
"description": "A cacheable dns.lookup(…) that respects TTL",
"engines": {
"node": ">=10.6.0"
"node": ">=14.16"
},
"files": [
"source",
"index.d.ts"
],
"main": "source/index.js",
"types": "index.d.ts",
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./source/index.js"
},
"scripts": {
"test": "xo && nyc --reporter=lcovonly --reporter=text ava && tsd"
"//test": "xo && nyc --reporter=lcovonly --reporter=text ava && tsd",
"test": "tsd"
},
"repository": {
"type": "git",
Expand All @@ -32,14 +36,26 @@
"homepage": "https://github.com/szmarczak/cacheable-lookup#readme",
"devDependencies": {
"@types/keyv": "^3.1.1",
"ava": "^3.8.2",
"ava": "^4.3.3",
"benchmark": "^2.1.4",
"coveralls": "^3.0.9",
"keyv": "^4.0.0",
"nyc": "^15.0.0",
"proxyquire": "^2.1.3",
"tsd": "^0.11.0",
"quibble": "^0.6.14",
"quick-lru": "^5.1.0",
"tsd": "^0.11.0",
"xo": "^0.25.3"
},
"ava": {
"nodeArguments": [
"--loader=quibble"
]
},
"xo": {
"rules": {
"unicorn/import-index": "off",
"import/extensions": "off",
"import/no-useless-path-segments": "off"
}
}
}
22 changes: 9 additions & 13 deletions source/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use strict';
const {
import {
V4MAPPED,
ADDRCONFIG,
ALL,
promises: {
Resolver: AsyncResolver
},
lookup: dnsLookup
} = require('dns');
const {promisify} = require('util');
const os = require('os');
promises as dnsPromises,
lookup as dnsLookup
} from 'node:dns';
import {promisify} from 'node:util';
import os from 'node:os';

const {Resolver: AsyncResolver} = dnsPromises;

const kCacheableLookupCreateConnection = Symbol('cacheableLookupCreateConnection');
const kCacheableLookupInstance = Symbol('cacheableLookupInstance');
Expand Down Expand Up @@ -82,7 +81,7 @@ const all = {all: true};
const all4 = {all: true, family: 4};
const all6 = {all: true, family: 6};

class CacheableLookup {
export default class CacheableLookup {
constructor({
cache = new Map(),
maxTtl = Infinity,
Expand Down Expand Up @@ -451,6 +450,3 @@ class CacheableLookup {
this._cache.clear();
}
}

module.exports = CacheableLookup;
module.exports.default = CacheableLookup;
Loading

0 comments on commit 08a6478

Please sign in to comment.