@vltpkg/registry-client 1.0.0-rc.23 → 1.0.0-rc.24

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 (43) hide show
  1. package/dist/add-header.d.ts +1 -0
  2. package/dist/add-header.js +26 -0
  3. package/dist/auth.d.ts +15 -0
  4. package/dist/auth.js +53 -0
  5. package/dist/cache-entry.d.ts +140 -0
  6. package/dist/cache-entry.js +517 -0
  7. package/dist/cache-revalidate.d.ts +1 -0
  8. package/dist/cache-revalidate.js +56 -0
  9. package/dist/delete-header.d.ts +1 -0
  10. package/dist/delete-header.js +31 -0
  11. package/dist/env.d.ts +6 -0
  12. package/dist/env.js +12 -0
  13. package/dist/get-header.d.ts +1 -0
  14. package/dist/get-header.js +36 -0
  15. package/dist/handle-304-response.d.ts +3 -0
  16. package/dist/handle-304-response.js +8 -0
  17. package/dist/index.d.ts +145 -0
  18. package/dist/index.js +326 -0
  19. package/dist/is-cacheable.d.ts +4 -0
  20. package/dist/is-cacheable.js +9 -0
  21. package/dist/is-iterable.d.ts +1 -0
  22. package/dist/is-iterable.js +1 -0
  23. package/dist/oidc.d.ts +17 -0
  24. package/dist/oidc.js +151 -0
  25. package/dist/otplease.d.ts +3 -0
  26. package/dist/otplease.js +62 -0
  27. package/dist/raw-header.d.ts +8 -0
  28. package/dist/raw-header.js +33 -0
  29. package/dist/redirect.d.ts +22 -0
  30. package/dist/redirect.js +64 -0
  31. package/dist/revalidate.d.ts +4 -0
  32. package/dist/revalidate.js +50 -0
  33. package/dist/set-cache-headers.d.ts +3 -0
  34. package/dist/set-cache-headers.js +16 -0
  35. package/dist/set-raw-header.d.ts +5 -0
  36. package/dist/set-raw-header.js +20 -0
  37. package/dist/string-encoding.d.ts +8 -0
  38. package/dist/string-encoding.js +24 -0
  39. package/dist/token-response.d.ts +4 -0
  40. package/dist/token-response.js +7 -0
  41. package/dist/web-auth-challenge.d.ts +5 -0
  42. package/dist/web-auth-challenge.js +13 -0
  43. package/package.json +9 -9
