Gathering detailed insights and metrics for make-fetch-happen
Gathering detailed insights and metrics for make-fetch-happen
Gathering detailed insights and metrics for make-fetch-happen
Gathering detailed insights and metrics for make-fetch-happen
@types/make-fetch-happen
TypeScript definitions for make-fetch-happen
@ember-data/request
⚡️ A simple, small and fast framework-agnostic library to make `fetch` happen
@ember-data-mirror/request
⚡️ A simple, small and fast framework-agnostic library to make `fetch` happen
@apollo/utils.fetcher
Minimal web-style fetch TypeScript typings
npm install make-fetch-happen
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
191 Stars
501 Commits
35 Forks
12 Watching
19 Branches
100 Contributors
Updated on 20 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
3%
4,004,278
Compared to previous day
Last week
6.5%
21,172,686
Compared to previous week
Last month
13.2%
85,259,060
Compared to previous month
Last year
32.7%
932,132,873
Compared to previous year
make-fetch-happen
is a Node.js
library that wraps minipass-fetch
with additional
features minipass-fetch
doesn't intend to include, including HTTP Cache support, request
pooling, proxies, retries, and more!
$ npm install --save make-fetch-happen
1const fetch = require('make-fetch-happen').defaults({ 2 cachePath: './my-cache' // path where cache will be written (and read) 3}) 4 5fetch('https://registry.npmjs.org/make-fetch-happen').then(res => { 6 return res.json() // download the body as JSON 7}).then(body => { 8 console.log(`got ${body.name} from web`) 9 return fetch('https://registry.npmjs.org/make-fetch-happen', { 10 cache: 'no-cache' // forces a conditional request 11 }) 12}).then(res => { 13 console.log(res.status) // 304! cache validated! 14 return res.json().then(body => { 15 console.log(`got ${body.name} from cache`) 16 }) 17})
minipass-fetch
for the core fetch
API implementationCache-Control
, ETag
, 304
s, cache fallback on error, etc).@npmcli/agent
)@npmcli/agent
)> fetch(uriOrRequest, [opts]) -> Promise<Response>
This function implements most of the fetch
API: given a uri
string or a Request
instance, it will fire off an http request and return a Promise containing the relevant response.
If opts
is provided, the minipass-fetch
-specific options will be passed to that library. There are also additional options specific to make-fetch-happen that add various features, such as HTTP caching, integrity verification, proxy support, and more.
1fetch('https://google.com').then(res => res.buffer())
> fetch.defaults([defaultUrl], [defaultOpts])
Returns a new fetch
function that will call make-fetch-happen
using defaultUrl
and defaultOpts
as default values to any calls.
A defaulted fetch
will also have a .defaults()
method, so they can be chained.
1const fetch = require('make-fetch-happen').defaults({ 2 cachePath: './my-local-cache' 3}) 4 5fetch('https://registry.npmjs.org/make-fetch-happen') // will always use the cache
> minipass-fetch options
The following options for minipass-fetch
are used as-is:
These other options are modified or augmented by make-fetch-happen:
User-Agent
set to make-fetch happen. Connection
is set to keep-alive
or close
automatically depending on opts.agent
.For more details, see the documentation for minipass-fetch
itself.
> make-fetch-happen options
make-fetch-happen augments the minipass-fetch
API with additional features available through extra options. The following extra options are available:
opts.cachePath
- Cache target to read/writeopts.cache
- fetch
cache mode. Controls cache behavior.opts.cacheAdditionalHeaders
- Store additional headers in the cacheopts.proxy
- Proxy agentopts.noProxy
- Domain segments to disable proxying for.opts.ca, opts.cert, opts.key, opts.strictSSL
opts.localAddress
opts.maxSockets
opts.retry
- Request retry settingsopts.onRetry
- a function called whenever a retry is attemptedopts.integrity
- Subresource Integrity metadata.opts.dns
- DNS cache optionsopts.agent
- http/https/proxy/socks agent options. See @npmcli/agent
for more info.> opts.cachePath
A string Path
to be used as the cache root for cacache
.
NOTE: Requests will not be cached unless their response bodies are consumed. You will need to use one of the res.json()
, res.buffer()
, etc methods on the response, or drain the res.body
stream, in order for it to be written.
The default cache manager also adds the following headers to cached responses:
X-Local-Cache
: Path to the cache the content was found inX-Local-Cache-Key
: Unique cache entry key for this responseX-Local-Cache-Mode
: Always stream
to indicate how the response was read from cacacheX-Local-Cache-Hash
: Specific integrity hash for the cached entryX-Local-Cache-Status
: One of miss
, hit
, stale
, revalidated
, updated
, or skip
to signal how the response was createdX-Local-Cache-Time
: UTCString of the cache insertion time for the entryUsing cacache
, a call like this may be used to
manually fetch the cached entry:
1const h = response.headers 2cacache.get(h.get('x-local-cache'), h.get('x-local-cache-key')) 3 4// grab content only, directly: 5cacache.get.byDigest(h.get('x-local-cache'), h.get('x-local-cache-hash'))
1fetch('https://registry.npmjs.org/make-fetch-happen', { 2 cachePath: './my-local-cache' 3}) // -> 200-level response will be written to disk
> opts.cache
This option follows the standard fetch
API cache option. This option will do nothing if opts.cachePath
is null. The following values are accepted (as strings):
default
- Fetch will inspect the HTTP cache on the way to the network. If there is a fresh response it will be used. If there is a stale response a conditional request will be created, and a normal request otherwise. It then updates the HTTP cache with the response. If the revalidation request fails (for example, on a 500 or if you're offline), the stale response will be returned.no-store
- Fetch behaves as if there is no HTTP cache at all.reload
- Fetch behaves as if there is no HTTP cache on the way to the network. Ergo, it creates a normal request and updates the HTTP cache with the response.no-cache
- Fetch creates a conditional request if there is a response in the HTTP cache and a normal request otherwise. It then updates the HTTP cache with the response.force-cache
- Fetch uses any response in the HTTP cache matching the request, not paying attention to staleness. If there was no response, it creates a normal request and updates the HTTP cache with the response.only-if-cached
- Fetch uses any response in the HTTP cache matching the request, not paying attention to staleness. If there was no response, it returns a network error. (Can only be used when request’s mode is "same-origin". Any cached redirects will be followed assuming request’s redirect mode is "follow" and the redirects do not violate request’s mode.)(Note: option descriptions are taken from https://fetch.spec.whatwg.org/#http-network-or-cache-fetch)
1const fetch = require('make-fetch-happen').defaults({ 2 cachePath: './my-cache' 3}) 4 5// Will error with ENOTCACHED if we haven't already cached this url 6fetch('https://registry.npmjs.org/make-fetch-happen', { 7 cache: 'only-if-cached' 8}) 9 10// Will refresh any local content and cache the new response 11fetch('https://registry.npmjs.org/make-fetch-happen', { 12 cache: 'reload' 13}) 14 15// Will use any local data, even if stale. Otherwise, will hit network. 16fetch('https://registry.npmjs.org/make-fetch-happen', { 17 cache: 'force-cache' 18})
> opts.cacheAdditionalHeaders
The following headers are always stored in the cache when present:
cache-control
content-encoding
content-language
content-type
date
etag
expires
last-modified
link
location
pragma
vary
This option allows a user to store additional custom headers in the cache.
1fetch('https://registry.npmjs.org/make-fetch-happen', { 2 cacheAdditionalHeaders: ['my-custom-header'], 3})
> opts.proxy
A string or new url.URL()
-d URI to proxy through. Different Proxy handlers will be
used depending on the proxy's protocol.
Additionally, process.env.HTTP_PROXY
, process.env.HTTPS_PROXY
, and
process.env.PROXY
are used if present and no opts.proxy
value is provided.
(Pending) process.env.NO_PROXY
may also be configured to skip proxying requests for all, or specific domains.
1fetch('https://registry.npmjs.org/make-fetch-happen', { 2 proxy: 'https://corporate.yourcompany.proxy:4445' 3}) 4 5fetch('https://registry.npmjs.org/make-fetch-happen', { 6 proxy: { 7 protocol: 'https:', 8 hostname: 'corporate.yourcompany.proxy', 9 port: 4445 10 } 11})
> opts.noProxy
If present, should be a comma-separated string or an array of domain extensions that a proxy should not be used for.
This option may also be provided through process.env.NO_PROXY
.
> opts.ca, opts.cert, opts.key, opts.strictSSL
These values are passed in directly to the HTTPS agent and will be used for both
proxied and unproxied outgoing HTTPS requests. They mostly correspond to the
same options the https
module accepts, which will be themselves passed to
tls.connect()
. opts.strictSSL
corresponds to rejectUnauthorized
.
> opts.localAddress
Passed directly to http
and https
request calls. Determines the local
address to bind to.
> opts.maxSockets
Default: 15
Maximum number of active concurrent sockets to use for the underlying Http/Https/Proxy agents. This setting applies once per spawned agent.
15 is probably a pretty good value for most use-cases, and balances speed with, uh, not knocking out people's routers. 🤓
> opts.retry
An object that can be used to tune request retry settings. Retries will only be attempted on the following conditions:
POST
AND408
, 420
, 429
, or any status in the 500-range. ORECONNRESET
, ECONNREFUSED
, EADDRINUSE
, ETIMEDOUT
, or the fetch
error request-timeout
.The following are worth noting as explicitly not retried:
getaddrinfo ENOTFOUND
and will be assumed to be either an unreachable domain or the user will be assumed offline. If a response is cached, it will be returned immediately.If opts.retry
is false
, it is equivalent to {retries: 0}
If opts.retry
is a number, it is equivalent to {retries: num}
The following retry options are available if you want more control over it:
For details on what each of these do, refer to the retry
documentation.
1fetch('https://flaky.site.com', { 2 retry: { 3 retries: 10, 4 randomize: true 5 } 6}) 7 8fetch('http://reliable.site.com', { 9 retry: false 10}) 11 12fetch('http://one-more.site.com', { 13 retry: 3 14})
> opts.onRetry
A function called with the response or error which caused the retry whenever one is attempted.
1fetch('https://flaky.site.com', { 2 onRetry(cause) { 3 console.log('we will retry because of', cause) 4 } 5})
> opts.integrity
Matches the response body against the given Subresource Integrity metadata. If verification fails, the request will fail with an EINTEGRITY
error.
integrity
may either be a string or an ssri
Integrity
-like.
1fetch('https://registry.npmjs.org/make-fetch-happen/-/make-fetch-happen-1.0.0.tgz', { 2 integrity: 'sha1-o47j7zAYnedYFn1dF/fR9OV3z8Q=' 3}) // -> ok 4 5fetch('https://malicious-registry.org/make-fetch-happen/-/make-fetch-happen-1.0.0.tgz', { 6 integrity: 'sha1-o47j7zAYnedYFn1dF/fR9OV3z8Q=' 7}) // Error: EINTEGRITY
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
20 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
Found 7/8 approved changesets -- score normalized to 8
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More