@vltpkg/registry-client 1.0.7 → 1.0.9

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
@@ -11,6 +11,13 @@ import type { WebAuthChallenge } from './web-auth-challenge.ts';
11
11
  import { oidc } from './oidc.ts';
12
12
  import type { OidcOptions } from './oidc.ts';
13
13
  export { CacheEntry, clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, runtimeTokens, setRuntimeToken, setToken, type JSONObj, type OidcOptions, type Token, type TokenResponse, type WebAuthChallenge, };
14
+ /** A response body read straight out of the cache file. */
15
+ export type CachedBody = {
16
+ /** the cache file it came from */
17
+ path: string;
18
+ /** the body, a view into the file's bytes */
19
+ body: Buffer;
20
+ };
14
21
  export type CacheableMethod = 'GET' | 'HEAD';
15
22
  export declare const isCacheableMethod: (m: unknown) => m is CacheableMethod;
16
23
  export declare const cacheKey: (method: string, url: URL | string) => string;
@@ -148,5 +155,23 @@ export declare class RegistryClient {
148
155
  * hang on the `doneUrl` until it returns a {@link TokenResponse} object.
149
156
  */
150
157
  webAuthOpener({ doneUrl, authUrl }: WebAuthChallenge): Promise<TokenResponse>;
158
+ /**
159
+ * Read a valid cached non-JSON body straight off disk, bypassing the
160
+ * in-memory cache LRU so that a tarball is never held in it.
161
+ *
162
+ * One read: the head is decoded from the same bytes the body comes
163
+ * from, so `cache-unzip` rewriting the head cannot land between the
164
+ * two and hand back a body at a stale offset.
165
+ *
166
+ * Does not log a cache hit -- the caller may still reject the body
167
+ * and fall back to {@link request}, which logs its own.
168
+ *
169
+ * Returns undefined on anything unexpected; callers fall back to
170
+ * {@link request}.
171
+ */
172
+ cachedBody(url: URL | string, options?: {
173
+ integrity?: Integrity;
174
+ method?: CacheableMethod;
175
+ }): CachedBody | undefined;
151
176
  request(url: URL | string, options?: RegistryClientRequestOptions): Promise<CacheEntry>;
152
177
  }
package/dist/index.js CHANGED
@@ -6,6 +6,8 @@ import { logRequest } from '@vltpkg/output';
6
6
  import { urlOpen } from '@vltpkg/url-open';
7
7
  import { XDG } from '@vltpkg/xdg';
8
8
  import { randomUUID } from 'node:crypto';
9
+ import { readFileSync } from 'node:fs';
10
+ import { STATUS_CODES } from 'node:http';
9
11
  import { availableParallelism } from 'node:os';
10
12
  import { dirname, resolve } from 'node:path';
11
13
  import { setTimeout } from 'node:timers/promises';
@@ -25,6 +27,8 @@ import { getTokenResponse } from "./token-response.js";
25
27
  import { getWebAuthChallenge } from "./web-auth-challenge.js";
26
28
  import { collectHeaders, readBody } from "./response.js";
27
29
  import { oidc } from "./oidc.js";
30
+ // eslint-disable-next-line no-console
31
+ const log = (msg) => console.error(msg);
28
32
  export { CacheEntry, clearRuntimeTokens, deleteToken, getKC, getToken, getTokenByURL, isToken, keychains, normalizeRegistryKey, oidc, registryBase, runtimeTokens, setRuntimeToken, setToken, };
29
33
  export const isCacheableMethod = (m) => m === 'GET' || m === 'HEAD';
30
34
  export const cacheKey = (method, url) => `${method !== 'GET' ? method + ' ' : ''}${url}`;
@@ -199,11 +203,16 @@ export class RegistryClient {
199
203
  return;
200
204
  }
201
205
  }
202
- /* c8 ignore start */
203
206
  // TODO: fall back to username/password login, and/or couchdb PUT login
204
- throw error('Failed to perform web login', { response });
207
+ const { statusCode } = response;
208
+ const status = STATUS_CODES[statusCode];
209
+ throw error(`Failed to perform web login: ${statusCode}${status ? ` ${status}` : ''}`, {
210
+ code: 'EREQUEST',
211
+ url: webLoginURL,
212
+ status: statusCode,
213
+ response,
214
+ });
205
215
  }
