@preprio/toolkit 0.5.0-rc.5 → 0.5.0-rc.7

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.
Files changed (38) hide show
  1. package/README.md +133 -26
  2. package/dist/astro/index.cjs +126 -30
  3. package/dist/astro/index.js +130 -30
  4. package/dist/{chunk-3X55JL52.js → chunk-HVS5QLWG.js} +1 -1
  5. package/dist/{chunk-TG6N3C22.js → chunk-IPMB32AZ.js} +1 -1
  6. package/dist/{chunk-CMUFTCG3.js → chunk-JUXP5KRX.js} +1 -1
  7. package/dist/{chunk-74KY5EAM.js → chunk-KD3GK6TO.js} +2 -0
  8. package/dist/{chunk-SBLZANL5.js → chunk-SUAFASBB.js} +11 -3
  9. package/dist/codegen/index.cjs +82 -0
  10. package/dist/codegen/index.d.cts +70 -0
  11. package/dist/codegen/index.d.ts +70 -0
  12. package/dist/codegen/index.js +64 -0
  13. package/dist/index.cjs +133 -28
  14. package/dist/index.d.cts +14 -2
  15. package/dist/index.d.ts +14 -2
  16. package/dist/index.js +108 -6
  17. package/dist/{live-preview-H4643QYF.js → live-preview-DLXGXRE5.js} +2 -2
  18. package/dist/nextjs/components.cjs +11 -3
  19. package/dist/nextjs/components.js +2 -2
  20. package/dist/nextjs/index.cjs +124 -28
  21. package/dist/nextjs/index.js +128 -28
  22. package/dist/nextjs/live-action.js +3 -3
  23. package/dist/nextjs/live.js +1 -1
  24. package/dist/nuxt/chunk-BWPGO25E.js +340 -0
  25. package/dist/nuxt/index.cjs +1 -1
  26. package/dist/nuxt/index.js +1 -1
  27. package/dist/nuxt/{live-preview-O2ZTJNW4.js → live-preview-2RCU35W5.js} +6 -337
  28. package/dist/nuxt/live.cjs +1007 -911
  29. package/dist/nuxt/live.js +109 -5
  30. package/dist/react/components.cjs +11 -3
  31. package/dist/react/components.js +11 -3
  32. package/dist/sveltekit/chunk-BWPGO25E.js +340 -0
  33. package/dist/sveltekit/index.cjs +1 -1
  34. package/dist/sveltekit/index.js +1 -1
  35. package/dist/sveltekit/{live-preview-O2ZTJNW4.js → live-preview-2RCU35W5.js} +6 -337
  36. package/dist/sveltekit/live.cjs +1008 -909
  37. package/dist/sveltekit/live.js +118 -11
  38. package/package.json +11 -1
