@diviops/mcp-server 1.5.30 → 1.5.32
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/README.md +30 -12
- package/dist/compatibility.d.ts +11 -0
- package/dist/cross-env-preflight/header-preflight.d.ts +7 -0
- package/dist/cross-env-preflight/header-preflight.js +94 -0
- package/dist/cross-env-preflight/source-payload-ref.d.ts +20 -0
- package/dist/cross-env-preflight/source-payload-ref.js +178 -0
- package/dist/index.js +238 -24
- package/dist/preset-cli/cli.d.ts +1 -1
- package/dist/preset-cli/cli.js +12 -3
- package/dist/preset-cli/nav-emitter.d.ts +17 -6
- package/dist/preset-cli/nav-emitter.js +148 -36
- package/dist/wp-cli.d.ts +10 -0
- package/dist/wp-cli.js +110 -4
- package/dist/wp-client.js +5 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -83,7 +83,7 @@ The skill enforces the Divi block format, the design system, and the response co
|
|
|
83
83
|
|
|
84
84
|
## Tools at a glance
|
|
85
85
|
|
|
86
|
-
The server exposes **
|
|
86
|
+
The server exposes **78 always-on tools** across the categories below. Each category links to representative tools; the full table lives in [server-reference.md](../docs/server-reference.md).
|
|
87
87
|
|
|
88
88
|
| Category | Use case | Tool prefixes |
|
|
89
89
|
|----------|----------|---------------|
|
|
@@ -97,6 +97,8 @@ The server exposes **77 always-on tools** across the categories below. Each cate
|
|
|
97
97
|
| WP-CLI passthrough | Escape hatch for site ops | `meta_wp_cli` |
|
|
98
98
|
| Cache + meta | Connection probe, identity, icons, cache flush | `meta_*` |
|
|
99
99
|
|
|
100
|
+
Use `diviops_meta_info` as the S0 preflight before dogfooding or product work. It returns `server_version`, a numeric `tool_count`, a `tools` catalog summary (`registered_total`, always-on count, Pro possible/registered counts by target), `plugins` version records for `diviops-agent`, `diviops-agent-pro`, FluentCart, and FluentCart Pro when available, plus the existing handshake and slice state.
|
|
101
|
+
|
|
100
102
|
Additional **conditionally-registered Pro tools** appear only on sites that have the Pro plugin (`diviops-agent-pro`) active alongside the target coverage plugin:
|
|
101
103
|
|
|
102
104
|
| Category | Conditional gate | Tool names |
|
|
@@ -172,11 +174,13 @@ The MVP supports `tb_header_layout` source payloads preflighted against an
|
|
|
172
174
|
existing `tb_header_layout` target context. It reports source-domain upload URL
|
|
173
175
|
rewrites, attachment remap status, `gcid-*` target resolution including Divi
|
|
174
176
|
built-in customizer colors, resolved target global-color value evidence,
|
|
175
|
-
off-canvas/canvas refusal, and
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
177
|
+
referenced `modulePreset` target-presence status, off-canvas/canvas refusal, and
|
|
178
|
+
the required cache cleanup plan. Each report also emits a deterministic
|
|
179
|
+
`confirmation_binding` fingerprint over the reviewed source identity/checksum,
|
|
180
|
+
target identity/current target checksum, rewrite plan, cache plan,
|
|
181
|
+
blocker/operator-action codes, reference-resolution summaries, target module
|
|
182
|
+
preset resolution, and the per-`gcid` value evidence for target colors
|
|
183
|
+
referenced by the source layout.
|
|
180
184
|
The fingerprint is reviewed-plan evidence for the Pro-gated
|
|
181
185
|
`diviops_cross_env_header_apply` tool. The CLI itself still has no apply path.
|
|
182
186
|
|
|
@@ -185,8 +189,17 @@ read-only MCP tool `diviops_cross_env_source_export_get` and save the returned
|
|
|
185
189
|
`data` object as `source.json`. The export includes the source origin, header
|
|
186
190
|
layout metadata, sanitized markup, a bare SHA-256 checksum of the exported
|
|
187
191
|
markup, export metadata, and best-effort attachment inventory from upload URLs
|
|
188
|
-
and attachment IDs. It
|
|
189
|
-
|
|
192
|
+
and attachment IDs. It also inventories referenced `attrs.modulePreset` IDs
|
|
193
|
+
without exporting preset definitions. It strips query strings, fragments,
|
|
194
|
+
credentials, nonces, cookies, signed URL material, admin URLs, and local
|
|
195
|
+
filesystem paths.
|
|
196
|
+
|
|
197
|
+
The MCP server also writes the same source payload to a bounded local artifact
|
|
198
|
+
under `.diviops-tmp/cross-env-source-payloads/` and returns
|
|
199
|
+
`data.source_payload_ref`. Use that reference for large real headers when
|
|
200
|
+
calling the Pro `diviops_cross_env_header_apply` tool; it avoids asking an LLM
|
|
201
|
+
to re-emit large markup byte-for-byte. The reference is a server-created handle
|
|
202
|
+
plus checksum, not an arbitrary filesystem path.
|
|
190
203
|
|
|
191
204
|
To collect the target JSON from the target WordPress site, call the Free/core
|
|
192
205
|
read-only MCP tool `diviops_cross_env_target_context_get` against the target
|
|
@@ -200,6 +213,9 @@ to the target state without exposing the raw target content. It also includes
|
|
|
200
213
|
`global_color_value_evidence`, a deterministic SHA-256 digest map for resolved
|
|
201
214
|
user global colors and WP Customizer-backed built-ins; preflight binds only the
|
|
202
215
|
entries that are referenced by source markup.
|
|
216
|
+
It also includes target D5 module preset IDs, without preset definitions, so the
|
|
217
|
+
preflight can fail closed when source markup references a module preset the
|
|
218
|
+
target site does not have.
|
|
203
219
|
|
|
204
220
|
Workflow:
|
|
205
221
|
|
|
@@ -210,9 +226,11 @@ Workflow:
|
|
|
210
226
|
3. Run `diviops-cross-env-preflight --source source.json --target target.json --dry-run`.
|
|
211
227
|
|
|
212
228
|
`--apply` is intentionally refused. To mutate a target, use the separate
|
|
213
|
-
Pro-gated `diviops_cross_env_header_apply` tool with the reviewed fingerprint
|
|
214
|
-
|
|
215
|
-
|
|
229
|
+
Pro-gated `diviops_cross_env_header_apply` tool with the reviewed fingerprint
|
|
230
|
+
and either inline `source_payload` for small/disposable tests or
|
|
231
|
+
`source_payload_ref` for large real headers. That MVP still refuses media
|
|
232
|
+
upload/import, global color creation/import, off-canvas reconcile, and new
|
|
233
|
+
target layout creation.
|
|
216
234
|
|
|
217
235
|
## Response contract
|
|
218
236
|
|
|
@@ -299,7 +317,7 @@ Common quick fixes — full reference in [troubleshooting.md](../docs/troublesho
|
|
|
299
317
|
- **`npx` fails with "could not determine executable to run"** — use `npx -y --package @diviops/mcp-server diviops-mcp`; this explicitly selects the MCP server bin.
|
|
300
318
|
- **"Connection failed"** — verify the plugin is active by visiting `{WP_URL}/wp-json/diviops/v1/schema/settings`; test the credentials with `curl -u "user:pass" …`.
|
|
301
319
|
- **"This tool requires plugin capability"** — the plugin doesn't advertise the capability this tool needs. Update the plugin to the latest release.
|
|
302
|
-
- **Preset edits not visible on the frontend** — Divi serves frontend CSS from `wp-content/et-cache/{post_id}/`, which `wp cache flush` doesn't touch. Use `diviops_meta_flush_cache` after preset writes.
|
|
320
|
+
- **Preset edits not visible on the frontend** — Divi serves frontend CSS from `wp-content/et-cache/{post_id}/`, which `wp cache flush` doesn't touch. Use `diviops_meta_flush_cache` after preset writes; `post_id` mode also sweeps that exact directory and reports `post_dir_sweep` evidence.
|
|
303
321
|
|
|
304
322
|
## Learn more
|
|
305
323
|
|
package/dist/compatibility.d.ts
CHANGED
|
@@ -38,6 +38,15 @@ export interface HandshakeTarget {
|
|
|
38
38
|
present: boolean;
|
|
39
39
|
version?: string | null;
|
|
40
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Advisory plugin version/presence record contributed by extension plugins.
|
|
43
|
+
* This is a diagnostics/preflight surface only; capability gates continue to
|
|
44
|
+
* use the explicit capability map and target/module flags.
|
|
45
|
+
*/
|
|
46
|
+
export interface HandshakePluginInfo {
|
|
47
|
+
active: boolean;
|
|
48
|
+
version?: string | null;
|
|
49
|
+
}
|
|
41
50
|
/**
|
|
42
51
|
* Shape returned by `POST /diviops/v1/handshake`.
|
|
43
52
|
*
|
|
@@ -70,6 +79,8 @@ export interface HandshakeResult {
|
|
|
70
79
|
available_targets?: Record<string, HandshakeTarget>;
|
|
71
80
|
/** ADR-003 — per-target admin-controlled activation toggle. */
|
|
72
81
|
active_modules?: Record<string, boolean>;
|
|
82
|
+
/** Advisory installed-plugin versions for S0 preflight reports. */
|
|
83
|
+
plugins?: Record<string, HandshakePluginInfo>;
|
|
73
84
|
}
|
|
74
85
|
export declare function proToolGatesSatisfied(state: {
|
|
75
86
|
proActive: boolean;
|
|
@@ -15,6 +15,7 @@ export interface SourceLayoutPayload {
|
|
|
15
15
|
markup: string;
|
|
16
16
|
checksum?: string;
|
|
17
17
|
attachments?: SourceAttachmentInventoryItem[];
|
|
18
|
+
module_preset_ids?: unknown;
|
|
18
19
|
}
|
|
19
20
|
export interface TargetAttachmentInventoryItem {
|
|
20
21
|
id?: number;
|
|
@@ -42,6 +43,7 @@ export interface TargetLayoutContext {
|
|
|
42
43
|
global_colors?: unknown;
|
|
43
44
|
global_color_value_evidence?: unknown;
|
|
44
45
|
builtin_customizer_color_ids?: unknown;
|
|
46
|
+
module_preset_ids?: unknown;
|
|
45
47
|
cache_scope?: string;
|
|
46
48
|
}
|
|
47
49
|
export interface CrossEnvHeaderPreflightInput {
|
|
@@ -84,6 +86,10 @@ export interface OffCanvasReference {
|
|
|
84
86
|
token: string;
|
|
85
87
|
reason: string;
|
|
86
88
|
}
|
|
89
|
+
export interface ModulePresetReference {
|
|
90
|
+
id: string;
|
|
91
|
+
status: "target_module_preset" | "missing_target_context" | "missing_definition";
|
|
92
|
+
}
|
|
87
93
|
export interface CrossEnvHeaderPreflightReport {
|
|
88
94
|
type: "cross_env_header_preflight";
|
|
89
95
|
dry_run: true;
|
|
@@ -115,6 +121,7 @@ export interface CrossEnvHeaderPreflightReport {
|
|
|
115
121
|
};
|
|
116
122
|
attachments: AttachmentReference[];
|
|
117
123
|
global_colors: GcidReference[];
|
|
124
|
+
module_presets: ModulePresetReference[];
|
|
118
125
|
off_canvas: OffCanvasReference[];
|
|
119
126
|
};
|
|
120
127
|
rewrite_plan: {
|
|
@@ -462,6 +462,72 @@ function findGcidRefs(markup, target) {
|
|
|
462
462
|
return { id, status: "missing_definition" };
|
|
463
463
|
});
|
|
464
464
|
}
|
|
465
|
+
function isSafePresetId(value) {
|
|
466
|
+
return value !== "" && value !== "default" && /^[A-Za-z0-9_-]+$/.test(value);
|
|
467
|
+
}
|
|
468
|
+
function collectPresetIdsFromList(value) {
|
|
469
|
+
if (value === undefined || value === null)
|
|
470
|
+
return null;
|
|
471
|
+
const ids = new Set();
|
|
472
|
+
const add = (candidate) => {
|
|
473
|
+
if (typeof candidate === "string" && isSafePresetId(candidate)) {
|
|
474
|
+
ids.add(candidate);
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
if (Array.isArray(value)) {
|
|
478
|
+
for (const item of value)
|
|
479
|
+
add(item);
|
|
480
|
+
}
|
|
481
|
+
else if (value && typeof value === "object") {
|
|
482
|
+
for (const [key, item] of Object.entries(value)) {
|
|
483
|
+
add(key);
|
|
484
|
+
if (item && typeof item === "object") {
|
|
485
|
+
add(item.id);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
else {
|
|
490
|
+
add(value);
|
|
491
|
+
}
|
|
492
|
+
return ids;
|
|
493
|
+
}
|
|
494
|
+
function findModulePresetRefs(source, target) {
|
|
495
|
+
const refs = collectPresetIdsFromList(source.module_preset_ids) ?? new Set();
|
|
496
|
+
const decoded = decodeMarkup(source.markup);
|
|
497
|
+
if (decoded.includes('"modulePreset"')) {
|
|
498
|
+
const blockRe = /<!--\s+(?:\/)?wp:([A-Za-z0-9_-]+\/[A-Za-z0-9_-]+)(.*?)(?:\/)?-->/gs;
|
|
499
|
+
let match;
|
|
500
|
+
while ((match = blockRe.exec(decoded)) !== null) {
|
|
501
|
+
const blockName = match[1];
|
|
502
|
+
if (!blockName.startsWith("divi/"))
|
|
503
|
+
continue;
|
|
504
|
+
const attrsText = jsonObjectFromBlockCommentTail(match[2] ?? "");
|
|
505
|
+
if (!attrsText)
|
|
506
|
+
continue;
|
|
507
|
+
try {
|
|
508
|
+
const attrs = JSON.parse(attrsText);
|
|
509
|
+
const values = Array.isArray(attrs.modulePreset)
|
|
510
|
+
? attrs.modulePreset
|
|
511
|
+
: [attrs.modulePreset];
|
|
512
|
+
for (const id of values) {
|
|
513
|
+
if (typeof id === "string" && isSafePresetId(id))
|
|
514
|
+
refs.add(id);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
catch {
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
const targetIds = collectPresetIdsFromList(target.module_preset_ids);
|
|
523
|
+
return uniqueSorted(refs).map((id) => {
|
|
524
|
+
if (targetIds === null)
|
|
525
|
+
return { id, status: "missing_target_context" };
|
|
526
|
+
if (targetIds.has(id))
|
|
527
|
+
return { id, status: "target_module_preset" };
|
|
528
|
+
return { id, status: "missing_definition" };
|
|
529
|
+
});
|
|
530
|
+
}
|
|
465
531
|
function findOffCanvasRefs(markup) {
|
|
466
532
|
const decoded = decodeMarkup(markup);
|
|
467
533
|
const refs = new Map();
|
|
@@ -561,6 +627,16 @@ function buildConfirmationBinding(report) {
|
|
|
561
627
|
value_evidence: color.value_evidence ?? null,
|
|
562
628
|
}))
|
|
563
629
|
.sort((a, b) => canonicalJson(a).localeCompare(canonicalJson(b))),
|
|
630
|
+
...(report.findings.module_presets.length > 0
|
|
631
|
+
? {
|
|
632
|
+
module_preset_resolution: report.findings.module_presets
|
|
633
|
+
.map((preset) => ({
|
|
634
|
+
id: preset.id,
|
|
635
|
+
status: preset.status,
|
|
636
|
+
}))
|
|
637
|
+
.sort((a, b) => canonicalJson(a).localeCompare(canonicalJson(b))),
|
|
638
|
+
}
|
|
639
|
+
: {}),
|
|
564
640
|
};
|
|
565
641
|
const schema = "diviops.cross_env.header_preflight.confirmation_binding.v1";
|
|
566
642
|
const fingerprint = sha256(canonicalJson({ schema, input }));
|
|
@@ -694,6 +770,23 @@ export function preflightCrossEnvHeaderSync(input) {
|
|
|
694
770
|
});
|
|
695
771
|
}
|
|
696
772
|
}
|
|
773
|
+
const modulePresets = findModulePresetRefs(input.source, input.target);
|
|
774
|
+
for (const preset of modulePresets) {
|
|
775
|
+
if (preset.status === "missing_target_context") {
|
|
776
|
+
operatorActions.push({
|
|
777
|
+
code: "missing_module_preset_context",
|
|
778
|
+
message: "Provide target D5 module preset IDs before apply.",
|
|
779
|
+
detail: { id: preset.id },
|
|
780
|
+
});
|
|
781
|
+
}
|
|
782
|
+
else if (preset.status === "missing_definition") {
|
|
783
|
+
operatorActions.push({
|
|
784
|
+
code: "missing_module_preset_definition",
|
|
785
|
+
message: "Referenced modulePreset is not defined on the target.",
|
|
786
|
+
detail: { id: preset.id },
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
}
|
|
697
790
|
const offCanvas = findOffCanvasRefs(input.source.markup);
|
|
698
791
|
if (offCanvas.length > 0) {
|
|
699
792
|
blockers.push({
|
|
@@ -757,6 +850,7 @@ export function preflightCrossEnvHeaderSync(input) {
|
|
|
757
850
|
},
|
|
758
851
|
attachments,
|
|
759
852
|
global_colors: globalColors,
|
|
853
|
+
module_presets: modulePresets,
|
|
760
854
|
off_canvas: offCanvas,
|
|
761
855
|
},
|
|
762
856
|
rewrite_plan: {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type SourceLayoutPayload } from "./header-preflight.js";
|
|
2
|
+
export interface SourcePayloadRef {
|
|
3
|
+
handle: string;
|
|
4
|
+
checksum: string;
|
|
5
|
+
algorithm: "sha256";
|
|
6
|
+
storage: "server_local_artifact";
|
|
7
|
+
expires_at?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface SourcePayloadRefEnvelope {
|
|
10
|
+
type: "diviops.cross_env.source_payload_ref.v1";
|
|
11
|
+
created_at: string;
|
|
12
|
+
expires_at: string | null;
|
|
13
|
+
source_payload: SourceLayoutPayload;
|
|
14
|
+
checksum: {
|
|
15
|
+
algorithm: "sha256";
|
|
16
|
+
computed: string;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export declare function createSourcePayloadRef(payload: SourceLayoutPayload): SourcePayloadRef;
|
|
20
|
+
export declare function loadSourcePayloadRef(ref: unknown): SourceLayoutPayload;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
const DEFAULT_TTL_SECONDS = 24 * 60 * 60;
|
|
5
|
+
const MAX_TTL_SECONDS = 10 * 365 * 24 * 60 * 60;
|
|
6
|
+
const HANDLE_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
|
|
7
|
+
function sha256(value) {
|
|
8
|
+
return createHash("sha256").update(value).digest("hex");
|
|
9
|
+
}
|
|
10
|
+
function payloadRoot() {
|
|
11
|
+
return (process.env.DIVIOPS_CROSS_ENV_PAYLOAD_REF_DIR ||
|
|
12
|
+
join(process.cwd(), ".diviops-tmp", "cross-env-source-payloads"));
|
|
13
|
+
}
|
|
14
|
+
function ttlSeconds() {
|
|
15
|
+
const raw = process.env.DIVIOPS_CROSS_ENV_PAYLOAD_REF_TTL_SECONDS;
|
|
16
|
+
if (!raw)
|
|
17
|
+
return DEFAULT_TTL_SECONDS;
|
|
18
|
+
const parsed = Number(raw);
|
|
19
|
+
if (!Number.isFinite(parsed) || parsed < 0 || parsed > MAX_TTL_SECONDS) {
|
|
20
|
+
return DEFAULT_TTL_SECONDS;
|
|
21
|
+
}
|
|
22
|
+
return Math.floor(parsed);
|
|
23
|
+
}
|
|
24
|
+
function normalizeChecksum(value) {
|
|
25
|
+
if (typeof value !== "string")
|
|
26
|
+
return "";
|
|
27
|
+
return value.trim().replace(/^sha256:/i, "").toLowerCase();
|
|
28
|
+
}
|
|
29
|
+
function assertHandle(handle) {
|
|
30
|
+
if (!HANDLE_PATTERN.test(handle) || handle.includes("..")) {
|
|
31
|
+
throw new Error("source_payload_ref.handle is invalid.");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function payloadPath(handle) {
|
|
35
|
+
assertHandle(handle);
|
|
36
|
+
return join(payloadRoot(), `${handle}.json`);
|
|
37
|
+
}
|
|
38
|
+
function assertSourcePayload(value) {
|
|
39
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
40
|
+
throw new Error("source payload artifact does not contain an object.");
|
|
41
|
+
}
|
|
42
|
+
const payload = value;
|
|
43
|
+
if (typeof payload.markup !== "string") {
|
|
44
|
+
throw new Error("source payload artifact is missing markup.");
|
|
45
|
+
}
|
|
46
|
+
if (typeof payload.origin !== "string" || payload.origin.trim() === "") {
|
|
47
|
+
throw new Error("source payload artifact is missing origin.");
|
|
48
|
+
}
|
|
49
|
+
if (typeof payload.object_kind !== "string" || payload.object_kind.trim() === "") {
|
|
50
|
+
throw new Error("source payload artifact is missing object_kind.");
|
|
51
|
+
}
|
|
52
|
+
if (payload.attachments !== undefined) {
|
|
53
|
+
if (!Array.isArray(payload.attachments)) {
|
|
54
|
+
throw new Error("source payload artifact attachments must be an array.");
|
|
55
|
+
}
|
|
56
|
+
for (const attachment of payload.attachments) {
|
|
57
|
+
if (!attachment || typeof attachment !== "object" || Array.isArray(attachment)) {
|
|
58
|
+
throw new Error("source payload artifact attachments must contain objects.");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return payload;
|
|
63
|
+
}
|
|
64
|
+
function checksumPayload(payload) {
|
|
65
|
+
return sha256(payload.markup);
|
|
66
|
+
}
|
|
67
|
+
function handleForPayload(payload) {
|
|
68
|
+
const id = payload.object_id === undefined ? "unknown" : String(payload.object_id);
|
|
69
|
+
const slug = id
|
|
70
|
+
.replace(/[^A-Za-z0-9._-]+/g, "-")
|
|
71
|
+
.replace(/^-+|-+$/g, "")
|
|
72
|
+
.slice(0, 64) || "source";
|
|
73
|
+
return `src-${slug}-${checksumPayload(payload).slice(0, 16)}-${randomUUID().slice(0, 8)}`;
|
|
74
|
+
}
|
|
75
|
+
export function createSourcePayloadRef(payload) {
|
|
76
|
+
const source = assertSourcePayload(payload);
|
|
77
|
+
const computed = checksumPayload(source);
|
|
78
|
+
const declared = normalizeChecksum(source.checksum);
|
|
79
|
+
if (declared && declared !== computed) {
|
|
80
|
+
throw new Error("source payload checksum does not match markup.");
|
|
81
|
+
}
|
|
82
|
+
const createdAt = new Date().toISOString();
|
|
83
|
+
const ttl = ttlSeconds();
|
|
84
|
+
const expiresAt = ttl > 0 ? new Date(Date.now() + ttl * 1000).toISOString() : null;
|
|
85
|
+
const handle = handleForPayload(source);
|
|
86
|
+
const envelope = {
|
|
87
|
+
type: "diviops.cross_env.source_payload_ref.v1",
|
|
88
|
+
created_at: createdAt,
|
|
89
|
+
expires_at: expiresAt,
|
|
90
|
+
source_payload: { ...source, checksum: computed },
|
|
91
|
+
checksum: {
|
|
92
|
+
algorithm: "sha256",
|
|
93
|
+
computed,
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
mkdirSync(payloadRoot(), { recursive: true });
|
|
97
|
+
writeFileSync(payloadPath(handle), `${JSON.stringify(envelope, null, 2)}\n`, {
|
|
98
|
+
encoding: "utf8",
|
|
99
|
+
mode: 0o600,
|
|
100
|
+
});
|
|
101
|
+
return {
|
|
102
|
+
handle,
|
|
103
|
+
checksum: computed,
|
|
104
|
+
algorithm: "sha256",
|
|
105
|
+
storage: "server_local_artifact",
|
|
106
|
+
...(expiresAt ? { expires_at: expiresAt } : {}),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
export function loadSourcePayloadRef(ref) {
|
|
110
|
+
if (!ref || typeof ref !== "object" || Array.isArray(ref)) {
|
|
111
|
+
throw new Error("source_payload_ref must be an object.");
|
|
112
|
+
}
|
|
113
|
+
const sourceRef = ref;
|
|
114
|
+
if (typeof sourceRef.handle !== "string") {
|
|
115
|
+
throw new Error("source_payload_ref.handle is required.");
|
|
116
|
+
}
|
|
117
|
+
assertHandle(sourceRef.handle);
|
|
118
|
+
const expected = normalizeChecksum(sourceRef.checksum);
|
|
119
|
+
if (!/^[a-f0-9]{64}$/.test(expected)) {
|
|
120
|
+
throw new Error("source_payload_ref.checksum must be a sha256 hex value.");
|
|
121
|
+
}
|
|
122
|
+
const file = payloadPath(sourceRef.handle);
|
|
123
|
+
let content;
|
|
124
|
+
try {
|
|
125
|
+
content = readFileSync(file, "utf8");
|
|
126
|
+
}
|
|
127
|
+
catch (err) {
|
|
128
|
+
const code = typeof err === "object" && err !== null && "code" in err
|
|
129
|
+
? err.code
|
|
130
|
+
: undefined;
|
|
131
|
+
if (code === "ENOENT") {
|
|
132
|
+
throw new Error("source_payload_ref artifact was not found.");
|
|
133
|
+
}
|
|
134
|
+
throw new Error("source_payload_ref artifact could not be read.");
|
|
135
|
+
}
|
|
136
|
+
let parsed;
|
|
137
|
+
try {
|
|
138
|
+
parsed = JSON.parse(content);
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
throw new Error("source_payload_ref artifact is not valid JSON.");
|
|
142
|
+
}
|
|
143
|
+
const envelope = parsed;
|
|
144
|
+
const isEnvelope = envelope?.type === "diviops.cross_env.source_payload_ref.v1";
|
|
145
|
+
if (isEnvelope) {
|
|
146
|
+
if (envelope.expires_at) {
|
|
147
|
+
const expiresAt = Date.parse(envelope.expires_at);
|
|
148
|
+
if (!Number.isFinite(expiresAt) || Date.now() > expiresAt) {
|
|
149
|
+
throw new Error("source_payload_ref artifact is expired.");
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
const ttl = ttlSeconds();
|
|
155
|
+
if (ttl > 0) {
|
|
156
|
+
let stats;
|
|
157
|
+
try {
|
|
158
|
+
stats = statSync(file);
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
throw new Error("source_payload_ref artifact could not be read.");
|
|
162
|
+
}
|
|
163
|
+
if (Date.now() - stats.mtimeMs > ttl * 1000) {
|
|
164
|
+
throw new Error("source_payload_ref artifact is expired.");
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const payload = assertSourcePayload(isEnvelope ? envelope.source_payload : parsed);
|
|
169
|
+
const computed = checksumPayload(payload);
|
|
170
|
+
const declared = normalizeChecksum(payload.checksum);
|
|
171
|
+
if (declared && declared !== computed) {
|
|
172
|
+
throw new Error("source_payload_ref artifact checksum does not match markup.");
|
|
173
|
+
}
|
|
174
|
+
if (computed !== expected) {
|
|
175
|
+
throw new Error("source_payload_ref checksum does not match artifact markup.");
|
|
176
|
+
}
|
|
177
|
+
return { ...payload, checksum: computed };
|
|
178
|
+
}
|