Public: anyone can read this; the crew can change it.
/tab starts in Safari and Chrome: the worker sees its host page, and the page waits for activation #17998
Merged 15h 50m ago, live in production
sold/tab-start → main 0 commits 2 threads · 2 unresolved
4 files
changed +69
−6
src/tab/active.ts added +24
| @@ -0,0 +1,24 @@ | ||
| 1 | /** The registration's worker, once it is active: the point the frame may load a page it serves. */ | |
| 2 | export interface Worker1 { | |
| 3 | state: string; | |
| 4 | addEventListener(type: "statechange", listener: () => void): void; | |
| 5 | } | |
| 6 | export interface Registration { | |
| 7 | readonly installing: Worker1 | null; | |
| 8 | readonly waiting: Worker1 | null; | |
| 9 | readonly active: Worker1 | null; | |
| 10 | } | |
| 11 | ||
| 12 | export function activated(registration: Registration): Promise<void> { | |
| 13 | const worker = registration.installing ?? registration.waiting ?? registration.active; | |
| 14 | if (worker === null) return Promise.reject(new Error("no worker registered")); | |
| 15 | return new Promise((resolve, reject) => { | |
| 16 | const check = (): boolean => { | |
| 17 | if (worker.state === "activated") resolve(); | |
| 18 | else if (worker.state === "redundant") reject(new Error("worker redundant")); | |
| 19 | else return false; | |
| 20 | return true; | |
| 21 | }; | |
| 22 | if (!check()) worker.addEventListener("statechange", check); | |
| 23 | }); | |
| 24 | } |
src/tab/boot.ts +5 −4
| @@ -3,6 +3,7 @@ | ||
| 3 | 3 | * forge in a frame. The page is the host: its requests from the worker are relayed from the |
| 4 | 4 | * service worker, so closing the tab stops the forge. |
| 5 | 5 | */ |
| 6 | import { activated, type Registration } from "./active.ts"; | |
| 6 | 7 | import type { TabRequest } from "./forge.ts"; |
| 7 | 8 | |
| 8 | 9 | // The few browser globals this file touches, named here rather than pulling in the DOM library. |
| @@ -17,8 +18,7 @@ interface Box { | ||
| 17 | 18 | interface Registrations { |
| 18 | 19 | addEventListener(type: "message", listener: (e: MessageEvent<{ forge?: TabRequest }>) => void): void; |
| 19 | 20 | startMessages(): void; |
| 20 | register(script: string, options: { scope: string }): Promise<unknown>; | |
| 21 | readonly ready: Promise<unknown>; | |
| 21 | register(script: string, options: { scope: string }): Promise<Registration>; | |
| 22 | 22 | } |
| 23 | 23 | declare const document: { getElementById(id: string): Box | null; readonly documentElement: { readonly dataset: Record<string, string> } }; |
| 24 | 24 | declare const navigator: { readonly serviceWorker?: Registrations; readonly storage?: { getDirectory?: unknown } }; |
| @@ -63,8 +63,9 @@ async function start(button: Box): Promise<void> { | ||
| 63 | 63 | } |
| 64 | 64 | holder = worker; |
| 65 | 65 | try { |
| 66 | await workers.register(button.dataset.sw!, { scope: "/tab/f/" }); | |
| 67 | await workers.ready; | |
| 66 | // Not `workers.ready`: it waits for a worker whose scope covers this page (/tab), and the scope | |
| 67 | // is /tab/f/, so it never settles. | |
| 68 | await activated(await workers.register(button.dataset.sw!, { scope: "/tab/f/" })); | |
| 68 | 69 | } catch (error) { |
| 69 | 70 | console.warn("tab forge: service worker", error); |
| 70 | 71 | holder = null; |
src/tab/sw.ts +4 −2
| @@ -11,7 +11,7 @@ interface Client { | ||
| 11 | 11 | } |
| 12 | 12 | interface Scope { |
| 13 | 13 | readonly location: { readonly origin: string }; |
| 14 | readonly clients: { matchAll(options: { type: "window" }): Promise<Client[]>; claim(): Promise<void> }; | |
| 14 | readonly clients: { matchAll(options: { type: "window"; includeUncontrolled: boolean }): Promise<Client[]>; claim(): Promise<void> }; | |
| 15 | 15 | skipWaiting(): Promise<void>; |
| 16 | 16 | addEventListener(type: "install", listener: () => void): void; |
| 17 | 17 | addEventListener(type: "activate", listener: (e: { waitUntil(p: Promise<unknown>): void }) => void): void; |
| @@ -64,7 +64,9 @@ async function answer(request: Request): Promise<Response> { | ||
| 64 | 64 | if (text === null) return reply({ status: 413, html: "Too large." }); |
| 65 | 65 | form = Object.fromEntries(new URLSearchParams(text)); |
| 66 | 66 | } |
| 67 | const hosts = (await sw.clients.matchAll({ type: "window" })).filter((c) => new URL(c.url).pathname === "/tab"); | |
| 67 | // The /tab page lies outside this worker's scope (/tab/f/), so it is never one of the clients the | |
| 68 | // worker controls: without includeUncontrolled the host is invisible and the forge reads as stopped. | |
| 69 | const hosts = (await sw.clients.matchAll({ type: "window", includeUncontrolled: true })).filter((c) => new URL(c.url).pathname === "/tab"); | |
| 68 | 70 | if (hosts.length === 0) return stopped(); |
| 69 | 71 | const path = url.pathname.slice(PREFIX.length) || "/"; |
| 70 | 72 | // Ask every /tab window: only the one that started the forge answers, so the first reply is the |
test/tab-host-visible.test.ts added +36
| @@ -0,0 +1,36 @@ | ||
| 1 | /** The tab forge's service worker sees the uncontrolled /tab page, and the page's wait ends. */ | |
| 2 | import { expect, test } from "bun:test"; | |
| 3 | import { activated } from "../src/tab/active.ts"; | |
| 4 | ||
| 5 | test("the service worker asks windows it does not control, so the /tab host is found", async () => { | |
| 6 | let listener: ((e: { request: Request; respondWith(r: Promise<Response>): void }) => void) | undefined; | |
| 7 | const host = { | |
| 8 | url: "https://forge.test/tab", | |
| 9 | postMessage(_m: unknown, [port]: any[]) { | |
| 10 | port.postMessage({ status: 200, html: "ok" }); | |
| 11 | }, | |
| 12 | }; | |
| 13 | (globalThis as any).self = { | |
| 14 | location: { origin: "https://forge.test" }, | |
| 15 | clients: { matchAll: async (o: { includeUncontrolled?: boolean }) => (o.includeUncontrolled === true ? [host] : []), claim: async () => {} }, | |
| 16 | skipWaiting: async () => {}, | |
| 17 | addEventListener(type: string, l: any) { | |
| 18 | if (type === "fetch") listener = l; | |
| 19 | }, | |
| 20 | }; | |
| 21 | await import("../src/tab/sw.ts"); | |
| 22 | let out: Promise<Response> | undefined; | |
| 23 | listener!({ request: new Request("https://forge.test/tab/f/"), respondWith: (r) => (out = r) }); | |
| 24 | const res = await out!; | |
| 25 | expect(res.status).toBe(200); | |
| 26 | }); | |
| 27 | ||
| 28 | test("activated settles when the worker activates, without a controlled page", async () => { | |
| 29 | let fire = (): void => {}; | |
| 30 | const worker = { state: "installing", addEventListener: (_t: "statechange", l: () => void) => (fire = l) }; | |
| 31 | const done = activated({ installing: worker, waiting: null, active: null }); | |
| 32 | worker.state = "activated"; | |
| 33 | fire(); | |
| 34 | await done; | |
| 35 | await expect(activated({ installing: null, waiting: null, active: { state: "activated", addEventListener() {} } })).resolves.toBeUndefined(); | |
| 36 | }); |
new above
offline
Worker1is a vague name. PreferActivatingWorkeror similar.