sf-plugin-data 0.0.0-stage → 0.1.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 +257 -2
- package/bin/run.js +4 -0
- package/docs/artifact-format.md +633 -0
- package/docs/capabilities.md +217 -0
- package/docs/git-workflow.md +37 -0
- package/docs/handover.md +2632 -0
- package/lib/artifacts/dmo-document.d.ts +181 -0
- package/lib/artifacts/dmo-document.js +848 -0
- package/lib/artifacts/dmo-update-delta.d.ts +28 -0
- package/lib/artifacts/dmo-update-delta.js +86 -0
- package/lib/artifacts/read-deploy-input.d.ts +23 -0
- package/lib/artifacts/read-deploy-input.js +199 -0
- package/lib/artifacts/write-other-dmo.d.ts +17 -0
- package/lib/artifacts/write-other-dmo.js +140 -0
- package/lib/commands/data360/deploy.d.ts +84 -0
- package/lib/commands/data360/deploy.js +208 -0
- package/lib/commands/data360/retrieve.d.ts +75 -0
- package/lib/commands/data360/retrieve.js +168 -0
- package/lib/plugin.d.ts +4 -0
- package/lib/plugin.js +3 -0
- package/lib/services/execute-dmo-create.d.ts +24 -0
- package/lib/services/execute-dmo-create.js +118 -0
- package/lib/services/execute-dmo-deploy.d.ts +33 -0
- package/lib/services/execute-dmo-deploy.js +197 -0
- package/lib/services/preflight-dmo-deploy.d.ts +69 -0
- package/lib/services/preflight-dmo-deploy.js +161 -0
- package/lib/services/retrieve-other-dmo.d.ts +46 -0
- package/lib/services/retrieve-other-dmo.js +140 -0
- package/lib/transport/connect-client.d.ts +67 -0
- package/lib/transport/connect-client.js +306 -0
- package/package.json +73 -4
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { DmoDocument, DmoField } from './dmo-document.js';
|
|
2
|
+
export type DmoUpdatePatch = {
|
|
3
|
+
label?: string;
|
|
4
|
+
description?: string;
|
|
5
|
+
fields?: Array<Pick<DmoField, 'name' | 'label' | 'dataType' | 'description' | 'category'>>;
|
|
6
|
+
};
|
|
7
|
+
export type DmoUpdateDelta = {
|
|
8
|
+
status: 'blocked';
|
|
9
|
+
blockers: string[];
|
|
10
|
+
} | {
|
|
11
|
+
status: 'unchanged';
|
|
12
|
+
blockers: [];
|
|
13
|
+
expected: DmoDocument;
|
|
14
|
+
} | {
|
|
15
|
+
status: 'candidate';
|
|
16
|
+
blockers: [];
|
|
17
|
+
patch: DmoUpdatePatch;
|
|
18
|
+
expected: DmoDocument;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Build an offline PATCH contract candidate, never execution authorization.
|
|
22
|
+
* Current must already be qualified by the caller; authored JSON alone is not org evidence.
|
|
23
|
+
* @param {unknown} desired Authored desired document.
|
|
24
|
+
* @param {unknown} current Qualified current authored document.
|
|
25
|
+
* @param {unknown} targetDataSpace Explicit default binding.
|
|
26
|
+
* @returns {DmoUpdateDelta} Atomic candidate/overlay or payload-free blockers only.
|
|
27
|
+
*/
|
|
28
|
+
export declare function buildDmoUpdateDelta(desired: unknown, current: unknown, targetDataSpace: unknown): DmoUpdateDelta;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { validateDmoDocument } from './dmo-document.js';
|
|
2
|
+
/**
|
|
3
|
+
* Build an offline PATCH contract candidate, never execution authorization.
|
|
4
|
+
* Current must already be qualified by the caller; authored JSON alone is not org evidence.
|
|
5
|
+
* @param {unknown} desired Authored desired document.
|
|
6
|
+
* @param {unknown} current Qualified current authored document.
|
|
7
|
+
* @param {unknown} targetDataSpace Explicit default binding.
|
|
8
|
+
* @returns {DmoUpdateDelta} Atomic candidate/overlay or payload-free blockers only.
|
|
9
|
+
*/
|
|
10
|
+
export function buildDmoUpdateDelta(desired, current, targetDataSpace) {
|
|
11
|
+
const source = validateDmoDocument(desired);
|
|
12
|
+
const target = validateDmoDocument(current);
|
|
13
|
+
if (targetDataSpace !== 'default' ||
|
|
14
|
+
source.identity.dataSpace !== 'default' ||
|
|
15
|
+
target.identity.dataSpace !== 'default')
|
|
16
|
+
return { status: 'blocked', blockers: ['INVALID_SCOPE'] };
|
|
17
|
+
if (source.identity.apiName !== target.identity.apiName)
|
|
18
|
+
return { status: 'blocked', blockers: ['OBJECT_IDENTITY_CHANGED'] };
|
|
19
|
+
const blockers = new Set();
|
|
20
|
+
const patch = {};
|
|
21
|
+
const expected = structuredClone(target);
|
|
22
|
+
if (source.spec.category !== target.spec.category)
|
|
23
|
+
blockers.add('CATEGORY_CHANGED');
|
|
24
|
+
for (const key of ['eventDateTimeFieldName', 'recordModifiedFieldName'])
|
|
25
|
+
if (source.spec[key] !== target.spec[key])
|
|
26
|
+
blockers.add('DATE_DESIGNATION_CHANGED');
|
|
27
|
+
if (source.spec.label !== target.spec.label) {
|
|
28
|
+
patch.label = source.spec.label;
|
|
29
|
+
expected.spec.label = source.spec.label;
|
|
30
|
+
}
|
|
31
|
+
if ('description' in source.spec && source.spec.description !== target.spec.description) {
|
|
32
|
+
if (source.spec.description?.trim()) {
|
|
33
|
+
patch.description = source.spec.description;
|
|
34
|
+
expected.spec.description = source.spec.description;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
blockers.add('DESCRIPTION_CLEAR_UNPROVEN');
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const existing = new Map(expected.spec.fields.map((field) => [field.name, field]));
|
|
41
|
+
const foldedNames = new Set(target.spec.fields.map((field) => field.name.toLowerCase()));
|
|
42
|
+
const changes = [];
|
|
43
|
+
// Sort a fresh array so candidate order does not depend on the desired field order.
|
|
44
|
+
// eslint-disable-next-line unicorn/no-array-sort -- The configured TypeScript target lacks toSorted.
|
|
45
|
+
const fields = [...source.spec.fields].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
46
|
+
for (const field of fields) {
|
|
47
|
+
const prior = existing.get(field.name);
|
|
48
|
+
if (!prior) {
|
|
49
|
+
if (foldedNames.has(field.name.toLowerCase()))
|
|
50
|
+
blockers.add('FIELD_IDENTITY_CHANGED');
|
|
51
|
+
if (field.isPrimaryKey || field.isDynamicLookup)
|
|
52
|
+
blockers.add('NEW_FIELD_FLAGS_UNSUPPORTED');
|
|
53
|
+
if ('description' in field && !field.description?.trim())
|
|
54
|
+
blockers.add('DESCRIPTION_CLEAR_UNPROVEN');
|
|
55
|
+
const addition = {
|
|
56
|
+
name: field.name,
|
|
57
|
+
label: field.label,
|
|
58
|
+
dataType: field.dataType,
|
|
59
|
+
...('description' in field ? { description: field.description } : {}),
|
|
60
|
+
...('category' in field ? { category: field.category } : {}),
|
|
61
|
+
};
|
|
62
|
+
changes.push(addition);
|
|
63
|
+
expected.spec.fields.push(structuredClone(field));
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (field.dataType !== prior.dataType ||
|
|
67
|
+
field.isPrimaryKey !== prior.isPrimaryKey ||
|
|
68
|
+
field.isDynamicLookup !== prior.isDynamicLookup ||
|
|
69
|
+
field.category !== prior.category ||
|
|
70
|
+
'category' in field !== 'category' in prior ||
|
|
71
|
+
('description' in field && field.description !== prior.description))
|
|
72
|
+
blockers.add('FIELD_CONFIGURATION_UNSUPPORTED');
|
|
73
|
+
if (field.label !== prior.label) {
|
|
74
|
+
// Provisional field-label contract: exact name and unchanged dataType identify the field.
|
|
75
|
+
changes.push({ name: field.name, label: field.label, dataType: field.dataType });
|
|
76
|
+
prior.label = field.label;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (blockers.size > 0)
|
|
80
|
+
return { status: 'blocked', blockers: [...blockers] };
|
|
81
|
+
if (changes.length > 0)
|
|
82
|
+
patch.fields = changes;
|
|
83
|
+
if (Object.keys(patch).length === 0)
|
|
84
|
+
return { status: 'unchanged', blockers: [], expected };
|
|
85
|
+
return { status: 'candidate', blockers: [], patch, expected };
|
|
86
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { DmoDocument } from './dmo-document.js';
|
|
2
|
+
/** Payload-free local selection refusal. */
|
|
3
|
+
export declare class DeployInputError extends Error {
|
|
4
|
+
readonly code: 'INVALID_SELECTION' | 'UNSAFE_SOURCE' | 'INVALID_SOURCE' | 'DUPLICATE_IDENTITY';
|
|
5
|
+
constructor(code: 'INVALID_SELECTION' | 'UNSAFE_SOURCE' | 'INVALID_SOURCE' | 'DUPLICATE_IDENTITY');
|
|
6
|
+
}
|
|
7
|
+
export type DeployInput = {
|
|
8
|
+
path: string;
|
|
9
|
+
document: DmoDocument;
|
|
10
|
+
};
|
|
11
|
+
export type DeployInputFlags = {
|
|
12
|
+
cwd: string;
|
|
13
|
+
sourceDir?: readonly string[];
|
|
14
|
+
metadata?: readonly string[];
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Resolve explicit authored files/directories or exact project-relative DMO selectors, read-only.
|
|
18
|
+
* Limits are 256 files, 1 MiB per file, 1024 visited entries and 16 directory levels.
|
|
19
|
+
* Repeated identical selections coalesce; different files claiming one identity fail closed.
|
|
20
|
+
* @param {DeployInputFlags} flags Explicit mutually exclusive source modes and captured cwd.
|
|
21
|
+
* @returns {Promise<DeployInput[]>} Sorted, independently validated authored inputs.
|
|
22
|
+
*/
|
|
23
|
+
export declare function readDeployInputs(flags: DeployInputFlags): Promise<DeployInput[]>;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { lstat, open, readdir, realpath } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { buildDmoCreateRequestFromDocument, validateDmoDocument } from './dmo-document.js';
|
|
4
|
+
/** Payload-free local selection refusal. */
|
|
5
|
+
export class DeployInputError extends Error {
|
|
6
|
+
code;
|
|
7
|
+
constructor(code) {
|
|
8
|
+
super('Select explicit, bounded, authored-only DMO sources without links or conflicting identities.');
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.name = 'DeployInputError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const refuse = () => {
|
|
14
|
+
throw new DeployInputError('UNSAFE_SOURCE');
|
|
15
|
+
};
|
|
16
|
+
const canonical = (value) => process.platform === 'win32' ? value.toLowerCase() : value;
|
|
17
|
+
/**
|
|
18
|
+
* Inspect every path component without admitting symlinks, junctions or canonical aliases.
|
|
19
|
+
* @param {string} file Absolute selected path.
|
|
20
|
+
* @returns {Promise<import('node:fs').Stats>} Checked filesystem entry.
|
|
21
|
+
*/
|
|
22
|
+
async function inspect(file) {
|
|
23
|
+
const parent = path.dirname(file);
|
|
24
|
+
if (parent !== file) {
|
|
25
|
+
const ancestor = await inspect(parent);
|
|
26
|
+
if (!ancestor.isDirectory())
|
|
27
|
+
refuse();
|
|
28
|
+
}
|
|
29
|
+
const stat = await lstat(file);
|
|
30
|
+
if (stat.isSymbolicLink() || canonical(await realpath(file)) !== canonical(file))
|
|
31
|
+
refuse();
|
|
32
|
+
return stat;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Parse JSON without silently accepting duplicate or escaped-equivalent object keys.
|
|
36
|
+
* @param {string} text Complete UTF-8 source.
|
|
37
|
+
* @returns {unknown} Parsed JSON value.
|
|
38
|
+
*/
|
|
39
|
+
function strictJson(text) {
|
|
40
|
+
const value = JSON.parse(text);
|
|
41
|
+
const stack = [];
|
|
42
|
+
const tokens = /"(?:[^"\\]|\\.)*"|[{}[\]]/gu;
|
|
43
|
+
for (const token of text.matchAll(tokens)) {
|
|
44
|
+
switch (token[0]) {
|
|
45
|
+
case '{': {
|
|
46
|
+
stack.push(new Set());
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
case '[': {
|
|
50
|
+
stack.push(undefined);
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
case '}':
|
|
54
|
+
case ']': {
|
|
55
|
+
stack.pop();
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
default: {
|
|
59
|
+
if (/^\s*:/u.test(text.slice(token.index + token[0].length))) {
|
|
60
|
+
const key = JSON.parse(token[0]);
|
|
61
|
+
const keys = stack.at(-1);
|
|
62
|
+
if (keys.has(key))
|
|
63
|
+
throw new DeployInputError('INVALID_SOURCE');
|
|
64
|
+
keys.add(key);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return value;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Resolve explicit authored files/directories or exact project-relative DMO selectors, read-only.
|
|
73
|
+
* Limits are 256 files, 1 MiB per file, 1024 visited entries and 16 directory levels.
|
|
74
|
+
* Repeated identical selections coalesce; different files claiming one identity fail closed.
|
|
75
|
+
* @param {DeployInputFlags} flags Explicit mutually exclusive source modes and captured cwd.
|
|
76
|
+
* @returns {Promise<DeployInput[]>} Sorted, independently validated authored inputs.
|
|
77
|
+
*/
|
|
78
|
+
export async function readDeployInputs(flags) {
|
|
79
|
+
const { sourceDir, metadata } = flags;
|
|
80
|
+
const values = sourceDir ?? metadata;
|
|
81
|
+
if ((sourceDir === undefined) === (metadata === undefined) ||
|
|
82
|
+
!values?.length ||
|
|
83
|
+
values.some((value) => typeof value !== 'string' || !value.trim() || value !== value.trim()))
|
|
84
|
+
throw new DeployInputError('INVALID_SELECTION');
|
|
85
|
+
if (!path.isAbsolute(flags.cwd))
|
|
86
|
+
throw new DeployInputError('INVALID_SELECTION');
|
|
87
|
+
const selectors = metadata?.map((value) => {
|
|
88
|
+
if (!/^dmo:[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*__dlm$/u.test(value))
|
|
89
|
+
throw new DeployInputError('INVALID_SELECTION');
|
|
90
|
+
return value.slice(4);
|
|
91
|
+
});
|
|
92
|
+
const roots = selectors
|
|
93
|
+
? selectors.map((name) => path.join(flags.cwd, 'data360', 'default', 'dmo', `${name}.json`))
|
|
94
|
+
: sourceDir.map((value) => {
|
|
95
|
+
if (value.includes(String.fromCodePoint(0)) ||
|
|
96
|
+
/[*?]/u.test(value) ||
|
|
97
|
+
value.split(/[\\/]/u).includes('..') ||
|
|
98
|
+
/^[A-Za-z]:[^\\/]/u.test(value))
|
|
99
|
+
throw new DeployInputError('INVALID_SELECTION');
|
|
100
|
+
return path.resolve(flags.cwd, value);
|
|
101
|
+
});
|
|
102
|
+
const candidates = new Map();
|
|
103
|
+
let visited = 0;
|
|
104
|
+
const walk = async (file, depth) => {
|
|
105
|
+
if (++visited > 1024 || depth > 16)
|
|
106
|
+
refuse();
|
|
107
|
+
const stat = await inspect(file);
|
|
108
|
+
if (stat.isDirectory()) {
|
|
109
|
+
if (path.dirname(file) === file)
|
|
110
|
+
refuse();
|
|
111
|
+
const entries = await readdir(file);
|
|
112
|
+
// eslint-disable-next-line unicorn/no-array-sort -- Sort the fresh directory listing within the existing TypeScript library target.
|
|
113
|
+
for (const entry of entries.sort())
|
|
114
|
+
await walk(path.join(file, entry), depth + 1);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
if (!stat.isFile() ||
|
|
118
|
+
path.extname(file) !== '.json' ||
|
|
119
|
+
stat.nlink !== 1 ||
|
|
120
|
+
stat.size > 1_048_576)
|
|
121
|
+
refuse();
|
|
122
|
+
candidates.set(canonical(file), file);
|
|
123
|
+
if (candidates.size > 256)
|
|
124
|
+
refuse();
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
try {
|
|
128
|
+
for (const root of new Set(roots)) {
|
|
129
|
+
if (selectors) {
|
|
130
|
+
const selected = await lstat(root);
|
|
131
|
+
if (!selected.isFile())
|
|
132
|
+
refuse();
|
|
133
|
+
}
|
|
134
|
+
await walk(root, 0);
|
|
135
|
+
}
|
|
136
|
+
if (candidates.size === 0)
|
|
137
|
+
throw new DeployInputError('INVALID_SELECTION');
|
|
138
|
+
const inputs = [];
|
|
139
|
+
const identities = new Set();
|
|
140
|
+
// eslint-disable-next-line unicorn/no-array-sort -- Sort a fresh candidate array within the existing TypeScript library target.
|
|
141
|
+
for (const file of [...candidates.values()].sort()) {
|
|
142
|
+
const before = await inspect(file);
|
|
143
|
+
const handle = await open(file, 'r');
|
|
144
|
+
let text;
|
|
145
|
+
try {
|
|
146
|
+
const opened = await handle.stat();
|
|
147
|
+
if (!opened.isFile() ||
|
|
148
|
+
opened.nlink !== 1 ||
|
|
149
|
+
opened.dev !== before.dev ||
|
|
150
|
+
opened.ino !== before.ino ||
|
|
151
|
+
opened.size !== before.size)
|
|
152
|
+
refuse();
|
|
153
|
+
const buffer = Buffer.alloc(1_048_577);
|
|
154
|
+
let length = 0;
|
|
155
|
+
while (length < buffer.length) {
|
|
156
|
+
const { bytesRead } = await handle.read(buffer, length, buffer.length - length, length);
|
|
157
|
+
if (bytesRead === 0)
|
|
158
|
+
break;
|
|
159
|
+
length += bytesRead;
|
|
160
|
+
}
|
|
161
|
+
const after = await handle.stat();
|
|
162
|
+
if (length > 1_048_576 ||
|
|
163
|
+
after.size !== before.size ||
|
|
164
|
+
after.mtimeMs !== before.mtimeMs ||
|
|
165
|
+
after.ctimeMs !== before.ctimeMs)
|
|
166
|
+
refuse();
|
|
167
|
+
await inspect(file);
|
|
168
|
+
text = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(buffer.subarray(0, length));
|
|
169
|
+
}
|
|
170
|
+
finally {
|
|
171
|
+
await handle.close();
|
|
172
|
+
}
|
|
173
|
+
let document;
|
|
174
|
+
try {
|
|
175
|
+
const parsed = strictJson(text);
|
|
176
|
+
buildDmoCreateRequestFromDocument(parsed, 'default');
|
|
177
|
+
document = validateDmoDocument(parsed);
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
throw new DeployInputError('INVALID_SOURCE');
|
|
181
|
+
}
|
|
182
|
+
if (selectors && !selectors.includes(document.identity.apiName))
|
|
183
|
+
throw new DeployInputError('INVALID_SELECTION');
|
|
184
|
+
if (selectors && path.basename(file) !== `${document.identity.apiName}.json`)
|
|
185
|
+
throw new DeployInputError('INVALID_SELECTION');
|
|
186
|
+
const identity = `${document.identity.dataSpace}:${document.identity.apiName}`.toLowerCase();
|
|
187
|
+
if (identities.has(identity))
|
|
188
|
+
throw new DeployInputError('DUPLICATE_IDENTITY');
|
|
189
|
+
identities.add(identity);
|
|
190
|
+
inputs.push({ path: file, document });
|
|
191
|
+
}
|
|
192
|
+
return inputs;
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
if (error instanceof DeployInputError)
|
|
196
|
+
throw error;
|
|
197
|
+
throw new DeployInputError('UNSAFE_SOURCE');
|
|
198
|
+
}
|
|
199
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Payload-free filesystem refusals, including failed atomic publication. */
|
|
2
|
+
export declare class DmoWriteError extends Error {
|
|
3
|
+
readonly code: 'UNSAFE_PATH' | 'OVERWRITE_REQUIRED' | 'OUTPUT_FAILED';
|
|
4
|
+
constructor(code: 'UNSAFE_PATH' | 'OVERWRITE_REQUIRED' | 'OUTPUT_FAILED');
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Publish only independently validated authored DMO/default JSON, with atomic file visibility.
|
|
8
|
+
* Staging is retained in owned OS temp; cross-volume atomic publication fails closed.
|
|
9
|
+
* @param {unknown} value Authored candidate, never the runtime projection.
|
|
10
|
+
* @param {string} projectRoot Output project root (the CLI supplies cwd when omitted).
|
|
11
|
+
* @param {boolean} overwrite Permit replacing differing existing regular-file bytes.
|
|
12
|
+
* @returns {Promise<{ path: string; outcome: 'retrieved' | 'unchanged' }>} Exact path and outcome.
|
|
13
|
+
*/
|
|
14
|
+
export declare function writeDmoDocument(value: unknown, projectRoot: string, overwrite: boolean): Promise<{
|
|
15
|
+
path: string;
|
|
16
|
+
outcome: 'retrieved' | 'unchanged';
|
|
17
|
+
}>;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { lstat, mkdir, mkdtemp, open, readFile, realpath, rename, link, unlink, } from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { buildDmoCreateRequestFromDocument, serializeDmoDocument, validateDmoDocument, } from './dmo-document.js';
|
|
6
|
+
/** Payload-free filesystem refusals, including failed atomic publication. */
|
|
7
|
+
export class DmoWriteError extends Error {
|
|
8
|
+
code;
|
|
9
|
+
constructor(code) {
|
|
10
|
+
super('DMO output could not be completed; inspect the selected destination before retrying.');
|
|
11
|
+
this.code = code;
|
|
12
|
+
this.name = 'DmoWriteError';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Check every existing ancestor without following links, creating directories one at a time.
|
|
17
|
+
* @param {string} directory Absolute directory to validate or create.
|
|
18
|
+
* @returns {Promise<void>} Resolves only for a real directory chain.
|
|
19
|
+
*/
|
|
20
|
+
async function ensureDirectory(directory) {
|
|
21
|
+
const parent = path.dirname(directory);
|
|
22
|
+
if (parent !== directory)
|
|
23
|
+
await ensureDirectory(parent);
|
|
24
|
+
try {
|
|
25
|
+
await lstat(directory);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
if (error.code !== 'ENOENT')
|
|
29
|
+
throw error;
|
|
30
|
+
try {
|
|
31
|
+
await mkdir(directory);
|
|
32
|
+
}
|
|
33
|
+
catch (creationError) {
|
|
34
|
+
if (creationError.code !== 'EEXIST')
|
|
35
|
+
throw creationError;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const stat = await lstat(directory);
|
|
39
|
+
const actual = path.resolve(await realpath(directory));
|
|
40
|
+
const matches = process.platform === 'win32'
|
|
41
|
+
? actual.toLowerCase() === directory.toLowerCase()
|
|
42
|
+
: actual === directory;
|
|
43
|
+
if (stat.isSymbolicLink() || !stat.isDirectory() || !matches)
|
|
44
|
+
throw new DmoWriteError('UNSAFE_PATH');
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Read only an existing regular file, never a directory or link.
|
|
48
|
+
* @param {string} file Exact destination.
|
|
49
|
+
* @returns {Promise<Buffer | undefined>} Existing bytes or explicit absence.
|
|
50
|
+
*/
|
|
51
|
+
async function existingBytes(file) {
|
|
52
|
+
try {
|
|
53
|
+
const stat = await lstat(file);
|
|
54
|
+
if (stat.isSymbolicLink() || !stat.isFile())
|
|
55
|
+
throw new DmoWriteError('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 only independently validated authored DMO/default JSON, with atomic file visibility.
|
|
66
|
+
* Staging is retained in owned OS temp; cross-volume atomic publication fails closed.
|
|
67
|
+
* @param {unknown} value Authored candidate, never the runtime projection.
|
|
68
|
+
* @param {string} projectRoot Output project root (the CLI supplies cwd when omitted).
|
|
69
|
+
* @param {boolean} overwrite Permit replacing differing existing regular-file bytes.
|
|
70
|
+
* @returns {Promise<{ path: string; outcome: 'retrieved' | 'unchanged' }>} Exact path and outcome.
|
|
71
|
+
*/
|
|
72
|
+
export async function writeDmoDocument(value, projectRoot, overwrite) {
|
|
73
|
+
// Validate the standalone candidate independently before any filesystem side effect.
|
|
74
|
+
buildDmoCreateRequestFromDocument(value, 'default');
|
|
75
|
+
const document = validateDmoDocument(value);
|
|
76
|
+
const bytes = Buffer.from(serializeDmoDocument(document), 'utf8');
|
|
77
|
+
if (!projectRoot.trim())
|
|
78
|
+
throw new DmoWriteError('UNSAFE_PATH');
|
|
79
|
+
const root = path.resolve(projectRoot);
|
|
80
|
+
const destination = path.join(root, 'data360', 'default', 'dmo', `${document.identity.apiName}.json`);
|
|
81
|
+
const relative = path.relative(root, destination);
|
|
82
|
+
if (!relative || relative.startsWith('..') || path.isAbsolute(relative))
|
|
83
|
+
throw new DmoWriteError('UNSAFE_PATH');
|
|
84
|
+
try {
|
|
85
|
+
await ensureDirectory(path.dirname(destination));
|
|
86
|
+
const previous = await existingBytes(destination);
|
|
87
|
+
if (previous?.equals(bytes))
|
|
88
|
+
return { path: destination, outcome: 'unchanged' };
|
|
89
|
+
if (previous && !overwrite)
|
|
90
|
+
throw new DmoWriteError('OVERWRITE_REQUIRED');
|
|
91
|
+
// Stage complete flushed bytes under the verified OS temp directory, never beside user files.
|
|
92
|
+
if (process.platform === 'win32') {
|
|
93
|
+
for (const value of [
|
|
94
|
+
process.env.TEMP,
|
|
95
|
+
os.tmpdir(),
|
|
96
|
+
process.env.TMP,
|
|
97
|
+
process.env.TMPDIR,
|
|
98
|
+
].filter((entry) => entry !== undefined && entry !== '')) {
|
|
99
|
+
if (!path.isAbsolute(value) ||
|
|
100
|
+
path
|
|
101
|
+
.resolve(value)
|
|
102
|
+
.replace(/[\\/]+$/u, '')
|
|
103
|
+
.toLowerCase() !== String.raw `f:\temp`)
|
|
104
|
+
throw new DmoWriteError('UNSAFE_PATH');
|
|
105
|
+
}
|
|
106
|
+
if (!process.env.TEMP)
|
|
107
|
+
throw new DmoWriteError('UNSAFE_PATH');
|
|
108
|
+
}
|
|
109
|
+
const stagingRoot = await mkdtemp(path.join(os.tmpdir(), 'data360-output-'));
|
|
110
|
+
const staged = await open(path.join(stagingRoot, 'document.json'), 'wx');
|
|
111
|
+
try {
|
|
112
|
+
await staged.writeFile(bytes);
|
|
113
|
+
await staged.sync();
|
|
114
|
+
}
|
|
115
|
+
finally {
|
|
116
|
+
await staged.close();
|
|
117
|
+
}
|
|
118
|
+
const marker = await open(path.join(stagingRoot, 'ownership'), 'wx');
|
|
119
|
+
await marker.writeFile(randomUUID());
|
|
120
|
+
await marker.close();
|
|
121
|
+
// Recheck the chain and observed bytes immediately before publication; no snapshot guarantee.
|
|
122
|
+
await ensureDirectory(path.dirname(destination));
|
|
123
|
+
const current = await existingBytes(destination);
|
|
124
|
+
if (previous === undefined ? current !== undefined : !current?.equals(previous))
|
|
125
|
+
throw new DmoWriteError('OUTPUT_FAILED');
|
|
126
|
+
if (previous)
|
|
127
|
+
await rename(path.join(stagingRoot, 'document.json'), destination);
|
|
128
|
+
else {
|
|
129
|
+
await link(path.join(stagingRoot, 'document.json'), destination);
|
|
130
|
+
// Remove only this run-created file entry so retained staging cannot alter published bytes.
|
|
131
|
+
await unlink(path.join(stagingRoot, 'document.json'));
|
|
132
|
+
}
|
|
133
|
+
return { path: destination, outcome: 'retrieved' };
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
if (error instanceof DmoWriteError)
|
|
137
|
+
throw error;
|
|
138
|
+
throw new DmoWriteError('OUTPUT_FAILED');
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { SfCommand } from '@salesforce/sf-plugins-core';
|
|
2
|
+
import { readDeployInputs } from '../../artifacts/read-deploy-input.js';
|
|
3
|
+
import { preflightDmoDeploy } from '../../services/preflight-dmo-deploy.js';
|
|
4
|
+
import type { DmoDeployPreview } from '../../services/preflight-dmo-deploy.js';
|
|
5
|
+
import { executeDmoDeploy } from '../../services/execute-dmo-deploy.js';
|
|
6
|
+
import type { DmoDeployExecution } from '../../services/execute-dmo-deploy.js';
|
|
7
|
+
type ExecutionItem = Omit<DmoDeployExecution, 'outcome'> & {
|
|
8
|
+
outcome: DmoDeployExecution['outcome'] | 'not-attempted';
|
|
9
|
+
identity: {
|
|
10
|
+
dataSpace: string;
|
|
11
|
+
apiName: string;
|
|
12
|
+
};
|
|
13
|
+
operation: 'execute';
|
|
14
|
+
path: string;
|
|
15
|
+
};
|
|
16
|
+
export type DeployEnvelope = {
|
|
17
|
+
contract: 'sf-data360-deploy';
|
|
18
|
+
contractVersion: '1.0.0';
|
|
19
|
+
status: 'success' | 'blocked' | 'failed';
|
|
20
|
+
metadata: {
|
|
21
|
+
operation: 'deploy';
|
|
22
|
+
preview: boolean;
|
|
23
|
+
plugin: {
|
|
24
|
+
name: string;
|
|
25
|
+
version: string;
|
|
26
|
+
};
|
|
27
|
+
apiVersion: string;
|
|
28
|
+
};
|
|
29
|
+
diagnostics: {
|
|
30
|
+
warnings: string[];
|
|
31
|
+
errors: Array<{
|
|
32
|
+
code: string;
|
|
33
|
+
message: string;
|
|
34
|
+
retryable: false;
|
|
35
|
+
}>;
|
|
36
|
+
};
|
|
37
|
+
provenance: {
|
|
38
|
+
producer: string;
|
|
39
|
+
targetOrgId: string | null;
|
|
40
|
+
pluginVersion: string;
|
|
41
|
+
command: string;
|
|
42
|
+
generatedAt: string;
|
|
43
|
+
};
|
|
44
|
+
result: {
|
|
45
|
+
scope: {
|
|
46
|
+
dataSpace: string;
|
|
47
|
+
};
|
|
48
|
+
selection: {
|
|
49
|
+
sourceDir: string[];
|
|
50
|
+
metadata: string[];
|
|
51
|
+
};
|
|
52
|
+
items: Array<(DmoDeployPreview & {
|
|
53
|
+
identity: {
|
|
54
|
+
dataSpace: string;
|
|
55
|
+
apiName: string;
|
|
56
|
+
};
|
|
57
|
+
operation: 'preview';
|
|
58
|
+
path: string;
|
|
59
|
+
}) | ExecutionItem>;
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
/** Preview or sequential guarded deployment of explicitly selected authored configuration. */
|
|
63
|
+
export default class Deploy extends SfCommand<DeployEnvelope> {
|
|
64
|
+
static readonly aliases: string[];
|
|
65
|
+
static readonly summary = "Deploy explicit authored Data360 configuration or preview without writes.";
|
|
66
|
+
static readonly description = "Requires explicit files/directories OR exact dmo:ApiName selectors. Repeated selections coalesce. Bare types and implicit deploy-all are forbidden. Preview is strictly read-only. Execution preflights the whole selection, then creates or updates only object and existing non-primary field labels sequentially with fresh checks and qualified readback. Other changes block before writes. Stops on first blocked or failed item; no retry or rollback. No local report/artifact writes.";
|
|
67
|
+
static readonly flags: {
|
|
68
|
+
'target-org': import("@oclif/core/interfaces").OptionFlag<import("@salesforce/core").Org, import("@oclif/core/interfaces").CustomOptions>;
|
|
69
|
+
'api-version': import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
70
|
+
'data-space': import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
|
|
71
|
+
'source-dir': import("@oclif/core/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
72
|
+
metadata: import("@oclif/core/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
73
|
+
preview: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
74
|
+
};
|
|
75
|
+
protected resolveInputs: typeof readDeployInputs;
|
|
76
|
+
protected preflight: typeof preflightDmoDeploy;
|
|
77
|
+
protected execute: typeof executeDmoDeploy;
|
|
78
|
+
/**
|
|
79
|
+
* Validate all inputs and preflight the whole selection before any sequential execution.
|
|
80
|
+
* @returns {Promise<DeployEnvelope>} Versioned preview or execution outcomes with mutation evidence.
|
|
81
|
+
*/
|
|
82
|
+
run(): Promise<DeployEnvelope>;
|
|
83
|
+
}
|
|
84
|
+
export {};
|