@highway1/core 0.1.51 → 0.1.54

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.
Files changed (42) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.js +44 -10
  3. package/dist/index.js.map +1 -1
  4. package/package.json +5 -18
  5. package/src/discovery/agent-card-encoder.ts +0 -119
  6. package/src/discovery/agent-card-schema.ts +0 -87
  7. package/src/discovery/agent-card-types.ts +0 -99
  8. package/src/discovery/agent-card.ts +0 -190
  9. package/src/discovery/bootstrap.ts +0 -63
  10. package/src/discovery/capability-matcher.ts +0 -167
  11. package/src/discovery/dht.ts +0 -310
  12. package/src/discovery/index.ts +0 -3
  13. package/src/discovery/relay-index.ts +0 -98
  14. package/src/discovery/search-index.ts +0 -247
  15. package/src/discovery/semantic-search.ts +0 -218
  16. package/src/identity/did.ts +0 -48
  17. package/src/identity/document.ts +0 -77
  18. package/src/identity/index.ts +0 -4
  19. package/src/identity/keys.ts +0 -79
  20. package/src/identity/signer.ts +0 -55
  21. package/src/index.ts +0 -39
  22. package/src/messaging/codec.ts +0 -47
  23. package/src/messaging/defense.ts +0 -236
  24. package/src/messaging/envelope.ts +0 -107
  25. package/src/messaging/index.ts +0 -8
  26. package/src/messaging/queue.ts +0 -181
  27. package/src/messaging/rate-limiter.ts +0 -85
  28. package/src/messaging/router.ts +0 -209
  29. package/src/messaging/storage.ts +0 -281
  30. package/src/messaging/types.ts +0 -149
  31. package/src/transport/connection.ts +0 -77
  32. package/src/transport/index.ts +0 -2
  33. package/src/transport/node.ts +0 -154
  34. package/src/transport/relay-client.ts +0 -390
  35. package/src/transport/relay-types.ts +0 -195
  36. package/src/trust/endorsement.ts +0 -167
  37. package/src/trust/index.ts +0 -194
  38. package/src/trust/interaction-history.ts +0 -155
  39. package/src/trust/sybil-defense.ts +0 -232
  40. package/src/trust/trust-score.ts +0 -136
  41. package/src/utils/errors.ts +0 -38
  42. package/src/utils/logger.ts +0 -48
package/dist/index.d.ts CHANGED
@@ -274,6 +274,7 @@ interface CardMessage {
274
274
  }
275
275
  interface GoodbyeMessage {
276
276
  type: 'GOODBYE';
277
+ reconnectAfter?: number;
277
278
  }
278
279
  interface PresenceProof {
279
280
  did: string;
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import * as ed25519 from '@noble/ed25519';
2
2
  import { base58btc } from 'multiformats/bases/base58';
3
3
  import { WebSocket } from 'ws';
4
- import Ajv from 'ajv';
5
4
  import { encode, decode } from 'cbor-x';
5
+ import Ajv from 'ajv';
6
6
  import lunr from 'lunr';
7
7
  import Fuse from 'fuse.js';
8
8
  import { Level } from 'level';
@@ -207,6 +207,7 @@ function createRelayClient(config) {
207
207
  reconnectAttempt: 0,
208
208
  reconnectTimer: null,
209
209
  stableTimer: null,
210
+ pingTimer: null,
210
211
  peerCount: 0
211
212
  }));
212
213
  let deliveryHandler = null;
