@vltpkg/registry-client 0.0.0-3 → 0.0.0-31

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 (47) hide show
  1. package/README.md +50 -53
  2. package/dist/esm/cache-entry.d.ts +84 -14
  3. package/dist/esm/cache-entry.d.ts.map +1 -1
  4. package/dist/esm/cache-entry.js +284 -90
  5. package/dist/esm/cache-entry.js.map +1 -1
  6. package/dist/esm/cache-revalidate.d.ts +2 -0
  7. package/dist/esm/cache-revalidate.d.ts.map +1 -0
  8. package/dist/esm/cache-revalidate.js +66 -0
  9. package/dist/esm/cache-revalidate.js.map +1 -0
  10. package/dist/esm/handle-304-response.js +1 -1
  11. package/dist/esm/handle-304-response.js.map +1 -1
  12. package/dist/esm/index.d.ts +40 -9
  13. package/dist/esm/index.d.ts.map +1 -1
  14. package/dist/esm/index.js +100 -55
  15. package/dist/esm/index.js.map +1 -1
  16. package/dist/esm/otplease.d.ts.map +1 -1
  17. package/dist/esm/otplease.js +33 -25
  18. package/dist/esm/otplease.js.map +1 -1
  19. package/dist/esm/raw-header.d.ts +3 -3
  20. package/dist/esm/raw-header.d.ts.map +1 -1
  21. package/dist/esm/raw-header.js +12 -11
  22. package/dist/esm/raw-header.js.map +1 -1
  23. package/dist/esm/redirect.d.ts +1 -1
  24. package/dist/esm/redirect.d.ts.map +1 -1
  25. package/dist/esm/redirect.js +5 -1
  26. package/dist/esm/redirect.js.map +1 -1
  27. package/dist/esm/revalidate.d.ts +5 -0
  28. package/dist/esm/revalidate.d.ts.map +1 -0
  29. package/dist/esm/revalidate.js +55 -0
  30. package/dist/esm/revalidate.js.map +1 -0
  31. package/dist/esm/set-raw-header.d.ts +1 -1
  32. package/dist/esm/set-raw-header.d.ts.map +1 -1
  33. package/dist/esm/set-raw-header.js +6 -4
  34. package/dist/esm/set-raw-header.js.map +1 -1
  35. package/dist/esm/string-encoding.d.ts +9 -0
  36. package/dist/esm/string-encoding.d.ts.map +1 -0
  37. package/dist/esm/string-encoding.js +25 -0
  38. package/dist/esm/string-encoding.js.map +1 -0
  39. package/dist/esm/token-response.d.ts +1 -1
  40. package/dist/esm/token-response.d.ts.map +1 -1
  41. package/dist/esm/token-response.js +5 -2
  42. package/dist/esm/token-response.js.map +1 -1
  43. package/dist/esm/web-auth-challenge.d.ts +2 -2
  44. package/dist/esm/web-auth-challenge.d.ts.map +1 -1
  45. package/dist/esm/web-auth-challenge.js +13 -6
  46. package/dist/esm/web-auth-challenge.js.map +1 -1
  47. package/package.json +20 -20
package/README.md CHANGED
@@ -2,76 +2,73 @@
2
2
 
3
3
  # @vltpkg/registry-client
4
4
 
5
- This is a very light wrapper around undici, optimized for interfacing with an npm registry.
5
+ This is a very light wrapper around undici, optimized for interfacing
6
+ with an npm registry.
6
7
 
