Skip to content

Commit 48102c2

Browse files
committed
[scramjet/controller] fix sw bugs and add client
1 parent c3dd882 commit 48102c2

File tree

3 files changed

+96
-14
lines changed

3 files changed

+96
-14
lines changed

packages/scramjet/packages/controller/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"dependencies": {
88
"@mercuryworkshop/scramjet": "workspace:*",
99
"@mercuryworkshop/rpc": "workspace:*",
10-
"@mercuryworkshop/bare-mux-custom": "workspace:*"
10+
"@mercuryworkshop/bare-mux-custom": "workspace:*",
11+
"@mercuryworkshop/libcurl-transport": "workspace:*"
1112
}
1213
}

packages/scramjet/packages/controller/src/index.ts

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
import { MethodsDefinition, RpcHelper } from "@mercuryworkshop/rpc";
22
import {
3+
codecDecode,
34
CookieJar,
5+
rewriteUrl,
46
ScramjetFetchHandler,
57
ScramjetHeaders,
8+
setConfig,
69
type ScramjetFetchContext,
710
} from "@mercuryworkshop/scramjet";
811
import { Controllerbound, SWbound } from "./types";
12+
import LibcurlClient from "@mercuryworkshop/libcurl-transport";
13+
import { BareClient } from "@mercuryworkshop/bare-mux-custom";
14+
15+
let lc = new LibcurlClient({
16+
wisp: "wss://anura.pro/",
17+
});
18+
const client = new BareClient(lc);
19+
console.log(lc);
20+
console.log(client);
921

1022
const cookieJar = new CookieJar();
1123

@@ -16,11 +28,58 @@ type Config = {
1628
prefix: string;
1729
};
1830