@@ -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
+ };
@@ -35,7 +35,7 @@ __export(nuxt_exports, {
35
35
  module.exports = __toCommonJS(nuxt_exports);
36
36
 
37
37
  // src/version.ts
38
- var VERSION = "0.5.0-rc.5";
38
+ var VERSION = "0.5.0-rc.7";
39
39
 
40
40
  // src/core/constants.ts
41
41
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var VERSION = "0.5.0-rc.5";
2
+ var VERSION = "0.5.0-rc.7";
3
3
 
4
4
  // src/core/constants.ts
5
5
  var COOKIE_PREVIEW_MODE = "Prepr-Preview-Mode";
@@ -1,3 +1,9 @@
1
+ import {
2
+ collectFields,
3
+ readQueryDocument,
4
+ typeConditions
5
+ } from "./chunk-BWPGO25E.js";
6
+
1
7
  // src/core/utils.ts
2
8
  var trustedParentOrigin = null;
3
9
  function getTrustedParentOrigin() {
@@ -741,343 +747,6 @@ import {
741
747
  vercelStegaDecode,
742
748
  vercelStegaSplit as vercelStegaSplit2
743
749
  } from "@vercel/stega";
744
-
745
- // src/core/live-preview/query-document.ts
746
- var NAME_START = /[_A-Za-z]/;
747
- var NAME_CONTINUE = /[_0-9A-Za-z]/;
748
- var PUNCT = /* @__PURE__ */ new Set([
749
- "!",
750
- "$",
751
- "&",
752
- "(",
753
- ")",
754
- ":",
755
- "=",
756
- "@",
757
- "[",
758
- "]",
759
- "{",
760
- "|",
761
- "}"
762
- ]);
763
- function tokenize(source) {
764
- const tokens = [];
765
- let i = 0;
766
- const n = source.length;
767
- while (i < n) {
768
- const ch = source[i];
769
- if (ch === " " || ch === " " || ch === "\n" || ch === "\r" || ch === "," || ch === "\uFEFF") {
770
- i++;
771
- } else if (ch === "#") {
772
- while (i < n && source[i] !== "\n" && source[i] !== "\r") i++;
773
- } else if (ch === "." && source.startsWith("...", i)) {
774
- tokens.push({ type: "punct", value: "..." });
775
- i += 3;
776
- } else if (PUNCT.has(ch)) {
777
- tokens.push({ type: "punct", value: ch });
778
- i++;
779
- } else if (NAME_START.test(ch)) {
780
- const start = i;
781
- while (i < n && NAME_CONTINUE.test(source[i])) i++;
782
- tokens.push({ type: "name", value: source.slice(start, i) });
783
- } else if (ch === '"') {
784
- if (source.startsWith('"""', i)) {
785
- i += 3;
786
- while (i < n && !source.startsWith('"""', i)) {
787
- i += source.startsWith('\\"""', i) ? 4 : 1;
788
- }
789
- if (i >= n) throw new SyntaxError("Unterminated block string");
790
- i += 3;
791
- } else {
792
- i++;
793
- while (i < n && source[i] !== '"') {
794
- if (source[i] === "\n" || source[i] === "\r") {
795
- throw new SyntaxError("Unterminated string");
796
- }
797
- i += source[i] === "\\" ? 2 : 1;
798
- }
799
- if (i >= n) throw new SyntaxError("Unterminated string");
800
- i++;
801
- }
802
- tokens.push({ type: "value" });
803
- } else if (ch === "-" || ch >= "0" && ch <= "9") {
804
- i++;
805
- while (i < n && /[0-9.eE+-]/.test(source[i])) i++;
806
- tokens.push({ type: "value" });
807
- } else {
808
- throw new SyntaxError(`Unexpected character "${ch}"`);
809
- }
810
- }
811
- return tokens;
812
- }
813
- function parseSource(source, operationName) {
814
- const tokens = tokenize(source);
815
- let pos = 0;
816
- const peek = () => tokens[pos];
817
- const isPunct = (value) => {
818
- const t = tokens[pos];
819
- return t?.type === "punct" && t.value === value;
820
- };
821
- const expectPunct = (value) => {
822
- if (!isPunct(value)) throw new SyntaxError(`Expected "${value}"`);
823
- pos++;
824
- };
825
- const expectName = () => {
826
- const t = tokens[pos];
827
- if (t?.type !== "name") throw new SyntaxError("Expected a name");
828
- pos++;
829
- return t.value;
830
- };
831
- const skipParens = () => {
832
- expectPunct("(");
833
- let depth = 1;
834
- while (depth > 0) {
835
- const t = tokens[pos++];
836
- if (!t) throw new SyntaxError("Unbalanced parentheses");
837
- if (t.type === "punct" && t.value === "(") depth++;
838
- else if (t.type === "punct" && t.value === ")") depth--;
839
- }
840
- };
841
- const skipDirectives = () => {
842
- while (isPunct("@")) {
843
- pos++;
844
- expectName();
845
- if (isPunct("(")) skipParens();
846
- }
847
- };
848
- const parseSelectionSet = () => {
849
- expectPunct("{");
850
- const selections = [];
851
- while (!isPunct("}")) {
852
- if (!peek()) throw new SyntaxError("Unterminated selection set");
853
- selections.push(parseSelection());
854
- }
855
- pos++;
856
- return selections;
857
- };
858
- const parseSelection = () => {
859
- if (isPunct("...")) {
860
- pos++;
861
- const t = peek();
862
- if (t?.type === "name" && t.value !== "on") {
863
- pos++;
864
- skipDirectives();
865
- return { kind: "spread", name: t.value };
866
- }
867
- let on = null;
868
- if (t?.type === "name") {
869
- pos++;
870
- on = expectName();
871
- }
872
- skipDirectives();
873
- return { kind: "inline", on, selections: parseSelectionSet() };
874
- }
875
- let name = expectName();
876
- const key = name;
877
- if (isPunct(":")) {
878
- pos++;
879
- name = expectName();
880
- }
881
- let hasArgs = false;
882
- if (isPunct("(")) {
883
- hasArgs = true;
884
- skipParens();
885
- }
886
- skipDirectives();
887
- const selections = isPunct("{") ? parseSelectionSet() : null;
888
- return { kind: "field", name, key, hasArgs, selections };
889
- };
890
- const fragments = /* @__PURE__ */ new Map();
891
- const operations = [];
892
- while (pos < tokens.length) {
893
- if (isPunct("{")) {
894
- operations.push({ name: null, selections: parseSelectionSet() });
895
- continue;
896
- }
897
- const keyword = expectName();
898
- if (keyword === "fragment") {
899
- const name = expectName();
900
- if (expectName() !== "on") throw new SyntaxError('Expected "on"');
901
- const on = expectName();
902
- skipDirectives();
903
- fragments.set(name, { on, selections: parseSelectionSet() });
904
- } else if (keyword === "query" || keyword === "mutation" || keyword === "subscription") {
905
- const t = peek();
906
- let name = null;
907
- if (t?.type === "name") name = expectName();
908
- if (isPunct("(")) skipParens();
909
- skipDirectives();
910
- operations.push({ name, selections: parseSelectionSet() });
911
- } else {
912
- throw new SyntaxError(`Unexpected "${keyword}"`);
913
- }
914
- }
915
- return { operation: pickOperation(operations, operationName), fragments };
916
- }
917
- function pickOperation(operations, operationName) {
918
- const op = operationName ? operations.find((o) => o.name === operationName) : operations[0];
919
- if (!op) throw new SyntaxError("The document has no operation");
920
- return op.selections;
921
- }
922
- function fromAstSelections(set) {
923
- return set.selections.map((s) => {
924
- if (s.kind === "FragmentSpread")
925
- return { kind: "spread", name: s.name.value };
926
- if (s.kind === "InlineFragment") {
927
- return {
928
- kind: "inline",
929
- on: s.typeCondition?.name.value ?? null,
930
- selections: fromAstSelections(s.selectionSet)
931
- };
932
- }
933
- const name = s.name.value;
934
- return {
935
- kind: "field",
936
- name,
937
- key: s.alias?.value ?? name,
938
- hasArgs: Boolean(s.arguments?.length),
939
- selections: s.selectionSet ? fromAstSelections(s.selectionSet) : null
940
- };
941
- });
942
- }
943
- function fromAst(doc, operationName) {
944
- const fragments = /* @__PURE__ */ new Map();
945
- const operations = [];
946
- for (const def of doc.definitions ?? []) {
947
- if (def.kind === "FragmentDefinition" && def.name && def.typeCondition && def.selectionSet) {
948
- fragments.set(def.name.value, {
949
- on: def.typeCondition.name.value,
950
- selections: fromAstSelections(def.selectionSet)
951
- });
952
- } else if (def.kind === "OperationDefinition" && def.selectionSet) {
953
- operations.push({
954
- name: def.name?.value ?? null,
955
- selections: fromAstSelections(def.selectionSet)
956
- });
957
- }
958
- }
959
- return { operation: pickOperation(operations, operationName), fragments };
960
- }
961
- var astCache = /* @__PURE__ */ new WeakMap();
962
- var sourceCache = /* @__PURE__ */ new Map();
963
- var SOURCE_CACHE_LIMIT = 200;
964
- function readQueryDocument(query, operationName) {
965
- const cacheKey = operationName ?? "";
966
- if (typeof query === "object" && query !== null && query.kind === "Document") {
967
- let byName = astCache.get(query);
968
- if (!byName) astCache.set(query, byName = /* @__PURE__ */ new Map());
969
- let parsed2 = byName.get(cacheKey);
970
- if (!parsed2) {
971
- parsed2 = fromAst(
972
- query,
973
- operationName
974
- );
975
- byName.set(cacheKey, parsed2);
976
- }
977
- return parsed2;
978
- }
979
- const source = String(query);
980
- const sourceKey = `${cacheKey}
981
- ${source}`;
982
- let parsed = sourceCache.get(sourceKey);
983
- if (!parsed) {
984
- parsed = parseSource(source, operationName);
985
- if (sourceCache.size >= SOURCE_CACHE_LIMIT) {
986
- sourceCache.delete(sourceCache.keys().next().value);
987
- }
988
- sourceCache.set(sourceKey, parsed);
989
- }
990
- return parsed;
991
- }
992
- var fieldsCache = /* @__PURE__ */ new WeakMap();
993
- function collectFields(selections, typename, doc, options = {}) {
994
- const cacheKey = `${options.strict ? "s" : "l"}:${typename ?? ""}`;
995
- let cached = fieldsCache.get(selections);
996
- if (!cached) {
997
- cached = /* @__PURE__ */ new Map();
998
- fieldsCache.set(selections, cached);
999
- }
1000
- const hit = cached.get(cacheKey);
1001
- if (hit) return hit;
1002
- const fields = resolveFields(selections, typename, doc, options);
1003
- cached.set(cacheKey, fields);
1004
- return fields;
1005
- }
1006
- function resolveFields(selections, typename, doc, options) {
1007
- const exact = /* @__PURE__ */ new Map();
1008
- const loose = /* @__PURE__ */ new Map();
1009
- const merged = /* @__PURE__ */ new Map();
1010
- const walkedMatched = /* @__PURE__ */ new Set();
1011
- const walkedLoose = /* @__PURE__ */ new Set();
1012
- const add = (target, s) => {
1013
- let field = target.get(s.key);
1014
- if (!field) {
1015
- field = { name: s.name, key: s.key, hasArgs: s.hasArgs, selections: [] };
1016
- target.set(s.key, field);
1017
- merged.set(field, /* @__PURE__ */ new Set());
1018
- }
1019
- const done = merged.get(field);
1020
- if (s.selections && !done.has(s.selections)) {
1021
- done.add(s.selections);
1022
- field.selections = [...field.selections, ...s.selections];
1023
- }
1024
- };
1025
- const applies = (on, list) => conditionMatches(on, typename) || typename !== void 0 && typeConditions(list, doc).includes(typename);
1026
- const walk = (list, matched) => {
1027
- for (const s of list) {
1028
- if (s.kind === "field") {
1029
- add(matched ? exact : loose, s);
1030
- } else if (s.kind === "inline") {
1031
- walk(s.selections, matched && applies(s.on, s.selections));
1032
- } else {
1033
- const fragment = doc.fragments.get(s.name);
1034
- if (!fragment) continue;
1035
- const asMatched = matched && applies(fragment.on, fragment.selections);
1036
- if (walkedMatched.has(fragment)) continue;
1037
- if (!asMatched && walkedLoose.has(fragment)) continue;
1038
- (asMatched ? walkedMatched : walkedLoose).add(fragment);
1039
- walk(fragment.selections, asMatched);
1040
- }
1041
- }
1042
- };
1043
- walk(selections, true);
1044
- if (!options.strict) {
1045
- for (const [key, field] of loose) {
1046
- if (!exact.has(key)) exact.set(key, field);
1047
- }
1048
- }
1049
- return exact;
1050
- }
1051
- function conditionMatches(on, typename) {
1052
- return on === null || typename === void 0 || on === typename;
1053
- }
1054
- var conditionsCache = /* @__PURE__ */ new WeakMap();
1055
- function typeConditions(selections, doc) {
1056
- const hit = conditionsCache.get(selections);
1057
- if (hit) return hit;
1058
- const names = /* @__PURE__ */ new Set();
1059
- const seen = /* @__PURE__ */ new Set();
1060
- const walk = (list) => {
1061
- for (const s of list) {
1062
- if (s.kind === "inline") {
1063
- if (s.on) names.add(s.on);
1064
- walk(s.selections);
1065
- } else if (s.kind === "spread") {
1066
- const fragment = doc.fragments.get(s.name);
1067
- if (!fragment || seen.has(fragment)) continue;
1068
- seen.add(fragment);
1069
- names.add(fragment.on);
1070
- walk(fragment.selections);
1071
- }
1072
- }
1073
- };
1074
- walk(selections);
1075
- const conditions = [...names];
1076
- conditionsCache.set(selections, conditions);
1077
- return conditions;
1078
- }
1079
-
1080
- // src/core/live-preview/normalize-query.ts
1081
750
  var debug5 = createScopedLogger("live-preview:normalize");
1082
751
  var MAX_LOG_ENTRIES2 = 500;
1083
752
  var MAX_LOG_SOURCE_LENGTH = 1e6;