7
- **[Cache Unzipped](#cache-unzipped)**
8
- ·
9
- **[Integrity Options](#integrity-options)**
10
- ·
11
- **[Usage](#usage)**
8
+ **[Cache Unzipped](#cache-unzipped)** ·
9
+ **[Integrity Options](#integrity-options)** · **[Usage](#usage)**
12
10
 
13
11
  ## Overview
14
12
 
15
- Any response with `immutable` in the `cache-control` header, or with a `content-type` of `application/octet-stream` or a path ending in `.tgz`, will be cached forever and never requested again as long as the cache survives.
13
+ Any response with `immutable` in the `cache-control` header, or with a
14
+ `content-type` of `application/octet-stream` or a path ending in
15
+ `.tgz`, will be cached forever and never requested again as long as
16
+ the cache survives.
16
17
 
17
18
  If the request has a cached response:
18
19
 
19
- - Cached responses with `immutable` in the `cache-control`
20
- header will be returned from cache without a network request,
21
- no matter what.
22
- - Cached responses with a `content-type` of
23
- `application/octet-stream` will be returned from cache without
24
- a network request, no matter what, because tarballs are
25
- immutable.
20
+ - Cached responses with `immutable` in the `cache-control` header will
21
+ be returned from cache without a network request, no matter what.
22
+ - Cached responses with a `content-type` of `application/octet-stream`
23
+ will be returned from cache without a network request, no matter
24
+ what, because tarballs are immutable.
26
25
  - Cached responses with `max-age=<n>` or `s-max-age=<n>` will be
27
- served from cache without a network request if it's less than
28
- `<n>` seconds old.
26
+ served from cache without a network request if it's less than `<n>`
27
+ seconds old.
29
28
  - Otherwise, a network request to the registry will be made
30
- - if an `etag` is present in the cached response, it will be
31
- used as the `if-none-match` header.
32
- - If a `last-modified` header is in the response, that will
33
- be used as the `if-modified-since` request header.
34
- - If there is no `last-modified` header, then use the `mtime`
35
- of the cache file as the `if-modified-since` header.
29
+ - if an `etag` is present in the cached response, it will be used as
30
+ the `if-none-match` header.
31
+ - If a `last-modified` header is in the response, that will be used
32
+ as the `if-modified-since` request header.
33
+ - If there is no `last-modified` header, then use the `mtime` of the
34
+ cache file as the `if-modified-since` header.
36
35
 
37
36
  This is the extent of the cache control logic. It is not a
38
- full-featured spec-compliant caching HTTP client, because that is
39
- not needed for this use case. Every response will be cached, even
40
- if the registry headers don't technically allow it.
37
+ full-featured spec-compliant caching HTTP client, because that is not
38
+ needed for this use case. Every response will be cached, even if the
39
+ registry headers don't technically allow it.
41
40
 
42
41
  ## Cache Unzipped
43
42
 
44
- Client always sends `accept-encoding: gzip;q=1.0, *;q=0.5`
45
- header when making requests, to save time on the wire.
43
+ Client always sends `accept-encoding: gzip;q=1.0, *;q=0.5` header when
44
+ making requests, to save time on the wire.
46
45
 
47
- If response has `content-encoding: gzip`, then we swap out the
48
- body for the unzipped response body in the cache, as if it was
49
- not gzipped in the first place. This _must_ be done before
50
- returning the response, because you can't `JSON.parse()` a
51
- gzipped response anyway.
46
+ If response has `content-encoding: gzip`, then we swap out the body
47
+ for the unzipped response body in the cache, as if it was not gzipped
48
+ in the first place. This _must_ be done before returning the response,
49
+ because you can't `JSON.parse()` a gzipped response anyway.
52
50
 
53
- If the response is `content-type: application/octet-stream` and
54
- starts with the gzip header, then we return the raw body as we
55
- received it, but as a best-effort background job, unzip it and
56
- update the cache entry to be an unzipped response body. This is
57
- done in the `@vltpkg/cache-unzip` worker.
51
+ If the response is `content-type: application/octet-stream` and starts
52
+ with the gzip header, then we return the raw body as we received it,
53
+ but as a best-effort background job, unzip it and update the cache
54
+ entry to be an unzipped response body. This is done in the
55
+ `@vltpkg/cache-unzip` child process.
58
56
 
59
57
  So,
60
58
 
61
- - json responses will always be un-zipped, in the response and in
62
- the cache.
63
- - artifact responses _may_ be gzipped (and thus, have to be
64
- unzipped by the unpack operation), but will eventually be
65
- cached as unzipped tarballs.
59
+ - json responses will always be un-zipped, in the response and in the
60
+ cache.
61
+ - artifact responses _may_ be gzipped (and thus, have to be unzipped
62
+ by the unpack operation), but will eventually be cached as unzipped
63
+ tarballs.
66
64
 
67
- Thus, the `content-length` response header will _usually_ not
68
- match the actual byte length of the response body.
65
+ Thus, the `content-length` response header will _usually_ not match
66
+ the actual byte length of the response body.
69
67
 
70
68
  ## Integrity Options
71
69
 
72
- An `integrity` option may be specified in a
73
- `fetchOptions.context` object, or in the options provided to
74
- `cache.set()`. For example:
70
+ An `integrity` option may be specified in a `fetchOptions.context`
71
+ object, or in the options provided to `cache.set()`. For example:
75
72
 
76
73
  ```js
77
74
  const integrity = `sha512-${base64hash}`
@@ -86,16 +83,16 @@ cache.set(key, value, { integrity })
86
83
  const value = await cache.fetch(key, { context: { integrity } })
87
84
  ```
88
85
 
89
- If the integrity provided is obviously not a valid sha512
90
- `Integrity` string, then it is ignored.
86
+ If the integrity provided is obviously not a valid sha512 `Integrity`
87
+ string, then it is ignored.
91
88
 
92
- Integrity values are not calculated or verified. The caller must do this
93
- check, if desired.
89
+ Integrity values are not calculated or verified. The caller must do
90
+ this check, if desired.
94
91
 
95
92
  Note that the integrity provided to `cache.fetch()` or `cache.set()`
96
93
  does _not_ typically match the calculated integrity of the object
97
- being cached. Typically, the integrity is related to the body of
98
- the response that a `@vltpkg/registry-client.CacheEntry` object
94
+ being cached. Typically, the integrity is related to the body of the
95
+ response that a `@vltpkg/registry-client.CacheEntry` object
99
96
  represents.
100
97
 
101
98
  ## Usage
@@ -1,44 +1,113 @@
1
+ import type { ErrorCauseOptions } from '@vltpkg/error-cause';
1
2
  import type { Integrity, JSONField } from '@vltpkg/types';
2
- import type { InspectOptions } from 'util';
3
+ import ccp from 'cache-control-parser';
4
+ import type { InspectOptions } from 'node:util';
3
5
  export type JSONObj = Record<string, JSONField>;
4
6
  declare const kCustomInspect: unique symbol;
7
+ export type CacheEntryOptions = {
8
+ /**
9
+ * An optional body to use.
10
+ *
11
+ * This is used when decoding a cache entry from a buffer, and the body
12
+ * is already in a ArrayBuffer we can use. When this option is
13
+ * provided the `addBody` method should not be used.
14
+ */
15
+ body?: Uint8Array;
16
+ /**
17
+ * An optional content length of the body to use, if undefined the
18
+ * content-length header will be used.
19
+ */
20
+ contentLength?: number;
21
+ /**
22
+ * The expected integrity value for this response body
23
+ */
24
+ integrity?: Integrity;
25
+ /**
26
+ * Whether to trust the integrity, or calculate the actual value.
27
+ *
28
+ * This indicates that we just accept whatever the integrity is as the actual
29
+ * integrity for saving back to the cache, because it's coming directly from
30
+ * the registry that we fetched a packument from, and is an initial gzipped
31
+ * artifact request.
32
+ */
33
+ trustIntegrity?: boolean;
34
+ /**
35
+ * If the server does not serve a `stale-while-revalidate` value in the
36
+ * `cache-control` header, then this multiplier is applied to the `max-age`
37
+ * or `s-maxage` values.
38
+ *
39
+ * By default, this is `60`, so for example a response that is cacheable for
40
+ * 5 minutes will allow a stale response while revalidating for up to 5
41
+ * hours.
42
+ *
43
+ * If the server *does* provide a `stale-while-revalidate` value, then that
44
+ * is always used.
45
+ *
46
+ * Set to 0 to prevent any `stale-while-revalidate` behavior unless
47
+ * explicitly allowed by the server's `cache-control` header.
48
+ */
49
+ 'stale-while-revalidate-factor'?: number;
50
+ };
5
51
  export declare class CacheEntry {
6
52
  #private;
7
- constructor(statusCode: number, headers: Buffer[], integrity?: Integrity);
53
+ constructor(statusCode: number, headers: Uint8Array[], { body, integrity, trustIntegrity, 'stale-while-revalidate-factor': staleWhileRevalidateFactor, contentLength, }?: CacheEntryOptions);
54
+ toJSON(): {
55
+ [k: string]: string | number | boolean | [string, string][] | Date | ccp.CacheControl | undefined;
56
+ };
8
57
  [kCustomInspect](depth: number, options: InspectOptions): string;
58
+ get date(): Date | undefined;
59
+ get maxAge(): number;
60
+ get cacheControl(): ccp.CacheControl;
61
+ get staleWhileRevalidate(): boolean;
62
+ get contentType(): string;
63
+ get valid(): boolean;
9
64
  /**
10
- * `true` if the entry represents a cached response that is still
11
- * valid to use.
65
+ * Add contents to the entry body.
12
66
  */
13
- get valid(): boolean;
14
- addBody(b: Buffer): void;
67
+ addBody(b: Uint8Array): void;
15
68
  get statusCode(): number;
16
- get headers(): Buffer<ArrayBufferLike>[];
69
+ get headers(): Uint8Array[];
17
70
  /**
18
- * check that the sri integrity string that was provided to the ctor
71
+ * Returns the body as a single Uint8Array, concatenating parts if needed.
72
+ */
73
+ get _body(): Uint8Array;
74
+ /**
75
+ * Check that the sri integrity string that was provided to the ctor
19
76
  * matches the body that we actually received. This should only be called
20
77
  * AFTER the entire body has been completely downloaded.
21
78
  *
79
+ * This method **will throw** if the integrity values do not match.
80
+ *
22
81
  * Note that this will *usually* not be true if the value is coming out of
23
82
  * the cache, because the cache entries are un-gzipped in place. It should
24
83
  * _only_ be called for artifacts that come from an actual http response.
84
+ *
85
+ * Returns true if anything was actually verified.
25
86
  */
26
- checkIntegrity(): boolean;
87
+ checkIntegrity(context?: ErrorCauseOptions): this is CacheEntry & {
88
+ integrity: Integrity;
89
+ };
27
90
  get integrityActual(): Integrity;
28
- get integrity(): `sha512-${string}` | undefined;
91
+ set integrityActual(i: Integrity);
92
+ set integrity(i: Integrity | undefined);
93
+ get integrity(): Integrity | undefined;
29
94
  /**
30
95
  * Give it a key, and it'll return the buffer of that header value
31
96
  */
32
- getHeader(h: string): Buffer<ArrayBufferLike> | undefined;
97
+ getHeader(h: string): Uint8Array | undefined;
98
+ /**
99
+ * Give it a key, and it'll return the decoded string of that header value
100
+ */
101
+ getHeaderString(h: string): string | undefined;
33
102
  /**
34
103
  * Set a header to a specific value
35
104
  */
36
- setHeader(h: string, value: Buffer | string): void;
105
+ setHeader(h: string, value: Uint8Array | string): void;
37
106
  /**
38
107
  * Return the body of the entry as a Buffer
39
108
  */
40
109
  buffer(): Buffer;
41
- get body(): Buffer | Record<string, any>;
110
+ get body(): Uint8Array | Record<string, any>;
42
111
  get isJSON(): boolean;
43
112
  get isGzip(): boolean;
44
113
  /**
@@ -61,7 +130,8 @@ export declare class CacheEntry {
61
130
  * and this static method will decode it into a CacheEntry representing
62
131
  * the cached response.
63
132
  */
64
- static decode(buffer: Buffer): CacheEntry;
133
+ static decode(buffer: Uint8Array): CacheEntry;
134
+ static isGzipEntry(buffer: Uint8Array): boolean;
65
135
  /**
66
136
  * Encode the entry as a single Buffer for writing to the cache
67
137
  */
@@ -1 +1 @@
1
- {"version":3,"file":"cache-entry.d.ts","sourceRoot":"","sources":["../../src/cache-entry.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAIzD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAA;AAI1C,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;AAyB/C,QAAA,MAAM,cAAc,eAA2C,CAAA;AAE/D,qBAAa,UAAU;;gBAUnB,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EAAE,EACjB,SAAS,CAAC,EAAE,SAAS;IAiBvB,CAAC,cAAc,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,GAAG,MAAM;IAehE;;;OAGG;IACH,IAAI,KAAK,IAAI,OAAO,CAsBnB;IAED,OAAO,CAAC,CAAC,EAAE,MAAM;IAKjB,IAAI,UAAU,WAEb;IACD,IAAI,OAAO,8BAEV;IAED;;;;;;;;OAQG;IACH,cAAc,IAAI,OAAO;IAIzB,IAAI,eAAe,IAAI,SAAS,CAM/B;IACD,IAAI,SAAS,mCAEZ;IAED;;OAEG;IACH,SAAS,CAAC,C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