@nx/js 24.1.0-beta.0 → 24.1.0-beta.1
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/src/executors/verdaccio/placeholder-auth-token.d.ts +8 -0
- package/dist/src/executors/verdaccio/placeholder-auth-token.js +36 -0
- package/dist/src/executors/verdaccio/verdaccio.impl.js +49 -15
- package/dist/src/generators/setup-verdaccio/files/config.yml +8 -0
- package/dist/src/generators/typescript-sync/typescript-sync.js +5 -7
- package/dist/src/migrations/update-24-1-0/add-verdaccio-jwt-security.d.ts +10 -0
- package/dist/src/migrations/update-24-1-0/add-verdaccio-jwt-security.js +145 -0
- package/dist/src/migrations/update-24-1-0/add-verdaccio-jwt-security.md +49 -0
- package/dist/src/plugins/jest/start-local-registry.d.ts +4 -4
- package/dist/src/plugins/jest/start-local-registry.js +46 -32
- package/migrations.json +6 -0
- package/package.json +12 -12
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const PLACEHOLDER_AUTH_TOKEN = "secretVerdaccioToken";
|
|
2
|
+
/**
|
|
3
|
+
* Verdaccio 7+ answers 401 to a token it cannot decode, unless its config
|
|
4
|
+
* enables JWT tokens, which treat such a token as anonymous. Resolves `false`
|
|
5
|
+
* when the registry cannot be reached, so callers keep the token.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isPlaceholderAuthTokenRejected(registryUrl: string): Promise<boolean>;
|
|
8
|
+
export declare function warnPlaceholderAuthTokenRejected(registryUrl: string): void;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PLACEHOLDER_AUTH_TOKEN = void 0;
|
|
4
|
+
exports.isPlaceholderAuthTokenRejected = isPlaceholderAuthTokenRejected;
|
|
5
|
+
exports.warnPlaceholderAuthTokenRejected = warnPlaceholderAuthTokenRejected;
|
|
6
|
+
const devkit_1 = require("@nx/devkit");
|
|
7
|
+
// npm and bun refuse to publish to a registry without a token for it
|
|
8
|
+
exports.PLACEHOLDER_AUTH_TOKEN = 'secretVerdaccioToken';
|
|
9
|
+
/**
|
|
10
|
+
* Verdaccio 7+ answers 401 to a token it cannot decode, unless its config
|
|
11
|
+
* enables JWT tokens, which treat such a token as anonymous. Resolves `false`
|
|
12
|
+
* when the registry cannot be reached, so callers keep the token.
|
|
13
|
+
*/
|
|
14
|
+
async function isPlaceholderAuthTokenRejected(registryUrl) {
|
|
15
|
+
try {
|
|
16
|
+
const response = await fetch(new URL('-/ping', registryUrl), {
|
|
17
|
+
headers: { authorization: `Bearer ${exports.PLACEHOLDER_AUTH_TOKEN}` },
|
|
18
|
+
signal: AbortSignal.timeout(5000),
|
|
19
|
+
});
|
|
20
|
+
return response.status === 401;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function warnPlaceholderAuthTokenRejected(registryUrl) {
|
|
27
|
+
devkit_1.logger.warn(`Verdaccio at ${registryUrl} rejects the placeholder npm auth token. Without a token, npm and bun cannot publish to the registry.
|
|
28
|
+
If the registry requires a login, publish with a token from \`npm login\`.
|
|
29
|
+
Otherwise, make Verdaccio accept the placeholder by adding the following to its config file. Existing tokens from \`npm login\` then stop working until users log in again.
|
|
30
|
+
|
|
31
|
+
security:
|
|
32
|
+
api:
|
|
33
|
+
jwt:
|
|
34
|
+
sign:
|
|
35
|
+
expiresIn: 7d`);
|
|
36
|
+
}
|
|
@@ -9,6 +9,7 @@ const detect_port_1 = tslib_1.__importDefault(require("detect-port"));
|
|
|
9
9
|
const node_fs_1 = require("node:fs");
|
|
10
10
|
const path_1 = require("path");
|
|
11
11
|
const semver_1 = require("semver");
|
|
12
|
+
const placeholder_auth_token_1 = require("./placeholder-auth-token");
|
|
12
13
|
let childProcess;
|
|
13
14
|
let env = {
|
|
14
15
|
SKIP_YARN_COREPACK_CHECK: 'true',
|
|
@@ -38,7 +39,8 @@ async function verdaccioExecutor(options, context) {
|
|
|
38
39
|
devkit_1.logger.info(`Port ${options.port} was occupied. Using port ${port}.`);
|
|
39
40
|
options.port = port;
|
|
40
41
|
}
|
|
41
|
-
const
|
|
42
|
+
const npm = options.location === 'none' ? undefined : setupNpm(options);
|
|
43
|
+
const cleanupFunctions = npm ? [npm.cleanup, setupYarn(options)] : [];
|
|
42
44
|
const processExitListener = (signal) => {
|
|
43
45
|
for (const fn of cleanupFunctions) {
|
|
44
46
|
fn();
|
|
@@ -52,7 +54,7 @@ async function verdaccioExecutor(options, context) {
|
|
|
52
54
|
process.on('SIGINT', processExitListener);
|
|
53
55
|
process.on('SIGHUP', processExitListener);
|
|
54
56
|
try {
|
|
55
|
-
await startVerdaccio(options, context.root);
|
|
57
|
+
await startVerdaccio(options, context.root, npm?.authTokenSet ?? false);
|
|
56
58
|
}
|
|
57
59
|
catch (e) {
|
|
58
60
|
devkit_1.logger.error('Failed to start verdaccio: ' + e?.toString());
|
|
@@ -76,7 +78,7 @@ function getVerdaccioBinPath() {
|
|
|
76
78
|
/**
|
|
77
79
|
* Fork the verdaccio process: https://verdaccio.org/docs/verdaccio-programmatically/#using-fork-from-child_process-module
|
|
78
80
|
*/
|
|
79
|
-
function startVerdaccio(options, workspaceRoot) {
|
|
81
|
+
function startVerdaccio(options, workspaceRoot, npmAuthTokenSet) {
|
|
80
82
|
return new Promise((resolve, reject) => {
|
|
81
83
|
childProcess = (0, child_process_1.fork)(getVerdaccioBinPath(), createVerdaccioOptions(options, workspaceRoot), {
|
|
82
84
|
env: {
|
|
@@ -91,12 +93,25 @@ function startVerdaccio(options, workspaceRoot) {
|
|
|
91
93
|
childProcess.on('error', (err) => {
|
|
92
94
|
reject(err);
|
|
93
95
|
});
|
|
94
|
-
childProcess.on('message', (message) => {
|
|
95
|
-
if (message?.verdaccio_started
|
|
96
|
-
|
|
96
|
+
childProcess.on('message', async (message) => {
|
|
97
|
+
if (message?.verdaccio_started !== true) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const address = getVerdaccioAddress(options);
|
|
101
|
+
const url = address ? getVerdaccioListeningUrl(address) : undefined;
|
|
102
|
+
// Dependent tasks start on readiness, so drop a rejected token first
|
|
103
|
+
if (npmAuthTokenSet && url) {
|
|
104
|
+
await removeRejectedNpmAuthToken(options, url);
|
|
105
|
+
}
|
|
106
|
+
if (address) {
|
|
107
|
+
// startLocalRegistry waits for an address on stdout, and Verdaccio 7
|
|
108
|
+
// logs its own at info level, which the generated config hides
|
|
109
|
+
devkit_1.logger.info(`Verdaccio is listening on ${address}`);
|
|
110
|
+
}
|
|
111
|
+
if (process.env.NX_READY_SIGNAL_SOCKET) {
|
|
97
112
|
// Nx versions without signal readiness do not export this API.
|
|
98
113
|
const { emitReadySignal } = require('@nx/devkit');
|
|
99
|
-
emitReadySignal(
|
|
114
|
+
emitReadySignal(url);
|
|
100
115
|
}
|
|
101
116
|
});
|
|
102
117
|
childProcess.on('disconnect', (err) => {
|
|
@@ -114,7 +129,9 @@ function startVerdaccio(options, workspaceRoot) {
|
|
|
114
129
|
});
|
|
115
130
|
});
|
|
116
131
|
}
|
|
117
|
-
|
|
132
|
+
// Keeps wildcard hosts like Verdaccio's own address line, because
|
|
133
|
+
// startLocalRegistry matches the host its caller passes
|
|
134
|
+
function getVerdaccioAddress(options) {
|
|
118
135
|
const listenAddress = options.listenAddress
|
|
119
136
|
? `${options.listenAddress}:${options.port}`
|
|
120
137
|
: String(options.port);
|
|
@@ -123,9 +140,10 @@ function getVerdaccioListeningUrl(options) {
|
|
|
123
140
|
if (!address)
|
|
124
141
|
return undefined;
|
|
125
142
|
const endpoint = `${address[1] ?? 'http'}://${address[2] || 'localhost'}:${address[3]}/`;
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
143
|
+
return URL.canParse(endpoint) ? new URL(endpoint).href : undefined;
|
|
144
|
+
}
|
|
145
|
+
function getVerdaccioListeningUrl(address) {
|
|
146
|
+
const url = new URL(address);
|
|
129
147
|
if (url.hostname === '0.0.0.0')
|
|
130
148
|
url.hostname = '127.0.0.1';
|
|
131
149
|
if (url.hostname === '[::]')
|
|
@@ -148,12 +166,27 @@ function createVerdaccioOptions(options, workspaceRoot) {
|
|
|
148
166
|
}
|
|
149
167
|
return verdaccioArgs;
|
|
150
168
|
}
|
|
169
|
+
function getNpmAuthTokenKey(options) {
|
|
170
|
+
return `//${options.listenAddress}:${options.port}/:_authToken`;
|
|
171
|
+
}
|
|
172
|
+
async function removeRejectedNpmAuthToken(options, registryUrl) {
|
|
173
|
+
if (!(await (0, placeholder_auth_token_1.isPlaceholderAuthTokenRejected)(registryUrl))) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
(0, child_process_1.execSync)(`npm config delete ${getNpmAuthTokenKey(options)} --location ${options.location}`, { env, windowsHide: true });
|
|
178
|
+
}
|
|
179
|
+
catch (e) {
|
|
180
|
+
devkit_1.logger.warn(`Failed to remove the npm auth token: ${e.message}`);
|
|
181
|
+
}
|
|
182
|
+
(0, placeholder_auth_token_1.warnPlaceholderAuthTokenRejected)(registryUrl);
|
|
183
|
+
}
|
|
151
184
|
function setupNpm(options) {
|
|
152
185
|
try {
|
|
153
186
|
(0, child_process_1.execSync)('npm --version', { env, windowsHide: true });
|
|
154
187
|
}
|
|
155
188
|
catch (e) {
|
|
156
|
-
return () => { };
|
|
189
|
+
return { cleanup: () => { }, authTokenSet: false };
|
|
157
190
|
}
|
|
158
191
|
let npmRegistryPaths = [];
|
|
159
192
|
const scopes = ['', ...(options.scopes || [])];
|
|
@@ -167,14 +200,14 @@ function setupNpm(options) {
|
|
|
167
200
|
?.replace('\u001b[2K\u001b[1G', '') // strip out ansi codes
|
|
168
201
|
);
|
|
169
202
|
(0, child_process_1.execSync)(`npm config set ${registryName} http://${options.listenAddress}:${options.port}/ --location ${options.location}`, { env, windowsHide: true });
|
|
170
|
-
(0, child_process_1.execSync)(`npm config set
|
|
203
|
+
(0, child_process_1.execSync)(`npm config set ${getNpmAuthTokenKey(options)}="${placeholder_auth_token_1.PLACEHOLDER_AUTH_TOKEN}" --location ${options.location}`, { env, windowsHide: true });
|
|
171
204
|
devkit_1.logger.info(`Set npm ${registryName} to http://${options.listenAddress}:${options.port}/`);
|
|
172
205
|
}
|
|
173
206
|
catch (e) {
|
|
174
207
|
throw new Error(`Failed to set npm ${registryName} to http://${options.listenAddress}:${options.port}/: ${e.message}`);
|
|
175
208
|
}
|
|
176
209
|
});
|
|
177
|
-
|
|
210
|
+
const cleanup = () => {
|
|
178
211
|
try {
|
|
179
212
|
const currentNpmRegistryPath = (0, child_process_1.execSync)(`npm config get registry --location ${options.location}`, { env, windowsHide: true })
|
|
180
213
|
?.toString()
|
|
@@ -195,12 +228,13 @@ function setupNpm(options) {
|
|
|
195
228
|
devkit_1.logger.info('Cleared custom npm registry');
|
|
196
229
|
}
|
|
197
230
|
});
|
|
198
|
-
(0, child_process_1.execSync)(`npm config delete
|
|
231
|
+
(0, child_process_1.execSync)(`npm config delete ${getNpmAuthTokenKey(options)} --location ${options.location}`, { env, windowsHide: true });
|
|
199
232
|
}
|
|
200
233
|
catch (e) {
|
|
201
234
|
throw new Error(`Failed to reset npm registry: ${e.message}`);
|
|
202
235
|
}
|
|
203
236
|
};
|
|
237
|
+
return { cleanup, authTokenSet: true };
|
|
204
238
|
}
|
|
205
239
|
catch (e) {
|
|
206
240
|
throw new Error(`Failed to set npm registry to http://${options.listenAddress}:${options.port}/: ${e.message}`);
|
|
@@ -26,3 +26,11 @@ log:
|
|
|
26
26
|
|
|
27
27
|
publish:
|
|
28
28
|
allow_offline: true # set offline to true to allow publish offline
|
|
29
|
+
|
|
30
|
+
# jwt tokens make verdaccio 7+ treat the placeholder npm auth token set by nx
|
|
31
|
+
# as anonymous instead of rejecting it
|
|
32
|
+
security:
|
|
33
|
+
api:
|
|
34
|
+
jwt:
|
|
35
|
+
sign:
|
|
36
|
+
expiresIn: 7d
|
|
@@ -25,6 +25,7 @@ async function syncGenerator(tree) {
|
|
|
25
25
|
composite: new Map(),
|
|
26
26
|
content: new Map(),
|
|
27
27
|
exists: new Map(),
|
|
28
|
+
extendedConfigs: new Map(),
|
|
28
29
|
};
|
|
29
30
|
// Root tsconfig containing project references for the whole workspace
|
|
30
31
|
const rootTsconfigPath = 'tsconfig.json';
|
|
@@ -148,7 +149,7 @@ async function syncGenerator(tree) {
|
|
|
148
149
|
// duplicate the project references in the tsconfig.json file
|
|
149
150
|
let keepReferencesInTsconfigJson = true;
|
|
150
151
|
if (foundRuntimeTsConfig) {
|
|
151
|
-
const sourceProjectTsconfig = parseTsconfig(sourceProjectTsconfigPath, tsSysFromTree);
|
|
152
|
+
const sourceProjectTsconfig = parseTsconfig(sourceProjectTsconfigPath, tsSysFromTree, tsconfigInfoCaches);
|
|
152
153
|
keepReferencesInTsconfigJson = sourceProjectTsconfig.fileNames.length > 0;
|
|
153
154
|
}
|
|
154
155
|
updateTsConfigReferences(tree, tsSysFromTree, tsconfigInfoCaches, sourceProjectTsconfigPath, keepReferencesInTsconfigJson ? dependencies : [], sourceProjectNode.data.root, projectRoots, changedFiles);
|
|
@@ -185,8 +186,8 @@ function readRawTsconfigContents(tree, tsconfigInfoCaches, tsconfigPath) {
|
|
|
185
186
|
}
|
|
186
187
|
return tsconfigInfoCaches.content.get(tsconfigPath);
|
|
187
188
|
}
|
|
188
|
-
function parseTsconfig(tsconfigPath, sys) {
|
|
189
|
-
return ts.parseJsonConfigFileContent(ts.readConfigFile(tsconfigPath, sys.readFile).config, sys, (0, posix_1.dirname)(tsconfigPath));
|
|
189
|
+
function parseTsconfig(tsconfigPath, sys, tsconfigInfoCaches) {
|
|
190
|
+
return ts.parseJsonConfigFileContent(ts.readConfigFile(tsconfigPath, sys.readFile).config, sys, (0, posix_1.dirname)(tsconfigPath), undefined, undefined, undefined, undefined, tsconfigInfoCaches.extendedConfigs);
|
|
190
191
|
}
|
|
191
192
|
/**
|
|
192
193
|
* Within the context of a sync generator, performance is a key concern,
|
|
@@ -412,10 +413,7 @@ function patchTsconfigJsonReferences(tree, tsconfigInfoCaches, tsconfigPath, upd
|
|
|
412
413
|
}
|
|
413
414
|
function hasCompositeEnabled(tsSysFromTree, tsconfigInfoCaches, tsconfigPath) {
|
|
414
415
|
if (!tsconfigInfoCaches.composite.has(tsconfigPath)) {
|
|
415
|
-
const parsed = parseTsconfig(tsconfigPath, {
|
|
416
|
-
...tsSysFromTree,
|
|
417
|
-
readDirectory: () => [],
|
|
418
|
-
});
|
|
416
|
+
const parsed = parseTsconfig(tsconfigPath, { ...tsSysFromTree, readDirectory: () => [] }, tsconfigInfoCaches);
|
|
419
417
|
tsconfigInfoCaches.composite.set(tsconfigPath, parsed.options.composite === true);
|
|
420
418
|
}
|
|
421
419
|
return tsconfigInfoCaches.composite.get(tsconfigPath);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Tree } from '@nx/devkit';
|
|
2
|
+
export default function addVerdaccioJwtSecurity(tree: Tree): Promise<{
|
|
3
|
+
nextSteps: string[];
|
|
4
|
+
agentContext: string[];
|
|
5
|
+
skipAgentic?: undefined;
|
|
6
|
+
} | {
|
|
7
|
+
nextSteps?: undefined;
|
|
8
|
+
agentContext?: undefined;
|
|
9
|
+
skipAgentic: boolean;
|
|
10
|
+
}>;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = addVerdaccioJwtSecurity;
|
|
4
|
+
const devkit_1 = require("@nx/devkit");
|
|
5
|
+
const internal_1 = require("@nx/devkit/internal");
|
|
6
|
+
const js_yaml_1 = require("@zkochan/js-yaml");
|
|
7
|
+
const target_defaults_matching_1 = require("../../utils/target-defaults-matching");
|
|
8
|
+
const VERDACCIO_EXECUTOR = '@nx/js:verdaccio';
|
|
9
|
+
// The groups Verdaccio gives a user that is not logged in
|
|
10
|
+
const ANONYMOUS_GROUPS = new Set(['$all', '$anonymous', '@all', '@anonymous']);
|
|
11
|
+
const JWT_SECURITY_BLOCK = `# jwt tokens make verdaccio 7+ treat the placeholder npm auth token set by nx
|
|
12
|
+
# as anonymous instead of rejecting it
|
|
13
|
+
security:
|
|
14
|
+
api:
|
|
15
|
+
jwt:
|
|
16
|
+
sign:
|
|
17
|
+
expiresIn: 7d
|
|
18
|
+
`;
|
|
19
|
+
const MANUAL_STEP = 'To let Verdaccio 7+ accept the placeholder npm auth token that Nx sets, set `security.api.jwt.sign.expiresIn` (for example to `7d`) in it.';
|
|
20
|
+
async function addVerdaccioJwtSecurity(tree) {
|
|
21
|
+
const nextSteps = [];
|
|
22
|
+
for (const path of findVerdaccioConfigPaths(tree)) {
|
|
23
|
+
if (!tree.exists(path))
|
|
24
|
+
continue;
|
|
25
|
+
const content = tree.read(path, 'utf-8');
|
|
26
|
+
const config = parseYamlMap(content);
|
|
27
|
+
if (!config) {
|
|
28
|
+
nextSteps.push(`Could not parse ${path}. ${MANUAL_STEP}`);
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (acceptsPlaceholderToken(config))
|
|
32
|
+
continue;
|
|
33
|
+
if (reliesOnLoggedInUsers(config)) {
|
|
34
|
+
nextSteps.push(`${path} relies on logged-in users, so it was not changed. On Verdaccio 7+, npm and bun need a token from \`npm login\` to publish to it. Setting \`security.api.jwt\` would let them publish packages open to anonymous users with the placeholder npm auth token that Nx sets. It would also make existing tokens from \`npm login\` stop working until users log in again.`);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if ('security' in config) {
|
|
38
|
+
nextSteps.push(`${path} already has a \`security\` section, so it was not changed. ${MANUAL_STEP}`);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const updated = `${content}${content.endsWith('\n') ? '' : '\n'}\n${JWT_SECURITY_BLOCK}`;
|
|
42
|
+
// Appending breaks configs written as a single flow mapping
|
|
43
|
+
if (!parseYamlMap(updated)?.security?.api?.jwt?.sign) {
|
|
44
|
+
nextSteps.push(`Could not update ${path}. ${MANUAL_STEP}`);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
tree.write(path, updated);
|
|
48
|
+
}
|
|
49
|
+
return nextSteps.length
|
|
50
|
+
? { nextSteps, agentContext: nextSteps }
|
|
51
|
+
: { skipAgentic: true };
|
|
52
|
+
}
|
|
53
|
+
function findVerdaccioConfigPaths(tree) {
|
|
54
|
+
const targetDefaults = (0, devkit_1.readNxJson)(tree)?.targetDefaults;
|
|
55
|
+
const paths = new Set();
|
|
56
|
+
for (const [projectName, project] of (0, devkit_1.getProjects)(tree)) {
|
|
57
|
+
const projectNode = {
|
|
58
|
+
name: projectName,
|
|
59
|
+
type: project.projectType === 'application' ? 'app' : 'lib',
|
|
60
|
+
data: project,
|
|
61
|
+
};
|
|
62
|
+
for (const [targetName, projectTarget] of Object.entries(project.targets ?? {})) {
|
|
63
|
+
try {
|
|
64
|
+
const target = resolveTarget({ targetName, projectName, projectNode, target: projectTarget }, targetDefaults);
|
|
65
|
+
if (target.executor !== VERDACCIO_EXECUTOR)
|
|
66
|
+
continue;
|
|
67
|
+
for (const options of [
|
|
68
|
+
target.options,
|
|
69
|
+
...Object.values(target.configurations ?? {}),
|
|
70
|
+
]) {
|
|
71
|
+
if (typeof options?.config !== 'string')
|
|
72
|
+
continue;
|
|
73
|
+
const { config } = (0, internal_1.resolveNxTokensInOptions)({ config: options.config }, project, `${projectName}:${targetName}`);
|
|
74
|
+
paths.add((0, devkit_1.joinPathFragments)(config));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
// Project graph creation fails on targets that cannot be resolved, so no task reads them
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return paths;
|
|
83
|
+
}
|
|
84
|
+
// Like project graph creation: `command` means nx:run-commands, defaults entries incompatible
|
|
85
|
+
// with the target are dropped, and the rest merge in order, so an incompatible one replaces earlier ones
|
|
86
|
+
function resolveTarget(ref, targetDefaults) {
|
|
87
|
+
const root = ref.projectNode.data.root;
|
|
88
|
+
const target = (0, internal_1.resolveCommandSyntacticSugar)(ref.target, root);
|
|
89
|
+
const matchedRef = {
|
|
90
|
+
...ref,
|
|
91
|
+
target,
|
|
92
|
+
matcherExecutor: target.executor,
|
|
93
|
+
};
|
|
94
|
+
const key = targetDefaults
|
|
95
|
+
? (0, target_defaults_matching_1.selectDefaultsKey)(matchedRef, targetDefaults, target.executor)
|
|
96
|
+
: null;
|
|
97
|
+
let defaults;
|
|
98
|
+
for (const entry of (0, target_defaults_matching_1.compatibleDefaultsEntries)(matchedRef, key, targetDefaults)) {
|
|
99
|
+
// The reader merges a key's entries without resolving `command`, so read one at a time
|
|
100
|
+
const matched = (0, internal_1.readTargetDefaultsForTarget)(ref.targetName, { [key]: [entry] }, target.executor, { projectName: ref.projectName, projectNode: ref.projectNode });
|
|
101
|
+
if (matched) {
|
|
102
|
+
defaults = (0, internal_1.mergeTargetConfigurations)((0, internal_1.resolveCommandSyntacticSugar)(matched, root), defaults);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return defaults ? (0, internal_1.mergeTargetConfigurations)(target, defaults) : target;
|
|
106
|
+
}
|
|
107
|
+
// Verdaccio 7+ rejects undecodable tokens only while `security.api.legacy` is on and `jwt` is unset
|
|
108
|
+
function acceptsPlaceholderToken(config) {
|
|
109
|
+
return (config.security?.api?.jwt != null || config.security?.api?.legacy === false);
|
|
110
|
+
}
|
|
111
|
+
// jwt tokens make existing login tokens anonymous, which can lose access under these configs
|
|
112
|
+
function reliesOnLoggedInUsers(config) {
|
|
113
|
+
if (config.publish?.check_owners === true)
|
|
114
|
+
return true;
|
|
115
|
+
// Auth plugins other than htpasswd can grant access or parse tokens their own way
|
|
116
|
+
if (config.auth &&
|
|
117
|
+
typeof config.auth === 'object' &&
|
|
118
|
+
Object.keys(config.auth).some((plugin) => plugin !== 'htpasswd')) {
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
return Object.values(config.packages ?? {}).some((rule) => rule &&
|
|
122
|
+
typeof rule === 'object' &&
|
|
123
|
+
['access', 'publish', 'unpublish', 'stage'].some((action) => {
|
|
124
|
+
// An empty list denies everyone, and empty `unpublish` or `stage` falls back to `publish`
|
|
125
|
+
const groups = normalizeGroups(rule[action]);
|
|
126
|
+
return (groups.length > 0 &&
|
|
127
|
+
!groups.some((group) => ANONYMOUS_GROUPS.has(group)));
|
|
128
|
+
}));
|
|
129
|
+
}
|
|
130
|
+
function normalizeGroups(groups) {
|
|
131
|
+
if (typeof groups === 'string')
|
|
132
|
+
return groups.split(/\s+/).filter(Boolean);
|
|
133
|
+
return Array.isArray(groups) ? groups.map(String) : [];
|
|
134
|
+
}
|
|
135
|
+
function parseYamlMap(content) {
|
|
136
|
+
try {
|
|
137
|
+
const value = (0, js_yaml_1.load)(content);
|
|
138
|
+
return value && typeof value === 'object' && !Array.isArray(value)
|
|
139
|
+
? value
|
|
140
|
+
: undefined;
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
#### Enable JWT API tokens in Verdaccio configs
|
|
2
|
+
|
|
3
|
+
The `@nx/js:verdaccio` executor and `startLocalRegistry` set a placeholder npm auth token for the local registry, because npm and bun refuse to publish to a registry without one. Verdaccio 7 rejects a token it cannot decode with a 401 error, which breaks `npm publish` and `npm view` against the local registry. With JWT API tokens enabled, Verdaccio 6 and 7 treat the placeholder token as anonymous instead.
|
|
4
|
+
|
|
5
|
+
The migration adds a `security.api.jwt` section to each Verdaccio config file that an `@nx/js:verdaccio` target points at through its `config` option, including target configurations and `targetDefaults` in `nx.json`. Config files that already set `security.api.jwt`, or set `security.api.legacy` to `false`, already accept the placeholder token and stay unchanged.
|
|
6
|
+
|
|
7
|
+
JWT API tokens turn existing tokens from `npm login` into anonymous ones. The migration therefore lists, and leaves unchanged, config files that rely on logged-in users:
|
|
8
|
+
|
|
9
|
+
- a package rule grants `access`, `publish`, `unpublish` or `stage` only to logged-in users
|
|
10
|
+
- `publish.check_owners` is enabled
|
|
11
|
+
- an auth plugin other than `htpasswd` is configured
|
|
12
|
+
|
|
13
|
+
It also lists config files that already have a `security` section, and files it cannot parse or update.
|
|
14
|
+
|
|
15
|
+
In the config files it changes, users who logged in before the migration act as anonymous users until they log in again. Installs and publishes keep working, but `npm whoami` fails on Verdaccio 7. New tokens from `npm login` expire after 7 days.
|
|
16
|
+
|
|
17
|
+
#### Sample code changes
|
|
18
|
+
|
|
19
|
+
##### Before
|
|
20
|
+
|
|
21
|
+
```yaml title=".verdaccio/config.yml"
|
|
22
|
+
storage: ../tmp/local-registry/storage
|
|
23
|
+
|
|
24
|
+
packages:
|
|
25
|
+
'**':
|
|
26
|
+
access: $all
|
|
27
|
+
publish: $all
|
|
28
|
+
unpublish: $all
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
##### After
|
|
32
|
+
|
|
33
|
+
```yaml title=".verdaccio/config.yml"
|
|
34
|
+
storage: ../tmp/local-registry/storage
|
|
35
|
+
|
|
36
|
+
packages:
|
|
37
|
+
'**':
|
|
38
|
+
access: $all
|
|
39
|
+
publish: $all
|
|
40
|
+
unpublish: $all
|
|
41
|
+
|
|
42
|
+
# jwt tokens make verdaccio 7+ treat the placeholder npm auth token set by nx
|
|
43
|
+
# as anonymous instead of rejecting it
|
|
44
|
+
security:
|
|
45
|
+
api:
|
|
46
|
+
jwt:
|
|
47
|
+
sign:
|
|
48
|
+
expiresIn: 7d
|
|
49
|
+
```
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* This function is used to start a local registry for testing purposes.
|
|
3
3
|
* @param localRegistryTarget the target to run to start the local registry e.g. workspace:local-registry
|
|
4
|
-
* @param storage the storage location for the local registry
|
|
5
|
-
* @param verbose whether to
|
|
6
|
-
* @param clearStorage whether to clear the
|
|
7
|
-
* @param listenAddress the address
|
|
4
|
+
* @param storage the storage location for the local registry, passed to the target as `--storage`
|
|
5
|
+
* @param verbose whether to print the target's stdout until the registry address appears (stderr is always printed)
|
|
6
|
+
* @param clearStorage whether to ask the target to clear the registry storage before starting, passed to the target as `--clear` (default `true`)
|
|
7
|
+
* @param listenAddress the address the target listens on (default `localhost`). It is not passed to the target; the helper waits for `http://<listenAddress>:<port>` in the target's stdout and uses it as the registry URL.
|
|
8
8
|
*/
|
|
9
9
|
export declare function startLocalRegistry({ localRegistryTarget, storage, verbose, clearStorage, listenAddress, }: {
|
|
10
10
|
localRegistryTarget: string;
|
|
@@ -3,13 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.startLocalRegistry = startLocalRegistry;
|
|
4
4
|
const internal_1 = require("@nx/devkit/internal");
|
|
5
5
|
const child_process_1 = require("child_process");
|
|
6
|
+
const placeholder_auth_token_1 = require("../../executors/verdaccio/placeholder-auth-token");
|
|
6
7
|
/**
|
|
7
8
|
* This function is used to start a local registry for testing purposes.
|
|
8
9
|
* @param localRegistryTarget the target to run to start the local registry e.g. workspace:local-registry
|
|
9
|
-
* @param storage the storage location for the local registry
|
|
10
|
-
* @param verbose whether to
|
|
11
|
-
* @param clearStorage whether to clear the
|
|
12
|
-
* @param listenAddress the address
|
|
10
|
+
* @param storage the storage location for the local registry, passed to the target as `--storage`
|
|
11
|
+
* @param verbose whether to print the target's stdout until the registry address appears (stderr is always printed)
|
|
12
|
+
* @param clearStorage whether to ask the target to clear the registry storage before starting, passed to the target as `--clear` (default `true`)
|
|
13
|
+
* @param listenAddress the address the target listens on (default `localhost`). It is not passed to the target; the helper waits for `http://<listenAddress>:<port>` in the target's stdout and uses it as the registry URL.
|
|
13
14
|
*/
|
|
14
15
|
function startLocalRegistry({ localRegistryTarget, storage, verbose, clearStorage, listenAddress, }) {
|
|
15
16
|
listenAddress ??= 'localhost';
|
|
@@ -26,37 +27,50 @@ function startLocalRegistry({ localRegistryTarget, storage, verbose, clearStorag
|
|
|
26
27
|
process.stdout.write(data);
|
|
27
28
|
console.log('Waiting for local registry to start...');
|
|
28
29
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
const output = data.toString();
|
|
31
|
+
const addressPrefix = `http://${listenAddress}:`;
|
|
32
|
+
const addressIndex = output.indexOf(addressPrefix);
|
|
33
|
+
if (addressIndex !== -1) {
|
|
34
|
+
childProcess?.stdout?.off('data', listener);
|
|
35
|
+
const port = parseInt(output.slice(addressIndex + addressPrefix.length));
|
|
32
36
|
const registry = `http://${listenAddress}:${port}`;
|
|
33
|
-
const
|
|
37
|
+
const authTokenKey = `//${listenAddress}:${port}/:_authToken`;
|
|
34
38
|
console.log(`Local registry started on ${registry}`);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
39
|
+
(0, placeholder_auth_token_1.isPlaceholderAuthTokenRejected)(registry)
|
|
40
|
+
.then((authTokenRejected) => {
|
|
41
|
+
if (authTokenRejected) {
|
|
42
|
+
(0, placeholder_auth_token_1.warnPlaceholderAuthTokenRejected)(registry);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
(0, child_process_1.execSync)(`npm config set ${authTokenKey} "${placeholder_auth_token_1.PLACEHOLDER_AUTH_TOKEN}" --ws=false`, {
|
|
46
|
+
windowsHide: true,
|
|
47
|
+
});
|
|
48
|
+
process.env[`pnpm_config_${authTokenKey}`] =
|
|
49
|
+
placeholder_auth_token_1.PLACEHOLDER_AUTH_TOKEN;
|
|
50
|
+
process.env.BUN_CONFIG_TOKEN = placeholder_auth_token_1.PLACEHOLDER_AUTH_TOKEN;
|
|
51
|
+
}
|
|
52
|
+
process.env.npm_config_registry = registry;
|
|
53
|
+
// pnpm 11 reads pnpm_config_* env vars instead of npm_config_*, and
|
|
54
|
+
// they take precedence over any registry configured in ~/.npmrc
|
|
55
|
+
process.env.pnpm_config_registry = registry;
|
|
56
|
+
// bun
|
|
57
|
+
process.env.BUN_CONFIG_REGISTRY = registry;
|
|
58
|
+
// yarnv1
|
|
59
|
+
process.env.YARN_REGISTRY = registry;
|
|
60
|
+
// yarnv2
|
|
61
|
+
process.env.YARN_NPM_REGISTRY_SERVER = registry;
|
|
62
|
+
process.env.YARN_UNSAFE_HTTP_WHITELIST = listenAddress;
|
|
63
|
+
console.log('Set npm, pnpm, bun, and yarn config registry to ' + registry);
|
|
64
|
+
resolve(() => {
|
|
65
|
+
childProcess.kill();
|
|
66
|
+
if (!authTokenRejected) {
|
|
67
|
+
(0, child_process_1.execSync)(`npm config delete ${authTokenKey} --ws=false`, {
|
|
68
|
+
windowsHide: true,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
57
71
|
});
|
|
58
|
-
})
|
|
59
|
-
|
|
72
|
+
})
|
|
73
|
+
.catch(reject);
|
|
60
74
|
}
|
|
61
75
|
};
|
|
62
76
|
childProcess?.stdout?.on('data', listener);
|
package/migrations.json
CHANGED
|
@@ -64,6 +64,12 @@
|
|
|
64
64
|
"description": "Replaces the removed `classProperties.loose` option with `loose` for the `@nx/js/babel`, `@nx/react/babel` and `@nx/web/babel` presets in JSON babel config files.",
|
|
65
65
|
"implementation": "./dist/src/migrations/update-24-1-0/replace-babel-class-properties-loose",
|
|
66
66
|
"documentation": "./dist/src/migrations/update-24-1-0/replace-babel-class-properties-loose.md"
|
|
67
|
+
},
|
|
68
|
+
"update-24-1-0-add-verdaccio-jwt-security": {
|
|
69
|
+
"version": "24.1.0-beta.1",
|
|
70
|
+
"description": "Enables JWT API tokens in Verdaccio config files used by `@nx/js:verdaccio` targets, so Verdaccio 7+ accepts the placeholder npm auth token that Nx sets for the local registry.",
|
|
71
|
+
"implementation": "./dist/src/migrations/update-24-1-0/add-verdaccio-jwt-security",
|
|
72
|
+
"documentation": "./dist/src/migrations/update-24-1-0/add-verdaccio-jwt-security.md"
|
|
67
73
|
}
|
|
68
74
|
},
|
|
69
75
|
"packageJsonUpdates": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nx/js",
|
|
3
|
-
"version": "24.1.0-beta.
|
|
3
|
+
"version": "24.1.0-beta.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"files": [
|
|
@@ -162,8 +162,8 @@
|
|
|
162
162
|
"@babel/preset-env": "^7.23.2",
|
|
163
163
|
"@babel/preset-typescript": "^7.22.5",
|
|
164
164
|
"@babel/runtime": "^7.22.6",
|
|
165
|
-
"@nx/devkit": "24.1.0-beta.
|
|
166
|
-
"@nx/workspace": "24.1.0-beta.
|
|
165
|
+
"@nx/devkit": "24.1.0-beta.1",
|
|
166
|
+
"@nx/workspace": "24.1.0-beta.1",
|
|
167
167
|
"@zkochan/js-yaml": "0.0.7",
|
|
168
168
|
"babel-plugin-const-enum": "^1.0.1",
|
|
169
169
|
"babel-plugin-macros": "^3.1.0",
|
|
@@ -183,18 +183,18 @@
|
|
|
183
183
|
"tslib": "^2.3.0"
|
|
184
184
|
},
|
|
185
185
|
"devDependencies": {
|
|
186
|
-
"@nx/esbuild": "24.1.0-beta.
|
|
187
|
-
"@nx/eslint": "24.1.0-beta.
|
|
188
|
-
"@nx/jest": "24.1.0-beta.
|
|
189
|
-
"@nx/oxlint": "24.1.0-beta.
|
|
190
|
-
"@nx/rollup": "24.1.0-beta.
|
|
191
|
-
"@nx/vite": "24.1.0-beta.
|
|
192
|
-
"@nx/vitest": "24.1.0-beta.
|
|
193
|
-
"nx": "24.1.0-beta.
|
|
186
|
+
"@nx/esbuild": "24.1.0-beta.1",
|
|
187
|
+
"@nx/eslint": "24.1.0-beta.1",
|
|
188
|
+
"@nx/jest": "24.1.0-beta.1",
|
|
189
|
+
"@nx/oxlint": "24.1.0-beta.1",
|
|
190
|
+
"@nx/rollup": "24.1.0-beta.1",
|
|
191
|
+
"@nx/vite": "24.1.0-beta.1",
|
|
192
|
+
"@nx/vitest": "24.1.0-beta.1",
|
|
193
|
+
"nx": "24.1.0-beta.1"
|
|
194
194
|
},
|
|
195
195
|
"peerDependencies": {
|
|
196
196
|
"@swc/cli": ">=0.6.0 <0.9.0",
|
|
197
|
-
"verdaccio": "^6.0.5"
|
|
197
|
+
"verdaccio": "^6.0.5 || ^7.0.0"
|
|
198
198
|
},
|
|
199
199
|
"peerDependenciesMeta": {
|
|
200
200
|
"@swc/cli": {
|