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,493 @@
|
|
|
1
|
+
// ../shared/dist/runtime-urls.js
|
|
2
|
+
function runtimeValuesUrl(apiOrigin, siteId) {
|
|
3
|
+
return `${siteUrl(apiOrigin, siteId)}/values`;
|
|
4
|
+
}
|
|
5
|
+
function runtimeCdnValuesUrl(apiOrigin, siteId) {
|
|
6
|
+
return `${runtimeValuesUrl(apiOrigin, siteId)}/cdn`;
|
|
7
|
+
}
|
|
8
|
+
function runtimeDraftsUrl(apiOrigin, siteId) {
|
|
9
|
+
return `${siteUrl(apiOrigin, siteId)}/drafts`;
|
|
10
|
+
}
|
|
11
|
+
function runtimeSigningKeysUrl(apiOrigin) {
|
|
12
|
+
return `${runtimeApi(apiOrigin)}/signing-keys`;
|
|
13
|
+
}
|
|
14
|
+
function siteUrl(apiOrigin, siteId) {
|
|
15
|
+
return `${runtimeApi(apiOrigin)}/sites/${encodeURIComponent(siteId)}`;
|
|
16
|
+
}
|
|
17
|
+
function runtimeApi(apiOrigin) {
|
|
18
|
+
return `${apiOrigin.replace(/\/+$/u, "")}/api/runtime/v1`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// ../shared/dist/website-status.js
|
|
22
|
+
function isRevision(value) {
|
|
23
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// ../shared/dist/siteplane-config.js
|
|
27
|
+
var siteIdPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/u;
|
|
28
|
+
function readSiteplaneConfig(value) {
|
|
29
|
+
if (typeof value !== "object" || value === null)
|
|
30
|
+
return null;
|
|
31
|
+
const { siteId, apiOrigin, editorOrigins } = value;
|
|
32
|
+
if (typeof siteId !== "string" || !siteIdPattern.test(siteId) || !isWebOrigin(apiOrigin) || !Array.isArray(editorOrigins) || !editorOrigins.every(isWebOrigin)) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
return { siteId, apiOrigin, editorOrigins: [...editorOrigins] };
|
|
36
|
+
}
|
|
37
|
+
function isWebOrigin(value) {
|
|
38
|
+
if (typeof value !== "string")
|
|
39
|
+
return false;
|
|
40
|
+
try {
|
|
41
|
+
const url = new URL(value);
|
|
42
|
+
return (url.protocol === "https:" || url.protocol === "http:") && url.origin === value;
|
|
43
|
+
} catch {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ../shared/dist/signing-keys.js
|
|
49
|
+
var pkcs8Ed25519Prefix = Uint8Array.from([
|
|
50
|
+
48,
|
|
51
|
+
46,
|
|
52
|
+
2,
|
|
53
|
+
1,
|
|
54
|
+
0,
|
|
55
|
+
48,
|
|
56
|
+
5,
|
|
57
|
+
6,
|
|
58
|
+
3,
|
|
59
|
+
43,
|
|
60
|
+
101,
|
|
61
|
+
112,
|
|
62
|
+
4,
|
|
63
|
+
34,
|
|
64
|
+
4,
|
|
65
|
+
32
|
|
66
|
+
]);
|
|
67
|
+
var ed25519 = { name: "Ed25519" };
|
|
68
|
+
async function verifyWithSigningKey(publicKey, signature, data) {
|
|
69
|
+
const key = await crypto.subtle.importKey("raw", decodeKey(publicKey, "whpk_"), ed25519, false, ["verify"]);
|
|
70
|
+
return crypto.subtle.verify(ed25519, key, signature, data);
|
|
71
|
+
}
|
|
72
|
+
function decodeBase64(value) {
|
|
73
|
+
const standard = value.replace(/-/gu, "+").replace(/_/gu, "/");
|
|
74
|
+
return Uint8Array.from(atob(standard.padEnd(Math.ceil(standard.length / 4) * 4, "=")), (character) => character.charCodeAt(0));
|
|
75
|
+
}
|
|
76
|
+
function decodeKey(key, prefix) {
|
|
77
|
+
let bytes;
|
|
78
|
+
try {
|
|
79
|
+
if (key.startsWith(prefix))
|
|
80
|
+
bytes = decodeBase64(key.slice(prefix.length));
|
|
81
|
+
} catch {
|
|
82
|
+
bytes = void 0;
|
|
83
|
+
}
|
|
84
|
+
if (bytes?.length !== 32)
|
|
85
|
+
throw new Error("invalid_signing_key");
|
|
86
|
+
return bytes;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ../shared/dist/preview-token.js
|
|
90
|
+
var PREVIEW_TOKEN_MAX_SECONDS = 60 * 60;
|
|
91
|
+
var header = encodeJson({ alg: "EdDSA", typ: "JWT" });
|
|
92
|
+
var clockSkewSeconds = 60;
|
|
93
|
+
async function verifyPreviewToken(input) {
|
|
94
|
+
const parts = input.token.split(".");
|
|
95
|
+
const nowSeconds = Math.floor((input.now ?? /* @__PURE__ */ new Date()).getTime() / 1e3);
|
|
96
|
+
let claims = null;
|
|
97
|
+
let signature = null;
|
|
98
|
+
if (parts.length === 3 && parts[0] === header) {
|
|
99
|
+
try {
|
|
100
|
+
claims = readClaims(JSON.parse(new TextDecoder().decode(decodeBase64(parts[1]))), nowSeconds);
|
|
101
|
+
signature = decodeBase64(parts[2]);
|
|
102
|
+
} catch {
|
|
103
|
+
claims = null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (!claims || !signature)
|
|
107
|
+
return { ok: false, reason: "malformed" };
|
|
108
|
+
const signingInput = new TextEncoder().encode(`${parts[0]}.${parts[1]}`);
|
|
109
|
+
let signed = false;
|
|
110
|
+
for (const publicKey of input.publicKeys) {
|
|
111
|
+
signed = await verifyWithSigningKey(publicKey, signature, signingInput);
|
|
112
|
+
if (signed)
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
if (!signed)
|
|
116
|
+
return { ok: false, reason: "signature_invalid" };
|
|
117
|
+
if (claims.aud !== input.siteId)
|
|
118
|
+
return { ok: false, reason: "wrong_site" };
|
|
119
|
+
if (claims.exp <= nowSeconds)
|
|
120
|
+
return { ok: false, reason: "expired" };
|
|
121
|
+
return { ok: true, claims };
|
|
122
|
+
}
|
|
123
|
+
function readClaims(value, nowSeconds) {
|
|
124
|
+
if (typeof value !== "object" || value === null)
|
|
125
|
+
return null;
|
|
126
|
+
const claims = value;
|
|
127
|
+
const { aud, sub, scope, actor, editor_origin: editorOrigin, iat, exp, jti } = claims;
|
|
128
|
+
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) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
return { aud, sub, scope, actor, editor_origin: editorOrigin, iat, exp, jti };
|
|
132
|
+
}
|
|
133
|
+
function isOrigin(value) {
|
|
134
|
+
try {
|
|
135
|
+
return new URL(value).origin === value;
|
|
136
|
+
} catch {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
function encodeJson(value) {
|
|
141
|
+
return toBase64Url(new TextEncoder().encode(JSON.stringify(value)));
|
|
142
|
+
}
|
|
143
|
+
function toBase64Url(bytes) {
|
|
144
|
+
let binary = "";
|
|
145
|
+
for (const byte of bytes)
|
|
146
|
+
binary += String.fromCharCode(byte);
|
|
147
|
+
return btoa(binary).replace(/\+/gu, "-").replace(/\//gu, "_").replace(/=+$/u, "");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ../shared/dist/webhook-signature.js
|
|
151
|
+
var WEBHOOK_TOLERANCE_SECONDS = 5 * 60;
|
|
152
|
+
var encoder = new TextEncoder();
|
|
153
|
+
function webhookSignedContent(id, timestamp, payload) {
|
|
154
|
+
return `${id}.${timestamp}.${payload}`;
|
|
155
|
+
}
|
|
156
|
+
async function verifyWebhook(input) {
|
|
157
|
+
const read = (name) => input.headers instanceof Headers ? input.headers.get(name) : input.headers[name];
|
|
158
|
+
const id = read("webhook-id");
|
|
159
|
+
const timestamp = read("webhook-timestamp");
|
|
160
|
+
const signatureHeader = read("webhook-signature");
|
|
161
|
+
if (!id || !timestamp || !signatureHeader)
|
|
162
|
+
return { ok: false, reason: "missing_headers" };
|
|
163
|
+
const nowSeconds = Math.floor((input.now ?? /* @__PURE__ */ new Date()).getTime() / 1e3);
|
|
164
|
+
if (!/^\d{1,12}$/u.test(timestamp) || Math.abs(nowSeconds - Number(timestamp)) > WEBHOOK_TOLERANCE_SECONDS) {
|
|
165
|
+
return { ok: false, reason: "timestamp_out_of_window" };
|
|
166
|
+
}
|
|
167
|
+
const content = encoder.encode(webhookSignedContent(id, timestamp, input.payload));
|
|
168
|
+
const signatures = signatureHeader.split(" ").flatMap((entry) => {
|
|
169
|
+
const [version, signature] = entry.split(",");
|
|
170
|
+
if (version !== "v1a" || !signature)
|
|
171
|
+
return [];
|
|
172
|
+
try {
|
|
173
|
+
return [decodeBase64(signature)];
|
|
174
|
+
} catch {
|
|
175
|
+
return [];
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
for (const publicKey of input.publicKeys) {
|
|
179
|
+
for (const signature of signatures) {
|
|
180
|
+
if (await verifyWithSigningKey(publicKey, signature, content))
|
|
181
|
+
return { ok: true };
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return { ok: false, reason: "no_matching_signature" };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// src/runtime/preview.ts
|
|
188
|
+
var PREVIEW_COOKIE = "siteplane-preview";
|
|
189
|
+
async function verifySitePreviewToken(config, token, keys, now) {
|
|
190
|
+
const verify = async (reload) => verifyPreviewToken({
|
|
191
|
+
token,
|
|
192
|
+
siteId: config.siteId,
|
|
193
|
+
publicKeys: await keys(config, "preview", reload ? { reload } : {}),
|
|
194
|
+
now
|
|
195
|
+
});
|
|
196
|
+
let result = await verify(false);
|
|
197
|
+
if (!result.ok && result.reason === "signature_invalid") result = await verify(true);
|
|
198
|
+
return result.ok ? result.claims : null;
|
|
199
|
+
}
|
|
200
|
+
async function verifySiteRevalidation(config, request, keys, now) {
|
|
201
|
+
const verify = async (reload) => verifyWebhook({
|
|
202
|
+
headers: request.headers,
|
|
203
|
+
payload: request.payload,
|
|
204
|
+
publicKeys: await keys(config, "revalidation", reload ? { reload } : {}),
|
|
205
|
+
now
|
|
206
|
+
});
|
|
207
|
+
let result = await verify(false);
|
|
208
|
+
if (!result.ok && result.reason === "no_matching_signature") result = await verify(true);
|
|
209
|
+
return result.ok;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// src/runtime/fetch-json.ts
|
|
213
|
+
var SITEPLANE_DEADLINE_MS = 3e3;
|
|
214
|
+
var SITEPLANE_DRAFT_MODE_DEADLINE_MS = 1e4;
|
|
215
|
+
var MAX_BODY_BYTES = 10 * 1024 * 1024;
|
|
216
|
+
async function fetchJson(fetchImpl, url, init, deadlineMs = SITEPLANE_DEADLINE_MS) {
|
|
217
|
+
const controller = new AbortController();
|
|
218
|
+
let timer;
|
|
219
|
+
const deadline = new Promise((resolve) => {
|
|
220
|
+
timer = setTimeout(() => {
|
|
221
|
+
controller.abort();
|
|
222
|
+
resolve(null);
|
|
223
|
+
}, deadlineMs);
|
|
224
|
+
});
|
|
225
|
+
const answer = (async () => {
|
|
226
|
+
const response = await fetchImpl(url, { ...init, signal: controller.signal });
|
|
227
|
+
const text = await readBoundedText(response.body, MAX_BODY_BYTES);
|
|
228
|
+
if (text === null) return null;
|
|
229
|
+
return { status: response.status, body: JSON.parse(text) };
|
|
230
|
+
})().catch(() => null);
|
|
231
|
+
try {
|
|
232
|
+
return await Promise.race([answer, deadline]);
|
|
233
|
+
} finally {
|
|
234
|
+
clearTimeout(timer);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
async function readBoundedText(body, maxBytes) {
|
|
238
|
+
if (!body) return "";
|
|
239
|
+
const reader = body.getReader();
|
|
240
|
+
const chunks = [];
|
|
241
|
+
let bytes = 0;
|
|
242
|
+
for (; ; ) {
|
|
243
|
+
const chunk = await reader.read();
|
|
244
|
+
if (chunk.done) break;
|
|
245
|
+
bytes += chunk.value.byteLength;
|
|
246
|
+
if (bytes > maxBytes) {
|
|
247
|
+
await reader.cancel();
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
chunks.push(chunk.value);
|
|
251
|
+
}
|
|
252
|
+
const combined = new Uint8Array(bytes);
|
|
253
|
+
let offset = 0;
|
|
254
|
+
for (const chunk of chunks) {
|
|
255
|
+
combined.set(chunk, offset);
|
|
256
|
+
offset += chunk.byteLength;
|
|
257
|
+
}
|
|
258
|
+
return new TextDecoder().decode(combined);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// src/runtime/server-state.ts
|
|
262
|
+
function serverShared(name, create) {
|
|
263
|
+
const key = /* @__PURE__ */ Symbol.for(`siteplane.${name}`);
|
|
264
|
+
const store = globalThis;
|
|
265
|
+
return store[key] ??= create();
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// src/runtime/values.ts
|
|
269
|
+
var BUILD_READ_LIMITS = { attempts: 3, budgetMs: 1e4, attemptMs: SITEPLANE_DEADLINE_MS, pausesMs: [500, 1e3] };
|
|
270
|
+
var MAX_VALUES_BYTES = 10 * 1024 * 1024;
|
|
271
|
+
var realTiming = {
|
|
272
|
+
now: () => performance.now(),
|
|
273
|
+
sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
274
|
+
};
|
|
275
|
+
function siteCacheTag(siteId) {
|
|
276
|
+
return `siteplane:${siteId}`;
|
|
277
|
+
}
|
|
278
|
+
function createValueLoader(dependencies) {
|
|
279
|
+
const log = dependencies.log ?? ((message) => console.warn(message));
|
|
280
|
+
const timing = dependencies.timing ?? realTiming;
|
|
281
|
+
const state = dependencies.state ?? serverShared("valueLoader", () => ({ lastGood: /* @__PURE__ */ new Map(), buildFailures: /* @__PURE__ */ new Map(), buildNotice: { logged: false } }));
|
|
282
|
+
async function published(config, mode) {
|
|
283
|
+
const key = `${config.apiOrigin} ${config.siteId}`;
|
|
284
|
+
const { url, init } = dependencies.publishedRequest(config, mode);
|
|
285
|
+
const once = (deadlineMs) => readValues(dependencies.fetch, config, url, init, deadlineMs);
|
|
286
|
+
let read;
|
|
287
|
+
if (mode === "build") {
|
|
288
|
+
const failed = state.buildFailures.get(key);
|
|
289
|
+
const notice = (reason) => {
|
|
290
|
+
if (state.buildNotice.logged) return;
|
|
291
|
+
state.buildNotice.logged = true;
|
|
292
|
+
log(buildRetryMessage(config, reason));
|
|
293
|
+
};
|
|
294
|
+
read = failed === void 0 ? await readValuesForBuild(once, timing, notice) : { ok: false, reason: failed };
|
|
295
|
+
if (!read.ok) state.buildFailures.set(key, read.reason);
|
|
296
|
+
} else {
|
|
297
|
+
read = await once(mode === "preview" ? SITEPLANE_DRAFT_MODE_DEADLINE_MS : SITEPLANE_DEADLINE_MS);
|
|
298
|
+
}
|
|
299
|
+
if (read.ok) {
|
|
300
|
+
const stand = { values: read.values, revision: read.revision };
|
|
301
|
+
state.lastGood.set(key, stand);
|
|
302
|
+
return stand;
|
|
303
|
+
}
|
|
304
|
+
const previous = state.lastGood.get(key);
|
|
305
|
+
if (previous) {
|
|
306
|
+
const revision = previous.revision === null ? "" : ` (revision ${previous.revision})`;
|
|
307
|
+
const kept = mode === "build" ? "this build uses the content it read for its earlier pages" : "this server shows its last good content";
|
|
308
|
+
log(`[Siteplane] Could not read the content (${read.reason}); ${kept}${revision}.`);
|
|
309
|
+
return previous;
|
|
310
|
+
}
|
|
311
|
+
throw new Error(mode === "build" ? buildFailedMessage(config, read.reason) : readFailedMessage(config, read.reason));
|
|
312
|
+
}
|
|
313
|
+
return {
|
|
314
|
+
async load(config, { mode, previewToken }) {
|
|
315
|
+
const drafts = previewToken ? readDrafts(dependencies.fetch, config, previewToken) : Promise.resolve(null);
|
|
316
|
+
const [stand, draftValues] = await Promise.all([published(config, mode), drafts]);
|
|
317
|
+
return draftValues ? { ...stand.values, ...draftValues } : stand.values;
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
function readFailedMessage(config, reason) {
|
|
322
|
+
const host = hostOf(config);
|
|
323
|
+
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.`;
|
|
324
|
+
}
|
|
325
|
+
function buildRetryMessage(config, reason) {
|
|
326
|
+
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.`;
|
|
327
|
+
}
|
|
328
|
+
function buildFailedMessage(config, reason) {
|
|
329
|
+
const host = hostOf(config);
|
|
330
|
+
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.`;
|
|
331
|
+
}
|
|
332
|
+
function hostOf(config) {
|
|
333
|
+
try {
|
|
334
|
+
return new URL(config.apiOrigin).host;
|
|
335
|
+
} catch {
|
|
336
|
+
return "Siteplane";
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
async function readValuesForBuild(read, timing = realTiming, firstFailed) {
|
|
340
|
+
const started = timing.now();
|
|
341
|
+
const elapsed = () => timing.now() - started;
|
|
342
|
+
let first = null;
|
|
343
|
+
let attempts = 0;
|
|
344
|
+
while (attempts < BUILD_READ_LIMITS.attempts) {
|
|
345
|
+
const left = BUILD_READ_LIMITS.budgetMs - elapsed();
|
|
346
|
+
if (left <= 0) break;
|
|
347
|
+
attempts += 1;
|
|
348
|
+
const result = await read(Math.min(BUILD_READ_LIMITS.attemptMs, left));
|
|
349
|
+
if (result.ok) return { ...result, attempts, elapsedMs: elapsed() };
|
|
350
|
+
if (first === null) firstFailed?.(result.reason);
|
|
351
|
+
first ??= result.reason;
|
|
352
|
+
const pause = BUILD_READ_LIMITS.pausesMs[attempts - 1];
|
|
353
|
+
if (attempts < BUILD_READ_LIMITS.attempts && pause !== void 0 && elapsed() + pause < BUILD_READ_LIMITS.budgetMs) {
|
|
354
|
+
await timing.sleep(pause);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return { ok: false, reason: first ?? `no answer within ${BUILD_READ_LIMITS.budgetMs / 1e3} s`, attempts, elapsedMs: elapsed() };
|
|
358
|
+
}
|
|
359
|
+
async function readValues(fetchImpl, config, url, init, deadlineMs) {
|
|
360
|
+
const controller = new AbortController();
|
|
361
|
+
let timer;
|
|
362
|
+
const deadline = new Promise((resolve) => {
|
|
363
|
+
timer = setTimeout(() => {
|
|
364
|
+
controller.abort();
|
|
365
|
+
resolve({ ok: false, reason: `no answer within ${deadlineMs / 1e3} s` });
|
|
366
|
+
}, deadlineMs);
|
|
367
|
+
});
|
|
368
|
+
const attempt = (async () => {
|
|
369
|
+
let response;
|
|
370
|
+
try {
|
|
371
|
+
response = await fetchImpl(url, { ...init, signal: controller.signal });
|
|
372
|
+
} catch (error) {
|
|
373
|
+
return { ok: false, reason: networkReason(error) };
|
|
374
|
+
}
|
|
375
|
+
if (response.status !== 200) {
|
|
376
|
+
void response.body?.cancel().catch(() => {
|
|
377
|
+
});
|
|
378
|
+
return { ok: false, reason: `HTTP ${response.status}` };
|
|
379
|
+
}
|
|
380
|
+
let text;
|
|
381
|
+
try {
|
|
382
|
+
text = await readBoundedText(response.body, MAX_VALUES_BYTES);
|
|
383
|
+
} catch (error) {
|
|
384
|
+
return { ok: false, reason: networkReason(error) };
|
|
385
|
+
}
|
|
386
|
+
if (text === null) return { ok: false, reason: "an answer larger than 10 MB" };
|
|
387
|
+
let body;
|
|
388
|
+
try {
|
|
389
|
+
body = JSON.parse(text);
|
|
390
|
+
} catch {
|
|
391
|
+
return { ok: false, reason: "an answer that is not JSON" };
|
|
392
|
+
}
|
|
393
|
+
const { siteId, values, revision } = isRecord(body) ? body : {};
|
|
394
|
+
if (siteId !== config.siteId) return { ok: false, reason: "an answer for another site" };
|
|
395
|
+
if (!isRecord(values)) return { ok: false, reason: "an answer without content" };
|
|
396
|
+
return { ok: true, values, revision: isRevision(revision) ? revision : null };
|
|
397
|
+
})();
|
|
398
|
+
try {
|
|
399
|
+
return await Promise.race([attempt, deadline]);
|
|
400
|
+
} finally {
|
|
401
|
+
clearTimeout(timer);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
async function readDrafts(fetchImpl, config, previewToken) {
|
|
405
|
+
const read = await readValues(
|
|
406
|
+
fetchImpl,
|
|
407
|
+
config,
|
|
408
|
+
runtimeDraftsUrl(config.apiOrigin, config.siteId),
|
|
409
|
+
{ cache: "no-store", headers: { authorization: `Bearer ${previewToken}` } },
|
|
410
|
+
SITEPLANE_DRAFT_MODE_DEADLINE_MS
|
|
411
|
+
);
|
|
412
|
+
return read.ok ? read.values : null;
|
|
413
|
+
}
|
|
414
|
+
function networkReason(error) {
|
|
415
|
+
const code = errorCode(error);
|
|
416
|
+
if (code === "ECONNREFUSED") return "connection refused";
|
|
417
|
+
if (code === "ECONNRESET" || code === "UND_ERR_SOCKET" || code === "EPIPE" || code === "UND_ERR_CLOSED") return "connection closed";
|
|
418
|
+
if (code === "ENOTFOUND" || code === "EAI_AGAIN") return "host not found";
|
|
419
|
+
return "network error";
|
|
420
|
+
}
|
|
421
|
+
function errorCode(error) {
|
|
422
|
+
for (let current = error, depth = 0; isRecord(current) && depth < 4; depth += 1) {
|
|
423
|
+
if (typeof current.code === "string") return current.code;
|
|
424
|
+
const first = Array.isArray(current.errors) ? current.errors[0] : void 0;
|
|
425
|
+
if (isRecord(first) && typeof first.code === "string") return first.code;
|
|
426
|
+
current = current.cause;
|
|
427
|
+
}
|
|
428
|
+
return null;
|
|
429
|
+
}
|
|
430
|
+
function isRecord(value) {
|
|
431
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// src/astro/server.ts
|
|
435
|
+
import projectConfig from "siteplane/config";
|
|
436
|
+
import projectSchema from "siteplane/schema-file";
|
|
437
|
+
import build from "virtual:siteplane/build";
|
|
438
|
+
|
|
439
|
+
// src/runtime/signing-keys.ts
|
|
440
|
+
var MAX_AGE_MS = 60 * 60 * 1e3;
|
|
441
|
+
var RELOAD_THROTTLE_MS = 60 * 1e3;
|
|
442
|
+
function createSigningKeyStore(dependencies) {
|
|
443
|
+
const cached = /* @__PURE__ */ new Map();
|
|
444
|
+
const lastAttempt = /* @__PURE__ */ new Map();
|
|
445
|
+
async function load(apiOrigin) {
|
|
446
|
+
lastAttempt.set(apiOrigin, dependencies.now());
|
|
447
|
+
const answer = await fetchJson(dependencies.fetch, runtimeSigningKeysUrl(apiOrigin), { cache: "no-store" });
|
|
448
|
+
const keys = answer?.status === 200 ? readKeySet(answer.body) : null;
|
|
449
|
+
if (keys) cached.set(apiOrigin, { keys, loadedAt: dependencies.now() });
|
|
450
|
+
}
|
|
451
|
+
return {
|
|
452
|
+
async keys(config, purpose, options = {}) {
|
|
453
|
+
const entry = cached.get(config.apiOrigin);
|
|
454
|
+
const now = dependencies.now();
|
|
455
|
+
const expired = !entry || now - entry.loadedAt > MAX_AGE_MS;
|
|
456
|
+
const reloadAllowed = now - (lastAttempt.get(config.apiOrigin) ?? Number.NEGATIVE_INFINITY) >= RELOAD_THROTTLE_MS;
|
|
457
|
+
if ((expired || options.reload) && (reloadAllowed || !entry)) await load(config.apiOrigin);
|
|
458
|
+
return cached.get(config.apiOrigin)?.keys[purpose] ?? [];
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
function readKeySet(body) {
|
|
463
|
+
if (typeof body !== "object" || body === null) return null;
|
|
464
|
+
const { revalidation, preview } = body;
|
|
465
|
+
return isKeyList(revalidation) && isKeyList(preview) ? { revalidation, preview } : null;
|
|
466
|
+
}
|
|
467
|
+
function isKeyList(value) {
|
|
468
|
+
return Array.isArray(value) && value.every((key) => typeof key === "string" && key.startsWith("whpk_"));
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// src/astro/server.ts
|
|
472
|
+
var siteConfig = readSiteplaneConfig(projectConfig);
|
|
473
|
+
var siteSchema = projectSchema;
|
|
474
|
+
var buildInfo = build;
|
|
475
|
+
var webFetch = (input, init) => fetch(input, init);
|
|
476
|
+
var signingKeys = createSigningKeyStore({ fetch: webFetch, now: () => Date.now() });
|
|
477
|
+
|
|
478
|
+
export {
|
|
479
|
+
runtimeValuesUrl,
|
|
480
|
+
runtimeCdnValuesUrl,
|
|
481
|
+
PREVIEW_COOKIE,
|
|
482
|
+
verifySitePreviewToken,
|
|
483
|
+
verifySiteRevalidation,
|
|
484
|
+
serverShared,
|
|
485
|
+
readBoundedText,
|
|
486
|
+
siteCacheTag,
|
|
487
|
+
createValueLoader,
|
|
488
|
+
siteConfig,
|
|
489
|
+
siteSchema,
|
|
490
|
+
buildInfo,
|
|
491
|
+
webFetch,
|
|
492
|
+
signingKeys
|
|
493
|
+
};
|