aamio 0.2.1 → 0.2.2

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 +44 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aamio",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
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
@@ -416,6 +416,44 @@ class Board {
416
416
  }
417
417
  }
418
418
 
419
+ /**
420
+ * The documented field names of an answer, from whatever a sender called them.
421
+ *
422
+ * Only for a message that carries a post id in some spelling: rewriting w or
423
+ * id everywhere would turn other message kinds into answers they are not.
424
+ * Senders are still strict, readers are generous, and any disagreement
425
+ * between two spellings is reported rather than silently resolved.
426
+ */
427
+ const ANSWER_ALIASES = {
428
+ post: ["post_id", "postId"],
429
+ reply_to: ["replyTo", "w", "reply_address"],
430
+ text: ["reply", "message"],
431
+ };
432
+
433
+ function canonicalAnswer(body) {
434
+ const looksLikeAnswer = ["post", ...ANSWER_ALIASES.post].some((name) => name in body);
435
+ if (!looksLikeAnswer) return { body };
436
+ const renamed = {};
437
+ const conflicting = {};
438
+ for (const [canonical, spellings] of Object.entries(ANSWER_ALIASES)) {
439
+ const present = spellings.filter((s) => typeof body[s] === "string" || typeof body[s] === "number");
440
+ if (canonical in body) {
441
+ for (const s of present) if (String(body[s]) !== String(body[canonical])) conflicting[s] = body[s];
442
+ continue;
443
+ }
444
+ if (present.length) {
445
+ body[canonical] = body[present[0]];
446
+ renamed[present[0]] = canonical;
447
+ for (const s of present.slice(1)) conflicting[s] = body[s];
448
+ }
449
+ }
450
+ return {
451
+ body,
452
+ renamed: Object.keys(renamed).length ? renamed : undefined,
453
+ conflicting: Object.keys(conflicting).length ? conflicting : undefined,
454
+ };
455
+ }
456
+
419
457
  // ------------------------------------------------------------------- client
420
458
 
421
459
  export class AamioError extends Error {
@@ -577,6 +615,12 @@ export class Aamio {
577
615
  out.json = undefined;
578
616
  }
579
617
  }
618
+ if (out.json && typeof out.json === "object" && !Array.isArray(out.json)) {
619
+ const named = canonicalAnswer(out.json);
620
+ out.json = named.body;
621
+ if (named.renamed) out.renamed = named.renamed;
622
+ if (named.conflicting) out.conflicting = named.conflicting;
623
+ }
580
624
  return out;
581
625
  }
582
626