File size: 1,954 Bytes
bee6636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// entrypoint for scramjet.client.js

import { loadCodecs, setConfig } from "../shared/index";
import { SCRAMJETCLIENT } from "../symbols";
import { ScramjetClient } from "./client";
import { ScramjetContextEvent, UrlChangeEvent } from "./events";
import { ScramjetServiceWorkerRuntime } from "./swruntime";

export const iswindow = "window" in self && window instanceof Window;
export const isworker = "WorkerGlobalScope" in self;
export const issw = "ServiceWorkerGlobalScope" in self;
export const isdedicated = "DedicatedWorkerGlobalScope" in self;
export const isshared = "SharedWorkerGlobalScope" in self;
export const isemulatedsw =
	new URL(self.location.href).searchParams.get("dest") === "serviceworker";

function createFrameId() {
	return `${Array(8)

		.fill(0)

		.map(() => Math.floor(Math.random() * 36).toString(36))

		.join("")}`;
}

export function clientInitHook(config: ScramjetConfig) {
	setConfig(config);
	dbg.log("initializing scramjet client");
	// if it already exists, that means the handlers have probably already been setup by the parent document
	if (!(SCRAMJETCLIENT in <Partial<typeof self>>self)) {
		loadCodecs();

		const client = new ScramjetClient(self);
		const frame: HTMLIFrameElement = self.frameElement as HTMLIFrameElement;
		if (frame && !frame.name) {
			// all frames need to be named for our logic to work
			frame.name = createFrameId();
		}

		if (self.COOKIE) client.loadcookies(self.COOKIE);

		client.hook();

		if (isemulatedsw) {
			const runtime = new ScramjetServiceWorkerRuntime(client);
			runtime.hook();
		}

		const contextev = new ScramjetContextEvent(client.global.window, client);
		client.frame?.dispatchEvent(contextev);
		const urlchangeev = new UrlChangeEvent(client.url.href);
		if (!client.isSubframe) client.frame?.dispatchEvent(urlchangeev);
	}

	Reflect.deleteProperty(self, "WASM");
	Reflect.deleteProperty(self, "COOKIE");
}