fix: bound Service Worker marker reads and attribute native effects

SW-RR-01. The activation marker was read with response.text() whenever no
Content-Length was present, so a large or non-terminating body could consume
the whole activation step. It now reads through a bounded reader that stops one
byte past the ceiling, cancels its reader, applies a read deadline and decodes
UTF-8 fatally.

SW-RR-02. A matching nonce is not identity. An activation or reset result whose
event.source is null can no longer stand in for the expected worker; only a
strict identity match is admitted.

SW-RR-03. The build generator and the shared manifest decoder now read one
exported extension table, so .mjs and .png stop being emitted-then-refused.
.json is deliberately outside it: every JSON file in a build output is a control
document the generator already excludes, not a cacheable asset.

SW-RR-04. Both caches.open and cache.match are closed as a miss. Letting a
match rejection propagate rejected respondWith itself, so the entry never
reached its network fallback.

WP-RR-01. focus and openWindow now carry the certainty phase showNotification
already had — NOT_APPLIED, MAYBE_APPLIED, CONFIRMED — and an effect that lands
after the handler deadline is observed exactly once. The evidence never
authorizes a retry.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-08-14 14:10:41 +09:00
co-authored by Claude Opus 5
parent bd90e0c983
commit efc577de63
6 changed files with 272 additions and 25 deletions
@@ -3,6 +3,7 @@ import {
WEB_PUSH_PROTOCOLS,
webPushFailure,
webPushSuccess,
type WebPushNativeEffectCertainty,
type WebPushObserver,
type WebPushResult,
} from "../../../contracts/web-push.ts";
@@ -44,6 +45,29 @@ export type NotificationClickAdapter = Readonly<{
handle(event: NotificationClickEventFacade): Promise<WebPushResult<void>>;
}>;
/**
* WP-RR-01. Bounds a native effect by the handler lifetime while keeping the
* abandoned promise observable exactly once.
*/
const ABORT_OWNED = Symbol("web-push-click-aborted");
async function raceAbort<Value>(
operation: Promise<Value>,
signal: AbortSignal,
): Promise<Value | typeof ABORT_OWNED> {
if (signal.aborted) return ABORT_OWNED;
let onAbort: (() => void) | undefined;
const aborted = new Promise<typeof ABORT_OWNED>((resolve) => {
onAbort = () => resolve(ABORT_OWNED);
signal.addEventListener("abort", onAbort, { once: true });
});
try {
return await Promise.race([operation, aborted]);
} finally {
if (onAbort) signal.removeEventListener("abort", onAbort);
}
}
export function createNotificationClickAdapter(dependencies: Readonly<{
fenceStore: PushAssociationFenceStore;
clients: WorkerClientsFacade;
@@ -156,23 +180,98 @@ export function createNotificationClickAdapter(dependencies: Readonly<{
expiresAt: decoded.value.expiresAt,
path,
});
// WP-RR-01. `focus` and `openWindow` are user-visible native effects, so
// they carry the same certainty phase `showNotification` already does:
// NOT_APPLIED before the call, MAYBE_APPLIED while the promise is pending,
// CONFIRMED on fulfilment. An effect that lands after this handler's
// deadline is still observed exactly once — as evidence only, never as
// authorization to retry.
let nativeEffect: WebPushNativeEffectCertainty = "NOT_APPLIED";
const observeLateEffect = (
pending: Promise<unknown>,
succeeded: (value: unknown) => boolean,
): void => {
let observed = false;
void pending.then(
(value) => {
if (observed) return;
observed = true;
observeWebPush(dependencies.observer, {
event: "web_push_click_dispatched",
outcome: succeeded(value) ? "DEGRADED" : "FAILED",
reason: "ABORTED",
nativeEffect: succeeded(value) ? "CONFIRMED" : "NOT_APPLIED",
});
},
() => {
if (observed) return;
observed = true;
observeWebPush(dependencies.observer, {
event: "web_push_click_dispatched",
outcome: "FAILED",
reason: "ABORTED",
nativeEffect: "NOT_APPLIED",
});
},
);
};
try {
if (signal.aborted) {
return webPushFailure("ABORTED", "NOTIFICATION_CLICK");
}
if (existing) {
existing.postMessage(handoff);
await existing.focus();
const focused = Promise.resolve(existing.focus());
nativeEffect = "MAYBE_APPLIED";
const raced = await raceAbort(focused, signal);
if (raced === ABORT_OWNED) {
observeLateEffect(focused, () => true);
return webPushFailure("ABORTED", "NOTIFICATION_CLICK");
}
nativeEffect = "CONFIRMED";
} else {
const opened = await dependencies.clients.openWindow(target);
if (!opened) return nativeFailure("NOTIFICATION_CLICK", true);
const opening = Promise.resolve(
dependencies.clients.openWindow(target),
);
nativeEffect = "MAYBE_APPLIED";
const raced = await raceAbort(opening, signal);
if (raced === ABORT_OWNED) {
observeLateEffect(opening, (value) => value !== null);
return webPushFailure("ABORTED", "NOTIFICATION_CLICK");
}
if (!raced) {
nativeEffect = "NOT_APPLIED";
observeWebPush(dependencies.observer, {
event: "web_push_click_dispatched",
outcome: "FAILED",
nativeEffect,
});
return nativeFailure("NOTIFICATION_CLICK", true);
}
nativeEffect = "CONFIRMED";
}
if (signal.aborted) {
observeWebPush(dependencies.observer, {
event: "web_push_click_dispatched",
outcome: "DEGRADED",
reason: "ABORTED",
nativeEffect,
});
return webPushFailure("ABORTED", "NOTIFICATION_CLICK");
}
} catch {
observeWebPush(dependencies.observer, {
event: "web_push_click_dispatched",
outcome: "FAILED",
nativeEffect,
});
return nativeFailure("NOTIFICATION_CLICK", true);
}
observeWebPush(dependencies.observer, {
event: "web_push_click_dispatched",
outcome: "SUCCEEDED",
nativeEffect,
});
return webPushSuccess(undefined);
}