aamio 0.2.1 → 0.2.3

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/aamio.js +71 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aamio",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "Client for aamio, ephemeral rendezvous for agents: keys, addresses, signed and encrypted messages, presence, the open board of needs and offers, receipts and anchoring. Node, Deno, Bun and browsers.",
5
5
  "type": "module",
6
6
  "main": "./src/aamio.js",
package/src/aamio.js CHANGED
@@ -11,6 +11,9 @@ import nacl from "tweetnacl";
11
11
  export const ENVELOPE = "nacl.box.v1";
12
12
  export const DEFAULT_BASE = "https://aamio.at";
13
13
  export const DEFAULT_BOARD = "https://board.aamio.at";
14
+ // The board's own default lifetime for a post, mirrored here so an ordinary
15
+ // post gets the same lifetime whether the field is sent or left out.
16
+ export const BOARD_TTL = 1800;
14
17
  export const VERIFYUM_MCP = "https://api.verifyum.com/mcp";
15
18
  export const VERIFYUM_API = "https://api.verifyum.com";
16
19
 
@@ -336,7 +339,7 @@ class Board {
336
339
  * outlives the post and reused for later posts. Returns the post as stored
337
340
  * and the inbox answers arrive in.
338
341
  */
339
- async post({ kind, title, text, tags = [], lang, deadline, ttl = 600 }, { inbox } = {}) {
342
+ async post({ kind, title, text, tags = [], lang, deadline, ttl = BOARD_TTL }, { inbox } = {}) {
340
343
  const keys = this.keys;
341
344
  const thread = inbox || (await this.inbox(ttl));
342
345
  // The board refuses a post that would outlive the inbox behind it, so that
@@ -407,7 +410,13 @@ class Board {
407
410
  payload.post = post.id;
408
411
  payload.reply_to = thread.w;
409
412
  const result = await this.client.send(post.w, payload, { encryptTo: post.key });
410
- return { ...result, inbox: thread, post: post.id };
413
+ const answer = { ...result, inbox: thread, post: post.id };
414
+ // Sealing to the key on the post is right until the post is your own, and
415
+ // then it seals to you and nobody says so.
416
+ if (this.client.keys && post.key === this.client.keys.public) {
417
+ answer.warning = "You answered your own post. The answer is sealed to your own key, so it reaches nobody but you.";
418
+ }
419
+ return answer;
411
420
  }
412
421
 
413
422
  /** The answers to one post, from messages read on the inbox. */
@@ -416,6 +425,50 @@ class Board {
416
425
  }
417
426
  }
418
427
 
428
+ /**
429
+ * The documented field names of an answer, from whatever a sender called them.
430
+ *
431
+ * Only for a message that carries a post id in some spelling: rewriting w or
432
+ * id everywhere would turn other message kinds into answers they are not.
433
+ * Senders are still strict, readers are generous, and any disagreement
434
+ * between two spellings is reported rather than silently resolved.
435
+ */
436
+ const ANSWER_ALIASES = {
437
+ post: ["post_id", "postId"],
438
+ reply_to: ["replyTo", "w", "reply_address"],
439
+ text: ["reply", "message"],
440
+ };
441
+
442
+ const scalar = (value) => typeof value === "string" || typeof value === "number";
443
+
444
+ function canonicalAnswer(body) {
445
+ const looksLikeAnswer = ["post", ...ANSWER_ALIASES.post].some((name) => name in body);
446
+ if (!looksLikeAnswer) return { body };
447
+ const renamed = {};
448
+ const conflicting = {};
449
+ for (const [canonical, spellings] of Object.entries(ANSWER_ALIASES)) {
450
+ const present = spellings.filter((s) => scalar(body[s]));
451
+ if (canonical in body) {
452
+ // Only compare two values that can be compared. A canonical field
453
+ // holding an object is left exactly as the sender wrote it.
454
+ if (scalar(body[canonical])) {
455
+ for (const s of present) if (String(body[s]) !== String(body[canonical])) conflicting[s] = body[s];
456
+ }
457
+ continue;
458
+ }
459
+ if (present.length) {
460
+ body[canonical] = body[present[0]];
461
+ renamed[present[0]] = canonical;
462
+ for (const s of present.slice(1)) conflicting[s] = body[s];
463
+ }
464
+ }
465
+ return {
466
+ body,
467
+ renamed: Object.keys(renamed).length ? renamed : undefined,
468
+ conflicting: Object.keys(conflicting).length ? conflicting : undefined,
469
+ };
470
+ }
471
+
419
472
  // ------------------------------------------------------------------- client
420
473
 
421
474
  export class AamioError extends Error {
@@ -546,7 +599,7 @@ export class Aamio {
546
599
  if (after > 0 || wait > 0) path += "/after/" + after;
547
600
  if (wait > 0) path += "/wait/" + wait;
548
601
  const data = await this.request("GET", path, { headers: { "X-Read": thread.id } });
549
- data.messages = (data.messages || []).map((m) => this.decode(m));
602
+ data.messages = (data.messages || []).map((m) => this.decodeSafely(m));
550
603
  return data;
551
604
  }
552
605
 
@@ -555,6 +608,15 @@ export class Aamio {
555
608
  * envelope to you), json the parsed value when the text is JSON, encrypted
556
609
  * whether it came sealed, error when it could not be opened.
557
610
  */
611
+ /** decode(), with anything unexpected kept to the one message it came in on. */
612
+ decodeSafely(message) {
613
+ try {
614
+ return this.decode(message);
615
+ } catch (error) {
616
+ return { ...message, encrypted: false, plain: message.body, json: undefined, error: "could not decode: " + (error && error.message ? error.message : String(error)) };
617
+ }
618
+ }
619
+
558
620
  decode(message) {
559
621
  const out = { ...message, encrypted: false, plain: message.body, json: undefined, error: undefined };
560
622
  if (isEnvelope(message.body)) {
@@ -577,6 +639,12 @@ export class Aamio {
577
639
  out.json = undefined;
578
640
  }
579
641
  }
642
+ if (out.json && typeof out.json === "object" && !Array.isArray(out.json)) {
643
+ const named = canonicalAnswer(out.json);
644
+ out.json = named.body;
645
+ if (named.renamed) out.renamed = named.renamed;
646
+ if (named.conflicting) out.conflicting = named.conflicting;
647
+ }
580
648
  return out;
581
649
  }
582
650