@waku/sds 0.0.4-10590da.0 → 0.0.4-383e0b2.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.
@@ -1,11 +1,9 @@
1
1
  import { TypedEventEmitter } from "@libp2p/interface";
2
2
  import { sha256 } from "@noble/hashes/sha256";
3
3
  import { bytesToHex } from "@noble/hashes/utils";
4
- import { DefaultBloomFilter } from "./bloom.js";
5
- export var MessageChannelEvent;
6
- (function (MessageChannelEvent) {
7
- MessageChannelEvent["MessageDelivered"] = "messageDelivered";
8
- })(MessageChannelEvent || (MessageChannelEvent = {}));
4
+ import { DefaultBloomFilter } from "../bloom_filter/bloom.js";
5
+ import { Command } from "./command_queue.js";
6
+ import { MessageChannelEvent } from "./events.js";
9
7
  export const DEFAULT_BLOOM_FILTER_OPTIONS = {
10
8
  capacity: 10000,
11
9
  errorRate: 0.001
@@ -25,7 +23,18 @@ export class MessageChannel extends TypedEventEmitter {
25
23
  timeReceived;
26
24
  receivedMessageTimeoutEnabled;
27
25
  receivedMessageTimeout;
28
- deliveredMessageCallback;
26
+ tasks = [];
27
+ handlers = {
28
+ [Command.Send]: async (params) => {
29
+ await this._sendMessage(params.payload, params.callback);
30
+ },
31
+ [Command.Receive]: async (params) => {
32
+ this._receiveMessage(params.message);
33
+ },
34
+ [Command.SendEphemeral]: async (params) => {
35
+ await this._sendEphemeralMessage(params.payload, params.callback);
36
+ }
37
+ };
29
38
  constructor(channelId, options = {}) {
30
39
  super();
31
40
  this.channelId = channelId;
@@ -43,7 +52,22 @@ export class MessageChannel extends TypedEventEmitter {
43
52
  options.receivedMessageTimeoutEnabled ?? false;
44
53
  this.receivedMessageTimeout =
45
54
  options.receivedMessageTimeout ?? DEFAULT_RECEIVED_MESSAGE_TIMEOUT;
46
- this.deliveredMessageCallback = options.deliveredMessageCallback;
55
+ }
56
+ // Periodically called by the library consumer to process async operations
57
+ // in a sequential manner.
58
+ async processTasks() {
59
+ while (this.tasks.length > 0) {
60
+ const item = this.tasks.shift();
61
+ if (!item) {
62
+ continue;
63
+ }
64
+ // Use a generic helper function to ensure type safety
65
+ await this.executeTask(item);
66
+ }
67
+ }
68
+ async executeTask(item) {
69
+ const handler = this.handlers[item.command];
70
+ await handler(item.params);
47
71
  }
48
72
  static getMessageId(payload) {
49
73
  return bytesToHex(sha256(payload));
@@ -65,6 +89,15 @@ export class MessageChannel extends TypedEventEmitter {
65
89
  * @param callback - A callback function that returns a boolean indicating whether the message was sent successfully.
66
90
  */
67
91
  async sendMessage(payload, callback) {
92
+ this.tasks.push({
93
+ command: Command.Send,
94
+ params: {
95
+ payload,
96
+ callback
97
+ }
98
+ });
99
+ }
100
+ async _sendMessage(payload, callback) {
68
101
  this.lamportTimestamp++;
69
102
  const messageId = MessageChannel.getMessageId(payload);
70
103
  const message = {
@@ -89,6 +122,10 @@ export class MessageChannel extends TypedEventEmitter {
89
122
  retrievalHint
90
123
  }
91
124
  });
125
+ this.timeReceived.set(messageId, Date.now());
126
+ this.safeDispatchEvent(MessageChannelEvent.MessageSent, {
127
+ detail: message
128
+ });
92
129
  }
93
130
  }
94
131
  }
@@ -105,7 +142,16 @@ export class MessageChannel extends TypedEventEmitter {
105
142
  * @param payload - The payload to send.
106
143
  * @param callback - A callback function that returns a boolean indicating whether the message was sent successfully.
107
144
  */
108
- sendEphemeralMessage(payload, callback) {
145
+ async sendEphemeralMessage(payload, callback) {
146
+ this.tasks.push({
147
+ command: Command.SendEphemeral,
148
+ params: {
149
+ payload,
150
+ callback
151
+ }
152
+ });
153
+ }
154
+ async _sendEphemeralMessage(payload, callback) {
109
155
  const message = {
110
156
  messageId: MessageChannel.getMessageId(payload),
111
157
  channelId: this.channelId,
@@ -115,7 +161,7 @@ export class MessageChannel extends TypedEventEmitter {
115
161
  bloomFilter: undefined
116
162
  };
117
163
  if (callback) {
118
- callback(message);
164
+ await callback(message);
119
165
  }
120
166
  }
121
167
  /**
@@ -133,11 +179,35 @@ export class MessageChannel extends TypedEventEmitter {
133
179
  * @param message - The received SDS message.
134
180
  */
135
181
  receiveMessage(message) {
182
+ this.tasks.push({
183
+ command: Command.Receive,
184
+ params: {
185
+ message
186
+ }
187
+ });
188
+ }
189
+ _receiveMessage(message) {
190
+ if (message.content &&
191
+ message.content.length > 0 &&
192
+ this.timeReceived.has(message.messageId)) {
193
+ // Received a duplicate message
194
+ return;
195
+ }
136
196
  if (!message.lamportTimestamp) {
137
197
  // Messages with no timestamp are ephemeral messages and should be delivered immediately
138
198
  this.deliverMessage(message);
139
199
  return;
140
200
  }
201
+ if (message.content?.length === 0) {
202
+ this.safeDispatchEvent(MessageChannelEvent.SyncReceived, {
203
+ detail: message
204
+ });
205
+ }
206
+ else {
207
+ this.safeDispatchEvent(MessageChannelEvent.MessageReceived, {
208
+ detail: message
209
+ });
210
+ }
141
211
  // review ack status
142
212
  this.reviewAckStatus(message);
143
213
  // add to bloom filter (skip for messages with empty content)
@@ -152,9 +222,17 @@ export class MessageChannel extends TypedEventEmitter {
152
222
  }
153
223
  else {
154
224
  this.deliverMessage(message);
225
+ this.safeDispatchEvent(MessageChannelEvent.MessageDelivered, {
226
+ detail: {
227
+ messageId: message.messageId,
228
+ sentOrReceived: "received"
229
+ }
230
+ });
155
231
  }
156
232
  }
157
233
  // https://rfc.vac.dev/vac/raw/sds/#periodic-incoming-buffer-sweep
234
+ // Note that even though this function has side effects, it is not async
235
+ // and does not need to be called through the queue.
158
236
  sweepIncomingBuffer() {
159
237
  const { buffer, missing } = this.incomingBuffer.reduce(({ buffer, missing }, message) => {
160
238
  // Check each message for missing dependencies
@@ -163,6 +241,12 @@ export class MessageChannel extends TypedEventEmitter {
163
241
  // Any message with no missing dependencies is delivered
164
242
  // and removed from the buffer (implicitly by not adding it to the new incoming buffer)
165
243
  this.deliverMessage(message);
244
+ this.safeDispatchEvent(MessageChannelEvent.MessageDelivered, {
245
+ detail: {
246
+ messageId: message.messageId,
247
+ sentOrReceived: "received"
248
+ }
249
+ });
166
250
  return { buffer, missing };
167
251
  }
168
252
  // Optionally, if a message has not been received after a predetermined amount of time,
@@ -176,14 +260,20 @@ export class MessageChannel extends TypedEventEmitter {
176
260
  }
177
261
  // Any message with missing dependencies stays in the buffer
178
262
  // and the missing message IDs are returned for processing.
263
+ missingDependencies.forEach((dependency) => {
264
+ missing.add(dependency);
265
+ });
179
266
  return {
180
267
  buffer: buffer.concat(message),
181
- missing: missing.concat(missingDependencies)
268
+ missing
182
269
  };
183
- }, { buffer: new Array(), missing: new Array() });
270
+ }, { buffer: new Array(), missing: new Set() });
184
271
  // Update the incoming buffer to only include messages with no missing dependencies
185
272
  this.incomingBuffer = buffer;
186
- return missing;
273
+ this.safeDispatchEvent(MessageChannelEvent.MissedMessages, {
274
+ detail: Array.from(missing)
275
+ });
276
+ return Array.from(missing);
187
277
  }
188
278
  // https://rfc.vac.dev/vac/raw/sds/#periodic-outgoing-buffer-sweep
189
279
  sweepOutgoingBuffer() {
@@ -214,7 +304,7 @@ export class MessageChannel extends TypedEventEmitter {
214
304
  *
215
305
  * @param callback - A callback function that returns a boolean indicating whether the message was sent successfully.
216
306
  */
217
- sendSyncMessage(callback) {
307
+ async sendSyncMessage(callback) {
218
308
  this.lamportTimestamp++;
219
309
  const emptyMessage = new Uint8Array();
220
310
  const message = {
@@ -228,13 +318,16 @@ export class MessageChannel extends TypedEventEmitter {
228
318
  content: emptyMessage
229
319
  };
230
320
  if (callback) {
231
- return callback(message);
321
+ await callback(message);
322
+ this.safeDispatchEvent(MessageChannelEvent.SyncSent, {
323
+ detail: message
324
+ });
325
+ return true;
232
326
  }
233
- return Promise.resolve(false);
327
+ return false;
234
328
  }
235
329
  // See https://rfc.vac.dev/vac/raw/sds/#deliver-message
236
330
  deliverMessage(message, retrievalHint) {
237
- this.notifyDeliveredMessage(message.messageId);
238
331
  const messageLamportTimestamp = message.lamportTimestamp ?? 0;
239
332
  if (messageLamportTimestamp > this.lamportTimestamp) {
240
333
  this.lamportTimestamp = messageLamportTimestamp;
@@ -271,7 +364,15 @@ export class MessageChannel extends TypedEventEmitter {
271
364
  reviewAckStatus(receivedMessage) {
272
365
  // the participant MUST mark all messages in the received causal_history as acknowledged.
273
366
  receivedMessage.causalHistory.forEach(({ messageId }) => {
274
- this.outgoingBuffer = this.outgoingBuffer.filter(({ messageId: outgoingMessageId }) => outgoingMessageId !== messageId);
367
+ this.outgoingBuffer = this.outgoingBuffer.filter(({ messageId: outgoingMessageId }) => {
368
+ if (outgoingMessageId !== messageId) {
369
+ return true;
370
+ }
371
+ this.safeDispatchEvent(MessageChannelEvent.MessageAcknowledged, {
372
+ detail: messageId
373
+ });
374
+ return false;
375
+ });
275
376
  this.acknowledgements.delete(messageId);
276
377
  if (!this.filter.lookup(messageId)) {
277
378
  this.filter.insert(messageId);
@@ -292,6 +393,12 @@ export class MessageChannel extends TypedEventEmitter {
292
393
  const count = (this.acknowledgements.get(message.messageId) ?? 0) + 1;
293
394
  if (count < this.acknowledgementCount) {
294
395
  this.acknowledgements.set(message.messageId, count);
396
+ this.safeDispatchEvent(MessageChannelEvent.PartialAcknowledgement, {
397
+ detail: {
398
+ messageId: message.messageId,
399
+ count
400
+ }
401
+ });
295
402
  return true;
296
403
  }
297
404
  this.acknowledgements.delete(message.messageId);
@@ -302,13 +409,5 @@ export class MessageChannel extends TypedEventEmitter {
302
409
  getAcknowledgementCount() {
303
410
  return 2;
304
411
  }
305
- notifyDeliveredMessage(messageId) {
306
- if (this.deliveredMessageCallback) {
307
- this.deliveredMessageCallback(messageId);
308
- }
309
- this.dispatchEvent(new CustomEvent(MessageChannelEvent.MessageDelivered, {
310
- detail: messageId
311
- }));
312
- }
313
412
  }
314
- //# sourceMappingURL=sds.js.map
413
+ //# sourceMappingURL=message_channel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"message_channel.js","sourceRoot":"","sources":["../../src/message_channel/message_channel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EA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iBACF,CAAC,CAAC;gBACH,OAAO,IAAI,CAAC;YACd,CAAC;YACD,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;YAChD,OAAO,KAAK,CAAC;QACf,CAAC,CAAC,CAAC;IACL,CAAC;IAED,4FAA4F;IACpF,uBAAuB;QAC7B,OAAO,CAAC,CAAC;IACX,CAAC;CACF"}
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@waku/sds","version":"0.0.4-10590da.0","description":"Scalable Data Sync implementation for the browser. Based on https://github.com/vacp2p/rfc-index/blob/main/vac/raw/sds.md","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js"}},"typesVersions":{"*":{"*":["*","dist/*","dist/*/index"]}},"type":"module","author":"Waku Team","homepage":"https://github.com/waku-org/js-waku/tree/master/packages/scalable-data-sync#readme","repository":{"type":"git","url":"https://github.com/waku-org/js-waku.git"},"bugs":{"url":"https://github.com/waku-org/js-waku/issues"},"license":"MIT OR Apache-2.0","keywords":["waku","decentralized","secure","communication","web3","ethereum","dapps","privacy"],"scripts":{"build":"run-s build:**","build:esm":"tsc","build:bundle":"rollup --config rollup.config.js","fix":"run-s fix:*","fix:lint":"eslint src *.js --fix","check":"run-s check:*","check:lint":"eslint src *.js","check:spelling":"cspell \"{README.md,src/**/*.ts}\"","check:tsc":"tsc -p tsconfig.dev.json","prepublish":"npm run build","reset-hard":"git clean -dfx -e .idea && git reset --hard && npm i && npm run build","test":"NODE_ENV=test run-s test:*","test:node":"NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha"},"engines":{"node":">=20"},"dependencies":{"@libp2p/interface":"2.7.0","@noble/hashes":"^1.7.1","@waku/message-hash":"0.1.20-10590da.0","@waku/proto":"0.0.11-10590da.0","@waku/utils":"0.0.24-10590da.0","chai":"^5.1.2"},"devDependencies":{"@rollup/plugin-commonjs":"^25.0.7","@rollup/plugin-json":"^6.0.0","@rollup/plugin-node-resolve":"^15.2.3","@waku/build-utils":"*","allure-commandline":"^2.27.0","allure-mocha":"^2.9.2","cspell":"^8.6.1","fast-check":"^3.19.0","mocha-multi-reporters":"^1.5.1","npm-run-all":"^4.1.5","rollup":"^4.12.0"},"files":["dist","bundle","src/**/*.ts","!**/*.spec.*","!**/*.json","CHANGELOG.md","LICENSE","README.md"]}
1
+ {"name":"@waku/sds","version":"0.0.4-383e0b2.0","description":"Scalable Data Sync implementation for the browser. Based on https://github.com/vacp2p/rfc-index/blob/main/vac/raw/sds.md","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js"}},"typesVersions":{"*":{"*":["*","dist/*","dist/*/index"]}},"type":"module","author":"Waku Team","homepage":"https://github.com/waku-org/js-waku/tree/master/packages/scalable-data-sync#readme","repository":{"type":"git","url":"https://github.com/waku-org/js-waku.git"},"bugs":{"url":"https://github.com/waku-org/js-waku/issues"},"license":"MIT OR Apache-2.0","keywords":["waku","decentralized","secure","communication","web3","ethereum","dapps","privacy"],"scripts":{"build":"run-s build:**","build:esm":"tsc","build:bundle":"rollup --config rollup.config.js","fix":"run-s fix:*","fix:lint":"eslint src *.js --fix","check":"run-s check:*","check:lint":"eslint src *.js","check:spelling":"cspell \"{README.md,src/**/*.ts}\"","check:tsc":"tsc -p tsconfig.dev.json","prepublish":"npm run build","reset-hard":"git clean -dfx -e .idea && git reset --hard && npm i && npm run build","test":"NODE_ENV=test run-s test:*","test:node":"NODE_ENV=test TS_NODE_PROJECT=./tsconfig.dev.json mocha"},"engines":{"node":">=20"},"dependencies":{"@libp2p/interface":"2.7.0","@noble/hashes":"^1.7.1","@waku/message-hash":"0.1.20-383e0b2.0","@waku/proto":"0.0.11-383e0b2.0","@waku/utils":"0.0.24-383e0b2.0","chai":"^5.1.2"},"devDependencies":{"@rollup/plugin-commonjs":"^25.0.7","@rollup/plugin-json":"^6.0.0","@rollup/plugin-node-resolve":"^15.2.3","@waku/build-utils":"*","allure-commandline":"^2.27.0","allure-mocha":"^2.9.2","cspell":"^8.6.1","fast-check":"^3.19.0","mocha-multi-reporters":"^1.5.1","npm-run-all":"^4.1.5","rollup":"^4.12.0"},"files":["dist","bundle","src/**/*.ts","!**/*.spec.*","!**/*.json","CHANGELOG.md","LICENSE","README.md"]}
@@ -1,5 +1,5 @@
1
- import { hashN } from "./nim_hashn/nim_hashn.mjs";
2
- import { getMOverNBitsForK } from "./probabilities.js";
1
+ import { hashN } from "../nim_hashn/nim_hashn.mjs";
2
+ import { getMOverNBitsForK } from "../probabilities.js";
3
3
 
4
4
  export interface BloomFilterOptions {
5
5
  // The expected maximum number of elements for which this BloomFilter is sized.
package/src/index.ts CHANGED
@@ -1,3 +1,5 @@
1
- import { BloomFilter } from "./bloom.js";
1
+ import { BloomFilter } from "./bloom_filter/bloom.js";
2
+
3
+ export * from "./message_channel/index.js";
2
4
 
3
5
  export { BloomFilter };
@@ -0,0 +1,34 @@
1
+ import { Message } from "./events.js";
2
+
3
+ export enum Command {
4
+ Send = "send",
5
+ Receive = "receive",
6
+ SendEphemeral = "sendEphemeral"
7
+ }
8
+
9
+ export interface ParamsByAction {
10
+ [Command.Send]: {
11
+ payload: Uint8Array;
12
+ callback?: (message: Message) => Promise<{
13
+ success: boolean;
14
+ retrievalHint?: Uint8Array;
15
+ }>;
16
+ };
17
+ [Command.Receive]: {
18
+ message: Message;
19
+ };
20
+ [Command.SendEphemeral]: {
21
+ payload: Uint8Array;
22
+ callback?: (message: Message) => Promise<boolean>;
23
+ };
24
+ }
25
+
26
+ export type Task<A extends Command = Command> = {
27
+ command: A;
28
+ params: ParamsByAction[A];
29
+ };
30
+
31
+ // Define a mapping for handlers based on action type
32
+ export type Handlers = {
33
+ [A in Command]: (params: ParamsByAction[A]) => Promise<void>;
34
+ };
@@ -0,0 +1,41 @@
1
+ import { proto_sds_message } from "@waku/proto";
2
+
3
+ export enum MessageChannelEvent {
4
+ MessageSent = "messageSent",
5
+ MessageDelivered = "messageDelivered",
6
+ MessageReceived = "messageReceived",
7
+ MessageAcknowledged = "messageAcknowledged",
8
+ PartialAcknowledgement = "partialAcknowledgement",
9
+ MissedMessages = "missedMessages",
10
+ SyncSent = "syncSent",
11
+ SyncReceived = "syncReceived"
12
+ }
13
+
14
+ export type Message = proto_sds_message.SdsMessage;
15
+ export type HistoryEntry = proto_sds_message.HistoryEntry;
16
+ export type ChannelId = string;
17
+
18
+ export function encodeMessage(message: Message): Uint8Array {
19
+ return proto_sds_message.SdsMessage.encode(message);
20
+ }
21
+
22
+ export function decodeMessage(data: Uint8Array): Message {
23
+ return proto_sds_message.SdsMessage.decode(data);
24
+ }
25
+
26
+ export type MessageChannelEvents = {
27
+ [MessageChannelEvent.MessageSent]: CustomEvent<Message>;
28
+ [MessageChannelEvent.MessageDelivered]: CustomEvent<{
29
+ messageId: string;
30
+ sentOrReceived: "sent" | "received";
31
+ }>;
32
+ [MessageChannelEvent.MessageReceived]: CustomEvent<Message>;
33
+ [MessageChannelEvent.MessageAcknowledged]: CustomEvent<string>;
34
+ [MessageChannelEvent.PartialAcknowledgement]: CustomEvent<{
35
+ messageId: string;
36
+ count: number;
37
+ }>;
38
+ [MessageChannelEvent.MissedMessages]: CustomEvent<HistoryEntry[]>;
39
+ [MessageChannelEvent.SyncSent]: CustomEvent<Message>;
40
+ [MessageChannelEvent.SyncReceived]: CustomEvent<Message>;
41
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./command_queue.js";
2
+ export * from "./events.js";
3
+ export * from "./message_channel.js";