@@ -227,8 +228,8 @@ function createRelayClient(config) {
227
228
  logger.info("WebSocket connected", { url: conn.url });
228
229
  try {
229
230
  const timestamp = Date.now();
230
- const helloData = JSON.stringify({ did, card, timestamp });
231
- const signature = await sign(new TextEncoder().encode(helloData), keyPair.privateKey);
231
+ const helloData = encode({ did, card, timestamp });
232
+ const signature = await sign(helloData, keyPair.privateKey);
232
233
  const hello = {
233
234
  type: "HELLO",
234
235
  protocolVersion: 1,
@@ -237,7 +238,7 @@ function createRelayClient(config) {
237
238
  timestamp,
238
239
  signature
239
240
  };
240
- ws.send(JSON.stringify(hello));
241
+ ws.send(encode(hello));
241
242
  resolve();
242
243
  } catch (err) {
243
244
  reject(err);
@@ -250,7 +251,7 @@ function createRelayClient(config) {
250
251
  });
251
252
  ws.on("message", async (data) => {
252
253
  try {
253
- const msg = JSON.parse(data.toString());
254
+ const msg = decode(data);
254
255
  await handleRelayMessage(conn, msg);
255
256
  } catch (err) {
256
257
  logger.warn("Failed to parse relay message", { error: err.message });
@@ -264,6 +265,10 @@ function createRelayClient(config) {
264
265
  clearTimeout(conn.stableTimer);
265
266
  conn.stableTimer = null;
266
267
  }
268
+ if (conn.pingTimer) {
269
+ clearInterval(conn.pingTimer);
270
+ conn.pingTimer = null;
271
+ }
267
272
  if (!stopped) {
268
273
  scheduleReconnect(conn);
269
274
  }
@@ -306,6 +311,13 @@ function createRelayClient(config) {
306
311
  conn.reconnectAttempt = 0;
307
312
  logger.debug("Connection stable", { url: conn.url });
308
313
  }, reconnectConfig.stableAfterMs);
314
+ if (conn.pingTimer) clearInterval(conn.pingTimer);
315
+ conn.pingTimer = setInterval(() => {
316
+ if (conn.ws && conn.connected) {
317
+ conn.ws.send(encode({ type: "PING" }));
318
+ logger.debug("Sent PING", { url: conn.url });
319
+ }
320
+ }, 3e4);
309
321
  break;
310
322
  }
311
323
  case "DELIVER": {
@@ -314,6 +326,10 @@ function createRelayClient(config) {
314
326
  if (deliveryHandler) {
315
327
  await deliveryHandler(deliver);
316
328
  }
329
+ if (conn.ws && conn.connected) {
330
+ conn.ws.send(encode({ type: "ACK", messageId: deliver.messageId }));
331
+ logger.debug("Sent ACK", { messageId: deliver.messageId });
332
+ }
317
333
  break;
318
334
  }
319
335
  case "DELIVERY_REPORT": {
@@ -329,6 +345,20 @@ function createRelayClient(config) {
329
345
  logger.debug("Received PONG", { peers: msg.peers });
330
346
  break;
331
347
  }
348
+ case "GOODBYE": {
349
+ const goodbye = msg;
350
+ const reconnectAfter = goodbye.reconnectAfter || 5e3;
351
+ logger.info("Received GOODBYE", { url: conn.url, reconnectAfter });
352
+ if (conn.ws) {
353
+ conn.ws.close();
354
+ }
355
+ setTimeout(() => {
356
+ if (!stopped) {
357
+ connectToRelay(conn);
358
+ }
359
+ }, reconnectAfter);
360
+ break;
361
+ }
332
362
  default:
333
363
  logger.debug("Received relay message", { type: msg.type });
334
364
  }
@@ -341,7 +371,7 @@ function createRelayClient(config) {
341
371
  if (!conn || !conn.ws) {
342
372
  throw new TransportError("No connected relay");
343
373
  }
344
- conn.ws.send(JSON.stringify(msg));
374
+ conn.ws.send(encode(msg));
345
375
  }
346
376
  return {
347
377
  async start() {
@@ -376,6 +406,10 @@ function createRelayClient(config) {
376
406
  clearTimeout(conn.stableTimer);
377
407
  conn.stableTimer = null;
378
408
  }
409
+ if (conn.pingTimer) {
410
+ clearInterval(conn.pingTimer);
411
+ conn.pingTimer = null;
412
+ }
379
413
  if (conn.ws) {
380
414
  conn.ws.close();
381
415
  conn.ws = null;
@@ -405,7 +439,7 @@ function createRelayClient(config) {
405
439
  }, 1e4);
406
440
  const handler = (data) => {
407
441
  try {
408
- const msg = JSON.parse(data.toString());
442
+ const msg = decode(data);
409
443
  if (msg.type === "DISCOVERED") {
410
444
  clearTimeout(timeout);
411
445
  ws.off("message", handler);
@@ -415,7 +449,7 @@ function createRelayClient(config) {
415
449
  }
416
450
  };
417
451
  ws.on("message", handler);
418
- ws.send(JSON.stringify({ type: "DISCOVER", query, minTrust, limit }));
452
+ ws.send(encode({ type: "DISCOVER", query, minTrust, limit }));
419
453
  });
420
454
  },
421
455
  async fetchCard(did2) {
@@ -430,7 +464,7 @@ function createRelayClient(config) {
430
464
  }, 5e3);
431
465
  const handler = (data) => {
432
466
  try {
433
- const msg = JSON.parse(data.toString());
467
+ const msg = decode(data);
434
468
  if (msg.type === "CARD" && msg.did === did2) {
435
469
  clearTimeout(timeout);
436
470
  ws.off("message", handler);
@@ -440,7 +474,7 @@ function createRelayClient(config) {
440
474
  }
441
475
  };
442
476
  ws.on("message", handler);
443
- ws.send(JSON.stringify({ type: "FETCH_CARD", did: did2 }));
477
+ ws.send(encode({ type: "FETCH_CARD", did: did2 }));
444
478
  });
445
479
  },
446
480
  onDeliver(handler) {