@automattic/vip 4.0.10 → 4.0.13

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.
@@ -0,0 +1,216 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ exports.__esModule = true;
5
+ exports.defensiveModeConfigureCommand = defensiveModeConfigureCommand;
6
+ var _chalk = _interopRequireDefault(require("chalk"));
7
+ var _enquirer = require("enquirer");
8
+ var _command = _interopRequireDefault(require("../lib/cli/command"));
9
+ var _format = require("../lib/cli/format");
10
+ var _api = require("../lib/defensive-mode/api");
11
+ var _cliHelpers = require("../lib/defensive-mode/cli-helpers");
12
+ var _input = require("../lib/envvar/input");
13
+ var _tracker = require("../lib/tracker");
14
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
+ const baseUsage = 'vip defensive-mode configure';
16
+ const exampleUsage = 'vip @example-app.production defensive-mode configure';
17
+ const examples = [{
18
+ usage: `${exampleUsage} --enabled=true --challenge-type=1`,
19
+ description: 'Update defensive mode configuration non-interactively (minimal required flags).'
20
+ }, {
21
+ usage: `${exampleUsage} --enabled=true --challenge-type=2 --connection-threshold-absolute=5000 --connection-threshold-percentage=80`,
22
+ description: 'Update with explicit thresholds.'
23
+ }];
24
+ function parseBoolean(raw) {
25
+ if (raw === true || raw === false) {
26
+ return raw;
27
+ }
28
+ if (typeof raw !== 'string') {
29
+ return null;
30
+ }
31
+ const normalized = raw.trim().toLowerCase();
32
+ if (['true', 'yes', '1', 'on', 'enable', 'enabled'].includes(normalized)) {
33
+ return true;
34
+ }
35
+ if (['false', 'no', '0', 'off', 'disable', 'disabled'].includes(normalized)) {
36
+ return false;
37
+ }
38
+ return null;
39
+ }
40
+ function parsePositiveInt(raw) {
41
+ // A bare flag (e.g. `--challenge-type` with no value) arrives as boolean true,
42
+ // which Number() would silently coerce to 1.
43
+ if (raw === undefined || raw === null || typeof raw === 'boolean') {
44
+ return null;
45
+ }
46
+ if (typeof raw === 'string' && raw.trim() === '') {
47
+ return null;
48
+ }
49
+ const num = Number(raw);
50
+ if (!Number.isInteger(num) || num < 0) {
51
+ return null;
52
+ }
53
+ return num;
54
+ }
55
+ function validateFlags(opt) {
56
+ const errors = [];
57
+ const enabled = opt.enabled === undefined ? null : parseBoolean(opt.enabled);
58
+ if (opt.enabled !== undefined && enabled === null) {
59
+ errors.push(`Invalid value for --enabled: ${opt.enabled}. Expected true or false.`);
60
+ }
61
+ const challengeType = opt.challengeType === undefined ? null : parsePositiveInt(opt.challengeType);
62
+ if (opt.challengeType !== undefined && challengeType === null) {
63
+ errors.push(`Invalid value for --challenge-type: ${opt.challengeType}. Expected a non-negative integer.`);
64
+ }
65
+ const absolute = opt.connectionThresholdAbsolute === undefined ? undefined : parsePositiveInt(opt.connectionThresholdAbsolute);
66
+ if (opt.connectionThresholdAbsolute !== undefined && absolute === null) {
67
+ errors.push(`Invalid value for --connection-threshold-absolute: ${opt.connectionThresholdAbsolute}. Expected a non-negative integer.`);
68
+ }
69
+ const percentage = opt.connectionThresholdPercentage === undefined ? undefined : parsePositiveInt(opt.connectionThresholdPercentage);
70
+ if (opt.connectionThresholdPercentage !== undefined && percentage === null) {
71
+ errors.push(`Invalid value for --connection-threshold-percentage: ${opt.connectionThresholdPercentage}. Expected a non-negative integer.`);
72
+ }
73
+ return {
74
+ enabled,
75
+ challengeType,
76
+ absolute,
77
+ percentage,
78
+ errors
79
+ };
80
+ }
81
+ function formatSettingValue(value) {
82
+ return value === undefined || value === null ? '-' : String(value);
83
+ }
84
+ function buildSettingRows(currentConfig, {
85
+ enabled,
86
+ challengeType,
87
+ absolute,
88
+ percentage
89
+ }) {
90
+ return [{
91
+ setting: 'Enabled',
92
+ current: formatSettingValue(currentConfig?.enabled),
93
+ proposed: formatSettingValue(enabled)
94
+ }, {
95
+ setting: 'Challenge type',
96
+ current: formatSettingValue(currentConfig?.challengeType),
97
+ proposed: formatSettingValue(challengeType)
98
+ }, {
99
+ setting: 'Connection threshold (absolute)',
100
+ current: formatSettingValue(currentConfig?.connectionThresholdAbsolute),
101
+ proposed: absolute === undefined ? '(not specified)' : formatSettingValue(absolute)
102
+ }, {
103
+ setting: 'Connection threshold (percentage)',
104
+ current: formatSettingValue(currentConfig?.connectionThresholdPercentage),
105
+ proposed: percentage === undefined ? '(not specified)' : formatSettingValue(percentage)
106
+ }];
107
+ }
108
+ async function resolveRequiredViaPrompt(missing, enabled, challengeType) {
109
+ const answers = await (0, _enquirer.prompt)(missing.map(flag => flag === '--enabled' ? {
110
+ type: 'confirm',
111
+ name: 'enabled',
112
+ message: 'Enable defensive mode?'
113
+ } : {
114
+ type: 'input',
115
+ name: 'challengeType',
116
+ message: 'Challenge type (integer):'
117
+ }));
118
+ let resolvedEnabled = enabled;
119
+ let resolvedChallengeType = challengeType;
120
+ if (resolvedEnabled === null && 'enabled' in answers) {
121
+ resolvedEnabled = Boolean(answers.enabled);
122
+ }
123
+ if (resolvedChallengeType === null && 'challengeType' in answers) {
124
+ resolvedChallengeType = parsePositiveInt(answers.challengeType);
125
+ if (resolvedChallengeType === null) {
126
+ console.error(_chalk.default.red('Challenge type must be a non-negative integer.'));
127
+ process.exit(1);
128
+ }
129
+ }
130
+ return {
131
+ enabled: resolvedEnabled,
132
+ challengeType: resolvedChallengeType
133
+ };
134
+ }
135
+ async function defensiveModeConfigureCommand(_args, opt) {
136
+ const interactive = (0, _cliHelpers.isInteractive)(opt);
137
+ const trackingParams = {
138
+ app_id: opt.app.id,
139
+ command: baseUsage,
140
+ env_id: opt.env.id,
141
+ org_id: opt.app.organization.id,
142
+ org_sfid: opt.app.organization.salesforceId,
143
+ interactive,
144
+ skip_confirm: Boolean(opt.skipConfirmation)
145
+ };
146
+ await (0, _tracker.trackEvent)('defensive_mode_configure_command_execute', trackingParams);
147
+ const {
148
+ enabled: rawEnabled,
149
+ challengeType: rawChallengeType,
150
+ absolute,
151
+ percentage,
152
+ errors
153
+ } = validateFlags(opt);
154
+ if (errors.length > 0) {
155
+ errors.forEach(msg => console.error(_chalk.default.red(msg)));
156
+ process.exit(1);
157
+ }
158
+ const missing = [];
159
+ if (rawEnabled === null) {
160
+ missing.push('--enabled');
161
+ }
162
+ if (rawChallengeType === null) {
163
+ missing.push('--challenge-type');
164
+ }
165
+ let enabled = rawEnabled;
166
+ let challengeType = rawChallengeType;
167
+ if (missing.length > 0) {
168
+ if (!interactive) {
169
+ console.error(_chalk.default.red(`Missing required flags in non-interactive mode: ${missing.join(', ')}`));
170
+ console.error('Re-run with all required flags, or remove --non-interactive and run on a TTY.');
171
+ await (0, _tracker.trackEvent)('defensive_mode_configure_command_error', {
172
+ ...trackingParams,
173
+ error: 'missing-required-flags'
174
+ });
175
+ process.exit(1);
176
+ }
177
+ ({
178
+ enabled,
179
+ challengeType
180
+ } = await resolveRequiredViaPrompt(missing, enabled, challengeType));
181
+ }
182
+ const input = {
183
+ appId: opt.app.id,
184
+ envId: opt.env.id,
185
+ enabled,
186
+ challengeType
187
+ };
188
+ if (absolute !== undefined) {
189
+ input.connectionThresholdAbsolute = absolute;
190
+ }
191
+ if (percentage !== undefined) {
192
+ input.connectionThresholdPercentage = percentage;
193
+ }
194
+ const currentConfig = opt.env.defensiveMode?.config?.effective ?? null;
195
+ const settingRows = buildSettingRows(currentConfig, {
196
+ enabled,
197
+ challengeType,
198
+ absolute,
199
+ percentage
200
+ });
201
+ console.log(`Defensive mode configuration for ${_chalk.default.bold(opt.app.name)} (${(0, _format.formatEnvironment)(opt.env.type)}):`);
202
+ console.log((0, _format.table)(settingRows));
203
+
204
+ // Production mutations require confirmation. In non-interactive contexts this
205
+ // hard-errors unless --skip-confirmation is passed, so unattended/CI runs fail
206
+ // fast rather than silently mutating production — matching enable/disable.
207
+ await (0, _cliHelpers.guardProductionMutation)(opt, 'configure', trackingParams, _input.confirm, _tracker.trackEvent, _format.formatEnvironment);
208
+ const result = await (0, _api.updateDefensiveModeConfig)(input);
209
+ await (0, _cliHelpers.reportMutationResult)(result, trackingParams, 'configure', opt.app.name, opt.env.type, 'configuration updated', 'update defensive mode config', _tracker.trackEvent);
210
+ }
211
+ (0, _command.default)({
212
+ appContext: true,
213
+ appQuery: _api.appQuery,
214
+ envContext: true,
215
+ usage: baseUsage
216
+ }).option('enabled', 'Whether defensive mode should be enabled (true|false). Required.').option('challenge-type', 'Challenge type integer. Required.').option('connection-threshold-absolute', 'Absolute connection threshold that triggers defensive mode.').option('connection-threshold-percentage', 'Connection threshold percentage that triggers defensive mode.').option('non-interactive', 'Disable prompts and browser-open; fail fast if a required flag is missing.', false).option('skip-confirmation', 'Skip the confirmation prompt for production envs.', false).option('rechallenge-wait', 'When step-up verification is required non-interactively, print the URL and wait for verification on another device instead of failing fast.', false).examples(examples).argv(process.argv, defensiveModeConfigureCommand);
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ exports.__esModule = true;
5
+ exports.defensiveModeDisableCommand = defensiveModeDisableCommand;
6
+ var _command = _interopRequireDefault(require("../lib/cli/command"));
7
+ var _format = require("../lib/cli/format");
8
+ var _api = require("../lib/defensive-mode/api");
9
+ var _cliHelpers = require("../lib/defensive-mode/cli-helpers");
10
+ var _input = require("../lib/envvar/input");
11
+ var _tracker = require("../lib/tracker");
12
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
+ const baseUsage = 'vip defensive-mode disable';
14
+ const exampleUsage = 'vip @example-app.production defensive-mode disable';
15
+ const examples = [{
16
+ usage: exampleUsage,
17
+ description: 'Disable defensive mode for the environment.'
18
+ }];
19
+ async function defensiveModeDisableCommand(_args, opt) {
20
+ const trackingParams = {
21
+ app_id: opt.app.id,
22
+ command: baseUsage,
23
+ env_id: opt.env.id,
24
+ org_id: opt.app.organization.id,
25
+ org_sfid: opt.app.organization.salesforceId,
26
+ skip_confirm: Boolean(opt.skipConfirmation)
27
+ };
28
+ await (0, _tracker.trackEvent)('defensive_mode_disable_command_execute', trackingParams);
29
+ await (0, _cliHelpers.guardProductionMutation)(opt, 'disable', trackingParams, _input.confirm, _tracker.trackEvent, _format.formatEnvironment);
30
+ const result = await (0, _api.updateDefensiveModeStatus)({
31
+ appId: opt.app.id,
32
+ envId: opt.env.id,
33
+ enabled: false
34
+ });
35
+ await (0, _cliHelpers.reportMutationResult)(result, trackingParams, 'disable', opt.app.name, opt.env.type, 'disabled', 'disable defensive mode', _tracker.trackEvent);
36
+ }
37
+ (0, _command.default)({
38
+ appContext: true,
39
+ appQuery: _api.appQuery,
40
+ envContext: true,
41
+ usage: baseUsage
42
+ }).option('skip-confirmation', 'Skip the confirmation prompt for production envs.', false).option('non-interactive', 'Disable prompts; error if a production mutation is attempted without --skip-confirmation.', false).option('rechallenge-wait', 'When step-up verification is required non-interactively, print the URL and wait for verification on another device instead of failing fast.', false).examples(examples).argv(process.argv, defensiveModeDisableCommand);
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ exports.__esModule = true;
5
+ exports.defensiveModeEnableCommand = defensiveModeEnableCommand;
6
+ var _command = _interopRequireDefault(require("../lib/cli/command"));
7
+ var _format = require("../lib/cli/format");
8
+ var _api = require("../lib/defensive-mode/api");
9
+ var _cliHelpers = require("../lib/defensive-mode/cli-helpers");
10
+ var _input = require("../lib/envvar/input");
11
+ var _tracker = require("../lib/tracker");
12
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
+ const baseUsage = 'vip defensive-mode enable';
14
+ const exampleUsage = 'vip @example-app.production defensive-mode enable';
15
+ const examples = [{
16
+ usage: exampleUsage,
17
+ description: 'Enable defensive mode for the environment (interactive).'
18
+ }, {
19
+ usage: `${exampleUsage} --skip-confirmation`,
20
+ description: 'Enable defensive mode without the production confirmation prompt.'
21
+ }];
22
+ async function defensiveModeEnableCommand(_args, opt) {
23
+ const trackingParams = {
24
+ app_id: opt.app.id,
25
+ command: baseUsage,
26
+ env_id: opt.env.id,
27
+ org_id: opt.app.organization.id,
28
+ org_sfid: opt.app.organization.salesforceId,
29
+ skip_confirm: Boolean(opt.skipConfirmation)
30
+ };
31
+ await (0, _tracker.trackEvent)('defensive_mode_enable_command_execute', trackingParams);
32
+ await (0, _cliHelpers.guardProductionMutation)(opt, 'enable', trackingParams, _input.confirm, _tracker.trackEvent, _format.formatEnvironment);
33
+ const result = await (0, _api.updateDefensiveModeStatus)({
34
+ appId: opt.app.id,
35
+ envId: opt.env.id,
36
+ enabled: true
37
+ });
38
+ await (0, _cliHelpers.reportMutationResult)(result, trackingParams, 'enable', opt.app.name, opt.env.type, 'enabled', 'enable defensive mode', _tracker.trackEvent);
39
+ }
40
+ (0, _command.default)({
41
+ appContext: true,
42
+ appQuery: _api.appQuery,
43
+ envContext: true,
44
+ usage: baseUsage
45
+ }).option('skip-confirmation', 'Skip the confirmation prompt for production envs.', false).option('non-interactive', 'Disable prompts; error if a production mutation is attempted without --skip-confirmation.', false).option('rechallenge-wait', 'When step-up verification is required non-interactively, print the URL and wait for verification on another device instead of failing fast.', false).examples(examples).argv(process.argv, defensiveModeEnableCommand);
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ var _command = _interopRequireDefault(require("../lib/cli/command"));
5
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
6
+ const usage = 'vip defensive-mode';
7
+ const exampleUsage = 'vip @example-app.production defensive-mode';
8
+ const examples = [{
9
+ usage: `${exampleUsage} enable`,
10
+ description: 'Enable defensive mode for the environment.'
11
+ }, {
12
+ usage: `${exampleUsage} disable`,
13
+ description: 'Disable defensive mode for the environment.'
14
+ }, {
15
+ usage: `${exampleUsage} configure --enabled=true --challenge-type=1`,
16
+ description: 'Update the defensive mode configuration non-interactively.'
17
+ }];
18
+ (0, _command.default)({
19
+ requiredArgs: 1,
20
+ usage
21
+ }).command('enable', 'Enable defensive mode (step-up auth required).').command('disable', 'Disable defensive mode (step-up auth required).').command('configure', 'Update the defensive mode configuration (step-up auth required).').examples(examples).argv(process.argv);
@@ -48,12 +48,6 @@ const appQuery = `
48
48
  dbOperationInProgress
49
49
  importInProgress
50
50
  }
51
- wpSitesSDS {
52
- nodes {
53
- homeUrl
54
- id
55
- }
56
- }
57
51
  }
58
52
  `;
