-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
41 lines (37 loc) · 1.31 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// deno-lint-ignore-file no-explicit-any no-explicit-any
// @ts-ignore: Deno doesn't know about WorkerLocation
class WorkerLocationPolyfill implements WorkerLocation {
#url: URL;
constructor(href: string) { this.#url = new URL(href) }
get hash(): string { return '' }
get host(): string { return this.#url.host }
get hostname(): string { return this.#url.hostname }
get href(): string { return this.#url.href }
get origin(): string { return this.#url.origin }
get pathname(): string { return '/' }
get port(): string { return this.#url.port }
get protocol(): string { return this.#url.protocol }
get search(): string { return '' }
toString(): string { return this.href }
}
function defineProperty(url: string, writable = false) {
Object.defineProperty(self, 'location', {
configurable: false,
enumerable: true,
writable,
value: new WorkerLocationPolyfill(url),
});
}
function polyfillLocation(event: Event): void {
// @ts-ignore: Deno doesn't know about FetchEvent
const _event = event as FetchEvent;
defineProperty(_event.request.url, true);
}
if (!('location' in self)) {
const envLoc = ((<any>self).WORKER_LOCATION) ?? ((<any>self).process?.env?.WORKER_LOCATION)
if (envLoc) {
defineProperty(envLoc);
} else {
self.addEventListener('fetch', polyfillLocation);
}
}