@peerbit/pubsub-interface 1.1.5 → 2.0.1
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.d.ts +32 -21
- package/lib/esm/index.js +25 -15
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/messages.d.ts +4 -15
- package/lib/esm/messages.js +9 -51
- package/lib/esm/messages.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +47 -42
- package/src/messages.ts +11 -47
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,34 +1,46 @@
|
|
|
1
1
|
import { PublicSignKey } from "@peerbit/crypto";
|
|
2
|
-
import { PubSubData
|
|
3
|
-
import { Message, DataMessage, WaitForPeer, PeerEvents } from "@peerbit/stream-interface";
|
|
4
|
-
import { EventHandler } from "@libp2p/interface
|
|
2
|
+
import { PubSubData } from "./messages.js";
|
|
3
|
+
import { Message, DataMessage, WaitForPeer, PeerEvents, DeliveryMode } from "@peerbit/stream-interface";
|
|
4
|
+
import { EventHandler } from "@libp2p/interface";
|
|
5
5
|
import { PeerId as Libp2pPeerId } from "@libp2p/interface/peer-id";
|
|
6
6
|
export declare class SubscriptionEvent {
|
|
7
7
|
from: PublicSignKey;
|
|
8
|
-
subscriptions:
|
|
9
|
-
constructor(from: PublicSignKey, subscriptions:
|
|
8
|
+
subscriptions: string[];
|
|
9
|
+
constructor(from: PublicSignKey, subscriptions: string[]);
|
|
10
10
|
}
|
|
11
11
|
export declare class UnsubcriptionEvent {
|
|
12
12
|
from: PublicSignKey;
|
|
13
|
-
unsubscriptions:
|
|
14
|
-
constructor(from: PublicSignKey, unsubscriptions:
|
|
13
|
+
unsubscriptions: string[];
|
|
14
|
+
constructor(from: PublicSignKey, unsubscriptions: string[]);
|
|
15
|
+
}
|
|
16
|
+
export declare class PublishEvent {
|
|
17
|
+
data: PubSubData;
|
|
18
|
+
message: DataMessage;
|
|
19
|
+
client?: string;
|
|
20
|
+
constructor(properties: {
|
|
21
|
+
client?: string;
|
|
22
|
+
data: PubSubData;
|
|
23
|
+
message: DataMessage;
|
|
24
|
+
});
|
|
15
25
|
}
|
|
16
26
|
export declare class DataEvent {
|
|
17
27
|
data: PubSubData;
|
|
18
28
|
message: DataMessage;
|
|
19
|
-
constructor(
|
|
29
|
+
constructor(properties: {
|
|
30
|
+
data: PubSubData;
|
|
31
|
+
message: DataMessage;
|
|
32
|
+
});
|
|
20
33
|
}
|
|
21
34
|
export declare class SubscriptionData {
|
|
22
35
|
publicKey: PublicSignKey;
|
|
23
36
|
timestamp: bigint;
|
|
24
|
-
data?: Uint8Array;
|
|
25
37
|
constructor(properties: {
|
|
26
38
|
publicKey: PublicSignKey;
|
|
27
39
|
timestamp: bigint;
|
|
28
|
-
data?: Uint8Array;
|
|
29
40
|
});
|
|
30
41
|
}
|
|
31
42
|
export interface PubSubEvents extends PeerEvents {
|
|
43
|
+
publish: CustomEvent<DataEvent>;
|
|
32
44
|
data: CustomEvent<DataEvent>;
|
|
33
45
|
subscribe: CustomEvent<SubscriptionEvent>;
|
|
34
46
|
unsubscribe: CustomEvent<UnsubcriptionEvent>;
|
|
@@ -40,26 +52,25 @@ export interface IEventEmitter<EventMap extends Record<string, any>> {
|
|
|
40
52
|
dispatchEvent(event: Event): MaybePromise<boolean>;
|
|
41
53
|
}
|
|
42
54
|
type MaybePromise<T> = Promise<T> | T;
|
|
43
|
-
export type PublishOptions = {
|
|
55
|
+
export type PublishOptions = ({
|
|
44
56
|
topics?: string[];
|
|
45
57
|
to?: (string | PublicSignKey | Libp2pPeerId)[];
|
|
46
|
-
|
|
58
|
+
mode?: DeliveryMode | undefined;
|
|
47
59
|
} | {
|
|
48
60
|
topics: string[];
|
|
49
61
|
to: (string | PublicSignKey | Libp2pPeerId)[];
|
|
50
|
-
|
|
62
|
+
mode?: DeliveryMode | undefined;
|
|
63
|
+
}) & {
|
|
64
|
+
client?: string;
|
|
51
65
|
};
|
|
52
66
|
export interface PubSub extends IEventEmitter<PubSubEvents>, WaitForPeer {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
subscribe(topic: string, options?: {
|
|
57
|
-
data?: Uint8Array;
|
|
58
|
-
}): Promise<void>;
|
|
67
|
+
getSubscribers(topic: string): MaybePromise<PublicSignKey[] | undefined>;
|
|
68
|
+
requestSubscribers(topic: string, from?: PublicSignKey): MaybePromise<void>;
|
|
69
|
+
subscribe(topic: string): MaybePromise<void>;
|
|
59
70
|
unsubscribe(topic: string, options?: {
|
|
60
71
|
force?: boolean;
|
|
61
72
|
data?: Uint8Array;
|
|
62
|
-
}):
|
|
63
|
-
publish(data: Uint8Array, options?: PublishOptions):
|
|
73
|
+
}): MaybePromise<boolean>;
|
|
74
|
+
publish(data: Uint8Array, options?: PublishOptions): MaybePromise<Uint8Array>;
|
|
64
75
|
}
|
|
65
76
|
export * from "./messages.js";
|
package/lib/esm/index.js
CHANGED
|
@@ -8,9 +8,9 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
10
|
import { PublicSignKey } from "@peerbit/crypto";
|
|
11
|
-
import { PubSubData
|
|
11
|
+
import { PubSubData } from "./messages.js";
|
|
12
12
|
import { DataMessage } from "@peerbit/stream-interface";
|
|
13
|
-
import { field,
|
|
13
|
+
import { field, vec } from "@dao-xyz/borsh";
|
|
14
14
|
export class SubscriptionEvent {
|
|
15
15
|
from;
|
|
16
16
|
subscriptions;
|
|
@@ -24,7 +24,7 @@ __decorate([
|
|
|
24
24
|
__metadata("design:type", PublicSignKey)
|
|
25
25
|
], SubscriptionEvent.prototype, "from", void 0);
|
|
26
26
|
__decorate([
|
|
27
|
-
field({ type: vec(
|
|
27
|
+
field({ type: vec("string") }),
|
|
28
28
|
__metadata("design:type", Array)
|
|
29
29
|
], SubscriptionEvent.prototype, "subscriptions", void 0);
|
|
30
30
|
export class UnsubcriptionEvent {
|
|
@@ -40,15 +40,33 @@ __decorate([
|
|
|
40
40
|
__metadata("design:type", PublicSignKey)
|
|
41
41
|
], UnsubcriptionEvent.prototype, "from", void 0);
|
|
42
42
|
__decorate([
|
|
43
|
-
field({ type: vec(
|
|
43
|
+
field({ type: vec("string") }),
|
|
44
44
|
__metadata("design:type", Array)
|
|
45
45
|
], UnsubcriptionEvent.prototype, "unsubscriptions", void 0);
|
|
46
|
+
export class PublishEvent {
|
|
47
|
+
data;
|
|
48
|
+
message;
|
|
49
|
+
client;
|
|
50
|
+
constructor(properties) {
|
|
51
|
+
this.client = properties.client;
|
|
52
|
+
this.data = properties.data;
|
|
53
|
+
this.message = properties.message;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
__decorate([
|
|
57
|
+
field({ type: PubSubData }),
|
|
58
|
+
__metadata("design:type", PubSubData)
|
|
59
|
+
], PublishEvent.prototype, "data", void 0);
|
|
60
|
+
__decorate([
|
|
61
|
+
field({ type: DataMessage }),
|
|
62
|
+
__metadata("design:type", DataMessage)
|
|
63
|
+
], PublishEvent.prototype, "message", void 0);
|
|
46
64
|
export class DataEvent {
|
|
47
65
|
data;
|
|
48
66
|
message;
|
|
49
|
-
constructor(
|
|
50
|
-
this.data = data;
|
|
51
|
-
this.message = message;
|
|
67
|
+
constructor(properties) {
|
|
68
|
+
this.data = properties.data;
|
|
69
|
+
this.message = properties.message;
|
|
52
70
|
}
|
|
53
71
|
}
|
|
54
72
|
__decorate([
|
|
@@ -62,11 +80,9 @@ __decorate([
|
|
|
62
80
|
export class SubscriptionData {
|
|
63
81
|
publicKey;
|
|
64
82
|
timestamp;
|
|
65
|
-
data;
|
|
66
83
|
constructor(properties) {
|
|
67
84
|
this.publicKey = properties.publicKey;
|
|
68
85
|
this.timestamp = properties.timestamp;
|
|
69
|
-
this.data = properties.data;
|
|
70
86
|
}
|
|
71
87
|
}
|
|
72
88
|
__decorate([
|
|
@@ -77,11 +93,5 @@ __decorate([
|
|
|
77
93
|
field({ type: "u64" }),
|
|
78
94
|
__metadata("design:type", BigInt)
|
|
79
95
|
], SubscriptionData.prototype, "timestamp", void 0);
|
|
80
|
-
__decorate([
|
|
81
|
-
field({
|
|
82
|
-
type: option(Uint8Array)
|
|
83
|
-
}),
|
|
84
|
-
__metadata("design:type", Uint8Array)
|
|
85
|
-
], SubscriptionData.prototype, "data", void 0);
|
|
86
96
|
export * from "./messages.js";
|
|
87
97
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAEN,WAAW,EAIX,MAAM,2BAA2B,CAAC;AAGnC,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAE5C,MAAM,OAAO,iBAAiB;IAE7B,IAAI,CAAgB;IAGpB,aAAa,CAAW;IAExB,YAAY,IAAmB,EAAE,aAAuB;QACvD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;IACpC,CAAC;CACD;AATA;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC;8BACzB,aAAa;+CAAC;AAGpB;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;;wDACP;AAQzB,MAAM,OAAO,kBAAkB;IAE9B,IAAI,CAAgB;IAGpB,eAAe,CAAW;IAE1B,YAAY,I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;QACtC,IAAI,CAAC,SAAS,GAAG,UAAU,CAAC,SAAS,CAAC;IACvC,CAAC;CACD;AATA;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC;8BACpB,aAAa;mDAAC;AAGzB;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;;mDACL;AA6DnB,cAAc,eAAe,CAAC"}
|
package/lib/esm/messages.d.ts
CHANGED
|
@@ -17,34 +17,23 @@ export declare class PubSubData extends PubSubMessage {
|
|
|
17
17
|
bytes(): Uint8Array | Uint8ArrayList;
|
|
18
18
|
static from(bytes: Uint8Array | Uint8ArrayList): PubSubData;
|
|
19
19
|
}
|
|
20
|
-
export declare class Subscription {
|
|
21
|
-
topic: string;
|
|
22
|
-
data?: Uint8Array;
|
|
23
|
-
constructor(topic: string, data?: Uint8Array);
|
|
24
|
-
}
|
|
25
20
|
export declare class Subscribe extends PubSubMessage {
|
|
26
|
-
|
|
21
|
+
topics: string[];
|
|
27
22
|
constructor(options: {
|
|
28
|
-
|
|
23
|
+
topics: string[];
|
|
29
24
|
});
|
|
30
|
-
_serialized
|
|
25
|
+
private _serialized;
|
|
31
26
|
bytes(): Uint8Array | Uint8ArrayList;
|
|
32
27
|
static from(bytes: Uint8Array | Uint8ArrayList): Subscribe;
|
|
33
|
-
get topics(): string[];
|
|
34
|
-
}
|
|
35
|
-
export declare class Unsubscription {
|
|
36
|
-
topic: string;
|
|
37
|
-
constructor(topic: string);
|
|
38
28
|
}
|
|
39
29
|
export declare class Unsubscribe extends PubSubMessage {
|
|
40
|
-
|
|
30
|
+
topics: string[];
|
|
41
31
|
constructor(options: {
|
|
42
32
|
topics: string[];
|
|
43
33
|
});
|
|
44
34
|
_serialized: Uint8ArrayList;
|
|
45
35
|
bytes(): Uint8Array | Uint8ArrayList;
|
|
46
36
|
static from(bytes: Uint8Array | Uint8ArrayList): Unsubscribe;
|
|
47
|
-
get topics(): string[];
|
|
48
37
|
}
|
|
49
38
|
export declare class GetSubscribers extends PubSubMessage {
|
|
50
39
|
topics: string[];
|
package/lib/esm/messages.js
CHANGED
|
@@ -9,7 +9,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
9
9
|
};
|
|
10
10
|
var PubSubData_1, Subscribe_1, Unsubscribe_1, GetSubscribers_1;
|
|
11
11
|
import { Uint8ArrayList } from "uint8arraylist";
|
|
12
|
-
import { field, vec, variant, serialize, deserialize
|
|
12
|
+
import { field, vec, variant, serialize, deserialize } from "@dao-xyz/borsh";
|
|
13
13
|
export class PubSubMessage {
|
|
14
14
|
static from(bytes) {
|
|
15
15
|
const first = bytes[0];
|
|
@@ -74,32 +74,11 @@ PubSubData = PubSubData_1 = __decorate([
|
|
|
74
74
|
__metadata("design:paramtypes", [Object])
|
|
75
75
|
], PubSubData);
|
|
76
76
|
export { PubSubData };
|
|
77
|
-
let Subscription = class Subscription {
|
|
78
|
-
topic;
|
|
79
|
-
data; // if omitted, the subcription event is a no-op (will not replace anything)
|
|
80
|
-
constructor(topic, data) {
|
|
81
|
-
this.topic = topic;
|
|
82
|
-
this.data = data;
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
__decorate([
|
|
86
|
-
field({ type: "string" }),
|
|
87
|
-
__metadata("design:type", String)
|
|
88
|
-
], Subscription.prototype, "topic", void 0);
|
|
89
|
-
__decorate([
|
|
90
|
-
field({ type: option(Uint8Array) }),
|
|
91
|
-
__metadata("design:type", Uint8Array)
|
|
92
|
-
], Subscription.prototype, "data", void 0);
|
|
93
|
-
Subscription = __decorate([
|
|
94
|
-
variant(0),
|
|
95
|
-
__metadata("design:paramtypes", [String, Uint8Array])
|
|
96
|
-
], Subscription);
|
|
97
|
-
export { Subscription };
|
|
98
77
|
let Subscribe = Subscribe_1 = class Subscribe extends PubSubMessage {
|
|
99
|
-
|
|
78
|
+
topics;
|
|
100
79
|
constructor(options) {
|
|
101
80
|
super();
|
|
102
|
-
this.
|
|
81
|
+
this.topics = options.topics;
|
|
103
82
|
}
|
|
104
83
|
_serialized;
|
|
105
84
|
bytes() {
|
|
@@ -115,39 +94,21 @@ let Subscribe = Subscribe_1 = class Subscribe extends PubSubMessage {
|
|
|
115
94
|
}
|
|
116
95
|
return ret;
|
|
117
96
|
}
|
|
118
|
-
get topics() {
|
|
119
|
-
return this.subscriptions.map((x) => x.topic);
|
|
120
|
-
}
|
|
121
97
|
};
|
|
122
98
|
__decorate([
|
|
123
|
-
field({ type: vec(
|
|
99
|
+
field({ type: vec("string") }),
|
|
124
100
|
__metadata("design:type", Array)
|
|
125
|
-
], Subscribe.prototype, "
|
|
101
|
+
], Subscribe.prototype, "topics", void 0);
|
|
126
102
|
Subscribe = Subscribe_1 = __decorate([
|
|
127
103
|
variant(1),
|
|
128
104
|
__metadata("design:paramtypes", [Object])
|
|
129
105
|
], Subscribe);
|
|
130
106
|
export { Subscribe };
|
|
131
|
-
let Unsubscription = class Unsubscription {
|
|
132
|
-
topic;
|
|
133
|
-
constructor(topic) {
|
|
134
|
-
this.topic = topic;
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
__decorate([
|
|
138
|
-
field({ type: "string" }),
|
|
139
|
-
__metadata("design:type", String)
|
|
140
|
-
], Unsubscription.prototype, "topic", void 0);
|
|
141
|
-
Unsubscription = __decorate([
|
|
142
|
-
variant(0),
|
|
143
|
-
__metadata("design:paramtypes", [String])
|
|
144
|
-
], Unsubscription);
|
|
145
|
-
export { Unsubscription };
|
|
146
107
|
let Unsubscribe = Unsubscribe_1 = class Unsubscribe extends PubSubMessage {
|
|
147
|
-
|
|
108
|
+
topics;
|
|
148
109
|
constructor(options) {
|
|
149
110
|
super();
|
|
150
|
-
this.
|
|
111
|
+
this.topics = options.topics;
|
|
151
112
|
}
|
|
152
113
|
_serialized;
|
|
153
114
|
bytes() {
|
|
@@ -163,14 +124,11 @@ let Unsubscribe = Unsubscribe_1 = class Unsubscribe extends PubSubMessage {
|
|
|
163
124
|
}
|
|
164
125
|
return ret;
|
|
165
126
|
}
|
|
166
|
-
get topics() {
|
|
167
|
-
return this.unsubscriptions.map((x) => x.topic);
|
|
168
|
-
}
|
|
169
127
|
};
|
|
170
128
|
__decorate([
|
|
171
|
-
field({ type: vec(
|
|
129
|
+
field({ type: vec("string") }),
|
|
172
130
|
__metadata("design:type", Array)
|
|
173
|
-
], Unsubscribe.prototype, "
|
|
131
|
+
], Unsubscribe.prototype, "topics", void 0);
|
|
174
132
|
Unsubscribe = Unsubscribe_1 = __decorate([
|
|
175
133
|
variant(2),
|
|
176
134
|
__metadata("design:paramtypes", [Object])
|
package/lib/esm/messages.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/messages.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,
|
|
1
|
+
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/messages.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7E,MAAM,OAAgB,aAAa;IAElC,MAAM,CAAC,IAAI,CAAC,KAAiB;QAC5B,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACvB,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;YACjB,OAAO,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC/B,CAAC;QACD,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;YACjB,OAAO,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC9B,CAAC;QACD,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;YACjB,OAAO,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAChC,CAAC;QAED,IAAI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CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,EAAE,EACtD,gBAAc,CACd,CAAC;QACF,IAAI,KAAK,YAAY,cAAc,EAAE,CAAC;YACrC,GAAG,CAAC,WAAW,GAAG,KAAK,CAAC;QACzB,CAAC;QACD,OAAO,GAAG,CAAC;IACZ,CAAC;CACD,CAAA;AA5BA;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;;8CACd;AAFL,cAAc;IAD1B,OAAO,CAAC,CAAC,CAAC;;GACE,cAAc,CA8B1B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/pubsub-interface",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Block store streaming",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -50,8 +50,8 @@
|
|
|
50
50
|
],
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"@dao-xyz/borsh": "^5.1.8",
|
|
53
|
-
"@peerbit/crypto": "1.0
|
|
54
|
-
"@peerbit/stream-interface": "^
|
|
53
|
+
"@peerbit/crypto": "2.1.0",
|
|
54
|
+
"@peerbit/stream-interface": "^2.0.1"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "cba5d4b1a6f3343f044c7d3194494c3572710cc8"
|
|
57
57
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
import { PublicSignKey } from "@peerbit/crypto";
|
|
2
|
-
import { PubSubData
|
|
2
|
+
import { PubSubData } from "./messages.js";
|
|
3
3
|
import {
|
|
4
4
|
Message,
|
|
5
5
|
DataMessage,
|
|
6
6
|
WaitForPeer,
|
|
7
|
-
PeerEvents
|
|
7
|
+
PeerEvents,
|
|
8
|
+
DeliveryMode
|
|
8
9
|
} from "@peerbit/stream-interface";
|
|
9
|
-
import { EventHandler } from "@libp2p/interface
|
|
10
|
+
import { EventHandler } from "@libp2p/interface";
|
|
10
11
|
import { PeerId as Libp2pPeerId } from "@libp2p/interface/peer-id";
|
|
11
|
-
import { field,
|
|
12
|
+
import { field, vec } from "@dao-xyz/borsh";
|
|
12
13
|
|
|
13
14
|
export class SubscriptionEvent {
|
|
14
15
|
@field({ type: PublicSignKey })
|
|
15
16
|
from: PublicSignKey;
|
|
16
17
|
|
|
17
|
-
@field({ type: vec(
|
|
18
|
-
subscriptions:
|
|
18
|
+
@field({ type: vec("string") })
|
|
19
|
+
subscriptions: string[];
|
|
19
20
|
|
|
20
|
-
constructor(from: PublicSignKey, subscriptions:
|
|
21
|
+
constructor(from: PublicSignKey, subscriptions: string[]) {
|
|
21
22
|
this.from = from;
|
|
22
23
|
this.subscriptions = subscriptions;
|
|
23
24
|
}
|
|
@@ -27,24 +28,45 @@ export class UnsubcriptionEvent {
|
|
|
27
28
|
@field({ type: PublicSignKey })
|
|
28
29
|
from: PublicSignKey;
|
|
29
30
|
|
|
30
|
-
@field({ type: vec(
|
|
31
|
-
unsubscriptions:
|
|
31
|
+
@field({ type: vec("string") })
|
|
32
|
+
unsubscriptions: string[];
|
|
32
33
|
|
|
33
|
-
constructor(from: PublicSignKey, unsubscriptions:
|
|
34
|
+
constructor(from: PublicSignKey, unsubscriptions: string[]) {
|
|
34
35
|
this.from = from;
|
|
35
36
|
this.unsubscriptions = unsubscriptions;
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
39
|
|
|
40
|
+
export class PublishEvent {
|
|
41
|
+
@field({ type: PubSubData })
|
|
42
|
+
data: PubSubData;
|
|
43
|
+
|
|
44
|
+
@field({ type: DataMessage })
|
|
45
|
+
message: DataMessage;
|
|
46
|
+
|
|
47
|
+
client?: string;
|
|
48
|
+
|
|
49
|
+
constructor(properties: {
|
|
50
|
+
client?: string;
|
|
51
|
+
data: PubSubData;
|
|
52
|
+
message: DataMessage;
|
|
53
|
+
}) {
|
|
54
|
+
this.client = properties.client;
|
|
55
|
+
this.data = properties.data;
|
|
56
|
+
this.message = properties.message;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
39
60
|
export class DataEvent {
|
|
40
61
|
@field({ type: PubSubData })
|
|
41
62
|
data: PubSubData;
|
|
42
63
|
|
|
43
64
|
@field({ type: DataMessage })
|
|
44
65
|
message: DataMessage;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
this.
|
|
66
|
+
|
|
67
|
+
constructor(properties: { data: PubSubData; message: DataMessage }) {
|
|
68
|
+
this.data = properties.data;
|
|
69
|
+
this.message = properties.message;
|
|
48
70
|
}
|
|
49
71
|
}
|
|
50
72
|
|
|
@@ -55,23 +77,14 @@ export class SubscriptionData {
|
|
|
55
77
|
@field({ type: "u64" })
|
|
56
78
|
timestamp: bigint;
|
|
57
79
|
|
|
58
|
-
|
|
59
|
-
type: option(Uint8Array)
|
|
60
|
-
})
|
|
61
|
-
data?: Uint8Array;
|
|
62
|
-
|
|
63
|
-
constructor(properties: {
|
|
64
|
-
publicKey: PublicSignKey;
|
|
65
|
-
timestamp: bigint;
|
|
66
|
-
data?: Uint8Array;
|
|
67
|
-
}) {
|
|
80
|
+
constructor(properties: { publicKey: PublicSignKey; timestamp: bigint }) {
|
|
68
81
|
this.publicKey = properties.publicKey;
|
|
69
82
|
this.timestamp = properties.timestamp;
|
|
70
|
-
this.data = properties.data;
|
|
71
83
|
}
|
|
72
84
|
}
|
|
73
85
|
|
|
74
86
|
export interface PubSubEvents extends PeerEvents {
|
|
87
|
+
publish: CustomEvent<DataEvent>;
|
|
75
88
|
data: CustomEvent<DataEvent>;
|
|
76
89
|
subscribe: CustomEvent<SubscriptionEvent>;
|
|
77
90
|
unsubscribe: CustomEvent<UnsubcriptionEvent>;
|
|
@@ -92,33 +105,25 @@ export interface IEventEmitter<EventMap extends Record<string, any>> {
|
|
|
92
105
|
}
|
|
93
106
|
|
|
94
107
|
type MaybePromise<T> = Promise<T> | T;
|
|
95
|
-
export type PublishOptions =
|
|
108
|
+
export type PublishOptions = (
|
|
96
109
|
| {
|
|
97
110
|
topics?: string[];
|
|
98
111
|
to?: (string | PublicSignKey | Libp2pPeerId)[];
|
|
99
|
-
|
|
112
|
+
mode?: DeliveryMode | undefined;
|
|
100
113
|
}
|
|
101
114
|
| {
|
|
102
115
|
topics: string[];
|
|
103
116
|
to: (string | PublicSignKey | Libp2pPeerId)[];
|
|
104
|
-
|
|
105
|
-
}
|
|
117
|
+
mode?: DeliveryMode | undefined;
|
|
118
|
+
}
|
|
119
|
+
) & { client?: string };
|
|
106
120
|
|
|
107
121
|
export interface PubSub extends IEventEmitter<PubSubEvents>, WaitForPeer {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
getSubscribers(
|
|
111
|
-
topic: string
|
|
112
|
-
): MaybePromise<Map<string, SubscriptionData> | undefined>;
|
|
122
|
+
getSubscribers(topic: string): MaybePromise<PublicSignKey[] | undefined>;
|
|
113
123
|
|
|
114
|
-
requestSubscribers(topic: string, from?: PublicSignKey):
|
|
124
|
+
requestSubscribers(topic: string, from?: PublicSignKey): MaybePromise<void>;
|
|
115
125
|
|
|
116
|
-
subscribe(
|
|
117
|
-
topic: string,
|
|
118
|
-
options?: {
|
|
119
|
-
data?: Uint8Array;
|
|
120
|
-
}
|
|
121
|
-
): Promise<void>;
|
|
126
|
+
subscribe(topic: string): MaybePromise<void>;
|
|
122
127
|
|
|
123
128
|
unsubscribe(
|
|
124
129
|
topic: string,
|
|
@@ -126,9 +131,9 @@ export interface PubSub extends IEventEmitter<PubSubEvents>, WaitForPeer {
|
|
|
126
131
|
force?: boolean;
|
|
127
132
|
data?: Uint8Array;
|
|
128
133
|
}
|
|
129
|
-
):
|
|
134
|
+
): MaybePromise<boolean>;
|
|
130
135
|
|
|
131
|
-
publish(data: Uint8Array, options?: PublishOptions):
|
|
136
|
+
publish(data: Uint8Array, options?: PublishOptions): MaybePromise<Uint8Array>;
|
|
132
137
|
}
|
|
133
138
|
|
|
134
139
|
export * from "./messages.js";
|
package/src/messages.ts
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import { Uint8ArrayList } from "uint8arraylist";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
vec,
|
|
5
|
-
variant,
|
|
6
|
-
serialize,
|
|
7
|
-
deserialize,
|
|
8
|
-
option
|
|
9
|
-
} from "@dao-xyz/borsh";
|
|
2
|
+
import { field, vec, variant, serialize, deserialize } from "@dao-xyz/borsh";
|
|
3
|
+
|
|
10
4
|
export abstract class PubSubMessage {
|
|
11
5
|
abstract bytes(): Uint8Array | Uint8ArrayList;
|
|
12
6
|
static from(bytes: Uint8Array) {
|
|
@@ -24,6 +18,7 @@ export abstract class PubSubMessage {
|
|
|
24
18
|
if (first === 3) {
|
|
25
19
|
return GetSubscribers.from(bytes);
|
|
26
20
|
}
|
|
21
|
+
|
|
27
22
|
throw new Error("Unsupported");
|
|
28
23
|
}
|
|
29
24
|
}
|
|
@@ -77,31 +72,17 @@ export class PubSubData extends PubSubMessage {
|
|
|
77
72
|
}
|
|
78
73
|
}
|
|
79
74
|
|
|
80
|
-
@variant(0)
|
|
81
|
-
export class Subscription {
|
|
82
|
-
@field({ type: "string" })
|
|
83
|
-
topic: string;
|
|
84
|
-
|
|
85
|
-
@field({ type: option(Uint8Array) })
|
|
86
|
-
data?: Uint8Array; // if omitted, the subcription event is a no-op (will not replace anything)
|
|
87
|
-
|
|
88
|
-
constructor(topic: string, data?: Uint8Array) {
|
|
89
|
-
this.topic = topic;
|
|
90
|
-
this.data = data;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
75
|
@variant(1)
|
|
95
76
|
export class Subscribe extends PubSubMessage {
|
|
96
|
-
@field({ type: vec(
|
|
97
|
-
|
|
77
|
+
@field({ type: vec("string") })
|
|
78
|
+
topics: string[];
|
|
98
79
|
|
|
99
|
-
constructor(options: {
|
|
80
|
+
constructor(options: { topics: string[] }) {
|
|
100
81
|
super();
|
|
101
|
-
this.
|
|
82
|
+
this.topics = options.topics;
|
|
102
83
|
}
|
|
103
84
|
|
|
104
|
-
_serialized: Uint8ArrayList;
|
|
85
|
+
private _serialized: Uint8ArrayList;
|
|
105
86
|
|
|
106
87
|
bytes() {
|
|
107
88
|
if (this._serialized) {
|
|
@@ -119,29 +100,16 @@ export class Subscribe extends PubSubMessage {
|
|
|
119
100
|
}
|
|
120
101
|
return ret;
|
|
121
102
|
}
|
|
122
|
-
|
|
123
|
-
get topics() {
|
|
124
|
-
return this.subscriptions.map((x) => x.topic);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
@variant(0)
|
|
129
|
-
export class Unsubscription {
|
|
130
|
-
@field({ type: "string" })
|
|
131
|
-
topic: string;
|
|
132
|
-
constructor(topic: string) {
|
|
133
|
-
this.topic = topic;
|
|
134
|
-
}
|
|
135
103
|
}
|
|
136
104
|
|
|
137
105
|
@variant(2)
|
|
138
106
|
export class Unsubscribe extends PubSubMessage {
|
|
139
|
-
@field({ type: vec(
|
|
140
|
-
|
|
107
|
+
@field({ type: vec("string") })
|
|
108
|
+
topics: string[];
|
|
141
109
|
|
|
142
110
|
constructor(options: { topics: string[] }) {
|
|
143
111
|
super();
|
|
144
|
-
this.
|
|
112
|
+
this.topics = options.topics;
|
|
145
113
|
}
|
|
146
114
|
|
|
147
115
|
_serialized: Uint8ArrayList;
|
|
@@ -163,10 +131,6 @@ export class Unsubscribe extends PubSubMessage {
|
|
|
163
131
|
}
|
|
164
132
|
return ret;
|
|
165
133
|
}
|
|
166
|
-
|
|
167
|
-
get topics() {
|
|
168
|
-
return this.unsubscriptions.map((x) => x.topic);
|
|
169
|
-
}
|
|
170
134
|
}
|
|
171
135
|
|
|
172
136
|
@variant(3)
|