@peerbit/program 1.0.5 → 2.0.0

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/src/program.ts ADDED
@@ -0,0 +1,470 @@
1
+ import { PublicSignKey, getPublicKeyFromPeerId } from "@peerbit/crypto";
2
+ import { Constructor, getSchema, variant } from "@dao-xyz/borsh";
3
+ import { getValuesWithType } from "./utils.js";
4
+ import { serialize, deserialize } from "@dao-xyz/borsh";
5
+ import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events";
6
+ import { Client } from "./client.js";
7
+ import { waitForAsync } from "@peerbit/time";
8
+ import { Blocks } from "@peerbit/blocks-interface";
9
+ import { PeerId as Libp2pPeerId } from "@libp2p/interface-peer-id";
10
+ import {
11
+ SubscriptionEvent,
12
+ UnsubcriptionEvent,
13
+ } from "@peerbit/pubsub-interface";
14
+ import { Address } from "./address.js";
15
+ import {
16
+ EventOptions,
17
+ Handler,
18
+ Manageable,
19
+ ProgramInitializationOptions,
20
+ } from "./handler.js";
21
+
22
+ const intersection = (
23
+ a: Set<string> | undefined,
24
+ b: Set<string> | IterableIterator<string>
25
+ ) => {
26
+ const newSet = new Set<string>();
27
+ for (const el of b) {
28
+ if (!a || a.has(el)) {
29
+ newSet.add(el);
30
+ }
31
+ }
32
+ return newSet;
33
+ };
34
+
35
+ export type OpenProgram = (program: Program) => Promise<Program>;
36
+
37
+ export interface NetworkEvents {
38
+ join: CustomEvent<PublicSignKey>;
39
+ leave: CustomEvent<PublicSignKey>;
40
+ }
41
+
42
+ export interface LifeCycleEvents {
43
+ drop: CustomEvent<Program>;
44
+ open: CustomEvent<Program>;
45
+ close: CustomEvent<Program>;
46
+ }
47
+
48
+ export interface ProgramEvents extends NetworkEvents, LifeCycleEvents {}
49
+
50
+ const getAllParentAddresses = (p: Program): string[] => {
51
+ return getAllParent(p, [])
52
+ .filter((x) => x instanceof Program)
53
+ .map((x) => (x as Program).address);
54
+ };
55
+
56
+ const getAllParent = (a: Program, arr: Program[] = [], includeThis = false) => {
57
+ includeThis && arr.push(a);
58
+ if (a.parents) {
59
+ for (const p of a.parents) {
60
+ if (p) {
61
+ getAllParent(p, arr, true);
62
+ }
63
+ }
64
+ }
65
+ return arr;
66
+ };
67
+
68
+ export type ProgramClient = Client<Program>;
69
+ class ProgramHandler extends Handler<Program> {
70
+ constructor(properties: { client: ProgramClient }) {
71
+ super({
72
+ client: properties.client,
73
+ shouldMonitor: (p) => p instanceof Program,
74
+ load: Program.load,
75
+ });
76
+ }
77
+ }
78
+ export { ProgramHandler };
79
+
80
+ @variant(0)
81
+ export abstract class Program<
82
+ Args = any,
83
+ Events extends ProgramEvents = ProgramEvents
84
+ > implements Manageable<Args>
85
+ {
86
+ private _node: ProgramClient;
87
+ private _allPrograms: Program[] | undefined;
88
+
89
+ private _events: EventEmitter<ProgramEvents>;
90
+ private _closed: boolean;
91
+
92
+ parents: (Program<any> | undefined)[];
93
+ children: Program<Args>[];
94
+
95
+ private _address?: Address;
96
+
97
+ get address(): Address {
98
+ if (!this._address) {
99
+ throw new Error(
100
+ "Address does not exist, please open or save this program once to obtain it"
101
+ );
102
+ }
103
+ return this._address;
104
+ }
105
+
106
+ set address(address: Address) {
107
+ this._address = address;
108
+ }
109
+
110
+ addParent(program: Program<any> | undefined) {
111
+ (this.parents || (this.parents = [])).push(program);
112
+ if (program) {
113
+ (program.children || (program.children = [])).push(this);
114
+ }
115
+ }
116
+
117
+ get events(): EventEmitter<Events> {
118
+ return this._events || (this._events = new EventEmitter());
119
+ }
120
+
121
+ get closed(): boolean {
122
+ if (this._closed == null) {
123
+ return true;
124
+ }
125
+ return this._closed;
126
+ }
127
+ set closed(closed: boolean) {
128
+ this._closed = closed;
129
+ }
130
+
131
+ get node(): ProgramClient {
132
+ return this._node;
133
+ }
134
+
135
+ set node(node: ProgramClient) {
136
+ this._node = node;
137
+ }
138
+
139
+ private _eventOptions: EventOptions | undefined;
140
+
141
+ async beforeOpen(
142
+ node: ProgramClient,
143
+ options?: ProgramInitializationOptions<Args, this>
144
+ ) {
145
+ // check that a discriminator exist
146
+ const schema = getSchema(this.constructor);
147
+ if (!schema || typeof schema.variant !== "string") {
148
+ throw new Error(
149
+ `Expecting class to be decorated with a string variant. Example:\n\'import { variant } "@dao-xyz/borsh"\n@variant("example-db")\nclass ${this.constructor.name} { ...`
150
+ );
151
+ }
152
+
153
+ await this.save(node.services.blocks);
154
+ if (getAllParentAddresses(this as Program).includes(this.address)) {
155
+ throw new Error(
156
+ "Subprogram has same address as some parent program. This is not currently supported"
157
+ );
158
+ }
159
+
160
+ if (!this.closed) {
161
+ this.addParent(options?.parent);
162
+ return;
163
+ } else {
164
+ this.addParent(options?.parent);
165
+ }
166
+ this._eventOptions = options;
167
+ this.node = node;
168
+ const nexts = this.programs;
169
+ for (const next of nexts) {
170
+ await next.beforeOpen(node, { ...options, parent: this });
171
+ }
172
+
173
+ await this.node.services.pubsub.addEventListener(
174
+ "subscribe",
175
+ this._subscriptionEventListener ||
176
+ (this._subscriptionEventListener = (s) =>
177
+ !this.closed && this._emitJoinNetworkEvents(s.detail))
178
+ );
179
+ await this.node.services.pubsub.addEventListener(
180
+ "unsubscribe",
181
+ this._unsubscriptionEventListener ||
182
+ (this._unsubscriptionEventListener = (s) =>
183
+ !this.closed && this._emitLeaveNetworkEvents(s.detail))
184
+ );
185
+
186
+ await this._eventOptions?.onBeforeOpen?.(this);
187
+ }
188
+
189
+ async afterOpen() {
190
+ this.emitEvent(new CustomEvent("open", { detail: this }), true);
191
+ await this._eventOptions?.onOpen?.(this);
192
+ this.closed = false;
193
+ const nexts = this.programs;
194
+ for (const next of nexts) {
195
+ await next.afterOpen();
196
+ }
197
+ }
198
+
199
+ abstract open(args?: Args): Promise<void>;
200
+
201
+ private _clear() {
202
+ this._allPrograms = undefined;
203
+ }
204
+
205
+ private async _emitJoinNetworkEvents(s: SubscriptionEvent) {
206
+ const allTopics = this.programs
207
+ .map((x) => x.getTopics?.())
208
+ .filter((x) => x)
209
+ .flat() as string[];
210
+
211
+ // if subscribing to all topics, emit "join" event
212
+ for (const topic of allTopics) {
213
+ if (
214
+ !(await this.node.services.pubsub.getSubscribers(topic))?.has(
215
+ s.from.hashcode()
216
+ )
217
+ ) {
218
+ return;
219
+ }
220
+ }
221
+ this.events.dispatchEvent(new CustomEvent("join", { detail: s.from }));
222
+ }
223
+
224
+ private async _emitLeaveNetworkEvents(s: UnsubcriptionEvent) {
225
+ const allTopics = this.programs
226
+ .map((x) => x.getTopics?.())
227
+ .filter((x) => x)
228
+ .flat() as string[];
229
+
230
+ // if subscribing not subscribing to any topics, emit "leave" event
231
+ for (const topic of allTopics) {
232
+ if (
233
+ (await this.node.services.pubsub.getSubscribers(topic))?.has(
234
+ s.from.hashcode()
235
+ )
236
+ ) {
237
+ return;
238
+ }
239
+ }
240
+ this.events.dispatchEvent(new CustomEvent("leave", { detail: s.from }));
241
+ }
242
+
243
+ private _subscriptionEventListener: (
244
+ e: CustomEvent<SubscriptionEvent>
245
+ ) => void;
246
+ private _unsubscriptionEventListener: (
247
+ e: CustomEvent<UnsubcriptionEvent>
248
+ ) => void;
249
+
250
+ private async processEnd(type: "drop" | "close") {
251
+ if (!this.closed) {
252
+ this.emitEvent(new CustomEvent(type, { detail: this }), true);
253
+ if (type === "close") {
254
+ this._eventOptions?.onClose?.(this);
255
+ } else if (type === "drop") {
256
+ this._eventOptions?.onDrop?.(this);
257
+ } else {
258
+ throw new Error("Unsupported event type: " + type);
259
+ }
260
+
261
+ const promises: Promise<void | boolean>[] = [];
262
+
263
+ if (this.children) {
264
+ for (const program of this.children) {
265
+ promises.push(program[type](this as Program)); // TODO types
266
+ }
267
+ this.children = [];
268
+ }
269
+ await Promise.all(promises);
270
+
271
+ this._clear();
272
+ this.closed = true;
273
+ return true;
274
+ } else {
275
+ this._clear();
276
+ return true;
277
+ }
278
+ }
279
+
280
+ private async end(type: "drop" | "close", from?: Program): Promise<boolean> {
281
+ if (this.closed) {
282
+ return true;
283
+ }
284
+
285
+ let parentIdx = -1;
286
+ let close = true;
287
+ if (this.parents) {
288
+ parentIdx = this.parents.findIndex((x) => x == from);
289
+ if (parentIdx !== -1) {
290
+ if (this.parents.length === 1) {
291
+ close = true;
292
+ } else {
293
+ this.parents.splice(parentIdx, 1);
294
+ close = false;
295
+ }
296
+ } else if (from) {
297
+ throw new Error("Could not find from in parents");
298
+ }
299
+ }
300
+
301
+ const end = close && (await this.processEnd(type));
302
+ if (end) {
303
+ this.node?.services.pubsub.removeEventListener(
304
+ "subscribe",
305
+ this._subscriptionEventListener
306
+ );
307
+ this.node?.services.pubsub.removeEventListener(
308
+ "unsubscribe",
309
+ this._unsubscriptionEventListener
310
+ );
311
+
312
+ this._eventOptions = undefined;
313
+
314
+ if (parentIdx !== -1) {
315
+ this.parents.splice(parentIdx, 1); // We splice this here because this._end depends on this parent to exist
316
+ }
317
+ }
318
+
319
+ return end;
320
+ }
321
+ async close(from?: Program): Promise<boolean> {
322
+ return this.end("close", from);
323
+ }
324
+
325
+ async drop(from?: Program): Promise<boolean> {
326
+ const dropped = await this.end("drop", from);
327
+ if (dropped) {
328
+ await this.delete();
329
+ }
330
+ return dropped;
331
+ }
332
+
333
+ emitEvent(event: CustomEvent, parents = false) {
334
+ this.events.dispatchEvent(event);
335
+ if (parents) {
336
+ if (this.parents) {
337
+ for (const parent of this.parents) {
338
+ parent?.emitEvent(event);
339
+ }
340
+ }
341
+ }
342
+ }
343
+
344
+ /**
345
+ * Wait for another peer to be 'ready' to talk with you for this particular program
346
+ * @param other
347
+ */
348
+ async waitFor(...other: (PublicSignKey | Libp2pPeerId)[]): Promise<void> {
349
+ const expectedHashes = new Set(
350
+ other.map((x) =>
351
+ x instanceof PublicSignKey
352
+ ? x.hashcode()
353
+ : getPublicKeyFromPeerId(x).hashcode()
354
+ )
355
+ );
356
+ await waitForAsync(
357
+ async () => {
358
+ return (
359
+ intersection(expectedHashes, await this.getReady()).size ===
360
+ expectedHashes.size
361
+ );
362
+ },
363
+ { delayInterval: 200, timeout: 10 * 1000 }
364
+ ); // 200 ms delay since this is an expensive op. TODO, make event based instead
365
+ }
366
+
367
+ async getReady(): Promise<Set<string>> {
368
+ // all peers that subscribe to all topics
369
+ let ready: Set<string> | undefined = undefined; // the interesection of all ready
370
+ for (const program of this.allPrograms) {
371
+ if (program.getTopics) {
372
+ const topics = program.getTopics();
373
+ for (const topic of topics) {
374
+ const subscribers = await this.node.services.pubsub.getSubscribers(
375
+ topic
376
+ );
377
+ if (!subscribers) {
378
+ throw new Error(
379
+ "client is not subscriber to topic data, do not have any info about peer readiness"
380
+ );
381
+ }
382
+ ready = intersection(ready, subscribers.keys());
383
+ }
384
+ }
385
+ }
386
+ if (ready == null) {
387
+ throw new Error("Do not have any info about peer readiness");
388
+ }
389
+ return ready;
390
+ }
391
+
392
+ get allPrograms(): Program[] {
393
+ if (this._allPrograms) {
394
+ return this._allPrograms;
395
+ }
396
+ const arr: Program[] = this.programs;
397
+ const nexts = this.programs;
398
+ for (const next of nexts) {
399
+ arr.push(...next.allPrograms);
400
+ }
401
+ this._allPrograms = arr;
402
+ return this._allPrograms;
403
+ }
404
+
405
+ get programs(): Program[] {
406
+ return getValuesWithType(this, Program);
407
+ }
408
+
409
+ clone(): this {
410
+ return deserialize(serialize(this), this.constructor);
411
+ }
412
+
413
+ getTopics?(): string[];
414
+
415
+ async save(store: Blocks = this.node.services.blocks): Promise<Address> {
416
+ const existingAddress = this._address;
417
+ const hash = await store.put(serialize(this));
418
+
419
+ this._address = hash;
420
+ if (!this.address) {
421
+ throw new Error("Unexpected");
422
+ }
423
+
424
+ if (existingAddress && existingAddress !== this.address) {
425
+ throw new Error(
426
+ "Program properties has been changed after constructor so that the hash has changed. Make sure that the 'setup(...)' function does not modify any properties that are to be serialized"
427
+ );
428
+ }
429
+
430
+ return this._address!;
431
+ }
432
+
433
+ async delete(): Promise<void> {
434
+ if (this.address) {
435
+ return this.node.services.blocks.rm(this.address);
436
+ }
437
+ // Not saved
438
+ }
439
+
440
+ static async load<P extends Program<any>>(
441
+ address: Address,
442
+ store: Blocks,
443
+ options?: {
444
+ timeout?: number;
445
+ }
446
+ ): Promise<P | undefined> {
447
+ const bytes = await store.get(address, options);
448
+ if (!bytes) {
449
+ return undefined;
450
+ }
451
+ const der = deserialize(bytes, Program);
452
+ der.address = address;
453
+ return der as P;
454
+ }
455
+
456
+ static async open<T extends Program<Args>, Args = any>(
457
+ this: Constructor<T>,
458
+ address: Address,
459
+ node: ProgramClient,
460
+ options?: ProgramInitializationOptions<Args, T>
461
+ ): Promise<T> {
462
+ const p = await Program.load<T>(address, node.services.blocks);
463
+
464
+ if (!p) {
465
+ throw new Error("Failed to load program");
466
+ }
467
+ await node.open(p, options);
468
+ return p as T;
469
+ }
470
+ }
package/lib/esm/node.js DELETED
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=node.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"node.js","sourceRoot":"","sources":["../../src/node.ts"],"names":[],"mappings":""}
@@ -1,3 +0,0 @@
1
- {
2
- "type": "module"
3
- }