@preprio/toolkit 0.5.0-rc.4 → 0.5.0-rc.6
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 +337 -165
- package/dist/astro/index.cjs +126 -30
- package/dist/astro/index.js +130 -30
- package/dist/{chunk-TG6N3C22.js → chunk-IPMB32AZ.js} +1 -1
- package/dist/{chunk-74KY5EAM.js → chunk-KD3GK6TO.js} +2 -0
- package/dist/{chunk-AHBN6FQP.js → chunk-KZIRM54F.js} +1 -1
- package/dist/{chunk-PDNCNTDB.js → chunk-L3SDL6MP.js} +11 -3
- package/dist/{chunk-46ISDRHT.js → chunk-WHZNCYUS.js} +1 -1
- package/dist/codegen/index.cjs +79 -0
- package/dist/codegen/index.d.cts +46 -0
- package/dist/codegen/index.d.ts +46 -0
- package/dist/codegen/index.js +62 -0
- package/dist/index.cjs +133 -28
- package/dist/index.d.cts +14 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.js +108 -6
- package/dist/{live-preview-H4643QYF.js → live-preview-DLXGXRE5.js} +2 -2
- package/dist/nextjs/components.cjs +11 -3
- package/dist/nextjs/components.js +2 -2
- package/dist/nextjs/index.cjs +124 -28
- package/dist/nextjs/index.d.cts +2 -0
- package/dist/nextjs/index.d.ts +2 -0
- package/dist/nextjs/index.js +128 -28
- package/dist/nextjs/live-action.js +3 -3
- package/dist/nextjs/live.cjs +4 -1
- package/dist/nextjs/live.d.cts +3 -1
- package/dist/nextjs/live.d.ts +3 -1
- package/dist/nextjs/live.js +5 -2
- 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-O2ZTJNW4.js → live-preview-2RCU35W5.js} +6 -337
- package/dist/nuxt/live.cjs +1007 -911
- package/dist/nuxt/live.js +109 -5
- package/dist/react/components.cjs +11 -3
- package/dist/react/components.js +11 -3
- 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 +16 -1
package/dist/nuxt/live.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
collectFields,
|
|
3
|
+
readQueryDocument
|
|
4
|
+
} from "./chunk-BWPGO25E.js";
|
|
5
|
+
|
|
1
6
|
// src/nuxt/live.ts
|
|
2
7
|
import {
|
|
3
8
|
computed,
|
|
@@ -7,6 +12,105 @@ import {
|
|
|
7
12
|
unref
|
|
8
13
|
} from "vue";
|
|
9
14
|
|
|
15
|
+
// src/core/live-preview/edit-links.ts
|
|
16
|
+
import {
|
|
17
|
+
vercelStegaCombine,
|
|
18
|
+
vercelStegaDecode,
|
|
19
|
+
vercelStegaSplit
|
|
20
|
+
} from "@vercel/stega";
|
|
21
|
+
var ENTRY_PARAM = "entry";
|
|
22
|
+
var TOP_LEVEL_ENTRY = /^[A-Za-z0-9_]+\.\d+$/;
|
|
23
|
+
function entryIdOf(element) {
|
|
24
|
+
if (!isRecord(element)) return void 0;
|
|
25
|
+
if (typeof element._id === "string") return element._id;
|
|
26
|
+
if (element.__typename !== "Assets" || !Array.isArray(element.items)) {
|
|
27
|
+
return void 0;
|
|
28
|
+
}
|
|
29
|
+
const first = element.items[0];
|
|
30
|
+
return isRecord(first) && typeof first._id === "string" ? first._id : void 0;
|
|
31
|
+
}
|
|
32
|
+
function isRecord(value) {
|
|
33
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
34
|
+
}
|
|
35
|
+
function annotate(value, ctx) {
|
|
36
|
+
if (!ctx.entryId) return value;
|
|
37
|
+
const { cleaned, encoded } = vercelStegaSplit(value);
|
|
38
|
+
if (!encoded) return value;
|
|
39
|
+
let payload;
|
|
40
|
+
try {
|
|
41
|
+
payload = vercelStegaDecode(encoded);
|
|
42
|
+
} catch {
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
if (typeof payload?.href !== "string") return value;
|
|
46
|
+
let url;
|
|
47
|
+
try {
|
|
48
|
+
url = new URL(payload.href);
|
|
49
|
+
} catch {
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
if (url.searchParams.has(ENTRY_PARAM)) return value;
|
|
53
|
+
url.searchParams.set(ENTRY_PARAM, ctx.entryId);
|
|
54
|
+
const itemId = decodeURIComponent(url.pathname.split("/").pop() ?? "");
|
|
55
|
+
if (!url.searchParams.has("field") && ctx.entryField && TOP_LEVEL_ENTRY.test(ctx.entryField) && itemId === ctx.entryOwnerId) {
|
|
56
|
+
url.searchParams.set("field", ctx.entryField);
|
|
57
|
+
}
|
|
58
|
+
return vercelStegaCombine(
|
|
59
|
+
cleaned,
|
|
60
|
+
{ ...payload, href: url.toString() },
|
|
61
|
+
false
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
function walk(value, selections, ctx) {
|
|
65
|
+
if (typeof value === "string") return annotate(value, ctx);
|
|
66
|
+
if (Array.isArray(value)) {
|
|
67
|
+
let changed2 = false;
|
|
68
|
+
const next2 = value.map((element, index) => {
|
|
69
|
+
const path = [...ctx.path, String(index)];
|
|
70
|
+
const entryId = ctx.inAssets ? void 0 : entryIdOf(element);
|
|
71
|
+
const entry = entryId ? {
|
|
72
|
+
entryId,
|
|
73
|
+
entryField: ctx.ownerId ? path.join(".") : void 0,
|
|
74
|
+
entryOwnerId: ctx.ownerId,
|
|
75
|
+
inAssets: isRecord(element) && element._id === void 0
|
|
76
|
+
} : {};
|
|
77
|
+
const out = walk(element, selections, { ...ctx, ...entry, path });
|
|
78
|
+
if (out !== element) changed2 = true;
|
|
79
|
+
return out;
|
|
80
|
+
});
|
|
81
|
+
return changed2 ? next2 : value;
|
|
82
|
+
}
|
|
83
|
+
if (!isRecord(value)) return value;
|
|
84
|
+
const id = typeof value._id === "string" ? value._id : void 0;
|
|
85
|
+
const typename = typeof value.__typename === "string" ? value.__typename : void 0;
|
|
86
|
+
const fields = ctx.doc && selections ? collectFields(selections, typename, ctx.doc) : null;
|
|
87
|
+
let changed = false;
|
|
88
|
+
const next = {};
|
|
89
|
+
for (const [key, child] of Object.entries(value)) {
|
|
90
|
+
const field = fields?.get(key);
|
|
91
|
+
const name = field?.name ?? key;
|
|
92
|
+
const out = walk(child, field?.selections ?? null, {
|
|
93
|
+
...ctx,
|
|
94
|
+
ownerId: id ?? ctx.ownerId,
|
|
95
|
+
path: id ? [name] : [...ctx.path, name]
|
|
96
|
+
});
|
|
97
|
+
if (out !== child) changed = true;
|
|
98
|
+
next[key] = out;
|
|
99
|
+
}
|
|
100
|
+
return changed ? next : value;
|
|
101
|
+
}
|
|
102
|
+
function annotateEditLinks(result, query, operationName) {
|
|
103
|
+
let doc = null;
|
|
104
|
+
if (query !== void 0) {
|
|
105
|
+
try {
|
|
106
|
+
doc = readQueryDocument(query, operationName);
|
|
107
|
+
} catch {
|
|
108
|
+
doc = null;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return walk(result, doc?.operation ?? null, { doc, path: [] });
|
|
112
|
+
}
|
|
113
|
+
|
|
10
114
|
// src/core/live-preview/test-reset.ts
|
|
11
115
|
var resets = /* @__PURE__ */ new Set();
|
|
12
116
|
function onTestReset(reset) {
|
|
@@ -26,7 +130,7 @@ function isFramed() {
|
|
|
26
130
|
return typeof window !== "undefined" && window.self !== window.top;
|
|
27
131
|
}
|
|
28
132
|
function startLive() {
|
|
29
|
-
starting ?? (starting = import("./live-preview-
|
|
133
|
+
starting ?? (starting = import("./live-preview-2RCU35W5.js").then(
|
|
30
134
|
({ applyPatchLog, createLiveBridge, createLiveStore }) => {
|
|
31
135
|
applyLog = applyPatchLog;
|
|
32
136
|
const started = createLiveStore();
|
|
@@ -54,9 +158,9 @@ function whenLive(listener) {
|
|
|
54
158
|
return () => startListeners.delete(listener);
|
|
55
159
|
}
|
|
56
160
|
function useNormalizedQuery(query, data, options = {}) {
|
|
57
|
-
if (!options.enabled
|
|
58
|
-
|
|
59
|
-
|
|
161
|
+
if (!options.enabled) return computed(() => unref(data));
|
|
162
|
+
const annotated = computed(() => annotateEditLinks(unref(data), query));
|
|
163
|
+
if (!isFramed()) return annotated;
|
|
60
164
|
if (options.strictMode) strictMode = true;
|
|
61
165
|
if (options.allowedEditorOrigins) {
|
|
62
166
|
allowedEditorOrigins = [...options.allowedEditorOrigins];
|
|
@@ -74,7 +178,7 @@ function useNormalizedQuery(query, data, options = {}) {
|
|
|
74
178
|
});
|
|
75
179
|
}
|
|
76
180
|
return computed(() => {
|
|
77
|
-
const value =
|
|
181
|
+
const value = annotated.value;
|
|
78
182
|
return log.value.length && applyLog ? applyLog(query, value, log.value) : value;
|
|
79
183
|
});
|
|
80
184
|
}
|
|
@@ -536,6 +536,7 @@ function decodeStega(str) {
|
|
|
536
536
|
|
|
537
537
|
// src/core/stega/dom.ts
|
|
538
538
|
var IGNORED_ANCESTORS = "script, style, noscript";
|
|
539
|
+
var AUTO_ENTRY_ATTR = "data-prepr-entry-auto";
|
|
539
540
|
function isIgnoredTextNode(node) {
|
|
540
541
|
if (node.parentElement?.closest(IGNORED_ANCESTORS)) return true;
|
|
541
542
|
return !node.textContent?.trim();
|
|
@@ -569,7 +570,9 @@ function parseEditHref(href) {
|
|
|
569
570
|
return {
|
|
570
571
|
id: url.pathname.split("/").pop() || void 0,
|
|
571
572
|
field: url.searchParams.get("field") || void 0,
|
|
572
|
-
locale: url.searchParams.get("locale") || void 0
|
|
573
|
+
locale: url.searchParams.get("locale") || void 0,
|
|
574
|
+
// Added by the toolkit's `annotateEditLinks`, not by Prepr.
|
|
575
|
+
entry: url.searchParams.get("entry") || void 0
|
|
573
576
|
};
|
|
574
577
|
} catch {
|
|
575
578
|
return {};
|
|
@@ -586,11 +589,16 @@ function tagEncodedElement(element, decoded) {
|
|
|
586
589
|
element.setAttribute("data-prepr-encoded", "");
|
|
587
590
|
element.setAttribute("data-prepr-href", decoded.href);
|
|
588
591
|
element.setAttribute("data-prepr-origin", decoded.origin);
|
|
589
|
-
const { id, field, locale } = parseEditHref(decoded.href);
|
|
592
|
+
const { id, field, locale, entry } = parseEditHref(decoded.href);
|
|
590
593
|
setOrRemove(element, "data-prepr-id", id);
|
|
591
594
|
const fieldHint = element.getAttribute("data-prepr-edit-target")?.trim() || void 0;
|
|
592
595
|
setOrRemove(element, "data-prepr-field", field ?? fieldHint);
|
|
593
596
|
setOrRemove(element, "data-prepr-locale", locale);
|
|
597
|
+
const siteEntry = element.hasAttribute("data-prepr-entry-id") && !element.hasAttribute(AUTO_ENTRY_ATTR);
|
|
598
|
+
if (!siteEntry) {
|
|
599
|
+
setOrRemove(element, "data-prepr-entry-id", entry);
|
|
600
|
+
setOrRemove(element, AUTO_ENTRY_ATTR, entry && "true");
|
|
601
|
+
}
|
|
594
602
|
}
|
|
595
603
|
function readEditPayload(element) {
|
|
596
604
|
return {
|
|
@@ -3513,7 +3521,7 @@ function createPreprPreview(opts = {}) {
|
|
|
3513
3521
|
}
|
|
3514
3522
|
|
|
3515
3523
|
// src/version.ts
|
|
3516
|
-
var VERSION = "0.5.0-rc.
|
|
3524
|
+
var VERSION = "0.5.0-rc.6";
|
|
3517
3525
|
|
|
3518
3526
|
// src/core/pixel.ts
|
|
3519
3527
|
var PIXEL_SCRIPT_URL = "https://cdn.tracking.prepr.io/js/prepr.min.js";
|
package/dist/react/components.js
CHANGED
|
@@ -512,6 +512,7 @@ function decodeStega(str) {
|
|
|
512
512
|
|
|
513
513
|
// src/core/stega/dom.ts
|
|
514
514
|
var IGNORED_ANCESTORS = "script, style, noscript";
|
|
515
|
+
var AUTO_ENTRY_ATTR = "data-prepr-entry-auto";
|
|
515
516
|
function isIgnoredTextNode(node) {
|
|
516
517
|
if (node.parentElement?.closest(IGNORED_ANCESTORS)) return true;
|
|
517
518
|
return !node.textContent?.trim();
|
|
@@ -545,7 +546,9 @@ function parseEditHref(href) {
|
|
|
545
546
|
return {
|
|
546
547
|
id: url.pathname.split("/").pop() || void 0,
|
|
547
548
|
field: url.searchParams.get("field") || void 0,
|
|
548
|
-
locale: url.searchParams.get("locale") || void 0
|
|
549
|
+
locale: url.searchParams.get("locale") || void 0,
|
|
550
|
+
// Added by the toolkit's `annotateEditLinks`, not by Prepr.
|
|
551
|
+
entry: url.searchParams.get("entry") || void 0
|
|
549
552
|
};
|
|
550
553
|
} catch {
|
|
551
554
|
return {};
|
|
@@ -562,11 +565,16 @@ function tagEncodedElement(element, decoded) {
|
|
|
562
565
|
element.setAttribute("data-prepr-encoded", "");
|
|
563
566
|
element.setAttribute("data-prepr-href", decoded.href);
|
|
564
567
|
element.setAttribute("data-prepr-origin", decoded.origin);
|
|
565
|
-
const { id, field, locale } = parseEditHref(decoded.href);
|
|
568
|
+
const { id, field, locale, entry } = parseEditHref(decoded.href);
|
|
566
569
|
setOrRemove(element, "data-prepr-id", id);
|
|
567
570
|
const fieldHint = element.getAttribute("data-prepr-edit-target")?.trim() || void 0;
|
|
568
571
|
setOrRemove(element, "data-prepr-field", field ?? fieldHint);
|
|
569
572
|
setOrRemove(element, "data-prepr-locale", locale);
|
|
573
|
+
const siteEntry = element.hasAttribute("data-prepr-entry-id") && !element.hasAttribute(AUTO_ENTRY_ATTR);
|
|
574
|
+
if (!siteEntry) {
|
|
575
|
+
setOrRemove(element, "data-prepr-entry-id", entry);
|
|
576
|
+
setOrRemove(element, AUTO_ENTRY_ATTR, entry && "true");
|
|
577
|
+
}
|
|
570
578
|
}
|
|
571
579
|
function readEditPayload(element) {
|
|
572
580
|
return {
|
|
@@ -3489,7 +3497,7 @@ function createPreprPreview(opts = {}) {
|
|
|
3489
3497
|
}
|
|
3490
3498
|
|
|
3491
3499
|
// src/version.ts
|
|
3492
|
-
var VERSION = "0.5.0-rc.
|
|
3500
|
+
var VERSION = "0.5.0-rc.6";
|
|
3493
3501
|
|
|
3494
3502
|
// src/core/pixel.ts
|
|
3495
3503
|
var PIXEL_SCRIPT_URL = "https://cdn.tracking.prepr.io/js/prepr.min.js";
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
// src/core/live-preview/query-document.ts
|
|
2
|
+
var NAME_START = /[_A-Za-z]/;
|
|
3
|
+
var NAME_CONTINUE = /[_0-9A-Za-z]/;
|
|
4
|
+
var PUNCT = /* @__PURE__ */ new Set([
|
|
5
|
+
"!",
|
|
6
|
+
"$",
|
|
7
|
+
"&",
|
|
8
|
+
"(",
|
|
9
|
+
")",
|
|
10
|
+
":",
|
|
11
|
+
"=",
|
|
12
|
+
"@",
|
|
13
|
+
"[",
|
|
14
|
+
"]",
|
|
15
|
+
"{",
|
|
16
|
+
"|",
|
|
17
|
+
"}"
|
|
18
|
+
]);
|
|
19
|
+
function tokenize(source) {
|
|
20
|
+
const tokens = [];
|
|
21
|
+
let i = 0;
|
|
22
|
+
const n = source.length;
|
|
23
|
+
while (i < n) {
|
|
24
|
+
const ch = source[i];
|
|
25
|
+
if (ch === " " || ch === " " || ch === "\n" || ch === "\r" || ch === "," || ch === "\uFEFF") {
|
|
26
|
+
i++;
|
|
27
|
+
} else if (ch === "#") {
|
|
28
|
+
while (i < n && source[i] !== "\n" && source[i] !== "\r") i++;
|
|
29
|
+
} else if (ch === "." && source.startsWith("...", i)) {
|
|
30
|
+
tokens.push({ type: "punct", value: "..." });
|
|
31
|
+
i += 3;
|
|
32
|
+
} else if (PUNCT.has(ch)) {
|
|
33
|
+
tokens.push({ type: "punct", value: ch });
|
|
34
|
+
i++;
|
|
35
|
+
} else if (NAME_START.test(ch)) {
|
|
36
|
+
const start = i;
|
|
37
|
+
while (i < n && NAME_CONTINUE.test(source[i])) i++;
|
|
38
|
+
tokens.push({ type: "name", value: source.slice(start, i) });
|
|
39
|
+
} else if (ch === '"') {
|
|
40
|
+
if (source.startsWith('"""', i)) {
|
|
41
|
+
i += 3;
|
|
42
|
+
while (i < n && !source.startsWith('"""', i)) {
|
|
43
|
+
i += source.startsWith('\\"""', i) ? 4 : 1;
|
|
44
|
+
}
|
|
45
|
+
if (i >= n) throw new SyntaxError("Unterminated block string");
|
|
46
|
+
i += 3;
|
|
47
|
+
} else {
|
|
48
|
+
i++;
|
|
49
|
+
while (i < n && source[i] !== '"') {
|
|
50
|
+
if (source[i] === "\n" || source[i] === "\r") {
|
|
51
|
+
throw new SyntaxError("Unterminated string");
|
|
52
|
+
}
|
|
53
|
+
i += source[i] === "\\" ? 2 : 1;
|
|
54
|
+
}
|
|
55
|
+
if (i >= n) throw new SyntaxError("Unterminated string");
|
|
56
|
+
i++;
|
|
57
|
+
}
|
|
58
|
+
tokens.push({ type: "value" });
|
|
59
|
+
} else if (ch === "-" || ch >= "0" && ch <= "9") {
|
|
60
|
+
i++;
|
|
61
|
+
while (i < n && /[0-9.eE+-]/.test(source[i])) i++;
|
|
62
|
+
tokens.push({ type: "value" });
|
|
63
|
+
} else {
|
|
64
|
+
throw new SyntaxError(`Unexpected character "${ch}"`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return tokens;
|
|
68
|
+
}
|
|
69
|
+
function parseSource(source, operationName) {
|
|
70
|
+
const tokens = tokenize(source);
|
|
71
|
+
let pos = 0;
|
|
72
|
+
const peek = () => tokens[pos];
|
|
73
|
+
const isPunct = (value) => {
|
|
74
|
+
const t = tokens[pos];
|
|
75
|
+
return t?.type === "punct" && t.value === value;
|
|
76
|
+
};
|
|
77
|
+
const expectPunct = (value) => {
|
|
78
|
+
if (!isPunct(value)) throw new SyntaxError(`Expected "${value}"`);
|
|
79
|
+
pos++;
|
|
80
|
+
};
|
|
81
|
+
const expectName = () => {
|
|
82
|
+
const t = tokens[pos];
|
|
83
|
+
if (t?.type !== "name") throw new SyntaxError("Expected a name");
|
|
84
|
+
pos++;
|
|
85
|
+
return t.value;
|
|
86
|
+
};
|
|
87
|
+
const skipParens = () => {
|
|
88
|
+
expectPunct("(");
|
|
89
|
+
let depth = 1;
|
|
90
|
+
while (depth > 0) {
|
|
91
|
+
const t = tokens[pos++];
|
|
92
|
+
if (!t) throw new SyntaxError("Unbalanced parentheses");
|
|
93
|
+
if (t.type === "punct" && t.value === "(") depth++;
|
|
94
|
+
else if (t.type === "punct" && t.value === ")") depth--;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const skipDirectives = () => {
|
|
98
|
+
while (isPunct("@")) {
|
|
99
|
+
pos++;
|
|
100
|
+
expectName();
|
|
101
|
+
if (isPunct("(")) skipParens();
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const parseSelectionSet = () => {
|
|
105
|
+
expectPunct("{");
|
|
106
|
+
const selections = [];
|
|
107
|
+
while (!isPunct("}")) {
|
|
108
|
+
if (!peek()) throw new SyntaxError("Unterminated selection set");
|
|
109
|
+
selections.push(parseSelection());
|
|
110
|
+
}
|
|
111
|
+
pos++;
|
|
112
|
+
return selections;
|
|
113
|
+
};
|
|
114
|
+
const parseSelection = () => {
|
|
115
|
+
if (isPunct("...")) {
|
|
116
|
+
pos++;
|
|
117
|
+
const t = peek();
|
|
118
|
+
if (t?.type === "name" && t.value !== "on") {
|
|
119
|
+
pos++;
|
|
120
|
+
skipDirectives();
|
|
121
|
+
return { kind: "spread", name: t.value };
|
|
122
|
+
}
|
|
123
|
+
let on = null;
|
|
124
|
+
if (t?.type === "name") {
|
|
125
|
+
pos++;
|
|
126
|
+
on = expectName();
|
|
127
|
+
}
|
|
128
|
+
skipDirectives();
|
|
129
|
+
return { kind: "inline", on, selections: parseSelectionSet() };
|
|
130
|
+
}
|
|
131
|
+
let name = expectName();
|
|
132
|
+
const key = name;
|
|
133
|
+
if (isPunct(":")) {
|
|
134
|
+
pos++;
|
|
135
|
+
name = expectName();
|
|
136
|
+
}
|
|
137
|
+
let hasArgs = false;
|
|
138
|
+
if (isPunct("(")) {
|
|
139
|
+
hasArgs = true;
|
|
140
|
+
skipParens();
|
|
141
|
+
}
|
|
142
|
+
skipDirectives();
|
|
143
|
+
const selections = isPunct("{") ? parseSelectionSet() : null;
|
|
144
|
+
return { kind: "field", name, key, hasArgs, selections };
|
|
145
|
+
};
|
|
146
|
+
const fragments = /* @__PURE__ */ new Map();
|
|
147
|
+
const operations = [];
|
|
148
|
+
while (pos < tokens.length) {
|
|
149
|
+
if (isPunct("{")) {
|
|
150
|
+
operations.push({ name: null, selections: parseSelectionSet() });
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const keyword = expectName();
|
|
154
|
+
if (keyword === "fragment") {
|
|
155
|
+
const name = expectName();
|
|
156
|
+
if (expectName() !== "on") throw new SyntaxError('Expected "on"');
|
|
157
|
+
const on = expectName();
|
|
158
|
+
skipDirectives();
|
|
159
|
+
fragments.set(name, { on, selections: parseSelectionSet() });
|
|
160
|
+
} else if (keyword === "query" || keyword === "mutation" || keyword === "subscription") {
|
|
161
|
+
const t = peek();
|
|
162
|
+
let name = null;
|
|
163
|
+
if (t?.type === "name") name = expectName();
|
|
164
|
+
if (isPunct("(")) skipParens();
|
|
165
|
+
skipDirectives();
|
|
166
|
+
operations.push({ name, selections: parseSelectionSet() });
|
|
167
|
+
} else {
|
|
168
|
+
throw new SyntaxError(`Unexpected "${keyword}"`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return { operation: pickOperation(operations, operationName), fragments };
|
|
172
|
+
}
|
|
173
|
+
function pickOperation(operations, operationName) {
|
|
174
|
+
const op = operationName ? operations.find((o) => o.name === operationName) : operations[0];
|
|
175
|
+
if (!op) throw new SyntaxError("The document has no operation");
|
|
176
|
+
return op.selections;
|
|
177
|
+
}
|
|
178
|
+
function fromAstSelections(set) {
|
|
179
|
+
return set.selections.map((s) => {
|
|
180
|
+
if (s.kind === "FragmentSpread")
|
|
181
|
+
return { kind: "spread", name: s.name.value };
|
|
182
|
+
if (s.kind === "InlineFragment") {
|
|
183
|
+
return {
|
|
184
|
+
kind: "inline",
|
|
185
|
+
on: s.typeCondition?.name.value ?? null,
|
|
186
|
+
selections: fromAstSelections(s.selectionSet)
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
const name = s.name.value;
|
|
190
|
+
return {
|
|
191
|
+
kind: "field",
|
|
192
|
+
name,
|
|
193
|
+
key: s.alias?.value ?? name,
|
|
194
|
+
hasArgs: Boolean(s.arguments?.length),
|
|
195
|
+
selections: s.selectionSet ? fromAstSelections(s.selectionSet) : null
|
|
196
|
+
};
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
function fromAst(doc, operationName) {
|
|
200
|
+
const fragments = /* @__PURE__ */ new Map();
|
|
201
|
+
const operations = [];
|
|
202
|
+
for (const def of doc.definitions ?? []) {
|
|
203
|
+
if (def.kind === "FragmentDefinition" && def.name && def.typeCondition && def.selectionSet) {
|
|
204
|
+
fragments.set(def.name.value, {
|
|
205
|
+
on: def.typeCondition.name.value,
|
|
206
|
+
selections: fromAstSelections(def.selectionSet)
|
|
207
|
+
});
|
|
208
|
+
} else if (def.kind === "OperationDefinition" && def.selectionSet) {
|
|
209
|
+
operations.push({
|
|
210
|
+
name: def.name?.value ?? null,
|
|
211
|
+
selections: fromAstSelections(def.selectionSet)
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return { operation: pickOperation(operations, operationName), fragments };
|
|
216
|
+
}
|
|
217
|
+
var astCache = /* @__PURE__ */ new WeakMap();
|
|
218
|
+
var sourceCache = /* @__PURE__ */ new Map();
|
|
219
|
+
var SOURCE_CACHE_LIMIT = 200;
|
|
220
|
+
function readQueryDocument(query, operationName) {
|
|
221
|
+
const cacheKey = operationName ?? "";
|
|
222
|
+
if (typeof query === "object" && query !== null && query.kind === "Document") {
|
|
223
|
+
let byName = astCache.get(query);
|
|
224
|
+
if (!byName) astCache.set(query, byName = /* @__PURE__ */ new Map());
|
|
225
|
+
let parsed2 = byName.get(cacheKey);
|
|
226
|
+
if (!parsed2) {
|
|
227
|
+
parsed2 = fromAst(
|
|
228
|
+
query,
|
|
229
|
+
operationName
|
|
230
|
+
);
|
|
231
|
+
byName.set(cacheKey, parsed2);
|
|
232
|
+
}
|
|
233
|
+
return parsed2;
|
|
234
|
+
}
|
|
235
|
+
const source = String(query);
|
|
236
|
+
const sourceKey = `${cacheKey}
|
|
237
|
+
${source}`;
|
|
238
|
+
let parsed = sourceCache.get(sourceKey);
|
|
239
|
+
if (!parsed) {
|
|
240
|
+
parsed = parseSource(source, operationName);
|
|
241
|
+
if (sourceCache.size >= SOURCE_CACHE_LIMIT) {
|
|
242
|
+
sourceCache.delete(sourceCache.keys().next().value);
|
|
243
|
+
}
|
|
244
|
+
sourceCache.set(sourceKey, parsed);
|
|
245
|
+
}
|
|
246
|
+
return parsed;
|
|
247
|
+
}
|
|
248
|
+
var fieldsCache = /* @__PURE__ */ new WeakMap();
|
|
249
|
+
function collectFields(selections, typename, doc, options = {}) {
|
|
250
|
+
const cacheKey = `${options.strict ? "s" : "l"}:${typename ?? ""}`;
|
|
251
|
+
let cached = fieldsCache.get(selections);
|
|
252
|
+
if (!cached) {
|
|
253
|
+
cached = /* @__PURE__ */ new Map();
|
|
254
|
+
fieldsCache.set(selections, cached);
|
|
255
|
+
}
|
|
256
|
+
const hit = cached.get(cacheKey);
|
|
257
|
+
if (hit) return hit;
|
|
258
|
+
const fields = resolveFields(selections, typename, doc, options);
|
|
259
|
+
cached.set(cacheKey, fields);
|
|
260
|
+
return fields;
|
|
261
|
+
}
|
|
262
|
+
function resolveFields(selections, typename, doc, options) {
|
|
263
|
+
const exact = /* @__PURE__ */ new Map();
|
|
264
|
+
const loose = /* @__PURE__ */ new Map();
|
|
265
|
+
const merged = /* @__PURE__ */ new Map();
|
|
266
|
+
const walkedMatched = /* @__PURE__ */ new Set();
|
|
267
|
+
const walkedLoose = /* @__PURE__ */ new Set();
|
|
268
|
+
const add = (target, s) => {
|
|
269
|
+
let field = target.get(s.key);
|
|
270
|
+
if (!field) {
|
|
271
|
+
field = { name: s.name, key: s.key, hasArgs: s.hasArgs, selections: [] };
|
|
272
|
+
target.set(s.key, field);
|
|
273
|
+
merged.set(field, /* @__PURE__ */ new Set());
|
|
274
|
+
}
|
|
275
|
+
const done = merged.get(field);
|
|
276
|
+
if (s.selections && !done.has(s.selections)) {
|
|
277
|
+
done.add(s.selections);
|
|
278
|
+
field.selections = [...field.selections, ...s.selections];
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
const applies = (on, list) => conditionMatches(on, typename) || typename !== void 0 && typeConditions(list, doc).includes(typename);
|
|
282
|
+
const walk = (list, matched) => {
|
|
283
|
+
for (const s of list) {
|
|
284
|
+
if (s.kind === "field") {
|
|
285
|
+
add(matched ? exact : loose, s);
|
|
286
|
+
} else if (s.kind === "inline") {
|
|
287
|
+
walk(s.selections, matched && applies(s.on, s.selections));
|
|
288
|
+
} else {
|
|
289
|
+
const fragment = doc.fragments.get(s.name);
|
|
290
|
+
if (!fragment) continue;
|
|
291
|
+
const asMatched = matched && applies(fragment.on, fragment.selections);
|
|
292
|
+
if (walkedMatched.has(fragment)) continue;
|
|
293
|
+
if (!asMatched && walkedLoose.has(fragment)) continue;
|
|
294
|
+
(asMatched ? walkedMatched : walkedLoose).add(fragment);
|
|
295
|
+
walk(fragment.selections, asMatched);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
walk(selections, true);
|
|
300
|
+
if (!options.strict) {
|
|
301
|
+
for (const [key, field] of loose) {
|
|
302
|
+
if (!exact.has(key)) exact.set(key, field);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return exact;
|
|
306
|
+
}
|
|
307
|
+
function conditionMatches(on, typename) {
|
|
308
|
+
return on === null || typename === void 0 || on === typename;
|
|
309
|
+
}
|
|
310
|
+
var conditionsCache = /* @__PURE__ */ new WeakMap();
|
|
311
|
+
function typeConditions(selections, doc) {
|
|
312
|
+
const hit = conditionsCache.get(selections);
|
|
313
|
+
if (hit) return hit;
|
|
314
|
+
const names = /* @__PURE__ */ new Set();
|
|
315
|
+
const seen = /* @__PURE__ */ new Set();
|
|
316
|
+
const walk = (list) => {
|
|
317
|
+
for (const s of list) {
|
|
318
|
+
if (s.kind === "inline") {
|
|
319
|
+
if (s.on) names.add(s.on);
|
|
320
|
+
walk(s.selections);
|
|
321
|
+
} else if (s.kind === "spread") {
|
|
322
|
+
const fragment = doc.fragments.get(s.name);
|
|
323
|
+
if (!fragment || seen.has(fragment)) continue;
|
|
324
|
+
seen.add(fragment);
|
|
325
|
+
names.add(fragment.on);
|
|
326
|
+
walk(fragment.selections);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
walk(selections);
|
|
331
|
+
const conditions = [...names];
|
|
332
|
+
conditionsCache.set(selections, conditions);
|
|
333
|
+
return conditions;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export {
|
|
337
|
+
readQueryDocument,
|
|
338
|
+
collectFields,
|
|
339
|
+
typeConditions
|
|
340
|
+
};
|
package/dist/sveltekit/index.cjs
CHANGED
package/dist/sveltekit/index.js
CHANGED