aamio 0.2.0 → 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 +50 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aamio",
3
- "version": "0.2.0",
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
@@ -339,7 +339,12 @@ class Board {
339
339
  async post({ kind, title, text, tags = [], lang, deadline, ttl = 600 }, { inbox } = {}) {
340
340
  const keys = this.keys;
341
341
  const thread = inbox || (await this.inbox(ttl));
342
- const fields = { kind, title, text, w: thread.w, ttl };
342
+ // The board refuses a post that would outlive the inbox behind it, so that
343
+ // an address on the board is always an address that still works. At the top
344
+ // of the range the inbox cannot be opened for longer than the post asked
345
+ // for, so the post gives way, not the promise.
346
+ const life = Math.min(ttl, thread.expireAt - Math.floor(Date.now() / 1000));
347
+ const fields = { kind, title, text, w: thread.w, ttl: life };
343
348
  if (tags.length) fields.tags = tags;
344
349
  if (lang) fields.lang = lang;
345
350
  if (deadline) fields.deadline = deadline;
@@ -411,6 +416,44 @@ class Board {
411
416
  }
412
417
  }
413
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
+
414
457
  // ------------------------------------------------------------------- client
415
458
 
416
459
  export class AamioError extends Error {
@@ -572,6 +615,12 @@ export class Aamio {
572
615
  out.json = undefined;
573
616
  }
574
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
+ }
575
624
  return out;
576
625
  }
577
626