@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,481 @@
1
+ import { field, fixedArray, option, variant, vec } from "@dao-xyz/borsh";
2
+ import { events, method, service } from "@dao-xyz/borsh-rpc";
3
+ import { Context, IterationRequest } from "@peerbit/document-interface";
4
+ import { IdKey } from "@peerbit/indexer-interface";
5
+ import { SharedLogService } from "@peerbit/shared-log-proxy";
6
+
7
+ @variant("bytes")
8
+ export class Bytes {
9
+ @field({ type: Uint8Array })
10
+ value: Uint8Array;
11
+
12
+ constructor(properties?: { value?: Uint8Array }) {
13
+ this.value = properties?.value ?? new Uint8Array();
14
+ }
15
+ }
16
+
17
+ @variant("documents_index_result")
18
+ export class DocumentsIndexResult {
19
+ @field({ type: Context })
20
+ context: Context;
21
+
22
+ @field({ type: option(Uint8Array) })
23
+ value?: Uint8Array;
24
+
25
+ @field({ type: option(Uint8Array) })
26
+ indexed?: Uint8Array;
27
+
28
+ constructor(properties: {
29
+ context: Context;
30
+ value?: Uint8Array;
31
+ indexed?: Uint8Array;
32
+ }) {
33
+ this.context = properties.context;
34
+ this.value = properties.value;
35
+ this.indexed = properties.indexed;
36
+ }
37
+ }
38
+
39
+ @variant("documents_change")
40
+ export class DocumentsChange {
41
+ @field({ type: vec(DocumentsIndexResult) })
42
+ added: DocumentsIndexResult[];
43
+
44
+ @field({ type: vec(DocumentsIndexResult) })
45
+ removed: DocumentsIndexResult[];
46
+
47
+ constructor(properties?: {
48
+ added?: DocumentsIndexResult[];
49
+ removed?: DocumentsIndexResult[];
50
+ }) {
51
+ this.added = properties?.added ?? [];
52
+ this.removed = properties?.removed ?? [];
53
+ }
54
+ }
55
+
56
+ @variant("open_documents")
57
+ export class OpenDocumentsRequest {
58
+ @field({ type: fixedArray("u8", 32) })
59
+ id: Uint8Array;
60
+
61
+ @field({ type: "string" })
62
+ type: string;
63
+
64
+ constructor(properties?: { id?: Uint8Array; type?: string }) {
65
+ this.id = properties?.id ?? new Uint8Array(32);
66
+ this.type = properties?.type ?? "";
67
+ }
68
+ }
69
+
70
+ @variant("documents_remote_options")
71
+ export class DocumentsRemoteOptions {
72
+ @field({ type: option("string") })
73
+ strategy?: "fallback";
74
+
75
+ @field({ type: option("u32") })
76
+ timeoutMs?: number;
77
+
78
+ @field({ type: option(vec("string")) })
79
+ from?: string[];
80
+
81
+ @field({ type: option("bool") })
82
+ reachEager?: boolean;
83
+
84
+ @field({ type: option("u32") })
85
+ waitTimeoutMs?: number;
86
+
87
+ constructor(properties?: {
88
+ strategy?: "fallback";
89
+ timeoutMs?: number;
90
+ from?: string[];
91
+ reachEager?: boolean;
92
+ waitTimeoutMs?: number;
93
+ }) {
94
+ this.strategy = properties?.strategy;
95
+ this.timeoutMs = properties?.timeoutMs;
96
+ this.from = properties?.from;
97
+ this.reachEager = properties?.reachEager;
98
+ this.waitTimeoutMs = properties?.waitTimeoutMs;
99
+ }
100
+ }
101
+
102
+ @variant("documents_get_request")
103
+ export class DocumentsGetRequest {
104
+ @field({ type: IdKey })
105
+ id: IdKey;
106
+
107
+ @field({ type: option("bool") })
108
+ resolve?: boolean;
109
+
110
+ @field({ type: option("bool") })
111
+ local?: boolean;
112
+
113
+ @field({ type: option("bool") })
114
+ remote?: boolean;
115
+
116
+ @field({ type: option(DocumentsRemoteOptions) })
117
+ remoteOptions?: DocumentsRemoteOptions;
118
+
119
+ @field({ type: option("u32") })
120
+ waitForMs?: number;
121
+
122
+ constructor(properties: {
123
+ id: IdKey;
124
+ resolve?: boolean;
125
+ local?: boolean;
126
+ remote?: boolean;
127
+ remoteOptions?: DocumentsRemoteOptions;
128
+ waitForMs?: number;
129
+ }) {
130
+ this.id = properties.id;
131
+ this.resolve = properties.resolve;
132
+ this.local = properties.local;
133
+ this.remote = properties.remote;
134
+ this.remoteOptions = properties.remoteOptions;
135
+ this.waitForMs = properties.waitForMs;
136
+ }
137
+ }
138
+
139
+ @variant("documents_iterator_batch")
140
+ export class DocumentsIteratorBatch {
141
+ @field({ type: vec(DocumentsIndexResult) })
142
+ results: DocumentsIndexResult[];
143
+
144
+ @field({ type: "bool" })
145
+ done: boolean;
146
+
147
+ constructor(properties: {
148
+ results?: DocumentsIndexResult[];
149
+ done?: boolean;
150
+ }) {
151
+ this.results = properties.results ?? [];
152
+ this.done = properties.done ?? false;
153
+ }
154
+ }
155
+
156
+ @variant("documents_iterator_update")
157
+ export class DocumentsIteratorUpdate {
158
+ @field({ type: "string" })
159
+ reason: string;
160
+
161
+ @field({ type: vec(DocumentsIndexResult) })
162
+ results: DocumentsIndexResult[];
163
+
164
+ constructor(properties: {
165
+ reason: string;
166
+ results?: DocumentsIndexResult[];
167
+ }) {
168
+ this.reason = properties.reason;
169
+ this.results = properties.results ?? [];
170
+ }
171
+ }
172
+
173
+ @variant("documents_iterate_request")
174
+ export class DocumentsIterateRequest {
175
+ @field({ type: IterationRequest })
176
+ request: IterationRequest;
177
+
178
+ @field({ type: option("bool") })
179
+ local?: boolean;
180
+
181
+ @field({ type: option("bool") })
182
+ remote?: boolean;
183
+
184
+ @field({ type: option(DocumentsRemoteOptions) })
185
+ remoteOptions?: DocumentsRemoteOptions;
186
+
187
+ @field({ type: option("string") })
188
+ closePolicy?: "onEmpty" | "manual";
189
+
190
+ @field({ type: "bool" })
191
+ emitUpdates: boolean;
192
+
193
+ constructor(properties: {
194
+ request: IterationRequest;
195
+ local?: boolean;
196
+ remote?: boolean;
197
+ remoteOptions?: DocumentsRemoteOptions;
198
+ closePolicy?: "onEmpty" | "manual";
199
+ emitUpdates?: boolean;
200
+ }) {
201
+ this.request = properties.request;
202
+ this.local = properties.local;
203
+ this.remote = properties.remote;
204
+ this.remoteOptions = properties.remoteOptions;
205
+ this.closePolicy = properties.closePolicy;
206
+ this.emitUpdates = properties.emitUpdates ?? false;
207
+ }
208
+ }
209
+
210
+ @variant("documents_put_with_context")
211
+ export class DocumentsPutWithContextRequest {
212
+ @field({ type: Bytes })
213
+ value: Bytes;
214
+
215
+ @field({ type: IdKey })
216
+ id: IdKey;
217
+
218
+ @field({ type: Context })
219
+ context: Context;
220
+
221
+ constructor(properties: { value: Bytes; id: IdKey; context: Context }) {
222
+ this.value = properties.value;
223
+ this.id = properties.id;
224
+ this.context = properties.context;
225
+ }
226
+ }
227
+
228
+ @variant("documents_index_put")
229
+ export class DocumentsIndexPutRequest {
230
+ @field({ type: Uint8Array })
231
+ indexed: Uint8Array;
232
+
233
+ @field({ type: Context })
234
+ context: Context;
235
+
236
+ constructor(properties: { indexed: Uint8Array; context: Context }) {
237
+ this.indexed = properties.indexed;
238
+ this.context = properties.context;
239
+ }
240
+ }
241
+
242
+ @variant("documents_count")
243
+ export class DocumentsCountRequest {
244
+ @field({ type: "bool" })
245
+ approximate: boolean;
246
+
247
+ constructor(properties?: { approximate?: boolean }) {
248
+ this.approximate = properties?.approximate ?? true;
249
+ }
250
+ }
251
+
252
+ @variant("documents_wait_for")
253
+ export class DocumentsWaitForRequest {
254
+ @field({ type: vec("string") })
255
+ peers: string[];
256
+
257
+ @field({ type: option("string") })
258
+ seek?: "any" | "present";
259
+
260
+ @field({ type: option("u32") })
261
+ timeoutMs?: number;
262
+
263
+ @field({ type: option("string") })
264
+ requestId?: string;
265
+
266
+ constructor(properties: {
267
+ peers: string[];
268
+ seek?: "any" | "present";
269
+ timeoutMs?: number;
270
+ requestId?: string;
271
+ }) {
272
+ this.peers = properties.peers;
273
+ this.seek = properties.seek;
274
+ this.timeoutMs = properties.timeoutMs;
275
+ this.requestId = properties.requestId;
276
+ }
277
+ }
278
+
279
+ @service()
280
+ export class DocumentsIteratorService {
281
+ private _impl:
282
+ | {
283
+ next: (amount: number) => Promise<DocumentsIteratorBatch>;
284
+ pending: () => Promise<bigint | undefined>;
285
+ done: () => Promise<boolean>;
286
+ close: () => Promise<void>;
287
+ }
288
+ | undefined;
289
+
290
+ @events(DocumentsIteratorUpdate)
291
+ updates = new EventTarget();
292
+
293
+ constructor(impl?: {
294
+ next: (amount: number) => Promise<DocumentsIteratorBatch>;
295
+ pending: () => Promise<bigint | undefined>;
296
+ done: () => Promise<boolean>;
297
+ close: () => Promise<void>;
298
+ }) {
299
+ this._impl = impl;
300
+ }
301
+
302
+ @method({ args: "u32", returns: DocumentsIteratorBatch })
303
+ async next(amount: number): Promise<DocumentsIteratorBatch> {
304
+ if (!this._impl) throw new Error("DocumentsIteratorService not bound");
305
+ return this._impl.next(amount);
306
+ }
307
+
308
+ @method({ returns: option("u64") })
309
+ async pending(): Promise<bigint | undefined> {
310
+ if (!this._impl) throw new Error("DocumentsIteratorService not bound");
311
+ return this._impl.pending();
312
+ }
313
+
314
+ @method({ returns: "bool" })
315
+ async done(): Promise<boolean> {
316
+ if (!this._impl) throw new Error("DocumentsIteratorService not bound");
317
+ return this._impl.done();
318
+ }
319
+
320
+ @method({ returns: "void" })
321
+ async close(): Promise<void> {
322
+ if (!this._impl) throw new Error("DocumentsIteratorService not bound");
323
+ return this._impl.close();
324
+ }
325
+ }
326
+
327
+ @service()
328
+ export class DocumentsService {
329
+ private _impl:
330
+ | {
331
+ put: (doc: Bytes) => Promise<void>;
332
+ get: (
333
+ request: DocumentsGetRequest,
334
+ ) => Promise<DocumentsIndexResult | undefined>;
335
+ del: (id: IdKey) => Promise<void>;
336
+ iterate: (
337
+ request: DocumentsIterateRequest,
338
+ ) => Promise<DocumentsIteratorService>;
339
+ putWithContext?: (
340
+ request: DocumentsPutWithContextRequest,
341
+ ) => Promise<void>;
342
+ indexPut?: (request: DocumentsIndexPutRequest) => Promise<void>;
343
+ count?: (request: DocumentsCountRequest) => Promise<bigint>;
344
+ indexSize?: () => Promise<bigint>;
345
+ waitFor?: (request: DocumentsWaitForRequest) => Promise<string[]>;
346
+ indexWaitFor?: (request: DocumentsWaitForRequest) => Promise<string[]>;
347
+ cancelWait?: (requestId: string) => Promise<void>;
348
+ recover?: () => Promise<void>;
349
+ openLog?: () => Promise<SharedLogService>;
350
+ close: () => Promise<void>;
351
+ }
352
+ | undefined;
353
+
354
+ @events(DocumentsChange)
355
+ changes = new EventTarget();
356
+
357
+ constructor(impl?: {
358
+ put: (doc: Bytes) => Promise<void>;
359
+ get: (
360
+ request: DocumentsGetRequest,
361
+ ) => Promise<DocumentsIndexResult | undefined>;
362
+ del: (id: IdKey) => Promise<void>;
363
+ iterate: (
364
+ request: DocumentsIterateRequest,
365
+ ) => Promise<DocumentsIteratorService>;
366
+ putWithContext?: (request: DocumentsPutWithContextRequest) => Promise<void>;
367
+ indexPut?: (request: DocumentsIndexPutRequest) => Promise<void>;
368
+ count?: (request: DocumentsCountRequest) => Promise<bigint>;
369
+ indexSize?: () => Promise<bigint>;
370
+ waitFor?: (request: DocumentsWaitForRequest) => Promise<string[]>;
371
+ indexWaitFor?: (request: DocumentsWaitForRequest) => Promise<string[]>;
372
+ cancelWait?: (requestId: string) => Promise<void>;
373
+ recover?: () => Promise<void>;
374
+ openLog?: () => Promise<SharedLogService>;
375
+ close: () => Promise<void>;
376
+ }) {
377
+ this._impl = impl;
378
+ }
379
+
380
+ @method({ args: Bytes, returns: "void" })
381
+ async put(doc: Bytes): Promise<void> {
382
+ if (!this._impl) throw new Error("DocumentsService not bound");
383
+ return this._impl.put(doc);
384
+ }
385
+
386
+ @method({ args: DocumentsGetRequest, returns: option(DocumentsIndexResult) })
387
+ async get(
388
+ request: DocumentsGetRequest,
389
+ ): Promise<DocumentsIndexResult | undefined> {
390
+ if (!this._impl) throw new Error("DocumentsService not bound");
391
+ return this._impl.get(request);
392
+ }
393
+
394
+ @method({ args: IdKey, returns: "void" })
395
+ async del(id: IdKey): Promise<void> {
396
+ if (!this._impl) throw new Error("DocumentsService not bound");
397
+ return this._impl.del(id);
398
+ }
399
+
400
+ @method({ args: DocumentsIterateRequest, returns: DocumentsIteratorService })
401
+ async iterate(
402
+ request: DocumentsIterateRequest,
403
+ ): Promise<DocumentsIteratorService> {
404
+ if (!this._impl) throw new Error("DocumentsService not bound");
405
+ return this._impl.iterate(request);
406
+ }
407
+
408
+ @method({ args: DocumentsPutWithContextRequest, returns: "void" })
409
+ async putWithContext(request: DocumentsPutWithContextRequest): Promise<void> {
410
+ if (!this._impl?.putWithContext) {
411
+ throw new Error("DocumentsService not bound");
412
+ }
413
+ return this._impl.putWithContext(request);
414
+ }
415
+
416
+ @method({ args: DocumentsIndexPutRequest, returns: "void" })
417
+ async indexPut(request: DocumentsIndexPutRequest): Promise<void> {
418
+ if (!this._impl?.indexPut) {
419
+ throw new Error("DocumentsService not bound");
420
+ }
421
+ return this._impl.indexPut(request);
422
+ }
423
+
424
+ @method({ args: DocumentsCountRequest, returns: "u64" })
425
+ async count(request: DocumentsCountRequest): Promise<bigint> {
426
+ if (!this._impl?.count) throw new Error("DocumentsService not bound");
427
+ return this._impl.count(request);
428
+ }
429
+
430
+ @method({ returns: "u64" })
431
+ async indexSize(): Promise<bigint> {
432
+ if (!this._impl?.indexSize) throw new Error("DocumentsService not bound");
433
+ return this._impl.indexSize();
434
+ }
435
+
436
+ @method({ args: DocumentsWaitForRequest, returns: vec("string") })
437
+ async waitFor(request: DocumentsWaitForRequest): Promise<string[]> {
438
+ if (!this._impl?.waitFor) throw new Error("DocumentsService not bound");
439
+ return this._impl.waitFor(request);
440
+ }
441
+
442
+ @method({ args: DocumentsWaitForRequest, returns: vec("string") })
443
+ async indexWaitFor(request: DocumentsWaitForRequest): Promise<string[]> {
444
+ if (!this._impl?.indexWaitFor)
445
+ throw new Error("DocumentsService not bound");
446
+ return this._impl.indexWaitFor(request);
447
+ }
448
+
449
+ @method({ args: "string", returns: "void" })
450
+ async cancelWait(requestId: string): Promise<void> {
451
+ if (!this._impl?.cancelWait) throw new Error("DocumentsService not bound");
452
+ return this._impl.cancelWait(requestId);
453
+ }
454
+
455
+ @method({ returns: "void" })
456
+ async recover(): Promise<void> {
457
+ if (!this._impl?.recover) throw new Error("DocumentsService not bound");
458
+ return this._impl.recover();
459
+ }
460
+
461
+ @method({ returns: SharedLogService })
462
+ async openLog(): Promise<SharedLogService> {
463
+ if (!this._impl?.openLog) throw new Error("DocumentsService not bound");
464
+ return this._impl.openLog();
465
+ }
466
+
467
+ @method({ returns: "void" })
468
+ async close(): Promise<void> {
469
+ if (!this._impl) throw new Error("DocumentsService not bound");
470
+ return this._impl.close();
471
+ }
472
+ }
473
+
474
+ const setStableServiceName = (ctor: Function, name: string): void => {
475
+ try {
476
+ Object.defineProperty(ctor, "name", { value: name, configurable: true });
477
+ } catch {}
478
+ };
479
+
480
+ setStableServiceName(DocumentsIteratorService, "DocumentsIteratorService");
481
+ setStableServiceName(DocumentsService, "DocumentsService");