@preprio/toolkit 0.6.0-alpha.8 → 1.0.0-rc.1
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/README.md +111 -1363
- package/dist/astro/index.cjs +1555 -182
- package/dist/astro/index.d.cts +201 -1
- package/dist/astro/index.d.ts +201 -1
- package/dist/astro/index.js +1557 -29
- package/dist/{chunk-ATPB7GEC.js → chunk-33GWVTRJ.js} +1 -1
- package/dist/chunk-A2J7R4LR.js +256 -0
- package/dist/{chunk-T7ESBVBI.js → chunk-DSJ22P6J.js} +6 -1
- package/dist/chunk-KM2GME62.js +1255 -0
- package/dist/chunk-UTKH7DOI.js +58 -0
- package/dist/chunk-V7MCHPE7.js +85 -0
- package/dist/{chunk-2QGUP7VQ.js → chunk-WBMN4L7G.js} +1680 -579
- package/dist/chunk-Y7JWV7WV.js +374 -0
- package/dist/codegen/index.cjs +82 -0
- package/dist/codegen/index.d.cts +70 -0
- package/dist/codegen/index.d.ts +70 -0
- package/dist/codegen/index.js +64 -0
- package/dist/index.cjs +3729 -553
- package/dist/index.d.cts +403 -19
- package/dist/index.d.ts +403 -19
- package/dist/index.js +345 -24
- package/dist/live-preview-HCBNBC6C.js +19 -0
- package/dist/nextjs/components.cjs +1872 -729
- package/dist/nextjs/components.d.cts +5 -51
- package/dist/nextjs/components.d.ts +5 -51
- package/dist/nextjs/components.js +8 -274
- package/dist/nextjs/index.cjs +1228 -181
- package/dist/nextjs/index.d.cts +247 -92
- package/dist/nextjs/index.d.ts +247 -92
- package/dist/nextjs/index.js +1629 -111
- package/dist/nextjs/live-action.cjs +298 -0
- package/dist/nextjs/live-action.d.cts +12 -0
- package/dist/nextjs/live-action.d.ts +12 -0
- package/dist/nextjs/live-action.js +68 -0
- package/dist/nextjs/live.cjs +2010 -0
- package/dist/nextjs/live.d.cts +54 -0
- package/dist/nextjs/live.d.ts +54 -0
- package/dist/nextjs/live.js +287 -0
- package/dist/nuxt/chunk-5RBZPRQN.js +350 -0
- package/dist/nuxt/chunk-LUKVPOJ2.js +32 -0
- package/dist/nuxt/index.cjs +15 -16
- package/dist/nuxt/index.d.cts +167 -6
- package/dist/nuxt/index.d.ts +167 -6
- package/dist/nuxt/index.js +402 -15
- package/dist/nuxt/live-preview-KO4IKWTX.js +1234 -0
- package/dist/nuxt/live.cjs +1928 -0
- package/dist/nuxt/live.d.cts +45 -0
- package/dist/nuxt/live.d.ts +45 -0
- package/dist/nuxt/live.js +216 -0
- package/dist/{types-BYH9XqmN.d.cts → pixel-DqU7NvgW.d.cts} +102 -5
- package/dist/{types-BYH9XqmN.d.ts → pixel-DqU7NvgW.d.ts} +102 -5
- package/dist/react/components.cjs +1865 -480
- package/dist/react/components.d.cts +43 -8
- package/dist/react/components.d.ts +43 -8
- package/dist/react/components.js +1865 -480
- package/dist/sveltekit/chunk-5RBZPRQN.js +350 -0
- package/dist/sveltekit/chunk-LUKVPOJ2.js +32 -0
- package/dist/sveltekit/index.cjs +15 -16
- package/dist/sveltekit/index.d.cts +111 -10
- package/dist/sveltekit/index.d.ts +111 -10
- package/dist/sveltekit/index.js +398 -15
- package/dist/sveltekit/live-preview-KO4IKWTX.js +1234 -0
- package/dist/sveltekit/live.cjs +1940 -0
- package/dist/sveltekit/live.d.cts +61 -0
- package/dist/sveltekit/live.d.ts +61 -0
- package/dist/sveltekit/live.js +220 -0
- package/package.json +132 -71
- package/src/astro/PreprToolbar.astro +7 -1
- package/src/astro/PreprTrackingPixel.astro +2 -4
- package/src/nuxt/PreprToolbar.vue +12 -1
- package/src/nuxt/PreprTrackingPixel.vue +2 -4
- package/src/sveltekit/PreprToolbar.svelte +17 -2
- package/src/sveltekit/PreprTrackingPixel.svelte +2 -4
- package/dist/astro/chunk-R2YPGDVU.js +0 -173
- package/dist/astro/graphql.cjs +0 -1078
- package/dist/astro/graphql.d.cts +0 -300
- package/dist/astro/graphql.d.ts +0 -300
- package/dist/astro/graphql.js +0 -984
- package/dist/astro/index-XT_z-RYT.d.cts +0 -130
- package/dist/astro/index-XT_z-RYT.d.ts +0 -130
- package/dist/chunk-3NCEVJJ3.js +0 -10
- package/dist/chunk-OY7OKO6Q.js +0 -499
- package/dist/chunk-S5RWVMDJ.js +0 -148
- package/dist/graphql/codegen.cjs +0 -557
- package/dist/graphql/codegen.d.cts +0 -23
- package/dist/graphql/codegen.d.ts +0 -23
- package/dist/graphql/codegen.js +0 -99
- package/dist/graphql.cjs +0 -1020
- package/dist/graphql.d.cts +0 -335
- package/dist/graphql.d.ts +0 -335
- package/dist/graphql.js +0 -448
- package/dist/nextjs/chunk-O2SBSBDG.js +0 -549
- package/dist/nextjs/graphql.cjs +0 -1811
- package/dist/nextjs/graphql.d.cts +0 -498
- package/dist/nextjs/graphql.d.ts +0 -498
- package/dist/nextjs/graphql.js +0 -1397
- package/dist/nextjs/server-BL-sqZvu.d.cts +0 -156
- package/dist/nextjs/server-BL-sqZvu.d.ts +0 -156
- package/dist/nuxt/chunk-SX3MSVPK.js +0 -417
- package/dist/nuxt/graphql.cjs +0 -1085
- package/dist/nuxt/graphql.d.cts +0 -297
- package/dist/nuxt/graphql.d.ts +0 -297
- package/dist/nuxt/graphql.js +0 -985
- package/dist/nuxt/server-8v6OHbsw.d.cts +0 -158
- package/dist/nuxt/server-8v6OHbsw.d.ts +0 -158
- package/dist/pixel-DwW7cXJm.d.cts +0 -49
- package/dist/pixel-DwW7cXJm.d.ts +0 -49
- package/dist/server-Brr1UgtW.d.ts +0 -59
- package/dist/server-DJuS3YU0.d.cts +0 -59
- package/dist/sveltekit/chunk-UOASSHZO.js +0 -413
- package/dist/sveltekit/graphql.cjs +0 -1091
- package/dist/sveltekit/graphql.d.cts +0 -302
- package/dist/sveltekit/graphql.d.ts +0 -302
- package/dist/sveltekit/graphql.js +0 -991
- package/dist/sveltekit/server-CO847mEV.d.cts +0 -98
- package/dist/sveltekit/server-CO847mEV.d.ts +0 -98
package/dist/nextjs/index.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,26 +17,30 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/nextjs/index.ts
|
|
21
31
|
var nextjs_exports = {};
|
|
22
32
|
__export(nextjs_exports, {
|
|
23
33
|
PreprError: () => PreprError,
|
|
24
|
-
PreprPersonalizationSwitch: () => import_components.PreprPersonalizationSwitch,
|
|
25
34
|
PreprToolbar: () => import_components.PreprToolbar,
|
|
26
35
|
PreprTrackingPixel: () => import_components.PreprTrackingPixel,
|
|
27
|
-
createPreprDraftRoute: () => createPreprDraftRoute,
|
|
28
36
|
createPreprMiddleware: () => createPreprMiddleware,
|
|
29
|
-
createPreprWebhookRoute: () => createPreprWebhookRoute,
|
|
30
37
|
extractAccessToken: () => extractAccessToken,
|
|
31
38
|
getActiveSegment: () => getActiveSegment,
|
|
32
39
|
getActiveVariant: () => getActiveVariant,
|
|
33
40
|
getPreprHeaders: () => getPreprHeaders,
|
|
34
41
|
getPreprUUID: () => getPreprUUID,
|
|
35
42
|
getToolbarProps: () => getToolbarProps,
|
|
36
|
-
|
|
37
|
-
preprDraftRedirect: () => preprDraftRedirect,
|
|
43
|
+
normalizeQuery: () => normalizeQuery,
|
|
38
44
|
validatePreprToken: () => validatePreprToken
|
|
39
45
|
});
|
|
40
46
|
module.exports = __toCommonJS(nextjs_exports);
|
|
@@ -43,7 +49,7 @@ module.exports = __toCommonJS(nextjs_exports);
|
|
|
43
49
|
var import_server = require("next/server");
|
|
44
50
|
|
|
45
51
|
// src/version.ts
|
|
46
|
-
var VERSION = "0.
|
|
52
|
+
var VERSION = "1.0.0-rc.1";
|
|
47
53
|
|
|
48
54
|
// src/core/constants.ts
|
|
49
55
|
var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
|
|
@@ -55,6 +61,8 @@ var PARAM_SEGMENT = "prepr_preview_segment";
|
|
|
55
61
|
var PARAM_VARIANT = "prepr_preview_ab";
|
|
56
62
|
var PARAM_HIDE_BAR = "prepr_hide_bar";
|
|
57
63
|
var ONE_YEAR_SECONDS = 60 * 60 * 24 * 365;
|
|
64
|
+
var ATTR_ENCODED = "data-prepr-encoded";
|
|
65
|
+
var ENCODED_SELECTOR = `[${ATTR_ENCODED}]`;
|
|
58
66
|
|
|
59
67
|
// src/core/features.ts
|
|
60
68
|
function isEnabled(config) {
|
|
@@ -83,6 +91,16 @@ var UTM_PARAMS = [
|
|
|
83
91
|
"utm_campaign"
|
|
84
92
|
];
|
|
85
93
|
var MAX_HEADER_VALUE_LENGTH = 2048;
|
|
94
|
+
function randomUUID() {
|
|
95
|
+
if (typeof crypto.randomUUID === "function") return crypto.randomUUID();
|
|
96
|
+
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
|
97
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
98
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
99
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join(
|
|
100
|
+
""
|
|
101
|
+
);
|
|
102
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
103
|
+
}
|
|
86
104
|
function sanitizeHeaderValue(value) {
|
|
87
105
|
const stripped = value.replace(/[\u0000-\u001f\u007f]/g, "");
|
|
88
106
|
return stripped.length > MAX_HEADER_VALUE_LENGTH ? stripped.slice(0, MAX_HEADER_VALUE_LENGTH) : stripped;
|
|
@@ -154,7 +172,7 @@ function processFrameworkRequest(request, opts, framework) {
|
|
|
154
172
|
}
|
|
155
173
|
let uid = sanitizeHeaderValue(requestCookies.get(COOKIE_UID) ?? "");
|
|
156
174
|
if (!uid) {
|
|
157
|
-
uid =
|
|
175
|
+
uid = randomUUID();
|
|
158
176
|
responseCookies.push({
|
|
159
177
|
name: COOKIE_UID,
|
|
160
178
|
value: uid,
|
|
@@ -223,10 +241,6 @@ function processFrameworkRequest(request, opts, framework) {
|
|
|
223
241
|
});
|
|
224
242
|
}
|
|
225
243
|
}
|
|
226
|
-
if (requestHeaders.has("Prepr-Segments")) {
|
|
227
|
-
requestHeaders.delete("Prepr-Customer-Id");
|
|
228
|
-
requestHeaders.delete("Prepr-Customer-Id-Created");
|
|
229
|
-
}
|
|
230
244
|
return { requestHeaders, responseCookies };
|
|
231
245
|
}
|
|
232
246
|
|
|
@@ -273,6 +287,1189 @@ function createPreprMiddleware(request, responseOrOptions, maybeOptions) {
|
|
|
273
287
|
|
|
274
288
|
// src/nextjs/server.ts
|
|
275
289
|
var import_headers = require("next/headers");
|
|
290
|
+
var navigation = __toESM(require("next/navigation"), 1);
|
|
291
|
+
|
|
292
|
+
// src/core/live-preview/guards.ts
|
|
293
|
+
function isScalar(value) {
|
|
294
|
+
return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
|
|
295
|
+
}
|
|
296
|
+
function isObject(value) {
|
|
297
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// src/core/live-preview/entry-edit-links.ts
|
|
301
|
+
var import_stega = require("@vercel/stega");
|
|
302
|
+
|
|
303
|
+
// src/core/live-preview/validate.ts
|
|
304
|
+
function isPlainRecord(v) {
|
|
305
|
+
return v !== null && typeof v === "object" && Object.getPrototypeOf(v) === Object.prototype;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// src/core/live-preview/entry-edit-links.ts
|
|
309
|
+
var EDIT_PATH = /\/content\/edit\/[^/?#]+/;
|
|
310
|
+
function editLinks(result, entry) {
|
|
311
|
+
const edit = entry.fields?.__edit;
|
|
312
|
+
if (!isPlainRecord(edit) || typeof edit.item !== "string") return null;
|
|
313
|
+
return editLinksFor(result, edit.item);
|
|
314
|
+
}
|
|
315
|
+
var ITEM_ID = /^[A-Za-z0-9_-]+$/;
|
|
316
|
+
function editLinksFor(result, item) {
|
|
317
|
+
if (!ITEM_ID.test(item)) return null;
|
|
318
|
+
const template = findEditLink(result);
|
|
319
|
+
if (!template) return null;
|
|
320
|
+
return {
|
|
321
|
+
encode(text, field) {
|
|
322
|
+
if ((0, import_stega.vercelStegaSplit)(text).encoded) return text;
|
|
323
|
+
let href;
|
|
324
|
+
try {
|
|
325
|
+
href = new URL(template.href);
|
|
326
|
+
} catch {
|
|
327
|
+
return text;
|
|
328
|
+
}
|
|
329
|
+
href.pathname = href.pathname.replace(
|
|
330
|
+
EDIT_PATH,
|
|
331
|
+
() => `/content/edit/${item}`
|
|
332
|
+
);
|
|
333
|
+
href.searchParams.set("field", field);
|
|
334
|
+
const link = href.toString().replace(/%2E/gi, ".").replace(/%2F/gi, "/");
|
|
335
|
+
return (0, import_stega.vercelStegaCombine)(text, { origin: template.origin, href: link });
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
var linkCache = /* @__PURE__ */ new WeakMap();
|
|
340
|
+
function findEditLink(root) {
|
|
341
|
+
if (root === null || typeof root !== "object") return null;
|
|
342
|
+
if (linkCache.has(root)) return linkCache.get(root) ?? null;
|
|
343
|
+
let found = null;
|
|
344
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
345
|
+
const walk2 = (node) => {
|
|
346
|
+
if (found) return;
|
|
347
|
+
if (typeof node === "string") {
|
|
348
|
+
if (!(0, import_stega.vercelStegaSplit)(node).encoded) return;
|
|
349
|
+
try {
|
|
350
|
+
const decoded = (0, import_stega.vercelStegaDecode)(node);
|
|
351
|
+
if (typeof decoded?.origin === "string" && typeof decoded.href === "string" && EDIT_PATH.test(decoded.href)) {
|
|
352
|
+
found = { origin: decoded.origin, href: decoded.href };
|
|
353
|
+
}
|
|
354
|
+
} catch {
|
|
355
|
+
}
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
if (node === null || typeof node !== "object" || seen.has(node)) return;
|
|
359
|
+
seen.add(node);
|
|
360
|
+
for (const value of Object.values(node)) walk2(value);
|
|
361
|
+
};
|
|
362
|
+
walk2(root);
|
|
363
|
+
linkCache.set(root, found);
|
|
364
|
+
return found;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// src/core/live-preview/normalize-query.ts
|
|
368
|
+
var import_stega3 = require("@vercel/stega");
|
|
369
|
+
|
|
370
|
+
// src/core/shared-state.ts
|
|
371
|
+
var KEY = /* @__PURE__ */ Symbol.for("@preprio/toolkit:shared-state");
|
|
372
|
+
function initialState() {
|
|
373
|
+
return {
|
|
374
|
+
trustedParentOrigin: null,
|
|
375
|
+
debugEnabled: void 0,
|
|
376
|
+
strictMode: false,
|
|
377
|
+
liveAnnouncedStrict: void 0
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
function sharedState() {
|
|
381
|
+
const scope = globalThis;
|
|
382
|
+
return scope[KEY] ?? (scope[KEY] = initialState());
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// src/core/utils.ts
|
|
386
|
+
var DebugLogger = class {
|
|
387
|
+
constructor(options) {
|
|
388
|
+
this.options = {
|
|
389
|
+
prefix: "[Prepr]",
|
|
390
|
+
...options
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
// A local `enabled` wins; otherwise defer to the shared flag so scoped
|
|
394
|
+
// loggers created before initDebugLogger() still pick up the setting.
|
|
395
|
+
isEnabled() {
|
|
396
|
+
if (this.options.enabled !== void 0) {
|
|
397
|
+
return this.options.enabled;
|
|
398
|
+
}
|
|
399
|
+
return sharedState().debugEnabled ?? false;
|
|
400
|
+
}
|
|
401
|
+
log(message, ...args) {
|
|
402
|
+
if (!this.isEnabled()) return;
|
|
403
|
+
const prefix = this.options.prefix;
|
|
404
|
+
console.log(`${prefix} ${message}`, ...args);
|
|
405
|
+
}
|
|
406
|
+
warn(message, ...args) {
|
|
407
|
+
if (!this.isEnabled()) return;
|
|
408
|
+
const prefix = this.options.prefix;
|
|
409
|
+
console.warn(`${prefix} ${message}`, ...args);
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
function createScopedLogger(scopeName) {
|
|
413
|
+
return new DebugLogger({
|
|
414
|
+
prefix: `[Prepr][${scopeName}]`
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// src/core/live-preview/field-name.ts
|
|
419
|
+
var UPPERCASE_BOUNDARY = /(.)(?=[A-Z])/g;
|
|
420
|
+
function graphqlFieldName(apiId) {
|
|
421
|
+
return apiId.replace(UPPERCASE_BOUNDARY, "$1_").toLowerCase();
|
|
422
|
+
}
|
|
423
|
+
var TEXT_BODY_FIELDS = [
|
|
424
|
+
{ name: "body", plain: false },
|
|
425
|
+
{ name: "html", plain: false },
|
|
426
|
+
{ name: "text", plain: true }
|
|
427
|
+
];
|
|
428
|
+
var TAG_PATTERN = /<[^>]+>/g;
|
|
429
|
+
function plainTextFromHtml(html) {
|
|
430
|
+
return html.replace(TAG_PATTERN, "");
|
|
431
|
+
}
|
|
432
|
+
function isTextEntry(record) {
|
|
433
|
+
const typename = record.__typename;
|
|
434
|
+
if (typeof typename === "string") return typename === "Text";
|
|
435
|
+
return "html" in record || "text" in record && "format" in record;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// src/core/live-preview/index-snapshot.ts
|
|
439
|
+
var debug = createScopedLogger("live-preview");
|
|
440
|
+
function indexSnapshot(data) {
|
|
441
|
+
const index = /* @__PURE__ */ new Map();
|
|
442
|
+
const visited = /* @__PURE__ */ new WeakSet();
|
|
443
|
+
const path = [];
|
|
444
|
+
function walk2(node) {
|
|
445
|
+
if (node === null || typeof node !== "object") return;
|
|
446
|
+
if (visited.has(node)) return;
|
|
447
|
+
visited.add(node);
|
|
448
|
+
if (Array.isArray(node)) {
|
|
449
|
+
for (let i = 0; i < node.length; i++) {
|
|
450
|
+
path.push(i);
|
|
451
|
+
walk2(node[i]);
|
|
452
|
+
path.pop();
|
|
453
|
+
}
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
const record = node;
|
|
457
|
+
if (typeof record._id === "string") {
|
|
458
|
+
const paths = index.get(record._id);
|
|
459
|
+
if (paths) paths.push([...path]);
|
|
460
|
+
else index.set(record._id, [[...path]]);
|
|
461
|
+
} else if (typeof record.__typename === "string") {
|
|
462
|
+
debug.log("item without _id will not live-update", {
|
|
463
|
+
__typename: record.__typename
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
for (const key in record) {
|
|
467
|
+
if (!Object.prototype.hasOwnProperty.call(record, key)) continue;
|
|
468
|
+
path.push(key);
|
|
469
|
+
walk2(record[key]);
|
|
470
|
+
path.pop();
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
walk2(data);
|
|
474
|
+
return index;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// src/core/live-preview/patch.ts
|
|
478
|
+
var import_stega2 = require("@vercel/stega");
|
|
479
|
+
var FORBIDDEN_SEGMENTS = /* @__PURE__ */ new Set(["__proto__", "prototype", "constructor"]);
|
|
480
|
+
function mergeObject(current, incoming) {
|
|
481
|
+
let changed = false;
|
|
482
|
+
const next = { ...current };
|
|
483
|
+
for (const key of Object.keys(current)) {
|
|
484
|
+
if (!(key in incoming) || incoming[key] === void 0) continue;
|
|
485
|
+
const value = isObject(current[key]) && isObject(incoming[key]) ? mergeObject(current[key], incoming[key]) : incoming[key];
|
|
486
|
+
if (value !== current[key]) {
|
|
487
|
+
next[key] = value;
|
|
488
|
+
changed = true;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
return changed ? next : current;
|
|
492
|
+
}
|
|
493
|
+
function shapeLike(template, incoming) {
|
|
494
|
+
const out = {};
|
|
495
|
+
for (const key of Object.keys(template)) {
|
|
496
|
+
const value = incoming[key];
|
|
497
|
+
out[key] = value === void 0 ? null : isObject(template[key]) && isObject(value) ? shapeLike(template[key], value) : value;
|
|
498
|
+
}
|
|
499
|
+
return out;
|
|
500
|
+
}
|
|
501
|
+
function isAsset(value) {
|
|
502
|
+
return isObject(value) && (typeof value.url === "string" || value.__typename === "Asset");
|
|
503
|
+
}
|
|
504
|
+
function hasId(list, entry) {
|
|
505
|
+
return entry._id !== void 0 && list.some((c) => isObject(c) && c._id === entry._id);
|
|
506
|
+
}
|
|
507
|
+
function isAssetValue(value) {
|
|
508
|
+
return Array.isArray(value) ? value.length > 0 && value.every(isAsset) : isAsset(value);
|
|
509
|
+
}
|
|
510
|
+
function keepRemovedAssets(current, next) {
|
|
511
|
+
const listed = /* @__PURE__ */ new Set();
|
|
512
|
+
for (const entry of next) {
|
|
513
|
+
if (isObject(entry) && entry._id !== void 0) listed.add(entry._id);
|
|
514
|
+
}
|
|
515
|
+
let out = next;
|
|
516
|
+
current.forEach((entry, position) => {
|
|
517
|
+
if (!isObject(entry) || entry._id === void 0) return;
|
|
518
|
+
if (listed.has(entry._id)) return;
|
|
519
|
+
if (out === next) out = [...next];
|
|
520
|
+
out.splice(Math.min(position, out.length), 0, entry);
|
|
521
|
+
});
|
|
522
|
+
return out;
|
|
523
|
+
}
|
|
524
|
+
function mergeStructured(current, incoming) {
|
|
525
|
+
if (incoming === null || Array.isArray(incoming) && !incoming.length) {
|
|
526
|
+
if (isAssetValue(current)) return current;
|
|
527
|
+
if (Array.isArray(current)) return current.length ? [] : current;
|
|
528
|
+
return null;
|
|
529
|
+
}
|
|
530
|
+
const list = Array.isArray(incoming) ? incoming : [incoming];
|
|
531
|
+
if (current === null || current === void 0) return incoming;
|
|
532
|
+
if (Array.isArray(current)) {
|
|
533
|
+
const template = current.find(isObject);
|
|
534
|
+
const next = list.map((entry) => {
|
|
535
|
+
const match = current.find(
|
|
536
|
+
(c) => isObject(c) && c._id !== void 0 && c._id === entry._id
|
|
537
|
+
);
|
|
538
|
+
if (isObject(match)) return mergeObject(match, entry);
|
|
539
|
+
return isObject(template) ? shapeLike(template, entry) : entry;
|
|
540
|
+
});
|
|
541
|
+
if (!isAssetValue(current) || list.some((entry) => !hasId(current, entry))) {
|
|
542
|
+
return next;
|
|
543
|
+
}
|
|
544
|
+
const kept = keepRemovedAssets(current, next);
|
|
545
|
+
return kept.length === current.length && kept.every((entry, i) => entry === current[i]) ? current : kept;
|
|
546
|
+
}
|
|
547
|
+
if (isObject(current)) return mergeObject(current, list[0]);
|
|
548
|
+
return current;
|
|
549
|
+
}
|
|
550
|
+
function isPatchable(current) {
|
|
551
|
+
return current === null || typeof current === "string" || typeof current === "number" || typeof current === "boolean";
|
|
552
|
+
}
|
|
553
|
+
function cloneNode(node) {
|
|
554
|
+
return Array.isArray(node) ? [...node] : { ...node };
|
|
555
|
+
}
|
|
556
|
+
function writeAtPath(root, fullPath, value, markChanged = () => {
|
|
557
|
+
}) {
|
|
558
|
+
if (!fullPath.length) return root;
|
|
559
|
+
if (fullPath.some((s) => FORBIDDEN_SEGMENTS.has(String(s)))) return root;
|
|
560
|
+
const last = fullPath.length - 1;
|
|
561
|
+
let node = root;
|
|
562
|
+
for (let i = 0; i < last; i++) {
|
|
563
|
+
if (node === null || typeof node !== "object") return root;
|
|
564
|
+
node = node[fullPath[i]];
|
|
565
|
+
}
|
|
566
|
+
if (node === null || typeof node !== "object") return root;
|
|
567
|
+
const container = node;
|
|
568
|
+
const key = fullPath[last];
|
|
569
|
+
if (!(key in container)) return root;
|
|
570
|
+
let next;
|
|
571
|
+
if (value === null || typeof value === "object") {
|
|
572
|
+
next = mergeStructured(container[key], value);
|
|
573
|
+
} else if (!isPatchable(container[key])) {
|
|
574
|
+
return root;
|
|
575
|
+
} else {
|
|
576
|
+
next = keepEditLink(container[key], value);
|
|
577
|
+
}
|
|
578
|
+
if (container[key] === next) return root;
|
|
579
|
+
const newRoot = cloneNode(root);
|
|
580
|
+
let cursor = newRoot;
|
|
581
|
+
for (let i = 0; i < last; i++) {
|
|
582
|
+
const child = cloneNode(cursor[fullPath[i]]);
|
|
583
|
+
cursor[fullPath[i]] = child;
|
|
584
|
+
cursor = child;
|
|
585
|
+
}
|
|
586
|
+
cursor[key] = next;
|
|
587
|
+
markChanged();
|
|
588
|
+
return newRoot;
|
|
589
|
+
}
|
|
590
|
+
function listKeysOf(entry) {
|
|
591
|
+
if (!isObject(entry)) return [];
|
|
592
|
+
const keys = [];
|
|
593
|
+
if (typeof entry._id === "string") keys.push(entry._id);
|
|
594
|
+
const first = Array.isArray(entry.items) ? entry.items[0] : null;
|
|
595
|
+
if (isObject(first) && typeof first._id === "string") {
|
|
596
|
+
keys.push(first._id);
|
|
597
|
+
}
|
|
598
|
+
return keys;
|
|
599
|
+
}
|
|
600
|
+
function buildText(entry, template) {
|
|
601
|
+
const body = typeof entry.body === "string" ? entry.body : "";
|
|
602
|
+
const full = {
|
|
603
|
+
__typename: "Text",
|
|
604
|
+
_id: entry._id,
|
|
605
|
+
body,
|
|
606
|
+
html: body,
|
|
607
|
+
text: plainTextFromHtml(body),
|
|
608
|
+
format: entry.format ?? null
|
|
609
|
+
};
|
|
610
|
+
if (!template) return full;
|
|
611
|
+
const out = {};
|
|
612
|
+
for (const key of Object.keys(template)) {
|
|
613
|
+
out[key] = key in full ? full[key] : null;
|
|
614
|
+
}
|
|
615
|
+
return out;
|
|
616
|
+
}
|
|
617
|
+
function syncList(current, entries, build) {
|
|
618
|
+
if (current === null || current === void 0) {
|
|
619
|
+
const built = syncList([], entries, build);
|
|
620
|
+
return built.length ? built : current;
|
|
621
|
+
}
|
|
622
|
+
if (!Array.isArray(current)) return current;
|
|
623
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
624
|
+
const templates = /* @__PURE__ */ new Map();
|
|
625
|
+
for (const c of current) {
|
|
626
|
+
for (const key of listKeysOf(c)) byKey.set(key, c);
|
|
627
|
+
if (isObject(c) && typeof c.__typename === "string") {
|
|
628
|
+
if (!templates.has(c.__typename)) templates.set(c.__typename, c);
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
if (!entries.length) return current.length ? [] : current;
|
|
632
|
+
if (current.length && !byKey.size) return current;
|
|
633
|
+
if (current.length && !entries.some((e) => byKey.has(e._id))) return current;
|
|
634
|
+
const next = [];
|
|
635
|
+
for (const entry of entries) {
|
|
636
|
+
const existing = byKey.get(entry._id);
|
|
637
|
+
const built = existing === void 0 && entry.fields ? build?.(entry) : void 0;
|
|
638
|
+
if (existing !== void 0) {
|
|
639
|
+
next.push(existing);
|
|
640
|
+
} else if (built) {
|
|
641
|
+
next.push(built);
|
|
642
|
+
} else if (entry.__typename === "Text") {
|
|
643
|
+
next.push(buildText(entry, templates.get("Text")));
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
const same = next.length === current.length && next.every((n, i) => n === current[i]);
|
|
647
|
+
return same ? current : next;
|
|
648
|
+
}
|
|
649
|
+
function syncListAtPath(root, fullPath, entries, build) {
|
|
650
|
+
if (!fullPath.length) return root;
|
|
651
|
+
if (fullPath.some((s) => FORBIDDEN_SEGMENTS.has(String(s)))) return root;
|
|
652
|
+
let node = root;
|
|
653
|
+
for (const key of fullPath) {
|
|
654
|
+
node = node !== null && typeof node === "object" ? node[key] : void 0;
|
|
655
|
+
}
|
|
656
|
+
const next = syncList(node, entries, build);
|
|
657
|
+
if (next === node) return root;
|
|
658
|
+
const newRoot = cloneNode(root);
|
|
659
|
+
let cursor = newRoot;
|
|
660
|
+
for (let i = 0; i < fullPath.length - 1; i++) {
|
|
661
|
+
const child = cloneNode(cursor[fullPath[i]]);
|
|
662
|
+
cursor[fullPath[i]] = child;
|
|
663
|
+
cursor = child;
|
|
664
|
+
}
|
|
665
|
+
cursor[fullPath[fullPath.length - 1]] = next;
|
|
666
|
+
return newRoot;
|
|
667
|
+
}
|
|
668
|
+
function keepEditLink(current, value) {
|
|
669
|
+
if (typeof current !== "string" || typeof value !== "string") return value;
|
|
670
|
+
const { encoded } = (0, import_stega2.vercelStegaSplit)(current);
|
|
671
|
+
if (!encoded || (0, import_stega2.vercelStegaSplit)(value).encoded) return value;
|
|
672
|
+
return value + encoded;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// src/core/live-preview/query-document.ts
|
|
676
|
+
var NAME_START = /[_A-Za-z]/;
|
|
677
|
+
var NAME_CONTINUE = /[_0-9A-Za-z]/;
|
|
678
|
+
var PUNCT = /* @__PURE__ */ new Set([
|
|
679
|
+
"!",
|
|
680
|
+
"$",
|
|
681
|
+
"&",
|
|
682
|
+
"(",
|
|
683
|
+
")",
|
|
684
|
+
":",
|
|
685
|
+
"=",
|
|
686
|
+
"@",
|
|
687
|
+
"[",
|
|
688
|
+
"]",
|
|
689
|
+
"{",
|
|
690
|
+
"|",
|
|
691
|
+
"}"
|
|
692
|
+
]);
|
|
693
|
+
function tokenize(source) {
|
|
694
|
+
const tokens = [];
|
|
695
|
+
let i = 0;
|
|
696
|
+
const n = source.length;
|
|
697
|
+
while (i < n) {
|
|
698
|
+
const ch = source[i];
|
|
699
|
+
if (ch === " " || ch === " " || ch === "\n" || ch === "\r" || ch === "," || ch === "\uFEFF") {
|
|
700
|
+
i++;
|
|
701
|
+
} else if (ch === "#") {
|
|
702
|
+
while (i < n && source[i] !== "\n" && source[i] !== "\r") i++;
|
|
703
|
+
} else if (ch === "." && source.startsWith("...", i)) {
|
|
704
|
+
tokens.push({ type: "punct", value: "..." });
|
|
705
|
+
i += 3;
|
|
706
|
+
} else if (PUNCT.has(ch)) {
|
|
707
|
+
tokens.push({ type: "punct", value: ch });
|
|
708
|
+
i++;
|
|
709
|
+
} else if (NAME_START.test(ch)) {
|
|
710
|
+
const start = i;
|
|
711
|
+
while (i < n && NAME_CONTINUE.test(source[i])) i++;
|
|
712
|
+
tokens.push({ type: "name", value: source.slice(start, i) });
|
|
713
|
+
} else if (ch === '"') {
|
|
714
|
+
if (source.startsWith('"""', i)) {
|
|
715
|
+
i += 3;
|
|
716
|
+
while (i < n && !source.startsWith('"""', i)) {
|
|
717
|
+
i += source.startsWith('\\"""', i) ? 4 : 1;
|
|
718
|
+
}
|
|
719
|
+
if (i >= n) throw new SyntaxError("Unterminated block string");
|
|
720
|
+
i += 3;
|
|
721
|
+
} else {
|
|
722
|
+
i++;
|
|
723
|
+
while (i < n && source[i] !== '"') {
|
|
724
|
+
if (source[i] === "\n" || source[i] === "\r") {
|
|
725
|
+
throw new SyntaxError("Unterminated string");
|
|
726
|
+
}
|
|
727
|
+
i += source[i] === "\\" ? 2 : 1;
|
|
728
|
+
}
|
|
729
|
+
if (i >= n) throw new SyntaxError("Unterminated string");
|
|
730
|
+
i++;
|
|
731
|
+
}
|
|
732
|
+
tokens.push({ type: "value" });
|
|
733
|
+
} else if (ch === "-" || ch >= "0" && ch <= "9") {
|
|
734
|
+
i++;
|
|
735
|
+
while (i < n && /[0-9.eE+-]/.test(source[i])) i++;
|
|
736
|
+
tokens.push({ type: "value" });
|
|
737
|
+
} else {
|
|
738
|
+
throw new SyntaxError(`Unexpected character "${ch}"`);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
return tokens;
|
|
742
|
+
}
|
|
743
|
+
function parseSource(source, operationName) {
|
|
744
|
+
const tokens = tokenize(source);
|
|
745
|
+
let pos = 0;
|
|
746
|
+
const peek = () => tokens[pos];
|
|
747
|
+
const isPunct = (value) => {
|
|
748
|
+
const t = tokens[pos];
|
|
749
|
+
return t?.type === "punct" && t.value === value;
|
|
750
|
+
};
|
|
751
|
+
const expectPunct = (value) => {
|
|
752
|
+
if (!isPunct(value)) throw new SyntaxError(`Expected "${value}"`);
|
|
753
|
+
pos++;
|
|
754
|
+
};
|
|
755
|
+
const expectName = () => {
|
|
756
|
+
const t = tokens[pos];
|
|
757
|
+
if (t?.type !== "name") throw new SyntaxError("Expected a name");
|
|
758
|
+
pos++;
|
|
759
|
+
return t.value;
|
|
760
|
+
};
|
|
761
|
+
const skipParens = () => {
|
|
762
|
+
expectPunct("(");
|
|
763
|
+
let depth = 1;
|
|
764
|
+
while (depth > 0) {
|
|
765
|
+
const t = tokens[pos++];
|
|
766
|
+
if (!t) throw new SyntaxError("Unbalanced parentheses");
|
|
767
|
+
if (t.type === "punct" && t.value === "(") depth++;
|
|
768
|
+
else if (t.type === "punct" && t.value === ")") depth--;
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
const skipDirectives = () => {
|
|
772
|
+
while (isPunct("@")) {
|
|
773
|
+
pos++;
|
|
774
|
+
expectName();
|
|
775
|
+
if (isPunct("(")) skipParens();
|
|
776
|
+
}
|
|
777
|
+
};
|
|
778
|
+
const parseSelectionSet = () => {
|
|
779
|
+
expectPunct("{");
|
|
780
|
+
const selections = [];
|
|
781
|
+
while (!isPunct("}")) {
|
|
782
|
+
if (!peek()) throw new SyntaxError("Unterminated selection set");
|
|
783
|
+
selections.push(parseSelection());
|
|
784
|
+
}
|
|
785
|
+
pos++;
|
|
786
|
+
return selections;
|
|
787
|
+
};
|
|
788
|
+
const parseSelection = () => {
|
|
789
|
+
if (isPunct("...")) {
|
|
790
|
+
pos++;
|
|
791
|
+
const t = peek();
|
|
792
|
+
if (t?.type === "name" && t.value !== "on") {
|
|
793
|
+
pos++;
|
|
794
|
+
skipDirectives();
|
|
795
|
+
return { kind: "spread", name: t.value };
|
|
796
|
+
}
|
|
797
|
+
let on = null;
|
|
798
|
+
if (t?.type === "name") {
|
|
799
|
+
pos++;
|
|
800
|
+
on = expectName();
|
|
801
|
+
}
|
|
802
|
+
skipDirectives();
|
|
803
|
+
return { kind: "inline", on, selections: parseSelectionSet() };
|
|
804
|
+
}
|
|
805
|
+
let name = expectName();
|
|
806
|
+
const key = name;
|
|
807
|
+
if (isPunct(":")) {
|
|
808
|
+
pos++;
|
|
809
|
+
name = expectName();
|
|
810
|
+
}
|
|
811
|
+
let hasArgs = false;
|
|
812
|
+
if (isPunct("(")) {
|
|
813
|
+
hasArgs = true;
|
|
814
|
+
skipParens();
|
|
815
|
+
}
|
|
816
|
+
skipDirectives();
|
|
817
|
+
const selections = isPunct("{") ? parseSelectionSet() : null;
|
|
818
|
+
return { kind: "field", name, key, hasArgs, selections };
|
|
819
|
+
};
|
|
820
|
+
const fragments = /* @__PURE__ */ new Map();
|
|
821
|
+
const operations = [];
|
|
822
|
+
while (pos < tokens.length) {
|
|
823
|
+
if (isPunct("{")) {
|
|
824
|
+
operations.push({ name: null, selections: parseSelectionSet() });
|
|
825
|
+
continue;
|
|
826
|
+
}
|
|
827
|
+
const keyword = expectName();
|
|
828
|
+
if (keyword === "fragment") {
|
|
829
|
+
const name = expectName();
|
|
830
|
+
if (expectName() !== "on") throw new SyntaxError('Expected "on"');
|
|
831
|
+
const on = expectName();
|
|
832
|
+
skipDirectives();
|
|
833
|
+
fragments.set(name, { on, selections: parseSelectionSet() });
|
|
834
|
+
} else if (keyword === "query" || keyword === "mutation" || keyword === "subscription") {
|
|
835
|
+
const t = peek();
|
|
836
|
+
let name = null;
|
|
837
|
+
if (t?.type === "name") name = expectName();
|
|
838
|
+
if (isPunct("(")) skipParens();
|
|
839
|
+
skipDirectives();
|
|
840
|
+
operations.push({ name, selections: parseSelectionSet() });
|
|
841
|
+
} else {
|
|
842
|
+
throw new SyntaxError(`Unexpected "${keyword}"`);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
return { operation: pickOperation(operations, operationName), fragments };
|
|
846
|
+
}
|
|
847
|
+
function pickOperation(operations, operationName) {
|
|
848
|
+
const op = operationName ? operations.find((o) => o.name === operationName) : operations[0];
|
|
849
|
+
if (!op) throw new SyntaxError("The document has no operation");
|
|
850
|
+
return op.selections;
|
|
851
|
+
}
|
|
852
|
+
function fromAstSelections(set) {
|
|
853
|
+
return set.selections.map((s) => {
|
|
854
|
+
if (s.kind === "FragmentSpread")
|
|
855
|
+
return { kind: "spread", name: s.name.value };
|
|
856
|
+
if (s.kind === "InlineFragment") {
|
|
857
|
+
return {
|
|
858
|
+
kind: "inline",
|
|
859
|
+
on: s.typeCondition?.name.value ?? null,
|
|
860
|
+
selections: fromAstSelections(s.selectionSet)
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
const name = s.name.value;
|
|
864
|
+
return {
|
|
865
|
+
kind: "field",
|
|
866
|
+
name,
|
|
867
|
+
key: s.alias?.value ?? name,
|
|
868
|
+
hasArgs: Boolean(s.arguments?.length),
|
|
869
|
+
selections: s.selectionSet ? fromAstSelections(s.selectionSet) : null
|
|
870
|
+
};
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
function fromAst(doc, operationName) {
|
|
874
|
+
const fragments = /* @__PURE__ */ new Map();
|
|
875
|
+
const operations = [];
|
|
876
|
+
for (const def of doc.definitions ?? []) {
|
|
877
|
+
if (def.kind === "FragmentDefinition" && def.name && def.typeCondition && def.selectionSet) {
|
|
878
|
+
fragments.set(def.name.value, {
|
|
879
|
+
on: def.typeCondition.name.value,
|
|
880
|
+
selections: fromAstSelections(def.selectionSet)
|
|
881
|
+
});
|
|
882
|
+
} else if (def.kind === "OperationDefinition" && def.selectionSet) {
|
|
883
|
+
operations.push({
|
|
884
|
+
name: def.name?.value ?? null,
|
|
885
|
+
selections: fromAstSelections(def.selectionSet)
|
|
886
|
+
});
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
return { operation: pickOperation(operations, operationName), fragments };
|
|
890
|
+
}
|
|
891
|
+
var astCache = /* @__PURE__ */ new WeakMap();
|
|
892
|
+
var sourceCache = /* @__PURE__ */ new Map();
|
|
893
|
+
var SOURCE_CACHE_LIMIT = 200;
|
|
894
|
+
function readQueryDocument(query, operationName) {
|
|
895
|
+
const cacheKey = operationName ?? "";
|
|
896
|
+
if (typeof query === "object" && query !== null && query.kind === "Document") {
|
|
897
|
+
let byName = astCache.get(query);
|
|
898
|
+
if (!byName) astCache.set(query, byName = /* @__PURE__ */ new Map());
|
|
899
|
+
let parsed2 = byName.get(cacheKey);
|
|
900
|
+
if (!parsed2) {
|
|
901
|
+
parsed2 = fromAst(
|
|
902
|
+
query,
|
|
903
|
+
operationName
|
|
904
|
+
);
|
|
905
|
+
byName.set(cacheKey, parsed2);
|
|
906
|
+
}
|
|
907
|
+
return parsed2;
|
|
908
|
+
}
|
|
909
|
+
const source = String(query);
|
|
910
|
+
const sourceKey = `${cacheKey}
|
|
911
|
+
${source}`;
|
|
912
|
+
let parsed = sourceCache.get(sourceKey);
|
|
913
|
+
if (!parsed) {
|
|
914
|
+
parsed = parseSource(source, operationName);
|
|
915
|
+
if (sourceCache.size >= SOURCE_CACHE_LIMIT) {
|
|
916
|
+
sourceCache.delete(sourceCache.keys().next().value);
|
|
917
|
+
}
|
|
918
|
+
sourceCache.set(sourceKey, parsed);
|
|
919
|
+
}
|
|
920
|
+
return parsed;
|
|
921
|
+
}
|
|
922
|
+
var fieldsCache = /* @__PURE__ */ new WeakMap();
|
|
923
|
+
function collectFields(selections, typename, doc, options = {}) {
|
|
924
|
+
const cacheKey = `${options.strict ? "s" : "l"}:${typename ?? ""}`;
|
|
925
|
+
let cached = fieldsCache.get(selections);
|
|
926
|
+
if (!cached) {
|
|
927
|
+
cached = /* @__PURE__ */ new Map();
|
|
928
|
+
fieldsCache.set(selections, cached);
|
|
929
|
+
}
|
|
930
|
+
const hit = cached.get(cacheKey);
|
|
931
|
+
if (hit) return hit;
|
|
932
|
+
const fields = resolveFields(selections, typename, doc, options);
|
|
933
|
+
cached.set(cacheKey, fields);
|
|
934
|
+
return fields;
|
|
935
|
+
}
|
|
936
|
+
function resolveFields(selections, typename, doc, options) {
|
|
937
|
+
const exact = /* @__PURE__ */ new Map();
|
|
938
|
+
const loose = /* @__PURE__ */ new Map();
|
|
939
|
+
const merged = /* @__PURE__ */ new Map();
|
|
940
|
+
const walkedMatched = /* @__PURE__ */ new Set();
|
|
941
|
+
const walkedLoose = /* @__PURE__ */ new Set();
|
|
942
|
+
const add = (target, s) => {
|
|
943
|
+
let field = target.get(s.key);
|
|
944
|
+
if (!field) {
|
|
945
|
+
field = { name: s.name, key: s.key, hasArgs: s.hasArgs, selections: [] };
|
|
946
|
+
target.set(s.key, field);
|
|
947
|
+
merged.set(field, /* @__PURE__ */ new Set());
|
|
948
|
+
}
|
|
949
|
+
const done = merged.get(field);
|
|
950
|
+
if (s.selections && !done.has(s.selections)) {
|
|
951
|
+
done.add(s.selections);
|
|
952
|
+
field.selections = [...field.selections, ...s.selections];
|
|
953
|
+
}
|
|
954
|
+
};
|
|
955
|
+
const applies = (on, list) => conditionMatches(on, typename) || typename !== void 0 && typeConditions(list, doc).includes(typename);
|
|
956
|
+
const walk2 = (list, matched) => {
|
|
957
|
+
for (const s of list) {
|
|
958
|
+
if (s.kind === "field") {
|
|
959
|
+
add(matched ? exact : loose, s);
|
|
960
|
+
} else if (s.kind === "inline") {
|
|
961
|
+
walk2(s.selections, matched && applies(s.on, s.selections));
|
|
962
|
+
} else {
|
|
963
|
+
const fragment = doc.fragments.get(s.name);
|
|
964
|
+
if (!fragment) continue;
|
|
965
|
+
const asMatched = matched && applies(fragment.on, fragment.selections);
|
|
966
|
+
if (walkedMatched.has(fragment)) continue;
|
|
967
|
+
if (!asMatched && walkedLoose.has(fragment)) continue;
|
|
968
|
+
(asMatched ? walkedMatched : walkedLoose).add(fragment);
|
|
969
|
+
walk2(fragment.selections, asMatched);
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
};
|
|
973
|
+
walk2(selections, true);
|
|
974
|
+
if (!options.strict) {
|
|
975
|
+
for (const [key, field] of loose) {
|
|
976
|
+
if (!exact.has(key)) exact.set(key, field);
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
return exact;
|
|
980
|
+
}
|
|
981
|
+
function conditionMatches(on, typename) {
|
|
982
|
+
return on === null || typename === void 0 || on === typename;
|
|
983
|
+
}
|
|
984
|
+
var conditionsCache = /* @__PURE__ */ new WeakMap();
|
|
985
|
+
function typeConditions(selections, doc) {
|
|
986
|
+
const hit = conditionsCache.get(selections);
|
|
987
|
+
if (hit) return hit;
|
|
988
|
+
const names = /* @__PURE__ */ new Set();
|
|
989
|
+
const seen = /* @__PURE__ */ new Set();
|
|
990
|
+
const walk2 = (list) => {
|
|
991
|
+
for (const s of list) {
|
|
992
|
+
if (s.kind === "inline") {
|
|
993
|
+
if (s.on) names.add(s.on);
|
|
994
|
+
walk2(s.selections);
|
|
995
|
+
} else if (s.kind === "spread") {
|
|
996
|
+
const fragment = doc.fragments.get(s.name);
|
|
997
|
+
if (!fragment || seen.has(fragment)) continue;
|
|
998
|
+
seen.add(fragment);
|
|
999
|
+
names.add(fragment.on);
|
|
1000
|
+
walk2(fragment.selections);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
};
|
|
1004
|
+
walk2(selections);
|
|
1005
|
+
const conditions = [...names];
|
|
1006
|
+
conditionsCache.set(selections, conditions);
|
|
1007
|
+
return conditions;
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
// src/core/live-preview/normalize-query.ts
|
|
1011
|
+
var debug2 = createScopedLogger("live-preview:normalize");
|
|
1012
|
+
var INDEX_SEGMENT = /^\d+$/;
|
|
1013
|
+
var FORBIDDEN_SEGMENTS2 = /* @__PURE__ */ new Set(["__proto__", "prototype", "constructor"]);
|
|
1014
|
+
var ENVELOPE_KEYS = /* @__PURE__ */ new Set(["data", "errors", "extensions"]);
|
|
1015
|
+
function applyPatchLog(query, result, log, operationName) {
|
|
1016
|
+
if (!log.length || result === null || typeof result !== "object") {
|
|
1017
|
+
return result;
|
|
1018
|
+
}
|
|
1019
|
+
if (isEnvelope(result)) {
|
|
1020
|
+
const data = applyPatchLog(query, result.data, log, operationName);
|
|
1021
|
+
return data === result.data ? result : { ...result, data };
|
|
1022
|
+
}
|
|
1023
|
+
let doc = null;
|
|
1024
|
+
try {
|
|
1025
|
+
doc = readQueryDocument(query, operationName);
|
|
1026
|
+
} catch (error) {
|
|
1027
|
+
debug2.log("could not read the query; applying fields by literal path", {
|
|
1028
|
+
error: String(error)
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
let root = result;
|
|
1032
|
+
let index = null;
|
|
1033
|
+
for (const patch of log) {
|
|
1034
|
+
try {
|
|
1035
|
+
index ?? (index = indexSnapshot(root));
|
|
1036
|
+
const next = applyPatch(root, index, patch, doc, result);
|
|
1037
|
+
if (next !== root) {
|
|
1038
|
+
root = next;
|
|
1039
|
+
if (patch.type === "list" || !isScalarValue(patch.value)) {
|
|
1040
|
+
index = null;
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
} catch (error) {
|
|
1044
|
+
debug2.log("could not apply a change", {
|
|
1045
|
+
itemId: patch.itemId,
|
|
1046
|
+
field: patch.field,
|
|
1047
|
+
error: String(error)
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
return root;
|
|
1052
|
+
}
|
|
1053
|
+
function isScalarValue(value) {
|
|
1054
|
+
return value === null || typeof value !== "object";
|
|
1055
|
+
}
|
|
1056
|
+
function applyPatch(root, index, patch, doc, result) {
|
|
1057
|
+
const ownerPaths = index.get(patch.itemId);
|
|
1058
|
+
if (!ownerPaths?.length) return root;
|
|
1059
|
+
const segments = patch.field.split(".");
|
|
1060
|
+
if (segments.some((s) => !s || FORBIDDEN_SEGMENTS2.has(s))) return root;
|
|
1061
|
+
const targets = [];
|
|
1062
|
+
const plain = /* @__PURE__ */ new Set();
|
|
1063
|
+
for (const ownerPath of ownerPaths) {
|
|
1064
|
+
if (doc) {
|
|
1065
|
+
const resolved = resolveTargets(root, doc, ownerPath, segments);
|
|
1066
|
+
root = resolved.root;
|
|
1067
|
+
targets.push(...resolved.targets);
|
|
1068
|
+
resolved.plain.forEach((target) => plain.add(target));
|
|
1069
|
+
} else {
|
|
1070
|
+
targets.push([...ownerPath, ...segments.map(toPathSegment)]);
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
if (!targets.length) {
|
|
1074
|
+
debug2.log("no field in the query matches", {
|
|
1075
|
+
itemId: patch.itemId,
|
|
1076
|
+
field: patch.field
|
|
1077
|
+
});
|
|
1078
|
+
return root;
|
|
1079
|
+
}
|
|
1080
|
+
if (patch.type === "field") {
|
|
1081
|
+
for (const target of targets) {
|
|
1082
|
+
const pushed = plain.has(target) && typeof patch.value === "string" ? plainTextFromHtml(patch.value) : patch.value;
|
|
1083
|
+
let value = keepEditLink(readAtPath(root, target), pushed);
|
|
1084
|
+
if (patch.edit && typeof value === "string" && !(0, import_stega3.vercelStegaSplit)(value).encoded) {
|
|
1085
|
+
value = editLinksFor(result, patch.edit.item)?.encode(
|
|
1086
|
+
value,
|
|
1087
|
+
patch.edit.field
|
|
1088
|
+
) ?? value;
|
|
1089
|
+
}
|
|
1090
|
+
root = writeAtPath(root, target, value);
|
|
1091
|
+
}
|
|
1092
|
+
} else {
|
|
1093
|
+
for (const target of targets) {
|
|
1094
|
+
const selections = doc ? selectionsAt(root, doc, target) : null;
|
|
1095
|
+
const build = doc && selections ? (entry) => buildEntry(doc, selections, entry, {
|
|
1096
|
+
links: editLinks(result, entry),
|
|
1097
|
+
lists: listKeysIn(result)
|
|
1098
|
+
}) : void 0;
|
|
1099
|
+
root = syncListAtPath(root, target, patch.entries, build);
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
return root;
|
|
1103
|
+
}
|
|
1104
|
+
function readAtPath(root, path) {
|
|
1105
|
+
let node = root;
|
|
1106
|
+
for (const step of path) {
|
|
1107
|
+
if (node === null || typeof node !== "object") return void 0;
|
|
1108
|
+
node = node[step];
|
|
1109
|
+
}
|
|
1110
|
+
return node;
|
|
1111
|
+
}
|
|
1112
|
+
function isEnvelope(value) {
|
|
1113
|
+
if (Array.isArray(value) || !("data" in value)) return false;
|
|
1114
|
+
return Object.keys(value).every((key) => ENVELOPE_KEYS.has(key));
|
|
1115
|
+
}
|
|
1116
|
+
function toPathSegment(segment) {
|
|
1117
|
+
return INDEX_SEGMENT.test(segment) ? Number(segment) : segment;
|
|
1118
|
+
}
|
|
1119
|
+
function typenameOf(node) {
|
|
1120
|
+
const typename = node.__typename;
|
|
1121
|
+
return typeof typename === "string" ? typename : void 0;
|
|
1122
|
+
}
|
|
1123
|
+
function selectionsAt(root, doc, path) {
|
|
1124
|
+
let node = root;
|
|
1125
|
+
let selections = doc.operation;
|
|
1126
|
+
for (const step of path) {
|
|
1127
|
+
if (Array.isArray(node)) {
|
|
1128
|
+
node = node[step];
|
|
1129
|
+
continue;
|
|
1130
|
+
}
|
|
1131
|
+
if (node === null || typeof node !== "object") return null;
|
|
1132
|
+
const field = collectFields(selections, typenameOf(node), doc).get(
|
|
1133
|
+
String(step)
|
|
1134
|
+
);
|
|
1135
|
+
if (!field) return null;
|
|
1136
|
+
selections = field.selections;
|
|
1137
|
+
node = node[String(step)];
|
|
1138
|
+
}
|
|
1139
|
+
return selections;
|
|
1140
|
+
}
|
|
1141
|
+
function resolveTargets(root, doc, ownerPath, segments) {
|
|
1142
|
+
let owner = root;
|
|
1143
|
+
for (const step of ownerPath) {
|
|
1144
|
+
owner = owner[step];
|
|
1145
|
+
}
|
|
1146
|
+
const selections = selectionsAt(root, doc, ownerPath);
|
|
1147
|
+
if (!selections) return { root, targets: [], plain: /* @__PURE__ */ new Set() };
|
|
1148
|
+
let current = root;
|
|
1149
|
+
const out = [];
|
|
1150
|
+
const plain = /* @__PURE__ */ new Set();
|
|
1151
|
+
const walk2 = (node, sel, i, path) => {
|
|
1152
|
+
if (i === segments.length) {
|
|
1153
|
+
out.push(path);
|
|
1154
|
+
return;
|
|
1155
|
+
}
|
|
1156
|
+
const segment = segments[i];
|
|
1157
|
+
if (Array.isArray(node)) {
|
|
1158
|
+
if (INDEX_SEGMENT.test(segment)) {
|
|
1159
|
+
const position = Number(segment);
|
|
1160
|
+
if (position < node.length) {
|
|
1161
|
+
walk2(node[position], sel, i + 1, [...path, position]);
|
|
1162
|
+
}
|
|
1163
|
+
} else if (segment === "items") {
|
|
1164
|
+
walk2(node, sel, i + 1, path);
|
|
1165
|
+
}
|
|
1166
|
+
return;
|
|
1167
|
+
}
|
|
1168
|
+
if (node === null || typeof node !== "object") return;
|
|
1169
|
+
const record = node;
|
|
1170
|
+
const wanted = graphqlFieldName(segment);
|
|
1171
|
+
const textBody = i === segments.length - 1 && segment === "body" && isTextEntry(record) ? TEXT_BODY_FIELDS : null;
|
|
1172
|
+
let matched = false;
|
|
1173
|
+
for (const field of collectFields(sel, typenameOf(node), doc).values()) {
|
|
1174
|
+
const alias = textBody?.find((f) => f.name === field.name);
|
|
1175
|
+
if (field.name !== segment && field.name !== wanted && !alias) continue;
|
|
1176
|
+
if (!Object.prototype.hasOwnProperty.call(record, field.key)) continue;
|
|
1177
|
+
matched = true;
|
|
1178
|
+
const fieldPath = [...path, field.key];
|
|
1179
|
+
if (alias?.plain) plain.add(fieldPath);
|
|
1180
|
+
let value = record[field.key];
|
|
1181
|
+
const next = segments[i + 1];
|
|
1182
|
+
if (value === null && field.selections.length && next !== void 0 && next !== "items" && !INDEX_SEGMENT.test(next)) {
|
|
1183
|
+
const typename = resolveTypename(
|
|
1184
|
+
doc,
|
|
1185
|
+
field.selections,
|
|
1186
|
+
void 0,
|
|
1187
|
+
void 0
|
|
1188
|
+
);
|
|
1189
|
+
if (typename !== void 0) {
|
|
1190
|
+
value = fillObject(doc, field.selections, typename, {}, void 0, {
|
|
1191
|
+
links: null,
|
|
1192
|
+
lists: listKeysIn(root)
|
|
1193
|
+
});
|
|
1194
|
+
current = writeAtPath(current, fieldPath, value);
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
walk2(value, field.selections, i + 1, fieldPath);
|
|
1198
|
+
}
|
|
1199
|
+
if (!matched && segment === "items") walk2(node, sel, i + 1, path);
|
|
1200
|
+
};
|
|
1201
|
+
walk2(owner, selections, 0, ownerPath);
|
|
1202
|
+
return { root: current, targets: out, plain };
|
|
1203
|
+
}
|
|
1204
|
+
var NON_ALPHANUMERIC = /[^a-z0-9]/gi;
|
|
1205
|
+
var typeKey = (name) => name.replace(NON_ALPHANUMERIC, "").toLowerCase();
|
|
1206
|
+
function resolveTypename(doc, selections, typename, typeName) {
|
|
1207
|
+
const known = typeConditions(selections, doc);
|
|
1208
|
+
const explicit = typeof typename === "string" ? typename : void 0;
|
|
1209
|
+
if (!known.length) return explicit ?? null;
|
|
1210
|
+
if (explicit && known.includes(explicit)) return explicit;
|
|
1211
|
+
for (const candidate of [explicit, typeName]) {
|
|
1212
|
+
if (typeof candidate !== "string" || !candidate) continue;
|
|
1213
|
+
const match = known.find((name) => typeKey(name) === typeKey(candidate));
|
|
1214
|
+
if (match) return match;
|
|
1215
|
+
}
|
|
1216
|
+
const named = explicit !== void 0 || typeof typeName === "string";
|
|
1217
|
+
return known.length === 1 && !named ? known[0] : void 0;
|
|
1218
|
+
}
|
|
1219
|
+
function buildEntry(doc, selections, entry, ctx) {
|
|
1220
|
+
const typename = resolveTypename(
|
|
1221
|
+
doc,
|
|
1222
|
+
selections,
|
|
1223
|
+
entry.__typename,
|
|
1224
|
+
entry.typeName
|
|
1225
|
+
);
|
|
1226
|
+
if (typename === void 0) return void 0;
|
|
1227
|
+
return fillObject(
|
|
1228
|
+
doc,
|
|
1229
|
+
selections,
|
|
1230
|
+
typename,
|
|
1231
|
+
entry.fields ?? {},
|
|
1232
|
+
entry._id,
|
|
1233
|
+
ctx
|
|
1234
|
+
);
|
|
1235
|
+
}
|
|
1236
|
+
function fillObject(doc, selections, typename, values, id, ctx) {
|
|
1237
|
+
const out = {};
|
|
1238
|
+
const { links } = ctx;
|
|
1239
|
+
const lists = ctx.lists(typename);
|
|
1240
|
+
const edit = isPlainRecord(values.__edit) ? values.__edit : null;
|
|
1241
|
+
const apiIdOf = /* @__PURE__ */ new Map();
|
|
1242
|
+
for (const key of Object.keys(values)) {
|
|
1243
|
+
apiIdOf.set(key, key);
|
|
1244
|
+
const name = graphqlFieldName(key);
|
|
1245
|
+
if (!apiIdOf.has(name)) apiIdOf.set(name, key);
|
|
1246
|
+
}
|
|
1247
|
+
if (edit && isPlainRecord(edit.apiIds)) {
|
|
1248
|
+
for (const [name, apiId] of Object.entries(edit.apiIds)) {
|
|
1249
|
+
if (typeof apiId === "string") apiIdOf.set(name, apiId);
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
const valueOf = (name) => {
|
|
1253
|
+
if (Object.prototype.hasOwnProperty.call(values, name)) return values[name];
|
|
1254
|
+
const key = apiIdOf.get(name);
|
|
1255
|
+
return key !== void 0 && key in values ? values[key] : void 0;
|
|
1256
|
+
};
|
|
1257
|
+
const editField = (name) => {
|
|
1258
|
+
if (!edit) return null;
|
|
1259
|
+
if (typeof edit.field === "string")
|
|
1260
|
+
return name === "body" ? edit.field : null;
|
|
1261
|
+
if (typeof edit.path !== "string") return null;
|
|
1262
|
+
const apiId = apiIdOf.get(name) ?? name;
|
|
1263
|
+
const text = Array.isArray(edit.text) ? edit.text : [];
|
|
1264
|
+
if (!text.includes(apiId) && !text.includes(name)) return null;
|
|
1265
|
+
return `${edit.path}.items.${apiId}`;
|
|
1266
|
+
};
|
|
1267
|
+
const fields = collectFields(selections, typename ?? void 0, doc, {
|
|
1268
|
+
strict: true
|
|
1269
|
+
});
|
|
1270
|
+
for (const field of fields.values()) {
|
|
1271
|
+
if (field.name === "__typename") {
|
|
1272
|
+
out[field.key] = typename;
|
|
1273
|
+
} else if (field.name === "_id") {
|
|
1274
|
+
out[field.key] = typeof id === "string" ? id : values._id ?? null;
|
|
1275
|
+
} else if (field.selections.length) {
|
|
1276
|
+
const value = valueOf(field.name);
|
|
1277
|
+
out[field.key] = value == null && lists.has(field.key) ? [] : fillNested(doc, field.selections, value, ctx);
|
|
1278
|
+
} else {
|
|
1279
|
+
const value = valueOf(field.name);
|
|
1280
|
+
if (Array.isArray(value) && value.every(isScalar)) {
|
|
1281
|
+
out[field.key] = value;
|
|
1282
|
+
continue;
|
|
1283
|
+
}
|
|
1284
|
+
if (value == null && lists.has(field.key)) {
|
|
1285
|
+
out[field.key] = [];
|
|
1286
|
+
continue;
|
|
1287
|
+
}
|
|
1288
|
+
const scalar = value === void 0 || value !== null && typeof value === "object" ? null : value;
|
|
1289
|
+
const target = typeof scalar === "string" ? editField(field.name) : null;
|
|
1290
|
+
out[field.key] = target && links ? links.encode(scalar, target) : scalar;
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
return out;
|
|
1294
|
+
}
|
|
1295
|
+
function fillNested(doc, selections, value, ctx) {
|
|
1296
|
+
const one = (item) => {
|
|
1297
|
+
if (!isPlainRecord(item)) return null;
|
|
1298
|
+
const typename = resolveTypename(
|
|
1299
|
+
doc,
|
|
1300
|
+
selections,
|
|
1301
|
+
item.__typename,
|
|
1302
|
+
item.typeName
|
|
1303
|
+
);
|
|
1304
|
+
if (typename === void 0) return null;
|
|
1305
|
+
return fillObject(doc, selections, typename, item, item._id, ctx);
|
|
1306
|
+
};
|
|
1307
|
+
if (Array.isArray(value)) return value.map(one).filter((v) => v !== null);
|
|
1308
|
+
return value === void 0 ? null : one(value);
|
|
1309
|
+
}
|
|
1310
|
+
var NO_KEYS = /* @__PURE__ */ new Set();
|
|
1311
|
+
var listKeysCache = /* @__PURE__ */ new WeakMap();
|
|
1312
|
+
function listKeysIn(result) {
|
|
1313
|
+
return (typename) => {
|
|
1314
|
+
if (!typename || result === null || typeof result !== "object") {
|
|
1315
|
+
return NO_KEYS;
|
|
1316
|
+
}
|
|
1317
|
+
let byType = listKeysCache.get(result);
|
|
1318
|
+
if (!byType) {
|
|
1319
|
+
byType = /* @__PURE__ */ new Map();
|
|
1320
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
1321
|
+
const walk2 = (node) => {
|
|
1322
|
+
if (node === null || typeof node !== "object" || seen.has(node)) return;
|
|
1323
|
+
seen.add(node);
|
|
1324
|
+
if (Array.isArray(node)) {
|
|
1325
|
+
node.forEach(walk2);
|
|
1326
|
+
return;
|
|
1327
|
+
}
|
|
1328
|
+
const record = node;
|
|
1329
|
+
const type = typeof record.__typename === "string" ? record.__typename : null;
|
|
1330
|
+
for (const [key, value] of Object.entries(record)) {
|
|
1331
|
+
if (type && Array.isArray(value)) {
|
|
1332
|
+
let keys = byType.get(type);
|
|
1333
|
+
if (!keys) byType.set(type, keys = /* @__PURE__ */ new Set());
|
|
1334
|
+
keys.add(key);
|
|
1335
|
+
}
|
|
1336
|
+
walk2(value);
|
|
1337
|
+
}
|
|
1338
|
+
};
|
|
1339
|
+
walk2(result);
|
|
1340
|
+
listKeysCache.set(result, byType);
|
|
1341
|
+
}
|
|
1342
|
+
return byType.get(typename) ?? NO_KEYS;
|
|
1343
|
+
};
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
// src/core/live-preview/edit-links.ts
|
|
1347
|
+
var import_stega4 = require("@vercel/stega");
|
|
1348
|
+
var ENTRY_PARAM = "entry";
|
|
1349
|
+
var TOP_LEVEL_ENTRY = /^[A-Za-z0-9_]+\.\d+$/;
|
|
1350
|
+
function entryIdOf(element) {
|
|
1351
|
+
if (!isObject(element)) return void 0;
|
|
1352
|
+
if (typeof element._id === "string") return element._id;
|
|
1353
|
+
if (element.__typename !== "Assets" || !Array.isArray(element.items)) {
|
|
1354
|
+
return void 0;
|
|
1355
|
+
}
|
|
1356
|
+
const first = element.items[0];
|
|
1357
|
+
return isObject(first) && typeof first._id === "string" ? first._id : void 0;
|
|
1358
|
+
}
|
|
1359
|
+
function annotate(value, ctx) {
|
|
1360
|
+
if (!ctx.entryId) return value;
|
|
1361
|
+
const { cleaned, encoded } = (0, import_stega4.vercelStegaSplit)(value);
|
|
1362
|
+
if (!encoded) return value;
|
|
1363
|
+
let payload;
|
|
1364
|
+
try {
|
|
1365
|
+
payload = (0, import_stega4.vercelStegaDecode)(encoded);
|
|
1366
|
+
} catch {
|
|
1367
|
+
return value;
|
|
1368
|
+
}
|
|
1369
|
+
if (typeof payload?.href !== "string") return value;
|
|
1370
|
+
let url;
|
|
1371
|
+
try {
|
|
1372
|
+
url = new URL(payload.href);
|
|
1373
|
+
} catch {
|
|
1374
|
+
return value;
|
|
1375
|
+
}
|
|
1376
|
+
if (url.searchParams.has(ENTRY_PARAM)) return value;
|
|
1377
|
+
url.searchParams.set(ENTRY_PARAM, ctx.entryId);
|
|
1378
|
+
const itemId = decodeURIComponent(url.pathname.split("/").pop() ?? "");
|
|
1379
|
+
if (!url.searchParams.has("field") && ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) && itemId === ctx.entryOwnerId) {
|
|
1380
|
+
url.searchParams.set("field", ctx.entryField);
|
|
1381
|
+
}
|
|
1382
|
+
return (0, import_stega4.vercelStegaCombine)(
|
|
1383
|
+
cleaned,
|
|
1384
|
+
{ ...payload, href: url.toString() },
|
|
1385
|
+
false
|
|
1386
|
+
);
|
|
1387
|
+
}
|
|
1388
|
+
function walk(value, selections, ctx) {
|
|
1389
|
+
if (typeof value === "string") return annotate(value, ctx);
|
|
1390
|
+
if (Array.isArray(value)) {
|
|
1391
|
+
let changed2 = false;
|
|
1392
|
+
const next2 = value.map((element, index) => {
|
|
1393
|
+
const path = [...ctx.path, String(index)];
|
|
1394
|
+
const entryId = ctx.inAssets ? void 0 : entryIdOf(element);
|
|
1395
|
+
const entry = entryId ? {
|
|
1396
|
+
entryId,
|
|
1397
|
+
entryField: ctx.ownerId ? path.join(".") : void 0,
|
|
1398
|
+
entryOwnerId: ctx.ownerId,
|
|
1399
|
+
inAssets: isObject(element) && element._id === void 0
|
|
1400
|
+
} : {};
|
|
1401
|
+
const out = walk(element, selections, { ...ctx, ...entry, path });
|
|
1402
|
+
if (out !== element) changed2 = true;
|
|
1403
|
+
return out;
|
|
1404
|
+
});
|
|
1405
|
+
return changed2 ? next2 : value;
|
|
1406
|
+
}
|
|
1407
|
+
if (!isObject(value)) return value;
|
|
1408
|
+
const id = typeof value._id === "string" ? value._id : void 0;
|
|
1409
|
+
const typename = typeof value.__typename === "string" ? value.__typename : void 0;
|
|
1410
|
+
const fields = ctx.doc && selections ? collectFields(selections, typename, ctx.doc) : null;
|
|
1411
|
+
let changed = false;
|
|
1412
|
+
const next = {};
|
|
1413
|
+
for (const [key, child] of Object.entries(value)) {
|
|
1414
|
+
const field = fields?.get(key);
|
|
1415
|
+
const name = field?.name ?? key;
|
|
1416
|
+
const out = walk(child, field?.selections ?? null, {
|
|
1417
|
+
...ctx,
|
|
1418
|
+
ownerId: id ?? ctx.ownerId,
|
|
1419
|
+
path: id ? [name] : [...ctx.path, name]
|
|
1420
|
+
});
|
|
1421
|
+
if (out !== child) changed = true;
|
|
1422
|
+
next[key] = out;
|
|
1423
|
+
}
|
|
1424
|
+
return changed ? next : value;
|
|
1425
|
+
}
|
|
1426
|
+
function annotateEditLinks(result, query, operationName) {
|
|
1427
|
+
let doc = null;
|
|
1428
|
+
if (query !== void 0) {
|
|
1429
|
+
try {
|
|
1430
|
+
doc = readQueryDocument(query, operationName);
|
|
1431
|
+
} catch {
|
|
1432
|
+
doc = null;
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
return walk(result, doc?.operation ?? null, { doc, path: [] });
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1438
|
+
// src/nextjs/server.ts
|
|
1439
|
+
var React = __toESM(require("react"), 1);
|
|
1440
|
+
|
|
1441
|
+
// src/nextjs/live-log.ts
|
|
1442
|
+
var LIVE_LOG_COOKIE = "__prepr_live";
|
|
1443
|
+
var MAX_SLOTS = 50;
|
|
1444
|
+
var TOKEN_PATTERN = /^[0-9a-f]{32}$/;
|
|
1445
|
+
var KEY2 = /* @__PURE__ */ Symbol.for("@preprio/toolkit/live-log");
|
|
1446
|
+
function slots() {
|
|
1447
|
+
const holder = globalThis;
|
|
1448
|
+
let map = holder[KEY2];
|
|
1449
|
+
if (!map) {
|
|
1450
|
+
map = /* @__PURE__ */ new Map();
|
|
1451
|
+
holder[KEY2] = map;
|
|
1452
|
+
}
|
|
1453
|
+
return map;
|
|
1454
|
+
}
|
|
1455
|
+
function prune(map, now) {
|
|
1456
|
+
for (const [token, slot] of map) {
|
|
1457
|
+
if (slot.expires <= now) map.delete(token);
|
|
1458
|
+
}
|
|
1459
|
+
while (map.size >= MAX_SLOTS) {
|
|
1460
|
+
map.delete(map.keys().next().value);
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
function takeLiveLog(token) {
|
|
1464
|
+
if (!token || !TOKEN_PATTERN.test(token)) return [];
|
|
1465
|
+
const map = slots();
|
|
1466
|
+
const slot = map.get(token);
|
|
1467
|
+
map.delete(token);
|
|
1468
|
+
const now = Date.now();
|
|
1469
|
+
prune(map, now);
|
|
1470
|
+
if (!slot || slot.expires <= now) return [];
|
|
1471
|
+
return slot.log;
|
|
1472
|
+
}
|
|
276
1473
|
|
|
277
1474
|
// src/core/server.ts
|
|
278
1475
|
var PreprError = class extends Error {
|
|
@@ -461,173 +1658,27 @@ async function getToolbarProps(token, features) {
|
|
|
461
1658
|
const h = await readHeaders();
|
|
462
1659
|
return getToolbarPropsFromHeaders(h, token, features);
|
|
463
1660
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
var
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
return PERSONALIZED_VARY.test(headers2.get("vary") ?? "");
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
// src/nextjs/draft.ts
|
|
473
|
-
var import_headers2 = require("next/headers");
|
|
474
|
-
var import_navigation = require("next/navigation");
|
|
475
|
-
var import_server3 = require("next/server");
|
|
476
|
-
var DRAFT_COOKIE = "__prerender_bypass";
|
|
477
|
-
var RETURN_PARAM = "prepr_draft";
|
|
478
|
-
var DEFAULT_ROUTE = "/api/prepr/draft";
|
|
479
|
-
function preprDraftRedirect(request, options) {
|
|
480
|
-
if (!options.preview) return null;
|
|
481
|
-
if (request.method !== "GET" && request.method !== "HEAD") return null;
|
|
482
|
-
const url = request.nextUrl;
|
|
483
|
-
if (url.pathname === (options.route ?? DEFAULT_ROUTE)) return null;
|
|
484
|
-
if (request.cookies.has(DRAFT_COOKIE)) {
|
|
485
|
-
if (!url.searchParams.has(RETURN_PARAM)) return null;
|
|
486
|
-
const clean = url.clone();
|
|
487
|
-
clean.searchParams.delete(RETURN_PARAM);
|
|
488
|
-
return import_server3.NextResponse.redirect(clean, 307);
|
|
489
|
-
}
|
|
490
|
-
if (url.searchParams.has(RETURN_PARAM)) return null;
|
|
491
|
-
const target = new URL(options.route ?? DEFAULT_ROUTE, request.url);
|
|
492
|
-
target.searchParams.set("to", url.pathname + url.search);
|
|
493
|
-
return import_server3.NextResponse.redirect(target, 307);
|
|
494
|
-
}
|
|
495
|
-
function createPreprDraftRoute(options) {
|
|
496
|
-
return async (request) => {
|
|
497
|
-
if (!options.preview) return new Response("Not found", { status: 404 });
|
|
498
|
-
(await (0, import_headers2.draftMode)()).enable();
|
|
499
|
-
const self = new URL(request.url);
|
|
500
|
-
if (self.protocol === "https:") {
|
|
501
|
-
const jar = await (0, import_headers2.cookies)();
|
|
502
|
-
const draft = jar.get(DRAFT_COOKIE);
|
|
503
|
-
if (draft) {
|
|
504
|
-
jar.set({
|
|
505
|
-
name: DRAFT_COOKIE,
|
|
506
|
-
value: draft.value,
|
|
507
|
-
path: "/",
|
|
508
|
-
httpOnly: true,
|
|
509
|
-
secure: true,
|
|
510
|
-
sameSite: "none",
|
|
511
|
-
partitioned: true
|
|
512
|
-
});
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
let to = new URL(self.searchParams.get("to") ?? "/", self);
|
|
516
|
-
if (to.origin !== self.origin) to = new URL("/", self);
|
|
517
|
-
to.searchParams.set(RETURN_PARAM, "1");
|
|
518
|
-
(0, import_navigation.redirect)(to.pathname + to.search);
|
|
519
|
-
};
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
// src/nextjs/webhook.ts
|
|
523
|
-
var import_cache = require("next/cache");
|
|
524
|
-
|
|
525
|
-
// src/core/graphql/cache-tags.ts
|
|
526
|
-
var PREPR_ANY_ITEM_TAG = "prepr:any";
|
|
527
|
-
var PREPR_SCHEMA_TAG = "prepr:schema";
|
|
528
|
-
var preprItemTag = (id) => `prepr:${id}`;
|
|
529
|
-
var NOT_ALPHANUMERIC = /[^a-z0-9]/g;
|
|
530
|
-
var preprModelTag = (model) => `prepr:model:${model.toLowerCase().replace(NOT_ALPHANUMERIC, "")}`;
|
|
531
|
-
|
|
532
|
-
// src/nextjs/webhook.ts
|
|
533
|
-
var CONTENT_EVENTS = /* @__PURE__ */ new Set([
|
|
534
|
-
"content_item.published",
|
|
535
|
-
"content_item.unpublished",
|
|
536
|
-
"content_item.deleted",
|
|
537
|
-
"content_item.invalidated",
|
|
538
|
-
"asset.changed",
|
|
539
|
-
"asset.deleted"
|
|
540
|
-
]);
|
|
541
|
-
var MODEL_EVENTS = /* @__PURE__ */ new Set([
|
|
542
|
-
"content_item.published",
|
|
543
|
-
"content_item.unpublished",
|
|
544
|
-
"content_item.deleted"
|
|
545
|
-
]);
|
|
546
|
-
var MAX_AGE_MS = 5 * 60 * 1e3;
|
|
547
|
-
var HEX_SIGNATURE = /^[0-9a-f]{64}$/i;
|
|
548
|
-
var BASE64_SIGNATURE = /^[A-Za-z0-9+/]{43}=$/;
|
|
549
|
-
var SIGNATURE_PREFIX = /^sha256=/i;
|
|
550
|
-
var MAX_ID_LENGTH = 200;
|
|
551
|
-
var text = (body, status) => new Response(body, { status, headers: { "Content-Type": "text/plain" } });
|
|
552
|
-
function signatureBytes(header) {
|
|
553
|
-
const value = header.trim().replace(SIGNATURE_PREFIX, "");
|
|
554
|
-
if (HEX_SIGNATURE.test(value)) {
|
|
555
|
-
const bytes = new Uint8Array(32);
|
|
556
|
-
for (let i = 0; i < 32; i++) {
|
|
557
|
-
bytes[i] = parseInt(value.slice(i * 2, i * 2 + 2), 16);
|
|
558
|
-
}
|
|
559
|
-
return bytes;
|
|
560
|
-
}
|
|
561
|
-
if (BASE64_SIGNATURE.test(value)) {
|
|
562
|
-
return Uint8Array.from(atob(value), (char) => char.charCodeAt(0));
|
|
563
|
-
}
|
|
564
|
-
return null;
|
|
565
|
-
}
|
|
566
|
-
async function verify(secret, signature, body) {
|
|
567
|
-
const key = await crypto.subtle.importKey(
|
|
568
|
-
"raw",
|
|
569
|
-
new TextEncoder().encode(secret),
|
|
570
|
-
{ name: "HMAC", hash: "SHA-256" },
|
|
571
|
-
false,
|
|
572
|
-
["verify"]
|
|
573
|
-
);
|
|
574
|
-
return crypto.subtle.verify("HMAC", key, signature, body);
|
|
575
|
-
}
|
|
576
|
-
function parse(body) {
|
|
577
|
-
try {
|
|
578
|
-
const value = JSON.parse(new TextDecoder().decode(body));
|
|
579
|
-
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
580
|
-
} catch {
|
|
581
|
-
return null;
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
function eventTime(value) {
|
|
585
|
-
if (typeof value === "number") return value * 1e3;
|
|
586
|
-
if (typeof value === "string") return Date.parse(value);
|
|
587
|
-
return NaN;
|
|
588
|
-
}
|
|
589
|
-
function field(value, key) {
|
|
590
|
-
return typeof value === "object" && value !== null ? value[key] : void 0;
|
|
591
|
-
}
|
|
592
|
-
var usable = (value) => typeof value === "string" && value !== "" && value.length <= MAX_ID_LENGTH;
|
|
593
|
-
function tagsFor(event) {
|
|
594
|
-
const name = event.event;
|
|
595
|
-
if (name === "schema.changed") return [PREPR_SCHEMA_TAG];
|
|
596
|
-
if (typeof name !== "string" || !CONTENT_EVENTS.has(name)) return [];
|
|
597
|
-
const tags = [PREPR_ANY_ITEM_TAG];
|
|
598
|
-
const id = field(event.payload, "id");
|
|
599
|
-
if (usable(id)) tags.push(preprItemTag(id));
|
|
600
|
-
const model = field(field(event.payload, "model"), "body_singular");
|
|
601
|
-
if (MODEL_EVENTS.has(name) && usable(model)) tags.push(preprModelTag(model));
|
|
602
|
-
return tags;
|
|
603
|
-
}
|
|
604
|
-
function createPreprWebhookRoute(options) {
|
|
605
|
-
const { profile = { expire: 0 } } = options;
|
|
606
|
-
return async (request) => {
|
|
607
|
-
const { secret } = options;
|
|
608
|
-
if (!secret) return text("Webhook secret not configured", 500);
|
|
609
|
-
const signature = signatureBytes(
|
|
610
|
-
request.headers.get("prepr-signature") ?? ""
|
|
611
|
-
);
|
|
612
|
-
let body;
|
|
1661
|
+
var HEADER_NEXT_ACTION = "next-action";
|
|
1662
|
+
var cacheForRender = React.cache ?? ((fn) => fn);
|
|
1663
|
+
var getPreprLiveLog = cacheForRender(
|
|
1664
|
+
async () => {
|
|
1665
|
+
let token;
|
|
613
1666
|
try {
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
1667
|
+
if (!(await (0, import_headers.headers)()).has(HEADER_NEXT_ACTION)) return [];
|
|
1668
|
+
token = (await (0, import_headers.cookies)()).get(LIVE_LOG_COOKIE)?.value;
|
|
1669
|
+
} catch (error) {
|
|
1670
|
+
navigation.unstable_rethrow?.(error);
|
|
1671
|
+
return [];
|
|
617
1672
|
}
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
const tags = tagsFor(event);
|
|
628
|
-
for (const tag of tags) (0, import_cache.revalidateTag)(tag, profile);
|
|
629
|
-
return Response.json({ revalidated: tags });
|
|
630
|
-
};
|
|
1673
|
+
return takeLiveLog(token);
|
|
1674
|
+
}
|
|
1675
|
+
);
|
|
1676
|
+
async function normalizeQuery(query, result, options = {}) {
|
|
1677
|
+
if (!options.enabled) return result;
|
|
1678
|
+
const annotated = annotateEditLinks(result, query, options.operationName);
|
|
1679
|
+
const log = await getPreprLiveLog();
|
|
1680
|
+
if (!log.length) return annotated;
|
|
1681
|
+
return applyPatchLog(query, annotated, log, options.operationName);
|
|
631
1682
|
}
|
|
632
1683
|
|
|
633
1684
|
// src/nextjs/index.ts
|
|
@@ -635,19 +1686,15 @@ var import_components = require("./components.cjs");
|
|
|
635
1686
|
// Annotate the CommonJS export names for ESM import in node:
|
|
636
1687
|
0 && (module.exports = {
|
|
637
1688
|
PreprError,
|
|
638
|
-
PreprPersonalizationSwitch,
|
|
639
1689
|
PreprToolbar,
|
|
640
1690
|
PreprTrackingPixel,
|
|
641
|
-
createPreprDraftRoute,
|
|
642
1691
|
createPreprMiddleware,
|
|
643
|
-
createPreprWebhookRoute,
|
|
644
1692
|
extractAccessToken,
|
|
645
1693
|
getActiveSegment,
|
|
646
1694
|
getActiveVariant,
|
|
647
1695
|
getPreprHeaders,
|
|
648
1696
|
getPreprUUID,
|
|
649
1697
|
getToolbarProps,
|
|
650
|
-
|
|
651
|
-
preprDraftRedirect,
|
|
1698
|
+
normalizeQuery,
|
|
652
1699
|
validatePreprToken
|
|
653
1700
|
});
|