@plitzi/sdk-server 0.38.23 → 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 +172 -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 +75 -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 +11 -3
- package/dist/hosts/project/serveProject.js +94 -36
- 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 +21 -0
- package/dist/modules/functions/context.js +6 -2
- package/dist/modules/functions/contract.d.ts +45 -1
- package/dist/modules/functions/driver.d.ts +2 -1
- package/dist/modules/functions/driver.js +39 -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 +3 -1
- package/dist/modules/functions/oauth.js +20 -7
- package/dist/modules/functions/protocol.d.ts +9 -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
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { guestBytes } from "./bytes.js";
|
|
2
|
+
import { guestCrypto } from "./crypto.js";
|
|
3
|
+
import { guestEvents } from "./events.js";
|
|
4
|
+
import { guestFetch } from "./fetch.js";
|
|
5
|
+
//#region src/modules/functions/sandbox/guest/index.ts
|
|
6
|
+
/**
|
|
7
|
+
* The runtime a space's code finds in an isolate: the web APIs a bare V8 lacks (`Headers`, `Request`, `Response`,
|
|
8
|
+
* `TextEncoder`, `AbortSignal`, timers, `crypto`) and the `ctx` its functions are handed.
|
|
9
|
+
*
|
|
10
|
+
* It is installed by PRINTING this function into the isolate (`installGuest.toString()`), after the prelude that brings
|
|
11
|
+
* `URL`, `atob`, `structuredClone` and `DOMException` — and after its web APIs, each part printed the same way and
|
|
12
|
+
* handed in built (`parts`). So none of them may use anything from outside its own body — no import, no helper beside
|
|
13
|
+
* it — and all they have of the runner is `host`, the functions in `shared` and the parts before them. Streams are not
|
|
14
|
+
* provided: a body is text or bytes.
|
|
15
|
+
*/
|
|
16
|
+
var installGuest = (scope, host, shared, { bytes, events, fetch, crypto }) => {
|
|
17
|
+
const { isRecord, GuestTextEncoder, GuestTextDecoder } = bytes;
|
|
18
|
+
const { GuestEvent, GuestEventTarget, GuestAbortSignal, GuestAbortController, guestSetTimeout, guestSetInterval, clearTimer } = events;
|
|
19
|
+
const { GuestHeaders, GuestRequest, GuestResponse, wireOf, headerPairs, bodyOfWire } = fetch;
|
|
20
|
+
const { getRandomValues, randomUUID, subtle } = crypto;
|
|
21
|
+
/**
|
|
22
|
+
* A refusal: what the code threw to tell whoever asked — a wrong password, a full board — rather than a fault. It is
|
|
23
|
+
* `ActionRefusal` from `@plitzi/sdk-server/functions`, known here by its name: the bundle carries its own copy of the
|
|
24
|
+
* class, printed from the platform's (`build.ts`).
|
|
25
|
+
*/
|
|
26
|
+
const REFUSAL = "ActionRefusal";
|
|
27
|
+
const isRefusal = (error) => error instanceof Error && error.name === REFUSAL;
|
|
28
|
+
const refusal = (message, code) => Object.assign(new Error(message), { name: REFUSAL }, code === void 0 ? {} : { code });
|
|
29
|
+
const call = async (message) => {
|
|
30
|
+
const answer = JSON.parse(await host.call(JSON.stringify(message)));
|
|
31
|
+
if (isRecord(answer) && answer.ok === true) return answer.value;
|
|
32
|
+
const said = isRecord(answer) && typeof answer.error === "string" ? answer.error : "The platform refused the call";
|
|
33
|
+
if (isRecord(answer) && answer.refused === true) throw refusal(said, typeof answer.code === "string" ? answer.code : void 0);
|
|
34
|
+
throw new Error(said);
|
|
35
|
+
};
|
|
36
|
+
const run = new GuestAbortController();
|
|
37
|
+
const driver = shared.createFunctionsDriver({
|
|
38
|
+
call,
|
|
39
|
+
signal: run.signal,
|
|
40
|
+
responseOf: (wire) => new GuestResponse(bodyOfWire(wire.body), {
|
|
41
|
+
status: typeof wire.status === "number" ? wire.status : 502,
|
|
42
|
+
statusText: typeof wire.statusText === "string" ? wire.statusText : "",
|
|
43
|
+
headers: headerPairs(wire.headers)
|
|
44
|
+
}),
|
|
45
|
+
requestOf: (wire) => new GuestRequest(String(wire.url), {
|
|
46
|
+
method: String(wire.method),
|
|
47
|
+
headers: headerPairs(wire.headers),
|
|
48
|
+
body: wire.method === "GET" || wire.method === "HEAD" ? null : bodyOfWire(wire.body),
|
|
49
|
+
signal: run.signal
|
|
50
|
+
}),
|
|
51
|
+
wireOfResponse: (response) => Promise.resolve(response instanceof GuestResponse ? {
|
|
52
|
+
status: response.status,
|
|
53
|
+
statusText: response.statusText,
|
|
54
|
+
headers: [...response.headers],
|
|
55
|
+
body: wireOf(response)
|
|
56
|
+
} : void 0),
|
|
57
|
+
changeKv: shared.changeKv,
|
|
58
|
+
changeListEntry: shared.changeListEntry,
|
|
59
|
+
refusal
|
|
60
|
+
});
|
|
61
|
+
const { log } = driver;
|
|
62
|
+
const refuseFetch = () => Promise.reject(/* @__PURE__ */ new TypeError("Functions reach the network through ctx.fetch, which knows the hosts they may reach"));
|
|
63
|
+
Object.assign(scope, {
|
|
64
|
+
TextEncoder: GuestTextEncoder,
|
|
65
|
+
TextDecoder: GuestTextDecoder,
|
|
66
|
+
Event: GuestEvent,
|
|
67
|
+
EventTarget: GuestEventTarget,
|
|
68
|
+
AbortSignal: GuestAbortSignal,
|
|
69
|
+
AbortController: GuestAbortController,
|
|
70
|
+
Headers: GuestHeaders,
|
|
71
|
+
Request: GuestRequest,
|
|
72
|
+
Response: GuestResponse,
|
|
73
|
+
setTimeout: guestSetTimeout,
|
|
74
|
+
clearTimeout: clearTimer,
|
|
75
|
+
setInterval: guestSetInterval,
|
|
76
|
+
clearInterval: clearTimer,
|
|
77
|
+
queueMicrotask: (callback) => {
|
|
78
|
+
Promise.resolve().then(callback);
|
|
79
|
+
},
|
|
80
|
+
crypto: {
|
|
81
|
+
getRandomValues,
|
|
82
|
+
randomUUID,
|
|
83
|
+
subtle
|
|
84
|
+
},
|
|
85
|
+
console: {
|
|
86
|
+
log,
|
|
87
|
+
info: log,
|
|
88
|
+
warn: log,
|
|
89
|
+
error: log,
|
|
90
|
+
debug: log
|
|
91
|
+
},
|
|
92
|
+
fetch: refuseFetch
|
|
93
|
+
});
|
|
94
|
+
const describe = (definition) => JSON.stringify({
|
|
95
|
+
ok: true,
|
|
96
|
+
value: shared.describeFunctions(definition)
|
|
97
|
+
});
|
|
98
|
+
return {
|
|
99
|
+
describe,
|
|
100
|
+
invoke: async (definition, invocationText) => {
|
|
101
|
+
let answer;
|
|
102
|
+
try {
|
|
103
|
+
const invocation = JSON.parse(invocationText);
|
|
104
|
+
answer = {
|
|
105
|
+
ok: true,
|
|
106
|
+
value: await driver.invoke(definition, isRecord(invocation) ? invocation : {})
|
|
107
|
+
};
|
|
108
|
+
} catch (error) {
|
|
109
|
+
answer = {
|
|
110
|
+
ok: false,
|
|
111
|
+
error: error instanceof Error ? error.message : String(error),
|
|
112
|
+
...isRefusal(error) ? { refused: true } : {}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
return JSON.stringify(answer);
|
|
117
|
+
} catch (error) {
|
|
118
|
+
return JSON.stringify({
|
|
119
|
+
ok: false,
|
|
120
|
+
error: `What the function answered is not JSON: ${error instanceof Error ? error.message : String(error)}`
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
abort: (reason) => {
|
|
125
|
+
run.abort(new DOMException(reason, "AbortError"));
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
//#endregion
|
|
130
|
+
export { guestBytes, guestCrypto, guestEvents, guestFetch, installGuest };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { changeKv } from '
|
|
2
|
-
import { createFunctionsDriver, describeFunctions } from '
|
|
1
|
+
import { changeKv, changeListEntry } from '../../../actions/runtime/kvChange';
|
|
2
|
+
import { createFunctionsDriver, describeFunctions } from '../../driver';
|
|
3
3
|
/** What the runner hands the guest: calls into the runner's own process, the only way out of the isolate. */
|
|
4
4
|
export type GuestHost = {
|
|
5
5
|
/** A {@link FunctionCall} for the platform, as JSON; answers a `FunctionAnswer`, as JSON. */
|
|
@@ -19,6 +19,7 @@ export type GuestHost = {
|
|
|
19
19
|
export type GuestShared = {
|
|
20
20
|
/** `ctx.kv.change`: the read-change-write loop, over the guest's own `get` and `swap` calls. */
|
|
21
21
|
changeKv: typeof changeKv;
|
|
22
|
+
changeListEntry: typeof changeListEntry;
|
|
22
23
|
/** What a definition declares, as a manifest. */
|
|
23
24
|
describeFunctions: typeof describeFunctions;
|
|
24
25
|
/** An invocation of a definition with its `ctx` — the same driver a space runtime runs. */
|
|
@@ -33,13 +34,5 @@ export type GuestDriver = {
|
|
|
33
34
|
/** The run was aborted: `ctx.signal` fires, for code that listens, before the runner stops it. */
|
|
34
35
|
abort: (reason: string) => void;
|
|
35
36
|
};
|
|
36
|
-
/**
|
|
37
|
-
|
|
38
|
-
* `TextEncoder`, `AbortSignal`, timers, `crypto`) and the `ctx` its functions are handed.
|
|
39
|
-
*
|
|
40
|
-
* It is installed by PRINTING this function into the isolate (`installGuest.toString()`), after the prelude that brings
|
|
41
|
-
* `URL`, `atob`, `structuredClone` and `DOMException`. So it may use nothing from outside its own body — no import,
|
|
42
|
-
* no helper beside it — and all it has of the runner is `host`, and the functions in `shared`. Streams are not
|
|
43
|
-
* provided: a body is text or bytes.
|
|
44
|
-
*/
|
|
45
|
-
export declare const installGuest: (scope: Record<string, unknown>, host: GuestHost, shared: GuestShared) => GuestDriver;
|
|
37
|
+
/** The bytes every part of the guest reads and writes. */
|
|
38
|
+
export type Bytes = Uint8Array<ArrayBuffer>;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import { changeKv } from "../../actions/runtime/kvChange.js";
|
|
1
|
+
import { changeKv, changeListEntry } from "../../actions/runtime/kvChange.js";
|
|
2
2
|
import { FunctionFailure, failedAnswerOf } from "../protocol.js";
|
|
3
|
-
import {
|
|
3
|
+
import { guestBytes } from "./guest/bytes.js";
|
|
4
|
+
import { guestCrypto } from "./guest/crypto.js";
|
|
5
|
+
import { guestEvents } from "./guest/events.js";
|
|
6
|
+
import { guestFetch } from "./guest/fetch.js";
|
|
7
|
+
import { installGuest } from "./guest/index.js";
|
|
4
8
|
import { guestPrelude } from "./prelude.js";
|
|
5
9
|
import { createFunctionsDriver, describeFunctions } from "../driver.js";
|
|
6
10
|
import { createHash, createHmac, pbkdf2, randomBytes } from "node:crypto";
|
|
@@ -111,7 +115,8 @@ var cryptoAnswer = async (message, charge) => {
|
|
|
111
115
|
};
|
|
112
116
|
/**
|
|
113
117
|
* Sets the guest up in a fresh context: the runner's four calls, taken off the global the moment they are captured,
|
|
114
|
-
* and the guest printed from its own source — with the platform's functions it runs
|
|
118
|
+
* and the guest printed from its own source, its web APIs first, a part at a time — with the platform's functions it runs
|
|
119
|
+
* as they are (`GuestShared`),
|
|
115
120
|
* printed from theirs. Answers the driver.
|
|
116
121
|
*/
|
|
117
122
|
var BOOTSTRAP = `(() => {
|
|
@@ -121,12 +126,21 @@ var BOOTSTRAP = `(() => {
|
|
|
121
126
|
delete globalThis.__plitziRandom;
|
|
122
127
|
delete globalThis.__plitziCrypto;
|
|
123
128
|
const transfer = { arguments: { copy: true }, result: { promise: true, copy: true } };
|
|
124
|
-
|
|
129
|
+
const host = {
|
|
125
130
|
call: message => refs.call.apply(undefined, [message], transfer),
|
|
126
131
|
sleep: ms => refs.sleep.apply(undefined, [ms], transfer),
|
|
127
132
|
random: length => refs.random(length),
|
|
128
133
|
crypto: message => refs.crypto.apply(undefined, [message], transfer)
|
|
129
|
-
}
|
|
134
|
+
};
|
|
135
|
+
const bytes = (${guestBytes.toString()})();
|
|
136
|
+
const events = (${guestEvents.toString()})(host);
|
|
137
|
+
const parts = {
|
|
138
|
+
bytes,
|
|
139
|
+
events,
|
|
140
|
+
fetch: (${guestFetch.toString()})(bytes, events),
|
|
141
|
+
crypto: (${guestCrypto.toString()})(host, bytes)
|
|
142
|
+
};
|
|
143
|
+
return (${installGuest.toString()})(globalThis, host, { changeKv: ${changeKv.toString()}, changeListEntry: ${changeListEntry.toString()}, describeFunctions: ${describeFunctions.toString()}, createFunctionsDriver: ${createFunctionsDriver.toString()} }, parts);
|
|
130
144
|
})()`;
|
|
131
145
|
/**
|
|
132
146
|
* The runner as V8 isolates in this process — what the runner service serves, and what a deployment may use directly
|
|
@@ -272,7 +286,7 @@ var createIsolateRunner = ({ concurrency = 8, cacheBytes = 67108864 } = {}) => {
|
|
|
272
286
|
});
|
|
273
287
|
await module.evaluate({ timeout: limits.cpuMs });
|
|
274
288
|
const definition = module.namespace.getSync("default", { reference: true });
|
|
275
|
-
if (definition.typeof !== "object") throw new FunctionFailure("error", "functions
|
|
289
|
+
if (definition.typeof !== "object") throw new FunctionFailure("error", "The functions bundle exports no definition: each file at the top of functions/ exports one by default — export default defineFunctions({ tasks: [ … ] })");
|
|
276
290
|
const text = await drive(driver, definition);
|
|
277
291
|
if (Buffer.byteLength(text) > limits.outputBytes) stop("output");
|
|
278
292
|
if (stopped) throw new FunctionFailure(stopped, stopMessage(stopped, limits));
|
|
@@ -166,19 +166,13 @@ var prepareFunctions = async (source, runner, reserved, ceilings, scope) => {
|
|
|
166
166
|
} catch (error) {
|
|
167
167
|
return {
|
|
168
168
|
ok: false,
|
|
169
|
-
problems: [{
|
|
170
|
-
file: "index.ts",
|
|
171
|
-
message: error instanceof Error ? error.message : String(error)
|
|
172
|
-
}]
|
|
169
|
+
problems: [{ message: error instanceof Error ? error.message : String(error) }]
|
|
173
170
|
};
|
|
174
171
|
}
|
|
175
172
|
const { manifest, problems } = readManifest(declared, reserved, ceilings, scope);
|
|
176
173
|
return problems.length ? {
|
|
177
174
|
ok: false,
|
|
178
|
-
problems: problems.map((message) => ({
|
|
179
|
-
file: "index.ts",
|
|
180
|
-
message
|
|
181
|
-
}))
|
|
175
|
+
problems: problems.map((message) => ({ message }))
|
|
182
176
|
} : {
|
|
183
177
|
ok: true,
|
|
184
178
|
functions: {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ImageTransform } from './transform';
|
|
2
|
-
import {
|
|
2
|
+
import { OutboundOptions } from '../../helpers/outboundGuard';
|
|
3
3
|
/** What a request for a picture asked: the picture, the width, and what the browser takes (its `Accept`). */
|
|
4
4
|
export type ImageRequest = {
|
|
5
5
|
url?: string;
|
|
@@ -18,7 +18,8 @@ export type ImageProxyOptions = {
|
|
|
18
18
|
fetch: typeof fetch;
|
|
19
19
|
/** `sharp`'s, when it is installed; without one a picture is passed through, cached. */
|
|
20
20
|
transform?: ImageTransform;
|
|
21
|
-
lookup
|
|
21
|
+
/** The deployment's own origins a picture may come from on its network (`outbound.privateOrigins`); a test's lookup. */
|
|
22
|
+
outbound?: OutboundOptions;
|
|
22
23
|
now?: () => number;
|
|
23
24
|
};
|
|
24
25
|
/**
|
|
@@ -94,7 +94,7 @@ var sha = (text) => createHash("sha256").update(text).digest("hex");
|
|
|
94
94
|
* its own network.
|
|
95
95
|
*/
|
|
96
96
|
var createImageProxy = (options) => {
|
|
97
|
-
const { domains, cacheDir, transform,
|
|
97
|
+
const { domains, cacheDir, transform, outbound, now = Date.now } = options;
|
|
98
98
|
const failedUntil = /* @__PURE__ */ new Map();
|
|
99
99
|
const revalidating = oneAtATime();
|
|
100
100
|
const fetchingOriginal = oneAtATime();
|
|
@@ -146,7 +146,7 @@ var createImageProxy = (options) => {
|
|
|
146
146
|
response = await fetchOutbound(allowedFetch, source, {
|
|
147
147
|
headers,
|
|
148
148
|
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS)
|
|
149
|
-
},
|
|
149
|
+
}, outbound);
|
|
150
150
|
} catch (error) {
|
|
151
151
|
throw error instanceof HostNotAllowed ? refuse(403, `The picture moved to ${error.message}, which is not an allowed host.`) : refuse(502, "The picture could not be fetched.");
|
|
152
152
|
}
|
|
@@ -3,13 +3,9 @@ import { TERMINAL_JOB_STATUSES } from "@plitzi/sdk-shared/actions";
|
|
|
3
3
|
//#region src/modules/mongo/jobQueue.ts
|
|
4
4
|
/** Mongo's code for a unique index that refused a second document with the same key. */
|
|
5
5
|
var DUPLICATE_KEY = 11e3;
|
|
6
|
-
/** Mongo's code for a collection that does not exist yet. */
|
|
7
|
-
var NAMESPACE_NOT_FOUND = 26;
|
|
8
6
|
/** Spread into a plain array: the queries take a mutable list. */
|
|
9
7
|
var TERMINAL = [...TERMINAL_JOB_STATUSES];
|
|
10
8
|
var DAY_MS = 864e5;
|
|
11
|
-
/** The index jobs were looked up by their action with, before they had a target. */
|
|
12
|
-
var LEGACY_ACTION_INDEX = "spaceId_1_actionId_1_updatedAt_-1";
|
|
13
9
|
var resolveDb = (source) => typeof source === "function" ? source() : source;
|
|
14
10
|
var scheduleId = (spaceId, actionId) => `${spaceId}:${actionId}`;
|
|
15
11
|
/**
|
|
@@ -61,15 +57,6 @@ var createMongoJobQueue = ({ db, collections: names = {}, retentionDays = 90 })
|
|
|
61
57
|
return localTime ? localTime.getTime() : Date.now();
|
|
62
58
|
});
|
|
63
59
|
let ready;
|
|
64
|
-
/** Whether the index jobs were found by before targets is there — on a collection not made yet, nothing is. */
|
|
65
|
-
const legacyIndexIsThere = async () => {
|
|
66
|
-
try {
|
|
67
|
-
return await jobsOf().indexExists(LEGACY_ACTION_INDEX);
|
|
68
|
-
} catch (error) {
|
|
69
|
-
if (error.code === NAMESPACE_NOT_FOUND) return false;
|
|
70
|
-
throw error;
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
60
|
/**
|
|
74
61
|
* The indexes the queue cannot work without, built once on first use.
|
|
75
62
|
*
|
|
@@ -78,12 +65,6 @@ var createMongoJobQueue = ({ db, collections: names = {}, retentionDays = 90 })
|
|
|
78
65
|
* index each and merged already sorted.
|
|
79
66
|
*/
|
|
80
67
|
const build = async () => {
|
|
81
|
-
await jobsOf().updateMany({ target: { $exists: false } }, [{ $set: { target: {
|
|
82
|
-
kind: "action",
|
|
83
|
-
name: "$actionId",
|
|
84
|
-
trigger: "$trigger"
|
|
85
|
-
} } }, { $unset: ["actionId", "trigger"] }]);
|
|
86
|
-
if (await legacyIndexIsThere()) await jobsOf().dropIndex(LEGACY_ACTION_INDEX);
|
|
87
68
|
await Promise.all([
|
|
88
69
|
jobsOf().createIndex({
|
|
89
70
|
status: 1,
|
|
@@ -125,8 +125,8 @@ var createAccountStore = (db, t) => {
|
|
|
125
125
|
return;
|
|
126
126
|
}
|
|
127
127
|
await execute(db, `INSERT INTO ${t.session}
|
|
128
|
-
(account_id, token, expires_at, refresh_token, refresh_expires_at, user_agent, ip, app_name, app_id)
|
|
129
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
|
|
128
|
+
(account_id, token, expires_at, refresh_token, refresh_expires_at, user_agent, ip, app_name, app_id, host)
|
|
129
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
|
|
130
130
|
userId,
|
|
131
131
|
session.token,
|
|
132
132
|
session.expiresAt,
|
|
@@ -135,7 +135,8 @@ var createAccountStore = (db, t) => {
|
|
|
135
135
|
context?.client?.userAgent?.slice(0, 255) ?? null,
|
|
136
136
|
context?.client?.ip ?? null,
|
|
137
137
|
context?.client?.app?.name.slice(0, 120) ?? null,
|
|
138
|
-
context?.client?.app?.softwareId?.slice(0, 64) ?? null
|
|
138
|
+
context?.client?.app?.softwareId?.slice(0, 64) ?? null,
|
|
139
|
+
context?.client?.host?.slice(0, 255) ?? null
|
|
139
140
|
]);
|
|
140
141
|
},
|
|
141
142
|
/**
|
|
@@ -145,6 +146,10 @@ var createAccountStore = (db, t) => {
|
|
|
145
146
|
* and the statement that would sign out an entire deployment must not be reachable by leaving an argument off.
|
|
146
147
|
*/
|
|
147
148
|
clearSession: async (target) => {
|
|
149
|
+
if (target.host !== void 0) {
|
|
150
|
+
if (target.userId !== void 0) await execute(db, `DELETE FROM ${t.session} WHERE account_id = ? AND host = ?`, [target.userId, target.host]);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
148
153
|
const conditions = [];
|
|
149
154
|
const params = [];
|
|
150
155
|
if (target.userId !== void 0) {
|
|
@@ -181,10 +186,12 @@ var createAccountStore = (db, t) => {
|
|
|
181
186
|
},
|
|
182
187
|
createAccount: async (account) => {
|
|
183
188
|
return {
|
|
184
|
-
id: (await execute(db, `INSERT INTO ${t.account} (username, email, password_hash) VALUES (?, ?, ?)`, [
|
|
189
|
+
id: (await execute(db, `INSERT INTO ${t.account} (username, email, password_hash, terms_version, terms_accepted_at) VALUES (?, ?, ?, ?, ?)`, [
|
|
185
190
|
account.username,
|
|
186
191
|
account.email,
|
|
187
|
-
account.passwordHash
|
|
192
|
+
account.passwordHash,
|
|
193
|
+
account.termsAccepted?.version ?? null,
|
|
194
|
+
account.termsAccepted?.at ?? null
|
|
188
195
|
])).insertId,
|
|
189
196
|
username: account.username,
|
|
190
197
|
email: account.email,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createMysqlJobQueue } from './jobQueue';
|
|
2
2
|
export { createMysqlKv } from './kv';
|
|
3
|
-
export { mysqlJobSchemaStatements
|
|
3
|
+
export { mysqlJobSchemaStatements } from './schema';
|
|
4
4
|
export type { MysqlJobQueue, MysqlJobQueueOptions } from './jobQueue';
|
|
5
5
|
export type { MysqlKvOptions } from './kv';
|
|
@@ -22,12 +22,6 @@ export declare const jobTables: (prefix?: string) => JobTables;
|
|
|
22
22
|
* refuses the write, or cuts it short where it is not strict.
|
|
23
23
|
*/
|
|
24
24
|
export declare const mysqlJobSchemaStatements: (prefix?: string) => string[];
|
|
25
|
-
/**
|
|
26
|
-
* What brings tables made before the columns above up to them — for a deployment that runs its own migrations: each
|
|
27
|
-
* statement once, the ones its tables still lack. `ensureJobTables` does it by itself, and only where a table needs
|
|
28
|
-
* it: an `ALTER` copies the table.
|
|
29
|
-
*/
|
|
30
|
-
export declare const mysqlJobSchemaUpgrades: (prefix?: string) => string[];
|
|
31
25
|
/** The server's now, in epoch ms, as an SQL expression — what every comparison reads instead of a caller's clock. */
|
|
32
26
|
export declare const NOW_MS = "CAST(ROUND(UNIX_TIMESTAMP(NOW(3)) * 1000) AS SIGNED)";
|
|
33
27
|
/** Creates the tables once per process, on first use; a failure is retried by the next call rather than remembered. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { execute
|
|
1
|
+
import { execute } from "../query.js";
|
|
2
2
|
//#region src/modules/mysql/jobs/schema.ts
|
|
3
3
|
var PREFIX = /^[A-Za-z0-9_]*$/;
|
|
4
4
|
/**
|
|
@@ -89,49 +89,6 @@ var mysqlJobSchemaStatements = (prefix = "") => {
|
|
|
89
89
|
) ${CHARSET}`
|
|
90
90
|
];
|
|
91
91
|
};
|
|
92
|
-
/** The columns a table of this database has, by name, with their types. */
|
|
93
|
-
var columnsOf = async (db, table) => {
|
|
94
|
-
const columns = await selectRows(db, `SELECT COLUMN_NAME AS name, DATA_TYPE AS type FROM INFORMATION_SCHEMA.COLUMNS
|
|
95
|
-
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?`, [table]);
|
|
96
|
-
return new Map(columns.map((column) => [column.name, column.type.toLowerCase()]));
|
|
97
|
-
};
|
|
98
|
-
var UPGRADES = [
|
|
99
|
-
{
|
|
100
|
-
needed: async (db, prefix) => {
|
|
101
|
-
const columns = await columnsOf(db, `${prefix}action_kv`);
|
|
102
|
-
return columns.get("k") !== "varbinary" || columns.get("v") !== "mediumtext";
|
|
103
|
-
},
|
|
104
|
-
statements: (prefix) => [`ALTER TABLE ${jobTables(prefix).kv} MODIFY k VARBINARY(764) NOT NULL, MODIFY v MEDIUMTEXT NOT NULL`]
|
|
105
|
-
},
|
|
106
|
-
{
|
|
107
|
-
needed: async (db, prefix) => !(await columnsOf(db, `${prefix}action_jobs`)).has("job_key"),
|
|
108
|
-
statements: (prefix) => [`ALTER TABLE ${jobTables(prefix).jobs} ADD COLUMN job_key VARCHAR(191) NULL AFTER missed,
|
|
109
|
-
ADD KEY action_jobs_key (space_id, job_key, status)`]
|
|
110
|
-
},
|
|
111
|
-
{
|
|
112
|
-
needed: async (db, prefix) => !(await columnsOf(db, `${prefix}action_jobs`)).has("target_kind"),
|
|
113
|
-
statements: (prefix) => {
|
|
114
|
-
const { jobs } = jobTables(prefix);
|
|
115
|
-
return [
|
|
116
|
-
`ALTER TABLE ${jobs} ADD COLUMN target_kind VARCHAR(16) NULL AFTER space_id,
|
|
117
|
-
ADD COLUMN target_name VARCHAR(191) NULL AFTER target_kind, ADD COLUMN target MEDIUMTEXT NULL AFTER target_name,
|
|
118
|
-
ADD COLUMN state MEDIUMTEXT NULL, ADD COLUMN progress TEXT NULL, ADD COLUMN result MEDIUMTEXT NULL,
|
|
119
|
-
ADD COLUMN pause_requested TINYINT(1) NOT NULL DEFAULT 0, ADD COLUMN active_ms BIGINT NOT NULL DEFAULT 0`,
|
|
120
|
-
`UPDATE ${jobs} SET target_kind = 'action', target_name = action_id,
|
|
121
|
-
target = JSON_OBJECT('kind', 'action', 'name', action_id, 'trigger', trigger_type)`,
|
|
122
|
-
`ALTER TABLE ${jobs} MODIFY target_kind VARCHAR(16) NOT NULL, MODIFY target_name VARCHAR(191) NOT NULL,
|
|
123
|
-
MODIFY target MEDIUMTEXT NOT NULL, DROP COLUMN action_id, DROP COLUMN trigger_type,
|
|
124
|
-
ADD KEY action_jobs_target (space_id, target_name, updated_at)`
|
|
125
|
-
];
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
];
|
|
129
|
-
/**
|
|
130
|
-
* What brings tables made before the columns above up to them — for a deployment that runs its own migrations: each
|
|
131
|
-
* statement once, the ones its tables still lack. `ensureJobTables` does it by itself, and only where a table needs
|
|
132
|
-
* it: an `ALTER` copies the table.
|
|
133
|
-
*/
|
|
134
|
-
var mysqlJobSchemaUpgrades = (prefix = "") => UPGRADES.flatMap((upgrade) => upgrade.statements(prefix));
|
|
135
92
|
/** The server's now, in epoch ms, as an SQL expression — what every comparison reads instead of a caller's clock. */
|
|
136
93
|
var NOW_MS = "CAST(ROUND(UNIX_TIMESTAMP(NOW(3)) * 1000) AS SIGNED)";
|
|
137
94
|
/** Creates the tables once per process, on first use; a failure is retried by the next call rather than remembered. */
|
|
@@ -141,7 +98,6 @@ var ensureJobTables = (db, prefix, create) => {
|
|
|
141
98
|
if (!create) return;
|
|
142
99
|
ready ??= (async () => {
|
|
143
100
|
for (const statement of mysqlJobSchemaStatements(prefix)) await execute(db, statement);
|
|
144
|
-
for (const upgrade of UPGRADES) if (await upgrade.needed(db, prefix)) for (const statement of upgrade.statements(prefix)) await execute(db, statement);
|
|
145
101
|
})().catch((error) => {
|
|
146
102
|
ready = void 0;
|
|
147
103
|
throw error;
|
|
@@ -150,4 +106,4 @@ var ensureJobTables = (db, prefix, create) => {
|
|
|
150
106
|
};
|
|
151
107
|
};
|
|
152
108
|
//#endregion
|
|
153
|
-
export { NOW_MS, ensureJobTables, jobTables, mysqlJobSchemaStatements
|
|
109
|
+
export { NOW_MS, ensureJobTables, jobTables, mysqlJobSchemaStatements };
|
|
@@ -198,7 +198,7 @@ var step3 = (t) => {
|
|
|
198
198
|
};
|
|
199
199
|
/**
|
|
200
200
|
* What tells one device from another in the account's session list: the application holding it, when it is not a
|
|
201
|
-
* browser (`Plitzi CLI on
|
|
201
|
+
* browser (`Plitzi CLI on dev-laptop`, software `plitzi-cli`), and when it was last used — see `sessionActivity`.
|
|
202
202
|
*/
|
|
203
203
|
var step4 = (t) => {
|
|
204
204
|
const session = unquote(t.session);
|
|
@@ -213,6 +213,23 @@ var step4 = (t) => {
|
|
|
213
213
|
* stays valid for (RFC 6238 §5.2) — see `MfaRecord.lastUsedStep`.
|
|
214
214
|
*/
|
|
215
215
|
var step5 = (t) => [...addColumn(unquote(t.mfa), "last_used_step", "BIGINT NULL AFTER recovery_codes")];
|
|
216
|
+
/**
|
|
217
|
+
* The host a session was signed in on (`SessionClient.host`), and an index to end a site's sessions by: a space's
|
|
218
|
+
* visitors sign in on its own host, and "sign out of this site everywhere" is every session of the account there.
|
|
219
|
+
*/
|
|
220
|
+
var step6 = (t) => {
|
|
221
|
+
const session = unquote(t.session);
|
|
222
|
+
return [...addColumn(session, "host", "VARCHAR(255) NULL AFTER app_id"), ...addIndex(session, "session_account_host", "(account_id, host(191))")];
|
|
223
|
+
};
|
|
224
|
+
/**
|
|
225
|
+
* The terms an account was opened under, and when they were accepted (`AuthApiConfig.terms`, `termsAccepted` on
|
|
226
|
+
* `createAccount`): the record of what each person agreed to. Null for an account opened before a deployment named
|
|
227
|
+
* terms, or through another provider.
|
|
228
|
+
*/
|
|
229
|
+
var step7 = (t) => {
|
|
230
|
+
const account = unquote(t.account);
|
|
231
|
+
return [...addColumn(account, "terms_version", "VARCHAR(32) NULL AFTER verified"), ...addColumn(account, "terms_accepted_at", "DATETIME NULL AFTER terms_version")];
|
|
232
|
+
};
|
|
216
233
|
/** Append only. A released version is never edited — the deployments that already ran it would never see the change. */
|
|
217
234
|
var STEPS = [
|
|
218
235
|
{
|
|
@@ -234,6 +251,14 @@ var STEPS = [
|
|
|
234
251
|
{
|
|
235
252
|
version: 5,
|
|
236
253
|
statements: step5
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
version: 6,
|
|
257
|
+
statements: step6
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
version: 7,
|
|
261
|
+
statements: step7
|
|
237
262
|
}
|
|
238
263
|
];
|
|
239
264
|
var SCHEMA_VERSION = STEPS[STEPS.length - 1].version;
|
|
@@ -9,7 +9,7 @@ import { execute, selectRows } from "./query.js";
|
|
|
9
9
|
*/
|
|
10
10
|
var createSessionStore = (db, t) => {
|
|
11
11
|
const list = async (accountId, currentToken) => {
|
|
12
|
-
return (await selectRows(db, `SELECT id, token, user_agent, ip, app_name, app_id, created_at, last_active_at, expires_at
|
|
12
|
+
return (await selectRows(db, `SELECT id, token, user_agent, ip, app_name, app_id, host, created_at, last_active_at, expires_at
|
|
13
13
|
FROM ${t.session}
|
|
14
14
|
WHERE account_id = ?
|
|
15
15
|
ORDER BY created_at DESC`, [accountId])).map((row) => ({
|
|
@@ -20,6 +20,7 @@ var createSessionStore = (db, t) => {
|
|
|
20
20
|
name: row.app_name,
|
|
21
21
|
...row.app_id ? { softwareId: row.app_id } : {}
|
|
22
22
|
} } : {},
|
|
23
|
+
...row.host ? { host: row.host } : {},
|
|
23
24
|
createdAt: Math.floor(row.created_at.getTime() / 1e3),
|
|
24
25
|
...row.last_active_at ? { lastActiveAt: row.last_active_at } : {},
|
|
25
26
|
expiresAt: row.expires_at,
|
|
@@ -6,8 +6,11 @@ import { RawResponse } from '../../helpers/buildResponseHelpers';
|
|
|
6
6
|
import { ChannelDeclaration, RealtimeSender, SSRRequest, SSRResponseHelpers } from '@plitzi/sdk-shared';
|
|
7
7
|
export type RealtimeRefusal = {
|
|
8
8
|
topic: string;
|
|
9
|
-
/**
|
|
10
|
-
|
|
9
|
+
/**
|
|
10
|
+
* `ungranted`: the channel is declared `grant: true`, and no grant the page sent opens this topic. `unavailable`: the
|
|
11
|
+
* deployment does not open this space's channels (`realtime.admitSpace`).
|
|
12
|
+
*/
|
|
13
|
+
reason: 'undeclared' | 'unauthenticated' | 'forbidden' | 'ungranted' | 'unavailable';
|
|
11
14
|
};
|
|
12
15
|
/** An answer in HTTP terms, whichever transport carries it: a status, and what went wrong when something did. */
|
|
13
16
|
export type RealtimeAnswer = {
|
|
@@ -37,7 +40,7 @@ export type Admission = {
|
|
|
37
40
|
* declares and against the visitor's access. One that fails is reported, not a reason to refuse the rest; all failing
|
|
38
41
|
* is a `403`.
|
|
39
42
|
*/
|
|
40
|
-
export declare const admit: (req: SSRRequest, resolveChannels: ChannelResolver, grants: RealtimeGrants, jobs?: JobLookup) => Promise<Admission>;
|
|
43
|
+
export declare const admit: (req: SSRRequest, resolveChannels: ChannelResolver, grants: RealtimeGrants, jobs?: JobLookup, admitSpace?: (spaceId: number) => Promise<string | undefined>) => Promise<Admission>;
|
|
41
44
|
/** A new connection for what was admitted, speaking through `send`. */
|
|
42
45
|
export declare const connectionFor: (admission: Extract<Admission, {
|
|
43
46
|
ok: true;
|
|
@@ -63,6 +66,8 @@ export type SubscribeDeps = {
|
|
|
63
66
|
grants: RealtimeGrants;
|
|
64
67
|
/** The way to the jobs `$job:<id>` is about — absent where the server runs none. */
|
|
65
68
|
jobs?: JobLookup;
|
|
69
|
+
/** The deployment's say on whether a space opens its channels (`realtime.admitSpace`). */
|
|
70
|
+
admitSpace?: (spaceId: number) => Promise<string | undefined>;
|
|
66
71
|
};
|
|
67
72
|
/**
|
|
68
73
|
* `GET /_realtime?topics=a,b` (and `&grants=g1,g2` for the topics of a `grant: true` channel) — one Server-Sent Events connection for every topic a page listens to.
|
|
@@ -70,7 +75,7 @@ export type SubscribeDeps = {
|
|
|
70
75
|
* The first event, `ready`, tells the page its public name (`from`) and the secret its publishes carry. Resolves when
|
|
71
76
|
* the page goes.
|
|
72
77
|
*/
|
|
73
|
-
export declare const handleRealtimeSubscribe: ({ req, res, raw, signal, hub, resolveChannels, grants, jobs }: SubscribeDeps) => Promise<void>;
|
|
78
|
+
export declare const handleRealtimeSubscribe: ({ req, res, raw, signal, hub, resolveChannels, grants, jobs, admitSpace }: SubscribeDeps) => Promise<void>;
|
|
74
79
|
export type PublishDeps = {
|
|
75
80
|
req: SSRRequest;
|
|
76
81
|
res: SSRResponseHelpers;
|
|
@@ -29,7 +29,7 @@ var grantFor = async (grants, space, topic, offered) => {
|
|
|
29
29
|
* declares and against the visitor's access. One that fails is reported, not a reason to refuse the rest; all failing
|
|
30
30
|
* is a `403`.
|
|
31
31
|
*/
|
|
32
|
-
var admit = async (req, resolveChannels, grants, jobs) => {
|
|
32
|
+
var admit = async (req, resolveChannels, grants, jobs, admitSpace) => {
|
|
33
33
|
const { environment = "main", spaceId, revision } = req.ctx.spaceDeployment ?? {};
|
|
34
34
|
if (typeof spaceId !== "number") return {
|
|
35
35
|
ok: false,
|
|
@@ -50,6 +50,7 @@ var admit = async (req, resolveChannels, grants, jobs) => {
|
|
|
50
50
|
}
|
|
51
51
|
};
|
|
52
52
|
const channels = await resolveChannels(spaceId, environment, revision);
|
|
53
|
+
const spaceRefusal = await admitSpace?.(spaceId);
|
|
53
54
|
const accepted = /* @__PURE__ */ new Map();
|
|
54
55
|
const granted = /* @__PURE__ */ new Map();
|
|
55
56
|
const refused = [];
|
|
@@ -68,6 +69,13 @@ var admit = async (req, resolveChannels, grants, jobs) => {
|
|
|
68
69
|
else accepted.set(topic, JOB_CHANNEL);
|
|
69
70
|
continue;
|
|
70
71
|
}
|
|
72
|
+
if (spaceRefusal !== void 0) {
|
|
73
|
+
refused.push({
|
|
74
|
+
topic,
|
|
75
|
+
reason: "unavailable"
|
|
76
|
+
});
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
71
79
|
const match = matchChannel(topic, channels);
|
|
72
80
|
const refusal = match ? accessRefusal(match.declaration.access, req.ctx.user) : "undeclared";
|
|
73
81
|
const grant = match && !refusal && match.declaration.grant === true ? await grantFor(grants, space, topic, offered) : void 0;
|
|
@@ -84,6 +92,15 @@ var admit = async (req, resolveChannels, grants, jobs) => {
|
|
|
84
92
|
if (grant !== void 0) granted.set(topic, grant);
|
|
85
93
|
}
|
|
86
94
|
}
|
|
95
|
+
if (accepted.size === 0 && spaceRefusal !== void 0) return {
|
|
96
|
+
ok: false,
|
|
97
|
+
answer: {
|
|
98
|
+
status: 402,
|
|
99
|
+
error: spaceRefusal,
|
|
100
|
+
reason: "unavailable",
|
|
101
|
+
refused
|
|
102
|
+
}
|
|
103
|
+
};
|
|
87
104
|
if (accepted.size === 0) return {
|
|
88
105
|
ok: false,
|
|
89
106
|
answer: {
|
|
@@ -189,8 +206,8 @@ var publishFrom = async (hub, connection, { topic, type, data, echo }) => {
|
|
|
189
206
|
* The first event, `ready`, tells the page its public name (`from`) and the secret its publishes carry. Resolves when
|
|
190
207
|
* the page goes.
|
|
191
208
|
*/
|
|
192
|
-
var handleRealtimeSubscribe = async ({ req, res, raw, signal, hub, resolveChannels, grants, jobs }) => {
|
|
193
|
-
const admission = await admit(req, resolveChannels, grants, jobs);
|
|
209
|
+
var handleRealtimeSubscribe = async ({ req, res, raw, signal, hub, resolveChannels, grants, jobs, admitSpace }) => {
|
|
210
|
+
const admission = await admit(req, resolveChannels, grants, jobs, admitSpace);
|
|
194
211
|
if (!admission.ok) {
|
|
195
212
|
answer(res, admission.answer);
|
|
196
213
|
return;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RealtimePresence } from './presence';
|
|
1
2
|
import { RealtimeRetained } from './retained';
|
|
2
3
|
import { ChannelDeclaration, PubSubAdapter, RealtimeMessage, RealtimeSender } from '@plitzi/sdk-shared';
|
|
3
4
|
/** The space a connection belongs to: its topics are that space's and no other's. */
|
|
@@ -39,7 +40,7 @@ export type RealtimeHub = ReturnType<typeof createRealtimeHub>;
|
|
|
39
40
|
* A hundred pages on one board are a hundred connections here and a single subscription to the adapter: a message
|
|
40
41
|
* arrives once and is written to each of them. The last one to leave a topic unsubscribes it.
|
|
41
42
|
*/
|
|
42
|
-
export declare const createRealtimeHub: (pubsub: PubSubAdapter, retained: RealtimeRetained) => {
|
|
43
|
+
export declare const createRealtimeHub: (pubsub: PubSubAdapter, retained: RealtimeRetained, presence?: RealtimePresence) => {
|
|
43
44
|
publish: (space: RealtimeSpace, message: RealtimeMessage) => Promise<void>;
|
|
44
45
|
from: (connection: RealtimeConnection, topic: string, type: string, data: unknown, echo?: boolean) => RealtimeMessage;
|
|
45
46
|
find: (token: string) => RealtimeConnection | undefined;
|
|
@@ -51,7 +52,8 @@ export declare const createRealtimeHub: (pubsub: PubSubAdapter, retained: Realti
|
|
|
51
52
|
/**
|
|
52
53
|
* Subscribes a new connection to its topics, hands it what each `retain: true` one keeps, and tells the members of
|
|
53
54
|
* each presence channel it arrived. What is kept is read AFTER joining, so nothing said in between is missed; a
|
|
54
|
-
* message heard live and then kept arrives twice, and the page keeps the newer of a type by its `at`.
|
|
55
|
+
* message heard live and then kept arrives twice, and the page keeps the newer of a type by its `at`. Each kept one
|
|
56
|
+
* is `replayed`: a page that joins tells it from what is said now.
|
|
55
57
|
*/
|
|
56
58
|
connect: (connection: RealtimeConnection) => Promise<void>;
|
|
57
59
|
/** Unsubscribes a connection that went away, and tells the channels it announced itself on. */
|