@protobase/cli 0.45.16 → 0.45.17

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.16";
2
+ var version = "0.45.17";
3
3
  //#endregion
4
4
  export { version };
package/dist/main.js CHANGED
@@ -22,6 +22,7 @@ import { registerFiles } from "./files/register.js";
22
22
  import { registerEncryption } from "./encryption/register.js";
23
23
  import { registerMigration } from "./migration/register.js";
24
24
  import { registerMobile } from "./mobile/register.js";
25
+ import { registerNotifications } from "./notifications/register.js";
25
26
  import { registerSkill } from "./skill/register.js";
26
27
  import { registerImport } from "./supabase/register.js";
27
28
  import { registerToken, registerUsers } from "./users/register.js";
@@ -138,6 +139,7 @@ registerEncryption(program);
138
139
  registerSkill(program);
139
140
  registerImport(program);
140
141
  registerMobile(program);
142
+ registerNotifications(program);
141
143
  const run = (argv) => program.parseAsync(argv).catch((error) => {
142
144
  process.stderr.write(`error: ${error.message}\n`);
143
145
  process.exitCode = 1;
@@ -0,0 +1,13 @@
1
+ import { generateVapidKeys, vapidPrivateKeyVariable, vapidPublicKeyVariable, vapidSubjectVariable } from "@protobase/server";
2
+ //#region src/notifications/print-vapid-keys.ts
3
+ /**
4
+ * Prints a fresh VAPID key pair as the two environment variables Web Push reads, and on `note` how to set the third.
5
+ * It reads no project, writes no file and makes no request.
6
+ */
7
+ const printVapidKeys = (out, note) => {
8
+ const { publicKey, privateKey } = generateVapidKeys();
9
+ out(`${vapidPublicKeyVariable}=${publicKey}\n${vapidPrivateKeyVariable}=${privateKey}\n`);
10
+ note(`Set ${vapidSubjectVariable} too, to a mailto: address or https: URL push services can reach you at. Keep the private key secret.\n`);
11
+ };
12
+ //#endregion
13
+ export { printVapidKeys };
@@ -0,0 +1,7 @@
1
+ import { printVapidKeys } from "./print-vapid-keys.js";
2
+ //#region src/notifications/register.ts
3
+ const registerNotifications = (program) => {
4
+ program.command("notifications").description("Set up the delivery of notifications").command("vapid-keys").description("Print a fresh VAPID key pair for Web Push, as PROTOBASE_VAPID_PUBLIC_KEY and PROTOBASE_VAPID_PRIVATE_KEY").action(() => printVapidKeys((text) => void process.stdout.write(text), (text) => void process.stderr.write(text)));
5
+ };
6
+ //#endregion
7
+ export { registerNotifications };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/cli",
3
- "version": "0.45.16",
3
+ "version": "0.45.17",
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.16",
40
- "@protobase/query": "0.45.16",
41
- "@protobase/schema": "0.45.16",
42
- "@protobase/server": "0.45.16",
43
- "@protobase/ui": "0.45.16",
39
+ "@protobase/layout": "0.45.17",
40
+ "@protobase/query": "0.45.17",
41
+ "@protobase/schema": "0.45.17",
42
+ "@protobase/server": "0.45.17",
43
+ "@protobase/ui": "0.45.17",
44
44
  "@react-email/render": "^2.1.0",
45
45
  "@scalar/hono-api-reference": "^0.12.10",
46
46
  "@tailwindcss/vite": "^4.3.3",
@@ -10,7 +10,7 @@ A bell in the top bar and push to browsers and phones, for what your app tells i
10
10
  - [Table names](../reference/notifications.md#table-names) (in development): The built-in stores' schema, tables and columns can be renamed, as auth's users table can.
11
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
12
  - Notification history (coming soon): Every notification a user got is kept, read or unread, for the bell to list.
13
- - Web Push (coming soon): Notifications reach the browsers a user registered, also while the app is closed.
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.
16
16
  - Notification bell (coming soon): A bell in the top bar shows a user's unread notifications and lists the latest.
@@ -459,6 +459,14 @@ Encrypts every plaintext value of the encrypted fields with the current key, enc
459
459
 
460
460
  Run it at update time, after the migrations and before the new version serves, and again once an old version that wrote plaintext has stopped. It works over one database connection, so a project's own `db` with a pool of one is enough.
461
461
 
462
+ ## `notifications vapid-keys`
463
+
464
+ ```sh
465
+ protobase notifications vapid-keys
466
+ ```
467
+
468
+ Prints a fresh key pair for [Web Push](notifications.md#web-push) as `PROTOBASE_VAPID_PUBLIC_KEY` and `PROTOBASE_VAPID_PRIVATE_KEY` lines to paste into the environment or `.env`, and says to set `PROTOBASE_VAPID_SUBJECT` too. It reads no project, writes no file and makes no request.
469
+
462
470
  ## `users`
463
471
 
464
472
  ```sh
@@ -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 keep the devices in Postgres or a store of your own.
5
+ Turn on notifications, let each signed-in user register their browsers and phones, deliver to browsers with Web Push, and keep the devices in Postgres or a store of your own.
6
6
 
7
7
  ## Turning them on
8
8
 
9
9
  :::caution[In development]
10
- Nothing sends notifications yet: sending, the history, Web Push, push on Android and iOS and the bell are coming soon.
10
+ Nothing sends notifications yet: sending, the history, 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`:
@@ -28,7 +28,7 @@ The bell and push are coming soon. `notifications({ ui: false })` keeps only the
28
28
  ## Devices
29
29
 
30
30
  :::caution[In development]
31
- A registered device receives nothing yet: Web Push and push on Android and iOS are coming soon.
31
+ A registered device receives nothing yet: sending is coming soon, and so are push on Android and iOS.
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,10 +60,34 @@ 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
+ ## Web Push
64
+
65
+ :::caution[In development]
66
+ Browsers cannot register for push yet, so nothing reaches them until browser registration lands.
67
+ :::
68
+
69
+ Browsers receive push through the Web Push protocol: desktop and Android browsers, and web apps added to the home screen on iPhone. You generate a key pair with the CLI and set it, with a contact address, in three environment variables:
70
+
71
+ ```sh
72
+ protobase notifications vapid-keys
73
+ ```
74
+
75
+ | Variable | |
76
+ | --- | --- |
77
+ | `PROTOBASE_VAPID_PUBLIC_KEY` | the public key the command prints |
78
+ | `PROTOBASE_VAPID_PRIVATE_KEY` | the private key the command prints |
79
+ | `PROTOBASE_VAPID_SUBJECT` | how push services reach you: a `mailto:` address or an `https:` URL |
80
+
81
+ Without them Web Push is off and the app has no sender for browsers. One or two of them set, a blank one, a key that is not one, or a public key that is not the private key's stops startup, naming the variable without its value.
82
+
83
+ 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.
84
+
85
+ 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.
86
+
63
87
  ## Your own parts
64
88
 
65
89
  :::caution[In development]
66
- Only the device store can be replaced yet: senders, the history store and the bell are coming soon.
90
+ The device store and the senders can be replaced; the history store and the bell are coming soon.
67
91
  :::
68
92
 
69
93
  The device store is an interface with a Postgres default, the [devices table](#table-names). You pass your own store to `notifications()`, such as one from a package that keeps devices elsewhere, and then no table is needed and `protobase migration` writes none:
@@ -87,7 +111,49 @@ Every method takes and returns plain data: the user id as text, and devices as `
87
111
 
88
112
  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, options).removeAllForUser(userId)` as it deletes one, with the options it gave `notifications()`.
89
113
 
90
- Senders, the history store and the bell are coming soon.
114
+ ### Senders
115
+
116
+ A sender delivers notifications to the devices of the kinds it names, through their push service. Web Push is built in; you pass your own senders to `notifications()`, such as from a package, and they replace the built-in ones, at most one for each kind:
117
+
118
+ ```ts
119
+ import { defineConfig, notifications, type PushSender } from '@protobase/server'
120
+
121
+ const sender: PushSender = {
122
+ kinds: ['fcm'],
123
+ send: async (message, devices) => devices.map((device) => ({ id: device.id, result: 'ok' })),
124
+ }
125
+
126
+ export default defineConfig({ extends: [notifications({ senders: [sender] })] })
127
+ ```
128
+
129
+ `send` takes a message and a batch of devices of those kinds, and answers each device by its id with a result:
130
+
131
+ | Result | |
132
+ | --- | --- |
133
+ | `ok` | the push service took the message |
134
+ | `gone` | the device no longer exists there, and should be removed |
135
+ | `retry` | the push service may take it later; `retryAfterSeconds` says when, if the service does |
136
+
137
+ 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.
138
+
139
+ A message is plain data:
140
+
141
+ | Field | |
142
+ | --- | --- |
143
+ | `title` | the title, required |
144
+ | `body` | the text under it |
145
+ | `url` | opened on tap: an absolute `http` or `https` URL, on the app's origin or any other |
146
+ | `data` | JSON the app reads when the notification is opened |
147
+ | `badge` | the number on the app's icon |
148
+ | `sound` | the sound to play |
149
+ | `tag` | a later notification with the same tag replaces this one: up to 32 letters, digits, `-` and `_` |
150
+ | `ttl` | how long, in seconds, a push service keeps it for a device that is offline, up to 28 days |
151
+ | `urgency` | `very-low`, `low`, `normal` or `high` |
152
+ | `platform` | each protocol's own fields: `web`, `apns` and `fcm` |
153
+
154
+ `pushMessage(message)` checks a message and answers it as senders get it, or throws a `PushMessageError` that names the field at fault, never its value. Every sender gets a checked message: a `url` with any other scheme, such as `javascript:` or `data:`, or a relative one, is refused, and so is a field that is not one of the message's, a key `__proto__`, `constructor` or `prototype` anywhere in it, or text with control characters. A sender for `web` devices names the VAPID `publicKey` browsers subscribe with, which `/meta` gives every caller; it must be a P-256 public key in base64url.
155
+
156
+ The history store and the bell are coming soon.
91
157
 
92
158
  ## Table names
93
159