sf-plugin-data 0.3.0 → 0.5.0

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.
Files changed (46) hide show
  1. package/README.md +34 -15
  2. package/docs/artifact-format.md +4 -4
  3. package/docs/capabilities.md +10 -10
  4. package/docs/git-workflow.md +5 -6
  5. package/docs/handover.md +32 -32
  6. package/lib/api.d.ts +7 -0
  7. package/lib/api.js +5 -1
  8. package/lib/commands/data360/delete.d.ts +57 -12
  9. package/lib/commands/data360/delete.js +191 -42
  10. package/lib/commands/data360/deploy.d.ts +14 -2
  11. package/lib/commands/data360/deploy.js +32 -20
  12. package/lib/commands/data360/publish.d.ts +2 -2
  13. package/lib/commands/data360/publish.js +14 -12
  14. package/lib/commands/data360/retrieve.d.ts +1 -4
  15. package/lib/commands/data360/retrieve.js +17 -32
  16. package/lib/errors/codes/calculated-insight.js +1 -1
  17. package/lib/errors/codes/data-graph.js +2 -2
  18. package/lib/errors/codes/data-stream.js +1 -1
  19. package/lib/errors/codes/data-transform.js +1 -1
  20. package/lib/errors/codes/dlo.js +1 -1
  21. package/lib/errors/codes/dmo.js +1 -1
  22. package/lib/errors/codes/general.js +6 -4
  23. package/lib/errors/codes/identity-resolution.js +1 -1
  24. package/lib/errors/codes/mapping.js +1 -1
  25. package/lib/errors/codes/relationship.js +1 -1
  26. package/lib/errors/codes/segment.js +2 -2
  27. package/lib/services/command-native-data-graph.js +1 -1
  28. package/lib/services/command-native-dmo.js +1 -1
  29. package/lib/services/delete-calculated-insight.js +1 -1
  30. package/lib/services/delete-identity-resolution.js +1 -1
  31. package/lib/services/delete-segment.js +1 -1
  32. package/lib/services/native-data-graph-crd.d.ts +2 -2
  33. package/lib/services/native-data-graph-crd.js +2 -2
  34. package/lib/services/operation-diagnostic.js +1 -1
  35. package/lib/services/publish-segment.d.ts +1 -1
  36. package/lib/services/run-delete.d.ts +43 -0
  37. package/lib/services/run-delete.js +111 -0
  38. package/lib/services/run-deploy.d.ts +51 -0
  39. package/lib/services/run-deploy.js +107 -0
  40. package/lib/services/run-options.d.ts +23 -0
  41. package/lib/services/run-options.js +38 -0
  42. package/lib/services/run-retrieve.d.ts +3 -3
  43. package/lib/services/run-retrieve.js +6 -2
  44. package/lib/transport/connect-client.d.ts +8 -0
  45. package/lib/transport/connect-client.js +25 -1
  46. package/package.json +1 -1
