@plitzi/sdk-actions 0.0.0-stage → 0.39.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +54 -2
- package/dist/chunks/credentials-DwxZeAvH.mjs +27 -0
- package/dist/chunks/errors-bCBfIe7c.mjs +54 -0
- package/dist/chunks/grants-BsD-WlLE.mjs +168 -0
- package/dist/chunks/load-DBWRZn7G.mjs +48 -0
- package/dist/chunks/module-WdDzbjHy.mjs +7032 -0
- package/dist/chunks/queueDocument-xYz-KZ5V.mjs +21 -0
- package/dist/chunks/queueRules-BCCVgeUE.mjs +487 -0
- package/dist/chunks/remote-DopPgCMK.mjs +404 -0
- package/dist/chunks/storeClock-Btm7EaVm.mjs +31 -0
- package/dist/chunks/taskCatalog-S1IozUTD.mjs +201 -0
- package/dist/connectors/connectors.d.ts +7 -0
- package/dist/connectors/engine.d.ts +47 -0
- package/dist/connectors/getByPath.d.ts +8 -0
- package/dist/connectors/projection.d.ts +18 -0
- package/dist/connectors/resolver.d.ts +27 -0
- package/dist/connectors/types.d.ts +19 -0
- package/dist/engine/configSeam.d.ts +10 -0
- package/dist/engine/connectorWrite.d.ts +12 -0
- package/dist/engine/fileStore.d.ts +60 -0
- package/dist/engine/fleet.d.ts +20 -0
- package/dist/engine/jobs/calls.d.ts +49 -0
- package/dist/engine/jobs/claimSpaces.d.ts +10 -0
- package/dist/engine/jobs/codeMarks.d.ts +10 -0
- package/dist/engine/jobs/endings.d.ts +42 -0
- package/dist/engine/jobs/failureStreak.d.ts +15 -0
- package/dist/engine/jobs/fileQueue.d.ts +34 -0
- package/dist/engine/jobs/flow.d.ts +11 -0
- package/dist/engine/jobs/jobs.d.ts +36 -0
- package/dist/engine/jobs/later.d.ts +27 -0
- package/dist/engine/jobs/memoryNeeds.d.ts +24 -0
- package/dist/engine/jobs/memoryQueue.d.ts +33 -0
- package/dist/engine/jobs/memoryWatch.d.ts +21 -0
- package/dist/engine/jobs/notNow.d.ts +39 -0
- package/dist/engine/jobs/queueDocument.d.ts +15 -0
- package/dist/engine/jobs/queueRules.d.ts +64 -0
- package/dist/engine/jobs/scheduler.d.ts +70 -0
- package/dist/engine/jobs/schedules.d.ts +24 -0
- package/dist/engine/jobs/storeClock.d.ts +16 -0
- package/dist/engine/jobs/wake.d.ts +6 -0
- package/dist/engine/jobs/worker.d.ts +99 -0
- package/dist/engine/module.d.ts +96 -0
- package/dist/engine/moduleFor.d.ts +3 -0
- package/dist/engine/runtime/check.d.ts +24 -0
- package/dist/engine/runtime/credentials.d.ts +8 -0
- package/dist/engine/runtime/deadline.d.ts +19 -0
- package/dist/engine/runtime/email.d.ts +38 -0
- package/dist/engine/runtime/errors.d.ts +28 -0
- package/dist/engine/runtime/fileKv.d.ts +35 -0
- package/dist/engine/runtime/flags.d.ts +16 -0
- package/dist/engine/runtime/guards.d.ts +190 -0
- package/dist/engine/runtime/kvList.d.ts +28 -0
- package/dist/engine/runtime/kvStore.d.ts +36 -0
- package/dist/engine/runtime/limits.d.ts +12 -0
- package/dist/engine/runtime/memoryKv.d.ts +28 -0
- package/dist/engine/runtime/namespaceKv.d.ts +9 -0
- package/dist/engine/runtime/precheck.d.ts +28 -0
- package/dist/engine/runtime/rateLimit.d.ts +9 -0
- package/dist/engine/runtime/redisKv.d.ts +27 -0
- package/dist/engine/runtime/renderFailure.d.ts +24 -0
- package/dist/engine/runtime/renderResolver.d.ts +16 -0
- package/dist/engine/runtime/renderShare.d.ts +12 -0
- package/dist/engine/runtime/report.d.ts +15 -0
- package/dist/engine/runtime/runAction.d.ts +26 -0
- package/dist/engine/runtime/runLogger.d.ts +37 -0
- package/dist/engine/runtime/runStats.d.ts +8 -0
- package/dist/engine/runtime/scope.d.ts +40 -0
- package/dist/engine/runtime/signing.d.ts +27 -0
- package/dist/engine/runtime/taskContext.d.ts +54 -0
- package/dist/engine/runtime/triggers.d.ts +12 -0
- package/dist/engine/taskCatalog.d.ts +28 -0
- package/dist/engine/tasks/auth.d.ts +2 -0
- package/dist/engine/tasks/builtins.d.ts +10 -0
- package/dist/engine/tasks/connector.d.ts +2 -0
- package/dist/engine/tasks/db.d.ts +2 -0
- package/dist/engine/tasks/email.d.ts +2 -0
- package/dist/engine/tasks/flow.d.ts +3 -0
- package/dist/engine/tasks/helpers.d.ts +16 -0
- package/dist/engine/tasks/http.d.ts +2 -0
- package/dist/engine/tasks/kv.d.ts +2 -0
- package/dist/engine/tasks/list.d.ts +2 -0
- package/dist/engine/tasks/names.d.ts +8 -0
- package/dist/engine/tasks/realtime.d.ts +3 -0
- package/dist/engine/tasks/registry.d.ts +19 -0
- package/dist/engine/tasks/transform.d.ts +2 -0
- package/dist/engine/transport/callHandler.d.ts +34 -0
- package/dist/engine/transport/cancelHandler.d.ts +23 -0
- package/dist/engine/transport/catalogHandler.d.ts +22 -0
- package/dist/engine/transport/stream.d.ts +22 -0
- package/dist/engine/transport/verifySignature.d.ts +16 -0
- package/dist/engine/transport/webhookHandler.d.ts +26 -0
- package/dist/engine/types.d.ts +442 -0
- package/dist/functions/build.d.ts +27 -0
- package/dist/functions/capabilities.d.ts +11 -0
- package/dist/functions/config.d.ts +111 -0
- package/dist/functions/context.d.ts +15 -0
- package/dist/functions/index.d.ts +17 -0
- package/dist/functions/index.mjs +461 -0
- package/dist/functions/jobControl.d.ts +22 -0
- package/dist/functions/jobs.d.ts +104 -0
- package/dist/functions/load.d.ts +22 -0
- package/dist/functions/local.d.ts +36 -0
- package/dist/functions/manifest.d.ts +32 -0
- package/dist/functions/native.d.ts +13 -0
- package/dist/functions/oauth.d.ts +33 -0
- package/dist/functions/routes.d.ts +50 -0
- package/dist/functions/runner/remote.d.ts +26 -0
- package/dist/functions/scope.d.ts +28 -0
- package/dist/functions/space.d.ts +41 -0
- package/dist/functions/tryEntry.d.ts +9 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.mjs +545 -0
- package/dist/mongo/claimable.d.ts +5 -0
- package/dist/mongo/index.d.ts +4 -0
- package/dist/mongo/index.mjs +779 -0
- package/dist/mongo/jobQueue.d.ts +38 -0
- package/dist/mongo/kv.d.ts +15 -0
- package/dist/mysql/index.d.ts +5 -0
- package/dist/mysql/index.mjs +808 -0
- package/dist/mysql/jobQueue.d.ts +40 -0
- package/dist/mysql/kv.d.ts +18 -0
- package/dist/mysql/rows.d.ts +64 -0
- package/dist/mysql/schema.d.ts +28 -0
- package/dist/mysql/summary.d.ts +7 -0
- package/dist/realtime/declarations.d.ts +11 -0
- package/dist/realtime/failure.d.ts +6 -0
- package/dist/realtime/grants.d.ts +22 -0
- package/dist/realtime/handlers.d.ts +86 -0
- package/dist/realtime/hub.d.ts +71 -0
- package/dist/realtime/index.d.ts +105 -0
- package/dist/realtime/index.mjs +824 -0
- package/dist/realtime/jobs.d.ts +18 -0
- package/dist/realtime/message.d.ts +3 -0
- package/dist/realtime/presence.d.ts +33 -0
- package/dist/realtime/retained.d.ts +21 -0
- package/dist/realtime/socket.d.ts +30 -0
- package/dist/render/index.d.ts +10 -0
- package/dist/render/index.mjs +1 -0
- package/dist/render/protocol.d.ts +47 -0
- package/dist/render/remote.d.ts +16 -0
- package/dist/runtimes/bundle.d.ts +29 -0
- package/dist/runtimes/index.d.ts +3 -0
- package/dist/runtimes/index.mjs +229 -0
- package/dist/runtimes/stages.d.ts +52 -0
- package/dist/sqlite/index.d.ts +4 -0
- package/dist/sqlite/index.mjs +160 -0
- package/dist/sqlite/jobQueue.d.ts +35 -0
- package/dist/sqlite/kv.d.ts +34 -0
- package/dist/sqlite/open.d.ts +3 -0
- package/dist/types/config.d.ts +293 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.mjs +1 -0
- package/dist/types/jobs.d.ts +317 -0
- package/dist/worker/elsewhere.d.ts +16 -0
- package/dist/worker/index.d.ts +5 -0
- package/dist/worker/index.mjs +1703 -0
- package/dist/worker/origin.d.ts +12 -0
- package/dist/worker/pageEngine.d.ts +11 -0
- package/dist/worker/stages/action.d.ts +24 -0
- package/dist/worker/stages/cors.d.ts +9 -0
- package/dist/worker/stages/forward.d.ts +12 -0
- package/dist/worker/stages/functionRoutes.d.ts +10 -0
- package/dist/worker/stages/queuedCalls.d.ts +15 -0
- package/dist/worker/stages/realtime.d.ts +9 -0
- package/dist/worker/stages/render.d.ts +9 -0
- package/dist/worker/worker.d.ts +39 -0
- package/package.json +76 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# @plitzi/sdk-actions
|
|
2
|
+
|
|
3
|
+
## 0.39.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- @plitzi/sdk-kernel@0.39.3
|
|
8
|
+
- @plitzi/sdk-shared@0.39.3
|
|
9
|
+
|
|
10
|
+
## 0.39.2
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- fad1264: Released with every `@plitzi` package at this version. What changed in each, and what to do when you upgrade, is in
|
|
15
|
+
`@plitzi/cli`'s `CHANGELOG.md`: one copy of the notes, not one per package.
|
|
16
|
+
- Updated dependencies [fad1264]
|
|
17
|
+
- @plitzi/sdk-kernel@0.39.2
|
|
18
|
+
- @plitzi/sdk-shared@0.39.2
|
package/README.md
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @plitzi/sdk-actions
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The actions engine: server actions, a space's functions and runtimes as the platform holds them, the connectors,
|
|
4
|
+
realtime, the jobs, and the stock queues and key/value stores. It holds a run and its secrets: the credentials a step
|
|
5
|
+
uses, the space's store, the run's log. What runs a space's own code is elsewhere (`@plitzi/sdk-runtime`), and is
|
|
6
|
+
handed to it as a runner; it imports nothing of that package, nor of the page server.
|
|
7
|
+
|
|
8
|
+
It runs in two ways:
|
|
9
|
+
|
|
10
|
+
- **Beside the pages, in one process.** `createPlatform` (`@plitzi/sdk-server/platform`) builds the engine and hands it to
|
|
11
|
+
the page server.
|
|
12
|
+
- **As a worker of its own.** `createWorker` serves `/_action`, webhooks, `/fn/`, `/_realtime` and the jobs on
|
|
13
|
+
`@plitzi/sdk-kernel` alone, beside the page servers or on another machine.
|
|
14
|
+
|
|
15
|
+
## Entries
|
|
16
|
+
|
|
17
|
+
| Entry | What it holds |
|
|
18
|
+
|---|---|
|
|
19
|
+
| `@plitzi/sdk-actions` | `createActionsModule`, the tasks and their catalogue, the kv stores (`createMemoryKv`, `createFileKv`, `createRedisKv`), the job queues and the scheduler, the run log (`createRunLogger`), the connectors |
|
|
20
|
+
| `@plitzi/sdk-actions/worker` | `createWorker`, `createPageEngine` (what a page server is handed), `engineOnWorker` (what it is handed when its actions run on a worker — with `render`, its server elements too) |
|
|
21
|
+
| `@plitzi/sdk-actions/functions` | The host side of functions: their configuration and limits, building and loading a definition (`buildFunctions`, `loadFunctions`), the remote runner's client, `createLocalFunctions` |
|
|
22
|
+
| `@plitzi/sdk-actions/runtimes` | A space's runtime as the platform reaches it: packing and loading one, `serveRuntime`, the proxy stage |
|
|
23
|
+
| `@plitzi/sdk-actions/realtime` | The realtime channels: the hub, presence, grants, the publishing side for a process that serves no page |
|
|
24
|
+
| `@plitzi/sdk-actions/render` | Resolving a page's server element that names an action or a connector: in this process (`createActionResolver`), or on a worker (`createRemoteRenderResolver`, asked at its `/_render` with `RENDER_PROTOCOL`) |
|
|
25
|
+
| `@plitzi/sdk-actions/types` | Types alone: the job queue's contract (`JobQueue`, `ActionSchedule`), the engine's configuration (`EngineOptions`, `EngineConfig`, `SSRActionConfig`) |
|
|
26
|
+
| `@plitzi/sdk-actions/mysql`, `/mongo`, `/sqlite` | The job queue and the kv store over a database the deployment owns |
|
|
27
|
+
|
|
28
|
+
## A worker
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { createWorker } from '@plitzi/sdk-actions/worker';
|
|
32
|
+
|
|
33
|
+
const worker = createWorker({
|
|
34
|
+
adapters: { getSpaceDeployment, getOfflineData },
|
|
35
|
+
identity: auth.ssrIdentityAdapters,
|
|
36
|
+
action: { lookups, kv, jobs: { queue } },
|
|
37
|
+
realtime: { pubsub }
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
await worker.listen(8892);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
It resolves which space a host is and who is calling with the same adapters a page server has (`identity` is
|
|
44
|
+
`@plitzi/sdk-kernel/identity`'s `RequestIdentity`), so a gateway sends those paths of a space's host to it and the
|
|
45
|
+
browser sees one origin. Started with nothing of the engine to run, it refuses and says what to name.
|
|
46
|
+
|
|
47
|
+
The page servers beside it are told where their pages call (`engineOnWorker`), or run the engine for their own renders
|
|
48
|
+
too (`createPlatform` with `action.jobs.workers: 0`). The guide is `@plitzi/sdk-server`'s README, *Actions apart from
|
|
49
|
+
pages*, and `docs/en/server-actions.md`.
|
|
50
|
+
|
|
51
|
+
## What it may import
|
|
52
|
+
|
|
53
|
+
The kernel and `sdk-shared`. Never `@plitzi/sdk-server` nor `@plitzi/sdk-runtime`, types included: ESLint refuses them
|
|
54
|
+
outside its tests. `nodemailer` and `ws` are its own; `mysql2`, `mongodb` and `esbuild` are optional peers, each needed
|
|
55
|
+
only by the entry that uses it.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { isRecord } from "@plitzi/sdk-shared/helpers";
|
|
2
|
+
// src/engine/runtime/credentials.ts
|
|
3
|
+
/** Where a project's server — and `plitzi functions dev` — reads the credentials its space's code names. */
|
|
4
|
+
var CREDENTIALS_ENV = "PLITZI_CREDENTIALS";
|
|
5
|
+
var EXAMPLE = "{\"google\":{\"clientId\":\"…\",\"clientSecret\":\"…\"}}";
|
|
6
|
+
/**
|
|
7
|
+
* The credentials a deployment with no credential store keeps in its environment: one JSON object, credential id → its
|
|
8
|
+
* keys, each a string — what `ctx.fetch({ credential })`, a connector and an `http.request` step name. Read whole or
|
|
9
|
+
* refused with why: a value that does not read would otherwise leave every credential missing, and the integration
|
|
10
|
+
* failing far from the cause.
|
|
11
|
+
*/
|
|
12
|
+
var credentialsFromEnv = (env = process.env) => {
|
|
13
|
+
const raw = env[CREDENTIALS_ENV];
|
|
14
|
+
if (raw === void 0 || raw.trim() === "") return {};
|
|
15
|
+
let value;
|
|
16
|
+
try {
|
|
17
|
+
value = JSON.parse(raw);
|
|
18
|
+
} catch (error) {
|
|
19
|
+
throw new Error(`${CREDENTIALS_ENV} is not JSON: write one object, credential id → its keys — ${EXAMPLE}`, { cause: error });
|
|
20
|
+
}
|
|
21
|
+
if (!isRecord(value)) throw new Error(`${CREDENTIALS_ENV} is one object, credential id → its keys — ${EXAMPLE}`);
|
|
22
|
+
return Object.fromEntries(Object.entries(value).map(([id, keys]) => {
|
|
23
|
+
if (!isRecord(keys) || !Object.values(keys).every((key) => typeof key === "string")) throw new Error(`${CREDENTIALS_ENV}: "${id}" is an object of strings, its keys by name — ${EXAMPLE}`);
|
|
24
|
+
return [id, Object.fromEntries(Object.entries(keys).map(([name, key]) => [name, String(key)]))];
|
|
25
|
+
}));
|
|
26
|
+
};
|
|
27
|
+
export { credentialsFromEnv as t };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { FunctionFailure, isFunctionLimit } from "@plitzi/sdk-shared/functions";
|
|
2
|
+
// src/engine/runtime/triggers.ts
|
|
3
|
+
/**
|
|
4
|
+
* The step that starts a flow, for one way in.
|
|
5
|
+
*
|
|
6
|
+
* An action holds its ways in as trigger STEPS, exactly as an element holds `onClick` and `onSubmit` in one node
|
|
7
|
+
* map — so finding the entry point means finding the trigger whose kind matches what fired, not reading a list
|
|
8
|
+
* beside the flow. An action with no step for that kind cannot be started that way, and that is the whole of the
|
|
9
|
+
* "does it declare this trigger" check.
|
|
10
|
+
*/
|
|
11
|
+
var findTriggerNode = (nodes, kind) => Object.values(nodes).find((node) => node.type === "trigger" && node.action === kind);
|
|
12
|
+
/** What a trigger step carries: who may start it, what it takes, and whatever its kind needs. */
|
|
13
|
+
var triggerParams = (node) => node.params;
|
|
14
|
+
// src/engine/runtime/errors.ts
|
|
15
|
+
/**
|
|
16
|
+
* A run that stopped for a reason the caller is entitled to know, as opposed to one whose step threw.
|
|
17
|
+
*
|
|
18
|
+
* Its own file so the precheck and the runner can both raise it without either importing the other — the
|
|
19
|
+
* precheck is called BY the runner and BEFORE it, and a shared error type is the only thing they have in common.
|
|
20
|
+
*/
|
|
21
|
+
var ActionRunError = class extends Error {
|
|
22
|
+
reason;
|
|
23
|
+
/**
|
|
24
|
+
* The steps a run that STARTED took before it hit this — its deadline, a budget — set by the runner as it rethrows.
|
|
25
|
+
* A refusal before the run began has none. The same outline a completed run returns, and nothing more.
|
|
26
|
+
*/
|
|
27
|
+
steps;
|
|
28
|
+
/** The limit it is, when it is one (its deadline, its step budget…): a ceiling reached, never a fault. */
|
|
29
|
+
limit;
|
|
30
|
+
constructor(reason, message, limit) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.name = "ActionRunError";
|
|
33
|
+
this.reason = reason;
|
|
34
|
+
if (limit) this.limit = limit;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* A step stopped at a limit of what its run may spend — its account's CPU budget — failing that step as any error
|
|
39
|
+
* does, while saying it was a limit and not a fault.
|
|
40
|
+
*/
|
|
41
|
+
var RunLimitReached = class extends Error {
|
|
42
|
+
limit;
|
|
43
|
+
constructor(limit, message) {
|
|
44
|
+
super(message);
|
|
45
|
+
this.name = "RunLimitReached";
|
|
46
|
+
this.limit = limit;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
/** The limit an error that ended a run says it is: a ceiling of the run's, one of its step's, or its code's runner's. */
|
|
50
|
+
var limitOf = (error) => {
|
|
51
|
+
if (error instanceof ActionRunError || error instanceof RunLimitReached) return error.limit;
|
|
52
|
+
return error instanceof FunctionFailure && isFunctionLimit(error.reason) ? error.reason : void 0;
|
|
53
|
+
};
|
|
54
|
+
export { triggerParams as a, findTriggerNode as i, RunLimitReached as n, limitOf as r, ActionRunError as t };
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
// src/engine/runtime/memoryKv.ts
|
|
3
|
+
/** How many writes go by before the map is swept. Cheap enough to be unnoticeable, often enough to bound it. */
|
|
4
|
+
var SWEEP_EVERY = 256;
|
|
5
|
+
/** Every method of the store, for the copy a server's workers share (`FleetStores`, `core/server/fleet/link.ts`). */
|
|
6
|
+
var KV_METHODS = {
|
|
7
|
+
get: true,
|
|
8
|
+
set: true,
|
|
9
|
+
delete: true,
|
|
10
|
+
increment: true,
|
|
11
|
+
expire: true,
|
|
12
|
+
swap: true
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* The store's rules over a map somebody holds: what `createMemoryKv` is, and what a store that keeps the map
|
|
16
|
+
* somewhere (`createFileKv`) is too — so the two answer identically and differ only in where the map lives.
|
|
17
|
+
*
|
|
18
|
+
* An adapter like any other: it stores strings and obeys no rules of its own. What a counter DOES lives in
|
|
19
|
+
* `createKvStore`, so the in-process store and a deployment's own behave identically rather than nearly so.
|
|
20
|
+
*
|
|
21
|
+
* `changed` is told after every write that changed the map — an expired entry swept or read past included.
|
|
22
|
+
*/
|
|
23
|
+
var createMapKv = (entries, changed = () => void 0) => {
|
|
24
|
+
let writes = 0;
|
|
25
|
+
/**
|
|
26
|
+
* Expiry only ever happened on READ, which is fine for a counter that is read as often as it is written and
|
|
27
|
+
* unbounded for everything else: a rate-limit window keyed by caller, an idempotency key per delivery, anything
|
|
28
|
+
* whose key is used once and never asked for again. Those entries expire and stay, and the map is the process's
|
|
29
|
+
* memory. Redis sweeps its own; this is the same courtesy, amortised over the writes that cause the growth.
|
|
30
|
+
*/
|
|
31
|
+
const sweep = () => {
|
|
32
|
+
writes += 1;
|
|
33
|
+
if (writes % SWEEP_EVERY !== 0) return;
|
|
34
|
+
const now = Date.now();
|
|
35
|
+
const before = entries.size;
|
|
36
|
+
entries.forEach((entry, key) => {
|
|
37
|
+
if (entry.expiresAt !== void 0 && entry.expiresAt <= now) entries.delete(key);
|
|
38
|
+
});
|
|
39
|
+
if (entries.size !== before) changed();
|
|
40
|
+
};
|
|
41
|
+
const read = (key) => {
|
|
42
|
+
const entry = entries.get(key);
|
|
43
|
+
if (!entry) return;
|
|
44
|
+
if (entry.expiresAt !== void 0 && entry.expiresAt <= Date.now()) {
|
|
45
|
+
entries.delete(key);
|
|
46
|
+
changed();
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
return entry;
|
|
50
|
+
};
|
|
51
|
+
const expiry = (ttlSeconds) => ttlSeconds === void 0 ? void 0 : Date.now() + ttlSeconds * 1e3;
|
|
52
|
+
const write = (key, entry) => {
|
|
53
|
+
entries.set(key, entry);
|
|
54
|
+
changed();
|
|
55
|
+
sweep();
|
|
56
|
+
};
|
|
57
|
+
return {
|
|
58
|
+
get: (key) => Promise.resolve(read(key)?.value),
|
|
59
|
+
set: (key, value, ttlSeconds) => {
|
|
60
|
+
write(key, {
|
|
61
|
+
value,
|
|
62
|
+
expiresAt: expiry(ttlSeconds)
|
|
63
|
+
});
|
|
64
|
+
return Promise.resolve();
|
|
65
|
+
},
|
|
66
|
+
delete: (key) => {
|
|
67
|
+
if (entries.delete(key)) changed();
|
|
68
|
+
return Promise.resolve();
|
|
69
|
+
},
|
|
70
|
+
increment: (key, amount) => {
|
|
71
|
+
const current = read(key);
|
|
72
|
+
const base = Number(current?.value ?? 0);
|
|
73
|
+
const next = (Number.isFinite(base) ? base : 0) + amount;
|
|
74
|
+
write(key, {
|
|
75
|
+
value: String(next),
|
|
76
|
+
expiresAt: current?.expiresAt
|
|
77
|
+
});
|
|
78
|
+
return Promise.resolve(next);
|
|
79
|
+
},
|
|
80
|
+
expire: (key, ttlSeconds) => {
|
|
81
|
+
const current = read(key);
|
|
82
|
+
if (current) {
|
|
83
|
+
entries.set(key, {
|
|
84
|
+
...current,
|
|
85
|
+
expiresAt: expiry(ttlSeconds)
|
|
86
|
+
});
|
|
87
|
+
changed();
|
|
88
|
+
}
|
|
89
|
+
return Promise.resolve();
|
|
90
|
+
},
|
|
91
|
+
swap: (key, expected, next, ttlSeconds) => {
|
|
92
|
+
if (read(key)?.value !== expected) return Promise.resolve(false);
|
|
93
|
+
write(key, {
|
|
94
|
+
value: next,
|
|
95
|
+
expiresAt: expiry(ttlSeconds)
|
|
96
|
+
});
|
|
97
|
+
return Promise.resolve(true);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* The `kv` tasks' fallback store: in this process, and nowhere else — gone when it stops.
|
|
103
|
+
*
|
|
104
|
+
* Real enough for a single replica and for local work, and deliberately not pretending otherwise — a deployment
|
|
105
|
+
* running several replicas passes an adapter over something shared, because a counter that only counts its own
|
|
106
|
+
* replica is a rate limit that multiplies by the number of them. One that must survive a restart passes
|
|
107
|
+
* `createFileKv` (one process) or `createSqliteKv` (`@plitzi/sdk-actions/sqlite`).
|
|
108
|
+
*/
|
|
109
|
+
var createMemoryKv = () => createMapKv(/* @__PURE__ */ new Map());
|
|
110
|
+
// src/realtime/grants.ts
|
|
111
|
+
/** How long a grant opens its topic when the flow does not say: a day, which outlives any one visit. */
|
|
112
|
+
var DEFAULT_GRANT_SECONDS = 86400;
|
|
113
|
+
/** The longest a grant may last: thirty days. A page that stays open longer asks its action for a new one. */
|
|
114
|
+
var MAX_GRANT_SECONDS = 2592e3;
|
|
115
|
+
/** What a grant looks like on the wire: long enough to be unguessable, and nothing a query string has to escape. */
|
|
116
|
+
var TOKEN = /^[A-Za-z0-9_-]{24,64}$/;
|
|
117
|
+
var parseGrant = (raw) => {
|
|
118
|
+
try {
|
|
119
|
+
const value = JSON.parse(raw ?? "");
|
|
120
|
+
return typeof value === "object" && value !== null && "topic" in value && typeof value.topic === "string" && "generation" in value && typeof value.generation === "number" ? {
|
|
121
|
+
topic: value.topic,
|
|
122
|
+
generation: value.generation
|
|
123
|
+
} : void 0;
|
|
124
|
+
} catch {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
var grantSeconds = (ttlSeconds) => ttlSeconds === void 0 || !Number.isFinite(ttlSeconds) ? DEFAULT_GRANT_SECONDS : Math.min(MAX_GRANT_SECONDS, Math.max(60, Math.floor(ttlSeconds)));
|
|
129
|
+
/**
|
|
130
|
+
* The grants a server has issued, kept in the store its actions' `kv` uses — so a grant one replica issued opens the
|
|
131
|
+
* topic on whichever replica the page connects to, and nothing new has to be configured for it. A random token rather
|
|
132
|
+
* than a signature: there is no secret to share between replicas, and a grant is only ever as old as its lifetime.
|
|
133
|
+
*
|
|
134
|
+
* Keyed under `realtime-grant:`, a prefix no key a flow writes can reach (theirs are `kv:action:<space>:…`).
|
|
135
|
+
*/
|
|
136
|
+
var createRealtimeGrants = (store) => {
|
|
137
|
+
const key = ({ spaceId, environment }, grant) => `realtime-grant:${String(spaceId)}:${environment}:${grant}`;
|
|
138
|
+
/**
|
|
139
|
+
* How many times every grant of a topic was revoked at once. A grant carries the number it was issued under, so
|
|
140
|
+
* raising it voids them all — the ones in use and the ones not used yet — without a list of them to walk.
|
|
141
|
+
*/
|
|
142
|
+
const generationKey = ({ spaceId, environment }, topic) => `realtime-grant-generation:${String(spaceId)}:${environment}:${topic}`;
|
|
143
|
+
const generationOf = async (space, topic) => Number(await store.get(generationKey(space, topic))) || 0;
|
|
144
|
+
return {
|
|
145
|
+
issue: async (space, topic, ttlSeconds) => {
|
|
146
|
+
const grant = randomBytes(18).toString("base64url");
|
|
147
|
+
const stored = {
|
|
148
|
+
topic,
|
|
149
|
+
generation: await generationOf(space, topic)
|
|
150
|
+
};
|
|
151
|
+
await store.set(key(space, grant), JSON.stringify(stored), grantSeconds(ttlSeconds));
|
|
152
|
+
return grant;
|
|
153
|
+
},
|
|
154
|
+
opens: async (space, topic, grant) => {
|
|
155
|
+
if (!TOKEN.test(grant)) return false;
|
|
156
|
+
const stored = parseGrant(await store.get(key(space, grant)));
|
|
157
|
+
return stored?.topic === topic && stored.generation === await generationOf(space, topic);
|
|
158
|
+
},
|
|
159
|
+
revoke: async (space, topic, grant) => {
|
|
160
|
+
if (grant === void 0) {
|
|
161
|
+
await store.increment(generationKey(space, topic), 1);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (TOKEN.test(grant) && parseGrant(await store.get(key(space, grant)))?.topic === topic) await store.delete(key(space, grant));
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
export { createMapKv as a, KV_METHODS as i, MAX_GRANT_SECONDS as n, createMemoryKv as o, createRealtimeGrants as r, DEFAULT_GRANT_SECONDS as t };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { i as buildFunctions } from "./module-WdDzbjHy.mjs";
|
|
2
|
+
import { isRecord } from "@plitzi/sdk-shared/helpers";
|
|
3
|
+
import { readFunctionsSource } from "@plitzi/sdk-shared/actions";
|
|
4
|
+
import { readFile, readdir } from "node:fs/promises";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
// src/functions/load.ts
|
|
7
|
+
var nodeReader = {
|
|
8
|
+
list: async (dir) => (await readdir(dir, { withFileTypes: true })).map((entry) => ({
|
|
9
|
+
name: entry.name,
|
|
10
|
+
directory: entry.isDirectory()
|
|
11
|
+
})),
|
|
12
|
+
read: (file) => readFile(file, "utf8")
|
|
13
|
+
};
|
|
14
|
+
/** Whether a module's default export has a definition's shape: what a server can register without surprises. */
|
|
15
|
+
var isFunctionsDefinition = (value) => {
|
|
16
|
+
if (!isRecord(value)) return false;
|
|
17
|
+
const { tasks, routes, allow } = value;
|
|
18
|
+
const tasksOk = tasks === void 0 || Array.isArray(tasks) && tasks.every((task) => isRecord(task) && typeof task.run === "function");
|
|
19
|
+
const routesOk = routes === void 0 || isRecord(routes) && Object.values(routes).every((route) => typeof route === "function");
|
|
20
|
+
const allowOk = allow === void 0 || isRecord(allow) && (allow.hosts === void 0 || Array.isArray(allow.hosts) && allow.hosts.every((host) => typeof host === "string"));
|
|
21
|
+
return tasksOk && routesOk && allowOk;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* A `functions/` directory as a source — the files the platform's rule counts, by their path inside it: what a space's
|
|
25
|
+
* functions are saved as, and what `loadFunctions` builds.
|
|
26
|
+
*/
|
|
27
|
+
var readFunctionsDir = (dir) => readFunctionsSource(typeof dir === "string" ? dir : fileURLToPath(dir), nodeReader);
|
|
28
|
+
/**
|
|
29
|
+
* A server's own functions from a directory — a project's `src/functions/`, the working copy `plitzi functions` keeps —
|
|
30
|
+
* built exactly as the platform builds a space's (the same rules, one bundle) and loaded natively, for
|
|
31
|
+
* `createServer({ functions: { native } })`. The same on `node src/main.ts` and on a compiled server: nothing here
|
|
32
|
+
* depends on how the server itself runs. A directory with nothing in it is no functions; one that does not build, or
|
|
33
|
+
* none of whose top-level files exports a definition, stops the server with where it is wrong.
|
|
34
|
+
*/
|
|
35
|
+
var loadFunctions = async (dir) => loadFunctionsSource(await readFunctionsDir(dir));
|
|
36
|
+
/**
|
|
37
|
+
* The same, from the files themselves (`FunctionsSource`, by their path): what a packed plugin carries as
|
|
38
|
+
* `functions.source.json`, loaded by a server that runs the plugin as it was packed.
|
|
39
|
+
*/
|
|
40
|
+
var loadFunctionsSource = async (source) => {
|
|
41
|
+
if (!Object.keys(source).length) return [];
|
|
42
|
+
const { code } = await buildFunctions(source);
|
|
43
|
+
const loaded = await import(`data:text/javascript;base64,${Buffer.from(code).toString("base64")}`);
|
|
44
|
+
const definition = isRecord(loaded) ? loaded.default : void 0;
|
|
45
|
+
if (!isFunctionsDefinition(definition)) throw new Error("The functions bundle exports no definition: each file at the top of functions/ exports one by default — export default defineFunctions({ tasks: [ … ] })");
|
|
46
|
+
return [definition];
|
|
47
|
+
};
|
|
48
|
+
export { readFunctionsDir as i, loadFunctions as n, loadFunctionsSource as r, isFunctionsDefinition as t };
|