@pnpm/network.fetch 1100.0.2 → 1100.0.4

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/lib/dispatcher.js CHANGED
@@ -23,7 +23,44 @@ setGlobalDispatcher(new Agent({
23
23
  connect: {
24
24
  autoSelectFamily: true,
25
25
  },
26
- }));
26
+ }).compose(stripSecFetchHeaders));
27
+ // undici's fetch() automatically adds sec-fetch-* headers (e.g. sec-fetch-mode: cors)
28
+ // per the Fetch spec. Some registries like Azure DevOps Artifacts interpret these as
29
+ // browser requests and reject them with HTTP 400. Since pnpm is a CLI tool, these
30
+ // headers serve no purpose and must be stripped.
31
+ // See https://github.com/pnpm/pnpm/issues/11572
32
+ function stripSecFetchHeaders(dispatch) {
33
+ return (opts, handler) => {
34
+ if (opts.headers) {
35
+ if (Array.isArray(opts.headers)) {
36
+ // Flat array format: [key1, val1, key2, val2, ...]
37
+ const filtered = [];
38
+ for (let i = 0; i < opts.headers.length; i += 2) {
39
+ if (!opts.headers[i].toLowerCase().startsWith('sec-fetch-')) {
40
+ filtered.push(opts.headers[i], opts.headers[i + 1]);
41
+ }
42
+ }
43
+ opts = { ...opts, headers: filtered };
44
+ }
45
+ else if (typeof opts.headers === 'object') {
46
+ // undici also accepts an iterable of [key, value] pairs (e.g. a Map or
47
+ // web Headers). Use that iterator when present; otherwise fall back to
48
+ // Object.entries for plain IncomingHttpHeaders objects.
49
+ const entries = Symbol.iterator in opts.headers
50
+ ? opts.headers
51
+ : Object.entries(opts.headers);
52
+ const headers = {};
53
+ for (const [key, value] of entries) {
54
+ if (!key.toLowerCase().startsWith('sec-fetch-')) {
55
+ headers[key] = value;
56
+ }
57
+ }
58
+ opts = { ...opts, headers };
59
+ }
60
+ }
61
+ return dispatch(opts, handler);
62
+ };
63
+ }
27
64
  const DISPATCHER_CACHE = new LRUCache({
28
65
  max: 50,
29
66
  dispose: (dispatcher) => {
@@ -130,6 +167,7 @@ function getProxyDispatcher(parsedUri, opts) {
130
167
  else {
131
168
  dispatcher = createHttpProxyDispatcher(proxyUrl, isHttps, opts, { ca, cert, key: certKey });
132
169
  }
170
+ dispatcher = dispatcher.compose(stripSecFetchHeaders);
133
171
  DISPATCHER_CACHE.set(key, dispatcher);
134
172
  return dispatcher;
135
173
  }
@@ -247,8 +285,9 @@ function getNonProxyDispatcher(parsedUri, opts) {
247
285
  localAddress: opts.localAddress,
248
286
  },
249
287
  });
250
- DISPATCHER_CACHE.set(key, agent);
251
- return agent;
288
+ const dispatcher = agent.compose(stripSecFetchHeaders);
289
+ DISPATCHER_CACHE.set(key, dispatcher);
290
+ return dispatcher;
252
291
  }
253
292
  function checkNoProxy(parsedUri, opts) {
254
293
  const host = parsedUri.hostname
@@ -7,6 +7,20 @@ export interface FetchWithDispatcherOptions extends RequestInit {
7
7
  dispatcherOptions: DispatcherOptions;
8
8
  }
9
9
  export declare function fetchWithDispatcher(url: string | URL, opts: FetchWithDispatcherOptions): Promise<Response>;
10
+ export interface CreateDispatchedFetchOptions extends DispatcherOptions {
11
+ /**
12
+ * Per-registry config (TLS, auth, etc.). When set, the matching TLS entries
13
+ * are automatically extracted into `clientCertificates` so callers don't
14
+ * have to do it themselves.
15
+ */
16
+ configByUri?: Record<string, RegistryConfig>;
17
+ }
18
+ /**
19
+ * Returns a {@link fetch} pre-bound to the given dispatcher options, so callers
20
+ * that need a fetch function (rather than a one-shot call) can route their
21
+ * requests through the configured proxy / TLS / local-address settings.
22
+ */
23
+ export declare function createDispatchedFetch(opts: CreateDispatchedFetchOptions): (url: string | URL, opts?: RequestInit) => Promise<Response>;
10
24
  export type { DispatcherOptions };
11
25
  export interface CreateFetchFromRegistryOptions extends DispatcherOptions {
12
26
  userAgent?: string;
@@ -17,7 +17,20 @@ export function fetchWithDispatcher(url, opts) {
17
17
  dispatcher,
18
18
  });
19
19
  }
