* fix(gui): desktop push status counts only Puter's devices and follows subscription changes - fix(gui): desktop push status counts only Puter's own devices - fix(gui): the desktop keeps its push subscription when the browser replaces it - docs(gui): shorten push comments to one line - fix(gui): removed browsers stay off after a subscription change, and websites can reach Puter push - docs(puter-js): fit the desktop push request comment on one line - fix(gui): background push registration checks every earlier endpoint and waits out a 429 - fix(puter-js): inside the desktop, requestPermission leaves a held push grant to the desktop - fix(gui): a browser that stays off after being removed forgets its receipt - fix(gui): push status holds right after enable(), and a subscription change is picked up after a run in flight - fix(gui): an app's push grant counts whenever the app list can't be read * fix(puter.js): a held push grant is left to the desktop only when it says it answers push The desktop lists `push-permission` in `puter.gui_features` on the app iframe's URL. Without it the SDK settles a held `notifications:push` from its own check, so a desktop whose requestPermission handler has no held check shows no dialog for a grant the app already holds. * fix(gui): a held push grant isn't asked again beside other access, and never counts without an app uid requestPermission drops a push grant held for Puter's devices from the dialog and asks only for what is missing. The app list lookup answers no for a missing app uid, whatever the list says; an app the list leaves out still keeps its grant, since the list is capped and can trail a grant. * test(puter.js): e2e for a push grant the app already holds inside the desktop A held grant resolves true with no dialog, and a request for it beside access the app is missing shows only the missing access. --------- Co-authored-by: Daniel Salazar <daniel.salazar@puter.com>
11 KiB
Browser push client
Puter's desktop shows pushes through /sw.js. Core shows Settings → Notifications only when the pushEnabled gui param is true; core never sets it. The worker is registered only when push is turned on in the browser, and a load removes one left without a subscription. A backend extension that delivers pushes sets it with services.homepage.setGuiParam('pushEnabled', true) and can load its own code into the worker with services.homepage.registerServiceWorkerScript(path).
Core subscribes the browser to a push service only when the server also publishes pushWebPushKey (see Built-in Web Push client), and it has no /push/* routes of its own. The device, app, preference and subscription endpoints below come from that extension. On a deployment without one, such as most self-hosted servers, they answer core's plain 404 (code: 'not_found'), which puter.notifications reports as push_unavailable and the Notifications tab shows as unavailable. A device the extension can't find is device_not_found.
Push payload
sw.js reads the push's JSON body. The message is its puter field, or data.puter for services that only carry string data, as an object or a JSON string:
{ uid?: string; title?: string; body?: string; icon?: string; group?: string;
target?: { app?: string; path?: string }; data?: unknown; channel?: string;
type?: 'challenge'; challenge?: string }
uid is the notification's uid, a later push with the same group replaces the one showing, and target and data reach the app when the user taps it.
When the browser replaces the subscription (pushsubscriptionchange), sw.js keeps the new one, or subscribes again with Puter's stored key when the browser gave none and the event, the receipt or an earlier mark names the subscription that went. It writes puter:subscription-changed:<scope> to the puter-push IndexedDB as { at, oldEndpoint }, the old endpoint kept from an earlier mark no page handled yet, else taken from the event or from the receipt, forgets the receipt kept for that scope, and posts { type: 'puter-push-subscription-changed' } to every open window on Puter's origin. The worker has no token or session, so an open page runs helpers/pushSync.js on that message, after any run already in flight, and a later load does the same through the marker.
Receipts
Before a notification is pushed, the server asks the open clients whether they received it live. It sends notif.receipt with { uid } on the user's sockets for the app that sent it, or on the desktop's sockets for Puter's own notifications. The SDK remembers the uids its page received through onNotification(), onPush() or the desktop's feed, and answers once its page has that notification, or with { received: false } when it never gets it. The server waits up to 5 seconds (notificationAckTimeoutMs).
An answer carries visibilityState and focused as they were when the page received the notification. Pages never show a browser notification themselves; pushes do.
- An answer from a page that was visible and focused stops the push (
handledonnotif.delivery.settled). An older client that doesn't sendfocusedcounts while visible. - Otherwise the notification is pushed, to every browser the user subscribed. A website's pushes go only to the browsers that subscribed through its own worker, never to Puter's
/sw.js. An app inside the desktop pushes through/sw.js, which shows the app's name and icon. - A client that can't answer, such as an older puter.js, doesn't hold the push back.
The ask reaches only the sockets in the asking region. So that other regions hear about a page too, a page that receives a notification live while visible and focused also sends notif.handled with { uid } on the socket it came on. Core relays it as the node-local notif.handled event with the socket's user and app, at most 60 a minute per user; an extension that delivers pushes decides what to do with it.
Registering
The registry is in src/gui/src/helpers/pushClient.js. A service script registers its client like this:
service_script(async (api) => {
api.use()['push.registerPushClient']?.(client);
});
use(id) throws when nothing is registered under id, so the form above also runs on a Puter version without the registry and does nothing there. Any script that runs after the GUI bundle can call use()['push.registerPushClient']?.(client) the same way. Registering again replaces the client. Registering dispatches push-client-registered on window, and an open Notifications tab redraws.
With no client registered, the tab says push notifications aren't available in this browser.
Built-in Web Push client
When the server also sets the pushWebPushKey gui param (a VAPID public key), core registers its own client (webPushClient.js) unless an extension registered one first. It uses puter.notifications.enable() / disable(), the same standard Web Push flow apps use on their own origins:
GET /push/web/keyfor the key, thenpushManager.subscribe()on the/sw.jsregistration.POST /push/web/subscriptions { subscription }. A subscription the server already has answers{ status: 'active' }. A new one answers{ status: 'pending' }, and the server sends it a push with{ type: 'challenge', challenge }.- The worker shows that push and posts
{ type: 'puter-push-challenge', challenge }to the origin's windows; the page sends it toPOST /push/web/subscriptions/verify. The device is registered once the page hands the confirmation back. DELETE /push/web/subscriptions { endpoint }removes it.
Apps on their own origin do the same through push-sw.js, which they import into their own worker. A subscription made with an app token is the app's: its pushes are that app's notifications only.
Interface
interface PushClient {
enable(options?: { background?: boolean }): Promise<{ deviceUid: string }>;
disable(): Promise<void>;
status(options?: { cached?: boolean }): Promise<{
permission: 'default' | 'granted' | 'denied';
registered: boolean;
deviceUid?: string;
}>;
}
enable()may ask for notification permission, so core calls it from a click: Turn on in the Notifications tab, Allow on a permission prompt that includesnotifications:pushwhile this browser is off, and the "Turn on notifications" button of the/action/enable-pushpopup and the matching desktop dialog (UIEnablePushDialog). Core also calls it without a click, fromhelpers/pushSync.js, whenNotification.permissionis alreadygranted,status()reports the browser as not registered, the user has not turned push off in this browser, and an app may push to Puter's devices (puterDevicesinGET /push/apps); the browser does not prompt then. That call passes{ background: true }: a browser the user removed in Puter stays removed, and the built-in client rejects with codepush_device_removed, after which core records the browser as turned off. Before a background call the built-in client asksPOST /push/web/subscriptions/statusabout every earlier endpoint it knows: the marker'soldEndpointand the last endpoint the server answeredactivefor (puter.push.last-endpoint:<user uuid>inlocalStorage). On a 429 it registers nothing and rejects withtoo_many_requests, which the sync reports aslater, keeping the marker for the next run. When any answer isremoved, it ends the current subscription in the browser, clears the marker and rejects withpush_device_removed, so a subscription change or a dropped subscription never brings back a browser removed from another device. A click renews it as before. For a click the built-in client runsputer.notifications.enable(), which subscribes with the/sw.jsregistration and registers the browser through the Web Push flow above (platform: 'webpush'); in the background it runs the same registration without replacing a removed subscription or opening Puter's sign-in. It then resolves theuidof this browser'swebpushdevice fromlistDevices()and clears the subscription-changed marker. It rejects when permission is refused or the browser wasn't registered.disable()runsputer.notifications.disable(), which sendsDELETE /push/web/subscriptions { endpoint }and ends the browser's subscription. The tab calls it for Turn off, and for Remove on the device marked as this browser. Remove on any other device isDELETE /push/devices/:uid.status()reports the state without prompting.permissionisNotification.permission.registeredis true when this browser has a device registered for the signed-in user andPOST /push/web/subscriptions/statusanswersactivefor the current subscription's endpoint, anddeviceUidis that device'suid. A server that can't answer leaves the device match alone, and so does anunknownfor an endpointenable()registered in this page in the last 5 minutes, since that check can trail a registration. While the subscription-changed marker is set,registeredis false. With{ cached: true }a client may answer with the last registered state it read in this page. Core passes it for the silent registration on app launch and for an app'sputer.notifications.status(); the Notifications tab and the push dialogs read fresh. The built-in client keeps that state in memory for the same user and subscription for up to 5 minutes, fromenable()or once the server answeredactivefor the endpoint, and answers from it without either request. Reads of the same user and endpoint share one real check while it is in flight. It drops it ondisable(), a failedenable(), an unregistered answer, a set subscription-changed marker, and the worker'sputer-push-subscription-changedmessage, so a new page load always asks the server. Whether the calling app may push is checked on every call, against aGET /push/appsanswer the page reuses for 30 seconds and drops onenable(),disable(), a grant, a revoke and a user change. When it can't be read, or leaves the app out, the grant alone counts.
After enable() resolves, the tab calls POST /push/devices/:uid/test and asks the user whether a notification showed up.
The tab also reads GET /push/apps and GET/POST /push/preferences.
Core keeps its own record of a browser the user turned off, puter.push.browser-off:<user uuid> in localStorage, written by Turn off, by Remove on this browser's device, and by a silent registration the server refuses with push_device_removed, and cleared when enable() resolves. The silent registration above skips a browser with that record, so a device removed here or from another device stays removed until the user turns it on from a click, while a new sign-in in a browser that already allows notifications registers again.