@@ -20,7 +20,7 @@ const args = Object.fromEntries(
2020 process . argv . slice ( 2 ) . reduce ( ( acc , cur , i , a ) => {
2121 if ( cur . startsWith ( "--" ) ) acc . push ( [ cur . slice ( 2 ) , a [ i + 1 ] ] ) ;
2222 return acc ;
23- } , [ ] )
23+ } , [ ] ) ,
2424) ;
2525const DIR = path . resolve ( args . dir ?? "ota-out" ) ;
2626const PORT = parseInt ( args . port ?? "8099" , 10 ) ;
@@ -43,23 +43,34 @@ const LOG_HEADERS = [
4343function platforms ( ) {
4444 return fs
4545 . readdirSync ( DIR , { withFileTypes : true } )
46- . filter ( ( d ) => d . isDirectory ( ) && fs . existsSync ( path . join ( DIR , d . name , "manifest.json" ) ) )
46+ . filter (
47+ ( d ) =>
48+ d . isDirectory ( ) &&
49+ fs . existsSync ( path . join ( DIR , d . name , "manifest.json" ) ) ,
50+ )
4751 . map ( ( d ) => d . name ) ;
4852}
4953
5054function loadManifest ( platform , host ) {
51- const raw = JSON . parse ( fs . readFileSync ( path . join ( DIR , platform , "manifest.json" ) , "utf8" ) ) ;
55+ const raw = JSON . parse (
56+ fs . readFileSync ( path . join ( DIR , platform , "manifest.json" ) , "utf8" ) ,
57+ ) ;
5258 const rewrite = ( u ) => `http://${ host } ${ new URL ( u ) . pathname } ` ;
5359 raw . launchAsset . url = rewrite ( raw . launchAsset . url ) ;
5460 raw . assets = raw . assets . map ( ( a ) => ( { ...a , url : rewrite ( a . url ) } ) ) ;
5561 return raw ;
5662}
5763
5864function contentTypeMap ( platform ) {
59- const m = JSON . parse ( fs . readFileSync ( path . join ( DIR , platform , "manifest.json" ) , "utf8" ) ) ;
65+ const m = JSON . parse (
66+ fs . readFileSync ( path . join ( DIR , platform , "manifest.json" ) , "utf8" ) ,
67+ ) ;
6068 const map = new Map ( ) ;
6169 for ( const a of m . assets ) map . set ( a . key , a . contentType ) ;
62- map . set ( path . basename ( new URL ( m . launchAsset . url ) . pathname ) , m . launchAsset . contentType ) ;
70+ map . set (
71+ path . basename ( new URL ( m . launchAsset . url ) . pathname ) ,
72+ m . launchAsset . contentType ,
73+ ) ;
6374 return map ;
6475}
6576
@@ -71,8 +82,16 @@ function multipartBody(manifest) {
7182 `content-type: ${ contentType } \r\n\r\n` +
7283 `${ body } \r\n` ;
7384 const body =
74- part ( "manifest" , JSON . stringify ( manifest ) , "application/json; charset=utf-8" ) +
75- part ( "extensions" , JSON . stringify ( { assetRequestHeaders : { } } ) , "application/json" ) +
85+ part (
86+ "manifest" ,
87+ JSON . stringify ( manifest ) ,
88+ "application/json; charset=utf-8" ,
89+ ) +
90+ part (
91+ "extensions" ,
92+ JSON . stringify ( { assetRequestHeaders : { } } ) ,
93+ "application/json" ,
94+ ) +
7695 `--${ boundary } --\r\n` ;
7796 return { body, contentType : `multipart/mixed; boundary=${ boundary } ` } ;
7897}
@@ -89,7 +108,11 @@ function chooseFraming(req, url) {
89108 const q = url . searchParams . get ( "framing" ) ;
90109 if ( q === "json" || q === "multipart" ) return q ;
91110 const accept = req . headers [ "accept" ] ?? "" ;
92- if ( accept . includes ( "multipart/mixed" ) || req . headers [ "expo-protocol-version" ] ) return "multipart" ;
111+ if (
112+ accept . includes ( "multipart/mixed" ) ||
113+ req . headers [ "expo-protocol-version" ]
114+ )
115+ return "multipart" ;
93116 return "json" ;
94117}
95118
@@ -155,14 +178,17 @@ const server = http.createServer((req, res) => {
155178 const { body, contentType } = multipartBody ( manifest ) ;
156179 // OTA_NO_PROTO_HEADERS mimics raw S3, which can set content-type but not
157180 // arbitrary response headers like expo-protocol-version / expo-sfv-version.
158- const headers = { "content-type" : contentType , "cache-control" : "private, max-age=0" } ;
181+ const headers = {
182+ "content-type" : contentType ,
183+ "cache-control" : "private, max-age=0" ,
184+ } ;
159185 if ( ! process . env . OTA_NO_PROTO_HEADERS ) {
160186 headers [ "expo-protocol-version" ] = "1" ;
161187 headers [ "expo-sfv-version" ] = "0" ;
162188 }
163189 res . writeHead ( 200 , headers ) ;
164190 console . log (
165- ` -> 200 multipart (${ body . length } bytes)${ process . env . OTA_NO_PROTO_HEADERS ? " [no proto headers / S3-mimic]" : "" } `
191+ ` -> 200 multipart (${ body . length } bytes)${ process . env . OTA_NO_PROTO_HEADERS ? " [no proto headers / S3-mimic]" : "" } ` ,
166192 ) ;
167193 res . end ( body ) ;
168194 } else {
@@ -180,8 +206,10 @@ const server = http.createServer((req, res) => {
180206
181207 // bundle / assets
182208 let filePath = null ;
183- if ( parts [ 1 ] === "bundle.hbc" ) filePath = path . join ( DIR , platform , "bundle.hbc" ) ;
184- else if ( parts [ 1 ] === "assets" && parts [ 2 ] ) filePath = path . join ( DIR , platform , "assets" , parts [ 2 ] ) ;
209+ if ( parts [ 1 ] === "bundle.hbc" )
210+ filePath = path . join ( DIR , platform , "bundle.hbc" ) ;
211+ else if ( parts [ 1 ] === "assets" && parts [ 2 ] )
212+ filePath = path . join ( DIR , platform , "assets" , parts [ 2 ] ) ;
185213
186214 if ( filePath && fs . existsSync ( filePath ) ) {
187215 const name = path . basename ( filePath ) ;
@@ -197,6 +225,8 @@ const server = http.createServer((req, res) => {
197225} ) ;
198226
199227server . listen ( PORT , "0.0.0.0" , ( ) => {
200- console . log ( `OTA test server on http://0.0.0.0:${ PORT } (platforms: ${ platforms ( ) . join ( ", " ) || "none" } )` ) ;
228+ console . log (
229+ `OTA test server on http://0.0.0.0:${ PORT } (platforms: ${ platforms ( ) . join ( ", " ) || "none" } )` ,
230+ ) ;
201231 console . log ( `Open on the phone: http://<this-mac-LAN-ip>:${ PORT } /` ) ;
202232} ) ;
0 commit comments