20
+ /**
21
+ * Returns a {@link fetch} pre-bound to the given dispatcher options, so callers
22
+ * that need a fetch function (rather than a one-shot call) can route their
23
+ * requests through the configured proxy / TLS / local-address settings.
24
+ */
25
+ export function createDispatchedFetch(opts) {
26
+ const dispatcherOptions = {
27
+ ...opts,
28
+ clientCertificates: opts.clientCertificates ?? extractTlsConfigs(opts.configByUri),
29
+ };
30
+ return (url, fetchOpts) => fetchWithDispatcher(url, { ...fetchOpts, dispatcherOptions });
31
+ }
20
32
  export function createFetchFromRegistry(defaultOpts) {
33
+ const clientCertificates = extractTlsConfigs(defaultOpts.configByUri);
21
34
  return async (url, opts) => {
22
35
  const headers = {
23
36
  'user-agent': USER_AGENT,
@@ -51,7 +64,7 @@ export function createFetchFromRegistry(defaultOpts) {
51
64
  ...defaultOpts,
52
65
  ...opts,
53
66
  strictSsl: defaultOpts.strictSsl ?? true,
54
- clientCertificates: extractTlsConfigs(defaultOpts.configByUri),
67
+ clientCertificates,
55
68
  };
56
69
  const response = await fetchWithDispatcher(urlObject, {
57
70
  dispatcherOptions,
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { clearDispatcherCache, getDispatcher } from './dispatcher.js';
2
2
  export { fetch, isRedirect, type RetryTimeoutOptions } from './fetch.js';
3
- export { createFetchFromRegistry, type CreateFetchFromRegistryOptions, type DispatcherOptions, fetchWithDispatcher } from './fetchFromRegistry.js';
3
+ export { createDispatchedFetch, type CreateDispatchedFetchOptions, createFetchFromRegistry, type CreateFetchFromRegistryOptions, type DispatcherOptions, fetchWithDispatcher } from './fetchFromRegistry.js';
4
4
  export type { FetchFromRegistry } from '@pnpm/fetching.types';
package/lib/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export { clearDispatcherCache, getDispatcher } from './dispatcher.js';
2
2
  export { fetch, isRedirect } from './fetch.js';
3
- export { createFetchFromRegistry, fetchWithDispatcher } from './fetchFromRegistry.js';
3
+ export { createDispatchedFetch, createFetchFromRegistry, fetchWithDispatcher } from './fetchFromRegistry.js';
4
4
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pnpm/network.fetch",
3
- "version": "1100.0.2",
3
+ "version": "1100.0.4",
4
4
  "description": "Native fetch with retries",
5
5
  "keywords": [
6
6
  "pnpm",
@@ -31,10 +31,10 @@
31
31
  "lru-cache": "^11.2.7",
32
32
  "socks": "^2.8.1",
33
33
  "undici": "^7.2.0",
34
- "@pnpm/core-loggers": "1100.0.1",
34
+ "@pnpm/core-loggers": "1100.0.2",
35
+ "@pnpm/error": "1100.0.0",
35
36
  "@pnpm/fetching.types": "1100.0.1",
36
- "@pnpm/types": "1101.0.0",
37
- "@pnpm/error": "1100.0.0"
37
+ "@pnpm/types": "1101.1.0"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "@pnpm/logger": ">=1001.0.0 <1002.0.0"
@@ -43,7 +43,7 @@
43
43
  "@jest/globals": "30.3.0",
44
44
  "https-proxy-server-express": "0.1.2",
45
45
  "@pnpm/logger": "1100.0.0",
46
- "@pnpm/network.fetch": "1100.0.2"
46
+ "@pnpm/network.fetch": "1100.0.4"
47
47
  },
48
48
  "engines": {
49
49
  "node": ">=22.13"