59
53
  const START_IMPORT_MUTATION = (0, _graphqlTag.default)`
@@ -403,7 +397,6 @@ const displayPlaybook = ({
403
397
  fileName,
404
398
  domain,
405
399
  formattedEnvironment,
406
- unformattedEnvironment,
407
400
  isMultiSite,
408
401
  app
409
402
  }) => {
@@ -418,53 +411,19 @@ const displayPlaybook = ({
418
411
  };
419
412
  (0, _format.formatSearchReplaceValues)(searchReplace, output);
420
413
  }
421
- let siteArray = [];
422
414
  if (isMultiSite) {
423
415
  console.log(` multisite: ${isMultiSite.toString()}`);
424
- const selectedEnvironmentObj = app?.environments?.find(env => unformattedEnvironment === env.type);
425
- siteArray = selectedEnvironmentObj?.wpSitesSDS?.nodes;
426
416
  }
427
417
  if (!tableNames.length) {
428
418
  debug('Validation was skipped. No playbook information will be displayed.');
429
419
  } else {
430
420
  // output the table names
431
421
  console.log();
432
- if (!isMultiSite) {
433
- console.log('Tables that will be imported by this process:');
434
- console.log((0, _cliColumns.default)(tableNames));
435
- } else {
436
- // we have siteArray from the API, use it and the table names together
437
- if (siteArray === 'undefined' || !siteArray) {
438
- console.log(_chalk.default.yellowBright('Unable to determine the network sites affected by this import. Please proceed only if you are confident that the contents of the file are valid for import.'));
439
- return;
440
- } else if (!siteArray?.length) {
441
- throw new Error('There were no sites in your multisite installation.');
442
- }
443
- const multiSiteBreakdown = siteArray.map(wpSite => {
444
- let siteRegex;
445
- if (wpSite.id === 1) {
446
- siteRegex = /^wp_[a-z]+/i;
447
- } else {
448
- // eslint-disable-next-line security/detect-non-literal-regexp
449
- siteRegex = new RegExp(`^wp_${parseInt(wpSite.id, 10)}_[a-z]+`, 'i');
450
- }
451
- const tableNamesInGroup = tableNames.filter(name => siteRegex.test(name));
452
- return {
453
- id: wpSite.id,
454
- url: wpSite.homeUrl,
455
- tables: tableNamesInGroup
456
- };
457
- });
458
- if (launched) {
459
- console.log(_chalk.default.yellowBright('You are updating tables in a launched multisite environment. The performance of sites on the network might be impacted by this operation.'));
460
- }
461
- console.log(_chalk.default.yellow('The following sites will be affected by the import:'));
462
- multiSiteBreakdown.forEach(siteObject => {
463
- console.log();
464
- console.log(_chalk.default.blueBright(`Blog with ID ${siteObject.id} and URL ${siteObject.url} will import the following tables:`));
465
- console.log((0, _cliColumns.default)(siteObject.tables));
466
- });
422
+ if (isMultiSite && launched) {
423
+ console.log(_chalk.default.yellowBright('You are updating tables in a launched multisite environment. The performance of sites on the network might be impacted by this operation.'));
467
424
  }
425
+ console.log('Tables that will be imported by this process:');
426
+ console.log((0, _cliColumns.default)(tableNames));
468
427
  }
469
428
  };
470
429
  (0, _command.default)({
@@ -546,7 +505,6 @@ const displayPlaybook = ({
546
505
  fileName: fileNameOrURL,
547
506
  domain,
548
507
  formattedEnvironment,
549
- unformattedEnvironment: opts.env.type,
550
508
  isMultiSite,
551
509
  app
552
510
  });
package/dist/bin/vip.js CHANGED
@@ -9,6 +9,7 @@ var _command = _interopRequireWildcard(require("../lib/cli/command"));
9
9
  var _config = _interopRequireDefault(require("../lib/cli/config"));
10
10
  var _internalBinLoader = require("../lib/cli/internal-bin-loader");
11
11
  var _seaDispatch = require("../lib/cli/sea-dispatch");
12
+ var _tokenCache = _interopRequireDefault(require("../lib/rechallenge/token-cache"));
12
13
  var _token = _interopRequireDefault(require("../lib/token"));
13
14
  var _tracker = require("../lib/tracker");
14
15
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
@@ -42,7 +43,7 @@ async function maybeExecuteSeaTargetCommand() {
42
43
  }
43
44
  const runCmd = async function () {
44
45
  const cmd = (0, _command.default)();
45
- cmd.command('logout', 'Log out the current authenticated VIP-CLI user.').command('app', 'Interact with applications that the current authenticated VIP-CLI user has permission to access.').command('backup', 'Generate a backup of an environment.').command('cache', 'Manage page cache for an environment.').command('config', 'Manage environment configurations.').command('dev-env', 'Create and manage VIP Local Development Environments.').command('export', 'Export a copy of data associated with an environment.').command('import', 'Import media or SQL database files to an environment.').command('logs', 'Retrieve Runtime Logs from an environment.').command('search-replace', 'Search for a string in a local SQL file and replace it with a new string.').command('slowlogs', 'Retrieve MySQL slow query logs from an environment.').command('db', "Access an environment's database.").command('sync', 'Sync the database from production to a non-production environment.').command('whoami', 'Retrieve details about the current authenticated VIP-CLI user.').command('wp', 'Execute a WP-CLI command against an environment.');
46
+ cmd.command('logout', 'Log out the current authenticated VIP-CLI user.').command('app', 'Interact with applications that the current authenticated VIP-CLI user has permission to access.').command('backup', 'Generate a backup of an environment.').command('cache', 'Manage page cache for an environment.').command('config', 'Manage environment configurations.').command('dev-env', 'Create and manage VIP Local Development Environments.').command('export', 'Export a copy of data associated with an environment.').command('import', 'Import media or SQL database files to an environment.').command('logs', 'Retrieve Runtime Logs from an environment.').command('search-replace', 'Search for a string in a local SQL file and replace it with a new string.').command('slowlogs', 'Retrieve MySQL slow query logs from an environment.').command('db', "Access an environment's database.").command('defensive-mode', 'Manage VIP defensive mode for an environment.').command('sync', 'Sync the database from production to a non-production environment.').command('whoami', 'Retrieve details about the current authenticated VIP-CLI user.').command('wp', 'Execute a WP-CLI command against an environment.');
46
47
  await cmd.argv(process.argv);
47
48
  };
48
49
 
@@ -123,6 +124,10 @@ async function runLoginFlow() {
123
124
  throw err;
124
125
  }
125
126
 
127
+ // Elevated tokens are keyed by API host + scope, not by user identity. Drop any
128
+ // cached elevation from a previous login so it cannot carry across identities.
129
+ await _tokenCache.default.clearAll();
130
+
126
131
  // De-anonymize user for tracking
127
132
  await (0, _tracker.aliasUser)(token.id);
128
133
  await (0, _tracker.trackEvent)('login_command_token_submit_success');
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.PRODUCTION_API_HOST = exports.API_URL = exports.API_HOST = void 0;
5
+ const PRODUCTION_API_HOST = exports.PRODUCTION_API_HOST = 'https://api.wpvip.com';
6
+ const API_HOST = exports.API_HOST = process.env.API_HOST || PRODUCTION_API_HOST; // NOSONAR
7
+ const API_URL = exports.API_URL = `${API_HOST}/graphql`;
@@ -5,9 +5,22 @@ exports.get = get;
5
5
  var _graphqlTag = _interopRequireDefault(require("graphql-tag"));
6
6
  var _api = _interopRequireDefault(require("../../lib/api"));
7
7
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
8
- const api = (0, _api.default)({
9
- silenceAuthErrors: true
10
- });
8
+ // Lazy-initialize the API client so that this module can be imported during the
9
+ // rechallenge module chain without triggering a circular-dependency crash. The
10
+ // cycle that existed before this change:
11
+ // api.ts → rechallenge/link.ts → flow.ts → tracker.ts → tracks.ts
12
+ // → cli/apiConfig.ts → api/feature-flags.ts → api.ts
13
+ // By deferring construction to the first call we ensure api.ts is fully
14
+ // evaluated before API() is invoked.
15
+ let api = null;
16
+ function getApi() {
17
+ if (!api) {
18
+ api = (0, _api.default)({
19
+ silenceAuthErrors: true
20
+ });
21
+ }
22
+ return api;
23
+ }
11
24
  const isVipQuery = (0, _graphqlTag.default)`
