sf-plugin-data 0.0.0-stage → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +589 -2
- package/bin/run.js +4 -0
- package/docs/artifact-format.md +527 -0
- package/docs/capabilities.md +1208 -0
- package/docs/git-workflow.md +160 -0
- package/docs/handover.md +10446 -0
- package/lib/api.d.ts +7 -0
- package/lib/api.js +7 -0
- package/lib/artifacts/code-fields.d.ts +49 -0
- package/lib/artifacts/code-fields.js +142 -0
- package/lib/artifacts/data-graph-document.d.ts +29 -0
- package/lib/artifacts/data-graph-document.js +52 -0
- package/lib/artifacts/data-space-field.d.ts +65 -0
- package/lib/artifacts/data-space-field.js +98 -0
- package/lib/artifacts/data-stream-document.d.ts +264 -0
- package/lib/artifacts/data-stream-document.js +680 -0
- package/lib/artifacts/data-transform-document.d.ts +152 -0
- package/lib/artifacts/data-transform-document.js +560 -0
- package/lib/artifacts/dependency-selection.d.ts +51 -0
- package/lib/artifacts/dependency-selection.js +154 -0
- package/lib/artifacts/dlo-document.d.ts +93 -0
- package/lib/artifacts/dlo-document.js +253 -0
- package/lib/artifacts/enum-case.d.ts +39 -0
- package/lib/artifacts/enum-case.js +67 -0
- package/lib/artifacts/git-deploy-selection.d.ts +68 -0
- package/lib/artifacts/git-deploy-selection.js +474 -0
- package/lib/artifacts/identity-resolution-document.d.ts +133 -0
- package/lib/artifacts/identity-resolution-document.js +625 -0
- package/lib/artifacts/mapping-document.d.ts +1 -0
- package/lib/artifacts/mapping-document.js +1 -0
- package/lib/artifacts/native-calculated-insight-document.d.ts +142 -0
- package/lib/artifacts/native-calculated-insight-document.js +321 -0
- package/lib/artifacts/native-data-graph-document.d.ts +124 -0
- package/lib/artifacts/native-data-graph-document.js +780 -0
- package/lib/artifacts/native-dmo-document.d.ts +151 -0
- package/lib/artifacts/native-dmo-document.js +313 -0
- package/lib/artifacts/native-mapping-document.d.ts +67 -0
- package/lib/artifacts/native-mapping-document.js +187 -0
- package/lib/artifacts/native-relationship-document.d.ts +76 -0
- package/lib/artifacts/native-relationship-document.js +232 -0
- package/lib/artifacts/path-selector.d.ts +55 -0
- package/lib/artifacts/path-selector.js +167 -0
- package/lib/artifacts/read-deploy-input.d.ts +239 -0
- package/lib/artifacts/read-deploy-input.js +1359 -0
- package/lib/artifacts/segment-document.d.ts +183 -0
- package/lib/artifacts/segment-document.js +570 -0
- package/lib/artifacts/verbatim-document.d.ts +203 -0
- package/lib/artifacts/verbatim-document.js +339 -0
- package/lib/artifacts/write-data-graph.d.ts +17 -0
- package/lib/artifacts/write-data-graph.js +162 -0
- package/lib/artifacts/write-data-stream.d.ts +11 -0
- package/lib/artifacts/write-data-stream.js +175 -0
- package/lib/artifacts/write-data-transform.d.ts +11 -0
- package/lib/artifacts/write-data-transform.js +180 -0
- package/lib/artifacts/write-dlo.d.ts +11 -0
- package/lib/artifacts/write-dlo.js +158 -0
- package/lib/artifacts/write-identity-resolution.d.ts +18 -0
- package/lib/artifacts/write-identity-resolution.js +165 -0
- package/lib/artifacts/write-mapping-definition.d.ts +17 -0
- package/lib/artifacts/write-mapping-definition.js +127 -0
- package/lib/artifacts/write-member-export.d.ts +88 -0
- package/lib/artifacts/write-member-export.js +199 -0
- package/lib/artifacts/write-native-calculated-insight.d.ts +21 -0
- package/lib/artifacts/write-native-calculated-insight.js +176 -0
- package/lib/artifacts/write-native-dmo.d.ts +11 -0
- package/lib/artifacts/write-native-dmo.js +158 -0
- package/lib/artifacts/write-native-mapping.d.ts +11 -0
- package/lib/artifacts/write-native-mapping.js +157 -0
- package/lib/artifacts/write-native-relationship.d.ts +11 -0
- package/lib/artifacts/write-native-relationship.js +157 -0
- package/lib/artifacts/write-segment.d.ts +26 -0
- package/lib/artifacts/write-segment.js +212 -0
- package/lib/commands/data360/delete.d.ts +280 -0
- package/lib/commands/data360/delete.js +2297 -0
- package/lib/commands/data360/deploy.d.ts +303 -0
- package/lib/commands/data360/deploy.js +2400 -0
- package/lib/commands/data360/publish.d.ts +93 -0
- package/lib/commands/data360/publish.js +292 -0
- package/lib/commands/data360/retrieve.d.ts +322 -0
- package/lib/commands/data360/retrieve.js +1963 -0
- package/lib/commands/data360/segment/members/export.d.ts +101 -0
- package/lib/commands/data360/segment/members/export.js +318 -0
- package/lib/errors/codes/calculated-insight.d.ts +2 -0
- package/lib/errors/codes/calculated-insight.js +25 -0
- package/lib/errors/codes/data-graph.d.ts +2 -0
- package/lib/errors/codes/data-graph.js +22 -0
- package/lib/errors/codes/data-stream.d.ts +2 -0
- package/lib/errors/codes/data-stream.js +70 -0
- package/lib/errors/codes/data-transform.d.ts +2 -0
- package/lib/errors/codes/data-transform.js +35 -0
- package/lib/errors/codes/dependency.d.ts +2 -0
- package/lib/errors/codes/dependency.js +11 -0
- package/lib/errors/codes/dlo.d.ts +2 -0
- package/lib/errors/codes/dlo.js +31 -0
- package/lib/errors/codes/dmo.d.ts +2 -0
- package/lib/errors/codes/dmo.js +28 -0
- package/lib/errors/codes/general.d.ts +2 -0
- package/lib/errors/codes/general.js +57 -0
- package/lib/errors/codes/identity-resolution.d.ts +2 -0
- package/lib/errors/codes/identity-resolution.js +24 -0
- package/lib/errors/codes/mapping.d.ts +2 -0
- package/lib/errors/codes/mapping.js +42 -0
- package/lib/errors/codes/relationship.d.ts +2 -0
- package/lib/errors/codes/relationship.js +21 -0
- package/lib/errors/codes/segment.d.ts +2 -0
- package/lib/errors/codes/segment.js +49 -0
- package/lib/errors/registry.d.ts +60 -0
- package/lib/errors/registry.js +123 -0
- package/lib/plugin.d.ts +5 -0
- package/lib/plugin.js +5 -0
- package/lib/services/acquire-retrieve-selection.d.ts +43 -0
- package/lib/services/acquire-retrieve-selection.js +145 -0
- package/lib/services/bind-identity-resolution-dependencies.d.ts +53 -0
- package/lib/services/bind-identity-resolution-dependencies.js +238 -0
- package/lib/services/bind-segment-dependencies.d.ts +49 -0
- package/lib/services/bind-segment-dependencies.js +115 -0
- package/lib/services/bounded-reads.d.ts +17 -0
- package/lib/services/bounded-reads.js +42 -0
- package/lib/services/calculated-insight-sql-checks.d.ts +33 -0
- package/lib/services/calculated-insight-sql-checks.js +193 -0
- package/lib/services/command-dlo.d.ts +48 -0
- package/lib/services/command-dlo.js +103 -0
- package/lib/services/command-native-data-graph.d.ts +54 -0
- package/lib/services/command-native-data-graph.js +485 -0
- package/lib/services/command-native-data-stream.d.ts +104 -0
- package/lib/services/command-native-data-stream.js +257 -0
- package/lib/services/command-native-dmo.d.ts +124 -0
- package/lib/services/command-native-dmo.js +338 -0
- package/lib/services/command-native-mapping.d.ts +65 -0
- package/lib/services/command-native-mapping.js +129 -0
- package/lib/services/command-native-relationship.d.ts +46 -0
- package/lib/services/command-native-relationship.js +103 -0
- package/lib/services/cross-dataspace-deploy.d.ts +111 -0
- package/lib/services/cross-dataspace-deploy.js +493 -0
- package/lib/services/cross-dataspace-rename.d.ts +74 -0
- package/lib/services/cross-dataspace-rename.js +1314 -0
- package/lib/services/cross-dataspace-sql.d.ts +109 -0
- package/lib/services/cross-dataspace-sql.js +1301 -0
- package/lib/services/cross-space-context.d.ts +23 -0
- package/lib/services/cross-space-context.js +36 -0
- package/lib/services/crud-plan.d.ts +137 -0
- package/lib/services/crud-plan.js +408 -0
- package/lib/services/data-space-check.d.ts +89 -0
- package/lib/services/data-space-check.js +162 -0
- package/lib/services/data-space-prefix.d.ts +77 -0
- package/lib/services/data-space-prefix.js +127 -0
- package/lib/services/data-transform-space.d.ts +31 -0
- package/lib/services/data-transform-space.js +79 -0
- package/lib/services/delete-calculated-insight.d.ts +44 -0
- package/lib/services/delete-calculated-insight.js +101 -0
- package/lib/services/delete-data-transform.d.ts +52 -0
- package/lib/services/delete-data-transform.js +222 -0
- package/lib/services/delete-dmo-field.d.ts +211 -0
- package/lib/services/delete-dmo-field.js +472 -0
- package/lib/services/delete-identity-resolution.d.ts +43 -0
- package/lib/services/delete-identity-resolution.js +295 -0
- package/lib/services/delete-segment.d.ts +42 -0
- package/lib/services/delete-segment.js +248 -0
- package/lib/services/deploy-result.d.ts +54 -0
- package/lib/services/deploy-result.js +60 -0
- package/lib/services/escaped-copy.d.ts +23 -0
- package/lib/services/escaped-copy.js +88 -0
- package/lib/services/execute-calculated-insight-deploy.d.ts +42 -0
- package/lib/services/execute-calculated-insight-deploy.js +96 -0
- package/lib/services/execute-data-transform-deploy.d.ts +56 -0
- package/lib/services/execute-data-transform-deploy.js +168 -0
- package/lib/services/execute-native-identity-resolution-deploy.d.ts +48 -0
- package/lib/services/execute-native-identity-resolution-deploy.js +360 -0
- package/lib/services/execute-segment-deploy.d.ts +45 -0
- package/lib/services/execute-segment-deploy.js +393 -0
- package/lib/services/local-file-removal.d.ts +36 -0
- package/lib/services/local-file-removal.js +103 -0
- package/lib/services/native-calculated-insight.d.ts +87 -0
- package/lib/services/native-calculated-insight.js +164 -0
- package/lib/services/native-crm-data-stream.d.ts +91 -0
- package/lib/services/native-crm-data-stream.js +272 -0
- package/lib/services/native-data-graph-context.d.ts +12 -0
- package/lib/services/native-data-graph-context.js +139 -0
- package/lib/services/native-data-graph-crd.d.ts +261 -0
- package/lib/services/native-data-graph-crd.js +866 -0
- package/lib/services/native-data-stream.d.ts +329 -0
- package/lib/services/native-data-stream.js +1830 -0
- package/lib/services/native-dlo.d.ts +163 -0
- package/lib/services/native-dlo.js +402 -0
- package/lib/services/native-dmo.d.ts +191 -0
- package/lib/services/native-dmo.js +614 -0
- package/lib/services/native-mapping.d.ts +255 -0
- package/lib/services/native-mapping.js +1226 -0
- package/lib/services/native-relationship.d.ts +132 -0
- package/lib/services/native-relationship.js +512 -0
- package/lib/services/operation-diagnostic.d.ts +83 -0
- package/lib/services/operation-diagnostic.js +272 -0
- package/lib/services/preflight-calculated-insight-deploy.d.ts +58 -0
- package/lib/services/preflight-calculated-insight-deploy.js +95 -0
- package/lib/services/preflight-data-transform-deploy.d.ts +33 -0
- package/lib/services/preflight-data-transform-deploy.js +104 -0
- package/lib/services/preflight-identity-resolution-deploy.d.ts +107 -0
- package/lib/services/preflight-identity-resolution-deploy.js +281 -0
- package/lib/services/preflight-segment-deploy.d.ts +31 -0
- package/lib/services/preflight-segment-deploy.js +109 -0
- package/lib/services/publish-calculated-insight.d.ts +40 -0
- package/lib/services/publish-calculated-insight.js +95 -0
- package/lib/services/publish-segment.d.ts +65 -0
- package/lib/services/publish-segment.js +194 -0
- package/lib/services/readback-pace.d.ts +16 -0
- package/lib/services/readback-pace.js +23 -0
- package/lib/services/retrieve-data-transform.d.ts +26 -0
- package/lib/services/retrieve-data-transform.js +65 -0
- package/lib/services/retrieve-identity-resolution.d.ts +40 -0
- package/lib/services/retrieve-identity-resolution.js +87 -0
- package/lib/services/retrieve-inventory.d.ts +32 -0
- package/lib/services/retrieve-inventory.js +151 -0
- package/lib/services/retrieve-segment.d.ts +120 -0
- package/lib/services/retrieve-segment.js +502 -0
- package/lib/services/segment-lookup.d.ts +46 -0
- package/lib/services/segment-lookup.js +108 -0
- package/lib/services/segment-members.d.ts +144 -0
- package/lib/services/segment-members.js +402 -0
- package/lib/services/select-native-retrieve.d.ts +60 -0
- package/lib/services/select-native-retrieve.js +226 -0
- package/lib/services/settle-calculated-insight.d.ts +22 -0
- package/lib/services/settle-calculated-insight.js +53 -0
- package/lib/services/sql-tokens.d.ts +58 -0
- package/lib/services/sql-tokens.js +147 -0
- package/lib/transport/api-version.d.ts +36 -0
- package/lib/transport/api-version.js +89 -0
- package/lib/transport/bound-ssot.d.ts +27 -0
- package/lib/transport/bound-ssot.js +116 -0
- package/lib/transport/calculated-insight-rest.d.ts +28 -0
- package/lib/transport/calculated-insight-rest.js +62 -0
- package/lib/transport/connect-client.d.ts +129 -0
- package/lib/transport/connect-client.js +446 -0
- package/lib/transport/connect-diagnostic.d.ts +89 -0
- package/lib/transport/connect-diagnostic.js +331 -0
- package/lib/transport/data-graph-rest.d.ts +27 -0
- package/lib/transport/data-graph-rest.js +76 -0
- package/lib/transport/data-stream-rest.d.ts +62 -0
- package/lib/transport/data-stream-rest.js +348 -0
- package/lib/transport/data-transform-rest.d.ts +25 -0
- package/lib/transport/data-transform-rest.js +100 -0
- package/lib/transport/dlo-native-rest.d.ts +22 -0
- package/lib/transport/dlo-native-rest.js +89 -0
- package/lib/transport/dlo-rest.d.ts +23 -0
- package/lib/transport/dlo-rest.js +135 -0
- package/lib/transport/dmo-native-rest.d.ts +18 -0
- package/lib/transport/dmo-native-rest.js +55 -0
- package/lib/transport/dmo-rest.d.ts +30 -0
- package/lib/transport/dmo-rest.js +163 -0
- package/lib/transport/mapping-rest.d.ts +63 -0
- package/lib/transport/mapping-rest.js +341 -0
- package/lib/transport/project-metadata.d.ts +36 -0
- package/lib/transport/project-metadata.js +118 -0
- package/lib/transport/relationship-rest.d.ts +43 -0
- package/lib/transport/relationship-rest.js +201 -0
- package/package.json +96 -4
|
@@ -0,0 +1,1830 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
+
import { refuseEscapedCopy } from './escaped-copy.js';
|
|
3
|
+
import { explainInput, explainMismatch, printableKey } from './operation-diagnostic.js';
|
|
4
|
+
import { setTimeout as wait } from 'node:timers/promises';
|
|
5
|
+
import { DataStreamError, buildDataStreamCreateBody, buildDataStreamPatchBody, createSpacesRefusal, validateDataStreamDocument, } from '../artifacts/data-stream-document.js';
|
|
6
|
+
import { dataSpaceKey, isDataSpaceName, sortedDataSpaces } from '../artifacts/data-space-field.js';
|
|
7
|
+
import { ConnectError } from '../transport/connect-client.js';
|
|
8
|
+
import { DataStreamHttpError } from '../transport/data-stream-rest.js';
|
|
9
|
+
import { verbatimReason } from '../artifacts/verbatim-document.js';
|
|
10
|
+
import { sameEnum, sameRecord } from '../artifacts/enum-case.js';
|
|
11
|
+
const name = /^[A-Za-z][A-Za-z0-9_]{0,199}$/u;
|
|
12
|
+
const recordId = /^[A-Za-z0-9]{18}$/u;
|
|
13
|
+
/** The primary-key field type is stored in Salesforce's letter case; a case-only difference is no change. */
|
|
14
|
+
const dloTypeKey = new Set(['dataType']);
|
|
15
|
+
const connectionPageSize = 200;
|
|
16
|
+
/**
|
|
17
|
+
* Deploy-mode connection listing: the prior bare list or the observed complete first-page
|
|
18
|
+
* envelope only. No continuation contract is admitted, and a full page cannot prove exhaustion.
|
|
19
|
+
* @param {Json} listed Connection list response.
|
|
20
|
+
* @returns {Json[]} Listed connections.
|
|
21
|
+
*/
|
|
22
|
+
function strictConnections(listed) {
|
|
23
|
+
const keys = Object.keys(listed);
|
|
24
|
+
const bare = keys.length === 1 && keys[0] === 'connections';
|
|
25
|
+
const completePage = keys.length === 3 &&
|
|
26
|
+
keys.every((key) => ['connections', 'currentPageUrl', 'totalSize'].includes(key)) &&
|
|
27
|
+
listed.currentPageUrl ===
|
|
28
|
+
'/services/data/v68.0/ssot/connections?connectorType=IngestApi&limit=200&offset=0' &&
|
|
29
|
+
Number.isSafeInteger(listed.totalSize) &&
|
|
30
|
+
listed.totalSize === (Array.isArray(listed.connections) ? listed.connections.length : -1);
|
|
31
|
+
if (!Array.isArray(listed.connections) ||
|
|
32
|
+
listed.connections.length >= 200 ||
|
|
33
|
+
(!bare && !completePage))
|
|
34
|
+
throw new DataStreamError('INCOMPLETE_CONNECTION_LIST');
|
|
35
|
+
return objects(listed.connections);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Every listed entry as an object.
|
|
39
|
+
* @param {unknown[]} values Listed entries.
|
|
40
|
+
* @returns {Json[]} The entries; a non-object entry refuses the read.
|
|
41
|
+
*/
|
|
42
|
+
function objects(values) {
|
|
43
|
+
return values.map((value) => object(value));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Retrieve-mode connection listing: every page of the IngestApi connection list, following its
|
|
47
|
+
* offset until `totalSize`, otherwise while a page is full or names a next page. Extra envelope
|
|
48
|
+
* keys and the page URL format are ignored; a page that adds no new connection ends the listing.
|
|
49
|
+
* @param {DataStreamReadClients} clients Read-only dependency boundaries.
|
|
50
|
+
* @returns {Promise<unknown[]>} Every listed connection entry, once each.
|
|
51
|
+
*/
|
|
52
|
+
async function listConnectionsLeniently(clients) {
|
|
53
|
+
const connections = [];
|
|
54
|
+
const seen = new Set();
|
|
55
|
+
for (let offset = 0;; offset += connectionPageSize) {
|
|
56
|
+
const page = await clients.connections.list(offset);
|
|
57
|
+
if (!Array.isArray(page.connections))
|
|
58
|
+
throw new DataStreamError('INCOMPLETE_CONNECTION_LIST');
|
|
59
|
+
let added = 0;
|
|
60
|
+
for (const value of page.connections) {
|
|
61
|
+
const id = value && typeof value === 'object' && typeof value.id === 'string'
|
|
62
|
+
? value.id
|
|
63
|
+
: JSON.stringify(value);
|
|
64
|
+
if (seen.has(id))
|
|
65
|
+
continue;
|
|
66
|
+
seen.add(id);
|
|
67
|
+
connections.push(value);
|
|
68
|
+
added++;
|
|
69
|
+
}
|
|
70
|
+
const total = Number.isSafeInteger(page.totalSize) ? page.totalSize : undefined;
|
|
71
|
+
const more = added > 0 &&
|
|
72
|
+
(total === undefined
|
|
73
|
+
? page.connections.length >= connectionPageSize ||
|
|
74
|
+
(typeof page.nextPageUrl === 'string' && page.nextPageUrl !== '')
|
|
75
|
+
: offset + connectionPageSize < total);
|
|
76
|
+
if (!more)
|
|
77
|
+
return connections;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Compare exact mapping multisets, consuming each match so duplicates cannot hide drift.
|
|
82
|
+
* @param {unknown} left First native mapping inventory.
|
|
83
|
+
* @param {unknown} right Independent native mapping inventory.
|
|
84
|
+
* @returns {boolean} Whether only entry ordering differs.
|
|
85
|
+
*/
|
|
86
|
+
function sameStreamMappings(left, right) {
|
|
87
|
+
if (!Array.isArray(left) || !Array.isArray(right))
|
|
88
|
+
return isDeepStrictEqual(left, right);
|
|
89
|
+
if (left.length !== right.length)
|
|
90
|
+
return false;
|
|
91
|
+
const remaining = [...right];
|
|
92
|
+
for (const entry of left) {
|
|
93
|
+
const index = remaining.findIndex((candidate) => isDeepStrictEqual(entry, candidate));
|
|
94
|
+
if (index === -1)
|
|
95
|
+
return false;
|
|
96
|
+
remaining.splice(index, 1);
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Order a native mapping inventory by target field, then source field, then formula.
|
|
102
|
+
* Comparisons stay order-insensitive (sameStreamMappings); this only makes retrieved bytes stable.
|
|
103
|
+
* @param {unknown[]} mappings Native mapping inventory.
|
|
104
|
+
* @returns {unknown[]} Sorted copy.
|
|
105
|
+
*/
|
|
106
|
+
export function sortedStreamMappings(mappings) {
|
|
107
|
+
// eslint-disable-next-line unicorn/no-array-sort -- Sort a copied array within the existing ES2022 library target.
|
|
108
|
+
return [...mappings].sort((left, right) => {
|
|
109
|
+
const a = streamMappingKey(left);
|
|
110
|
+
const b = streamMappingKey(right);
|
|
111
|
+
for (let index = 0; index < a.length; index++)
|
|
112
|
+
if (a[index] !== b[index])
|
|
113
|
+
return a[index] < b[index] ? -1 : 1;
|
|
114
|
+
return 0;
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The sort key of one native stream mapping.
|
|
119
|
+
* @param {unknown} value Native mapping entry.
|
|
120
|
+
* @returns {string[]} Target field, source field and formula; absent parts are empty.
|
|
121
|
+
*/
|
|
122
|
+
function streamMappingKey(value) {
|
|
123
|
+
const entry = (value && typeof value === 'object' ? value : {});
|
|
124
|
+
return [entry.targetFieldName, entry.sourceFieldName, entry.formula].map((part) => typeof part === 'string' ? part : '');
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Order locally derived mapping entries by target field.
|
|
128
|
+
* @param {T[]} entries Derived entries.
|
|
129
|
+
* @returns {T[]} The same array, sorted.
|
|
130
|
+
*/
|
|
131
|
+
function byTarget(entries) {
|
|
132
|
+
// eslint-disable-next-line unicorn/no-array-sort -- Sort a locally derived array within the existing ES2022 library target.
|
|
133
|
+
return entries.sort((a, b) => a.targetFieldName < b.targetFieldName ? -1 : a.targetFieldName > b.targetFieldName ? 1 : 0);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Compare snapshots allowing only observed native mapping inventory permutations.
|
|
137
|
+
* All other properties and arrays, including unknown fields, remain strictly compared.
|
|
138
|
+
* @param {unknown} left Native stream or lifecycle plan snapshot.
|
|
139
|
+
* @param {unknown} right Independent snapshot with the same shape.
|
|
140
|
+
* @param {boolean} postWrite Whether isolated stream lifecycle differences may await settlement.
|
|
141
|
+
* @returns {boolean} Whether protected state is unchanged.
|
|
142
|
+
*/
|
|
143
|
+
function sameStreamSnapshot(left, right, postWrite = false) {
|
|
144
|
+
const first = structuredClone(left);
|
|
145
|
+
const second = structuredClone(right);
|
|
146
|
+
// Explicit native/portable observation slots only; never normalize authored mappings.
|
|
147
|
+
const paths = [
|
|
148
|
+
['mappings'],
|
|
149
|
+
['observed', 'mappings'],
|
|
150
|
+
['binding', 'target', 'mappings'],
|
|
151
|
+
['binding', 'inventory', '0', 'mappings'],
|
|
152
|
+
['target', 'observed', 'mappings'],
|
|
153
|
+
['document', 'observed', 'mappings'],
|
|
154
|
+
['patch', 'observed', 'mappings'],
|
|
155
|
+
];
|
|
156
|
+
for (const path of paths) {
|
|
157
|
+
const key = path.at(-1);
|
|
158
|
+
const parent = (value) => {
|
|
159
|
+
let current = value;
|
|
160
|
+
for (const part of path.slice(0, -1)) {
|
|
161
|
+
if (!current || typeof current !== 'object')
|
|
162
|
+
return undefined;
|
|
163
|
+
current = current[part];
|
|
164
|
+
}
|
|
165
|
+
return current && typeof current === 'object' ? current : undefined;
|
|
166
|
+
};
|
|
167
|
+
const a = parent(first);
|
|
168
|
+
const b = parent(second);
|
|
169
|
+
if (a && b && Array.isArray(a[key]) && Array.isArray(b[key])) {
|
|
170
|
+
if (!sameStreamMappings(a[key], b[key]))
|
|
171
|
+
return false;
|
|
172
|
+
b[key] = a[key];
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (isDeepStrictEqual(first, second))
|
|
176
|
+
return true;
|
|
177
|
+
// Only independent post-write reads may defer an isolated stream lifecycle transition.
|
|
178
|
+
if (postWrite && first && second && typeof first === 'object' && typeof second === 'object') {
|
|
179
|
+
const leftStream = first;
|
|
180
|
+
const rightStream = second;
|
|
181
|
+
if (leftStream.status !== rightStream.status &&
|
|
182
|
+
['ACTIVE', 'PROCESSING'].includes(leftStream.status) &&
|
|
183
|
+
['ACTIVE', 'PROCESSING'].includes(rightStream.status) &&
|
|
184
|
+
isDeepStrictEqual(first, { ...rightStream, status: leftStream.status }))
|
|
185
|
+
throw new DataStreamError('STREAM_SNAPSHOT_PENDING');
|
|
186
|
+
}
|
|
187
|
+
// Only the observed linked-DLO lifecycle transition can be pending, never configuration drift.
|
|
188
|
+
const a = first && typeof first === 'object' ? first.dataLakeObjectInfo : undefined;
|
|
189
|
+
const b = second && typeof second === 'object' ? second.dataLakeObjectInfo : undefined;
|
|
190
|
+
if (a && b && typeof a === 'object' && typeof b === 'object') {
|
|
191
|
+
const leftInfo = a;
|
|
192
|
+
const rightInfo = b;
|
|
193
|
+
if (leftInfo.status !== rightInfo.status &&
|
|
194
|
+
['ACTIVE', 'PROCESSING'].includes(leftInfo.status) &&
|
|
195
|
+
['ACTIVE', 'PROCESSING'].includes(rightInfo.status)) {
|
|
196
|
+
rightInfo.status = leftInfo.status;
|
|
197
|
+
if (isDeepStrictEqual(first, second))
|
|
198
|
+
throw new DataStreamError('STREAM_SNAPSHOT_PENDING');
|
|
199
|
+
// After a write the stream and its DLO can move between PROCESSING and ACTIVE together.
|
|
200
|
+
const leftStream = first;
|
|
201
|
+
const rightStream = second;
|
|
202
|
+
if (postWrite &&
|
|
203
|
+
leftStream.status !== rightStream.status &&
|
|
204
|
+
['ACTIVE', 'PROCESSING'].includes(leftStream.status) &&
|
|
205
|
+
['ACTIVE', 'PROCESSING'].includes(rightStream.status) &&
|
|
206
|
+
isDeepStrictEqual(first, { ...rightStream, status: leftStream.status }))
|
|
207
|
+
throw new DataStreamError('STREAM_SNAPSHOT_PENDING');
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Require an object without interpreting unknown response fields as authored intent.
|
|
214
|
+
* @param {unknown} value Raw response.
|
|
215
|
+
* @returns {Json} Checked object.
|
|
216
|
+
*/
|
|
217
|
+
function object(value) {
|
|
218
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
219
|
+
throw new DataStreamError('INVALID_STREAM_READ');
|
|
220
|
+
return value;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Validate the independently observed supported stream scope, not runtime settlement.
|
|
224
|
+
* @param {unknown} value Native stream detail/inventory entry.
|
|
225
|
+
* @returns {Json} Raw detached response preserving all inventory fields.
|
|
226
|
+
*/
|
|
227
|
+
export function inspectNativeDataStream(value) {
|
|
228
|
+
const raw = object(value);
|
|
229
|
+
const dlo = object(raw.dataLakeObjectInfo);
|
|
230
|
+
const spaces = dlo.dataSpaceInfo;
|
|
231
|
+
if (typeof raw.name !== 'string' ||
|
|
232
|
+
!name.test(raw.name) ||
|
|
233
|
+
typeof raw.recordId !== 'string' ||
|
|
234
|
+
!recordId.test(raw.recordId) ||
|
|
235
|
+
typeof raw.dataSource !== 'string' ||
|
|
236
|
+
!name.test(raw.dataSource) ||
|
|
237
|
+
raw.dataStreamType !== 'INGESTAPI' ||
|
|
238
|
+
object(raw.connectorInfo).connectorType !== 'IngestApi' ||
|
|
239
|
+
typeof object(raw.refreshConfig).refreshMode !== 'string' ||
|
|
240
|
+
!object(raw.refreshConfig).refreshMode ||
|
|
241
|
+
(dlo.category !== undefined && (typeof dlo.category !== 'string' || !dlo.category)) ||
|
|
242
|
+
typeof dlo.name !== 'string' ||
|
|
243
|
+
!/^[A-Za-z][A-Za-z0-9_]*__dll$/u.test(dlo.name) ||
|
|
244
|
+
!Array.isArray(spaces) ||
|
|
245
|
+
spaces.length === 0 ||
|
|
246
|
+
!spaces.every((entry) => isDataSpaceName(object(entry).name)) ||
|
|
247
|
+
typeof raw.status !== 'string' ||
|
|
248
|
+
typeof raw.label !== 'string' ||
|
|
249
|
+
!raw.label ||
|
|
250
|
+
!Array.isArray(raw.sourceFields) ||
|
|
251
|
+
!Array.isArray(raw.mappings))
|
|
252
|
+
throw new DataStreamError('UNSUPPORTED_STREAM_READ');
|
|
253
|
+
return structuredClone(raw);
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* The item checks of a retrieved stream GET: a stream of another name, or one whose DLO does not
|
|
257
|
+
* belong to the run's data space (selection is per data space), stays an item failure.
|
|
258
|
+
* @param {unknown} value Stream GET response.
|
|
259
|
+
* @param {string} apiName Requested stream name.
|
|
260
|
+
* @param {string} dataSpace The run's data space.
|
|
261
|
+
* @returns {Json} The GET.
|
|
262
|
+
*/
|
|
263
|
+
export function checkRetrievedStreamScope(value, apiName, dataSpace) {
|
|
264
|
+
const raw = object(value);
|
|
265
|
+
if (raw.name !== apiName)
|
|
266
|
+
throw new DataStreamError('STREAM_TARGET_MISMATCH');
|
|
267
|
+
const bag = (entry) => entry && typeof entry === 'object' && !Array.isArray(entry) ? entry : {};
|
|
268
|
+
const dlo = bag(raw.dataLakeObjectInfo);
|
|
269
|
+
const spaces = Array.isArray(dlo.dataSpaceInfo)
|
|
270
|
+
? dlo.dataSpaceInfo.map((entry) => bag(entry).name)
|
|
271
|
+
: [];
|
|
272
|
+
if (spaces.length > 0 && !spaces.includes(dataSpace))
|
|
273
|
+
throw outsideRunSpace(String(raw.name), dataSpace);
|
|
274
|
+
return raw;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* The refusal of a stream whose landing DLO does not belong to the run's data space.
|
|
278
|
+
* @param {string} apiName Stream name.
|
|
279
|
+
* @param {string} dataSpace The run's data space.
|
|
280
|
+
* @returns {DataStreamError} UNSUPPORTED_STREAM_DATASPACE with an explanation.
|
|
281
|
+
*/
|
|
282
|
+
function outsideRunSpace(apiName, dataSpace) {
|
|
283
|
+
return explainInput(new DataStreamError('UNSUPPORTED_STREAM_DATASPACE'), `The landing DLO of ${printableKey(apiName)} does not belong to the data space ${printableKey(dataSpace)}. Run the command with --dataspace set to one of its DLO's data spaces.`);
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* The data spaces of a native DLO read, sorted; an entry without a usable name, or a name listed
|
|
287
|
+
* twice, is INVALID_STREAM_DLO.
|
|
288
|
+
* @param {Json} dlo Native DLO object.
|
|
289
|
+
* @returns {string[]} Its data spaces.
|
|
290
|
+
*/
|
|
291
|
+
function streamDloSpaces(dlo) {
|
|
292
|
+
const names = Array.isArray(dlo.dataSpaceInfo)
|
|
293
|
+
? dlo.dataSpaceInfo.map((entry) => bag(entry).name)
|
|
294
|
+
: [];
|
|
295
|
+
if (names.length === 0 ||
|
|
296
|
+
!names.every((name) => isDataSpaceName(name)) ||
|
|
297
|
+
new Set(names).size !== names.length)
|
|
298
|
+
throw new DataStreamError('INVALID_STREAM_DLO');
|
|
299
|
+
return sortedDataSpaces(names);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* The data spaces of a stream's landing DLO, sorted: the names the stream GET lists, or, when it
|
|
303
|
+
* names none, the DLO's own `dataSpaceInfo` read by name. A GET that names no space is never taken
|
|
304
|
+
* as the run's. The run's data space must be among them (UNSUPPORTED_STREAM_DATASPACE otherwise).
|
|
305
|
+
* @param {unknown} value Stream GET response.
|
|
306
|
+
* @param {string} apiName Stream name.
|
|
307
|
+
* @param {string} dataSpace The run's data space.
|
|
308
|
+
* @param {Pick<DataStreamReadClients, 'dlo'>} [clients] DLO reader, needed when the GET names no
|
|
309
|
+
* data space (INVALID_STREAM_DLO without one).
|
|
310
|
+
* @returns {Promise<string[]>} The data spaces.
|
|
311
|
+
*/
|
|
312
|
+
export async function streamSpaces(value, apiName, dataSpace, clients) {
|
|
313
|
+
const info = bag(bag(value).dataLakeObjectInfo);
|
|
314
|
+
const listed = Array.isArray(info.dataSpaceInfo)
|
|
315
|
+
? info.dataSpaceInfo.map((entry) => bag(entry).name)
|
|
316
|
+
: [];
|
|
317
|
+
let spaces;
|
|
318
|
+
if (listed.length > 0) {
|
|
319
|
+
if (!listed.every((name) => isDataSpaceName(name)))
|
|
320
|
+
throw new DataStreamError('UNSUPPORTED_STREAM_READ');
|
|
321
|
+
spaces = sortedDataSpaces(listed);
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
if (!clients?.dlo ||
|
|
325
|
+
typeof info.name !== 'string' ||
|
|
326
|
+
!/^[A-Za-z][A-Za-z0-9_]*__dll$/u.test(info.name))
|
|
327
|
+
throw new DataStreamError('INVALID_STREAM_DLO');
|
|
328
|
+
const wrapper = object(await clients.dlo.get(info.name));
|
|
329
|
+
if (Array.isArray(wrapper.dataLakeObjects) && wrapper.dataLakeObjects.length !== 1)
|
|
330
|
+
throw new DataStreamError('INVALID_STREAM_DLO');
|
|
331
|
+
spaces = streamDloSpaces(Array.isArray(wrapper.dataLakeObjects) ? object(wrapper.dataLakeObjects[0]) : wrapper);
|
|
332
|
+
}
|
|
333
|
+
if (!spaces.includes(dataSpace))
|
|
334
|
+
throw outsideRunSpace(apiName, dataSpace);
|
|
335
|
+
return spaces;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Whether two data space lists name the same spaces.
|
|
339
|
+
* @param {readonly string[]} left One list.
|
|
340
|
+
* @param {readonly string[]} right The other list.
|
|
341
|
+
* @returns {boolean} Same spaces.
|
|
342
|
+
*/
|
|
343
|
+
export function sameSpaces(left, right) {
|
|
344
|
+
return isDeepStrictEqual(sortedDataSpaces(left), sortedDataSpaces(right));
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* The refusal of a stream file whose c__dataSpace list differs from its DLO's data spaces.
|
|
348
|
+
* @param {string} apiName Stream name.
|
|
349
|
+
* @param {readonly string[]} file The file's list.
|
|
350
|
+
* @param {readonly string[]} org The DLO's data spaces.
|
|
351
|
+
* @returns {DataStreamError} STREAM_DATASPACE_MISMATCH with an explanation.
|
|
352
|
+
*/
|
|
353
|
+
export function spacesDiffer(apiName, file, org) {
|
|
354
|
+
return explainInput(new DataStreamError('STREAM_DATASPACE_MISMATCH'), `The ${dataSpaceKey} list of ${printableKey(apiName)} (${file.map((space) => printableKey(space)).join(', ')}) differs from the data spaces of its DLO (${org.map((space) => printableKey(space)).join(', ')}). A stream file only mirrors its DLO: change the spaces with the DLO file, or retrieve the stream again.`);
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* An object, or an empty one for anything else.
|
|
358
|
+
* @param {unknown} entry Candidate.
|
|
359
|
+
* @returns {Json} The object or {}.
|
|
360
|
+
*/
|
|
361
|
+
function bag(entry) {
|
|
362
|
+
return entry && typeof entry === 'object' && !Array.isArray(entry) ? entry : {};
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Field names with their types, sorted, as one comparable string.
|
|
366
|
+
* @param {unknown} values Field list.
|
|
367
|
+
* @param {string} typeKey Key of the type (`dataType` in a schema, `datatype` in a stream GET).
|
|
368
|
+
* @returns {string} Comparable text.
|
|
369
|
+
*/
|
|
370
|
+
function fieldsOf(values, typeKey) {
|
|
371
|
+
return JSON.stringify((Array.isArray(values) ? values : [])
|
|
372
|
+
.map((value) => {
|
|
373
|
+
const field = object(value);
|
|
374
|
+
return `${String(field.name)}:${String(field[typeKey])}`;
|
|
375
|
+
})
|
|
376
|
+
// eslint-disable-next-line unicorn/no-array-sort -- Sort a fresh array within the existing ES2022 library target.
|
|
377
|
+
.sort());
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Retrieve-mode triage of a stream GET, before any connection or schema read: a stream of another
|
|
381
|
+
* name stays an item failure, as does one whose DLO is in another data space (selection is per
|
|
382
|
+
* data space). Streams of other connectors never reach this projection (they use the GET-shaped
|
|
383
|
+
* file); a GET that names no connector type, or the Ingestion API connector with another stream
|
|
384
|
+
* type, fits neither file shape and is refused, so retrieve writes the GET as-is.
|
|
385
|
+
* @param {unknown} value Stream GET response.
|
|
386
|
+
* @param {string} apiName Requested stream name.
|
|
387
|
+
* @param {string} dataSpace The run's data space.
|
|
388
|
+
*/
|
|
389
|
+
function triageRetrievedStream(value, apiName, dataSpace) {
|
|
390
|
+
const raw = checkRetrievedStreamScope(value, apiName, dataSpace);
|
|
391
|
+
if (raw.dataStreamType !== 'INGESTAPI' || bag(raw.connectorInfo).connectorType !== 'IngestApi')
|
|
392
|
+
throw new DataStreamError('UNSUPPORTED_STREAM_CONNECTOR');
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* The event of a connection a stream reads. The stream GET does not name it; Salesforce names an
|
|
396
|
+
* Ingestion API stream `<connection>_<event>_<hash>` and labels it `<connection>-<event>`. A
|
|
397
|
+
* connection with one event needs no match. When several events fit the name or label, the one
|
|
398
|
+
* whose fields equal the stream's source fields is taken.
|
|
399
|
+
* @param {Json} stream Stream GET.
|
|
400
|
+
* @param {unknown[]} events Event schemas of the stream's connection.
|
|
401
|
+
* @returns {string} Event name.
|
|
402
|
+
*/
|
|
403
|
+
export function streamEvent(stream, events) {
|
|
404
|
+
const schemas = events.map((value) => object(value));
|
|
405
|
+
if (schemas.length === 1 && typeof schemas[0].name === 'string')
|
|
406
|
+
return schemas[0].name;
|
|
407
|
+
const fits = (event) => (typeof stream.name === 'string' &&
|
|
408
|
+
new RegExp(`_${event}_[0-9A-Fa-f]{8}$`, 'u').test(stream.name)) ||
|
|
409
|
+
(typeof stream.label === 'string' && stream.label.endsWith(`-${event}`));
|
|
410
|
+
let candidates = schemas.filter((schema) => typeof schema.name === 'string' && name.test(schema.name) && fits(schema.name));
|
|
411
|
+
if (candidates.length > 1)
|
|
412
|
+
candidates = candidates.filter((schema) => fieldsOf(schema.fields, 'dataType') === fieldsOf(stream.sourceFields, 'datatype'));
|
|
413
|
+
if (candidates.length !== 1)
|
|
414
|
+
throw new DataStreamError('AMBIGUOUS_STREAM_EVENT');
|
|
415
|
+
return candidates[0].name;
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Resolve an exact portable connection/event independently from generated stream names.
|
|
419
|
+
* A missing source name alone never authorizes creation; this read result authorizes no write.
|
|
420
|
+
* @param {object} selection Portable requested name, connection/event and Text primary key.
|
|
421
|
+
* @param {string} selection.apiName Portable stream name.
|
|
422
|
+
* @param {string} selection.connectionName Connection developer name.
|
|
423
|
+
* @param {string} selection.event Uploaded event name.
|
|
424
|
+
* @param {string} selection.primaryKey Explicit Text primary key.
|
|
425
|
+
* @param {DataStreamReadClients} clients Read-only dependency boundaries.
|
|
426
|
+
* @param {boolean} postWrite Whether this read belongs to bounded create/update settlement.
|
|
427
|
+
* @param {DataStreamReadMode} mode `retrieve` lists connections leniently (every page, extra
|
|
428
|
+
* envelope keys ignored); `deploy` keeps the strict single-page envelope check.
|
|
429
|
+
* @returns {Promise<DataStreamBinding>} Private target-local binding and complete inventory.
|
|
430
|
+
*/
|
|
431
|
+
export async function resolveDataStreamBinding(selection, clients, postWrite = false, mode = 'deploy') {
|
|
432
|
+
if (![selection.apiName, selection.connectionName, selection.event, selection.primaryKey].every((value) => name.test(value)))
|
|
433
|
+
throw new DataStreamError('INVALID_STREAM_BINDING');
|
|
434
|
+
const connections = mode === 'retrieve'
|
|
435
|
+
? objects(await listConnectionsLeniently(clients))
|
|
436
|
+
: strictConnections(await clients.connections.list());
|
|
437
|
+
const seen = new Set();
|
|
438
|
+
const ids = new Set();
|
|
439
|
+
for (const connection of connections) {
|
|
440
|
+
if (typeof connection.name !== 'string' ||
|
|
441
|
+
!name.test(connection.name) ||
|
|
442
|
+
typeof connection.id !== 'string' ||
|
|
443
|
+
!recordId.test(connection.id) ||
|
|
444
|
+
connection.connectorType !== 'IngestApi' ||
|
|
445
|
+
seen.has(connection.name.toLowerCase()) ||
|
|
446
|
+
ids.has(connection.id))
|
|
447
|
+
throw new DataStreamError('AMBIGUOUS_STREAM_CONNECTION');
|
|
448
|
+
seen.add(connection.name.toLowerCase());
|
|
449
|
+
ids.add(connection.id);
|
|
450
|
+
}
|
|
451
|
+
const selected = connections.filter((connection) => connection.name === selection.connectionName);
|
|
452
|
+
if (selected.length !== 1)
|
|
453
|
+
throw new DataStreamError('AMBIGUOUS_STREAM_CONNECTION');
|
|
454
|
+
const connectionId = selected[0].id;
|
|
455
|
+
const detail = await clients.connections.get(connectionId);
|
|
456
|
+
if (detail.id !== connectionId ||
|
|
457
|
+
detail.name !== selection.connectionName ||
|
|
458
|
+
detail.connectorType !== 'IngestApi' ||
|
|
459
|
+
detail.status !== 'CONNECTED')
|
|
460
|
+
throw new DataStreamError('STREAM_CONNECTION_MISMATCH');
|
|
461
|
+
const schemas = await clients.connections.schema(connectionId);
|
|
462
|
+
// The connection may hold several events, one stream each; the selected event is named.
|
|
463
|
+
if (!Array.isArray(schemas.schemas) || schemas.schemas.length === 0)
|
|
464
|
+
throw new DataStreamError('AMBIGUOUS_STREAM_EVENT');
|
|
465
|
+
const events = schemas.schemas;
|
|
466
|
+
const named = events
|
|
467
|
+
.map((value) => object(value))
|
|
468
|
+
.filter((value) => value.name === selection.event);
|
|
469
|
+
if (named.length > 1)
|
|
470
|
+
throw new DataStreamError('AMBIGUOUS_STREAM_EVENT');
|
|
471
|
+
const event = named[0] ?? {};
|
|
472
|
+
if (event.name !== selection.event ||
|
|
473
|
+
event.schemaType !== 'IngestApi' ||
|
|
474
|
+
!['Available', 'In Use'].includes(event.availabilityStatus) ||
|
|
475
|
+
!Array.isArray(event.fields) ||
|
|
476
|
+
event.fields.length === 0)
|
|
477
|
+
throw new DataStreamError('INVALID_STREAM_SCHEMA');
|
|
478
|
+
const fieldNames = new Set();
|
|
479
|
+
const sourceFields = event.fields.map((value) => {
|
|
480
|
+
const field = object(value);
|
|
481
|
+
if (typeof field.name !== 'string' ||
|
|
482
|
+
!name.test(field.name) ||
|
|
483
|
+
typeof field.dataType !== 'string' ||
|
|
484
|
+
!field.dataType ||
|
|
485
|
+
typeof field.label !== 'string' ||
|
|
486
|
+
!field.label ||
|
|
487
|
+
fieldNames.has(field.name.toLowerCase()))
|
|
488
|
+
throw new DataStreamError('INVALID_STREAM_SCHEMA');
|
|
489
|
+
fieldNames.add(field.name.toLowerCase());
|
|
490
|
+
return { name: field.name, label: field.label, dataType: field.dataType };
|
|
491
|
+
});
|
|
492
|
+
if (!sourceFields.some((field) => field.name === selection.primaryKey))
|
|
493
|
+
throw new DataStreamError('INVALID_STREAM_PRIMARY_KEY');
|
|
494
|
+
const listed = await clients.streams.list(selection.connectionName);
|
|
495
|
+
const listedStreams = listed.map((value) => object(value));
|
|
496
|
+
if (listedStreams.some((stream) => stream.dataSource !== selection.connectionName))
|
|
497
|
+
throw new DataStreamError('AMBIGUOUS_STREAM_BINDING');
|
|
498
|
+
// Only the streams of the selected event; the connection's other events have their own.
|
|
499
|
+
const inventory = listedStreams
|
|
500
|
+
.filter((stream) => streamEvent(stream, events) === selection.event)
|
|
501
|
+
.map((value) => inspectNativeDataStream(value));
|
|
502
|
+
if (inventory.length > 1)
|
|
503
|
+
throw new DataStreamError('AMBIGUOUS_STREAM_BINDING');
|
|
504
|
+
let existing;
|
|
505
|
+
try {
|
|
506
|
+
existing = inspectNativeDataStream(await clients.streams.get(selection.apiName));
|
|
507
|
+
if (existing.name !== selection.apiName)
|
|
508
|
+
throw new DataStreamError('STREAM_TARGET_MISMATCH');
|
|
509
|
+
}
|
|
510
|
+
catch (error) {
|
|
511
|
+
if (!(error instanceof DataStreamHttpError) ||
|
|
512
|
+
error.status !== 404 ||
|
|
513
|
+
!['REQUEST_FAILED', 'STREAM_HTTP_ERROR'].includes(error.code))
|
|
514
|
+
throw error;
|
|
515
|
+
}
|
|
516
|
+
const target = inventory[0];
|
|
517
|
+
if (existing &&
|
|
518
|
+
(!target ||
|
|
519
|
+
existing.recordId !== target.recordId ||
|
|
520
|
+
existing.name !== target.name ||
|
|
521
|
+
existing.dataSource !== selection.connectionName))
|
|
522
|
+
throw new DataStreamError('STREAM_TARGET_MISMATCH');
|
|
523
|
+
if (target) {
|
|
524
|
+
if (event.availabilityStatus !== 'In Use')
|
|
525
|
+
throw explainMismatch(new DataStreamError('STREAM_BINDING_DRIFT'), 'resolve existing stream event availability', selection.apiName, 'In Use', event.availabilityStatus, 'event.availabilityStatus');
|
|
526
|
+
const read = inspectNativeDataStream(await clients.streams.get(target.name));
|
|
527
|
+
if (!sameStreamSnapshot(read, target, postWrite))
|
|
528
|
+
throw explainMismatch(new DataStreamError('STREAM_BINDING_DRIFT'), 'stream inventory/detail agreement', target.name, target, read);
|
|
529
|
+
// eslint-disable-next-line unicorn/no-array-sort -- Sort fresh arrays within the existing ES2022 library target.
|
|
530
|
+
const expected = sourceFields.map((field) => [field.name, field.dataType]).sort();
|
|
531
|
+
const observed = read.sourceFields
|
|
532
|
+
.map((value) => {
|
|
533
|
+
const field = object(value);
|
|
534
|
+
return [field.name, field.datatype];
|
|
535
|
+
})
|
|
536
|
+
// eslint-disable-next-line unicorn/no-array-sort -- Sort a fresh array within the existing ES2022 library target.
|
|
537
|
+
.sort();
|
|
538
|
+
if (!isDeepStrictEqual(expected, observed))
|
|
539
|
+
throw explainMismatch(new DataStreamError('STREAM_SOURCE_DRIFT'), 'connection/stream source field agreement', target.name, expected, observed, 'sourceFields');
|
|
540
|
+
const fields = object(read.dataLakeObjectInfo).fields;
|
|
541
|
+
if (!Array.isArray(fields))
|
|
542
|
+
throw new DataStreamError('INVALID_STREAM_PRIMARY_KEY');
|
|
543
|
+
const keys = fields
|
|
544
|
+
.map((value) => object(value))
|
|
545
|
+
.filter((field) => field.isPrimaryKey === true);
|
|
546
|
+
if (keys.length !== 1 || keys[0].name !== selection.primaryKey)
|
|
547
|
+
throw new DataStreamError('INVALID_STREAM_PRIMARY_KEY');
|
|
548
|
+
}
|
|
549
|
+
else if (event.availabilityStatus !== 'Available' || existing)
|
|
550
|
+
throw explainMismatch(new DataStreamError('STREAM_BINDING_DRIFT'), 'resolve absent stream binding', selection.apiName, { availabilityStatus: 'Available', target: undefined }, { availabilityStatus: event.availabilityStatus, target: existing });
|
|
551
|
+
return structuredClone({
|
|
552
|
+
connectionId,
|
|
553
|
+
connectionName: selection.connectionName,
|
|
554
|
+
event: selection.event,
|
|
555
|
+
availability: event.availabilityStatus,
|
|
556
|
+
sourceFields,
|
|
557
|
+
inventory,
|
|
558
|
+
...(target ? { target } : {}),
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Retrieve exact supported native state as a portable document, retaining generated inventory.
|
|
563
|
+
* Unsupported mappings block projection rather than silently dropping authored intent.
|
|
564
|
+
* @param {string} apiName Exact server-generated stream name.
|
|
565
|
+
* @param {DataStreamReadClients} clients Internal read clients.
|
|
566
|
+
* @param {string} dataSpace The run's data space: the stream's DLO must belong to it.
|
|
567
|
+
* @param {boolean} postWrite Whether this read belongs to bounded create/update settlement.
|
|
568
|
+
* @param {DataStreamReadMode} mode `retrieve` lists connections leniently; `deploy` strictly.
|
|
569
|
+
* @returns {Promise<DataStreamDocument>} Detached validated portable document without runtime IDs.
|
|
570
|
+
*/
|
|
571
|
+
export async function retrieveNativeDataStream(apiName, clients, dataSpace = 'default', postWrite = false, mode = 'deploy') {
|
|
572
|
+
if (!name.test(apiName) || !isDataSpaceName(dataSpace))
|
|
573
|
+
throw new DataStreamError('INVALID_STREAM_BINDING');
|
|
574
|
+
return projectNativeDataStream(await clients.streams.get(apiName), apiName, clients, dataSpace, postWrite, mode);
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Project an Ingestion API stream GET into its portable document (see retrieveNativeDataStream).
|
|
578
|
+
* @param {unknown} read Stream GET response.
|
|
579
|
+
* @param {string} apiName Exact stream name.
|
|
580
|
+
* @param {DataStreamReadClients} clients Internal read clients.
|
|
581
|
+
* @param {string} dataSpace The run's data space: the stream's DLO must belong to it.
|
|
582
|
+
* @param {boolean} postWrite Whether this read belongs to bounded create/update settlement.
|
|
583
|
+
* @param {DataStreamReadMode} mode `retrieve` lists connections leniently; `deploy` strictly.
|
|
584
|
+
* @returns {Promise<DataStreamDocument>} Detached validated portable document; its c__dataSpace
|
|
585
|
+
* lists every data space of the DLO.
|
|
586
|
+
*/
|
|
587
|
+
export async function projectNativeDataStream(read, apiName, clients, dataSpace = 'default', postWrite = false, mode = 'deploy') {
|
|
588
|
+
// Decide from the stream GET alone whether the plugin can model it, before connection reads.
|
|
589
|
+
if (mode === 'retrieve')
|
|
590
|
+
triageRetrievedStream(read, apiName, dataSpace);
|
|
591
|
+
const raw = inspectNativeDataStream(read);
|
|
592
|
+
if (raw.name !== apiName)
|
|
593
|
+
throw new DataStreamError('STREAM_TARGET_MISMATCH');
|
|
594
|
+
let listed;
|
|
595
|
+
if (mode === 'retrieve')
|
|
596
|
+
listed = await listConnectionsLeniently(clients);
|
|
597
|
+
else {
|
|
598
|
+
const connectionIdList = await clients.connections.list();
|
|
599
|
+
if (!Array.isArray(connectionIdList.connections))
|
|
600
|
+
throw new DataStreamError('INCOMPLETE_CONNECTION_LIST');
|
|
601
|
+
listed = connectionIdList.connections;
|
|
602
|
+
}
|
|
603
|
+
const matches = listed
|
|
604
|
+
.map((value) => object(value))
|
|
605
|
+
.filter((item) => item.name === raw.dataSource);
|
|
606
|
+
if (matches.length !== 1 || typeof matches[0].id !== 'string' || !recordId.test(matches[0].id))
|
|
607
|
+
throw new DataStreamError('AMBIGUOUS_STREAM_CONNECTION');
|
|
608
|
+
const schema = await clients.connections.schema(matches[0].id);
|
|
609
|
+
if (!Array.isArray(schema.schemas) || schema.schemas.length === 0)
|
|
610
|
+
throw new DataStreamError('AMBIGUOUS_STREAM_EVENT');
|
|
611
|
+
const eventName = streamEvent(raw, schema.schemas);
|
|
612
|
+
const info = object(raw.dataLakeObjectInfo);
|
|
613
|
+
if (!Array.isArray(info.fields))
|
|
614
|
+
throw new DataStreamError('INVALID_STREAM_PRIMARY_KEY');
|
|
615
|
+
const keys = info.fields
|
|
616
|
+
.map((value) => object(value))
|
|
617
|
+
.filter((field) => field.isPrimaryKey === true);
|
|
618
|
+
if (keys.length !== 1 || typeof keys[0].name !== 'string')
|
|
619
|
+
throw new DataStreamError('INVALID_STREAM_PRIMARY_KEY');
|
|
620
|
+
const binding = await resolveDataStreamBinding({
|
|
621
|
+
apiName,
|
|
622
|
+
connectionName: raw.dataSource,
|
|
623
|
+
event: eventName,
|
|
624
|
+
primaryKey: keys[0].name,
|
|
625
|
+
}, clients, postWrite, mode);
|
|
626
|
+
if (!binding.target || !sameStreamSnapshot(raw, binding.target, postWrite))
|
|
627
|
+
throw explainMismatch(new DataStreamError('STREAM_BINDING_DRIFT'), 'retrieve binding agreement', apiName, raw, binding.target);
|
|
628
|
+
const wrapper = object(await clients.dlo.get(info.name));
|
|
629
|
+
if (!Array.isArray(wrapper.dataLakeObjects) || wrapper.dataLakeObjects.length !== 1)
|
|
630
|
+
throw new DataStreamError('INVALID_STREAM_DLO');
|
|
631
|
+
const dlo = object(wrapper.dataLakeObjects[0]);
|
|
632
|
+
if (dlo.name !== info.name ||
|
|
633
|
+
typeof dlo.id !== 'string' ||
|
|
634
|
+
!recordId.test(dlo.id) ||
|
|
635
|
+
dlo.category !== info.category ||
|
|
636
|
+
!Array.isArray(dlo.fields))
|
|
637
|
+
throw new DataStreamError('INVALID_STREAM_DLO');
|
|
638
|
+
// The DLO read names every data space it belongs to; the stream file mirrors that list.
|
|
639
|
+
const spaces = streamDloSpaces(dlo);
|
|
640
|
+
if (!spaces.includes(dataSpace))
|
|
641
|
+
throw outsideRunSpace(apiName, dataSpace);
|
|
642
|
+
const dloFields = dlo.fields.map((value) => {
|
|
643
|
+
const field = object(value);
|
|
644
|
+
return {
|
|
645
|
+
name: field.name,
|
|
646
|
+
label: field.label,
|
|
647
|
+
dataType: field.dataType,
|
|
648
|
+
isPrimaryKey: field.isPrimaryKey,
|
|
649
|
+
};
|
|
650
|
+
});
|
|
651
|
+
const expectedFields = info.fields.map((value) => {
|
|
652
|
+
const field = object(value);
|
|
653
|
+
return {
|
|
654
|
+
name: `${String(field.name)}__c`,
|
|
655
|
+
label: field.label,
|
|
656
|
+
dataType: field.dataType,
|
|
657
|
+
isPrimaryKey: field.isPrimaryKey,
|
|
658
|
+
};
|
|
659
|
+
});
|
|
660
|
+
const sorted = (fields) =>
|
|
661
|
+
// eslint-disable-next-line unicorn/no-array-sort -- Sort a copied array within the existing ES2022 library target.
|
|
662
|
+
[...fields].sort((a, b) => String(a.name).localeCompare(String(b.name)));
|
|
663
|
+
if (!isDeepStrictEqual(sorted(dloFields), sorted(expectedFields)))
|
|
664
|
+
throw explainMismatch(new DataStreamError('STREAM_DLO_DRIFT'), 'retrieve linked DLO field agreement', info.name, sorted(expectedFields), sorted(dloFields), 'fields');
|
|
665
|
+
const additions = [];
|
|
666
|
+
const formulas = [];
|
|
667
|
+
for (const value of raw.mappings) {
|
|
668
|
+
const mapping = object(value);
|
|
669
|
+
const source = binding.sourceFields.find((field) => field.name === mapping.sourceFieldName);
|
|
670
|
+
if (source && mapping.targetFieldName === source.name && mapping.formula === undefined)
|
|
671
|
+
continue;
|
|
672
|
+
if ((mapping.targetFieldName === 'DataSource' &&
|
|
673
|
+
mapping.formula === `"${binding.connectionName}"`) ||
|
|
674
|
+
(mapping.targetFieldName === 'DataSourceObject' && mapping.formula === `"${apiName}"`) ||
|
|
675
|
+
// Salesforce adds the partition-date formula of an Engagement DLO itself.
|
|
676
|
+
(typeof mapping.targetFieldName === 'string' &&
|
|
677
|
+
mapping.targetFieldName.startsWith('cdp_sys_') &&
|
|
678
|
+
typeof mapping.formula === 'string'))
|
|
679
|
+
continue;
|
|
680
|
+
const target = info.fields
|
|
681
|
+
.map((value) => object(value))
|
|
682
|
+
.find((field) => field.name === mapping.targetFieldName);
|
|
683
|
+
// An additive mapping reads back as its source label in `formula` when a PATCH added it, and
|
|
684
|
+
// as a plain source mapping onto another target when the create carried it (live, API 68).
|
|
685
|
+
const additiveSource = typeof mapping.formula === 'string' && mapping.sourceFieldName === undefined
|
|
686
|
+
? binding.sourceFields.find((field) => field.label === mapping.formula)
|
|
687
|
+
: mapping.formula === undefined
|
|
688
|
+
? binding.sourceFields.find((field) => field.name === mapping.sourceFieldName)
|
|
689
|
+
: undefined;
|
|
690
|
+
// The file names an additive source by its label; a label several event fields share cannot
|
|
691
|
+
// say which one, so such a stream is written as-is rather than as a file deploy refuses.
|
|
692
|
+
if (additiveSource &&
|
|
693
|
+
binding.sourceFields.filter((field) => field.label === additiveSource.label).length !== 1)
|
|
694
|
+
throw new DataStreamError('UNSUPPORTED_STREAM_PROJECTION');
|
|
695
|
+
if (!target ||
|
|
696
|
+
typeof target.dataType !== 'string' ||
|
|
697
|
+
(mapping.formula !== undefined && mapping.sourceFieldName !== undefined) ||
|
|
698
|
+
(typeof mapping.formula !== 'string' && !additiveSource))
|
|
699
|
+
throw new DataStreamError('UNSUPPORTED_STREAM_PROJECTION');
|
|
700
|
+
if (additiveSource)
|
|
701
|
+
additions.push({
|
|
702
|
+
sourceFieldLabel: additiveSource.label,
|
|
703
|
+
targetFieldName: target.name,
|
|
704
|
+
targetFieldReturntype: target.dataType,
|
|
705
|
+
});
|
|
706
|
+
else
|
|
707
|
+
formulas.push({
|
|
708
|
+
targetFieldName: target.name,
|
|
709
|
+
formula: mapping.formula,
|
|
710
|
+
targetFieldReturntype: target.dataType,
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
return validateDataStreamDocument({
|
|
714
|
+
schemaVersion: 1,
|
|
715
|
+
kind: 'data-stream',
|
|
716
|
+
[dataSpaceKey]: spaces,
|
|
717
|
+
identity: { apiName },
|
|
718
|
+
spec: {
|
|
719
|
+
connectorType: 'IngestApi',
|
|
720
|
+
connectionName: binding.connectionName,
|
|
721
|
+
event: binding.event,
|
|
722
|
+
datastreamType: 'INGEST_API',
|
|
723
|
+
refreshMode: object(raw.refreshConfig).refreshMode,
|
|
724
|
+
label: raw.label,
|
|
725
|
+
newDlo: {
|
|
726
|
+
name: dlo.name,
|
|
727
|
+
label: dlo.label,
|
|
728
|
+
...(info.category === undefined ? {} : { category: info.category }),
|
|
729
|
+
...(typeof info.eventDateTimeFieldName === 'string'
|
|
730
|
+
? { eventDateTimeFieldName: info.eventDateTimeFieldName }
|
|
731
|
+
: {}),
|
|
732
|
+
primaryKey: { name: keys[0].name, label: keys[0].label, dataType: keys[0].dataType },
|
|
733
|
+
},
|
|
734
|
+
// Canonicalize only projections derived here; caller-authored mapping order stays strict.
|
|
735
|
+
additiveFieldMappings: byTarget(additions),
|
|
736
|
+
...(formulas.length > 0 ? { formulaMappings: byTarget(formulas) } : {}),
|
|
737
|
+
},
|
|
738
|
+
observed: {
|
|
739
|
+
// Salesforce lists stream mappings in varying order; sort so one org state writes one file.
|
|
740
|
+
mappings: sortedStreamMappings(raw.mappings),
|
|
741
|
+
sourceFields: raw.sourceFields,
|
|
742
|
+
dloFields,
|
|
743
|
+
actualDloName: dlo.name,
|
|
744
|
+
},
|
|
745
|
+
}, mode);
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Prove proposed parent absence separately from the logical stream binding.
|
|
749
|
+
* @param {string} apiName Exact proposed DLO name.
|
|
750
|
+
* @param {DataStreamReadClients} clients Independent native readers.
|
|
751
|
+
* @returns {Promise<void>} Resolves only on a genuine native HTTP404.
|
|
752
|
+
*/
|
|
753
|
+
async function requireAbsentStreamDlo(apiName, clients) {
|
|
754
|
+
if (!(await streamDloAbsent(apiName, clients)))
|
|
755
|
+
throw new DataStreamError('STREAM_PROPOSED_DLO_EXISTS');
|
|
756
|
+
}
|
|
757
|
+
/**
|
|
758
|
+
* Observe DLO absence by name or ID: true only on a genuine native HTTP 404.
|
|
759
|
+
* @param {string} identity DLO name or ID.
|
|
760
|
+
* @param {DataStreamReadClients} clients Native DLO reader.
|
|
761
|
+
* @returns {Promise<boolean>} True when the DLO reads 404; false while it still reads.
|
|
762
|
+
*/
|
|
763
|
+
async function streamDloAbsent(identity, clients) {
|
|
764
|
+
try {
|
|
765
|
+
await clients.dlo.get(identity);
|
|
766
|
+
}
|
|
767
|
+
catch (error) {
|
|
768
|
+
if ((error instanceof ConnectError &&
|
|
769
|
+
error.code === 'REQUEST_FAILED' &&
|
|
770
|
+
error.statusCode === 404) ||
|
|
771
|
+
(error instanceof DataStreamHttpError &&
|
|
772
|
+
['REQUEST_FAILED', 'STREAM_HTTP_ERROR'].includes(error.code) &&
|
|
773
|
+
error.status === 404))
|
|
774
|
+
return true;
|
|
775
|
+
throw error;
|
|
776
|
+
}
|
|
777
|
+
return false;
|
|
778
|
+
}
|
|
779
|
+
const failureCauses = new WeakMap();
|
|
780
|
+
/**
|
|
781
|
+
* Keep the error behind a failed stream deploy result, so the command can report its explanation.
|
|
782
|
+
* @template {object} T
|
|
783
|
+
* @param {T} result The item result built from the error.
|
|
784
|
+
* @param {unknown} error The caught error.
|
|
785
|
+
* @returns {T} The same result.
|
|
786
|
+
*/
|
|
787
|
+
export function retainStreamFailure(result, error) {
|
|
788
|
+
failureCauses.set(result, error);
|
|
789
|
+
return result;
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* The error behind a failed stream deploy result, when the executor kept it.
|
|
793
|
+
* @param {object} result Item result.
|
|
794
|
+
* @returns {unknown} The caught error, or undefined.
|
|
795
|
+
*/
|
|
796
|
+
export function streamFailureCause(result) {
|
|
797
|
+
return failureCauses.get(result);
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* Refuse a stream file whose c__dataSpace list does not name the run's data space.
|
|
801
|
+
* @param {DataStreamDocument} document Stream file.
|
|
802
|
+
* @param {string} dataSpace The run's data space.
|
|
803
|
+
*/
|
|
804
|
+
export function requireRunSpace(document, dataSpace) {
|
|
805
|
+
if (!document.c__dataSpace.includes(dataSpace))
|
|
806
|
+
throw explainInput(new DataStreamError('STREAM_DATASPACE_MISMATCH'), `The ${dataSpaceKey} list of ${printableKey(document.identity.apiName)} (${document.c__dataSpace.map((space) => printableKey(space)).join(', ')}) does not name the run's data space ${printableKey(dataSpace)}. Deploy it with --dataspace set to one of its data spaces.`);
|
|
807
|
+
}
|
|
808
|
+
/**
|
|
809
|
+
* Every additive mapping's source label must name exactly one field of the event: Salesforce stores
|
|
810
|
+
* an unknown label without checking it, as a mapping that never carries data (live, API 68).
|
|
811
|
+
* @param {DataStreamMapping[]} additions Additive mappings of the file.
|
|
812
|
+
* @param {Array<{name: string; label: string; dataType: string}>} sourceFields Event fields.
|
|
813
|
+
* @returns {Array<{name: string; label: string}>} The source field of each mapping, in order.
|
|
814
|
+
*/
|
|
815
|
+
function additiveSources(additions, sourceFields) {
|
|
816
|
+
return additions.map((desired, index) => {
|
|
817
|
+
const sources = sourceFields.filter((field) => field.label === desired.sourceFieldLabel);
|
|
818
|
+
if (sources.length !== 1)
|
|
819
|
+
throw explainInput(new DataStreamError('UNSUPPORTED_STREAM_MAPPING_SOURCE'), `spec.additiveFieldMappings[${index}].sourceFieldLabel ${sources.length === 0 ? 'names no' : 'names more than one'} field of the event. Salesforce would store the mapping without checking it, and it would never carry data. Use the exact label of one source field.`);
|
|
820
|
+
return sources[0];
|
|
821
|
+
});
|
|
822
|
+
}
|
|
823
|
+
/**
|
|
824
|
+
* Admit a create with independent name and binding absence; its additive mappings must name
|
|
825
|
+
* source fields of the event. The file must list the run's data space alone: the create puts
|
|
826
|
+
* the landing DLO into it.
|
|
827
|
+
* @param {unknown} value Portable authored stream.
|
|
828
|
+
* @param {DataStreamReadClients} clients Independent read boundaries.
|
|
829
|
+
* @param {string} [dataSpace] The run's data space.
|
|
830
|
+
* @returns {Promise<DataStreamCreatePlan>} Private read-only create admission, never dispatch authority.
|
|
831
|
+
*/
|
|
832
|
+
export async function preflightNativeDataStreamCreate(value, clients, dataSpace = 'default') {
|
|
833
|
+
const document = validateDataStreamDocument(value);
|
|
834
|
+
requireRunSpace(document, dataSpace);
|
|
835
|
+
if (document.c__dataSpace.length !== 1)
|
|
836
|
+
throw createSpacesRefusal(document.c__dataSpace);
|
|
837
|
+
buildDataStreamCreateBody(document);
|
|
838
|
+
const binding = await resolveDataStreamBinding({
|
|
839
|
+
...document.identity,
|
|
840
|
+
connectionName: document.spec.connectionName,
|
|
841
|
+
event: document.spec.event,
|
|
842
|
+
primaryKey: document.spec.newDlo.primaryKey.name,
|
|
843
|
+
}, clients);
|
|
844
|
+
if (binding.target)
|
|
845
|
+
throw new DataStreamError('STREAM_EXISTING_BINDING_REQUIRES_UPDATE');
|
|
846
|
+
const key = binding.sourceFields.find((field) => field.name === document.spec.newDlo.primaryKey.name);
|
|
847
|
+
if (key?.label !== document.spec.newDlo.primaryKey.label)
|
|
848
|
+
throw new DataStreamError('INVALID_STREAM_PRIMARY_KEY');
|
|
849
|
+
additiveSources(document.spec.additiveFieldMappings, binding.sourceFields);
|
|
850
|
+
await requireAbsentStreamDlo(document.spec.newDlo.name, clients);
|
|
851
|
+
return { operation: 'create', document, binding };
|
|
852
|
+
}
|
|
853
|
+
/**
|
|
854
|
+
* Reconcile case-sensitive persisted identifiers, never definition identifiers.
|
|
855
|
+
* @param {string} echoed Native create recordId, 15 or 18 characters.
|
|
856
|
+
* @param {string} persisted Independently read 18-character recordId.
|
|
857
|
+
* @returns {boolean} Whether the exact corresponding Salesforce ID was observed.
|
|
858
|
+
*/
|
|
859
|
+
function sameStreamRecordId(echoed, persisted) {
|
|
860
|
+
if (echoed.length === 18)
|
|
861
|
+
return echoed === persisted;
|
|
862
|
+
if (!/^[A-Za-z0-9]{15}$/u.test(echoed))
|
|
863
|
+
return false;
|
|
864
|
+
let suffix = '';
|
|
865
|
+
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345';
|
|
866
|
+
for (let group = 0; group < 3; group++) {
|
|
867
|
+
let bits = 0;
|
|
868
|
+
for (let index = 0; index < 5; index++)
|
|
869
|
+
if (/[A-Z]/u.test(echoed[group * 5 + index]))
|
|
870
|
+
bits |= 1 << index;
|
|
871
|
+
suffix += alphabet[bits];
|
|
872
|
+
}
|
|
873
|
+
return persisted === echoed + suffix;
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* Dispatch one freshly guarded create and independently settle generated stream and DLO identities.
|
|
877
|
+
* No public command calls this create-only subchunk; errors after dispatch must remain unknown.
|
|
878
|
+
* @param {unknown} value Portable authored stream.
|
|
879
|
+
* @param {DataStreamDeployClients} clients Internal read/write boundaries.
|
|
880
|
+
* @param {DataStreamDeployOptions} options Explicit processing acknowledgement and immutable input guard.
|
|
881
|
+
* @returns {Promise<object>} Confirmed generated identities and portable independent readback.
|
|
882
|
+
*/
|
|
883
|
+
export async function createNativeDataStream(value, clients, options) {
|
|
884
|
+
const document = validateDataStreamDocument(value);
|
|
885
|
+
const dataSpace = options.dataSpace ?? 'default';
|
|
886
|
+
if (options.allowProcessing !== true)
|
|
887
|
+
throw new DataStreamError('PROCESSING_ACKNOWLEDGEMENT_REQUIRED');
|
|
888
|
+
await options.verifyInput();
|
|
889
|
+
const plan = await preflightNativeDataStreamCreate(document, clients, dataSpace);
|
|
890
|
+
await options.verifyInput();
|
|
891
|
+
const fresh = await preflightNativeDataStreamCreate(document, clients, dataSpace);
|
|
892
|
+
if (!sameStreamSnapshot(plan, fresh))
|
|
893
|
+
throw explainMismatch(new DataStreamError('STREAM_BINDING_DRIFT'), 'pre-dispatch plan recheck', document.identity.apiName, plan, fresh);
|
|
894
|
+
await options.verifyInput();
|
|
895
|
+
const now = options.now ?? Date.now;
|
|
896
|
+
const pause = options.wait ?? wait;
|
|
897
|
+
const deadline = now() + 15 * 60 * 1000;
|
|
898
|
+
const echo = object(await clients.streams.create(document, plan.binding.sourceFields.map((field) => ({ name: field.name, dataType: field.dataType }))));
|
|
899
|
+
if (typeof echo.name !== 'string' ||
|
|
900
|
+
!name.test(echo.name) ||
|
|
901
|
+
typeof echo.recordId !== 'string' ||
|
|
902
|
+
!/^[A-Za-z0-9]{15}(?:[A-Za-z0-9]{3})?$/u.test(echo.recordId))
|
|
903
|
+
throw new DataStreamError('STREAM_CREATE_UNCONFIRMED');
|
|
904
|
+
let settledDloId;
|
|
905
|
+
for (let attempt = 0; attempt < 20 && now() < deadline; attempt++) {
|
|
906
|
+
try {
|
|
907
|
+
const binding = await resolveDataStreamBinding({
|
|
908
|
+
apiName: echo.name,
|
|
909
|
+
connectionName: document.spec.connectionName,
|
|
910
|
+
event: document.spec.event,
|
|
911
|
+
primaryKey: document.spec.newDlo.primaryKey.name,
|
|
912
|
+
}, clients, true);
|
|
913
|
+
const target = binding.target;
|
|
914
|
+
if (!target ||
|
|
915
|
+
target.name !== echo.name ||
|
|
916
|
+
!sameStreamRecordId(echo.recordId, target.recordId) ||
|
|
917
|
+
binding.connectionId !== plan.binding.connectionId ||
|
|
918
|
+
!isDeepStrictEqual(binding.sourceFields, plan.binding.sourceFields))
|
|
919
|
+
throw new DataStreamError('STREAM_CREATE_IDENTITY_UNCONFIRMED');
|
|
920
|
+
const byId = inspectNativeDataStream(await clients.streams.get(target.recordId));
|
|
921
|
+
if (!sameStreamSnapshot(target, byId, true))
|
|
922
|
+
throw explainMismatch(new DataStreamError('STREAM_BINDING_DRIFT'), 'created stream name/ID readback agreement', target.name, target, byId);
|
|
923
|
+
const actual = await retrieveNativeDataStream(echo.name, clients, dataSpace, true);
|
|
924
|
+
if (actual.spec.newDlo.label !== document.spec.newDlo.label ||
|
|
925
|
+
!isDeepStrictEqual(actual.spec.newDlo.primaryKey, document.spec.newDlo.primaryKey) ||
|
|
926
|
+
plan.binding.sourceFields.some((field) => !actual.observed?.mappings.some((mapping) => mapping.sourceFieldName === field.name &&
|
|
927
|
+
mapping.targetFieldName === field.name &&
|
|
928
|
+
mapping.formula === undefined)))
|
|
929
|
+
throw new DataStreamError('STREAM_CREATE_PERSISTENCE_UNCONFIRMED');
|
|
930
|
+
const wrapper = object(await clients.dlo.get(actual.spec.newDlo.name));
|
|
931
|
+
if (!Array.isArray(wrapper.dataLakeObjects) || wrapper.dataLakeObjects.length !== 1)
|
|
932
|
+
throw new DataStreamError('INVALID_STREAM_DLO');
|
|
933
|
+
const dlo = object(wrapper.dataLakeObjects[0]);
|
|
934
|
+
if (dlo.name !== actual.spec.newDlo.name ||
|
|
935
|
+
typeof dlo.id !== 'string' ||
|
|
936
|
+
!recordId.test(dlo.id) ||
|
|
937
|
+
dlo.category !== actual.spec.newDlo.category ||
|
|
938
|
+
!sameSpaces(streamDloSpaces(dlo), document.c__dataSpace) ||
|
|
939
|
+
!Array.isArray(dlo.fields) ||
|
|
940
|
+
!isDeepStrictEqual(dlo.fields.map((value) => {
|
|
941
|
+
const field = object(value);
|
|
942
|
+
return {
|
|
943
|
+
name: field.name,
|
|
944
|
+
label: field.label,
|
|
945
|
+
dataType: field.dataType,
|
|
946
|
+
isPrimaryKey: field.isPrimaryKey,
|
|
947
|
+
};
|
|
948
|
+
}), actual.observed?.dloFields))
|
|
949
|
+
throw explainMismatch(new DataStreamError('STREAM_DLO_DRIFT'), 'create linked parent verification', actual.spec.newDlo.name, {
|
|
950
|
+
name: actual.spec.newDlo.name,
|
|
951
|
+
category: actual.spec.newDlo.category,
|
|
952
|
+
dataSpaceInfo: document.c__dataSpace.map((space) => ({ name: space })),
|
|
953
|
+
fields: actual.observed?.dloFields,
|
|
954
|
+
id: true,
|
|
955
|
+
}, {
|
|
956
|
+
name: dlo.name,
|
|
957
|
+
category: dlo.category,
|
|
958
|
+
dataSpaceInfo: Array.isArray(dlo.dataSpaceInfo)
|
|
959
|
+
? dlo.dataSpaceInfo.map((entry) => ({ name: entry?.name }))
|
|
960
|
+
: dlo.dataSpaceInfo,
|
|
961
|
+
fields: Array.isArray(dlo.fields)
|
|
962
|
+
? dlo.fields.map((entry) => {
|
|
963
|
+
const field = entry;
|
|
964
|
+
return {
|
|
965
|
+
name: field?.name,
|
|
966
|
+
label: field?.label,
|
|
967
|
+
dataType: field?.dataType,
|
|
968
|
+
isPrimaryKey: field?.isPrimaryKey,
|
|
969
|
+
};
|
|
970
|
+
})
|
|
971
|
+
: dlo.fields,
|
|
972
|
+
id: typeof dlo.id === 'string' && recordId.test(dlo.id),
|
|
973
|
+
});
|
|
974
|
+
if (settledDloId !== undefined && settledDloId !== dlo.id)
|
|
975
|
+
throw explainMismatch(new DataStreamError('STREAM_DLO_DRIFT'), 'create settled parent identity preservation', dlo.name, settledDloId, dlo.id, 'recordId');
|
|
976
|
+
settledDloId = dlo.id;
|
|
977
|
+
if (now() >= deadline)
|
|
978
|
+
break;
|
|
979
|
+
const linkedStatus = object(target.dataLakeObjectInfo).status;
|
|
980
|
+
if (target.status === 'ACTIVE' &&
|
|
981
|
+
dlo.status === 'ACTIVE' &&
|
|
982
|
+
(linkedStatus === undefined || linkedStatus === 'ACTIVE')) {
|
|
983
|
+
await options.verifyInput();
|
|
984
|
+
if (now() >= deadline)
|
|
985
|
+
break;
|
|
986
|
+
const formulasOf = (entries) => (entries ?? []).map((entry) => JSON.stringify([
|
|
987
|
+
entry.targetFieldName,
|
|
988
|
+
entry.formula,
|
|
989
|
+
entry.targetFieldReturntype.toUpperCase(),
|
|
990
|
+
]));
|
|
991
|
+
const additionsOf = (entries) => entries.map((entry) => JSON.stringify([
|
|
992
|
+
entry.sourceFieldLabel,
|
|
993
|
+
entry.targetFieldName,
|
|
994
|
+
entry.targetFieldReturntype.toUpperCase(),
|
|
995
|
+
]));
|
|
996
|
+
const missing = [
|
|
997
|
+
...formulasOf(document.spec.formulaMappings).filter((entry) => !formulasOf(actual.spec.formulaMappings).includes(entry)),
|
|
998
|
+
...additionsOf(document.spec.additiveFieldMappings).filter((entry) => !additionsOf(actual.spec.additiveFieldMappings).includes(entry)),
|
|
999
|
+
];
|
|
1000
|
+
return {
|
|
1001
|
+
operation: 'create',
|
|
1002
|
+
targetApiName: echo.name,
|
|
1003
|
+
targetDloName: actual.spec.newDlo.name,
|
|
1004
|
+
document: actual,
|
|
1005
|
+
notices: missing.length > 0
|
|
1006
|
+
? [
|
|
1007
|
+
{
|
|
1008
|
+
code: 'STREAM_CHANGE_NOT_APPLIED',
|
|
1009
|
+
message: 'Salesforce created the stream but it does not hold every formula or additive mapping of the file. Retrieve the stream to see what it holds.',
|
|
1010
|
+
},
|
|
1011
|
+
]
|
|
1012
|
+
: [],
|
|
1013
|
+
};
|
|
1014
|
+
}
|
|
1015
|
+
if (!['ACTIVE', 'PROCESSING'].includes(target.status) ||
|
|
1016
|
+
!['ACTIVE', 'PROCESSING'].includes(dlo.status) ||
|
|
1017
|
+
(linkedStatus !== undefined && !['ACTIVE', 'PROCESSING'].includes(linkedStatus)))
|
|
1018
|
+
throw new DataStreamError('STREAM_CREATE_SETTLEMENT_UNCONFIRMED');
|
|
1019
|
+
}
|
|
1020
|
+
catch (error) {
|
|
1021
|
+
if (!(error instanceof DataStreamError) || error.code !== 'STREAM_SNAPSHOT_PENDING')
|
|
1022
|
+
throw error;
|
|
1023
|
+
}
|
|
1024
|
+
if (attempt < 19 && now() < deadline)
|
|
1025
|
+
await pause(Math.min(5000, deadline - now()));
|
|
1026
|
+
}
|
|
1027
|
+
throw new DataStreamError('STREAM_CREATE_SETTLEMENT_UNCONFIRMED');
|
|
1028
|
+
}
|
|
1029
|
+
/**
|
|
1030
|
+
* Independently bind the persisted parent identity and raw field inventory to the portable readback.
|
|
1031
|
+
* @param {DataStreamDocument} document Independently retrieved target.
|
|
1032
|
+
* @param {DataStreamReadClients} clients Native DLO reader.
|
|
1033
|
+
* @returns {Promise<Json>} Detached parent observation, including protected runtime identity.
|
|
1034
|
+
*/
|
|
1035
|
+
async function readUpdateStreamDlo(document, clients) {
|
|
1036
|
+
const wrapper = object(await clients.dlo.get(document.spec.newDlo.name));
|
|
1037
|
+
if (!Array.isArray(wrapper.dataLakeObjects) || wrapper.dataLakeObjects.length !== 1)
|
|
1038
|
+
throw new DataStreamError('INVALID_STREAM_DLO');
|
|
1039
|
+
const dlo = object(wrapper.dataLakeObjects[0]);
|
|
1040
|
+
if (dlo.name !== document.spec.newDlo.name ||
|
|
1041
|
+
typeof dlo.id !== 'string' ||
|
|
1042
|
+
!recordId.test(dlo.id) ||
|
|
1043
|
+
dlo.category !== document.spec.newDlo.category ||
|
|
1044
|
+
!sameSpaces(streamDloSpaces(dlo), document.c__dataSpace) ||
|
|
1045
|
+
!Array.isArray(dlo.fields) ||
|
|
1046
|
+
!isDeepStrictEqual(dlo.fields.map((value) => {
|
|
1047
|
+
const field = object(value);
|
|
1048
|
+
return {
|
|
1049
|
+
name: field.name,
|
|
1050
|
+
label: field.label,
|
|
1051
|
+
dataType: field.dataType,
|
|
1052
|
+
isPrimaryKey: field.isPrimaryKey,
|
|
1053
|
+
};
|
|
1054
|
+
}), document.observed?.dloFields))
|
|
1055
|
+
throw explainMismatch(new DataStreamError('STREAM_DLO_DRIFT'), 'update linked parent verification', document.spec.newDlo.name, {
|
|
1056
|
+
name: document.spec.newDlo.name,
|
|
1057
|
+
category: document.spec.newDlo.category,
|
|
1058
|
+
dataSpaceInfo: document.c__dataSpace.map((space) => ({ name: space })),
|
|
1059
|
+
fields: document.observed?.dloFields,
|
|
1060
|
+
id: true,
|
|
1061
|
+
}, {
|
|
1062
|
+
name: dlo.name,
|
|
1063
|
+
category: dlo.category,
|
|
1064
|
+
dataSpaceInfo: Array.isArray(dlo.dataSpaceInfo)
|
|
1065
|
+
? dlo.dataSpaceInfo.map((entry) => ({ name: entry?.name }))
|
|
1066
|
+
: dlo.dataSpaceInfo,
|
|
1067
|
+
fields: Array.isArray(dlo.fields)
|
|
1068
|
+
? dlo.fields.map((entry) => {
|
|
1069
|
+
const field = entry;
|
|
1070
|
+
return {
|
|
1071
|
+
name: field?.name,
|
|
1072
|
+
label: field?.label,
|
|
1073
|
+
dataType: field?.dataType,
|
|
1074
|
+
isPrimaryKey: field?.isPrimaryKey,
|
|
1075
|
+
};
|
|
1076
|
+
})
|
|
1077
|
+
: dlo.fields,
|
|
1078
|
+
id: typeof dlo.id === 'string' && recordId.test(dlo.id),
|
|
1079
|
+
});
|
|
1080
|
+
return structuredClone(dlo);
|
|
1081
|
+
}
|
|
1082
|
+
/**
|
|
1083
|
+
* Compare the file with the stream. Mapping changes are sent as the file holds them and Salesforce
|
|
1084
|
+
* decides (live, API 68, it refuses any change or removal of an existing mapping on an Ingestion
|
|
1085
|
+
* API stream); settings Salesforce answers with 200 but ignores are refused before any write.
|
|
1086
|
+
* The file's c__dataSpace list must name the run's data space and equal the DLO's data spaces
|
|
1087
|
+
* (check only: spaces change through the DLO file).
|
|
1088
|
+
* @param {unknown} value The stream file (desired state of its mappings).
|
|
1089
|
+
* @param {DataStreamReadClients} clients Independent binding, source and parent readers.
|
|
1090
|
+
* @param {string} [dataSpace] The run's data space.
|
|
1091
|
+
* @returns {Promise<DataStreamUpdatePlan>} Private update/noop comparison without dispatch.
|
|
1092
|
+
*/
|
|
1093
|
+
export async function preflightNativeDataStreamUpdate(value, clients, dataSpace = 'default') {
|
|
1094
|
+
const document = validateDataStreamDocument(value);
|
|
1095
|
+
requireRunSpace(document, dataSpace);
|
|
1096
|
+
const binding = await resolveDataStreamBinding({
|
|
1097
|
+
...document.identity,
|
|
1098
|
+
connectionName: document.spec.connectionName,
|
|
1099
|
+
event: document.spec.event,
|
|
1100
|
+
primaryKey: document.spec.newDlo.primaryKey.name,
|
|
1101
|
+
}, clients);
|
|
1102
|
+
if (!binding.target)
|
|
1103
|
+
throw new DataStreamError('STREAM_UPDATE_TARGET_ABSENT');
|
|
1104
|
+
const target = await retrieveNativeDataStream(binding.target.name, clients, dataSpace);
|
|
1105
|
+
if (!sameSpaces(document.c__dataSpace, target.c__dataSpace))
|
|
1106
|
+
throw spacesDiffer(document.identity.apiName, document.c__dataSpace, target.c__dataSpace);
|
|
1107
|
+
refuseEscapedCopy(document, target, (code) => new DataStreamError(code));
|
|
1108
|
+
const confirmation = inspectNativeDataStream(await clients.streams.get(binding.target.recordId));
|
|
1109
|
+
if (!sameStreamSnapshot(confirmation, binding.target))
|
|
1110
|
+
throw explainMismatch(new DataStreamError('STREAM_BINDING_DRIFT'), 'update name/ID readback agreement', binding.target.name, binding.target, confirmation);
|
|
1111
|
+
// A PATCH of these answers 200 and changes nothing, so it would report a change that did not
|
|
1112
|
+
// happen; the primary key and refresh mode have no write at all (live, API 68).
|
|
1113
|
+
const settings = [
|
|
1114
|
+
...(sameEnum(document.spec.refreshMode, target.spec.refreshMode) ? [] : ['spec.refreshMode']),
|
|
1115
|
+
...(document.spec.newDlo.label === target.spec.newDlo.label ? [] : ['spec.newDlo.label']),
|
|
1116
|
+
...(sameEnum(document.spec.newDlo.category, target.spec.newDlo.category)
|
|
1117
|
+
? []
|
|
1118
|
+
: ['spec.newDlo.category']),
|
|
1119
|
+
...(document.spec.newDlo.eventDateTimeFieldName === target.spec.newDlo.eventDateTimeFieldName
|
|
1120
|
+
? []
|
|
1121
|
+
: ['spec.newDlo.eventDateTimeFieldName']),
|
|
1122
|
+
...(sameRecord(document.spec.newDlo.primaryKey, target.spec.newDlo.primaryKey, dloTypeKey)
|
|
1123
|
+
? []
|
|
1124
|
+
: ['spec.newDlo.primaryKey']),
|
|
1125
|
+
];
|
|
1126
|
+
if (settings.length > 0)
|
|
1127
|
+
throw explainInput(new DataStreamError('UNSUPPORTED_STREAM_SETTINGS_CHANGE'), `The file differs from the stream in ${settings.join(', ')}, which no stream update changes (Salesforce ignores them or has no write for them). Use the values from a freshly retrieved file, or change them in Salesforce.`);
|
|
1128
|
+
const dlo = await readUpdateStreamDlo(target, clients);
|
|
1129
|
+
if (binding.target.status !== 'ACTIVE' ||
|
|
1130
|
+
dlo.status !== 'ACTIVE' ||
|
|
1131
|
+
(object(binding.target.dataLakeObjectInfo).status !== undefined &&
|
|
1132
|
+
object(binding.target.dataLakeObjectInfo).status !== 'ACTIVE'))
|
|
1133
|
+
throw new DataStreamError('STREAM_NOT_READY');
|
|
1134
|
+
additiveSources(document.spec.additiveFieldMappings, binding.sourceFields);
|
|
1135
|
+
// An additive mapping the stream already holds unchanged is not sent; any other is.
|
|
1136
|
+
const additions = document.spec.additiveFieldMappings.filter((desired) => {
|
|
1137
|
+
const existing = target.spec.additiveFieldMappings.find((entry) => entry.targetFieldName === desired.targetFieldName);
|
|
1138
|
+
return !(existing &&
|
|
1139
|
+
existing.sourceFieldLabel === desired.sourceFieldLabel &&
|
|
1140
|
+
sameEnum(existing.targetFieldReturntype, desired.targetFieldReturntype));
|
|
1141
|
+
});
|
|
1142
|
+
const patch = validateDataStreamDocument({
|
|
1143
|
+
...document,
|
|
1144
|
+
spec: { ...document.spec, additiveFieldMappings: additions },
|
|
1145
|
+
});
|
|
1146
|
+
if (additions.length > 0)
|
|
1147
|
+
buildDataStreamPatchBody(patch);
|
|
1148
|
+
// Formulas are sent as the file holds them; Salesforce decides (it refuses a removal).
|
|
1149
|
+
const wanted = document.spec.formulaMappings ?? [];
|
|
1150
|
+
const have = target.spec.formulaMappings ?? [];
|
|
1151
|
+
const formulaChanges = [];
|
|
1152
|
+
for (const mapping of wanted) {
|
|
1153
|
+
const existing = have.find((entry) => entry.targetFieldName === mapping.targetFieldName);
|
|
1154
|
+
if (existing?.formula !== mapping.formula ||
|
|
1155
|
+
!sameEnum(existing?.targetFieldReturntype, mapping.targetFieldReturntype))
|
|
1156
|
+
formulaChanges.push({
|
|
1157
|
+
targetFieldName: mapping.targetFieldName,
|
|
1158
|
+
transformationFormula: mapping.formula,
|
|
1159
|
+
targetFieldReturntype: mapping.targetFieldReturntype,
|
|
1160
|
+
});
|
|
1161
|
+
}
|
|
1162
|
+
// A mapping the file leaves out is sent as a removal (an empty formula); Salesforce decides.
|
|
1163
|
+
const kept = new Set([...wanted, ...document.spec.additiveFieldMappings].map((entry) => entry.targetFieldName));
|
|
1164
|
+
for (const mapping of [...have, ...target.spec.additiveFieldMappings])
|
|
1165
|
+
if (!kept.has(mapping.targetFieldName))
|
|
1166
|
+
formulaChanges.push({ targetFieldName: mapping.targetFieldName, transformationFormula: '' });
|
|
1167
|
+
const labelChange = document.spec.label !== target.spec.label;
|
|
1168
|
+
return {
|
|
1169
|
+
operation: additions.length > 0 || formulaChanges.length > 0 || labelChange ? 'update' : 'noop',
|
|
1170
|
+
document,
|
|
1171
|
+
patch,
|
|
1172
|
+
binding,
|
|
1173
|
+
target,
|
|
1174
|
+
dlo,
|
|
1175
|
+
additions,
|
|
1176
|
+
formulaChanges,
|
|
1177
|
+
labelChange,
|
|
1178
|
+
};
|
|
1179
|
+
}
|
|
1180
|
+
/**
|
|
1181
|
+
* The PATCH of an update with formula or label parts: `datastreamType` plus the mappings to add,
|
|
1182
|
+
* change or remove and, when it is not sent through the Metadata API, the label.
|
|
1183
|
+
* @param {DataStreamUpdatePlan} plan Update plan.
|
|
1184
|
+
* @param {boolean} [withLabel] Whether the PATCH carries the label change.
|
|
1185
|
+
* @returns {Json} PATCH body.
|
|
1186
|
+
*/
|
|
1187
|
+
export function ingestDataStreamPatchBody(plan, withLabel = true) {
|
|
1188
|
+
const mappings = [...plan.additions, ...plan.formulaChanges];
|
|
1189
|
+
return {
|
|
1190
|
+
datastreamType: plan.document.spec.datastreamType,
|
|
1191
|
+
...(mappings.length > 0 ? { mappings: structuredClone(mappings) } : {}),
|
|
1192
|
+
...(plan.labelChange && withLabel ? { label: plan.document.spec.label } : {}),
|
|
1193
|
+
};
|
|
1194
|
+
}
|
|
1195
|
+
/**
|
|
1196
|
+
* The notice for a label Salesforce accepted but did not apply.
|
|
1197
|
+
* @param {boolean} kept Whether the stream still has another label than the file.
|
|
1198
|
+
* @returns {DataStreamNotice[]} Notices.
|
|
1199
|
+
*/
|
|
1200
|
+
function labelNotApplied(kept) {
|
|
1201
|
+
return kept
|
|
1202
|
+
? [
|
|
1203
|
+
{
|
|
1204
|
+
code: 'STREAM_CHANGE_NOT_APPLIED',
|
|
1205
|
+
message: 'Salesforce accepted the label change but kept the old label; change it in Salesforce. Retrieve the stream to see what it holds.',
|
|
1206
|
+
},
|
|
1207
|
+
]
|
|
1208
|
+
: [];
|
|
1209
|
+
}
|
|
1210
|
+
/**
|
|
1211
|
+
* Remove only runtime settlement status when comparing preserved native configuration.
|
|
1212
|
+
* @param {Json} value Detached raw native object.
|
|
1213
|
+
* @param {string[]} omitted Explicit mutable collections/status keys.
|
|
1214
|
+
* @returns {Json} Protected configuration, retaining unknown fields conservatively.
|
|
1215
|
+
*/
|
|
1216
|
+
function streamProtected(value, omitted) {
|
|
1217
|
+
return Object.fromEntries(Object.entries(value).filter(([key]) => !omitted.includes(key)));
|
|
1218
|
+
}
|
|
1219
|
+
/**
|
|
1220
|
+
* Require all prior entries byte-equivalent in meaning, and only the requested extra targets.
|
|
1221
|
+
* @param {unknown[]} before Original entries.
|
|
1222
|
+
* @param {unknown[]} after Current entries.
|
|
1223
|
+
* @param {string} key Exact identity field.
|
|
1224
|
+
* @param {string[]} added Allowed new identities.
|
|
1225
|
+
* @returns {boolean} Whether no prior entry was changed or removed and no other entry appeared.
|
|
1226
|
+
*/
|
|
1227
|
+
function streamEntriesPreserved(before, after, key, added) {
|
|
1228
|
+
return (after.length === before.length + added.length &&
|
|
1229
|
+
before.every((entry) => after.some((current) => isDeepStrictEqual(entry, current))) &&
|
|
1230
|
+
after.every((entry) => before.some((prior) => isDeepStrictEqual(prior, entry)) ||
|
|
1231
|
+
added.includes(object(entry)[key])));
|
|
1232
|
+
}
|
|
1233
|
+
/**
|
|
1234
|
+
* Execute one guarded additive PATCH or return accurate zero-write noop, then settle independently.
|
|
1235
|
+
* @param {unknown} value Portable additive intent.
|
|
1236
|
+
* @param {DataStreamDeployClients} clients Independent read/write boundaries.
|
|
1237
|
+
* @param {DataStreamDeployOptions} options Processing acknowledgement and exact local input guards.
|
|
1238
|
+
* @returns {Promise<object>} Independently confirmed operation and actual target identities.
|
|
1239
|
+
*/
|
|
1240
|
+
export async function updateNativeDataStream(value, clients, options) {
|
|
1241
|
+
const document = validateDataStreamDocument(value);
|
|
1242
|
+
const dataSpace = options.dataSpace ?? 'default';
|
|
1243
|
+
await options.verifyInput();
|
|
1244
|
+
const plan = await preflightNativeDataStreamUpdate(document, clients, dataSpace);
|
|
1245
|
+
if (plan.operation === 'update' && options.allowProcessing !== true)
|
|
1246
|
+
throw new DataStreamError('PROCESSING_ACKNOWLEDGEMENT_REQUIRED');
|
|
1247
|
+
await options.verifyInput();
|
|
1248
|
+
const fresh = await preflightNativeDataStreamUpdate(document, clients, dataSpace);
|
|
1249
|
+
if (!sameStreamSnapshot(plan, fresh))
|
|
1250
|
+
throw explainMismatch(new DataStreamError('STREAM_BINDING_DRIFT'), 'pre-dispatch plan recheck', document.identity.apiName, plan, fresh);
|
|
1251
|
+
await options.verifyInput();
|
|
1252
|
+
const apiName = plan.binding.target.name;
|
|
1253
|
+
if (plan.operation === 'noop')
|
|
1254
|
+
return {
|
|
1255
|
+
operation: 'noop',
|
|
1256
|
+
targetApiName: apiName,
|
|
1257
|
+
targetDloName: plan.target.spec.newDlo.name,
|
|
1258
|
+
document: plan.target,
|
|
1259
|
+
notices: [],
|
|
1260
|
+
};
|
|
1261
|
+
const now = options.now ?? Date.now;
|
|
1262
|
+
const pause = options.wait ?? wait;
|
|
1263
|
+
const deadline = now() + 15 * 60 * 1000;
|
|
1264
|
+
const targetId = plan.binding.target.recordId;
|
|
1265
|
+
// Salesforce ignores a label in a stream PATCH; the Metadata API update of the stream
|
|
1266
|
+
// definition changes it (live, API 68). It is sent first, while the stream is ACTIVE.
|
|
1267
|
+
const relabel = plan.labelChange ? clients.streams.relabel : undefined;
|
|
1268
|
+
if (relabel)
|
|
1269
|
+
await relabel(apiName, plan.document.spec.label);
|
|
1270
|
+
if (plan.formulaChanges.length === 0 && (!plan.labelChange || relabel)) {
|
|
1271
|
+
if (plan.additions.length > 0)
|
|
1272
|
+
await clients.streams.update(targetId, plan.patch);
|
|
1273
|
+
}
|
|
1274
|
+
else
|
|
1275
|
+
await clients.streams.patch(targetId, ingestDataStreamPatchBody(plan, !relabel));
|
|
1276
|
+
let labelReads = 0;
|
|
1277
|
+
// Targets whose existing field or mapping the PATCH changes or removes, and new targets.
|
|
1278
|
+
const existingTargets = new Set([
|
|
1279
|
+
...object(plan.binding.target.dataLakeObjectInfo).fields.map((entry) => object(entry).name),
|
|
1280
|
+
...plan.binding.target.mappings.map((entry) => object(entry).targetFieldName),
|
|
1281
|
+
]);
|
|
1282
|
+
const changed = new Set([...plan.additions, ...plan.formulaChanges]
|
|
1283
|
+
.map((entry) => entry.targetFieldName)
|
|
1284
|
+
.filter((target) => existingTargets.has(target)));
|
|
1285
|
+
const newAdditions = plan.additions.filter((entry) => !changed.has(entry.targetFieldName));
|
|
1286
|
+
const newFormulas = plan.formulaChanges.filter((entry) => !changed.has(entry.targetFieldName));
|
|
1287
|
+
// A removal leaves the DLO field in place (Salesforce drops only the mapping), so the fields
|
|
1288
|
+
// of removal targets stay protected; only added and changed targets may change their field.
|
|
1289
|
+
const fieldChanged = new Set([
|
|
1290
|
+
...plan.additions,
|
|
1291
|
+
...plan.formulaChanges.filter((entry) => entry.transformationFormula !== ''),
|
|
1292
|
+
]
|
|
1293
|
+
.map((entry) => entry.targetFieldName)
|
|
1294
|
+
.filter((target) => changed.has(target)));
|
|
1295
|
+
const unchanged = (entries, key, suffix = '', targets = changed) => entries.filter((entry) => ![...targets].some((target) => `${target}${suffix}` === object(entry)[key]));
|
|
1296
|
+
for (let attempt = 0; attempt < 20 && now() < deadline; attempt++) {
|
|
1297
|
+
try {
|
|
1298
|
+
const actual = await retrieveNativeDataStream(apiName, clients, dataSpace, true);
|
|
1299
|
+
const binding = await resolveDataStreamBinding({
|
|
1300
|
+
apiName,
|
|
1301
|
+
connectionName: plan.document.spec.connectionName,
|
|
1302
|
+
event: plan.document.spec.event,
|
|
1303
|
+
primaryKey: plan.document.spec.newDlo.primaryKey.name,
|
|
1304
|
+
}, clients, true);
|
|
1305
|
+
const raw = binding.target;
|
|
1306
|
+
if (!raw ||
|
|
1307
|
+
binding.connectionId !== plan.binding.connectionId ||
|
|
1308
|
+
!isDeepStrictEqual(binding.sourceFields, plan.binding.sourceFields))
|
|
1309
|
+
throw explainMismatch(new DataStreamError('STREAM_BINDING_DRIFT'), 'update connection/source preservation', apiName, {
|
|
1310
|
+
connectionId: plan.binding.connectionId,
|
|
1311
|
+
sourceFields: plan.binding.sourceFields,
|
|
1312
|
+
target: true,
|
|
1313
|
+
}, {
|
|
1314
|
+
connectionId: binding.connectionId,
|
|
1315
|
+
sourceFields: binding.sourceFields,
|
|
1316
|
+
target: Boolean(raw),
|
|
1317
|
+
});
|
|
1318
|
+
const old = plan.binding.target;
|
|
1319
|
+
const dlo = await readUpdateStreamDlo(actual, clients);
|
|
1320
|
+
const oldInfo = object(old.dataLakeObjectInfo);
|
|
1321
|
+
const info = object(raw.dataLakeObjectInfo);
|
|
1322
|
+
const added = [
|
|
1323
|
+
...newAdditions.map((entry) => entry.targetFieldName),
|
|
1324
|
+
...newFormulas.map((entry) => entry.targetFieldName),
|
|
1325
|
+
];
|
|
1326
|
+
const mutable = [
|
|
1327
|
+
'status',
|
|
1328
|
+
'mappings',
|
|
1329
|
+
'dataLakeObjectInfo',
|
|
1330
|
+
...(plan.labelChange ? ['label'] : []),
|
|
1331
|
+
];
|
|
1332
|
+
if (!isDeepStrictEqual(streamProtected(old, mutable), streamProtected(raw, mutable)) ||
|
|
1333
|
+
!isDeepStrictEqual(streamProtected(oldInfo, ['status', 'fields', 'dataLakeFieldInfoRepresentation']), streamProtected(info, ['status', 'fields', 'dataLakeFieldInfoRepresentation'])) ||
|
|
1334
|
+
!isDeepStrictEqual(streamProtected(plan.dlo, ['status', 'fields', 'dataLakeFieldInfoRepresentation']), streamProtected(dlo, ['status', 'fields', 'dataLakeFieldInfoRepresentation'])))
|
|
1335
|
+
throw new DataStreamError('STREAM_UPDATE_PRESERVATION_FAILED');
|
|
1336
|
+
const byId = inspectNativeDataStream(await clients.streams.get(raw.recordId));
|
|
1337
|
+
if (!sameStreamSnapshot(byId, raw, true) ||
|
|
1338
|
+
!sameStreamMappings(actual.observed?.mappings, raw.mappings) ||
|
|
1339
|
+
actual.identity.apiName !== raw.name ||
|
|
1340
|
+
actual.spec.newDlo.name !== info.name)
|
|
1341
|
+
throw explainMismatch(new DataStreamError('STREAM_BINDING_DRIFT'), 'update independent readback agreement', apiName, { target: raw, mappings: actual.observed?.mappings, name: raw.name, apiName: info.name }, {
|
|
1342
|
+
target: byId,
|
|
1343
|
+
mappings: raw.mappings,
|
|
1344
|
+
name: actual.identity.apiName,
|
|
1345
|
+
apiName: actual.spec.newDlo.name,
|
|
1346
|
+
});
|
|
1347
|
+
const newMappings = raw.mappings;
|
|
1348
|
+
const newFields = info.fields;
|
|
1349
|
+
if (!unchanged(old.mappings, 'targetFieldName').every((entry) => newMappings.some((current) => isDeepStrictEqual(entry, current))) ||
|
|
1350
|
+
!unchanged(oldInfo.fields, 'name', '', fieldChanged).every((entry) => newFields.some((current) => isDeepStrictEqual(entry, current))) ||
|
|
1351
|
+
!unchanged(plan.dlo.fields, 'name', '__c', fieldChanged).every((entry) => dlo.fields.some((current) => isDeepStrictEqual(entry, current))))
|
|
1352
|
+
throw new DataStreamError('STREAM_UPDATE_PRESERVATION_FAILED');
|
|
1353
|
+
const mappingsReady = plan.additions.every((entry) => newMappings.some((value) => {
|
|
1354
|
+
const mapping = object(value);
|
|
1355
|
+
const source = plan.binding.sourceFields.find((field) => field.label === entry.sourceFieldLabel);
|
|
1356
|
+
return (mapping.targetFieldName === entry.targetFieldName &&
|
|
1357
|
+
((typeof mapping.formula === 'string' &&
|
|
1358
|
+
mapping.formula === entry.sourceFieldLabel &&
|
|
1359
|
+
mapping.sourceFieldName === undefined) ||
|
|
1360
|
+
(mapping.formula === undefined && mapping.sourceFieldName === source?.name)));
|
|
1361
|
+
})) &&
|
|
1362
|
+
plan.formulaChanges.every((entry) => {
|
|
1363
|
+
const onTarget = newMappings
|
|
1364
|
+
.map((value) => object(value))
|
|
1365
|
+
.filter((mapping) => mapping.targetFieldName === entry.targetFieldName);
|
|
1366
|
+
// A removal is applied only when nothing maps onto the target any more: a plain-shaped
|
|
1367
|
+
// additive mapping has no formula, so a missing formula alone proves nothing.
|
|
1368
|
+
if (entry.transformationFormula === '')
|
|
1369
|
+
return onTarget.every((mapping) => mapping.formula === undefined && mapping.sourceFieldName === undefined);
|
|
1370
|
+
const formula = onTarget[0]?.formula;
|
|
1371
|
+
return typeof formula === 'string' && formula === entry.transformationFormula;
|
|
1372
|
+
});
|
|
1373
|
+
const fieldsReady = plan.additions.every((entry) => newFields.some((value) => {
|
|
1374
|
+
const field = object(value);
|
|
1375
|
+
return (field.name === entry.targetFieldName &&
|
|
1376
|
+
sameEnum(field.dataType, entry.targetFieldReturntype) &&
|
|
1377
|
+
(changed.has(entry.targetFieldName) ||
|
|
1378
|
+
(field.label === entry.targetFieldName && field.isPrimaryKey === false)));
|
|
1379
|
+
})) &&
|
|
1380
|
+
newFormulas.every((entry) => newFields.some((value) => {
|
|
1381
|
+
const field = object(value);
|
|
1382
|
+
return field.name === entry.targetFieldName && field.isPrimaryKey === false;
|
|
1383
|
+
}));
|
|
1384
|
+
if (mappingsReady && fieldsReady) {
|
|
1385
|
+
if (!streamEntriesPreserved(unchanged(old.mappings, 'targetFieldName'), unchanged(newMappings, 'targetFieldName'), 'targetFieldName', added) ||
|
|
1386
|
+
!streamEntriesPreserved(unchanged(oldInfo.fields, 'name', '', fieldChanged), unchanged(newFields, 'name', '', fieldChanged), 'name', added) ||
|
|
1387
|
+
((oldInfo.dataLakeFieldInfoRepresentation !== undefined ||
|
|
1388
|
+
info.dataLakeFieldInfoRepresentation !== undefined) &&
|
|
1389
|
+
(!Array.isArray(oldInfo.dataLakeFieldInfoRepresentation) ||
|
|
1390
|
+
!Array.isArray(info.dataLakeFieldInfoRepresentation) ||
|
|
1391
|
+
!streamEntriesPreserved(unchanged(oldInfo.dataLakeFieldInfoRepresentation, 'name', '', fieldChanged), unchanged(info.dataLakeFieldInfoRepresentation, 'name', '', fieldChanged), 'name', added) ||
|
|
1392
|
+
!plan.additions.every((entry) => info.dataLakeFieldInfoRepresentation.some((field) => isDeepStrictEqual(field, newFields.find((value) => object(value).name === entry.targetFieldName)))))) ||
|
|
1393
|
+
!streamEntriesPreserved(unchanged(plan.dlo.fields, 'name', '__c', fieldChanged), unchanged(dlo.fields, 'name', '__c', fieldChanged), 'name', added.map((field) => `${field}__c`)) ||
|
|
1394
|
+
(plan.dlo.dataLakeFieldInfoRepresentation !== undefined &&
|
|
1395
|
+
(!Array.isArray(dlo.dataLakeFieldInfoRepresentation) ||
|
|
1396
|
+
!streamEntriesPreserved(unchanged(plan.dlo.dataLakeFieldInfoRepresentation, 'name', '__c', fieldChanged), unchanged(dlo.dataLakeFieldInfoRepresentation, 'name', '__c', fieldChanged), 'name', added.map((field) => `${field}__c`)))))
|
|
1397
|
+
throw new DataStreamError('STREAM_UPDATE_PRESERVATION_FAILED');
|
|
1398
|
+
if (raw.status === 'ACTIVE' &&
|
|
1399
|
+
dlo.status === 'ACTIVE' &&
|
|
1400
|
+
((oldInfo.status === undefined && info.status === undefined) ||
|
|
1401
|
+
info.status === 'ACTIVE') &&
|
|
1402
|
+
now() < deadline) {
|
|
1403
|
+
const labelKept = plan.labelChange && raw.label !== plan.document.spec.label;
|
|
1404
|
+
// A label set through the Metadata API may show a read later; wait up to 3 reads.
|
|
1405
|
+
if (!labelKept || ++labelReads >= 3) {
|
|
1406
|
+
await options.verifyInput();
|
|
1407
|
+
if (now() >= deadline)
|
|
1408
|
+
break;
|
|
1409
|
+
return {
|
|
1410
|
+
operation: 'update',
|
|
1411
|
+
targetApiName: apiName,
|
|
1412
|
+
targetDloName: actual.spec.newDlo.name,
|
|
1413
|
+
document: actual,
|
|
1414
|
+
notices: labelNotApplied(labelKept),
|
|
1415
|
+
};
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
if (!['ACTIVE', 'PROCESSING'].includes(raw.status) ||
|
|
1420
|
+
!['ACTIVE', 'PROCESSING'].includes(dlo.status) ||
|
|
1421
|
+
((oldInfo.status !== undefined || info.status !== undefined) &&
|
|
1422
|
+
!['ACTIVE', 'PROCESSING'].includes(info.status)))
|
|
1423
|
+
throw new DataStreamError('STREAM_UPDATE_SETTLEMENT_UNCONFIRMED');
|
|
1424
|
+
}
|
|
1425
|
+
catch (error) {
|
|
1426
|
+
if (!(error instanceof DataStreamError) || error.code !== 'STREAM_SNAPSHOT_PENDING')
|
|
1427
|
+
throw error;
|
|
1428
|
+
}
|
|
1429
|
+
if (attempt < 19 && now() < deadline)
|
|
1430
|
+
await pause(Math.min(5000, deadline - now()));
|
|
1431
|
+
}
|
|
1432
|
+
throw new DataStreamError('STREAM_UPDATE_SETTLEMENT_UNCONFIRMED');
|
|
1433
|
+
}
|
|
1434
|
+
/**
|
|
1435
|
+
* A stream read answered by Salesforce with HTTP 404: that stream does not exist.
|
|
1436
|
+
* @param {unknown} error Error of a native stream read.
|
|
1437
|
+
* @returns {boolean} True only for a genuine HTTP 404 of the stream route.
|
|
1438
|
+
*/
|
|
1439
|
+
export function isStreamNotFound(error) {
|
|
1440
|
+
return (error instanceof DataStreamHttpError &&
|
|
1441
|
+
error.status === 404 &&
|
|
1442
|
+
['REQUEST_FAILED', 'STREAM_HTTP_ERROR'].includes(error.code));
|
|
1443
|
+
}
|
|
1444
|
+
/**
|
|
1445
|
+
* Admit only the observed complete dependency response, never a transport-success shortcut.
|
|
1446
|
+
* @param {unknown} value Independent native dependency result.
|
|
1447
|
+
* @returns {Json} Complete dependency inventory with exact observed shape.
|
|
1448
|
+
*/
|
|
1449
|
+
function streamDeleteDependencies(value) {
|
|
1450
|
+
const result = object(value);
|
|
1451
|
+
if (Object.keys(result).some((key) => !['dependencies', 'totalSize'].includes(key)) ||
|
|
1452
|
+
!Array.isArray(result.dependencies) ||
|
|
1453
|
+
!Number.isSafeInteger(result.totalSize) ||
|
|
1454
|
+
result.totalSize !== result.dependencies.length)
|
|
1455
|
+
throw new DataStreamError('INCOMPLETE_STREAM_DEPENDENCIES');
|
|
1456
|
+
return structuredClone(result);
|
|
1457
|
+
}
|
|
1458
|
+
/**
|
|
1459
|
+
* Check the dependents of a stream and its DLO: only the DLO's own entry for this stream may be
|
|
1460
|
+
* listed. In a preview, `covered` admits dependents the same delete deletes earlier.
|
|
1461
|
+
* @param {Json} streamDependencies Stream dependents.
|
|
1462
|
+
* @param {Json} dloDependencies DLO dependents.
|
|
1463
|
+
* @param {object} target The stream.
|
|
1464
|
+
* @param {unknown} target.recordId Its record ID.
|
|
1465
|
+
* @param {unknown} target.label Its label (not compared: the entry may keep an older label).
|
|
1466
|
+
* @param {string} dloName Landing DLO name.
|
|
1467
|
+
* @param {Function} [covered] Preview only: a dependent the same delete deletes earlier.
|
|
1468
|
+
* @returns {Promise<StreamDependent[] | undefined>} Planned dependents, or undefined when none.
|
|
1469
|
+
*/
|
|
1470
|
+
async function checkStreamDeleteDependents(streamDependencies, dloDependencies, target, dloName, covered) {
|
|
1471
|
+
const dependencies = dloDependencies.dependencies;
|
|
1472
|
+
// The DLO's own entry for this stream, by record ID: after a label change through the
|
|
1473
|
+
// Metadata API the entry still carries the old label (live, API 68).
|
|
1474
|
+
const ownEntry = (value) => object(value).componentType === 'DataStream' &&
|
|
1475
|
+
object(value).id === target.recordId &&
|
|
1476
|
+
typeof object(value).name === 'string' &&
|
|
1477
|
+
!Object.keys(object(value)).some((key) => !['id', 'name', 'componentType'].includes(key));
|
|
1478
|
+
// Both modes refuse unknown/shared dependencies; cascade additionally has an explicit opt-in.
|
|
1479
|
+
if (streamDependencies.totalSize === 0 && dependencies.length === 1 && ownEntry(dependencies[0]))
|
|
1480
|
+
return undefined;
|
|
1481
|
+
// Preview only: dependents an earlier phase of the same delete plans to delete.
|
|
1482
|
+
if (covered === undefined || dependencies.filter((entry) => ownEntry(entry)).length > 1)
|
|
1483
|
+
throw new DataStreamError('STREAM_DELETE_DEPENDENCIES_BLOCKED');
|
|
1484
|
+
const planned = [];
|
|
1485
|
+
for (const entry of [
|
|
1486
|
+
...streamDependencies.dependencies,
|
|
1487
|
+
...dependencies.filter((value) => !ownEntry(value)),
|
|
1488
|
+
]) {
|
|
1489
|
+
const { componentType, name: dependent } = object(entry);
|
|
1490
|
+
if (typeof componentType !== 'string' ||
|
|
1491
|
+
typeof dependent !== 'string' ||
|
|
1492
|
+
!(await covered({ componentType, name: dependent }, dloName)))
|
|
1493
|
+
throw new DataStreamError('STREAM_DELETE_DEPENDENCIES_BLOCKED');
|
|
1494
|
+
planned.push({ componentType, name: dependent });
|
|
1495
|
+
}
|
|
1496
|
+
return planned;
|
|
1497
|
+
}
|
|
1498
|
+
/**
|
|
1499
|
+
* The refusal of a stream delete with --delete-dlo whose landing DLO belongs to other data spaces
|
|
1500
|
+
* than the run's: deleting the DLO would remove it from all of them.
|
|
1501
|
+
* @param {string} dloName Landing DLO.
|
|
1502
|
+
* @param {string} apiName Stream name.
|
|
1503
|
+
* @param {string} dataSpace The run's data space.
|
|
1504
|
+
* @returns {DataStreamError} DLO_DELETE_OTHER_DATASPACES with an explanation.
|
|
1505
|
+
*/
|
|
1506
|
+
function otherSpacesRefusal(dloName, apiName, dataSpace) {
|
|
1507
|
+
return explainInput(new DataStreamError('DLO_DELETE_OTHER_DATASPACES'), `The landing DLO ${dloName} of ${apiName} does not belong to the data space ${dataSpace} alone, and deleting it with the stream would remove it from every data space. Delete the stream without --delete-dlo, or take the DLO out of the other data spaces first.`);
|
|
1508
|
+
}
|
|
1509
|
+
/**
|
|
1510
|
+
* Plan the by-name delete of a stream the plugin cannot model, from its raw GET only.
|
|
1511
|
+
* @param {string} apiName Exact selected stream name.
|
|
1512
|
+
* @param {DataStreamReadClients} clients Native readers.
|
|
1513
|
+
* @param {object} options Cascade choice, survivors and preview planning.
|
|
1514
|
+
* @param {boolean} options.deleteDlo Whether the landing DLO is deleted too.
|
|
1515
|
+
* @param {readonly string[]} options.survivorDloNames Must be empty on this path.
|
|
1516
|
+
* @param {Function} [options.covered] Preview only: a dependent the same delete deletes earlier.
|
|
1517
|
+
* @returns {Promise<UnmodelledDataStreamDeletePlan>} Guarded plan.
|
|
1518
|
+
*/
|
|
1519
|
+
async function preflightUnmodelledDataStreamDelete(apiName, clients, options) {
|
|
1520
|
+
const dataSpace = options.dataSpace ?? 'default';
|
|
1521
|
+
const raw = object(await clients.streams.get(apiName));
|
|
1522
|
+
if (raw.name !== apiName)
|
|
1523
|
+
throw new DataStreamError('STREAM_TARGET_MISMATCH');
|
|
1524
|
+
if (typeof raw.recordId !== 'string' || !recordId.test(raw.recordId))
|
|
1525
|
+
throw new DataStreamError('INVALID_STREAM_TARGET_ID');
|
|
1526
|
+
const info = raw.dataLakeObjectInfo && typeof raw.dataLakeObjectInfo === 'object'
|
|
1527
|
+
? raw.dataLakeObjectInfo
|
|
1528
|
+
: {};
|
|
1529
|
+
// The stream's data spaces (from the GET, or from its DLO when the GET names none) must hold
|
|
1530
|
+
// the run's.
|
|
1531
|
+
await streamSpaces(raw, apiName, dataSpace, clients);
|
|
1532
|
+
if (raw.status !== 'ACTIVE')
|
|
1533
|
+
throw new DataStreamError('STREAM_NOT_READY');
|
|
1534
|
+
if (typeof info.name !== 'string' || !/^[A-Za-z][A-Za-z0-9_]*__dll$/u.test(info.name))
|
|
1535
|
+
throw new DataStreamError('INVALID_STREAM_DLO');
|
|
1536
|
+
const dloName = info.name;
|
|
1537
|
+
if (options.survivorDloNames.length > 0)
|
|
1538
|
+
throw new DataStreamError('INVALID_STREAM_DELETE_SURVIVOR');
|
|
1539
|
+
// Deleting the landing DLO removes it from every data space it belongs to: it must belong to
|
|
1540
|
+
// the run's data space alone, read from the DLO itself.
|
|
1541
|
+
if (options.deleteDlo) {
|
|
1542
|
+
const wrapper = object(await clients.dlo.get(dloName));
|
|
1543
|
+
// A wrapped read must hold exactly the one DLO; another count says nothing about its spaces.
|
|
1544
|
+
if (Array.isArray(wrapper.dataLakeObjects) && wrapper.dataLakeObjects.length !== 1)
|
|
1545
|
+
throw new DataStreamError('INVALID_STREAM_DLO');
|
|
1546
|
+
const dlo = Array.isArray(wrapper.dataLakeObjects)
|
|
1547
|
+
? object(wrapper.dataLakeObjects[0])
|
|
1548
|
+
: wrapper;
|
|
1549
|
+
const memberships = Array.isArray(dlo.dataSpaceInfo)
|
|
1550
|
+
? dlo.dataSpaceInfo.map((entry) => object(entry).name)
|
|
1551
|
+
: [];
|
|
1552
|
+
if (memberships.length !== 1 || memberships[0] !== dataSpace)
|
|
1553
|
+
throw otherSpacesRefusal(dloName, apiName, dataSpace);
|
|
1554
|
+
}
|
|
1555
|
+
const streamDependencies = streamDeleteDependencies(await clients.dependencies.stream(raw.recordId));
|
|
1556
|
+
const dloDependencies = streamDeleteDependencies(await clients.dependencies.dlo(dloName));
|
|
1557
|
+
const plannedDependencies = await checkStreamDeleteDependents(streamDependencies, dloDependencies, { recordId: raw.recordId, label: raw.label }, dloName, options.covered);
|
|
1558
|
+
return {
|
|
1559
|
+
operation: 'delete',
|
|
1560
|
+
unmodelled: true,
|
|
1561
|
+
apiName,
|
|
1562
|
+
deleteDlo: options.deleteDlo,
|
|
1563
|
+
recordId: raw.recordId,
|
|
1564
|
+
label: raw.label,
|
|
1565
|
+
dlo: { name: dloName },
|
|
1566
|
+
streamDependencies,
|
|
1567
|
+
dloDependencies,
|
|
1568
|
+
...(plannedDependencies === undefined ? {} : { plannedDependencies }),
|
|
1569
|
+
};
|
|
1570
|
+
}
|
|
1571
|
+
/**
|
|
1572
|
+
* Snapshot exact selected target, source, linked parent and explicitly protected survivors.
|
|
1573
|
+
* @param {string} apiName Exact selected target developer name, never a portable proposal.
|
|
1574
|
+
* @param {DataStreamReadClients} clients Independent native readers.
|
|
1575
|
+
* @param {object} options Explicit cascade choice and protected unrelated parent names.
|
|
1576
|
+
* @param {boolean} options.deleteDlo Explicit acknowledgement of linked-parent deletion.
|
|
1577
|
+
* @param {readonly string[]} options.survivorDloNames Unselected parents to preserve independently.
|
|
1578
|
+
* @param {Function} [options.covered] Preview only: a dependent the same delete deletes earlier.
|
|
1579
|
+
* @returns {Promise<DataStreamDeletePlan>} Private guarded plan, not dispatch authority.
|
|
1580
|
+
*/
|
|
1581
|
+
export async function preflightNativeDataStreamDelete(apiName, clients, options) {
|
|
1582
|
+
if (!name.test(apiName) || typeof options.deleteDlo !== 'boolean')
|
|
1583
|
+
throw new DataStreamError('INVALID_STREAM_DELETE_SELECTION');
|
|
1584
|
+
const dataSpace = options.dataSpace ?? 'default';
|
|
1585
|
+
let document;
|
|
1586
|
+
try {
|
|
1587
|
+
document = await retrieveNativeDataStream(apiName, clients, dataSpace);
|
|
1588
|
+
}
|
|
1589
|
+
catch (error) {
|
|
1590
|
+
// A stream the plugin cannot model (another connector, settings it does not know) is
|
|
1591
|
+
// deleted by name from its raw GET. Read failures, absence and identity stay refusals.
|
|
1592
|
+
if (verbatimReason(error) === undefined)
|
|
1593
|
+
throw error;
|
|
1594
|
+
return preflightUnmodelledDataStreamDelete(apiName, clients, options);
|
|
1595
|
+
}
|
|
1596
|
+
const binding = await resolveDataStreamBinding({
|
|
1597
|
+
...document.identity,
|
|
1598
|
+
connectionName: document.spec.connectionName,
|
|
1599
|
+
event: document.spec.event,
|
|
1600
|
+
primaryKey: document.spec.newDlo.primaryKey.name,
|
|
1601
|
+
}, clients);
|
|
1602
|
+
const target = binding.target;
|
|
1603
|
+
let confirmation;
|
|
1604
|
+
if (!target ||
|
|
1605
|
+
target.name !== apiName ||
|
|
1606
|
+
!sameStreamSnapshot(target, (confirmation = inspectNativeDataStream(await clients.streams.get(target.recordId)))))
|
|
1607
|
+
throw explainMismatch(new DataStreamError('STREAM_BINDING_DRIFT'), 'delete stream identity/readback agreement', apiName, target?.name === apiName ? target : { name: apiName }, confirmation ?? target);
|
|
1608
|
+
const dlo = await readUpdateStreamDlo(document, clients);
|
|
1609
|
+
// Deleting the landing DLO removes it from every data space it belongs to.
|
|
1610
|
+
if (options.deleteDlo &&
|
|
1611
|
+
(document.c__dataSpace.length !== 1 || document.c__dataSpace[0] !== dataSpace))
|
|
1612
|
+
throw otherSpacesRefusal(String(dlo.name), apiName, dataSpace);
|
|
1613
|
+
if (target.status !== 'ACTIVE' || dlo.status !== 'ACTIVE')
|
|
1614
|
+
throw new DataStreamError('STREAM_NOT_READY');
|
|
1615
|
+
const parentById = object(await clients.dlo.get(dlo.id));
|
|
1616
|
+
if (!isDeepStrictEqual(parentById.dataLakeObjects, [dlo]))
|
|
1617
|
+
throw explainMismatch(new DataStreamError('STREAM_DLO_DRIFT'), 'delete parent name/ID agreement', dlo.name, [dlo], parentById.dataLakeObjects, 'parents');
|
|
1618
|
+
const streamDependencies = streamDeleteDependencies(await clients.dependencies.stream(target.recordId));
|
|
1619
|
+
const dloDependencies = streamDeleteDependencies(await clients.dependencies.dlo(dlo.name));
|
|
1620
|
+
const plannedDependencies = await checkStreamDeleteDependents(streamDependencies, dloDependencies, { recordId: target.recordId, label: target.label }, String(dlo.name), options.covered);
|
|
1621
|
+
const survivors = [];
|
|
1622
|
+
const seen = new Set();
|
|
1623
|
+
for (const survivor of options.survivorDloNames) {
|
|
1624
|
+
if (!/^[A-Za-z][A-Za-z0-9_]*__dll$/u.test(survivor) ||
|
|
1625
|
+
survivor.toLowerCase() === String(dlo.name).toLowerCase() ||
|
|
1626
|
+
seen.has(survivor.toLowerCase()))
|
|
1627
|
+
throw new DataStreamError('INVALID_STREAM_DELETE_SURVIVOR');
|
|
1628
|
+
seen.add(survivor.toLowerCase());
|
|
1629
|
+
const wrapper = object(await clients.dlo.get(survivor));
|
|
1630
|
+
if (!Array.isArray(wrapper.dataLakeObjects) || wrapper.dataLakeObjects.length !== 1)
|
|
1631
|
+
throw new DataStreamError('INVALID_STREAM_DELETE_SURVIVOR');
|
|
1632
|
+
const value = object(wrapper.dataLakeObjects[0]);
|
|
1633
|
+
if (value.name !== survivor ||
|
|
1634
|
+
!recordId.test(String(value.id)) ||
|
|
1635
|
+
value.id === dlo.id ||
|
|
1636
|
+
value.status !== 'ACTIVE')
|
|
1637
|
+
throw new DataStreamError('INVALID_STREAM_DELETE_SURVIVOR');
|
|
1638
|
+
survivors.push(structuredClone(value));
|
|
1639
|
+
}
|
|
1640
|
+
return {
|
|
1641
|
+
operation: 'delete',
|
|
1642
|
+
apiName,
|
|
1643
|
+
deleteDlo: options.deleteDlo,
|
|
1644
|
+
document,
|
|
1645
|
+
binding,
|
|
1646
|
+
dlo,
|
|
1647
|
+
streamDependencies,
|
|
1648
|
+
dloDependencies,
|
|
1649
|
+
survivors,
|
|
1650
|
+
...(plannedDependencies === undefined ? {} : { plannedDependencies }),
|
|
1651
|
+
};
|
|
1652
|
+
}
|
|
1653
|
+
/**
|
|
1654
|
+
* Observe typed native stream absence without interpreting arbitrary errors as missing state.
|
|
1655
|
+
* While deletion runs the stream may still read with any status; only its identity is checked.
|
|
1656
|
+
* @param {string} apiName Previously proven exact selected name.
|
|
1657
|
+
* @param {string} streamId Record ID of the deleted stream.
|
|
1658
|
+
* @param {DataStreamReadClients} clients Native stream reader.
|
|
1659
|
+
* @returns {Promise<boolean>} True only for genuine HTTP404; existing DELETING remains pending.
|
|
1660
|
+
*/
|
|
1661
|
+
async function deletedStreamAbsent(apiName, streamId, clients) {
|
|
1662
|
+
try {
|
|
1663
|
+
const current = object(await clients.streams.get(apiName));
|
|
1664
|
+
if (current.name !== apiName || current.recordId !== streamId)
|
|
1665
|
+
throw explainMismatch(new DataStreamError('STREAM_DELETE_GUARD_DRIFT'), 'delete remaining stream identity check', apiName, { name: apiName, recordId: streamId }, { name: current.name, recordId: current.recordId });
|
|
1666
|
+
return false;
|
|
1667
|
+
}
|
|
1668
|
+
catch (error) {
|
|
1669
|
+
if (isStreamNotFound(error))
|
|
1670
|
+
return true;
|
|
1671
|
+
throw error;
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
/**
|
|
1675
|
+
* Delete a stream the plugin cannot model by its record ID once both guarded plans agree, then
|
|
1676
|
+
* wait (bounded) until the stream reads 404 and, with --delete-dlo, its DLO too; without it the
|
|
1677
|
+
* DLO must no longer list dependents.
|
|
1678
|
+
* @param {DataStreamDeletePlan | UnmodelledDataStreamDeletePlan} plan First plan.
|
|
1679
|
+
* @param {DataStreamDeletePlan | UnmodelledDataStreamDeletePlan} fresh Plan read right before the DELETE.
|
|
1680
|
+
* @param {DataStreamDeleteClients} clients Native DELETE and readers.
|
|
1681
|
+
* @param {DataStreamDeleteOptions} options Cascade, input guard and read bounds.
|
|
1682
|
+
* @returns {Promise<object>} Confirmed deletion.
|
|
1683
|
+
*/
|
|
1684
|
+
async function deleteUnmodelledDataStream(plan, fresh, clients, options) {
|
|
1685
|
+
if (!('unmodelled' in plan) || !isDeepStrictEqual(plan, fresh))
|
|
1686
|
+
throw explainMismatch(new DataStreamError('STREAM_DELETE_GUARD_DRIFT'), 'delete pre-dispatch guard recheck', plan.apiName, plan, fresh);
|
|
1687
|
+
await options.verifyInput();
|
|
1688
|
+
const now = options.now ?? Date.now;
|
|
1689
|
+
const pause = options.wait ?? wait;
|
|
1690
|
+
const deadline = now() + 15 * 60 * 1000;
|
|
1691
|
+
const dloName = plan.dlo.name;
|
|
1692
|
+
await clients.streams.delete(plan.recordId, plan.deleteDlo);
|
|
1693
|
+
for (let attempt = 0; attempt < 20 && now() < deadline; attempt++) {
|
|
1694
|
+
let absent = false;
|
|
1695
|
+
try {
|
|
1696
|
+
const current = object(await clients.streams.get(plan.apiName));
|
|
1697
|
+
if (current.name !== plan.apiName || current.recordId !== plan.recordId)
|
|
1698
|
+
throw new DataStreamError('STREAM_DELETE_GUARD_DRIFT');
|
|
1699
|
+
}
|
|
1700
|
+
catch (error) {
|
|
1701
|
+
if (!isStreamNotFound(error))
|
|
1702
|
+
throw error;
|
|
1703
|
+
absent = true;
|
|
1704
|
+
}
|
|
1705
|
+
// With --delete-dlo the DLO can still read for a while after its stream answers 404.
|
|
1706
|
+
if (absent && (!plan.deleteDlo || (await streamDloAbsent(dloName, clients)))) {
|
|
1707
|
+
if (!plan.deleteDlo) {
|
|
1708
|
+
const released = streamDeleteDependencies(await clients.dependencies.dlo(dloName));
|
|
1709
|
+
if (released.totalSize !== 0)
|
|
1710
|
+
throw new DataStreamError('STREAM_DELETE_DEPENDENCIES_BLOCKED');
|
|
1711
|
+
}
|
|
1712
|
+
await options.verifyInput();
|
|
1713
|
+
return { operation: 'delete', targetApiName: plan.apiName, targetDloName: dloName };
|
|
1714
|
+
}
|
|
1715
|
+
if (attempt < 19 && now() < deadline)
|
|
1716
|
+
await pause(Math.min(5000, deadline - now()));
|
|
1717
|
+
}
|
|
1718
|
+
throw new DataStreamError('STREAM_DELETE_SETTLEMENT_UNCONFIRMED');
|
|
1719
|
+
}
|
|
1720
|
+
/**
|
|
1721
|
+
* Delete once after fresh complete guards; HTTP204 never substitutes for independent settlement.
|
|
1722
|
+
* @param {string} apiName Exact target developer name.
|
|
1723
|
+
* @param {DataStreamDeleteClients} clients Native single-attempt DELETE and independent readers.
|
|
1724
|
+
* @param {DataStreamDeleteOptions} options Cascade acknowledgement, immutable input and read bounds.
|
|
1725
|
+
* @returns {Promise<object>} Confirmed deletion only after binding release and parent/survivor checks.
|
|
1726
|
+
*/
|
|
1727
|
+
export async function deleteNativeDataStream(apiName, clients, options) {
|
|
1728
|
+
// Freeze caller-owned selection before the first asynchronous boundary.
|
|
1729
|
+
const selection = Object.freeze({
|
|
1730
|
+
deleteDlo: options.deleteDlo,
|
|
1731
|
+
survivorDloNames: Object.freeze([...options.survivorDloNames]),
|
|
1732
|
+
dataSpace: options.dataSpace ?? 'default',
|
|
1733
|
+
});
|
|
1734
|
+
await options.verifyInput();
|
|
1735
|
+
const plan = await preflightNativeDataStreamDelete(apiName, clients, selection);
|
|
1736
|
+
await options.verifyInput();
|
|
1737
|
+
const fresh = await preflightNativeDataStreamDelete(apiName, clients, selection);
|
|
1738
|
+
if ('unmodelled' in plan || 'unmodelled' in fresh)
|
|
1739
|
+
return deleteUnmodelledDataStream(plan, fresh, clients, options);
|
|
1740
|
+
if (!sameStreamSnapshot(plan, fresh))
|
|
1741
|
+
throw explainMismatch(new DataStreamError('STREAM_DELETE_GUARD_DRIFT'), 'delete pre-dispatch guard recheck', apiName, plan, fresh);
|
|
1742
|
+
await options.verifyInput();
|
|
1743
|
+
const now = options.now ?? Date.now;
|
|
1744
|
+
const pause = options.wait ?? wait;
|
|
1745
|
+
const deadline = now() + 15 * 60 * 1000;
|
|
1746
|
+
const streamId = plan.binding.target.recordId;
|
|
1747
|
+
await clients.streams.delete(streamId, selection.deleteDlo);
|
|
1748
|
+
for (let attempt = 0; attempt < 20 && now() < deadline; attempt++) {
|
|
1749
|
+
const next = async () => {
|
|
1750
|
+
if (attempt < 19 && now() < deadline)
|
|
1751
|
+
await pause(Math.min(5000, deadline - now()));
|
|
1752
|
+
};
|
|
1753
|
+
// While the stream still reads (any status, e.g. DELETING) its deletion is running: wait.
|
|
1754
|
+
if (!(await deletedStreamAbsent(apiName, streamId, clients))) {
|
|
1755
|
+
await next();
|
|
1756
|
+
continue;
|
|
1757
|
+
}
|
|
1758
|
+
let binding;
|
|
1759
|
+
try {
|
|
1760
|
+
binding = await resolveDataStreamBinding({
|
|
1761
|
+
...plan.document.identity,
|
|
1762
|
+
connectionName: plan.binding.connectionName,
|
|
1763
|
+
event: plan.binding.event,
|
|
1764
|
+
primaryKey: plan.document.spec.newDlo.primaryKey.name,
|
|
1765
|
+
}, clients);
|
|
1766
|
+
}
|
|
1767
|
+
catch (error) {
|
|
1768
|
+
// After its 404 the list can still name the stream, list and detail can disagree, the
|
|
1769
|
+
// list's total can still count it (INCOMPLETE_STREAM_LIST, seen live after a successful
|
|
1770
|
+
// --delete-dlo delete) and the event can still be In Use for a while: pending, never drift
|
|
1771
|
+
// of the deleted stream. The DELETE is never sent again.
|
|
1772
|
+
if (!isStreamNotFound(error) &&
|
|
1773
|
+
!(error instanceof DataStreamError &&
|
|
1774
|
+
[
|
|
1775
|
+
'INCOMPLETE_STREAM_LIST',
|
|
1776
|
+
'STREAM_BINDING_DRIFT',
|
|
1777
|
+
'STREAM_SNAPSHOT_PENDING',
|
|
1778
|
+
'STREAM_TARGET_MISMATCH',
|
|
1779
|
+
'UNSUPPORTED_STREAM_READ',
|
|
1780
|
+
].includes(error.code)))
|
|
1781
|
+
throw error;
|
|
1782
|
+
await next();
|
|
1783
|
+
continue;
|
|
1784
|
+
}
|
|
1785
|
+
if (binding.connectionId !== plan.binding.connectionId ||
|
|
1786
|
+
!isDeepStrictEqual(binding.sourceFields, plan.binding.sourceFields))
|
|
1787
|
+
throw explainMismatch(new DataStreamError('STREAM_DELETE_GUARD_DRIFT'), 'delete connection/source preservation', apiName, { connectionId: plan.binding.connectionId, sourceFields: plan.binding.sourceFields }, { connectionId: binding.connectionId, sourceFields: binding.sourceFields });
|
|
1788
|
+
for (const survivor of plan.survivors) {
|
|
1789
|
+
for (const identity of [survivor.name, survivor.id]) {
|
|
1790
|
+
const wrapper = object(await clients.dlo.get(identity));
|
|
1791
|
+
if (!isDeepStrictEqual(wrapper.dataLakeObjects, [survivor]))
|
|
1792
|
+
throw explainMismatch(new DataStreamError('STREAM_DELETE_SURVIVOR_DRIFT'), 'delete survivor preservation', survivor.name, [survivor], wrapper.dataLakeObjects, 'parents');
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
if (!binding.target && binding.inventory.length === 0 && binding.availability === 'Available') {
|
|
1796
|
+
let settled = true;
|
|
1797
|
+
if (selection.deleteDlo) {
|
|
1798
|
+
// The DLO can still read (e.g. DELETING) after its stream answers 404: pending.
|
|
1799
|
+
settled =
|
|
1800
|
+
(await streamDloAbsent(plan.dlo.name, clients)) &&
|
|
1801
|
+
(await streamDloAbsent(plan.dlo.id, clients));
|
|
1802
|
+
}
|
|
1803
|
+
else {
|
|
1804
|
+
for (const identity of [plan.dlo.name, plan.dlo.id]) {
|
|
1805
|
+
const wrapper = object(await clients.dlo.get(identity));
|
|
1806
|
+
if (!isDeepStrictEqual(wrapper.dataLakeObjects, [plan.dlo]))
|
|
1807
|
+
throw explainMismatch(new DataStreamError('STREAM_DELETE_PARENT_DRIFT'), 'delete retained parent preservation', plan.dlo.name, [plan.dlo], wrapper.dataLakeObjects, 'parents');
|
|
1808
|
+
}
|
|
1809
|
+
const released = streamDeleteDependencies(await clients.dependencies.dlo(plan.dlo.name));
|
|
1810
|
+
if (released.totalSize !== 0)
|
|
1811
|
+
throw new DataStreamError('STREAM_DELETE_DEPENDENCIES_BLOCKED');
|
|
1812
|
+
}
|
|
1813
|
+
if (settled) {
|
|
1814
|
+
await options.verifyInput();
|
|
1815
|
+
if (now() >= deadline)
|
|
1816
|
+
break;
|
|
1817
|
+
return {
|
|
1818
|
+
operation: 'delete',
|
|
1819
|
+
targetApiName: apiName,
|
|
1820
|
+
targetDloName: plan.dlo.name,
|
|
1821
|
+
};
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
if (binding.target &&
|
|
1825
|
+
(binding.target.name !== apiName || binding.target.recordId !== plan.binding.target.recordId))
|
|
1826
|
+
throw explainMismatch(new DataStreamError('STREAM_DELETE_GUARD_DRIFT'), 'delete remaining stream identity check', apiName, { name: apiName, recordId: plan.binding.target.recordId }, { name: binding.target.name, recordId: binding.target.recordId });
|
|
1827
|
+
await next();
|
|
1828
|
+
}
|
|
1829
|
+
throw new DataStreamError('STREAM_DELETE_SETTLEMENT_UNCONFIRMED');
|
|
1830
|
+
}
|