@protobase/cli 0.37.0 → 0.37.2

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.37.0";
2
+ var version = "0.37.2";
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.37.0",
3
+ "version": "0.37.2",
4
4
  "description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -36,11 +36,11 @@
36
36
  "@better-auth/core": "^1.7.12",
37
37
  "@hono/node-server": "^2.1.3",
38
38
  "@hono/vite-dev-server": "^0.26.1",
39
- "@protobase/layout": "0.37.0",
40
- "@protobase/query": "0.37.0",
41
- "@protobase/schema": "0.37.0",
42
- "@protobase/server": "0.37.0",
43
- "@protobase/ui": "0.37.0",
39
+ "@protobase/layout": "0.37.2",
40
+ "@protobase/query": "0.37.2",
41
+ "@protobase/schema": "0.37.2",
42
+ "@protobase/server": "0.37.2",
43
+ "@protobase/ui": "0.37.2",
44
44
  "@react-email/render": "^2.1.0",
45
45
  "@scalar/hono-api-reference": "^0.12.10",
46
46
  "@tailwindcss/vite": "^4.3.3",
@@ -174,19 +174,40 @@ A client that is no browser sends `Authorization: Bearer <token>` with its upgra
174
174
  | `onOpen(socket)` | The socket is open. |
175
175
  | `onMessage(data, socket)` | Text arrives as a string, binary as an `ArrayBuffer`, byte for byte. Messages come one at a time and in order. |
176
176
  | `onClose(code, reason)` | Either side closed it, after the last message. |
177
+ | `recheck()` | Runs every minute while the socket is open, after the framework's own check. A `401`, `403` or `404` problem it throws closes the socket with `1008`; any other error is reported and closes it with `1011`, as does a check that has not finished a minute later. What it returns is ignored, so refuse by throwing. |
177
178
 
178
179
  `socket.send(data)` takes a string, an `ArrayBuffer` or a `Uint8Array`, and `socket.close(code?, reason?)` closes it. A handler that throws closes the socket with `1011`, and the error is reported like any other.
179
180
 
181
+ Sockets need `@hono/node-server`'s `upgradeWebSocket`, the only upgrade supported, which `protobase dev` and `protobase serve` use. A server of your own that passes another host's upgrade to `createAdmin` gets sockets that misbehave, since they rely on its send callback and `bufferedAmount`.
182
+
180
183
  ### Who may open one
181
184
 
182
185
  - **Refusals come before the socket.** A caller without a valid session or token gets the usual `401`, and one without the function's roles a `403`; the handler never runs. The browser only sees a socket that failed to open, since an upgrade's refusal has a status but no body.
183
186
  - **Only the app's own pages** may open a socket with the cookie. An upgrade from a page on any other origin is a `403`, so another site cannot borrow a visitor's session.
184
- - **While it is open,** a protected function's caller is checked again every minute, as a plain call would be. Signing out, a ban, a role taken away, or another active role or organization closes the socket with `1008`, and a socket opened with a bearer token closes once the token expires. The client opens it again, as who they are now.
187
+ - **While it is open,** the framework checks the caller again every minute: their session or token, the function's roles, and that the user, tenant, organization and roles are unchanged. It does so for a protected function, and for a public one once `caller()` resolved. Signing out, a ban, a role taken away, or another active role or organization closes the socket with `1008`, and a socket opened with a bearer token closes once the token expires. The client opens it again, as who they are now.
188
+ - **The function's own checks are not repeated,** such as whether the caller may still see one record. Pass them as `recheck`, which runs right after the framework's check:
189
+
190
+ ```ts title="functions/repair-watch.ts"
191
+ import { defineFunction, notFound } from '@protobase/server'
192
+
193
+ // /api/functions/repair-watch/7, opened as a WebSocket by a signed-in page: repair 7's status, for as long as the caller
194
+ // may see the repair. A repair their access rules hide is a 404 and no socket; once it moves out of their sight, such
195
+ // as to another mechanic, `recheck` rejects with that 404 and the socket closes with 1008 within a minute.
196
+ export default defineFunction({ roles: ['mechanic', 'desk'] }, async (request, { records, acceptWebSocket }) => {
197
+ const id = Number(new URL(request.url).pathname.slice(1))
198
+ if (!Number.isInteger(id) || id <= 0) throw notFound('Watch a repair at /api/functions/repair-watch/<id>')
199
+ const { record: repair } = await records.get('repairs', id)
200
+ return acceptWebSocket({
201
+ onOpen: (socket) => socket.send(JSON.stringify({ status: repair.status })),
202
+ recheck: () => records.get('repairs', id),
203
+ })
204
+ })
205
+ ```
185
206
  - **A plain request** to a function that accepts a socket is a `426` problem.
186
207
 
187
208
  ### Agents and other clients without a cookie
188
209
 
189
- A [public function](#public-functions-and-webhooks) can accept a socket too, and checks its caller itself, for example a host agent's key in a header. The framework does not check it again while the socket is open, so a function whose access can be revoked keeps checking, and calls `socket.close()` when it is.
210
+ A [public function](#public-functions-and-webhooks) can accept a socket too. One that admits its caller with `await caller()` is checked again like a protected one. One that checks a credential of its own, for example a host agent's key in a header, passes `recheck` to check it again, since the framework cannot.
190
211
 
191
212
  A page with no session cookie of the app, such as the workshop's display board on its own origin, gets a short-lived ticket from a plain call with its token and puts it in the socket's address:
192
213
 
@@ -232,11 +253,17 @@ import { ticketUser } from './_shared/tickets'
232
253
 
233
254
  // /api/functions/display?ticket=…, the workshop's display board, opened as a WebSocket from a page with no session
234
255
  // cookie of this app. Public, so it checks the ticket itself: a missing, forged or expired one gets a 401 and no socket.
256
+ // Nothing checks the ticket's user again while the socket is open, so `recheck` ends it after 15 minutes, as a signed-in
257
+ // token would end, and the board opens it again with a new ticket.
235
258
  export default defineFunction({ public: true }, (request, { acceptWebSocket }) => {
236
259
  const user = ticketUser(new URL(request.url).searchParams.get('ticket') ?? '')
237
260
  if (!user) throw unauthorized('Get a ticket from /api/functions/display-ticket first')
261
+ const until = Date.now() + 15 * 60_000
238
262
  return acceptWebSocket({
239
263
  onOpen: (socket) => socket.send(JSON.stringify({ shownFor: user })),
264
+ recheck: () => {
265
+ if (Date.now() >= until) throw unauthorized('Get a new ticket')
266
+ },
240
267
  })
241
268
  })
242
269
  ```
@@ -247,7 +274,7 @@ const { ticket } = await client.invoke('display-ticket', { method: 'POST' })
247
274
  const board = new WebSocket(`wss://pips-bikes.example/api/functions/display?ticket=${encodeURIComponent(ticket)}`)
248
275
  ```
249
276
 
250
- The ticket only lets the socket open: anyone who holds it may use it for its 30 seconds, so keep it out of logs. For a socket that stays open long, check the user again from the function, as a protected function's socket is.
277
+ The ticket only lets the socket open, and anyone who holds it may open one with it during its 30 seconds. It is in the address, so it appears in the request log when `REQUEST_LOG` is `true`. Nothing checks the ticket's user again, so `display.ts`'s `recheck` ends the socket after 15 minutes, and the board opens it again with a new ticket, which `display-ticket` mints only after checking the role again.
251
278
 
252
279
  ### Slow readers and large messages
253
280