pnpm 6.17.2 → 6.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +26 -24
  2. package/dist/node_modules/make-fetch-happen/{agent.js → lib/agent.js} +14 -29
  3. package/dist/node_modules/make-fetch-happen/lib/cache/entry.js +460 -0
  4. package/dist/node_modules/make-fetch-happen/lib/cache/errors.js +10 -0
  5. package/dist/node_modules/make-fetch-happen/lib/cache/index.js +45 -0
  6. package/dist/node_modules/make-fetch-happen/lib/cache/key.js +17 -0
  7. package/dist/node_modules/make-fetch-happen/lib/cache/policy.js +161 -0
  8. package/dist/node_modules/make-fetch-happen/lib/fetch.js +100 -0
  9. package/dist/node_modules/make-fetch-happen/lib/index.js +40 -0
  10. package/dist/node_modules/make-fetch-happen/lib/options.js +44 -0
  11. package/dist/node_modules/make-fetch-happen/lib/remote.js +102 -0
  12. package/dist/node_modules/make-fetch-happen/package.json +21 -17
  13. package/dist/node_modules/negotiator/LICENSE +24 -0
  14. package/dist/node_modules/negotiator/index.js +124 -0
  15. package/dist/node_modules/negotiator/lib/charset.js +169 -0
  16. package/dist/node_modules/negotiator/lib/encoding.js +184 -0
  17. package/dist/node_modules/negotiator/lib/language.js +179 -0
  18. package/dist/node_modules/negotiator/lib/mediaType.js +294 -0
  19. package/dist/node_modules/negotiator/package.json +42 -0
  20. package/dist/node_modules/node-gyp/.github/workflows/tests.yml +1 -1
  21. package/dist/node_modules/node-gyp/gyp/pylib/gyp/MSVSVersion.py +16 -1
  22. package/dist/node_modules/node-gyp/gyp/setup.py +1 -1
  23. package/dist/node_modules/node-gyp/lib/configure.js +5 -97
  24. package/dist/node_modules/node-gyp/lib/create-config-gypi.js +119 -0
  25. package/dist/node_modules/node-gyp/package.json +2 -2
  26. package/dist/node_modules/socks-proxy-agent/dist/agent.js +4 -3
  27. package/dist/node_modules/socks-proxy-agent/dist/agent.js.map +1 -1
  28. package/dist/node_modules/socks-proxy-agent/dist/index.js.map +1 -1
  29. package/dist/node_modules/socks-proxy-agent/package.json +20 -20
  30. package/dist/pnpm.cjs +13490 -13531
  31. package/dist/pnpmrc +2 -0
  32. package/dist/pnpx.cjs +6 -4
  33. package/package.json +34 -33
  34. package/dist/node_modules/make-fetch-happen/cache.js +0 -260
  35. package/dist/node_modules/make-fetch-happen/index.js +0 -457
  36. package/dist/node_modules/make-fetch-happen/utils/configure-options.js +0 -32
  37. package/dist/node_modules/make-fetch-happen/utils/initialize-cache.js +0 -26
  38. package/dist/node_modules/make-fetch-happen/utils/is-header-conditional.js +0 -17
  39. package/dist/node_modules/make-fetch-happen/utils/iterable-to-object.js +0 -9
  40. package/dist/node_modules/make-fetch-happen/utils/make-policy.js +0 -19
  41. package/dist/node_modules/make-fetch-happen/warning.js +0 -24
