@@ -3,13 +3,14 @@ import {
33 type ScramjetFetchContext ,
44 type ScramjetFetchResponse ,
55 CookieJar ,
6- handleFetch ,
6+ ScramjetFetchHandler ,
77 rewriteUrl ,
88 setConfig ,
99 unrewriteUrl ,
1010 type URLMeta ,
11- ScramjetServiceWorker ,
11+ setInterface ,
1212} from "@mercuryworkshop/scramjet/bundled" ;
13+
1314import { BareClient } from "@mercuryworkshop/bare-mux-custom" ;
1415import { ElementType , type Handler , Parser } from "htmlparser2" ;
1516import { type ChildNode , DomHandler , Element , Comment , Node } from "domhandler" ;
@@ -86,6 +87,12 @@ const cfg = {
8687} ;
8788
8889setConfig ( cfg ) ;
90+ setInterface ( {
91+ onServerbound : ( type , fn ) => { } ,
92+ onClientbound : ( type , fn ) => { } ,
93+ sendClientbound : ( type , message ) => { } ,
94+ sendServerbound : ( type , message ) => { } ,
95+ } ) ;
8996export const bare = new BareClient (
9097 new LibcurlClient ( {
9198 wisp : cfg . wisp ,
@@ -94,7 +101,8 @@ export const bare = new BareClient(
94101
95102type Controller = {
96103 controllerframe : HTMLIFrameElement ;
97- cookiestore : CookieJar ;
104+ cookieJar : CookieJar ;
105+ fetchHandler : ScramjetFetchHandler ;
98106 rootdomain : string ;
99107 baseurl : URL ;
100108 prefix : URL ;
@@ -139,7 +147,33 @@ function makeController(url: URL): Controller {
139147 } ) ;
140148
141149 const prefix = new URL ( baseurl . protocol + baseurl . host + cfg . prefix ) ;
142- const cookiestore = new CookieJar ( ) ;
150+ const cookieJar = new CookieJar ( ) ;
151+
152+ const fetchHandler = new ScramjetFetchHandler ( {
153+ client : bare ,
154+ cookieJar,
155+ prefix : prefix ,
156+ } ) ;
157+ fetchHandler . addEventListener ( "htmlPostRewrite" , ( e : any ) => {
158+ const handler = e . handler as DomHandler ;
159+ function findhead ( node : Element ) : Element | null {
160+ if ( node . type === ElementType . Tag && node . name === "head" ) {
161+ return node as Element ;
162+ } else if ( node . childNodes ) {
163+ for ( const child of node . childNodes ) {
164+ const head = findhead ( child as Element ) ;
165+ if ( head ) return head ;
166+ }
167+ }
168+
169+ return null ;
170+ }
171+
172+ const head = findhead ( handler . root as Node as Element ) ! ;
173+
174+ // inject after the scramjet scripts and before the rest of the page
175+ head . children . splice ( 3 , 0 , new Element ( "script" , { src : inject_script } ) ) ;
176+ } ) ;
143177
144178 const controller = {
145179 controllerframe : frame ,
@@ -149,7 +183,8 @@ function makeController(url: URL): Controller {
149183 prefix,
150184 ready,
151185 readyResolve : readyResolve ! ,
152- cookiestore,
186+ cookieJar,
187+ fetchHandler,
153188 } ;
154189 controllers . push ( controller ) ;
155190
@@ -190,7 +225,7 @@ const methods = {
190225 controller : Controller
191226 ) : Promise < [ ScramjetFetchResponse , Transferable [ ] | undefined ] > {
192227 // repopulate fetchcontext fields with the items that weren't cloned over postMessage
193- data . cookieStore = controller . cookiestore ;
228+ data . cookieStore = controller . cookieJar ;
194229 data . rawUrl = new URL ( data . rawUrl ) ;
195230 if ( data . rawClientUrl ) data . rawClientUrl = new URL ( data . rawClientUrl ) ;
196231 let headers = new ScramjetHeaders ( ) ;
@@ -257,36 +292,7 @@ const methods = {
257292 }
258293 }
259294
260- // TODO fix eventtarget jank
261- const tgt = new EventTarget ( ) ;
262- tgt . addEventListener ( "htmlPostRewrite" , ( e : any ) => {
263- const handler = e . handler as DomHandler ;
264- function findhead ( node : Element ) : Element | null {
265- if ( node . type === ElementType . Tag && node . name === "head" ) {
266- return node as Element ;
267- } else if ( node . childNodes ) {
268- for ( const child of node . childNodes ) {
269- const head = findhead ( child as Element ) ;
270- if ( head ) return head ;
271- }
272- }
273-
274- return null ;
275- }
276-
277- const head = findhead ( handler . root as Node as Element ) ! ;
278-
279- // inject after the scramjet scripts and before the rest of the page
280- head . children . splice ( 3 , 0 , new Element ( "script" , { src : inject_script } ) ) ;
281- } ) ;
282-
283- const fetchresponse = await handleFetch . call (
284- tgt as any ,
285- data ,
286- cfg ,
287- bare ,
288- controller . prefix
289- ) ;
295+ const fetchresponse = await controller . fetchHandler . handleFetch ( data ) ;
290296
291297 let transfer : any [ ] | undefined = undefined ;
292298 if (
0 commit comments