siteplane 0.2.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.
@@ -0,0 +1,161 @@
1
+ import {
2
+ PREVIEW_COOKIE,
3
+ buildInfo,
4
+ readBoundedText,
5
+ signingKeys,
6
+ siteCacheTag,
7
+ siteConfig,
8
+ verifySitePreviewToken,
9
+ verifySiteRevalidation
10
+ } from "../chunk-46ZC6NGK.js";
11
+
12
+ // ../shared/dist/safe-redirect.js
13
+ var UNSAFE_URL_CHARACTERS = /[\\\u0000-\u001f\u007f]/u;
14
+ var MAX_REDIRECT_PATH_LENGTH = 2048;
15
+ var INTERNAL_BASE = "https://siteplane.invalid";
16
+ function hasUnsafeUrlCharacters(value) {
17
+ return UNSAFE_URL_CHARACTERS.test(value);
18
+ }
19
+ function parseSafeRedirectPath(value) {
20
+ if (typeof value !== "string" || value.length > MAX_REDIRECT_PATH_LENGTH || !value.startsWith("/") || value.startsWith("//") || hasUnsafeUrlCharacters(value)) {
21
+ return null;
22
+ }
23
+ let url;
24
+ try {
25
+ url = new URL(value, INTERNAL_BASE);
26
+ } catch {
27
+ return null;
28
+ }
29
+ return url.origin === INTERNAL_BASE && !url.pathname.startsWith("//") ? `${url.pathname}${url.search}${url.hash}` : null;
30
+ }
31
+
32
+ // src/route/handlers.ts
33
+ var MAX_REVALIDATION_BYTES = 16 * 1024;
34
+ var noStore = { "Cache-Control": "no-store" };
35
+ function createRouteHandlers(dependencies) {
36
+ async function GET2(request) {
37
+ const route2 = routeName(request);
38
+ const config = dependencies.config();
39
+ if (!config || route2 !== "status" && route2 !== "draft") return notFound();
40
+ return route2 === "status" ? status(config) : draft(request, config);
41
+ }
42
+ async function POST2(request) {
43
+ const config = dependencies.config();
44
+ if (!config || routeName(request) !== "revalidate") return notFound();
45
+ return revalidate(request, config);
46
+ }
47
+ function status(config) {
48
+ const { VERCEL_GIT_COMMIT_SHA: commit, VERCEL_ENV: environment } = dependencies.env;
49
+ const answer = {
50
+ siteId: config.siteId,
51
+ version: dependencies.version,
52
+ commit: commit || null,
53
+ environment: environment || null,
54
+ ...statusDetails(),
55
+ ...dependencies.devServer ? { devServer: true } : {}
56
+ };
57
+ return Response.json(answer, { headers: noStore });
58
+ }
59
+ function statusDetails() {
60
+ try {
61
+ return dependencies.statusDetails?.() ?? {};
62
+ } catch {
63
+ return {};
64
+ }
65
+ }
66
+ async function draft(request, config) {
67
+ const url = new URL(request.url);
68
+ const query = url.searchParams;
69
+ const count = query.has("count");
70
+ if (count && !dependencies.devServer) return notFound();
71
+ const path = parseSafeRedirectPath(query.get("path") ?? "/");
72
+ if (!path) return Response.json({ error: "invalid_path" }, { status: 400, headers: noStore });
73
+ if (count) {
74
+ return redirect(url, path, await dependencies.enableCount?.() ?? {});
75
+ }
76
+ const now = dependencies.now();
77
+ const token = query.get("token") ?? "";
78
+ const claims = token ? await verifySitePreviewToken(config, token, dependencies.keys, now) : null;
79
+ if (!claims) return Response.json({ error: "preview_token_invalid" }, { status: 401, headers: noStore });
80
+ await dependencies.enableDraftMode({ token, maxAgeSeconds: claims.exp - Math.floor(now.getTime() / 1e3) });
81
+ return redirect(url, path);
82
+ }
83
+ function redirect(url, path, headers = {}) {
84
+ const root = url.pathname.endsWith("/") ? "../../../" : "../../";
85
+ return new Response(null, { status: 307, headers: { ...noStore, ...headers, Location: `${root}${path.slice(1)}` } });
86
+ }
87
+ async function revalidate(request, config) {
88
+ const payload = await readBoundedText(request.body, MAX_REVALIDATION_BYTES);
89
+ if (payload === null) return Response.json({ error: "too_large" }, { status: 413, headers: noStore });
90
+ const now = dependencies.now();
91
+ if (!await verifySiteRevalidation(config, { headers: request.headers, payload }, dependencies.keys, now)) {
92
+ return Response.json({ error: "invalid_signature" }, { status: 401, headers: noStore });
93
+ }
94
+ if (readSiteId(payload) !== config.siteId) {
95
+ return Response.json({ error: "wrong_site" }, { status: 403, headers: noStore });
96
+ }
97
+ dependencies.revalidateTag(siteCacheTag(config.siteId));
98
+ return Response.json({ revalidated: true }, { headers: noStore });
99
+ }
100
+ return { GET: GET2, POST: POST2 };
101
+ }
102
+ function routeName(request) {
103
+ return /\/api\/siteplane\/([a-z]+)\/?$/u.exec(new URL(request.url).pathname)?.[1] ?? null;
104
+ }
105
+ function notFound() {
106
+ return Response.json({ error: "not_found" }, { status: 404, headers: noStore });
107
+ }
108
+ function readSiteId(payload) {
109
+ try {
110
+ return JSON.parse(payload).data?.siteId;
111
+ } catch {
112
+ return void 0;
113
+ }
114
+ }
115
+
116
+ // src/version.ts
117
+ var PACKAGE_VERSION = "0.3.0";
118
+
119
+ // src/astro/route.ts
120
+ var embeddable = { httpOnly: true, secure: true, sameSite: "none", partitioned: true, path: "/" };
121
+ var DRAFT_ROUTE = /\/api\/siteplane\/draft\/?$/u;
122
+ function createSiteplaneRoute(dependencies) {
123
+ const { config, build } = dependencies;
124
+ const handlers = (context) => createRouteHandlers({
125
+ config: () => config,
126
+ keys: dependencies.keys,
127
+ revalidateTag: () => {
128
+ },
129
+ enableDraftMode: async ({ token, maxAgeSeconds }) => {
130
+ context.cookies.set(PREVIEW_COOKIE, token, { ...embeddable, maxAge: maxAgeSeconds });
131
+ },
132
+ env: { VERCEL_GIT_COMMIT_SHA: build.commit ?? void 0, VERCEL_ENV: build.environment ?? void 0 },
133
+ version: PACKAGE_VERSION,
134
+ now: dependencies.now,
135
+ statusDetails: () => ({
136
+ stack: "astro",
137
+ rendering: "server",
138
+ builtAt: build.builtAt,
139
+ revision: build.revision,
140
+ prerenderedFields: build.prerenderedFields,
141
+ pageCache: build.pageCache
142
+ })
143
+ });
144
+ return {
145
+ GET: (context) => {
146
+ if (config && !build.drafts && DRAFT_ROUTE.test(context.url.pathname) && !context.url.searchParams.has("count")) {
147
+ return Response.json({ error: "preview_page_cache" }, { status: 409, headers: { "Cache-Control": "no-store" } });
148
+ }
149
+ return handlers(context).GET(context.request);
150
+ },
151
+ POST: (context) => handlers(context).POST(context.request)
152
+ };
153
+ }
154
+ var route = createSiteplaneRoute({ config: siteConfig, build: buildInfo, keys: signingKeys.keys, now: () => /* @__PURE__ */ new Date() });
155
+ var GET = route.GET;
156
+ var POST = route.POST;
157
+ export {
158
+ GET,
159
+ POST,
160
+ createSiteplaneRoute
161
+ };