@vercel/fetch-cached-dns
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -1,6 +0,6 @@ | ||
import { Request, RequestInit, Response } from 'node-fetch'; | ||
import NodeFetch, { Request, RequestInit, Response } from 'node-fetch'; | ||
export default function createFetch(): ( | ||
url: string | Request, | ||
init?: RequestInit | ||
export default function createFetch(fetch?: typeof NodeFetch): ( | ||
url: string | Request, | ||
init?: RequestInit, | ||
) => Promise<Response>; |
76
index.js
@@ -1,46 +0,46 @@ | ||
const { isIP } = require('net') | ||
const { format, parse } = require('url') | ||
const resolve = require('@zeit/dns-cached-resolve').default | ||
const { dnsCachedUrl } = require('./util') | ||
const { isIP } = require('net'); | ||
const { format, parse } = require('url'); | ||
const resolve = require('@zeit/dns-cached-resolve').default; | ||
const { dnsCachedUrl } = require('./util'); | ||
module.exports = setup | ||
module.exports = setup; | ||
const isRedirect = v => ((v / 100) | 0) === 3 | ||
const isRedirect = (v) => ((v / 100) | 0) === 3; | ||
function setup(fetch) { | ||
if (!fetch) { | ||
fetch = require('node-fetch') | ||
fetch = require('node-fetch'); | ||
} | ||
const { Headers } = fetch | ||
const { Headers } = fetch; | ||
async function fetchCachedDns(url, opts) { | ||
const parsed = parse(url) | ||
const originalHost = parsed.host | ||
const ip = isIP(parsed.hostname) | ||
const parsed = parse(url); | ||
const originalHost = parsed.host; | ||
const ip = isIP(parsed.hostname); | ||
if (ip === 0) { | ||
if (!opts) opts = {} | ||
opts.headers = new Headers(opts.headers) | ||
if (!opts) opts = {}; | ||
opts.headers = new Headers(opts.headers); | ||
if (!opts.headers.has('Host')) { | ||
opts.headers.set('Host', parsed.host) | ||
opts.headers.set('Host', parsed.host); | ||
} | ||
opts.redirect = 'manual' | ||
parsed.host = await resolve(parsed.hostname) | ||
opts.redirect = 'manual'; | ||
parsed.host = await resolve(parsed.hostname); | ||
if (parsed.port) { | ||
parsed.host += `:${parsed.port}` | ||
parsed.host += `:${parsed.port}`; | ||
} | ||
url = format(parsed) | ||
url = format(parsed); | ||
} | ||
const res = await fetch(url, opts) | ||
const res = await fetch(url, opts); | ||
// Update `res.url` to contain the original hostname instead of the IP address | ||
res[dnsCachedUrl] = url | ||
res[dnsCachedUrl] = url; | ||
Object.defineProperty(res, 'url', { | ||
get() { | ||
return parsed.href | ||
} | ||
}) | ||
return parsed.href; | ||
}, | ||
}); | ||
if (isRedirect(res.status)) { | ||
const redirectOpts = Object.assign({}, opts) | ||
redirectOpts.headers = new Headers(opts.headers) | ||
const redirectOpts = { ...opts }; | ||
redirectOpts.headers = new Headers(opts.headers); | ||
@@ -53,5 +53,5 @@ // Per fetch spec, for POST request with 301/302 response, or any | ||
) { | ||
redirectOpts.method = 'GET' | ||
redirectOpts.body = null | ||
redirectOpts.headers.delete('content-length') | ||
redirectOpts.method = 'GET'; | ||
redirectOpts.body = null; | ||
redirectOpts.headers.delete('content-length'); | ||
} | ||
@@ -62,25 +62,25 @@ | ||
// replaced with the original hostname as well. | ||
const location = res.headers.get('Location') | ||
const parsedLocation = parse(location) | ||
const location = res.headers.get('Location'); | ||
const parsedLocation = parse(location); | ||
if (parsedLocation.host === parsed.host) { | ||
parsedLocation.host = originalHost | ||
parsedLocation.host = originalHost; | ||
} | ||
redirectOpts.headers.set('Host', parsedLocation.host) | ||
redirectOpts.headers.set('Host', parsedLocation.host); | ||
if (opts.onRedirect) { | ||
opts.onRedirect(res, redirectOpts) | ||
opts.onRedirect(res, redirectOpts); | ||
} | ||
return fetchCachedDns(format(parsedLocation), redirectOpts) | ||
return fetchCachedDns(format(parsedLocation), redirectOpts); | ||
} | ||
return res | ||
return res; | ||
} | ||
for (const key of Object.keys(fetch)) { | ||
fetchCachedDns[key] = fetch[key] | ||
fetchCachedDns[key] = fetch[key]; | ||
} | ||
fetchCachedDns.default = fetchCachedDns | ||
fetchCachedDns.default = fetchCachedDns; | ||
return fetchCachedDns | ||
return fetchCachedDns; | ||
} |
{ | ||
"name": "@vercel/fetch-cached-dns", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "A decorator on top of `fetch` that caches the DNS query of the `hostname` of the passed URL", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
@@ -11,7 +13,2 @@ "index.js", | ||
], | ||
"scripts": { | ||
"test": "jest test" | ||
}, | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"repository": { | ||
@@ -22,6 +19,6 @@ "type": "git", | ||
}, | ||
"author": "Nathan Rajlich <nate@vercel.com>", | ||
"peerDependencies": { | ||
"node-fetch": "^2.6.1" | ||
}, | ||
"contributors": [ | ||
"Nathan Rajlich <nate@vercel.com>", | ||
"Ethan Arrowood <ethan.arrowood@vercel.com>" | ||
], | ||
"dependencies": { | ||
@@ -31,2 +28,5 @@ "@types/node-fetch": "^2.6.1", | ||
}, | ||
"peerDependencies": { | ||
"node-fetch": "^2.6.1" | ||
}, | ||
"devDependencies": { | ||
@@ -36,3 +36,8 @@ "async-listen": "^1.2.0", | ||
"node-fetch": "^2.6.1" | ||
} | ||
} | ||
}, | ||
"scripts": { | ||
"test": "jest test", | ||
"lint": "cd ../.. && pnpm eslint packages/fetch-cached-dns/**/*.js" | ||
}, | ||
"readme": "# @vercel/fetch-cached-dns\n\n[![Build Status](https://github.com/vercel/fetch/workflows/CI/badge.svg)](https://github.com/vercel/fetch/actions?workflow=CI)\n\nA decorator on top of `fetch` that caches the DNS query of the `hostname` of the passed URL.\n\n## How to use\n\n```js\nconst fetch = require('@vercel/fetch-cached-dns')(require('node-fetch'));\n```\n\nSince this implementation is implementing redirects we are providing an `onRedirect` extra\noption to the `fetch` call that gets called with the response object and the options that\nwill be used for the next request. This allows to access the request from outside and to\nmodify the options.\n\n_NOTE: if the fetch implementation is not supplied, it will attempt to use peerDep `node-fetch`_\n" | ||
} |
@@ -10,6 +10,6 @@ # @vercel/fetch-cached-dns | ||
```js | ||
const fetch = require('@vercel/fetch-cached-dns')(require('node-fetch')) | ||
const fetch = require('@vercel/fetch-cached-dns')(require('node-fetch')); | ||
``` | ||
Since this implementation is implementing redirects we are providing an `onRedirect` extra | ||
Since this implementation is implementing redirects we are providing an `onRedirect` extra | ||
option to the `fetch` call that gets called with the response object and the options that | ||
@@ -19,2 +19,2 @@ will be used for the next request. This allows to access the request from outside and to | ||
*NOTE: if the fetch implementation is not supplied, it will attempt to use peerDep `node-fetch`* | ||
_NOTE: if the fetch implementation is not supplied, it will attempt to use peerDep `node-fetch`_ |
// Used for testing | ||
exports.dnsCachedUrl = Symbol('dnsCachedUrl') | ||
exports.dnsCachedUrl = Symbol('dnsCachedUrl'); |
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
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
6286