@preprio/toolkit 0.5.0-rc.1 → 0.5.0-rc.11
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 +430 -23
- package/dist/astro/index.cjs +1290 -1
- package/dist/astro/index.d.cts +47 -1
- package/dist/astro/index.d.ts +47 -1
- package/dist/astro/index.js +1297 -1
- package/dist/{chunk-4JNKYIEV.js → chunk-6423Q744.js} +1449 -642
- package/dist/chunk-AOHA5INA.js +26 -0
- package/dist/chunk-IPMB32AZ.js +246 -0
- package/dist/chunk-KD3GK6TO.js +1221 -0
- package/dist/chunk-QSXIHRH4.js +184 -0
- package/dist/chunk-S3VT4UWM.js +75 -0
- package/dist/chunk-URXSDJOU.js +356 -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 +3367 -416
- package/dist/index.d.cts +276 -5
- package/dist/index.d.ts +276 -5
- package/dist/index.js +307 -144
- package/dist/live-preview-DLXGXRE5.js +18 -0
- package/dist/nextjs/components.cjs +1608 -386
- package/dist/nextjs/components.d.cts +1 -1
- package/dist/nextjs/components.d.ts +1 -1
- package/dist/nextjs/components.js +4 -1
- package/dist/nextjs/index.cjs +1181 -6
- package/dist/nextjs/index.d.cts +83 -3
- package/dist/nextjs/index.d.ts +83 -3
- package/dist/nextjs/index.js +1179 -7
- package/dist/nextjs/live-action.cjs +285 -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 +1899 -0
- package/dist/nextjs/live.d.cts +54 -0
- package/dist/nextjs/live.d.ts +54 -0
- package/dist/nextjs/live.js +268 -0
- package/dist/nuxt/chunk-BWPGO25E.js +340 -0
- package/dist/nuxt/index.cjs +1 -1
- package/dist/nuxt/index.js +1 -1
- package/dist/nuxt/live-preview-2RCU35W5.js +1189 -0
- package/dist/nuxt/live.cjs +1816 -0
- package/dist/nuxt/live.d.cts +43 -0
- package/dist/nuxt/live.d.ts +43 -0
- package/dist/nuxt/live.js +198 -0
- package/dist/{pixel-e1fUWl2Y.d.cts → pixel-CX422dCO.d.cts} +18 -3
- package/dist/{pixel-e1fUWl2Y.d.ts → pixel-CX422dCO.d.ts} +18 -3
- package/dist/react/components.cjs +1608 -386
- package/dist/react/components.d.cts +17 -2
- package/dist/react/components.d.ts +17 -2
- package/dist/react/components.js +1608 -386
- package/dist/sveltekit/chunk-BWPGO25E.js +340 -0
- package/dist/sveltekit/index.cjs +1 -1
- package/dist/sveltekit/index.js +1 -1
- package/dist/sveltekit/live-preview-2RCU35W5.js +1189 -0
- package/dist/sveltekit/live.cjs +1830 -0
- package/dist/sveltekit/live.d.cts +59 -0
- package/dist/sveltekit/live.d.ts +59 -0
- package/dist/sveltekit/live.js +204 -0
- package/package.json +37 -11
- package/src/astro/PreprToolbar.astro +7 -1
package/dist/astro/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/version.ts
|
|
2
|
-
var VERSION = "0.5.0-rc.
|
|
2
|
+
var VERSION = "0.5.0-rc.11";
|
|
3
3
|
|
|
4
4
|
// src/core/features.ts
|
|
5
5
|
function isEnabled(config) {
|
|
@@ -162,6 +162,7 @@ var PARAM_SEGMENT = "prepr_preview_segment";
|
|
|
162
162
|
var PARAM_VARIANT = "prepr_preview_ab";
|
|
163
163
|
var PARAM_HIDE_BAR = "prepr_hide_bar";
|
|
164
164
|
var ONE_YEAR_SECONDS = 60 * 60 * 24 * 365;
|
|
165
|
+
var HEADER_LIVE_RENDER = "x-prepr-live";
|
|
165
166
|
|
|
166
167
|
// src/core/middleware.ts
|
|
167
168
|
var UTM_PARAMS = [
|
|
@@ -329,7 +330,1284 @@ function processFrameworkRequest(request, opts, framework) {
|
|
|
329
330
|
return { requestHeaders, responseCookies };
|
|
330
331
|
}
|
|
331
332
|
|
|
333
|
+
// src/core/live-preview/normalize-query.ts
|
|
334
|
+
import {
|
|
335
|
+
vercelStegaCombine,
|
|
336
|
+
vercelStegaDecode,
|
|
337
|
+
vercelStegaSplit as vercelStegaSplit2
|
|
338
|
+
} from "@vercel/stega";
|
|
339
|
+
|
|
340
|
+
// src/core/utils.ts
|
|
341
|
+
var DebugLogger = class {
|
|
342
|
+
constructor(options) {
|
|
343
|
+
this.options = {
|
|
344
|
+
prefix: "[Prepr]",
|
|
345
|
+
...options
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
// A local `enabled` wins; otherwise defer to the global logger so scoped
|
|
349
|
+
// loggers created before initDebugLogger() still pick up the setting.
|
|
350
|
+
isEnabled() {
|
|
351
|
+
if (this.options.enabled !== void 0) {
|
|
352
|
+
return this.options.enabled;
|
|
353
|
+
}
|
|
354
|
+
return globalDebugLogger?.options?.enabled ?? false;
|
|
355
|
+
}
|
|
356
|
+
log(message, ...args) {
|
|
357
|
+
if (!this.isEnabled()) return;
|
|
358
|
+
const prefix = this.options.prefix;
|
|
359
|
+
console.log(`${prefix} ${message}`, ...args);
|
|
360
|
+
}
|
|
361
|
+
warn(message, ...args) {
|
|
362
|
+
if (!this.isEnabled()) return;
|
|
363
|
+
const prefix = this.options.prefix;
|
|
364
|
+
console.warn(`${prefix} ${message}`, ...args);
|
|
365
|
+
}
|
|
366
|
+
error(message, ...args) {
|
|
367
|
+
if (!this.isEnabled()) return;
|
|
368
|
+
const prefix = this.options.prefix;
|
|
369
|
+
console.error(`${prefix} ${message}`, ...args);
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
var globalDebugLogger = null;
|
|
373
|
+
function createScopedLogger(scopeName) {
|
|
374
|
+
return new DebugLogger({
|
|
375
|
+
prefix: `[Prepr][${scopeName}]`
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// src/core/live-preview/field-name.ts
|
|
380
|
+
var UPPERCASE_BOUNDARY = /(.)(?=[A-Z])/g;
|
|
381
|
+
function graphqlFieldName(apiId) {
|
|
382
|
+
return apiId.replace(UPPERCASE_BOUNDARY, "$1_").toLowerCase();
|
|
383
|
+
}
|
|
384
|
+
var TEXT_BODY_FIELDS = [
|
|
385
|
+
{ name: "body", plain: false },
|
|
386
|
+
{ name: "html", plain: false },
|
|
387
|
+
{ name: "text", plain: true }
|
|
388
|
+
];
|
|
389
|
+
var TAG_PATTERN = /<[^>]+>/g;
|
|
390
|
+
function plainTextFromHtml(html) {
|
|
391
|
+
return html.replace(TAG_PATTERN, "");
|
|
392
|
+
}
|
|
393
|
+
function isTextEntry(record) {
|
|
394
|
+
const typename = record.__typename;
|
|
395
|
+
if (typeof typename === "string") return typename === "Text";
|
|
396
|
+
return "html" in record || "text" in record && "format" in record;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// src/core/live-preview/index-snapshot.ts
|
|
400
|
+
var debug = createScopedLogger("live-preview");
|
|
401
|
+
function indexSnapshot(data) {
|
|
402
|
+
const index = /* @__PURE__ */ new Map();
|
|
403
|
+
const visited = /* @__PURE__ */ new WeakSet();
|
|
404
|
+
const path = [];
|
|
405
|
+
function walk2(node) {
|
|
406
|
+
if (node === null || typeof node !== "object") return;
|
|
407
|
+
if (visited.has(node)) return;
|
|
408
|
+
visited.add(node);
|
|
409
|
+
if (Array.isArray(node)) {
|
|
410
|
+
for (let i = 0; i < node.length; i++) {
|
|
411
|
+
path.push(i);
|
|
412
|
+
walk2(node[i]);
|
|
413
|
+
path.pop();
|
|
414
|
+
}
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
const record = node;
|
|
418
|
+
if (typeof record._id === "string") {
|
|
419
|
+
const paths = index.get(record._id);
|
|
420
|
+
if (paths) paths.push([...path]);
|
|
421
|
+
else index.set(record._id, [[...path]]);
|
|
422
|
+
} else if (typeof record.__typename === "string") {
|
|
423
|
+
debug.log("item without _id will not live-update", {
|
|
424
|
+
__typename: record.__typename
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
for (const key in record) {
|
|
428
|
+
if (!Object.prototype.hasOwnProperty.call(record, key)) continue;
|
|
429
|
+
path.push(key);
|
|
430
|
+
walk2(record[key]);
|
|
431
|
+
path.pop();
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
walk2(data);
|
|
435
|
+
return index;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// src/core/live-preview/patch.ts
|
|
439
|
+
import { vercelStegaSplit } from "@vercel/stega";
|
|
440
|
+
var FORBIDDEN_SEGMENTS = /* @__PURE__ */ new Set(["__proto__", "prototype", "constructor"]);
|
|
441
|
+
function isPlainObject(value) {
|
|
442
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
443
|
+
}
|
|
444
|
+
function mergeObject(current, incoming) {
|
|
445
|
+
let changed = false;
|
|
446
|
+
const next = { ...current };
|
|
447
|
+
for (const key of Object.keys(current)) {
|
|
448
|
+
if (!(key in incoming) || incoming[key] === void 0) continue;
|
|
449
|
+
const value = isPlainObject(current[key]) && isPlainObject(incoming[key]) ? mergeObject(current[key], incoming[key]) : incoming[key];
|
|
450
|
+
if (value !== current[key]) {
|
|
451
|
+
next[key] = value;
|
|
452
|
+
changed = true;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
return changed ? next : current;
|
|
456
|
+
}
|
|
457
|
+
function shapeLike(template, incoming) {
|
|
458
|
+
const out = {};
|
|
459
|
+
for (const key of Object.keys(template)) {
|
|
460
|
+
const value = incoming[key];
|
|
461
|
+
out[key] = value === void 0 ? null : isPlainObject(template[key]) && isPlainObject(value) ? shapeLike(template[key], value) : value;
|
|
462
|
+
}
|
|
463
|
+
return out;
|
|
464
|
+
}
|
|
465
|
+
function mergeStructured(current, incoming) {
|
|
466
|
+
if (incoming === null || Array.isArray(incoming) && !incoming.length) {
|
|
467
|
+
if (Array.isArray(current)) return current.length ? [] : current;
|
|
468
|
+
return null;
|
|
469
|
+
}
|
|
470
|
+
const list = Array.isArray(incoming) ? incoming : [incoming];
|
|
471
|
+
if (current === null || current === void 0) return incoming;
|
|
472
|
+
if (Array.isArray(current)) {
|
|
473
|
+
const template = current.find(isPlainObject);
|
|
474
|
+
return list.map((entry) => {
|
|
475
|
+
const match = current.find(
|
|
476
|
+
(c) => isPlainObject(c) && c._id !== void 0 && c._id === entry._id
|
|
477
|
+
);
|
|
478
|
+
if (isPlainObject(match)) return mergeObject(match, entry);
|
|
479
|
+
return isPlainObject(template) ? shapeLike(template, entry) : entry;
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
if (isPlainObject(current)) return mergeObject(current, list[0]);
|
|
483
|
+
return current;
|
|
484
|
+
}
|
|
485
|
+
function isPatchable(current) {
|
|
486
|
+
return current === null || typeof current === "string" || typeof current === "number" || typeof current === "boolean";
|
|
487
|
+
}
|
|
488
|
+
function cloneNode(node) {
|
|
489
|
+
return Array.isArray(node) ? [...node] : { ...node };
|
|
490
|
+
}
|
|
491
|
+
function writeAtPath(root, fullPath, value, markChanged = () => {
|
|
492
|
+
}) {
|
|
493
|
+
if (!fullPath.length) return root;
|
|
494
|
+
if (fullPath.some((s) => FORBIDDEN_SEGMENTS.has(String(s)))) return root;
|
|
495
|
+
const last = fullPath.length - 1;
|
|
496
|
+
let node = root;
|
|
497
|
+
for (let i = 0; i < last; i++) {
|
|
498
|
+
if (node === null || typeof node !== "object") return root;
|
|
499
|
+
node = node[fullPath[i]];
|
|
500
|
+
}
|
|
501
|
+
if (node === null || typeof node !== "object") return root;
|
|
502
|
+
const container = node;
|
|
503
|
+
const key = fullPath[last];
|
|
504
|
+
if (!(key in container)) return root;
|
|
505
|
+
let next;
|
|
506
|
+
if (value === null || typeof value === "object") {
|
|
507
|
+
next = mergeStructured(container[key], value);
|
|
508
|
+
} else if (!isPatchable(container[key])) {
|
|
509
|
+
return root;
|
|
510
|
+
} else {
|
|
511
|
+
next = keepEditLink(container[key], value);
|
|
512
|
+
}
|
|
513
|
+
if (container[key] === next) return root;
|
|
514
|
+
const newRoot = cloneNode(root);
|
|
515
|
+
let cursor = newRoot;
|
|
516
|
+
for (let i = 0; i < last; i++) {
|
|
517
|
+
const child = cloneNode(cursor[fullPath[i]]);
|
|
518
|
+
cursor[fullPath[i]] = child;
|
|
519
|
+
cursor = child;
|
|
520
|
+
}
|
|
521
|
+
cursor[key] = next;
|
|
522
|
+
markChanged();
|
|
523
|
+
return newRoot;
|
|
524
|
+
}
|
|
525
|
+
function listKeysOf(entry) {
|
|
526
|
+
if (!isPlainObject(entry)) return [];
|
|
527
|
+
const keys = [];
|
|
528
|
+
if (typeof entry._id === "string") keys.push(entry._id);
|
|
529
|
+
const first = Array.isArray(entry.items) ? entry.items[0] : null;
|
|
530
|
+
if (isPlainObject(first) && typeof first._id === "string") {
|
|
531
|
+
keys.push(first._id);
|
|
532
|
+
}
|
|
533
|
+
return keys;
|
|
534
|
+
}
|
|
535
|
+
function buildText(entry, template) {
|
|
536
|
+
const body = typeof entry.body === "string" ? entry.body : "";
|
|
537
|
+
const full = {
|
|
538
|
+
__typename: "Text",
|
|
539
|
+
_id: entry._id,
|
|
540
|
+
body,
|
|
541
|
+
html: body,
|
|
542
|
+
text: plainTextFromHtml(body),
|
|
543
|
+
format: entry.format ?? null
|
|
544
|
+
};
|
|
545
|
+
if (!template) return full;
|
|
546
|
+
const out = {};
|
|
547
|
+
for (const key of Object.keys(template)) {
|
|
548
|
+
out[key] = key in full ? full[key] : null;
|
|
549
|
+
}
|
|
550
|
+
return out;
|
|
551
|
+
}
|
|
552
|
+
function syncList(current, entries, build) {
|
|
553
|
+
if (current === null || current === void 0) {
|
|
554
|
+
const built = syncList([], entries, build);
|
|
555
|
+
return built.length ? built : current;
|
|
556
|
+
}
|
|
557
|
+
if (!Array.isArray(current)) return current;
|
|
558
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
559
|
+
const templates = /* @__PURE__ */ new Map();
|
|
560
|
+
for (const c of current) {
|
|
561
|
+
for (const key of listKeysOf(c)) byKey.set(key, c);
|
|
562
|
+
if (isPlainObject(c) && typeof c.__typename === "string") {
|
|
563
|
+
if (!templates.has(c.__typename)) templates.set(c.__typename, c);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
if (!entries.length) return current.length ? [] : current;
|
|
567
|
+
if (current.length && !byKey.size) return current;
|
|
568
|
+
if (current.length && !entries.some((e) => byKey.has(e._id))) return current;
|
|
569
|
+
const next = [];
|
|
570
|
+
for (const entry of entries) {
|
|
571
|
+
const existing = byKey.get(entry._id);
|
|
572
|
+
const built = existing === void 0 && entry.fields ? build?.(entry) : void 0;
|
|
573
|
+
if (existing !== void 0) {
|
|
574
|
+
next.push(existing);
|
|
575
|
+
} else if (built) {
|
|
576
|
+
next.push(built);
|
|
577
|
+
} else if (entry.__typename === "Text") {
|
|
578
|
+
next.push(buildText(entry, templates.get("Text")));
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
const same = next.length === current.length && next.every((n, i) => n === current[i]);
|
|
582
|
+
return same ? current : next;
|
|
583
|
+
}
|
|
584
|
+
function syncListAtPath(root, fullPath, entries, build) {
|
|
585
|
+
if (!fullPath.length) return root;
|
|
586
|
+
if (fullPath.some((s) => FORBIDDEN_SEGMENTS.has(String(s)))) return root;
|
|
587
|
+
let node = root;
|
|
588
|
+
for (const key of fullPath) {
|
|
589
|
+
node = node !== null && typeof node === "object" ? node[key] : void 0;
|
|
590
|
+
}
|
|
591
|
+
const next = syncList(node, entries, build);
|
|
592
|
+
if (next === node) return root;
|
|
593
|
+
const newRoot = cloneNode(root);
|
|
594
|
+
let cursor = newRoot;
|
|
595
|
+
for (let i = 0; i < fullPath.length - 1; i++) {
|
|
596
|
+
const child = cloneNode(cursor[fullPath[i]]);
|
|
597
|
+
cursor[fullPath[i]] = child;
|
|
598
|
+
cursor = child;
|
|
599
|
+
}
|
|
600
|
+
cursor[fullPath[fullPath.length - 1]] = next;
|
|
601
|
+
return newRoot;
|
|
602
|
+
}
|
|
603
|
+
function keepEditLink(current, value) {
|
|
604
|
+
if (typeof current !== "string" || typeof value !== "string") return value;
|
|
605
|
+
const { encoded } = vercelStegaSplit(current);
|
|
606
|
+
if (!encoded || vercelStegaSplit(value).encoded) return value;
|
|
607
|
+
return value + encoded;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// src/core/live-preview/query-document.ts
|
|
611
|
+
var NAME_START = /[_A-Za-z]/;
|
|
612
|
+
var NAME_CONTINUE = /[_0-9A-Za-z]/;
|
|
613
|
+
var PUNCT = /* @__PURE__ */ new Set([
|
|
614
|
+
"!",
|
|
615
|
+
"$",
|
|
616
|
+
"&",
|
|
617
|
+
"(",
|
|
618
|
+
")",
|
|
619
|
+
":",
|
|
620
|
+
"=",
|
|
621
|
+
"@",
|
|
622
|
+
"[",
|
|
623
|
+
"]",
|
|
624
|
+
"{",
|
|
625
|
+
"|",
|
|
626
|
+
"}"
|
|
627
|
+
]);
|
|
628
|
+
function tokenize(source) {
|
|
629
|
+
const tokens = [];
|
|
630
|
+
let i = 0;
|
|
631
|
+
const n = source.length;
|
|
632
|
+
while (i < n) {
|
|
633
|
+
const ch = source[i];
|
|
634
|
+
if (ch === " " || ch === " " || ch === "\n" || ch === "\r" || ch === "," || ch === "\uFEFF") {
|
|
635
|
+
i++;
|
|
636
|
+
} else if (ch === "#") {
|
|
637
|
+
while (i < n && source[i] !== "\n" && source[i] !== "\r") i++;
|
|
638
|
+
} else if (ch === "." && source.startsWith("...", i)) {
|
|
639
|
+
tokens.push({ type: "punct", value: "..." });
|
|
640
|
+
i += 3;
|
|
641
|
+
} else if (PUNCT.has(ch)) {
|
|
642
|
+
tokens.push({ type: "punct", value: ch });
|
|
643
|
+
i++;
|
|
644
|
+
} else if (NAME_START.test(ch)) {
|
|
645
|
+
const start = i;
|
|
646
|
+
while (i < n && NAME_CONTINUE.test(source[i])) i++;
|
|
647
|
+
tokens.push({ type: "name", value: source.slice(start, i) });
|
|
648
|
+
} else if (ch === '"') {
|
|
649
|
+
if (source.startsWith('"""', i)) {
|
|
650
|
+
i += 3;
|
|
651
|
+
while (i < n && !source.startsWith('"""', i)) {
|
|
652
|
+
i += source.startsWith('\\"""', i) ? 4 : 1;
|
|
653
|
+
}
|
|
654
|
+
if (i >= n) throw new SyntaxError("Unterminated block string");
|
|
655
|
+
i += 3;
|
|
656
|
+
} else {
|
|
657
|
+
i++;
|
|
658
|
+
while (i < n && source[i] !== '"') {
|
|
659
|
+
if (source[i] === "\n" || source[i] === "\r") {
|
|
660
|
+
throw new SyntaxError("Unterminated string");
|
|
661
|
+
}
|
|
662
|
+
i += source[i] === "\\" ? 2 : 1;
|
|
663
|
+
}
|
|
664
|
+
if (i >= n) throw new SyntaxError("Unterminated string");
|
|
665
|
+
i++;
|
|
666
|
+
}
|
|
667
|
+
tokens.push({ type: "value" });
|
|
668
|
+
} else if (ch === "-" || ch >= "0" && ch <= "9") {
|
|
669
|
+
i++;
|
|
670
|
+
while (i < n && /[0-9.eE+-]/.test(source[i])) i++;
|
|
671
|
+
tokens.push({ type: "value" });
|
|
672
|
+
} else {
|
|
673
|
+
throw new SyntaxError(`Unexpected character "${ch}"`);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
return tokens;
|
|
677
|
+
}
|
|
678
|
+
function parseSource(source, operationName) {
|
|
679
|
+
const tokens = tokenize(source);
|
|
680
|
+
let pos = 0;
|
|
681
|
+
const peek = () => tokens[pos];
|
|
682
|
+
const isPunct = (value) => {
|
|
683
|
+
const t = tokens[pos];
|
|
684
|
+
return t?.type === "punct" && t.value === value;
|
|
685
|
+
};
|
|
686
|
+
const expectPunct = (value) => {
|
|
687
|
+
if (!isPunct(value)) throw new SyntaxError(`Expected "${value}"`);
|
|
688
|
+
pos++;
|
|
689
|
+
};
|
|
690
|
+
const expectName = () => {
|
|
691
|
+
const t = tokens[pos];
|
|
692
|
+
if (t?.type !== "name") throw new SyntaxError("Expected a name");
|
|
693
|
+
pos++;
|
|
694
|
+
return t.value;
|
|
695
|
+
};
|
|
696
|
+
const skipParens = () => {
|
|
697
|
+
expectPunct("(");
|
|
698
|
+
let depth = 1;
|
|
699
|
+
while (depth > 0) {
|
|
700
|
+
const t = tokens[pos++];
|
|
701
|
+
if (!t) throw new SyntaxError("Unbalanced parentheses");
|
|
702
|
+
if (t.type === "punct" && t.value === "(") depth++;
|
|
703
|
+
else if (t.type === "punct" && t.value === ")") depth--;
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
const skipDirectives = () => {
|
|
707
|
+
while (isPunct("@")) {
|
|
708
|
+
pos++;
|
|
709
|
+
expectName();
|
|
710
|
+
if (isPunct("(")) skipParens();
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
const parseSelectionSet = () => {
|
|
714
|
+
expectPunct("{");
|
|
715
|
+
const selections = [];
|
|
716
|
+
while (!isPunct("}")) {
|
|
717
|
+
if (!peek()) throw new SyntaxError("Unterminated selection set");
|
|
718
|
+
selections.push(parseSelection());
|
|
719
|
+
}
|
|
720
|
+
pos++;
|
|
721
|
+
return selections;
|
|
722
|
+
};
|
|
723
|
+
const parseSelection = () => {
|
|
724
|
+
if (isPunct("...")) {
|
|
725
|
+
pos++;
|
|
726
|
+
const t = peek();
|
|
727
|
+
if (t?.type === "name" && t.value !== "on") {
|
|
728
|
+
pos++;
|
|
729
|
+
skipDirectives();
|
|
730
|
+
return { kind: "spread", name: t.value };
|
|
731
|
+
}
|
|
732
|
+
let on = null;
|
|
733
|
+
if (t?.type === "name") {
|
|
734
|
+
pos++;
|
|
735
|
+
on = expectName();
|
|
736
|
+
}
|
|
737
|
+
skipDirectives();
|
|
738
|
+
return { kind: "inline", on, selections: parseSelectionSet() };
|
|
739
|
+
}
|
|
740
|
+
let name = expectName();
|
|
741
|
+
const key = name;
|
|
742
|
+
if (isPunct(":")) {
|
|
743
|
+
pos++;
|
|
744
|
+
name = expectName();
|
|
745
|
+
}
|
|
746
|
+
let hasArgs = false;
|
|
747
|
+
if (isPunct("(")) {
|
|
748
|
+
hasArgs = true;
|
|
749
|
+
skipParens();
|
|
750
|
+
}
|
|
751
|
+
skipDirectives();
|
|
752
|
+
const selections = isPunct("{") ? parseSelectionSet() : null;
|
|
753
|
+
return { kind: "field", name, key, hasArgs, selections };
|
|
754
|
+
};
|
|
755
|
+
const fragments = /* @__PURE__ */ new Map();
|
|
756
|
+
const operations = [];
|
|
757
|
+
while (pos < tokens.length) {
|
|
758
|
+
if (isPunct("{")) {
|
|
759
|
+
operations.push({ name: null, selections: parseSelectionSet() });
|
|
760
|
+
continue;
|
|
761
|
+
}
|
|
762
|
+
const keyword = expectName();
|
|
763
|
+
if (keyword === "fragment") {
|
|
764
|
+
const name = expectName();
|
|
765
|
+
if (expectName() !== "on") throw new SyntaxError('Expected "on"');
|
|
766
|
+
const on = expectName();
|
|
767
|
+
skipDirectives();
|
|
768
|
+
fragments.set(name, { on, selections: parseSelectionSet() });
|
|
769
|
+
} else if (keyword === "query" || keyword === "mutation" || keyword === "subscription") {
|
|
770
|
+
const t = peek();
|
|
771
|
+
let name = null;
|
|
772
|
+
if (t?.type === "name") name = expectName();
|
|
773
|
+
if (isPunct("(")) skipParens();
|
|
774
|
+
skipDirectives();
|
|
775
|
+
operations.push({ name, selections: parseSelectionSet() });
|
|
776
|
+
} else {
|
|
777
|
+
throw new SyntaxError(`Unexpected "${keyword}"`);
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
return { operation: pickOperation(operations, operationName), fragments };
|
|
781
|
+
}
|
|
782
|
+
function pickOperation(operations, operationName) {
|
|
783
|
+
const op = operationName ? operations.find((o) => o.name === operationName) : operations[0];
|
|
784
|
+
if (!op) throw new SyntaxError("The document has no operation");
|
|
785
|
+
return op.selections;
|
|
786
|
+
}
|
|
787
|
+
function fromAstSelections(set) {
|
|
788
|
+
return set.selections.map((s) => {
|
|
789
|
+
if (s.kind === "FragmentSpread")
|
|
790
|
+
return { kind: "spread", name: s.name.value };
|
|
791
|
+
if (s.kind === "InlineFragment") {
|
|
792
|
+
return {
|
|
793
|
+
kind: "inline",
|
|
794
|
+
on: s.typeCondition?.name.value ?? null,
|
|
795
|
+
selections: fromAstSelections(s.selectionSet)
|
|
796
|
+
};
|
|
797
|
+
}
|
|
798
|
+
const name = s.name.value;
|
|
799
|
+
return {
|
|
800
|
+
kind: "field",
|
|
801
|
+
name,
|
|
802
|
+
key: s.alias?.value ?? name,
|
|
803
|
+
hasArgs: Boolean(s.arguments?.length),
|
|
804
|
+
selections: s.selectionSet ? fromAstSelections(s.selectionSet) : null
|
|
805
|
+
};
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
function fromAst(doc, operationName) {
|
|
809
|
+
const fragments = /* @__PURE__ */ new Map();
|
|
810
|
+
const operations = [];
|
|
811
|
+
for (const def of doc.definitions ?? []) {
|
|
812
|
+
if (def.kind === "FragmentDefinition" && def.name && def.typeCondition && def.selectionSet) {
|
|
813
|
+
fragments.set(def.name.value, {
|
|
814
|
+
on: def.typeCondition.name.value,
|
|
815
|
+
selections: fromAstSelections(def.selectionSet)
|
|
816
|
+
});
|
|
817
|
+
} else if (def.kind === "OperationDefinition" && def.selectionSet) {
|
|
818
|
+
operations.push({
|
|
819
|
+
name: def.name?.value ?? null,
|
|
820
|
+
selections: fromAstSelections(def.selectionSet)
|
|
821
|
+
});
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
return { operation: pickOperation(operations, operationName), fragments };
|
|
825
|
+
}
|
|
826
|
+
var astCache = /* @__PURE__ */ new WeakMap();
|
|
827
|
+
var sourceCache = /* @__PURE__ */ new Map();
|
|
828
|
+
var SOURCE_CACHE_LIMIT = 200;
|
|
829
|
+
function readQueryDocument(query, operationName) {
|
|
830
|
+
const cacheKey = operationName ?? "";
|
|
831
|
+
if (typeof query === "object" && query !== null && query.kind === "Document") {
|
|
832
|
+
let byName = astCache.get(query);
|
|
833
|
+
if (!byName) astCache.set(query, byName = /* @__PURE__ */ new Map());
|
|
834
|
+
let parsed2 = byName.get(cacheKey);
|
|
835
|
+
if (!parsed2) {
|
|
836
|
+
parsed2 = fromAst(
|
|
837
|
+
query,
|
|
838
|
+
operationName
|
|
839
|
+
);
|
|
840
|
+
byName.set(cacheKey, parsed2);
|
|
841
|
+
}
|
|
842
|
+
return parsed2;
|
|
843
|
+
}
|
|
844
|
+
const source = String(query);
|
|
845
|
+
const sourceKey = `${cacheKey}
|
|
846
|
+
${source}`;
|
|
847
|
+
let parsed = sourceCache.get(sourceKey);
|
|
848
|
+
if (!parsed) {
|
|
849
|
+
parsed = parseSource(source, operationName);
|
|
850
|
+
if (sourceCache.size >= SOURCE_CACHE_LIMIT) {
|
|
851
|
+
sourceCache.delete(sourceCache.keys().next().value);
|
|
852
|
+
}
|
|
853
|
+
sourceCache.set(sourceKey, parsed);
|
|
854
|
+
}
|
|
855
|
+
return parsed;
|
|
856
|
+
}
|
|
857
|
+
var fieldsCache = /* @__PURE__ */ new WeakMap();
|
|
858
|
+
function collectFields(selections, typename, doc, options = {}) {
|
|
859
|
+
const cacheKey = `${options.strict ? "s" : "l"}:${typename ?? ""}`;
|
|
860
|
+
let cached = fieldsCache.get(selections);
|
|
861
|
+
if (!cached) {
|
|
862
|
+
cached = /* @__PURE__ */ new Map();
|
|
863
|
+
fieldsCache.set(selections, cached);
|
|
864
|
+
}
|
|
865
|
+
const hit = cached.get(cacheKey);
|
|
866
|
+
if (hit) return hit;
|
|
867
|
+
const fields = resolveFields(selections, typename, doc, options);
|
|
868
|
+
cached.set(cacheKey, fields);
|
|
869
|
+
return fields;
|
|
870
|
+
}
|
|
871
|
+
function resolveFields(selections, typename, doc, options) {
|
|
872
|
+
const exact = /* @__PURE__ */ new Map();
|
|
873
|
+
const loose = /* @__PURE__ */ new Map();
|
|
874
|
+
const merged = /* @__PURE__ */ new Map();
|
|
875
|
+
const walkedMatched = /* @__PURE__ */ new Set();
|
|
876
|
+
const walkedLoose = /* @__PURE__ */ new Set();
|
|
877
|
+
const add = (target, s) => {
|
|
878
|
+
let field = target.get(s.key);
|
|
879
|
+
if (!field) {
|
|
880
|
+
field = { name: s.name, key: s.key, hasArgs: s.hasArgs, selections: [] };
|
|
881
|
+
target.set(s.key, field);
|
|
882
|
+
merged.set(field, /* @__PURE__ */ new Set());
|
|
883
|
+
}
|
|
884
|
+
const done = merged.get(field);
|
|
885
|
+
if (s.selections && !done.has(s.selections)) {
|
|
886
|
+
done.add(s.selections);
|
|
887
|
+
field.selections = [...field.selections, ...s.selections];
|
|
888
|
+
}
|
|
889
|
+
};
|
|
890
|
+
const applies = (on, list) => conditionMatches(on, typename) || typename !== void 0 && typeConditions(list, doc).includes(typename);
|
|
891
|
+
const walk2 = (list, matched) => {
|
|
892
|
+
for (const s of list) {
|
|
893
|
+
if (s.kind === "field") {
|
|
894
|
+
add(matched ? exact : loose, s);
|
|
895
|
+
} else if (s.kind === "inline") {
|
|
896
|
+
walk2(s.selections, matched && applies(s.on, s.selections));
|
|
897
|
+
} else {
|
|
898
|
+
const fragment = doc.fragments.get(s.name);
|
|
899
|
+
if (!fragment) continue;
|
|
900
|
+
const asMatched = matched && applies(fragment.on, fragment.selections);
|
|
901
|
+
if (walkedMatched.has(fragment)) continue;
|
|
902
|
+
if (!asMatched && walkedLoose.has(fragment)) continue;
|
|
903
|
+
(asMatched ? walkedMatched : walkedLoose).add(fragment);
|
|
904
|
+
walk2(fragment.selections, asMatched);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
};
|
|
908
|
+
walk2(selections, true);
|
|
909
|
+
if (!options.strict) {
|
|
910
|
+
for (const [key, field] of loose) {
|
|
911
|
+
if (!exact.has(key)) exact.set(key, field);
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
return exact;
|
|
915
|
+
}
|
|
916
|
+
function conditionMatches(on, typename) {
|
|
917
|
+
return on === null || typename === void 0 || on === typename;
|
|
918
|
+
}
|
|
919
|
+
var conditionsCache = /* @__PURE__ */ new WeakMap();
|
|
920
|
+
function typeConditions(selections, doc) {
|
|
921
|
+
const hit = conditionsCache.get(selections);
|
|
922
|
+
if (hit) return hit;
|
|
923
|
+
const names = /* @__PURE__ */ new Set();
|
|
924
|
+
const seen = /* @__PURE__ */ new Set();
|
|
925
|
+
const walk2 = (list) => {
|
|
926
|
+
for (const s of list) {
|
|
927
|
+
if (s.kind === "inline") {
|
|
928
|
+
if (s.on) names.add(s.on);
|
|
929
|
+
walk2(s.selections);
|
|
930
|
+
} else if (s.kind === "spread") {
|
|
931
|
+
const fragment = doc.fragments.get(s.name);
|
|
932
|
+
if (!fragment || seen.has(fragment)) continue;
|
|
933
|
+
seen.add(fragment);
|
|
934
|
+
names.add(fragment.on);
|
|
935
|
+
walk2(fragment.selections);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
};
|
|
939
|
+
walk2(selections);
|
|
940
|
+
const conditions = [...names];
|
|
941
|
+
conditionsCache.set(selections, conditions);
|
|
942
|
+
return conditions;
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
// src/core/live-preview/validate.ts
|
|
946
|
+
function isScalar(v) {
|
|
947
|
+
return typeof v === "string" || typeof v === "number" || typeof v === "boolean";
|
|
948
|
+
}
|
|
949
|
+
var MAX_DEPTH = 3;
|
|
950
|
+
var MAX_ENTRIES = 50;
|
|
951
|
+
var FORBIDDEN_KEYS = /* @__PURE__ */ new Set(["__proto__", "prototype", "constructor"]);
|
|
952
|
+
var SAFE_URL = /^https?:\/\//i;
|
|
953
|
+
var URL_KEY = /^(?:url|href|src|link)$|(?:_url|Url)$/;
|
|
954
|
+
var URL_IGNORED_ANYWHERE = /[\t\n\r]/g;
|
|
955
|
+
var SCRIPT_SCHEME = /^(?:javascript|vbscript|data):/i;
|
|
956
|
+
function isSafeLinkValue(value) {
|
|
957
|
+
const stripped = value.replace(URL_IGNORED_ANYWHERE, "");
|
|
958
|
+
let start = 0;
|
|
959
|
+
while (start < stripped.length && stripped.charCodeAt(start) <= 32) start++;
|
|
960
|
+
return !SCRIPT_SCHEME.test(stripped.slice(start));
|
|
961
|
+
}
|
|
962
|
+
function isUrlKey(key) {
|
|
963
|
+
return URL_KEY.test(key);
|
|
964
|
+
}
|
|
965
|
+
function isSafeKeyValue(key, value) {
|
|
966
|
+
if (typeof value !== "string") return true;
|
|
967
|
+
if (key === "url") return SAFE_URL.test(value);
|
|
968
|
+
return !isUrlKey(key) || isSafeLinkValue(value);
|
|
969
|
+
}
|
|
970
|
+
function isLiveValue(v, depth = 0) {
|
|
971
|
+
if (v === null || isScalar(v)) return true;
|
|
972
|
+
if (depth >= MAX_DEPTH) return false;
|
|
973
|
+
if (Array.isArray(v)) {
|
|
974
|
+
return depth === 0 && v.length <= MAX_ENTRIES && v.every((entry) => isPlainRecord(entry) && isLiveValue(entry, depth + 1));
|
|
975
|
+
}
|
|
976
|
+
if (!isPlainRecord(v)) return false;
|
|
977
|
+
const keys = Object.keys(v);
|
|
978
|
+
if (keys.length > MAX_ENTRIES) return false;
|
|
979
|
+
return keys.every((key) => {
|
|
980
|
+
if (FORBIDDEN_KEYS.has(key)) return false;
|
|
981
|
+
const value = v[key];
|
|
982
|
+
if (!isSafeKeyValue(key, value)) return false;
|
|
983
|
+
return !Array.isArray(value) && isLiveValue(value, depth + 1);
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
function isLiveFieldChange(field, value) {
|
|
987
|
+
if (!isLiveValue(value)) return false;
|
|
988
|
+
const name = field.slice(field.lastIndexOf(".") + 1);
|
|
989
|
+
return typeof value !== "string" || !isUrlKey(name) || isSafeLinkValue(value);
|
|
990
|
+
}
|
|
991
|
+
var MAX_LIST_ENTRIES = 200;
|
|
992
|
+
function parseListEntries(value) {
|
|
993
|
+
if (!Array.isArray(value) || value.length > MAX_LIST_ENTRIES) return null;
|
|
994
|
+
const list = [];
|
|
995
|
+
for (const raw of value) {
|
|
996
|
+
if (!isPlainRecord(raw) || typeof raw._id !== "string" || !raw._id) {
|
|
997
|
+
return null;
|
|
998
|
+
}
|
|
999
|
+
const entry = { _id: raw._id };
|
|
1000
|
+
if (typeof raw.__typename === "string") entry.__typename = raw.__typename;
|
|
1001
|
+
if (typeof raw.body === "string") entry.body = raw.body;
|
|
1002
|
+
if (typeof raw.format === "string" || raw.format === null) {
|
|
1003
|
+
entry.format = raw.format;
|
|
1004
|
+
}
|
|
1005
|
+
if (typeof raw.typeName === "string" && raw.typeName.length <= 200) {
|
|
1006
|
+
entry.typeName = raw.typeName;
|
|
1007
|
+
}
|
|
1008
|
+
if (raw.fields !== void 0) {
|
|
1009
|
+
if (!isPlainRecord(raw.fields) || !isFieldValue(raw.fields, 0)) {
|
|
1010
|
+
return null;
|
|
1011
|
+
}
|
|
1012
|
+
entry.fields = raw.fields;
|
|
1013
|
+
}
|
|
1014
|
+
list.push(entry);
|
|
1015
|
+
}
|
|
1016
|
+
return list;
|
|
1017
|
+
}
|
|
1018
|
+
function isPlainRecord(v) {
|
|
1019
|
+
return v !== null && typeof v === "object" && Object.getPrototypeOf(v) === Object.prototype;
|
|
1020
|
+
}
|
|
1021
|
+
var MAX_FIELD_DEPTH = 6;
|
|
1022
|
+
var MAX_FIELD_KEYS = 100;
|
|
1023
|
+
function isFieldValue(v, depth) {
|
|
1024
|
+
if (v === null || isScalar(v)) return true;
|
|
1025
|
+
if (depth >= MAX_FIELD_DEPTH) return false;
|
|
1026
|
+
if (Array.isArray(v)) {
|
|
1027
|
+
return v.length <= MAX_ENTRIES && v.every(
|
|
1028
|
+
(entry) => isScalar(entry) || isPlainRecord(entry) && isFieldValue(entry, depth + 1)
|
|
1029
|
+
);
|
|
1030
|
+
}
|
|
1031
|
+
if (!isPlainRecord(v)) return false;
|
|
1032
|
+
const keys = Object.keys(v);
|
|
1033
|
+
if (keys.length > MAX_FIELD_KEYS) return false;
|
|
1034
|
+
return keys.every((key) => {
|
|
1035
|
+
if (FORBIDDEN_KEYS.has(key)) return false;
|
|
1036
|
+
const value = v[key];
|
|
1037
|
+
if (!isSafeKeyValue(key, value)) return false;
|
|
1038
|
+
return isFieldValue(value, depth + 1);
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1041
|
+
var MAX_EDIT_LENGTH = 1e3;
|
|
1042
|
+
function parseEditTarget(raw) {
|
|
1043
|
+
if (!isPlainRecord(raw)) return void 0;
|
|
1044
|
+
const { item, field } = raw;
|
|
1045
|
+
if (typeof item !== "string" || !item || item.length > MAX_EDIT_LENGTH) {
|
|
1046
|
+
return void 0;
|
|
1047
|
+
}
|
|
1048
|
+
if (typeof field !== "string" || !field || field.length > MAX_EDIT_LENGTH) {
|
|
1049
|
+
return void 0;
|
|
1050
|
+
}
|
|
1051
|
+
return { item, field };
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
// src/core/live-preview/normalize-query.ts
|
|
1055
|
+
var debug2 = createScopedLogger("live-preview:normalize");
|
|
1056
|
+
var MAX_LOG_ENTRIES = 500;
|
|
1057
|
+
var MAX_LOG_SOURCE_LENGTH = 1e6;
|
|
1058
|
+
var INDEX_SEGMENT = /^\d+$/;
|
|
1059
|
+
var FORBIDDEN_SEGMENTS2 = /* @__PURE__ */ new Set(["__proto__", "prototype", "constructor"]);
|
|
1060
|
+
var ENVELOPE_KEYS = /* @__PURE__ */ new Set(["data", "errors", "extensions"]);
|
|
1061
|
+
function parsePatchLog(raw) {
|
|
1062
|
+
let value = raw;
|
|
1063
|
+
if (typeof value === "string") {
|
|
1064
|
+
if (!value || value.length > MAX_LOG_SOURCE_LENGTH) return [];
|
|
1065
|
+
try {
|
|
1066
|
+
value = JSON.parse(value);
|
|
1067
|
+
} catch {
|
|
1068
|
+
return [];
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
if (!Array.isArray(value)) return [];
|
|
1072
|
+
const log = [];
|
|
1073
|
+
for (const entry of value.slice(-MAX_LOG_ENTRIES)) {
|
|
1074
|
+
if (!isPlainRecord(entry)) continue;
|
|
1075
|
+
const { type, itemId, field } = entry;
|
|
1076
|
+
if (typeof itemId !== "string" || !itemId) continue;
|
|
1077
|
+
if (typeof field !== "string" || !field) continue;
|
|
1078
|
+
if (type === "field" && isLiveFieldChange(field, entry.value)) {
|
|
1079
|
+
const edit = parseEditTarget(entry.edit);
|
|
1080
|
+
log.push(
|
|
1081
|
+
edit ? { type, itemId, field, value: entry.value, edit } : { type, itemId, field, value: entry.value }
|
|
1082
|
+
);
|
|
1083
|
+
} else if (type === "list") {
|
|
1084
|
+
const entries = parseListEntries(entry.entries);
|
|
1085
|
+
if (entries) log.push({ type, itemId, field, entries });
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
return log;
|
|
1089
|
+
}
|
|
1090
|
+
function normalizeQuery(query, result, options = {}) {
|
|
1091
|
+
return applyPatchLog(
|
|
1092
|
+
query,
|
|
1093
|
+
result,
|
|
1094
|
+
parsePatchLog(options.log),
|
|
1095
|
+
options.operationName
|
|
1096
|
+
);
|
|
1097
|
+
}
|
|
1098
|
+
function applyPatchLog(query, result, log, operationName) {
|
|
1099
|
+
if (!log.length || result === null || typeof result !== "object") {
|
|
1100
|
+
return result;
|
|
1101
|
+
}
|
|
1102
|
+
if (isEnvelope(result)) {
|
|
1103
|
+
const data = applyPatchLog(query, result.data, log, operationName);
|
|
1104
|
+
return data === result.data ? result : { ...result, data };
|
|
1105
|
+
}
|
|
1106
|
+
let doc = null;
|
|
1107
|
+
try {
|
|
1108
|
+
doc = readQueryDocument(query, operationName);
|
|
1109
|
+
} catch (error) {
|
|
1110
|
+
debug2.log("could not read the query; applying fields by literal path", {
|
|
1111
|
+
error: String(error)
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
let root = result;
|
|
1115
|
+
let index = null;
|
|
1116
|
+
for (const patch of log) {
|
|
1117
|
+
try {
|
|
1118
|
+
index ?? (index = indexSnapshot(root));
|
|
1119
|
+
const next = applyPatch(root, index, patch, doc, result);
|
|
1120
|
+
if (next !== root) {
|
|
1121
|
+
root = next;
|
|
1122
|
+
if (patch.type === "list" || !isScalarValue(patch.value)) {
|
|
1123
|
+
index = null;
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
} catch (error) {
|
|
1127
|
+
debug2.log("could not apply a change", {
|
|
1128
|
+
itemId: patch.itemId,
|
|
1129
|
+
field: patch.field,
|
|
1130
|
+
error: String(error)
|
|
1131
|
+
});
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
return root;
|
|
1135
|
+
}
|
|
1136
|
+
function isScalarValue(value) {
|
|
1137
|
+
return value === null || typeof value !== "object";
|
|
1138
|
+
}
|
|
1139
|
+
function applyPatch(root, index, patch, doc, result) {
|
|
1140
|
+
const ownerPaths = index.get(patch.itemId);
|
|
1141
|
+
if (!ownerPaths?.length) return root;
|
|
1142
|
+
const segments = patch.field.split(".");
|
|
1143
|
+
if (segments.some((s) => !s || FORBIDDEN_SEGMENTS2.has(s))) return root;
|
|
1144
|
+
const targets = [];
|
|
1145
|
+
const plain = /* @__PURE__ */ new Set();
|
|
1146
|
+
for (const ownerPath of ownerPaths) {
|
|
1147
|
+
if (doc) {
|
|
1148
|
+
const resolved = resolveTargets(root, doc, ownerPath, segments);
|
|
1149
|
+
root = resolved.root;
|
|
1150
|
+
targets.push(...resolved.targets);
|
|
1151
|
+
resolved.plain.forEach((target) => plain.add(target));
|
|
1152
|
+
} else {
|
|
1153
|
+
targets.push([...ownerPath, ...segments.map(toPathSegment)]);
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
if (!targets.length) {
|
|
1157
|
+
debug2.log("no field in the query matches", {
|
|
1158
|
+
itemId: patch.itemId,
|
|
1159
|
+
field: patch.field
|
|
1160
|
+
});
|
|
1161
|
+
return root;
|
|
1162
|
+
}
|
|
1163
|
+
if (patch.type === "field") {
|
|
1164
|
+
for (const target of targets) {
|
|
1165
|
+
const pushed = plain.has(target) && typeof patch.value === "string" ? plainTextFromHtml(patch.value) : patch.value;
|
|
1166
|
+
let value = keepEditLink(readAtPath(root, target), pushed);
|
|
1167
|
+
if (patch.edit && typeof value === "string" && !vercelStegaSplit2(value).encoded) {
|
|
1168
|
+
value = editLinksFor(result, patch.edit.item)?.encode(
|
|
1169
|
+
value,
|
|
1170
|
+
patch.edit.field
|
|
1171
|
+
) ?? value;
|
|
1172
|
+
}
|
|
1173
|
+
root = writeAtPath(root, target, value);
|
|
1174
|
+
}
|
|
1175
|
+
} else {
|
|
1176
|
+
for (const target of targets) {
|
|
1177
|
+
const selections = doc ? selectionsAt(root, doc, target) : null;
|
|
1178
|
+
const build = doc && selections ? (entry) => buildEntry(doc, selections, entry, {
|
|
1179
|
+
links: editLinks(result, entry),
|
|
1180
|
+
lists: listKeysIn(result)
|
|
1181
|
+
}) : void 0;
|
|
1182
|
+
root = syncListAtPath(root, target, patch.entries, build);
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
return root;
|
|
1186
|
+
}
|
|
1187
|
+
function readAtPath(root, path) {
|
|
1188
|
+
let node = root;
|
|
1189
|
+
for (const step of path) {
|
|
1190
|
+
if (node === null || typeof node !== "object") return void 0;
|
|
1191
|
+
node = node[step];
|
|
1192
|
+
}
|
|
1193
|
+
return node;
|
|
1194
|
+
}
|
|
1195
|
+
function isEnvelope(value) {
|
|
1196
|
+
if (Array.isArray(value) || !("data" in value)) return false;
|
|
1197
|
+
return Object.keys(value).every((key) => ENVELOPE_KEYS.has(key));
|
|
1198
|
+
}
|
|
1199
|
+
function toPathSegment(segment) {
|
|
1200
|
+
return INDEX_SEGMENT.test(segment) ? Number(segment) : segment;
|
|
1201
|
+
}
|
|
1202
|
+
function typenameOf(node) {
|
|
1203
|
+
const typename = node.__typename;
|
|
1204
|
+
return typeof typename === "string" ? typename : void 0;
|
|
1205
|
+
}
|
|
1206
|
+
function selectionsAt(root, doc, path) {
|
|
1207
|
+
let node = root;
|
|
1208
|
+
let selections = doc.operation;
|
|
1209
|
+
for (const step of path) {
|
|
1210
|
+
if (Array.isArray(node)) {
|
|
1211
|
+
node = node[step];
|
|
1212
|
+
continue;
|
|
1213
|
+
}
|
|
1214
|
+
if (node === null || typeof node !== "object") return null;
|
|
1215
|
+
const field = collectFields(selections, typenameOf(node), doc).get(
|
|
1216
|
+
String(step)
|
|
1217
|
+
);
|
|
1218
|
+
if (!field) return null;
|
|
1219
|
+
selections = field.selections;
|
|
1220
|
+
node = node[String(step)];
|
|
1221
|
+
}
|
|
1222
|
+
return selections;
|
|
1223
|
+
}
|
|
1224
|
+
function resolveTargets(root, doc, ownerPath, segments) {
|
|
1225
|
+
let owner = root;
|
|
1226
|
+
for (const step of ownerPath) {
|
|
1227
|
+
owner = owner[step];
|
|
1228
|
+
}
|
|
1229
|
+
const selections = selectionsAt(root, doc, ownerPath);
|
|
1230
|
+
if (!selections) return { root, targets: [], plain: /* @__PURE__ */ new Set() };
|
|
1231
|
+
let current = root;
|
|
1232
|
+
const out = [];
|
|
1233
|
+
const plain = /* @__PURE__ */ new Set();
|
|
1234
|
+
const walk2 = (node, sel, i, path) => {
|
|
1235
|
+
if (i === segments.length) {
|
|
1236
|
+
out.push(path);
|
|
1237
|
+
return;
|
|
1238
|
+
}
|
|
1239
|
+
const segment = segments[i];
|
|
1240
|
+
if (Array.isArray(node)) {
|
|
1241
|
+
if (INDEX_SEGMENT.test(segment)) {
|
|
1242
|
+
const position = Number(segment);
|
|
1243
|
+
if (position < node.length) {
|
|
1244
|
+
walk2(node[position], sel, i + 1, [...path, position]);
|
|
1245
|
+
}
|
|
1246
|
+
} else if (segment === "items") {
|
|
1247
|
+
walk2(node, sel, i + 1, path);
|
|
1248
|
+
}
|
|
1249
|
+
return;
|
|
1250
|
+
}
|
|
1251
|
+
if (node === null || typeof node !== "object") return;
|
|
1252
|
+
const record = node;
|
|
1253
|
+
const wanted = graphqlFieldName(segment);
|
|
1254
|
+
const textBody = i === segments.length - 1 && segment === "body" && isTextEntry(record) ? TEXT_BODY_FIELDS : null;
|
|
1255
|
+
let matched = false;
|
|
1256
|
+
for (const field of collectFields(sel, typenameOf(node), doc).values()) {
|
|
1257
|
+
const alias = textBody?.find((f) => f.name === field.name);
|
|
1258
|
+
if (field.name !== segment && field.name !== wanted && !alias) continue;
|
|
1259
|
+
if (!Object.prototype.hasOwnProperty.call(record, field.key)) continue;
|
|
1260
|
+
matched = true;
|
|
1261
|
+
const fieldPath = [...path, field.key];
|
|
1262
|
+
if (alias?.plain) plain.add(fieldPath);
|
|
1263
|
+
let value = record[field.key];
|
|
1264
|
+
const next = segments[i + 1];
|
|
1265
|
+
if (value === null && field.selections.length && next !== void 0 && next !== "items" && !INDEX_SEGMENT.test(next)) {
|
|
1266
|
+
const typename = resolveTypename(
|
|
1267
|
+
doc,
|
|
1268
|
+
field.selections,
|
|
1269
|
+
void 0,
|
|
1270
|
+
void 0
|
|
1271
|
+
);
|
|
1272
|
+
if (typename !== void 0) {
|
|
1273
|
+
value = fillObject(doc, field.selections, typename, {}, void 0, {
|
|
1274
|
+
links: null,
|
|
1275
|
+
lists: listKeysIn(root)
|
|
1276
|
+
});
|
|
1277
|
+
current = writeAtPath(current, fieldPath, value);
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
walk2(value, field.selections, i + 1, fieldPath);
|
|
1281
|
+
}
|
|
1282
|
+
if (!matched && segment === "items") walk2(node, sel, i + 1, path);
|
|
1283
|
+
};
|
|
1284
|
+
walk2(owner, selections, 0, ownerPath);
|
|
1285
|
+
return { root: current, targets: out, plain };
|
|
1286
|
+
}
|
|
1287
|
+
var NON_ALPHANUMERIC = /[^a-z0-9]/gi;
|
|
1288
|
+
var typeKey = (name) => name.replace(NON_ALPHANUMERIC, "").toLowerCase();
|
|
1289
|
+
function resolveTypename(doc, selections, typename, typeName) {
|
|
1290
|
+
const known = typeConditions(selections, doc);
|
|
1291
|
+
const explicit = typeof typename === "string" ? typename : void 0;
|
|
1292
|
+
if (!known.length) return explicit ?? null;
|
|
1293
|
+
if (explicit && known.includes(explicit)) return explicit;
|
|
1294
|
+
for (const candidate of [explicit, typeName]) {
|
|
1295
|
+
if (typeof candidate !== "string" || !candidate) continue;
|
|
1296
|
+
const match = known.find((name) => typeKey(name) === typeKey(candidate));
|
|
1297
|
+
if (match) return match;
|
|
1298
|
+
}
|
|
1299
|
+
const named = explicit !== void 0 || typeof typeName === "string";
|
|
1300
|
+
return known.length === 1 && !named ? known[0] : void 0;
|
|
1301
|
+
}
|
|
1302
|
+
function buildEntry(doc, selections, entry, ctx) {
|
|
1303
|
+
const typename = resolveTypename(
|
|
1304
|
+
doc,
|
|
1305
|
+
selections,
|
|
1306
|
+
entry.__typename,
|
|
1307
|
+
entry.typeName
|
|
1308
|
+
);
|
|
1309
|
+
if (typename === void 0) return void 0;
|
|
1310
|
+
return fillObject(
|
|
1311
|
+
doc,
|
|
1312
|
+
selections,
|
|
1313
|
+
typename,
|
|
1314
|
+
entry.fields ?? {},
|
|
1315
|
+
entry._id,
|
|
1316
|
+
ctx
|
|
1317
|
+
);
|
|
1318
|
+
}
|
|
1319
|
+
function fillObject(doc, selections, typename, values, id, ctx) {
|
|
1320
|
+
const out = {};
|
|
1321
|
+
const { links } = ctx;
|
|
1322
|
+
const lists = ctx.lists(typename);
|
|
1323
|
+
const edit = isPlainRecord(values.__edit) ? values.__edit : null;
|
|
1324
|
+
const apiIdOf = /* @__PURE__ */ new Map();
|
|
1325
|
+
for (const key of Object.keys(values)) {
|
|
1326
|
+
apiIdOf.set(key, key);
|
|
1327
|
+
const name = graphqlFieldName(key);
|
|
1328
|
+
if (!apiIdOf.has(name)) apiIdOf.set(name, key);
|
|
1329
|
+
}
|
|
1330
|
+
if (edit && isPlainRecord(edit.apiIds)) {
|
|
1331
|
+
for (const [name, apiId] of Object.entries(edit.apiIds)) {
|
|
1332
|
+
if (typeof apiId === "string") apiIdOf.set(name, apiId);
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
const valueOf = (name) => {
|
|
1336
|
+
if (Object.prototype.hasOwnProperty.call(values, name)) return values[name];
|
|
1337
|
+
const key = apiIdOf.get(name);
|
|
1338
|
+
return key !== void 0 && key in values ? values[key] : void 0;
|
|
1339
|
+
};
|
|
1340
|
+
const editField = (name) => {
|
|
1341
|
+
if (!edit) return null;
|
|
1342
|
+
if (typeof edit.field === "string")
|
|
1343
|
+
return name === "body" ? edit.field : null;
|
|
1344
|
+
if (typeof edit.path !== "string") return null;
|
|
1345
|
+
const apiId = apiIdOf.get(name) ?? name;
|
|
1346
|
+
const text = Array.isArray(edit.text) ? edit.text : [];
|
|
1347
|
+
if (!text.includes(apiId) && !text.includes(name)) return null;
|
|
1348
|
+
return `${edit.path}.items.${apiId}`;
|
|
1349
|
+
};
|
|
1350
|
+
const fields = collectFields(selections, typename ?? void 0, doc, {
|
|
1351
|
+
strict: true
|
|
1352
|
+
});
|
|
1353
|
+
for (const field of fields.values()) {
|
|
1354
|
+
if (field.name === "__typename") {
|
|
1355
|
+
out[field.key] = typename;
|
|
1356
|
+
} else if (field.name === "_id") {
|
|
1357
|
+
out[field.key] = typeof id === "string" ? id : values._id ?? null;
|
|
1358
|
+
} else if (field.selections.length) {
|
|
1359
|
+
const value = valueOf(field.name);
|
|
1360
|
+
out[field.key] = value == null && lists.has(field.key) ? [] : fillNested(doc, field.selections, value, ctx);
|
|
1361
|
+
} else {
|
|
1362
|
+
const value = valueOf(field.name);
|
|
1363
|
+
if (Array.isArray(value) && value.every(isScalarItem)) {
|
|
1364
|
+
out[field.key] = value;
|
|
1365
|
+
continue;
|
|
1366
|
+
}
|
|
1367
|
+
if (value == null && lists.has(field.key)) {
|
|
1368
|
+
out[field.key] = [];
|
|
1369
|
+
continue;
|
|
1370
|
+
}
|
|
1371
|
+
const scalar = value === void 0 || value !== null && typeof value === "object" ? null : value;
|
|
1372
|
+
const target = typeof scalar === "string" ? editField(field.name) : null;
|
|
1373
|
+
out[field.key] = target && links ? links.encode(scalar, target) : scalar;
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
return out;
|
|
1377
|
+
}
|
|
1378
|
+
function fillNested(doc, selections, value, ctx) {
|
|
1379
|
+
const one = (item) => {
|
|
1380
|
+
if (!isPlainRecord(item)) return null;
|
|
1381
|
+
const typename = resolveTypename(
|
|
1382
|
+
doc,
|
|
1383
|
+
selections,
|
|
1384
|
+
item.__typename,
|
|
1385
|
+
item.typeName
|
|
1386
|
+
);
|
|
1387
|
+
if (typename === void 0) return null;
|
|
1388
|
+
return fillObject(doc, selections, typename, item, item._id, ctx);
|
|
1389
|
+
};
|
|
1390
|
+
if (Array.isArray(value)) return value.map(one).filter((v) => v !== null);
|
|
1391
|
+
return value === void 0 ? null : one(value);
|
|
1392
|
+
}
|
|
1393
|
+
function isScalarItem(value) {
|
|
1394
|
+
return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
|
|
1395
|
+
}
|
|
1396
|
+
var NO_KEYS = /* @__PURE__ */ new Set();
|
|
1397
|
+
var listKeysCache = /* @__PURE__ */ new WeakMap();
|
|
1398
|
+
function listKeysIn(result) {
|
|
1399
|
+
return (typename) => {
|
|
1400
|
+
if (!typename || result === null || typeof result !== "object") {
|
|
1401
|
+
return NO_KEYS;
|
|
1402
|
+
}
|
|
1403
|
+
let byType = listKeysCache.get(result);
|
|
1404
|
+
if (!byType) {
|
|
1405
|
+
byType = /* @__PURE__ */ new Map();
|
|
1406
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
1407
|
+
const walk2 = (node) => {
|
|
1408
|
+
if (node === null || typeof node !== "object" || seen.has(node)) return;
|
|
1409
|
+
seen.add(node);
|
|
1410
|
+
if (Array.isArray(node)) {
|
|
1411
|
+
node.forEach(walk2);
|
|
1412
|
+
return;
|
|
1413
|
+
}
|
|
1414
|
+
const record = node;
|
|
1415
|
+
const type = typeof record.__typename === "string" ? record.__typename : null;
|
|
1416
|
+
for (const [key, value] of Object.entries(record)) {
|
|
1417
|
+
if (type && Array.isArray(value)) {
|
|
1418
|
+
let keys = byType.get(type);
|
|
1419
|
+
if (!keys) byType.set(type, keys = /* @__PURE__ */ new Set());
|
|
1420
|
+
keys.add(key);
|
|
1421
|
+
}
|
|
1422
|
+
walk2(value);
|
|
1423
|
+
}
|
|
1424
|
+
};
|
|
1425
|
+
walk2(result);
|
|
1426
|
+
listKeysCache.set(result, byType);
|
|
1427
|
+
}
|
|
1428
|
+
return byType.get(typename) ?? NO_KEYS;
|
|
1429
|
+
};
|
|
1430
|
+
}
|
|
1431
|
+
var EDIT_PATH = /\/content\/edit\/[^/?#]+/;
|
|
1432
|
+
function editLinks(result, entry) {
|
|
1433
|
+
const edit = entry.fields?.__edit;
|
|
1434
|
+
if (!isPlainRecord(edit) || typeof edit.item !== "string") return null;
|
|
1435
|
+
return editLinksFor(result, edit.item);
|
|
1436
|
+
}
|
|
1437
|
+
var ITEM_ID = /^[A-Za-z0-9_-]+$/;
|
|
1438
|
+
function editLinksFor(result, item) {
|
|
1439
|
+
if (!ITEM_ID.test(item)) return null;
|
|
1440
|
+
const template = findEditLink(result);
|
|
1441
|
+
if (!template) return null;
|
|
1442
|
+
return {
|
|
1443
|
+
encode(text, field) {
|
|
1444
|
+
if (vercelStegaSplit2(text).encoded) return text;
|
|
1445
|
+
let href;
|
|
1446
|
+
try {
|
|
1447
|
+
href = new URL(template.href);
|
|
1448
|
+
} catch {
|
|
1449
|
+
return text;
|
|
1450
|
+
}
|
|
1451
|
+
href.pathname = href.pathname.replace(
|
|
1452
|
+
EDIT_PATH,
|
|
1453
|
+
() => `/content/edit/${item}`
|
|
1454
|
+
);
|
|
1455
|
+
href.searchParams.set("field", field);
|
|
1456
|
+
const link = href.toString().replace(/%2E/gi, ".").replace(/%2F/gi, "/");
|
|
1457
|
+
return vercelStegaCombine(text, { origin: template.origin, href: link });
|
|
1458
|
+
}
|
|
1459
|
+
};
|
|
1460
|
+
}
|
|
1461
|
+
var linkCache = /* @__PURE__ */ new WeakMap();
|
|
1462
|
+
function findEditLink(root) {
|
|
1463
|
+
if (root === null || typeof root !== "object") return null;
|
|
1464
|
+
if (linkCache.has(root)) return linkCache.get(root) ?? null;
|
|
1465
|
+
let found = null;
|
|
1466
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
1467
|
+
const walk2 = (node) => {
|
|
1468
|
+
if (found) return;
|
|
1469
|
+
if (typeof node === "string") {
|
|
1470
|
+
if (!vercelStegaSplit2(node).encoded) return;
|
|
1471
|
+
try {
|
|
1472
|
+
const decoded = vercelStegaDecode(node);
|
|
1473
|
+
if (typeof decoded?.origin === "string" && typeof decoded.href === "string" && EDIT_PATH.test(decoded.href)) {
|
|
1474
|
+
found = { origin: decoded.origin, href: decoded.href };
|
|
1475
|
+
}
|
|
1476
|
+
} catch {
|
|
1477
|
+
}
|
|
1478
|
+
return;
|
|
1479
|
+
}
|
|
1480
|
+
if (node === null || typeof node !== "object" || seen.has(node)) return;
|
|
1481
|
+
seen.add(node);
|
|
1482
|
+
for (const value of Object.values(node)) walk2(value);
|
|
1483
|
+
};
|
|
1484
|
+
walk2(root);
|
|
1485
|
+
linkCache.set(root, found);
|
|
1486
|
+
return found;
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
// src/core/live-preview/live-request.ts
|
|
1490
|
+
var MAX_BODY_BYTES = 1e6;
|
|
1491
|
+
async function readLiveLogFromRequest(request) {
|
|
1492
|
+
try {
|
|
1493
|
+
if (request.method !== "POST") return [];
|
|
1494
|
+
if (request.headers.get(HEADER_LIVE_RENDER) !== "1") return [];
|
|
1495
|
+
const origin = request.headers.get("origin");
|
|
1496
|
+
if (!origin || origin !== new URL(request.url).origin) return [];
|
|
1497
|
+
const type = request.headers.get("content-type") ?? "";
|
|
1498
|
+
if (!type.toLowerCase().startsWith("application/json")) return [];
|
|
1499
|
+
const length = Number(request.headers.get("content-length") ?? "0");
|
|
1500
|
+
if (length > MAX_BODY_BYTES) return [];
|
|
1501
|
+
const text = await request.clone().text();
|
|
1502
|
+
if (text.length > MAX_BODY_BYTES) return [];
|
|
1503
|
+
const body = JSON.parse(text);
|
|
1504
|
+
return parsePatchLog(body?.log);
|
|
1505
|
+
} catch {
|
|
1506
|
+
return [];
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
// src/core/live-preview/edit-links.ts
|
|
1511
|
+
import {
|
|
1512
|
+
vercelStegaCombine as vercelStegaCombine2,
|
|
1513
|
+
vercelStegaDecode as vercelStegaDecode2,
|
|
1514
|
+
vercelStegaSplit as vercelStegaSplit3
|
|
1515
|
+
} from "@vercel/stega";
|
|
1516
|
+
var ENTRY_PARAM = "entry";
|
|
1517
|
+
var TOP_LEVEL_ENTRY = /^[A-Za-z0-9_]+\.\d+$/;
|
|
1518
|
+
function entryIdOf(element) {
|
|
1519
|
+
if (!isRecord(element)) return void 0;
|
|
1520
|
+
if (typeof element._id === "string") return element._id;
|
|
1521
|
+
if (element.__typename !== "Assets" || !Array.isArray(element.items)) {
|
|
1522
|
+
return void 0;
|
|
1523
|
+
}
|
|
1524
|
+
const first = element.items[0];
|
|
1525
|
+
return isRecord(first) && typeof first._id === "string" ? first._id : void 0;
|
|
1526
|
+
}
|
|
1527
|
+
function isRecord(value) {
|
|
1528
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1529
|
+
}
|
|
1530
|
+
function annotate(value, ctx) {
|
|
1531
|
+
if (!ctx.entryId) return value;
|
|
1532
|
+
const { cleaned, encoded } = vercelStegaSplit3(value);
|
|
1533
|
+
if (!encoded) return value;
|
|
1534
|
+
let payload;
|
|
1535
|
+
try {
|
|
1536
|
+
payload = vercelStegaDecode2(encoded);
|
|
1537
|
+
} catch {
|
|
1538
|
+
return value;
|
|
1539
|
+
}
|
|
1540
|
+
if (typeof payload?.href !== "string") return value;
|
|
1541
|
+
let url;
|
|
1542
|
+
try {
|
|
1543
|
+
url = new URL(payload.href);
|
|
1544
|
+
} catch {
|
|
1545
|
+
return value;
|
|
1546
|
+
}
|
|
1547
|
+
if (url.searchParams.has(ENTRY_PARAM)) return value;
|
|
1548
|
+
url.searchParams.set(ENTRY_PARAM, ctx.entryId);
|
|
1549
|
+
const itemId = decodeURIComponent(url.pathname.split("/").pop() ?? "");
|
|
1550
|
+
if (!url.searchParams.has("field") && ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) && itemId === ctx.entryOwnerId) {
|
|
1551
|
+
url.searchParams.set("field", ctx.entryField);
|
|
1552
|
+
}
|
|
1553
|
+
return vercelStegaCombine2(
|
|
1554
|
+
cleaned,
|
|
1555
|
+
{ ...payload, href: url.toString() },
|
|
1556
|
+
false
|
|
1557
|
+
);
|
|
1558
|
+
}
|
|
1559
|
+
function walk(value, selections, ctx) {
|
|
1560
|
+
if (typeof value === "string") return annotate(value, ctx);
|
|
1561
|
+
if (Array.isArray(value)) {
|
|
1562
|
+
let changed2 = false;
|
|
1563
|
+
const next2 = value.map((element, index) => {
|
|
1564
|
+
const path = [...ctx.path, String(index)];
|
|
1565
|
+
const entryId = ctx.inAssets ? void 0 : entryIdOf(element);
|
|
1566
|
+
const entry = entryId ? {
|
|
1567
|
+
entryId,
|
|
1568
|
+
entryField: ctx.ownerId ? path.join(".") : void 0,
|
|
1569
|
+
entryOwnerId: ctx.ownerId,
|
|
1570
|
+
inAssets: isRecord(element) && element._id === void 0
|
|
1571
|
+
} : {};
|
|
1572
|
+
const out = walk(element, selections, { ...ctx, ...entry, path });
|
|
1573
|
+
if (out !== element) changed2 = true;
|
|
1574
|
+
return out;
|
|
1575
|
+
});
|
|
1576
|
+
return changed2 ? next2 : value;
|
|
1577
|
+
}
|
|
1578
|
+
if (!isRecord(value)) return value;
|
|
1579
|
+
const id = typeof value._id === "string" ? value._id : void 0;
|
|
1580
|
+
const typename = typeof value.__typename === "string" ? value.__typename : void 0;
|
|
1581
|
+
const fields = ctx.doc && selections ? collectFields(selections, typename, ctx.doc) : null;
|
|
1582
|
+
let changed = false;
|
|
1583
|
+
const next = {};
|
|
1584
|
+
for (const [key, child] of Object.entries(value)) {
|
|
1585
|
+
const field = fields?.get(key);
|
|
1586
|
+
const name = field?.name ?? key;
|
|
1587
|
+
const out = walk(child, field?.selections ?? null, {
|
|
1588
|
+
...ctx,
|
|
1589
|
+
ownerId: id ?? ctx.ownerId,
|
|
1590
|
+
path: id ? [name] : [...ctx.path, name]
|
|
1591
|
+
});
|
|
1592
|
+
if (out !== child) changed = true;
|
|
1593
|
+
next[key] = out;
|
|
1594
|
+
}
|
|
1595
|
+
return changed ? next : value;
|
|
1596
|
+
}
|
|
1597
|
+
function annotateEditLinks(result, query, operationName) {
|
|
1598
|
+
let doc = null;
|
|
1599
|
+
if (query !== void 0) {
|
|
1600
|
+
try {
|
|
1601
|
+
doc = readQueryDocument(query, operationName);
|
|
1602
|
+
} catch {
|
|
1603
|
+
doc = null;
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
return walk(result, doc?.operation ?? null, { doc, path: [] });
|
|
1607
|
+
}
|
|
1608
|
+
|
|
332
1609
|
// src/astro/index.ts
|
|
1610
|
+
var LIVE_LOG = /* @__PURE__ */ Symbol.for("@preprio/toolkit/astro-live-log");
|
|
333
1611
|
async function onPreprRequest(context, next, options) {
|
|
334
1612
|
const { requestHeaders, responseCookies } = processFrameworkRequest(
|
|
335
1613
|
context.request,
|
|
@@ -339,7 +1617,14 @@ async function onPreprRequest(context, next, options) {
|
|
|
339
1617
|
requestHeaders.forEach((value, key) => {
|
|
340
1618
|
context.request.headers.set(key, value);
|
|
341
1619
|
});
|
|
1620
|
+
const log = options?.preview && context.locals ? await readLiveLogFromRequest(context.request) : [];
|
|
1621
|
+
if (log.length && context.locals) {
|
|
1622
|
+
context.locals[LIVE_LOG] = log;
|
|
1623
|
+
}
|
|
342
1624
|
const response = await next();
|
|
1625
|
+
if (log.length) {
|
|
1626
|
+
response.headers.set("Cache-Control", "private, no-store");
|
|
1627
|
+
}
|
|
343
1628
|
for (const cookie of responseCookies) {
|
|
344
1629
|
response.headers.append("Set-Cookie", serializeCookie(cookie));
|
|
345
1630
|
}
|
|
@@ -360,11 +1645,22 @@ function getPreprHeaders(headers) {
|
|
|
360
1645
|
async function getToolbarProps(headers, token, features) {
|
|
361
1646
|
return getToolbarPropsFromHeaders(headers, token, features);
|
|
362
1647
|
}
|
|
1648
|
+
function normalizeQuery2(query, result, options = {}) {
|
|
1649
|
+
if (!options.enabled) return result;
|
|
1650
|
+
const annotated = annotateEditLinks(result, query, options.operationName);
|
|
1651
|
+
const log = options.locals?.[LIVE_LOG];
|
|
1652
|
+
if (!log?.length) return annotated;
|
|
1653
|
+
return normalizeQuery(query, annotated, {
|
|
1654
|
+
log,
|
|
1655
|
+
operationName: options.operationName
|
|
1656
|
+
});
|
|
1657
|
+
}
|
|
363
1658
|
export {
|
|
364
1659
|
getActiveSegment,
|
|
365
1660
|
getActiveVariant,
|
|
366
1661
|
getPreprHeaders,
|
|
367
1662
|
getPreprUUID,
|
|
368
1663
|
getToolbarProps,
|
|
1664
|
+
normalizeQuery2 as normalizeQuery,
|
|
369
1665
|
onPreprRequest
|
|
370
1666
|
};
|