@vltpkg/registry-client 1.0.9 → 1.0.10

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.
package/dist/index.d.ts CHANGED
@@ -115,6 +115,12 @@ export type RegistryClientRequestOptions = Omit<Dispatcher.RequestOptions, 'meth
115
115
  * @internal
116
116
  */
117
117
  staleWhileRevalidate?: false;
118
+ /**
119
+ * Revalidate a cached response even when it is still within max-age.
120
+ * Conditional request headers are still sent when available.
121
+ * @internal
122
+ */
123
+ forceRevalidate?: boolean;
118
124
  };
119
125
  export declare const userAgent: string;
120
126
  export declare class RegistryClient {
package/dist/index.js CHANGED
@@ -350,7 +350,7 @@ export class RegistryClient {
350
350
  }
351
351
  async request(url, options = {}) {
352
352
  const u = typeof url === 'string' ? new URL(url) : url;
353
- const { method = 'GET', integrity, redirections = new Set(), signal, otp = (process.env.VLT_OTP ?? '').trim(), staleWhileRevalidate = true, } = options;
353
+ const { method = 'GET', integrity, redirections = new Set(), signal, otp = (process.env.VLT_OTP ?? '').trim(), staleWhileRevalidate = true, forceRevalidate = false, } = options;
354
354
  let { trustIntegrity } = options;
355
355
  const m = isCacheableMethod(method) ? method : undefined;
356
356
  const { useCache = !!m } = options;
@@ -363,11 +363,21 @@ export class RegistryClient {
363
363
  await this.cache.fetch(key, { context: { integrity } })
364
364
  : undefined;
365
365
  const entry = buffer ? this.#decodeCached(buffer) : undefined;
366
- if (entry?.valid) {
366
+ if (entry?.valid && !forceRevalidate) {
367
367
  logRequest(url, 'cache', { method });
368
368
  return entry;
369
369
  }
370
- if (staleWhileRevalidate && entry?.staleWhileRevalidate && m) {
370
+ // what the two early returns below would have served had
371
+ // forceRevalidate not skipped them. read once: staleWhileRevalidate
372
+ // memoizes a deadline, not an answer, so an entry can expire during
373
+ // a slow failure and the fallback would silently miss.
374
+ const forcedFallback = forceRevalidate && entry?.staleWhileRevalidate ?
375
+ entry
376
+ : undefined;
377
+ if (!forceRevalidate &&
378
+ staleWhileRevalidate &&
379
+ entry?.staleWhileRevalidate &&
380
+ m) {
371
381
  // revalidate while returning the stale entry
372
382
  register(dirname(this.cache.path()), m, url);
373
383
  logRequest(url, 'stale', { method });
@@ -397,6 +407,17 @@ export class RegistryClient {
397
407
  /* c8 ignore start */
398
408
  }
399
409
  catch (er) {
410
+ // a forced revalidation that can't reach the registry must be no
411
+ // worse than the entry it skipped past. also covers 5xx/429, which
412
+ // land here once RetryAgent gives up. an abort is the caller's own
413
+ // doing, so it still rejects.
414
+ if (forcedFallback &&
415
+ !signal?.aborted) {
416
+ // 'stale', not 'cache': a 'start' was already logged, and unlike
417
+ // the swr path above nothing revalidates in the background.
418
+ logRequest(url, 'stale', { method });
419
+ return forcedFallback;
420
+ }
400
421
  // Rethrow so we get a better stack trace
401
422
  throw error('Request failed', {
402
423
  code: 'EREQUEST',
@@ -418,7 +439,16 @@ export class RegistryClient {
418
439
  if (result.isGzip && !trustIntegrity) {
419
440
  result.checkIntegrity({ url });
420
441
  }
421
- if (useCache) {
442
+ // a forced revalidation must never replace a cached entry with an
443
+ // error response -- the flat 200-only rule revalidate-entry.ts has.
444
+ // keyed on entry existing at all, not on validity: staleWhileRevalidate
445
+ // is true forever for an entry with no date header, which would make
446
+ // one permanently unreplaceable.
447
+ // safe on the 304 path: handleCacheHitResponse returns the cached
448
+ // entry, whose statusCode is the original 200, so the date refresh
449
+ // still gets written back.
450
+ const clobbersCachedEntry = forceRevalidate && !!entry && result.statusCode !== 200;
451
+ if (useCache && !clobbersCachedEntry) {
422
452
  // Get the encoded buffer from the cache entry
423
453
  const buffer = result.encode();
424
454
  this.cache.set(key, Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength), {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/registry-client",
3
3
  "description": "Fetch package artifacts and metadata from registries",
4
- "version": "1.0.9",
4
+ "version": "1.0.10",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -13,14 +13,14 @@
13
13
  "url": "http://vlt.sh"
14
14
  },
15
15
  "dependencies": {
16
- "@vltpkg/cache": "1.0.9",
17
- "@vltpkg/cache-unzip": "1.0.9",
18
- "@vltpkg/error-cause": "1.0.9",
19
- "@vltpkg/keychain": "1.0.9",
20
- "@vltpkg/output": "1.0.9",
21
- "@vltpkg/types": "1.0.9",
22
- "@vltpkg/url-open": "1.0.9",
23
- "@vltpkg/xdg": "1.0.9",
16
+ "@vltpkg/cache": "1.0.10",
17
+ "@vltpkg/cache-unzip": "1.0.10",
18
+ "@vltpkg/error-cause": "1.0.10",
19
+ "@vltpkg/keychain": "1.0.10",
20
+ "@vltpkg/output": "1.0.10",
21
+ "@vltpkg/types": "1.0.10",
22
+ "@vltpkg/url-open": "1.0.10",
23
+ "@vltpkg/xdg": "1.0.10",
24
24
  "cache-control-parser": "^2.0.6",
25
25
  "package-json-from-dist": "^1.0.1",
26
26
  "undici": "^7.16.0"