@pnpm/network.auth-header 1101.1.5 → 1101.1.6

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/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @pnpm/network.auth-header
2
2
 
3
+ ## 1101.1.6
4
+
5
+ ### Patch Changes
6
+
7
+ - Republished every package: the tarballs published by the v11.13.1 through v11.16.0 releases were missing most of their compiled files due to a packing bug [#13164](https://github.com/pnpm/pnpm/issues/13164).
8
+
9
+ - Updated dependencies:
10
+ - @pnpm/error@1100.1.0
11
+ - @pnpm/types@1101.6.0
12
+
3
13
  ## 1101.1.5
4
14
 
5
15
  ### Patch Changes
@@ -0,0 +1,9 @@
1
+ import { type RegistryConfig, type TokenHelper } from '@pnpm/types';
2
+ export interface AuthHeaders {
3
+ authHeaderValueByURI: Record<string, string>;
4
+ scopedAuthHeaderValueByURI: Record<string, Record<string, string>>;
5
+ }
6
+ export type AuthHeadersByScope = Record<string, Record<string, string>>;
7
+ export declare function getAuthHeadersFromCreds(configByUri: Record<string, RegistryConfig>): AuthHeaders;
8
+ export declare function getAuthHeadersByScope(authHeaders: AuthHeaders): AuthHeadersByScope;
9
+ export declare function executeTokenHelper(tokenHelper: TokenHelper, timeoutMs?: number): string;
@@ -0,0 +1,92 @@
1
+ import { spawnSync } from 'node:child_process';
2
+ import { PnpmError } from '@pnpm/error';
3
+ import { DEFAULT_REGISTRY_SCOPE } from '@pnpm/types';
4
+ export function getAuthHeadersFromCreds(configByUri) {
5
+ const authHeaders = {
6
+ authHeaderValueByURI: {},
7
+ scopedAuthHeaderValueByURI: {},
8
+ };
9
+ for (const [uri, registryConfig] of Object.entries(configByUri)) {
10
+ const normalizedUri = normalizeAuthKey(uri);
11
+ const header = credsToHeader(registryConfig[DEFAULT_REGISTRY_SCOPE]);
12
+ if (header) {
13
+ authHeaders.authHeaderValueByURI[normalizedUri] = header;
14
+ }
15
+ for (const scope of getRegistryScopes(registryConfig)) {
16
+ if (scope === DEFAULT_REGISTRY_SCOPE)
17
+ continue;
18
+ const scopedCreds = registryConfig[scope];
19
+ const scopedHeader = credsToHeader(scopedCreds);
20
+ if (scopedHeader) {
21
+ authHeaders.scopedAuthHeaderValueByURI[normalizedUri] ??= {};
22
+ authHeaders.scopedAuthHeaderValueByURI[normalizedUri][scope] = scopedHeader;
23
+ }
24
+ }
25
+ }
26
+ return authHeaders;
27
+ }
28
+ export function getAuthHeadersByScope(authHeaders) {
29
+ const result = {};
30
+ for (const [registryURI, authHeader] of Object.entries(authHeaders.authHeaderValueByURI)) {
31
+ result[registryURI] ??= {};
32
+ result[registryURI][DEFAULT_REGISTRY_SCOPE] = authHeader;
33
+ }
34
+ for (const [registryURI, scopedAuthHeaders] of Object.entries(authHeaders.scopedAuthHeaderValueByURI)) {
35
+ result[registryURI] ??= {};
36
+ for (const [scope, authHeader] of Object.entries(scopedAuthHeaders)) {
37
+ result[registryURI][scope] = authHeader;
38
+ }
39
+ }
40
+ return result;
41
+ }
42
+ function getRegistryScopes(registryConfig) {
43
+ return Object.keys(registryConfig).filter((scope) => scope.startsWith('@'));
44
+ }
45
+ function normalizeAuthKey(uri) {
46
+ if (!uri)
47
+ return uri;
48
+ return uri.endsWith('/') ? uri : `${uri}/`;
49
+ }
50
+ function credsToHeader(creds) {
51
+ if (!creds)
52
+ return undefined;
53
+ if (creds.tokenHelper) {
54
+ return executeTokenHelper(creds.tokenHelper);
55
+ }
56
+ if (creds.authToken) {
57
+ return `Bearer ${creds.authToken}`;
58
+ }
59
+ if (creds.basicAuth) {
60
+ return `Basic ${Buffer.from(`${creds.basicAuth.username}:${creds.basicAuth.password}`, 'utf8').toString('base64')}`;
61
+ }
62
+ return undefined;
63
+ }
64
+ // A token helper only prints a token, so this is a generous bound that turns a
65
+ // hung helper (deadlock, stuck I/O) into a clear error instead of a command
66
+ // that hangs forever. Matches pacquet's `TOKEN_HELPER_TIMEOUT`.
67
+ const TOKEN_HELPER_TIMEOUT = 60_000;
68
+ export function executeTokenHelper(tokenHelper, timeoutMs = TOKEN_HELPER_TIMEOUT) {
69
+ const [cmd, ...args] = tokenHelper;
70
+ // On Windows, .bat/.cmd files require a shell to execute.
71
+ const shell = process.platform === 'win32' && /\.(?:bat|cmd)$/i.test(cmd);
72
+ const spawnResult = spawnSync(cmd, args, { stdio: 'pipe', shell, timeout: timeoutMs });
73
+ // A helper that outlives the timeout is killed; spawnSync then reports the
74
+ // kill signal rather than a clean exit, so surface it as a distinct error.
75
+ if (spawnResult.error != null && spawnResult.error.code === 'ETIMEDOUT') {
76
+ throw new PnpmError('TOKEN_HELPER_TIMEOUT', `Token helper "${cmd}" timed out after ${timeoutMs} ms`);
77
+ }
78
+ if (spawnResult.status !== 0) {
79
+ throw new PnpmError('TOKEN_HELPER_ERROR_STATUS', `Error running "${cmd}" as a token helper. Exit code ${spawnResult.status?.toString() ?? ''}`);
80
+ }
81
+ const token = spawnResult.stdout.toString('utf8').trimEnd();
82
+ if (!token) {
83
+ throw new PnpmError('TOKEN_HELPER_EMPTY_TOKEN', `Token helper "${cmd}" returned an empty token`);
84
+ }
85
+ // If the token already contains an auth scheme (e.g. "Bearer ...", "Basic ..."),
86
+ // return it as-is.
87
+ if (/^[A-Z]+ /i.test(token)) {
88
+ return token;
89
+ }
90
+ return `Bearer ${token}`;
91
+ }
92
+ //# sourceMappingURL=getAuthHeadersFromConfig.js.map
@@ -0,0 +1,2 @@
1
+ import type { URL } from 'node:url';
2
+ export declare function removePort(urlObj: URL): string;
@@ -0,0 +1,7 @@
1
+ export function removePort(urlObj) {
2
+ if (urlObj.port === '')
3
+ return urlObj.href;
4
+ urlObj.port = '';
5
+ return urlObj.toString();
6
+ }
7
+ //# sourceMappingURL=removePort.js.map
package/lib/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import type { RegistryConfig } from '@pnpm/types';
2
+ import { type AuthHeaders, type AuthHeadersByScope, getAuthHeadersByScope, getAuthHeadersFromCreds } from './getAuthHeadersFromConfig.js';
3
+ export { type AuthHeaders, type AuthHeadersByScope, getAuthHeadersByScope, getAuthHeadersFromCreds };
4
+ interface GetAuthHeaderOptions {
5
+ pkgName?: string;
6
+ }
7
+ export declare function createGetAuthHeaderByURI(configByUri: Record<string, RegistryConfig>): (uri: string, opts?: GetAuthHeaderOptions) => string | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pnpm/network.auth-header",
3
- "version": "1101.1.5",
3
+ "version": "1101.1.6",
4
4
  "description": "Gets the authorization header for the given URI",
5
5
  "keywords": [
6
6
  "pnpm",
@@ -29,12 +29,12 @@
29
29
  ],
30
30
  "dependencies": {
31
31
  "@pnpm/config.nerf-dart": "^2.0.1",
32
- "@pnpm/error": "1100.0.1",
33
- "@pnpm/types": "1101.5.0"
32
+ "@pnpm/error": "1100.1.0",
33
+ "@pnpm/types": "1101.6.0"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@jest/globals": "30.4.1",
37
- "@pnpm/network.auth-header": "1101.1.5",
37
+ "@pnpm/network.auth-header": "1101.1.6",
38
38
  "safe-buffer": "5.2.1"
39
39
  },
40
40
  "engines": {