@protobase/cli 0.34.3 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build/npm-ws.js +15 -0
- package/dist/build/serve-runtime.js +2 -1
- package/dist/cli/package.js +1 -1
- package/dist/dev/api-websockets.js +2 -2
- package/dist/project/api-websocket-server.js +40 -0
- package/dist/serve/lifecycle.js +2 -2
- package/package.json +6 -6
- package/skills/protobase/references/reference/custom-components.md +2 -0
- package/skills/protobase/references/reference/data-config.md +3 -1
- package/skills/protobase/references/reference/supabase-realtime.md +2 -0
- package/skills/protobase/references/reference/webhooks.md +2 -2
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
//#region src/build/npm-ws.ts
|
|
4
|
+
const require = createRequire(import.meta.url);
|
|
5
|
+
/**
|
|
6
|
+
* Bundles npm `ws` for a bare `ws` import. Built on Bun, Vite takes `ws` for a builtin and leaves it out, and the runtime
|
|
7
|
+
* then gets Bun's own shim, which realtime refuses (`apiWebSocketServer`).
|
|
8
|
+
*/
|
|
9
|
+
const npmWs = () => ({
|
|
10
|
+
name: "protobase-npm-ws",
|
|
11
|
+
enforce: "pre",
|
|
12
|
+
resolveId: (id) => id === "ws" ? path.join(path.dirname(require.resolve("ws/package.json")), "wrapper.mjs") : void 0
|
|
13
|
+
});
|
|
14
|
+
//#endregion
|
|
15
|
+
export { npmWs };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { viteBundle } from "./vite-bundle.js";
|
|
2
2
|
import { moduleFile } from "../module-file.js";
|
|
3
|
+
import { npmWs } from "./npm-ws.js";
|
|
3
4
|
import { fileURLToPath } from "node:url";
|
|
4
5
|
//#region src/build/serve-runtime.ts
|
|
5
6
|
const cliRoot = fileURLToPath(new URL("../..", import.meta.url));
|
|
@@ -10,7 +11,7 @@ const buildServeRuntime = async ({ outFile, bun }) => {
|
|
|
10
11
|
entry: serveMain,
|
|
11
12
|
outFile,
|
|
12
13
|
bun,
|
|
13
|
-
plugins: []
|
|
14
|
+
plugins: [npmWs()]
|
|
14
15
|
});
|
|
15
16
|
};
|
|
16
17
|
//#endregion
|
package/dist/cli/package.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { apiWebSocketServer } from "../project/api-websocket-server.js";
|
|
1
2
|
import { isAllowedHost } from "./allowed-host.js";
|
|
2
3
|
import { isProjectFile } from "./project-files.js";
|
|
3
4
|
import { createAdaptorServer } from "@hono/node-server";
|
|
4
|
-
import { WebSocketServer } from "ws";
|
|
5
5
|
//#region src/dev/api-websockets.ts
|
|
6
6
|
const isApiWebSocket = (url) => /^\/api\/sb\/realtime\/v1\/websocket(\?|$)/.test(url) || /^\/api\/functions\/[^/?#]+(\/|\?|$)/.test(url);
|
|
7
7
|
const restartCode = 1012;
|
|
@@ -15,7 +15,7 @@ const apiUpgrades = (allowedHosts, forward) => (request, socket, head) => {
|
|
|
15
15
|
socket.end("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
|
|
16
16
|
};
|
|
17
17
|
const apiWebSockets = (entry, dirs) => {
|
|
18
|
-
const wss =
|
|
18
|
+
const wss = apiWebSocketServer();
|
|
19
19
|
return {
|
|
20
20
|
name: "protobase-api-websockets",
|
|
21
21
|
configureServer: (server) => {
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { realtimeLimits } from "@protobase/server";
|
|
4
|
+
import { WebSocketServer } from "ws";
|
|
5
|
+
//#region src/project/api-websocket-server.ts
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
const npmWebSocketServer = () => {
|
|
8
|
+
if (process.versions.bun === void 0 || WebSocketServer !== require("ws").WebSocketServer) return WebSocketServer;
|
|
9
|
+
return require(path.join(path.dirname(require.resolve("ws/package.json")), "index.js")).WebSocketServer;
|
|
10
|
+
};
|
|
11
|
+
const functionsPrefix = "/api/functions/";
|
|
12
|
+
const functionMaxPayload = 104857600;
|
|
13
|
+
/**
|
|
14
|
+
* The frame cap of a socket upgraded at `url`: ws's default for an api function's socket, and realtime's
|
|
15
|
+
* `maxFrameBytes` for any other, so a path this does not recognise as a function's, such as an encoded one, gets the
|
|
16
|
+
* smaller cap.
|
|
17
|
+
*/
|
|
18
|
+
const maxPayloadOf = (url) => {
|
|
19
|
+
if (!URL.canParse(url, "http://localhost")) return realtimeLimits.maxFrameBytes;
|
|
20
|
+
return new URL(url, "http://localhost").pathname.startsWith(functionsPrefix) ? functionMaxPayload : realtimeLimits.maxFrameBytes;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* The server of npm `ws` that upgrades the API's sockets, realtime's and the app's functions', on Node and Bun: a frame
|
|
24
|
+
* over its socket's cap (`maxPayloadOf`) closes the socket with 1009 before it is read.
|
|
25
|
+
*/
|
|
26
|
+
const apiWebSocketServer = () => {
|
|
27
|
+
const Base = npmWebSocketServer();
|
|
28
|
+
class ApiWebSocketServer extends Base {
|
|
29
|
+
handleUpgrade(...[request, ...rest]) {
|
|
30
|
+
this.options.maxPayload = maxPayloadOf(request.url ?? "");
|
|
31
|
+
super.handleUpgrade(request, ...rest);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return new ApiWebSocketServer({
|
|
35
|
+
noServer: true,
|
|
36
|
+
maxPayload: realtimeLimits.maxFrameBytes
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
//#endregion
|
|
40
|
+
export { apiWebSocketServer, maxPayloadOf };
|
package/dist/serve/lifecycle.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { apiWebSocketServer } from "../project/api-websocket-server.js";
|
|
1
2
|
import { withClientAddress } from "./client-address.js";
|
|
2
3
|
import { projectAdmin } from "../project/project-admin.js";
|
|
3
4
|
import { createPgDb } from "../project/pg-db.js";
|
|
@@ -6,7 +7,6 @@ import { startCleanupTimer } from "./cleanup-timer.js";
|
|
|
6
7
|
import { closeSockets } from "./close-sockets.js";
|
|
7
8
|
import { backgroundSettled, checkAi, checkAuthSchema, checkChanges, checkComments, checkComputed, checkJobsSchema, checkWebhooksSchema, configExports, createFileCleanup, startJobs } from "@protobase/server";
|
|
8
9
|
import { createAdaptorServer, upgradeWebSocket } from "@hono/node-server";
|
|
9
|
-
import { WebSocketServer } from "ws";
|
|
10
10
|
import pg from "pg";
|
|
11
11
|
//#region src/serve/lifecycle.ts
|
|
12
12
|
const listen = (server, port) => new Promise((resolve, reject) => {
|
|
@@ -49,7 +49,7 @@ const startServe = async ({ project, env, createDb = (url) => createPgDb(pg, url
|
|
|
49
49
|
...jobs && { jobs },
|
|
50
50
|
...env.drainToken && { drainToken: env.drainToken }
|
|
51
51
|
});
|
|
52
|
-
const sockets =
|
|
52
|
+
const sockets = apiWebSocketServer();
|
|
53
53
|
const server = createAdaptorServer({
|
|
54
54
|
fetch: withClientAddress(app.fetch, env.trustedProxies),
|
|
55
55
|
overrideGlobalObjects: false,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.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": {
|
|
@@ -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.
|
|
40
|
-
"@protobase/query": "0.
|
|
41
|
-
"@protobase/schema": "0.
|
|
42
|
-
"@protobase/server": "0.
|
|
43
|
-
"@protobase/ui": "0.
|
|
39
|
+
"@protobase/layout": "0.36.0",
|
|
40
|
+
"@protobase/query": "0.36.0",
|
|
41
|
+
"@protobase/schema": "0.36.0",
|
|
42
|
+
"@protobase/server": "0.36.0",
|
|
43
|
+
"@protobase/ui": "0.36.0",
|
|
44
44
|
"@react-email/render": "^2.1.0",
|
|
45
45
|
"@scalar/hono-api-reference": "^0.12.10",
|
|
46
46
|
"@tailwindcss/vite": "^4.3.3",
|
|
@@ -71,6 +71,8 @@ The element stays data, `{ type: 'OrdersPerDay', props: { days: 30 }, children:
|
|
|
71
71
|
|
|
72
72
|
A component that asks for a one-time code uses `OtpInput` from `@protobase/ui`, the field of the app's own code pages: a box per digit drawn over one real input, so pasting a whole code into any box, the system's code autofill from a mail or a text message, and the phone's digit keyboard all work. It takes `value` and `onChange`, `length` (6 by default), `invalid` and `disabled`, and `onComplete` gets the code once its last digit is in, to check it at once. Ask for a code on a step of its own, before anything else.
|
|
73
73
|
|
|
74
|
+
A component that shows markdown, such as a chat message, uses `Markdown` from `@protobase/ui`, which markdown fields draw with: `source` is the markdown, in GitHub's flavour with line breaks kept, shown in the theme's styles. Raw HTML in it shows as text, links open in a new tab, and only links and images to web, mail and phone addresses or relative ones stay. `className` adds classes to its outer element, and `inLink` draws it as running text inside a link, such as a card, with its links and images as their text.
|
|
75
|
+
|
|
74
76
|
`protobase build` fails when the config declares a component that `protobase.ui.tsx` does not export, naming it; while you work, `protobase dev` draws a warning in its place. A component a plugin provides is declared with the plugin's name, `component('DraftOrders', { plugin: 'sales-tools' })`, and the build does not look for it in `protobase.ui.tsx`. A component that throws while drawing shows a warning in its own place, not a blank app. The object form, `defineUi({ components: { OrdersPerDay } })`, still works, and a name given both ways must be the same component.
|
|
75
77
|
|
|
76
78
|
### Fields and resources in props
|
|
@@ -206,6 +206,8 @@ iban: f.text().sensitive().access({ read: roles.can('screening.read') }),
|
|
|
206
206
|
|
|
207
207
|
A sensitive field cannot be `.filterable()`, `.sortable()`, aliased, searched, or the key, tenant, owner or soft delete field, and a view may only put it in a record page section: in a list, filter, chart, sidebar, title, recent-record group or action it would always be empty, so the config fails to load.
|
|
208
208
|
|
|
209
|
+
A [webhook subscription](webhooks.md)) delivering `as` a role runs that role's access rules on a captured change, which holds no sensitive field. A rule or row filter that reads one there throws, and the change is kept as failed for that subscription instead of delivered, so decide that access on a field that is not sensitive. A rule that reads the field before it checks the role, such as `record.api_secret == null || ctx.user.roles.includes('admin')`, fails even for `as: { roles: ['admin'] }`: check the role first, as in `ctx.user.roles.includes('admin') || ...`.
|
|
210
|
+
|
|
209
211
|
## Encrypted fields
|
|
210
212
|
|
|
211
213
|
An encrypted field is a [sensitive field](#sensitive-fields) whose value the server encrypts before it reaches the database, so a dump, a backup, a replica, a realtime subscriber or anyone running SQL sees only ciphertext. Only its reveal decrypts it, for a caller who may read the record and the field, and every reveal is audited. Every API write encrypts it: the record form, REST, batches, imports, [public forms](forms.md)) and `records` in [API functions](../guides/api-functions.md)). The [Supabase-compatible REST API](supabase-rest.md#encrypted-fields)) refuses to write or filter it.
|
|
@@ -218,7 +220,7 @@ It takes `f.text()` only, and cannot be `.readOnly()` or have a `.dbDefault()`.
|
|
|
218
220
|
|
|
219
221
|
**Write it only through the API.** SQL of your own, in an API function's `db`, a write hook's transaction or the assistant's approved changes, stores the value unencrypted, and its reveal then fails until [`protobase encryption apply`](cli.md#encryption-apply)) encrypts it.
|
|
220
222
|
|
|
221
|
-
**Your code never sees the stored value, only whether it is set.** Access rules cannot filter on it: a rule that returns a filter on it fails the request. Record rules, validators and write hooks get a stored record with a set value as `encrypted` (exported from `@protobase/schema`) and an empty one as `null`. Check whether it is set with `== null`; the marker is never equal to anything, so a rule comparing it with a constant decides as if it held another value, and reading it as text throws.
|
|
223
|
+
**Your code never sees the stored value, only whether it is set.** Access rules cannot filter on it: a rule that returns a filter on it fails the request. Record rules, validators and write hooks get a stored record with a set value as `encrypted` (exported from `@protobase/schema`) and an empty one as `null`. Check whether it is set with `== null`; the marker is never equal to anything, so a rule comparing it with a constant decides as if it held another value, and reading it as text throws. A [webhook subscription's access check](#sensitive-fields) cannot know whether it is set, as for every sensitive field. Rules and validators see the plaintext only where a write gives it, so a validator written this way checks each value written and leaves a stored one alone:
|
|
222
224
|
|
|
223
225
|
```ts
|
|
224
226
|
.validate((r) => (typeof r.iban === 'string' && !r.iban.startsWith('NL') ? [{ field: 'iban', message: 'A Dutch IBAN' }] : undefined))
|
|
@@ -44,6 +44,8 @@ room
|
|
|
44
44
|
|
|
45
45
|
A broadcast reaches the topic's other channels, and the sender's own with `self`; with `ack`, `send` resolves once the server has it. Binary payloads go to clients on the default serializer (`vsn` 2.0.0) only. Presence follows Phoenix Presence: a channel tracks one presence under its `presence.key` (a random one when it names none), tracking again replaces it, and channels with presence enabled get the state when they join and each join and leave after, their own included. A channel that leaves, closes or whose token expires leaves the presence it tracked.
|
|
46
46
|
|
|
47
|
+
Limits, near Supabase's Free plan, apply per socket and per topic. A socket sends at most 100 broadcast and presence messages a second, joins at most 100 times a second and holds at most 100 channels; on each channel it sends at most 5 presence updates in 30 seconds, also counted across a rejoin. A topic takes 100 broadcasts a second, whoever sends them, so one busy sender can use up a topic's budget. A topic is at most 255 bytes, and a tracked presence at most 10 000 bytes of JSON. A message over a limit gets an error reply and goes to no one, where Supabase would close the channel. A frame over 256 KiB closes the socket with 1009. It closes with 1008 when the client leaves more than 4 MiB unread, or sends more than 300 messages or 4 MiB while earlier ones wait, such as behind a token check.
|
|
48
|
+
|
|
47
49
|
Broadcasts and presence go between the channels of one serving process, which is how a cloud app runs; several processes behind one address do not reach each other's channels. Sending without a joined channel, which supabase-js does over `POST /api/sb/realtime/v1/api/broadcast`, is not supported.
|
|
48
50
|
|
|
49
51
|
## What a subscriber receives
|
|
@@ -54,7 +54,7 @@ export const rushOrders = subscription('rushOrders', {
|
|
|
54
54
|
| --- | --- |
|
|
55
55
|
| `events` | Event names (`orders.created`), every event of a resource (`orders.*`), or all of them (`*`). Each must match a declared event. |
|
|
56
56
|
| `when` | A filter on the record after the change, or before it for a delete, in the language of access rules. |
|
|
57
|
-
| `as: { roles }` | Delivers as these roles: only records they may read, and only fields they may read. Without it, every record and every field but the sensitive ones. |
|
|
57
|
+
| `as: { roles }` | Delivers as these roles: only records they may read, and only fields they may read. Without it, every record and every field but the sensitive ones. Their access rules cannot read a sensitive field, which a captured change does not hold: a change they would need one for is kept as failed. Check the role before such a field, as in `ctx.user.roles.includes('admin') \|\| record.api_secret == null`, or decide on a field that is not sensitive (see [Sensitive fields](data-config.md#sensitive-fields))). |
|
|
58
58
|
| `tenant` | Only events of this tenant's records. |
|
|
59
59
|
| `payload` | `thin` leaves `after` and `before` out, for a receiver that reads the record through the API. Default `full`. |
|
|
60
60
|
| `ordered` | One delivery at a time per record, in commit order. Default false. |
|
|
@@ -96,7 +96,7 @@ A function target is called as the subscription's `as`, so `context.caller()` an
|
|
|
96
96
|
- **After the last try** the job is kept as failed, on the [failed jobs page](jobs.md#failed-jobs-page)), where it can be retried.
|
|
97
97
|
- **Ordered subscriptions** get no first try from the dispatch job: a `webhooks.deliver-ordered` job delivers a record's events of one dispatch in order, retried with doubling waits from 5 s up to a day, 10 tries. Meanwhile the record's later events to that subscription wait. A retry sends again the events of that job that went through before the one that failed.
|
|
98
98
|
- **At least once:** a process that stops after delivering but before recording it delivers again when the dispatch job is retried, under the same `webhook-id`.
|
|
99
|
-
- **Event code that throws**, such as a `match` that reads a field it did not expect to be empty, a subscription's `when` or an access rule of its `as` roles, keeps that change as a failed `webhooks.dispatch-change` job on the failed jobs page, naming the event or subscription and the error, and dispatch moves on to later changes. Retry it once the code is fixed to deliver the change's events. A database error instead fails the whole dispatch job, which is retried.
|
|
99
|
+
- **Event code that throws**, such as a `match` that reads a field it did not expect to be empty, a subscription's `when` or an access rule of its `as` roles, keeps that change as a failed `webhooks.dispatch-change` job on the failed jobs page, naming the event or subscription and the error, and dispatch moves on to later changes. Retry it once the code is fixed to deliver the change's events. An access rule of `as` roles keeps the change as failed for that subscription only: the other subscriptions still receive it, and the retry delivers it to that subscription alone. A database error instead fails the whole dispatch job, which is retried.
|
|
100
100
|
|
|
101
101
|
Every outcome is a line in the server log, such as `webhook newOrdersToSlack orders.created evt_48211_created delivered (200)`.
|
|
102
102
|
|