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.
Files changed (256) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +589 -2
  3. package/bin/run.js +4 -0
  4. package/docs/artifact-format.md +527 -0
  5. package/docs/capabilities.md +1208 -0
  6. package/docs/git-workflow.md +160 -0
  7. package/docs/handover.md +10446 -0
  8. package/lib/api.d.ts +7 -0
  9. package/lib/api.js +7 -0
  10. package/lib/artifacts/code-fields.d.ts +49 -0
  11. package/lib/artifacts/code-fields.js +142 -0
  12. package/lib/artifacts/data-graph-document.d.ts +29 -0
  13. package/lib/artifacts/data-graph-document.js +52 -0
  14. package/lib/artifacts/data-space-field.d.ts +65 -0
  15. package/lib/artifacts/data-space-field.js +98 -0
  16. package/lib/artifacts/data-stream-document.d.ts +264 -0
  17. package/lib/artifacts/data-stream-document.js +680 -0
  18. package/lib/artifacts/data-transform-document.d.ts +152 -0
  19. package/lib/artifacts/data-transform-document.js +560 -0
  20. package/lib/artifacts/dependency-selection.d.ts +51 -0
  21. package/lib/artifacts/dependency-selection.js +154 -0
  22. package/lib/artifacts/dlo-document.d.ts +93 -0
  23. package/lib/artifacts/dlo-document.js +253 -0
  24. package/lib/artifacts/enum-case.d.ts +39 -0
  25. package/lib/artifacts/enum-case.js +67 -0
  26. package/lib/artifacts/git-deploy-selection.d.ts +68 -0
  27. package/lib/artifacts/git-deploy-selection.js +474 -0
  28. package/lib/artifacts/identity-resolution-document.d.ts +133 -0
  29. package/lib/artifacts/identity-resolution-document.js +625 -0
  30. package/lib/artifacts/mapping-document.d.ts +1 -0
  31. package/lib/artifacts/mapping-document.js +1 -0
  32. package/lib/artifacts/native-calculated-insight-document.d.ts +142 -0
  33. package/lib/artifacts/native-calculated-insight-document.js +321 -0
  34. package/lib/artifacts/native-data-graph-document.d.ts +124 -0
  35. package/lib/artifacts/native-data-graph-document.js +780 -0
  36. package/lib/artifacts/native-dmo-document.d.ts +151 -0
  37. package/lib/artifacts/native-dmo-document.js +313 -0
  38. package/lib/artifacts/native-mapping-document.d.ts +67 -0
  39. package/lib/artifacts/native-mapping-document.js +187 -0
  40. package/lib/artifacts/native-relationship-document.d.ts +76 -0
  41. package/lib/artifacts/native-relationship-document.js +232 -0
  42. package/lib/artifacts/path-selector.d.ts +55 -0
  43. package/lib/artifacts/path-selector.js +167 -0
  44. package/lib/artifacts/read-deploy-input.d.ts +239 -0
  45. package/lib/artifacts/read-deploy-input.js +1359 -0
  46. package/lib/artifacts/segment-document.d.ts +183 -0
  47. package/lib/artifacts/segment-document.js +570 -0
  48. package/lib/artifacts/verbatim-document.d.ts +203 -0
  49. package/lib/artifacts/verbatim-document.js +339 -0
  50. package/lib/artifacts/write-data-graph.d.ts +17 -0
  51. package/lib/artifacts/write-data-graph.js +162 -0
  52. package/lib/artifacts/write-data-stream.d.ts +11 -0
  53. package/lib/artifacts/write-data-stream.js +175 -0
  54. package/lib/artifacts/write-data-transform.d.ts +11 -0
  55. package/lib/artifacts/write-data-transform.js +180 -0
  56. package/lib/artifacts/write-dlo.d.ts +11 -0
  57. package/lib/artifacts/write-dlo.js +158 -0
  58. package/lib/artifacts/write-identity-resolution.d.ts +18 -0
  59. package/lib/artifacts/write-identity-resolution.js +165 -0
  60. package/lib/artifacts/write-mapping-definition.d.ts +17 -0
  61. package/lib/artifacts/write-mapping-definition.js +127 -0
  62. package/lib/artifacts/write-member-export.d.ts +88 -0
  63. package/lib/artifacts/write-member-export.js +199 -0
  64. package/lib/artifacts/write-native-calculated-insight.d.ts +21 -0
  65. package/lib/artifacts/write-native-calculated-insight.js +176 -0
  66. package/lib/artifacts/write-native-dmo.d.ts +11 -0
  67. package/lib/artifacts/write-native-dmo.js +158 -0
  68. package/lib/artifacts/write-native-mapping.d.ts +11 -0
  69. package/lib/artifacts/write-native-mapping.js +157 -0
  70. package/lib/artifacts/write-native-relationship.d.ts +11 -0
  71. package/lib/artifacts/write-native-relationship.js +157 -0
  72. package/lib/artifacts/write-segment.d.ts +26 -0
  73. package/lib/artifacts/write-segment.js +212 -0
  74. package/lib/commands/data360/delete.d.ts +280 -0
  75. package/lib/commands/data360/delete.js +2297 -0
  76. package/lib/commands/data360/deploy.d.ts +303 -0
  77. package/lib/commands/data360/deploy.js +2400 -0
  78. package/lib/commands/data360/publish.d.ts +93 -0
  79. package/lib/commands/data360/publish.js +292 -0
  80. package/lib/commands/data360/retrieve.d.ts +322 -0
  81. package/lib/commands/data360/retrieve.js +1963 -0
  82. package/lib/commands/data360/segment/members/export.d.ts +101 -0
  83. package/lib/commands/data360/segment/members/export.js +318 -0
  84. package/lib/errors/codes/calculated-insight.d.ts +2 -0
  85. package/lib/errors/codes/calculated-insight.js +25 -0
  86. package/lib/errors/codes/data-graph.d.ts +2 -0
  87. package/lib/errors/codes/data-graph.js +22 -0
  88. package/lib/errors/codes/data-stream.d.ts +2 -0
  89. package/lib/errors/codes/data-stream.js +70 -0
  90. package/lib/errors/codes/data-transform.d.ts +2 -0
  91. package/lib/errors/codes/data-transform.js +35 -0
  92. package/lib/errors/codes/dependency.d.ts +2 -0
  93. package/lib/errors/codes/dependency.js +11 -0
  94. package/lib/errors/codes/dlo.d.ts +2 -0
  95. package/lib/errors/codes/dlo.js +31 -0
  96. package/lib/errors/codes/dmo.d.ts +2 -0
  97. package/lib/errors/codes/dmo.js +28 -0
  98. package/lib/errors/codes/general.d.ts +2 -0
  99. package/lib/errors/codes/general.js +57 -0
  100. package/lib/errors/codes/identity-resolution.d.ts +2 -0
  101. package/lib/errors/codes/identity-resolution.js +24 -0
  102. package/lib/errors/codes/mapping.d.ts +2 -0
  103. package/lib/errors/codes/mapping.js +42 -0
  104. package/lib/errors/codes/relationship.d.ts +2 -0
  105. package/lib/errors/codes/relationship.js +21 -0
  106. package/lib/errors/codes/segment.d.ts +2 -0
  107. package/lib/errors/codes/segment.js +49 -0
  108. package/lib/errors/registry.d.ts +60 -0
  109. package/lib/errors/registry.js +123 -0
  110. package/lib/plugin.d.ts +5 -0
  111. package/lib/plugin.js +5 -0
  112. package/lib/services/acquire-retrieve-selection.d.ts +43 -0
  113. package/lib/services/acquire-retrieve-selection.js +145 -0
  114. package/lib/services/bind-identity-resolution-dependencies.d.ts +53 -0
  115. package/lib/services/bind-identity-resolution-dependencies.js +238 -0
  116. package/lib/services/bind-segment-dependencies.d.ts +49 -0
  117. package/lib/services/bind-segment-dependencies.js +115 -0
  118. package/lib/services/bounded-reads.d.ts +17 -0
  119. package/lib/services/bounded-reads.js +42 -0
  120. package/lib/services/calculated-insight-sql-checks.d.ts +33 -0
  121. package/lib/services/calculated-insight-sql-checks.js +193 -0
  122. package/lib/services/command-dlo.d.ts +48 -0
  123. package/lib/services/command-dlo.js +103 -0
  124. package/lib/services/command-native-data-graph.d.ts +54 -0
  125. package/lib/services/command-native-data-graph.js +485 -0
  126. package/lib/services/command-native-data-stream.d.ts +104 -0
  127. package/lib/services/command-native-data-stream.js +257 -0
  128. package/lib/services/command-native-dmo.d.ts +124 -0
  129. package/lib/services/command-native-dmo.js +338 -0
  130. package/lib/services/command-native-mapping.d.ts +65 -0
  131. package/lib/services/command-native-mapping.js +129 -0
  132. package/lib/services/command-native-relationship.d.ts +46 -0
  133. package/lib/services/command-native-relationship.js +103 -0
  134. package/lib/services/cross-dataspace-deploy.d.ts +111 -0
  135. package/lib/services/cross-dataspace-deploy.js +493 -0
  136. package/lib/services/cross-dataspace-rename.d.ts +74 -0
  137. package/lib/services/cross-dataspace-rename.js +1314 -0
  138. package/lib/services/cross-dataspace-sql.d.ts +109 -0
  139. package/lib/services/cross-dataspace-sql.js +1301 -0
  140. package/lib/services/cross-space-context.d.ts +23 -0
  141. package/lib/services/cross-space-context.js +36 -0
  142. package/lib/services/crud-plan.d.ts +137 -0
  143. package/lib/services/crud-plan.js +408 -0
  144. package/lib/services/data-space-check.d.ts +89 -0
  145. package/lib/services/data-space-check.js +162 -0
  146. package/lib/services/data-space-prefix.d.ts +77 -0
  147. package/lib/services/data-space-prefix.js +127 -0
  148. package/lib/services/data-transform-space.d.ts +31 -0
  149. package/lib/services/data-transform-space.js +79 -0
  150. package/lib/services/delete-calculated-insight.d.ts +44 -0
  151. package/lib/services/delete-calculated-insight.js +101 -0
  152. package/lib/services/delete-data-transform.d.ts +52 -0
  153. package/lib/services/delete-data-transform.js +222 -0
  154. package/lib/services/delete-dmo-field.d.ts +211 -0
  155. package/lib/services/delete-dmo-field.js +472 -0
  156. package/lib/services/delete-identity-resolution.d.ts +43 -0
  157. package/lib/services/delete-identity-resolution.js +295 -0
  158. package/lib/services/delete-segment.d.ts +42 -0
  159. package/lib/services/delete-segment.js +248 -0
  160. package/lib/services/deploy-result.d.ts +54 -0
  161. package/lib/services/deploy-result.js +60 -0
  162. package/lib/services/escaped-copy.d.ts +23 -0
  163. package/lib/services/escaped-copy.js +88 -0
  164. package/lib/services/execute-calculated-insight-deploy.d.ts +42 -0
  165. package/lib/services/execute-calculated-insight-deploy.js +96 -0
  166. package/lib/services/execute-data-transform-deploy.d.ts +56 -0
  167. package/lib/services/execute-data-transform-deploy.js +168 -0
  168. package/lib/services/execute-native-identity-resolution-deploy.d.ts +48 -0
  169. package/lib/services/execute-native-identity-resolution-deploy.js +360 -0
  170. package/lib/services/execute-segment-deploy.d.ts +45 -0
  171. package/lib/services/execute-segment-deploy.js +393 -0
  172. package/lib/services/local-file-removal.d.ts +36 -0
  173. package/lib/services/local-file-removal.js +103 -0
  174. package/lib/services/native-calculated-insight.d.ts +87 -0
  175. package/lib/services/native-calculated-insight.js +164 -0
  176. package/lib/services/native-crm-data-stream.d.ts +91 -0
  177. package/lib/services/native-crm-data-stream.js +272 -0
  178. package/lib/services/native-data-graph-context.d.ts +12 -0
  179. package/lib/services/native-data-graph-context.js +139 -0
  180. package/lib/services/native-data-graph-crd.d.ts +261 -0
  181. package/lib/services/native-data-graph-crd.js +866 -0
  182. package/lib/services/native-data-stream.d.ts +329 -0
  183. package/lib/services/native-data-stream.js +1830 -0
  184. package/lib/services/native-dlo.d.ts +163 -0
  185. package/lib/services/native-dlo.js +402 -0
  186. package/lib/services/native-dmo.d.ts +191 -0
  187. package/lib/services/native-dmo.js +614 -0
  188. package/lib/services/native-mapping.d.ts +255 -0
  189. package/lib/services/native-mapping.js +1226 -0
  190. package/lib/services/native-relationship.d.ts +132 -0
  191. package/lib/services/native-relationship.js +512 -0
  192. package/lib/services/operation-diagnostic.d.ts +83 -0
  193. package/lib/services/operation-diagnostic.js +272 -0
  194. package/lib/services/preflight-calculated-insight-deploy.d.ts +58 -0
  195. package/lib/services/preflight-calculated-insight-deploy.js +95 -0
  196. package/lib/services/preflight-data-transform-deploy.d.ts +33 -0
  197. package/lib/services/preflight-data-transform-deploy.js +104 -0
  198. package/lib/services/preflight-identity-resolution-deploy.d.ts +107 -0
  199. package/lib/services/preflight-identity-resolution-deploy.js +281 -0
  200. package/lib/services/preflight-segment-deploy.d.ts +31 -0
  201. package/lib/services/preflight-segment-deploy.js +109 -0
  202. package/lib/services/publish-calculated-insight.d.ts +40 -0
  203. package/lib/services/publish-calculated-insight.js +95 -0
  204. package/lib/services/publish-segment.d.ts +65 -0
  205. package/lib/services/publish-segment.js +194 -0
  206. package/lib/services/readback-pace.d.ts +16 -0
  207. package/lib/services/readback-pace.js +23 -0
  208. package/lib/services/retrieve-data-transform.d.ts +26 -0
  209. package/lib/services/retrieve-data-transform.js +65 -0
  210. package/lib/services/retrieve-identity-resolution.d.ts +40 -0
  211. package/lib/services/retrieve-identity-resolution.js +87 -0
  212. package/lib/services/retrieve-inventory.d.ts +32 -0
  213. package/lib/services/retrieve-inventory.js +151 -0
  214. package/lib/services/retrieve-segment.d.ts +120 -0
  215. package/lib/services/retrieve-segment.js +502 -0
  216. package/lib/services/segment-lookup.d.ts +46 -0
  217. package/lib/services/segment-lookup.js +108 -0
  218. package/lib/services/segment-members.d.ts +144 -0
  219. package/lib/services/segment-members.js +402 -0
  220. package/lib/services/select-native-retrieve.d.ts +60 -0
  221. package/lib/services/select-native-retrieve.js +226 -0
  222. package/lib/services/settle-calculated-insight.d.ts +22 -0
  223. package/lib/services/settle-calculated-insight.js +53 -0
  224. package/lib/services/sql-tokens.d.ts +58 -0
  225. package/lib/services/sql-tokens.js +147 -0
  226. package/lib/transport/api-version.d.ts +36 -0
  227. package/lib/transport/api-version.js +89 -0
  228. package/lib/transport/bound-ssot.d.ts +27 -0
  229. package/lib/transport/bound-ssot.js +116 -0
  230. package/lib/transport/calculated-insight-rest.d.ts +28 -0
  231. package/lib/transport/calculated-insight-rest.js +62 -0
  232. package/lib/transport/connect-client.d.ts +129 -0
  233. package/lib/transport/connect-client.js +446 -0
  234. package/lib/transport/connect-diagnostic.d.ts +89 -0
  235. package/lib/transport/connect-diagnostic.js +331 -0
  236. package/lib/transport/data-graph-rest.d.ts +27 -0
  237. package/lib/transport/data-graph-rest.js +76 -0
  238. package/lib/transport/data-stream-rest.d.ts +62 -0
  239. package/lib/transport/data-stream-rest.js +348 -0
  240. package/lib/transport/data-transform-rest.d.ts +25 -0
  241. package/lib/transport/data-transform-rest.js +100 -0
  242. package/lib/transport/dlo-native-rest.d.ts +22 -0
  243. package/lib/transport/dlo-native-rest.js +89 -0
  244. package/lib/transport/dlo-rest.d.ts +23 -0
  245. package/lib/transport/dlo-rest.js +135 -0
  246. package/lib/transport/dmo-native-rest.d.ts +18 -0
  247. package/lib/transport/dmo-native-rest.js +55 -0
  248. package/lib/transport/dmo-rest.d.ts +30 -0
  249. package/lib/transport/dmo-rest.js +163 -0
  250. package/lib/transport/mapping-rest.d.ts +63 -0
  251. package/lib/transport/mapping-rest.js +341 -0
  252. package/lib/transport/project-metadata.d.ts +36 -0
  253. package/lib/transport/project-metadata.js +118 -0
  254. package/lib/transport/relationship-rest.d.ts +43 -0
  255. package/lib/transport/relationship-rest.js +201 -0
  256. package/package.json +96 -4
