@uniformdev/canvas-next-rsc 19.79.1-alpha.11 → 19.79.1-alpha.18

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/dist/handler.js CHANGED
@@ -27,83 +27,147 @@ __export(handler_exports, {
27
27
  module.exports = __toCommonJS(handler_exports);
28
28
 
29
29
  // src/handler/createPreviewGETRouteHandler.ts
30
- var import_canvas4 = require("@uniformdev/canvas");
31
- var import_headers2 = require("next/headers");
32
- var import_navigation = require("next/navigation");
33
-
34
- // src/utils/route.ts
35
- var import_canvas3 = require("@uniformdev/canvas");
36
- var import_canvas_next_rsc_shared2 = require("@uniformdev/canvas-next-rsc-shared");
37
- var import_redirect = require("@uniformdev/redirect");
38
-
39
- // src/config/helpers.ts
40
- var import_canvas_next_rsc_shared = require("@uniformdev/canvas-next-rsc-shared");
41
-
42
- // src/utils/draft.ts
43
30
  var import_canvas = require("@uniformdev/canvas");
44
31
  var import_headers = require("next/headers");
45
- var isDraftModeEnabled = ({
46
- searchParams
47
- }) => {
48
- if (isDevelopmentEnvironment()) {
49
- return isIncontextEditingEnabled({ searchParams });
50
- }
51
- let draftModeEnabled = false;
52
- try {
53
- draftModeEnabled = (0, import_headers.draftMode)().isEnabled;
54
- } catch (e) {
32
+ var import_navigation = require("next/navigation");
33
+ var BASE_URL_EXAMPLE = "https://example.com";
34
+ var getQueryParam = (req, paramName) => {
35
+ const value = req.nextUrl.searchParams.get(paramName);
36
+ if (typeof value === "undefined") {
37
+ return void 0;
55
38
  }
56
- return draftModeEnabled || isIncontextEditingEnabled({ searchParams });
57
- };
58
- var isIncontextEditingEnabled = ({
59
- searchParams
60
- }) => {
61
- const containsKey = typeof (searchParams == null ? void 0 : searchParams[import_canvas.IN_CONTEXT_EDITOR_QUERY_STRING_PARAM]) !== "undefined";
62
- return containsKey;
63
- };
64
- var isOnVercelPreviewEnvironment = () => {
65
- return process.env.VERCEL_ENV === "preview";
66
- };
67
- var isDevelopmentEnvironment = () => {
68
- return process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test";
39
+ return Array.isArray(value) ? value[0] : value;
69
40
  };
70
-
71
- // src/config/helpers.ts
72
- var shouldCacheBeDisabled = (options) => {
73
- if (isDraftModeEnabled(options)) {
74
- return { disabled: true, reason: "DRAFT" };
75
- }
76
- if (isIncontextEditingEnabled(options)) {
77
- return { disabled: true, reason: "INCONTEXT" };
78
- }
79
- if (isDevelopmentEnvironment()) {
80
- return { disabled: true, reason: "DEV" };
81
- }
82
- return {
83
- disabled: false,
84
- reason: void 0
41
+ var contextualEditingQueryParams = [
42
+ import_canvas.IN_CONTEXT_EDITOR_QUERY_STRING_PARAM,
43
+ import_canvas.IN_CONTEXT_EDITOR_PLAYGROUND_QUERY_STRING_PARAM
44
+ ];
45
+ var createPreviewGETRouteHandler = (options) => {
46
+ return async (request) => {
47
+ const isConfigCheck = getQueryParam(request, "is_config_check") === "true";
48
+ if (isConfigCheck) {
49
+ return Response.json(
50
+ {
51
+ hasPlayground: Boolean(options == null ? void 0 : options.playgroundPath),
52
+ isUsingCustomFullPathResolver: false
53
+ },
54
+ {
55
+ headers: {
56
+ "Access-Control-Allow-Origin": process.env.UNIFORM_CLI_BASE_URL || "https://uniform.app"
57
+ }
58
+ }
59
+ );
60
+ }
61
+ if (request.headers.get("sec-fetch-mode") === "no-cors") {
62
+ return new Response(null, { status: 204 });
63
+ }
64
+ if (!process.env.UNIFORM_PREVIEW_SECRET) {
65
+ return new Response("No preview secret is configured", { status: 401 });
66
+ }
67
+ const { searchParams } = new URL(request.url);
68
+ const isPlayground = searchParams.get(import_canvas.IN_CONTEXT_EDITOR_PLAYGROUND_QUERY_STRING_PARAM) === "true";
69
+ let pathToRedirectTo;
70
+ if (isPlayground) {
71
+ if (!(options == null ? void 0 : options.playgroundPath)) {
72
+ return new Response("No playground path is configured", { status: 401 });
73
+ }
74
+ pathToRedirectTo = options.playgroundPath;
75
+ }
76
+ const id = getQueryParam(request, compositionQueryParam.id);
77
+ const slug = getQueryParam(request, compositionQueryParam.slug);
78
+ const path = getQueryParam(request, compositionQueryParam.path);
79
+ const locale = getQueryParam(request, compositionQueryParam.locale);
80
+ const disable = getQueryParam(request, "disable");
81
+ const secret = getQueryParam(request, import_canvas.SECRET_QUERY_STRING_PARAM);
82
+ const referer = request.headers.get("referer");
83
+ const isUniformContextualEditing = getQueryParam(request, import_canvas.IN_CONTEXT_EDITOR_QUERY_STRING_PARAM) === "true" && (0, import_canvas.isAllowedReferrer)(referer || void 0);
84
+ if (typeof pathToRedirectTo === "undefined") {
85
+ const resolveFullPath = (options == null ? void 0 : options.resolveFullPath) || resolveFullPathDefault;
86
+ pathToRedirectTo = resolveFullPath({ id, slug, path, locale });
87
+ }
88
+ validateLocalRedirectUrl(pathToRedirectTo);
89
+ if (!pathToRedirectTo) {
90
+ return new Response("Could not resolve the full path of the preview page", { status: 400 });
91
+ }
92
+ if (disable) {
93
+ (0, import_headers.draftMode)().disable();
94
+ (0, import_navigation.redirect)(pathToRedirectTo);
95
+ return;
96
+ }
97
+ if (secret !== process.env.UNIFORM_PREVIEW_SECRET) {
98
+ return new Response("Invalid preview secret", { status: 401 });
99
+ }
100
+ (0, import_headers.draftMode)().enable();
101
+ const redirectionUrl = new URL(pathToRedirectTo, BASE_URL_EXAMPLE);
102
+ assignRequestQueryToSearchParams(redirectionUrl.searchParams, searchParams);
103
+ if (isPlayground) {
104
+ redirectionUrl.searchParams.set("id", searchParams.get("id") || "");
105
+ }
106
+ if (!isUniformContextualEditing) {
107
+ contextualEditingQueryParams.forEach((param) => {
108
+ redirectionUrl.searchParams.delete(param);
109
+ });
110
+ }
111
+ const fullPathToRedirectTo = redirectionUrl.href.replace(BASE_URL_EXAMPLE, "");
112
+ (0, import_navigation.redirect)(fullPathToRedirectTo);
85
113
  };
86
114
  };
87
- var getCanvasCacheStrategy = (options) => {
88
- const { disabled, reason } = shouldCacheBeDisabled(options);
89
- const config = (0, import_canvas_next_rsc_shared.getServerConfig)();
90
- if (disabled) {
91
- if (reason === "DEV" && config.canvasCache) {
92
- console.warn("Canvas cache (disabled) has been overridden by canvasCache config.");
93
- return config.canvasCache;
115
+ function validateLocalRedirectUrl(pathToRedirectTo) {
116
+ if (pathToRedirectTo == null ? void 0 : pathToRedirectTo.match(/^[a-z]+:\/\//g)) {
117
+ throw new Error("Tried to redirect to absolute URL with protocol. Disallowing open redirect.");
118
+ }
119
+ }
120
+ var resolveFullPathDefault = ({ slug, path }) => {
121
+ return path || slug;
122
+ };
123
+ var compositionQueryParam = {
124
+ id: "id",
125
+ slug: "slug",
126
+ path: "path",
127
+ locale: "locale"
128
+ };
129
+ var assignRequestQueryToSearchParams = (searchParams, query) => {
130
+ const compositionQueryParamNames = Object.values(compositionQueryParam);
131
+ for (const [name, value] of query.entries()) {
132
+ if (name === import_canvas.SECRET_QUERY_STRING_PARAM) {
133
+ continue;
94
134
  }
95
- return {
96
- type: "no-cache"
97
- };
98
- }
99
- if (config.canvasCache) {
100
- return config.canvasCache;
135
+ if (compositionQueryParamNames.includes(name)) {
136
+ continue;
137
+ }
138
+ if (typeof value === "undefined") {
139
+ continue;
140
+ }
141
+ searchParams.append(name, value);
101
142
  }
102
- return {
103
- type: "force-cache"
143
+ };
144
+
145
+ // src/handler/createPreviewOPTIONSRouteHandler.ts
146
+ var createPreviewOPTIONSRouteHandler = () => {
147
+ return async () => {
148
+ return new Response(null, {
149
+ headers: {
150
+ "Access-Control-Allow-Origin": process.env.UNIFORM_CLI_BASE_URL || "https://uniform.app",
151
+ "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
152
+ "Access-Control-Allow-Headers": "*"
153
+ }
154
+ });
104
155
  };
105
156
  };
106
157
 
158
+ // src/handler/createPreviewPOSTRouteHandler.ts
159
+ var import_canvas5 = require("@uniformdev/canvas");
160
+ var import_cache = require("next/cache");
161
+
162
+ // src/handler/helpers.ts
163
+ var import_canvas4 = require("@uniformdev/canvas");
164
+ var import_canvas_next_rsc_shared = require("@uniformdev/canvas-next-rsc-shared");
165
+ var import_edge_config = require("@vercel/edge-config");
166
+ var import_svix = require("svix");
167
+
168
+ // src/clients/canvasClient.ts
169
+ var import_canvas2 = require("@uniformdev/canvas");
170
+
107
171
  // src/env/index.ts
108
172
  var env = {
109
173
  getProjectId: () => {
@@ -145,11 +209,11 @@ var determineFetchCacheOptions = (mode) => {
145
209
  };
146
210
  };
147
211
 
148
- // src/clients/routeClient.ts
149
- var import_canvas2 = require("@uniformdev/canvas");
150
- var getRouteClient = (options) => {
151
- const client = new import_canvas2.RouteClient({
212
+ // src/clients/canvasClient.ts
213
+ var getCanvasClient = (options) => {
214
+ return new import_canvas2.CanvasClient({
152
215
  projectId: env.getProjectId(),
216
+ apiHost: env.getApiHost(),
153
217
  apiKey: env.getApiKey(),
154
218
  edgeApiHost: env.getEdgeApiHost(),
155
219
  fetch: (req, init) => {
@@ -163,166 +227,36 @@ var getRouteClient = (options) => {
163
227
  }
164
228
  const tags = [];
165
229
  if (requestedUrl) {
166
- const pathKey = "path";
167
- const path = requestedUrl.searchParams.get(pathKey);
168
- if (path) {
169
- const pieces = path.split("/");
170
- for (let i = 0; i < pieces.length; i++) {
171
- const segmentPieces = pieces.slice(0, i + 1);
172
- const segment = segmentPieces.join("/");
173
- tags.push(buildPathTag(segment));
230
+ const compositionIdKey = "compositionId";
231
+ const compositionIdsKey = "compositionIDs";
232
+ const compositionId = requestedUrl.searchParams.get(compositionIdKey);
233
+ const compositionIds = requestedUrl.searchParams.get(compositionIdsKey);
234
+ if (compositionId) {
235
+ tags.push(buildCompositionTag(compositionId));
236
+ }
237
+ if (compositionIds) {
238
+ const ids = compositionIds.split(",");
239
+ for (let i = 0; i < ids.length; i++) {
240
+ tags.push(buildCompositionTag(ids[i]));
174
241
  }
175
242
  }
176
243
  }
177
244
  const { cache, revalidate } = determineFetchCacheOptions(options.cache);
178
245
  return fetch(req, {
179
246
  ...init,
247
+ cache,
180
248
  headers: {
181
249
  ...init == null ? void 0 : init.headers,
182
250
  "x-bypass-cache": "true"
183
251
  },
184
- cache,
185
252
  next: {
186
- revalidate,
187
- tags: tags.length ? tags : void 0
253
+ revalidate
188
254
  }
189
255
  });
190
256
  }
191
257
  });
192
- return client;
193
- };
194
- var getDefaultRouteClient = ({ searchParams }) => {
195
- return getRouteClient({
196
- cache: getCanvasCacheStrategy({
197
- searchParams
198
- })
199
- });
200
- };
201
-
202
- // src/utils/route.ts
203
- var retrieveRouteByPath = async ({
204
- path,
205
- state,
206
- searchParams
207
- }) => {
208
- var _a;
209
- const client = getDefaultRouteClient({
210
- searchParams
211
- });
212
- return await client.getRoute({
213
- path,
214
- state,
215
- withComponentIDs: true,
216
- withContentSourceMap: isOnVercelPreviewEnvironment() && ((_a = (0, import_canvas_next_rsc_shared2.getServerConfig)().experimental) == null ? void 0 : _a.vercelVisualEditing)
217
- });
218
- };
219
- var resolveRedirectHref = (resolveResult, path) => {
220
- let href;
221
- if (resolveResult.redirect.targetProjectMapNodeId) {
222
- const requestUrl = `${(0, import_canvas_next_rsc_shared2.getBaseUrl)()}${path}`;
223
- const expandedUrl = (0, import_redirect.getTargetVariableExpandedUrl)(requestUrl, resolveResult.redirect);
224
- const url = new URL(expandedUrl);
225
- href = url.pathname;
226
- } else {
227
- href = resolveResult.redirect.targetUrl;
228
- }
229
- return href;
230
- };
231
-
232
- // src/handler/createPreviewGETRouteHandler.ts
233
- var getQueryParam = (req, paramName) => {
234
- const value = req.nextUrl.searchParams.get(paramName);
235
- if (typeof value === "undefined") {
236
- return void 0;
237
- }
238
- return Array.isArray(value) ? value[0] : value;
239
- };
240
- var createPreviewGETRouteHandler = (options) => {
241
- return async (request) => {
242
- const isConfigCheck = getQueryParam(request, "is_config_check") === "true";
243
- if (isConfigCheck) {
244
- return Response.json(
245
- {
246
- hasPlayground: Boolean(options == null ? void 0 : options.playgroundPath),
247
- isUsingCustomFullPathResolver: false
248
- },
249
- {
250
- headers: {
251
- "Access-Control-Allow-Origin": process.env.UNIFORM_CLI_BASE_URL || "https://uniform.app"
252
- }
253
- }
254
- );
255
- }
256
- if (request.headers.get("sec-fetch-mode") === "no-cors") {
257
- return new Response(null, { status: 204 });
258
- }
259
- if (!process.env.UNIFORM_PREVIEW_SECRET) {
260
- return new Response("No preview secret is configured", { status: 401 });
261
- }
262
- const { searchParams } = new URL(request.url);
263
- const secret = searchParams.get("secret");
264
- const path = searchParams.get("path");
265
- const isPlayground = searchParams.get(import_canvas4.IN_CONTEXT_EDITOR_PLAYGROUND_QUERY_STRING_PARAM) === "true";
266
- const id = searchParams.get("id");
267
- if (secret !== process.env.UNIFORM_PREVIEW_SECRET) {
268
- return new Response("Invalid preview secret", { status: 401 });
269
- }
270
- if (!isPlayground) {
271
- if (!path) {
272
- return new Response("Path not provided", { status: 401 });
273
- }
274
- const resolveResult = await retrieveRouteByPath({
275
- path,
276
- state: import_canvas4.CANVAS_DRAFT_STATE,
277
- searchParams: {
278
- // about to be in draft mode so pretend for now.
279
- [import_canvas4.IN_CONTEXT_EDITOR_QUERY_STRING_PARAM]: "true"
280
- }
281
- });
282
- (0, import_headers2.draftMode)().enable();
283
- if (resolveResult.type === "redirect") {
284
- const href = resolveRedirectHref(resolveResult, path);
285
- if (resolveResult.redirect.targetProjectMapNodeId) {
286
- (0, import_navigation.redirect)(`${href}?${import_canvas4.IN_CONTEXT_EDITOR_QUERY_STRING_PARAM}=true`);
287
- } else {
288
- (0, import_navigation.redirect)(href);
289
- }
290
- }
291
- if (resolveResult.type === "notFound") {
292
- return new Response("Invalid path", { status: 401 });
293
- }
294
- (0, import_navigation.redirect)(`${path}?${import_canvas4.IN_CONTEXT_EDITOR_QUERY_STRING_PARAM}=true`);
295
- } else {
296
- if (!(options == null ? void 0 : options.playgroundPath)) {
297
- return new Response("No playground path is configured", { status: 401 });
298
- }
299
- (0, import_headers2.draftMode)().enable();
300
- (0, import_navigation.redirect)(`${options.playgroundPath}?id=${id}&${import_canvas4.IN_CONTEXT_EDITOR_QUERY_STRING_PARAM}=true`);
301
- }
302
- };
303
258
  };
304
259
 
305
- // src/handler/createPreviewOPTIONSRouteHandler.ts
306
- var createPreviewOPTIONSRouteHandler = () => {
307
- return async () => {
308
- return new Response(null, {
309
- headers: {
310
- "Access-Control-Allow-Origin": process.env.UNIFORM_CLI_BASE_URL || "https://uniform.app",
311
- "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
312
- "Access-Control-Allow-Headers": "*"
313
- }
314
- });
315
- };
316
- };
317
-
318
- // src/handler/createPreviewPOSTRouteHandler.ts
319
- var import_cache = require("next/cache");
320
-
321
- // src/handler/helpers.ts
322
- var import_canvas_next_rsc_shared3 = require("@uniformdev/canvas-next-rsc-shared");
323
- var import_edge_config = require("@vercel/edge-config");
324
- var import_svix = require("svix");
325
-
326
260
  // src/clients/projectMapClient.ts
327
261
  var import_project_map = require("@uniformdev/project-map");
328
262
  var getProjectMapClient = (options) => {
@@ -348,6 +282,53 @@ var getProjectMapClient = (options) => {
348
282
  return manifestClient;
349
283
  };
350
284
 
285
+ // src/clients/routeClient.ts
286
+ var import_canvas3 = require("@uniformdev/canvas");
287
+ var getRouteClient = (options) => {
288
+ const client = new import_canvas3.RouteClient({
289
+ projectId: env.getProjectId(),
290
+ apiKey: env.getApiKey(),
291
+ edgeApiHost: env.getEdgeApiHost(),
292
+ fetch: (req, init) => {
293
+ let requestedUrl;
294
+ if (typeof req === "string") {
295
+ requestedUrl = new URL(req);
296
+ } else if (req instanceof URL) {
297
+ requestedUrl = req;
298
+ } else {
299
+ requestedUrl = new URL(req.url);
300
+ }
301
+ const tags = ["route"];
302
+ if (requestedUrl) {
303
+ const pathKey = "path";
304
+ const path = requestedUrl.searchParams.get(pathKey);
305
+ if (path) {
306
+ const pieces = path.split("/");
307
+ for (let i = 0; i < pieces.length; i++) {
308
+ const segmentPieces = pieces.slice(0, i + 1);
309
+ const segment = segmentPieces.join("/");
310
+ tags.push(buildPathTag(segment));
311
+ }
312
+ }
313
+ }
314
+ const { cache, revalidate } = determineFetchCacheOptions(options.cache);
315
+ return fetch(req, {
316
+ ...init,
317
+ headers: {
318
+ ...init == null ? void 0 : init.headers,
319
+ "x-bypass-cache": "true"
320
+ },
321
+ cache,
322
+ next: {
323
+ revalidate,
324
+ tags: tags.length ? tags : void 0
325
+ }
326
+ });
327
+ }
328
+ });
329
+ return client;
330
+ };
331
+
351
332
  // src/handler/helpers.ts
352
333
  var isSvixMessage = async (request) => {
353
334
  const requiredHeaders = ["svix-id", "svix-timestamp", "svix-signature"];
@@ -382,10 +363,16 @@ var processCompositionChange = async (compositionId) => {
382
363
  type: "no-cache"
383
364
  }
384
365
  });
385
- const { nodes } = await projectMapClient.getNodes({
386
- compositionId
387
- });
366
+ const [{ nodes }, composition] = await Promise.all([
367
+ projectMapClient.getNodes({
368
+ compositionId
369
+ }),
370
+ getComposition({ compositionId })
371
+ ]);
388
372
  const tags = [];
373
+ if (composition == null ? void 0 : composition.pattern) {
374
+ tags.push("route");
375
+ }
389
376
  if (nodes) {
390
377
  for (let i = 0; i < nodes.length; i++) {
391
378
  const node = nodes[i];
@@ -400,6 +387,24 @@ var processCompositionChange = async (compositionId) => {
400
387
  tags
401
388
  };
402
389
  };
390
+ var getComposition = async ({ compositionId }) => {
391
+ const canvasClient = getCanvasClient({
392
+ cache: {
393
+ type: "no-cache"
394
+ }
395
+ });
396
+ try {
397
+ const composition = await canvasClient.getCompositionById({
398
+ compositionId
399
+ });
400
+ return composition;
401
+ } catch (err) {
402
+ if (err instanceof import_canvas4.ApiClientError && err.statusCode === 404) {
403
+ return null;
404
+ }
405
+ throw err;
406
+ }
407
+ };
403
408
  var buildProjectMapNodePaths = (path) => {
404
409
  const tags = [];
405
410
  const isDynamic = path.includes(":");
@@ -442,7 +447,7 @@ var processEdgeConfigChange = async ({ source_url }) => {
442
447
  console.warn("UNIFORM_VERCEL_EDGE_CONFIG_TOKEN is not set, skipping edge redirect upsert");
443
448
  return;
444
449
  }
445
- const config = (0, import_canvas_next_rsc_shared3.getServerConfig)();
450
+ const config = (0, import_canvas_next_rsc_shared.getServerConfig)();
446
451
  const routeClient = getRouteClient({
447
452
  cache: {
448
453
  type: "no-cache"
@@ -532,7 +537,7 @@ var handleManifestPublished = async (body) => {
532
537
  };
533
538
 
534
539
  // src/handler/messages/handleProjectMapNodeDelete.ts
535
- var import_canvas_next_rsc_shared4 = require("@uniformdev/canvas-next-rsc-shared");
540
+ var import_canvas_next_rsc_shared2 = require("@uniformdev/canvas-next-rsc-shared");
536
541
  var import_webhooks5 = require("@uniformdev/webhooks");
537
542
  var handleProjectMapNodeDelete = async (body) => {
538
543
  var _a;
@@ -542,7 +547,7 @@ var handleProjectMapNodeDelete = async (body) => {
542
547
  }
543
548
  const tags = [];
544
549
  tags.push(...buildProjectMapNodePaths(parsed.data.path));
545
- const config = (0, import_canvas_next_rsc_shared4.getServerConfig)();
550
+ const config = (0, import_canvas_next_rsc_shared2.getServerConfig)();
546
551
  if ((_a = config.experimental) == null ? void 0 : _a.edgeCompositions) {
547
552
  await processEdgeConfigChange({
548
553
  source_url: parsed.data.path
@@ -554,7 +559,7 @@ var handleProjectMapNodeDelete = async (body) => {
554
559
  };
555
560
 
556
561
  // src/handler/messages/handleProjectMapNodeInsert.ts
557
- var import_canvas_next_rsc_shared5 = require("@uniformdev/canvas-next-rsc-shared");
562
+ var import_canvas_next_rsc_shared3 = require("@uniformdev/canvas-next-rsc-shared");
558
563
  var import_webhooks6 = require("@uniformdev/webhooks");
559
564
  var handleProjectMapNodeInsert = async (body) => {
560
565
  var _a;
@@ -564,7 +569,7 @@ var handleProjectMapNodeInsert = async (body) => {
564
569
  }
565
570
  const tags = [];
566
571
  tags.push(...buildProjectMapNodePaths(parsed.data.path));
567
- const config = (0, import_canvas_next_rsc_shared5.getServerConfig)();
572
+ const config = (0, import_canvas_next_rsc_shared3.getServerConfig)();
568
573
  if ((_a = config.experimental) == null ? void 0 : _a.edgeCompositions) {
569
574
  await processEdgeConfigChange({
570
575
  source_url: parsed.data.path
@@ -576,7 +581,7 @@ var handleProjectMapNodeInsert = async (body) => {
576
581
  };
577
582
 
578
583
  // src/handler/messages/handleProjectMapNodeUpdate.ts
579
- var import_canvas_next_rsc_shared6 = require("@uniformdev/canvas-next-rsc-shared");
584
+ var import_canvas_next_rsc_shared4 = require("@uniformdev/canvas-next-rsc-shared");
580
585
  var import_webhooks7 = require("@uniformdev/webhooks");
581
586
  var handleProjectMapNodeUpdate = async (body) => {
582
587
  var _a;
@@ -587,7 +592,7 @@ var handleProjectMapNodeUpdate = async (body) => {
587
592
  const tags = [];
588
593
  tags.push(...buildProjectMapNodePaths(parsed.data.path));
589
594
  tags.push(...buildProjectMapNodePaths(parsed.data.previous_path));
590
- const config = (0, import_canvas_next_rsc_shared6.getServerConfig)();
595
+ const config = (0, import_canvas_next_rsc_shared4.getServerConfig)();
591
596
  if ((_a = config.experimental) == null ? void 0 : _a.edgeCompositions) {
592
597
  await processEdgeConfigChange({
593
598
  source_url: parsed.data.path
@@ -599,7 +604,7 @@ var handleProjectMapNodeUpdate = async (body) => {
599
604
  };
600
605
 
601
606
  // src/handler/messages/handleRedirectDelete.ts
602
- var import_canvas_next_rsc_shared7 = require("@uniformdev/canvas-next-rsc-shared");
607
+ var import_canvas_next_rsc_shared5 = require("@uniformdev/canvas-next-rsc-shared");
603
608
  var import_webhooks8 = require("@uniformdev/webhooks");
604
609
  var handleRedirectDelete = async (body) => {
605
610
  var _a;
@@ -607,7 +612,7 @@ var handleRedirectDelete = async (body) => {
607
612
  if (!parsed.success) {
608
613
  return void 0;
609
614
  }
610
- const config = (0, import_canvas_next_rsc_shared7.getServerConfig)();
615
+ const config = (0, import_canvas_next_rsc_shared5.getServerConfig)();
611
616
  if ((_a = config.experimental) == null ? void 0 : _a.edgeRedirects) {
612
617
  await processEdgeConfigChange(parsed.data);
613
618
  }
@@ -615,7 +620,7 @@ var handleRedirectDelete = async (body) => {
615
620
  };
616
621
 
617
622
  // src/handler/messages/handleRedirectInsert.ts
618
- var import_canvas_next_rsc_shared8 = require("@uniformdev/canvas-next-rsc-shared");
623
+ var import_canvas_next_rsc_shared6 = require("@uniformdev/canvas-next-rsc-shared");
619
624
  var import_webhooks9 = require("@uniformdev/webhooks");
620
625
  var handleRedirectInsert = async (body) => {
621
626
  var _a;
@@ -623,7 +628,7 @@ var handleRedirectInsert = async (body) => {
623
628
  if (!parsed.success) {
624
629
  return void 0;
625
630
  }
626
- const config = (0, import_canvas_next_rsc_shared8.getServerConfig)();
631
+ const config = (0, import_canvas_next_rsc_shared6.getServerConfig)();
627
632
  if ((_a = config.experimental) == null ? void 0 : _a.edgeRedirects) {
628
633
  await processEdgeConfigChange(parsed.data);
629
634
  }
@@ -631,7 +636,7 @@ var handleRedirectInsert = async (body) => {
631
636
  };
632
637
 
633
638
  // src/handler/messages/handleRedirectUpdate.ts
634
- var import_canvas_next_rsc_shared9 = require("@uniformdev/canvas-next-rsc-shared");
639
+ var import_canvas_next_rsc_shared7 = require("@uniformdev/canvas-next-rsc-shared");
635
640
  var import_webhooks10 = require("@uniformdev/webhooks");
636
641
  var handleRedirectUpdate = async (body) => {
637
642
  var _a;
@@ -639,7 +644,7 @@ var handleRedirectUpdate = async (body) => {
639
644
  if (!parsed.success) {
640
645
  return void 0;
641
646
  }
642
- const config = (0, import_canvas_next_rsc_shared9.getServerConfig)();
647
+ const config = (0, import_canvas_next_rsc_shared7.getServerConfig)();
643
648
  if ((_a = config.experimental) == null ? void 0 : _a.edgeRedirects) {
644
649
  await processEdgeConfigChange(parsed.data);
645
650
  }
@@ -649,6 +654,25 @@ var handleRedirectUpdate = async (body) => {
649
654
  // src/handler/createPreviewPOSTRouteHandler.ts
650
655
  var createPreviewPOSTRouteHandler = () => {
651
656
  return async (request) => {
657
+ let previewSecretPassed = false;
658
+ if (process.env.UNIFORM_PREVIEW_SECRET) {
659
+ const secretValue = request.nextUrl.searchParams.get(import_canvas5.SECRET_QUERY_STRING_PARAM);
660
+ if (secretValue !== process.env.UNIFORM_PREVIEW_SECRET) {
661
+ console.warn(
662
+ "The preview secret passed in the query string does not match the UNIFORM_PREVIEW_SECRET env var."
663
+ );
664
+ } else {
665
+ previewSecretPassed = true;
666
+ }
667
+ } else {
668
+ if (!process.env.UNIFORM_WEBHOOK_SECRET) {
669
+ console.warn("Both UNIFORM_PREVIEW_SECRET and UNIFORM_WEBHOOK_SECRET are not set.");
670
+ }
671
+ previewSecretPassed = true;
672
+ }
673
+ if (!previewSecretPassed) {
674
+ return new Response("The request could not be validated.", { status: 401 });
675
+ }
652
676
  const { looksLikeMessage, validation } = await isSvixMessage(request);
653
677
  if (looksLikeMessage) {
654
678
  if (!validation) {
@@ -673,7 +697,13 @@ var handleSvixMessage = async (request) => {
673
697
  handleManifestPublished
674
698
  ];
675
699
  let tags = void 0;
676
- const jsonBody = await request.json();
700
+ let jsonBody;
701
+ try {
702
+ jsonBody = await request.json();
703
+ } catch (err) {
704
+ console.error("Error parsing the request body as JSON.", err);
705
+ return new Response("Error parsing the request body as JSON.", { status: 400 });
706
+ }
677
707
  for (let i = 0; i < handlers.length; i++) {
678
708
  const handler = handlers[i];
679
709
  const result = await handler(jsonBody);