@plitzi/sdk-server 0.38.17 → 0.38.18
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 +70 -0
- package/README.md +29 -14
- package/dist/core/auth/csrf.d.ts +16 -11
- package/dist/core/auth/csrf.js +35 -17
- package/dist/core/http/stages/crossSiteWrites.d.ts +5 -4
- package/dist/core/http/stages/crossSiteWrites.js +6 -5
- package/dist/core/http/stages/livePages.d.ts +43 -0
- package/dist/core/http/stages/livePages.js +80 -0
- package/dist/core/server/baseServer.d.ts +6 -2
- package/dist/core/server/baseServer.js +1 -0
- package/dist/core/server/pageServer.js +11 -8
- package/dist/helpers/cache/keys.js +1 -1
- package/dist/hosts/authoringHost/index.d.ts +13 -5
- package/dist/hosts/authoringHost/index.js +29 -24
- package/dist/hosts/project/plugins.d.ts +1 -1
- package/dist/hosts/project/plugins.js +1 -1
- package/dist/hosts/project/serveProject.d.ts +1 -1
- package/dist/hosts/project/serveProject.js +2 -2
- package/dist/hosts/project/spaceFailure.js +2 -2
- package/dist/modules/realtime/socket.js +1 -1
- package/dist/modules/rsc/publicFileResolver.js +1 -1
- package/dist/modules/ssr/prepareRender.js +5 -3
- package/dist/modules/ssr/views/template.ejs +18 -9
- package/dist/plugins/watch.d.ts +1 -1
- package/dist/plugins/watch.js +1 -1
- package/package.json +3 -3
- package/dist/core/http/stages/devReload.d.ts +0 -29
- package/dist/core/http/stages/devReload.js +0 -57
- /package/dist/core/{createServer.devReload.test.d.ts → createServer.livePages.test.d.ts} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,75 @@
|
|
|
1
1
|
# @plitzi/sdk-server
|
|
2
2
|
|
|
3
|
+
## 0.38.18
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- b3023c9: ## Security
|
|
8
|
+
|
|
9
|
+
- **A page of the same site on another origin is no longer this server's own** (`@plitzi/sdk-server`): the guard
|
|
10
|
+
against writes another page caused (`crossSiteWrites`, the sign-in's login-CSRF check and `createOriginGuardMiddleware`)
|
|
11
|
+
trusted `Sec-Fetch-Site: same-site` — and a site has no port and spans every sub-domain, so another port of the host
|
|
12
|
+
(where `createAuthoringHost` serves pages an agent wrote) or a sibling sub-domain (every space of a platform on one
|
|
13
|
+
domain) could write as if it were the server's own page. Now only `same-origin` (and `none`, a typed URL) is; `same-site`
|
|
14
|
+
is refused 403 `reason: 'foreign'` unless its origin is named (`crossSiteWrites.origins`, `identity.platformOrigins`),
|
|
15
|
+
and the `Origin` fallback compares host and port. In production an app and its spaces are already on different sites,
|
|
16
|
+
so a deployment that works there needs nothing; one that relied on a sibling sub-domain locally names it.
|
|
17
|
+
`createAuthoringHost`'s docs now say to serve on a host of its own — a cookie belongs to a host whatever its port.
|
|
18
|
+
|
|
19
|
+
## Server
|
|
20
|
+
- **An open page takes a new version of its space where it stands** (`@plitzi/sdk-server`, `@plitzi/plitzi-sdk`):
|
|
21
|
+
`server.updatePages(documents, { draft? })` hands the pages showing that space — or that draft — its new documents,
|
|
22
|
+
and each swaps them in place, its state, scroll and focus kept (`replaceSpace` on what the SDK's `render` returns;
|
|
23
|
+
`false`, and the page loads again, for a page with elements the server draws). The development loop of a project uses
|
|
24
|
+
it: a save to the space no longer reloads the open page. `createAuthoringHost`'s `put` over an id that pages show
|
|
25
|
+
hands them the new version and answers how many took it (`updated`), so a page an agent keeps improving is seen to
|
|
26
|
+
grow instead of reloading — no second iframe needed.
|
|
27
|
+
- **`devReload` is `livePages`** (`@plitzi/sdk-server`): the channel the open pages listen on now carries the space,
|
|
28
|
+
not only "load again", and a host that is not developing uses it too. Its address is `/__plitzi/live`; a draft's page
|
|
29
|
+
listens for its own changes alone (`?__pt=`). A plugin swapped where it is drawn stays a development server's
|
|
30
|
+
(`devMode`). `plitzi upgrade migrations --write` renames the option in a project's code (`livePagesOption`).
|
|
31
|
+
|
|
32
|
+
## Packages
|
|
33
|
+
- **`@plitzi/sdk-server` starts in half the time** (`@plitzi/sdk-shared`, `@plitzi/sdk-server`): importing it took
|
|
34
|
+
~310 ms, ~200 of them `date-fns` — 108 modules nothing on the server uses, brought by `@plitzi/sdk-shared/helpers` —
|
|
35
|
+
and loaded the browser's hooks and contexts of every folder of `@plitzi/sdk-shared` it read, `@plitzi/nexus/react`
|
|
36
|
+
with them. Now ~150 ms (`@plitzi/sdk-shared` 139 → 113 modules, `@plitzi/nexus` 53 → 31). What moved, each
|
|
37
|
+
rewritten by `plitzi upgrade migrations --write` (`movedExports`):
|
|
38
|
+
- the dates — `formatDate`, `formatDateUTC`, `formatFromNow`, `formatUTCToLocal`, `getDurationMs`, `isDate`,
|
|
39
|
+
`isValidFormat`, `parseDate`, `toUnixSeconds`, `SupportedLocale` — from `@plitzi/sdk-shared/helpers` to
|
|
40
|
+
`@plitzi/sdk-shared/dates`;
|
|
41
|
+
- React's bindings to an entry of their own beside each area, as `@plitzi/nexus/react` is: `useDisplayMode`,
|
|
42
|
+
`useElementSize` → `@plitzi/sdk-shared/style/react`; `SchemaContext` → `schema/react`; `PluginsContext` →
|
|
43
|
+
`plugins/react`; `DevToolsContext` → `devTools/react`; `useFlagResolution` → `flags/react` (`flagUserFrom` stays
|
|
44
|
+
in `flags`);
|
|
45
|
+
- `PROJECT_DATA_PREFIX` from `@plitzi/sdk-shared/server` to `@plitzi/sdk-shared/project/paths` — the project's
|
|
46
|
+
layout names, an entry a browser imports too (`@plitzi/sdk-shared/project` stays Node only and re-exports them).
|
|
47
|
+
The theme cookie's helpers (`themeFromCookies`…) are also `@plitzi/sdk-shared/theme/cookie`, still exported by
|
|
48
|
+
`@plitzi/sdk-shared/theme`.
|
|
49
|
+
|
|
50
|
+
`yarn check:server-footprint` (in CI after the build) holds what the server's root loads, package by package — Plitzi's
|
|
51
|
+
and every other — to a budget: a heavy library or a folder of hooks coming back fails with what came in.
|
|
52
|
+
|
|
53
|
+
## CLI
|
|
54
|
+
- **`plitzi upgrade migrations --write` writes a file import's new home when it has one** (`@plitzi/cli`): the
|
|
55
|
+
`folderImports` migration of 0.38.17 named every import of a package's FILE and wrote none. It now reads the installed
|
|
56
|
+
declarations with the project's own TypeScript and rewrites each `import` / `export … from` whose every name the folder
|
|
57
|
+
exports under one name — `import usePlitzi from '@plitzi/sdk-shared/hooks/usePlitzi'` →
|
|
58
|
+
`import { usePlitzi } from '@plitzi/sdk-shared/hooks'`, `type` modifiers and aliases kept. What has no one answer is
|
|
59
|
+
said with why (a namespace import, an `import()` or mock, a name the folder does not export, a project with no
|
|
60
|
+
TypeScript). The report says which places `--write` writes (`writable` in `--json`) instead of calling every one
|
|
61
|
+
"yours to change"; a migration now gives each place its own `edit` in place of a whole-file `rewrite`
|
|
62
|
+
(`docs/en/releases.md`).
|
|
63
|
+
- **`plitzi doctor` says an import of a path the installed package does not export** (`@plitzi/cli`):
|
|
64
|
+
`import-not-exported`, an error, at the importing file — what Node otherwise reports at start as an
|
|
65
|
+
`ERR_MODULE_NOT_FOUND` into the package's `dist`, which reads as a broken install rather than a changed API. For a
|
|
66
|
+
`@plitzi` package the fix names `plitzi upgrade migrations --write`; for any other, its `exports`. Asked only of a
|
|
67
|
+
package that is installed.
|
|
68
|
+
|
|
69
|
+
- Updated dependencies [b3023c9]
|
|
70
|
+
- @plitzi/plitzi-sdk@0.38.18
|
|
71
|
+
- @plitzi/sdk-shared@0.38.18
|
|
72
|
+
|
|
3
73
|
## 0.38.17
|
|
4
74
|
|
|
5
75
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -235,16 +235,18 @@ What it wires from the project:
|
|
|
235
235
|
records the name, the port, the URL this machine reaches it at (`https` with a certificate), its network addresses
|
|
236
236
|
and its `publicUrl` — for `plitzi page check`, `shot`, `verify` and `visual`. A signal closes it (`closeOnSignals`).
|
|
237
237
|
|
|
238
|
-
While developing — `NODE_ENV` other than `production` — `devMode` and `
|
|
238
|
+
While developing — `NODE_ENV` other than `production` — `devMode` and `livePages` are on, and:
|
|
239
239
|
|
|
240
240
|
- **A save to the space is authored again** by the project's `plitzi/author.ts`, in a process of its own (an import is
|
|
241
241
|
never read twice in one process), run with `--ipc`: it hands the documents back over the IPC channel, the server
|
|
242
|
-
swaps them in memory and every open page
|
|
243
|
-
|
|
242
|
+
swaps them in memory and hands them to every open page, which swaps the space where it stands — its state, scroll and
|
|
243
|
+
focus kept (`updatePages`); a page with elements the server draws loads again instead. What the script refuses it
|
|
244
|
+
prints, and the page keeps the last space that authored. A save to the server's own code — `main.ts`, `src/config/`,
|
|
244
245
|
`src/actions/`, `src/connectors/`, `src/functions/`, `src/runtime/` — is `start:dev`'s to restart on; of a plugin,
|
|
245
246
|
only its `declaration.ts` is the space's.
|
|
246
247
|
- **A plugin folder added** is registered and the open pages load again; one removed is turned off; a plugin's
|
|
247
|
-
`functions/` changed is loaded again in place. A plugin's component changed is
|
|
248
|
+
`functions/` changed is loaded again in place. A plugin's component changed is swapped where it is drawn (`livePages`
|
|
249
|
+
with `devMode`). A folder
|
|
248
250
|
it cannot build — no entry yet, a name that is no type — or a `functions/` with no `index.ts` is said in the
|
|
249
251
|
terminal as the layout check says it (`[plugins] …`, once until it changes), and the server goes on; the folder is
|
|
250
252
|
registered once it is whole.
|
|
@@ -263,7 +265,7 @@ what `serveProject` wires itself (see [Projects](#projects)).
|
|
|
263
265
|
| `httpVersion` | `1 \| 2 \| 3` | `2` with `tls`, else `1` | HTTP protocol version. Falls back to the nearest available lower version. |
|
|
264
266
|
| `tls` | `{ key, cert, minVersion? }` | — | TLS key and certificate. Required for versions 2 and 3; optional for version 1. |
|
|
265
267
|
| `devMode` | `boolean` | `false` | Enables development mode: appends `?dev` to esm.sh CDN URLs for React, and activates per-request timing metrics (see [Dev metrics](#dev-metrics)). Off, the process must run with `NODE_ENV=production` or React renders with its development build; the server says so once, at `error`. |
|
|
266
|
-
| `
|
|
268
|
+
| `livePages` | `boolean` | `false` | Every page this server renders listens for what changes under it: new documents for the space it shows, swapped where it stands with its state, scroll and focus kept (`server.updatePages(documents, { draft? })` — a draft's pages hear their own alone), or everything loaded again (`server.reloadPages()`); and it loads again when it reconnects to a server that restarted (a new process), so a change to the server's code reaches the open page. What `serveProject` uses while developing, and `createAuthoringHost` always. With `devMode` too, a plugin whose source changes is built again and swapped where it is drawn on every open page; a plugin whose declaration changed loads the page again. Not turned on by `devMode`: each open page holds a connection for it. |
|
|
267
269
|
| `assetVersion` | `string` | — | Cache-buster appended as `?v=<assetVersion>` to all default SDK asset URLs. Compute from file mtime or package version at startup. |
|
|
268
270
|
| `cacheTtlMs` | `number` | `300000` | TTL in milliseconds for the SSR render cache. Set to `0` to disable. |
|
|
269
271
|
| `flags` | `Record<string, boolean> \| ({ spaceId, environment }) => Record<string, boolean>` | — | This deployment's say over the spaces' feature flags: above what a space declares, below the SDK's `flags` prop and a tester's dev tools, and only for flags the space declares. A function answers per space. Server actions and RSC resolve with it too. See `docs/en/feature-flags.md` in the workspace. |
|
|
@@ -405,8 +407,9 @@ per page and needs it served with the quality of a project's, without trusting w
|
|
|
405
407
|
import { authorIsolated } from '@plitzi/sdk-authoring/node';
|
|
406
408
|
import { createAuthoringHost } from '@plitzi/sdk-server';
|
|
407
409
|
|
|
410
|
+
// The app is at https://localhost:8080; the pages at https://127.0.0.1:8091 — another HOST, not only another port.
|
|
408
411
|
const host = createAuthoringHost({ dir: 'state/pages', port: 8091, tls, frameAncestors: ['https://localhost:8080'] });
|
|
409
|
-
await host.server.listen(8091, '
|
|
412
|
+
await host.server.listen(8091, '127.0.0.1');
|
|
410
413
|
|
|
411
414
|
const result = await authorIsolated('tmp/pages/weather.ts'); // its own process: reads the module and its packages, nothing else
|
|
412
415
|
if (result.ok) {
|
|
@@ -419,14 +422,22 @@ if (result.ok) {
|
|
|
419
422
|
- **`authorIsolated`** authors the module in a process Node's permission model holds to reading the module's folder and
|
|
420
423
|
the packages it imports: no file written, no process or worker started, no variable of the caller's environment. A
|
|
421
424
|
module that runs too long is killed (`timeoutMs`), and what it answers is validated again in the caller, so one that
|
|
422
|
-
forges its answer gains nothing. Before Node 25 the permission model leaves the network open: serve the result on
|
|
423
|
-
|
|
425
|
+
forges its answer gains nothing. Before Node 25 the permission model leaves the network open: serve the result on a
|
|
426
|
+
host of its own, never the app's.
|
|
424
427
|
- **`createAuthoringHost`** serves each page as a draft of its one space, at the address `put` answers — the way a page
|
|
425
428
|
server tells many spaces apart on one host (a space is chosen by host or credential, never by path). The page
|
|
426
429
|
arrives with its whole space, navigation is the browser's, and a reload that lost the address finds it in the draft's
|
|
427
430
|
cookie. So a page here is data and its look: no server provider, action, function or channel of its own. Anything
|
|
428
431
|
but a page it holds answers `404`, saying where they are.
|
|
429
|
-
- **
|
|
432
|
+
- **Seen to grow:** `put` again under an id and the pages open at it take the new version where they stand — scroll,
|
|
433
|
+
focus and what the person was doing kept, no reload (`livePages`, on for the host) — and `updated` says how many did.
|
|
434
|
+
An agent that improves a page in steps shows each one as it is ready.
|
|
435
|
+
- **On a host of its own, never another port of the app's.** A cookie belongs to a host whatever its port, so a page
|
|
436
|
+
at `localhost:8091` reads the app's readable cookies at `localhost:8080` and its requests carry the app's session;
|
|
437
|
+
and another port, or a sibling sub-domain, is the same SITE, which the app's page server refuses writes from (it
|
|
438
|
+
trusts its own origin and the ones it names, never the site). Locally, `127.0.0.1` beside `localhost`; in production,
|
|
439
|
+
a domain of its own (`pages-example.net` beside `example.com`), as user content is served everywhere.
|
|
440
|
+
- **Checked like a project's page:** `plitzi page check "/?__pt=weather" --url https://127.0.0.1:8091 --space
|
|
430
441
|
tmp/pages/weather.ts` — the CLI's whole check (elements on screen, hydration, console, contrast) against that module.
|
|
431
442
|
|
|
432
443
|
## JSON adapters (offline mode)
|
|
@@ -1251,9 +1262,12 @@ After that it depends on what the flow does, because there are two different att
|
|
|
1251
1262
|
| **`/auth/exchange`** | Its own grant | It acts for a space and is already refused unless the origin is one that space declared — narrower than any list here. A space is embedded on somebody else's domain by design |
|
|
1252
1263
|
| **`/auth/refresh`, `/auth/logout`** | Nothing | Both authenticate with the refresh credential and must work when the access token has lapsed. Forging either gains an attacker nothing: renewing somebody's session hands the new credential to their own browser |
|
|
1253
1264
|
|
|
1254
|
-
**"A site this deployment recognises"** is decided by two headers a page cannot
|
|
1255
|
-
browser's own account of where the request came from
|
|
1256
|
-
this
|
|
1265
|
+
**"A site this deployment recognises"** is an ORIGIN — host and port, not just the site — decided by two headers a page cannot
|
|
1266
|
+
set: `Sec-Fetch-Site` — the browser's own account of where the request came from, where only `same-origin` (or `none`,
|
|
1267
|
+
a typed URL) is this server's own — and, where that is absent, `Origin`, matched exactly against this host or
|
|
1268
|
+
`identity.platformOrigins`, the hosts you already declared. **`same-site` is not trusted**: another port of this host,
|
|
1269
|
+
or a sibling sub-domain, is the same site, and is exactly where somebody else's code is served by design — an
|
|
1270
|
+
authoring host, every space of a platform on sub-domains of one domain. A sibling that is yours is named. **Neither header present means the caller
|
|
1257
1271
|
is not a browser**, and a client that is not a browser has no victim's session in it to forge with — which is what
|
|
1258
1272
|
lets every API client, mobile app and script sign in with nothing extra to send.
|
|
1259
1273
|
|
|
@@ -1293,13 +1307,14 @@ There is nothing to configure: the secret, the cookie scope and the origins all
|
|
|
1293
1307
|
|
|
1294
1308
|
Everything a page server answers past its self-gating flows — server actions (`/_action`), realtime publishes, a
|
|
1295
1309
|
space's `/fn/` routes, its runtime's endpoints, your own `data` stages, and the adapter-only `loginPath`, `logoutPath`
|
|
1296
|
-
and `exchangePath` — refuses a write that a browser says **another
|
|
1310
|
+
and `exchangePath` — refuses a write that a browser says **a page of another origin caused**, with `403` and `reason: 'foreign'`.
|
|
1297
1311
|
**Whether or not a session cookie came with it**: a server holding credentials of its own (a token for somebody's
|
|
1298
1312
|
calendar, an API key) or a tool nobody signs in to acts for whoever reaches it, and a plain `text/plain` POST from any
|
|
1299
1313
|
page open in the same browser reaches it without a preflight.
|
|
1300
1314
|
|
|
1301
1315
|
The same two headers decide, under the same rules: what passes is a safe method, anything with `Authorization`, a
|
|
1302
|
-
caller that is not a browser (a webhook, `curl`), a page of this
|
|
1316
|
+
caller that is not a browser (a webhook, `curl`), a page of this origin (`same-origin`, never merely `same-site`),
|
|
1317
|
+
and what you name:
|
|
1303
1318
|
|
|
1304
1319
|
```ts
|
|
1305
1320
|
createServer({
|
package/dist/core/auth/csrf.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export interface CsrfConfig {
|
|
|
6
6
|
/** Naming and scope for the cookie, so it lands beside the session one. */
|
|
7
7
|
cookie?: SSRAuthCookie;
|
|
8
8
|
/**
|
|
9
|
-
* Origins that count as this deployment's own, beyond the request's own
|
|
9
|
+
* Origins that count as this deployment's own, beyond the request's own origin.
|
|
10
10
|
*
|
|
11
11
|
* What it decides is which cross-site page may hand out a session — see `foreign` below. `createAuth` fills it
|
|
12
12
|
* from `identity.platformOrigins`, so a deployment that already declared its hosts has said this too, and this
|
|
@@ -57,24 +57,29 @@ export declare const carrierOf: (req: {
|
|
|
57
57
|
body?: unknown;
|
|
58
58
|
}, method?: string | undefined) => CsrfCarrier;
|
|
59
59
|
/**
|
|
60
|
-
* Did
|
|
60
|
+
* Did a page of another ORIGIN cause this request — `allowed` naming the origins this deployment counts as its own?
|
|
61
61
|
*
|
|
62
62
|
* This is what protects signing in, where there is no session to bind a token to and no cookie to look for, and
|
|
63
63
|
* every write a page server answers, cookie or none (`crossSiteWritesStage`). It rests on two headers a page CANNOT
|
|
64
64
|
* set — the browser attaches both, and script may not override either:
|
|
65
65
|
*
|
|
66
66
|
* - **`Sec-Fetch-Site`** is the browser's own account of where the request came from, and is believed first.
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* derivable here.
|
|
67
|
+
* `same-origin`, and `none` (a typed URL, a bookmark), are this server's own; `same-site` and `cross-site` are a
|
|
68
|
+
* page of another origin, foreign unless its origin was named.
|
|
70
69
|
* - **`Origin`** is the fallback, for a browser too old to send the above — Safari only did from 16.4. Matched
|
|
71
|
-
* **exactly
|
|
70
|
+
* **exactly** — host and port; one port does not serve two schemes — against this host or an origin the deployment
|
|
71
|
+
* named.
|
|
72
72
|
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
73
|
+
* **`same-site` is not trusted, and was once.** A site is a registrable domain, with no port in it: another port of
|
|
74
|
+
* this host, or a sibling sub-domain, is the same site. Both are where somebody else's code is served by design —
|
|
75
|
+
* `createAuthoringHost` on a port of its own, every space of a platform on a sub-domain of one domain (two spaces at
|
|
76
|
+
* `*.plitzi.app` are one site until that domain is on the public suffix list) — and trusting the site let any of them
|
|
77
|
+
* write here as if they were this server's own pages. A sibling that IS this deployment's own is named, as it always
|
|
78
|
+
* had to be in production, where the app and its spaces are on different sites: `createAuth` fills the list from
|
|
79
|
+
* `identity.platformOrigins`, a page server from `crossSiteWrites.origins`.
|
|
80
|
+
*
|
|
81
|
+
* Nor is a domain boundary guessed: this once compared *registrable domains*, last-two-labels, and with no public
|
|
82
|
+
* suffix list every other `.co.uk` counted as the same site as a deployment at `acme.co.uk`.
|
|
78
83
|
*
|
|
79
84
|
* **Neither header means this is not a browser**, and a client that is not a browser cannot be made to forge
|
|
80
85
|
* anything: there is no victim's session sitting in it. That is what keeps every API client, mobile app and
|
package/dist/core/auth/csrf.js
CHANGED
|
@@ -41,7 +41,7 @@ var csrfFailureMessage = {
|
|
|
41
41
|
malformed: "CSRF token malformed",
|
|
42
42
|
expired: "CSRF token expired",
|
|
43
43
|
mismatch: "CSRF token does not match this session",
|
|
44
|
-
foreign: "A request from another
|
|
44
|
+
foreign: "A request from another origin may not act with this session"
|
|
45
45
|
};
|
|
46
46
|
/**
|
|
47
47
|
* The carrier, read off whatever request a host hands over — named field by field, never spread.
|
|
@@ -75,33 +75,51 @@ var readCookieHeader = (carrier, name) => {
|
|
|
75
75
|
if (index !== -1 && part.slice(0, index).trim() === name) return decodeURIComponent(part.slice(index + 1).trim());
|
|
76
76
|
}
|
|
77
77
|
};
|
|
78
|
-
/**
|
|
79
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Whether `origin` is the request's own: its host and port, as the `Host` header names them. When a proxy rewrote
|
|
80
|
+
* `Host` (it no longer names the hostname the request is answered as), the hostname alone is what is left to compare.
|
|
81
|
+
*/
|
|
82
|
+
var isOwnOrigin = (origin, carrier) => {
|
|
83
|
+
let url;
|
|
84
|
+
try {
|
|
85
|
+
url = new URL(origin);
|
|
86
|
+
} catch {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
const host = header(carrier, "host");
|
|
90
|
+
if (host === void 0) return url.hostname === carrier.hostname;
|
|
91
|
+
let own;
|
|
80
92
|
try {
|
|
81
|
-
|
|
93
|
+
own = new URL(`${url.protocol}//${host}`);
|
|
82
94
|
} catch {
|
|
83
|
-
return;
|
|
95
|
+
return false;
|
|
84
96
|
}
|
|
97
|
+
return own.hostname === carrier.hostname ? url.host === own.host : url.hostname === carrier.hostname;
|
|
85
98
|
};
|
|
86
99
|
/**
|
|
87
|
-
* Did
|
|
100
|
+
* Did a page of another ORIGIN cause this request — `allowed` naming the origins this deployment counts as its own?
|
|
88
101
|
*
|
|
89
102
|
* This is what protects signing in, where there is no session to bind a token to and no cookie to look for, and
|
|
90
103
|
* every write a page server answers, cookie or none (`crossSiteWritesStage`). It rests on two headers a page CANNOT
|
|
91
104
|
* set — the browser attaches both, and script may not override either:
|
|
92
105
|
*
|
|
93
106
|
* - **`Sec-Fetch-Site`** is the browser's own account of where the request came from, and is believed first.
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
* derivable here.
|
|
107
|
+
* `same-origin`, and `none` (a typed URL, a bookmark), are this server's own; `same-site` and `cross-site` are a
|
|
108
|
+
* page of another origin, foreign unless its origin was named.
|
|
97
109
|
* - **`Origin`** is the fallback, for a browser too old to send the above — Safari only did from 16.4. Matched
|
|
98
|
-
* **exactly
|
|
110
|
+
* **exactly** — host and port; one port does not serve two schemes — against this host or an origin the deployment
|
|
111
|
+
* named.
|
|
112
|
+
*
|
|
113
|
+
* **`same-site` is not trusted, and was once.** A site is a registrable domain, with no port in it: another port of
|
|
114
|
+
* this host, or a sibling sub-domain, is the same site. Both are where somebody else's code is served by design —
|
|
115
|
+
* `createAuthoringHost` on a port of its own, every space of a platform on a sub-domain of one domain (two spaces at
|
|
116
|
+
* `*.plitzi.app` are one site until that domain is on the public suffix list) — and trusting the site let any of them
|
|
117
|
+
* write here as if they were this server's own pages. A sibling that IS this deployment's own is named, as it always
|
|
118
|
+
* had to be in production, where the app and its spaces are on different sites: `createAuth` fills the list from
|
|
119
|
+
* `identity.platformOrigins`, a page server from `crossSiteWrites.origins`.
|
|
99
120
|
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
* every other `.co.uk` in the world counted as the same site and could sign a visitor in. Guessing where a
|
|
103
|
-
* domain boundary falls is not something to do in a security decision — a sibling sub-domain that needs to be
|
|
104
|
-
* trusted is one line in `allowedOrigins`, which `createAuth` already fills from `identity.platformOrigins`.
|
|
121
|
+
* Nor is a domain boundary guessed: this once compared *registrable domains*, last-two-labels, and with no public
|
|
122
|
+
* suffix list every other `.co.uk` counted as the same site as a deployment at `acme.co.uk`.
|
|
105
123
|
*
|
|
106
124
|
* **Neither header means this is not a browser**, and a client that is not a browser cannot be made to forge
|
|
107
125
|
* anything: there is no victim's session sitting in it. That is what keeps every API client, mobile app and
|
|
@@ -111,9 +129,9 @@ var crossSiteRequest = (carrier, allowed = () => false) => {
|
|
|
111
129
|
const site = header(carrier, "sec-fetch-site");
|
|
112
130
|
const origin = header(carrier, "origin");
|
|
113
131
|
const vouched = origin !== void 0 && allowed(origin);
|
|
114
|
-
if (site !== void 0) return site
|
|
132
|
+
if (site !== void 0) return site !== "same-origin" && site !== "none" && !vouched;
|
|
115
133
|
if (!origin) return false;
|
|
116
|
-
return
|
|
134
|
+
return !isOwnOrigin(origin, carrier) && !vouched;
|
|
117
135
|
};
|
|
118
136
|
var createCsrf = (config) => {
|
|
119
137
|
const { secret, cookie, allowedOrigins = [] } = config;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseContext, Stage } from '../types';
|
|
2
2
|
/**
|
|
3
|
-
* Whether this request is a write a page
|
|
3
|
+
* Whether this request is a write a page of another origin caused, and so is refused — answered here, 403
|
|
4
4
|
* `reason: 'foreign'`, when it is.
|
|
5
5
|
*
|
|
6
6
|
* Not only a write a session cookie could authenticate: a server holding credentials of its own (a token for somebody's
|
|
@@ -8,15 +8,16 @@ import { BaseContext, Stage } from '../types';
|
|
|
8
8
|
* page open in the same browser reaches it without a preflight. Passes:
|
|
9
9
|
*
|
|
10
10
|
* - a safe method;
|
|
11
|
-
* - `Authorization` — a page
|
|
11
|
+
* - `Authorization` — a page of another origin cannot set it without a preflight this server never allows;
|
|
12
12
|
* - a request that is not a browser's (no `Sec-Fetch-Site`, no `Origin`): a webhook, `curl`, another server;
|
|
13
|
-
* - a page of this
|
|
13
|
+
* - a page of this origin (`same-origin` — never merely `same-site`: another port of this host, or a sibling
|
|
14
|
+
* sub-domain, is where somebody else's pages are served), or of an origin in `crossSiteWrites.origins`;
|
|
14
15
|
* - a path in `crossSiteWrites.paths` — a form other sites post to by design.
|
|
15
16
|
*/
|
|
16
17
|
export declare const refusedCrossSite: (ctx: BaseContext) => boolean;
|
|
17
18
|
/**
|
|
18
19
|
* Every write the page server answers after it — server actions, realtime publishes, a space's `/fn/` routes and its
|
|
19
|
-
* runtime's endpoints, the deployment's own data stages — refused when another
|
|
20
|
+
* runtime's endpoints, the deployment's own data stages — refused when a page of another origin caused it (see
|
|
20
21
|
* {@link refusedCrossSite}). After the stages that gate themselves (`preAuth`: the `/auth` flows with their own CSRF,
|
|
21
22
|
* MCP and its OAuth, the preview's shared secret), before everything else.
|
|
22
23
|
*/
|
|
@@ -7,7 +7,7 @@ var SAFE_METHODS = /* @__PURE__ */ new Set([
|
|
|
7
7
|
"OPTIONS"
|
|
8
8
|
]);
|
|
9
9
|
/**
|
|
10
|
-
* Whether this request is a write a page
|
|
10
|
+
* Whether this request is a write a page of another origin caused, and so is refused — answered here, 403
|
|
11
11
|
* `reason: 'foreign'`, when it is.
|
|
12
12
|
*
|
|
13
13
|
* Not only a write a session cookie could authenticate: a server holding credentials of its own (a token for somebody's
|
|
@@ -15,9 +15,10 @@ var SAFE_METHODS = /* @__PURE__ */ new Set([
|
|
|
15
15
|
* page open in the same browser reaches it without a preflight. Passes:
|
|
16
16
|
*
|
|
17
17
|
* - a safe method;
|
|
18
|
-
* - `Authorization` — a page
|
|
18
|
+
* - `Authorization` — a page of another origin cannot set it without a preflight this server never allows;
|
|
19
19
|
* - a request that is not a browser's (no `Sec-Fetch-Site`, no `Origin`): a webhook, `curl`, another server;
|
|
20
|
-
* - a page of this
|
|
20
|
+
* - a page of this origin (`same-origin` — never merely `same-site`: another port of this host, or a sibling
|
|
21
|
+
* sub-domain, is where somebody else's pages are served), or of an origin in `crossSiteWrites.origins`;
|
|
21
22
|
* - a path in `crossSiteWrites.paths` — a form other sites post to by design.
|
|
22
23
|
*/
|
|
23
24
|
var refusedCrossSite = (ctx) => {
|
|
@@ -28,14 +29,14 @@ var refusedCrossSite = (ctx) => {
|
|
|
28
29
|
res.setStatus(403);
|
|
29
30
|
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
|
30
31
|
res.send(JSON.stringify({
|
|
31
|
-
error: `A page
|
|
32
|
+
error: `A page of another origin may not ${req.method} ${req.path} here: name its origin in the server's \`crossSiteWrites.origins\`, or the path in \`crossSiteWrites.paths\``,
|
|
32
33
|
reason: "foreign"
|
|
33
34
|
}));
|
|
34
35
|
return true;
|
|
35
36
|
};
|
|
36
37
|
/**
|
|
37
38
|
* Every write the page server answers after it — server actions, realtime publishes, a space's `/fn/` routes and its
|
|
38
|
-
* runtime's endpoints, the deployment's own data stages — refused when another
|
|
39
|
+
* runtime's endpoints, the deployment's own data stages — refused when a page of another origin caused it (see
|
|
39
40
|
* {@link refusedCrossSite}). After the stages that gate themselves (`preAuth`: the `/auth` flows with their own CSRF,
|
|
40
41
|
* MCP and its OAuth, the preview's shared secret), before everything else.
|
|
41
42
|
*/
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Stage } from '../types';
|
|
2
|
+
import { OfflineDataRaw, PluginEntry } from '@plitzi/sdk-shared';
|
|
3
|
+
/** Where an open page listens for what the server has to tell it: its space changed, a plugin was built again. */
|
|
4
|
+
export declare const LIVE_PAGES_PATH = "/__plitzi/live";
|
|
5
|
+
/**
|
|
6
|
+
* The pages open on this server, told what changed under them without the server restarting.
|
|
7
|
+
*
|
|
8
|
+
* - **The space** (`updatePages`): every page showing it swaps it where it stands — its state, scroll and focus kept —
|
|
9
|
+
* or loads again when it cannot (`replaceSpace` of the SDK's `render`). The edit loop of a project that authors its
|
|
10
|
+
* space in code (`serveProject` re-authors and hands the new documents over), and a page an agent keeps rewriting
|
|
11
|
+
* (`createAuthoringHost`'s `update`). A page listens for the space it was drawn with: the host's own, or one draft
|
|
12
|
+
* (`?__pt=<id>`, named by the page in its listening address — never by the cookie, which one browser shares between
|
|
13
|
+
* every draft it has open).
|
|
14
|
+
* - **Everything** (`reloadPages`): every page loads again.
|
|
15
|
+
* - **A plugin built again** (`plugin`): every page imports the new bundle and swaps it where it is drawn.
|
|
16
|
+
*
|
|
17
|
+
* Only with `livePages` on: every open page keeps a connection for it, which a server whose space never changes under
|
|
18
|
+
* it — a published one, or a platform's — would pay for and get nothing from. The template listens only then too.
|
|
19
|
+
*
|
|
20
|
+
* Each connection is greeted with this process's `boot`, and a page that reconnects to a different one loads again: the
|
|
21
|
+
* server restarted — `start:dev` saw its code change (`main.ts`, its options, its actions, `src/functions/`) — and
|
|
22
|
+
* what is on screen was built by the one before. Without it the page kept the old plugin until somebody reloaded it,
|
|
23
|
+
* which reads as "my change did nothing".
|
|
24
|
+
*/
|
|
25
|
+
export declare const createLivePages: () => {
|
|
26
|
+
stage: Stage;
|
|
27
|
+
/** Every page open on this server loads again. */
|
|
28
|
+
reload: () => void;
|
|
29
|
+
/**
|
|
30
|
+
* The pages showing the host's own space — or, with `draft`, that draft — swap it for `documents` where they stand;
|
|
31
|
+
* one that cannot loads again. How many pages were told.
|
|
32
|
+
*/
|
|
33
|
+
update: (documents: OfflineDataRaw, { draft }?: {
|
|
34
|
+
draft?: string;
|
|
35
|
+
}) => number;
|
|
36
|
+
/**
|
|
37
|
+
* A plugin built again: every open page imports its new bundle and swaps it where it is drawn (`keyName` is what
|
|
38
|
+
* the page registered it as), and points its stylesheet at the new one — without loading again.
|
|
39
|
+
*/
|
|
40
|
+
plugin: ({ keyName, js, css }: PluginEntry) => void;
|
|
41
|
+
/** The streams ended — the server is going away, and each page reconnects to whichever comes up next. */
|
|
42
|
+
close: () => void;
|
|
43
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { openEventStream } from "../sse.js";
|
|
2
|
+
import "../../previewToken.js";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
//#region src/core/http/stages/livePages.ts
|
|
5
|
+
/** Where an open page listens for what the server has to tell it: its space changed, a plugin was built again. */
|
|
6
|
+
var LIVE_PAGES_PATH = "/__plitzi/live";
|
|
7
|
+
/**
|
|
8
|
+
* The pages open on this server, told what changed under them without the server restarting.
|
|
9
|
+
*
|
|
10
|
+
* - **The space** (`updatePages`): every page showing it swaps it where it stands — its state, scroll and focus kept —
|
|
11
|
+
* or loads again when it cannot (`replaceSpace` of the SDK's `render`). The edit loop of a project that authors its
|
|
12
|
+
* space in code (`serveProject` re-authors and hands the new documents over), and a page an agent keeps rewriting
|
|
13
|
+
* (`createAuthoringHost`'s `update`). A page listens for the space it was drawn with: the host's own, or one draft
|
|
14
|
+
* (`?__pt=<id>`, named by the page in its listening address — never by the cookie, which one browser shares between
|
|
15
|
+
* every draft it has open).
|
|
16
|
+
* - **Everything** (`reloadPages`): every page loads again.
|
|
17
|
+
* - **A plugin built again** (`plugin`): every page imports the new bundle and swaps it where it is drawn.
|
|
18
|
+
*
|
|
19
|
+
* Only with `livePages` on: every open page keeps a connection for it, which a server whose space never changes under
|
|
20
|
+
* it — a published one, or a platform's — would pay for and get nothing from. The template listens only then too.
|
|
21
|
+
*
|
|
22
|
+
* Each connection is greeted with this process's `boot`, and a page that reconnects to a different one loads again: the
|
|
23
|
+
* server restarted — `start:dev` saw its code change (`main.ts`, its options, its actions, `src/functions/`) — and
|
|
24
|
+
* what is on screen was built by the one before. Without it the page kept the old plugin until somebody reloaded it,
|
|
25
|
+
* which reads as "my change did nothing".
|
|
26
|
+
*/
|
|
27
|
+
var createLivePages = () => {
|
|
28
|
+
/** Each open page's stream, by the space it shows: `''` the host's own, otherwise the draft's token. */
|
|
29
|
+
const listening = /* @__PURE__ */ new Map();
|
|
30
|
+
const boot = randomUUID();
|
|
31
|
+
const stage = (ctx) => {
|
|
32
|
+
if (!ctx.config.livePages || ctx.req.path !== "/__plitzi/live") return Promise.resolve(false);
|
|
33
|
+
const stream = openEventStream(ctx.rawRes, {
|
|
34
|
+
onAbort: () => listening.delete(stream),
|
|
35
|
+
retryMs: 1e3
|
|
36
|
+
});
|
|
37
|
+
listening.set(stream, ctx.req.query["__pt"] ?? "");
|
|
38
|
+
stream.send("hello", { boot });
|
|
39
|
+
ctx.raw.once("close", () => {
|
|
40
|
+
listening.delete(stream);
|
|
41
|
+
stream.close();
|
|
42
|
+
});
|
|
43
|
+
return Promise.resolve(true);
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
stage,
|
|
47
|
+
/** Every page open on this server loads again. */
|
|
48
|
+
reload: () => listening.forEach((_, stream) => stream.send("reload", {})),
|
|
49
|
+
/**
|
|
50
|
+
* The pages showing the host's own space — or, with `draft`, that draft — swap it for `documents` where they stand;
|
|
51
|
+
* one that cannot loads again. How many pages were told.
|
|
52
|
+
*/
|
|
53
|
+
update: (documents, { draft = "" } = {}) => {
|
|
54
|
+
let told = 0;
|
|
55
|
+
listening.forEach((scope, stream) => {
|
|
56
|
+
if (scope === draft) {
|
|
57
|
+
stream.send("space", { documents });
|
|
58
|
+
told += 1;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
return told;
|
|
62
|
+
},
|
|
63
|
+
/**
|
|
64
|
+
* A plugin built again: every open page imports its new bundle and swaps it where it is drawn (`keyName` is what
|
|
65
|
+
* the page registered it as), and points its stylesheet at the new one — without loading again.
|
|
66
|
+
*/
|
|
67
|
+
plugin: ({ keyName, js, css }) => listening.forEach((_, stream) => stream.send("plugin", {
|
|
68
|
+
key: keyName,
|
|
69
|
+
js,
|
|
70
|
+
css
|
|
71
|
+
})),
|
|
72
|
+
/** The streams ended — the server is going away, and each page reconnects to whichever comes up next. */
|
|
73
|
+
close: () => {
|
|
74
|
+
listening.forEach((_, stream) => stream.close());
|
|
75
|
+
listening.clear();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
//#endregion
|
|
80
|
+
export { LIVE_PAGES_PATH, createLivePages };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Handler } from '../transports';
|
|
2
|
-
import { CacheManager, PluginRegistry, SSRServer, SSRServerConfig } from '@plitzi/sdk-shared';
|
|
2
|
+
import { CacheManager, OfflineDataRaw, PluginRegistry, SSRServer, SSRServerConfig } from '@plitzi/sdk-shared';
|
|
3
3
|
export interface HttpServerParts {
|
|
4
4
|
label: string;
|
|
5
5
|
cache: CacheManager | null;
|
|
@@ -18,8 +18,12 @@ export interface HttpServerParts {
|
|
|
18
18
|
/** What the primary does once, before it starts the workers — work each of them would otherwise repeat, or race
|
|
19
19
|
* the others to do. */
|
|
20
20
|
beforeFork?: () => Promise<void>;
|
|
21
|
-
/** The open pages told to load again (`SSRServer.reloadPages`) — a page server
|
|
21
|
+
/** The open pages told to load again (`SSRServer.reloadPages`) — a page server with `livePages`; nothing elsewhere. */
|
|
22
22
|
reloadPages?: () => void;
|
|
23
|
+
/** The open pages handed new documents (`SSRServer.updatePages`) — a page server with `livePages`; none elsewhere. */
|
|
24
|
+
updatePages?: (documents: OfflineDataRaw, options?: {
|
|
25
|
+
draft?: string;
|
|
26
|
+
}) => number;
|
|
23
27
|
/** A plugin's server half replaced while the server runs (`SSRServer.functions`) — a page server that runs actions. */
|
|
24
28
|
setPluginFunctions?: (plugin: string, definition: unknown) => void;
|
|
25
29
|
}
|
|
@@ -35,6 +35,7 @@ var createHttpServer = (config, makeHandlerForPort, parts) => {
|
|
|
35
35
|
cache: channel && parts.cache ? relayCache(parts.cache, `${channel}.cache`) : parts.cache,
|
|
36
36
|
plugins: channel && parts.forgetPlugins ? relayPlugins(parts.plugins, parts.forgetPlugins, `${channel}.plugins`, label) : parts.plugins,
|
|
37
37
|
reloadPages: parts.reloadPages ?? (() => void 0),
|
|
38
|
+
updatePages: parts.updatePages ?? (() => 0),
|
|
38
39
|
functions: { setPlugin: parts.setPluginFunctions ?? (() => {
|
|
39
40
|
throw new Error(`[${label}] This server runs no actions, so no plugin's functions`);
|
|
40
41
|
}) },
|
|
@@ -16,7 +16,7 @@ import { compileTemplate } from "../../modules/ssr/template.js";
|
|
|
16
16
|
import { PluginManager } from "../../plugins/manager.js";
|
|
17
17
|
import { watchPluginSources } from "../../plugins/watch.js";
|
|
18
18
|
import { makeHandler } from "../http/dispatcher.js";
|
|
19
|
-
import {
|
|
19
|
+
import { createLivePages } from "../http/stages/livePages.js";
|
|
20
20
|
import { buildPagePipeline } from "../services/registry.js";
|
|
21
21
|
//#region src/core/server/pageServer.ts
|
|
22
22
|
/** The page-serving machinery: html/rsc caches, the render template and the plugin manager, driving the page
|
|
@@ -52,19 +52,19 @@ var createPageServer = (config, services, extensions) => {
|
|
|
52
52
|
};
|
|
53
53
|
const actions = actionsModuleFor(config);
|
|
54
54
|
const realtime = realtimeModuleFor(config);
|
|
55
|
-
const
|
|
55
|
+
const livePages = config.livePages ? createLivePages() : void 0;
|
|
56
56
|
/**
|
|
57
57
|
* A plugin whose source changed is built again and handed to the open pages, which swap it where it is drawn — the
|
|
58
|
-
* rest of the page, its state included, stays as it was. Only while developing with `
|
|
58
|
+
* rest of the page, its state included, stays as it was. Only while developing (`devMode`) with `livePages` on: nobody edits a
|
|
59
59
|
* deployment's plugins under it, and a page that does not listen has nothing to swap.
|
|
60
60
|
*/
|
|
61
|
-
const stopWatchingPlugins =
|
|
61
|
+
const stopWatchingPlugins = livePages && config.devMode ? watchPluginSources(pluginManager, (key) => {
|
|
62
62
|
invalidatePluginComponentCache();
|
|
63
63
|
pluginManager.rebuild(key).then((entry) => {
|
|
64
|
-
if (entry?.js)
|
|
64
|
+
if (entry?.js) livePages.plugin(entry);
|
|
65
65
|
});
|
|
66
66
|
}) : void 0;
|
|
67
|
-
const stages = [...
|
|
67
|
+
const stages = [...livePages ? [livePages.stage] : [], ...buildPagePipeline(services, extensions)];
|
|
68
68
|
const makeHandlerForPort = (port) => {
|
|
69
69
|
const buildContext = (raw, rawRes, req, res) => ({
|
|
70
70
|
raw,
|
|
@@ -99,9 +99,12 @@ var createPageServer = (config, services, extensions) => {
|
|
|
99
99
|
onClosing: () => {
|
|
100
100
|
realtime?.close();
|
|
101
101
|
stopWatchingPlugins?.();
|
|
102
|
-
|
|
102
|
+
livePages?.close();
|
|
103
103
|
},
|
|
104
|
-
...
|
|
104
|
+
...livePages ? {
|
|
105
|
+
reloadPages: livePages.reload,
|
|
106
|
+
updatePages: livePages.update
|
|
107
|
+
} : {},
|
|
105
108
|
...actions ? { setPluginFunctions: (plugin, definition) => {
|
|
106
109
|
if (definition !== void 0 && !isFunctionsDefinition(definition)) throw new Error(`[SSR] The functions of plugin "${plugin}" are not a definition: defineFunctions({ … })`);
|
|
107
110
|
actions.setPluginFunctions(plugin, definition);
|
|
@@ -3,7 +3,7 @@ import { ssrPaintedCookieName } from "../paintedCookie.js";
|
|
|
3
3
|
import { readCookie } from "../readCookie.js";
|
|
4
4
|
import { flagsCookieName } from "@plitzi/sdk-shared/flags";
|
|
5
5
|
import { debugCookieName } from "@plitzi/sdk-shared/devTools";
|
|
6
|
-
import { themeFromCookies } from "@plitzi/sdk-shared/theme";
|
|
6
|
+
import { themeFromCookies } from "@plitzi/sdk-shared/theme/cookie";
|
|
7
7
|
//#region src/helpers/cache/keys.ts
|
|
8
8
|
var flagsVersionOf = (req) => req.ctx?.spaceDeployment?.flagsVersion ?? "";
|
|
9
9
|
/** The fields of an HTML key, in the order they are joined. */
|
|
@@ -8,14 +8,19 @@ import { SSRServer } from '@plitzi/sdk-shared';
|
|
|
8
8
|
* Each page is a space kept as a draft of the host's one space (`?__pt=<id>`), which is how a page server tells many
|
|
9
9
|
* apart on one host: the page arrives with its whole space in it, every page after it is the browser's, and a reload
|
|
10
10
|
* that lost the address finds it again in the draft's cookie. So a page here is drawn in the browser alone — no server
|
|
11
|
-
* provider, action, function or channel of its own: it is data and its look.
|
|
11
|
+
* provider, action, function or channel of its own: it is data and its look. `put` again under the same id and the
|
|
12
|
+
* pages open at it take the new version where they stand (`livePages`), so a page an agent keeps improving is seen to
|
|
13
|
+
* grow rather than reload.
|
|
12
14
|
*
|
|
13
15
|
* Author them where they cannot reach anything (`authorIsolated` of `@plitzi/sdk-authoring/node`) and serve them here,
|
|
14
|
-
* on
|
|
16
|
+
* on a HOST of their own — never the app's, nor another port of it: a cookie belongs to a host whatever its port, so a
|
|
17
|
+
* page somebody else wrote would run beside the app's cookies. Locally `127.0.0.1` beside the app's `localhost`; in
|
|
18
|
+
* production a domain of its own, since a sibling sub-domain shares the app's site and its domain-wide cookies.
|
|
15
19
|
*
|
|
16
20
|
* ```ts
|
|
21
|
+
* // The app at https://localhost:8080.
|
|
17
22
|
* const host = createAuthoringHost({ dir: 'tmp/pages', port: 8091, frameAncestors: ['https://localhost:8080'] });
|
|
18
|
-
* await host.server.listen(8091, '
|
|
23
|
+
* await host.server.listen(8091, '127.0.0.1'); // its pages at https://127.0.0.1:8091
|
|
19
24
|
*
|
|
20
25
|
* const result = await authorIsolated('tmp/pages/weather.ts');
|
|
21
26
|
* if (result.ok) {
|
|
@@ -32,14 +37,17 @@ export type AuthoringHostConfig = Omit<ServerConfig, 'adapters' | 'auth' | 'draf
|
|
|
32
37
|
export type AuthoringHost = {
|
|
33
38
|
server: SSRServer;
|
|
34
39
|
/**
|
|
35
|
-
* Keeps a space under `id` — a new one when left out, which nobody can guess —
|
|
36
|
-
*
|
|
40
|
+
* Keeps a space under `id` — a new one when left out, which nobody can guess — and answers the address its pages are
|
|
41
|
+
* at: its home is `path`, any other page that path with the page's own in front of `?`. Replacing what `id` held, the
|
|
42
|
+
* pages open at it swap the new space where they stand — scroll, focus and what the person was doing kept — and
|
|
43
|
+
* `updated` says how many did.
|
|
37
44
|
*/
|
|
38
45
|
put: (documents: AuthoredDocuments, options?: {
|
|
39
46
|
id?: string;
|
|
40
47
|
}) => {
|
|
41
48
|
id: string;
|
|
42
49
|
path: string;
|
|
50
|
+
updated: number;
|
|
43
51
|
};
|
|
44
52
|
/** Forgets a page: its address answers 404 from then on. Whether there was one. */
|
|
45
53
|
drop: (id: string) => boolean;
|
|
@@ -73,36 +73,41 @@ var createAuthoringHost = ({ dir, frameAncestors, ...config }) => {
|
|
|
73
73
|
revision: 0,
|
|
74
74
|
...frameAncestors ? { frameAncestors } : {}
|
|
75
75
|
};
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
76
|
+
const server = createServer({
|
|
77
|
+
livePages: true,
|
|
78
|
+
...config,
|
|
79
|
+
preview: {
|
|
80
|
+
sessionTtlMs: SESSION_TTL_MS,
|
|
81
|
+
...config.preview
|
|
82
|
+
},
|
|
83
|
+
draftStore,
|
|
84
|
+
adapters: {
|
|
85
|
+
getOfflineData: () => Promise.resolve(void 0),
|
|
86
|
+
getSpaceDeployment: (req) => {
|
|
87
|
+
const id = readDraftToken(req);
|
|
88
|
+
if (id === void 0) return Promise.resolve({ error: {
|
|
89
|
+
code: 404,
|
|
90
|
+
message: `Nothing here: each page of this host is at the address put() answered — /?${PREVIEW_TOKEN_PARAM}=<id>`
|
|
91
|
+
} });
|
|
92
|
+
if (!pages.has(id)) return Promise.resolve({ error: {
|
|
93
|
+
code: 404,
|
|
94
|
+
message: `No page "${id}" here: put() it, or it was dropped`
|
|
95
|
+
} });
|
|
96
|
+
return Promise.resolve(deployment);
|
|
98
97
|
}
|
|
99
|
-
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
return {
|
|
101
|
+
server,
|
|
100
102
|
put: (documents, { id = randomBytes(16).toString("base64url") } = {}) => {
|
|
101
103
|
if (!ID.test(id)) throw new Error(`A page id is letters, digits, "-" and "_", up to 64 of them — not "${id}"`);
|
|
104
|
+
const replacing = pages.has(id);
|
|
102
105
|
keep(id, documents);
|
|
106
|
+
const updated = replacing ? server.updatePages(documents, { draft: id }) : 0;
|
|
103
107
|
return {
|
|
104
108
|
id,
|
|
105
|
-
path: `/?${PREVIEW_TOKEN_PARAM}=${id}
|
|
109
|
+
path: `/?${PREVIEW_TOKEN_PARAM}=${id}`,
|
|
110
|
+
updated
|
|
106
111
|
};
|
|
107
112
|
},
|
|
108
113
|
drop,
|
|
@@ -22,7 +22,7 @@ export type ProjectPlugins = {
|
|
|
22
22
|
export declare const projectPlugins: (root: string) => Promise<ProjectPlugins>;
|
|
23
23
|
/**
|
|
24
24
|
* A plugin edited while developing is built again by the server, and the open pages swap it where it is drawn — the
|
|
25
|
-
* rest of the page, its state included, stays as it was (`
|
|
25
|
+
* rest of the page, its state included, stays as it was (`livePages`). Its server half (`functions/`) is loaded again
|
|
26
26
|
* here, in place. A plugin ADDED — a new folder with an `index.ts`, what `plitzi plugin add` writes — is registered
|
|
27
27
|
* here, and the pages load again to render it; one removed is turned off. A folder the server cannot build, or whose
|
|
28
28
|
* server half has no entry, is said in the terminal as the layout check says it at boot — once, until it changes — and
|
|
@@ -86,7 +86,7 @@ var projectPlugins = async (root) => {
|
|
|
86
86
|
};
|
|
87
87
|
/**
|
|
88
88
|
* A plugin edited while developing is built again by the server, and the open pages swap it where it is drawn — the
|
|
89
|
-
* rest of the page, its state included, stays as it was (`
|
|
89
|
+
* rest of the page, its state included, stays as it was (`livePages`). Its server half (`functions/`) is loaded again
|
|
90
90
|
* here, in place. A plugin ADDED — a new folder with an `index.ts`, what `plitzi plugin add` writes — is registered
|
|
91
91
|
* here, and the pages load again to render it; one removed is turned off. A folder the server cannot build, or whose
|
|
92
92
|
* server half has no entry, is said in the terminal as the layout check says it at boot — once, until it changes — and
|
|
@@ -8,7 +8,7 @@ type SetByProject = 'port' | 'health' | 'adapters' | 'plugins' | 'publicDir' | '
|
|
|
8
8
|
* What a project's server does besides serving its space — its `src/config/serverOptions.ts`: what `createServer`
|
|
9
9
|
* takes, but for what `serveProject` wires from the project's layout, and an action's `lookups`, which are the
|
|
10
10
|
* project's `src/actions/`. `logLevel` and `logger` are the project's to change; `action.kv` too. So are `devMode` and
|
|
11
|
-
* `
|
|
11
|
+
* `livePages`, which follow `NODE_ENV` when left out — on unless it is `production`. A deployment started without it (a
|
|
12
12
|
* container's `CMD`, a process manager with no environment) says `devMode: false` here, and a public action answers
|
|
13
13
|
* with its output alone, never the trace of every step.
|
|
14
14
|
*/
|
|
@@ -226,7 +226,7 @@ var startProject = async (options) => {
|
|
|
226
226
|
...tls ? { tls } : {},
|
|
227
227
|
port,
|
|
228
228
|
devMode: serverOptions.devMode ?? developing,
|
|
229
|
-
|
|
229
|
+
livePages: serverOptions.livePages ?? serverOptions.devMode ?? developing,
|
|
230
230
|
health: { name },
|
|
231
231
|
adapters,
|
|
232
232
|
plugins: plugins.sources,
|
|
@@ -264,7 +264,7 @@ var startProject = async (options) => {
|
|
|
264
264
|
const watching = developing ? [...held ? [watchSpace(root, (authored) => {
|
|
265
265
|
held.documents = authored;
|
|
266
266
|
spaceFailure.clear();
|
|
267
|
-
server.
|
|
267
|
+
server.updatePages(authored);
|
|
268
268
|
})] : [], watchProjectPlugins(root, server, plugins)] : [];
|
|
269
269
|
const close = async () => {
|
|
270
270
|
watching.forEach((stop) => stop());
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LIVE_PAGES_PATH } from "../../core/http/stages/livePages.js";
|
|
2
2
|
import { escapeHtml } from "../../helpers/escapeHtml.js";
|
|
3
3
|
//#region src/hosts/project/spaceFailure.ts
|
|
4
4
|
/**
|
|
@@ -33,7 +33,7 @@ var pageOf = (message) => `<!doctype html>
|
|
|
33
33
|
<script>
|
|
34
34
|
(() => {
|
|
35
35
|
let boot;
|
|
36
|
-
const events = new EventSource(${JSON.stringify(
|
|
36
|
+
const events = new EventSource(${JSON.stringify(LIVE_PAGES_PATH)});
|
|
37
37
|
events.addEventListener('reload', () => location.reload());
|
|
38
38
|
events.addEventListener('hello', event => {
|
|
39
39
|
const next = JSON.parse(event.data).boot;
|
|
@@ -54,7 +54,7 @@ var handleRealtimeSocket = async ({ req, res, raw, upgrade, hub, resolveChannels
|
|
|
54
54
|
if (!originAllowed(raw, req, allowedOrigins)) {
|
|
55
55
|
answer(res, {
|
|
56
56
|
status: 403,
|
|
57
|
-
error: "A page
|
|
57
|
+
error: "A page of another origin may not open this",
|
|
58
58
|
reason: "foreign"
|
|
59
59
|
});
|
|
60
60
|
return;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { readFile } from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import { PROJECT_DATA_PREFIX } from "@plitzi/sdk-shared/
|
|
3
|
+
import { PROJECT_DATA_PREFIX } from "@plitzi/sdk-shared/project/paths";
|
|
4
4
|
//#region src/modules/rsc/publicFileResolver.ts
|
|
5
5
|
/** Where a provider's `query` is a path: a URL, a protocol-relative one or one with `{{tokens}}` is not. */
|
|
6
6
|
var pathOf = (query) => {
|
|
@@ -8,7 +8,7 @@ import { ssrPaintedCookieName } from "../../helpers/paintedCookie.js";
|
|
|
8
8
|
import { readCookie } from "../../helpers/readCookie.js";
|
|
9
9
|
import { buildOfflineDataCacheKey } from "../../helpers/cache/keys.js";
|
|
10
10
|
import { loadPluginComponents } from "./loadPluginComponents.js";
|
|
11
|
-
import "../../core/previewToken.js";
|
|
11
|
+
import { readDraftToken } from "../../core/previewToken.js";
|
|
12
12
|
import { resolveActionEndpoint, resolveRscEndpoint } from "../../core/services/resolve.js";
|
|
13
13
|
import { createOfflineDataLoader } from "../../helpers/offlineDataLoader.js";
|
|
14
14
|
import { hydrationPayload, spaceDocument } from "../../helpers/hydrationPayload.js";
|
|
@@ -23,7 +23,7 @@ import { resolveDebugMode } from "../../helpers/resolveDebugMode.js";
|
|
|
23
23
|
import { flagUserFromSSR, flagValues, resolveFlags } from "@plitzi/sdk-shared/flags";
|
|
24
24
|
import { collectServerElements, notFoundProvider, pageElementTypes, pageNotFound, pageServerContext } from "@plitzi/sdk-shared/schema";
|
|
25
25
|
import { debugCookieName } from "@plitzi/sdk-shared/devTools";
|
|
26
|
-
import { themeFromCookies } from "@plitzi/sdk-shared/theme";
|
|
26
|
+
import { themeFromCookies } from "@plitzi/sdk-shared/theme/cookie";
|
|
27
27
|
import { paintedKeys, paintedStateFor } from "@plitzi/sdk-shared/state";
|
|
28
28
|
import { fontUrlResolver, fontsToHead } from "@plitzi/sdk-shared/style";
|
|
29
29
|
import { pluginDeclarationOf, pluginTypesOf } from "@plitzi/sdk-shared/plugins";
|
|
@@ -285,7 +285,9 @@ var prepareRender = async (req, config, spaceId, environment, revision, pluginMa
|
|
|
285
285
|
plugins: templatePlugins,
|
|
286
286
|
debugMode: debugAuthorized,
|
|
287
287
|
ssrOnly: config.ssrOnly === true,
|
|
288
|
-
|
|
288
|
+
livePages: config.livePages === true,
|
|
289
|
+
hotPlugins: config.livePages === true && config.devMode === true,
|
|
290
|
+
...config.livePages === true && offlineDataOverride !== void 0 ? { liveDraft: readDraftToken(req) } : {},
|
|
289
291
|
offlineData: offlineDataStr
|
|
290
292
|
},
|
|
291
293
|
cacheable: actionRuns === void 0 && forcedFlags === void 0,
|
|
@@ -205,11 +205,19 @@
|
|
|
205
205
|
|
|
206
206
|
__plitziParams.offlineData = await response.json();
|
|
207
207
|
}
|
|
208
|
-
<% if (locals.
|
|
209
|
-
const __plitziApp = render('plitzi', __plitziParams, __plitziPlugins, <%= locals.debugMode ? true : false %>, true, { hotPlugins: true });
|
|
208
|
+
<% if (locals.livePages) { %>
|
|
209
|
+
const __plitziApp = render('plitzi', __plitziParams, __plitziPlugins, <%= locals.debugMode ? true : false %>, true, { hotPlugins: <%= locals.hotPlugins ? true : false %> });
|
|
210
|
+
// New documents for the space this page shows: swapped where it stands, its state kept. A page that cannot take
|
|
211
|
+
// them in place (one with elements the server draws) loads again.
|
|
212
|
+
window.__plitziLiveEvents?.addEventListener('space', event => {
|
|
213
|
+
const { documents } = JSON.parse(event.data);
|
|
214
|
+
if (!__plitziApp?.replaceSpace(documents)) {
|
|
215
|
+
location.reload();
|
|
216
|
+
}
|
|
217
|
+
});
|
|
210
218
|
// A plugin the server built again: its new bundle swapped in where it is drawn and its stylesheet pointed at the
|
|
211
219
|
// new one — the rest of the page stays as it is. One that cannot be swapped in place loads the page again.
|
|
212
|
-
window.
|
|
220
|
+
window.__plitziLiveEvents?.addEventListener('plugin', async event => {
|
|
213
221
|
const { key, js, css } = JSON.parse(event.data);
|
|
214
222
|
const sheet = css && [...document.querySelectorAll('link[data-plitzi-plugin]')].find(link => link.dataset.plitziPlugin === key);
|
|
215
223
|
if (sheet) {
|
|
@@ -230,14 +238,15 @@
|
|
|
230
238
|
<% } %>
|
|
231
239
|
</script>
|
|
232
240
|
<% } %>
|
|
233
|
-
<% if (locals.
|
|
234
|
-
<%#
|
|
235
|
-
|
|
241
|
+
<% if (locals.livePages) { %>
|
|
242
|
+
<%# Only with `livePages`: the server says what changed under the page — everything (`reloadPages`), and the page
|
|
243
|
+
loads again, as does a page that reconnects to a server that restarted (a different `boot`): its code changed
|
|
244
|
+
under it. A draft listens for its own changes alone, by its token. %>
|
|
236
245
|
<script>
|
|
237
246
|
(() => {
|
|
238
|
-
const events = new EventSource('/__plitzi/
|
|
239
|
-
// The page's module script listens here too, for a plugin built again (`plugin`).
|
|
240
|
-
window.
|
|
247
|
+
const events = new EventSource(<%- JSON.stringify(locals.liveDraft ? `/__plitzi/live?__pt=${encodeURIComponent(locals.liveDraft)}` : '/__plitzi/live').replace(/</g, '\\u003c') %>);
|
|
248
|
+
// The page's module script listens here too, for its space (`space`) and a plugin built again (`plugin`).
|
|
249
|
+
window.__plitziLiveEvents = events;
|
|
241
250
|
let boot;
|
|
242
251
|
events.addEventListener('hello', event => {
|
|
243
252
|
const next = JSON.parse(event.data).boot;
|
package/dist/plugins/watch.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { PluginManager } from './manager';
|
|
2
2
|
/**
|
|
3
3
|
* Calls `onChange(key)` when a file a built plugin was built from changes: what a development server rebuilds and
|
|
4
|
-
* hands to the open pages (`
|
|
4
|
+
* hands to the open pages (`livePages`), without restarting the process that serves them.
|
|
5
5
|
*
|
|
6
6
|
* The FOLDERS are watched, not the files. An editor that saves through a temporary file renames it over the old one,
|
|
7
7
|
* and a watcher on the old file hears nothing after the first save. Which files matter comes from the build itself
|
package/dist/plugins/watch.js
CHANGED
|
@@ -5,7 +5,7 @@ import { watch } from "node:fs";
|
|
|
5
5
|
var SETTLE_MS = 80;
|
|
6
6
|
/**
|
|
7
7
|
* Calls `onChange(key)` when a file a built plugin was built from changes: what a development server rebuilds and
|
|
8
|
-
* hands to the open pages (`
|
|
8
|
+
* hands to the open pages (`livePages`), without restarting the process that serves them.
|
|
9
9
|
*
|
|
10
10
|
* The FOLDERS are watched, not the files. An editor that saves through a temporary file renames it over the old one,
|
|
11
11
|
* and a watcher on the old file hears nothing after the first save. Which files matter comes from the build itself
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plitzi/sdk-server",
|
|
3
|
-
"version": "0.38.
|
|
3
|
+
"version": "0.38.18",
|
|
4
4
|
"license": "AGPL-3.0",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -111,8 +111,8 @@
|
|
|
111
111
|
"./dist/env.js"
|
|
112
112
|
],
|
|
113
113
|
"dependencies": {
|
|
114
|
-
"@plitzi/plitzi-sdk": "0.38.
|
|
115
|
-
"@plitzi/sdk-shared": "0.38.
|
|
114
|
+
"@plitzi/plitzi-sdk": "0.38.18",
|
|
115
|
+
"@plitzi/sdk-shared": "0.38.18",
|
|
116
116
|
"ejs": "^7.0.1",
|
|
117
117
|
"esbuild": "^0.28.2",
|
|
118
118
|
"jsonwebtoken": "^9.0.3",
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { Stage } from '../types';
|
|
2
|
-
import { PluginEntry } from '@plitzi/sdk-shared';
|
|
3
|
-
/** Where an open page listens for "the space changed: load again", in development. */
|
|
4
|
-
export declare const DEV_RELOAD_PATH = "/__plitzi/reload";
|
|
5
|
-
/**
|
|
6
|
-
* The page reloading itself when the space it shows changed — the edit loop of a project that authors its space in
|
|
7
|
-
* code, without restarting the server: `serveProject` re-authors in a process of its own, swaps the documents and calls
|
|
8
|
-
* `reloadPages()`, and every page open on this server loads again.
|
|
9
|
-
*
|
|
10
|
-
* Only with `devReload` on: every open page keeps a connection for it, which a server that never calls `reloadPages`
|
|
11
|
-
* — a published one, or a platform's — would pay for and get nothing from. The template listens only then too.
|
|
12
|
-
*
|
|
13
|
-
* Each connection is greeted with this process's `boot`, and a page that reconnects to a different one loads again: the
|
|
14
|
-
* server restarted — `start:dev` saw its code change (`main.ts`, its options, its actions, `src/functions/`) — and
|
|
15
|
-
* what is on screen was built by the one before. Without it the page kept the old plugin until somebody reloaded it,
|
|
16
|
-
* which reads as "my change did nothing".
|
|
17
|
-
*/
|
|
18
|
-
export declare const createDevReload: () => {
|
|
19
|
-
stage: Stage;
|
|
20
|
-
/** Every page open on this server loads again. */
|
|
21
|
-
reload: () => void;
|
|
22
|
-
/**
|
|
23
|
-
* A plugin built again: every open page imports its new bundle and swaps it where it is drawn (`keyName` is what
|
|
24
|
-
* the page registered it as), and points its stylesheet at the new one — without loading again.
|
|
25
|
-
*/
|
|
26
|
-
plugin: ({ keyName, js, css }: PluginEntry) => void;
|
|
27
|
-
/** The streams ended — the server is going away, and each page reconnects to whichever comes up next. */
|
|
28
|
-
close: () => void;
|
|
29
|
-
};
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { openEventStream } from "../sse.js";
|
|
2
|
-
import { randomUUID } from "node:crypto";
|
|
3
|
-
//#region src/core/http/stages/devReload.ts
|
|
4
|
-
/** Where an open page listens for "the space changed: load again", in development. */
|
|
5
|
-
var DEV_RELOAD_PATH = "/__plitzi/reload";
|
|
6
|
-
/**
|
|
7
|
-
* The page reloading itself when the space it shows changed — the edit loop of a project that authors its space in
|
|
8
|
-
* code, without restarting the server: `serveProject` re-authors in a process of its own, swaps the documents and calls
|
|
9
|
-
* `reloadPages()`, and every page open on this server loads again.
|
|
10
|
-
*
|
|
11
|
-
* Only with `devReload` on: every open page keeps a connection for it, which a server that never calls `reloadPages`
|
|
12
|
-
* — a published one, or a platform's — would pay for and get nothing from. The template listens only then too.
|
|
13
|
-
*
|
|
14
|
-
* Each connection is greeted with this process's `boot`, and a page that reconnects to a different one loads again: the
|
|
15
|
-
* server restarted — `start:dev` saw its code change (`main.ts`, its options, its actions, `src/functions/`) — and
|
|
16
|
-
* what is on screen was built by the one before. Without it the page kept the old plugin until somebody reloaded it,
|
|
17
|
-
* which reads as "my change did nothing".
|
|
18
|
-
*/
|
|
19
|
-
var createDevReload = () => {
|
|
20
|
-
const listening = /* @__PURE__ */ new Set();
|
|
21
|
-
const boot = randomUUID();
|
|
22
|
-
const stage = (ctx) => {
|
|
23
|
-
if (!ctx.config.devReload || ctx.req.path !== "/__plitzi/reload") return Promise.resolve(false);
|
|
24
|
-
const stream = openEventStream(ctx.rawRes, {
|
|
25
|
-
onAbort: () => listening.delete(stream),
|
|
26
|
-
retryMs: 1e3
|
|
27
|
-
});
|
|
28
|
-
listening.add(stream);
|
|
29
|
-
stream.send("hello", { boot });
|
|
30
|
-
ctx.raw.once("close", () => {
|
|
31
|
-
listening.delete(stream);
|
|
32
|
-
stream.close();
|
|
33
|
-
});
|
|
34
|
-
return Promise.resolve(true);
|
|
35
|
-
};
|
|
36
|
-
return {
|
|
37
|
-
stage,
|
|
38
|
-
/** Every page open on this server loads again. */
|
|
39
|
-
reload: () => listening.forEach((stream) => stream.send("reload", {})),
|
|
40
|
-
/**
|
|
41
|
-
* A plugin built again: every open page imports its new bundle and swaps it where it is drawn (`keyName` is what
|
|
42
|
-
* the page registered it as), and points its stylesheet at the new one — without loading again.
|
|
43
|
-
*/
|
|
44
|
-
plugin: ({ keyName, js, css }) => listening.forEach((stream) => stream.send("plugin", {
|
|
45
|
-
key: keyName,
|
|
46
|
-
js,
|
|
47
|
-
css
|
|
48
|
-
})),
|
|
49
|
-
/** The streams ended — the server is going away, and each page reconnects to whichever comes up next. */
|
|
50
|
-
close: () => {
|
|
51
|
-
listening.forEach((stream) => stream.close());
|
|
52
|
-
listening.clear();
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
//#endregion
|
|
57
|
-
export { DEV_RELOAD_PATH, createDevReload };
|
|
File without changes
|