@@ -0,0 +1,158 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { link, mkdir, mkdtemp, open, rename, unlink } from 'node:fs/promises';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+ import { DloError, leanDloDocument, validateDloDocument } from './dlo-document.js';
6
+ import { inspectSegmentPath, verifyTemporaryDirectory } from '../transport/project-metadata.js';
7
+ import { isVerbatimDocument, serializeVerbatimDocument } from './verbatim-document.js';
8
+ /**
9
+ * Validate each real directory ancestor before publication.
10
+ * @param {string} directory Destination directory.
11
+ */
12
+ async function ensure(directory) {
13
+ const parent = path.dirname(directory);
14
+ if (parent !== directory)
15
+ await ensure(parent);
16
+ try {
17
+ const stat = await inspectSegmentPath(directory);
18
+ if (!stat.isDirectory())
19
+ throw new DloError('UNSAFE_PATH');
20
+ }
21
+ catch (error) {
22
+ if (error.code !== 'ENOENT')
23
+ throw error;
24
+ try {
25
+ await mkdir(directory);
26
+ }
27
+ catch (error) {
28
+ if (error.code !== 'EEXIST')
29
+ throw error;
30
+ }
31
+ const stat = await inspectSegmentPath(directory);
32
+ if (!stat.isDirectory())
33
+ throw new DloError('UNSAFE_PATH');
34
+ }
35
+ }
36
+ /**
37
+ * Bind existing bytes to an unchanged single-link file handle.
38
+ * @param {string} file Exact destination.
39
+ * @returns {Promise<Buffer | undefined>} Existing bytes or confirmed absence.
40
+ */
41
+ async function existing(file) {
42
+ try {
43
+ const before = await inspectSegmentPath(file);
44
+ if (!before.isFile() || before.nlink !== 1 || before.size > 1_048_576)
45
+ throw new DloError('UNSAFE_PATH');
46
+ const handle = await open(file, 'r');
47
+ try {
48
+ const opened = await handle.stat();
49
+ if (opened.dev !== before.dev || opened.ino !== before.ino || opened.nlink !== 1)
50
+ throw new DloError('OUTPUT_CHANGED');
51
+ const buffer = Buffer.alloc(1_048_577);
52
+ let length = 0;
53
+ while (length < buffer.length) {
54
+ const { bytesRead } = await handle.read(buffer, length, buffer.length - length, length);
55
+ if (!bytesRead)
56
+ break;
57
+ length += bytesRead;
58
+ }
59
+ const after = await handle.stat();
60
+ const current = await inspectSegmentPath(file);
61
+ if (length !== before.size ||
62
+ length > 1_048_576 ||
63
+ after.size !== before.size ||
64
+ after.mtimeMs !== before.mtimeMs ||
65
+ after.ctimeMs !== before.ctimeMs ||
66
+ current.dev !== before.dev ||
67
+ current.ino !== before.ino ||
68
+ current.nlink !== 1 ||
69
+ current.mtimeMs !== before.mtimeMs ||
70
+ current.ctimeMs !== before.ctimeMs)
71
+ throw new DloError('OUTPUT_CHANGED');
72
+ return buffer.subarray(0, length);
73
+ }
74
+ finally {
75
+ await handle.close();
76
+ }
77
+ }
78
+ catch (error) {
79
+ if (error.code === 'ENOENT')
80
+ return undefined;
81
+ throw error;
82
+ }
83
+ }
84
+ /**
85
+ * Publish portable API68 DLO JSON with no-clobber creation and guarded replacement.
86
+ * @param {unknown} value Portable document without source IDs.
87
+ * @param {string} projectRoot Explicit project root.
88
+ * @param {boolean} overwrite Permit replacing differing bytes.
89
+ * @returns {Promise<{path: string; outcome: 'retrieved' | 'unchanged'}>} Verified publication.
90
+ */
91
+ export async function writeDloDocument(value, projectRoot, overwrite) {
92
+ const verbatim = isVerbatimDocument(value, 'dlo') ? value : undefined;
93
+ // Authored content only: the fields a deploy keeps from the org when the file omits them.
94
+ const document = verbatim ?? leanDloDocument(validateDloDocument(value, 'retrieve'));
95
+ const bytes = Buffer.from(verbatim ? serializeVerbatimDocument(verbatim) : `${JSON.stringify(document, undefined, 2)}\n`);
96
+ if (!path.isAbsolute(projectRoot) ||
97
+ projectRoot !== projectRoot.trim() ||
98
+ /[*?\0]/u.test(projectRoot) ||
99
+ projectRoot.split(/[\\/]/u).includes('..') ||
100
+ bytes.length > 1_048_576)
101
+ throw new DloError('UNSAFE_PATH');
102
+ const root = path.resolve(projectRoot);
103
+ if (path.dirname(root) === root)
104
+ throw new DloError('UNSAFE_PATH');
105
+ const destination = path.join(root, 'data360', 'dlo', `${document.identity.apiName}.dlo-meta.json`);
106
+ let published = false;
107
+ try {
108
+ await ensure(path.dirname(destination));
109
+ const previous = await existing(destination);
110
+ if (previous?.equals(bytes))
111
+ return { path: destination, outcome: 'unchanged' };
112
+ // A DLO name is unique in the org and its file lists every data space, so the file at the
113
+ // path is the same DLO whatever spaces it names.
114
+ if (previous && !overwrite)
115
+ throw new DloError('OVERWRITE_REQUIRED');
116
+ verifyTemporaryDirectory();
117
+ const staging = await mkdtemp(path.join(os.tmpdir(), 'data360-dlo-output-'));
118
+ const marker = await open(path.join(staging, 'ownership'), 'wx');
119
+ try {
120
+ await marker.writeFile(randomUUID());
121
+ }
122
+ finally {
123
+ await marker.close();
124
+ }
125
+ const stagedPath = path.join(staging, 'document.json');
126
+ const handle = await open(stagedPath, 'wx');
127
+ try {
128
+ await handle.writeFile(bytes);
129
+ await handle.sync();
130
+ }
131
+ finally {
132
+ await handle.close();
133
+ }
134
+ await ensure(path.dirname(destination));
135
+ const current = await existing(destination);
136
+ if (previous === undefined ? current !== undefined : !current?.equals(previous))
137
+ throw new DloError('OUTPUT_CHANGED');
138
+ if (previous)
139
+ await rename(stagedPath, destination);
140
+ else {
141
+ await link(stagedPath, destination);
142
+ published = true;
143
+ await unlink(stagedPath);
144
+ }
145
+ published = true;
146
+ const completed = await existing(destination);
147
+ if (!completed?.equals(bytes))
148
+ throw new DloError('OUTPUT_CHANGED');
149
+ return { path: destination, outcome: 'retrieved' };
150
+ }
151
+ catch (error) {
152
+ if (published)
153
+ throw new DloError('PARTIAL_DLO_OUTPUT');
154
+ if (error instanceof DloError)
155
+ throw error;
156
+ throw new DloError('OUTPUT_FAILED');
157
+ }
158
+ }
@@ -0,0 +1,18 @@
1
+ import { link, rename, unlink } from 'node:fs/promises';
2
+ /**
3
+ * Publish only complete portable IR JSON; runtime expectations never enter the file.
4
+ * Staging stays in owned OS temp; cross-volume atomic operations fail closed.
5
+ * @param {unknown} value Complete portable document.
6
+ * @param {string} projectRoot Absolute output project root.
7
+ * @param {boolean} overwrite Permit differing existing bytes.
8
+ * @param {object} publish Native publication seam for offline race/failure tests.
9
+ * @returns {Promise<{path: string; outcome: 'retrieved' | 'unchanged'}>} Validated receipt.
10
+ */
11
+ export declare function writeIdentityResolutionDocument(value: unknown, projectRoot: string, overwrite: boolean, publish?: {
12
+ link: typeof link;
13
+ rename: typeof rename;
14
+ unlink: typeof unlink;
15
+ }): Promise<{
16
+ path: string;
17
+ outcome: 'retrieved' | 'unchanged';
18
+ }>;
@@ -0,0 +1,165 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { link, mkdir, mkdtemp, open, rename, unlink } from 'node:fs/promises';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+ import { IdentityResolutionError, serializeIdentityResolutionDocument, validateIdentityResolutionDocument, } from './identity-resolution-document.js';
6
+ import { inspectSegmentPath, verifyTemporaryDirectory } from '../transport/project-metadata.js';
7
+ import { isVerbatimDocument, serializeVerbatimDocument } from './verbatim-document.js';
8
+ const publication = { link, rename, unlink };
9
+ /**
10
+ * Validate or create only a real directory chain.
11
+ * @param {string} directory Absolute directory.
12
+ */
13
+ async function ensure(directory) {
14
+ const parent = path.dirname(directory);
15
+ if (parent !== directory)
16
+ await ensure(parent);
17
+ try {
18
+ const stat = await inspectSegmentPath(directory);
19
+ if (!stat.isDirectory())
20
+ throw new IdentityResolutionError('UNSAFE_PATH');
21
+ }
22
+ catch (error) {
23
+ if (error.code !== 'ENOENT')
24
+ throw error;
25
+ try {
26
+ await mkdir(directory);
27
+ }
28
+ catch (error_) {
29
+ if (error_.code !== 'EEXIST')
30
+ throw error_;
31
+ }
32
+ const stat = await inspectSegmentPath(directory);
33
+ if (!stat.isDirectory())
34
+ throw new IdentityResolutionError('UNSAFE_PATH');
35
+ }
36
+ }
37
+ /**
38
+ * Read bounded single-link bytes while binding the handle to the inspected path.
39
+ * @param {string} file Absolute file.
40
+ * @returns {Promise<Buffer | undefined>} Exact bytes or explicit absence.
41
+ */
42
+ async function existing(file) {
43
+ try {
44
+ const before = await inspectSegmentPath(file);
45
+ if (!before.isFile() || before.nlink !== 1 || before.size > 1_048_576)
46
+ throw new IdentityResolutionError('UNSAFE_PATH');
47
+ const handle = await open(file, 'r');
48
+ try {
49
+ const opened = await handle.stat();
50
+ if (opened.dev !== before.dev || opened.ino !== before.ino || opened.nlink !== 1)
51
+ throw new IdentityResolutionError('OUTPUT_CHANGED');
52
+ const buffer = Buffer.alloc(1_048_577);
53
+ let length = 0;
54
+ while (length < buffer.length) {
55
+ const { bytesRead } = await handle.read(buffer, length, buffer.length - length, length);
56
+ if (!bytesRead)
57
+ break;
58
+ length += bytesRead;
59
+ }
60
+ const after = await handle.stat();
61
+ const current = await inspectSegmentPath(file);
62
+ if (length !== before.size ||
63
+ length > 1_048_576 ||
64
+ after.size !== before.size ||
65
+ after.mtimeMs !== before.mtimeMs ||
66
+ after.ctimeMs !== before.ctimeMs ||
67
+ current.dev !== before.dev ||
68
+ current.ino !== before.ino ||
69
+ current.nlink !== 1 ||
70
+ current.mtimeMs !== before.mtimeMs ||
71
+ current.ctimeMs !== before.ctimeMs)
72
+ throw new IdentityResolutionError('OUTPUT_CHANGED');
73
+ return buffer.subarray(0, length);
74
+ }
75
+ finally {
76
+ await handle.close();
77
+ }
78
+ }
79
+ catch (error) {
80
+ if (error.code === 'ENOENT')
81
+ return undefined;
82
+ throw error;
83
+ }
84
+ }
85
+ /**
86
+ * Publish only complete portable IR JSON; runtime expectations never enter the file.
87
+ * Staging stays in owned OS temp; cross-volume atomic operations fail closed.
88
+ * @param {unknown} value Complete portable document.
89
+ * @param {string} projectRoot Absolute output project root.
90
+ * @param {boolean} overwrite Permit differing existing bytes.
91
+ * @param {object} publish Native publication seam for offline race/failure tests.
92
+ * @returns {Promise<{path: string; outcome: 'retrieved' | 'unchanged'}>} Validated receipt.
93
+ */
94
+ export async function writeIdentityResolutionDocument(value, projectRoot, overwrite, publish = publication) {
95
+ const verbatim = isVerbatimDocument(value, 'identity-resolution') ? value : undefined;
96
+ // Retrieve writes keep what the plugin does not model; deploy refuses such a file.
97
+ const strict = verbatim ? undefined : validateIdentityResolutionDocument(value, 'retrieve');
98
+ const document = verbatim ?? strict;
99
+ const bytes = Buffer.from(verbatim
100
+ ? serializeVerbatimDocument(verbatim)
101
+ : serializeIdentityResolutionDocument(strict, 'retrieve'));
102
+ if (!path.isAbsolute(projectRoot) ||
103
+ projectRoot !== projectRoot.trim() ||
104
+ /[*?\0]/u.test(projectRoot) ||
105
+ projectRoot.split(/[\\/]/u).includes('..') ||
106
+ bytes.length > 1_048_576)
107
+ throw new IdentityResolutionError('UNSAFE_PATH');
108
+ const root = path.resolve(projectRoot);
109
+ if (path.dirname(root) === root)
110
+ throw new IdentityResolutionError('UNSAFE_PATH');
111
+ let published = false;
112
+ try {
113
+ const destination = path.join(root, 'data360', 'identityResolution', `${document.identity.apiName}.identityResolution.json`);
114
+ await ensure(path.dirname(destination));
115
+ const previous = await existing(destination);
116
+ if (previous?.equals(bytes))
117
+ return { path: destination, outcome: 'unchanged' };
118
+ if (previous && !overwrite)
119
+ throw new IdentityResolutionError('OVERWRITE_REQUIRED');
120
+ verifyTemporaryDirectory();
121
+ const staging = await mkdtemp(path.join(os.tmpdir(), 'data360-ir-output-'));
122
+ const marker = await open(path.join(staging, 'ownership'), 'wx');
123
+ try {
124
+ await marker.writeFile(randomUUID());
125
+ }
126
+ finally {
127
+ await marker.close();
128
+ }
129
+ const stagedPath = path.join(staging, 'document.json');
130
+ const handle = await open(stagedPath, 'wx');
131
+ try {
132
+ await handle.writeFile(bytes);
133
+ await handle.sync();
134
+ }
135
+ finally {
136
+ await handle.close();
137
+ }
138
+ await ensure(path.dirname(destination));
139
+ const current = await existing(destination);
140
+ if (previous === undefined ? current !== undefined : !current?.equals(previous))
141
+ throw new IdentityResolutionError('OUTPUT_CHANGED');
142
+ if (previous)
143
+ await publish.rename(stagedPath, destination);
144
+ else {
145
+ await publish.link(stagedPath, destination);
146
+ published = true;
147
+ // Remove only the exact run-created staging entry, never recursively clean the run root.
148
+ await publish.unlink(stagedPath);
149
+ }
150
+ published = true;
151
+ const completed = await existing(destination);
152
+ if (!completed?.equals(bytes))
153
+ throw new IdentityResolutionError('OUTPUT_CHANGED');
154
+ if (!verbatim)
155
+ validateIdentityResolutionDocument(JSON.parse(completed.toString('utf8')), 'retrieve');
156
+ return { path: destination, outcome: 'retrieved' };
157
+ }
158
+ catch (error) {
159
+ if (published)
160
+ throw new IdentityResolutionError('PARTIAL_IDENTITY_RESOLUTION_OUTPUT');
161
+ if (error instanceof IdentityResolutionError)
162
+ throw error;
163
+ throw new IdentityResolutionError('OUTPUT_FAILED');
164
+ }
165
+ }
@@ -0,0 +1,17 @@
1
+ /** Payload-free mapping definition selection, acquisition and publication refusal. */
2
+ export declare class MappingDefinitionError extends Error {
3
+ readonly code: string;
4
+ constructor(code: string);
5
+ }
6
+ /**
7
+ * Publish exact acquired standard XML, not an operational plugin deploy document.
8
+ * @param {string} apiName Exact mapping developer name.
9
+ * @param {string} definitionXml Acquired opaque XML bytes as UTF-8 text.
10
+ * @param {string} projectRoot Selected output project root.
11
+ * @param {boolean} overwrite Permit replacing differing existing regular bytes.
12
+ * @returns {Promise<{path: string; outcome: 'retrieved' | 'unchanged'}>} Exact publication receipt.
13
+ */
14
+ export declare function writeMappingDefinition(apiName: string, definitionXml: string, projectRoot: string, overwrite: boolean): Promise<{
15
+ path: string;
16
+ outcome: 'retrieved' | 'unchanged';
17
+ }>;
@@ -0,0 +1,127 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { link, mkdir, mkdtemp, open, readFile, rename, unlink } from 'node:fs/promises';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+ import { mappingName } from './mapping-document.js';
6
+ import { segmentSourceRoot } from './write-segment.js';
7
+ import { inspectSegmentPath, verifyTemporaryDirectory } from '../transport/project-metadata.js';
8
+ /** Payload-free mapping definition selection, acquisition and publication refusal. */
9
+ export class MappingDefinitionError extends Error {
10
+ code;
11
+ constructor(code) {
12
+ super('Mapping definition export did not complete.');
13
+ this.code = code;
14
+ this.name = 'MappingDefinitionError';
15
+ }
16
+ }
17
+ /**
18
+ * Validate or create a real destination directory chain without following aliases.
19
+ * @param {string} directory Exact absolute directory.
20
+ * @returns {Promise<void>} Resolves for a safe directory chain.
21
+ */
22
+ async function ensure(directory) {
23
+ const parent = path.dirname(directory);
24
+ if (parent !== directory)
25
+ await ensure(parent);
26
+ try {
27
+ const stat = await inspectSegmentPath(directory);
28
+ if (!stat.isDirectory())
29
+ throw new MappingDefinitionError('UNSAFE_PATH');
30
+ }
31
+ catch (error) {
32
+ if (error.code !== 'ENOENT')
33
+ throw error;
34
+ try {
35
+ await mkdir(directory);
36
+ }
37
+ catch (creationError) {
38
+ if (creationError.code !== 'EEXIST')
39
+ throw creationError;
40
+ }
41
+ const stat = await inspectSegmentPath(directory);
42
+ if (!stat.isDirectory())
43
+ throw new MappingDefinitionError('UNSAFE_PATH');
44
+ }
45
+ }
46
+ /**
47
+ * Read an existing bounded regular single-link definition, distinguishing absence only.
48
+ * @param {string} file Exact destination.
49
+ * @returns {Promise<Buffer | undefined>} Observed bytes or absence.
50
+ */
51
+ async function existing(file) {
52
+ try {
53
+ const stat = await inspectSegmentPath(file);
54
+ if (!stat.isFile() || stat.nlink !== 1 || stat.size > 1_048_576)
55
+ throw new MappingDefinitionError('UNSAFE_PATH');
56
+ return await readFile(file);
57
+ }
58
+ catch (error) {
59
+ if (error.code === 'ENOENT')
60
+ return undefined;
61
+ throw error;
62
+ }
63
+ }
64
+ /**
65
+ * Publish exact acquired standard XML, not an operational plugin deploy document.
66
+ * @param {string} apiName Exact mapping developer name.
67
+ * @param {string} definitionXml Acquired opaque XML bytes as UTF-8 text.
68
+ * @param {string} projectRoot Selected output project root.
69
+ * @param {boolean} overwrite Permit replacing differing existing regular bytes.
70
+ * @returns {Promise<{path: string; outcome: 'retrieved' | 'unchanged'}>} Exact publication receipt.
71
+ */
72
+ export async function writeMappingDefinition(apiName, definitionXml, projectRoot, overwrite) {
73
+ if (!mappingName.test(apiName) || !projectRoot.trim())
74
+ throw new MappingDefinitionError('INVALID_SELECTION');
75
+ const bytes = Buffer.from(definitionXml);
76
+ if (bytes.length === 0 || bytes.length > 1_048_576)
77
+ throw new MappingDefinitionError('INVALID_DEFINITION');
78
+ try {
79
+ const base = await segmentSourceRoot(path.resolve(projectRoot));
80
+ const file = path.join(base, 'objectSourceTargetMaps', `${apiName}.objectSourceTargetMap-meta.xml`);
81
+ await ensure(path.dirname(file));
82
+ const previous = await existing(file);
83
+ if (previous?.equals(bytes))
84
+ return { path: file, outcome: 'unchanged' };
85
+ if (previous && !overwrite)
86
+ throw new MappingDefinitionError('OVERWRITE_REQUIRED');
87
+ verifyTemporaryDirectory();
88
+ const staging = await mkdtemp(path.join(os.tmpdir(), 'data360-mapping-output-'));
89
+ const marker = await open(path.join(staging, 'ownership'), 'wx');
90
+ await marker.writeFile(randomUUID());
91
+ await marker.close();
92
+ const staged = path.join(staging, 'definition.xml');
93
+ const handle = await open(staged, 'wx');
94
+ try {
95
+ await handle.writeFile(bytes);
96
+ await handle.sync();
97
+ }
98
+ finally {
99
+ await handle.close();
100
+ }
101
+ await ensure(path.dirname(file));
102
+ const current = await existing(file);
103
+ if (previous === undefined ? current !== undefined : !current?.equals(previous))
104
+ throw new MappingDefinitionError('OUTPUT_CHANGED');
105
+ if (previous)
106
+ await rename(staged, file);
107
+ else {
108
+ await link(staged, file);
109
+ await unlink(staged);
110
+ }
111
+ const completed = await existing(file);
112
+ if (!completed?.equals(bytes))
113
+ throw new MappingDefinitionError('OUTPUT_CHANGED');
114
+ return { path: file, outcome: 'retrieved' };
115
+ }
116
+ catch (error) {
117
+ if (error instanceof MappingDefinitionError)
118
+ throw error;
119
+ if (error && typeof error === 'object' && 'code' in error) {
120
+ if (error.code === 'UNSAFE_PATH')
121
+ throw new MappingDefinitionError('UNSAFE_PATH');
122
+ if (error.code === 'INVALID_SELECTION')
123
+ throw new MappingDefinitionError('INVALID_SELECTION');
124
+ }
125
+ throw new MappingDefinitionError('OUTPUT_FAILED');
126
+ }
127
+ }
@@ -0,0 +1,88 @@
1
+ /** Output format of a member export. */
2
+ export type MemberFormat = 'csv' | 'json';
3
+ /** Column delimiters of the CSV output. */
4
+ export declare const memberDelimiters: {
5
+ readonly COMMA: ",";
6
+ readonly SEMICOLON: ";";
7
+ readonly TAB: "\t";
8
+ readonly PIPE: "|";
9
+ readonly CARET: "^";
10
+ readonly BACKQUOTE: "`";
11
+ };
12
+ /** `--column-delimiter` values: a name of memberDelimiters or the character itself (not TAB). */
13
+ export declare const memberDelimiterOptions: string[];
14
+ /**
15
+ * The delimiter character of a `--column-delimiter` value.
16
+ * @param {string} value A name of memberDelimiters or the character itself.
17
+ * @returns {string} Delimiter character.
18
+ */
19
+ export declare function memberDelimiter(value: string): string;
20
+ /**
21
+ * Column headers of the detail output: each column's label (an empty label falls back to its
22
+ * API name) or its API name; a header used already gets a stable `_2`, `_3`, ... suffix in
23
+ * column order.
24
+ * @param {Array<{ name: string; label: string }>} columns Detail columns.
25
+ * @param {'label' | 'api-name'} mode Header source.
26
+ * @returns {string[]} One unique header per column.
27
+ */
28
+ export declare function memberColumnHeaders(columns: ReadonlyArray<{
29
+ name: string;
30
+ label: string;
31
+ }>, mode: 'label' | 'api-name'): string[];
32
+ /**
33
+ * A detail value formatted like the mcnext member export: a DATE as `YYYY-MM-DD` (the time
34
+ * part dropped), a DATE_TIME with a space instead of the `T` (offset kept). The type is
35
+ * compared without case, spaces, `_` and `-`, so `/ssot/metadata`'s `DATE_TIME` counts as
36
+ * DATETIME (mcnext compares `datetime` only and leaves `DATE_TIME` values as returned).
37
+ * Other types, non-text values and text that is not a date stay as they are.
38
+ * @param {unknown} value Cell value as Salesforce returned it.
39
+ * @param {string} [dataType] Metadata type of the field, e.g. `DATE` or `DATE_TIME`.
40
+ * @returns {unknown} Formatted value.
41
+ */
42
+ export declare function formatMemberValue(value: unknown, dataType?: string): unknown;
43
+ /**
44
+ * A CSV cell: text as is, other values as JSON, quoted when needed.
45
+ * @param {unknown} value Cell value.
46
+ * @param {string} delimiter Column delimiter.
47
+ * @returns {string} Cell text.
48
+ */
49
+ export declare function csvCell(value: unknown, delimiter: string): string;
50
+ /**
51
+ * A file name part made from a segment display name, as the mcnext export makes it (its
52
+ * `sanitizeFilename`): control characters and `< > : " / \ | ? *` become `-`, whitespace runs
53
+ * `-`, repeated `-` one; trailing `-`, `.` and spaces and leading `.` and spaces are dropped
54
+ * (safe on Windows and Linux). An empty result is `segment-members`; a Windows device name
55
+ * (CON, PRN, AUX, NUL, COM1-9, LPT1-9) gets the prefix `segment-`.
56
+ * @param {string} value Segment display name.
57
+ * @returns {string} File-safe name part.
58
+ */
59
+ export declare function memberFileName(value: string): string;
60
+ /**
61
+ * The output file: the given path, or a new `data/<data space>/<display name>-<timestamp>.<format>`
62
+ * below the working directory (never a `data360/` definition folder). The display name is
63
+ * used, as in the mcnext export, because a segment's API name stays the same when its display
64
+ * name changes (tested live); it is made file-safe with memberFileName.
65
+ * @param {string | undefined} requested --output-file value.
66
+ * @param {string} name Segment display name (the API name when the segment has none).
67
+ * @param {MemberFormat} format Output format.
68
+ * @param {string} cwd Working directory.
69
+ * @param {Date} [now] Time of the export.
70
+ * @param {string} [dataSpace] The run's data space, the folder below data/.
71
+ * @returns {Promise<string>} Absolute output path.
72
+ */
73
+ export declare function memberOutputFile(requested: string | undefined, name: string, format: MemberFormat, cwd: string, now?: Date, dataSpace?: string): Promise<string>;
74
+ /** Receives the rows of each page in order. */
75
+ export type RowSink = {
76
+ write: (rows: Array<Record<string, unknown>>) => Promise<void>;
77
+ };
78
+ /**
79
+ * Stream rows into a temporary file beside the output and rename it into place only when
80
+ * every page was read; on failure the temporary file is removed and no output is left.
81
+ * @param {string} file Absolute output path (an existing file is replaced).
82
+ * @param {MemberFormat} format CSV (header from the first row or the given columns) or a JSON array.
83
+ * @param {string} delimiter CSV column delimiter.
84
+ * @param {string[] | undefined} columns Fixed CSV header, or undefined to use the first row's keys.
85
+ * @param {(sink: RowSink) => Promise<void>} produce Writes all rows through the sink.
86
+ * @returns {Promise<number>} Rows written.
87
+ */
88
+ export declare function writeMemberExport(file: string, format: MemberFormat, delimiter: string, columns: string[] | undefined, produce: (sink: RowSink) => Promise<void>): Promise<number>;