@calimero-network/mero-js 23.2.0 → 23.3.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/dist/ephemeral/index.d.ts +6 -0
- package/dist/ephemeral/index.js +39 -29
- package/dist/ephemeral/types.d.ts +5 -0
- package/dist/index.browser.mjs +2 -2
- package/dist/index.browser.mjs.map +4 -4
- package/dist/index.cjs +170 -38
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/index.mjs +170 -38
- package/dist/index.mjs.map +3 -3
- package/dist/presence/relay-presence.d.ts +56 -0
- package/dist/presence/relay-presence.js +107 -0
- package/dist/presence/statement.d.ts +12 -0
- package/dist/presence/statement.js +17 -0
- package/dist/relay/relay-client.d.ts +13 -0
- package/dist/relay/relay-client.js +12 -0
- package/dist/transport/mero-client.d.ts +11 -12
- package/dist/transport/mero-client.js +14 -15
- package/package.json +1 -1
|
@@ -49,3 +49,9 @@ export declare class EphemeralClient {
|
|
|
49
49
|
*/
|
|
50
50
|
subscribe<T>(contextId: string, handler: (entry: EphemeralEntry<T>) => void, codec?: Codec<T>): () => void;
|
|
51
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Presence for one context off an event stream: a typed filter over the SSE
|
|
54
|
+
* events that already carry `{ contextId, type, data }`. Shared by a node
|
|
55
|
+
* client and an account's relay client, which read presence the same way.
|
|
56
|
+
*/
|
|
57
|
+
export declare function subscribePresence<T>(sse: SseClient, contextId: string, handler: (entry: EphemeralEntry<T>) => void, codec?: Codec<T>): () => void;
|
package/dist/ephemeral/index.js
CHANGED
|
@@ -48,34 +48,44 @@ export class EphemeralClient {
|
|
|
48
48
|
* and zero mean different things). Returns an unsubscribe function.
|
|
49
49
|
*/
|
|
50
50
|
subscribe(contextId, handler, codec = jsonCodec()) {
|
|
51
|
-
|
|
52
|
-
const e = event;
|
|
53
|
-
if (e.type !== 'Ephemeral' || e.contextId !== contextId)
|
|
54
|
-
return;
|
|
55
|
-
const data = e.data;
|
|
56
|
-
if (!data?.author)
|
|
57
|
-
return;
|
|
58
|
-
// `removed` is omitted on an upsert, so normalize to a boolean here and
|
|
59
|
-
// spare every caller the truthiness rule.
|
|
60
|
-
const removed = Boolean(data.removed);
|
|
61
|
-
const entry = {
|
|
62
|
-
author: data.author,
|
|
63
|
-
state: removed || data.state === undefined ? undefined : codec.decode(data.state),
|
|
64
|
-
removed,
|
|
65
|
-
};
|
|
66
|
-
// `ageMs` is present only on a replayed seed entry; pass it through as-is
|
|
67
|
-
// rather than defaulting a live delta to 0.
|
|
68
|
-
if (data.ageMs !== undefined)
|
|
69
|
-
entry.ageMs = data.ageMs;
|
|
70
|
-
handler(entry);
|
|
71
|
-
};
|
|
72
|
-
this.sse.on('event', listener);
|
|
73
|
-
// Errors surface via the SSE client's own 'error' event; nothing more to
|
|
74
|
-
// do with them here.
|
|
75
|
-
void this.sse.connect().catch(() => undefined);
|
|
76
|
-
void this.sse.subscribe([contextId]).catch(() => undefined);
|
|
77
|
-
return () => {
|
|
78
|
-
this.sse.off('event', listener);
|
|
79
|
-
};
|
|
51
|
+
return subscribePresence(this.sse, contextId, handler, codec);
|
|
80
52
|
}
|
|
81
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Presence for one context off an event stream: a typed filter over the SSE
|
|
56
|
+
* events that already carry `{ contextId, type, data }`. Shared by a node
|
|
57
|
+
* client and an account's relay client, which read presence the same way.
|
|
58
|
+
*/
|
|
59
|
+
export function subscribePresence(sse, contextId, handler, codec = jsonCodec()) {
|
|
60
|
+
const listener = (event) => {
|
|
61
|
+
const e = event;
|
|
62
|
+
if (e.type !== 'Ephemeral' || e.contextId !== contextId)
|
|
63
|
+
return;
|
|
64
|
+
const data = e.data;
|
|
65
|
+
if (!data?.author)
|
|
66
|
+
return;
|
|
67
|
+
// `removed` is omitted on an upsert, so normalize to a boolean here and
|
|
68
|
+
// spare every caller the truthiness rule.
|
|
69
|
+
const removed = Boolean(data.removed);
|
|
70
|
+
const entry = {
|
|
71
|
+
author: data.author,
|
|
72
|
+
state: removed || data.state === undefined ? undefined : codec.decode(data.state),
|
|
73
|
+
removed,
|
|
74
|
+
};
|
|
75
|
+
// `ageMs` is present only on a replayed seed entry; pass it through as-is
|
|
76
|
+
// rather than defaulting a live delta to 0.
|
|
77
|
+
if (data.ageMs !== undefined)
|
|
78
|
+
entry.ageMs = data.ageMs;
|
|
79
|
+
if (data.account !== undefined)
|
|
80
|
+
entry.account = data.account;
|
|
81
|
+
handler(entry);
|
|
82
|
+
};
|
|
83
|
+
sse.on('event', listener);
|
|
84
|
+
// Errors surface via the SSE client's own 'error' event; nothing more to
|
|
85
|
+
// do with them here.
|
|
86
|
+
void sse.connect().catch(() => undefined);
|
|
87
|
+
void sse.subscribe([contextId]).catch(() => undefined);
|
|
88
|
+
return () => {
|
|
89
|
+
sse.off('event', listener);
|
|
90
|
+
};
|
|
91
|
+
}
|
|
@@ -16,6 +16,11 @@ export interface Codec<T> {
|
|
|
16
16
|
* machines is needed. Bounded above by the node's 7s presence TTL. */
|
|
17
17
|
export interface EphemeralEntry<T> {
|
|
18
18
|
author: string;
|
|
19
|
+
/**
|
|
20
|
+
* The account a relay-carried update's device certificate names, hex. Absent
|
|
21
|
+
* for a node's own presence.
|
|
22
|
+
*/
|
|
23
|
+
account?: string;
|
|
19
24
|
state?: T;
|
|
20
25
|
removed?: boolean;
|
|
21
26
|
ageMs?: number;
|