-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b18fc5
commit 535a5e6
Showing
11 changed files
with
354 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import {FetchEvent} from "../../../fetch"; | ||
import {isRouteMatchCondition, RouterRule} from "../../../worker/service-worker/router"; | ||
import {DurableServiceWorkerScope} from "../../../worker/service-worker/types"; | ||
import {URLPattern} from "urlpattern-polyfill"; | ||
|
||
export interface URLPatternInit { | ||
baseURL?: string; | ||
username?: string; | ||
password?: string; | ||
protocol?: string; | ||
hostname?: string; | ||
port?: string; | ||
pathname?: string; | ||
search?: string; | ||
hash?: string; | ||
} | ||
|
||
declare var self: DurableServiceWorkerScope; | ||
|
||
export type RouterRequestMethodLower = "get" | "put" | "post" | "delete" | "options" | "patch" | ||
|
||
export interface OnRequestFn { | ||
(request: Request, event: FetchEvent): Response | undefined | void | Promise<Response | undefined | void> | ||
} | ||
|
||
export interface AddRouteAndHandlerFn { | ||
(pathnameOrInit: string | URLPatternInit, onRequest: OnRequestFn): void | ||
} | ||
|
||
export const requestMethod = makeRequestMethod(); | ||
|
||
function makeRequestMethod(): Record<RouterRequestMethodLower, AddRouteAndHandlerFn> { | ||
return { | ||
get: makeAddRequestMethodRouteAndHandler("get"), | ||
patch: makeAddRequestMethodRouteAndHandler("patch"), | ||
put: makeAddRequestMethodRouteAndHandler("put"), | ||
post: makeAddRequestMethodRouteAndHandler("post"), | ||
delete: makeAddRequestMethodRouteAndHandler("delete"), | ||
options: makeAddRequestMethodRouteAndHandler("options") | ||
} | ||
} | ||
|
||
function makeAddRequestMethodRouteAndHandler(requestMethod: RouterRequestMethodLower): AddRouteAndHandlerFn { | ||
return function addRequestRouteAndHandler( | ||
pathnameOrInit: string | URLPatternInit, | ||
onRequest: OnRequestFn) { | ||
return addRequestMethodRouteAndHandler( | ||
requestMethod, | ||
pathnameOrInit, | ||
onRequest | ||
) | ||
} | ||
} | ||
|
||
function addRequestMethodRouteAndHandler( | ||
requestMethod: string, | ||
pathnameOrInit: string | URLPatternInit, | ||
onRequest: OnRequestFn | ||
) { | ||
const urlPattern = new URLPattern( | ||
typeof pathnameOrInit === "string" ? { | ||
pathname: pathnameOrInit | ||
} : pathnameOrInit | ||
); | ||
const rule: RouterRule = { | ||
condition: [ | ||
{ | ||
requestMethod | ||
}, | ||
{ | ||
urlPattern | ||
} | ||
], | ||
source: "fetch-event" | ||
} | ||
self.addEventListener("install", event => { | ||
event.addRoutes(rule) | ||
}); | ||
self.addEventListener("fetch", event => { | ||
if (isRouteMatchCondition(self.registration, rule, event.request)) { | ||
event.waitUntil(intercept()); | ||
} | ||
|
||
async function intercept() { | ||
const response = await onRequest(event.request, event); | ||
if (response) { | ||
event.respondWith(response); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {serviceWorker} from "../../../worker"; | ||
import {createRouter} from "../../../worker/service-worker/router"; | ||
import {ok} from "../../../is"; | ||
import {fileURLToPath} from "node:url"; | ||
import {dirname, join} from "node:path"; | ||
import {v4} from "uuid"; | ||
import {waitForServiceWorker} from "./wait"; | ||
|
||
export {}; | ||
|
||
const pathname = fileURLToPath(import.meta.url); | ||
const worker = join(dirname(pathname), "./routes.worker.js"); | ||
|
||
{ | ||
const registration = await serviceWorker.register(worker); | ||
|
||
const fetch = await createRouter([ | ||
registration | ||
]); | ||
|
||
// wait for activated before starting to route | ||
// note we can create the router earlier than installing, though register needs to be done ahead of time | ||
await waitForServiceWorker(registration); | ||
|
||
{ | ||
const response = await fetch("/test"); | ||
|
||
console.log(response.status); | ||
ok(response.ok); | ||
|
||
console.log(await response.text()); | ||
} | ||
|
||
{ | ||
const body = `SOMETHING RANDOM: ${v4()}` | ||
|
||
const response = await fetch("/test", { | ||
method: "PUT", | ||
body | ||
}); | ||
|
||
console.log(response.status); | ||
ok(response.ok); | ||
|
||
const text = await response.text(); | ||
|
||
console.log(text); | ||
ok(text === body, "Expected returned body to match"); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {requestMethod} from "./routes.example"; | ||
|
||
requestMethod.get("/test", () => { | ||
console.log("In get handler"); | ||
return new Response("Hello from test get handler"); | ||
}) | ||
|
||
requestMethod.put("/test", (request) => { | ||
console.log("In put handler"); | ||
return new Response(request.body, { | ||
headers: request.headers | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {DurableServiceWorkerRegistration} from "../../../worker"; | ||
import {dispatchEvent} from "../../../events"; | ||
|
||
export async function waitForServiceWorker(registration: DurableServiceWorkerRegistration) { | ||
if (registration.active) { | ||
return registration.active; | ||
} | ||
// Send a push event to the worker to ensure it is activated | ||
await dispatchEvent({ | ||
type: "push", | ||
serviceWorkerId: registration.durable.serviceWorkerId, | ||
schedule: { | ||
immediate: true | ||
} | ||
}); | ||
await registration.update(); | ||
return waitForServiceWorker(registration); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.