@diviops/mcp-server 1.5.29 → 1.5.31

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.
@@ -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
+ }