@peerbit/program 1.0.6 → 2.1.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/lib/esm/index.js CHANGED
@@ -1,350 +1,4 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- var __metadata = (this && this.__metadata) || function (k, v) {
8
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
- };
10
- var AbstractProgram_1, Program_1;
11
- import { PublicSignKey, getPublicKeyFromPeerId } from "@peerbit/crypto";
12
- import { getSchema, variant } from "@dao-xyz/borsh";
13
- import { getValuesWithType } from "./utils.js";
14
- import { serialize, deserialize } from "@dao-xyz/borsh";
15
- import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events";
16
- import { waitForAsync } from "@peerbit/time";
17
- const intersection = (a, b) => {
18
- const newSet = new Set();
19
- for (const el of b) {
20
- if (!a || a.has(el)) {
21
- newSet.add(el);
22
- }
23
- }
24
- return newSet;
25
- };
26
- const getAllParentAddresses = (p) => {
27
- return getAllParent(p, [])
28
- .filter((x) => x instanceof Program)
29
- .map((x) => x.address);
30
- };
31
- const getAllParent = (a, arr = [], includeThis = false) => {
32
- includeThis && arr.push(a);
33
- if (a.parents) {
34
- for (const p of a.parents) {
35
- if (p) {
36
- getAllParent(p, arr, true);
37
- }
38
- }
39
- }
40
- return arr;
41
- };
42
- export let AbstractProgram = AbstractProgram_1 = class AbstractProgram {
43
- _node;
44
- _allPrograms;
45
- _events;
46
- _closed;
47
- parents;
48
- children;
49
- addParent(program) {
50
- (this.parents || (this.parents = [])).push(program);
51
- if (program) {
52
- (program.children || (program.children = [])).push(this);
53
- }
54
- }
55
- get events() {
56
- return this._events || (this._events = new EventEmitter());
57
- }
58
- get closed() {
59
- if (this._closed == null) {
60
- return true;
61
- }
62
- return this._closed;
63
- }
64
- set closed(closed) {
65
- this._closed = closed;
66
- }
67
- get node() {
68
- return this._node;
69
- }
70
- set node(node) {
71
- this._node = node;
72
- }
73
- _eventOptions;
74
- async beforeOpen(node, options) {
75
- if (!this.closed) {
76
- this.addParent(options?.parent);
77
- return this;
78
- }
79
- else {
80
- this.addParent(options?.parent);
81
- }
82
- this._eventOptions = options;
83
- this.node = node;
84
- const nexts = this.programs;
85
- for (const next of nexts) {
86
- await next.beforeOpen(node, { ...options, parent: this });
87
- }
88
- this.node.services.pubsub.addEventListener("subscribe", this._subscriptionEventListener ||
89
- (this._subscriptionEventListener = (s) => !this.closed && this._emitJoinNetworkEvents(s.detail)));
90
- this.node.services.pubsub.addEventListener("unsubscribe", this._unsubscriptionEventListener ||
91
- (this._unsubscriptionEventListener = (s) => !this.closed && this._emitLeaveNetworkEvents(s.detail)));
92
- await this._eventOptions?.onBeforeOpen?.(this);
93
- return this;
94
- }
95
- async afterOpen() {
96
- this.emitEvent(new CustomEvent("open", { detail: this }), true);
97
- await this._eventOptions?.onOpen?.(this);
98
- this.closed = false;
99
- const nexts = this.programs;
100
- for (const next of nexts) {
101
- await next.afterOpen();
102
- }
103
- return this;
104
- }
105
- _clear() {
106
- this._allPrograms = undefined;
107
- }
108
- async _emitJoinNetworkEvents(s) {
109
- const allTopics = this.programs
110
- .map((x) => x.getTopics?.())
111
- .filter((x) => x)
112
- .flat();
113
- // if subscribing to all topics, emit "join" event
114
- for (const topic of allTopics) {
115
- if (!this.node.services.pubsub.getSubscribers(topic)?.has(s.from.hashcode())) {
116
- return;
117
- }
118
- }
119
- this.events.dispatchEvent(new CustomEvent("join", { detail: s.from }));
120
- }
121
- async _emitLeaveNetworkEvents(s) {
122
- const allTopics = this.programs
123
- .map((x) => x.getTopics?.())
124
- .filter((x) => x)
125
- .flat();
126
- // if subscribing not subscribing to any topics, emit "leave" event
127
- for (const topic of allTopics) {
128
- if (this.node.services.pubsub.getSubscribers(topic)?.has(s.from.hashcode())) {
129
- return;
130
- }
131
- }
132
- this.events.dispatchEvent(new CustomEvent("leave", { detail: s.from }));
133
- }
134
- _subscriptionEventListener;
135
- _unsubscriptionEventListener;
136
- async processEnd(type) {
137
- if (!this.closed) {
138
- this.emitEvent(new CustomEvent(type, { detail: this }), true);
139
- if (type === "close") {
140
- this._eventOptions?.onClose?.(this);
141
- }
142
- else if (type === "drop") {
143
- this._eventOptions?.onDrop?.(this);
144
- }
145
- else {
146
- throw new Error("Unsupported event type: " + type);
147
- }
148
- const promises = [];
149
- if (this.children) {
150
- for (const program of this.children) {
151
- promises.push(program[type](this)); // TODO types
152
- }
153
- this.children = [];
154
- }
155
- await Promise.all(promises);
156
- this._clear();
157
- this.closed = true;
158
- return true;
159
- }
160
- else {
161
- this._clear();
162
- return true;
163
- }
164
- }
165
- async end(type, from) {
166
- if (this.closed) {
167
- return true;
168
- }
169
- let parentIdx = -1;
170
- let close = true;
171
- if (this.parents) {
172
- parentIdx = this.parents.findIndex((x) => x == from);
173
- if (parentIdx !== -1) {
174
- if (this.parents.length === 1) {
175
- close = true;
176
- }
177
- else {
178
- this.parents.splice(parentIdx, 1);
179
- close = false;
180
- }
181
- }
182
- else if (from) {
183
- throw new Error("Could not find from in parents");
184
- }
185
- }
186
- const end = close && (await this.processEnd(type));
187
- if (end) {
188
- this.node?.services.pubsub.removeEventListener("subscribe", this._subscriptionEventListener);
189
- this.node?.services.pubsub.removeEventListener("unsubscribe", this._unsubscriptionEventListener);
190
- this._eventOptions = undefined;
191
- if (parentIdx !== -1) {
192
- this.parents.splice(parentIdx, 1); // We splice this here because this._end depends on this parent to exist
193
- }
194
- }
195
- return end;
196
- }
197
- async close(from) {
198
- return this.end("close", from);
199
- }
200
- async drop(from) {
201
- return this.end("drop", from);
202
- }
203
- emitEvent(event, parents = false) {
204
- this.events.dispatchEvent(event);
205
- if (parents) {
206
- if (this.parents) {
207
- for (const parent of this.parents) {
208
- parent?.emitEvent(event);
209
- }
210
- }
211
- }
212
- }
213
- /**
214
- * Wait for another peer to be 'ready' to talk with you for this particular program
215
- * @param other
216
- */
217
- async waitFor(...other) {
218
- const expectedHashes = new Set(other.map((x) => x instanceof PublicSignKey
219
- ? x.hashcode()
220
- : getPublicKeyFromPeerId(x).hashcode()));
221
- await waitForAsync(async () => {
222
- return (intersection(expectedHashes, await this.getReady()).size ===
223
- expectedHashes.size);
224
- }, { delayInterval: 200, timeout: 10 * 1000 }); // 200 ms delay since this is an expensive op. TODO, make event based instead
225
- }
226
- async getReady() {
227
- // all peers that subscribe to all topics
228
- let ready = undefined; // the interesection of all ready
229
- for (const program of this.allPrograms) {
230
- if (program.getTopics) {
231
- const topics = program.getTopics();
232
- for (const topic of topics) {
233
- const subscribers = await this.node.services.pubsub.getSubscribers(topic);
234
- if (!subscribers) {
235
- throw new Error("client is not subscriber to topic data, do not have any info about peer readiness");
236
- }
237
- ready = intersection(ready, subscribers.keys());
238
- }
239
- }
240
- }
241
- if (ready == null) {
242
- throw new Error("Do not have any info about peer readiness");
243
- }
244
- return ready;
245
- }
246
- get allPrograms() {
247
- if (this._allPrograms) {
248
- return this._allPrograms;
249
- }
250
- const arr = this.programs;
251
- const nexts = this.programs;
252
- for (const next of nexts) {
253
- arr.push(...next.allPrograms);
254
- }
255
- this._allPrograms = arr;
256
- return this._allPrograms;
257
- }
258
- get programs() {
259
- return getValuesWithType(this, AbstractProgram_1);
260
- }
261
- clone() {
262
- return deserialize(serialize(this), this.constructor);
263
- }
264
- };
265
- AbstractProgram = AbstractProgram_1 = __decorate([
266
- variant(0)
267
- ], AbstractProgram);
268
- export let Program = Program_1 = class Program extends AbstractProgram {
269
- _address;
270
- constructor() {
271
- super();
272
- }
273
- get address() {
274
- if (!this._address) {
275
- throw new Error("Address does not exist, please open or save this program once to obtain it");
276
- }
277
- return this._address;
278
- }
279
- set address(address) {
280
- this._address = address;
281
- }
282
- async beforeOpen(node, options) {
283
- // check that a discriminator exist
284
- const schema = getSchema(this.constructor);
285
- if (!schema || typeof schema.variant !== "string") {
286
- throw new Error(`Expecting class to be decorated with a string variant. Example:\n\'import { variant } "@dao-xyz/borsh"\n@variant("example-db")\nclass ${this.constructor.name} { ...`);
287
- }
288
- await this.save(node.services.blocks);
289
- if (getAllParentAddresses(this).includes(this.address)) {
290
- throw new Error("Subprogram has same address as some parent program. This is not currently supported");
291
- }
292
- return super.beforeOpen(node, options);
293
- }
294
- static async open(address, node, options) {
295
- const p = await Program_1.load(address, node.services.blocks);
296
- if (!p) {
297
- throw new Error("Failed to load program");
298
- }
299
- await node.open(p, options);
300
- return p;
301
- }
302
- async save(store = this.node.services.blocks) {
303
- const existingAddress = this._address;
304
- const hash = await store.put(serialize(this));
305
- this._address = hash;
306
- if (!this.address) {
307
- throw new Error("Unexpected");
308
- }
309
- if (existingAddress && existingAddress !== this.address) {
310
- throw new Error("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");
311
- }
312
- return this._address;
313
- }
314
- async delete() {
315
- if (this.address) {
316
- return this.node.services.blocks.rm(this.address);
317
- }
318
- // Not saved
319
- }
320
- static async load(address, store, options) {
321
- const bytes = await store.get(address, options);
322
- if (!bytes) {
323
- return undefined;
324
- }
325
- const der = deserialize(bytes, Program_1);
326
- der.address = address;
327
- return der;
328
- }
329
- async drop(from) {
330
- const dropped = await super.drop(from);
331
- if (!dropped) {
332
- return dropped;
333
- }
334
- await this.delete();
335
- return true;
336
- }
337
- };
338
- Program = Program_1 = __decorate([
339
- variant(0),
340
- __metadata("design:paramtypes", [])
341
- ], Program);
342
- /**eve
343
- * Building block, but not something you use as a standalone
344
- */
345
- export let ComposableProgram = class ComposableProgram extends AbstractProgram {
346
- };
347
- ComposableProgram = __decorate([
348
- variant(1)
349
- ], ComposableProgram);
1
+ export * from "./client.js";
2
+ export * from "./program.js";
3
+ export * from "./address.js";
350
4
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AACxE,OAAO,EAAe,SAAS,EAAU,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzE,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAEtE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAe7C,MAAM,YAAY,GAAG,CACpB,CAA0B,EAC1B,CAAyC,EACxC,EAAE;IACH,MAAM,MAAM,GAAG,IAAI,GAAG,EAAU,CAAC;IACjC,KAAK,MAAM,EAAE,IAAI,CAAC,EAAE;QACnB,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;YACpB,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;SACf;KACD;IACD,OAAO,MAAM,CAAC;AACf,CAAC,CAAC;AA2CF,MAAM,qBAAqB,GAAG,CAAC,CAAkB,EAAY,EAAE;IAC9D,OAAO,YAAY,CAAC,CAAC,EAAE,EAAE,CAAC;SACxB,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,YAAY,OAAO,CAAC;SACnC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAE,CAAa,CAAC,OAAO,CAAC,CAAC;AACtC,CAAC,CAAC;AAEF,MAAM,YAAY,GAAG,CACpB,CAAkB,EAClB,MAAyB,EAAE,EAC3B,WAAW,GAAG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QACD,YAAY;IACb,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,IAAI,CAChB,OAAgB,EAChB,KAAa,EACb,OAEC;QAED,MAAM,KAAK,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAChD,IAAI,CAAC,KAAK,EAAE;YACX,OAAO,SAAS,CAAC;SACjB;QACD,MAAM,GAAG,GAAG,WAAW,CAAC,KAAK,EAAE,SAAO,CAAC,CAAC;QACxC,GAAG,CAAC,OAAO,GAAG,OAAO,CAAC;QACtB,OAAO,GAAQ,CAAC;IACjB,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,IAAsB;QAChC,MAAM,OAAO,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACvC,IAAI,CAAC,OAAO,EAAE;YACb,OAAO,OAAO,CAAC;SACf;QACD,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;QACpB,OAAO,IAAI,CAAC;IACb,CAAC;CACD,CAAA;AA9GqB,OAAO;IAD5B,OAAO,CAAC,CAAC,CAAC;;GACW,OAAO,CA8G5B;AAED;;GAEG;AAEI,WAAe,iBAAiB,GAAhC,MAAe,iBAGpB,SAAQ,eAA6B;CAAG,CAAA;AAHpB,iBAAiB;IADtC,OAAO,CAAC,CAAC,CAAC;GACW,iBAAiB,CAGG"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC"}
@@ -0,0 +1,76 @@
1
+ import { PublicSignKey } from "@peerbit/crypto";
2
+ import { Constructor } from "@dao-xyz/borsh";
3
+ import { EventEmitter } from "@libp2p/interfaces/events";
4
+ import { Client } from "./client.js";
5
+ import { Blocks } from "@peerbit/blocks-interface";
6
+ import { PeerId as Libp2pPeerId } from "@libp2p/interface-peer-id";
7
+ import { Address } from "./address.js";
8
+ import { Handler, Manageable, ProgramInitializationOptions } from "./handler.js";
9
+ export type OpenProgram = (program: Program) => Promise<Program>;
10
+ export interface NetworkEvents {
11
+ join: CustomEvent<PublicSignKey>;
12
+ leave: CustomEvent<PublicSignKey>;
13
+ }
14
+ export interface LifeCycleEvents {
15
+ drop: CustomEvent<Program>;
16
+ open: CustomEvent<Program>;
17
+ close: CustomEvent<Program>;
18
+ }
19
+ export interface ProgramEvents extends NetworkEvents, LifeCycleEvents {
20
+ }
21
+ export type ProgramClient = Client<Program>;
22
+ declare class ProgramHandler extends Handler<Program> {
23
+ constructor(properties: {
24
+ client: ProgramClient;
25
+ });
26
+ }
27
+ export { ProgramHandler };
28
+ export declare abstract class Program<Args = any, Events extends ProgramEvents = ProgramEvents> implements Manageable<Args> {
29
+ private _node;
30
+ private _allPrograms;
31
+ private _events;
32
+ private _closed;
33
+ parents: (Program<any> | undefined)[];
34
+ children: Program<Args>[];
35
+ private _address?;
36
+ get address(): Address;
37
+ set address(address: Address);
38
+ addParent(program: Program<any> | undefined): void;
39
+ get events(): EventEmitter<Events>;
40
+ get closed(): boolean;
41
+ set closed(closed: boolean);
42
+ get node(): ProgramClient;
43
+ set node(node: ProgramClient);
44
+ private _eventOptions;
45
+ beforeOpen(node: ProgramClient, options?: ProgramInitializationOptions<Args, this>): Promise<void>;
46
+ afterOpen(): Promise<void>;
47
+ abstract open(args?: Args): Promise<void>;
48
+ private _clear;
49
+ private _emitJoinNetworkEvents;
50
+ private _emitLeaveNetworkEvents;
51
+ private _subscriptionEventListener;
52
+ private _unsubscriptionEventListener;
53
+ private processEnd;
54
+ private end;
55
+ close(from?: Program): Promise<boolean>;
56
+ drop(from?: Program): Promise<boolean>;
57
+ emitEvent(event: CustomEvent, parents?: boolean): void;
58
+ /**
59
+ * Wait for another peer to be 'ready' to talk with you for this particular program
60
+ * @param other
61
+ */
62
+ waitFor(...other: (PublicSignKey | Libp2pPeerId)[]): Promise<void>;
63
+ getReady(): Promise<Set<string>>;
64
+ get allPrograms(): Program[];
65
+ get programs(): Program[];
66
+ clone(): this;
67
+ getTopics?(): string[];
68
+ save(store?: Blocks): Promise<Address>;
69
+ delete(): Promise<void>;
70
+ static load<P extends Program<any>>(address: Address, store: Blocks, options?: {
71
+ timeout?: number;
72
+ }): Promise<P | undefined>;
73
+ static open<T extends Program<Args>, Args = any>(this: Constructor<T>, address: Address, node: ProgramClient, options?: ProgramInitializationOptions<Args, T>): Promise<T>;
74
+ }
75
+ export declare const getProgramFromVariants: <T extends Program<any, ProgramEvents>>() => Constructor<T>[];
76
+ export declare const getProgramFromVariant: <T extends Program<any, ProgramEvents>>(variant: string) => Constructor<T> | undefined;