@seedprotocol/query 0.6.5 → 0.6.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.
package/dist/index.js CHANGED
@@ -1,1080 +1,3 @@
1
- import { i as e, n as t, r as n, t as r } from "./bootstrap-DQZ8ehyx.js";
2
- import { BaseArweaveClient as i, isKnownArweaveGatewayHostname as a } from "@seedprotocol/arweave";
3
- import { EasClient as o, GET_SEEDS as s, getItemPropertiesFromEas as c, getItemVersionsFromEas as l, getSeedsBySchemaName as u, pickLatestPropertyAttestationsByRefAndSchema as d, setSchemaUidForSchemaDefinition as f, withExcludeRevokedFilter as p } from "@seedprotocol/eas";
4
- import m from "pluralize";
5
- import { promises as h } from "fs";
6
- import { join as g } from "path";
7
- import { createHash as ee } from "crypto";
8
- //#region src/arweaveUrl.ts
9
- function _(e) {
10
- return `https://${i.getHost()}/${e}`;
11
- }
12
- //#endregion
13
- //#region src/listRelationKey.ts
14
- var te = /^(.+?)([A-Z][a-zA-Z]+)Ids$/;
15
- function v(e) {
16
- let t = te.exec(e);
17
- if (t) {
18
- let e = t[1];
19
- if (e) return m(e);
20
- }
21
- if (e.endsWith("_ids")) {
22
- let t = e.slice(0, -4), n = t.split("_").filter(Boolean);
23
- if (n.length >= 2) {
24
- let e = n[0];
25
- return e.endsWith("s") ? e : m(e);
26
- }
27
- return m(t);
28
- }
29
- return e;
30
- }
31
- function y(e, t) {
32
- for (let n of Object.keys(e)) n !== t && v(n) === t && delete e[n];
33
- }
34
- function b(e) {
35
- if (Array.isArray(e) || typeof e != "string") return e;
36
- let t = e.trim();
37
- if (!t.startsWith("[")) return e;
38
- try {
39
- let e = JSON.parse(t);
40
- if (Array.isArray(e)) return e;
41
- } catch {}
42
- return e;
43
- }
44
- //#endregion
45
- //#region src/imageRelationEnrichment.ts
46
- function x(e) {
47
- let t = e.storageTransactionId ?? e.storage_transaction_id;
48
- if (t && typeof t == "string" && t.trim()) try {
49
- e.arweaveUrl = _(t.trim());
50
- } catch {}
51
- }
52
- var ne = x, S = /* @__PURE__ */ new Set([
53
- "html",
54
- "file",
55
- "json"
56
- ]), re = S, C = "_feedFieldStorageModels", ie = "_feedListElementStorageModels";
57
- function w(e) {
58
- return e.trim().toLowerCase();
59
- }
60
- function T(e) {
61
- return S.has(w(e));
62
- }
63
- var ae = T;
64
- function E(e) {
65
- let t = w(e);
66
- return t === "html" ? 0 : t === "file" ? 1 : t === "json" ? 2 : 99;
67
- }
68
- var oe = E;
69
- function D(e) {
70
- let t = e[C];
71
- if (typeof t == "object" && t && !Array.isArray(t)) return t;
72
- }
73
- var se = D;
74
- function O(e) {
75
- let t = e[ie];
76
- if (typeof t == "object" && t && !Array.isArray(t)) return t;
77
- }
78
- var ce = O;
79
- function k(e, t, n) {
80
- let r = w(n), i = D(e);
81
- i || (i = {}, e[C] = i), i[t] = r;
82
- }
83
- var le = k;
84
- function A(e, t, n) {
85
- let r = O(e);
86
- r || (r = {}, e[ie] = r), r[t] = n.map(w);
87
- }
88
- var ue = A, de = [
89
- "html",
90
- "Html",
91
- "body",
92
- "Body",
93
- "content",
94
- "Content"
95
- ], fe = 8e6;
96
- function pe(e) {
97
- let t = e.trim();
98
- if (!t.startsWith("http://") && !t.startsWith("https://")) return !1;
99
- try {
100
- let e = new URL(t), n = e.hostname.toLowerCase();
101
- if (n !== i.getHost().toLowerCase() && !a(n)) return !1;
102
- let r = e.pathname.replace(/^\//, "").split("/").filter(Boolean);
103
- if (r.length !== 1) return !1;
104
- let o = r[0];
105
- return /^[A-Za-z0-9_-]{43}$/.test(o);
106
- } catch {
107
- return !1;
108
- }
109
- }
110
- async function me(e) {
111
- try {
112
- let t = await fetch(e, {
113
- headers: { Accept: "text/html, text/plain, text/markdown, application/json, */*" },
114
- signal: AbortSignal.timeout(15e3)
115
- });
116
- if (!t.ok) return null;
117
- let n = t.headers.get("content-type") ?? "";
118
- if (/^(image|video|audio)\//i.test(n)) return null;
119
- let r = await t.arrayBuffer();
120
- if (r.byteLength > fe) return null;
121
- let i = new TextDecoder("utf-8", { fatal: !1 }).decode(r);
122
- return i.length === 0 || i.includes("\0") ? null : i;
123
- } catch {
124
- return null;
125
- }
126
- }
127
- async function he(e, t, n) {
128
- if (n) try {
129
- let r = await n({
130
- propertyName: e,
131
- value: t
132
- });
133
- if (r != null && r.length > 0) return r;
134
- } catch {}
135
- return pe(t) ? me(t) : null;
136
- }
137
- async function ge(e, t, n) {
138
- let r = e[t];
139
- if (typeof r != "string" || r.trim() === "") return;
140
- let i = await he(t, r, n);
141
- i !== null && (e[t] = i);
142
- }
143
- async function j(e, t) {
144
- let n = t?.readStorageBody;
145
- for (let t of e) {
146
- let e = /* @__PURE__ */ new Set([...de]), r = D(t);
147
- if (r) for (let [t, n] of Object.entries(r)) T(n) && e.add(t);
148
- for (let r of e) await ge(t, r, n);
149
- let i = O(t);
150
- if (i) for (let [e, r] of Object.entries(i)) {
151
- let i = t[e];
152
- if (!Array.isArray(i)) continue;
153
- let a = Math.min(r.length, i.length);
154
- for (let t = 0; t < a; t++) {
155
- if (!T(r[t])) continue;
156
- let a = i[t];
157
- if (typeof a != "string") continue;
158
- let o = await he(e, a, n);
159
- o !== null && (i[t] = o);
160
- }
161
- }
162
- }
163
- }
164
- var _e = j;
165
- //#endregion
166
- //#region src/parseEasPropertyMetadata.ts
167
- function M(e) {
168
- let t = typeof e == "string" ? e.trim() : "";
169
- if (!t) return {
170
- ok: !1,
171
- reason: "empty"
172
- };
173
- let n;
174
- try {
175
- n = JSON.parse(t);
176
- } catch (e) {
177
- return {
178
- ok: !1,
179
- reason: "parse",
180
- error: e
181
- };
182
- }
183
- return !Array.isArray(n) || n.length === 0 || !n[0]?.value ? {
184
- ok: !1,
185
- reason: "shape"
186
- } : {
187
- ok: !0,
188
- metadata: n[0].value
189
- };
190
- }
191
- var ve = M;
192
- //#endregion
193
- //#region src/parseEasRelationPropertyName.ts
194
- function N(e) {
195
- let [t, n, r] = e.split("_");
196
- if (!t || !n) return null;
197
- let i = r === "ids";
198
- return {
199
- propertyName: t.endsWith("s") ? t : t + "s",
200
- modelName: n,
201
- isList: i
202
- };
203
- }
204
- //#endregion
205
- //#region src/source/remote.ts
206
- function ye(e, t) {
207
- let n = new Date(e, t - 1, 1), r = new Date(e, t, 0, 23, 59, 59);
208
- return {
209
- startTs: Math.floor(n.getTime() / 1e3),
210
- endTs: Math.floor(r.getTime() / 1e3) + 1
211
- };
212
- }
213
- function P() {
214
- return {
215
- kind: "remote",
216
- async getSeedByUid(e) {
217
- let { itemSeeds: t } = await o.getEasClient().request(s, {
218
- where: p({ id: { equals: e } }),
219
- take: 1,
220
- skip: 0
221
- });
222
- return (t ?? [])[0] ?? null;
223
- },
224
- async listSeedsBySchemaName(e, t) {
225
- return await u(e, t.limit, t.skip);
226
- },
227
- async listSeedsBySchemaNameForMonth(e, t, n) {
228
- let { startTs: r, endTs: i } = ye(t, n), a = p({ AND: [{ schema: { is: { schemaNames: { some: { name: { equals: e } } } } } }, { timeCreated: {
229
- gte: r,
230
- lt: i
231
- } }] }), { itemSeeds: c } = await o.getEasClient().request(s, {
232
- where: a,
233
- take: 1e3,
234
- skip: 0
235
- });
236
- return c ?? [];
237
- },
238
- async getVersionsForSeed(e) {
239
- return await l({ seedUids: [e] });
240
- },
241
- async getVersionsForSeeds(e) {
242
- return e.length === 0 ? [] : await l({ seedUids: e });
243
- },
244
- async getPropertiesForVersionUids(e) {
245
- return e.length === 0 ? [] : await c({ versionUids: e });
246
- },
247
- async getSeedsByUids(e) {
248
- if (e.length === 0) return [];
249
- let { itemSeeds: t } = await o.getEasClient().request(s, {
250
- where: p({ id: { in: e } }),
251
- take: e.length || 1,
252
- skip: 0
253
- });
254
- return t ?? [];
255
- }
256
- };
257
- }
258
- var F = null;
259
- function I() {
260
- return F ||= P(), F;
261
- }
262
- //#endregion
263
- //#region src/assembleSeeds.ts
264
- var L = "image", R = ["0x0000000000000000000000000000000000000000000000000000000000000020"], be = /* @__PURE__ */ new Set([
265
- "seedUid",
266
- "SeedUid",
267
- "timeCreated",
268
- "attester",
269
- "Attester",
270
- "storage_transaction_id",
271
- "storage_provider_transaction_id",
272
- "storageTransactionId",
273
- "storageProviderTransactionId",
274
- "versionUid"
275
- ]), z = (e) => e.replace(/_([a-z])/g, (e, t) => t.toUpperCase());
276
- function xe() {
277
- return {
278
- seedUidToModelType: /* @__PURE__ */ new Map(),
279
- relatedSeedUids: /* @__PURE__ */ new Set(),
280
- versionUidToSeedUid: /* @__PURE__ */ new Map(),
281
- assembledItems: /* @__PURE__ */ new Map(),
282
- versionsBySeedUid: /* @__PURE__ */ new Map(),
283
- latestVersionUidsBySeedUid: /* @__PURE__ */ new Map()
284
- };
285
- }
286
- function B(e, t) {
287
- !e.seedUid && !e.SeedUid ? (e.seedUid = t, e.SeedUid = t) : e.seedUid && !e.SeedUid ? e.SeedUid = e.seedUid : e.SeedUid && !e.seedUid && (e.seedUid = e.SeedUid), e.attester && !e.Attester ? e.Attester = e.attester : e.Attester && !e.attester && (e.attester = e.Attester);
288
- }
289
- async function Se(e, t) {
290
- let n = M(t.decodedDataJson);
291
- if (!n.ok) {
292
- let { id: e, refUID: r, schemaId: i } = t;
293
- n.reason === "empty" ? console.warn("[query] [processItemProperty] empty decodedDataJson for property:", e, r, i) : n.reason === "parse" ? console.warn("[query] [processItemProperty] failed to parse decodedDataJson for property:", e, r, i, n.error) : console.warn("[query] [processItemProperty] invalid decodedDataJson structure for property:", e, r, i);
294
- return;
295
- }
296
- let r = n.metadata, i = r.name;
297
- if (!i) return;
298
- let a = t.schemaId;
299
- f({
300
- text: i,
301
- schemaUid: a
302
- });
303
- let o = !1, s = !1, c = r.type, l = (c === "bytes32" || c === "bytes32[]") && i !== "storage_transaction_id" && i !== "storage_provider_transaction_id", u = !l && (i.endsWith("_id") || i.endsWith("_ids")) && i !== "storage_transaction_id" && i !== "storage_provider_transaction_id";
304
- if (l || u) {
305
- if (o = !0, Array.isArray(r.value)) {
306
- if (s = !0, u) {
307
- let e = N(i);
308
- e && (i = e.propertyName);
309
- }
310
- r.value.forEach((t) => {
311
- R.includes(t) || e.relatedSeedUids.add(t);
312
- });
313
- } else R.includes(r.value) || e.relatedSeedUids.add(r.value);
314
- }
315
- let d = r.value;
316
- o && s && Array.isArray(d) ? d = d.map((e) => String(e)) : typeof d != "string" && (d = JSON.stringify(d));
317
- let p = e.versionUidToSeedUid.get(t.refUID);
318
- if (!p) return;
319
- let m = e.assembledItems.get(p) || {};
320
- m[i] = d;
321
- let h = z(i);
322
- h !== i && (m[h] = d), e.assembledItems.set(p, m);
323
- }
324
- async function Ce(e, t, n) {
325
- let r = [];
326
- for (let n = 0; n < t.length; n++) {
327
- let i = t[n];
328
- if (!i) continue;
329
- r.push(i.id);
330
- let a = i.schema?.schemaNames?.[0]?.name ?? "unknown";
331
- e.seedUidToModelType.set(i.id, a), e.assembledItems.has(i.id) || e.assembledItems.set(i.id, {
332
- seedUid: i.id,
333
- timeCreated: i.timeCreated,
334
- attester: i.attester
335
- });
336
- }
337
- if (r.length === 0) return;
338
- let i = await n.getVersionsForSeeds(r);
339
- for (let t = 0; t < i.length; t++) {
340
- let n = i[t], r = n.refUID;
341
- e.versionUidToSeedUid.set(n.id, r);
342
- let a = e.versionsBySeedUid.get(r) || [];
343
- e.versionsBySeedUid.set(r, [...a, n]);
344
- }
345
- let a = [];
346
- for (let [t, n] of e.versionsBySeedUid.entries()) {
347
- if (e.latestVersionUidsBySeedUid.has(t)) continue;
348
- let r = [...n].sort((e, t) => t.timeCreated - e.timeCreated)[0];
349
- r && (a.push(r.id), e.latestVersionUidsBySeedUid.set(t, r.id));
350
- }
351
- if (a.length === 0) return;
352
- let o = await n.getPropertiesForVersionUids(a), s = d(o);
353
- for (let t = 0; t < s.length; t++) await Se(e, s[t]);
354
- }
355
- function we(e, t) {
356
- let n = Array.from(e.assembledItems.entries()).filter(([n]) => e.seedUidToModelType.get(n) === t);
357
- for (let [, t] of n) {
358
- let n = Object.keys(t).filter((e) => !e.startsWith("_") && !be.has(e));
359
- for (let r of n) {
360
- let n = t[r], i = b(n);
361
- if (i !== n && Array.isArray(i) && (t[r] = i, n = i), Array.isArray(n)) {
362
- let i = n;
363
- if (!i.some((t) => typeof t == "string" && !R.includes(t) && e.assembledItems.has(t))) continue;
364
- let a = [], o = [], s = !1;
365
- for (let t of i) {
366
- if (typeof t != "string" || R.includes(t)) {
367
- a.push(String(t)), o.push("unknown");
368
- continue;
369
- }
370
- let n = e.seedUidToModelType.get(t) ?? "unknown";
371
- if (n === L) {
372
- a.push(t), o.push(L);
373
- continue;
374
- }
375
- let r = e.assembledItems.get(t), i = r?.storageTransactionId ?? r?.storage_transaction_id;
376
- if (i && typeof i == "string" && i.trim()) try {
377
- a.push(_(i)), o.push(n), s = !0;
378
- } catch {
379
- a.push(t), o.push(n);
380
- }
381
- else a.push(t), o.push(n);
382
- }
383
- if (s) {
384
- let e = v(r);
385
- if (t[e] = a, A(t, e, o), y(t, e), e !== r) {
386
- delete t[r];
387
- let e = z(r);
388
- e !== r && delete t[e];
389
- }
390
- }
391
- } else if (typeof n == "string") {
392
- if (R.includes(n) || !e.assembledItems.has(n)) continue;
393
- let i = e.assembledItems.get(n);
394
- if (e.seedUidToModelType.get(n) === L) continue;
395
- let a = i?.storageTransactionId ?? i?.storage_transaction_id;
396
- if (a && typeof a == "string" && a.trim()) try {
397
- let i = r.endsWith("_id") ? r.replace(/_id$/, "") : r;
398
- t[i] = _(a);
399
- let o = e.seedUidToModelType.get(n) ?? "unknown";
400
- k(t, i, o);
401
- let s = z(i);
402
- if (s !== i && k(t, s, o), i !== r) {
403
- delete t[r];
404
- let e = z(r);
405
- e !== r && delete t[e];
406
- }
407
- } catch {}
408
- }
409
- }
410
- }
411
- }
412
- function Te(e, t, n) {
413
- if (!n) return;
414
- let r = Array.from(e.assembledItems.entries()).filter(([n]) => e.seedUidToModelType.get(n) === t);
415
- for (let [, t] of r) {
416
- let n = Object.keys(t).filter((e) => !e.startsWith("_") && !be.has(e));
417
- for (let r of n) {
418
- let n = t[r], i = b(n);
419
- i !== n && Array.isArray(i) && (t[r] = i, n = i);
420
- let a = Array.isArray(n), o = a ? n : [n], s = [], c = !1;
421
- for (let t of o) {
422
- if (typeof t != "string" || R.includes(t)) {
423
- s.push(t);
424
- continue;
425
- }
426
- let n = e.assembledItems.get(t);
427
- if (!n) {
428
- s.push(t);
429
- continue;
430
- }
431
- let r = n?.storageTransactionId ?? n?.storage_transaction_id;
432
- if (e.seedUidToModelType.get(t) === L) {
433
- let e = { ...n };
434
- B(e, t), e.schemaName = L, x(e), s.push(e), c = !0;
435
- continue;
436
- }
437
- if (r && typeof r == "string" && r.trim()) {
438
- s.push(t);
439
- continue;
440
- }
441
- let i = { ...n };
442
- B(i, t), s.push(i), c = !0;
443
- }
444
- if (c) {
445
- let e = a ? v(r) : r.endsWith("_id") ? r.replace(/_id$/, "") : r;
446
- if (t[e] = a ? s : s[0], y(t, e), e !== r) {
447
- delete t[r];
448
- let e = z(r);
449
- e !== r && delete t[e];
450
- }
451
- }
452
- }
453
- }
454
- }
455
- function Ee(e, t) {
456
- return Array.from(e.assembledItems.entries()).filter(([n]) => e.seedUidToModelType.get(n) === t).map(([n, r]) => {
457
- let i = e.latestVersionUidsBySeedUid.get(n) ?? "";
458
- i && (r.versionUid = i), B(r, n);
459
- let a = typeof r.timeCreated == "number" ? r.timeCreated : 0;
460
- return {
461
- seedUid: n,
462
- schemaName: t,
463
- attester: typeof r.attester == "string" ? r.attester : typeof r.Attester == "string" ? r.Attester : void 0,
464
- timeCreated: a,
465
- versionUid: i,
466
- data: r
467
- };
468
- });
469
- }
470
- async function V(e, t, n, r = I()) {
471
- let i = n?.expandRelations !== !1, a = n?.hydrateStorage !== !1, o = xe();
472
- await Ce(o, t, r);
473
- let s = Array.from(o.relatedSeedUids).filter((e) => !o.assembledItems.has(e));
474
- s.length > 0 && await Ce(o, await r.getSeedsByUids(s), r), we(o, e), Te(o, e, i);
475
- let c = Ee(o, e);
476
- return a && await j(c.map((e) => e.data), r.readStorageBody ? { readStorageBody: r.readStorageBody.bind(r) } : void 0), c;
477
- }
478
- //#endregion
479
- //#region src/changelog.ts
480
- var De = (e) => e.replace(/_([a-z])/g, (e, t) => t.toUpperCase());
481
- function H(e, t) {
482
- if (Object.is(e, t)) return !0;
483
- if (e === void 0 || t === void 0) return !1;
484
- if (e === null || t === null) return e === t;
485
- if (typeof e != typeof t || typeof e != "object") return !1;
486
- try {
487
- return JSON.stringify(e) === JSON.stringify(t);
488
- } catch {
489
- return !1;
490
- }
491
- }
492
- function U(e) {
493
- let t = M(e.decodedDataJson);
494
- if (!t.ok) return null;
495
- let n = t.metadata, r = n.name;
496
- if (!r) return null;
497
- let i = n.type, a = (i === "bytes32" || i === "bytes32[]") && r !== "storage_transaction_id" && r !== "storage_provider_transaction_id", o = !a && (r.endsWith("_id") || r.endsWith("_ids")) && r !== "storage_transaction_id" && r !== "storage_provider_transaction_id", s = !1, c = !1;
498
- if ((a || o) && (s = !0, Array.isArray(n.value) && (c = !0, o))) {
499
- let e = N(r);
500
- e?.isList && (r = e.propertyName);
501
- }
502
- let l = n.value;
503
- s && c && Array.isArray(l) ? l = l.map((e) => String(e)) : typeof l != "string" && (l = JSON.stringify(l));
504
- let u = De(r);
505
- return {
506
- propertyName: r,
507
- value: l,
508
- camelName: u === r ? void 0 : u
509
- };
510
- }
511
- function Oe(e) {
512
- let t = {};
513
- for (let n of e) {
514
- let e = U(n);
515
- e && (t[e.propertyName] = e.value, e.camelName && (t[e.camelName] = e.value));
516
- }
517
- return t;
518
- }
519
- function ke(e) {
520
- let t = [], n = {};
521
- for (let r of e) {
522
- let e = r.data, i = /* @__PURE__ */ new Set([...Object.keys(n), ...Object.keys(e)]), a = [];
523
- for (let t of i) H(n[t], e[t]) || a.push(t);
524
- a.sort(), t.push({
525
- type: "version",
526
- at: r.at,
527
- versionUid: r.versionUid,
528
- before: { ...n },
529
- after: { ...e },
530
- changedKeys: a
531
- }), n = e;
532
- }
533
- return t;
534
- }
535
- function Ae(e) {
536
- let t = [...e].sort((e, t) => e.timeCreated === t.timeCreated ? e.id.localeCompare(t.id) : e.timeCreated - t.timeCreated), n = /* @__PURE__ */ new Map(), r = [];
537
- for (let e of t) {
538
- let t = U(e);
539
- if (!t) continue;
540
- let i = [t.propertyName];
541
- t.camelName && i.push(t.camelName);
542
- let a = n.has(t.propertyName) ? n.get(t.propertyName) : void 0, o = t.value;
543
- H(a, o) || r.push({
544
- type: "property",
545
- at: e.timeCreated,
546
- versionUid: e.refUID,
547
- property: t.propertyName,
548
- attestationUid: e.id,
549
- previousValue: a,
550
- nextValue: o
551
- }), n.set(t.propertyName, o), t.camelName && n.set(t.camelName, o);
552
- }
553
- return r;
554
- }
555
- function je(e, t) {
556
- let n = e;
557
- if (typeof t?.since == "number") {
558
- let e = t.since;
559
- n = n.filter((t) => t.at >= e);
560
- }
561
- return typeof t?.limit == "number" && t.limit >= 0 && (n = [...n].sort((e, t) => t.at === e.at ? 0 : t.at - e.at).slice(0, t.limit).reverse()), n;
562
- }
563
- //#endregion
564
- //#region src/assembleChangelog.ts
565
- async function Me(e, t, n = I()) {
566
- let r = t?.changelog ?? {}, i = r.granularity ?? "version", a = [...await n.getVersionsForSeed(e)].sort((e, t) => e.timeCreated - t.timeCreated);
567
- if (a.length === 0) return {
568
- latestVersionUid: "",
569
- changelog: []
570
- };
571
- let o = a[a.length - 1].id, s = a.map((e) => e.id), c = await n.getPropertiesForVersionUids(s), l;
572
- if (i === "property") l = Ae(c);
573
- else {
574
- let e = /* @__PURE__ */ new Map();
575
- for (let t of c) {
576
- let n = e.get(t.refUID) ?? [];
577
- n.push(t), e.set(t.refUID, n);
578
- }
579
- let t = [];
580
- for (let n of a) {
581
- let r = e.get(n.id) ?? [], i = d(r);
582
- t.push({
583
- versionUid: n.id,
584
- at: n.timeCreated,
585
- data: Oe(i)
586
- });
587
- }
588
- l = ke(t);
589
- }
590
- return {
591
- latestVersionUid: o,
592
- changelog: je(l, r)
593
- };
594
- }
595
- //#endregion
596
- //#region src/cache/types.ts
597
- function Ne(e) {
598
- return !e || e === "data" ? "" : e === "data+changelog" ? "i1" : "i2";
599
- }
600
- function W(e) {
601
- let t = e?.expandRelations !== !1, n = e?.hydrateStorage !== !1, r = `e${+!!t}-h${+!!n}`, i = Ne(e?.include);
602
- return i ? `${r}-${i}-${e?.changelog?.granularity === "property" ? "gp" : "gv"}-${typeof e?.changelog?.since == "number" ? `s${e.changelog.since}` : "s0"}-${typeof e?.changelog?.limit == "number" ? `l${e.changelog.limit}` : "l0"}` : r;
603
- }
604
- function Pe(e) {
605
- return e.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 120);
606
- }
607
- //#endregion
608
- //#region src/cache/FileCache.ts
609
- var Fe = class {
610
- cacheDir;
611
- config;
612
- constructor(e) {
613
- this.cacheDir = e.cacheDir, this.config = e, this.ensureCacheDir().catch((e) => {
614
- console.error("Failed to create query cache directory:", e);
615
- });
616
- }
617
- async ensureCacheDir() {
618
- try {
619
- await h.mkdir(this.cacheDir, { recursive: !0 });
620
- } catch (e) {
621
- if (e.code !== "EEXIST") throw e;
622
- }
623
- }
624
- async ensureItemsDir() {
625
- try {
626
- await h.mkdir(g(this.cacheDir, "items"), { recursive: !0 });
627
- } catch (e) {
628
- if (e.code !== "EEXIST") throw e;
629
- }
630
- }
631
- collectionPath(e) {
632
- return g(this.cacheDir, `${e}.json`);
633
- }
634
- itemPath(e, t) {
635
- let n = Pe(e);
636
- return g(this.cacheDir, "items", `${n}-${t}.json`);
637
- }
638
- async getCollection(e) {
639
- try {
640
- let t = await h.readFile(this.collectionPath(e), "utf-8"), n = JSON.parse(t);
641
- return Math.floor(Date.now() / 1e3) - n.lastUpdated > this.config.ttl ? (await this.clearCollection(e), null) : n;
642
- } catch (t) {
643
- return t.code === "ENOENT" || console.error(`Error reading query collection cache for ${e}:`, t), null;
644
- }
645
- }
646
- async setCollection(e, t) {
647
- try {
648
- await this.ensureCacheDir(), await h.writeFile(this.collectionPath(e), JSON.stringify(t, null, 2), "utf-8");
649
- } catch (t) {
650
- console.error(`Error writing query collection cache for ${e}:`, t);
651
- }
652
- }
653
- async getItem(e, t) {
654
- try {
655
- let n = await h.readFile(this.itemPath(e, t), "utf-8"), r = JSON.parse(n);
656
- return Math.floor(Date.now() / 1e3) - r.lastUpdated > this.config.ttl ? (await this.clearItem(e, t), null) : r;
657
- } catch (t) {
658
- return t.code === "ENOENT" || console.error(`Error reading query item cache for ${e}:`, t), null;
659
- }
660
- }
661
- async setItem(e) {
662
- try {
663
- await this.ensureItemsDir(), await h.writeFile(this.itemPath(e.record.seedUid, e.optionsKey), JSON.stringify(e, null, 2), "utf-8");
664
- } catch (t) {
665
- console.error(`Error writing query item cache for ${e.record.seedUid}:`, t);
666
- }
667
- }
668
- async clearCollection(e) {
669
- try {
670
- await h.unlink(this.collectionPath(e));
671
- } catch (t) {
672
- t.code !== "ENOENT" && console.error(`Error clearing query collection cache for ${e}:`, t);
673
- }
674
- }
675
- async clearItem(e, t) {
676
- try {
677
- await h.unlink(this.itemPath(e, t));
678
- } catch (t) {
679
- t.code !== "ENOENT" && console.error(`Error clearing query item cache for ${e}:`, t);
680
- }
681
- }
682
- async clearAll() {
683
- try {
684
- let e = await h.readdir(this.cacheDir);
685
- for (let t of e) t.endsWith(".json") && await h.unlink(g(this.cacheDir, t));
686
- let t = g(this.cacheDir, "items");
687
- try {
688
- let e = await h.readdir(t);
689
- for (let n of e) n.endsWith(".json") && await h.unlink(g(t, n));
690
- } catch {}
691
- } catch (e) {
692
- console.error("Error clearing all query caches:", e);
693
- }
694
- }
695
- };
696
- //#endregion
697
- //#region src/cache/etag.ts
698
- function Ie(e) {
699
- return `"${ee("md5").update(e).digest("hex").substring(0, 16)}"`;
700
- }
701
- function Le(e, t, n) {
702
- return Ie(`${e}-data-${t}-${n}`);
703
- }
704
- function Re(e, t, n, r) {
705
- return Ie(`${e}-${t}-${n}-${r}`);
706
- }
707
- //#endregion
708
- //#region src/cache/MemoryCache.ts
709
- var ze = class {
710
- collectionCache = /* @__PURE__ */ new Map();
711
- itemCache = /* @__PURE__ */ new Map();
712
- config;
713
- refreshLocks = /* @__PURE__ */ new Map();
714
- constructor(e) {
715
- this.config = e;
716
- }
717
- itemKey(e, t) {
718
- return `${e}:${t}`;
719
- }
720
- getCollection(e) {
721
- let t = this.collectionCache.get(e);
722
- return t ? Math.floor(Date.now() / 1e3) - t.lastUpdated > this.config.ttl ? (this.collectionCache.delete(e), null) : t : null;
723
- }
724
- setCollection(e, t) {
725
- let n = Math.floor(Date.now() / 1e3), r = 0, i = "";
726
- for (let e of t) e.timeCreated && e.timeCreated > r && (r = e.timeCreated, i = e.seedUid);
727
- r === 0 && (r = n);
728
- let a = Le(e, r, t.length), o = {
729
- items: t.map((e) => ({
730
- ...e,
731
- data: { ...e.data }
732
- })),
733
- lastProcessedTimestamp: r,
734
- lastProcessedItemId: i,
735
- lastUpdated: n,
736
- etag: a
737
- };
738
- return this.collectionCache.set(e, o), o;
739
- }
740
- getItem(e, t) {
741
- let n = this.itemCache.get(this.itemKey(e, t));
742
- return n ? Math.floor(Date.now() / 1e3) - n.lastUpdated > this.config.ttl ? (this.itemCache.delete(this.itemKey(e, t)), null) : n : null;
743
- }
744
- setItem(e, t) {
745
- let n = Math.floor(Date.now() / 1e3), r = Re(e.seedUid, e.versionUid, e.timeCreated, t), i = {
746
- ...e,
747
- data: { ...e.data }
748
- };
749
- e.changelog && (i.changelog = [...e.changelog]);
750
- let a = {
751
- record: i,
752
- lastUpdated: n,
753
- etag: r,
754
- optionsKey: t
755
- };
756
- return this.itemCache.set(this.itemKey(e.seedUid, t), a), a;
757
- }
758
- clearCollection(e) {
759
- this.collectionCache.delete(e);
760
- }
761
- clearItem(e, t) {
762
- if (t) {
763
- this.itemCache.delete(this.itemKey(e, t));
764
- return;
765
- }
766
- for (let t of this.itemCache.keys()) t.startsWith(`${e}:`) && this.itemCache.delete(t);
767
- }
768
- clearAll() {
769
- this.collectionCache.clear(), this.itemCache.clear();
770
- }
771
- async withRefreshLock(e, t) {
772
- let n = this.refreshLocks.get(e);
773
- if (n) return n;
774
- let r = (async () => {
775
- try {
776
- return await t();
777
- } finally {
778
- this.refreshLocks.delete(e);
779
- }
780
- })();
781
- return this.refreshLocks.set(e, r), r;
782
- }
783
- updateConfig(e) {
784
- this.config = {
785
- ...this.config,
786
- ...e
787
- };
788
- }
789
- getStats() {
790
- return {
791
- collectionCount: this.collectionCache.size,
792
- itemCount: this.itemCache.size,
793
- activeLocks: this.refreshLocks.size
794
- };
795
- }
796
- }, G = class {
797
- memoryCache;
798
- fileCache;
799
- config;
800
- stats = {
801
- hits: 0,
802
- misses: 0,
803
- refreshes: 0,
804
- errors: 0
805
- };
806
- constructor(e) {
807
- this.config = e, this.memoryCache = new ze(e), this.fileCache = new Fe(e);
808
- }
809
- get enabled() {
810
- return this.config.enabled;
811
- }
812
- getConfig() {
813
- return { ...this.config };
814
- }
815
- async getCollection(e) {
816
- if (!this.config.enabled) return null;
817
- try {
818
- let t = this.memoryCache.getCollection(e);
819
- return t ? (this.stats.hits++, t) : (t = await this.fileCache.getCollection(e), t ? (this.memoryCache.setCollection(e, t.items), this.stats.hits++, t) : (this.stats.misses++, null));
820
- } catch (t) {
821
- return console.error(`Error getting query collection cache for ${e}:`, t), this.stats.errors++, null;
822
- }
823
- }
824
- async setCollection(e, t) {
825
- if (!this.config.enabled) return null;
826
- try {
827
- let n = this.memoryCache.setCollection(e, t);
828
- return await this.fileCache.setCollection(e, n), n;
829
- } catch (t) {
830
- return console.error(`Error setting query collection cache for ${e}:`, t), this.stats.errors++, null;
831
- }
832
- }
833
- async getItem(e, t) {
834
- if (!this.config.enabled) return null;
835
- try {
836
- let n = this.memoryCache.getItem(e, t);
837
- return n ? (this.stats.hits++, n) : (n = await this.fileCache.getItem(e, t), n ? (this.memoryCache.setItem(n.record, t), this.stats.hits++, n) : (this.stats.misses++, null));
838
- } catch (t) {
839
- return console.error(`Error getting query item cache for ${e}:`, t), this.stats.errors++, null;
840
- }
841
- }
842
- async setItem(e, t) {
843
- if (!this.config.enabled) return null;
844
- try {
845
- let n = this.memoryCache.setItem(e, t);
846
- return await this.fileCache.setItem(n), n;
847
- } catch (t) {
848
- return console.error(`Error setting query item cache for ${e.seedUid}:`, t), this.stats.errors++, null;
849
- }
850
- }
851
- async writeThroughItems(e, t) {
852
- if (this.config.enabled) for (let n of e) await this.setItem(n, t);
853
- }
854
- async clearCollection(e) {
855
- this.memoryCache.clearCollection(e), await this.fileCache.clearCollection(e);
856
- }
857
- async clearAll() {
858
- this.memoryCache.clearAll(), await this.fileCache.clearAll();
859
- }
860
- async withRefreshLock(e, t) {
861
- return this.memoryCache.withRefreshLock(e, t);
862
- }
863
- mergeRecords(e, t) {
864
- let n = /* @__PURE__ */ new Map();
865
- for (let t of e) t.seedUid && n.set(t.seedUid, t);
866
- for (let e of t) e.seedUid ? n.set(e.seedUid, e) : n.set(`temp-${Date.now()}-${Math.random()}`, e);
867
- let r = Array.from(n.values());
868
- return r.sort((e, t) => (t.timeCreated || 0) - (e.timeCreated || 0)), r;
869
- }
870
- filterNewRecords(e, t) {
871
- return e.filter((e) => e.timeCreated && e.timeCreated > t);
872
- }
873
- updateConfig(e) {
874
- this.config = {
875
- ...this.config,
876
- ...e
877
- }, this.memoryCache.updateConfig(this.config);
878
- }
879
- getStats() {
880
- return {
881
- ...this.stats,
882
- memoryStats: this.memoryCache.getStats()
883
- };
884
- }
885
- resetStats() {
886
- this.stats = {
887
- hits: 0,
888
- misses: 0,
889
- refreshes: 0,
890
- errors: 0
891
- };
892
- }
893
- };
894
- //#endregion
895
- //#region src/cache/config.ts
896
- function K() {
897
- let e = parseInt(process.env.CACHE_TTL || "3600", 10), t = process.env.CACHE_DIR || "./cache", n = process.env.CACHE_ENABLED === "false" || process.env.CACHE_ENABLED === "0" || process.env.CACHE_ENABLED === "no" || process.env.CACHE_ENABLED === "off", r = process.env.CACHE_ENABLED === "true" || process.env.CACHE_ENABLED === "1" || process.env.CACHE_ENABLED === "yes", i = process.env.NODE_ENV === "development", a;
898
- a = n ? !1 : r ? !0 : !i;
899
- let o = process.env.CACHE_BACKGROUND_REFRESH === "true", s = parseInt(process.env.CACHE_REFRESH_INTERVAL || "300", 10);
900
- return {
901
- ttl: e,
902
- cacheDir: t,
903
- enabled: a,
904
- backgroundRefresh: o,
905
- refreshInterval: s
906
- };
907
- }
908
- //#endregion
909
- //#region src/cache/index.ts
910
- var q = null;
911
- function J() {
912
- return q ||= new G(K()), q;
913
- }
914
- function Be() {
915
- q = null;
916
- }
917
- function Ve(e) {
918
- return new G({
919
- ...K(),
920
- ...e
921
- });
922
- }
923
- //#endregion
924
- //#region src/source/registry.ts
925
- var Y = null;
926
- function He(e) {
927
- if (e.kind !== "local") throw Error("[query] registerLocalQuerySource expects a source with kind: \"local\"");
928
- Y = e;
929
- }
930
- function Ue() {
931
- Y = null;
932
- }
933
- function We() {
934
- return Y;
935
- }
936
- function Ge() {
937
- return Y != null;
938
- }
939
- //#endregion
940
- //#region src/source/resolve.ts
941
- function X(e = "remote") {
942
- if (e === "remote") return {
943
- mode: "remote",
944
- dataSource: I(),
945
- useQueryCache: !0
946
- };
947
- let t = We();
948
- if (e === "local") {
949
- if (!t) throw Error("[query] source: \"local\" requires registerLocalQuerySource(...) (e.g. SDK DB ready)");
950
- return {
951
- mode: "local",
952
- dataSource: t,
953
- useQueryCache: !1
954
- };
955
- }
956
- return t ? {
957
- mode: "auto",
958
- dataSource: t,
959
- useQueryCache: !1
960
- } : {
961
- mode: "auto",
962
- dataSource: I(),
963
- useQueryCache: !0
964
- };
965
- }
966
- function Z(e) {
967
- return e ?? "remote";
968
- }
969
- //#endregion
970
- //#region src/api.ts
971
- function Q(e, t) {
972
- return !t || e?.cache === !1 ? !1 : J().enabled;
973
- }
974
- function Ke(e) {
975
- return e === "data+changelog" || e === "changelog";
976
- }
977
- function qe(e) {
978
- return e !== "changelog";
979
- }
980
- async function $(e, t, n, r, i) {
981
- return V(e, await i.listSeedsBySchemaName(e, {
982
- limit: t,
983
- skip: n
984
- }), r, i);
985
- }
986
- async function Je(e, t, n) {
987
- let r = await n(t);
988
- if (e !== "auto" || t.kind !== "local") return {
989
- items: r,
990
- dataSource: t,
991
- useQueryCache: t.kind === "remote"
992
- };
993
- if (r.length > 0) return {
994
- items: r,
995
- dataSource: t,
996
- useQueryCache: !1
997
- };
998
- let i = I();
999
- return {
1000
- items: await n(i),
1001
- dataSource: i,
1002
- useQueryCache: !0
1003
- };
1004
- }
1005
- async function Ye(e, n) {
1006
- await t();
1007
- let r = n?.limit ?? 100, i = n?.skip ?? 0, a = Z(n?.source), o = X(a), s = W(n), c = (t) => $(e, r, i, n, t);
1008
- if (Q(n, o.useQueryCache) && i === 0 && o.dataSource.kind === "remote") {
1009
- let t = J();
1010
- return t.withRefreshLock(e, async () => {
1011
- let a = await t.getCollection(e), c, l;
1012
- if (a) {
1013
- let i = await $(e, r, 0, n, o.dataSource), s = t.filterNewRecords(i, a.lastProcessedTimestamp);
1014
- c = s.length > 0 ? t.mergeRecords(a.items, s).slice(0, r) : a.items.slice(0, r);
1015
- } else c = await $(e, r, 0, n, o.dataSource);
1016
- return l = (await t.setCollection(e, c))?.etag, await t.writeThroughItems(c, s), {
1017
- items: c,
1018
- limit: r,
1019
- skip: i,
1020
- etag: l
1021
- };
1022
- });
1023
- }
1024
- let { items: l, dataSource: u, useQueryCache: d } = await Je(a, o.dataSource, (e) => c(e));
1025
- return Q(n, d) && u.kind === "remote" && await J().writeThroughItems(l, s), {
1026
- items: l,
1027
- limit: r,
1028
- skip: i
1029
- };
1030
- }
1031
- async function Xe(e, n) {
1032
- if (await t(), !e || typeof e != "string" || e.trim() === "") return null;
1033
- let r = e.trim(), i = n?.include ?? "data", a = Z(n?.source), o = X(a), s = W(n), c = o.dataSource, l = o.useQueryCache;
1034
- if (Q(n, l) && c.kind === "remote") {
1035
- let e = await J().getItem(r, s);
1036
- if (e) return e.record;
1037
- }
1038
- let u = await c.getSeedByUid(r);
1039
- if (!u && a === "auto" && c.kind === "local" && (c = I(), l = !0, u = await c.getSeedByUid(r), Q(n, l))) {
1040
- let e = await J().getItem(r, s);
1041
- if (e) return e.record;
1042
- }
1043
- if (!u) return null;
1044
- let d = u.schema?.schemaNames?.[0]?.name;
1045
- if (!d) return null;
1046
- let f = null;
1047
- if (!Ke(i)) f = (await V(d, [u], n, c))[0] ?? null;
1048
- else {
1049
- let { latestVersionUid: e, changelog: t } = await Me(r, n, c);
1050
- if (qe(i)) {
1051
- let e = (await V(d, [u], n, c))[0];
1052
- f = e ? {
1053
- ...e,
1054
- changelog: t
1055
- } : null;
1056
- } else f = {
1057
- seedUid: r,
1058
- schemaName: d,
1059
- attester: u.attester,
1060
- timeCreated: u.timeCreated,
1061
- versionUid: e,
1062
- data: {},
1063
- changelog: t
1064
- };
1065
- }
1066
- return f && Q(n, l) && c.kind === "remote" && await J().setItem(f, s), f;
1067
- }
1068
- async function Ze(e, n, r, i) {
1069
- await t();
1070
- let a = Z(i?.source), { items: o, dataSource: s, useQueryCache: c } = await Je(a, X(a).dataSource, async (t) => V(e, await t.listSeedsBySchemaNameForMonth(e, n, r), i, t));
1071
- if (Q(i, c) && s.kind === "remote") {
1072
- let e = W(i);
1073
- await J().writeThroughItems(o, e);
1074
- }
1075
- return o;
1076
- }
1077
- //#endregion
1078
- export { G as CacheManager, r as DEFAULT_ARWEAVE_HOST, re as FEED_RICH_BODY_STORAGE_SCHEMAS, S as RICH_BODY_STORAGE_SCHEMAS, je as applyChangelogFilters, Me as assembleSeedChangelog, V as assembleSeeds, W as buildAssembleOptionsKey, Oe as buildFlatSnapshotFromProperties, H as changelogValuesEqual, Ue as clearLocalQuerySource, Ve as createQueryCacheManager, P as createRemoteQueryDataSource, U as decodePropertyAttestation, Ae as diffPropertyAttestations, ke as diffVersionSnapshots, x as enrichImageSeedClone, ne as enrichImageSeedCloneForFeed, oe as feedRichBodyModelPriority, _ as getArweaveUrlForTransaction, se as getFeedFieldStorageModels, ce as getFeedListElementStorageModels, D as getFieldStorageModels, O as getListElementStorageModels, J as getQueryCacheManager, We as getRegisteredLocalQuerySource, I as getRemoteQueryDataSource, Xe as getSeed, Ge as hasLocalQuerySource, _e as hydrateArweaveRichTextInFeedItems, j as hydrateArweaveRichTextInItems, t as initializeQueryPlatform, pe as isArweaveTransactionGatewayUrl, ae as isFeedRichBodyStorageSchema, n as isQueryPlatformInitialized, T as isRichBodyStorageSchema, K as loadQueryCacheConfig, Z as normalizeSourceMode, M as parseEasPropertyMetadata, ve as parseEasPropertyMetadataForFeed, N as parseEasRelationPropertyName, v as publicListRelationPropertyKey, Ye as queryBySchema, Ze as queryBySchemaForMonth, He as registerLocalQuerySource, Be as resetQueryCacheManager, X as resolveQuerySource, E as richBodyModelPriority, le as setFeedFieldStorageModel, ue as setFeedListElementStorageModels, k as setFieldStorageModel, A as setListElementStorageModels, y as stripListRelationStorageAliasesForPublicKey, e as teardownQueryPlatform, b as tryCoerceJsonStringArray };
1079
-
1080
- //# sourceMappingURL=index.js.map
1
+ import { a as e, i as t, o as n, r, t as i } from "./types-DHRoT30t.js";
2
+ import { A as a, B as o, C as s, D as c, E as l, F as u, G as d, H as f, I as p, J as m, K as h, L as g, M as _, N as v, O as y, P as b, R as x, S, T as C, U as w, V as T, W as E, X as D, Y as O, Z as k, _ as A, a as j, b as M, c as N, d as P, f as F, g as I, i as L, j as R, k as z, l as B, m as V, n as H, o as U, p as W, q as G, r as K, s as q, t as J, u as Y, v as X, w as Z, x as Q, y as $, z as ee } from "./src-BoSE9m_G.js";
3
+ export { V as CacheManager, r as DEFAULT_ARWEAVE_HOST, _ as FEED_RICH_BODY_STORAGE_SCHEMAS, v as RICH_BODY_STORAGE_SCHEMAS, A as applyChangelogFilters, I as assembleSeedChangelog, s as assembleSeeds, i as buildAssembleOptionsKey, X as buildFlatSnapshotFromProperties, $ as changelogValuesEqual, U as clearLocalQuerySource, Y as createQueryCacheManager, Z as createRemoteQueryDataSource, M as decodePropertyAttestation, Q as diffPropertyAttestations, S as diffVersionSnapshots, h as enrichImageSeedClone, G as enrichImageSeedCloneForFeed, b as feedRichBodyModelPriority, k as getArweaveUrlForTransaction, u as getFeedFieldStorageModels, p as getFeedListElementStorageModels, g as getFieldStorageModels, x as getListElementStorageModels, P as getQueryCacheManager, q as getRegisteredLocalQuerySource, C as getRemoteQueryDataSource, J as getSeed, N as hasLocalQuerySource, z as hydrateArweaveRichTextInFeedItems, a as hydrateArweaveRichTextInItems, t as initializeQueryPlatform, R as isArweaveTransactionGatewayUrl, ee as isFeedRichBodyStorageSchema, e as isQueryPlatformInitialized, o as isRichBodyStorageSchema, W as loadQueryCacheConfig, L as normalizeSourceMode, c as parseEasPropertyMetadata, y as parseEasPropertyMetadataForFeed, l as parseEasRelationPropertyName, m as publicListRelationPropertyKey, H as queryBySchema, K as queryBySchemaForMonth, B as registerLocalQuerySource, F as resetQueryCacheManager, j as resolveQuerySource, T as richBodyModelPriority, f as setFeedFieldStorageModel, w as setFeedListElementStorageModels, E as setFieldStorageModel, d as setListElementStorageModels, O as stripListRelationStorageAliasesForPublicKey, n as teardownQueryPlatform, D as tryCoerceJsonStringArray };