Skip to content

Commit dc5c14d

Browse files
committed
[scramjet/bootstrap] write bootstrap logic
1 parent 82d0a55 commit dc5c14d

File tree

15 files changed

+928
-193
lines changed

15 files changed

+928
-193
lines changed

packages/scramjet/packages/bootstrap/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33
"version": "0.0.1",
44
"type": "module",
55
"packageManager": "pnpm@10.12.1",
6-
"main": "./dist/bootstrap.js",
6+
"main": "./dist/bootstrap-server.js",
7+
"types": "./dist/types/server.d.ts",
78
"dependencies": {
89
"server": "link:@mercuryworkshop/wisp-js/server",
910
"tar": "^7.5.1"
11+
},
12+
"devDependencies": {
13+
"@mercuryworkshop/epoxy-transport": "catalog:",
14+
"@mercuryworkshop/libcurl-transport": "catalog:",
15+
"@mercuryworkshop/proxy-transports": "catalog:",
16+
"@mercuryworkshop/scramjet-controller": "workspace:*"
1017
}
1118
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { loadScript, registerSw } from "./clientcommon";
2+
import { BootstrapOptions } from "./common";
3+
import type { ProxyTransport } from "@mercuryworkshop/proxy-transports";
4+
import type EpoxyClient from "@mercuryworkshop/epoxy-transport";
5+
import type LibcurlClient from "@mercuryworkshop/libcurl-transport";
6+
import * as ControllerApi from "@mercuryworkshop/scramjet-controller";
7+
8+
export async function init(cfg: BootstrapOptions) {
9+
let sw = await registerSw(cfg.swPath);
10+
loadRest(sw, cfg);
11+
}
12+
13+
export async function loadRest(sw: ServiceWorker, cfg: BootstrapOptions) {
14+
await loadScript(cfg.scramjetBundlePath);
15+
await loadScript(cfg.scramjetControllerApiPath);
16+
17+
let transport!: ProxyTransport;
18+
if (cfg.transport === "epoxy") {
19+
await loadScript(cfg.epoxyClientPath);
20+
let EpoxyCtor: typeof EpoxyClient = (window as any).EpoxyTransport
21+
.EpoxyClient;
22+
transport = new EpoxyCtor({ wisp: cfg.wispPath });
23+
} else if (cfg.transport === "libcurl") {
24+
await loadScript(cfg.libcurlClientPath);
25+
const LibcurlCtor: typeof LibcurlClient = (window as any).LibcurlTransport
26+
.LibcurlClient;
27+
transport = new LibcurlCtor({ wisp: cfg.wispPath });
28+
} else if (cfg.transport === "bare") {
29+
throw new Error("Bare transport not implemented yet");
30+
//...
31+
}
32+
let { Controller, config } = (window as any)
33+
.$scramjetController as typeof ControllerApi;
34+
config.injectPath = cfg.scramjetControllerInjectPath;
35+
config.wasmPath = cfg.scramjetWasmPath;
36+
config.scramjetPath = cfg.scramjetBundlePath;
37+
38+
let controller = new Controller({
39+
serviceworker: sw,
40+
transport,
41+
});
42+
console.log(">?/");
43+
44+
return controller;
45+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
export async function registerSw(path: string): Promise<ServiceWorker> {
2+
const registration = await navigator.serviceWorker.register(path, {
3+
type: "classic",
4+
updateViaCache: "none",
5+
});
6+
7+
// Wait for the service worker to be ready
8+
await navigator.serviceWorker.ready;
9+
10+
// If there's an active service worker, use it
11+
if (registration.active) {
12+
return registration.active;
13+
}
14+
15+
// If there's an installing service worker, wait for it
16+
if (registration.installing) {
17+
await new Promise<void>((resolve) => {
18+
const sw = registration.installing!;
19+
if (sw.state === "activated") {
20+
resolve();
21+
} else {
22+
sw.addEventListener("statechange", function onChange() {
23+
if (sw.state === "activated") {
24+
sw.removeEventListener("statechange", onChange);
25+
resolve();
26+
}
27+
});
28+
}
29+
});
30+
31+
return registration.active!;
32+
}
33+
34+
// If there's a waiting service worker, skip waiting and activate it
35+
if (registration.waiting) {
36+
registration.waiting.postMessage({ type: "SKIP_WAITING" });
37+
38+
await new Promise<void>((resolve) => {
39+
navigator.serviceWorker.addEventListener(
40+
"controllerchange",
41+
() => {
42+
resolve();
43+
},
44+
{ once: true }
45+
);
46+
});
47+
48+
return navigator.serviceWorker.controller!;
49+
}
50+
51+
throw new Error("No service worker found in registration");
52+
}
53+
54+
export function loadScript(url: string): Promise<void> {
55+
return new Promise((resolve, reject) => {
56+
const script = document.createElement("script");
57+
script.src = url;
58+
script.onload = () => resolve();
59+
script.onerror = () => reject(new Error("Failed to load script " + url));
60+
document.head.appendChild(script);
61+
});
62+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export const REGISTRY_URL = "https://registry.npmjs.org/";
2+
export const SCRAMJET_PACKAGE_NAME = "@mercuryworkshop/scramjet";
3+
4+
export const SCRAMJET_CONTROLLER_PACKAGE_NAME =
5+
"@mercuryworkshop/scramjet-controller";
6+
export const SCRAMJET_CONTROLLER_PINNED_MAJOR_VERSION = "0";
7+
8+
export const EPOXY_TRANSPORT_PACKAGE_NAME = "@mercuryworkshop/epoxy-transport";
9+
export const EPOXY_TRANSPORT_PINNED_MAJOR_VERSION = "3";
10+
11+
export const LIBCURL_TRANSPORT_PACKAGE_NAME =
12+
"@mercuryworkshop/libcurl-transport";
13+
export const LIBCURL_TRANSPORT_PINNED_MAJOR_VERSION = "2";
14+
15+
export type TransportOptions = "epoxy" | "libcurl" | "bare";
16+
17+
export type BootstrapOptions = {
18+
transport: TransportOptions;
19+
swPath: string;
20+
21+
wispPath: string;
22+
23+
scramjetBundlePath: string;
24+
scramjetWasmPath: string;
25+
26+
epoxyClientPath: string;
27+
libcurlClientPath: string;
28+
bareClientPath: string;
29+
scramjetControllerApiPath: string;
30+
scramjetControllerInjectPath: string;
31+
scramjetControllerSwPath: string;
32+
33+
bootstrapApiPath: string;
34+
bootstrapInitPath: string;
35+
36+
scramjetVersionPin?: string;
37+
scramjetControllerVersionPin?: string;
38+
epoxyTransportVersionPin?: string;
39+
libcurlTransportVersionPin?: string;
40+
bareTransportVersionPin?: string;
41+
};
42+
43+
export const defaultConfig: Partial<BootstrapOptions> = {
44+
transport: "libcurl",
45+
swPath: "/sw.js",
46+
wispPath: "/wisp/",
47+
48+
epoxyClientPath: "/clients/epoxy-client.js",
49+
libcurlClientPath: "/clients/libcurl-client.js",
50+
bareClientPath: "/clients/bare-client.js",
51+
bootstrapInitPath: "/bootstrap-init.js",
52+
53+
scramjetControllerApiPath: "/controller/controller.api.js",
54+
scramjetControllerInjectPath: "/controller/controller.inject.js",
55+
scramjetControllerSwPath: "/controller/controller.sw.js",
56+
scramjetBundlePath: "/scram/scramjet.js",
57+
scramjetWasmPath: "/scram/scramjet.wasm",
58+
};

packages/scramjet/packages/bootstrap/src/index.ts

Lines changed: 0 additions & 171 deletions
This file was deleted.

0 commit comments

Comments
 (0)