206
- /* c8 ignore stop */
207
216
  /**
208
217
  * Given a {@link WebAuthChallenge}, open the `authUrl` in a browser and
209
218
  * hang on the `doneUrl` until it returns a {@link TokenResponse} object.
@@ -218,10 +227,14 @@ export class RegistryClient {
218
227
  return result;
219
228
  }),
220
229
  urlOpen(authUrl, { signal }).catch((er) => {
230
+ // an aborted spawn is not a failure, e.g: login completed
231
+ // before the browser process exited
221
232
  if (asError(er).name === 'AbortError')
222
233
  return;
223
- ac.abort();
224
- throw er;
234
+ // the opener may exist but still fail to launch a browser,
235
+ // e.g: xdg-open on a headless server. Keep polling doneUrl so
236
+ // the user can complete the login by opening the url manually.
237
+ log(`Could not open a browser. Please open ${authUrl} manually.`);
225
238
  }),
226
239
  ]);
227
240
  /* c8 ignore stop */
@@ -274,6 +287,67 @@ export class RegistryClient {
274
287
  response,
275
288
  });
276
289
  }
290
+ /**
291
+ * Read a valid cached non-JSON body straight off disk, bypassing the
292
+ * in-memory cache LRU so that a tarball is never held in it.
293
+ *
294
+ * One read: the head is decoded from the same bytes the body comes
295
+ * from, so `cache-unzip` rewriting the head cannot land between the
296
+ * two and hand back a body at a stale offset.
297
+ *
298
+ * Does not log a cache hit -- the caller may still reject the body
299
+ * and fall back to {@link request}, which logs its own.
300
+ *
301
+ * Returns undefined on anything unexpected; callers fall back to
302
+ * {@link request}.
303
+ */
304
+ cachedBody(url, options = {}) {
305
+ const { integrity, method = 'GET' } = options;
306
+ try {
307
+ // normalize like request() does, or a url that is not already in
308
+ // canonical form (an explicit :443, say) keys a different file
309
+ // and the fast path silently never hits.
310
+ const key = cacheKey(method, typeof url === 'string' ? new URL(url) : url);
311
+ // an in-memory value can be newer than disk, since cache.set
312
+ // writes to disk asynchronously. let request() serve those.
313
+ // (an in-flight fetch() peeks as undefined, so the probe reads
314
+ // disk underneath it -- benign, tarball urls are immutable.)
315
+ if (this.cache.peek(key))
316
+ return undefined;
317
+ // same order as the cache's own disk read
318
+ const paths = new Set();
319
+ try {
320
+ const i = this.cache.integrityPath(integrity);
321
+ if (i)
322
+ paths.add(i);
323
+ }
324
+ catch { }
325
+ paths.add(this.cache.path(key));
326
+ for (const path of paths) {
327
+ let buf;
328
+ try {
329
+ buf = readFileSync(path);
330
+ }
331
+ catch {
332
+ continue;
333
+ }
334
+ const entry = CacheEntry.decode(buf);
335
+ // statusCode must stay first: an unparseable buffer decodes to
336
+ // the module-level emptyCacheEntry singleton, and valid/isJSON
337
+ // memoize onto `this`, so reading them off it would poison the
338
+ // singleton process-wide.
339
+ if (entry.statusCode !== 200 ||
340
+ !entry.valid ||
341
+ entry.isJSON) {
342
+ continue;
343
+ }
344
+ return { path, body: entry.buffer() };
345
+ }
346
+ /* c8 ignore next */
347
+ }
348
+ catch { }
349
+ return undefined;
350
+ }
277
351
  async request(url, options = {}) {
278
352
  const u = typeof url === 'string' ? new URL(url) : url;
279
353
  const { method = 'GET', integrity, redirections = new Set(), signal, otp = (process.env.VLT_OTP ?? '').trim(), staleWhileRevalidate = true, } = options;
package/dist/otplease.js CHANGED
@@ -48,7 +48,9 @@ export const otplease = async (client, options, response) => {
48
48
  const notice = String(npmNotice);
49
49
  const match = otpChallengeNotice.exec(notice);
50
50
  if (match?.[1]) {
51
- await urlOpen(match[1]);
51
+ // the opener may fail to launch a browser, e.g: xdg-open on a
52
+ // headless server. The url is printed in the notice below.
53
+ await urlOpen(match[1]).catch(() => { });
52
54
  log(notice);
53
55
  return {
54
56
  retry: {
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.7",
4
+ "version": "1.0.9",
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.7",
17
- "@vltpkg/cache-unzip": "1.0.7",
18
- "@vltpkg/error-cause": "1.0.7",
19
- "@vltpkg/keychain": "1.0.7",
20
- "@vltpkg/output": "1.0.7",
21
- "@vltpkg/types": "1.0.7",
22
- "@vltpkg/url-open": "1.0.7",
23
- "@vltpkg/xdg": "1.0.7",
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",
24
24
  "cache-control-parser": "^2.0.6",
25
25
  "package-json-from-dist": "^1.0.1",
26
26
  "undici": "^7.16.0"