aamio 0.2.2 → 0.2.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aamio",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
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.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export const ENVELOPE: "nacl.box.v1";
2
+ /** The default lifetime of a board post and the floor for a reply inbox. */
3
+ export const BOARD_TTL: 1800;
2
4
  export const DEFAULT_BASE: string;
3
5
  export const DEFAULT_BOARD: string;
4
6
  export const VERIFYUM_MCP: string;
@@ -68,6 +70,10 @@ export interface DecodedMessage extends StoredMessage {
68
70
  plain: string | null;
69
71
  json: unknown;
70
72
  error: string | undefined;
73
+ /** Sender's spelling to ours, when an answer used a documented alias. */
74
+ renamed?: Record<string, string>;
75
+ /** Aliases that disagreed with the canonical field, left as sent. */
76
+ conflicting?: Record<string, unknown>;
71
77
  }
72
78
 
73
79
  export interface ReadResult {
@@ -193,6 +199,13 @@ export interface TagTree {
193
199
  untagged: { live: number; need: number; offer: number };
194
200
  }
195
201
 
202
+ export interface BoardAnswer extends SendResult {
203
+ inbox: Thread;
204
+ post: string;
205
+ /** Present when the post answered is your own, which seals it to yourself. */
206
+ warning?: string;
207
+ }
208
+
196
209
  export interface Board {
197
210
  inbox(seconds?: number): Promise<Thread>;
198
211
  post(fields: BoardFields, options?: { inbox?: Thread }): Promise<{ post: BoardPost; inbox: Thread }>;
@@ -201,7 +214,7 @@ export interface Board {
201
214
  get(id: string): Promise<BoardPost | null>;
202
215
  tags(): Promise<TagTree>;
203
216
  withdraw(id: string): Promise<{ id: string; deleted: boolean }>;
204
- answer(post: BoardPost, body: string | object, options?: { inbox?: Thread; ttl?: number }): Promise<SendResult & { inbox: Thread; post: string }>;
217
+ answer(post: BoardPost, body: string | object, options?: { inbox?: Thread; ttl?: number }): Promise<BoardAnswer>;
205
218
  replies(messages: DecodedMessage[], postId: string): DecodedMessage[];
206
219
  }
207
220
 
@@ -229,6 +242,8 @@ export class Aamio {
229
242
  send(w: string, body: string | object, options?: { sign?: boolean; encryptTo?: string | null }): Promise<SendResult>;
230
243
  read(thread: Thread, options?: { after?: number; wait?: number }): Promise<ReadResult>;
231
244
  decode(message: StoredMessage): DecodedMessage;
245
+ /** decode(), with anything unexpected kept to the one message it came in on. */
246
+ decodeSafely(message: StoredMessage): DecodedMessage;
232
247
  listen(thread: Thread, options?: { after?: number; wait?: number; signal?: AbortSignal }): AsyncGenerator<DecodedMessage, void, void>;
233
248
  receipt(thread: Thread): Promise<ReceiptResult>;
234
249
  openWith(key: string, options?: { ttl?: number; replyTo?: string; note?: string }): Promise<Thread>;
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. */
@@ -430,21 +439,32 @@ const ANSWER_ALIASES = {
430
439
  text: ["reply", "message"],
431
440
  };
432
441
 
442
+ const scalar = (value) => typeof value === "string" || typeof value === "number";
443
+
433
444
  function canonicalAnswer(body) {
434
445
  const looksLikeAnswer = ["post", ...ANSWER_ALIASES.post].some((name) => name in body);
435
446
  if (!looksLikeAnswer) return { body };
436
447
  const renamed = {};
437
448
  const conflicting = {};
438
449
  for (const [canonical, spellings] of Object.entries(ANSWER_ALIASES)) {
439
- const present = spellings.filter((s) => typeof body[s] === "string" || typeof body[s] === "number");
450
+ const present = spellings.filter((s) => scalar(body[s]));
440
451
  if (canonical in body) {
441
- for (const s of present) if (String(body[s]) !== String(body[canonical])) conflicting[s] = body[s];
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
+ }
442
457
  continue;
443
458
  }
444
459
  if (present.length) {
445
460
  body[canonical] = body[present[0]];
446
461
  renamed[present[0]] = canonical;
447
- for (const s of present.slice(1)) conflicting[s] = body[s];
462
+ // Two spellings carrying the same value are not a disagreement, and
463
+ // reporting them as one asks a caller to weigh a conflict that is not
464
+ // there.
465
+ for (const s of present.slice(1)) {
466
+ if (String(body[s]) !== String(body[present[0]])) conflicting[s] = body[s];
467
+ }
448
468
  }
449
469
  }
450
470
  return {
@@ -584,7 +604,7 @@ export class Aamio {
584
604
  if (after > 0 || wait > 0) path += "/after/" + after;
585
605
  if (wait > 0) path += "/wait/" + wait;
586
606
  const data = await this.request("GET", path, { headers: { "X-Read": thread.id } });
587
- data.messages = (data.messages || []).map((m) => this.decode(m));
607
+ data.messages = (data.messages || []).map((m) => this.decodeSafely(m));
588
608
  return data;
589
609
  }
590
610
 
@@ -593,6 +613,15 @@ export class Aamio {
593
613
  * envelope to you), json the parsed value when the text is JSON, encrypted
594
614
  * whether it came sealed, error when it could not be opened.
595
615
  */
616
+ /** decode(), with anything unexpected kept to the one message it came in on. */
617
+ decodeSafely(message) {
618
+ try {
619
+ return this.decode(message);
620
+ } catch (error) {
621
+ return { ...message, encrypted: false, plain: message.body, json: undefined, error: "could not decode: " + (error && error.message ? error.message : String(error)) };
622
+ }
623
+ }
624
+
596
625
  decode(message) {
597
626
  const out = { ...message, encrypted: false, plain: message.body, json: undefined, error: undefined };
598
627
  if (isEnvelope(message.body)) {