siteplane 0.2.1-rc.1 → 0.3.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 +4 -0
- package/README.md +181 -22
- package/THIRD-PARTY-NOTICES.md +16 -0
- package/dist/astro/SiteplaneAnalytics.astro +29 -0
- package/dist/astro/SiteplaneBridge.astro +19 -0
- package/dist/astro/client.js +26 -0
- package/dist/astro/index.d.ts +114 -0
- package/dist/astro/index.js +1772 -0
- package/dist/astro/middleware.js +574 -0
- package/dist/astro/route.js +161 -0
- package/dist/bin/siteplane.js +17734 -1490
- package/dist/chunk-46ZC6NGK.js +493 -0
- package/dist/chunk-IOFM5PE3.js +532 -0
- package/dist/client.d.ts +38 -0
- package/dist/client.js +24 -0
- package/dist/components.d.ts +3 -3
- package/dist/components.js +5 -17
- package/dist/index.d.ts +90 -23
- package/dist/index.js +334 -22
- package/dist/internal/astro-client.js +122 -0
- package/dist/internal/bridge.js +2 -1
- package/dist/internal/empty.js +6 -0
- package/dist/next.cjs +70 -27
- package/dist/next.d.ts +10 -7
- package/dist/next.js +72 -27
- package/dist/route.d.ts +2 -1
- package/dist/route.js +39 -26
- package/dist/schema-file.d.ts +11 -0
- package/dist/schema-file.js +6 -0
- package/dist/schema.d.ts +64 -0
- package/dist/schema.js +7 -0
- package/package.json +37 -2
- package/dist/chunk-BLJCWIYD.js +0 -340
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
import {
|
|
2
|
+
readSiteplaneConfig
|
|
3
|
+
} from "./chunk-KZ3AQ6OA.js";
|
|
4
|
+
|
|
5
|
+
// ../shared/dist/website-status.js
|
|
6
|
+
function isRevision(value) {
|
|
7
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// ../shared/dist/runtime-urls.js
|
|
11
|
+
function runtimeValuesUrl(apiOrigin, siteId) {
|
|
12
|
+
return `${siteUrl(apiOrigin, siteId)}/values`;
|
|
13
|
+
}
|
|
14
|
+
function runtimeDraftsUrl(apiOrigin, siteId) {
|
|
15
|
+
return `${siteUrl(apiOrigin, siteId)}/drafts`;
|
|
16
|
+
}
|
|
17
|
+
function runtimeSigningKeysUrl(apiOrigin) {
|
|
18
|
+
return `${runtimeApi(apiOrigin)}/signing-keys`;
|
|
19
|
+
}
|
|
20
|
+
function siteUrl(apiOrigin, siteId) {
|
|
21
|
+
return `${runtimeApi(apiOrigin)}/sites/${encodeURIComponent(siteId)}`;
|
|
22
|
+
}
|
|
23
|
+
function runtimeApi(apiOrigin) {
|
|
24
|
+
return `${apiOrigin.replace(/\/+$/u, "")}/api/runtime/v1`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ../shared/dist/signing-keys.js
|
|
28
|
+
var pkcs8Ed25519Prefix = Uint8Array.from([
|
|
29
|
+
48,
|
|
30
|
+
46,
|
|
31
|
+
2,
|
|
32
|
+
1,
|
|
33
|
+
0,
|
|
34
|
+
48,
|
|
35
|
+
5,
|
|
36
|
+
6,
|
|
37
|
+
3,
|
|
38
|
+
43,
|
|
39
|
+
101,
|
|
40
|
+
112,
|
|
41
|
+
4,
|
|
42
|
+
34,
|
|
43
|
+
4,
|
|
44
|
+
32
|
|
45
|
+
]);
|
|
46
|
+
var ed25519 = { name: "Ed25519" };
|
|
47
|
+
async function verifyWithSigningKey(publicKey, signature, data) {
|
|
48
|
+
const key = await crypto.subtle.importKey("raw", decodeKey(publicKey, "whpk_"), ed25519, false, ["verify"]);
|
|
49
|
+
return crypto.subtle.verify(ed25519, key, signature, data);
|
|
50
|
+
}
|
|
51
|
+
function decodeBase64(value) {
|
|
52
|
+
const standard = value.replace(/-/gu, "+").replace(/_/gu, "/");
|
|
53
|
+
return Uint8Array.from(atob(standard.padEnd(Math.ceil(standard.length / 4) * 4, "=")), (character) => character.charCodeAt(0));
|
|
54
|
+
}
|
|
55
|
+
function decodeKey(key, prefix) {
|
|
56
|
+
let bytes;
|
|
57
|
+
try {
|
|
58
|
+
if (key.startsWith(prefix))
|
|
59
|
+
bytes = decodeBase64(key.slice(prefix.length));
|
|
60
|
+
} catch {
|
|
61
|
+
bytes = void 0;
|
|
62
|
+
}
|
|
63
|
+
if (bytes?.length !== 32)
|
|
64
|
+
throw new Error("invalid_signing_key");
|
|
65
|
+
return bytes;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ../shared/dist/preview-token.js
|
|
69
|
+
var PREVIEW_TOKEN_MAX_SECONDS = 60 * 60;
|
|
70
|
+
var header = encodeJson({ alg: "EdDSA", typ: "JWT" });
|
|
71
|
+
var clockSkewSeconds = 60;
|
|
72
|
+
async function verifyPreviewToken(input) {
|
|
73
|
+
const parts = input.token.split(".");
|
|
74
|
+
const nowSeconds = Math.floor((input.now ?? /* @__PURE__ */ new Date()).getTime() / 1e3);
|
|
75
|
+
let claims = null;
|
|
76
|
+
let signature = null;
|
|
77
|
+
if (parts.length === 3 && parts[0] === header) {
|
|
78
|
+
try {
|
|
79
|
+
claims = readClaims(JSON.parse(new TextDecoder().decode(decodeBase64(parts[1]))), nowSeconds);
|
|
80
|
+
signature = decodeBase64(parts[2]);
|
|
81
|
+
} catch {
|
|
82
|
+
claims = null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (!claims || !signature)
|
|
86
|
+
return { ok: false, reason: "malformed" };
|
|
87
|
+
const signingInput = new TextEncoder().encode(`${parts[0]}.${parts[1]}`);
|
|
88
|
+
let signed = false;
|
|
89
|
+
for (const publicKey of input.publicKeys) {
|
|
90
|
+
signed = await verifyWithSigningKey(publicKey, signature, signingInput);
|
|
91
|
+
if (signed)
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
if (!signed)
|
|
95
|
+
return { ok: false, reason: "signature_invalid" };
|
|
96
|
+
if (claims.aud !== input.siteId)
|
|
97
|
+
return { ok: false, reason: "wrong_site" };
|
|
98
|
+
if (claims.exp <= nowSeconds)
|
|
99
|
+
return { ok: false, reason: "expired" };
|
|
100
|
+
return { ok: true, claims };
|
|
101
|
+
}
|
|
102
|
+
function readClaims(value, nowSeconds) {
|
|
103
|
+
if (typeof value !== "object" || value === null)
|
|
104
|
+
return null;
|
|
105
|
+
const claims = value;
|
|
106
|
+
const { aud, sub, scope, actor, editor_origin: editorOrigin, iat, exp, jti } = claims;
|
|
107
|
+
if (typeof aud !== "string" || !aud || typeof sub !== "string" || !sub || scope !== "drafts:read" || actor !== "workspace_member" && actor !== "site_member" || typeof editorOrigin !== "string" || !isOrigin(editorOrigin) || !Number.isSafeInteger(iat) || !Number.isSafeInteger(exp) || typeof iat !== "number" || typeof exp !== "number" || exp - iat > PREVIEW_TOKEN_MAX_SECONDS || iat > nowSeconds + clockSkewSeconds || typeof jti !== "string" || !jti) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
return { aud, sub, scope, actor, editor_origin: editorOrigin, iat, exp, jti };
|
|
111
|
+
}
|
|
112
|
+
function isOrigin(value) {
|
|
113
|
+
try {
|
|
114
|
+
return new URL(value).origin === value;
|
|
115
|
+
} catch {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function encodeJson(value) {
|
|
120
|
+
return toBase64Url(new TextEncoder().encode(JSON.stringify(value)));
|
|
121
|
+
}
|
|
122
|
+
function toBase64Url(bytes) {
|
|
123
|
+
let binary = "";
|
|
124
|
+
for (const byte of bytes)
|
|
125
|
+
binary += String.fromCharCode(byte);
|
|
126
|
+
return btoa(binary).replace(/\+/gu, "-").replace(/\//gu, "_").replace(/=+$/u, "");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// ../shared/dist/webhook-signature.js
|
|
130
|
+
var WEBHOOK_TOLERANCE_SECONDS = 5 * 60;
|
|
131
|
+
var encoder = new TextEncoder();
|
|
132
|
+
function webhookSignedContent(id, timestamp, payload) {
|
|
133
|
+
return `${id}.${timestamp}.${payload}`;
|
|
134
|
+
}
|
|
135
|
+
async function verifyWebhook(input) {
|
|
136
|
+
const read = (name) => input.headers instanceof Headers ? input.headers.get(name) : input.headers[name];
|
|
137
|
+
const id = read("webhook-id");
|
|
138
|
+
const timestamp = read("webhook-timestamp");
|
|
139
|
+
const signatureHeader = read("webhook-signature");
|
|
140
|
+
if (!id || !timestamp || !signatureHeader)
|
|
141
|
+
return { ok: false, reason: "missing_headers" };
|
|
142
|
+
const nowSeconds = Math.floor((input.now ?? /* @__PURE__ */ new Date()).getTime() / 1e3);
|
|
143
|
+
if (!/^\d{1,12}$/u.test(timestamp) || Math.abs(nowSeconds - Number(timestamp)) > WEBHOOK_TOLERANCE_SECONDS) {
|
|
144
|
+
return { ok: false, reason: "timestamp_out_of_window" };
|
|
145
|
+
}
|
|
146
|
+
const content = encoder.encode(webhookSignedContent(id, timestamp, input.payload));
|
|
147
|
+
const signatures = signatureHeader.split(" ").flatMap((entry) => {
|
|
148
|
+
const [version, signature] = entry.split(",");
|
|
149
|
+
if (version !== "v1a" || !signature)
|
|
150
|
+
return [];
|
|
151
|
+
try {
|
|
152
|
+
return [decodeBase64(signature)];
|
|
153
|
+
} catch {
|
|
154
|
+
return [];
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
for (const publicKey of input.publicKeys) {
|
|
158
|
+
for (const signature of signatures) {
|
|
159
|
+
if (await verifyWithSigningKey(publicKey, signature, content))
|
|
160
|
+
return { ok: true };
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return { ok: false, reason: "no_matching_signature" };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// src/runtime/preview.ts
|
|
167
|
+
var PREVIEW_COOKIE = "siteplane-preview";
|
|
168
|
+
async function verifySitePreviewToken(config, token, keys, now) {
|
|
169
|
+
const verify = async (reload) => verifyPreviewToken({
|
|
170
|
+
token,
|
|
171
|
+
siteId: config.siteId,
|
|
172
|
+
publicKeys: await keys(config, "preview", reload ? { reload } : {}),
|
|
173
|
+
now
|
|
174
|
+
});
|
|
175
|
+
let result = await verify(false);
|
|
176
|
+
if (!result.ok && result.reason === "signature_invalid") result = await verify(true);
|
|
177
|
+
return result.ok ? result.claims : null;
|
|
178
|
+
}
|
|
179
|
+
async function verifySiteRevalidation(config, request, keys, now) {
|
|
180
|
+
const verify = async (reload) => verifyWebhook({
|
|
181
|
+
headers: request.headers,
|
|
182
|
+
payload: request.payload,
|
|
183
|
+
publicKeys: await keys(config, "revalidation", reload ? { reload } : {}),
|
|
184
|
+
now
|
|
185
|
+
});
|
|
186
|
+
let result = await verify(false);
|
|
187
|
+
if (!result.ok && result.reason === "no_matching_signature") result = await verify(true);
|
|
188
|
+
return result.ok;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// src/runtime/server.ts
|
|
192
|
+
import { cookies, draftMode } from "next/headers";
|
|
193
|
+
import { cache } from "react";
|
|
194
|
+
import projectConfig from "siteplane/config";
|
|
195
|
+
import projectSchema from "siteplane/schema-file";
|
|
196
|
+
|
|
197
|
+
// src/runtime/server-state.ts
|
|
198
|
+
function serverShared(name, create) {
|
|
199
|
+
const key = /* @__PURE__ */ Symbol.for(`siteplane.${name}`);
|
|
200
|
+
const store = globalThis;
|
|
201
|
+
return store[key] ??= create();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// src/runtime/fetch-json.ts
|
|
205
|
+
var SITEPLANE_DEADLINE_MS = 3e3;
|
|
206
|
+
var SITEPLANE_DRAFT_MODE_DEADLINE_MS = 1e4;
|
|
207
|
+
var MAX_BODY_BYTES = 10 * 1024 * 1024;
|
|
208
|
+
async function fetchJson(fetchImpl, url, init, deadlineMs = SITEPLANE_DEADLINE_MS) {
|
|
209
|
+
const controller = new AbortController();
|
|
210
|
+
let timer;
|
|
211
|
+
const deadline = new Promise((resolve) => {
|
|
212
|
+
timer = setTimeout(() => {
|
|
213
|
+
controller.abort();
|
|
214
|
+
resolve(null);
|
|
215
|
+
}, deadlineMs);
|
|
216
|
+
});
|
|
217
|
+
const answer = (async () => {
|
|
218
|
+
const response = await fetchImpl(url, { ...init, signal: controller.signal });
|
|
219
|
+
const text = await readBoundedText(response.body, MAX_BODY_BYTES);
|
|
220
|
+
if (text === null) return null;
|
|
221
|
+
return { status: response.status, body: JSON.parse(text) };
|
|
222
|
+
})().catch(() => null);
|
|
223
|
+
try {
|
|
224
|
+
return await Promise.race([answer, deadline]);
|
|
225
|
+
} finally {
|
|
226
|
+
clearTimeout(timer);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
async function readBoundedText(body, maxBytes) {
|
|
230
|
+
if (!body) return "";
|
|
231
|
+
const reader = body.getReader();
|
|
232
|
+
const chunks = [];
|
|
233
|
+
let bytes = 0;
|
|
234
|
+
for (; ; ) {
|
|
235
|
+
const chunk = await reader.read();
|
|
236
|
+
if (chunk.done) break;
|
|
237
|
+
bytes += chunk.value.byteLength;
|
|
238
|
+
if (bytes > maxBytes) {
|
|
239
|
+
await reader.cancel();
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
chunks.push(chunk.value);
|
|
243
|
+
}
|
|
244
|
+
const combined = new Uint8Array(bytes);
|
|
245
|
+
let offset = 0;
|
|
246
|
+
for (const chunk of chunks) {
|
|
247
|
+
combined.set(chunk, offset);
|
|
248
|
+
offset += chunk.byteLength;
|
|
249
|
+
}
|
|
250
|
+
return new TextDecoder().decode(combined);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// src/runtime/signing-keys.ts
|
|
254
|
+
var MAX_AGE_MS = 60 * 60 * 1e3;
|
|
255
|
+
var RELOAD_THROTTLE_MS = 60 * 1e3;
|
|
256
|
+
function createSigningKeyStore(dependencies) {
|
|
257
|
+
const cached = /* @__PURE__ */ new Map();
|
|
258
|
+
const lastAttempt = /* @__PURE__ */ new Map();
|
|
259
|
+
async function load(apiOrigin) {
|
|
260
|
+
lastAttempt.set(apiOrigin, dependencies.now());
|
|
261
|
+
const answer = await fetchJson(dependencies.fetch, runtimeSigningKeysUrl(apiOrigin), { cache: "no-store" });
|
|
262
|
+
const keys = answer?.status === 200 ? readKeySet(answer.body) : null;
|
|
263
|
+
if (keys) cached.set(apiOrigin, { keys, loadedAt: dependencies.now() });
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
async keys(config, purpose, options = {}) {
|
|
267
|
+
const entry = cached.get(config.apiOrigin);
|
|
268
|
+
const now = dependencies.now();
|
|
269
|
+
const expired = !entry || now - entry.loadedAt > MAX_AGE_MS;
|
|
270
|
+
const reloadAllowed = now - (lastAttempt.get(config.apiOrigin) ?? Number.NEGATIVE_INFINITY) >= RELOAD_THROTTLE_MS;
|
|
271
|
+
if ((expired || options.reload) && (reloadAllowed || !entry)) await load(config.apiOrigin);
|
|
272
|
+
return cached.get(config.apiOrigin)?.keys[purpose] ?? [];
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
function readKeySet(body) {
|
|
277
|
+
if (typeof body !== "object" || body === null) return null;
|
|
278
|
+
const { revalidation, preview } = body;
|
|
279
|
+
return isKeyList(revalidation) && isKeyList(preview) ? { revalidation, preview } : null;
|
|
280
|
+
}
|
|
281
|
+
function isKeyList(value) {
|
|
282
|
+
return Array.isArray(value) && value.every((key) => typeof key === "string" && key.startsWith("whpk_"));
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// src/runtime/values.ts
|
|
286
|
+
var BUILD_READ_LIMITS = { attempts: 3, budgetMs: 1e4, attemptMs: SITEPLANE_DEADLINE_MS, pausesMs: [500, 1e3] };
|
|
287
|
+
var MAX_VALUES_BYTES = 10 * 1024 * 1024;
|
|
288
|
+
var realTiming = {
|
|
289
|
+
now: () => performance.now(),
|
|
290
|
+
sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
291
|
+
};
|
|
292
|
+
function siteCacheTag(siteId) {
|
|
293
|
+
return `siteplane:${siteId}`;
|
|
294
|
+
}
|
|
295
|
+
function nextPublishedRequest(buildStamp) {
|
|
296
|
+
return (config, mode) => {
|
|
297
|
+
const url = runtimeValuesUrl(config.apiOrigin, config.siteId);
|
|
298
|
+
if (mode === "dev" || mode === "preview") return { url, init: { cache: "no-store" } };
|
|
299
|
+
const cached = { next: { revalidate: 60, tags: [siteCacheTag(config.siteId)] } };
|
|
300
|
+
return { url, init: mode === "build" && buildStamp ? { ...cached, headers: { "x-siteplane-build": buildStamp } } : cached };
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
function createValueLoader(dependencies) {
|
|
304
|
+
const log = dependencies.log ?? ((message) => console.warn(message));
|
|
305
|
+
const timing = dependencies.timing ?? realTiming;
|
|
306
|
+
const state = dependencies.state ?? serverShared("valueLoader", () => ({ lastGood: /* @__PURE__ */ new Map(), buildFailures: /* @__PURE__ */ new Map(), buildNotice: { logged: false } }));
|
|
307
|
+
async function published(config, mode) {
|
|
308
|
+
const key = `${config.apiOrigin} ${config.siteId}`;
|
|
309
|
+
const { url, init } = dependencies.publishedRequest(config, mode);
|
|
310
|
+
const once = (deadlineMs) => readValues(dependencies.fetch, config, url, init, deadlineMs);
|
|
311
|
+
let read;
|
|
312
|
+
if (mode === "build") {
|
|
313
|
+
const failed = state.buildFailures.get(key);
|
|
314
|
+
const notice = (reason) => {
|
|
315
|
+
if (state.buildNotice.logged) return;
|
|
316
|
+
state.buildNotice.logged = true;
|
|
317
|
+
log(buildRetryMessage(config, reason));
|
|
318
|
+
};
|
|
319
|
+
read = failed === void 0 ? await readValuesForBuild(once, timing, notice) : { ok: false, reason: failed };
|
|
320
|
+
if (!read.ok) state.buildFailures.set(key, read.reason);
|
|
321
|
+
} else {
|
|
322
|
+
read = await once(mode === "preview" ? SITEPLANE_DRAFT_MODE_DEADLINE_MS : SITEPLANE_DEADLINE_MS);
|
|
323
|
+
}
|
|
324
|
+
if (read.ok) {
|
|
325
|
+
const stand = { values: read.values, revision: read.revision };
|
|
326
|
+
state.lastGood.set(key, stand);
|
|
327
|
+
return stand;
|
|
328
|
+
}
|
|
329
|
+
const previous = state.lastGood.get(key);
|
|
330
|
+
if (previous) {
|
|
331
|
+
const revision = previous.revision === null ? "" : ` (revision ${previous.revision})`;
|
|
332
|
+
const kept = mode === "build" ? "this build uses the content it read for its earlier pages" : "this server shows its last good content";
|
|
333
|
+
log(`[Siteplane] Could not read the content (${read.reason}); ${kept}${revision}.`);
|
|
334
|
+
return previous;
|
|
335
|
+
}
|
|
336
|
+
throw new Error(mode === "build" ? buildFailedMessage(config, read.reason) : readFailedMessage(config, read.reason));
|
|
337
|
+
}
|
|
338
|
+
return {
|
|
339
|
+
async load(config, { mode, previewToken }) {
|
|
340
|
+
const drafts = previewToken ? readDrafts(dependencies.fetch, config, previewToken) : Promise.resolve(null);
|
|
341
|
+
const [stand, draftValues] = await Promise.all([published(config, mode), drafts]);
|
|
342
|
+
return draftValues ? { ...stand.values, ...draftValues } : stand.values;
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
function readFailedMessage(config, reason) {
|
|
347
|
+
const host = hostOf(config);
|
|
348
|
+
return `[Siteplane] The content of this website could not be read from ${host} (${reason}), and this server has no earlier content, so the page cannot render. It renders again as soon as ${host} answers.`;
|
|
349
|
+
}
|
|
350
|
+
function buildRetryMessage(config, reason) {
|
|
351
|
+
return `[Siteplane] Reading the content of this website from ${hostOf(config)} failed (${reason}); the build tries again, ${BUILD_READ_LIMITS.attempts} attempts within ${BUILD_READ_LIMITS.budgetMs / 1e3} s. If the build stops, this is the cause, not the code of the website. Build again once ${hostOf(config)} answers.`;
|
|
352
|
+
}
|
|
353
|
+
function buildFailedMessage(config, reason) {
|
|
354
|
+
const host = hostOf(config);
|
|
355
|
+
return `[Siteplane] The build could not read the content of this website from ${host} (${reason}; ${BUILD_READ_LIMITS.attempts} attempts within ${BUILD_READ_LIMITS.budgetMs / 1e3} s), and there is no content of an earlier build, so it stops here. The current version of the website stays online. Build again once ${host} answers.`;
|
|
356
|
+
}
|
|
357
|
+
function hostOf(config) {
|
|
358
|
+
try {
|
|
359
|
+
return new URL(config.apiOrigin).host;
|
|
360
|
+
} catch {
|
|
361
|
+
return "Siteplane";
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
async function readValuesForBuild(read, timing = realTiming, firstFailed) {
|
|
365
|
+
const started = timing.now();
|
|
366
|
+
const elapsed = () => timing.now() - started;
|
|
367
|
+
let first = null;
|
|
368
|
+
let attempts = 0;
|
|
369
|
+
while (attempts < BUILD_READ_LIMITS.attempts) {
|
|
370
|
+
const left = BUILD_READ_LIMITS.budgetMs - elapsed();
|
|
371
|
+
if (left <= 0) break;
|
|
372
|
+
attempts += 1;
|
|
373
|
+
const result = await read(Math.min(BUILD_READ_LIMITS.attemptMs, left));
|
|
374
|
+
if (result.ok) return { ...result, attempts, elapsedMs: elapsed() };
|
|
375
|
+
if (first === null) firstFailed?.(result.reason);
|
|
376
|
+
first ??= result.reason;
|
|
377
|
+
const pause = BUILD_READ_LIMITS.pausesMs[attempts - 1];
|
|
378
|
+
if (attempts < BUILD_READ_LIMITS.attempts && pause !== void 0 && elapsed() + pause < BUILD_READ_LIMITS.budgetMs) {
|
|
379
|
+
await timing.sleep(pause);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return { ok: false, reason: first ?? `no answer within ${BUILD_READ_LIMITS.budgetMs / 1e3} s`, attempts, elapsedMs: elapsed() };
|
|
383
|
+
}
|
|
384
|
+
async function readValues(fetchImpl, config, url, init, deadlineMs) {
|
|
385
|
+
const controller = new AbortController();
|
|
386
|
+
let timer;
|
|
387
|
+
const deadline = new Promise((resolve) => {
|
|
388
|
+
timer = setTimeout(() => {
|
|
389
|
+
controller.abort();
|
|
390
|
+
resolve({ ok: false, reason: `no answer within ${deadlineMs / 1e3} s` });
|
|
391
|
+
}, deadlineMs);
|
|
392
|
+
});
|
|
393
|
+
const attempt = (async () => {
|
|
394
|
+
let response;
|
|
395
|
+
try {
|
|
396
|
+
response = await fetchImpl(url, { ...init, signal: controller.signal });
|
|
397
|
+
} catch (error) {
|
|
398
|
+
return { ok: false, reason: networkReason(error) };
|
|
399
|
+
}
|
|
400
|
+
if (response.status !== 200) {
|
|
401
|
+
void response.body?.cancel().catch(() => {
|
|
402
|
+
});
|
|
403
|
+
return { ok: false, reason: `HTTP ${response.status}` };
|
|
404
|
+
}
|
|
405
|
+
let text;
|
|
406
|
+
try {
|
|
407
|
+
text = await readBoundedText(response.body, MAX_VALUES_BYTES);
|
|
408
|
+
} catch (error) {
|
|
409
|
+
return { ok: false, reason: networkReason(error) };
|
|
410
|
+
}
|
|
411
|
+
if (text === null) return { ok: false, reason: "an answer larger than 10 MB" };
|
|
412
|
+
let body;
|
|
413
|
+
try {
|
|
414
|
+
body = JSON.parse(text);
|
|
415
|
+
} catch {
|
|
416
|
+
return { ok: false, reason: "an answer that is not JSON" };
|
|
417
|
+
}
|
|
418
|
+
const { siteId, values, revision } = isRecord(body) ? body : {};
|
|
419
|
+
if (siteId !== config.siteId) return { ok: false, reason: "an answer for another site" };
|
|
420
|
+
if (!isRecord(values)) return { ok: false, reason: "an answer without content" };
|
|
421
|
+
return { ok: true, values, revision: isRevision(revision) ? revision : null };
|
|
422
|
+
})();
|
|
423
|
+
try {
|
|
424
|
+
return await Promise.race([attempt, deadline]);
|
|
425
|
+
} finally {
|
|
426
|
+
clearTimeout(timer);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
async function readDrafts(fetchImpl, config, previewToken) {
|
|
430
|
+
const read = await readValues(
|
|
431
|
+
fetchImpl,
|
|
432
|
+
config,
|
|
433
|
+
runtimeDraftsUrl(config.apiOrigin, config.siteId),
|
|
434
|
+
{ cache: "no-store", headers: { authorization: `Bearer ${previewToken}` } },
|
|
435
|
+
SITEPLANE_DRAFT_MODE_DEADLINE_MS
|
|
436
|
+
);
|
|
437
|
+
return read.ok ? read.values : null;
|
|
438
|
+
}
|
|
439
|
+
function networkReason(error) {
|
|
440
|
+
const code = errorCode(error);
|
|
441
|
+
if (code === "ECONNREFUSED") return "connection refused";
|
|
442
|
+
if (code === "ECONNRESET" || code === "UND_ERR_SOCKET" || code === "EPIPE" || code === "UND_ERR_CLOSED") return "connection closed";
|
|
443
|
+
if (code === "ENOTFOUND" || code === "EAI_AGAIN") return "host not found";
|
|
444
|
+
return "network error";
|
|
445
|
+
}
|
|
446
|
+
function errorCode(error) {
|
|
447
|
+
for (let current = error, depth = 0; isRecord(current) && depth < 4; depth += 1) {
|
|
448
|
+
if (typeof current.code === "string") return current.code;
|
|
449
|
+
const first = Array.isArray(current.errors) ? current.errors[0] : void 0;
|
|
450
|
+
if (isRecord(first) && typeof first.code === "string") return first.code;
|
|
451
|
+
current = current.cause;
|
|
452
|
+
}
|
|
453
|
+
return null;
|
|
454
|
+
}
|
|
455
|
+
function isRecord(value) {
|
|
456
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// src/runtime/server.ts
|
|
460
|
+
var siteConfig = readSiteplaneConfig(projectConfig);
|
|
461
|
+
var siteSchema = projectSchema;
|
|
462
|
+
var nextFetch = (input, init) => fetch(input, init);
|
|
463
|
+
var signingKeys = createSigningKeyStore({ fetch: nextFetch, now: () => Date.now() });
|
|
464
|
+
var valueLoader = createValueLoader({
|
|
465
|
+
fetch: nextFetch,
|
|
466
|
+
publishedRequest: nextPublishedRequest(process.env.SITEPLANE_BUILD_STAMP || void 0)
|
|
467
|
+
});
|
|
468
|
+
async function isDraftMode() {
|
|
469
|
+
try {
|
|
470
|
+
return (await draftMode()).isEnabled;
|
|
471
|
+
} catch {
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
var PREVIEW_WITHOUT_COOKIES = `[Siteplane] The editor's preview cannot show drafts here or make this content clickable: Next.js gives read no cookies at this place, as inside "use cache", so read cannot see the preview. Read this content outside "use cache" to see its drafts in the editor's preview.`;
|
|
476
|
+
async function readPreviewToken() {
|
|
477
|
+
if (!await isDraftMode()) return null;
|
|
478
|
+
try {
|
|
479
|
+
return (await cookies()).get(PREVIEW_COOKIE)?.value ?? null;
|
|
480
|
+
} catch {
|
|
481
|
+
if (process.env.NODE_ENV !== "development") {
|
|
482
|
+
const notice = serverShared("previewWithoutCookies", () => ({ logged: false }));
|
|
483
|
+
if (!notice.logged) {
|
|
484
|
+
notice.logged = true;
|
|
485
|
+
console.warn(PREVIEW_WITHOUT_COOKIES);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
return null;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
var readPreview = cache(async () => {
|
|
492
|
+
try {
|
|
493
|
+
const token = await readPreviewToken();
|
|
494
|
+
if (!siteConfig || !token) return null;
|
|
495
|
+
const claims = await verifySitePreviewToken(siteConfig, token, signingKeys.keys, /* @__PURE__ */ new Date());
|
|
496
|
+
return claims ? { token, editorOrigin: claims.editor_origin } : null;
|
|
497
|
+
} catch {
|
|
498
|
+
return null;
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
var readMarkingMode = cache(async () => {
|
|
502
|
+
if (await readPreview()) return "preview";
|
|
503
|
+
if (process.env.NODE_ENV === "development" && await isDraftMode()) return "count";
|
|
504
|
+
return null;
|
|
505
|
+
});
|
|
506
|
+
async function readMode() {
|
|
507
|
+
if (await isDraftMode()) return "preview";
|
|
508
|
+
if (process.env.NEXT_PHASE === "phase-production-build") return "build";
|
|
509
|
+
if (process.env.NODE_ENV === "development") return "dev";
|
|
510
|
+
return "server";
|
|
511
|
+
}
|
|
512
|
+
async function loadRequestValues() {
|
|
513
|
+
if (!siteConfig) return {};
|
|
514
|
+
const mode = await readMode();
|
|
515
|
+
return valueLoader.load(siteConfig, { mode, previewToken: mode === "preview" ? await readPreviewToken() : null });
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
export {
|
|
519
|
+
serverShared,
|
|
520
|
+
PREVIEW_COOKIE,
|
|
521
|
+
verifySitePreviewToken,
|
|
522
|
+
verifySiteRevalidation,
|
|
523
|
+
readBoundedText,
|
|
524
|
+
siteCacheTag,
|
|
525
|
+
siteConfig,
|
|
526
|
+
siteSchema,
|
|
527
|
+
signingKeys,
|
|
528
|
+
isDraftMode,
|
|
529
|
+
readPreview,
|
|
530
|
+
readMarkingMode,
|
|
531
|
+
loadRequestValues
|
|
532
|
+
};
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** Why a page is left out of the count (Dokument 68, 7.2). */
|
|
2
|
+
type IgnoreReason = "decoration" | "generated" | "third-party" | "code" | "a11y" | "legal" | "collection" | "owner-wish" | "unused";
|
|
3
|
+
|
|
4
|
+
/** What `attrs` puts on an element: the path of an object read with the marking on, or nothing. */
|
|
5
|
+
type SiteplaneFieldAttributes = {
|
|
6
|
+
"data-siteplane-field"?: string;
|
|
7
|
+
};
|
|
8
|
+
/** What `ignore` puts on an element in a development build, or nothing. */
|
|
9
|
+
type SiteplaneIgnoreAttributes = {
|
|
10
|
+
"data-siteplane-ignore"?: IgnoreReason;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Plan 69 S3b (Dokument 68, 5.2): marks an element by hand for the editor and
|
|
14
|
+
* the count, for a value whose characters the code loses (shortened, split
|
|
15
|
+
* into paragraphs, rewritten by a helper) and for an image without `alt`:
|
|
16
|
+
* `<p {...siteplane.attrs(zeile, "preis")}>`. It gives something only for an
|
|
17
|
+
* object that `read` gave a `_path`, so only while the marking is on; on the
|
|
18
|
+
* server and in client components alike, as `_path` travels with the object.
|
|
19
|
+
*/
|
|
20
|
+
declare function attrs(object: unknown, field?: string): SiteplaneFieldAttributes;
|
|
21
|
+
/**
|
|
22
|
+
* Plan 69 S3b (Dokument 68, 5.2): a value without the invisible characters of
|
|
23
|
+
* the preview, like `stegaClean` of Sanity: texts, objects and lists; other
|
|
24
|
+
* values stay. Needed where code compares a text, turns it into a number or an
|
|
25
|
+
* address, writes it into an attribute, trims or splits it at white space, or
|
|
26
|
+
* judges or cuts it by its length.
|
|
27
|
+
*/
|
|
28
|
+
declare function clean<Value>(value: Value): Value;
|
|
29
|
+
/**
|
|
30
|
+
* Plan 69 S3b (Dokument 68, 7.2): leaves an element and everything in it that
|
|
31
|
+
* is not bound to content out of the count, with a reason:
|
|
32
|
+
* `<div {...siteplane.ignore("decoration")}>`. Only a development build gets
|
|
33
|
+
* the attribute, as only the dev server counts; Next.js fixes NODE_ENV when it
|
|
34
|
+
* builds, so visitors never get it.
|
|
35
|
+
*/
|
|
36
|
+
declare function ignore(reason: IgnoreReason): SiteplaneIgnoreAttributes;
|
|
37
|
+
|
|
38
|
+
export { type SiteplaneFieldAttributes, type SiteplaneIgnoreAttributes, attrs, clean, ignore };
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
import { vercelStegaClean } from "@vercel/stega";
|
|
3
|
+
var FIELD_NAMES = /^[A-Za-z][A-Za-z0-9_]*(?:\.[A-Za-z][A-Za-z0-9_]*)*$/u;
|
|
4
|
+
function attrs(object, field) {
|
|
5
|
+
const path = typeof object === "object" && object !== null ? object._path : void 0;
|
|
6
|
+
if (typeof path !== "string") return {};
|
|
7
|
+
if (field === void 0) return { "data-siteplane-field": path };
|
|
8
|
+
if (FIELD_NAMES.test(field)) return { "data-siteplane-field": `${path}.${field}` };
|
|
9
|
+
console.warn(
|
|
10
|
+
`[Siteplane] siteplane.attrs(object, ${JSON.stringify(field)}) marks nothing: ${JSON.stringify(field)} is not the name of a field. Pass the object that holds the value and the name of its field as siteplane.schema.ts writes it, for example attrs(row, "price").`
|
|
11
|
+
);
|
|
12
|
+
return {};
|
|
13
|
+
}
|
|
14
|
+
function clean(value) {
|
|
15
|
+
return typeof value === "string" || typeof value === "object" && value !== null ? vercelStegaClean(value) : value;
|
|
16
|
+
}
|
|
17
|
+
function ignore(reason) {
|
|
18
|
+
return process.env.NODE_ENV === "development" ? { "data-siteplane-ignore": reason } : {};
|
|
19
|
+
}
|
|
20
|
+
export {
|
|
21
|
+
attrs,
|
|
22
|
+
clean,
|
|
23
|
+
ignore
|
|
24
|
+
};
|
package/dist/components.d.ts
CHANGED
|
@@ -2,9 +2,9 @@ import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* `<SiteplaneBridge />` in the root layout (Vertrag 54 §8.2). A server
|
|
5
|
-
* component: for normal visitors
|
|
6
|
-
*
|
|
7
|
-
* origin.
|
|
5
|
+
* component: for normal visitors, and for the count request on the dev server,
|
|
6
|
+
* it renders nothing. In Draft Mode with a valid preview token it loads the
|
|
7
|
+
* bridge, which talks only to the token's editor origin.
|
|
8
8
|
*/
|
|
9
9
|
declare function SiteplaneBridge(): Promise<react_jsx_runtime.JSX.Element | null>;
|
|
10
10
|
/**
|
package/dist/components.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
isDraftMode,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
verifySitePreviewToken
|
|
7
|
-
} from "./chunk-BLJCWIYD.js";
|
|
3
|
+
readPreview,
|
|
4
|
+
siteConfig
|
|
5
|
+
} from "./chunk-IOFM5PE3.js";
|
|
8
6
|
import "./chunk-KZ3AQ6OA.js";
|
|
9
7
|
|
|
10
8
|
// ../shared/dist/site-analytics.js
|
|
@@ -19,18 +17,8 @@ import { AnalyticsTracker } from "siteplane/internal/analytics";
|
|
|
19
17
|
import { BridgeLoader } from "siteplane/internal/bridge-loader";
|
|
20
18
|
import { jsx } from "react/jsx-runtime";
|
|
21
19
|
async function SiteplaneBridge() {
|
|
22
|
-
const
|
|
23
|
-
return
|
|
24
|
-
}
|
|
25
|
-
async function readEditorOrigin() {
|
|
26
|
-
try {
|
|
27
|
-
const token = await readPreviewToken();
|
|
28
|
-
if (!siteConfig || !token) return null;
|
|
29
|
-
const claims = await verifySitePreviewToken(siteConfig, token, signingKeys.keys, /* @__PURE__ */ new Date());
|
|
30
|
-
return claims?.editor_origin ?? null;
|
|
31
|
-
} catch {
|
|
32
|
-
return null;
|
|
33
|
-
}
|
|
20
|
+
const preview = await readPreview();
|
|
21
|
+
return preview ? /* @__PURE__ */ jsx(BridgeLoader, { editorOrigin: preview.editorOrigin }) : null;
|
|
34
22
|
}
|
|
35
23
|
async function SiteplaneAnalytics({ siteId }) {
|
|
36
24
|
if (await isDraftMode()) return null;
|