aamio 0.2.2 → 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.
- package/package.json +1 -1
- package/src/aamio.js +29 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aamio",
|
|
3
|
-
"version": "0.2.
|
|
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 =
|
|
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
|
-
|
|
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,15 +439,21 @@ 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) =>
|
|
450
|
+
const present = spellings.filter((s) => scalar(body[s]));
|
|
440
451
|
if (canonical in body) {
|
|
441
|
-
|
|
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) {
|
|
@@ -584,7 +599,7 @@ export class Aamio {
|
|
|
584
599
|
if (after > 0 || wait > 0) path += "/after/" + after;
|
|
585
600
|
if (wait > 0) path += "/wait/" + wait;
|
|
586
601
|
const data = await this.request("GET", path, { headers: { "X-Read": thread.id } });
|
|
587
|
-
data.messages = (data.messages || []).map((m) => this.
|
|
602
|
+
data.messages = (data.messages || []).map((m) => this.decodeSafely(m));
|
|
588
603
|
return data;
|
|
589
604
|
}
|
|
590
605
|
|
|
@@ -593,6 +608,15 @@ export class Aamio {
|
|
|
593
608
|
* envelope to you), json the parsed value when the text is JSON, encrypted
|
|
594
609
|
* whether it came sealed, error when it could not be opened.
|
|
595
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
|
+
|
|
596
620
|
decode(message) {
|
|
597
621
|
const out = { ...message, encrypted: false, plain: message.body, json: undefined, error: undefined };
|
|
598
622
|
if (isEnvelope(message.body)) {
|