diff --git a/package.json b/package.json index 1cd882e..0dd1e1f 100644 --- a/package.json +++ b/package.json @@ -17,10 +17,7 @@ "check": "biome check . --write", "typecheck": "tsc" }, - "files": [ - "dist", - "lib" - ], + "files": ["dist", "lib"], "keywords": [], "author": "z1g Project", "license": "MIT", diff --git a/server.ts b/server.ts index ff5d56c..abfe1d8 100644 --- a/server.ts +++ b/server.ts @@ -1,18 +1,18 @@ -import Fastify from 'fastify' import fastifyStatic from '@fastify/static' import { server as wisp } from '@mercuryworkshop/wisp-js' +import Fastify from 'fastify' import { createServer } from 'node:http' import type { Socket } from 'node:net' import { argv } from 'node:process' import { fileURLToPath } from 'node:url' -import { context } from 'esbuild' -import { rimraf } from 'rimraf' import { baremuxPath } from '@mercuryworkshop/bare-mux/node' // @ts-expect-error import { epoxyPath } from '@mercuryworkshop/epoxy-transport' import { consola } from 'consola' +import { context } from 'esbuild' +import { rimraf } from 'rimraf' const port = Number(process.env.PORT) || 9000 diff --git a/src/client/apis/history.ts b/src/client/apis/history.ts new file mode 100644 index 0000000..89e9a5f --- /dev/null +++ b/src/client/apis/history.ts @@ -0,0 +1,21 @@ +import { patchFunction } from '../patch' +import { rewriteStringOrUrl } from '../rewrite' + +window.history.replaceState = patchFunction( + window.history.replaceState, + ([state, , url]) => { + if (url) { + url = rewriteStringOrUrl(url) + } + return [state, '', url] + } +) +window.history.pushState = patchFunction( + window.history.pushState, + ([state, , url]) => { + if (url) { + url = rewriteStringOrUrl(url) + } + return [state, '', url] + } +) diff --git a/src/client/apis/location.ts b/src/client/apis/location.ts index f6d66ad..bd811a6 100644 --- a/src/client/apis/location.ts +++ b/src/client/apis/location.ts @@ -1,8 +1,8 @@ window.$location = Object.create(window.location) Object.defineProperties(window.$location, { toString: { - get() { - return () => self.$meteor.util.createOrigin().toString() + value() { + return self.$meteor.util.createOrigin().toString() } }, href: { diff --git a/src/client/apis/navigator.ts b/src/client/apis/navigator.ts index 8b29bb9..bf91270 100644 --- a/src/client/apis/navigator.ts +++ b/src/client/apis/navigator.ts @@ -3,24 +3,12 @@ import { rewriteStringOrUrl } from '../rewrite' Object.defineProperties(window.navigator, {}) -if ('serviceWorker' in globalThis.navigator) { - globalThis.navigator.serviceWorker.register = patchFunction( - globalThis.navigator.serviceWorker.register, - (args) => { - self.$meteor.util.log(`Attempting to register SW: /route/${self.$meteor.codecs.xor.encode(`${self.$location.origin}/${args[0]}`)}`, 'green') - args[0] = `/route/${self.$meteor.codecs.xor.encode(`${self.$location.origin}/${args[0]}`)}` - return args - } - ) -} - if ('sendBeacon' in globalThis.navigator) { - globalThis.navigator.sendBeacon = patchFunction( - globalThis.navigator.sendBeacon, - (args) => { - args[0] = rewriteStringOrUrl(args[0]) - args[1] = args[1] - return args - } - ) + globalThis.navigator.sendBeacon = patchFunction( + globalThis.navigator.sendBeacon, + (args) => { + args[0] = rewriteStringOrUrl(args[0]) + return args + } + ) } diff --git a/src/client/apis/ws.ts b/src/client/apis/ws.ts index a6033b3..451f2e8 100644 --- a/src/client/apis/ws.ts +++ b/src/client/apis/ws.ts @@ -15,7 +15,6 @@ globalThis.WebSocket = new Proxy(globalThis.WebSocket, { 'User-Agent': navigator.userAgent, origin: self.$meteor.util.createOrigin().origin }, - // @ts-expect-error ArrayBuffer.prototype ) } diff --git a/src/client/dom.ts b/src/client/dom.ts index 1a0c4c1..a7d5363 100644 --- a/src/client/dom.ts +++ b/src/client/dom.ts @@ -1,12 +1,22 @@ -const attributes: Record HTMLElement)[]> = - { - src: [HTMLScriptElement, HTMLVideoElement, HTMLImageElement], - href: [HTMLAnchorElement, HTMLLinkElement], - action: [HTMLFormElement], - formaction: [HTMLInputElement] - } +const urlAttributes: Record< + string, + (new ( + ...args: unknown[] + ) => HTMLElement)[] +> = { + src: [ + HTMLScriptElement, + HTMLVideoElement, + HTMLImageElement, + HTMLAudioElement, + HTMLIFrameElement + ], + href: [HTMLAnchorElement, HTMLLinkElement], + action: [HTMLFormElement], + formaction: [HTMLInputElement] +} -for (const [attr, elms] of Object.entries(attributes)) { +for (const [attr, elms] of Object.entries(urlAttributes)) { for (const elm of elms) { const descriptors = Object.getOwnPropertyDescriptor(elm.prototype, attr) Object.defineProperty(elm.prototype, attr, { diff --git a/src/client/index.ts b/src/client/index.ts index 15138cb..d6da84c 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -6,6 +6,7 @@ import './dom' import './apis/location' import './apis/requests' import './apis/workers' +import './apis/history' import './apis/ws' import './apis/navigator' import './apis/storage' diff --git a/src/worker.ts b/src/worker.ts index 9469db2..963e3b8 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -41,7 +41,9 @@ class MeteorServiceWorker { ...request.headers, 'content-type': request.headers.has('content-type') ? request.headers.get('content-type') - : 'text/plain', + : url.pathname.endsWith('.js') + ? 'application/javascript' + : 'text/plain', cookie: (await getCookies(url.host)).join('') }, credentials: 'omit',