11import { accepts } from "jsr:@std/http@1.0.12/negotiation"
22import { escape } from "jsr:@std/html@1.0.3"
3- import { decodeBase64 } from "jsr:@std/encoding@1.0.8/base64"
43
54function cleanStack ( str ?: string ) {
65 if ( ! str ) return undefined ;
@@ -78,7 +77,27 @@ function respondWithError(request: Request, error: Error) {
7877 ) ;
7978}
8079
81- const payload = JSON . parse ( Deno . args [ 0 ] ) ;
80+ const { SMALLWEB_SOCKET_PATH } = Deno . env . toObject ( ) ;
81+
82+ if ( ! SMALLWEB_SOCKET_PATH ) {
83+ console . error ( "SMALLWEB_SOCKET is not set." ) ;
84+ Deno . exit ( 1 ) ;
85+ }
86+
87+ const client = Deno . createHttpClient ( {
88+ proxy : {
89+ transport : "unix" ,
90+ path : SMALLWEB_SOCKET_PATH ,
91+ }
92+ } ) ;
93+
94+ const resp = await fetch ( "http://worker.localhost/payload" , { client } )
95+ if ( ! resp . ok ) {
96+ console . error ( "Could not get worker payload:" , resp . statusText ) ;
97+ Deno . exit ( 1 ) ;
98+ }
99+
100+ const payload = await resp . json ( ) ;
82101
83102if ( ! payload || ! payload . command ) {
84103 console . error ( "Invalid input." ) ;
@@ -89,10 +108,9 @@ if (payload.command === "fetch") {
89108 Deno . serve (
90109 {
91110 port : parseInt ( payload . port ) ,
92- onListen : ( ) => {
93- // This line will signal that the server is ready to the go
94- console . error ( "READY" ) ;
95- } ,
111+ onListen : async ( ) => {
112+ await fetch ( "http://worker.localhost/ready" , { client } ) ;
113+ }
96114 } ,
97115 async ( req ) => {
98116 try {
@@ -162,7 +180,13 @@ if (payload.command === "fetch") {
162180 Deno . exit ( 1 ) ;
163181 }
164182
165- await handler . run ( payload . args ) ;
183+ const resp = await fetch ( "http://worker.localhost/command/stdin" , { client } ) ;
184+ if ( ! resp . ok ) {
185+ console . error ( "Could not get command input:" , resp . statusText ) ;
186+ Deno . exit ( 1 ) ;
187+ }
188+
189+ await handler . run ( payload . args , await resp . body ) ;
166190} else if ( payload . command === "email" ) {
167191 const mod = await import ( payload . entrypoint ) ;
168192 if ( ! mod . default || typeof mod . default !== "object" ) {
@@ -178,9 +202,13 @@ if (payload.command === "fetch") {
178202 Deno . exit ( 1 ) ;
179203 }
180204
181- const data = decodeBase64 ( payload . msg )
182- const blob = new Blob ( [ data ] ) ;
183- await handler . email ( blob . stream ( ) ) ;
205+ const resp = await fetch ( "http://worker.localhost/email/msg" , { client } ) ;
206+ if ( ! resp . ok ) {
207+ console . error ( "Could not get email message:" , resp . statusText ) ;
208+ Deno . exit ( 1 ) ;
209+ }
210+
211+ await handler . email ( await resp . body ) ;
184212} else {
185213 console . error ( "Unknown command: " , payload . command ) ;
186214 Deno . exit ( 1 ) ;
0 commit comments