@plitzi/sdk-server 0.38.14 → 0.38.16

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.
Files changed (47) hide show
  1. package/CHANGELOG.md +171 -0
  2. package/README.md +68 -6
  3. package/dist/core/auth/createAuth.d.ts +5 -0
  4. package/dist/core/auth/createAuth.js +5 -0
  5. package/dist/core/auth/csrf.d.ts +25 -0
  6. package/dist/core/auth/csrf.js +34 -32
  7. package/dist/core/auth/identity.d.ts +4 -0
  8. package/dist/core/auth/identity.js +1 -0
  9. package/dist/core/createServer.js +4 -0
  10. package/dist/core/freePort.d.ts +4 -0
  11. package/dist/core/freePort.js +4 -4
  12. package/dist/core/http/stages/authRoutes.js +3 -0
  13. package/dist/core/http/stages/crossSiteWrites.d.ts +23 -0
  14. package/dist/core/http/stages/crossSiteWrites.js +44 -0
  15. package/dist/core/http/stages/crossSiteWrites.test.d.ts +1 -0
  16. package/dist/core/http/stages/exchangeRoute.js +2 -0
  17. package/dist/core/server/fleet/link.d.ts +1 -1
  18. package/dist/core/server/fleet/stores.js +1 -1
  19. package/dist/core/services/registry.js +2 -0
  20. package/dist/core/unusedPort.d.ts +7 -3
  21. package/dist/core/unusedPort.js +33 -15
  22. package/dist/functions-api.d.ts +5 -2
  23. package/dist/index.d.ts +2 -0
  24. package/dist/index.js +2 -1
  25. package/dist/modules/actions/tasks/realtime.js +1 -1
  26. package/dist/modules/authoringHost/index.d.ts +49 -0
  27. package/dist/modules/authoringHost/index.js +113 -0
  28. package/dist/modules/authoringHost/index.test.d.ts +1 -0
  29. package/dist/modules/functions/contract.d.ts +5 -2
  30. package/dist/modules/project/serveProject.js +4 -1
  31. package/dist/modules/realtime/hub.d.ts +7 -2
  32. package/dist/modules/realtime/hub.js +8 -4
  33. package/dist/modules/realtime/index.js +14 -7
  34. package/dist/modules/realtime/message.d.ts +3 -0
  35. package/dist/modules/realtime/message.js +5 -0
  36. package/dist/modules/realtime/retained.d.ts +24 -0
  37. package/dist/modules/realtime/retained.js +47 -0
  38. package/dist/modules/realtime/retained.test.d.ts +1 -0
  39. package/dist/modules/runtime/stages.d.ts +4 -1
  40. package/dist/modules/ssr/spaceDocument.js +5 -1
  41. package/package.json +3 -3
  42. package/skills/plitzi-authoring/SKILL.md +1 -1
  43. package/skills/plitzi-authoring/reference/index.md +3 -2
  44. package/skills/plitzi-authoring/reference/plugin-reach.md +91 -0
  45. package/skills/plitzi-authoring/reference/plugins.md +15 -31
  46. package/skills/plitzi-authoring/reference/realtime.md +11 -0
  47. package/skills/plitzi-authoring/reference/testing.md +3 -1