@@ -0,0 +1 @@
1
+ export declare const addHeader: <H extends [string, string[] | string][] | Iterable<[string, string[] | string | undefined]> | Record<string, string[] | string | undefined> | string[]>(headers: H | null | undefined, key: string, value?: string) => H;
@@ -0,0 +1,26 @@
1
+ import { deleteHeader } from "./delete-header.js";
2
+ import { isIterable } from "./is-iterable.js";
3
+ // this does some rude things with types, but not much way around it,
4
+ // since the opts.headers type is so loosey goosey to begin with.
5
+ export const addHeader = (headers, key, value) => {
6
+ if (!value)
7
+ return deleteHeader(headers, key);
8
+ if (!headers)
9
+ return { [key]: value };
10
+ if (Array.isArray(headers)) {
11
+ if (!headers.length)
12
+ return [[key, value]];
13
+ if (Array.isArray(headers[0]))
14
+ headers.push([key, value]);
15
+ else
16
+ headers.push(key, value);
17
+ return headers;
18
+ }
19
+ else if (isIterable(headers)) {
20
+ return [...headers, [key, value]];
21
+ }
22
+ else {
23
+ headers[key] = value;
24
+ return headers;
25
+ }
26
+ };
package/dist/auth.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ import { Keychain } from '@vltpkg/keychain';
2
+ export type Token = `Bearer ${string}` | `Basic ${string}`;
3
+ export declare const keychains: Map<string, Keychain<Token>>;
4
+ /**
5
+ * In-memory token store for OIDC-exchanged tokens.
6
+ * These take precedence over env vars and keychain.
7
+ */
8
+ export declare const runtimeTokens: Map<string, Token>;
9
+ export declare const setRuntimeToken: (registry: string, token: Token) => void;
10
+ export declare const clearRuntimeTokens: () => void;
11
+ export declare const getKC: (identity: string) => Keychain<Token>;
12
+ export declare const isToken: (t: any) => t is Token;
13
+ export declare const deleteToken: (registry: string, identity: string) => Promise<void>;
14
+ export declare const setToken: (registry: string, token: Token, identity: string) => Promise<void>;
15
+ export declare const getToken: (registry: string, identity: string) => Promise<Token | undefined>;
package/dist/auth.js ADDED
@@ -0,0 +1,53 @@
1
+ import { Keychain } from '@vltpkg/keychain';
2
+ // just exported for testing
3
+ export const keychains = new Map();
4
+ /**
5
+ * In-memory token store for OIDC-exchanged tokens.
6
+ * These take precedence over env vars and keychain.
7
+ */
8
+ export const runtimeTokens = new Map();
9
+ export const setRuntimeToken = (registry, token) => {
10
+ runtimeTokens.set(new URL(registry).origin, token);
11
+ };
12
+ export const clearRuntimeTokens = () => {
13
+ runtimeTokens.clear();
14
+ };
15
+ export const getKC = (identity) => {
16
+ const kc = keychains.get(identity);
17
+ if (kc)
18
+ return kc;
19
+ const i = identity ? `vlt/auth/${identity}` : 'vlt/auth';
20
+ const nkc = new Keychain(i);
21
+ keychains.set(identity, nkc);
22
+ return nkc;
23
+ };
24
+ export const isToken = (t) => typeof t === 'string' &&
25
+ (t.startsWith('Bearer ') || t.startsWith('Basic '));
26
+ export const deleteToken = async (registry, identity) => {
27
+ const kc = getKC(identity);
28
+ await kc.load();
29
+ kc.delete(new URL(registry).origin);
30
+ await kc.save();
31
+ };
32
+ export const setToken = async (registry, token, identity) => {
33
+ const kc = getKC(identity);
34
+ return kc.set(new URL(registry).origin, token);
35
+ };
36
+ export const getToken = async (registry, identity) => {
37
+ const kc = getKC(identity);
38
+ registry = new URL(registry).origin;
39
+ // Runtime tokens (e.g. from OIDC exchange) take precedence
40
+ const rt = runtimeTokens.get(registry);
41
+ if (rt)
42
+ return rt;
43
+ const envReg = process.env.VLT_REGISTRY;
44
+ if (envReg && registry === new URL(envReg).origin) {
45
+ const envTok = process.env.VLT_TOKEN;
46
+ if (envTok)
47
+ return `Bearer ${envTok}`;
48
+ }
49
+ const tok = process.env[`VLT_TOKEN_${registry.replace(/[^a-zA-Z0-9]+/g, '_')}`];
50
+ if (tok)
51
+ return `Bearer ${tok}`;
52
+ return kc.get(registry);
53
+ };
@@ -0,0 +1,140 @@
1
+ import type { ErrorCauseOptions } from '@vltpkg/error-cause';
2
+ import type { Integrity, JSONField } from '@vltpkg/types';
3
+ import ccp from 'cache-control-parser';
4
+ import type { InspectOptions } from 'node:util';
5
+ export type JSONObj = Record<string, JSONField>;
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
+ };
51
+ export declare class CacheEntry {
52
+ #private;
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
+ };
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;
64
+ /**
65
+ * Add contents to the entry body.
66
+ */
67
+ addBody(b: Uint8Array): void;
68
+ get statusCode(): number;
69
+ get headers(): Uint8Array[];
70
+ /**
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
76
+ * matches the body that we actually received. This should only be called
77
+ * AFTER the entire body has been completely downloaded.
78
+ *
79
+ * This method **will throw** if the integrity values do not match.
80
+ *
81
+ * Note that this will *usually* not be true if the value is coming out of
82
+ * the cache, because the cache entries are un-gzipped in place. It should
83
+ * _only_ be called for artifacts that come from an actual http response.
84
+ *
85
+ * Returns true if anything was actually verified.
86
+ */
87
+ checkIntegrity(context?: ErrorCauseOptions): this is CacheEntry & {
88
+ integrity: Integrity;
89
+ };
90
+ get integrityActual(): Integrity;
91
+ set integrityActual(i: Integrity);
92
+ set integrity(i: Integrity | undefined);
93
+ get integrity(): Integrity | undefined;
94
+ /**
95
+ * Give it a key, and it'll return the buffer of that header value
96
+ */
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;
102
+ /**
103
+ * Set a header to a specific value
104
+ */
105
+ setHeader(h: string, value: Uint8Array | string): void;
106
+ /**
107
+ * Return the body of the entry as a Buffer
108
+ */
109
+ buffer(): Buffer;
110
+ get body(): Uint8Array | Record<string, any>;
111
+ get isJSON(): boolean;
112
+ get isGzip(): boolean;
113
+ /**
114
+ * Un-gzip encode the body.
115
+ * Returns true if it was previously gzip (so something was done), otherwise
116
+ * returns false.
117
+ */
118
+ unzip(): boolean;
119
+ /**
120
+ * Return the body of the entry as utf8 text
121
+ * Automatically unzips if the content is gzip encoded
122
+ */
123
+ text(): string;
124
+ /**
125
+ * Parse the entry body as JSON and return the result
126
+ */
127
+ json(): JSONObj;
128
+ /**
129
+ * Pass the contents of a @vltpkg/cache.Cache object as a buffer,
130
+ * and this static method will decode it into a CacheEntry representing
131
+ * the cached response.
132
+ */
133
+ static decode(buffer: Uint8Array): CacheEntry;
134
+ static isGzipEntry(buffer: Uint8Array): boolean;
135
+ /**
136
+ * Encode the entry as a single Buffer for writing to the cache
137
+ */
138
+ encode(): Buffer;
139
+ }
140
+ export {};