web-push/codec-primitives.ts now owns OPAQUE_ID/validOpaqueId, isRecord,
hasExactKeys, exactRecord, validAuthority, base64UrlDecode and (to share the
alphabet) base64UrlEncode. push-codec,
push-registration-gateway, push-association-fence-store,
push-subscription-adapter and service-worker-runtime import them instead of
carrying their own copies (the fence store's control/association key checks
now use exactRecord/hasExactKeys with the same key lists). The duplicate-key
JSON scan in push-codec.ts is untouched (realtime lane, T76).
Where the copies differed, the stricter validated behavior is kept:
- validAuthority: the fence store's null guard. The gateway's copy threw a
TypeError for a null authority, so register()/reconcile() rejected instead
of returning INVALID_INPUT; they now return INVALID_INPUT (the only change a
caller can observe; characterization test was RED before the merge).
Non-record values (arrays) are also refused.
- base64UrlDecode: the gateway's decoder, which refuses a lone trailing
character (length % 4 == 1). The window adapter's copy accepted it when the
spare bits were zero; its only caller pre-checks an 87-char VAPID key, so
that difference was not reachable. `>>` vs `>>>` gave identical bytes.
- OPAQUE_ID, isRecord/hasExactKeys/exactRecord were already identical.
Page controller: PageControllerDependencies gains optional `document`; the
6-hour update check reads it (falling back to the global document), and
bootstrap/register-service-worker.ts now passes host.document through.
New adapter file: INVENTORY.md row 126 + renumber, total 137/137; detail row in
docs/reviews/adapters/05-service-worker-and-web-push.md.
Verified: characterization tests pinned the caller-visible behavior of every
copy before the merge (spare-bit keys, VAPID key, null authority in fence
store and gateway, array/extra-key backend answers, opaque ids); all pass
after, plus direct base64UrlDecode tests and an encode round-trip against
Node's base64url for lengths 0..66. Document injection test was RED
(update() ran while the injected document was hidden), green after. Gates:
check:architecture, types app/test/service-worker, lint, test:unit (only
pre-existing removal-fixture failure), test:component,
check:realtime-boundaries, check:adapter-inventory.
Realtime reference bundle (check:optional-recipes:source, already over budget
at b953142): 40139 -> 40097 gzip bytes after T89+T90.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
294 lines
8.4 KiB
TypeScript
294 lines
8.4 KiB
TypeScript
import {
|
|
webPushCountBucket,
|
|
WEB_PUSH_LIMITS,
|
|
WEB_PUSH_PROTOCOLS,
|
|
webPushFailure,
|
|
webPushSuccess,
|
|
type WebPushObserver,
|
|
} from "../../contracts/web-push.ts";
|
|
import { isRecord } from "./codec-primitives.ts";
|
|
import type { PushAssociationFenceStore } from "./push-association-fence-store.ts";
|
|
import {
|
|
createPushEventAdapter,
|
|
type PushEventFacade,
|
|
} from "./inbound/push-event-adapter.ts";
|
|
import {
|
|
createNotificationClickAdapter,
|
|
type NotificationClickEventFacade,
|
|
type WindowClientFacade,
|
|
} from "./inbound/notification-click-adapter.ts";
|
|
import type {
|
|
AssociationNotificationTagDigest,
|
|
WebPushNotificationRegistry,
|
|
} from "./notification-registry.ts";
|
|
import {
|
|
createLinkedAbortController,
|
|
nativeFailure,
|
|
observeWebPush,
|
|
withAbortableDeadline,
|
|
type TimeoutScheduler,
|
|
} from "./runtime-support.ts";
|
|
|
|
type FunctionalEventFacade = Readonly<{
|
|
waitUntil(task: Promise<void>): void;
|
|
}>;
|
|
|
|
type ServiceWorkerRegistrationFacade = Readonly<{
|
|
showNotification(
|
|
title: string,
|
|
options: Readonly<{
|
|
body: string;
|
|
data: unknown;
|
|
requireInteraction: false;
|
|
tag: string;
|
|
}>,
|
|
): Promise<void>;
|
|
}>;
|
|
|
|
type ServiceWorkerClientsFacade = Readonly<{
|
|
matchAll(input: Readonly<{
|
|
type: "window";
|
|
includeUncontrolled: boolean;
|
|
}>): Promise<readonly unknown[]>;
|
|
openWindow(url: string): Promise<unknown>;
|
|
}>;
|
|
|
|
/**
|
|
* Structural worker host used deliberately instead of exposing DOM worker
|
|
* globals to application/test compilation. A selected worker entry adapts its
|
|
* native scope to this facade; this factory has no registration side effect.
|
|
*/
|
|
export type ServiceWorkerEventHost = Readonly<{
|
|
origin: string;
|
|
registration: ServiceWorkerRegistrationFacade;
|
|
clients: ServiceWorkerClientsFacade;
|
|
addEventListener(type: string, listener: (event: unknown) => void): void;
|
|
removeEventListener(type: string, listener: (event: unknown) => void): void;
|
|
}>;
|
|
|
|
export type WebPushServiceWorkerRuntime = Readonly<{
|
|
dispose(): void;
|
|
}>;
|
|
|
|
export function createWebPushServiceWorkerRuntime(
|
|
dependencies: Readonly<{
|
|
host: ServiceWorkerEventHost;
|
|
fenceStore: PushAssociationFenceStore;
|
|
registry: WebPushNotificationRegistry;
|
|
now?: () => number;
|
|
scheduler?: TimeoutScheduler;
|
|
observer?: WebPushObserver;
|
|
tagDigest?: AssociationNotificationTagDigest;
|
|
}>,
|
|
): WebPushServiceWorkerRuntime {
|
|
const lifecycle = new AbortController();
|
|
const push = createPushEventAdapter({
|
|
fenceStore: dependencies.fenceStore,
|
|
registry: dependencies.registry,
|
|
notifications: {
|
|
showNotification: (title, options) =>
|
|
dependencies.host.registration.showNotification(title, options),
|
|
},
|
|
now: dependencies.now,
|
|
scheduler: dependencies.scheduler,
|
|
observer: dependencies.observer,
|
|
tagDigest: dependencies.tagDigest,
|
|
signal: lifecycle.signal,
|
|
});
|
|
const click = createNotificationClickAdapter({
|
|
fenceStore: dependencies.fenceStore,
|
|
registry: dependencies.registry,
|
|
clients: {
|
|
async matchControlledWindowClients() {
|
|
const candidates = await dependencies.host.clients.matchAll({
|
|
type: "window",
|
|
includeUncontrolled: false,
|
|
});
|
|
return candidates
|
|
.map(windowClientFacade)
|
|
.filter(
|
|
(candidate): candidate is WindowClientFacade =>
|
|
candidate !== null,
|
|
);
|
|
},
|
|
async openWindow(url) {
|
|
return windowClientFacade(
|
|
await dependencies.host.clients.openWindow(url),
|
|
);
|
|
},
|
|
},
|
|
origin: dependencies.host.origin,
|
|
now: dependencies.now,
|
|
scheduler: dependencies.scheduler,
|
|
observer: dependencies.observer,
|
|
signal: lifecycle.signal,
|
|
});
|
|
|
|
const onPush = (event: unknown) => {
|
|
const facade = pushEventFacade(event);
|
|
if (facade) void push.handle(facade);
|
|
};
|
|
const onNotificationClick = (event: unknown) => {
|
|
const facade = notificationClickEventFacade(event);
|
|
if (facade) void click.handle(facade);
|
|
};
|
|
const onSubscriptionChange = (event: unknown) => {
|
|
const facade = functionalEventFacade(event);
|
|
if (!facade) return;
|
|
const taskControl = createLinkedAbortController(lifecycle.signal);
|
|
// WP-06. Bounded fan-out is policy, but the operator must be able to see
|
|
// that only part of the client set was notified.
|
|
let observedClientCount = 0;
|
|
let truncatedClients = false;
|
|
const processing = withAbortableDeadline(
|
|
async (signal) => {
|
|
let clients: readonly unknown[];
|
|
try {
|
|
clients = await dependencies.host.clients.matchAll({
|
|
type: "window",
|
|
includeUncontrolled: true,
|
|
});
|
|
} catch {
|
|
return nativeFailure("SUBSCRIPTION_RECONCILE", true);
|
|
}
|
|
if (signal.aborted) {
|
|
return webPushFailure("ABORTED", "SUBSCRIPTION_RECONCILE");
|
|
}
|
|
observedClientCount = clients.length;
|
|
truncatedClients =
|
|
clients.length > WEB_PUSH_LIMITS.clientHandoffCount;
|
|
try {
|
|
for (const candidate of clients.slice(
|
|
0,
|
|
WEB_PUSH_LIMITS.clientHandoffCount,
|
|
)) {
|
|
if (signal.aborted) {
|
|
return webPushFailure(
|
|
"ABORTED",
|
|
"SUBSCRIPTION_RECONCILE",
|
|
);
|
|
}
|
|
const client = windowClientFacade(candidate);
|
|
client?.postMessage(
|
|
Object.freeze({
|
|
protocol: WEB_PUSH_PROTOCOLS.reconcileRequired,
|
|
}),
|
|
);
|
|
}
|
|
} catch {
|
|
return nativeFailure("SUBSCRIPTION_RECONCILE", true);
|
|
}
|
|
return webPushSuccess(undefined);
|
|
},
|
|
{
|
|
deadlineMs: WEB_PUSH_LIMITS.handlerDeadlineMs,
|
|
operation: "SUBSCRIPTION_RECONCILE",
|
|
signal: taskControl.signal,
|
|
scheduler: dependencies.scheduler,
|
|
},
|
|
).finally(taskControl.dispose);
|
|
const lifetime = processing.then((result) => {
|
|
observeWebPush(dependencies.observer, {
|
|
event: "web_push_subscription_rotated",
|
|
outcome:
|
|
result.ok && !truncatedClients ? "SUCCEEDED" : "DEGRADED",
|
|
...(result.ok
|
|
? truncatedClients
|
|
? { reason: "LIMIT_EXCEEDED" as const }
|
|
: {}
|
|
: { reason: result.error.code }),
|
|
countBucket: webPushCountBucket(observedClientCount),
|
|
truncated: truncatedClients,
|
|
});
|
|
});
|
|
try {
|
|
facade.waitUntil(lifetime);
|
|
} catch {
|
|
taskControl.abort();
|
|
void lifetime;
|
|
}
|
|
};
|
|
|
|
dependencies.host.addEventListener("push", onPush);
|
|
dependencies.host.addEventListener(
|
|
"notificationclick",
|
|
onNotificationClick,
|
|
);
|
|
dependencies.host.addEventListener(
|
|
"pushsubscriptionchange",
|
|
onSubscriptionChange,
|
|
);
|
|
|
|
let disposed = false;
|
|
return Object.freeze({
|
|
dispose() {
|
|
if (disposed) return;
|
|
disposed = true;
|
|
lifecycle.abort();
|
|
dependencies.host.removeEventListener("push", onPush);
|
|
dependencies.host.removeEventListener(
|
|
"notificationclick",
|
|
onNotificationClick,
|
|
);
|
|
dependencies.host.removeEventListener(
|
|
"pushsubscriptionchange",
|
|
onSubscriptionChange,
|
|
);
|
|
dependencies.fenceStore.close();
|
|
},
|
|
});
|
|
}
|
|
|
|
function functionalEventFacade(
|
|
value: unknown,
|
|
): FunctionalEventFacade | null {
|
|
try {
|
|
return isRecord(value) && typeof value.waitUntil === "function"
|
|
? (value as FunctionalEventFacade)
|
|
: null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function pushEventFacade(value: unknown): PushEventFacade | null {
|
|
if (
|
|
!isRecord(value) ||
|
|
typeof value.waitUntil !== "function" ||
|
|
!(
|
|
value.data === null ||
|
|
(isRecord(value.data) && typeof value.data.arrayBuffer === "function")
|
|
)
|
|
) {
|
|
return null;
|
|
}
|
|
return value as PushEventFacade;
|
|
}
|
|
|
|
function notificationClickEventFacade(
|
|
value: unknown,
|
|
): NotificationClickEventFacade | null {
|
|
if (
|
|
!isRecord(value) ||
|
|
typeof value.waitUntil !== "function" ||
|
|
!isRecord(value.notification) ||
|
|
typeof value.notification.close !== "function" ||
|
|
!Object.hasOwn(value.notification, "data")
|
|
) {
|
|
return null;
|
|
}
|
|
return value as NotificationClickEventFacade;
|
|
}
|
|
|
|
function windowClientFacade(value: unknown): WindowClientFacade | null {
|
|
if (
|
|
!isRecord(value) ||
|
|
typeof value.url !== "string" ||
|
|
typeof value.focus !== "function" ||
|
|
typeof value.postMessage !== "function"
|
|
) {
|
|
return null;
|
|
}
|
|
return value as WindowClientFacade;
|
|
}
|