@@ -0,0 +1,91 @@
1
+ # What a plugin says, and to whom
2
+
3
+ A plugin reaches the person, the space's other elements, the server and the other screens each one way. All of it is
4
+ imported from `@plitzi/plitzi-sdk` — the page's copy, which every plugin shares — never from `window` events or a
5
+ module of the project's (each plugin is a bundle of its own: a module two of them import is two).
6
+
7
+ ## To the person: `useNotify`
8
+
9
+ ```tsx
10
+ import { useNotify } from '@plitzi/plitzi-sdk';
11
+
12
+ const notify = useNotify();
13
+ notify('Deleted 79 recordings'); // what an `addNotification` step shows
14
+ notify('Training failed', { appearance: 'danger', autoDismiss: false });
15
+ ```
16
+
17
+ The page's own toasts, in the space's theme and with the look its `notifications` give them. For what only the plugin
18
+ knows; what the PAGE decides to say stays a flow (`on(…)` → `addNotification`).
19
+
20
+ ## To another element: an event, and the space's flow
21
+
22
+ A plugin never calls another one. It fires what happened, and the space's flow decides who acts — visible in the
23
+ space, and the same flow a button can start:
24
+
25
+ ```ts
26
+ // the assistant's declaration: triggers: { onOpenResult: { action: 'onOpenResult', preview: { index: 0 } } }
27
+ assistant({ id: 'jarvis', flows: [[
28
+ named('asked', declaredTrigger(assistantDeclaration, 'onOpenResult')),
29
+ declaredCallback(browserDeclaration, 'openResult', { on: 'browser', params: { index: '{{ asked.index }}' } })
30
+ ]] })
31
+ ```
32
+
33
+ The receiver declares the action (`callbacks`) and registers it from its declaration; a name either side does not
34
+ declare is refused when the space is authored.
35
+
36
+ ## To the server: `useServerAction`
37
+
38
+ One operation is one server action — `taskAction(tasks.done, …)` or `defineAction(…)` — whose input the server checks
39
+ before any code runs, never a `case` in one route. A plugin runs it awaited, typed by the action:
40
+
41
+ ```tsx
42
+ import { useServerAction } from '@plitzi/plitzi-sdk';
43
+ import type { habitDone } from '../../space/actions';
44
+
45
+ const done = useServerAction<typeof habitDone>('habits-done');
46
+ const result = await done({ id }); // the input its `call` way in declares
47
+ if (result.ok) { show(result.output.streak); } else { say(result.error ?? result.reason); }
48
+ ```
49
+
50
+ The same run a flow's `runServerAction` makes: its `access`, its refusals, its entry in the dev tools; a completed run
51
+ refreshes the page's cached requests (`{ invalidateQueries: 'none' }` for one that only reads).
52
+
53
+ ## State every screen shares: a channel that retains
54
+
55
+ What the server keeps and every page shows — a dashboard's lists, a board's settings — is a channel declared
56
+ `retain: true` (with `publish: 'server'`): the server keeps the latest message of each type and hands them to every
57
+ page that joins.
58
+
59
+ ```ts
60
+ channels: { desk: { access: { mode: 'session' }, publish: 'server', retain: true } }
61
+
62
+ // the server, in a task, a route or a flow's `realtime.publish`: kept, and said to every page on it
63
+ await ctx.publish('desk', 'lists', lists);
64
+ await ctx.publish('desk', 'lists', null); // forgotten
65
+
66
+ channel({ id: 'desk', topic: 'desk', children: [lists({ id: 'lists', bind: { lists: 'desk.retained.lists' } })] })
67
+ ```
68
+
69
+ `useChannel('desk').retained.lists` is the same value in a plugin, re-rendering when it changes; what the page is
70
+ handed on joining reaches `onMessage` too, as if just said. A page changes it by
71
+ asking the server (an action), which keeps it and says it. A topic keeps at most 64 types and 512 KB.
72
+
73
+ ## To other pages, at the speed of a cursor: `useChannel`
74
+
75
+ ```tsx
76
+ const room = useChannel(roomTopic, { onMessage: message => move(message.from, message.data) });
77
+ room.publish('pointer', { x, y }); // throttle it: ~20 a second, the latest position each time
78
+ room.publish('react', { emoji }, { echo: true }); // and heard here too
79
+ room.members; // who is here, with the state each announced
80
+ ```
81
+
82
+ Messages arrive through the callback and re-render nothing. Take the topic as a prop
83
+ (`bindTemplate('roomTopic', 'board.id', 'room:{{ source }}')`) so the space names its channels, and trust a message
84
+ you act on only when `message.from === 'server'`. The page has one connection and is one member per topic, however
85
+ many elements and plugins listen.
86
+
87
+ ## Its own server code
88
+
89
+ `plitzi plugin add board --server` writes `functions/index.ts` in its folder. Its routes answer under
90
+ `/fn/plugins/board/`, named with `usePluginRoute('board')('/layout')` (`undefined` where no server runs code, as on the
91
+ canvas). Its `ctx.kv` is the plugin's own; the space's credentials, channels and data are not.
@@ -55,8 +55,14 @@ const NebulaRun = ({ shipColor = 'amber', best = 0, className }: NebulaRunProps)
55
55
 
56
56
  - **Render `RootElement`**, not a `div`: it carries the element's id, classes and `data-plitzi-el`, so the CSS
57
57
  authored on the element applies, the builder can select it and a test can find it.
58
- - **Render the same thing on the server and on the first client pass.** A clock, a random number or anything read from
59
- `window` belongs in an effect; a hydration mismatch discards the whole tree, not the plugin.
58
+ - **Render the same thing on the server and on the first client pass** — a plugin is drawn on the server first. A
59
+ clock, a random number or anything read from `window` belongs in an effect; a hydration mismatch discards the whole
60
+ tree, not the plugin. A preference per device (a tab, muted) is never read from `localStorage` while rendering: write
61
+ it to `runtime.state` with `keepState` — and `paintedState` when it changes the first paint ([kept state](kept-state.md))
62
+ — and take it as a bound prop.
63
+ - **`@plitzi/plitzi-ui` is not the page's.** The build keeps out only React and the SDK: a plugin importing it carries
64
+ its own copy, whose contexts (its toasts) are not the page's and whose look is not the space's. Draw controls with the
65
+ space's tokens; tabs over the space's elements are a plugin that lays them out (below).
60
66
  - **Borrow colours.** `currentColor` and the space's `var(--…)` tokens follow the theme; a hard-coded colour is
61
67
  invisible in one of them. A canvas reads them with `getComputedStyle(element).getPropertyValue('--accent')` and
62
68
  again when `theme.resolved` changes (bind it as a prop).
