@zeit/fetch-cached-dns
Advanced tools
Comparing version 1.0.3 to 1.1.0
@@ -45,2 +45,7 @@ const { isIP } = require('net') | ||
redirectOpts.headers.delete('Host') | ||
if (opts.onRedirect) { | ||
opts.onRedirect(redirectOpts) | ||
} | ||
return fetchCachedDns(location, redirectOpts) | ||
@@ -47,0 +52,0 @@ } else { |
{ | ||
"name": "@zeit/fetch-cached-dns", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "A decorator on top of `fetch` that caches the DNS query of the `hostname` of the passed URL", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -11,2 +11,6 @@ # fetch-cached-dns | ||
Since this implementation is implementing redirects we are providing an `onRedirect` extra | ||
option to the `fetch` call that allows one to customize the redirect options just before it | ||
happens. | ||
*NOTE: if the fetch implementation is not supplied, it will attempt to use peerDep `node-fetch`* |
57
test.js
@@ -24,3 +24,3 @@ /* eslint-env jest*/ | ||
test('works with redirects', async () => { | ||
test('works with absolute redirects', async () => { | ||
let portA | ||
@@ -52,2 +52,26 @@ let portB | ||
test('works with relative redirects', async () => { | ||
let count = 0 | ||
const server = createServer((req, res) => { | ||
if (count === 0) { | ||
res.setHeader('Location', `/foo`) | ||
res.statusCode = 302 | ||
res.end() | ||
} else { | ||
res.end(req.url) | ||
} | ||
count++ | ||
}) | ||
await listen(server) | ||
const { port } = server.address() | ||
const res = await cachedDNSFetch(`http://localtest.me:${port}`) | ||
expect(count).toBe(2) | ||
expect(await res.status).toBe(200) | ||
expect(await res.text()).toBe(`/foo`) | ||
server.close() | ||
}) | ||
test('works with `headers` as an Object', async () => { | ||
@@ -69,1 +93,32 @@ const server = createServer((req, res) => { | ||
}) | ||
test('works with `onRedirect` option to customize opts', async () => { | ||
let count = 0 | ||
const server = createServer((req, res) => { | ||
if (count === 0) { | ||
res.setHeader('Location', `/foo`) | ||
res.statusCode = 302 | ||
res.end() | ||
} else { | ||
res.end(req.url) | ||
} | ||
count++ | ||
}) | ||
await listen(server) | ||
const { port } = server.address() | ||
const options = { | ||
onRedirect: jest.fn(opts => { | ||
opts.randomOption = true | ||
}) | ||
} | ||
await cachedDNSFetch(`http://localtest.me:${port}`, options) | ||
expect(options.onRedirect.mock.calls.length).toBe(1) | ||
expect(options.onRedirect.mock.calls[0][0].headers).toBeDefined() | ||
expect(options.onRedirect.mock.calls[0][0].randomOption).toBe(true) | ||
server.close() | ||
}) |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
115339
151
16
8