@protobase/cli 0.81.6 → 0.82.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.
@@ -1,4 +1,4 @@
1
1
  //#region package.json
2
- var version = "0.81.6";
2
+ var version = "0.82.0";
3
3
  //#endregion
4
4
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/cli",
3
- "version": "0.81.6",
3
+ "version": "0.82.0",
4
4
  "description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -43,13 +43,13 @@
43
43
  "dependencies": {
44
44
  "@better-auth/core": "^1.7.12",
45
45
  "@hono/node-server": "^2.1.3",
46
- "@protobase/config": "0.81.6",
47
- "@protobase/layout": "0.81.6",
48
- "@protobase/query": "0.81.6",
49
- "@protobase/schema": "0.81.6",
50
- "@protobase/server": "0.81.6",
51
- "@protobase/ui": "0.81.6",
52
- "@protobase/workflows": "0.81.6",
46
+ "@protobase/config": "0.82.0",
47
+ "@protobase/layout": "0.82.0",
48
+ "@protobase/query": "0.82.0",
49
+ "@protobase/schema": "0.82.0",
50
+ "@protobase/server": "0.82.0",
51
+ "@protobase/ui": "0.82.0",
52
+ "@protobase/workflows": "0.82.0",
53
53
  "@react-email/render": "^2.1.0",
54
54
  "@scalar/hono-api-reference": "^0.12.10",
55
55
  "@sentry/vite-plugin": "^11.10.0",
@@ -62,7 +62,7 @@ Who may call them:
62
62
 
63
63
  A device is one token, or one endpoint for a browser. An Android or iOS token, or a relay handle, someone else had moves to the caller, as a new device with a new id that keeps nothing of the previous user's, so a shared phone notifies whoever signed in on it last. A browser's endpoint never moves: registering one another user holds is a `409` of type `device-held` that stores nothing, and the browser subscribes again for a new endpoint, as [the browser helper](#in-the-browser) does. A user keeps up to 20 devices: registering one more removes their least recently seen. Deleting a user removes their devices.
64
64
 
65
- With Protobase's auth, a device belongs to the sign-in session it was registered in. Once that session ends, the device gets no more push and is removed at the next send: when the user signs out, someone signs in on the same browser, the user signs out everywhere, a password change signs out their other devices, an admin ends their sessions or bans them, or the session expires. Registering the device again ties it to the session of that request, which the browser helper does on its own for the same user. With an app's own authenticator, a device stays its user's until it is removed with `DELETE`.
65
+ With Protobase's auth, a device belongs to the sign-in session it was registered in. Once that session ends, the device gets no more push and is removed at the next send: when the user signs out, someone signs in on the same browser, the user signs out everywhere, a password change signs out their other devices, an admin ends their sessions or bans them, or the session expires. A device with no session recorded, such as one registered before devices kept their session, counts as ended too. Registering the device again ties it to the session of that request, which the browser helper does on its own for the same user, so such a device gets push again once its browser registers it. With an app's own authenticator, a device stays its user's until it is removed with `DELETE`. <gap>Devices cannot end with a session of an app's own authenticator because a way for `authenticate` to report that a session ended is not implemented.</gap>
66
66
 
67
67
  The path comes from `devicesPath('/api', id)`, and the name of its id parameter from `deviceIdParam`, both exported by `@protobase/server`.
68
68
 
@@ -187,7 +187,7 @@ Without them Web Push is off and the app has no sender for browsers. One or two
187
187
 
188
188
  The private key is a secret: keep it out of client code, `VITE_` variables and the repository. Anyone holding it and the stored subscriptions can push to every subscribed browser. Browsers subscribe with the public key, which `/meta` gives every caller as `notifications.webPushKey`, so a new key pair breaks every subscription until its browser registers again.
189
189
 
190
- Each message is encrypted for its browser and signed with the key. It goes only to an endpoint on one of the browsers' push services, never through a redirect; a device stored with any other endpoint, or with keys that are not a subscription's, is `refused`. <gap>Web Push cannot deliver to a push service outside the browsers' own because adding one to the list is not implemented.</gap> A message's `platform.web` sets the request's `ttl`, `urgency` and `topic` and fields merged into the JSON the browser receives (`payload`), in place of those built from the message; every URL in that JSON is checked as the message's `url` is. The JSON also carries `user`, the id of the user the device is registered for, over any `user` in `payload`, and at most 3993 bytes.
190
+ Each message is encrypted for its browser and signed with the key. It goes only to an endpoint on one of the browsers' push services, never through a redirect, and only to the address the endpoint's host resolves to when it is sent, which must be public: an endpoint whose host resolves to a private, loopback or link-local address gets no request and is `refused`; a device stored with any other endpoint, or with keys that are not a subscription's, is `refused`. <gap>Web Push cannot deliver to a push service outside the browsers' own because adding one to the list is not implemented.</gap> A message's `platform.web` sets the request's `ttl`, `urgency` and `topic` and fields merged into the JSON the browser receives (`payload`), in place of those built from the message; every URL in that JSON is checked as the message's `url` is. The JSON also carries `user`, the id of the user the device is registered for, over any `user` in `payload`, and at most 3993 bytes.
191
191
 
192
192
  ### In the browser
193
193
 
@@ -344,7 +344,7 @@ const store: DeviceStore = {
344
344
  export default store
345
345
  ```
346
346
 
347
- Every method takes and returns plain data: the user id as text, and devices as `{ id, userId, kind, token, p256dh, auth, environment, appId, userAgent, sessionId, createdAt, lastSeenAt }`, with `token` the endpoint for a browser and `sessionId` the sign-in session it was registered in. A method given an empty user id, id or token throws. `listByIds` and `removeGone` act on any user's devices, for the code that sends; the endpoints never call them. A store missing a method stops startup, naming it.
347
+ Every method takes and returns plain data: the user id as text, and devices as `{ id, userId, kind, token, p256dh, auth, environment, appId, userAgent, sessionId, createdAt, lastSeenAt }`, with `token` the endpoint for a browser and `sessionId` the sign-in session it was registered in. A store keeps `sessionId` as `register` gives it and returns it: with Protobase's auth, a device whose store returns no `sessionId` gets no push and is removed at the next send. A method given an empty user id, id or token throws. `listByIds` and `removeGone` act on any user's devices, for the code that sends; the endpoints never call them. A store missing a method stops startup, naming it.
348
348
 
349
349
  When Protobase's auth deletes a user, by an admin or with `protobase users delete`, it calls the store's `removeAllForUser` before anything is deleted, so a store that throws stops the deletion, and once more afterwards. An app that deletes users otherwise calls `deviceStore(db, notifications).removeAllForUser(userId)` as it deletes one, with the config's `notifications` (the devices table's names), or the `removeAllForUser` of its own `notifications/store.ts`.
350
350
 
@@ -386,7 +386,7 @@ const sender: PushSender = {
386
386
  export default [sender]
387
387
  ```
388
388
 
389
- `send` takes a message and a batch of devices of those kinds, and answers each device by its id with a result:
389
+ `send` takes a message and a batch of devices of those kinds, and answers each device by its id with a result. The send job is what calls it, and only with devices that may get push now: with Protobase's auth, ones whose session stands. Send through [`sendNotification`](#sending), never by calling a sender yourself, which would skip that check. The results:
390
390
 
391
391
  | Result | |
392
392
  | --- | --- |
@@ -4,15 +4,15 @@
4
4
 
5
5
  A signed-in user's browser reaches a TCP service only the app's server can reach, such as one on the workspace's private network, over an ordinary WebSocket.
6
6
 
7
- A TCP bridge lets a page of the app talk to a TCP service that only the app's server can reach, such as a server's VNC or SSH port, or a database, on the workspace's private network. The browser opens an ordinary WebSocket to the app, and the server connects to the service and passes the bytes through both ways. The user's device joins no network and installs no VPN client.
8
-
9
- A bridge only carries bytes. A console, a terminal or a database tool that speaks the service's protocol is a page or plugin of your own built on it.
10
-
11
7
  :::caution[Beta]
12
8
  <gap>A bridge token opens a bridge only on the server process that issued it because a token store shared between processes is not implemented.</gap>
13
9
  <gap>A page that signs in with a bearer token instead of the session cookie cannot open a bridge from a browser because function sockets do not take a token without a header.</gap>
14
10
  :::
15
11
 
12
+ A TCP bridge lets a page of the app talk to a TCP service that only the app's server can reach, such as a server's VNC or SSH port, or a database, on the workspace's private network. The browser opens an ordinary WebSocket to the app, and the server connects to the service and passes the bytes through both ways. The user's device joins no network and installs no VPN client.
13
+
14
+ A bridge only carries bytes. A console, a terminal or a database tool that speaks the service's protocol is a page or plugin of your own built on it.
15
+
16
16
  ## The access rule
17
17
 
18
18
  A bridge is an [API function](../guides/api-functions.md) whose handler is `tcpBridge({ target })`. `target(name, context)` is the app's access rule: given the name a page asks for, such as a server's record key, it answers where the caller may connect, `{ host, port, expiresInSeconds? }`, or `undefined` when they may not. `context` is the function's [context](../guides/api-functions.md#the-context), with the caller's `session` and `records`, so the rule can read a record as the caller. The function's own `roles` or `capability` decide who may call it at all.
@@ -45,10 +45,10 @@ export default defineFunction({ roles: ['mechanic', 'admin'] }, workshopConsole)
45
45
  ## Opening a bridge
46
46
 
47
47
  1. `POST /api/functions/<name>` with `{ "target": "<name>" }` runs the rule. It answers `201` with `{ token, open_by, expires_at }`, or a `404` problem for a target the caller may not reach, without connecting anywhere. A user has at most 20 tokens waiting to be used; asking for another drops their oldest.
48
- 2. A WebSocket to `/api/functions/<name>?token=<token>`, before `open_by`, 30 seconds after the token was issued, opens the bridge. The token opens one bridge, for the same caller only; a token used before, too late, or by someone else is a `404`. The rule runs again, then the server connects to the target, and a target that does not answer within 10 seconds is a `502` that names no address.
48
+ 2. A WebSocket to `/api/functions/<name>?token=<token>`, before `open_by`, 30 seconds after the token was issued, opens the bridge. The token opens one bridge, for the same caller only; a token used before, too late, or by someone else is a `404`. The rule runs again, then the server connects to the target, and a target that does not answer within 10 seconds is a `502` that names no address. A bridge whose `expires_at` passes, or whose target is withdrawn, while the rule runs or the server connects does not open either.
49
49
  3. Each binary message is written to the service as it is, and what the service sends comes back as binary messages, in order. A text message closes the bridge with `1003`.
50
50
 
51
- `client.bridges.open(name, target)` of [`@protobase/client`](client.md) does both steps and resolves to `{ socket, expiresAt }`, with the socket's binary as `ArrayBuffer`s:
51
+ `client.bridges.open(name, target)` of [`@protobase/client`](client.md) does both steps and resolves to `{ socket, expiresAt }`, with the socket's binary as `ArrayBuffer`s, as soon as the socket exists; wait for its `open` event before sending:
52
52
 
53
53
  ```ts title="docs-examples/bridges/open-truing-stand.ts"
54
54
  import { createClient } from '@protobase/client'
@@ -56,10 +56,15 @@ import { createClient } from '@protobase/client'
56
56
  const client = createClient()
57
57
 
58
58
  // A page of the app opens the truing stand's bridge and hands its bytes to a viewer of its own, such as a VNC client.
59
+ // `open` resolves as soon as the socket exists, so this waits for it to open before handing out its sender.
59
60
  export const openTruingStand = async (show: (bytes: Uint8Array) => void) => {
60
61
  const { socket, expiresAt } = await client.bridges.open('workshop-console', 'truing-stand')
61
62
  socket.onmessage = (event) => show(new Uint8Array(event.data as ArrayBuffer))
62
63
  socket.onclose = (event) => console.log('closed', event.code, event.reason, 'expires at', expiresAt)
64
+ await new Promise<void>((resolve, reject) => {
65
+ socket.addEventListener('open', () => resolve(), { once: true })
66
+ socket.addEventListener('close', () => reject(new Error('The truing stand could not be reached')), { once: true })
67
+ })
63
68
  return (bytes: ArrayBuffer) => socket.send(bytes)
64
69
  }
65
70
  ```
@@ -75,6 +80,6 @@ export const openTruingStand = async (show: (bytes: Uint8Array) => void) => {
75
80
 
76
81
  - **Expiry.** The server closes a bridge at its `expires_at`, whatever the rule answers since.
77
82
  - **The user leaves.** Closing the page closes the socket and the TCP connection with it. A client that vanishes without closing, such as a laptop gone to sleep, is closed once it misses a ping, within a minute. The caller is [checked again every minute](../guides/api-functions.md#who-may-open-one), as on every function socket.
78
- - **The app withdraws the target.** The rule runs again every minute while a bridge is open, and an `undefined` or a different `host` and `port` closes it. To close the open bridges to a target at once, call `withdraw(name)` on the bridge, such as `workshopConsole.withdraw('stock-db')`, from code in the same process.
83
+ - **The app withdraws the target.** The rule runs again every minute while a bridge is open, and an `undefined` or a different `host` and `port` closes it. To close the bridges to a target at once, open or still opening, call `withdraw(name)` on the bridge, such as `workshopConsole.withdraw('stock-db')`, from code in the same process.
79
84
 
80
- Data flows at the pace of the slower side: the server reads the next message only once the service took the last one, and reads from the service only once the page's socket took the last chunk.
85
+ Data flows at the pace of the slower side, one chunk in flight each way. The server writes the next message to the service once the service's connection has room again; until then later messages wait as on any function socket, which [stops reading from the page](../guides/api-functions.md#slow-readers-and-large-messages) above 1 MiB of them. It reads from the service again only once the page's socket took the last chunk.