Skip to content

Commit

Permalink
discord works a bit more now, patch history and remove useless sw patch
Browse files Browse the repository at this point in the history
  • Loading branch information
IncognitoTGT committed Jul 13, 2024
1 parent b5399d2 commit 3569caa
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 38 deletions.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
"check": "biome check . --write",
"typecheck": "tsc"
},
"files": [
"dist",
"lib"
],
"files": ["dist", "lib"],
"keywords": [],
"author": "z1g Project",
"license": "MIT",
Expand Down
6 changes: 3 additions & 3 deletions server.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down
21 changes: 21 additions & 0 deletions src/client/apis/history.ts
Original file line number Diff line number Diff line change
@@ -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]
}
)
4 changes: 2 additions & 2 deletions src/client/apis/location.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
26 changes: 7 additions & 19 deletions src/client/apis/navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)
}
1 change: 0 additions & 1 deletion src/client/apis/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
26 changes: 18 additions & 8 deletions src/client/dom.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
const attributes: Record<string, (new (...args: unknown[]) => 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, {
Expand Down
1 change: 1 addition & 0 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 3 additions & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 3569caa

Please sign in to comment.