11import { MethodsDefinition , RpcHelper } from "@mercuryworkshop/rpc" ;
22import {
3+ codecDecode ,
34 CookieJar ,
5+ rewriteUrl ,
46 ScramjetFetchHandler ,
57 ScramjetHeaders ,
8+ setConfig ,
69 type ScramjetFetchContext ,
710} from "@mercuryworkshop/scramjet" ;
811import { 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
1022const 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+
2483const frames : Record < string , Frame > = { } ;
2584
2685let 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}
0 commit comments