@peerbit/document-proxy 0.0.0-e209d2e

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.
@@ -0,0 +1,748 @@
1
+ import { deserialize, serialize } from "@dao-xyz/borsh";
2
+ import { createProxyFromService } from "@dao-xyz/borsh-rpc";
3
+ import { createMessagePortTransport, } from "@peerbit/canonical-client";
4
+ import { Context, IterationRequest, PushUpdatesMode, ResultIndexedValue, ResultValue, Results, } from "@peerbit/document-interface";
5
+ import * as indexerTypes from "@peerbit/indexer-interface";
6
+ import { createSharedLogProxyFromService, } from "@peerbit/shared-log-proxy/client";
7
+ import { coercePeerRefsToHashes, } from "@peerbit/stream-interface";
8
+ import { Bytes, DocumentsChange, DocumentsCountRequest, DocumentsGetRequest, DocumentsIndexPutRequest, DocumentsIndexResult, DocumentsIterateRequest, DocumentsIteratorBatch, DocumentsIteratorService, DocumentsIteratorUpdate, DocumentsPutWithContextRequest, DocumentsRemoteOptions, DocumentsService, DocumentsWaitForRequest, OpenDocumentsRequest, } from "./protocol.js";
9
+ const ensureCustomEvent = () => {
10
+ if (typeof globalThis.CustomEvent === "function") {
11
+ return;
12
+ }
13
+ class CustomEventPolyfill extends Event {
14
+ detail;
15
+ constructor(type, params) {
16
+ super(type, params);
17
+ this.detail = params?.detail;
18
+ }
19
+ }
20
+ globalThis.CustomEvent = CustomEventPolyfill;
21
+ };
22
+ const asInstanceOf = (value, type) => {
23
+ if (!value || typeof value !== "object") {
24
+ return value;
25
+ }
26
+ if (value instanceof type) {
27
+ return value;
28
+ }
29
+ return Object.assign(Object.create(type.prototype), value);
30
+ };
31
+ const coerceWithContext = (value, context) => {
32
+ if (value && typeof value === "object") {
33
+ value.__context = context;
34
+ }
35
+ return value;
36
+ };
37
+ const coerceWithIndexed = (value, indexed) => {
38
+ if (value && typeof value === "object") {
39
+ value.__indexed = indexed;
40
+ }
41
+ return value;
42
+ };
43
+ const normalizeUpdates = (updates) => {
44
+ if (!updates) {
45
+ return { emitUpdates: false };
46
+ }
47
+ if (updates === true) {
48
+ return { merge: true, emitUpdates: true };
49
+ }
50
+ if (typeof updates === "string") {
51
+ if (updates === "local") {
52
+ return { merge: true, emitUpdates: true };
53
+ }
54
+ if (updates === "remote") {
55
+ return { push: PushUpdatesMode.STREAM, emitUpdates: true };
56
+ }
57
+ if (updates === "all") {
58
+ return { merge: true, push: PushUpdatesMode.STREAM, emitUpdates: true };
59
+ }
60
+ return { emitUpdates: false };
61
+ }
62
+ if (typeof updates === "object") {
63
+ const hasMerge = Object.prototype.hasOwnProperty.call(updates, "merge");
64
+ const merge = hasMerge
65
+ ? updates.merge === false
66
+ ? undefined
67
+ : true
68
+ : true;
69
+ const push = typeof updates.push === "number"
70
+ ? updates.push
71
+ : updates.push
72
+ ? PushUpdatesMode.STREAM
73
+ : undefined;
74
+ const emitUpdates = !!(updates.notify || updates.onBatch || merge || push);
75
+ return { push, merge, emitUpdates };
76
+ }
77
+ return { emitUpdates: false };
78
+ };
79
+ const toRemoteOptions = (remote) => {
80
+ if (!remote || typeof remote === "boolean")
81
+ return undefined;
82
+ const waitTimeoutMs = typeof remote.wait === "object" ? remote.wait?.timeout : undefined;
83
+ return new DocumentsRemoteOptions({
84
+ strategy: remote.strategy,
85
+ timeoutMs: remote.timeout,
86
+ from: remote.from,
87
+ reachEager: remote.reach?.eager,
88
+ waitTimeoutMs,
89
+ });
90
+ };
91
+ export const openDocuments = async (properties) => {
92
+ ensureCustomEvent();
93
+ const channel = await properties.client.openPort("@peerbit/document", serialize(new OpenDocumentsRequest({
94
+ id: properties.id,
95
+ type: properties.typeName,
96
+ })));
97
+ const transport = createMessagePortTransport(channel, {
98
+ requestTimeoutMs: (method) => {
99
+ if (method === "waitFor" || method === "indexWaitFor")
100
+ return undefined;
101
+ return 30_000;
102
+ },
103
+ });
104
+ const raw = createProxyFromService(DocumentsService, transport);
105
+ const logService = await raw.openLog();
106
+ const log = await createSharedLogProxyFromService(logService);
107
+ let closed = false;
108
+ const decodeChangeValue = (result) => {
109
+ if (!result.value)
110
+ return undefined;
111
+ const value = deserialize(result.value, properties.type);
112
+ coerceWithContext(value, result.context);
113
+ const indexType = properties.indexType ?? properties.type;
114
+ if (result.indexed && indexType) {
115
+ const indexed = deserialize(result.indexed, indexType);
116
+ coerceWithIndexed(value, indexed);
117
+ }
118
+ return value;
119
+ };
120
+ const decodeChange = (change) => {
121
+ return {
122
+ added: (change.added ?? []).map(decodeChangeValue).filter(Boolean),
123
+ removed: (change.removed ?? [])
124
+ .map(decodeChangeValue)
125
+ .filter(Boolean),
126
+ };
127
+ };
128
+ const changes = new EventTarget();
129
+ const onChange = (e) => {
130
+ const detail = decodeChange(e.detail);
131
+ changes.dispatchEvent(new CustomEvent("change", { detail }));
132
+ };
133
+ raw.changes.addEventListener("change", onChange);
134
+ const coerceIdKey = (id) => {
135
+ return id instanceof indexerTypes.IdKey ? id : indexerTypes.toId(id);
136
+ };
137
+ const createGetRequest = (id, options, resolveOverride) => {
138
+ if (options?.signal?.aborted) {
139
+ throw new Error("AbortError");
140
+ }
141
+ const resolve = resolveOverride !== undefined
142
+ ? resolveOverride !== false
143
+ : options?.resolve !== false;
144
+ const remoteOption = options?.remote;
145
+ const remoteOptions = toRemoteOptions(remoteOption);
146
+ const remote = typeof remoteOption === "boolean"
147
+ ? remoteOption
148
+ : remoteOption
149
+ ? true
150
+ : undefined;
151
+ return {
152
+ resolve: resolve,
153
+ request: new DocumentsGetRequest({
154
+ id: coerceIdKey(id),
155
+ resolve,
156
+ local: options?.local,
157
+ remote,
158
+ remoteOptions,
159
+ waitForMs: options?.waitFor,
160
+ }),
161
+ };
162
+ };
163
+ const fetchValue = async (id, options, resolveOverride) => {
164
+ const { request, resolve } = createGetRequest(id, options, resolveOverride);
165
+ const result = await raw.get(request);
166
+ if (!result)
167
+ return undefined;
168
+ return decodeIndexResult(result, resolve);
169
+ };
170
+ const get = async (id, options) => {
171
+ return fetchValue(id, options, true);
172
+ };
173
+ const idPath = indexerTypes.getIdProperty(properties.indexType ?? properties.type) ??
174
+ ["id"];
175
+ const resolveId = (value) => {
176
+ let candidate = value;
177
+ if (candidate && typeof candidate === "object") {
178
+ if ("__indexed" in candidate && candidate.__indexed) {
179
+ candidate = candidate.__indexed;
180
+ }
181
+ }
182
+ const resolved = indexerTypes.extractFieldValue(candidate, idPath);
183
+ return indexerTypes.toId(resolved);
184
+ };
185
+ const decodeIndexResult = (result, resolve) => {
186
+ const context = result.context;
187
+ const indexType = properties.indexType ?? properties.type;
188
+ if (resolve !== false) {
189
+ if (!result.value)
190
+ return undefined;
191
+ const value = deserialize(result.value, properties.type);
192
+ coerceWithContext(value, context);
193
+ if (result.indexed && indexType) {
194
+ const indexed = deserialize(result.indexed, indexType);
195
+ coerceWithIndexed(value, indexed);
196
+ }
197
+ return value;
198
+ }
199
+ if (result.indexed && indexType) {
200
+ const indexed = deserialize(result.indexed, indexType);
201
+ coerceWithContext(indexed, context);
202
+ return indexed;
203
+ }
204
+ return undefined;
205
+ };
206
+ const decodeIteratorBatch = (batch, resolve) => {
207
+ const items = (batch.results ?? [])
208
+ .map((result) => decodeIndexResult(result, resolve))
209
+ .filter(Boolean);
210
+ return { items, done: batch.done };
211
+ };
212
+ const decodeUpdateResults = (update, resolve) => {
213
+ return (update.results ?? [])
214
+ .map((result) => decodeIndexResult(result, resolve))
215
+ .filter(Boolean);
216
+ };
217
+ const toNumber = (value) => {
218
+ return typeof value === "bigint" ? Number(value) : value;
219
+ };
220
+ const randomId = () => {
221
+ if (typeof crypto !== "undefined" &&
222
+ typeof crypto.randomUUID === "function") {
223
+ return crypto.randomUUID();
224
+ }
225
+ return `${Date.now()}-${Math.random().toString(16).slice(2)}`;
226
+ };
227
+ const abortError = () => new Error("AbortError");
228
+ const createWaitForRequest = (peers, options, requestId) => {
229
+ if (options?.signal?.aborted) {
230
+ throw new Error("AbortError");
231
+ }
232
+ const hashes = coercePeerRefsToHashes(peers);
233
+ return new DocumentsWaitForRequest({
234
+ peers: hashes,
235
+ seek: options?.seek,
236
+ timeoutMs: options?.timeout,
237
+ requestId,
238
+ });
239
+ };
240
+ const waitFor = async (peers, options) => {
241
+ const signal = options?.signal;
242
+ const requestId = signal ? randomId() : undefined;
243
+ const request = createWaitForRequest(peers, options, requestId);
244
+ const call = raw.waitFor(request);
245
+ if (!signal || !requestId) {
246
+ return call;
247
+ }
248
+ return new Promise((resolve, reject) => {
249
+ const cleanup = () => {
250
+ signal.removeEventListener("abort", onAbort);
251
+ };
252
+ const onAbort = () => {
253
+ cleanup();
254
+ void raw.cancelWait(requestId).catch(() => { });
255
+ reject(abortError());
256
+ };
257
+ if (signal.aborted) {
258
+ onAbort();
259
+ return;
260
+ }
261
+ signal.addEventListener("abort", onAbort, { once: true });
262
+ call.then((value) => {
263
+ cleanup();
264
+ resolve(value);
265
+ }, (error) => {
266
+ cleanup();
267
+ reject(error);
268
+ });
269
+ });
270
+ };
271
+ const indexWaitFor = async (peers, options) => {
272
+ const signal = options?.signal;
273
+ const requestId = signal ? randomId() : undefined;
274
+ const request = createWaitForRequest(peers, options, requestId);
275
+ const call = raw.indexWaitFor(request);
276
+ if (!signal || !requestId) {
277
+ return call;
278
+ }
279
+ return new Promise((resolve, reject) => {
280
+ const cleanup = () => {
281
+ signal.removeEventListener("abort", onAbort);
282
+ };
283
+ const onAbort = () => {
284
+ cleanup();
285
+ void raw.cancelWait(requestId).catch(() => { });
286
+ reject(abortError());
287
+ };
288
+ if (signal.aborted) {
289
+ onAbort();
290
+ return;
291
+ }
292
+ signal.addEventListener("abort", onAbort, { once: true });
293
+ call.then((value) => {
294
+ cleanup();
295
+ resolve(value);
296
+ }, (error) => {
297
+ cleanup();
298
+ reject(error);
299
+ });
300
+ });
301
+ };
302
+ const indexSize = async () => {
303
+ const size = await raw.indexSize();
304
+ return toNumber(size);
305
+ };
306
+ const index = {
307
+ get: async (id, options) => {
308
+ return fetchValue(id, options);
309
+ },
310
+ getDetailed: async (id, options) => {
311
+ const resolve = options?.resolve !== undefined ? options.resolve !== false : true;
312
+ const remoteOption = options?.remote;
313
+ const remoteOptions = toRemoteOptions(remoteOption);
314
+ const remote = typeof remoteOption === "boolean"
315
+ ? remoteOption
316
+ : remoteOption
317
+ ? true
318
+ : undefined;
319
+ const result = await raw.get(new DocumentsGetRequest({
320
+ id: coerceIdKey(id),
321
+ resolve,
322
+ local: options?.local,
323
+ remote,
324
+ remoteOptions,
325
+ }));
326
+ if (!result)
327
+ return undefined;
328
+ if (resolve) {
329
+ const value = decodeIndexResult(result, true);
330
+ if (!value)
331
+ return undefined;
332
+ const entry = new ResultValue({
333
+ source: result.value,
334
+ context: result.context,
335
+ value,
336
+ });
337
+ return [new Results({ results: [entry], kept: 0n })];
338
+ }
339
+ const indexed = decodeIndexResult(result, false);
340
+ if (!indexed)
341
+ return undefined;
342
+ const entry = new ResultIndexedValue({
343
+ source: result.indexed ?? new Uint8Array(),
344
+ indexed,
345
+ entries: [],
346
+ context: result.context,
347
+ });
348
+ return [new Results({ results: [entry], kept: 0n })];
349
+ },
350
+ resolveId,
351
+ iterate: (query, options) => {
352
+ const updates = normalizeUpdates(options?.updates);
353
+ const remoteOption = options?.remote;
354
+ const remoteOptions = toRemoteOptions(remoteOption);
355
+ let remote = typeof remoteOption === "boolean"
356
+ ? remoteOption
357
+ : remoteOption
358
+ ? true
359
+ : undefined;
360
+ let replicate = typeof remoteOption === "object" && remoteOption?.replicate
361
+ ? true
362
+ : false;
363
+ if (updates.push && remote !== false) {
364
+ remote = true;
365
+ replicate = true;
366
+ }
367
+ const queryObject = query;
368
+ const isQueryWrapper = !!queryObject &&
369
+ typeof queryObject === "object" &&
370
+ !Array.isArray(queryObject) &&
371
+ ("query" in queryObject ||
372
+ ("sort" in queryObject &&
373
+ Object.keys(queryObject).every((k) => k === "sort" || k === "query")));
374
+ const queryValue = isQueryWrapper ? queryObject.query : queryObject;
375
+ const sortValue = isQueryWrapper ? queryObject.sort : undefined;
376
+ const request = query instanceof IterationRequest
377
+ ? query
378
+ : new IterationRequest({
379
+ query: queryValue,
380
+ sort: sortValue,
381
+ fetch: options?.fetch ?? 10,
382
+ resolve: options?.resolve !== false,
383
+ replicate: options?.resolve !== false ? false : replicate,
384
+ });
385
+ const resolveBool = options?.resolve !== undefined
386
+ ? options.resolve !== false
387
+ : request.resolve !== false;
388
+ const resolve = resolveBool;
389
+ if (options?.resolve !== undefined) {
390
+ request.resolve = resolveBool;
391
+ }
392
+ if (!resolve && replicate && request.replicate !== true) {
393
+ request.replicate = true;
394
+ }
395
+ if (updates.emitUpdates) {
396
+ request.pushUpdates = updates.push;
397
+ request.mergeUpdates = updates.merge;
398
+ }
399
+ const emitUpdates = updates.emitUpdates ||
400
+ request.pushUpdates != null ||
401
+ request.mergeUpdates != null;
402
+ const iterateRequest = new DocumentsIterateRequest({
403
+ request,
404
+ local: options?.local,
405
+ remote,
406
+ remoteOptions,
407
+ closePolicy: options?.closePolicy,
408
+ emitUpdates,
409
+ });
410
+ let done = false;
411
+ let updatesListener;
412
+ const abortSignal = options?.signal;
413
+ let abortListener;
414
+ const proxyPromise = raw.iterate(iterateRequest);
415
+ const createIterator = (service) => {
416
+ if (updates.emitUpdates &&
417
+ options?.updates &&
418
+ typeof options.updates === "object") {
419
+ const updateConfig = options.updates;
420
+ updatesListener = (event) => {
421
+ const detail = event?.detail;
422
+ if (!detail)
423
+ return;
424
+ const reason = detail.reason;
425
+ if (updateConfig.notify) {
426
+ updateConfig.notify(reason);
427
+ }
428
+ if (updateConfig.onBatch && detail.results?.length) {
429
+ const items = decodeUpdateResults(detail, resolve);
430
+ if (items.length) {
431
+ updateConfig.onBatch(items, { reason });
432
+ }
433
+ }
434
+ };
435
+ service.updates.addEventListener("update", updatesListener);
436
+ }
437
+ const iterator = {
438
+ next: async (amount) => {
439
+ if (abortSignal?.aborted) {
440
+ throw abortError();
441
+ }
442
+ const batch = await service.next(amount);
443
+ const decoded = decodeIteratorBatch(batch, resolve);
444
+ done = decoded.done;
445
+ return decoded.items;
446
+ },
447
+ pending: async () => {
448
+ if (abortSignal?.aborted) {
449
+ throw abortError();
450
+ }
451
+ const pending = await service.pending();
452
+ return pending != null ? Number(pending) : undefined;
453
+ },
454
+ done: () => done,
455
+ all: async () => {
456
+ if (abortSignal?.aborted) {
457
+ throw abortError();
458
+ }
459
+ const out = [];
460
+ while (!done) {
461
+ const next = await iterator.next(options?.fetch ?? 10);
462
+ out.push(...next);
463
+ }
464
+ return out;
465
+ },
466
+ first: async () => {
467
+ if (abortSignal?.aborted) {
468
+ throw abortError();
469
+ }
470
+ const next = await iterator.next(1);
471
+ return next[0];
472
+ },
473
+ close: async () => {
474
+ done = true;
475
+ if (abortSignal && abortListener) {
476
+ abortSignal.removeEventListener("abort", abortListener);
477
+ }
478
+ if (updatesListener) {
479
+ service.updates.removeEventListener("update", updatesListener);
480
+ }
481
+ await service.close();
482
+ },
483
+ [Symbol.asyncIterator]: () => {
484
+ return {
485
+ next: async () => {
486
+ const items = await iterator.next(1);
487
+ if (items.length === 0) {
488
+ return { done: true, value: undefined };
489
+ }
490
+ return { done: false, value: items[0] };
491
+ },
492
+ return: async () => {
493
+ await iterator.close();
494
+ return { done: true, value: undefined };
495
+ },
496
+ };
497
+ },
498
+ };
499
+ return iterator;
500
+ };
501
+ let iterator;
502
+ abortListener = () => {
503
+ void iterator.close();
504
+ };
505
+ iterator = {
506
+ next: async (amount) => {
507
+ if (abortSignal?.aborted) {
508
+ throw abortError();
509
+ }
510
+ const service = await proxyPromise;
511
+ Object.assign(iterator, createIterator(service));
512
+ return iterator.next(amount);
513
+ },
514
+ pending: async () => {
515
+ if (abortSignal?.aborted) {
516
+ throw abortError();
517
+ }
518
+ const service = await proxyPromise;
519
+ Object.assign(iterator, createIterator(service));
520
+ return iterator.pending();
521
+ },
522
+ done: () => done,
523
+ all: async () => {
524
+ if (abortSignal?.aborted) {
525
+ throw abortError();
526
+ }
527
+ const service = await proxyPromise;
528
+ Object.assign(iterator, createIterator(service));
529
+ return iterator.all();
530
+ },
531
+ first: async () => {
532
+ if (abortSignal?.aborted) {
533
+ throw abortError();
534
+ }
535
+ const service = await proxyPromise;
536
+ Object.assign(iterator, createIterator(service));
537
+ return iterator.first();
538
+ },
539
+ close: async () => {
540
+ const service = await proxyPromise;
541
+ Object.assign(iterator, createIterator(service));
542
+ return iterator.close();
543
+ },
544
+ [Symbol.asyncIterator]: () => {
545
+ return {
546
+ next: async () => {
547
+ if (abortSignal?.aborted) {
548
+ throw abortError();
549
+ }
550
+ const service = await proxyPromise;
551
+ Object.assign(iterator, createIterator(service));
552
+ const iter = iterator[Symbol.asyncIterator]();
553
+ return iter.next();
554
+ },
555
+ return: async () => {
556
+ if (abortSignal?.aborted) {
557
+ return { done: true, value: undefined };
558
+ }
559
+ const service = await proxyPromise;
560
+ Object.assign(iterator, createIterator(service));
561
+ const iter = iterator[Symbol.asyncIterator]();
562
+ return iter.return
563
+ ? iter.return()
564
+ : { done: true, value: undefined };
565
+ },
566
+ };
567
+ },
568
+ };
569
+ if (abortSignal) {
570
+ if (abortSignal.aborted) {
571
+ throw abortError();
572
+ }
573
+ abortSignal.addEventListener("abort", abortListener, { once: true });
574
+ }
575
+ return iterator;
576
+ },
577
+ search: async (query, options) => {
578
+ const iterator = index.iterate(query, {
579
+ resolve: options?.resolve,
580
+ local: options?.local,
581
+ remote: options?.remote,
582
+ fetch: options?.fetch ?? 0xffffffff,
583
+ });
584
+ const out = await iterator.all();
585
+ await iterator.close();
586
+ const seen = new Set();
587
+ return out.filter((item) => {
588
+ const id = resolveId(item).primitive;
589
+ if (seen.has(String(id)))
590
+ return false;
591
+ seen.add(String(id));
592
+ return true;
593
+ });
594
+ },
595
+ getSize: indexSize,
596
+ waitFor: indexWaitFor,
597
+ };
598
+ const countByIterate = async (query) => {
599
+ const iterator = index.iterate(query ? { query } : undefined, {
600
+ resolve: false,
601
+ fetch: 100,
602
+ local: true,
603
+ remote: false,
604
+ });
605
+ let total = 0;
606
+ while (!iterator.done()) {
607
+ const batch = await iterator.next(100);
608
+ total += batch.length;
609
+ }
610
+ await iterator.close();
611
+ return total;
612
+ };
613
+ const toCountEstimate = (estimate) => ({
614
+ estimate,
615
+ errorMargin: undefined,
616
+ });
617
+ async function count(options) {
618
+ const approximate = options?.approximate === true || typeof options?.approximate === "object";
619
+ if (options?.query) {
620
+ const total = await countByIterate(options.query);
621
+ return approximate ? toCountEstimate(total) : total;
622
+ }
623
+ const total = await raw.count(new DocumentsCountRequest({ approximate }));
624
+ const estimate = toNumber(total);
625
+ return approximate ? toCountEstimate(estimate) : estimate;
626
+ }
627
+ class WrappedIndexedType {
628
+ __context;
629
+ constructor(value, context) {
630
+ Object.assign(this, value);
631
+ this.__context = context;
632
+ }
633
+ }
634
+ const putWithContext = async (value, id, context) => {
635
+ const ctx = context instanceof Context ? context : new Context(context);
636
+ const request = new DocumentsPutWithContextRequest({
637
+ value: new Bytes({ value: serialize(value) }),
638
+ id: coerceIdKey(id),
639
+ context: ctx,
640
+ });
641
+ await raw.putWithContext(request);
642
+ };
643
+ const indexPut = async (value) => {
644
+ const ctx = value?.__context;
645
+ if (!ctx) {
646
+ throw new Error("Missing __context for index.put");
647
+ }
648
+ const context = ctx instanceof Context ? ctx : new Context(ctx);
649
+ const indexType = properties.indexType ?? properties.type;
650
+ const stripped = { ...value };
651
+ delete stripped.__context;
652
+ const indexed = asInstanceOf(stripped, indexType);
653
+ const request = new DocumentsIndexPutRequest({
654
+ indexed: serialize(indexed),
655
+ context,
656
+ });
657
+ await raw.indexPut(request);
658
+ };
659
+ const createIndexIterator = (iterator) => {
660
+ const toIndexedResult = (value) => ({
661
+ id: resolveId(value),
662
+ value,
663
+ });
664
+ return {
665
+ next: async (amount) => {
666
+ const batch = await iterator.next(amount);
667
+ return batch.map(toIndexedResult);
668
+ },
669
+ all: async () => {
670
+ const batch = await iterator.all();
671
+ return batch.map(toIndexedResult);
672
+ },
673
+ done: () => iterator.done(),
674
+ pending: async () => {
675
+ const pending = await iterator.pending();
676
+ return pending ?? 0;
677
+ },
678
+ close: async () => {
679
+ await iterator.close();
680
+ },
681
+ };
682
+ };
683
+ index.putWithContext = putWithContext;
684
+ index.wrappedIndexedType = WrappedIndexedType;
685
+ index.index = {
686
+ count: async (options) => {
687
+ return countByIterate(options?.query);
688
+ },
689
+ getSize: indexSize,
690
+ get: async (id, _options) => {
691
+ const value = await fetchValue(id, {
692
+ resolve: false,
693
+ local: true,
694
+ remote: false,
695
+ });
696
+ return value
697
+ ? { id: coerceIdKey(id), value: value }
698
+ : undefined;
699
+ },
700
+ iterate: (request, _options) => {
701
+ const iterator = index.iterate(request, {
702
+ resolve: false,
703
+ local: true,
704
+ remote: false,
705
+ });
706
+ return createIndexIterator(iterator);
707
+ },
708
+ put: indexPut,
709
+ };
710
+ const close = async () => {
711
+ if (closed)
712
+ return;
713
+ closed = true;
714
+ raw.changes.removeEventListener("change", onChange);
715
+ try {
716
+ await log.close();
717
+ await raw.close();
718
+ }
719
+ finally {
720
+ channel.close?.();
721
+ }
722
+ };
723
+ const proxy = {
724
+ raw,
725
+ log,
726
+ events: changes,
727
+ changes,
728
+ index,
729
+ put: async (doc) => raw.put(new Bytes({ value: serialize(doc) })),
730
+ get,
731
+ del: async (id) => raw.del(coerceIdKey(id)),
732
+ count,
733
+ waitFor,
734
+ recover: async () => {
735
+ await raw.recover();
736
+ },
737
+ close,
738
+ };
739
+ Object.defineProperty(proxy, "closed", {
740
+ get: () => closed,
741
+ set: (value) => {
742
+ closed = value;
743
+ },
744
+ enumerable: true,
745
+ });
746
+ return proxy;
747
+ };
748
+ //# sourceMappingURL=client.js.map