@@ -90,39 +96,17 @@ import { elementChildren } from '@plitzi/plitzi-sdk';
90
96
  {elementChildren(children).map(({ id, node }) => <section key={id} style={placed[id]}>{node}</section>)}
91
97
  ```
92
98
 
93
- Never write `style` onto an element the plugin does not render: nothing promises to keep it. Its boxes share one
99
+ One that shows only some of them — a dashboard's panels, a wizard's step — says `choosesChildren: true` in its
100
+ declaration: the page checks then owe none at rest, and check the ones it draws. Never write `style` onto an element
101
+ the plugin does not render: nothing promises to keep it. Its boxes share one
94
102
  stacking context, the plugin's. `useDisplayMode()` names the breakpoint showing (`desktop`, `tablet`, `mobile`) at the
95
103
  space's widths; `useElementSize(ref)`, its own box (`undefined` until measured).
96
104
 
97
- ## Its own server code
105
+ ## What it says, and to whom
98
106
 
99
- `plitzi plugin add board --server` writes `functions/index.ts` in its folder (the CLI skill). Its routes answer
100
- under `/fn/plugins/board/`; the component names them, with nothing for the space to wire:
101
-
102
- ```tsx
103
- const route = usePluginRoute('board'); // from '@plitzi/plitzi-sdk'
104
- const url = route('/layout'); // undefined where no server runs code (the canvas)
105
- ```
106
-
107
- Its `ctx.kv` is the plugin's own; the space's credentials, channels and data are not.
108
-
109
- ## Talking to other pages
110
-
111
- A plugin that moves at the speed of a cursor reads a realtime channel through `useChannel` rather than through flows —
112
- messages arrive through a callback and re-render nothing:
113
-
114
- ```tsx
115
- import { useChannel } from '@plitzi/plitzi-sdk';
116
-
117
- const room = useChannel(roomTopic, { onMessage: message => move(message.from, message.data) });
118
- room.publish('pointer', { x, y }); // throttle it: ~20 a second, the latest position each time
119
- room.publish('react', { emoji }, { echo: true }); // and heard here too
120
- room.members; // who is here, with the state each announced
121
- ```
122
-
123
- Take the topic as a prop (`bindTemplate('roomTopic', 'board.id', 'room:{{ source }}')`) so the space names its
124
- channels, and trust a message you act on only when `message.from === 'server'`. The page has one connection and is one
125
- member per topic, however many elements and plugins listen.
107
+ The person (`useNotify`), another element (an event and the space's flow — never a `window` event), the server
108
+ (`useServerAction`, typed by the action), state every screen shares (a channel that retains), other pages
109
+ (`useChannel`) and its own server code: [plugin-reach](plugin-reach.md).
126
110
 
127
111
  ## Feature flags
128
112
 
@@ -33,6 +33,17 @@ channels: { 'order:{id}': { access: { mode: 'session' }, grant: true } }
33
33
  channel({ id: 'order', topic: 'order:{{ id }}', bind: { grant: 'state.follow.grant' }, children: [...] })
34
34
  ```
35
35
 
36
+ State every screen shows and the server keeps — a dashboard's lists, a board's settings — goes on a channel declared
37
+ `retain: true` (and `publish: 'server'`): the server keeps the latest message of each type, sends it as it is said and
38
+ again to every page that joins — and to its `onMessage` flows, as if just said — which reads it as
39
+ `desk.retained.lists` (a plugin, `useChannel('desk').retained`).
40
+ Publishing `null` forgets the type; a topic keeps at most 64 types and 512 KB. No read of its own, no copy in `state`:
41
+
42
+ ```ts
43
+ channels: { desk: { access: { mode: 'session' }, publish: 'server', retain: true } }
44
+ // a flow: { task: 'realtime.publish', params: { topic: 'desk', type: 'lists', data: '{{ saved.lists }}' } }
45
+ ```
46
+
36
47
  Two people writing the same thing at once: `kv.setIf` writes only if the value is still the one the flow read (empty
37
48
  `expected` = only if nothing is there yet — claim a seat, a username), `list.put`/`list.range` keep an ordered list
38
49
  (latest first, a leaderboard), and `flow.rateLimit` first in a public action that writes.
@@ -44,7 +44,9 @@ the list `inspectPage` holds a page to.
44
44
  Three flags say what a bare visit cannot promise, and `onScreen` leaves out:
45
45
 
46
46
  - `conditional` — on screen only under a condition of its own or of an ancestor, or with every child conditional (a
47
- list of flyouts at rest);
47
+ list of flyouts at rest). Under a plugin that chooses its children (`choosesChildren`), each child — and what is
48
+ inside it under no condition of its own — also carries `shownWith`, the child it comes with: once that child is
49
+ drawn it is owed like the rest (`onScreenIfChosen` lists them, and `inspectPage` checks them);
48
50
  - `repeated` — inside a list row, so rendered once per row: several copies, or none while the list is empty. Address
49
51
  one with `.first()` / `.nth(i)`, in a test that knows the data;
50
52
  - `boxless` — a provider with no tag, which renders its children and no element of its own; or a plugin whose