@openmuster/muster 0.3.1-alpha.5 → 0.3.1-alpha.7
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/README.md +2 -2
- package/dist/{args-CaFBa2IP.js → args-C_XaUVTS.js} +456 -65
- package/dist/cli.js +3 -3
- package/dist/index.d.ts +231 -85
- package/dist/index.js +156 -10
- package/dist/main-C-3gchLr.js +6 -0
- package/dist/{main-BeEB08iI.js → main-D8R2IP1C.js} +1080 -119
- package/dist/mcp-uFonN-GE.js +6 -0
- package/dist/{module-BX2IjSiv.js → module-D6Kgl3Do.js} +2 -2
- package/dist/{module-D9KdbEpS.js → module-DkA_1NkG.js} +1 -1
- package/dist/skills/loop-card/SKILL.md +22 -3
- package/dist/{steps-FNDWY1P_.js → steps-DVxqtI61.js} +405 -9
- package/docs/01-concepts/06-actors.md +3 -1
- package/docs/01-concepts/08-tasks.md +3 -0
- package/docs/01-concepts/10-schedules.md +9 -1
- package/docs/01-concepts/11-access.md +86 -2
- package/docs/01-concepts/12-files-and-data.md +139 -6
- package/docs/01-concepts/16-workflows.md +20 -0
- package/docs/01-concepts/17-board-module.md +3 -1
- package/docs/01-quickstart.md +1 -1
- package/docs/02-sync/04-slack.md +5 -0
- package/docs/03-guides/06-sandboxed-agents.md +47 -0
- package/docs/03-guides/17-mcp.md +44 -7
- package/docs/03-guides/20-a-feedback-loop.md +114 -1
- package/docs/03-guides/22-building-from-linear.md +19 -6
- package/docs/04-reference/01-cli.md +10 -1
- package/docs/04-reference/02-handle.md +1 -0
- package/docs/04-reference/03-http.md +56 -5
- package/docs/04-reference/05-errors.md +8 -1
- package/docs/04-reference/06-conformance.md +17 -5
- package/package.json +2 -2
- package/dist/main-CLzQkYeG.js +0 -6
- package/dist/mcp-CAAZRJgv.js +0 -6
|
@@ -79,6 +79,29 @@ const SEGMENT = /^[a-z0-9][a-z0-9_.-]*$/;
|
|
|
79
79
|
const LABEL_KEY = /^[a-z0-9][a-z0-9_.-]{0,63}$/;
|
|
80
80
|
const PROFILE = /^[a-z0-9][a-z0-9_.-]{0,63}$/;
|
|
81
81
|
const CONTROL$1 = /[\u0000-\u001f\u007f]/;
|
|
82
|
+
/**
|
|
83
|
+
* Characters that change how a name reads without showing: invisible formatting (bidi overrides, zero-width),
|
|
84
|
+
* and line and paragraph separators. This and the rules in `fileNameProblem` follow `fileNameProblem` in
|
|
85
|
+
* @vercel/board's `contract/files.ts` (Apache-2.0, Copyright Vercel, Inc.).
|
|
86
|
+
*/
|
|
87
|
+
const INVISIBLE = /[\p{Cf}\u2028\u2029]/gu;
|
|
88
|
+
/** A file's name as the board keeps it: its last path segment, with no control characters, trimmed. */
|
|
89
|
+
function fileName(name) {
|
|
90
|
+
return name.split(/[\\/]/).pop().replace(/[\u0000-\u001f\u007f]/g, "").trim();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Why a message can't carry a file under `name`, or undefined. The sender chooses a name and whoever downloads
|
|
94
|
+
* it saves it by that name, so it can't be a dotfile (`.npmrc`, `.envrc`: config something reads) or show other
|
|
95
|
+
* than it reads (`report\u202Efdp.exe` shows as `reportexe.pdf`).
|
|
96
|
+
*/
|
|
97
|
+
function fileNameProblem(name) {
|
|
98
|
+
if (name.length === 0 || name.length > 255) return "A file needs a name of at most 255 characters.";
|
|
99
|
+
if (name.startsWith(".")) return `A file's name can't start with a dot: ${shown$1(name)}.`;
|
|
100
|
+
if (fileName(name) !== name) return `A file's name is one name, with no path, control characters or spaces around it: ${shown$1(name)}.`;
|
|
101
|
+
if (new RegExp(INVISIBLE.source, "u").test(name)) return `A file's name can't hold invisible formatting characters: ${shown$1(name)}.`;
|
|
102
|
+
}
|
|
103
|
+
/** A name quoted for a message, with anything outside printable ASCII escaped, so a refusal can't show it disguised. */
|
|
104
|
+
const shown$1 = (name) => JSON.stringify(name).replace(/[^\x20-\x7e]/g, (char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, "0")}`);
|
|
82
105
|
/** A channel path as the board keeps it: `db/storage`. A leading `#` is how people write one. */
|
|
83
106
|
function channelPath(input) {
|
|
84
107
|
const path = input.startsWith("#") ? input.slice(1) : input;
|
|
@@ -561,6 +584,36 @@ function once(ctx, caller, key, request$1, run, recall) {
|
|
|
561
584
|
});
|
|
562
585
|
}
|
|
563
586
|
|
|
587
|
+
//#endregion
|
|
588
|
+
//#region src/store/files.ts
|
|
589
|
+
/** The folder beside the board's file, which a board uses when given no store: name it to move files back from another. */
|
|
590
|
+
const DISK = "disk";
|
|
591
|
+
/** A store in this process's memory, for tests: gone with the process. */
|
|
592
|
+
function memoryFiles(options = {}) {
|
|
593
|
+
const held$1 = /* @__PURE__ */ new Map();
|
|
594
|
+
return {
|
|
595
|
+
name: options.name ?? "memory",
|
|
596
|
+
...options.replaces ? { replaces: options.replaces } : {},
|
|
597
|
+
put: async (id$1, bytes) => void held$1.set(id$1, bytes.slice()),
|
|
598
|
+
get: async (id$1) => held$1.get(id$1)?.slice() ?? null
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Moves a board's files back to the folder beside its file from the store named `from`, once you have copied
|
|
603
|
+
* them there: the board forgets the store, and needs none from then on.
|
|
604
|
+
*/
|
|
605
|
+
function backToDisk(from) {
|
|
606
|
+
const unused = async () => {
|
|
607
|
+
throw new Error("backToDisk is not a store: the board uses its folder.");
|
|
608
|
+
};
|
|
609
|
+
return {
|
|
610
|
+
name: DISK,
|
|
611
|
+
replaces: from,
|
|
612
|
+
put: unused,
|
|
613
|
+
get: unused
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
|
|
564
617
|
//#endregion
|
|
565
618
|
//#region src/core/files.ts
|
|
566
619
|
/** The largest file a board takes in one request. */
|
|
@@ -591,21 +644,122 @@ function spend(ctx, caller, bytes) {
|
|
|
591
644
|
}
|
|
592
645
|
/** Stores a file and returns the record a message carries. The same bytes stored twice are one file. */
|
|
593
646
|
function filePut(ctx, caller, request$1) {
|
|
594
|
-
|
|
647
|
+
const { record, bytes } = fileCheck(ctx, caller, request$1);
|
|
648
|
+
return fileKept(ctx, caller, record, bytes, null);
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* Everything that may refuse a file, before its bytes go anywhere: a store over a network is never sent what
|
|
652
|
+
* the board would refuse. Spends the caller's allowance.
|
|
653
|
+
*/
|
|
654
|
+
function fileCheck(ctx, caller, request$1) {
|
|
655
|
+
if (typeof request$1.name !== "string") throw misuse("invalid_file", "A file needs a name of at most 255 characters.");
|
|
656
|
+
const name = fileName(request$1.name);
|
|
657
|
+
const problem = fileNameProblem(name);
|
|
658
|
+
if (problem) throw misuse("invalid_file", problem, "Rename it: env.example, not .env.example.");
|
|
595
659
|
if (caller.can && !Object.values(caller.can).some((permissions) => permissions.includes("post") || permissions.includes("manage"))) throw new Failure("forbidden", "This token can't post, so it can't store a file.");
|
|
596
660
|
const bytes = Buffer.from(request$1.data, "base64");
|
|
597
|
-
if (bytes.length > FILE_MAX) throw new Failure("file_too_large", `${
|
|
661
|
+
if (bytes.length > FILE_MAX) throw new Failure("file_too_large", `${name} is over 25 MB.`, { hint: "Compress it, or link to it." });
|
|
598
662
|
if (caller.can) spend(ctx, caller, bytes.length);
|
|
599
663
|
return {
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
664
|
+
record: {
|
|
665
|
+
id: fileId(bytes),
|
|
666
|
+
name,
|
|
667
|
+
type: request$1.type ?? "application/octet-stream",
|
|
668
|
+
size: bytes.length
|
|
669
|
+
},
|
|
670
|
+
bytes
|
|
604
671
|
};
|
|
605
672
|
}
|
|
673
|
+
/**
|
|
674
|
+
* Records a checked file as the board's: its bytes go to the folder beside the board, or, with `store`, were put
|
|
675
|
+
* there already and are noted as held. Either way, who stored it is noted (see `putter`).
|
|
676
|
+
*/
|
|
677
|
+
function fileKept(ctx, caller, record, bytes, store) {
|
|
678
|
+
const token = putter(caller);
|
|
679
|
+
if (store === null && token === void 0) {
|
|
680
|
+
storeBytes(ctx, bytes);
|
|
681
|
+
return record;
|
|
682
|
+
}
|
|
683
|
+
ctx.db.write(() => {
|
|
684
|
+
if (store === null) storeBytes(ctx, bytes);
|
|
685
|
+
else noteStored(ctx, record.id);
|
|
686
|
+
if (token !== void 0) {
|
|
687
|
+
ensurePuts(ctx);
|
|
688
|
+
ctx.db.raw.prepare("insert or ignore into file_puts (id, jti) values (?, ?)").run(record.id, token);
|
|
689
|
+
}
|
|
690
|
+
});
|
|
691
|
+
return record;
|
|
692
|
+
}
|
|
693
|
+
/** A file's ID: the sha256 of its bytes, in hex. */
|
|
694
|
+
const fileId = (bytes) => createHash("sha256").update(bytes).digest("hex");
|
|
695
|
+
/**
|
|
696
|
+
* Which files a store holds, so a message's write can ask, with no network, whether a file is there. Bookkeeping,
|
|
697
|
+
* like `file_puts`: losing it loses nothing, as a file on any message is still known (see `fileKnown`).
|
|
698
|
+
*/
|
|
699
|
+
const ensureStored = (ctx) => ctx.db.raw.exec("create table if not exists stored_files (id text primary key) without rowid");
|
|
700
|
+
/** Notes a file a store now holds. Inside a write. */
|
|
701
|
+
function noteStored(ctx, id$1) {
|
|
702
|
+
ensureStored(ctx);
|
|
703
|
+
ctx.db.raw.prepare("insert or ignore into stored_files (id) values (?)").run(id$1);
|
|
704
|
+
}
|
|
705
|
+
/** The board's record of where its files are: one row, or none for the folder beside it. */
|
|
706
|
+
const ensureKept = (ctx) => ctx.db.raw.exec("create table if not exists file_store (only integer primary key check (only = 1), name text not null)");
|
|
707
|
+
/** Where the board says its files are, without changing anything: what a write in progress may ask. */
|
|
708
|
+
function keptStore(ctx) {
|
|
709
|
+
ensureKept(ctx);
|
|
710
|
+
return ctx.db.raw.prepare("select name from file_store where only = 1").get()?.name ?? DISK;
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* The store this process should use for the board's files, or null for the folder beside it. The first store
|
|
714
|
+
* a board is given is the one it keeps; another, or none, is refused (`files_elsewhere`), so a process never
|
|
715
|
+
* looks in the wrong place, until one names the kept store as the one it `replaces`. Not inside a write.
|
|
716
|
+
*/
|
|
717
|
+
function storeOf(ctx) {
|
|
718
|
+
const given = ctx.files;
|
|
719
|
+
const wanted = given?.name ?? DISK;
|
|
720
|
+
const kept$1 = keptStore(ctx);
|
|
721
|
+
if (kept$1 === wanted) return wanted === DISK ? null : given;
|
|
722
|
+
if (given && (kept$1 === DISK || given.replaces === kept$1)) {
|
|
723
|
+
ctx.db.write(() => {
|
|
724
|
+
if (wanted === DISK) ctx.db.raw.prepare("delete from file_store").run();
|
|
725
|
+
else ctx.db.raw.prepare("insert into file_store (only, name) values (1, ?) on conflict (only) do update set name = excluded.name").run(wanted);
|
|
726
|
+
});
|
|
727
|
+
return wanted === DISK ? null : given;
|
|
728
|
+
}
|
|
729
|
+
throw Object.assign(new Failure("files_elsewhere", given ? "This board keeps its files in another store than the one this process was given." : "This board keeps its files in a store this process wasn't given.", { hint: given ? "If you copied them across, give the new store `replaces` with the old one's name; whoever runs the board can see both in its output." : "Use the board through the server that has the store (MUSTER_URL), or open it with the module that names it." }), { cause: /* @__PURE__ */ new Error(`the board keeps its files in ${kept$1}; this process was given ${wanted}`) });
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* A stored file's bytes, for the board's own use and for `fileGet` once it has said who may read them: from
|
|
733
|
+
* the folder beside the board first, which holds what was stored before a store, then from the store. Null
|
|
734
|
+
* when the board has none. Throws when the store can't be reached, or what it gave isn't the file.
|
|
735
|
+
*/
|
|
736
|
+
async function readBytes(ctx, id$1) {
|
|
737
|
+
const here = fileBytes(ctx, id$1);
|
|
738
|
+
if (here) return here;
|
|
739
|
+
const store = storeOf(ctx);
|
|
740
|
+
if (store === null || !/^[0-9a-f]{64}$/.test(id$1)) return null;
|
|
741
|
+
let got;
|
|
742
|
+
try {
|
|
743
|
+
got = await store.get(id$1);
|
|
744
|
+
} catch (error) {
|
|
745
|
+
throw Object.assign(new Failure("files_unreachable", "The board couldn't reach where it keeps its files.", { hint: "Try again. Whoever runs the board can see why in its output." }), { cause: /* @__PURE__ */ new Error(`${store.name}: ${error instanceof Error ? error.message : String(error)}`) });
|
|
746
|
+
}
|
|
747
|
+
if (got === null) return null;
|
|
748
|
+
const bytes = Buffer.from(got);
|
|
749
|
+
if (fileId(bytes) !== id$1) throw Object.assign(new Failure("file_corrupt", "The board's file store gave back other bytes for that file.", { hint: "Whoever runs the board can see which in its output." }), { cause: /* @__PURE__ */ new Error(`${store.name} gave back bytes that are not file ${id$1}`) });
|
|
750
|
+
return bytes;
|
|
751
|
+
}
|
|
752
|
+
/**
|
|
753
|
+
* Which token stored which bytes, so the token that uploaded a file may attach it before any message has it.
|
|
754
|
+
* Bookkeeping, like `sync_files`: not a board fact, so not an event. The log says what happened on the board,
|
|
755
|
+
* and an upload happens to no channel and is seen by no one until a message has it; the `message.posted`
|
|
756
|
+
* event is the fact. Losing this table only stops a token from attaching what it stored and has not yet
|
|
757
|
+
* attached, so a fold of the log needs none of it. Made here, for a board made before it existed.
|
|
758
|
+
*/
|
|
759
|
+
const ensurePuts = (ctx) => ctx.db.raw.exec("create table if not exists file_puts (id text not null, jti text not null, primary key (id, jti)) without rowid");
|
|
606
760
|
/** Stores bytes by their hash and returns it. Callers check size and permission first. */
|
|
607
761
|
function storeBytes(ctx, bytes) {
|
|
608
|
-
const id$1 =
|
|
762
|
+
const id$1 = fileId(bytes);
|
|
609
763
|
const dir = filesDir(ctx);
|
|
610
764
|
if (dir === null) held(ctx).set(id$1, bytes);
|
|
611
765
|
else {
|
|
@@ -629,31 +783,72 @@ function fileBytes(ctx, id$1) {
|
|
|
629
783
|
}
|
|
630
784
|
}
|
|
631
785
|
/** A file, for a caller who can read the message that carries it. A file's ID alone opens nothing. */
|
|
632
|
-
function fileGet(ctx, caller, request$1) {
|
|
633
|
-
const record =
|
|
634
|
-
|
|
635
|
-
const bytes = fileBytes(ctx, request$1.id);
|
|
786
|
+
async function fileGet(ctx, caller, request$1) {
|
|
787
|
+
const record = fileFind(ctx, caller, request$1);
|
|
788
|
+
const bytes = await readBytes(ctx, request$1.id);
|
|
636
789
|
if (!bytes) throw new Failure("not_found", `The board no longer has ${record.name}.`);
|
|
637
790
|
return {
|
|
638
791
|
...record,
|
|
639
792
|
data: bytes.toString("base64")
|
|
640
793
|
};
|
|
641
794
|
}
|
|
642
|
-
/**
|
|
795
|
+
/** The record of a file on a message the caller can read: asked before any bytes are looked for. */
|
|
796
|
+
function fileFind(ctx, caller, request$1) {
|
|
797
|
+
const record = messageFor(ctx, caller, request$1.message).attachments.find((file$1) => file$1.id === request$1.id);
|
|
798
|
+
if (!record || !/^[0-9a-f]{64}$/.test(request$1.id)) throw new Failure("not_found", `Message ${request$1.message} carries no such file.`);
|
|
799
|
+
return record;
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Whether the board has a file, asked inside a message's write, with no network: in the folder beside it, or,
|
|
803
|
+
* with a store, noted as stored there or on any message, which a store never loses a file from.
|
|
804
|
+
*/
|
|
643
805
|
function fileKnown(ctx, id$1) {
|
|
806
|
+
if (!/^[0-9a-f]{64}$/.test(id$1)) return false;
|
|
644
807
|
const dir = filesDir(ctx);
|
|
645
|
-
|
|
808
|
+
if (dir === null ? held(ctx).has(id$1) : existsSync(join(dir, id$1))) return true;
|
|
809
|
+
if (keptStore(ctx) === DISK && (ctx.files === void 0 || ctx.files.name === DISK)) return false;
|
|
810
|
+
ensureStored(ctx);
|
|
811
|
+
if (ctx.db.raw.prepare("select 1 from stored_files where id = ?").get(id$1)) return true;
|
|
812
|
+
return ctx.db.raw.prepare("select 1 from messages m, json_each(m.attachments) a where instr(m.attachments, ?) > 0 and json_extract(a.value, '$.id') = ? limit 1").get(id$1, id$1) !== void 0;
|
|
646
813
|
}
|
|
814
|
+
/**
|
|
815
|
+
* Whether `caller` may put a file on a message by its ID. The board's owner may attach any file the board has.
|
|
816
|
+
* A caller held to channels may attach only bytes it stored itself, or a file on a message it may read
|
|
817
|
+
* that isn't deleted: the same messages `fileGet` would give it the file through. Otherwise an ID, which is
|
|
818
|
+
* only the hash of the bytes, would open any file to a token that knew or guessed it.
|
|
819
|
+
*/
|
|
820
|
+
function fileAttachable(ctx, caller, id$1) {
|
|
821
|
+
if (!caller.can) return fileKnown(ctx, id$1);
|
|
822
|
+
return (storedBy(ctx, putter(caller), id$1) || onReadable(ctx, caller, id$1)) && fileKnown(ctx, id$1);
|
|
823
|
+
}
|
|
824
|
+
/**
|
|
825
|
+
* Whose upload a file is, in `file_puts`: the caller's token, or, for someone who signed in through the module's
|
|
826
|
+
* `authenticate` and holds none, who they are (`sso:<id>`). A token's ID is base64url, with no ":", so the two never meet.
|
|
827
|
+
*/
|
|
828
|
+
const putter = (caller) => caller.token ?? (caller.principal.startsWith("sso:") ? caller.principal : void 0);
|
|
829
|
+
const storedBy = (ctx, token, id$1) => {
|
|
830
|
+
if (token === void 0) return false;
|
|
831
|
+
ensurePuts(ctx);
|
|
832
|
+
return ctx.db.raw.prepare("select 1 from file_puts where id = ? and jti = ?").get(id$1, token) !== void 0;
|
|
833
|
+
};
|
|
834
|
+
const onReadable = (ctx, caller, id$1) => {
|
|
835
|
+
return ctx.db.raw.prepare("select distinct m.channel from messages m, json_each(m.attachments) a where m.deleted = 0 and instr(m.attachments, ?) > 0 and json_extract(a.value, '$.id') = ?").all(id$1, id$1).some((row$1) => allowed(caller, row$1.channel, "read"));
|
|
836
|
+
};
|
|
647
837
|
|
|
648
838
|
//#endregion
|
|
649
839
|
//#region src/core/messages.ts
|
|
650
840
|
const MENTION = /(^|[\s(])@([a-z0-9][a-z0-9_.-]*)/g;
|
|
651
841
|
const SETTLED = /@\[([^\]\s]+)\]/g;
|
|
652
|
-
/**
|
|
842
|
+
/**
|
|
843
|
+
* Stores each `@handle` that names exactly one known actor as `@[actor-id]`, and says who was mentioned. A
|
|
844
|
+
* profile by that name comes before a principal whose last part is that name.
|
|
845
|
+
*/
|
|
653
846
|
function settleMentions(ctx, text) {
|
|
654
847
|
const mentions = /* @__PURE__ */ new Set();
|
|
655
848
|
const settled = text.replace(MENTION, (whole, lead, handle) => {
|
|
656
|
-
const
|
|
849
|
+
const all = ctx.db.raw.prepare("select principal, profile from actors where handle = ?").all(handle);
|
|
850
|
+
const named$1 = all.filter((row$1) => row$1.profile !== "");
|
|
851
|
+
const rows = named$1.length ? named$1 : all;
|
|
657
852
|
if (rows.length !== 1) return whole;
|
|
658
853
|
const id$1 = actorId({
|
|
659
854
|
principal: rows[0].principal,
|
|
@@ -674,8 +869,12 @@ function cleanLabels(labels) {
|
|
|
674
869
|
if (Object.keys(out).length > LIMITS.labels) throw misuse("too_many_labels", `A message carries at most ${LIMITS.labels} labels.`);
|
|
675
870
|
return out;
|
|
676
871
|
}
|
|
677
|
-
|
|
678
|
-
|
|
872
|
+
/**
|
|
873
|
+
* Each file must be one the caller may attach (see `fileAttachable`). A file it may not attach is refused in
|
|
874
|
+
* the same words as one the board doesn't have, so a refusal never says whether the board holds some bytes.
|
|
875
|
+
*/
|
|
876
|
+
function knownFiles(ctx, caller, attachments) {
|
|
877
|
+
for (const file$1 of attachments) if (!fileAttachable(ctx, caller, file$1.id)) throw new Failure("unknown_file", `The board has no file ${file$1.name}.`, { hint: "Attach it with --attach, which stores it first." });
|
|
679
878
|
}
|
|
680
879
|
function cleanAttachments(attachments) {
|
|
681
880
|
const list$3 = attachments ?? [];
|
|
@@ -688,6 +887,8 @@ function cleanAttachments(attachments) {
|
|
|
688
887
|
size: Number(size)
|
|
689
888
|
};
|
|
690
889
|
if (file$1.name.length > 255 || file$1.type.length > 255) throw misuse("invalid_file", "A file's name and type are at most 255 characters each.");
|
|
890
|
+
const problem = fileNameProblem(file$1.name);
|
|
891
|
+
if (problem) throw misuse("invalid_file", problem, "Rename it: env.example, not .env.example.");
|
|
691
892
|
return file$1;
|
|
692
893
|
});
|
|
693
894
|
}
|
|
@@ -709,7 +910,7 @@ function post(ctx, caller, request$1) {
|
|
|
709
910
|
channel
|
|
710
911
|
}), (emit) => {
|
|
711
912
|
checkBlocks(ctx, text);
|
|
712
|
-
knownFiles(ctx, attachments);
|
|
913
|
+
knownFiles(ctx, caller, attachments);
|
|
713
914
|
const settled = settleMentions(ctx, text);
|
|
714
915
|
const event = emit({
|
|
715
916
|
type: "message.posted",
|
|
@@ -760,7 +961,7 @@ function reply(ctx, caller, request$1) {
|
|
|
760
961
|
});
|
|
761
962
|
}
|
|
762
963
|
checkBlocks(ctx, text);
|
|
763
|
-
knownFiles(ctx, attachments);
|
|
964
|
+
knownFiles(ctx, caller, attachments);
|
|
764
965
|
const settled = settleMentions(ctx, text);
|
|
765
966
|
const event = emit({
|
|
766
967
|
type: "message.posted",
|
|
@@ -992,7 +1193,6 @@ const STALE_BACKSTOP_S = 3600;
|
|
|
992
1193
|
/** How many claims may end without a result before the board holds the task. */
|
|
993
1194
|
const MAX_ATTEMPTS = 2;
|
|
994
1195
|
const NEEDS_MAX = 20;
|
|
995
|
-
const NEEDS_WALK_MAX = 500;
|
|
996
1196
|
const FROM_MAX = 32;
|
|
997
1197
|
const PLAN_MAX = 100;
|
|
998
1198
|
/** How long a result or a reason may be: what `complete`, `fail`, `block` and `cancel` take. */
|
|
@@ -1191,16 +1391,7 @@ function needsOf(ctx, caller, self, ids) {
|
|
|
1191
1391
|
if (row$1.state === "canceled") throw new Failure("invalid_needs", `Task ${id$1} was canceled, so nothing can wait for it.`);
|
|
1192
1392
|
}
|
|
1193
1393
|
if (self !== null) {
|
|
1194
|
-
|
|
1195
|
-
const queue = [...needs];
|
|
1196
|
-
while (queue.length) {
|
|
1197
|
-
const id$1 = queue.pop();
|
|
1198
|
-
if (id$1 === self) throw new Failure("invalid_needs", `That would make task ${self} wait for itself.`);
|
|
1199
|
-
if (seen.has(id$1)) continue;
|
|
1200
|
-
seen.add(id$1);
|
|
1201
|
-
if (seen.size > NEEDS_WALK_MAX) throw new Failure("invalid_needs", "Those tasks wait for too many others to check.");
|
|
1202
|
-
for (const next of ctx.db.raw.prepare("select needs from needs where task = ?").all(id$1)) queue.push(next.needs);
|
|
1203
|
-
}
|
|
1394
|
+
if (ctx.db.raw.prepare("with recursive reach(id) as (select value from json_each(?) union select n.needs from needs n join reach r on n.task = r.id) select 1 as hit from reach where id = ? limit 1").get(JSON.stringify(needs), self)) throw new Failure("invalid_needs", `That would make task ${self} wait for itself.`);
|
|
1204
1395
|
}
|
|
1205
1396
|
return needs;
|
|
1206
1397
|
}
|
|
@@ -2536,6 +2727,8 @@ function report(ctx, caller, request$1) {
|
|
|
2536
2727
|
//#endregion
|
|
2537
2728
|
//#region src/core/schedules.ts
|
|
2538
2729
|
const EVERY_MIN_S = 60;
|
|
2730
|
+
/** How far out a schedule reaches: its period, its first send and its stop. A year, and a day for a leap year. */
|
|
2731
|
+
const AHEAD_MAX_S = 366 * 86400;
|
|
2539
2732
|
/** How late a slot may be and still be sent: half the period, between two minutes and two hours. */
|
|
2540
2733
|
const graceMs = (everyS) => Math.min(Math.max((everyS ?? 240) * 500, 12e4), 72e5);
|
|
2541
2734
|
/** At most this many schedules catch up in one tick after downtime; the rest wait for the next. */
|
|
@@ -2562,9 +2755,12 @@ function create(ctx, caller, request$1) {
|
|
|
2562
2755
|
const text = messageText(request$1.text);
|
|
2563
2756
|
if (request$1.every !== void 0 && (!Number.isFinite(request$1.every) || request$1.every < EVERY_MIN_S)) throw misuse("invalid_schedule", `--every is at least ${EVERY_MIN_S} seconds.`);
|
|
2564
2757
|
if (request$1.times !== void 0 && (!Number.isInteger(request$1.times) || request$1.times < 1)) throw misuse("invalid_schedule", "--times is a whole number of at least 1.");
|
|
2758
|
+
if (request$1.every !== void 0 && request$1.every > AHEAD_MAX_S) throw misuse("invalid_schedule", "--every is at most a year.");
|
|
2565
2759
|
for (const instant of [request$1.at, request$1.until]) if (instant !== void 0 && Number.isNaN(Date.parse(instant))) throw misuse("invalid_schedule", `"${instant}" isn't a time.`);
|
|
2566
2760
|
const now = ctx.now();
|
|
2761
|
+
const horizon = now.getTime() + AHEAD_MAX_S * 1e3;
|
|
2567
2762
|
const first = request$1.at !== void 0 ? new Date(request$1.at) : new Date(now.getTime() + (request$1.every ?? 0) * 1e3);
|
|
2763
|
+
if (first.getTime() > horizon || request$1.until !== void 0 && Date.parse(request$1.until) > horizon) throw misuse("invalid_schedule", "A schedule reaches at most a year out: its first send and its stop.");
|
|
2568
2764
|
return ctx.db.write(() => {
|
|
2569
2765
|
let channel;
|
|
2570
2766
|
let thread = null;
|
|
@@ -2591,7 +2787,8 @@ function create(ctx, caller, request$1) {
|
|
|
2591
2787
|
every: request$1.every ?? null,
|
|
2592
2788
|
times: request$1.every === void 0 ? 1 : request$1.times ?? null,
|
|
2593
2789
|
until: request$1.until ?? null,
|
|
2594
|
-
...caller.token ? { jti: caller.token } : {}
|
|
2790
|
+
...caller.token ? { jti: caller.token } : {},
|
|
2791
|
+
...request$1.noSync === true || caller.noSync === true ? { noSync: true } : {}
|
|
2595
2792
|
}
|
|
2596
2793
|
}, now.toISOString()).seq);
|
|
2597
2794
|
});
|
|
@@ -2638,10 +2835,44 @@ function tick(ctx, stopped) {
|
|
|
2638
2835
|
const sent = [];
|
|
2639
2836
|
const skipped = [];
|
|
2640
2837
|
const canceled = [];
|
|
2641
|
-
for (const { id: id$1 } of due)
|
|
2838
|
+
for (const { id: id$1 } of due) try {
|
|
2839
|
+
sendDue(ctx, id$1, now, stopped, {
|
|
2840
|
+
sent,
|
|
2841
|
+
skipped,
|
|
2842
|
+
canceled
|
|
2843
|
+
});
|
|
2844
|
+
} catch (error) {
|
|
2845
|
+
ctx.db.write(() => {
|
|
2846
|
+
const row$1 = ctx.db.raw.prepare("select channel, thread from schedules where id = ?").get(id$1);
|
|
2847
|
+
append(ctx.db, {
|
|
2848
|
+
type: "schedule.canceled",
|
|
2849
|
+
by: {
|
|
2850
|
+
principal: "board",
|
|
2851
|
+
profile: null
|
|
2852
|
+
},
|
|
2853
|
+
channel: row$1.channel,
|
|
2854
|
+
thread: row$1.thread,
|
|
2855
|
+
data: {
|
|
2856
|
+
id: id$1,
|
|
2857
|
+
why: "unsendable",
|
|
2858
|
+
error: error.message
|
|
2859
|
+
}
|
|
2860
|
+
}, new Date(now).toISOString());
|
|
2861
|
+
});
|
|
2862
|
+
canceled.push(id$1);
|
|
2863
|
+
}
|
|
2864
|
+
return {
|
|
2865
|
+
sent,
|
|
2866
|
+
skipped,
|
|
2867
|
+
canceled
|
|
2868
|
+
};
|
|
2869
|
+
}
|
|
2870
|
+
/** Sends, skips or cancels one due schedule, in one write. */
|
|
2871
|
+
function sendDue(ctx, id$1, now, stopped, { sent, skipped, canceled }) {
|
|
2872
|
+
ctx.db.write(() => {
|
|
2642
2873
|
const row$1 = ctx.db.raw.prepare("select * from schedules where id = ?").get(id$1);
|
|
2643
2874
|
if (row$1.canceled === 1 || row$1.next_at === null || Date.parse(row$1.next_at) > now) return;
|
|
2644
|
-
const made = ctx.db.raw.prepare("select json_extract(data, '$.jti') as jti from events where seq = ?").get(id$1);
|
|
2875
|
+
const made = ctx.db.raw.prepare("select json_extract(data, '$.jti') as jti, json_extract(data, '$.noSync') as no_sync from events where seq = ?").get(id$1);
|
|
2645
2876
|
if (stopped && made?.jti && stopped(made.jti)) {
|
|
2646
2877
|
append(ctx.db, {
|
|
2647
2878
|
type: "schedule.canceled",
|
|
@@ -2700,7 +2931,7 @@ function tick(ctx, stopped) {
|
|
|
2700
2931
|
text: row$1.text,
|
|
2701
2932
|
labels: {},
|
|
2702
2933
|
attachments: [],
|
|
2703
|
-
noSync:
|
|
2934
|
+
noSync: made?.no_sync === 1 || root?.noSync === true,
|
|
2704
2935
|
mentions: [],
|
|
2705
2936
|
schedule: {
|
|
2706
2937
|
id: id$1,
|
|
@@ -2712,11 +2943,6 @@ function tick(ctx, stopped) {
|
|
|
2712
2943
|
}, at);
|
|
2713
2944
|
sent.push(id$1);
|
|
2714
2945
|
});
|
|
2715
|
-
return {
|
|
2716
|
-
sent,
|
|
2717
|
-
skipped,
|
|
2718
|
-
canceled
|
|
2719
|
-
};
|
|
2720
2946
|
}
|
|
2721
2947
|
|
|
2722
2948
|
//#endregion
|
|
@@ -3252,6 +3478,8 @@ function isOutage(error) {
|
|
|
3252
3478
|
//#region src/sync/files.ts
|
|
3253
3479
|
/** How many ticks a file that failed for a reason that may pass is tried before its message goes without it. */
|
|
3254
3480
|
const FETCH_TRIES = 5;
|
|
3481
|
+
/** Said of a file the board's store would not take for a day. */
|
|
3482
|
+
const NOT_STORED = "the board's file store did not take it";
|
|
3255
3483
|
/** What files one sync may bring onto the board in a day (UTC), unless its `filesPerDay` says otherwise. */
|
|
3256
3484
|
const SYNC_FILES_PER_DAY = 500 * 1024 * 1024;
|
|
3257
3485
|
/** What one tick fetches at most, in bytes and time: the rest waits for the next, so schedules and watchers keep theirs. */
|
|
@@ -3267,8 +3495,12 @@ const today = (ctx) => ctx.now().toISOString().slice(0, 10);
|
|
|
3267
3495
|
const storedToday = (ctx, key) => Number(ctx.db.raw.prepare("select bytes from sync_files where key = ? and day = ?").get(key, today(ctx))?.bytes ?? 0);
|
|
3268
3496
|
/** A file that will not be fetched however often it is tried: the reason is said in the message instead. */
|
|
3269
3497
|
const refused$2 = (why$1) => new Failure("file_refused", why$1);
|
|
3270
|
-
/**
|
|
3271
|
-
|
|
3498
|
+
/**
|
|
3499
|
+
* A file's name as the board keeps it: one line, no path, no brackets that would end a note, at most 255
|
|
3500
|
+
* characters. No one is there to rename a platform's file, so a dotfile loses its dots and invisible formatting
|
|
3501
|
+
* is dropped, as `fileNameProblem` asks of a name a person gives.
|
|
3502
|
+
*/
|
|
3503
|
+
const safeName = (name) => name.replace(INVISIBLE, "").replace(/[\u0000-\u001f\u007f/\\[\]]/g, "_").replace(/^[.\s]+/, "").trim().slice(0, 255) || "file";
|
|
3272
3504
|
/** A content type, only when it is one: `type/subtype`, nothing else. */
|
|
3273
3505
|
const safeType = (type) => {
|
|
3274
3506
|
const bare = (type ?? "").split(";")[0].trim().toLowerCase();
|
|
@@ -3339,13 +3571,23 @@ async function fetchFiles(ctx, sync, fetcher, budget = {
|
|
|
3339
3571
|
bytes: TICK_BYTES,
|
|
3340
3572
|
ms: TICK_MS
|
|
3341
3573
|
}) {
|
|
3342
|
-
const rows = ctx.db.raw.prepare("select key, id, payload from inbox where done = 0 and payload like '%\"files\":%' order by received, id").all();
|
|
3574
|
+
const rows = ctx.db.raw.prepare("select key, id, payload, received from inbox where done = 0 and payload like '%\"files\":%' order by received, id").all();
|
|
3343
3575
|
if (rows.length) ctx.db.write(() => ensureCount(ctx));
|
|
3344
3576
|
const started = performance.now();
|
|
3345
3577
|
let spent$2 = 0;
|
|
3346
3578
|
const spare = () => spent$2 < budget.bytes && performance.now() - started < budget.ms;
|
|
3347
3579
|
const save = (row$1, next) => ctx.db.write(() => ctx.db.raw.prepare("update inbox set payload = ? where key = ? and id = ? and done = 0").get(JSON.stringify(next), row$1.key, row$1.id));
|
|
3580
|
+
let store = null;
|
|
3581
|
+
let noStore = false;
|
|
3582
|
+
try {
|
|
3583
|
+
store = rows.length ? storeOf(ctx) : null;
|
|
3584
|
+
} catch {
|
|
3585
|
+
noStore = true;
|
|
3586
|
+
}
|
|
3587
|
+
let storeDown = noStore;
|
|
3348
3588
|
for (const row$1 of rows) {
|
|
3589
|
+
const late$1 = ctx.now().getTime() - Date.parse(row$1.received) > OUTAGE_MS;
|
|
3590
|
+
if (storeDown && !late$1) continue;
|
|
3349
3591
|
if (!spare()) return;
|
|
3350
3592
|
const provider = sync[row$1.key];
|
|
3351
3593
|
const event = JSON.parse(row$1.payload);
|
|
@@ -3401,6 +3643,10 @@ async function fetchFiles(ctx, sync, fetcher, budget = {
|
|
|
3401
3643
|
notes.push(noteIn(file$1.name, "this platform does not hand out files"));
|
|
3402
3644
|
continue;
|
|
3403
3645
|
}
|
|
3646
|
+
if (noStore) {
|
|
3647
|
+
notes.push(noteIn(file$1.name, NOT_STORED));
|
|
3648
|
+
continue;
|
|
3649
|
+
}
|
|
3404
3650
|
const allowance = provider.filesPerDay ?? SYNC_FILES_PER_DAY;
|
|
3405
3651
|
const remaining = allowance - storedToday(ctx, row$1.key);
|
|
3406
3652
|
const over = `over the ${sizeOf(allowance)} a day this sync may bring in`;
|
|
@@ -3418,9 +3664,22 @@ async function fetchFiles(ctx, sync, fetcher, budget = {
|
|
|
3418
3664
|
try {
|
|
3419
3665
|
const got = await provider.download(file$1, fetcher, limit);
|
|
3420
3666
|
spent$2 += got.bytes.length;
|
|
3667
|
+
if (store) try {
|
|
3668
|
+
await store.put(fileId(got.bytes), got.bytes);
|
|
3669
|
+
} catch {
|
|
3670
|
+
if (late$1) {
|
|
3671
|
+
notes.push(noteIn(file$1.name, NOT_STORED));
|
|
3672
|
+
continue;
|
|
3673
|
+
}
|
|
3674
|
+
storeDown = true;
|
|
3675
|
+
left = files.slice(index);
|
|
3676
|
+
break;
|
|
3677
|
+
}
|
|
3421
3678
|
const id$1 = ctx.db.write(() => {
|
|
3422
3679
|
ctx.db.raw.prepare("insert into sync_files (key, day, bytes) values (?, ?, ?) on conflict (key, day) do update set bytes = bytes + excluded.bytes").get(row$1.key, today(ctx), got.bytes.length);
|
|
3423
|
-
return storeBytes(ctx, got.bytes);
|
|
3680
|
+
if (!store) return storeBytes(ctx, got.bytes);
|
|
3681
|
+
noteStored(ctx, fileId(got.bytes));
|
|
3682
|
+
return fileId(got.bytes);
|
|
3424
3683
|
});
|
|
3425
3684
|
attachments.push({
|
|
3426
3685
|
id: id$1,
|
|
@@ -3962,7 +4221,7 @@ async function deliverPending(ctx, sync, fetcher = fetch) {
|
|
|
3962
4221
|
const rootCopy = event.thread === null ? void 0 : ctx.db.raw.prepare("select place from links where message = ? and key = ?").get(event.thread, key);
|
|
3963
4222
|
const where = copy?.place ?? rootCopy?.place ?? null;
|
|
3964
4223
|
if (copy && where && data.origin?.key !== key && !data.noSync && provider.mirrors(where)) try {
|
|
3965
|
-
if (event.type === "message.edited") await provider.update?.(copy.external, String(data.text ?? ""), where, fetcher);
|
|
4224
|
+
if (event.type === "message.edited") await provider.update?.(copy.external, String(data.text ?? ""), where, fetcher, { external: data.origin !== void 0 });
|
|
3966
4225
|
else await provider.remove?.(copy.external, where, fetcher);
|
|
3967
4226
|
} catch {}
|
|
3968
4227
|
advance$1();
|
|
@@ -4012,8 +4271,9 @@ async function deliverPending(ctx, sync, fetcher = fetch) {
|
|
|
4012
4271
|
}
|
|
4013
4272
|
sent ??= await provider.deliver(message, {
|
|
4014
4273
|
place,
|
|
4015
|
-
threadExternal
|
|
4016
|
-
|
|
4274
|
+
threadExternal,
|
|
4275
|
+
external: data.origin !== void 0
|
|
4276
|
+
}, fetcher, (id$1) => readBytes(ctx, id$1), marker);
|
|
4017
4277
|
answered(ctx, sync, key);
|
|
4018
4278
|
ctx.db.write(() => {
|
|
4019
4279
|
ctx.db.raw.prepare("delete from sending where key = ? and seq = ?").get(key, event.seq);
|
|
@@ -4876,7 +5136,8 @@ function file(options = {}) {
|
|
|
4876
5136
|
const ctx = () => ({
|
|
4877
5137
|
db: open(),
|
|
4878
5138
|
now: options.now ?? (() => /* @__PURE__ */ new Date()),
|
|
4879
|
-
alive: options.alive ?? sameProcess
|
|
5139
|
+
alive: options.alive ?? sameProcess,
|
|
5140
|
+
...options.files ? { files: options.files } : {}
|
|
4880
5141
|
});
|
|
4881
5142
|
/** The board's list of the tokens it made: a table in the board itself. */
|
|
4882
5143
|
const signingKey = () => options.key ?? boardKey(where());
|
|
@@ -5010,8 +5271,19 @@ function file(options = {}) {
|
|
|
5010
5271
|
const c = ctx();
|
|
5011
5272
|
return budgets(c, narrowed(c, caller));
|
|
5012
5273
|
},
|
|
5013
|
-
filePut:
|
|
5014
|
-
|
|
5274
|
+
filePut: async (caller, request$1) => {
|
|
5275
|
+
const c = ctx();
|
|
5276
|
+
const who = narrowed(c, caller);
|
|
5277
|
+
const store = storeOf(c);
|
|
5278
|
+
if (store === null) return filePut(c, who, request$1);
|
|
5279
|
+
const { record, bytes } = fileCheck(c, who, request$1);
|
|
5280
|
+
await store.put(record.id, bytes);
|
|
5281
|
+
return fileKept(c, who, record, bytes, store);
|
|
5282
|
+
},
|
|
5283
|
+
fileGet: async (caller, request$1) => {
|
|
5284
|
+
const c = ctx();
|
|
5285
|
+
return fileGet(c, narrowed(c, caller), request$1);
|
|
5286
|
+
},
|
|
5015
5287
|
schemaAdd: op(schemaAdd),
|
|
5016
5288
|
schemaList: async () => schemaList(ctx()),
|
|
5017
5289
|
scheduleCreate: op(create),
|
|
@@ -5496,6 +5768,32 @@ function withoutPids(value, depth = 0) {
|
|
|
5496
5768
|
for (const inner of Object.values(value)) withoutPids(inner, depth + 1);
|
|
5497
5769
|
}
|
|
5498
5770
|
const MOUNT = "/api/board";
|
|
5771
|
+
const SIGN_IN_ID = /^[A-Za-z0-9][A-Za-z0-9_.@-]{0,127}$/;
|
|
5772
|
+
/** The caller a sign-in names, or why it can't be one: the owner's code got it wrong, which is theirs to read. */
|
|
5773
|
+
function signInCaller(said) {
|
|
5774
|
+
if (!said || typeof said !== "object") return "returned something other than a sign-in or null";
|
|
5775
|
+
const { id: id$1, profile, can, noSync } = said;
|
|
5776
|
+
if (typeof id$1 !== "string" || !SIGN_IN_ID.test(id$1)) return "returned an id that is not 1 to 128 ASCII letters, digits and _ . @ -";
|
|
5777
|
+
if (!can || typeof can !== "object" || Array.isArray(can) || Object.keys(can).length === 0) return "returned no can: name the channels, or '*' for all";
|
|
5778
|
+
for (const [pattern, permissions] of Object.entries(can)) if (!Array.isArray(permissions) || permissions.length === 0 || !permissions.every((permission) => PERMISSIONS.includes(permission))) return `returned permissions for "${pattern}" that are not some of ${PERMISSIONS.join(", ")}`;
|
|
5779
|
+
let named$1 = null;
|
|
5780
|
+
if (profile !== void 0 && profile !== null) {
|
|
5781
|
+
try {
|
|
5782
|
+
named$1 = profileName(String(profile));
|
|
5783
|
+
} catch {
|
|
5784
|
+
return `returned "${String(profile)}", which is not a profile name`;
|
|
5785
|
+
}
|
|
5786
|
+
if (named$1 === WORKFLOWS_PROFILE) return `returned the daemon's own profile, ${WORKFLOWS_PROFILE}`;
|
|
5787
|
+
}
|
|
5788
|
+
return {
|
|
5789
|
+
principal: `sso:${id$1.toLowerCase()}`,
|
|
5790
|
+
profile: named$1,
|
|
5791
|
+
can,
|
|
5792
|
+
meter: "",
|
|
5793
|
+
...noSync === true ? { noSync: true } : {},
|
|
5794
|
+
...named$1 ? { profileHeld: true } : {}
|
|
5795
|
+
};
|
|
5796
|
+
}
|
|
5499
5797
|
/** Room for one file, as base64, and its envelope. */
|
|
5500
5798
|
const BODY_MAX = 36 * 1024 * 1024;
|
|
5501
5799
|
/** A webhook is read before anything can say who sent it, so it gets far less room than a request with a token. */
|
|
@@ -5562,6 +5860,32 @@ function createHandler(options) {
|
|
|
5562
5860
|
...claims.jti ? { token: claims.jti } : {}
|
|
5563
5861
|
};
|
|
5564
5862
|
};
|
|
5863
|
+
/**
|
|
5864
|
+
* The caller the owner's `authenticate` names for a request with no token. Nobody, or a hook that fails, is
|
|
5865
|
+
* answered as a request with no token is; why is the owner's to read, as it can name their identity provider.
|
|
5866
|
+
*/
|
|
5867
|
+
const signIn = async (req, url) => {
|
|
5868
|
+
const headers = {};
|
|
5869
|
+
for (const [name, value] of Object.entries(req.headers)) headers[name] = Array.isArray(value) ? value.join(", ") : value;
|
|
5870
|
+
let said;
|
|
5871
|
+
try {
|
|
5872
|
+
said = await options.authenticate({
|
|
5873
|
+
method: req.method ?? "GET",
|
|
5874
|
+
path: url.pathname + url.search,
|
|
5875
|
+
headers
|
|
5876
|
+
});
|
|
5877
|
+
} catch (error) {
|
|
5878
|
+
options.log?.(`sign-in authenticate threw: ${error instanceof Error ? error.message : String(error)}`);
|
|
5879
|
+
said = null;
|
|
5880
|
+
}
|
|
5881
|
+
if (said === null || said === void 0) return callerOf(req);
|
|
5882
|
+
const caller = signInCaller(said);
|
|
5883
|
+
if (typeof caller === "string") {
|
|
5884
|
+
options.log?.(`sign-in authenticate ${caller}`);
|
|
5885
|
+
return callerOf(req);
|
|
5886
|
+
}
|
|
5887
|
+
return caller;
|
|
5888
|
+
};
|
|
5565
5889
|
return async function handle(req, res) {
|
|
5566
5890
|
try {
|
|
5567
5891
|
const host = (req.headers.host ?? "").toLowerCase();
|
|
@@ -5664,15 +5988,37 @@ function createHandler(options) {
|
|
|
5664
5988
|
throw error;
|
|
5665
5989
|
}
|
|
5666
5990
|
}
|
|
5667
|
-
const
|
|
5991
|
+
const signedIn = bearer === void 0 && options.authenticate !== void 0;
|
|
5992
|
+
const caller = signedIn ? await signIn(req, url) : callerOf(req);
|
|
5993
|
+
if (signedIn) {
|
|
5994
|
+
const origin = req.headers.origin;
|
|
5995
|
+
let from = null;
|
|
5996
|
+
try {
|
|
5997
|
+
from = origin === void 0 ? null : new URL(origin).host.toLowerCase();
|
|
5998
|
+
} catch {
|
|
5999
|
+
from = "";
|
|
6000
|
+
}
|
|
6001
|
+
if (origin !== void 0 && from !== host && from !== name && !extra.has(from ?? "")) throw new Failure("forbidden", `This board doesn't take requests from "${origin}".`);
|
|
6002
|
+
if (req.method !== "GET" && !/^application\/json\b/i.test(req.headers["content-type"] ?? "")) throw new Failure("forbidden", "A request from a sign-in is sent as application/json.");
|
|
6003
|
+
if ([
|
|
6004
|
+
"tokenCreate",
|
|
6005
|
+
"tokenList",
|
|
6006
|
+
"tokenRevoke",
|
|
6007
|
+
"joinList",
|
|
6008
|
+
"joinApprove",
|
|
6009
|
+
"joinDeny",
|
|
6010
|
+
"mcp",
|
|
6011
|
+
"scheduleCreate"
|
|
6012
|
+
].includes(route)) throw new Failure("forbidden", "This takes a token, and this request came through the sign-in.", { hint: "Ask whoever runs the board." });
|
|
6013
|
+
}
|
|
5668
6014
|
if (route === "whoami" && req.method === "GET") {
|
|
5669
|
-
const held$1 = claimsOf(req);
|
|
6015
|
+
const held$1 = signedIn ? null : claimsOf(req);
|
|
5670
6016
|
return send$1(res, 200, { ok: {
|
|
5671
6017
|
principal: caller.principal,
|
|
5672
6018
|
profile: caller.profile,
|
|
5673
6019
|
can: caller.can ?? null,
|
|
5674
|
-
exp: held$1
|
|
5675
|
-
...held$1
|
|
6020
|
+
exp: held$1?.exp ?? null,
|
|
6021
|
+
...held$1?.sandbox ? { sandbox: true } : {}
|
|
5676
6022
|
} });
|
|
5677
6023
|
}
|
|
5678
6024
|
if (route === "tokenCreate" || route === "tokenList" || route === "tokenRevoke") {
|
|
@@ -5680,10 +6026,11 @@ function createHandler(options) {
|
|
|
5680
6026
|
const held$1 = claimsOf(req);
|
|
5681
6027
|
if (held$1.can || held$1.minted) throw new Failure("forbidden", "A token that is held to channels, or was made from another, can't manage tokens.", { hint: "Ask whoever runs the board." });
|
|
5682
6028
|
const [request$1] = (await bodyOf(req)).args ?? [];
|
|
5683
|
-
|
|
6029
|
+
const visible = () => tokens.list(now()).filter((token) => held$1.profile === null || token.profile === held$1.profile);
|
|
6030
|
+
if (route === "tokenList") return send$1(res, 200, { ok: { tokens: visible() } });
|
|
5684
6031
|
if (route === "tokenRevoke") {
|
|
5685
6032
|
const id$1 = request$1?.id;
|
|
5686
|
-
if (typeof id$1 !== "string" || !tokens.revoke(id$1)) throw new Failure("not_found", "No such token.");
|
|
6033
|
+
if (typeof id$1 !== "string" || held$1.profile !== null && !visible().some((token) => token.id === id$1) || !tokens.revoke(id$1)) throw new Failure("not_found", "No such token.");
|
|
5687
6034
|
return send$1(res, 200, { ok: { revoked: id$1 } });
|
|
5688
6035
|
}
|
|
5689
6036
|
return send$1(res, 200, { ok: mintToken(options.key, tokens, {
|
|
@@ -5704,10 +6051,39 @@ function createHandler(options) {
|
|
|
5704
6051
|
res.writeHead(405, { allow: "POST" });
|
|
5705
6052
|
return res.end();
|
|
5706
6053
|
}
|
|
5707
|
-
const
|
|
6054
|
+
const message = await bodyOf(req);
|
|
6055
|
+
const header = (name$1) => {
|
|
6056
|
+
const value = req.headers[name$1];
|
|
6057
|
+
return Array.isArray(value) ? value.join(",") : value;
|
|
6058
|
+
};
|
|
6059
|
+
const problem = options.mcpHeaders?.(message, header);
|
|
6060
|
+
if (problem) return send$1(res, problem.status, problem.body);
|
|
6061
|
+
const gaveUp = new AbortController();
|
|
6062
|
+
res.on("close", () => {
|
|
6063
|
+
if (!res.writableFinished) gaveUp.abort();
|
|
6064
|
+
});
|
|
6065
|
+
let streaming = false;
|
|
6066
|
+
const frame = (message$1) => res.write(`data: ${JSON.stringify(message$1)}\n\n`);
|
|
6067
|
+
const notify = (note) => {
|
|
6068
|
+
if (!streaming) {
|
|
6069
|
+
res.writeHead(200, {
|
|
6070
|
+
"content-type": "text/event-stream",
|
|
6071
|
+
"cache-control": "no-store",
|
|
6072
|
+
connection: "keep-alive"
|
|
6073
|
+
});
|
|
6074
|
+
streaming = true;
|
|
6075
|
+
}
|
|
6076
|
+
frame(note);
|
|
6077
|
+
};
|
|
6078
|
+
const response = await options.mcp(message, {
|
|
5708
6079
|
token: (req.headers.authorization ?? "").slice(7),
|
|
5709
6080
|
profile: caller.profile
|
|
5710
|
-
});
|
|
6081
|
+
}, gaveUp.signal, notify);
|
|
6082
|
+
if (gaveUp.signal.aborted) return;
|
|
6083
|
+
if (streaming) {
|
|
6084
|
+
if (response) frame(response);
|
|
6085
|
+
return res.end();
|
|
6086
|
+
}
|
|
5711
6087
|
if (response === null) {
|
|
5712
6088
|
res.writeHead(202);
|
|
5713
6089
|
return res.end();
|
|
@@ -5722,8 +6098,13 @@ function createHandler(options) {
|
|
|
5722
6098
|
if (!store) throw new Failure("joins_closed", "This board doesn't keep requests to join.");
|
|
5723
6099
|
const [request$1] = (await bodyOf(req)).args ?? [];
|
|
5724
6100
|
const given = request$1 ?? {};
|
|
5725
|
-
|
|
6101
|
+
const visible = () => store.list().filter((join$2) => held$1.profile === null || join$2.profile === held$1.profile);
|
|
6102
|
+
if (route === "joinList") return send$1(res, 200, { ok: { joins: visible() } });
|
|
5726
6103
|
if (typeof given.code !== "string") throw misuse("invalid_code", "Say which request: its code.");
|
|
6104
|
+
if (held$1.profile !== null) {
|
|
6105
|
+
const code = normalCode(given.code);
|
|
6106
|
+
if (!visible().some((join$2) => join$2.code === code)) throw new Failure("unknown_join", `No request is waiting under ${given.code}.`, { hint: "A request lasts 15 minutes: ask again." });
|
|
6107
|
+
}
|
|
5727
6108
|
if (route === "joinDeny") return send$1(res, 200, { ok: { join: store.answer(given.code, null) } });
|
|
5728
6109
|
const join$1 = store.pending(given.code);
|
|
5729
6110
|
const made = mintToken(options.key, tokens, {
|
|
@@ -5772,13 +6153,18 @@ function createHandler(options) {
|
|
|
5772
6153
|
if (!open) return res.end();
|
|
5773
6154
|
res.write(`: connected\n\n`);
|
|
5774
6155
|
let quiet = 0;
|
|
6156
|
+
let reader = caller;
|
|
5775
6157
|
while (open) {
|
|
5776
6158
|
try {
|
|
5777
|
-
|
|
6159
|
+
if (signedIn) {
|
|
6160
|
+
const fresh = await signIn(req, url);
|
|
6161
|
+
if (fresh.principal !== caller.principal || fresh.profile !== caller.profile) break;
|
|
6162
|
+
reader = fresh;
|
|
6163
|
+
} else claimsOf(req);
|
|
5778
6164
|
} catch {
|
|
5779
6165
|
break;
|
|
5780
6166
|
}
|
|
5781
|
-
const page = await options.backend.events(
|
|
6167
|
+
const page = await options.backend.events(reader, {
|
|
5782
6168
|
after,
|
|
5783
6169
|
filter
|
|
5784
6170
|
});
|
|
@@ -5799,10 +6185,13 @@ function createHandler(options) {
|
|
|
5799
6185
|
const result = await options.backend[route](caller, ...args);
|
|
5800
6186
|
send$1(res, 200, { ok: result === void 0 ? null : result });
|
|
5801
6187
|
} catch (error) {
|
|
5802
|
-
if (error instanceof Failure)
|
|
5803
|
-
error
|
|
5804
|
-
|
|
5805
|
-
|
|
6188
|
+
if (error instanceof Failure) {
|
|
6189
|
+
if (error.cause instanceof Error) options.log?.(`error ${req.method} ${(req.url ?? "").split("?")[0]}: ${error.reason}: ${error.cause.message}`);
|
|
6190
|
+
return send$1(res, statusOf(error), {
|
|
6191
|
+
error: error.toJSON(),
|
|
6192
|
+
exit: error.exit
|
|
6193
|
+
});
|
|
6194
|
+
}
|
|
5806
6195
|
options.log?.(`error ${req.method} ${(req.url ?? "").split("?")[0]}: ${error instanceof Error ? error.message : String(error)}`);
|
|
5807
6196
|
send$1(res, 500, {
|
|
5808
6197
|
error: {
|
|
@@ -5992,6 +6381,7 @@ function mount(backend, options = {}) {
|
|
|
5992
6381
|
retired: backend.retiredKeys?.() ?? [],
|
|
5993
6382
|
hosts: hosts$1,
|
|
5994
6383
|
log,
|
|
6384
|
+
...options.authenticate ? { authenticate: options.authenticate } : {},
|
|
5995
6385
|
...now ? { now: () => now().getTime() } : {}
|
|
5996
6386
|
}), base, log);
|
|
5997
6387
|
};
|
|
@@ -7225,6 +7615,7 @@ function board(options = {}) {
|
|
|
7225
7615
|
fetch: served,
|
|
7226
7616
|
close: () => backend.close(),
|
|
7227
7617
|
[Symbol.for("muster.copy")]: COPY,
|
|
7618
|
+
[Symbol.for("muster.authenticate")]: serve?.authenticate,
|
|
7228
7619
|
[ROOT]: () => root
|
|
7229
7620
|
};
|
|
7230
7621
|
for (const name of METHODS) handle[name] = async (...args) => backend[name](await who(), ...args);
|
|
@@ -7322,4 +7713,4 @@ function cursor(input, flag) {
|
|
|
7322
7713
|
}
|
|
7323
7714
|
|
|
7324
7715
|
//#endregion
|
|
7325
|
-
export {
|
|
7716
|
+
export { OPEN_STATES as $, boardPath as A, heldClaim as B, listen as C, VERSION as D, RELEASE_HOSTS as E, retiredKeys as F, nameOf as G, declared as H, rotateKey as I, refused$2 as J, fetchFile as K, verify$1 as L, boardKey as M, dropRetiredKeys as N, releaseTarball as O, mint as P, processStart as Q, configDir as R, isLoopback as S, SANDBOX_ROUTES as T, env as U, keepClaim as V, isEnv as W, withLines as X, sizeOf as Y, SEND_MS as Z, ping as _, list as a, WORKFLOWS_PROFILE as at, MOUNT as b, parse as c, channelScope as ct, ROOT as d, labelFilter as dt, backToDisk as et, board as f, profileName as ft, armHooks as g, HOOKS_LEASE as h, misuse as ht, id as i, PROGRESS as it, file as j, loadOidc as k, str as l, fileName as lt, DROPPED as m, cursorAhead as mt, cursor as n, INVISIBLE as nt, on as o, actorId as ot, syncLoop as p, Failure as pt, noteOut as q, duration as r, LIMITS as rt, pairs as s, channelPath as st, GLOBAL as t, memoryFiles as tt, COPY as u, inScope as ut, why as v, ROUTES as w, createHandler as x, holdLease as y, dropClaim as z };
|