@protobase/cli 0.45.18 → 0.45.19

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.45.18";
2
+ var version = "0.45.19";
3
3
  //#endregion
4
4
  export { version };
@@ -11,7 +11,7 @@ const migrationParts = (project) => {
11
11
  title: authMigrationTitle,
12
12
  sql: () => authSchemaMigration(project.auth)
13
13
  }] : [],
14
- ...project.jobs.length > 0 || project.models.some(isCaptured) || comments || aiFields || project.webhooks ? [{
14
+ ...project.jobs.length > 0 || project.models.some(isCaptured) || comments || aiFields || project.webhooks || project.notifications ? [{
15
15
  name: "jobs",
16
16
  title: "The jobs schema this version of Protobase needs",
17
17
  sql: () => jobsSchemaMigration(project.db)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/cli",
3
- "version": "0.45.18",
3
+ "version": "0.45.19",
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.45.18",
40
- "@protobase/query": "0.45.18",
41
- "@protobase/schema": "0.45.18",
42
- "@protobase/server": "0.45.18",
43
- "@protobase/ui": "0.45.18",
39
+ "@protobase/layout": "0.45.19",
40
+ "@protobase/query": "0.45.19",
41
+ "@protobase/schema": "0.45.19",
42
+ "@protobase/server": "0.45.19",
43
+ "@protobase/ui": "0.45.19",
44
44
  "@react-email/render": "^2.1.0",
45
45
  "@scalar/hono-api-reference": "^0.12.10",
46
46
  "@tailwindcss/vite": "^4.3.3",
@@ -8,8 +8,8 @@ A bell in the top bar and push to browsers and phones, for what your app tells i
8
8
  - [Devices](../reference/notifications.md#devices) (in development): Each signed-in user registers their own browsers and phones to receive push, and a device moves to whoever registers it next.
9
9
  - [Your own parts](../reference/notifications.md#your-own-parts) (in development): Senders, the device store, the history store and the bell can each be replaced by your own, such as a store outside the database.
10
10
  - [Table names](../reference/notifications.md#table-names) (beta): The built-in stores' schema, tables and columns can be renamed, as auth's users table can.
11
- - Sending notifications (coming soon): Your code tells a user, or several, about something in the app, and each gets it in the bell and on their devices.
12
- - [Notification history](../reference/notifications.md#history) (in development): Each user's inbox notifications are kept with when they were read, and the API lists them, counts the unread ones and marks them read.
11
+ - [Sending notifications](../reference/notifications.md#sending) (beta): Your code sends a notification to users or to devices as a background job, queued in the same transaction as a write, and says on each one whether it shows in the inbox.
12
+ - [Notification history](../reference/notifications.md#history) (beta): Each user's inbox notifications are kept with when they were read, and the API lists them, counts the unread ones and marks them read.
13
13
  - [Web Push](../reference/notifications.md#web-push) (in development): Notifications reach desktop and Android browsers and home-screen web apps on iPhone through Web Push, with keys you generate.
14
14
  - Push on Android (coming soon): Notifications reach the Android build of the app on a user's phones.
15
15
  - Push on iOS (coming soon): Notifications reach the iOS build of the app on a user's iPhones and iPads.
@@ -68,7 +68,7 @@ export default defineConfig({
68
68
  | --- | --- |
69
69
  | `name` | Letters, digits, `_`, `-`, `.` and `/`. Two jobs may not share a name. |
70
70
  | `payload` | A [Standard Schema](https://standardschema.dev) such as a zod schema, checked when the job is enqueued and again before it runs. A payload it refuses at run time fails the job for good. |
71
- | `run(payload, { db, job, signal, mail })` | The work. `db` is the app's pool: the enqueuer's transaction has committed by then. `job` holds its `id`, `name`, `attempt` and `maxAttempts`. `signal` aborts when the try times out or the worker shuts down. `mail` sends mail, see [Mail](#mail). |
71
+ | `run(payload, { db, job, signal, mail, sendNotification })` | The work. `db` is the app's pool: the enqueuer's transaction has committed by then. `job` holds its `id`, `name`, `attempt`, `maxAttempts` and `startedOn`, when this try took it. `signal` aborts when the try times out or the worker shuts down. `mail` sends mail, see [Mail](#mail). `sendNotification` sends a notification, see [Sending](notifications.md#sending); inside a transaction of `db`, a try that throws in it queues nothing. |
72
72
  | `maxAttempts` | Tries before the job is kept as failed; default 10. |
73
73
  | `retryDelay`, `maxRetryDelay` | The wait before the first retry, doubling with each one (with some jitter), and its ceiling; default `2s` and `1 hour`. Milliseconds or text such as `30 seconds`. |
74
74
  | `timeout` | How long one try may run before it counts as failed; default `15 minutes`. |
@@ -2,12 +2,12 @@
2
2
 
3
3
  # Notifications
4
4
 
5
- Turn on notifications, let each signed-in user register their browsers and phones and read their notification history, deliver to browsers with Web Push, and keep devices and history in Postgres or stores of your own.
5
+ Turn on notifications, send them to users or devices, let each signed-in user register their browsers and phones and read their notification history, deliver to browsers with Web Push, and keep devices and history in Postgres or stores of your own.
6
6
 
7
7
  ## Turning them on
8
8
 
9
9
  :::caution[In development]
10
- Nothing sends notifications yet: sending, registering a browser, push on Android and iOS and the bell are coming soon.
10
+ Sending keeps notifications in the history and pushes them to browsers, but browsers cannot register for push yet: registering a browser, push on Android and iOS and the bell are coming soon.
11
11
  :::
12
12
 
13
13
  Notifications tell a user about something in the app, in a bell in the top bar and as push on their devices. You turn them on in `protobase.config.ts`:
@@ -21,14 +21,14 @@ export default defineConfig({
21
21
  })
22
22
  ```
23
23
 
24
- Then run `protobase migration`, which writes the [devices and history tables](#table-names) as a migration of the project's own, and apply it with the project's migrations. `protobase dev` and `protobase serve` refuse to start while a table is missing or lacks what notifications need, and name it. Without `notifications()` there is none of it: no endpoints, no tables and no migration.
24
+ Then run `protobase migration`, which writes the [devices and history tables](#table-names), and the jobs schema that [sending](#sending) runs on, as a migration of the project's own, and apply it with the project's migrations. `protobase dev` and `protobase serve` refuse to start while a table is missing or lacks what notifications need, and name it. Without `notifications()` there is none of it: no endpoints, no tables and no migration.
25
25
 
26
26
  The bell and push are coming soon. `notifications({ ui: false })` keeps only the backend, without the bell.
27
27
 
28
28
  ## Devices
29
29
 
30
30
  :::caution[In development]
31
- A registered device receives nothing yet: sending is coming soon, and so are push on Android and iOS.
31
+ Only browsers receive push yet, through [Web Push](#web-push): push on Android and iOS is coming soon.
32
32
  :::
33
33
 
34
34
  A device is where a user receives push: a browser's push subscription, or an Android or iOS app's push token. Each signed-in user registers and removes only their own, through the API:
@@ -60,13 +60,61 @@ A device is one token, or one endpoint for a browser. Registering a device someo
60
60
 
61
61
  The path comes from `devicesPath('/api', id)`, and the name of its id parameter from `deviceIdParam`, both exported by `@protobase/server`.
62
62
 
63
+ ## Sending
64
+
65
+ :::caution[Beta]
66
+ Only browsers receive push yet; push on Android and iOS is coming soon. <gap>A send can reach a device twice, or keep an entry twice in a history store of your own, when a worker stops mid-send because saving the send's progress in the same step as the push is not implemented.</gap>
67
+ :::
68
+
69
+ You send a notification from your server code, such as a job, a hook, an event subscription or an API function, with `sendNotification(db, to, message)` from `@protobase/server`. It is queued as a background job in `db`'s transaction when `db` is one, so it goes out only once your write commits, and nothing goes out when the transaction rolls back. A running job also has it as `sendNotification` in its context, beside `mail`, when the app has `notifications()`.
70
+
71
+ Every send to users says whether it shows in their inbox:
72
+
73
+ - With `inbox: true` it is kept in each user's [history](#history), which the bell will show, and is pushed to their devices, such as a notice that the app has a new version.
74
+ - With `inbox: false` it is only pushed, such as a chat message that links to its chat.
75
+ - A send to devices, `{ deviceIds }`, is only pushed, and takes no inbox flag.
76
+
77
+ ```ts
78
+ import { appPath } from '@protobase/schema'
79
+ import { sendNotification, subscription } from '@protobase/server'
80
+
81
+ // The app's public origin, from its own configuration.
82
+ const appOrigin = 'https://tasks.example.com'
83
+
84
+ /** Tells a task's owner when someone changes it. */
85
+ export const taskChanged = subscription('taskChanged', {
86
+ events: ['tasks.updated'],
87
+ run: async ({ data }, { db }) => {
88
+ const owner = data.after?.owner_id
89
+ if (owner == null) return
90
+ const url = new URL(appPath({ kind: 'record', resource: 'tasks', key: data.key }), appOrigin).href
91
+ await sendNotification(db, { userIds: [String(owner)], inbox: true }, { title: `${data.after?.title} changed`, url })
92
+ },
93
+ })
94
+ ```
95
+
96
+ The message is the one [senders](#senders) take. Its `url` opens on tap: a URL on your app's origin opens inside the app, and any other `http` or `https` URL opens outside it. Any other scheme, such as `javascript:` or `data:`, is refused. Build the URL on your app's configured origin, never on one a request names in `Host`, `X-Forwarded-Host`, `Origin` or its own URL, so nobody can make a notification lead to their site.
97
+
98
+ A send is checked before anything is queued, and a send that is not valid throws a `NotificationSendError` that names the field at fault, never its value. A valid send:
99
+
100
+ - names 1 to 1000 users or devices;
101
+ - has a user send's `inbox` as `true` or `false`;
102
+ - has a message of at most 16 KB as JSON, without a field that is not one of the message's;
103
+ - for the inbox, has a message the history keeps.
104
+
105
+ The text you send shows on lock screens, where anyone holding the device can read it, so keep anything the recipient may not see out of it. It stays in the jobs table until the send is delivered, and in a failed send until it is retried or deleted in the admin app.
106
+
107
+ A send checks no permission. A device send reaches the devices whatever user holds them. So check ids taken from a request against the caller first, such as a device id against the caller's own devices.
108
+
109
+ A user send reaches the user's devices as they are when the job runs. A device that moved to someone else, or was removed, gets nothing. A device stays its user's when a session ends, until it is [removed](#devices), and keeps receiving their notifications until then. When a push service reports a device gone, the device is removed. A device whose push service asks to try again later is tried once more, on the job's next try, and then given up on without being removed. <gap>A push service's own wait before a retry cannot set when the device is tried again because retries follow the job's backoff and timing them per device is not implemented.</gap> A sender that fails is retried with its devices, up to 5 tries of the job. Devices of a kind without a sender are skipped. Without any sender, a send for the inbox is kept in the history and a send that is not does nothing.
110
+
63
111
  ## History
64
112
 
65
- :::caution[In development]
66
- Nothing writes to the history until sending lands. <gap>The history cannot fill because sending notifications is not implemented.</gap>
113
+ :::caution[Beta]
114
+ The bell that shows the history is coming soon.
67
115
  :::
68
116
 
69
- The history is each user's inbox: every notification sent to a user with the inbox flag on is kept for them, and one sent without it is only pushed. Sending, which fills it, is coming soon. Each signed-in user reaches only their own, through the API:
117
+ The history is each user's inbox: every notification [sent](#sending) to a user with the inbox flag on is kept for them, and one sent without it is only pushed. Each signed-in user reaches only their own, through the API:
70
118
 
71
119
  | Request | |
72
120
  | --- | --- |
@@ -107,7 +155,7 @@ Without them Web Push is off and the app has no sender for browsers. One or two
107
155
 
108
156
  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.
109
157
 
110
- 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. <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 carries at most 3993 bytes.
158
+ 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 carries at most 3993 bytes.
111
159
 
112
160
  ## Your own parts
113
161
 
@@ -177,6 +225,7 @@ export default defineConfig({ extends: [notifications({ senders: [sender] })] })
177
225
  | `ok` | the push service took the message |
178
226
  | `gone` | the device no longer exists there, and should be removed |
179
227
  | `retry` | the push service may take it later; `retryAfterSeconds` says when, if the service does |
228
+ | `refused` | the sender never delivers to the device as it is stored, so it is neither retried nor removed |
180
229
 
181
230
  A device with no answer, or an answer that is none of these, counts as `retry`, and answers for devices outside the batch are ignored. What a sender throws becomes an error that names only the sender's place in the list, since its own error may name devices.
182
231