@@ -65,10 +65,7 @@ async function updateLocalApk(
6565 cleanupCallback
6666 } = await createTmp ( { keep : true } ) ;
6767
68- const tmpApkStream = fs . createWriteStream ( tmpApk , {
69- fd : tmpApkFd ,
70- encoding : 'binary'
71- } ) ;
68+ const tmpApkStream = fs . createWriteStream ( tmpApk , { fd : tmpApkFd } ) ;
7269 apkStream . pipe ( tmpApkStream ) ;
7370
7471 await new Promise ( ( resolve , reject ) => {
@@ -124,15 +121,24 @@ export async function streamLatestApk(config: HtkConfig): Promise<stream.Readabl
124121 } else {
125122 console . log ( 'Streaming remote APK directly' ) ;
126123 const apkStream = ( await fetch ( latestApkRelease . url ) ) . body ;
127- updateLocalApk ( latestApkRelease . version , apkStream , config ) . catch ( reportError ) ;
128- return apkStream as stream . Readable ;
124+
125+ // We buffer output into two passthrough streams, so both file & install
126+ // stream usage can be set up async independently. Buffers are 10MB, to
127+ // avoid issues buffering the whole APK even in super weird cases.
128+ const apkFileStream = new stream . PassThrough ( { highWaterMark : 10485760 } ) ;
129+ apkStream . pipe ( apkFileStream ) ;
130+ const apkOutputStream = new stream . PassThrough ( { highWaterMark : 10485760 } ) ;
131+ apkStream . pipe ( apkOutputStream ) ;
132+
133+ updateLocalApk ( latestApkRelease . version , apkFileStream , config ) . catch ( reportError ) ;
134+ return apkOutputStream ;
129135 }
130136 }
131137
132138 if ( ! latestApkRelease || semver . gte ( localApk . version , latestApkRelease . version , true ) ) {
133139 console . log ( 'Streaming local APK' ) ;
134140 // If we have an APK locally and it's up to date, or we can't tell, just use it
135- return fs . createReadStream ( localApk . path , { encoding : 'binary' } ) ;
141+ return fs . createReadStream ( localApk . path ) ;
136142 }
137143
138144 // We have a local APK & a remote APK, and the remote is newer.
@@ -143,5 +149,5 @@ export async function streamLatestApk(config: HtkConfig): Promise<stream.Readabl
143149 } ) . catch ( reportError ) ;
144150
145151 console . log ( 'Streaming local APK, and updating it async' ) ;
146- return fs . createReadStream ( localApk . path , { encoding : 'binary' } ) ;
152+ return fs . createReadStream ( localApk . path ) ;
147153}
0 commit comments