@peerbit/pubsub-interface 1.0.3 → 1.0.5
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 +28 -18
- package/lib/esm/index.js +79 -0
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/messages.d.ts +10 -10
- package/lib/esm/messages.js +13 -13
- package/lib/esm/messages.js.map +1 -1
- package/package.json +5 -6
- package/src/index.ts +59 -20
- package/src/messages.ts +15 -14
- package/lib/esm/package.json +0 -3
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,31 +1,43 @@
|
|
|
1
1
|
import { PublicSignKey } from "@peerbit/crypto";
|
|
2
|
-
import { PubSubData, Subscription } from "./messages";
|
|
2
|
+
import { PubSubData, Subscription } from "./messages.js";
|
|
3
3
|
import { Message, DataMessage, WaitForPeer, PeerEvents } from "@peerbit/stream-interface";
|
|
4
4
|
import { EventHandler } from "@libp2p/interfaces/events";
|
|
5
5
|
import { PeerId as Libp2pPeerId } from "@libp2p/interface-peer-id";
|
|
6
|
-
|
|
6
|
+
export declare class SubscriptionEvent {
|
|
7
7
|
from: PublicSignKey;
|
|
8
|
-
}
|
|
9
|
-
export interface SubscriptionEvent extends From {
|
|
10
8
|
subscriptions: Subscription[];
|
|
9
|
+
constructor(from: PublicSignKey, subscriptions: Subscription[]);
|
|
11
10
|
}
|
|
12
|
-
export
|
|
11
|
+
export declare class UnsubcriptionEvent {
|
|
12
|
+
from: PublicSignKey;
|
|
13
13
|
unsubscriptions: Subscription[];
|
|
14
|
+
constructor(from: PublicSignKey, unsubscriptions: Subscription[]);
|
|
15
|
+
}
|
|
16
|
+
export declare class DataEvent {
|
|
17
|
+
data: PubSubData;
|
|
18
|
+
message: DataMessage;
|
|
19
|
+
constructor(data: PubSubData, message: DataMessage);
|
|
20
|
+
}
|
|
21
|
+
export declare class SubscriptionData {
|
|
22
|
+
timestamp: bigint;
|
|
23
|
+
data?: Uint8Array;
|
|
24
|
+
constructor(properties: {
|
|
25
|
+
timestamp: bigint;
|
|
26
|
+
data?: Uint8Array;
|
|
27
|
+
});
|
|
14
28
|
}
|
|
15
29
|
export interface PubSubEvents extends PeerEvents {
|
|
16
|
-
data: CustomEvent<
|
|
17
|
-
data: PubSubData;
|
|
18
|
-
message: DataMessage;
|
|
19
|
-
}>;
|
|
30
|
+
data: CustomEvent<DataEvent>;
|
|
20
31
|
subscribe: CustomEvent<SubscriptionEvent>;
|
|
21
32
|
unsubscribe: CustomEvent<UnsubcriptionEvent>;
|
|
22
33
|
message: CustomEvent<Message>;
|
|
23
34
|
}
|
|
24
35
|
export interface IEventEmitter<EventMap extends Record<string, any>> {
|
|
25
|
-
addEventListener<K extends keyof EventMap>(type: K, listener: EventHandler<EventMap[K]> | null, options?: boolean | AddEventListenerOptions): void
|
|
26
|
-
removeEventListener<K extends keyof EventMap>(type: K, listener?: EventHandler<EventMap[K]> | null, options?: boolean | EventListenerOptions): void
|
|
27
|
-
dispatchEvent(event: Event): boolean
|
|
36
|
+
addEventListener<K extends keyof EventMap>(type: K, listener: EventHandler<EventMap[K]> | null, options?: boolean | AddEventListenerOptions): MaybePromise<void>;
|
|
37
|
+
removeEventListener<K extends keyof EventMap>(type: K, listener?: EventHandler<EventMap[K]> | null, options?: boolean | EventListenerOptions): MaybePromise<void>;
|
|
38
|
+
dispatchEvent(event: Event): MaybePromise<boolean>;
|
|
28
39
|
}
|
|
40
|
+
type MaybePromise<T> = Promise<T> | T;
|
|
29
41
|
export type PublishOptions = {
|
|
30
42
|
topics?: string[];
|
|
31
43
|
to?: (string | PublicSignKey | Libp2pPeerId)[];
|
|
@@ -36,17 +48,15 @@ export type PublishOptions = {
|
|
|
36
48
|
strict: true;
|
|
37
49
|
};
|
|
38
50
|
export interface PubSub extends IEventEmitter<PubSubEvents>, WaitForPeer {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
data?: Uint8Array;
|
|
42
|
-
}> | undefined;
|
|
51
|
+
emitSelf: boolean;
|
|
52
|
+
getSubscribers(topic: string): MaybePromise<Map<string, SubscriptionData> | undefined>;
|
|
43
53
|
requestSubscribers(topic: string, from?: PublicSignKey): Promise<void>;
|
|
44
54
|
subscribe(topic: string, options?: {
|
|
45
55
|
data?: Uint8Array;
|
|
46
56
|
}): Promise<void>;
|
|
47
57
|
unsubscribe(topic: string, options?: {
|
|
48
|
-
force
|
|
49
|
-
data
|
|
58
|
+
force?: boolean;
|
|
59
|
+
data?: Uint8Array;
|
|
50
60
|
}): Promise<boolean>;
|
|
51
61
|
publish(data: Uint8Array, options?: PublishOptions): Promise<Uint8Array>;
|
|
52
62
|
}
|
package/lib/esm/index.js
CHANGED
|
@@ -1,2 +1,81 @@
|
|
|
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
|
+
import { PublicSignKey } from "@peerbit/crypto";
|
|
11
|
+
import { PubSubData, Subscription } from "./messages.js";
|
|
12
|
+
import { DataMessage, } from "@peerbit/stream-interface";
|
|
13
|
+
import { field, option, vec } from "@dao-xyz/borsh";
|
|
14
|
+
export class SubscriptionEvent {
|
|
15
|
+
from;
|
|
16
|
+
subscriptions;
|
|
17
|
+
constructor(from, subscriptions) {
|
|
18
|
+
this.from = from;
|
|
19
|
+
this.subscriptions = subscriptions;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
__decorate([
|
|
23
|
+
field({ type: PublicSignKey }),
|
|
24
|
+
__metadata("design:type", PublicSignKey)
|
|
25
|
+
], SubscriptionEvent.prototype, "from", void 0);
|
|
26
|
+
__decorate([
|
|
27
|
+
field({ type: vec(Subscription) }),
|
|
28
|
+
__metadata("design:type", Array)
|
|
29
|
+
], SubscriptionEvent.prototype, "subscriptions", void 0);
|
|
30
|
+
export class UnsubcriptionEvent {
|
|
31
|
+
from;
|
|
32
|
+
unsubscriptions;
|
|
33
|
+
constructor(from, unsubscriptions) {
|
|
34
|
+
this.from = from;
|
|
35
|
+
this.unsubscriptions = unsubscriptions;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
__decorate([
|
|
39
|
+
field({ type: PublicSignKey }),
|
|
40
|
+
__metadata("design:type", PublicSignKey)
|
|
41
|
+
], UnsubcriptionEvent.prototype, "from", void 0);
|
|
42
|
+
__decorate([
|
|
43
|
+
field({ type: vec(Subscription) }),
|
|
44
|
+
__metadata("design:type", Array)
|
|
45
|
+
], UnsubcriptionEvent.prototype, "unsubscriptions", void 0);
|
|
46
|
+
export class DataEvent {
|
|
47
|
+
data;
|
|
48
|
+
message;
|
|
49
|
+
constructor(data, message) {
|
|
50
|
+
this.data = data;
|
|
51
|
+
this.message = message;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
__decorate([
|
|
55
|
+
field({ type: PubSubData }),
|
|
56
|
+
__metadata("design:type", PubSubData)
|
|
57
|
+
], DataEvent.prototype, "data", void 0);
|
|
58
|
+
__decorate([
|
|
59
|
+
field({ type: DataMessage }),
|
|
60
|
+
__metadata("design:type", DataMessage)
|
|
61
|
+
], DataEvent.prototype, "message", void 0);
|
|
62
|
+
export class SubscriptionData {
|
|
63
|
+
timestamp;
|
|
64
|
+
data;
|
|
65
|
+
constructor(properties) {
|
|
66
|
+
this.timestamp = properties.timestamp;
|
|
67
|
+
this.data = properties.data;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
__decorate([
|
|
71
|
+
field({ type: "u64" }),
|
|
72
|
+
__metadata("design:type", BigInt)
|
|
73
|
+
], SubscriptionData.prototype, "timestamp", void 0);
|
|
74
|
+
__decorate([
|
|
75
|
+
field({
|
|
76
|
+
type: option(Uint8Array),
|
|
77
|
+
}),
|
|
78
|
+
__metadata("design:type", Uint8Array)
|
|
79
|
+
], SubscriptionData.prototype, "data", void 0);
|
|
1
80
|
export * from "./messages.js";
|
|
2
81
|
//# 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":"
|
|
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,YAAY,EAAE,MAAM,eAAe,CAAC;AACzD,OAAO,EAEN,WAAW,GAGX,MAAM,2BAA2B,CAAC;AAGnC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAEpD,MAAM,OAAO,iBAAiB;IAE7B,IAAI,CAAgB;IAGpB,aAAa,CAAiB;IAE9B,YAAY,IAAmB,EAAE,aAA6B;QAC7D,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,YAAY,CAAC,EAAE,CAAC;;wDACL;AAQ/B,MAAM,OAAO,kBAAkB;IAE9B,IAAI,CAAgB;IAGpB,eAAe,CAAiB;IAEhC,YAAY,IAAmB,EAAE,eAA+B;QAC/D,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;IACxC,CAAC;CACD;AATA;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC;8BACzB,aAAa;gDAAC;AAGpB;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,GAAG,CAAC,YAAY,CAAC,EAAE,CAAC;;2DACH;AAQjC,MAAM,OAAO,SAAS;IAErB,IAAI,CAAa;IAGjB,OAAO,CAAc;IACrB,YAAY,IAAgB,EAAE,OAAoB;QACjD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACxB,CAAC;CACD;AARA;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC;8BACtB,UAAU;uCAAC;AAGjB;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC;8BACpB,WAAW;0CAAC;AAOtB,MAAM,OAAO,gBAAgB;IAE5B,SAAS,CAAS;IAKlB,IAAI,CAAc;IAElB,YAAY,UAAoD;QAC/D,IAAI,CAAC,SAAS,GAAG,UAAU,CAAC,SAAS,CAAC;QACtC,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC;IAC7B,CAAC;CACD;AAXA;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;;mDACL;AAKlB;IAHC,KAAK,CAAC;QACN,IAAI,EAAE,MAAM,CAAC,UAAU,CAAC;KACxB,CAAC;8BACK,UAAU;8CAAC;AAoEnB,cAAc,eAAe,CAAC"}
|
package/lib/esm/messages.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Uint8ArrayList } from "uint8arraylist";
|
|
2
2
|
export declare abstract class PubSubMessage {
|
|
3
|
-
abstract
|
|
4
|
-
static
|
|
3
|
+
abstract bytes(): Uint8Array | Uint8ArrayList;
|
|
4
|
+
static from(bytes: Uint8Array): PubSubData | Subscribe | Unsubscribe | GetSubscribers;
|
|
5
5
|
}
|
|
6
6
|
export declare const toUint8Array: (arr: Uint8ArrayList | Uint8Array) => Uint8Array;
|
|
7
7
|
export declare class PubSubData extends PubSubMessage {
|
|
@@ -14,8 +14,8 @@ export declare class PubSubData extends PubSubMessage {
|
|
|
14
14
|
strict?: boolean;
|
|
15
15
|
});
|
|
16
16
|
_serialized: Uint8ArrayList;
|
|
17
|
-
|
|
18
|
-
static
|
|
17
|
+
bytes(): Uint8Array | Uint8ArrayList;
|
|
18
|
+
static from(bytes: Uint8Array | Uint8ArrayList): PubSubData;
|
|
19
19
|
}
|
|
20
20
|
export declare class Subscription {
|
|
21
21
|
topic: string;
|
|
@@ -28,8 +28,8 @@ export declare class Subscribe extends PubSubMessage {
|
|
|
28
28
|
subscriptions: Subscription[];
|
|
29
29
|
});
|
|
30
30
|
_serialized: Uint8ArrayList;
|
|
31
|
-
|
|
32
|
-
static
|
|
31
|
+
bytes(): Uint8Array | Uint8ArrayList;
|
|
32
|
+
static from(bytes: Uint8Array | Uint8ArrayList): Subscribe;
|
|
33
33
|
get topics(): string[];
|
|
34
34
|
}
|
|
35
35
|
export declare class Unsubscription {
|
|
@@ -42,8 +42,8 @@ export declare class Unsubscribe extends PubSubMessage {
|
|
|
42
42
|
topics: string[];
|
|
43
43
|
});
|
|
44
44
|
_serialized: Uint8ArrayList;
|
|
45
|
-
|
|
46
|
-
static
|
|
45
|
+
bytes(): Uint8Array | Uint8ArrayList;
|
|
46
|
+
static from(bytes: Uint8Array | Uint8ArrayList): Unsubscribe;
|
|
47
47
|
get topics(): string[];
|
|
48
48
|
}
|
|
49
49
|
export declare class GetSubscribers extends PubSubMessage {
|
|
@@ -52,6 +52,6 @@ export declare class GetSubscribers extends PubSubMessage {
|
|
|
52
52
|
topics: string[];
|
|
53
53
|
});
|
|
54
54
|
_serialized: Uint8ArrayList;
|
|
55
|
-
|
|
56
|
-
static
|
|
55
|
+
bytes(): Uint8Array | Uint8ArrayList;
|
|
56
|
+
static from(bytes: Uint8Array | Uint8ArrayList): GetSubscribers;
|
|
57
57
|
}
|
package/lib/esm/messages.js
CHANGED
|
@@ -11,19 +11,19 @@ var PubSubData_1, Subscribe_1, Unsubscribe_1, GetSubscribers_1;
|
|
|
11
11
|
import { Uint8ArrayList } from "uint8arraylist";
|
|
12
12
|
import { field, vec, variant, serialize, deserialize, option, } from "@dao-xyz/borsh";
|
|
13
13
|
export class PubSubMessage {
|
|
14
|
-
static
|
|
14
|
+
static from(bytes) {
|
|
15
15
|
const first = bytes[0];
|
|
16
16
|
if (first === 0) {
|
|
17
|
-
return PubSubData.
|
|
17
|
+
return PubSubData.from(bytes);
|
|
18
18
|
}
|
|
19
19
|
if (first === 1) {
|
|
20
|
-
return Subscribe.
|
|
20
|
+
return Subscribe.from(bytes);
|
|
21
21
|
}
|
|
22
22
|
if (first === 2) {
|
|
23
|
-
return Unsubscribe.
|
|
23
|
+
return Unsubscribe.from(bytes);
|
|
24
24
|
}
|
|
25
25
|
if (first === 3) {
|
|
26
|
-
return GetSubscribers.
|
|
26
|
+
return GetSubscribers.from(bytes);
|
|
27
27
|
}
|
|
28
28
|
throw new Error("Unsupported");
|
|
29
29
|
}
|
|
@@ -43,13 +43,13 @@ export let PubSubData = PubSubData_1 = class PubSubData extends PubSubMessage {
|
|
|
43
43
|
this.strict = options.strict ?? false;
|
|
44
44
|
}
|
|
45
45
|
_serialized;
|
|
46
|
-
|
|
46
|
+
bytes() {
|
|
47
47
|
if (this._serialized) {
|
|
48
48
|
return this._serialized;
|
|
49
49
|
}
|
|
50
50
|
return serialize(this);
|
|
51
51
|
}
|
|
52
|
-
static
|
|
52
|
+
static from(bytes) {
|
|
53
53
|
const ret = deserialize(bytes instanceof Uint8Array ? bytes : bytes.subarray(), PubSubData_1);
|
|
54
54
|
if (bytes instanceof Uint8ArrayList) {
|
|
55
55
|
ret._serialized = bytes;
|
|
@@ -100,13 +100,13 @@ export let Subscribe = Subscribe_1 = class Subscribe extends PubSubMessage {
|
|
|
100
100
|
this.subscriptions = options.subscriptions;
|
|
101
101
|
}
|
|
102
102
|
_serialized;
|
|
103
|
-
|
|
103
|
+
bytes() {
|
|
104
104
|
if (this._serialized) {
|
|
105
105
|
return this._serialized;
|
|
106
106
|
}
|
|
107
107
|
return serialize(this);
|
|
108
108
|
}
|
|
109
|
-
static
|
|
109
|
+
static from(bytes) {
|
|
110
110
|
const ret = deserialize(bytes instanceof Uint8Array ? bytes : bytes.subarray(), Subscribe_1);
|
|
111
111
|
if (bytes instanceof Uint8ArrayList) {
|
|
112
112
|
ret._serialized = bytes;
|
|
@@ -146,13 +146,13 @@ export let Unsubscribe = Unsubscribe_1 = class Unsubscribe extends PubSubMessage
|
|
|
146
146
|
this.unsubscriptions = options.topics.map((x) => new Unsubscription(x));
|
|
147
147
|
}
|
|
148
148
|
_serialized;
|
|
149
|
-
|
|
149
|
+
bytes() {
|
|
150
150
|
if (this._serialized) {
|
|
151
151
|
return this._serialized;
|
|
152
152
|
}
|
|
153
153
|
return serialize(this);
|
|
154
154
|
}
|
|
155
|
-
static
|
|
155
|
+
static from(bytes) {
|
|
156
156
|
const ret = deserialize(bytes instanceof Uint8Array ? bytes : bytes.subarray(), Unsubscribe_1);
|
|
157
157
|
if (bytes instanceof Uint8ArrayList) {
|
|
158
158
|
ret._serialized = bytes;
|
|
@@ -179,13 +179,13 @@ export let GetSubscribers = GetSubscribers_1 = class GetSubscribers extends PubS
|
|
|
179
179
|
this.topics = options.topics;
|
|
180
180
|
}
|
|
181
181
|
_serialized;
|
|
182
|
-
|
|
182
|
+
bytes() {
|
|
183
183
|
if (this._serialized) {
|
|
184
184
|
return this._serialized;
|
|
185
185
|
}
|
|
186
186
|
return serialize(this);
|
|
187
187
|
}
|
|
188
|
-
static
|
|
188
|
+
static from(bytes) {
|
|
189
189
|
const ret = deserialize(bytes instanceof Uint8Array ? bytes : bytes.subarray(), GetSubscribers_1);
|
|
190
190
|
if (bytes instanceof Uint8ArrayList) {
|
|
191
191
|
ret._serialized = bytes;
|
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,EACN,KAAK,EACL,GAAG,EACH,OAAO,EACP,SAAS,EACT,WAAW,EACX,MAAM,GACN,MAAM,gBAAgB,CAAC;AACxB,MAAM,OAAgB,aAAa;IAElC,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/messages.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EACN,KAAK,EACL,GAAG,EACH,OAAO,EACP,SAAS,EACT,WAAW,EACX,MAAM,GACN,MAAM,gBAAgB,CAAC;AACxB,MAAM,OAAgB,aAAa;IAElC,MAAM,CAAC,IAAI,CAAC,KAAiB;QAC5B,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACvB,IAAI,KAAK,KAAK,CAAC,EAAE;YAChB,OAAO,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;SAC9B;QACD,IAAI,KAAK,KAAK,CAAC,EAAE;YAChB,OAAO,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;SAC7B;QACD,IAAI,KAAK,KAAK,CAAC,EAAE;YAChB,OAAO,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;SAC/B;QAED,IAAI,KAAK,KAAK,CAAC,EAAE;YAChB,OAAO,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;SAClC;QACD,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC;IAChC,CAAC;CACD;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,C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|
|
2
2
|
"name": "@peerbit/pubsub-interface",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Block store streaming",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -16,8 +16,7 @@
|
|
|
16
16
|
"scripts": {
|
|
17
17
|
"clean": "shx rm -rf lib/*",
|
|
18
18
|
"build": "yarn clean && tsc -p tsconfig.json",
|
|
19
|
-
"
|
|
20
|
-
"test": "node ../../../node_modules/.binß/jest test -c ../../../jest.config.ts --runInBand --forceExit",
|
|
19
|
+
"test": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.ts --runInBand --forceExit",
|
|
21
20
|
"test:unit": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.unit.ts --runInBand --forceExit",
|
|
22
21
|
"test:integration": "node ../node_modules/.bin/jest test -c ../../../jest.config.integration.ts --runInBand --forceExit"
|
|
23
22
|
},
|
|
@@ -51,8 +50,8 @@
|
|
|
51
50
|
],
|
|
52
51
|
"dependencies": {
|
|
53
52
|
"@dao-xyz/borsh": "^5.1.5",
|
|
54
|
-
"@peerbit/crypto": "1.0.
|
|
55
|
-
"@peerbit/stream-interface": "^1.0.
|
|
53
|
+
"@peerbit/crypto": "1.0.4",
|
|
54
|
+
"@peerbit/stream-interface": "^1.0.5"
|
|
56
55
|
},
|
|
57
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "32f61ad9f3d5906e13d46f2a80f61c1826a3e817"
|
|
58
57
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PublicSignKey } from "@peerbit/crypto";
|
|
2
|
-
import { PubSubData, Subscription } from "./messages";
|
|
2
|
+
import { PubSubData, Subscription } from "./messages.js";
|
|
3
3
|
import {
|
|
4
4
|
Message,
|
|
5
5
|
DataMessage,
|
|
@@ -8,21 +8,63 @@ import {
|
|
|
8
8
|
} from "@peerbit/stream-interface";
|
|
9
9
|
import { EventHandler } from "@libp2p/interfaces/events";
|
|
10
10
|
import { PeerId as Libp2pPeerId } from "@libp2p/interface-peer-id";
|
|
11
|
+
import { field, option, vec } from "@dao-xyz/borsh";
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
export class SubscriptionEvent {
|
|
14
|
+
@field({ type: PublicSignKey })
|
|
13
15
|
from: PublicSignKey;
|
|
14
|
-
}
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
@field({ type: vec(Subscription) })
|
|
17
18
|
subscriptions: Subscription[];
|
|
19
|
+
|
|
20
|
+
constructor(from: PublicSignKey, subscriptions: Subscription[]) {
|
|
21
|
+
this.from = from;
|
|
22
|
+
this.subscriptions = subscriptions;
|
|
23
|
+
}
|
|
18
24
|
}
|
|
19
25
|
|
|
20
|
-
export
|
|
26
|
+
export class UnsubcriptionEvent {
|
|
27
|
+
@field({ type: PublicSignKey })
|
|
28
|
+
from: PublicSignKey;
|
|
29
|
+
|
|
30
|
+
@field({ type: vec(Subscription) })
|
|
21
31
|
unsubscriptions: Subscription[];
|
|
32
|
+
|
|
33
|
+
constructor(from: PublicSignKey, unsubscriptions: Subscription[]) {
|
|
34
|
+
this.from = from;
|
|
35
|
+
this.unsubscriptions = unsubscriptions;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class DataEvent {
|
|
40
|
+
@field({ type: PubSubData })
|
|
41
|
+
data: PubSubData;
|
|
42
|
+
|
|
43
|
+
@field({ type: DataMessage })
|
|
44
|
+
message: DataMessage;
|
|
45
|
+
constructor(data: PubSubData, message: DataMessage) {
|
|
46
|
+
this.data = data;
|
|
47
|
+
this.message = message;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export class SubscriptionData {
|
|
52
|
+
@field({ type: "u64" })
|
|
53
|
+
timestamp: bigint;
|
|
54
|
+
|
|
55
|
+
@field({
|
|
56
|
+
type: option(Uint8Array),
|
|
57
|
+
})
|
|
58
|
+
data?: Uint8Array;
|
|
59
|
+
|
|
60
|
+
constructor(properties: { timestamp: bigint; data?: Uint8Array }) {
|
|
61
|
+
this.timestamp = properties.timestamp;
|
|
62
|
+
this.data = properties.data;
|
|
63
|
+
}
|
|
22
64
|
}
|
|
23
65
|
|
|
24
66
|
export interface PubSubEvents extends PeerEvents {
|
|
25
|
-
data: CustomEvent<
|
|
67
|
+
data: CustomEvent<DataEvent>;
|
|
26
68
|
subscribe: CustomEvent<SubscriptionEvent>;
|
|
27
69
|
unsubscribe: CustomEvent<UnsubcriptionEvent>;
|
|
28
70
|
message: CustomEvent<Message>;
|
|
@@ -32,15 +74,16 @@ export interface IEventEmitter<EventMap extends Record<string, any>> {
|
|
|
32
74
|
type: K,
|
|
33
75
|
listener: EventHandler<EventMap[K]> | null,
|
|
34
76
|
options?: boolean | AddEventListenerOptions
|
|
35
|
-
): void
|
|
77
|
+
): MaybePromise<void>;
|
|
36
78
|
removeEventListener<K extends keyof EventMap>(
|
|
37
79
|
type: K,
|
|
38
80
|
listener?: EventHandler<EventMap[K]> | null,
|
|
39
81
|
options?: boolean | EventListenerOptions
|
|
40
|
-
): void
|
|
41
|
-
dispatchEvent(event: Event): boolean
|
|
82
|
+
): MaybePromise<void>;
|
|
83
|
+
dispatchEvent(event: Event): MaybePromise<boolean>;
|
|
42
84
|
}
|
|
43
85
|
|
|
86
|
+
type MaybePromise<T> = Promise<T> | T;
|
|
44
87
|
export type PublishOptions =
|
|
45
88
|
| {
|
|
46
89
|
topics?: string[];
|
|
@@ -54,15 +97,11 @@ export type PublishOptions =
|
|
|
54
97
|
};
|
|
55
98
|
|
|
56
99
|
export interface PubSub extends IEventEmitter<PubSubEvents>, WaitForPeer {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
data?: Uint8Array;
|
|
63
|
-
}
|
|
64
|
-
>
|
|
65
|
-
| undefined;
|
|
100
|
+
emitSelf: boolean;
|
|
101
|
+
|
|
102
|
+
getSubscribers(
|
|
103
|
+
topic: string
|
|
104
|
+
): MaybePromise<Map<string, SubscriptionData> | undefined>;
|
|
66
105
|
|
|
67
106
|
requestSubscribers(topic: string, from?: PublicSignKey): Promise<void>;
|
|
68
107
|
|
|
@@ -76,8 +115,8 @@ export interface PubSub extends IEventEmitter<PubSubEvents>, WaitForPeer {
|
|
|
76
115
|
unsubscribe(
|
|
77
116
|
topic: string,
|
|
78
117
|
options?: {
|
|
79
|
-
force
|
|
80
|
-
data
|
|
118
|
+
force?: boolean;
|
|
119
|
+
data?: Uint8Array;
|
|
81
120
|
}
|
|
82
121
|
): Promise<boolean>;
|
|
83
122
|
|
package/src/messages.ts
CHANGED
|
@@ -8,21 +8,21 @@ import {
|
|
|
8
8
|
option,
|
|
9
9
|
} from "@dao-xyz/borsh";
|
|
10
10
|
export abstract class PubSubMessage {
|
|
11
|
-
abstract
|
|
12
|
-
static
|
|
11
|
+
abstract bytes(): Uint8Array | Uint8ArrayList;
|
|
12
|
+
static from(bytes: Uint8Array) {
|
|
13
13
|
const first = bytes[0];
|
|
14
14
|
if (first === 0) {
|
|
15
|
-
return PubSubData.
|
|
15
|
+
return PubSubData.from(bytes);
|
|
16
16
|
}
|
|
17
17
|
if (first === 1) {
|
|
18
|
-
return Subscribe.
|
|
18
|
+
return Subscribe.from(bytes);
|
|
19
19
|
}
|
|
20
20
|
if (first === 2) {
|
|
21
|
-
return Unsubscribe.
|
|
21
|
+
return Unsubscribe.from(bytes);
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
if (first === 3) {
|
|
25
|
-
return GetSubscribers.
|
|
25
|
+
return GetSubscribers.from(bytes);
|
|
26
26
|
}
|
|
27
27
|
throw new Error("Unsupported");
|
|
28
28
|
}
|
|
@@ -58,14 +58,14 @@ export class PubSubData extends PubSubMessage {
|
|
|
58
58
|
|
|
59
59
|
_serialized: Uint8ArrayList;
|
|
60
60
|
|
|
61
|
-
|
|
61
|
+
bytes() {
|
|
62
62
|
if (this._serialized) {
|
|
63
63
|
return this._serialized;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
return serialize(this);
|
|
67
67
|
}
|
|
68
|
-
static
|
|
68
|
+
static from(bytes: Uint8Array | Uint8ArrayList): PubSubData {
|
|
69
69
|
const ret = deserialize(
|
|
70
70
|
bytes instanceof Uint8Array ? bytes : bytes.subarray(),
|
|
71
71
|
PubSubData
|
|
@@ -103,13 +103,13 @@ export class Subscribe extends PubSubMessage {
|
|
|
103
103
|
|
|
104
104
|
_serialized: Uint8ArrayList;
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
bytes() {
|
|
107
107
|
if (this._serialized) {
|
|
108
108
|
return this._serialized;
|
|
109
109
|
}
|
|
110
110
|
return serialize(this);
|
|
111
111
|
}
|
|
112
|
-
static
|
|
112
|
+
static from(bytes: Uint8Array | Uint8ArrayList): Subscribe {
|
|
113
113
|
const ret = deserialize(
|
|
114
114
|
bytes instanceof Uint8Array ? bytes : bytes.subarray(),
|
|
115
115
|
Subscribe
|
|
@@ -146,14 +146,14 @@ export class Unsubscribe extends PubSubMessage {
|
|
|
146
146
|
|
|
147
147
|
_serialized: Uint8ArrayList;
|
|
148
148
|
|
|
149
|
-
|
|
149
|
+
bytes() {
|
|
150
150
|
if (this._serialized) {
|
|
151
151
|
return this._serialized;
|
|
152
152
|
}
|
|
153
153
|
return serialize(this);
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
static
|
|
156
|
+
static from(bytes: Uint8Array | Uint8ArrayList): Unsubscribe {
|
|
157
157
|
const ret = deserialize(
|
|
158
158
|
bytes instanceof Uint8Array ? bytes : bytes.subarray(),
|
|
159
159
|
Unsubscribe
|
|
@@ -163,6 +163,7 @@ export class Unsubscribe extends PubSubMessage {
|
|
|
163
163
|
}
|
|
164
164
|
return ret;
|
|
165
165
|
}
|
|
166
|
+
|
|
166
167
|
get topics() {
|
|
167
168
|
return this.unsubscriptions.map((x) => x.topic);
|
|
168
169
|
}
|
|
@@ -182,14 +183,14 @@ export class GetSubscribers extends PubSubMessage {
|
|
|
182
183
|
|
|
183
184
|
_serialized: Uint8ArrayList;
|
|
184
185
|
|
|
185
|
-
|
|
186
|
+
bytes() {
|
|
186
187
|
if (this._serialized) {
|
|
187
188
|
return this._serialized;
|
|
188
189
|
}
|
|
189
190
|
return serialize(this);
|
|
190
191
|
}
|
|
191
192
|
|
|
192
|
-
static
|
|
193
|
+
static from(bytes: Uint8Array | Uint8ArrayList): GetSubscribers {
|
|
193
194
|
const ret = deserialize(
|
|
194
195
|
bytes instanceof Uint8Array ? bytes : bytes.subarray(),
|
|
195
196
|
GetSubscribers
|
package/lib/esm/package.json
DELETED