From 5904326f0cfea724a790ce6169b5ae2a06dfe95d Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sun, 25 Feb 2024 13:46:02 +0100 Subject: [PATCH] Minor bug fix in service worker --- common/proxy_fetch.ts | 2 +- web/service_worker.ts | 12 +++++------- web/syscalls/fetch.ts | 4 ++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/common/proxy_fetch.ts b/common/proxy_fetch.ts index 7edc0053..f9a6d6a1 100644 --- a/common/proxy_fetch.ts +++ b/common/proxy_fetch.ts @@ -1,4 +1,4 @@ -import { base64Decode, base64Encode } from "../lib/crypto.ts"; +import { base64Decode, base64Encode } from "$lib/crypto.ts"; export type ProxyFetchRequest = { method?: string; diff --git a/web/service_worker.ts b/web/service_worker.ts index b107619b..3d305298 100644 --- a/web/service_worker.ts +++ b/web/service_worker.ts @@ -71,8 +71,12 @@ self.addEventListener("fetch", (event: any) => { event.respondWith( (async () => { const request = event.request; + const requestUrl = new URL(request.url); - // console.log("Getting request", request, [...request.headers.entries()]); + // Are we fetching a URL from the same origin as the app? If not, we don't handle it and pass it on + if (location.host !== requestUrl.host) { + return fetch(request); + } // Any request with the X-Sync-Mode header originates from the sync engine: pass it on to the server if (request.headers.has("x-sync-mode")) { @@ -91,14 +95,8 @@ self.addEventListener("fetch", (event: any) => { return fetch(request); } - const requestUrl = new URL(request.url); const pathname = requestUrl.pathname; - // Are we fetching a URL from the same origin as the app? If not, we don't handle it and pass it on - if (location.host !== requestUrl.host) { - return fetch(request); - } - if (pathname === "/.auth" || pathname === "/index.json") { return fetch(request); } else if (/\/.+\.[a-zA-Z]+$/.test(pathname)) { diff --git a/web/syscalls/fetch.ts b/web/syscalls/fetch.ts index 8a99369e..7b8b7ed0 100644 --- a/web/syscalls/fetch.ts +++ b/web/syscalls/fetch.ts @@ -1,11 +1,11 @@ -import type { SysCallMapping } from "../../lib/plugos/system.ts"; +import type { SysCallMapping } from "$lib/plugos/system.ts"; import { performLocalFetch, ProxyFetchRequest, ProxyFetchResponse, } from "$common/proxy_fetch.ts"; import type { Client } from "../client.ts"; -import { base64Decode, base64Encode } from "../../lib/crypto.ts"; +import { base64Decode, base64Encode } from "$lib/crypto.ts"; export function sandboxFetchSyscalls( client: Client,