Files
puter/src/gui/src/helpers/webPushClient.js
T
Juan Fernando CastroandDaniel Salazar 4a958b3ca1 fix(gui): desktop push status counts only Puter's devices and follows subscription changes (#4550)
* 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>
2026-11-12 20:06:23 -07:00

247 lines
11 KiB
JavaScript

/*
* Copyright (C) 2024-present Puter Technologies Inc.
*
* This file is part of Puter.
*
* Puter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Puter's own push client: standard Web Push through `/sw.js` via `puter.notifications`, used when the server publishes `pushWebPushKey`.
import { forgetPushApps, webPushEndpointState } from './notificationApi.js';
/** The GUI's worker, as `puter.notifications` registers it. */
const GUI_WORKER = { url: '/sw.js', scope: '/' };
/** What `/sw.js` posts when the browser replaced the subscription. */
const SUBSCRIPTION_CHANGED_MESSAGE = 'puter-push-subscription-changed';
// Where `/sw.js` marks a subscription the browser replaced, as puter.js's pushStore.js does.
const DB_NAME = 'puter-push';
const HANDLERS = 'handlers';
const CLICKS = 'clicks';
export const SUBSCRIPTION_CHANGED_PREFIX = 'puter:subscription-changed:';
export const RECEIPT_PREFIX = 'puter:receipt:';
/** The endpoint the server last confirmed here for each user, in `localStorage`. */
export const LAST_ENDPOINT_PREFIX = 'puter.push.last-endpoint:';
const removedError = () => Object.assign(new Error('this browser was removed from push in Puter'), { code: 'push_device_removed' });
const busyError = () => Object.assign(new Error('Puter asked to wait before registering this browser'), { code: 'too_many_requests' });
/** Runs one request on the handlers store. @param {IDBFactory} idb @param {IDBTransactionMode} mode @param {(store: IDBObjectStore) => IDBRequest} action */
const handlersStore = (idb, mode, action) => new Promise((resolve, reject) => {
const open = idb.open(DB_NAME, 1);
open.onupgradeneeded = () => {
open.result.createObjectStore(HANDLERS);
open.result.createObjectStore(CLICKS);
};
open.onerror = () => reject(open.error);
open.onsuccess = () => {
const db = open.result;
try {
const req = action(db.transaction(HANDLERS, mode).objectStore(HANDLERS));
req.onsuccess = () => {
db.close();
resolve(req.result);
};
req.onerror = () => {
db.close();
reject(req.error);
};
} catch ( e ) {
db.close();
reject(e);
}
};
});
/** How long a page answers `cached` reads from its last `on`. */
export const ON_KEPT_MS = 5 * 60 * 1000;
/** @returns {import('./pushClient.js').PushClient} */
export const webPushClient = ({
notifications = globalThis.puter?.notifications,
NotificationApi = globalThis.Notification,
nav = globalThis.navigator,
userUid = () => globalThis.user?.uuid,
idb = globalThis.indexedDB,
endpointState = webPushEndpointState,
storage = globalThis.localStorage,
now = Date.now,
} = {}) => {
// The last `on` this page read; a new page always asks the server.
/** @type {{ userUid: string, endpoint: string, deviceUid: string, at: number, enabled?: boolean }|null} */
let lastOn = null;
let generation = 0;
const forgetOn = () => {
lastOn = null;
generation++;
};
/** The real check in flight, shared by every read of the same user and endpoint. */
let checking = null;
const check = (user, endpoint) => {
const key = `${generation}\n${user ?? ''}\n${endpoint}`;
if ( checking?.key === key ) return checking.promise;
const entry = { key, promise: null };
entry.promise = Promise.all([currentDevice(), endpointState(endpoint)]).finally(() => {
if ( checking === entry ) checking = null;
});
checking = entry;
return entry.promise;
};
nav?.serviceWorker?.addEventListener?.('message', (event) => {
if ( event.data?.type === SUBSCRIPTION_CHANGED_MESSAGE ) forgetOn();
});
const currentDevice = async () => {
const devices = await notifications.listDevices();
return devices.find((d) => d.current && d.platform === 'webpush') ?? null;
};
const registration = async () => await nav.serviceWorker?.getRegistration('/');
/** The worker's mark for a replaced subscription, else null. */
const marker = async (scope) => {
try {
return (await handlersStore(idb, 'readonly', (s) => s.get(SUBSCRIPTION_CHANGED_PREFIX + scope))) ?? null;
} catch ( e ) {
return null;
}
};
const rememberEndpoint = (endpoint) => {
const user = userUid();
if ( ! user ) return;
try {
storage.setItem(LAST_ENDPOINT_PREFIX + user, endpoint);
} catch ( e ) {
// Only the removed-elsewhere check below reads it.
}
};
const lastEndpoint = () => {
try {
return storage.getItem(LAST_ENDPOINT_PREFIX + userUid());
} catch ( e ) {
return null;
}
};
const clearChanged = async () => {
try {
const scope = (await registration())?.scope;
if ( scope ) await handlersStore(idb, 'readwrite', (s) => s.delete(SUBSCRIPTION_CHANGED_PREFIX + scope));
} catch ( e ) {
// The next load registers again.
}
};
/** `'removed'` when the user removed any earlier endpoint of this browser, ending the subscription here; `'busy'` on a 429. */
const removedElsewhere = async () => {
const worker = await registration();
const mark = worker?.scope ? await marker(worker.scope) : null;
const earlier = [...new Set([mark?.oldEndpoint, lastEndpoint()].filter((e) => typeof e === 'string' && e))];
const states = await Promise.all(earlier.map((endpoint) => endpointState(endpoint)));
if ( ! states.includes('removed') ) return states.includes('busy') ? 'busy' : null;
try {
await (await worker?.pushManager?.getSubscription())?.unsubscribe();
} catch ( e ) {
// The next background run asks again.
}
try {
if ( worker?.scope ) await handlersStore(idb, 'readwrite', (s) => s.delete(RECEIPT_PREFIX + worker.scope));
} catch ( e ) {
// A receipt is only sent for its own endpoint.
}
await clearChanged();
return 'removed';
};
return {
// Not async before `enable()`: it asks the browser inside the click.
enable: ({ background = false } = {}) => {
forgetOn();
forgetPushApps();
const turningOn = background
? removedElsewhere().then(async (earlier) => {
if ( earlier === 'removed' ) throw removedError();
// The mark stays, so the next run asks again.
if ( earlier === 'busy' ) throw busyError();
await notifications._enableWebPush(GUI_WORKER, { background: true });
return true;
})
: notifications.enable();
return turningOn.then(async (on) => {
if ( ! on ) throw new Error(`notifications ${NotificationApi?.permission ?? 'unavailable'}`);
const reading = generation;
const user = userUid();
const device = await currentDevice();
if ( ! device ) throw new Error('this browser was not registered');
await clearChanged();
const subscription = await (await registration())?.pushManager?.getSubscription();
if ( subscription?.endpoint && user === userUid() ) {
rememberEndpoint(subscription.endpoint);
if ( reading === generation && user ) {
lastOn = { userUid: user, endpoint: subscription.endpoint, deviceUid: device.uid, at: now(), enabled: true };
}
}
return { deviceUid: device.uid };
}).catch((e) => {
// A read begun meanwhile keeps nothing from before the failure.
forgetOn();
throw e;
});
},
disable: async () => {
forgetOn();
forgetPushApps();
try {
await notifications.disable();
} finally {
forgetOn();
}
},
status: async ({ cached = false } = {}) => {
const permission = NotificationApi?.permission ?? 'default';
const worker = await registration();
const subscription = await worker?.pushManager?.getSubscription();
if ( permission !== 'granted' || ! subscription ) {
forgetOn();
return { permission, registered: false };
}
// A subscription the browser replaced is registered again in full.
if ( await marker(worker.scope) != null ) {
forgetOn();
return { permission, registered: false };
}
const user = userUid();
const kept = lastOn && lastOn.userUid === user && lastOn.endpoint === subscription.endpoint
&& now() - lastOn.at < ON_KEPT_MS;
if ( cached && kept ) {
return { permission, registered: true, deviceUid: lastOn.deviceUid };
}
const reading = generation;
// Devices match by session, so the endpoint tells whether it is this subscription.
const [device, state] = await check(user, subscription.endpoint);
// The server's answer can trail what `enable()` just registered in this page.
const trailing = state === 'unknown' && kept && lastOn?.enabled === true;
const registered = !! device && (state === 'active' || state === null || state === 'busy' || trailing);
if ( registered && state === 'active' && user === userUid() ) rememberEndpoint(subscription.endpoint);
if ( reading === generation && ! trailing ) {
// Only an `on` the server confirmed for this endpoint and user is kept.
lastOn = registered && state === 'active' && user && user === userUid()
? { userUid: user, endpoint: subscription.endpoint, deviceUid: device.uid, at: now() }
: null;
}
return registered
? { permission, registered: true, deviceUid: device.uid }
: { permission, registered: false };
},
};
};