19-
const config: Config = {
31+
export const config: Config = {
2032
prefix: "/~/sj",
2133
virtualWasmPath: "/scramjet.wasm.js",
2234
};
2335

36+
const cfg = {
37+
prefix: "/scramjet/",
38+
globals: {
39+
wrapfn: "$scramjet$wrap",
40+
wrappropertybase: "$scramjet__",
41+
wrappropertyfn: "$scramjet$prop",
42+
cleanrestfn: "$scramjet$clean",
43+
importfn: "$scramjet$import",
44+
rewritefn: "$scramjet$rewrite",
45+
metafn: "$scramjet$meta",
46+
wrappostmessagefn: "$scramjet$wrappostmessage",
47+
pushsourcemapfn: "$scramjet$pushsourcemap",
48+
trysetfn: "$scramjet$tryset",
49+
templocid: "$scramjet$temploc",
50+
tempunusedid: "$scramjet$tempunused",
51+
},
52+
flags: {
53+
syncxhr: false,
54+
strictRewrites: true,
55+
rewriterLogs: false,
56+
captureErrors: true,
57+
cleanErrors: false,
58+
scramitize: false,
59+
sourcemaps: true,
60+
destructureRewrites: false,
61+
allowInvalidJs: false,
62+
allowFailedIntercepts: false,
63+
antiAntiDebugger: false,
64+
},
65+
siteFlags: {},
66+
codec: {
67+
encode: `(url) => {
68+
if (!url) return url;
69+
70+
return encodeURIComponent(url);
71+
}`,
72+
decode: `(url) => {
73+
if (!url) return url;
74+
75+
return decodeURIComponent(url);
76+
}`,
77+
},
78+
maskedfiles: ["inject.js", "scramjet.wasm.js"],
79+
};
80+
81+
setConfig(cfg);
82+
2483
const frames: Record<string, Frame> = {};
2584

2685
let wasmPayload: string | null = null;
@@ -29,7 +88,7 @@ function makeId(): string {
2988
return Math.random().toString(36).substring(2, 10);
3089
}
3190

32-
class Controller {
91+
export class Controller {
3392
id: string;
3493
prefix: string;
3594
frames: Frame[] = [];
@@ -44,9 +103,11 @@ class Controller {
44103
this.readyResolve();
45104
},
46105
request: async (data) => {
106+
console.log("REQUEST", data);
47107
let path = new URL(data.rawUrl).pathname;
48108
const frame = this.frames.find((f) => path.startsWith(f.prefix));
49109
if (!frame) throw new Error("No frame found for request");
110+
console.log("?");
50111

51112
if (path.startsWith(frame.prefix + "/" + config.virtualWasmPath)) {
52113
if (!wasmPayload) {
@@ -88,6 +149,8 @@ class Controller {
88149
}
89150
}
90151

152+
console.log("fR");
153+
91154
const fetchresponse = await frame.fetchHandler.handleFetch({
92155
initialHeaders: sjheaders,
93156
rawClientUrl: data.rawClientUrl
@@ -104,6 +167,8 @@ class Controller {
104167
cookieStore: this.cookieJar,
105168
});
106169

170+
console.log("???");
171+
107172
return [
108173
{
109174
body: fetchresponse.body,
@@ -130,7 +195,7 @@ class Controller {
130195
let channel = new MessageChannel();
131196
this.rpc = new RpcHelper<Controllerbound, SWbound>(
132197
this.methods,
133-
"swchannel",
198+
"tabchannel-" + this.id,
134199
(data, transfer) => {
135200
channel.port1.postMessage(data, transfer);
136201
}
@@ -151,8 +216,9 @@ class Controller {
151216
);
152217
}
153218

154-
createFrame(): Frame {
155-
const frame = new Frame(this);
219+
createFrame(element?: HTMLIFrameElement): Frame {
220+
element ??= document.createElement("iframe");
221+
const frame = new Frame(this, element);
156222
this.frames.push(frame);
157223
return frame;
158224
}
@@ -167,12 +233,15 @@ class Frame {
167233
id: string;
168234
prefix: string;
169235

170-
constructor(public controller: Controller) {
236+
constructor(
237+
public controller: Controller,
238+
public element: HTMLIFrameElement
239+
) {
171240
this.id = makeId();
172-
this.prefix = this.controller.prefix + "/" + this.id;
241+
this.prefix = this.controller.prefix + "/" + this.id + "/";
173242

174243
this.fetchHandler = new ScramjetFetchHandler({
175-
client: null,
244+
client,
176245
cookieJar: this.controller.cookieJar,
177246
prefix: new URL(
178247
config.prefix + "/" + this.controller.id + "/" + this.id,
@@ -182,4 +251,14 @@ class Frame {
182251
onServerbound: (type, listener) => {},
183252
});
184253
}
254+
255+
go(url: string) {
256+
const encoded = rewriteUrl(url, {
257+
prefix: new URL(this.prefix, location.href),
258+
origin: new URL(location.href),
259+
base: new URL(location.href),
260+
});
261+
console.log(encoded);
262+
this.element.src = encoded;
263+
}
185264
}

packages/scramjet/packages/controller/src/sw.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Tab {
1111
public id: string,
1212
port: MessagePort
1313
) {
14-
this.rpc = new RpcHelper({}, "tabchannel" + id, (data, transfer) => {
14+
this.rpc = new RpcHelper({}, "tabchannel-" + id, (data, transfer) => {
1515
port.postMessage(data, transfer);
1616
});
1717
port.addEventListener("message", (e) => {
@@ -34,13 +34,15 @@ addEventListener("message", (e) => {
3434
tabs.push(new Tab(init.prefix, init.id, e.ports[0]));
3535
});
3636

37-
function shouldRoute(event: FetchEvent): boolean {
38-
const tab = tabs.find((tab) => event.request.url.startsWith(tab.prefix));
37+
export function shouldRoute(event: FetchEvent): boolean {
38+
const url = new URL(event.request.url);
39+
const tab = tabs.find((tab) => url.pathname.startsWith(tab.prefix));
3940
return tab !== undefined;
4041
}
4142

42-
async function route(event: FetchEvent): Promise<Response> {
43-
const tab = tabs.find((tab) => event.request.url.startsWith(tab.prefix))!;
43+
export async function route(event: FetchEvent): Promise<Response> {
44+
const url = new URL(event.request.url);
45+
const tab = tabs.find((tab) => url.pathname.startsWith(tab.prefix))!;
4446
const client = await clients.get(event.clientId);
4547

4648
const bareheaders: BareHeaders = {};

0 commit comments

Comments
 (0)