12
25
  query isVIP {
13
26
  me {
@@ -16,7 +29,7 @@ const isVipQuery = (0, _graphqlTag.default)`
16
29
  }
17
30
  `;
18
31
  function get() {
19
- return api.query({
32
+ return getApi().query({
20
33
  query: isVipQuery,
21
34
  fetchPolicy: 'cache-first'
22
35
  });
@@ -4,7 +4,7 @@ exports.__esModule = true;
4
4
  exports.default = void 0;
5
5
  var _debug = _interopRequireDefault(require("debug"));
6
6
  var _undici = require("undici");
7
- var _api = require("../../lib/api");
7
+ var _constants = require("./constants");
8
8
  var _env = _interopRequireDefault(require("../../lib/env"));
9
9
  var _proxyDispatcher = require("../../lib/http/proxy-dispatcher");
10
10
  var _token = _interopRequireDefault(require("../../lib/token"));
@@ -31,8 +31,8 @@ var _default = async (path, options = {}) => {
31
31
 
32
32
  // For convenience, we support just passing in the path to this function...
33
33
  // but some things (Apollo) always pass the full url
34
- if (!path.startsWith(_api.API_HOST)) {
35
- url = `${_api.API_HOST}${path}`;
34
+ if (!path.startsWith(_constants.API_HOST)) {
35
+ url = `${_constants.API_HOST}${path}`;
36
36
  }
37
37
  const authToken = await _token.default.get();
38
38
  const proxyDispatcher = (0, _proxyDispatcher.createProxyDispatcher)(url);
package/dist/lib/api.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.PRODUCTION_API_HOST = exports.API_URL = exports.API_HOST = void 0;
4
+ exports.PRODUCTION_API_HOST = exports.API_HOST = void 0;
5
5
  exports.default = API;
6
6
  exports.disableGlobalGraphQLErrorHandling = disableGlobalGraphQLErrorHandling;
7
7
  exports.enableGlobalGraphQLErrorHandling = enableGlobalGraphQLErrorHandling;
@@ -12,12 +12,16 @@ var _retry = require("@apollo/client/link/retry");
12
12
  var _chalk = _interopRequireDefault(require("chalk"));
13
13
  var _debug = _interopRequireDefault(require("debug"));
14
14
  var _graphql = require("graphql");
15
+ var _constants = require("./api/constants");
16
+ exports.API_URL = _constants.API_URL;
17
+ exports.API_HOST = _constants.API_HOST;
18
+ exports.PRODUCTION_API_HOST = _constants.PRODUCTION_API_HOST;
15
19
  var _http = _interopRequireDefault(require("./api/http"));
16
20
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
17
- // Config
18
- const PRODUCTION_API_HOST = exports.PRODUCTION_API_HOST = 'https://api.wpvip.com';
19
- const API_HOST = exports.API_HOST = process.env.API_HOST || PRODUCTION_API_HOST; // NOSONAR
20
- const API_URL = exports.API_URL = `${API_HOST}/graphql`;
21
+ // Config — re-exported from ./api/constants so modules in the rechallenge tree
22
+ // can import them without pulling in the full api.ts graph (which would create
23
+ // a circular dependency via the rechallenge link).
24
+
21
25
  let globalGraphQLErrorHandlingEnabled = true;
22
26
  const RETRY_LINK_MAX_ATTEMPTS = 5;
23
27
  const RETRY_LINK_INITIAL_DELAY_MS = 1000; // 1 second
@@ -88,10 +92,10 @@ function API({
88
92
  uri: operation => {
89
93
  // to make it easier to write tests, we'll skip adding x_query for tests
90
94
  if (process.env.NODE_ENV === 'test') {
91
- return API_URL;
95
+ return _constants.API_URL;
92
96
  }
93
97
  const operationName = operation.operationName ?? '';
94
- return `${API_URL}?x_query=${encodeURIComponent(operationName)}`;
98
+ return `${_constants.API_URL}?x_query=${encodeURIComponent(operationName)}`;
95
99
  },
96
100
  fetch: _http.default
97
101
  });
@@ -102,8 +106,19 @@ function API({
102
106
  },
103
107
  attempts: shouldRetryRequest
104
108
  });
109
+
110
+ // Lazy-require the rechallenge link to avoid a circular-dependency issue in
111
+ // Jest tests. Importing at module top-level would cause rechallenge/client.ts
112
+ // to be loaded during jest.setupMocks.js (via apiConfig → feature-flags →
113
+ // api.ts → link.ts → client.ts), preventing jest.mock('../api/http') from
114
+ // intercepting the http reference captured inside client.ts. A require()
115
+ // call inside the function body is resolved after all mocks are registered.
116
+
117
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
118
+ const linkMod = require('./rechallenge/link');
119
+ const createRechallengeLink = linkMod.default;
105
120
  return new _core.ApolloClient({
106
- link: _core.ApolloLink.from([errorLink, retryLink, httpLink]),
121
+ link: _core.ApolloLink.from([errorLink, createRechallengeLink(), retryLink, httpLink]),
107
122
  cache: new _core.InMemoryCache({
108
123
  typePolicies: {
109
124
  WPSite: {
@@ -28,6 +28,10 @@ const internalBinLoaders = {
28
28
  'vip-config-software-update': () => import('../../bin/vip-config-software-update'),
29
29
  'vip-db': () => import('../../bin/vip-db'),
30
30
  'vip-db-phpmyadmin': () => import('../../bin/vip-db-phpmyadmin'),
31
+ 'vip-defensive-mode': () => import('../../bin/vip-defensive-mode'),
32
+ 'vip-defensive-mode-configure': () => import('../../bin/vip-defensive-mode-configure'),
33
+ 'vip-defensive-mode-disable': () => import('../../bin/vip-defensive-mode-disable'),
34
+ 'vip-defensive-mode-enable': () => import('../../bin/vip-defensive-mode-enable'),
31
35
  'vip-dev-env': () => import('../../bin/vip-dev-env'),
32
36
  'vip-dev-env-create': () => import('../../bin/vip-dev-env-create'),
33
37
  'vip-dev-env-destroy': () => import('../../bin/vip-dev-env-destroy'),