package/README.md CHANGED
@@ -9,6 +9,7 @@ Fast, disk space efficient package manager:
9
9
  * **[Great for monorepos](https://pnpm.io/workspaces).**
10
10
  * **Strict.** A package can access only dependencies that are specified in its `package.json`.
11
11
  * **Deterministic.** Has a lockfile called `pnpm-lock.yaml`.
12
+ * **Works as a Node.js version manager.** See [pnpm env use](https://pnpm.io/cli/env).
12
13
  * **Works everywhere.** Supports Windows, Linux, and macOS.
13
14
  * **Battle-tested.** Used in production by teams of [all sizes](https://pnpm.io/users) since 2016.
14
15
 
@@ -20,7 +21,24 @@ To quote the [Rush](https://rushjs.io/) team:
20
21
  [![Join the chat at Discord](https://img.shields.io/discord/731599538665553971.svg)](https://r.pnpm.io/chat)
21
22
  [![OpenCollective](https://opencollective.com/pnpm/backers/badge.svg)](#backers)
22
23
  [![OpenCollective](https://opencollective.com/pnpm/sponsors/badge.svg)](#sponsors)
23
- [![Twitter Follow](https://img.shields.io/twitter/follow/pnpmjs.svg?style=social&label=Follow)](https://twitter.com/pnpmjs)
24
+ [![Twitter Follow](https://img.shields.io/twitter/follow/pnpmjs.svg?style=social&label=Follow)](https://twitter.com/intent/follow?screen_name=pnpmjs&region=follow_link)
25
+
26
+ ## Sponsors
27
+
28
+ <table>
29
+ <tbody>
30
+ <tr>
31
+ <td align="center" valign="middle">
32
+ <a href="https://bit.dev" target="_blank"><img src="https://raw.githubusercontent.com/pnpm/pnpm.github.io/main/static/img/users/bit.svg" width="80"></a>
33
+ </td>
34
+ <td align="center" valign="middle">
35
+ <a href="https://prisma.io" target="_blank"><img src="https://raw.githubusercontent.com/pnpm/pnpm.github.io/main/static/img/users/prisma.svg" width="180"></a>
36
+ </td>
37
+ </tr>
38
+ </tbody>
39
+ </table>
40
+
41
+ Support this project by [becoming a sponsor](https://opencollective.com/pnpm#sponsor).
24
42
 
25
43
  ## Background
26
44
 
@@ -38,6 +56,8 @@ As a result, you save gigabytes of space on your disk and you have a lot faster
38
56
  If you'd like more details about the unique `node_modules` structure that pnpm creates and
39
57
  why it works fine with the Node.js ecosystem, read this small article: [Flat node_modules is not the only way](https://pnpm.io/blog/2020/05/27/flat-node-modules-is-not-the-only-way).
40
58
 
59
+ 💖 Like this project? Let people know with a [tweet](https://r.pnpm.io/tweet)
60
+
41
61
  ## Installation
42
62
 
43
63
  On macOS, Linux, or Windows Subsystem for Linux:
@@ -60,7 +80,7 @@ npx pnpm add -g pnpm
60
80
 
61
81
  For other installation options [visit our website](https://pnpm.io/installation).
62
82
 
63
- We also ship an experimental new version of pnpm that may be used even with no Node.js installed on the system, see [@pnpm/exe](https://www.npmjs.com/package/@pnpm/exe).
83
+ We also ship a version of pnpm that may be used even with no Node.js installed on the system, see [@pnpm/exe](https://www.npmjs.com/package/@pnpm/exe).
64
84
 
65
85
  ## Usage
66
86
 
@@ -92,36 +112,18 @@ Benchmarks on an app with lots of dependencies:
92
112
  - [Chat](https://r.pnpm.io/chat)
93
113
  - [Twitter](https://twitter.com/pnpmjs)
94
114
 
95
- ## Contributors
96
-
97
- This project exists thanks to all the people who contribute. [[Contribute](../../blob/main/CONTRIBUTING.md)].
98
- <a href="../../graphs/contributors"><img src="https://opencollective.com/pnpm/contributors.svg?width=890&button=false" /></a>
99
-
100
115
  ### Backers
101
116
 
102
- Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/pnpm#backer)]
117
+ Thank you to all our backers! [Become a backer](https://opencollective.com/pnpm#backer)
103
118
 
104
119
  <a href="https://opencollective.com/pnpm#backers" target="_blank"><img src="https://opencollective.com/pnpm/backers.svg?width=890"></a>
105
120
 
106
- ### Sponsors
121
+ ## Contributors
107
122
 
108
- Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/pnpm#sponsor)]
123
+ This project exists thanks to all the people who contribute. [Contribute](../../blob/main/CONTRIBUTING.md).
109
124
 
110
- <a href="https://opencollective.com/pnpm/sponsor/0/website" target="_blank"><img src="https://opencollective.com/pnpm/sponsor/0/avatar.svg"></a>
111
- <a href="https://opencollective.com/pnpm/sponsor/1/website" target="_blank"><img src="https://opencollective.com/pnpm/sponsor/1/avatar.svg"></a>
112
- <a href="https://opencollective.com/pnpm/sponsor/2/website" target="_blank"><img src="https://opencollective.com/pnpm/sponsor/2/avatar.svg"></a>
113
- <a href="https://opencollective.com/pnpm/sponsor/3/website" target="_blank"><img src="https://opencollective.com/pnpm/sponsor/3/avatar.svg"></a>
114
- <a href="https://opencollective.com/pnpm/sponsor/4/website" target="_blank"><img src="https://opencollective.com/pnpm/sponsor/4/avatar.svg"></a>
115
- <a href="https://opencollective.com/pnpm/sponsor/5/website" target="_blank"><img src="https://opencollective.com/pnpm/sponsor/5/avatar.svg"></a>
116
- <a href="https://opencollective.com/pnpm/sponsor/6/website" target="_blank"><img src="https://opencollective.com/pnpm/sponsor/6/avatar.svg"></a>
117
- <a href="https://opencollective.com/pnpm/sponsor/7/website" target="_blank"><img src="https://opencollective.com/pnpm/sponsor/7/avatar.svg"></a>
118
- <a href="https://opencollective.com/pnpm/sponsor/8/website" target="_blank"><img src="https://opencollective.com/pnpm/sponsor/8/avatar.svg"></a>
119
- <a href="https://opencollective.com/pnpm/sponsor/9/website" target="_blank"><img src="https://opencollective.com/pnpm/sponsor/9/avatar.svg"></a>
125
+ <a href="../../graphs/contributors"><img src="https://opencollective.com/pnpm/contributors.svg?width=890&button=false" /></a>
120
126
 
121
127
  ## License
122
128
 
123
129
  [MIT](https://github.com/pnpm/pnpm/blob/main/LICENSE)
124
-
125
- ***
126
-
127
- Like this project? Let people know with a [tweet](https://r.pnpm.io/tweet).
@@ -4,8 +4,8 @@ const url = require('url')
4
4
  const isLambda = require('is-lambda')
5
5
 
6
6
  const AGENT_CACHE = new LRU({ max: 50 })
7
- let HttpsAgent
8
- let HttpAgent
7
+ const HttpAgent = require('agentkeepalive')
8
+ const HttpsAgent = HttpAgent.HttpsAgent
9
9
 
10
10
  module.exports = getAgent
11
11
 
@@ -33,7 +33,7 @@ function getAgent (uri, opts) {
33
33
  ? `proxy:${pxuri.protocol}//${pxuri.host}:${pxuri.port}`
34
34
  : '>no-proxy<',
35
35
  `local-address:${opts.localAddress || '>no-local-address<'}`,
36
- `strict-ssl:${isHttps ? !!opts.strictSSL : '>no-strict-ssl<'}`,
36
+ `strict-ssl:${isHttps ? opts.rejectUnauthorized : '>no-strict-ssl<'}`,
37
37
  `ca:${(isHttps && opts.ca) || '>no-ca<'}`,
38
38
  `cert:${(isHttps && opts.cert) || '>no-cert<'}`,
39
39
  `key:${(isHttps && opts.key) || '>no-key<'}`,
@@ -66,18 +66,13 @@ function getAgent (uri, opts) {
66
66
  return proxy
67
67
  }
68
68
 
69
- if (!HttpsAgent) {
70
- HttpAgent = require('agentkeepalive')
71
- HttpsAgent = HttpAgent.HttpsAgent
72
- }
73
-
74
69
  const agent = isHttps ? new HttpsAgent({
75
70
  maxSockets: agentMaxSockets,
76
71
  ca: opts.ca,
77
72
  cert: opts.cert,
78
73
  key: opts.key,
79
74
  localAddress: opts.localAddress,
80
- rejectUnauthorized: opts.strictSSL,
75
+ rejectUnauthorized: opts.rejectUnauthorized,
81
76
  timeout: agentTimeout,
82
77
  }) : new HttpAgent({
83
78
  maxSockets: agentMaxSockets,
@@ -155,15 +150,15 @@ function getProxyUri (uri, opts) {
155
150
  }
156
151
 
157
152
  const getAuth = u =>
158
- u.username && u.password ? `${u.username}:${u.password}`
159
- : u.username ? u.username
153
+ u.username && u.password ? decodeURIComponent(`${u.username}:${u.password}`)
154
+ : u.username ? decodeURIComponent(u.username)
160
155
  : null
161
156
 
162
157
  const getPath = u => u.pathname + u.search + u.hash
163
158
 
164
- let HttpProxyAgent
165
- let HttpsProxyAgent
166
- let SocksProxyAgent
159
+ const HttpProxyAgent = require('http-proxy-agent')
160
+ const HttpsProxyAgent = require('https-proxy-agent')
161
+ const SocksProxyAgent = require('socks-proxy-agent')
167
162
  module.exports.getProxy = getProxy
168
163
  function getProxy (proxyUrl, opts, isHttps) {
169
164
  const popts = {
@@ -178,27 +173,17 @@ function getProxy (proxyUrl, opts, isHttps) {
178
173
  timeout: getAgentTimeout(opts.timeout),
179
174
  localAddress: opts.localAddress,
180
175
  maxSockets: getMaxSockets(opts.maxSockets),
181
- rejectUnauthorized: opts.strictSSL,
176
+ rejectUnauthorized: opts.rejectUnauthorized,
182
177
  }
183
178
 
184
179
  if (proxyUrl.protocol === 'http:' || proxyUrl.protocol === 'https:') {
185
- if (!isHttps) {
186
- if (!HttpProxyAgent)
187
- HttpProxyAgent = require('http-proxy-agent')
188
-
180
+ if (!isHttps)
189
181
  return new HttpProxyAgent(popts)
190
- } else {
191
- if (!HttpsProxyAgent)
192
- HttpsProxyAgent = require('https-proxy-agent')
193
-
182
+ else
194
183
  return new HttpsProxyAgent(popts)
195
- }
196
- } else if (proxyUrl.protocol.startsWith('socks')) {
197
- if (!SocksProxyAgent)
198
- SocksProxyAgent = require('socks-proxy-agent')
199
-
184
+ } else if (proxyUrl.protocol.startsWith('socks'))
200
185
  return new SocksProxyAgent(popts)
201
- } else {
186
+ else {
202
187
  throw Object.assign(
203
188
  new Error(`unsupported proxy protocol: '${proxyUrl.protocol}'`),
204
189
  {
@@ -0,0 +1,460 @@
1
+ const { Request, Response } = require('minipass-fetch')
2
+ const Minipass = require('minipass')
3
+ const MinipassCollect = require('minipass-collect')
4
+ const MinipassFlush = require('minipass-flush')
5
+ const MinipassPipeline = require('minipass-pipeline')
6
+ const cacache = require('cacache')
7
+ const url = require('url')
8
+
9
+ const CachePolicy = require('./policy.js')
10
+ const cacheKey = require('./key.js')
11
+ const remote = require('../remote.js')
12
+
13
+ const hasOwnProperty = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)
14
+
15
+ // maximum amount of data we will buffer into memory
16
+ // if we'll exceed this, we switch to streaming
17
+ const MAX_MEM_SIZE = 5 * 1024 * 1024 // 5MB
18
+
19
+ // allow list for request headers that will be written to the cache index
20
+ // note: we will also store any request headers
21
+ // that are named in a response's vary header
22
+ const KEEP_REQUEST_HEADERS = [
23
+ 'accept-charset',
24
+ 'accept-encoding',
25
+ 'accept-language',
26
+ 'accept',
27
+ 'cache-control',
28
+ ]
29
+
30
+ // allow list for response headers that will be written to the cache index
31
+ // note: we must not store the real response's age header, or when we load
32
+ // a cache policy based on the metadata it will think the cached response
33
+ // is always stale
34
+ const KEEP_RESPONSE_HEADERS = [
35
+ 'cache-control',
36
+ 'content-encoding',
37
+ 'content-language',
38
+ 'content-type',
39
+ 'date',
40
+ 'etag',
41
+ 'expires',
42
+ 'last-modified',
43
+ 'location',
44
+ 'pragma',
45
+ 'vary',
46
+ ]
47
+
48
+ // return an object containing all metadata to be written to the index
49
+ const getMetadata = (request, response, options) => {
50
+ const metadata = {
51
+ time: Date.now(),
52
+ url: request.url,
53
+ reqHeaders: {},
54
+ resHeaders: {},
55
+ }
56
+
57
+ // only save the status if it's not a 200 or 304
58
+ if (response.status !== 200 && response.status !== 304)
59
+ metadata.status = response.status
60
+
61
+ for (const name of KEEP_REQUEST_HEADERS) {
62
+ if (request.headers.has(name))
63
+ metadata.reqHeaders[name] = request.headers.get(name)
64
+ }
65
+
66
+ // if the request's host header differs from the host in the url
67
+ // we need to keep it, otherwise it's just noise and we ignore it
68
+ const host = request.headers.get('host')
69
+ const parsedUrl = new url.URL(request.url)
70
+ if (host && parsedUrl.host !== host)
71
+ metadata.reqHeaders.host = host
72
+
73
+ // if the response has a vary header, make sure
74
+ // we store the relevant request headers too
75
+ if (response.headers.has('vary')) {
76
+ const vary = response.headers.get('vary')
77
+ // a vary of "*" means every header causes a different response.
78
+ // in that scenario, we do not include any additional headers
79
+ // as the freshness check will always fail anyway and we don't
80
+ // want to bloat the cache indexes
81
+ if (vary !== '*') {
82
+ // copy any other request headers that will vary the response
83
+ const varyHeaders = vary.trim().toLowerCase().split(/\s*,\s*/)
84
+ for (const name of varyHeaders) {
85
+ // explicitly ignore accept-encoding here
86
+ if (name !== 'accept-encoding' && request.headers.has(name))
87
+ metadata.reqHeaders[name] = request.headers.get(name)
88
+ }
89
+ }
90
+ }
91
+
92
+ for (const name of KEEP_RESPONSE_HEADERS) {
93
+ if (response.headers.has(name))
94
+ metadata.resHeaders[name] = response.headers.get(name)
95
+ }
96
+
97
+ // we only store accept-encoding and content-encoding if the user
98
+ // has disabled automatic compression and decompression in minipass-fetch
99
+ // since if it's enabled (the default) then the content will have
100
+ // already been decompressed making the header a lie
101
+ if (options.compress === false) {
102
+ metadata.reqHeaders['accept-encoding'] = request.headers.get('accept-encoding')
103
+ metadata.resHeaders['content-encoding'] = response.headers.get('content-encoding')
104
+ }
105
+
106
+ return metadata
107
+ }
108
+
109
+ // symbols used to hide objects that may be lazily evaluated in a getter
110
+ const _request = Symbol('request')
111
+ const _response = Symbol('response')
112
+ const _policy = Symbol('policy')
113
+
114
+ class CacheEntry {
115
+ constructor ({ entry, request, response, options }) {
116
+ if (entry) {
117
+ this.key = entry.key
118
+ this.entry = entry
119
+ // previous versions of this module didn't write an explicit timestamp in
120
+ // the metadata, so fall back to the entry's timestamp. we can't use the
121
+ // entry timestamp to determine staleness because cacache will update it
122
+ // when it verifies its data
123
+ this.entry.metadata.time = this.entry.metadata.time || this.entry.time
124
+ } else
125
+ this.key = cacheKey(request)
126
+
127
+ this.options = options
128
+
129
+ // these properties are behind getters that lazily evaluate
130
+ this[_request] = request
131
+ this[_response] = response
132
+ this[_policy] = null
133
+ }
134
+
135
+ // returns a CacheEntry instance that satisfies the given request
136
+ // or undefined if no existing entry satisfies
137
+ static async find (request, options) {
138
+ try {
139
+ // compacts the index and returns an array of unique entries
140
+ var matches = await cacache.index.compact(options.cachePath, cacheKey(request), (A, B) => {
141
+ const entryA = new CacheEntry({ entry: A, options })
142
+ const entryB = new CacheEntry({ entry: B, options })
143
+ return entryA.policy.satisfies(entryB.request)
144
+ }, {
145
+ validateEntry: (entry) => {
146
+ // if an integrity is null, it needs to have a status specified
147
+ if (entry.integrity === null)
148
+ return !!(entry.metadata && entry.metadata.status)
149
+
150
+ return true
151
+ },
152
+ })
153
+ } catch (err) {
154
+ // if the compact request fails, ignore the error and return
155
+ return
156
+ }
157
+
158
+ // a cache mode of 'reload' means to behave as though we have no cache
159
+ // on the way to the network. return undefined to allow cacheFetch to
160
+ // create a brand new request no matter what.
161
+ if (options.cache === 'reload')
162
+ return
163
+
164
+ // find the specific entry that satisfies the request
165
+ let match
166
+ for (const entry of matches) {
167
+ const _entry = new CacheEntry({
168
+ entry,
169
+ options,
170
+ })
171
+
172
+ if (_entry.policy.satisfies(request)) {
173
+ match = _entry
174
+ break
175
+ }
176
+ }
177
+
178
+ return match
179
+ }
180
+
181
+ // if the user made a PUT/POST/PATCH then we invalidate our
182
+ // cache for the same url by deleting the index entirely
183
+ static async invalidate (request, options) {
184
+ const key = cacheKey(request)
185
+ try {
186
+ await cacache.rm.entry(options.cachePath, key, { removeFully: true })
187
+ } catch (err) {
188
+ // ignore errors
189
+ }
190
+ }
191
+
192
+ get request () {
193
+ if (!this[_request]) {
194
+ this[_request] = new Request(this.entry.metadata.url, {
195
+ method: 'GET',
196
+ headers: this.entry.metadata.reqHeaders,
197
+ })
198
+ }
199
+
200
+ return this[_request]
201
+ }
202
+
203
+ get response () {
204
+ if (!this[_response]) {
205
+ this[_response] = new Response(null, {
206
+ url: this.entry.metadata.url,
207
+ counter: this.options.counter,
208
+ status: this.entry.metadata.status || 200,
209
+ headers: {
210
+ ...this.entry.metadata.resHeaders,
211
+ 'content-length': this.entry.size,
212
+ },
213
+ })
214
+ }
215
+
216
+ return this[_response]
217
+ }
218
+
219
+ get policy () {
220
+ if (!this[_policy]) {
221
+ this[_policy] = new CachePolicy({
222
+ entry: this.entry,
223
+ request: this.request,
224
+ response: this.response,
225
+ options: this.options,
226
+ })
227
+ }
228
+
229
+ return this[_policy]
230
+ }
231
+
232
+ // wraps the response in a pipeline that stores the data
233
+ // in the cache while the user consumes it
234
+ async store (status) {
235
+ // if we got a status other than 200, 301, or 308,
236
+ // or the CachePolicy forbid storage, append the
237
+ // cache status header and return it untouched
238
+ if (this.request.method !== 'GET' || ![200, 301, 308].includes(this.response.status) || !this.policy.storable()) {
239
+ this.response.headers.set('x-local-cache-status', 'skip')
240
+ return this.response
241
+ }
242
+
243
+ const size = this.response.headers.get('content-length')
244
+ const fitsInMemory = !!size && Number(size) < MAX_MEM_SIZE
245
+ const shouldBuffer = this.options.memoize !== false && fitsInMemory
246
+ const cacheOpts = {
247
+ algorithms: this.options.algorithms,
248
+ metadata: getMetadata(this.request, this.response, this.options),
249
+ size,
250
+ memoize: fitsInMemory && this.options.memoize,
251
+ }
252
+
253
+ let body = null
254
+ // we only set a body if the status is a 200, redirects are
255
+ // stored as metadata only
256
+ if (this.response.status === 200) {
257
+ let cacheWriteResolve, cacheWriteReject
258
+ const cacheWritePromise = new Promise((resolve, reject) => {
259
+ cacheWriteResolve = resolve
260
+ cacheWriteReject = reject
261
+ })
262
+
263
+ body = new MinipassPipeline(new MinipassFlush({
264
+ flush () {
265
+ return cacheWritePromise
266
+ },
267
+ }))
268
+
269
+ let abortStream, onResume
270
+ if (shouldBuffer) {
271
+ // if the result fits in memory, use a collect stream to gather
272
+ // the response and write it to cacache while also passing it through
273
+ // to the user
274
+ onResume = () => {
275
+ const collector = new MinipassCollect.PassThrough()
276
+ abortStream = collector
277
+ collector.on('collect', (data) => {
278
+ // TODO if the cache write fails, log a warning but return the response anyway
279
+ cacache.put(this.options.cachePath, this.key, data, cacheOpts).then(cacheWriteResolve, cacheWriteReject)
280
+ })
281
+ body.unshift(collector)
282
+ body.unshift(this.response.body)
283
+ }
284
+ } else {
285
+ // if it does not fit in memory, create a tee stream and use
286
+ // that to pipe to both the cache and the user simultaneously
287
+ onResume = () => {
288
+ const tee = new Minipass()
289
+ const cacheStream = cacache.put.stream(this.options.cachePath, this.key, cacheOpts)
290
+ abortStream = cacheStream
291
+ tee.pipe(cacheStream)
292
+ // TODO if the cache write fails, log a warning but return the response anyway
293
+ cacheStream.promise().then(cacheWriteResolve, cacheWriteReject)
294
+ body.unshift(tee)
295
+ body.unshift(this.response.body)
296
+ }
297
+ }
298
+
299
+ body.once('resume', onResume)
300
+ body.once('end', () => body.removeListener('resume', onResume))
301
+ this.response.body.on('error', (err) => {
302
+ // the abortStream will either be a MinipassCollect if we buffer
303
+ // or a cacache write stream, either way be sure to listen for
304
+ // errors from the actual response and avoid writing data that we
305
+ // know to be invalid to the cache
306
+ abortStream.destroy(err)
307
+ })
308
+ } else
309
+ await cacache.index.insert(this.options.cachePath, this.key, null, cacheOpts)
310
+
311
+ // note: we do not set the x-local-cache-hash header because we do not know
312
+ // the hash value until after the write to the cache completes, which doesn't
313
+ // happen until after the response has been sent and it's too late to write
314
+ // the header anyway
315
+ this.response.headers.set('x-local-cache', encodeURIComponent(this.options.cachePath))
316
+ this.response.headers.set('x-local-cache-key', encodeURIComponent(this.key))
317
+ this.response.headers.set('x-local-cache-mode', shouldBuffer ? 'buffer' : 'stream')
318
+ this.response.headers.set('x-local-cache-status', status)
319
+ this.response.headers.set('x-local-cache-time', new Date().toISOString())
320
+ const newResponse = new Response(body, {
321
+ url: this.response.url,
322
+ status: this.response.status,
323
+ headers: this.response.headers,
324
+ counter: this.options.counter,
325
+ })
326
+ return newResponse
327
+ }
328
+
329
+ // use the cached data to create a response and return it
330
+ async respond (method, options, status) {
331
+ let response
332
+ const size = Number(this.response.headers.get('content-length'))
333
+ const fitsInMemory = !!size && size < MAX_MEM_SIZE
334
+ const shouldBuffer = this.options.memoize !== false && fitsInMemory
335
+ if (method === 'HEAD' || [301, 308].includes(this.response.status)) {
336
+ // if the request is a HEAD, or the response is a redirect,
337
+ // then the metadata in the entry already includes everything
338
+ // we need to build a response
339
+ response = this.response
340
+ } else {
341
+ // we're responding with a full cached response, so create a body
342
+ // that reads from cacache and attach it to a new Response
343
+ const body = new Minipass()
344
+ const removeOnResume = () => body.removeListener('resume', onResume)
345
+ let onResume
346
+ if (shouldBuffer) {
347
+ onResume = async () => {
348
+ removeOnResume()
349
+ try {
350
+ const content = await cacache.get.byDigest(this.options.cachePath, this.entry.integrity, { memoize: this.options.memoize })
351
+ body.end(content)
352
+ } catch (err) {
353
+ if (err.code === 'EINTEGRITY')
354
+ await cacache.rm.content(this.options.cachePath, this.entry.integrity, { memoize: this.options.memoize })
355
+ if (err.code === 'ENOENT' || err.code === 'EINTEGRITY')
356
+ await CacheEntry.invalidate(this.request, this.options)
357
+ body.emit('error', err)
358
+ }
359
+ }
360
+ } else {
361
+ onResume = () => {
362
+ const cacheStream = cacache.get.stream.byDigest(this.options.cachePath, this.entry.integrity, { memoize: this.options.memoize })
363
+ cacheStream.on('error', async (err) => {
364
+ cacheStream.pause()
365
+ if (err.code === 'EINTEGRITY')
366
+ await cacache.rm.content(this.options.cachePath, this.entry.integrity, { memoize: this.options.memoize })
367
+ if (err.code === 'ENOENT' || err.code === 'EINTEGRITY')
368
+ await CacheEntry.invalidate(this.request, this.options)
369
+ body.emit('error', err)
370
+ cacheStream.resume()
371
+ })
372
+ cacheStream.pipe(body)
373
+ }
374
+ }
375
+
376
+ body.once('resume', onResume)
377
+ body.once('end', removeOnResume)
378
+ response = new Response(body, {
379
+ url: this.entry.metadata.url,
380
+ counter: options.counter,
381
+ status: 200,
382
+ headers: {
383
+ ...this.policy.responseHeaders(),
384
+ },
385
+ })
386
+ }
387
+
388
+ response.headers.set('x-local-cache', encodeURIComponent(this.options.cachePath))
389
+ response.headers.set('x-local-cache-hash', encodeURIComponent(this.entry.integrity))
390
+ response.headers.set('x-local-cache-key', encodeURIComponent(this.key))
391
+ response.headers.set('x-local-cache-mode', shouldBuffer ? 'buffer' : 'stream')
392
+ response.headers.set('x-local-cache-status', status)
393
+ response.headers.set('x-local-cache-time', new Date(this.entry.metadata.time).toUTCString())
394
+ return response
395
+ }
396
+
397
+ // use the provided request along with this cache entry to
398
+ // revalidate the stored response. returns a response, either
399
+ // from the cache or from the update
400
+ async revalidate (request, options) {
401
+ const revalidateRequest = new Request(request, {
402
+ headers: this.policy.revalidationHeaders(request),
403
+ })
404
+
405
+ try {
406
+ // NOTE: be sure to remove the headers property from the
407
+ // user supplied options, since we have already defined
408
+ // them on the new request object. if they're still in the
409
+ // options then those will overwrite the ones from the policy
410
+ var response = await remote(revalidateRequest, {
411
+ ...options,
412
+ headers: undefined,
413
+ })
414
+ } catch (err) {
415
+ // if the network fetch fails, return the stale
416
+ // cached response unless it has a cache-control
417
+ // of 'must-revalidate'
418
+ if (!this.policy.mustRevalidate)
419
+ return this.respond(request.method, options, 'stale')
420
+
421
+ throw err
422
+ }
423
+
424
+ if (this.policy.revalidated(revalidateRequest, response)) {
425
+ // we got a 304, write a new index to the cache and respond from cache
426
+ const metadata = getMetadata(request, response, options)
427
+ // 304 responses do not include headers that are specific to the response data
428
+ // since they do not include a body, so we copy values for headers that were
429
+ // in the old cache entry to the new one, if the new metadata does not already
430
+ // include that header
431
+ for (const name of KEEP_RESPONSE_HEADERS) {
432
+ if (!hasOwnProperty(metadata.resHeaders, name) && hasOwnProperty(this.entry.metadata.resHeaders, name))
433
+ metadata.resHeaders[name] = this.entry.metadata.resHeaders[name]
434
+ }
435
+
436
+ try {
437
+ await cacache.index.insert(options.cachePath, this.key, this.entry.integrity, {
438
+ size: this.entry.size,
439
+ metadata,
440
+ })
441
+ } catch (err) {
442
+ // if updating the cache index fails, we ignore it and
443
+ // respond anyway
444
+ }
445
+ return this.respond(request.method, options, 'revalidated')
446
+ }
447
+
448
+ // if we got a modified response, create a new entry based on it
449
+ const newEntry = new CacheEntry({
450
+ request,
451
+ response,
452
+ options,
453
+ })
454
+
455
+ // respond with the new entry while writing it to the cache
456
+ return newEntry.store('updated')
457
+ }
458
+ }
459
+
460
+ module.exports = CacheEntry
@@ -0,0 +1,10 @@
1
+ class NotCachedError extends Error {
2
+ constructor (url) {
3
+ super(`request to ${url} failed: cache mode is 'only-if-cached' but no cached response is available.`)
4
+ this.code = 'ENOTCACHED'
5
+ }
6
+ }
7
+
8
+ module.exports = {
9
+ NotCachedError,
10
+ }