@plitzi/sdk-server 0.38.19 → 0.38.21
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 +178 -0
- package/README.md +2 -2
- package/dist/actions.d.ts +6 -0
- package/dist/actions.js +2 -1
- package/dist/core/auth/createAuth.d.ts +1 -1
- package/dist/core/auth/csrf.js +13 -8
- package/dist/core/auth/tokens.d.ts +1 -1
- package/dist/core/configSeam.js +2 -2
- package/dist/core/services/functionRoutes.js +8 -2
- package/dist/core/services/realtime.js +5 -2
- package/dist/functions-api.d.ts +228 -0
- package/dist/functions.d.ts +4 -2
- package/dist/functions.js +2 -1
- package/dist/helpers/serverLog.d.ts +1 -1
- package/dist/helpers/serverLog.js +1 -1
- package/dist/hosts/project/plugins.js +20 -3
- package/dist/modules/actions/index.js +16 -3
- package/dist/modules/actions/jobs/index.d.ts +4 -3
- package/dist/modules/actions/jobs/index.js +2 -1
- package/dist/modules/actions/jobs/later.d.ts +2 -2
- package/dist/modules/actions/jobs/later.js +5 -2
- package/dist/modules/actions/jobs/memoryQueue.d.ts +4 -4
- package/dist/modules/actions/jobs/memoryQueue.js +69 -25
- package/dist/modules/actions/jobs/scheduler.d.ts +2 -2
- package/dist/modules/actions/jobs/scheduler.js +5 -2
- package/dist/modules/actions/jobs/worker.d.ts +6 -3
- package/dist/modules/actions/jobs/worker.js +104 -10
- package/dist/modules/actions/runtime/runAction.d.ts +5 -2
- package/dist/modules/actions/runtime/runAction.js +5 -3
- package/dist/modules/actions/runtime/signing.d.ts +7 -0
- package/dist/modules/actions/runtime/signing.js +23 -1
- package/dist/modules/actions/runtime/taskContext.d.ts +12 -3
- package/dist/modules/actions/runtime/taskContext.js +8 -1
- package/dist/modules/actions/transport/callHandler.js +3 -0
- package/dist/modules/actions/types.d.ts +29 -4
- package/dist/modules/functions/capabilities.js +107 -0
- package/dist/modules/functions/config.d.ts +9 -0
- package/dist/modules/functions/context.d.ts +2 -1
- package/dist/modules/functions/context.js +21 -3
- package/dist/modules/functions/contract.d.ts +129 -1
- package/dist/modules/functions/driver.js +128 -2
- package/dist/modules/functions/jobControl.d.ts +21 -0
- package/dist/modules/functions/jobControl.js +43 -0
- package/dist/modules/functions/jobs.d.ts +98 -0
- package/dist/modules/functions/jobs.js +438 -0
- package/dist/modules/functions/manifest.js +117 -2
- package/dist/modules/functions/native.js +2 -1
- package/dist/modules/functions/oauth.d.ts +31 -0
- package/dist/modules/functions/oauth.js +326 -0
- package/dist/modules/functions/protocol.d.ts +88 -3
- package/dist/modules/functions/protocol.js +2 -1
- package/dist/modules/functions/routes.d.ts +11 -1
- package/dist/modules/functions/routes.js +71 -30
- package/dist/modules/functions/runner/messages.d.ts +0 -1
- package/dist/modules/functions/runner/messages.js +40 -2
- package/dist/modules/functions/runner/remote.js +58 -3
- package/dist/modules/functions/runner/service.js +53 -7
- package/dist/modules/functions/sandbox/isolate.js +5 -3
- package/dist/modules/functions/space.js +4 -4
- package/dist/modules/mongo/jobQueue.d.ts +3 -3
- package/dist/modules/mongo/jobQueue.js +137 -26
- package/dist/modules/mysql/jobs/jobQueue.d.ts +3 -3
- package/dist/modules/mysql/jobs/jobQueue.js +97 -33
- package/dist/modules/mysql/jobs/schema.js +38 -11
- package/dist/modules/realtime/handlers.d.ts +5 -2
- package/dist/modules/realtime/handlers.js +15 -4
- package/dist/modules/realtime/index.d.ts +11 -1
- package/dist/modules/realtime/index.js +8 -1
- package/dist/modules/realtime/jobs.d.ts +20 -0
- package/dist/modules/realtime/jobs.js +41 -0
- package/dist/modules/realtime/retained.d.ts +3 -2
- package/dist/modules/realtime/retained.js +2 -2
- package/dist/modules/realtime/socket.d.ts +4 -1
- package/dist/modules/realtime/socket.js +2 -2
- package/dist/modules/rsc/resolveRscData.js +2 -1
- package/dist/modules/runtime/host.js +1 -1
- package/dist/modules/runtime/runner.d.ts +1 -1
- package/dist/modules/runtime/runner.js +12 -2
- package/dist/modules/runtime/stages.js +1 -1
- package/dist/modules/ssr/applySSRResult.d.ts +5 -0
- package/dist/modules/ssr/applySSRResult.js +8 -1
- package/dist/modules/ssr/buildBody.js +2 -1
- package/dist/modules/ssr/prepareRender.d.ts +3 -0
- package/dist/modules/ssr/prepareRender.js +17 -5
- package/dist/modules/ssr/spaceDocument.d.ts +2 -1
- package/dist/modules/ssr/spaceDocument.js +14 -5
- package/dist/modules/ssr/streamBody.js +2 -1
- package/package.json +6 -3
- package/skills/plitzi-authoring/reference/auth.md +6 -0
- package/skills/plitzi-authoring/reference/authoring-errors.md +1 -0
- package/skills/plitzi-authoring/reference/plugin-reach.md +3 -0
- package/skills/plitzi-authoring/reference/realtime.md +4 -0
- package/dist/adapters/cloudAdapters.test.d.ts +0 -1
- package/dist/adapters/jsonAdapters.test.d.ts +0 -1
- package/dist/adapters/space/space.test.d.ts +0 -1
- package/dist/core/auth/api.test.d.ts +0 -1
- package/dist/core/auth/authorize.test.d.ts +0 -1
- package/dist/core/auth/createAuth.test.d.ts +0 -1
- package/dist/core/auth/csrf.test.d.ts +0 -1
- package/dist/core/auth/domains.test.d.ts +0 -1
- package/dist/core/auth/identity.test.d.ts +0 -1
- package/dist/core/auth/lifecycle.test.d.ts +0 -1
- package/dist/core/auth/oauth/oauth.test.d.ts +0 -1
- package/dist/core/auth/redirects.test.d.ts +0 -1
- package/dist/core/auth/routes.test.d.ts +0 -1
- package/dist/core/auth/session.test.d.ts +0 -1
- package/dist/core/auth/spaceTokens.test.d.ts +0 -1
- package/dist/core/auth/throttle.test.d.ts +0 -1
- package/dist/core/auth/tokens.test.d.ts +0 -1
- package/dist/core/auth/totp.test.d.ts +0 -1
- package/dist/core/configSeam.test.d.ts +0 -1
- package/dist/core/createServer.actions.test.d.ts +0 -1
- package/dist/core/createServer.functions.test.d.ts +0 -1
- package/dist/core/createServer.livePages.test.d.ts +0 -1
- package/dist/core/createServer.notFound.test.d.ts +0 -1
- package/dist/core/createServer.publicData.test.d.ts +0 -1
- package/dist/core/createServer.test.d.ts +0 -1
- package/dist/core/fontStore.test.d.ts +0 -1
- package/dist/core/freePort.test.d.ts +0 -1
- package/dist/core/handlers/csrfMiddleware.test.d.ts +0 -1
- package/dist/core/handlers/handlers.test.d.ts +0 -1
- package/dist/core/handlers/socialRouteHandlers.test.d.ts +0 -1
- package/dist/core/http/dispatcher.test.d.ts +0 -1
- package/dist/core/http/stages/crossSiteWrites.test.d.ts +0 -1
- package/dist/core/http/stages/exchangeRoute.test.d.ts +0 -1
- package/dist/core/http/stages/images.test.d.ts +0 -1
- package/dist/core/http/stages/middlewares.test.d.ts +0 -1
- package/dist/core/http/stages/sessionRenewal.test.d.ts +0 -1
- package/dist/core/http/stages/signInRoute.test.d.ts +0 -1
- package/dist/core/oauth/authorize.test.d.ts +0 -1
- package/dist/core/oauth/register.test.d.ts +0 -1
- package/dist/core/oauth/token.test.d.ts +0 -1
- package/dist/core/requestParser.test.d.ts +0 -1
- package/dist/core/server/baseServer.test.d.ts +0 -1
- package/dist/core/server/closeOnSignals.test.d.ts +0 -1
- package/dist/core/server/drain.test.d.ts +0 -1
- package/dist/core/server/fleet/channel.test.d.ts +0 -1
- package/dist/core/server/fleet/fixtures/fleetServer.d.ts +0 -1
- package/dist/core/server/fleet/fleet.integration.test.d.ts +0 -1
- package/dist/core/server/fleet/relay.test.d.ts +0 -1
- package/dist/core/server/fleet/role.test.d.ts +0 -1
- package/dist/core/server/fleet/supervisor.test.d.ts +0 -1
- package/dist/core/services/resolve.test.d.ts +0 -1
- package/dist/core/staticFiles.test.d.ts +0 -1
- package/dist/env.test.d.ts +0 -1
- package/dist/helpers/atomicFile.test.d.ts +0 -1
- package/dist/helpers/buildResponseHelpers.test.d.ts +0 -1
- package/dist/helpers/buildServerInfo.test.d.ts +0 -1
- package/dist/helpers/cache/CompressedFileCache.test.d.ts +0 -1
- package/dist/helpers/cache/cacheManager.test.d.ts +0 -1
- package/dist/helpers/cache/keys.test.d.ts +0 -1
- package/dist/helpers/compress.test.d.ts +0 -1
- package/dist/helpers/hydrationPayload.test.d.ts +0 -1
- package/dist/helpers/offlineDataLoader.test.d.ts +0 -1
- package/dist/helpers/onAbort.test.d.ts +0 -1
- package/dist/helpers/outboundGuard.test.d.ts +0 -1
- package/dist/helpers/reportReactBuild.test.d.ts +0 -1
- package/dist/helpers/resolveDebugMode.test.d.ts +0 -1
- package/dist/helpers/serverLog.test.d.ts +0 -1
- package/dist/hosts/authoringHost/index.test.d.ts +0 -1
- package/dist/hosts/project/serveProject.test.d.ts +0 -1
- package/dist/layers.test.d.ts +0 -1
- package/dist/middlewares/spaceDeployment.test.d.ts +0 -1
- package/dist/modules/actions/connectorWrite.test.d.ts +0 -1
- package/dist/modules/actions/jobs/failureStreak.test.d.ts +0 -1
- package/dist/modules/actions/jobs/later.test.d.ts +0 -1
- package/dist/modules/actions/jobs/memoryStores.test.d.ts +0 -1
- package/dist/modules/actions/jobs/scheduler.test.d.ts +0 -1
- package/dist/modules/actions/jobs/testing/jobQueueContract.d.ts +0 -25
- package/dist/modules/actions/jobs/worker.test.d.ts +0 -1
- package/dist/modules/actions/moduleFor.test.d.ts +0 -1
- package/dist/modules/actions/runtime/check.test.d.ts +0 -1
- package/dist/modules/actions/runtime/credentials.test.d.ts +0 -1
- package/dist/modules/actions/runtime/email.test.d.ts +0 -1
- package/dist/modules/actions/runtime/fileKv.test.d.ts +0 -1
- package/dist/modules/actions/runtime/flags.test.d.ts +0 -1
- package/dist/modules/actions/runtime/guards.test.d.ts +0 -1
- package/dist/modules/actions/runtime/kvStore.test.d.ts +0 -1
- package/dist/modules/actions/runtime/namespaceKv.test.d.ts +0 -1
- package/dist/modules/actions/runtime/redisKv.integration.test.d.ts +0 -1
- package/dist/modules/actions/runtime/renderResolver.test.d.ts +0 -1
- package/dist/modules/actions/runtime/renderShare.test.d.ts +0 -1
- package/dist/modules/actions/runtime/runAction.test.d.ts +0 -1
- package/dist/modules/actions/runtime/runLogger.test.d.ts +0 -1
- package/dist/modules/actions/runtime/signing.test.d.ts +0 -1
- package/dist/modules/actions/taskCatalog.test.d.ts +0 -1
- package/dist/modules/actions/tasks/db.test.d.ts +0 -1
- package/dist/modules/actions/tasks/email.test.d.ts +0 -1
- package/dist/modules/actions/tasks/http.test.d.ts +0 -1
- package/dist/modules/actions/tasks/kv.test.d.ts +0 -1
- package/dist/modules/actions/transport/callHandler.test.d.ts +0 -1
- package/dist/modules/actions/transport/webhookHandler.test.d.ts +0 -1
- package/dist/modules/connectors/engine.test.d.ts +0 -1
- package/dist/modules/connectors/projection.test.d.ts +0 -1
- package/dist/modules/connectors/resolver.test.d.ts +0 -1
- package/dist/modules/functions/build.test.d.ts +0 -1
- package/dist/modules/functions/load.test.d.ts +0 -1
- package/dist/modules/functions/local.test.d.ts +0 -1
- package/dist/modules/functions/manifest.test.d.ts +0 -1
- package/dist/modules/functions/native.test.d.ts +0 -1
- package/dist/modules/functions/plugins.test.d.ts +0 -1
- package/dist/modules/functions/routes.test.d.ts +0 -1
- package/dist/modules/functions/runner/remote.test.d.ts +0 -1
- package/dist/modules/functions/sandbox/isolate.test.d.ts +0 -1
- package/dist/modules/functions/testing/tasksOf.d.ts +0 -4
- package/dist/modules/images/imageProxy.test.d.ts +0 -1
- package/dist/modules/images/transform.test.d.ts +0 -1
- package/dist/modules/mongo/stores.integration.test.d.ts +0 -1
- package/dist/modules/mysql/accounts.test.d.ts +0 -1
- package/dist/modules/mysql/jobs/stores.integration.test.d.ts +0 -1
- package/dist/modules/mysql/schema.test.d.ts +0 -1
- package/dist/modules/mysql/store.integration.test.d.ts +0 -1
- package/dist/modules/realtime/realtime.test.d.ts +0 -1
- package/dist/modules/realtime/redisPubSub.test.d.ts +0 -1
- package/dist/modules/realtime/retained.test.d.ts +0 -1
- package/dist/modules/realtime/socket.test.d.ts +0 -1
- package/dist/modules/rsc/connectorRscData.test.d.ts +0 -1
- package/dist/modules/rsc/handler.test.d.ts +0 -1
- package/dist/modules/rsc/publicFileResolver.test.d.ts +0 -1
- package/dist/modules/rsc/resolveRscData.test.d.ts +0 -1
- package/dist/modules/runtime/elsewhere.test.d.ts +0 -1
- package/dist/modules/runtime/entry.test.d.ts +0 -1
- package/dist/modules/runtime/inside.test.d.ts +0 -1
- package/dist/modules/runtime/ownServer.test.d.ts +0 -1
- package/dist/modules/runtime/runtime.test.d.ts +0 -1
- package/dist/modules/sqlite/kv.test.d.ts +0 -1
- package/dist/modules/ssr/draftSession.test.d.ts +0 -1
- package/dist/modules/ssr/loadPluginComponents.test.d.ts +0 -1
- package/dist/modules/ssr/metering.test.d.ts +0 -1
- package/dist/modules/ssr/prepareRender.test.d.ts +0 -1
- package/dist/modules/ssr/redirect.test.d.ts +0 -1
- package/dist/modules/ssr/registerExternalPlugins.test.d.ts +0 -1
- package/dist/modules/ssr/reportMissingPlugins.test.d.ts +0 -1
- package/dist/modules/ssr/resolvePageSeo.test.d.ts +0 -1
- package/dist/modules/ssr/spaceDocument.test.d.ts +0 -1
- package/dist/modules/ssr/template.test.d.ts +0 -1
- package/dist/modules/ssr/testing/offlineData.d.ts +0 -5
- package/dist/modules/ssr/vendor.test.d.ts +0 -1
- package/dist/plugins/compile.test.d.ts +0 -1
- package/dist/plugins/copy.test.d.ts +0 -1
- package/dist/plugins/manager.test.d.ts +0 -1
- package/dist/plugins/validate.test.d.ts +0 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { TaskContextRequest } from './taskContext';
|
|
2
|
+
import { FunctionJobsService } from '../../functions/jobs';
|
|
2
3
|
import { ActionRunRequest, ActionRunResult, ActionsConfig, ActionTaskContext, ActionTaskRegistry } from '../types';
|
|
3
|
-
import {
|
|
4
|
+
import { JobQueue } from '@plitzi/sdk-shared';
|
|
4
5
|
export type ActionRunner = {
|
|
5
6
|
runAction: (request: ActionRunRequest) => Promise<ActionRunResult>;
|
|
6
7
|
};
|
|
@@ -18,6 +19,8 @@ export type TaskContextSource = (request: TaskContextRequest, signal: AbortSigna
|
|
|
18
19
|
*/
|
|
19
20
|
export declare const createActionRunner: (config: ActionsConfig, registryFor: (request: ActionRunRequest) => ActionTaskRegistry | Promise<ActionTaskRegistry>, baseFetch?: typeof fetch,
|
|
20
21
|
/** The queue runs set for later go into, read when asked: it is made after the runner, and a server may run none. */
|
|
21
|
-
jobsQueue?: () =>
|
|
22
|
+
jobsQueue?: () => JobQueue | undefined,
|
|
23
|
+
/** The space's jobs, for a function's `ctx.jobs` — made beside the runner, and reading back its `taskContext`. */
|
|
24
|
+
functionJobs?: FunctionJobsService) => ActionRunner & {
|
|
22
25
|
taskContext: TaskContextSource;
|
|
23
26
|
};
|
|
@@ -222,7 +222,7 @@ var runNode = async (node, scope, registry, buildContext) => {
|
|
|
222
222
|
* input contract — lives HERE rather than in a transport, because a deployment mounting its own trigger calls this
|
|
223
223
|
* directly. A check in the endpoint would be a check a custom trigger silently skips.
|
|
224
224
|
*/
|
|
225
|
-
var createActionRunner = (config, registryFor, baseFetch = fetch, jobsQueue = () => void 0) => {
|
|
225
|
+
var createActionRunner = (config, registryFor, baseFetch = fetch, jobsQueue = () => void 0, functionJobs) => {
|
|
226
226
|
const kv = createKvStore(config.kv ?? createMemoryKv());
|
|
227
227
|
const emailSender = createEmailSender({
|
|
228
228
|
...config.email,
|
|
@@ -270,7 +270,8 @@ var createActionRunner = (config, registryFor, baseFetch = fetch, jobsQueue = ()
|
|
|
270
270
|
email: emailSender,
|
|
271
271
|
redactor,
|
|
272
272
|
signing,
|
|
273
|
-
later
|
|
273
|
+
later,
|
|
274
|
+
...functionJobs ? { jobs: functionJobs } : {}
|
|
274
275
|
}, request, signal, runFetch);
|
|
275
276
|
/** The request budget ran out: said on the server's log at once, and in the step that hit it. */
|
|
276
277
|
let budgetSpent;
|
|
@@ -583,7 +584,8 @@ var createActionRunner = (config, registryFor, baseFetch = fetch, jobsQueue = ()
|
|
|
583
584
|
email: emailSender,
|
|
584
585
|
redactor: createRedactor(),
|
|
585
586
|
signing,
|
|
586
|
-
later
|
|
587
|
+
later,
|
|
588
|
+
...functionJobs ? { jobs: functionJobs } : {}
|
|
587
589
|
}, contextRequest, signal, createRunFetch(baseFetch, signal, resolveLimits(config.limits, void 0), lineage))({});
|
|
588
590
|
return {
|
|
589
591
|
runAction,
|
|
@@ -4,6 +4,13 @@ export type SpaceSigning = {
|
|
|
4
4
|
sign: (value: string) => Promise<string>;
|
|
5
5
|
/** Whether `signature` is what `sign` answers for `value` — compared in constant time. */
|
|
6
6
|
verify: (value: string, signature: string) => Promise<boolean>;
|
|
7
|
+
/**
|
|
8
|
+
* `value` encrypted (AES-256-GCM) with a key of the space's own that no signature reaches: what the platform keeps for
|
|
9
|
+
* a space and its code must not read — an OAuth token in the space's `kv`. base64url, a fresh IV each time.
|
|
10
|
+
*/
|
|
11
|
+
seal: (value: string) => string;
|
|
12
|
+
/** What `seal` encrypted — `undefined` for anything it did not, or that was changed since. */
|
|
13
|
+
open: (sealed: string) => string | undefined;
|
|
7
14
|
};
|
|
8
15
|
/** A signing secret is what every link and key a space hands out stands on: as long as a 256-bit key written out. */
|
|
9
16
|
export declare const MIN_SIGNING_SECRET_LENGTH = 32;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
1
|
+
import { createCipheriv, createDecipheriv, createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
2
2
|
//#region src/modules/actions/runtime/signing.ts
|
|
3
3
|
/** A signing secret is what every link and key a space hands out stands on: as long as a 256-bit key written out. */
|
|
4
4
|
var MIN_SIGNING_SECRET_LENGTH = 32;
|
|
@@ -14,12 +14,34 @@ var createSigning = (secret) => {
|
|
|
14
14
|
return ({ spaceId, environment }) => {
|
|
15
15
|
const key = createHmac("sha256", secret).update(`space-signing:${String(spaceId)}:${environment}`).digest();
|
|
16
16
|
const signatureOf = (value) => createHmac("sha256", key).update(value).digest("base64url");
|
|
17
|
+
const sealing = createHmac("sha256", secret).update(`space-sealing:${String(spaceId)}:${environment}`).digest();
|
|
17
18
|
return {
|
|
18
19
|
sign: (value) => Promise.resolve(signatureOf(value)),
|
|
19
20
|
verify: (value, signature) => {
|
|
20
21
|
const expected = Buffer.from(signatureOf(value));
|
|
21
22
|
const given = Buffer.from(signature);
|
|
22
23
|
return Promise.resolve(given.length === expected.length && timingSafeEqual(given, expected));
|
|
24
|
+
},
|
|
25
|
+
seal: (value) => {
|
|
26
|
+
const iv = randomBytes(12);
|
|
27
|
+
const cipher = createCipheriv("aes-256-gcm", sealing, iv);
|
|
28
|
+
const body = Buffer.concat([cipher.update(value, "utf8"), cipher.final()]);
|
|
29
|
+
return Buffer.concat([
|
|
30
|
+
iv,
|
|
31
|
+
cipher.getAuthTag(),
|
|
32
|
+
body
|
|
33
|
+
]).toString("base64url");
|
|
34
|
+
},
|
|
35
|
+
open: (sealed) => {
|
|
36
|
+
const bytes = Buffer.from(sealed, "base64url");
|
|
37
|
+
if (bytes.length < 28) return;
|
|
38
|
+
try {
|
|
39
|
+
const decipher = createDecipheriv("aes-256-gcm", sealing, bytes.subarray(0, 12));
|
|
40
|
+
decipher.setAuthTag(bytes.subarray(12, 28));
|
|
41
|
+
return Buffer.concat([decipher.update(bytes.subarray(28)), decipher.final()]).toString("utf8");
|
|
42
|
+
} catch {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
23
45
|
}
|
|
24
46
|
};
|
|
25
47
|
};
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { createEmailSender } from './email';
|
|
2
2
|
import { createRedactor } from './scope';
|
|
3
3
|
import { createSigning } from './signing';
|
|
4
|
+
import { FunctionJobsService } from '../../functions/jobs';
|
|
4
5
|
import { LaterDeps } from '../jobs/later';
|
|
5
6
|
import { ActionKvStore, ActionRunRequest, ActionsConfig, ActionTaskContext, ResolvedActionLimits } from '../types';
|
|
7
|
+
import { RunUser } from '@plitzi/sdk-shared';
|
|
6
8
|
/**
|
|
7
9
|
* Counts outbound calls, refuses past the budget, caps what one answer may carry back, and stamps the run's
|
|
8
10
|
* lineage on every one of them.
|
|
@@ -31,12 +33,19 @@ export type TaskContextDeps = {
|
|
|
31
33
|
signing?: ReturnType<typeof createSigning>;
|
|
32
34
|
/** What runs set for later go through: the module's queue, read when asked. */
|
|
33
35
|
later: LaterDeps;
|
|
36
|
+
/** The space's jobs, for a function's `ctx.jobs`. */
|
|
37
|
+
jobs?: FunctionJobsService;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Who and what the context is for — a run's request, or anything shaped like one (a function's route, a job's slice,
|
|
41
|
+
* whose user is who started it and holds no session).
|
|
42
|
+
*/
|
|
43
|
+
export type TaskContextRequest = Pick<ActionRunRequest, 'runId' | 'spaceId' | 'environment' | 'trigger' | 'callerId' | 'at' | 'emit' | 'origin'> & {
|
|
44
|
+
user?: RunUser;
|
|
34
45
|
};
|
|
35
|
-
/** Who and what the context is for — a run's request, or anything shaped like one (a function's route). */
|
|
36
|
-
export type TaskContextRequest = Pick<ActionRunRequest, 'runId' | 'spaceId' | 'environment' | 'trigger' | 'user' | 'callerId' | 'at' | 'emit'>;
|
|
37
46
|
/**
|
|
38
47
|
* What a task runs with — the one place it is made, for a step of a run and for a function answering a route alike:
|
|
39
48
|
* every secret it resolves registered with the redactor, the space's `kv` namespace, the run's outbound budget, the
|
|
40
49
|
* space's channels. Everything but `log`, which is the STEP's: each step keeps what it logged.
|
|
41
50
|
*/
|
|
42
|
-
export declare const taskContextFor: (config: ActionsConfig, { kv, email, redactor, signing, later }: TaskContextDeps, request: TaskContextRequest, signal: AbortSignal, runFetch: typeof fetch) => ((scope: Record<string, unknown>) => Omit<ActionTaskContext, "log">);
|
|
51
|
+
export declare const taskContextFor: (config: ActionsConfig, { kv, email, redactor, signing, later, jobs }: TaskContextDeps, request: TaskContextRequest, signal: AbortSignal, runFetch: typeof fetch) => ((scope: Record<string, unknown>) => Omit<ActionTaskContext, "log">);
|
|
@@ -67,11 +67,12 @@ var createRunFetch = (base, signal, limits, lineage, onBudgetSpent) => {
|
|
|
67
67
|
* every secret it resolves registered with the redactor, the space's `kv` namespace, the run's outbound budget, the
|
|
68
68
|
* space's channels. Everything but `log`, which is the STEP's: each step keeps what it logged.
|
|
69
69
|
*/
|
|
70
|
-
var taskContextFor = (config, { kv, email, redactor, signing, later }, request, signal, runFetch) => {
|
|
70
|
+
var taskContextFor = (config, { kv, email, redactor, signing, later, jobs }, request, signal, runFetch) => {
|
|
71
71
|
const { realtime } = config;
|
|
72
72
|
const readData = projectDataReader(config.lookups.getData, request.spaceId, request.at);
|
|
73
73
|
return (scope) => ({
|
|
74
74
|
runId: request.runId,
|
|
75
|
+
...request.origin ? { origin: request.origin } : {},
|
|
75
76
|
spaceId: request.spaceId,
|
|
76
77
|
environment: request.environment,
|
|
77
78
|
trigger: request.trigger,
|
|
@@ -117,6 +118,12 @@ var taskContextFor = (config, { kv, email, redactor, signing, later }, request,
|
|
|
117
118
|
environment: request.environment,
|
|
118
119
|
...request.at ? { at: request.at } : {}
|
|
119
120
|
}) : {},
|
|
121
|
+
...jobs ? { jobs: (declared) => jobs.forRun({
|
|
122
|
+
spaceId: request.spaceId,
|
|
123
|
+
environment: request.environment,
|
|
124
|
+
...request.at ? { at: request.at } : {},
|
|
125
|
+
...request.user ? { user: request.user } : {}
|
|
126
|
+
}, declared) } : {},
|
|
120
127
|
...realtime ? {
|
|
121
128
|
publish: (topic, type, data) => realtime.publish({
|
|
122
129
|
spaceId: request.spaceId,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { serverLog } from "../../../helpers/serverLog.js";
|
|
2
2
|
import { triggerParams } from "../runtime/triggers.js";
|
|
3
3
|
import { ActionRunError } from "../runtime/errors.js";
|
|
4
|
+
import { requestOrigin } from "../../../core/requestParser.js";
|
|
4
5
|
import { forcedFlagsFor } from "../../../helpers/flagOverrides.js";
|
|
5
6
|
import { precheckRun } from "../runtime/precheck.js";
|
|
6
7
|
import onAbort from "../../../helpers/onAbort.js";
|
|
@@ -108,6 +109,7 @@ var handleActionCall = async (deps) => {
|
|
|
108
109
|
return;
|
|
109
110
|
}
|
|
110
111
|
const limits = module.limitsFor(entry.document);
|
|
112
|
+
const origin = requestOrigin(req);
|
|
111
113
|
const input = body.input ?? {};
|
|
112
114
|
const begin = {
|
|
113
115
|
spaceId,
|
|
@@ -193,6 +195,7 @@ var handleActionCall = async (deps) => {
|
|
|
193
195
|
trigger: "call",
|
|
194
196
|
user: req.ctx.user,
|
|
195
197
|
callerId,
|
|
198
|
+
...origin ? { origin } : {},
|
|
196
199
|
runId: run.runId,
|
|
197
200
|
lineage,
|
|
198
201
|
at: {
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { LaterAnswer, LaterRequest } from './jobs/later';
|
|
2
2
|
import { ActionEmailSender } from './runtime/email';
|
|
3
3
|
import { FunctionsConfig } from '../functions/config';
|
|
4
|
+
import { FunctionJobs } from '../functions/contract';
|
|
4
5
|
import { SpaceFunctions } from '../functions/protocol';
|
|
5
6
|
import { KvChangeLifetime } from './runtime/kvChange';
|
|
6
7
|
import { KvListEntry, KvListPut, KvListPutOptions, KvListRange } from './runtime/kvList';
|
|
7
8
|
import { SpaceSigning } from './runtime/signing';
|
|
8
|
-
import { ActionEmailConfig,
|
|
9
|
+
import { ActionEmailConfig, Job, JobQueue, ActionEntry, ActionLimits, ActionRejectRecord, ActionRunRecord, ActionRunStatus, ActionRunStep, ActionTriggerType, ConnectorManifest, Environment, FunctionJobManifest, FunctionJobView, InteractionCallbackParam, InteractionNode, InteractionNodeStatus, RunUser, Schema, SpaceRevision, SSRServerConfig, SSRUser } from '@plitzi/sdk-shared';
|
|
9
10
|
/** Resolved secret material, as the connector engine already models it. */
|
|
10
11
|
export type ActionCredential = Record<string, string>;
|
|
11
12
|
/**
|
|
@@ -152,7 +153,8 @@ export type ActionTaskContext = {
|
|
|
152
153
|
spaceId: number;
|
|
153
154
|
environment: Environment;
|
|
154
155
|
trigger: ActionTriggerType;
|
|
155
|
-
|
|
156
|
+
/** Who is asking, as the session named them — or, for a job's slice, who started it. Never the session itself. */
|
|
157
|
+
user?: RunUser;
|
|
156
158
|
/**
|
|
157
159
|
* Who is asking, as the transport identified them: `user:<id>` for a session, `ip:<address>` for everyone else,
|
|
158
160
|
* and `render` or `schedule` for the runs no visitor started.
|
|
@@ -204,9 +206,23 @@ export type ActionTaskContext = {
|
|
|
204
206
|
later?: (request: LaterRequest) => Promise<LaterAnswer>;
|
|
205
207
|
/** Drops the runs waiting under `key`, and answers how many. */
|
|
206
208
|
cancelLater?: (key: string) => Promise<number>;
|
|
209
|
+
/**
|
|
210
|
+
* The space's jobs, for code over the jobs it declares (`functions/jobs.ts`) — `undefined` when the server runs no
|
|
211
|
+
* jobs. What a function's `ctx.jobs` goes through.
|
|
212
|
+
*/
|
|
213
|
+
jobs?: (declared: Readonly<Record<string, FunctionJobManifest>>) => FunctionJobs;
|
|
207
214
|
/** Signs with the space's own key (`ActionsConfig.signingSecret`) — `undefined` when the deployment gave none. */
|
|
208
215
|
sign?: SpaceSigning['sign'];
|
|
209
216
|
verify?: SpaceSigning['verify'];
|
|
217
|
+
/** Encrypts what the platform keeps for the space and its code must not read — `undefined` with no signingSecret. */
|
|
218
|
+
seal?: SpaceSigning['seal'];
|
|
219
|
+
open?: SpaceSigning['open'];
|
|
220
|
+
/**
|
|
221
|
+
* The public origin of the request that started the run (`https://shop.example.com`), as the server resolved it —
|
|
222
|
+
* what an address back to the space is made of (an OAuth `redirect_uri`). `undefined` for a run no request started:
|
|
223
|
+
* a schedule, a job.
|
|
224
|
+
*/
|
|
225
|
+
origin?: string;
|
|
210
226
|
/**
|
|
211
227
|
* One file of the space's data (`products.json`), parsed, as of the run's revision — read-only, and refused when
|
|
212
228
|
* the deployment keeps no data (`lookups.getData`, or `dataDir`). One lookup per run, whichever step reads it.
|
|
@@ -227,6 +243,13 @@ export type ActionRealtime = {
|
|
|
227
243
|
spaceId: number;
|
|
228
244
|
environment: string;
|
|
229
245
|
}, topic: string, grant?: string) => Promise<void>;
|
|
246
|
+
/** Says a job of the space's functions as it is now, on its own topic (`$job:<id>`), kept for a page that joins. */
|
|
247
|
+
publishJob?: (space: {
|
|
248
|
+
spaceId: number;
|
|
249
|
+
environment: string;
|
|
250
|
+
}, jobId: string, view: FunctionJobView) => Promise<void>;
|
|
251
|
+
/** Hands the realtime the way to a job, for the door of its topic: who started it, and who its `watch` lets in. */
|
|
252
|
+
watchJobs?: (lookup: (spaceId: number, jobId: string) => Promise<Job | undefined>) => void;
|
|
230
253
|
};
|
|
231
254
|
/**
|
|
232
255
|
* One step a server action can take.
|
|
@@ -344,14 +367,14 @@ export type ActionsConfig = {
|
|
|
344
367
|
*
|
|
345
368
|
* Supplying nothing still schedules — over an in-process queue, which is correct for one replica and silently
|
|
346
369
|
* wrong for two, since each gets its own copy of every schedule. A deployment with more than one supplies a
|
|
347
|
-
* {@link
|
|
370
|
+
* {@link JobQueue} over a store they share.
|
|
348
371
|
*/
|
|
349
372
|
export type ActionJobsConfig = {
|
|
350
373
|
/**
|
|
351
374
|
* Where jobs and schedules live. Omitted → this process's memory: nothing survives a restart, and a second
|
|
352
375
|
* replica produces the same fires all over again.
|
|
353
376
|
*/
|
|
354
|
-
queue?:
|
|
377
|
+
queue?: JobQueue;
|
|
355
378
|
/**
|
|
356
379
|
* How many jobs this replica runs at once. Default 4; `0` makes it a producer that never consumes.
|
|
357
380
|
*
|
|
@@ -409,6 +432,8 @@ export type ActionRunRequest = {
|
|
|
409
432
|
user?: SSRUser;
|
|
410
433
|
/** Who is asking. Stated by whatever started the run — see {@link ActionTaskContext.callerId}. */
|
|
411
434
|
callerId: string;
|
|
435
|
+
/** The public origin of the request that started it — see {@link ActionTaskContext.origin}. */
|
|
436
|
+
origin?: string;
|
|
412
437
|
runId: string;
|
|
413
438
|
/** Chain of run ids that caused this one; a run naming its own action is refused before its first node. */
|
|
414
439
|
lineage?: string[];
|
|
@@ -129,6 +129,85 @@ var fetchInitOf = (value) => {
|
|
|
129
129
|
};
|
|
130
130
|
};
|
|
131
131
|
var optionalString = (value, what) => value === void 0 || value === null ? void 0 : stringArg(value, what);
|
|
132
|
+
/** A call to `ctx.jobs`, read into the one method it names: a name and an object for `start`, an id for the rest. */
|
|
133
|
+
var jobsCallOf = (call) => {
|
|
134
|
+
switch (call.method) {
|
|
135
|
+
case "start": {
|
|
136
|
+
const key = optionalString(call.key, "A key");
|
|
137
|
+
if (call.input !== void 0 && !isRecord(call.input)) throw new CallError("A job is handed an object as its input");
|
|
138
|
+
return {
|
|
139
|
+
op: "jobs",
|
|
140
|
+
method: "start",
|
|
141
|
+
name: stringArg(call.name, "A job"),
|
|
142
|
+
input: isRecord(call.input) ? call.input : {},
|
|
143
|
+
...key === void 0 ? {} : { key }
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
case "get":
|
|
147
|
+
case "pause":
|
|
148
|
+
case "resume":
|
|
149
|
+
case "cancel": return {
|
|
150
|
+
op: "jobs",
|
|
151
|
+
method: call.method,
|
|
152
|
+
id: stringArg(call.id, "A job id")
|
|
153
|
+
};
|
|
154
|
+
default: throw new CallError("Not a jobs method the platform answers");
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
/** A job's `save` or `report`, from its slice. */
|
|
158
|
+
var jobCallOf = (call) => {
|
|
159
|
+
if (call.method === "save") return {
|
|
160
|
+
op: "job",
|
|
161
|
+
method: "save",
|
|
162
|
+
state: call.state
|
|
163
|
+
};
|
|
164
|
+
if (call.method === "report") {
|
|
165
|
+
if (typeof call.fraction !== "number" || !Number.isFinite(call.fraction)) throw new CallError("job.report takes how far it is, a number from 0 to 1");
|
|
166
|
+
const step = optionalString(call.step, "A step");
|
|
167
|
+
return {
|
|
168
|
+
op: "job",
|
|
169
|
+
method: "report",
|
|
170
|
+
fraction: call.fraction,
|
|
171
|
+
...step === void 0 ? {} : { step }
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
throw new CallError("Not a job method the platform answers");
|
|
175
|
+
};
|
|
176
|
+
/** A call to `ctx.oauth`, read into the one method it names: what each method takes, and an owner only as a string. */
|
|
177
|
+
var oauthCallOf = (call) => {
|
|
178
|
+
const connection = stringArg(call.connection, "An OAuth connection");
|
|
179
|
+
const owner = optionalString(call.owner, "An OAuth owner");
|
|
180
|
+
const own = owner === void 0 ? {} : { owner };
|
|
181
|
+
switch (call.method) {
|
|
182
|
+
case "connect": return {
|
|
183
|
+
op: "oauth",
|
|
184
|
+
method: "connect",
|
|
185
|
+
connection,
|
|
186
|
+
returnTo: stringArg(call.returnTo, "returnTo"),
|
|
187
|
+
...own
|
|
188
|
+
};
|
|
189
|
+
case "status":
|
|
190
|
+
case "disconnect":
|
|
191
|
+
case "token": return {
|
|
192
|
+
op: "oauth",
|
|
193
|
+
method: call.method,
|
|
194
|
+
connection,
|
|
195
|
+
...own
|
|
196
|
+
};
|
|
197
|
+
case "fetch": {
|
|
198
|
+
const { credential: _credential, ...init } = fetchInitOf(call.init ?? {});
|
|
199
|
+
return {
|
|
200
|
+
op: "oauth",
|
|
201
|
+
method: "fetch",
|
|
202
|
+
connection,
|
|
203
|
+
url: stringArg(call.url, "A URL"),
|
|
204
|
+
init,
|
|
205
|
+
...own
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
default: throw new CallError("Not an OAuth method the platform answers");
|
|
209
|
+
}
|
|
210
|
+
};
|
|
132
211
|
/** What a run set for later is checked against where it is queued (`laterFor`); here only that it is what it claims. */
|
|
133
212
|
var laterOf = (value) => {
|
|
134
213
|
const request = recordArg(value, "A run set for later");
|
|
@@ -214,6 +293,9 @@ var readCall = (value) => {
|
|
|
214
293
|
op: "data",
|
|
215
294
|
file: stringArg(call.file, "A file")
|
|
216
295
|
};
|
|
296
|
+
case "oauth": return oauthCallOf(call);
|
|
297
|
+
case "jobs": return jobsCallOf(call);
|
|
298
|
+
case "job": return jobCallOf(call);
|
|
217
299
|
case "log": return {
|
|
218
300
|
op: "log",
|
|
219
301
|
values: Array.isArray(call.values) ? call.values : []
|
|
@@ -242,6 +324,31 @@ var answerCall = async (ctx, call) => {
|
|
|
242
324
|
case "sign": return ctx.sign(call.value);
|
|
243
325
|
case "verify": return ctx.verify(call.value, call.signature);
|
|
244
326
|
case "data": return ctx.data(call.file);
|
|
327
|
+
case "oauth":
|
|
328
|
+
switch (call.method) {
|
|
329
|
+
case "connect": return ctx.oauth.connect(call.connection, {
|
|
330
|
+
returnTo: call.returnTo,
|
|
331
|
+
...call.owner === void 0 ? {} : { owner: call.owner }
|
|
332
|
+
});
|
|
333
|
+
case "status": return ctx.oauth.status(call.connection, call.owner === void 0 ? {} : { owner: call.owner });
|
|
334
|
+
case "disconnect": return ctx.oauth.disconnect(call.connection, call.owner === void 0 ? {} : { owner: call.owner });
|
|
335
|
+
case "token": return ctx.oauth.token(call.connection, call.owner === void 0 ? {} : { owner: call.owner });
|
|
336
|
+
case "fetch": return wireResponseOf(await ctx.oauth.fetch(call.connection, call.url, {
|
|
337
|
+
...call.init,
|
|
338
|
+
...call.owner === void 0 ? {} : { owner: call.owner }
|
|
339
|
+
}));
|
|
340
|
+
}
|
|
341
|
+
break;
|
|
342
|
+
case "jobs":
|
|
343
|
+
switch (call.method) {
|
|
344
|
+
case "start": return ctx.jobs.start(call.name, call.input, call.key === void 0 ? {} : { key: call.key });
|
|
345
|
+
case "get": return await ctx.jobs.get(call.id) ?? null;
|
|
346
|
+
case "pause": return ctx.jobs.pause(call.id);
|
|
347
|
+
case "resume": return ctx.jobs.resume(call.id);
|
|
348
|
+
case "cancel": return ctx.jobs.cancel(call.id);
|
|
349
|
+
}
|
|
350
|
+
break;
|
|
351
|
+
case "job": throw new CallError("job.save and job.report belong to a job, inside its run");
|
|
245
352
|
case "log":
|
|
246
353
|
ctx.log(...call.values);
|
|
247
354
|
return;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { FunctionsDefinition } from './contract';
|
|
2
|
+
import { FunctionJobLimits } from './jobs';
|
|
2
3
|
import { FunctionLimits, FunctionRunner, FunctionUsage } from './protocol';
|
|
3
4
|
import { FunctionTimeLimits } from '@plitzi/sdk-shared';
|
|
4
5
|
/** One invocation of a space's task, as metering sees it. */
|
|
@@ -39,6 +40,14 @@ export type FunctionsConfig = {
|
|
|
39
40
|
admit?: (spaceId: number) => Promise<string | undefined>;
|
|
40
41
|
/** Told what each invocation spent, however it ended: what `admit` budgets by, and what a deployment meters. */
|
|
41
42
|
onUsage?: (record: FunctionUsageRecord) => void;
|
|
43
|
+
/** The spaces' jobs (`defineFunctions({ jobs })`): what each may spend. */
|
|
44
|
+
jobs?: {
|
|
45
|
+
/**
|
|
46
|
+
* What one space's jobs may spend — its plan's — each unset one `DEFAULT_FUNCTION_JOB_LIMITS`'. `pool` names the
|
|
47
|
+
* spaces whose running jobs count together: an account's.
|
|
48
|
+
*/
|
|
49
|
+
limitsFor?: (spaceId: number) => Promise<Partial<FunctionJobLimits> | undefined>;
|
|
50
|
+
};
|
|
42
51
|
};
|
|
43
52
|
/** What an invocation gets unless its task asks for more CPU or time. */
|
|
44
53
|
export declare const DEFAULT_FUNCTION_LIMITS: FunctionLimits;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { FunctionContext } from './contract';
|
|
2
2
|
import { FunctionScope } from './scope';
|
|
3
3
|
import { ActionTaskContext } from '../actions/types';
|
|
4
|
+
import { FunctionJobManifest, FunctionOAuthConnection } from '@plitzi/sdk-shared';
|
|
4
5
|
/**
|
|
5
6
|
* Whether `hostname` is one the space declared: exactly, or under a `*.` wildcard (`*.example.com` is every subdomain of
|
|
6
7
|
* it, and not `example.com` itself).
|
|
@@ -16,4 +17,4 @@ export declare const lineOf: (values: readonly unknown[]) => string;
|
|
|
16
17
|
* A self-hosted server hands it to the code directly; the platform answers the sandbox's calls with it. With a `scope`,
|
|
17
18
|
* it is a plugin's code, and gets the plugin's corner of the space (`./scope`).
|
|
18
19
|
*/
|
|
19
|
-
export declare const functionContextFor: (ctx: ActionTaskContext, hosts: readonly string[], scope?: FunctionScope) => FunctionContext;
|
|
20
|
+
export declare const functionContextFor: (ctx: ActionTaskContext, hosts: readonly string[], scope?: FunctionScope, oauth?: Readonly<Record<string, FunctionOAuthConnection>>, jobs?: Readonly<Record<string, FunctionJobManifest>>) => FunctionContext;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { prefixKv } from "../actions/runtime/namespaceKv.js";
|
|
2
2
|
import { fetchOutbound } from "../../helpers/outboundGuard.js";
|
|
3
3
|
import { countRate, refusedOver } from "../actions/runtime/rateLimit.js";
|
|
4
|
+
import { noOAuth, oauthFor } from "./oauth.js";
|
|
4
5
|
import { pluginKvPrefix, pluginSigned } from "./scope.js";
|
|
5
6
|
import { processTwigParam } from "@plitzi/sdk-shared/helpers/twigWrapper";
|
|
6
7
|
//#region src/modules/functions/context.ts
|
|
@@ -65,6 +66,17 @@ var lineOf = (values) => values.map((value) => {
|
|
|
65
66
|
var unavailable = (what) => () => Promise.reject(/* @__PURE__ */ new Error(`This server has no realtime channels (${what})`));
|
|
66
67
|
var unsigned = () => Promise.reject(/* @__PURE__ */ new Error("This server signs nothing: its actions were given no signingSecret"));
|
|
67
68
|
var noJobs = () => Promise.reject(/* @__PURE__ */ new Error("This server runs no jobs: nothing can be set to run later"));
|
|
69
|
+
/** `ctx.jobs` where there is none to give: a server with no queue, or a plugin's code — the space's jobs are the space's. */
|
|
70
|
+
var noFunctionJobs = (why) => {
|
|
71
|
+
const refused = () => Promise.reject(new Error(why));
|
|
72
|
+
return {
|
|
73
|
+
start: refused,
|
|
74
|
+
get: refused,
|
|
75
|
+
pause: refused,
|
|
76
|
+
resume: refused,
|
|
77
|
+
cancel: refused
|
|
78
|
+
};
|
|
79
|
+
};
|
|
68
80
|
/**
|
|
69
81
|
* A plugin reads none of its space's data. The space keeps there what its pages must not carry, and a plugin was trusted
|
|
70
82
|
* to draw, not with what the space keeps — the same line as its credentials: handed the data, its `fetch` could send
|
|
@@ -76,7 +88,7 @@ var noData = (file) => Promise.reject(/* @__PURE__ */ new Error(`The functions o
|
|
|
76
88
|
* A self-hosted server hands it to the code directly; the platform answers the sandbox's calls with it. With a `scope`,
|
|
77
89
|
* it is a plugin's code, and gets the plugin's corner of the space (`./scope`).
|
|
78
90
|
*/
|
|
79
|
-
var functionContextFor = (ctx, hosts, scope) => {
|
|
91
|
+
var functionContextFor = (ctx, hosts, scope, oauth = {}, jobs = {}) => {
|
|
80
92
|
const shared = {
|
|
81
93
|
spaceId: ctx.spaceId,
|
|
82
94
|
environment: ctx.environment,
|
|
@@ -100,7 +112,11 @@ var functionContextFor = (ctx, hosts, scope) => {
|
|
|
100
112
|
revoke: ctx.revoke ?? unavailable("revoke"),
|
|
101
113
|
later: ctx.later ?? noJobs,
|
|
102
114
|
cancelLater: ctx.cancelLater ?? noJobs,
|
|
103
|
-
data: ctx.data
|
|
115
|
+
data: ctx.data,
|
|
116
|
+
jobs: ctx.jobs ? ctx.jobs(jobs) : noFunctionJobs("This server runs no jobs: its actions have no queue"),
|
|
117
|
+
oauth: oauthFor(ctx, oauth, (url) => {
|
|
118
|
+
if (!hostAllowed(url.hostname, hosts)) throw new Error(`"${url.hostname}" is not among the hosts this space's functions may reach (allow.hosts)`);
|
|
119
|
+
})
|
|
104
120
|
};
|
|
105
121
|
const kv = prefixKv(ctx.kv, pluginKvPrefix(scope.plugin));
|
|
106
122
|
const { sign, verify } = ctx;
|
|
@@ -117,7 +133,9 @@ var functionContextFor = (ctx, hosts, scope) => {
|
|
|
117
133
|
revoke: noChannel,
|
|
118
134
|
later: noLater,
|
|
119
135
|
cancelLater: noLater,
|
|
120
|
-
data: noData
|
|
136
|
+
data: noData,
|
|
137
|
+
jobs: noFunctionJobs("The functions of a plugin start none of the jobs of its space"),
|
|
138
|
+
oauth: noOAuth("The functions of a plugin make none of the OAuth connections of its space")
|
|
121
139
|
};
|
|
122
140
|
};
|
|
123
141
|
//#endregion
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { LaterAnswer, LaterRequest } from '../actions/jobs/later';
|
|
2
2
|
import { RateCount, RateLimit } from '../actions/runtime/rateLimit';
|
|
3
3
|
import { ActionKvStore } from '../actions/types';
|
|
4
|
-
import { Environment, FunctionTimeLimits, InteractionCallbackParam } from '@plitzi/sdk-shared';
|
|
4
|
+
import { ActionAccess, Environment, FunctionJobView, FunctionOAuthConnection, FunctionTimeLimits, InteractionCallbackParam } from '@plitzi/sdk-shared';
|
|
5
5
|
/**
|
|
6
6
|
* Who started the run, as a space's own code may know them: everything a function decides by — who, their roles and
|
|
7
7
|
* permissions — and never their session. A token inside the sandbox is a token a function could send anywhere.
|
|
@@ -27,6 +27,123 @@ export type FunctionFetchInit = {
|
|
|
27
27
|
};
|
|
28
28
|
/** What a function's `fetch` answers: a web `Response`, whichever runner it is in. */
|
|
29
29
|
export type FunctionFetch = (url: string | URL, init?: FunctionFetchInit) => Promise<Response>;
|
|
30
|
+
/**
|
|
31
|
+
* Whose an OAuth connection is: a signed-in visitor's own by default (`user:<id>`); `space` for one the whole space
|
|
32
|
+
* shares; or a key the code keeps for whom it serves — a profile, a team (`profile:ana`). Letters, digits and `:._@-`.
|
|
33
|
+
*/
|
|
34
|
+
export type FunctionOAuthOwner = {
|
|
35
|
+
owner?: string;
|
|
36
|
+
};
|
|
37
|
+
/** What `ctx.oauth.status` says of a connection — never its token. */
|
|
38
|
+
export type FunctionOAuthStatus = {
|
|
39
|
+
connected: boolean;
|
|
40
|
+
scopes?: string[];
|
|
41
|
+
expiresAt?: number;
|
|
42
|
+
};
|
|
43
|
+
/** The access token `ctx.oauth.token` hands out — never the refresh token, which stays with the platform. */
|
|
44
|
+
export type FunctionOAuthToken = {
|
|
45
|
+
token: string;
|
|
46
|
+
type: string;
|
|
47
|
+
expiresAt?: number;
|
|
48
|
+
scopes?: string[];
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* OAuth 2.0 on behalf of whoever the space serves, through the connections it declared (`oauth` in its functions): the
|
|
52
|
+
* consent, the code exchanged, the token kept sealed and renewed by the platform. The code never holds a token, as it
|
|
53
|
+
* never holds a credential — it names the connection.
|
|
54
|
+
*/
|
|
55
|
+
export type FunctionOAuth = {
|
|
56
|
+
/**
|
|
57
|
+
* Where to send the visitor to consent (authorization code, PKCE). They come back to `/fn/oauth/callback` — the
|
|
58
|
+
* address to register with the provider, on the space's host — and from there to `returnTo`, a path of the space,
|
|
59
|
+
* with `?oauth=<connection>&oauth_result=connected` or `&oauth_error=<what>`.
|
|
60
|
+
*/
|
|
61
|
+
connect: (connection: string, options: {
|
|
62
|
+
returnTo: string;
|
|
63
|
+
} & FunctionOAuthOwner) => Promise<{
|
|
64
|
+
url: string;
|
|
65
|
+
}>;
|
|
66
|
+
status: (connection: string, options?: FunctionOAuthOwner) => Promise<FunctionOAuthStatus>;
|
|
67
|
+
/**
|
|
68
|
+
* A request to the provider's API with the token written in (`Authorization: Bearer`), renewed when it is about to
|
|
69
|
+
* lapse and once more if the answer is 401. Only to the hosts the space declared (`allow.hosts`).
|
|
70
|
+
*/
|
|
71
|
+
fetch: (connection: string, url: string | URL, init?: Omit<FunctionFetchInit, 'credential'> & FunctionOAuthOwner) => Promise<Response>;
|
|
72
|
+
/** Forgets the connection — revoked at the provider when it says where — and answers whether there was one. */
|
|
73
|
+
disconnect: (connection: string, options?: FunctionOAuthOwner) => Promise<boolean>;
|
|
74
|
+
/**
|
|
75
|
+
* The access token itself, renewed first when it is about to lapse — only of a connection that declares
|
|
76
|
+
* `exposeToken: true`, for a provider's SDK in the page that asks for it (`getOAuthToken`). Whoever the code hands it
|
|
77
|
+
* to holds it until it lapses: answer it only to whom the connection is (its owner), from a route that checks so.
|
|
78
|
+
*/
|
|
79
|
+
token: (connection: string, options?: FunctionOAuthOwner) => Promise<FunctionOAuthToken>;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* The space's jobs, from its code: what starts one, reads it and stops it. Who may is the code's to decide — these are
|
|
83
|
+
* called from its routes and tasks, behind its own checks.
|
|
84
|
+
*/
|
|
85
|
+
export type FunctionJobs = {
|
|
86
|
+
/**
|
|
87
|
+
* Starts one of the jobs this code declares (`jobs` in `defineFunctions`), with `input`: answered at once, the job
|
|
88
|
+
* `pending`. With `key`, at most one of that key runs at a time — starting it again while one is under way answers that
|
|
89
|
+
* one (a button pressed twice), and once it ended starts it over with the new input.
|
|
90
|
+
*/
|
|
91
|
+
start: (name: string, input?: Record<string, unknown>, options?: {
|
|
92
|
+
key?: string;
|
|
93
|
+
}) => Promise<FunctionJobView>;
|
|
94
|
+
/** One of the space's jobs, as whoever may watch it sees it — or nothing, when there is none by that id. */
|
|
95
|
+
get: (id: string) => Promise<FunctionJobView | undefined>;
|
|
96
|
+
/** Sets it aside: it stops once its code saved, and waits as `paused`. Answers whether anything changed. */
|
|
97
|
+
pause: (id: string) => Promise<boolean>;
|
|
98
|
+
/** Puts a paused job back in the queue, to go on from what it saved. */
|
|
99
|
+
resume: (id: string) => Promise<boolean>;
|
|
100
|
+
/** Stops it for good: dropped if it waits, aborted if it runs. */
|
|
101
|
+
cancel: (id: string) => Promise<boolean>;
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Why a slice of a job is asked to end (`job.stopping`): it was set aside (`pause`) or cancelled (`cancel`), its time in
|
|
105
|
+
* one go is up (`limit`) or the server is going away (`shutdown`). Whichever, what was saved is where it goes on from.
|
|
106
|
+
*/
|
|
107
|
+
export type FunctionJobStop = 'pause' | 'cancel' | 'limit' | 'shutdown';
|
|
108
|
+
/**
|
|
109
|
+
* What a job's `run` is handed besides its input: the job, and how it keeps where it is. A job runs in slices — a pause,
|
|
110
|
+
* a lost server or its time in one go ending one — and every slice but the first starts from what the one before saved.
|
|
111
|
+
*/
|
|
112
|
+
export type FunctionJobHandle<S = unknown> = {
|
|
113
|
+
id: string;
|
|
114
|
+
name: string;
|
|
115
|
+
/** What it last saved — `undefined` on its first slice, and whenever it never saved. */
|
|
116
|
+
state: S | undefined;
|
|
117
|
+
/** Which attempt this is, from 1: a slice after a failure is the next attempt, from the last save. */
|
|
118
|
+
attempt: number;
|
|
119
|
+
/**
|
|
120
|
+
* Keeps `state` durably: the next slice starts with it. JSON, within what the server keeps for one job. A method, not
|
|
121
|
+
* a property: so a job whose state has a type of its own is still one of `FunctionsDefinition['jobs']`.
|
|
122
|
+
*/
|
|
123
|
+
save(state: S): Promise<void>;
|
|
124
|
+
/** How far it is, from 0 to 1, and what it is doing — for whoever watches it. Never fails the job. */
|
|
125
|
+
report: (fraction: number, step?: string) => void;
|
|
126
|
+
/** Why this slice is asked to end, once it is (`signal` aborted): save, and return. */
|
|
127
|
+
readonly stopping: FunctionJobStop | undefined;
|
|
128
|
+
/** Aborted when the slice is asked to end — the same as `ctx.signal`. Some seconds later it is cut. */
|
|
129
|
+
signal: AbortSignal;
|
|
130
|
+
};
|
|
131
|
+
/** A job of the space's: work longer than a request, run on the server slice by slice until it answers. */
|
|
132
|
+
export type FunctionJob<I extends Record<string, unknown> = Record<string, unknown>, S = unknown, R = unknown> = {
|
|
133
|
+
/**
|
|
134
|
+
* Who may watch it (`$job:<id>`, `useJob`) besides whoever started it — a channel's access, read against the visitor's
|
|
135
|
+
* permissions in the space. Absent, only whoever started it.
|
|
136
|
+
*/
|
|
137
|
+
watch?: ActionAccess;
|
|
138
|
+
/** How long it may run, every slice together, in seconds — within what the server allows. */
|
|
139
|
+
maxSeconds?: number;
|
|
140
|
+
/**
|
|
141
|
+
* The work. What it answers is the job's result; an `ActionRefusal` fails it without a retry. A method, not a property:
|
|
142
|
+
* so a job typed with an input and a state of its own is still one of `FunctionsDefinition['jobs']`, and one written
|
|
143
|
+
* in place there gets an object for its input and `unknown` for its state.
|
|
144
|
+
*/
|
|
145
|
+
run(input: I, job: FunctionJobHandle<S>, ctx: FunctionContext): R | Promise<R>;
|
|
146
|
+
};
|
|
30
147
|
/**
|
|
31
148
|
* Everything a space's code can do besides computing — one object, the same wherever it runs. In a self-hosted server
|
|
32
149
|
* it is built from the run's own context; in the sandbox each method is a call back to the platform, which scopes it
|
|
@@ -84,6 +201,10 @@ export type FunctionContext = {
|
|
|
84
201
|
data: (file: string) => Promise<unknown>;
|
|
85
202
|
/** A line for whoever reads the run: the builder's Try, and the deployment's logs. */
|
|
86
203
|
log: (...values: unknown[]) => void;
|
|
204
|
+
/** OAuth 2.0 through the space's declared connections — see {@link FunctionOAuth}. */
|
|
205
|
+
oauth: FunctionOAuth;
|
|
206
|
+
/** The space's jobs — see {@link FunctionJobs}. */
|
|
207
|
+
jobs: FunctionJobs;
|
|
87
208
|
/** Progress for a caller that asked for a stream — a no-op when nobody did. */
|
|
88
209
|
emit: (chunk: unknown) => void;
|
|
89
210
|
/** Aborted when the run is: cancelled, or out of time. Hand it to what can stop early. */
|
|
@@ -131,6 +252,13 @@ export type FunctionsDefinition = {
|
|
|
131
252
|
routes?: Record<string, FunctionRoute>;
|
|
132
253
|
/** More CPU or time than an invocation gets by default, for every task and route — a task may ask for its own. */
|
|
133
254
|
limits?: FunctionTimeLimits;
|
|
255
|
+
/**
|
|
256
|
+
* The OAuth connections its code makes (`ctx.oauth`), by the name it calls them — any provider, as data: its `issuer`
|
|
257
|
+
* (OpenID) or its endpoints, the credential with the app's id and secret, the scopes.
|
|
258
|
+
*/
|
|
259
|
+
oauth?: Record<string, FunctionOAuthConnection>;
|
|
260
|
+
/** Its jobs, by name (`ctx.jobs.start(name, input)`): work that outlives a request, run slice by slice. */
|
|
261
|
+
jobs?: Record<string, FunctionJob>;
|
|
134
262
|
};
|
|
135
263
|
/**
|
|
136
264
|
* What a function throws to refuse, with a reason written for whoever asked — a wrong password, a board that is
|