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,162 @@
1
+ import { validateNativeDataGraphDocument } from './native-data-graph-document.js';
2
+ import { randomUUID } from 'node:crypto';
3
+ import { link, mkdir, mkdtemp, open, rename, unlink } from 'node:fs/promises';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+ import { DataGraphError } from './data-graph-document.js';
7
+ import { inspectSegmentPath, verifyTemporaryDirectory } from '../transport/project-metadata.js';
8
+ import { isVerbatimDocument, serializeVerbatimDocument } from './verbatim-document.js';
9
+ const publication = { link, rename, unlink };
10
+ /**
11
+ * Validate or create only a real directory chain.
12
+ * @param {string} directory Absolute directory.
13
+ */
14
+ async function ensure(directory) {
15
+ const parent = path.dirname(directory);
16
+ if (parent !== directory)
17
+ await ensure(parent);
18
+ try {
19
+ const stat = await inspectSegmentPath(directory);
20
+ if (!stat.isDirectory())
21
+ throw new DataGraphError('UNSAFE_PATH');
22
+ }
23
+ catch (error) {
24
+ if (error.code !== 'ENOENT')
25
+ throw error;
26
+ try {
27
+ await mkdir(directory);
28
+ }
29
+ catch (error_) {
30
+ if (error_.code !== 'EEXIST')
31
+ throw error_;
32
+ }
33
+ const stat = await inspectSegmentPath(directory);
34
+ if (!stat.isDirectory())
35
+ throw new DataGraphError('UNSAFE_PATH');
36
+ }
37
+ }
38
+ /**
39
+ * Read bounded single-link bytes bound to the inspected path and handle.
40
+ * @param {string} file Absolute file.
41
+ * @returns {Promise<Buffer | undefined>} Exact bytes or explicit absence.
42
+ */
43
+ async function existing(file) {
44
+ try {
45
+ const before = await inspectSegmentPath(file);
46
+ if (!before.isFile() || before.nlink !== 1 || before.size > 1_048_576)
47
+ throw new DataGraphError('UNSAFE_PATH');
48
+ const handle = await open(file, 'r');
49
+ try {
50
+ const opened = await handle.stat();
51
+ if (opened.dev !== before.dev || opened.ino !== before.ino || opened.nlink !== 1)
52
+ throw new DataGraphError('OUTPUT_CHANGED');
53
+ const buffer = Buffer.alloc(1_048_577);
54
+ let length = 0;
55
+ while (length < buffer.length) {
56
+ const { bytesRead } = await handle.read(buffer, length, buffer.length - length, length);
57
+ if (!bytesRead)
58
+ break;
59
+ length += bytesRead;
60
+ }
61
+ const after = await handle.stat();
62
+ const current = await inspectSegmentPath(file);
63
+ if (length !== before.size ||
64
+ length > 1_048_576 ||
65
+ after.size !== before.size ||
66
+ after.mtimeMs !== before.mtimeMs ||
67
+ after.ctimeMs !== before.ctimeMs ||
68
+ current.dev !== before.dev ||
69
+ current.ino !== before.ino ||
70
+ current.nlink !== 1 ||
71
+ current.mtimeMs !== before.mtimeMs ||
72
+ current.ctimeMs !== before.ctimeMs)
73
+ throw new DataGraphError('OUTPUT_CHANGED');
74
+ return buffer.subarray(0, length);
75
+ }
76
+ finally {
77
+ await handle.close();
78
+ }
79
+ }
80
+ catch (error) {
81
+ if (error.code === 'ENOENT')
82
+ return undefined;
83
+ throw error;
84
+ }
85
+ }
86
+ /**
87
+ * Publish native Graph JSON only, with owned OS-temp staging and no-clobber publication.
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 failure tests.
92
+ * @returns {Promise<{path: string; outcome: 'retrieved' | 'unchanged'}>} Receipt.
93
+ */
94
+ export async function writeDataGraphDocument(value, projectRoot, overwrite, publish = publication) {
95
+ const verbatim = isVerbatimDocument(value, 'native-data-graph') ? value : undefined;
96
+ const document = verbatim ? undefined : validateNativeDataGraphDocument(value, 'retrieve');
97
+ const name = verbatim ? verbatim.identity.apiName : document.definition.name;
98
+ const bytes = Buffer.from(verbatim ? serializeVerbatimDocument(verbatim) : `${JSON.stringify(document, undefined, 2)}\n`);
99
+ if (!path.isAbsolute(projectRoot) ||
100
+ projectRoot !== projectRoot.trim() ||
101
+ /[*?\0]/u.test(projectRoot) ||
102
+ projectRoot.split(/[\\/]/u).includes('..') ||
103
+ bytes.length > 1_048_576)
104
+ throw new DataGraphError('UNSAFE_PATH');
105
+ const root = path.resolve(projectRoot);
106
+ if (path.dirname(root) === root)
107
+ throw new DataGraphError('UNSAFE_PATH');
108
+ let published = false;
109
+ try {
110
+ const destination = path.join(root, 'data360', 'dataGraphs', `${name}.native-dataGraph.json`);
111
+ await ensure(path.dirname(destination));
112
+ const previous = await existing(destination);
113
+ if (previous?.equals(bytes))
114
+ return { path: destination, outcome: 'unchanged' };
115
+ if (previous && !overwrite)
116
+ throw new DataGraphError('OVERWRITE_REQUIRED');
117
+ verifyTemporaryDirectory();
118
+ const staging = await mkdtemp(path.join(os.tmpdir(), 'data360-graph-output-'));
119
+ const marker = await open(path.join(staging, 'ownership'), 'wx');
120
+ try {
121
+ await marker.writeFile(randomUUID());
122
+ }
123
+ finally {
124
+ await marker.close();
125
+ }
126
+ const stagedPath = path.join(staging, 'document.json');
127
+ const handle = await open(stagedPath, 'wx');
128
+ try {
129
+ await handle.writeFile(bytes);
130
+ await handle.sync();
131
+ }
132
+ finally {
133
+ await handle.close();
134
+ }
135
+ await ensure(path.dirname(destination));
136
+ const current = await existing(destination);
137
+ if (previous === undefined ? current !== undefined : !current?.equals(previous))
138
+ throw new DataGraphError('OUTPUT_CHANGED');
139
+ if (previous)
140
+ await publish.rename(stagedPath, destination);
141
+ else {
142
+ await publish.link(stagedPath, destination);
143
+ published = true;
144
+ // Unlink only the exact run-created staging entry, never recursively clean its root.
145
+ await publish.unlink(stagedPath);
146
+ }
147
+ published = true;
148
+ const completed = await existing(destination);
149
+ if (!completed?.equals(bytes))
150
+ throw new DataGraphError('OUTPUT_CHANGED');
151
+ if (!verbatim)
152
+ validateNativeDataGraphDocument(JSON.parse(completed.toString('utf8')), 'retrieve');
153
+ return { path: destination, outcome: 'retrieved' };
154
+ }
155
+ catch (error) {
156
+ if (published)
157
+ throw new DataGraphError('PARTIAL_DATA_GRAPH_OUTPUT');
158
+ if (error instanceof DataGraphError)
159
+ throw error;
160
+ throw new DataGraphError('OUTPUT_FAILED');
161
+ }
162
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Publish retained stream evidence with bounded atomic publication; no recursive cleanup.
3
+ * @param {unknown} value Portable definition, not a native request body.
4
+ * @param {string} projectRoot Explicit absolute project root.
5
+ * @param {boolean} overwrite Permit replacing different bytes of the same identity.
6
+ * @returns {Promise<{path: string; outcome: 'retrieved' | 'unchanged'}>} Publication receipt.
7
+ */
8
+ export declare function writeDataStreamDocument(value: unknown, projectRoot: string, overwrite: boolean): Promise<{
9
+ path: string;
10
+ outcome: 'retrieved' | 'unchanged';
11
+ }>;
@@ -0,0 +1,175 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { link, mkdir, mkdtemp, open, readdir, rename, unlink } from 'node:fs/promises';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+ import { DataStreamError, validateAnyDataStreamDocument } from './data-stream-document.js';
6
+ import { inspectSegmentPath, verifyTemporaryDirectory } from '../transport/project-metadata.js';
7
+ import { isVerbatimDocument, previousIdentity, serializeVerbatimDocument, } from './verbatim-document.js';
8
+ /**
9
+ * Validate real ancestors 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 DataStreamError('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 DataStreamError('UNSAFE_PATH');
34
+ }
35
+ }
36
+ /**
37
+ * Bind existing bytes to one unchanged regular file.
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 DataStreamError('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 DataStreamError('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 DataStreamError('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
+ * Refuse cross-platform filename aliases, including case and trailing-space variants.
86
+ * @param {string} directory Publication directory.
87
+ * @param {string} filename Exact artifact filename.
88
+ */
89
+ async function checkFilename(directory, filename) {
90
+ for (const entry of await readdir(directory)) {
91
+ if (entry.replace(/[ .]+$/u, '').toLowerCase() === filename.toLowerCase() && entry !== filename)
92
+ throw new DataStreamError('STREAM_FILENAME_COLLISION');
93
+ }
94
+ }
95
+ /**
96
+ * Publish retained stream evidence with bounded atomic publication; no recursive cleanup.
97
+ * @param {unknown} value Portable definition, not a native request body.
98
+ * @param {string} projectRoot Explicit absolute project root.
99
+ * @param {boolean} overwrite Permit replacing different bytes of the same identity.
100
+ * @returns {Promise<{path: string; outcome: 'retrieved' | 'unchanged'}>} Publication receipt.
101
+ */
102
+ export async function writeDataStreamDocument(value, projectRoot, overwrite) {
103
+ const verbatim = isVerbatimDocument(value, 'data-stream') ? value : undefined;
104
+ const document = verbatim ?? validateAnyDataStreamDocument(value, 'retrieve');
105
+ const bytes = Buffer.from(verbatim ? serializeVerbatimDocument(verbatim) : `${JSON.stringify(document, undefined, 2)}\n`);
106
+ if (!path.isAbsolute(projectRoot) ||
107
+ projectRoot !== projectRoot.trim() ||
108
+ /[*?\0]/u.test(projectRoot) ||
109
+ projectRoot.split(/[\\/]/u).includes('..') ||
110
+ bytes.length > 1_048_576)
111
+ throw new DataStreamError('UNSAFE_PATH');
112
+ const root = path.resolve(projectRoot);
113
+ if (path.dirname(root) === root)
114
+ throw new DataStreamError('UNSAFE_PATH');
115
+ const filename = `${document.identity.apiName}.data-stream-meta.json`;
116
+ const directory = path.join(root, 'data360', 'data-stream');
117
+ const destination = path.join(directory, filename);
118
+ let published = false;
119
+ try {
120
+ await ensure(directory);
121
+ await checkFilename(directory, filename);
122
+ const previous = await existing(destination);
123
+ if (previous?.equals(bytes))
124
+ return { path: destination, outcome: 'unchanged' };
125
+ if (previous) {
126
+ const saved = previousIdentity(previous.toString('utf8'), (previousValue) => validateAnyDataStreamDocument(previousValue).identity);
127
+ if (saved.apiName !== undefined && saved.apiName !== document.identity.apiName)
128
+ throw new DataStreamError('STREAM_FILENAME_COLLISION');
129
+ if (!overwrite)
130
+ throw new DataStreamError('OVERWRITE_REQUIRED');
131
+ }
132
+ verifyTemporaryDirectory();
133
+ const staging = await mkdtemp(path.join(os.tmpdir(), 'data360-stream-output-'));
134
+ const marker = await open(path.join(staging, 'ownership'), 'wx');
135
+ try {
136
+ await marker.writeFile(randomUUID());
137
+ }
138
+ finally {
139
+ await marker.close();
140
+ }
141
+ const stagedPath = path.join(staging, 'document.json');
142
+ const handle = await open(stagedPath, 'wx');
143
+ try {
144
+ await handle.writeFile(bytes);
145
+ await handle.sync();
146
+ }
147
+ finally {
148
+ await handle.close();
149
+ }
150
+ await ensure(directory);
151
+ await checkFilename(directory, filename);
152
+ const current = await existing(destination);
153
+ if (previous === undefined ? current !== undefined : !current?.equals(previous))
154
+ throw new DataStreamError('OUTPUT_CHANGED');
155
+ if (previous)
156
+ await rename(stagedPath, destination);
157
+ else {
158
+ await link(stagedPath, destination);
159
+ published = true;
160
+ await unlink(stagedPath);
161
+ }
162
+ published = true;
163
+ const completed = await existing(destination);
164
+ if (!completed?.equals(bytes))
165
+ throw new DataStreamError('OUTPUT_CHANGED');
166
+ return { path: destination, outcome: 'retrieved' };
167
+ }
168
+ catch (error) {
169
+ if (published)
170
+ throw new DataStreamError('PARTIAL_STREAM_OUTPUT');
171
+ if (error instanceof DataStreamError)
172
+ throw error;
173
+ throw new DataStreamError('OUTPUT_FAILED');
174
+ }
175
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Publish authored Transform input separately from runtime observations.
3
+ * @param {unknown} value Qualified portable document.
4
+ * @param {string} projectRoot Absolute output root.
5
+ * @param {boolean} overwrite Permit differing existing bytes.
6
+ * @returns {Promise<{path: string; outcome: 'retrieved' | 'unchanged'}>} Exact publication receipt.
7
+ */
8
+ export declare function writeDataTransformDocument(value: unknown, projectRoot: string, overwrite: boolean): Promise<{
9
+ path: string;
10
+ outcome: 'retrieved' | 'unchanged';
11
+ }>;
@@ -0,0 +1,180 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { link, mkdir, mkdtemp, open, readdir, rename, unlink } from 'node:fs/promises';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+ import { DataTransformError, serializeDataTransformDocument, validateDataTransformDocument, } from './data-transform-document.js';
6
+ import { inspectSegmentPath, verifyTemporaryDirectory } from '../transport/project-metadata.js';
7
+ import { isVerbatimDocument, previousIdentity, serializeVerbatimDocument, } from './verbatim-document.js';
8
+ import { dataTransformArtifactPath } from '../services/retrieve-data-transform.js';
9
+ /**
10
+ * Create missing real directories without accepting linked ancestors.
11
+ * @param {string} directory Exact destination 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 DataTransformError('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 DataTransformError('UNSAFE_PATH');
35
+ }
36
+ }
37
+ /**
38
+ * Read bounded bytes bound to an unchanged regular single-link destination.
39
+ * @param {string} file Exact destination.
40
+ * @returns {Promise<Buffer | undefined>} Existing bytes or confirmed 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 DataTransformError('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 DataTransformError('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 DataTransformError('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
+ * Refuse case and trailing-space filename aliases across platforms.
87
+ * @param {string} directory Publication directory.
88
+ * @param {string} filename Exact artifact filename.
89
+ */
90
+ async function checkFilename(directory, filename) {
91
+ for (const entry of await readdir(directory))
92
+ if (entry.replace(/[ .]+$/u, '').toLowerCase() === filename.toLowerCase() && entry !== filename)
93
+ throw new DataTransformError('TRANSFORM_FILENAME_COLLISION');
94
+ }
95
+ /**
96
+ * Publish authored Transform input separately from runtime observations.
97
+ * @param {unknown} value Qualified portable document.
98
+ * @param {string} projectRoot Absolute output root.
99
+ * @param {boolean} overwrite Permit differing existing bytes.
100
+ * @returns {Promise<{path: string; outcome: 'retrieved' | 'unchanged'}>} Exact publication receipt.
101
+ */
102
+ export async function writeDataTransformDocument(value, projectRoot, overwrite) {
103
+ const verbatim = isVerbatimDocument(value, 'data-transform') ? value : undefined;
104
+ const document = verbatim ? undefined : validateDataTransformDocument(value, 'retrieve');
105
+ const name = verbatim ? verbatim.identity.apiName : document.definition.name;
106
+ const bytes = Buffer.from(verbatim
107
+ ? serializeVerbatimDocument(verbatim)
108
+ : serializeDataTransformDocument(document, 'retrieve'));
109
+ if (!path.isAbsolute(projectRoot) ||
110
+ projectRoot !== projectRoot.trim() ||
111
+ /[*?\0]/u.test(projectRoot) ||
112
+ projectRoot.split(/[\\/]/u).includes('..') ||
113
+ bytes.length > 1_048_576)
114
+ throw new DataTransformError('UNSAFE_PATH');
115
+ const root = path.resolve(projectRoot);
116
+ if (path.dirname(root) === root)
117
+ throw new DataTransformError('UNSAFE_PATH');
118
+ const destination = path.join(root, dataTransformArtifactPath(name));
119
+ const directory = path.dirname(destination);
120
+ const filename = path.basename(destination);
121
+ let published = false;
122
+ try {
123
+ await ensure(directory);
124
+ await checkFilename(directory, filename);
125
+ const previous = await existing(destination);
126
+ if (previous?.equals(bytes))
127
+ return { path: destination, outcome: 'unchanged' };
128
+ if (previous) {
129
+ const saved = previousIdentity(previous.toString('utf8'), (previousValue) => ({
130
+ apiName: validateDataTransformDocument(previousValue, 'retrieve').definition.name,
131
+ }));
132
+ if (saved.apiName !== undefined && saved.apiName !== name)
133
+ throw new DataTransformError('TRANSFORM_FILENAME_COLLISION');
134
+ if (!overwrite)
135
+ throw new DataTransformError('OVERWRITE_REQUIRED');
136
+ }
137
+ verifyTemporaryDirectory();
138
+ const staging = await mkdtemp(path.join(os.tmpdir(), 'data360-transform-output-'));
139
+ const marker = await open(path.join(staging, 'ownership'), 'wx');
140
+ try {
141
+ await marker.writeFile(randomUUID());
142
+ }
143
+ finally {
144
+ await marker.close();
145
+ }
146
+ const staged = path.join(staging, 'document.json');
147
+ const handle = await open(staged, 'wx');
148
+ try {
149
+ await handle.writeFile(bytes);
150
+ await handle.sync();
151
+ }
152
+ finally {
153
+ await handle.close();
154
+ }
155
+ await ensure(directory);
156
+ await checkFilename(directory, filename);
157
+ const current = await existing(destination);
158
+ if (previous === undefined ? current !== undefined : !current?.equals(previous))
159
+ throw new DataTransformError('OUTPUT_CHANGED');
160
+ if (previous)
161
+ await rename(staged, destination);
162
+ else {
163
+ await link(staged, destination);
164
+ published = true;
165
+ await unlink(staged);
166
+ }
167
+ published = true;
168
+ const completed = await existing(destination);
169
+ if (!completed?.equals(bytes))
170
+ throw new DataTransformError('OUTPUT_CHANGED');
171
+ return { path: destination, outcome: 'retrieved' };
172
+ }
173
+ catch (error) {
174
+ if (published)
175
+ throw new DataTransformError('PARTIAL_TRANSFORM_OUTPUT');
176
+ if (error instanceof DataTransformError)
177
+ throw error;
178
+ throw new DataTransformError('OUTPUT_FAILED');
179
+ }
180
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Publish portable API68 DLO JSON with no-clobber creation and guarded replacement.
3
+ * @param {unknown} value Portable document without source IDs.
4
+ * @param {string} projectRoot Explicit project root.
5
+ * @param {boolean} overwrite Permit replacing differing bytes.
6
+ * @returns {Promise<{path: string; outcome: 'retrieved' | 'unchanged'}>} Verified publication.
7
+ */
8
+ export declare function writeDloDocument(value: unknown, projectRoot: string, overwrite: boolean): Promise<{
9
+ path: string;
10
+ outcome: 'retrieved' | 'unchanged';
11
+ }>;