@plitzi/sdk-server 0.38.24 → 0.39.0
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/CHANGELOG.md +152 -1
- package/README.md +55 -9
- package/dist/actions.d.ts +4 -1
- package/dist/actions.js +3 -2
- package/dist/auth.d.ts +2 -2
- package/dist/auth.js +5 -4
- package/dist/core/auth/api/accounts.d.ts +11 -0
- package/dist/core/auth/api/accounts.js +175 -0
- package/dist/core/auth/api/admin.d.ts +41 -0
- package/dist/core/auth/api/admin.js +146 -0
- package/dist/core/auth/api/context.d.ts +119 -0
- package/dist/core/auth/api/context.js +262 -0
- package/dist/core/auth/api/factors.d.ts +31 -0
- package/dist/core/auth/api/factors.js +209 -0
- package/dist/core/auth/api/index.d.ts +94 -0
- package/dist/core/auth/api/index.js +43 -0
- package/dist/core/auth/api/profile.d.ts +43 -0
- package/dist/core/auth/api/profile.js +243 -0
- package/dist/core/auth/api/session.d.ts +62 -0
- package/dist/core/auth/api/session.js +312 -0
- package/dist/core/auth/{api.d.ts → api/types.d.ts} +41 -195
- package/dist/core/auth/api/values.d.ts +67 -0
- package/dist/core/auth/api/values.js +90 -0
- package/dist/core/auth/createAuth.d.ts +49 -39
- package/dist/core/auth/createAuth.js +21 -1
- package/dist/core/auth/oauth/client.d.ts +2 -2
- package/dist/core/auth/oauth/client.js +6 -1
- package/dist/core/auth/oauth/index.d.ts +30 -7
- package/dist/core/auth/oauth/index.js +33 -14
- package/dist/core/auth/oauth/providers/github.js +1 -1
- package/dist/core/auth/oauth/providers/google.js +1 -1
- package/dist/core/auth/oauth/providers/oidc.d.ts +40 -0
- package/dist/core/auth/oauth/providers/oidc.js +197 -0
- package/dist/core/auth/oauth/state.d.ts +2 -0
- package/dist/core/auth/oauth/state.js +2 -0
- package/dist/core/auth/oauth/types.d.ts +21 -3
- package/dist/core/auth/oauth/types.js +4 -1
- package/dist/core/auth/routes.js +1 -1
- package/dist/core/auth/tokens.d.ts +3 -1
- package/dist/core/auth/tokens.js +2 -1
- package/dist/core/configSeam.d.ts +0 -4
- package/dist/core/configSeam.js +12 -1
- package/dist/core/createServer.js +16 -4
- package/dist/core/freePort.d.ts +10 -0
- package/dist/core/freePort.js +39 -1
- package/dist/core/handlers/socialRouteHandlers.d.ts +9 -1
- package/dist/core/handlers/socialRouteHandlers.js +46 -12
- package/dist/core/health.d.ts +1 -0
- package/dist/core/health.js +2 -1
- package/dist/core/http/stages/authRoutes.d.ts +6 -0
- package/dist/core/http/stages/authRoutes.js +42 -1
- package/dist/core/http/stages/images.js +5 -3
- package/dist/core/http/stages/livePages.js +1 -1
- package/dist/core/server/fleet/relay.js +9 -1
- package/dist/core/server/fleet/stores.d.ts +7 -0
- package/dist/core/server/fleet/stores.js +15 -2
- package/dist/core/server/pageServer.js +5 -1
- package/dist/core/services/realtime.js +4 -2
- package/dist/core/services/registry.js +7 -1
- package/dist/core/services/robotsTxt.d.ts +16 -0
- package/dist/core/services/robotsTxt.js +50 -0
- package/dist/core/services/servedSpace.d.ts +8 -0
- package/dist/core/services/servedSpace.js +16 -0
- package/dist/core/services/sitemapXml.d.ts +7 -0
- package/dist/core/services/sitemapXml.js +37 -0
- package/dist/functions-api.d.ts +69 -2
- package/dist/helpers/fileStore.d.ts +60 -0
- package/dist/helpers/fileStore.js +127 -0
- package/dist/helpers/outboundGuard.d.ts +32 -3
- package/dist/helpers/outboundGuard.js +50 -5
- package/dist/hosts/project/agentRuns.d.ts +24 -0
- package/dist/hosts/project/agentRuns.js +107 -0
- package/dist/hosts/project/laterAsks.d.ts +41 -0
- package/dist/hosts/project/laterAsks.js +117 -0
- package/dist/hosts/project/plugins.js +11 -4
- package/dist/hosts/project/serveProject.d.ts +8 -1
- package/dist/hosts/project/serveProject.js +81 -24
- package/dist/hosts/project/spaceFailure.d.ts +4 -0
- package/dist/hosts/project/spaceFailure.js +10 -0
- package/dist/hosts/project/watchSpace.d.ts +5 -5
- package/dist/hosts/project/watchSpace.js +15 -6
- package/dist/index.js +2 -2
- package/dist/kernel.js +1 -1
- package/dist/modules/actions/connectorWrite.js +3 -1
- package/dist/modules/actions/index.js +1 -1
- package/dist/modules/actions/jobs/fileQueue.d.ts +34 -0
- package/dist/modules/actions/jobs/fileQueue.js +72 -0
- package/dist/modules/actions/jobs/index.js +3 -3
- package/dist/modules/actions/jobs/memoryQueue.js +6 -215
- package/dist/modules/actions/jobs/queueDocument.d.ts +14 -0
- package/dist/modules/actions/jobs/queueDocument.js +22 -0
- package/dist/modules/actions/jobs/queueRules.d.ts +39 -0
- package/dist/modules/actions/jobs/queueRules.js +270 -0
- package/dist/modules/actions/moduleFor.js +2 -0
- package/dist/modules/actions/runtime/fileKv.d.ts +3 -1
- package/dist/modules/actions/runtime/fileKv.js +12 -98
- package/dist/modules/actions/runtime/guards.d.ts +19 -1
- package/dist/modules/actions/runtime/guards.js +10 -1
- package/dist/modules/actions/runtime/kvChange.d.ts +40 -0
- package/dist/modules/actions/runtime/kvChange.js +23 -1
- package/dist/modules/actions/runtime/kvList.d.ts +4 -0
- package/dist/modules/actions/runtime/kvList.js +5 -1
- package/dist/modules/actions/runtime/kvStore.js +15 -1
- package/dist/modules/actions/runtime/namespaceKv.js +2 -0
- package/dist/modules/actions/runtime/renderResolver.js +31 -5
- package/dist/modules/actions/runtime/runAction.js +16 -6
- package/dist/modules/actions/runtime/scope.js +11 -0
- package/dist/modules/actions/runtime/taskContext.js +11 -32
- package/dist/modules/actions/tasks/auth.js +2 -0
- package/dist/modules/actions/tasks/connector.js +6 -2
- package/dist/modules/actions/tasks/flow.js +5 -0
- package/dist/modules/actions/tasks/http.js +2 -1
- package/dist/modules/actions/tasks/kv.js +5 -0
- package/dist/modules/actions/tasks/list.js +11 -2
- package/dist/modules/actions/tasks/transform.js +2 -0
- package/dist/modules/actions/transport/callHandler.js +25 -4
- package/dist/modules/actions/types.d.ts +46 -1
- package/dist/modules/connectors/engine.d.ts +7 -2
- package/dist/modules/connectors/engine.js +4 -4
- package/dist/modules/connectors/resolver.d.ts +2 -1
- package/dist/modules/connectors/resolver.js +3 -2
- package/dist/modules/functions/build.d.ts +7 -2
- package/dist/modules/functions/build.js +90 -6
- package/dist/modules/functions/capabilities.js +16 -0
- package/dist/modules/functions/context.js +6 -2
- package/dist/modules/functions/contract.d.ts +39 -1
- package/dist/modules/functions/driver.d.ts +2 -1
- package/dist/modules/functions/driver.js +35 -0
- package/dist/modules/functions/load.d.ts +1 -1
- package/dist/modules/functions/load.js +2 -2
- package/dist/modules/functions/manifest.js +7 -1
- package/dist/modules/functions/oauth.d.ts +1 -1
- package/dist/modules/functions/oauth.js +4 -4
- package/dist/modules/functions/protocol.d.ts +6 -0
- package/dist/modules/functions/sandbox/guest/bytes.d.ts +27 -0
- package/dist/modules/functions/sandbox/guest/bytes.js +108 -0
- package/dist/modules/functions/sandbox/guest/crypto.d.ts +47 -0
- package/dist/modules/functions/sandbox/guest/crypto.js +144 -0
- package/dist/modules/functions/sandbox/guest/events.d.ts +152 -0
- package/dist/modules/functions/sandbox/guest/events.js +143 -0
- package/dist/modules/functions/sandbox/guest/fetch.d.ts +305 -0
- package/dist/modules/functions/sandbox/guest/fetch.js +237 -0
- package/dist/modules/functions/sandbox/guest/index.d.ts +28 -0
- package/dist/modules/functions/sandbox/guest/index.js +130 -0
- package/dist/modules/functions/sandbox/{guest.d.ts → guest/types.d.ts} +5 -12
- package/dist/modules/functions/sandbox/isolate.js +20 -6
- package/dist/modules/functions/space.js +2 -8
- package/dist/modules/images/imageProxy.d.ts +3 -2
- package/dist/modules/images/imageProxy.js +2 -2
- package/dist/modules/mongo/jobQueue.js +0 -19
- package/dist/modules/mysql/accounts.js +12 -5
- package/dist/modules/mysql/jobs/index.d.ts +1 -1
- package/dist/modules/mysql/jobs/schema.d.ts +0 -6
- package/dist/modules/mysql/jobs/schema.js +2 -46
- package/dist/modules/mysql/schema.js +26 -1
- package/dist/modules/mysql/sessions.js +2 -1
- package/dist/modules/realtime/handlers.d.ts +9 -4
- package/dist/modules/realtime/handlers.js +20 -3
- package/dist/modules/realtime/hub.d.ts +4 -2
- package/dist/modules/realtime/hub.js +36 -4
- package/dist/modules/realtime/index.d.ts +14 -0
- package/dist/modules/realtime/index.js +14 -1
- package/dist/modules/realtime/presence.d.ts +33 -0
- package/dist/modules/realtime/presence.js +56 -0
- package/dist/modules/realtime/socket.d.ts +3 -1
- package/dist/modules/realtime/socket.js +2 -2
- package/dist/modules/rsc/connectorRscData.d.ts +6 -1
- package/dist/modules/rsc/connectorRscData.js +27 -6
- package/dist/modules/rsc/externalQueryReader.d.ts +28 -0
- package/dist/modules/rsc/externalQueryReader.js +43 -0
- package/dist/modules/rsc/handler.js +1 -1
- package/dist/modules/rsc/resolveRscData.d.ts +11 -1
- package/dist/modules/rsc/resolveRscData.js +86 -16
- package/dist/modules/runtime/inside.js +1 -1
- package/dist/modules/runtime/runner.js +2 -1
- package/dist/modules/sqlite/jobQueue.d.ts +35 -0
- package/dist/modules/sqlite/jobQueue.js +56 -0
- package/dist/modules/sqlite/kv.d.ts +1 -1
- package/dist/modules/sqlite/kv.js +2 -10
- package/dist/modules/sqlite/open.d.ts +3 -0
- package/dist/modules/sqlite/open.js +13 -0
- package/dist/modules/ssr/Component.d.ts +5 -1
- package/dist/modules/ssr/Component.js +3 -1
- package/dist/modules/ssr/applySSRResult.d.ts +8 -1
- package/dist/modules/ssr/applySSRResult.js +23 -3
- package/dist/modules/ssr/handler.js +30 -5
- package/dist/modules/ssr/prepareRender.js +27 -5
- package/dist/modules/ssr/preview.d.ts +2 -0
- package/dist/modules/ssr/preview.js +6 -3
- package/dist/modules/ssr/registerExternalPlugins.d.ts +2 -6
- package/dist/modules/ssr/registerExternalPlugins.js +2 -2
- package/dist/modules/ssr/resolvePageSeo.d.ts +11 -0
- package/dist/modules/ssr/resolvePageSeo.js +13 -2
- package/dist/modules/ssr/streamBody.js +2 -2
- package/dist/modules/ssr/views/template.ejs +21 -17
- package/dist/mysql.d.ts +1 -1
- package/dist/mysql.js +2 -2
- package/dist/plugins/manager.d.ts +5 -0
- package/dist/plugins/manager.js +9 -0
- package/dist/sqlite.d.ts +8 -1
- package/dist/sqlite.js +3 -1
- package/package.json +5 -5
- package/skills/plitzi-authoring/CHEATSHEET.md +6 -6
- package/skills/plitzi-authoring/SKILL.md +4 -2
- package/skills/plitzi-authoring/recipes/accordion.ts +2 -1
- package/skills/plitzi-authoring/recipes/carousel.ts +24 -3
- package/skills/plitzi-authoring/recipes/detail-page.ts +0 -1
- package/skills/plitzi-authoring/recipes/embed-and-svg.ts +3 -1
- package/skills/plitzi-authoring/recipes/filter-a-list.ts +0 -1
- package/skills/plitzi-authoring/recipes/show-data.ts +0 -1
- package/skills/plitzi-authoring/recipes/typed-data.ts +0 -1
- package/skills/plitzi-authoring/reference/accessibility.md +7 -2
- package/skills/plitzi-authoring/reference/auth.md +15 -4
- package/skills/plitzi-authoring/reference/authoring-errors.md +51 -10
- package/skills/plitzi-authoring/reference/colours-and-motion.md +7 -1
- package/skills/plitzi-authoring/reference/components.md +4 -1
- package/skills/plitzi-authoring/reference/data-and-visibility.md +31 -22
- package/skills/plitzi-authoring/reference/efficiency.md +2 -1
- package/skills/plitzi-authoring/reference/elements-and-styles.md +16 -15
- package/skills/plitzi-authoring/reference/flows.md +20 -8
- package/skills/plitzi-authoring/reference/kept-state.md +8 -0
- package/skills/plitzi-authoring/reference/layouts.md +18 -2
- package/skills/plitzi-authoring/reference/lists.md +20 -4
- package/skills/plitzi-authoring/reference/plugin-reach.md +12 -0
- package/skills/plitzi-authoring/reference/plugins.md +13 -2
- package/skills/plitzi-authoring/reference/realtime.md +8 -5
- package/skills/plitzi-authoring/reference/structure.md +17 -1
- package/skills/plitzi-authoring/reference/templates.md +11 -3
- package/skills/plitzi-authoring/reference/testing.md +82 -5
- package/skills/plitzi-authoring/reference/typed-sources.md +6 -0
- package/skills/plitzi-authoring/reference/validation.md +1 -1
- package/dist/core/auth/api.js +0 -1323
- package/dist/modules/functions/sandbox/guest.js +0 -662
|
@@ -1,65 +1,7 @@
|
|
|
1
|
-
import { serverLog } from "../../../helpers/serverLog.js";
|
|
2
1
|
import { createMapKv } from "./memoryKv.js";
|
|
3
|
-
import {
|
|
4
|
-
import path from "node:path";
|
|
2
|
+
import { documentWriter, holdFile, letGo } from "../../../helpers/fileStore.js";
|
|
5
3
|
import fs from "node:fs";
|
|
6
4
|
//#region src/modules/actions/runtime/fileKv.ts
|
|
7
|
-
var holderIn = (lock) => {
|
|
8
|
-
try {
|
|
9
|
-
const parsed = JSON.parse(fs.readFileSync(lock, "utf8"));
|
|
10
|
-
return typeof parsed === "object" && parsed !== null && "pid" in parsed && typeof parsed.pid === "number" ? {
|
|
11
|
-
pid: parsed.pid,
|
|
12
|
-
since: "since" in parsed && typeof parsed.since === "string" ? parsed.since : "?",
|
|
13
|
-
..."owner" in parsed && typeof parsed.owner === "string" ? { owner: parsed.owner } : {}
|
|
14
|
-
} : void 0;
|
|
15
|
-
} catch {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
/** Whether a process is still running: signal 0 asks without sending anything, and `EPERM` is one that is not ours. */
|
|
20
|
-
var running = (pid) => {
|
|
21
|
-
try {
|
|
22
|
-
process.kill(pid, 0);
|
|
23
|
-
return true;
|
|
24
|
-
} catch (error) {
|
|
25
|
-
return error.code === "EPERM";
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
/** The files this process holds, let go of together when it exits — however it does. */
|
|
29
|
-
var held = /* @__PURE__ */ new Set();
|
|
30
|
-
var letGo = (lock) => {
|
|
31
|
-
held.delete(lock);
|
|
32
|
-
if (holderIn(lock)?.pid === process.pid) fs.rmSync(lock, { force: true });
|
|
33
|
-
};
|
|
34
|
-
process.once("exit", () => held.forEach(letGo));
|
|
35
|
-
/**
|
|
36
|
-
* Takes the file for this process, or says who has it.
|
|
37
|
-
*
|
|
38
|
-
* Two processes on one file each load it and then write their whole map over it: every change one makes, the other
|
|
39
|
-
* erases with its next write, and nothing anywhere says so — a second server started beside the first, a `--watch`
|
|
40
|
-
* left running after a restart. So a file has one holder: a second is refused, naming the first. A lock whose process is
|
|
41
|
-
* gone — killed, crashed — is taken over.
|
|
42
|
-
*/
|
|
43
|
-
var take = (file, owner) => {
|
|
44
|
-
const lock = `${file}.lock`;
|
|
45
|
-
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
46
|
-
const mine = {
|
|
47
|
-
pid: process.pid,
|
|
48
|
-
since: (/* @__PURE__ */ new Date()).toISOString(),
|
|
49
|
-
...owner ? { owner } : {}
|
|
50
|
-
};
|
|
51
|
-
for (let attempt = 0; attempt < 2; attempt += 1) try {
|
|
52
|
-
fs.writeFileSync(lock, JSON.stringify(mine), { flag: "wx" });
|
|
53
|
-
held.add(lock);
|
|
54
|
-
return lock;
|
|
55
|
-
} catch (error) {
|
|
56
|
-
if (error.code !== "EEXIST") throw error;
|
|
57
|
-
const holder = holderIn(lock);
|
|
58
|
-
if (holder && running(holder.pid)) throw new Error(`createFileKv: ${file} is in use by process ${String(holder.pid)}${holder.owner ? ` — ${holder.owner}` : ""}, since ${holder.since}. Two processes on one file write over each other's changes: stop that one first, or point \`file\` elsewhere.`, { cause: error });
|
|
59
|
-
fs.rmSync(lock, { force: true });
|
|
60
|
-
}
|
|
61
|
-
throw new Error(`createFileKv: ${lock} could not be taken`);
|
|
62
|
-
};
|
|
63
5
|
var isEntry = (value) => typeof value === "object" && value !== null && "value" in value && typeof value.value === "string" && (!("expiresAt" in value) || typeof value.expiresAt === "number");
|
|
64
6
|
var load = (file) => {
|
|
65
7
|
if (!fs.existsSync(file)) return /* @__PURE__ */ new Map();
|
|
@@ -96,8 +38,11 @@ var load = (file) => {
|
|
|
96
38
|
* primary's through the fleet (`fleetKv`); replicas use `createSqliteKv` (`@plitzi/sdk-server/sqlite`) or a database.
|
|
97
39
|
* And a whole file per change: right for what a site keeps, not for a counter bumped thousands of times a second.
|
|
98
40
|
*/
|
|
99
|
-
var createFileKv = ({ file, owner }) => {
|
|
100
|
-
const lock =
|
|
41
|
+
var createFileKv = ({ file, owner, serves }) => {
|
|
42
|
+
const lock = holdFile(file, {
|
|
43
|
+
owner,
|
|
44
|
+
serves
|
|
45
|
+
}, "createFileKv");
|
|
101
46
|
let entries;
|
|
102
47
|
try {
|
|
103
48
|
entries = load(file);
|
|
@@ -105,10 +50,6 @@ var createFileKv = ({ file, owner }) => {
|
|
|
105
50
|
letGo(lock);
|
|
106
51
|
throw error;
|
|
107
52
|
}
|
|
108
|
-
let dirty = false;
|
|
109
|
-
let failed = false;
|
|
110
|
-
/** The write under way, resolving to whether everything it took on reached the disk. */
|
|
111
|
-
let writing;
|
|
112
53
|
const document = () => {
|
|
113
54
|
const now = Date.now();
|
|
114
55
|
const live = [...entries].filter(([, entry]) => entry.expiresAt === void 0 || entry.expiresAt > now);
|
|
@@ -118,39 +59,12 @@ var createFileKv = ({ file, owner }) => {
|
|
|
118
59
|
};
|
|
119
60
|
return `${JSON.stringify(body, null, 2)}\n`;
|
|
120
61
|
};
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
} catch (error) {
|
|
128
|
-
dirty = true;
|
|
129
|
-
failed = true;
|
|
130
|
-
serverLog.error("Actions", `kv file ${file} could not be written`, error);
|
|
131
|
-
return false;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return true;
|
|
135
|
-
};
|
|
136
|
-
const write = () => {
|
|
137
|
-
writing ??= new Promise((resolve) => {
|
|
138
|
-
queueMicrotask(resolve);
|
|
139
|
-
}).then(drain).finally(() => {
|
|
140
|
-
writing = void 0;
|
|
141
|
-
if (dirty && !failed) write();
|
|
142
|
-
});
|
|
143
|
-
return writing;
|
|
144
|
-
};
|
|
145
|
-
const changed = () => {
|
|
146
|
-
dirty = true;
|
|
147
|
-
failed = false;
|
|
148
|
-
write();
|
|
149
|
-
};
|
|
150
|
-
const flush = async () => {
|
|
151
|
-
failed = false;
|
|
152
|
-
while (writing !== void 0 || dirty) if (!await (writing ?? write())) throw new Error(`createFileKv: ${file} could not be written; the reason is in the log.`);
|
|
153
|
-
};
|
|
62
|
+
const { changed, flush } = documentWriter({
|
|
63
|
+
file,
|
|
64
|
+
document,
|
|
65
|
+
store: "createFileKv",
|
|
66
|
+
what: "kv file"
|
|
67
|
+
});
|
|
154
68
|
return {
|
|
155
69
|
...createMapKv(entries, changed),
|
|
156
70
|
flush,
|
|
@@ -8,6 +8,10 @@ import { ActionCallResult } from '@plitzi/sdk-shared';
|
|
|
8
8
|
*/
|
|
9
9
|
export type RunKind = 'call' | 'render';
|
|
10
10
|
/** A run currently holding a slot, and the handle that can stop it. */
|
|
11
|
+
/** What a run answered its caller: the call's result, and the reason a step wrote for the caller when it refused. */
|
|
12
|
+
export type RunAnswer = ActionCallResult & {
|
|
13
|
+
error?: string;
|
|
14
|
+
};
|
|
11
15
|
export type ActiveRun = {
|
|
12
16
|
runId: string;
|
|
13
17
|
runKey: string;
|
|
@@ -25,6 +29,13 @@ export type ActiveRun = {
|
|
|
25
29
|
* the input, and two identical calls a minute apart are usually two things somebody meant to happen twice.
|
|
26
30
|
*/
|
|
27
31
|
explicitKey: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* What the run answers, once it ends — `undefined` for one that ended with no answer (it threw): what a second
|
|
34
|
+
* identical call while it runs waits for, instead of being refused (`join`).
|
|
35
|
+
*/
|
|
36
|
+
answered: Promise<RunAnswer | undefined>;
|
|
37
|
+
/** Settles `answered`: called by `end`. */
|
|
38
|
+
settle: (outcome: RunAnswer | undefined) => void;
|
|
28
39
|
};
|
|
29
40
|
/**
|
|
30
41
|
* The half of a key/value store the guards need, and nothing more.
|
|
@@ -82,8 +93,15 @@ export type RunIdempotency = {
|
|
|
82
93
|
};
|
|
83
94
|
export type RunGuards = {
|
|
84
95
|
begin: (params: BeginRunParams) => Promise<ActiveRun>;
|
|
96
|
+
/**
|
|
97
|
+
* The answer of the run this same call already started in this process, waited for — `undefined` when none runs
|
|
98
|
+
* here (another replica holds it, or it ended). A page that calls the same action with the same input twice at once
|
|
99
|
+
* (loaded at two widths, two tabs, two components) is answered twice by one run, never refused: a run's room logged
|
|
100
|
+
* a 409 on every second load, and acceptance checks failed on it at random.
|
|
101
|
+
*/
|
|
102
|
+
join: (params: BeginRunParams) => Promise<RunAnswer | undefined> | undefined;
|
|
85
103
|
/** Releases the key, and — for a run the caller keyed itself — remembers the answer for {@link RunIdempotency}. */
|
|
86
|
-
end: (run: ActiveRun, outcome?:
|
|
104
|
+
end: (run: ActiveRun, outcome?: RunAnswer) => Promise<void>;
|
|
87
105
|
get: (runId: string) => ActiveRun | undefined;
|
|
88
106
|
/** The answer a run with this key already produced, when it is still within the replay window. */
|
|
89
107
|
replay: (params: BeginRunParams) => Promise<ActionCallResult | undefined>;
|
|
@@ -124,6 +124,10 @@ var createRunGuards = (config = {}, store, idempotency = {}) => {
|
|
|
124
124
|
*/
|
|
125
125
|
const kind = params.kind ?? "call";
|
|
126
126
|
if (kind === "render" ? count((run) => run.kind === "render") >= renderPerProcess : count((run) => run.kind === "call") >= perProcess || count((run) => run.kind === "call" && run.spaceId === params.spaceId) >= perSpace) throw new ActionRunError("over_capacity", "Too many actions are running right now");
|
|
127
|
+
let settle = () => void 0;
|
|
128
|
+
const answered = new Promise((resolve) => {
|
|
129
|
+
settle = resolve;
|
|
130
|
+
});
|
|
127
131
|
const run = {
|
|
128
132
|
runId: randomUUID(),
|
|
129
133
|
runKey,
|
|
@@ -133,7 +137,9 @@ var createRunGuards = (config = {}, store, idempotency = {}) => {
|
|
|
133
137
|
callerId: params.callerId,
|
|
134
138
|
controller: new AbortController(),
|
|
135
139
|
expiresAt: Date.now() + params.ttlMs,
|
|
136
|
-
explicitKey: Boolean(params.idempotencyKey)
|
|
140
|
+
explicitKey: Boolean(params.idempotencyKey),
|
|
141
|
+
answered,
|
|
142
|
+
settle
|
|
137
143
|
};
|
|
138
144
|
/**
|
|
139
145
|
* The local slot is taken BEFORE anything is awaited, and that ordering is the guarantee.
|
|
@@ -180,6 +186,7 @@ var createRunGuards = (config = {}, store, idempotency = {}) => {
|
|
|
180
186
|
const end = async (run, outcome) => {
|
|
181
187
|
byKey.delete(run.runKey);
|
|
182
188
|
byId.delete(run.runId);
|
|
189
|
+
run.settle(outcome);
|
|
183
190
|
if (outcome) await remember(run, outcome);
|
|
184
191
|
await store?.delete(`${keyPrefix}${run.runKey}`).catch(() => void 0);
|
|
185
192
|
await store?.delete(runOwnerKey(run.runId)).catch(() => void 0);
|
|
@@ -233,8 +240,10 @@ var createRunGuards = (config = {}, store, idempotency = {}) => {
|
|
|
233
240
|
return false;
|
|
234
241
|
}
|
|
235
242
|
};
|
|
243
|
+
const join = (params) => byKey.get(deriveRunKey(params))?.answered;
|
|
236
244
|
return {
|
|
237
245
|
begin,
|
|
246
|
+
join,
|
|
238
247
|
end,
|
|
239
248
|
get: (runId) => byId.get(runId),
|
|
240
249
|
replay,
|
|
@@ -25,3 +25,43 @@ export type KvChangeOptions<T> = {
|
|
|
25
25
|
* module's, only what the guest's globals also have (`setTimeout`, `Math`).
|
|
26
26
|
*/
|
|
27
27
|
export declare const changeKv: <V, T extends V>(store: KvChangeStore<V>, key: string, change: (current: V | undefined) => T | undefined | Promise<T | undefined>, { lifetime, refusal }: KvChangeOptions<T>) => Promise<T | undefined>;
|
|
28
|
+
/** What a change of a list's entry needs of a store: read the list, and write one entry only if it is still as read. */
|
|
29
|
+
export type KvListChangeStore = {
|
|
30
|
+
listRange: (list: string) => Promise<{
|
|
31
|
+
id: string;
|
|
32
|
+
score: number;
|
|
33
|
+
value: unknown;
|
|
34
|
+
}[]>;
|
|
35
|
+
listSwap: (list: string, id: string, expected: {
|
|
36
|
+
id: string;
|
|
37
|
+
score: number;
|
|
38
|
+
value: unknown;
|
|
39
|
+
} | undefined, next: {
|
|
40
|
+
id: string;
|
|
41
|
+
score: number;
|
|
42
|
+
value: unknown;
|
|
43
|
+
} | null) => Promise<boolean>;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Reads the list's entry `id`, hands it to `change`, and writes back what that answers (`null` takes it out) — unless
|
|
47
|
+
* somebody wrote it in between, and then it reads again and asks again. `undefined` writes nothing. Answers the entry
|
|
48
|
+
* written, `null` when it was taken out, `undefined` when nothing was written. Self-contained, as `changeKv` is: the
|
|
49
|
+
* sandbox's guest is handed this function's source.
|
|
50
|
+
*/
|
|
51
|
+
export declare const changeListEntry: (store: KvListChangeStore, list: string, id: string, change: (current: {
|
|
52
|
+
id: string;
|
|
53
|
+
score: number;
|
|
54
|
+
value: unknown;
|
|
55
|
+
} | undefined) => {
|
|
56
|
+
score: number;
|
|
57
|
+
value: unknown;
|
|
58
|
+
} | null | undefined | Promise<{
|
|
59
|
+
score: number;
|
|
60
|
+
value: unknown;
|
|
61
|
+
} | null | undefined>, { refusal }: {
|
|
62
|
+
refusal: (message: string) => Error;
|
|
63
|
+
}) => Promise<{
|
|
64
|
+
id: string;
|
|
65
|
+
score: number;
|
|
66
|
+
value: unknown;
|
|
67
|
+
} | null | undefined>;
|
|
@@ -19,5 +19,27 @@ var changeKv = async (store, key, change, { lifetime, refusal }) => {
|
|
|
19
19
|
}
|
|
20
20
|
throw refusal("Many people are changing this at once — try again in a moment");
|
|
21
21
|
};
|
|
22
|
+
/**
|
|
23
|
+
* Reads the list's entry `id`, hands it to `change`, and writes back what that answers (`null` takes it out) — unless
|
|
24
|
+
* somebody wrote it in between, and then it reads again and asks again. `undefined` writes nothing. Answers the entry
|
|
25
|
+
* written, `null` when it was taken out, `undefined` when nothing was written. Self-contained, as `changeKv` is: the
|
|
26
|
+
* sandbox's guest is handed this function's source.
|
|
27
|
+
*/
|
|
28
|
+
var changeListEntry = async (store, list, id, change, { refusal }) => {
|
|
29
|
+
const attempts = 25;
|
|
30
|
+
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
|
31
|
+
const current = (await store.listRange(list)).find((entry) => entry.id === id);
|
|
32
|
+
const next = await change(current);
|
|
33
|
+
if (next === void 0 || next === null && current === void 0) return;
|
|
34
|
+
const written = next === null ? null : {
|
|
35
|
+
id,
|
|
36
|
+
score: next.score,
|
|
37
|
+
value: next.value
|
|
38
|
+
};
|
|
39
|
+
if (await store.listSwap(list, id, current, written)) return written;
|
|
40
|
+
await new Promise((resolve) => setTimeout(resolve, Math.random() * Math.min(5 + attempt * 5, 50)));
|
|
41
|
+
}
|
|
42
|
+
throw refusal("Many people are changing this at once — try again in a moment");
|
|
43
|
+
};
|
|
22
44
|
//#endregion
|
|
23
|
-
export { changeKv };
|
|
45
|
+
export { changeKv, changeListEntry };
|
|
@@ -22,6 +22,10 @@ export declare const MAX_LIST_ENTRIES = 500;
|
|
|
22
22
|
* worth of entries, each an id and what the page shows — and past it, a list is being used as a table.
|
|
23
23
|
*/
|
|
24
24
|
export declare const MAX_LIST_BYTES = 128000;
|
|
25
|
+
/**
|
|
26
|
+
* Why an id is no list entry's — said with the id refused and the marks allowed quoted: "… : _ . @ -" ending the line
|
|
27
|
+
* read as cut short, and a run's id with a '|' took three rounds to find.
|
|
28
|
+
*/
|
|
25
29
|
export declare const listIdProblem: (id: unknown) => string | undefined;
|
|
26
30
|
/** The entries a stored list holds — none for a key that holds nothing, or something that is not a list. */
|
|
27
31
|
export declare const parseList: (raw: string | undefined) => KvListEntry[];
|
|
@@ -11,7 +11,11 @@ var MAX_LIST_ENTRIES = 500;
|
|
|
11
11
|
*/
|
|
12
12
|
var MAX_LIST_BYTES = 128e3;
|
|
13
13
|
var ID = /^[A-Za-z0-9:_.@-]{1,128}$/;
|
|
14
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Why an id is no list entry's — said with the id refused and the marks allowed quoted: "… : _ . @ -" ending the line
|
|
16
|
+
* read as cut short, and a run's id with a '|' took three rounds to find.
|
|
17
|
+
*/
|
|
18
|
+
var listIdProblem = (id) => typeof id === "string" && ID.test(id) ? void 0 : `A list entry’s id is 1 to 128 letters, digits and the marks ':' '_' '.' '@' '-': ${typeof id === "string" ? JSON.stringify(id.length > 60 ? `${id.slice(0, 60)}…` : id) : String(id)} is not`;
|
|
15
19
|
var isEntry = (value) => isRecord(value) && typeof value.id === "string" && typeof value.score === "number";
|
|
16
20
|
/** The entries a stored list holds — none for a key that holds nothing, or something that is not a list. */
|
|
17
21
|
var parseList = (raw) => {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ActionRefusal } from "./errors.js";
|
|
2
|
-
import { changeKv } from "./kvChange.js";
|
|
2
|
+
import { changeKv, changeListEntry } from "./kvChange.js";
|
|
3
3
|
import { listIdProblem, parseList, rangeOf, withEntry } from "./kvList.js";
|
|
4
4
|
/** Where a list lives, after the prefix: its own segment, so a list and a value of the same name never meet. */
|
|
5
5
|
var LIST_SEGMENT = "list:";
|
|
@@ -56,6 +56,20 @@ var createKvStore = (adapter, { prefix = "kv:" } = {}) => {
|
|
|
56
56
|
},
|
|
57
57
|
listRange: async (list, range) => rangeOf(parseList(await adapter.get(listKey(list))), range),
|
|
58
58
|
listRemove: (list, id) => changeList(list, (entries) => entries.some((entry) => entry.id === id) ? entries.filter((entry) => entry.id !== id) : void 0),
|
|
59
|
+
listSwap: async (list, id, expected, next) => {
|
|
60
|
+
if (next !== null) {
|
|
61
|
+
const problem = listIdProblem(next.id);
|
|
62
|
+
if (problem) throw new Error(problem);
|
|
63
|
+
if (next.id !== id || !Number.isFinite(next.score)) throw new Error("A list entry swapped in keeps its id, and its score is a number");
|
|
64
|
+
}
|
|
65
|
+
return changeList(list, (entries) => {
|
|
66
|
+
const current = entries.find((entry) => entry.id === id);
|
|
67
|
+
if (JSON.stringify(current) !== JSON.stringify(expected)) return;
|
|
68
|
+
if (next === null) return current === void 0 ? void 0 : entries.filter((entry) => entry.id !== id);
|
|
69
|
+
return withEntry(entries, next)?.kept;
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
listChange: (list, id, change) => changeListEntry(store, list, id, change, { refusal }),
|
|
59
73
|
change: (key, change, lifetime) => changeKv(store, key, change, {
|
|
60
74
|
lifetime,
|
|
61
75
|
refusal
|
|
@@ -20,6 +20,8 @@ var prefixKv = (store, prefix) => {
|
|
|
20
20
|
listPut: (list, entry, options) => store.listPut(scoped(list), entry, options),
|
|
21
21
|
listRange: (list, range) => store.listRange(scoped(list), range),
|
|
22
22
|
listRemove: (list, id) => store.listRemove(scoped(list), id),
|
|
23
|
+
listSwap: (list, id, expected, next) => store.listSwap(scoped(list), id, expected, next),
|
|
24
|
+
listChange: (list, id, change) => store.listChange(scoped(list), id, change),
|
|
23
25
|
change: (key, change, lifetime) => store.change(scoped(key), change, lifetime)
|
|
24
26
|
};
|
|
25
27
|
};
|
|
@@ -3,7 +3,9 @@ import { ActionRunError } from "./errors.js";
|
|
|
3
3
|
import onAbort from "../../../helpers/onAbort.js";
|
|
4
4
|
import { createRenderShare } from "./renderShare.js";
|
|
5
5
|
import { triggerCacheMs } from "@plitzi/sdk-shared/actions";
|
|
6
|
+
import { isRecord } from "@plitzi/sdk-shared/helpers";
|
|
6
7
|
import { createHash, randomUUID } from "node:crypto";
|
|
8
|
+
import { serverInputOf } from "@plitzi/sdk-shared/schema";
|
|
7
9
|
//#region src/modules/actions/runtime/renderResolver.ts
|
|
8
10
|
/**
|
|
9
11
|
* A render run that resolved nothing, carrying what it did.
|
|
@@ -13,9 +15,21 @@ import { createHash, randomUUID } from "node:crypto";
|
|
|
13
15
|
*/
|
|
14
16
|
var RenderRunFailure = class extends Error {
|
|
15
17
|
summary;
|
|
18
|
+
/**
|
|
19
|
+
* The action answered this visitor with a refusal (`ActionRefusal`): a room closed, data that is another's — the
|
|
20
|
+
* action doing its job, said as a warning, never an error with its stack (a run's log, a guest's every page).
|
|
21
|
+
*/
|
|
22
|
+
refused;
|
|
23
|
+
/**
|
|
24
|
+
* A refusal's own words, written for whoever called: what the element shows the visitor, alone — never the log's
|
|
25
|
+
* line around them ("Action … ended as failed, refused (code): …"), which a page had to split to show.
|
|
26
|
+
*/
|
|
27
|
+
said;
|
|
16
28
|
constructor(message, summary, options) {
|
|
17
29
|
super(message, options);
|
|
18
30
|
this.summary = summary;
|
|
31
|
+
this.refused = options?.refused === true;
|
|
32
|
+
if (options?.said !== void 0) this.said = options.said;
|
|
19
33
|
}
|
|
20
34
|
};
|
|
21
35
|
/**
|
|
@@ -41,7 +55,7 @@ var shareKey = (parts) => createHash("sha256").update(JSON.stringify(parts)).dig
|
|
|
41
55
|
*/
|
|
42
56
|
var createActionResolver = (lookups, module) => {
|
|
43
57
|
const share = createRenderShare();
|
|
44
|
-
return async ({ element, routeParams, queryParams, spaceId, environment, user, req, signal }) => {
|
|
58
|
+
return async ({ element, input: resolved, routeParams, queryParams, spaceId, environment, user, req, signal }) => {
|
|
45
59
|
/**
|
|
46
60
|
* Which version of the space is being rendered — environment AND revision from the same record.
|
|
47
61
|
*
|
|
@@ -53,7 +67,12 @@ var createActionResolver = (lookups, module) => {
|
|
|
53
67
|
environment: deployment?.environment ?? environment,
|
|
54
68
|
revision: deployment?.revision ?? 0
|
|
55
69
|
};
|
|
56
|
-
const { action: actionId
|
|
70
|
+
const { action: actionId } = element.attributes;
|
|
71
|
+
const bound = resolved ?? serverInputOf(element, {
|
|
72
|
+
routeParams,
|
|
73
|
+
queryParams
|
|
74
|
+
});
|
|
75
|
+
const input = isRecord(bound) ? bound : {};
|
|
57
76
|
if (!actionId) return;
|
|
58
77
|
const entry = await lookups.getAction(spaceId, actionId, at);
|
|
59
78
|
if (!entry) throw new Error(`Action "${actionId}" is not configured for space ${spaceId}`);
|
|
@@ -64,7 +83,8 @@ var createActionResolver = (lookups, module) => {
|
|
|
64
83
|
...req.ctx.rscParams
|
|
65
84
|
};
|
|
66
85
|
const trigger = findTriggerNode(entry.document.nodes, "render");
|
|
67
|
-
|
|
86
|
+
if (!trigger) throw new Error(`Action "${actionId}" has no "render" trigger: a server provider reads an action through one — give it a render trigger (its access says who may read it), as a "call" trigger is a page's to run`);
|
|
87
|
+
const ttlMs = triggerCacheMs(triggerParams(trigger));
|
|
68
88
|
const key = shareKey([
|
|
69
89
|
spaceId,
|
|
70
90
|
at.environment,
|
|
@@ -138,7 +158,13 @@ var createActionResolver = (lookups, module) => {
|
|
|
138
158
|
status: result.status,
|
|
139
159
|
steps: result.steps
|
|
140
160
|
});
|
|
141
|
-
if (result.status !== "completed")
|
|
161
|
+
if (result.status !== "completed") {
|
|
162
|
+
const said = result.error ?? result.steps.find((step) => step.status === "failed")?.error;
|
|
163
|
+
throw new RenderRunFailure(`Action "${actionId}" ended as ${result.status}${result.code ? `, refused (${result.code})` : ""}${said ? `: ${said}` : ""}`, summary, result.error === void 0 ? { refused: false } : {
|
|
164
|
+
refused: true,
|
|
165
|
+
said: result.error
|
|
166
|
+
});
|
|
167
|
+
}
|
|
142
168
|
return {
|
|
143
169
|
output: result.output,
|
|
144
170
|
summary
|
|
@@ -147,7 +173,7 @@ var createActionResolver = (lookups, module) => {
|
|
|
147
173
|
if (error instanceof RenderRunFailure) throw error;
|
|
148
174
|
if (error instanceof ActionRunError) {
|
|
149
175
|
const status = error.reason === "aborted" ? "aborted" : "failed";
|
|
150
|
-
throw new RenderRunFailure(`Action "${actionId}" refused this render: ${error.reason}`, summaryOf({
|
|
176
|
+
throw new RenderRunFailure(`Action "${actionId}" refused this render: ${error.reason}${error.message && error.message !== error.reason ? ` — ${error.message}` : ""}`, summaryOf({
|
|
151
177
|
status,
|
|
152
178
|
steps: error.steps ?? [],
|
|
153
179
|
error: error.message
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { serverLog } from "../../../helpers/serverLog.js";
|
|
2
2
|
import "../jobs/schedules.js";
|
|
3
|
-
import { ActionRefusal, ActionRunError, isActionRefusal } from "./errors.js";
|
|
3
|
+
import { ActionRefusal, ActionRunError, isActionRefusal, refusalCodeOf } from "./errors.js";
|
|
4
|
+
import { createMemoryKv } from "./memoryKv.js";
|
|
4
5
|
import { runCancelKey } from "./guards.js";
|
|
5
6
|
import { createKvStore } from "./kvStore.js";
|
|
6
7
|
import { resolveLimits } from "./limits.js";
|
|
7
|
-
import { createMemoryKv } from "./memoryKv.js";
|
|
8
8
|
import { createEmailSender } from "./email.js";
|
|
9
9
|
import { flagsForRun } from "./flags.js";
|
|
10
10
|
import { createRedactor, projectUser } from "./scope.js";
|
|
@@ -157,6 +157,10 @@ var MAX_LOG_LINE = 1e3;
|
|
|
157
157
|
*/
|
|
158
158
|
var asDeclared = (type, value) => {
|
|
159
159
|
if (typeof value === "number" && (type === "text" || type === "textarea")) return String(value);
|
|
160
|
+
if (type === "list") {
|
|
161
|
+
if (Array.isArray(value)) return value.flatMap((item) => typeof item === "string" || typeof item === "number" || typeof item === "boolean" ? [String(item)] : []);
|
|
162
|
+
return typeof value === "string" ? value.split(/[\n,]/).map((item) => item.trim()).filter((item) => item !== "") : value;
|
|
163
|
+
}
|
|
160
164
|
if (typeof value !== "string") return value;
|
|
161
165
|
if (type === "number") {
|
|
162
166
|
const parsed = Number(value.trim());
|
|
@@ -171,11 +175,12 @@ var asDeclared = (type, value) => {
|
|
|
171
175
|
*/
|
|
172
176
|
var paramProblem = (key, param, value) => {
|
|
173
177
|
const name = param.label ?? key;
|
|
174
|
-
if (value === void 0 || value === null || typeof value === "string" && value.trim() === "") {
|
|
175
|
-
const defaulted = param.defaultValue !== void 0 && param.defaultValue !== "";
|
|
178
|
+
if (value === void 0 || value === null || typeof value === "string" && value.trim() === "" || Array.isArray(value) && value.length === 0) {
|
|
179
|
+
const defaulted = param.defaultValue !== void 0 && param.defaultValue !== "" && !(Array.isArray(param.defaultValue) && param.defaultValue.length === 0);
|
|
176
180
|
return param.required && !defaulted ? `${name} is required` : void 0;
|
|
177
181
|
}
|
|
178
182
|
if ((param.type === "text" || param.type === "textarea") && param.maxLength !== void 0) return typeof value === "string" && value.length > param.maxLength ? `${name} is longer than ${String(param.maxLength)} characters` : void 0;
|
|
183
|
+
if (param.type === "list" && param.maxItems !== void 0 && Array.isArray(value) && value.length > param.maxItems) return `${name} takes at most ${String(param.maxItems)}`;
|
|
179
184
|
if (param.type === "number" && (param.min !== void 0 || param.max !== void 0)) {
|
|
180
185
|
if (typeof value !== "number") return `${name} is not a number`;
|
|
181
186
|
if (param.min !== void 0 && value < param.min) return `${name} is at least ${String(param.min)}`;
|
|
@@ -485,6 +490,7 @@ var createActionRunner = (config, registryFor, baseFetch = fetch, jobsQueue = ()
|
|
|
485
490
|
let fatal;
|
|
486
491
|
/** Set when a step refused with a reason written for the caller — the one failure a result may say the why of. */
|
|
487
492
|
let refusal;
|
|
493
|
+
let refusalCode;
|
|
488
494
|
try {
|
|
489
495
|
await Promise.race([runFlow(), deadline.promise]);
|
|
490
496
|
} catch (error) {
|
|
@@ -513,7 +519,10 @@ var createActionRunner = (config, registryFor, baseFetch = fetch, jobsQueue = ()
|
|
|
513
519
|
});
|
|
514
520
|
} else {
|
|
515
521
|
failure = error instanceof Error ? error.message : String(error);
|
|
516
|
-
if (isActionRefusal(error))
|
|
522
|
+
if (isActionRefusal(error)) {
|
|
523
|
+
refusal = redact(error.message);
|
|
524
|
+
refusalCode = refusalCodeOf(error);
|
|
525
|
+
}
|
|
517
526
|
trace.push({
|
|
518
527
|
node: errorNode("error"),
|
|
519
528
|
status: "failed",
|
|
@@ -576,7 +585,8 @@ var createActionRunner = (config, registryFor, baseFetch = fetch, jobsQueue = ()
|
|
|
576
585
|
output: redact(output),
|
|
577
586
|
trace: [...trace],
|
|
578
587
|
steps: [...steps],
|
|
579
|
-
...refusal === void 0 ? {} : { error: refusal }
|
|
588
|
+
...refusal === void 0 ? {} : { error: refusal },
|
|
589
|
+
...refusalCode === void 0 ? {} : { code: refusalCode }
|
|
580
590
|
};
|
|
581
591
|
};
|
|
582
592
|
const taskContext = (contextRequest, signal, lineage) => taskContextFor(config, {
|
|
@@ -33,6 +33,12 @@ var kindOf = (value) => {
|
|
|
33
33
|
var misfit = (key, type, value) => {
|
|
34
34
|
return `${key} (${type}, got ${kindOf(value)}${type === "text" && typeof value === "object" && value !== null ? "; declare the field `json` to take it" : ""})`;
|
|
35
35
|
};
|
|
36
|
+
/** Why a value of its type is outside its field's limits — `maxLength`, `min`, `max` — or nothing. */
|
|
37
|
+
var outOfLimits = (key, field, value) => {
|
|
38
|
+
if (typeof value === "string" && field.maxLength !== void 0 && value.length > field.maxLength) return `${key} (text, longer than ${String(field.maxLength)} characters)`;
|
|
39
|
+
if (typeof value === "number" && field.min !== void 0 && value < field.min) return `${key} (number, at least ${String(field.min)})`;
|
|
40
|
+
if (typeof value === "number" && field.max !== void 0 && value > field.max) return `${key} (number, at most ${String(field.max)})`;
|
|
41
|
+
};
|
|
36
42
|
/**
|
|
37
43
|
* Coerces a value bag against declared fields.
|
|
38
44
|
*
|
|
@@ -55,6 +61,11 @@ var applyFields = (fields, raw) => {
|
|
|
55
61
|
invalid.push(misfit(key, field.type, provided));
|
|
56
62
|
return;
|
|
57
63
|
}
|
|
64
|
+
const outside = outOfLimits(key, field, coerced);
|
|
65
|
+
if (outside !== void 0) {
|
|
66
|
+
invalid.push(outside);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
58
69
|
values[key] = coerced;
|
|
59
70
|
});
|
|
60
71
|
return {
|
|
@@ -1,38 +1,10 @@
|
|
|
1
|
+
import { cappedResponse } from "../../../helpers/outboundGuard.js";
|
|
1
2
|
import { ActionRunError } from "./errors.js";
|
|
2
3
|
import { namespaceKv } from "./namespaceKv.js";
|
|
3
4
|
import { projectDataReader } from "./projectData.js";
|
|
4
5
|
import { laterFor } from "../jobs/later.js";
|
|
5
6
|
//#region src/modules/actions/runtime/taskContext.ts
|
|
6
7
|
/**
|
|
7
|
-
* Holds an answer to what the run is allowed to carry.
|
|
8
|
-
*
|
|
9
|
-
* Two checks rather than one, because a body arrives in two ways. `Content-Length` is refused before a byte is
|
|
10
|
-
* read, which is the cheap half; a chunked answer that declares nothing is counted AS it streams and errored the
|
|
11
|
-
* moment it goes over — so the ceiling holds for a backend that lies about its size or never states one.
|
|
12
|
-
*
|
|
13
|
-
* The cap is on one response and not on the run: it exists so that a single answer cannot be unbounded, which is
|
|
14
|
-
* a different failure from a flow that makes many small calls (that is `maxRequests`).
|
|
15
|
-
*/
|
|
16
|
-
var capped = (response, maxBytes) => {
|
|
17
|
-
const declared = Number(response.headers.get("content-length") ?? "");
|
|
18
|
-
if (Number.isFinite(declared) && declared > maxBytes) throw new ActionRunError("over_capacity", `Response is larger than the ${maxBytes} byte budget`);
|
|
19
|
-
if (!response.body) return response;
|
|
20
|
-
let seen = 0;
|
|
21
|
-
const counted = response.body.pipeThrough(new TransformStream({ transform: (chunk, controller) => {
|
|
22
|
-
seen += chunk.byteLength;
|
|
23
|
-
if (seen > maxBytes) {
|
|
24
|
-
controller.error(new ActionRunError("over_capacity", `Response exceeded the ${maxBytes} byte budget`));
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
controller.enqueue(chunk);
|
|
28
|
-
} }));
|
|
29
|
-
return new Response(counted, {
|
|
30
|
-
status: response.status,
|
|
31
|
-
statusText: response.statusText,
|
|
32
|
-
headers: response.headers
|
|
33
|
-
});
|
|
34
|
-
};
|
|
35
|
-
/**
|
|
36
8
|
* Counts outbound calls, refuses past the budget, caps what one answer may carry back, and stamps the run's
|
|
37
9
|
* lineage on every one of them.
|
|
38
10
|
*
|
|
@@ -55,11 +27,11 @@ var createRunFetch = (base, signal, limits, lineage, onBudgetSpent) => {
|
|
|
55
27
|
}
|
|
56
28
|
const headers = new Headers(init?.headers);
|
|
57
29
|
headers.set("X-Plitzi-Action-Lineage", lineage.join(","));
|
|
58
|
-
return
|
|
30
|
+
return cappedResponse(await base(input, {
|
|
59
31
|
...init,
|
|
60
32
|
headers,
|
|
61
33
|
signal: init?.signal ?? signal
|
|
62
|
-
}), limits.maxResponseBytes);
|
|
34
|
+
}), limits.maxResponseBytes, (message) => new ActionRunError("over_capacity", message));
|
|
63
35
|
};
|
|
64
36
|
};
|
|
65
37
|
/**
|
|
@@ -71,6 +43,7 @@ var taskContextFor = (config, { kv, email, redactor, signing, later, jobs }, req
|
|
|
71
43
|
const { realtime } = config;
|
|
72
44
|
const oauthCallbackOrigin = config.functions?.oauth?.callbackOrigin;
|
|
73
45
|
const readData = projectDataReader(config.lookups.getData, request.spaceId, request.at);
|
|
46
|
+
const { getVisitor } = config.lookups;
|
|
74
47
|
return (scope) => ({
|
|
75
48
|
runId: request.runId,
|
|
76
49
|
...request.origin ? { origin: request.origin } : {},
|
|
@@ -106,11 +79,13 @@ var taskContextFor = (config, { kv, email, redactor, signing, later, jobs }, req
|
|
|
106
79
|
};
|
|
107
80
|
},
|
|
108
81
|
fetch: runFetch,
|
|
82
|
+
...config.outbound ? { outbound: config.outbound } : {},
|
|
109
83
|
kv: namespaceKv(kv, request.spaceId),
|
|
110
84
|
dbDrivers: config.dbDrivers ?? [],
|
|
111
85
|
email,
|
|
112
86
|
emit: (chunk) => request.emit?.(redactor.redact(chunk)),
|
|
113
87
|
data: readData,
|
|
88
|
+
...getVisitor ? { visitor: (who) => getVisitor(request.spaceId, request.environment, who) } : {},
|
|
114
89
|
...signing?.({
|
|
115
90
|
spaceId: request.spaceId,
|
|
116
91
|
environment: request.environment
|
|
@@ -138,7 +113,11 @@ var taskContextFor = (config, { kv, email, redactor, signing, later, jobs }, req
|
|
|
138
113
|
revoke: (topic, grant) => realtime.revoke({
|
|
139
114
|
spaceId: request.spaceId,
|
|
140
115
|
environment: request.environment
|
|
141
|
-
}, topic, grant)
|
|
116
|
+
}, topic, grant),
|
|
117
|
+
...realtime.presence ? { presence: (topic) => realtime.presence?.({
|
|
118
|
+
spaceId: request.spaceId,
|
|
119
|
+
environment: request.environment
|
|
120
|
+
}, topic) ?? Promise.reject(/* @__PURE__ */ new Error("This server counts nobody on its channels")) } : {}
|
|
142
121
|
} : {}
|
|
143
122
|
});
|
|
144
123
|
};
|
|
@@ -2,6 +2,7 @@ var authTasks = [{
|
|
|
2
2
|
namespace: "auth",
|
|
3
3
|
action: "currentUser",
|
|
4
4
|
title: "Current User",
|
|
5
|
+
description: "Who is calling: answers `signedIn` and, when signed in, `id`, `username`, `email`, `verified`, `permissions` and `roles`.",
|
|
5
6
|
params: {},
|
|
6
7
|
run: (_params, ctx) => {
|
|
7
8
|
const { user } = ctx;
|
|
@@ -21,6 +22,7 @@ var authTasks = [{
|
|
|
21
22
|
namespace: "auth",
|
|
22
23
|
action: "requireRole",
|
|
23
24
|
title: "Require Permissions",
|
|
25
|
+
description: "Fails the run unless the caller holds every permission named; answers `granted` when they do.",
|
|
24
26
|
params: { permissions: {
|
|
25
27
|
type: "text",
|
|
26
28
|
canBind: true,
|