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