@@ -0,0 +1,111 @@
1
+ import path from 'node:path';
2
+ import Delete from '../commands/data360/delete.js';
3
+ import { nativeApiVersion } from '../transport/api-version.js';
4
+ import { openBoundSsot } from '../transport/bound-ssot.js';
5
+ import { setRetryWarn } from '../transport/connect-client.js';
6
+ import { createNativeDloDeleteClient } from '../transport/dlo-native-rest.js';
7
+ import { createNativeDmoDeleteClient } from '../transport/dmo-native-rest.js';
8
+ import { createDmoClient } from '../transport/dmo-rest.js';
9
+ import { exclusiveOptions, keepExitCode } from './run-options.js';
10
+ /**
11
+ * The flags the CLI would parse from these options, with every CLI default and `--no-prompt`:
12
+ * the caller confirmed the delete. Flag misuse oclif refuses with exit 2 throws OptionError.
13
+ * @param {DeleteOptions} options Library options.
14
+ * @returns {DeleteFlags} Parsed flags.
15
+ */
16
+ function deleteFlags(options) {
17
+ const listed = (values) => values && values.length > 0 ? values : undefined;
18
+ const flags = {
19
+ 'target-org': undefined,
20
+ metadata: listed(options.metadata),
21
+ 'api-version': options.apiVersion,
22
+ dataspace: options.dataspace,
23
+ preview: options.preview ?? false,
24
+ 'changed-since': options.changedSince,
25
+ 'no-prompt': true,
26
+ 'delete-dlo': options.deleteDlo ?? false,
27
+ field: listed(options.field),
28
+ 'output-dir': options.outputDir,
29
+ 'keep-local-files': options.keepLocalFiles ?? false,
30
+ };
31
+ exclusiveOptions(flags, [['changed-since', 'metadata']]);
32
+ return flags;
33
+ }
34
+ /** The delete command over a Connection the caller holds; it never prints, prompts or exits. */
35
+ class ConnectionDelete extends Delete {
36
+ connection;
37
+ flagValues;
38
+ root;
39
+ // The caller's Connection is the target org; the delete services run at API 68.0.
40
+ resolveConnection = async () => {
41
+ this.connection.setApiVersion('68.0');
42
+ return this.connection;
43
+ };
44
+ streamConnection = async () => this.connection;
45
+ // Every bound SSOT request (and its Metadata API reads/updates) goes over the caller's Connection.
46
+ createMappingSender = (target, version) => this.bound(target, version);
47
+ createRelationshipSender = (target, version) => this.bound(target, version);
48
+ createDataSpaceSender = (target, version) => this.bound(target, version);
49
+ createDmoNativeDeleteClient = (target, apiVersion) => createNativeDmoDeleteClient(target, apiVersion, async () => this.connection);
50
+ createDloNativeDeleteClient = (target, apiVersion) => createNativeDloDeleteClient(target, apiVersion, async () => this.connection);
51
+ // Field-level delete: reads on the caller's Connection; the removal itself is the CLI child.
52
+ createDmoClient = (...[target, apiVersion, runner, sender, listed]) => createDmoClient(target, apiVersion, runner, sender ?? this.bound(target, nativeApiVersion(apiVersion)), listed);
53
+ constructor(connection, flagValues, root) {
54
+ // No oclif configuration: the command is never parsed, logged or rendered here.
55
+ super([], {});
56
+ this.connection = connection;
57
+ this.flagValues = flagValues;
58
+ this.root = root;
59
+ // Retries of the requests over the caller's Connection are not printed either.
60
+ setRetryWarn(connection, () => { });
61
+ }
62
+ jsonEnabled() {
63
+ return true;
64
+ }
65
+ // Graph progress is not printed.
66
+ graphProgress() { }
67
+ projectDirectory() {
68
+ return this.root;
69
+ }
70
+ // The flags as the CLI would have parsed them; never is assignable to oclif's generic result.
71
+ async parse() {
72
+ return { flags: this.flagValues };
73
+ }
74
+ /**
75
+ * A bound sender over the caller's Connection.
76
+ * @param {string} target Username of the Connection.
77
+ * @param {string} version Numeric API version of the routes.
78
+ * @returns {BoundSsot} Sender.
79
+ */
80
+ bound(target, version) {
81
+ return openBoundSsot(target, version, async () => this.connection);
82
+ }
83
+ }
84
+ /**
85
+ * Run `sf data360 delete` over a Connection the caller holds and return its envelope (what
86
+ * `--json` shows). It never asks: the caller confirmed the delete (as `--no-prompt`). Nothing is
87
+ * printed and `process.exitCode` is left alone; use exitCodeOf on the envelope for the Salesforce
88
+ * CLI exit code. A refusal is returned as a blocked envelope, not thrown. The delete services run
89
+ * at API 68.0 on the Connection; its API version is restored afterwards.
90
+ *
91
+ * Every REST and Metadata API call goes over the given Connection. The one exception: a mapping
92
+ * delete and a field-level delete (`field`) send their Metadata API destructive change through
93
+ * the Salesforce CLI child `sf project deploy start -o <username>`, so they need a global `sf`
94
+ * and a stored sf CLI authorization for the Connection's username.
95
+ * @param {Connection} connection Authenticated @salesforce/core Connection of the target org.
96
+ * @param {DeleteOptions} [options] The command's flags.
97
+ * @returns {Promise<DeleteEnvelope>} The delete envelope.
98
+ * @throws {OptionError} For flag misuse the CLI refuses at parse time (exit 2): `changedSince`
99
+ * with `metadata`.
100
+ */
101
+ export async function runDelete(connection, options = {}) {
102
+ const flags = deleteFlags(options);
103
+ const root = path.resolve(options.projectDir ?? process.cwd());
104
+ const version = connection.getApiVersion();
105
+ try {
106
+ return await keepExitCode(async () => new ConnectionDelete(connection, flags, root).run());
107
+ }
108
+ finally {
109
+ connection.setApiVersion(version);
110
+ }
111
+ }
@@ -0,0 +1,51 @@
1
+ import type { Connection } from '@salesforce/core';
2
+ import type { DeployEnvelope } from '../commands/data360/deploy.js';
3
+ /** The flags of `sf data360 deploy`, for a caller that holds its own Connection. */
4
+ export type DeployOptions = {
5
+ /** `--preview`: read-only comparison, nothing written; default false. */
6
+ preview?: boolean;
7
+ /** Selectors (`-m`), e.g. `dlo:Name__dll`; not with `changedSince`. */
8
+ metadata?: string[];
9
+ /** Files or directories (`--source-dir`), relative to `projectDir`; not with `changedSince`. */
10
+ sourceDir?: string[];
11
+ /** `--changed-since`: Git reference; selects the plugin files changed since its merge base. */
12
+ changedSince?: string;
13
+ /** Run data space (`--dataspace`); default `default`. */
14
+ dataspace?: string;
15
+ /** `--api-version`; default `auto`. */
16
+ apiVersion?: string;
17
+ /** Project root holding data360/ and the Git repository; default the current directory. */
18
+ projectDir?: string;
19
+ /** `--allow-cross-dataspace`; default false. */
20
+ allowCrossDataspace?: boolean;
21
+ /** `--allow-processing`; default false. */
22
+ allowProcessing?: boolean;
23
+ /** `--allow-field-removal`; default false. */
24
+ allowFieldRemoval?: boolean;
25
+ /** `--allow-dataspace-removal`; default false. */
26
+ allowDataspaceRemoval?: boolean;
27
+ /** `--async` (identity resolution only); default false. */
28
+ async?: boolean;
29
+ /** `--draft` (calculated insights only); default false. */
30
+ draft?: boolean;
31
+ /** `--wait` in whole minutes (data graphs only, at least 1); default 30 for each graph. */
32
+ wait?: number;
33
+ };
34
+ /**
35
+ * Run `sf data360 deploy` over a Connection the caller holds and return its envelope (what
36
+ * `--json` shows). Nothing is printed, nothing is asked and `process.exitCode` is left alone; use
37
+ * exitCodeOf on the envelope for the Salesforce CLI exit code. A refusal is returned as a blocked
38
+ * envelope, not thrown. The Connection's API version is restored afterwards.
39
+ *
40
+ * Every REST and Metadata API call goes over the given Connection. The one exception: a native
41
+ * DMO field removal (`allowFieldRemoval`) and a mapping pair removal send their Metadata API
42
+ * destructive change through the Salesforce CLI child `sf project deploy start -o <username>`,
43
+ * so they need a global `sf` and a stored sf CLI authorization for the Connection's username.
44
+ * @param {Connection} connection Authenticated @salesforce/core Connection of the target org.
45
+ * @param {DeployOptions} [options] The command's flags.
46
+ * @returns {Promise<DeployEnvelope>} The deploy envelope.
47
+ * @throws {OptionError} For flag misuse the CLI refuses at parse time (exit 2): `changedSince`
48
+ * with `sourceDir` or `metadata`, an `apiVersion` that is neither `auto` nor `NN.0`, a `wait`
49
+ * that is not a whole number of at least 1.
50
+ */
51
+ export declare function runDeploy(connection: Connection, options?: DeployOptions): Promise<DeployEnvelope>;
@@ -0,0 +1,107 @@
1
+ import path from 'node:path';
2
+ import Deploy from '../commands/data360/deploy.js';
3
+ import { openBoundSsot } from '../transport/bound-ssot.js';
4
+ import { setRetryWarn } from '../transport/connect-client.js';
5
+ import { exclusiveOptions, keepExitCode, OptionError } from './run-options.js';
6
+ /**
7
+ * The flags the CLI would parse from these options, with every CLI default; flag misuse oclif
8
+ * refuses with exit 2 throws OptionError.
9
+ * @param {DeployOptions} options Library options.
10
+ * @returns {DeployFlags} Parsed flags.
11
+ */
12
+ function deployFlags(options) {
13
+ const listed = (values) => values && values.length > 0 ? values : undefined;
14
+ const flags = {
15
+ 'target-org': undefined,
16
+ 'api-version': options.apiVersion ?? 'auto',
17
+ dataspace: options.dataspace,
18
+ 'source-dir': listed(options.sourceDir),
19
+ metadata: listed(options.metadata),
20
+ 'changed-since': options.changedSince,
21
+ 'allow-processing': options.allowProcessing ?? false,
22
+ 'allow-field-removal': options.allowFieldRemoval ?? false,
23
+ 'allow-cross-dataspace': options.allowCrossDataspace ?? false,
24
+ 'allow-dataspace-removal': options.allowDataspaceRemoval ?? false,
25
+ async: options.async ?? false,
26
+ wait: options.wait,
27
+ draft: options.draft ?? false,
28
+ preview: options.preview ?? false,
29
+ };
30
+ exclusiveOptions(flags, [
31
+ ['changed-since', 'source-dir'],
32
+ ['changed-since', 'metadata'],
33
+ ]);
34
+ if (flags['api-version'] !== 'auto' && !/^[1-9]\d\.0$/u.test(flags['api-version']))
35
+ throw new OptionError(`--api-version=${flags['api-version']} is not a valid API version.`);
36
+ if (flags.wait !== undefined && (!Number.isSafeInteger(flags.wait) || flags.wait < 1))
37
+ throw new OptionError(`--wait=${String(flags.wait)} must be a whole number of at least 1.`);
38
+ return flags;
39
+ }
40
+ /** The deploy command over a Connection the caller holds; it never prints, prompts or exits. */
41
+ class ConnectionDeploy extends Deploy {
42
+ connection;
43
+ flagValues;
44
+ root;
45
+ // The caller's Connection is the target org; each family sets the version it pins.
46
+ resolveTargetOrg = async () => ({
47
+ getOrgId: () => this.connection.getAuthInfoFields().orgId,
48
+ getUsername: () => this.connection.getUsername(),
49
+ getConnection: (version) => {
50
+ // Only a numeric version reaches the connection; @salesforce/core rejects `auto`.
51
+ if (version && /^[1-9]\d\.0$/u.test(version))
52
+ this.connection.setApiVersion(version);
53
+ return this.connection;
54
+ },
55
+ });
56
+ // Every bound SSOT request (and its Metadata API reads/updates) goes over the caller's Connection.
57
+ createSsotSender = (target, version) => openBoundSsot(target, version, async () => this.connection);
58
+ constructor(connection, flagValues, root) {
59
+ // No oclif configuration: the command is never parsed, logged or rendered here.
60
+ super([], {});
61
+ this.connection = connection;
62
+ this.flagValues = flagValues;
63
+ this.root = root;
64
+ // Retries of the requests over the caller's Connection are not printed either.
65
+ setRetryWarn(connection, () => { });
66
+ }
67
+ jsonEnabled() {
68
+ return true;
69
+ }
70
+ // Graph readiness progress is not printed.
71
+ graphProgress() { }
72
+ projectDirectory() {
73
+ return this.root;
74
+ }
75
+ // The flags as the CLI would have parsed them; never is assignable to oclif's generic result.
76
+ async parse() {
77
+ return { flags: this.flagValues };
78
+ }
79
+ }
80
+ /**
81
+ * Run `sf data360 deploy` over a Connection the caller holds and return its envelope (what
82
+ * `--json` shows). Nothing is printed, nothing is asked and `process.exitCode` is left alone; use
83
+ * exitCodeOf on the envelope for the Salesforce CLI exit code. A refusal is returned as a blocked
84
+ * envelope, not thrown. The Connection's API version is restored afterwards.
85
+ *
86
+ * Every REST and Metadata API call goes over the given Connection. The one exception: a native
87
+ * DMO field removal (`allowFieldRemoval`) and a mapping pair removal send their Metadata API
88
+ * destructive change through the Salesforce CLI child `sf project deploy start -o <username>`,
89
+ * so they need a global `sf` and a stored sf CLI authorization for the Connection's username.
90
+ * @param {Connection} connection Authenticated @salesforce/core Connection of the target org.
91
+ * @param {DeployOptions} [options] The command's flags.
92
+ * @returns {Promise<DeployEnvelope>} The deploy envelope.
93
+ * @throws {OptionError} For flag misuse the CLI refuses at parse time (exit 2): `changedSince`
94
+ * with `sourceDir` or `metadata`, an `apiVersion` that is neither `auto` nor `NN.0`, a `wait`
95
+ * that is not a whole number of at least 1.
96
+ */
97
+ export async function runDeploy(connection, options = {}) {
98
+ const flags = deployFlags(options);
99
+ const root = path.resolve(options.projectDir ?? process.cwd());
100
+ const version = connection.getApiVersion();
101
+ try {
102
+ return await keepExitCode(async () => new ConnectionDeploy(connection, flags, root).run());
103
+ }
104
+ finally {
105
+ connection.setApiVersion(version);
106
+ }
107
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Flag misuse the Salesforce CLI refuses while parsing (exit 2, no envelope), raised by the library
3
+ * calls before anything is read: options that cannot be combined or a value the flag does not
4
+ * take. `code` is `INVALID_OPTION`; `exitCode` is the CLI's 2. The message uses the CLI's wording.
5
+ */
6
+ export declare class OptionError extends Error {
7
+ readonly code = "INVALID_OPTION";
8
+ readonly exitCode = 2;
9
+ constructor(message: string);
10
+ }
11
+ /**
12
+ * Refuse flags that cannot be given together, as oclif's `exclusive` does.
13
+ * @param {Record<string, unknown>} flags Flags with their values (undefined when not given).
14
+ * @param {Array<[string, string]>} pairs Flag names that exclude each other.
15
+ */
16
+ export declare function exclusiveOptions(flags: Record<string, unknown>, pairs: Array<[string, string]>): void;
17
+ /**
18
+ * Run a command and leave `process.exitCode` as it was before.
19
+ * @template T
20
+ * @param {Function} call The command run.
21
+ * @returns {Promise<T>} Its result.
22
+ */
23
+ export declare function keepExitCode<T>(call: () => Promise<T>): Promise<T>;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Flag misuse the Salesforce CLI refuses while parsing (exit 2, no envelope), raised by the library
3
+ * calls before anything is read: options that cannot be combined or a value the flag does not
4
+ * take. `code` is `INVALID_OPTION`; `exitCode` is the CLI's 2. The message uses the CLI's wording.
5
+ */
6
+ export class OptionError extends Error {
7
+ code = 'INVALID_OPTION';
8
+ exitCode = 2;
9
+ constructor(message) {
10
+ super(message);
11
+ this.name = 'OptionError';
12
+ }
13
+ }
14
+ /**
15
+ * Refuse flags that cannot be given together, as oclif's `exclusive` does.
16
+ * @param {Record<string, unknown>} flags Flags with their values (undefined when not given).
17
+ * @param {Array<[string, string]>} pairs Flag names that exclude each other.
18
+ */
19
+ export function exclusiveOptions(flags, pairs) {
20
+ for (const [first, second] of pairs)
21
+ if (flags[first] !== undefined && flags[second] !== undefined)
22
+ throw new OptionError(`--${first}=${String(flags[first])} cannot also be provided when using --${second}`);
23
+ }
24
+ /**
25
+ * Run a command and leave `process.exitCode` as it was before.
26
+ * @template T
27
+ * @param {Function} call The command run.
28
+ * @returns {Promise<T>} Its result.
29
+ */
30
+ export async function keepExitCode(call) {
31
+ const exitCode = process.exitCode;
32
+ try {
33
+ return await call();
34
+ }
35
+ finally {
36
+ process.exitCode = exitCode;
37
+ }
38
+ }
@@ -8,15 +8,15 @@ export type RetrieveOptions = {
8
8
  metadata?: string[];
9
9
  /** Project root (`--output-dir`); default the current directory. */
10
10
  outputDir?: string;
11
- /** Permit replacing differing files (`--overwrite`). */
12
- overwrite?: boolean;
13
11
  /** `--api-version`; default `auto`. */
14
12
  apiVersion?: string;
15
13
  };
16
14
  /**
17
15
  * Run `sf data360 retrieve` over a Connection the caller holds and return its envelope (what
18
16
  * `--json` shows). Nothing is printed and `process.exitCode` is left alone; use exitCodeOf on the
19
- * envelope for the Salesforce CLI exit code. The Connection's API version is restored afterwards.
17
+ * envelope for the Salesforce CLI exit code. A local file whose bytes differ from the retrieved
18
+ * component is replaced; identical bytes are left unchanged. The Connection's API version is
19
+ * restored afterwards.
20
20
  * @param {Connection} connection Authenticated @salesforce/core Connection of the source org.
21
21
  * @param {RetrieveOptions} [options] The command's flags.
22
22
  * @returns {Promise<RetrieveEnvelope>} The retrieve envelope.
@@ -1,4 +1,5 @@
1
1
  import Retrieve from '../commands/data360/retrieve.js';
2
+ import { setRetryWarn } from '../transport/connect-client.js';
2
3
  /** The retrieve command over a Connection the caller holds; it never prints or exits. */
3
4
  class ConnectionRetrieve extends Retrieve {
4
5
  connection;
@@ -16,6 +17,8 @@ class ConnectionRetrieve extends Retrieve {
16
17
  // No oclif configuration: the command is never parsed, logged or rendered here.
17
18
  super([], {});
18
19
  this.connection = connection;
20
+ // Retries of the requests over the caller's Connection are not printed either.
21
+ setRetryWarn(connection, () => { });
19
22
  }
20
23
  /**
21
24
  * Retrieve as the command does with these options.
@@ -28,14 +31,15 @@ class ConnectionRetrieve extends Retrieve {
28
31
  dataspace: options.dataspace,
29
32
  metadata: options.metadata,
30
33
  'output-dir': options.outputDir,
31
- overwrite: options.overwrite ?? false,
32
34
  });
33
35
  }
34
36
  }
35
37
  /**
36
38
  * Run `sf data360 retrieve` over a Connection the caller holds and return its envelope (what
37
39
  * `--json` shows). Nothing is printed and `process.exitCode` is left alone; use exitCodeOf on the
38
- * envelope for the Salesforce CLI exit code. The Connection's API version is restored afterwards.
40
+ * envelope for the Salesforce CLI exit code. A local file whose bytes differ from the retrieved
41
+ * component is replaced; identical bytes are left unchanged. The Connection's API version is
42
+ * restored afterwards.
39
43
  * @param {Connection} connection Authenticated @salesforce/core Connection of the source org.
40
44
  * @param {RetrieveOptions} [options] The command's flags.
41
45
  * @returns {Promise<RetrieveEnvelope>} The retrieve envelope.
@@ -31,6 +31,7 @@ export declare class ConnectError extends Error {
31
31
  * Bounded retry of in-process requests. GETs retry after network errors and HTTP 429/503;
32
32
  * writes retry only when the request provably never reached Salesforce. `sleep` and `warn`
33
33
  * are replaceable (tests, commands); `warn` defaults to one stderr line so JSON stdout stays clean.
34
+ * A Connection with its own sink (setRetryWarn) does not use `warn`.
34
35
  */
35
36
  export declare const connectRetry: {
36
37
  attempts: number;
@@ -39,6 +40,13 @@ export declare const connectRetry: {
39
40
  sleep: (milliseconds: number) => Promise<void>;
40
41
  warn: (message: string) => void;
41
42
  };
43
+ /**
44
+ * Set where the retry warnings of every ConnectClient over this Connection go (a library run
45
+ * passes a no-op); other Connections keep `connectRetry.warn`.
46
+ * @param {object} connection Connection the clients are built on (or a prototype of it).
47
+ * @param {(message: string) => void} warn Sink for one warning line.
48
+ */
49
+ export declare function setRetryWarn(connection: object, warn: (message: string) => void): void;
42
50
  type AuthenticatedConnection = Pick<Connection, 'instanceUrl' | 'request'>;
43
51
  /** Bounded SSOT transport; Salesforce core owns credentials. */
44
52
  export declare class ConnectClient {
@@ -46,6 +46,7 @@ export class ConnectError extends Error {
46
46
  * Bounded retry of in-process requests. GETs retry after network errors and HTTP 429/503;
47
47
  * writes retry only when the request provably never reached Salesforce. `sleep` and `warn`
48
48
  * are replaceable (tests, commands); `warn` defaults to one stderr line so JSON stdout stays clean.
49
+ * A Connection with its own sink (setRetryWarn) does not use `warn`.
49
50
  */
50
51
  export const connectRetry = {
51
52
  attempts: 3,
@@ -60,6 +61,29 @@ export const connectRetry = {
60
61
  process.stderr.write(`Warning: ${message}\n`);
61
62
  },
62
63
  };
64
+ /** Retry warning sinks per Connection; a client without one uses `connectRetry.warn`. */
65
+ const retryWarnings = new WeakMap();
66
+ /**
67
+ * Set where the retry warnings of every ConnectClient over this Connection go (a library run
68
+ * passes a no-op); other Connections keep `connectRetry.warn`.
69
+ * @param {object} connection Connection the clients are built on (or a prototype of it).
70
+ * @param {(message: string) => void} warn Sink for one warning line.
71
+ */
72
+ export function setRetryWarn(connection, warn) {
73
+ retryWarnings.set(connection, warn);
74
+ }
75
+ /**
76
+ * Send one retry warning to the sink of a Connection (or of a receiver derived from it with
77
+ * Object.create), else to `connectRetry.warn`.
78
+ * @param {object} connection Connection of a client.
79
+ * @param {string} message Warning line.
80
+ */
81
+ function retryWarn(connection, message) {
82
+ let current = connection;
83
+ while (current && !retryWarnings.has(current))
84
+ current = Object.getPrototypeOf(current);
85
+ (current ? retryWarnings.get(current) : connectRetry.warn)(message);
86
+ }
63
87
  // Codes raised before a connection exists: the request never reached Salesforce.
64
88
  const unsentCodes = new Set(['ECONNREFUSED', 'ENOTFOUND', 'EHOSTUNREACH', 'EAI_AGAIN']);
65
89
  const readRetryCodes = new Set([...unsentCodes, 'ETIMEDOUT', 'ECONNRESET', 'ECONNABORTED']);
@@ -330,7 +354,7 @@ export class ConnectClient {
330
354
  throw error;
331
355
  const url = typeof request === 'object' ? request.url : request;
332
356
  const route = typeof url === 'string' ? url.split('?')[0].replace(this.origin, '') : '';
333
- connectRetry.warn(`${method} ${route} failed (${plan.reason}); retrying in ${Math.ceil(plan.wait / 1000)} s (attempt ${attempt + 1} of ${connectRetry.attempts}).`);
357
+ retryWarn(this.connection, `${method} ${route} failed (${plan.reason}); retrying in ${Math.ceil(plan.wait / 1000)} s (attempt ${attempt + 1} of ${connectRetry.attempts}).`);
334
358
  await connectRetry.sleep(plan.wait);
335
359
  }
336
360
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sf-plugin-data",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "Salesforce CLI bootstrap for portable Data360 configuration",
5
5
  "author": {
6
6
  "name": "Jörn Berkefeld",