Skip to content

Commit

Permalink
createRouter test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiancook committed Dec 2, 2023
1 parent 63e8cae commit ed9d5b0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 34 deletions.
4 changes: 1 addition & 3 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import type {WorkerPoolConfig} from "../worker/pool";
import type {PeriodicSyncScheduleConfig} from "../periodic-sync/schedule";
import type {EventScheduleConfig} from "../events/schedule/update";
import type {DurableStorageBucketConfig} from "../storage-buckets/manager";
import type {ServiceWorkerRouterConfig} from "../worker/service-worker/router";

export interface LogisticsConfig {

Expand All @@ -42,8 +41,7 @@ export interface Config extends
FastifyConfig,
WorkerPoolConfig,
PeriodicSyncScheduleConfig,
EventScheduleConfig,
ServiceWorkerRouterConfig {
EventScheduleConfig {
name: string;
version: string;
root: string;
Expand Down
19 changes: 19 additions & 0 deletions src/tests/worker/service-worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
registerServiceWorkerFetch
} from "../../../worker/service-worker/execute-fetch";
import {fileURLToPath} from "node:url";
import {createRouter, listRoutes} from "../../../worker/service-worker/router";

export {};

Expand Down Expand Up @@ -76,4 +77,22 @@ async function waitForServiceWorker(registration: DurableServiceWorkerRegistrati
}


}

{
const registration = await serviceWorker.register(worker);

const routes = await listRoutes(registration.durable.serviceWorkerId);

ok(routes.length);

const fetch = await createRouter([
registration
]);

const response = await fetch("https://example.com");

console.log(response.status, routes);
ok(response.ok);

}
36 changes: 9 additions & 27 deletions src/worker/service-worker/default-worker.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import "./dispatchers";
import {Promise, Push} from "@virtualstate/promise";
import {WORKER_ADD_CONTEXT, WORKER_BREAK, WORKER_INITIATED, WORKER_TERMINATE} from "./constants";
import {Push} from "@virtualstate/promise";
import {WORKER_BREAK, WORKER_INITIATED, WORKER_TERMINATE} from "./constants";
import {parentPort, workerData} from "node:worker_threads";
import {onServiceWorkerWorkerData, ServiceWorkerWorkerData} from "./worker";
import { ok } from "../../is";
import {dispatchWorkerEvent} from "./dispatch";
import {getConfig, withConfig} from "../../config";
import {dispatchEvent} from "../../events";
import {DurableEventData} from "../../data";
import {DurableServiceWorkerRegistration} from "./container";
import {AddRoutesOptions} from "./router";

console.log("Default worker!");

Expand Down Expand Up @@ -67,32 +63,18 @@ try {

workerData.postMessage(WORKER_TERMINATE);

async function addServiceWorkerRoutes(rules: AddRoutesOptions) {
const array = Array.isArray(rules) ? rules : [rules];
}

async function onServiceWorkerMessage(message: ServiceWorkerWorkerData) {
const { serviceWorkerId } = message;
ok(serviceWorkerId);

await withConfig({
addServiceWorkerRoutes,
dispatchEvent(event: DurableEventData) {
return dispatchEvent({
...event,
serviceWorkerId
})
if (!registration) {
registration = await onServiceWorkerWorkerData(message);
} else {
ok(message.serviceWorkerId === registration.durable.serviceWorkerId);
if (message.event) {
await dispatchWorkerEvent(message.event, message);
}
}, async () => {
if (!registration) {
registration = await onServiceWorkerWorkerData(message);
} else {
ok(message.serviceWorkerId === registration.durable.serviceWorkerId);
if (message.event) {
await dispatchWorkerEvent(message.event, message);
}
}
})
}
}

} catch (error) {
Expand Down
8 changes: 4 additions & 4 deletions src/worker/service-worker/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ export function listRoutes(serviceWorkerId = getServiceWorkerId()) {
return store.values();
}

export async function createRouter(): Promise<typeof fetch> {
const serviceWorkers = await listServiceWorkers();
export async function createRouter(serviceWorkers?: DurableServiceWorkerRegistration[]): Promise<typeof fetch> {
const resolveServiceWorkers = serviceWorkers ?? await listServiceWorkers();
const routes = Object.fromEntries(
await Promise.all(
serviceWorkers.map(
resolveServiceWorkers.map(
async ({ durable: { serviceWorkerId }}) => {
return [
serviceWorkerId,
Expand All @@ -134,7 +134,7 @@ export async function createRouter(): Promise<typeof fetch> {
);

const fetchers = Object.fromEntries(
serviceWorkers.map(
resolveServiceWorkers.map(
serviceWorker => [
serviceWorker.durable.serviceWorkerId,
createServiceWorkerFetch(serviceWorker)
Expand Down

0 comments on commit ed9d5b0

Please sign in to comment.