Skip to content

Commit

Permalink
Add example tagged route, WICG/service-worker-static-routing-api#17
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiancook committed Dec 8, 2023
1 parent 2fd97b9 commit 73e3499
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/fetch/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface DurableFetchEventData extends DurableEventData {
export interface FetchEvent extends Omit<DurableFetchEventData, "request">, FetchRespondWith {
request: Request;
waitUntil(promise: Promise<void | unknown>): void;
tag?: string;
}

function isFetchEvent(event: unknown): event is FetchEvent {
Expand Down
8 changes: 5 additions & 3 deletions src/tests/worker/service-worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ self.addEventListener("install", event => {
]
},
source: [
"network",
"fetch-event"
{
type: "fetch-event",
tag: "fetch-any"
}
]
}
])
Expand All @@ -34,7 +36,7 @@ self.addEventListener("activate", event => {
})

self.addEventListener("fetch", event => {
console.log("In fetch handler!");
console.log("In fetch handler!", event.tag || "untagged");
event.respondWith(onFetchEvent(event));
});

Expand Down
32 changes: 9 additions & 23 deletions src/worker/service-worker/execute-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import type {FetchResponseMessage} from "./dispatch";
import {DurableServiceWorkerRegistration, serviceWorker} from "./container";
import {getOrigin} from "../../listen/config";

export interface FetchInit extends RequestInit {
export interface ServiceWorkerFetchOptions {
tag?: string;
}

export interface FetchInit extends RequestInit, ServiceWorkerFetchOptions {
duplex?: "half";
}

Expand Down Expand Up @@ -42,18 +46,20 @@ export function createServiceWorkerFetch(registration: DurableServiceWorkerRegis
}
return executeServiceWorkerFetch(
registration,
request
request,
init
);
}
}

export async function executeServiceWorkerFetch(registration: DurableServiceWorkerRegistration, request: Request | DurableRequestData) {
export async function executeServiceWorkerFetch(registration: DurableServiceWorkerRegistration, request: Request | DurableRequestData, options?: ServiceWorkerFetchOptions) {
return executeServiceWorkerFetchEvent(registration, {
type: "fetch",
request: request instanceof Request ?
await fromRequest(request) :
request,
virtual: true,
tag: options?.tag
});
}

Expand All @@ -79,26 +85,6 @@ export async function executeServiceWorkerFetchEvent(registration: DurableServic
return fromDurableResponse(message.response)
}

function createBody(): undefined | BodyInit {
const stream = new ReadableStream({
async pull(controller) {
const message = await next();
if (!message) {
return controller.close();
}
if (message.data) {
controller.enqueue(message.data);
}
},
async cancel() {
await iterator.return?.();
}
});
ok<BodyInit>(stream);
return stream;
}


async function next() {
const { value, done } = await iterator.next();
if (done) {
Expand Down
31 changes: 24 additions & 7 deletions src/worker/service-worker/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,27 @@ export interface RouterSource {
behaviorEnum?: RouterSourceBehaviorEnum;
}

export interface RouterRaceNetworkAndFetchHandlerSource extends RouterSource {
type: "race-network-and-fetch-handler";
tag?: string;
}

export interface RouterNetworkSource extends RouterSource {
type: "network";
updatedCacheName?: string;
cacheErrorResponse?: boolean
cacheErrorResponse?: boolean;
}

export interface RouterCacheSource extends RouterSource {
type: "cache";
cacheName?: string;
request?: RequestInfo;
}

export interface RouterFetchEventSource extends RouterSource {
type: "fetch-event";
id?: string;
tag?: string;
}

export interface RouterURLPatternCondition {
Expand Down Expand Up @@ -98,6 +107,7 @@ export type RouterRuleSource =
| RouterSource
| RouterCacheSource
| RouterFetchEventSource
| RouterRaceNetworkAndFetchHandlerSource
| RouterSourceEnum;

export interface RouterRule {
Expand Down Expand Up @@ -158,14 +168,14 @@ export function isRouterNotCondition(condition: RouterCondition): condition is R
return "not" in condition && Boolean(condition.not);
}

export function isRouterSourceObject(source: RouterRuleSource): source is RouterNetworkSource {
export function isRouterSourceObject(source: RouterRuleSource): source is RouterSource {
return (
typeof source === "object" &&
"type" in source
);
}

export function isRouterSourceType(source: RouterRuleSource, type: RouterSourceEnum) {
export function isRouterSourceType<T extends RouterSourceEnum>(source: RouterRuleSource, type: T): source is RouterRuleSource & { type: T } | T {
return (
source === type ||
(
Expand Down Expand Up @@ -497,11 +507,18 @@ export async function createRouter(serviceWorkers?: DurableServiceWorkerRegistra
}
}

async function serviceWorkerFetch() {
async function serviceWorkerFetch(source: RouterSourceEnum | RouterFetchEventSource | RouterRaceNetworkAndFetchHandlerSource) {
// TODO check if any fetch event handlers added.
const fetch = fetchers.get(serviceWorker);
ok(fetch, "Expected to find fetcher for service worker, internal state corrupt");
return fetch(clone(), init);
let resolvedInit = init;
if (typeof source === "object" && source.tag) {
resolvedInit = {
...init,
tag: source.tag
}
}
return fetch(clone(), resolvedInit);
}

async function source(ruleSource: RouterRuleSource): Promise<Response | undefined> {
Expand All @@ -518,12 +535,12 @@ export async function createRouter(serviceWorkers?: DurableServiceWorkerRegistra
return response;
}
if (isRouterSourceType(ruleSource, "fetch-event")) {
return serviceWorkerFetch();
return serviceWorkerFetch(ruleSource);
}
if (isRouterSourceType(ruleSource, "race-network-and-fetch-handler")) {
return Promise.race([
source("network"),
serviceWorkerFetch()
serviceWorkerFetch(ruleSource)
]);
}
if (isRouterSourceType(ruleSource, "cache")) {
Expand Down
2 changes: 1 addition & 1 deletion src/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function getDefaultNodeWorker(url: string, options: WorkerOptions): Promis
worker.stderr.pipe(process.stderr);
}
return worker;
};
}

export function getWorkerURLForImportURL(relative: string, url: string | URL) {
const instance = new URL(url, getOrigin());
Expand Down

0 comments on commit 73e3499

Please sign in to comment.