Skip to content

Commit

Permalink
Update readme example
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiancook committed Nov 17, 2023
1 parent b586ef1 commit cbe18d2
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 530 deletions.
535 changes: 19 additions & 516 deletions README.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
"./main": "./esnext/main.js",
"./start": "./esnext/start.js"
},
"imports": {
"@virtualstate/internal": "./esnext/index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/virtualstate/internal.git"
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export * from "./fetch";
export * from "./sync";
export * from "./content-index";
export * from "./periodic-sync";
export * from "./worker";
// export * from "./start";
12 changes: 6 additions & 6 deletions src/tests/worker/service-worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import {
executeServiceWorkerFetch,
registerServiceWorkerFetch
} from "../../../worker/service-worker/execute-fetch";
import {fileURLToPath} from "node:url";

export {};

const instance = new URL(import.meta.url);
const { pathname } = instance;
const directory = dirname(pathname);
instance.pathname = join(directory, "./worker.js");
const worker = instance.toString();
const pathname = fileURLToPath(import.meta.url);
const worker = join(dirname(pathname), "./worker.js");

async function waitForServiceWorker(registration: DurableServiceWorkerRegistration) {
if (registration.active) {
Expand Down Expand Up @@ -78,4 +76,6 @@ async function waitForServiceWorker(registration: DurableServiceWorkerRegistrati
}


}
}

await import("./readme/main.js");
3 changes: 3 additions & 0 deletions src/tests/worker/service-worker/readme/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
self.addEventListener("fetch", event => {
event.respondWith(new Response("Hello"))
});
15 changes: 15 additions & 0 deletions src/tests/worker/service-worker/readme/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { serviceWorker, createServiceWorkerFetch } from "@virtualstate/internal";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";

const pathname = fileURLToPath(import.meta.url);
const worker = join(dirname(pathname), "./hello.js");

const registration = await serviceWorker.register(worker);

const fetch = createServiceWorkerFetch(registration);

const response = await fetch("/");
const text = await response.text();

console.log(response.status, text); // 200 "Hello";
1 change: 1 addition & 0 deletions src/worker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./service-worker";
3 changes: 2 additions & 1 deletion src/worker/service-worker/execute-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {executeServiceWorkerWorker} from "./execute";
import {isLike, ok} from "../../is";
import type {FetchResponseMessage} from "./dispatch";
import {DurableServiceWorkerRegistration, serviceWorker} from "./container";
import {getOrigin} from "../../listen/config";

export async function registerServiceWorkerFetch(worker: string, options?: RegistrationOptions) {
const registration = await serviceWorker.register(worker, options);
Expand All @@ -19,7 +20,7 @@ export function createServiceWorkerFetch(registration: DurableServiceWorkerRegis
request = new Request(input, init);
} else {
request = {
url: input,
url: new URL(input, getOrigin()).toString(),
method: init?.method,
headers: getFetchHeadersObject(
new Headers(init?.headers)
Expand Down
7 changes: 0 additions & 7 deletions src/worker/service-worker/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ export function getServiceWorkerWorkerWorker(options?: WorkerOptions) {
});
}

export function assertPushAsyncIterableIterator<T>(iterator: AsyncIterableIterator<T>): asserts iterator is PushAsyncIterableIterator<T> {
ok(
isLike<PushAsyncIterableIterator<T>>(iterator) &&
typeof iterator.clone === "function"
)
}

export async function executeServiceWorkerWorkerMessage(data?: ServiceWorkerWorkerData): Promise<unknown> {
const iterator = executeServiceWorkerWorker(data)[Symbol.asyncIterator]();
const next = await iterator.next();
Expand Down
3 changes: 3 additions & 0 deletions src/worker/service-worker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./container";
export * from "./execute-fetch";
export * from "./execute";

0 comments on commit cbe18d2

Please sign in to comment.