@@ -113,7 +113,10 @@ function createOutputZip(output: string) {
113113 const zipfile = new YazlZipFile ( ) ;
114114 const writePromise = new Promise < void > ( ( resolve , reject ) => {
115115 zipfile . outputStream . on ( 'error' , reject ) ;
116- zipfile . outputStream . pipe ( fs . createWriteStream ( output ) ) . on ( 'close' , ( ) => {
116+ const writeStream = fs . createWriteStream ( output ) ;
117+ // without this, a full disk / read-only target left the promise pending
118+ writeStream . on ( 'error' , reject ) ;
119+ zipfile . outputStream . pipe ( writeStream ) . on ( 'close' , ( ) => {
117120 resolve ( void 0 ) ;
118121 } ) ;
119122 } ) ;
@@ -248,18 +251,13 @@ async function buildStreamBundlePatch(
248251 originBuffer && newBuffer
249252 ? tryTransformPair ( originBuffer , newBuffer )
250253 : null ;
251- const oldFile = pair
252- ? path . join ( tempRoot , 'old-transformed.bin' )
253- : rawOldFile ;
254- const newFile = pair
255- ? path . join ( tempRoot , 'new-transformed.bin' )
256- : rawNewFile ;
257- const patchFile = path . join ( tempRoot , 'patch.bin' ) ;
254+ const transformedOldFile = path . join ( tempRoot , 'old-transformed.bin' ) ;
255+ const transformedNewFile = path . join ( tempRoot , 'new-transformed.bin' ) ;
258256
259257 if ( pair ) {
260258 await Promise . all ( [
261- fs . writeFile ( oldFile , pair . tOld ) ,
262- fs . writeFile ( newFile , pair . tNew ) ,
259+ fs . writeFile ( transformedOldFile , pair . tOld ) ,
260+ fs . writeFile ( transformedNewFile , pair . tNew ) ,
263261 ] ) ;
264262 }
265263 reportDiffPhase ( options , {
@@ -268,9 +266,29 @@ async function buildStreamBundlePatch(
268266 inputBytes,
269267 } ) ;
270268
269+ // 与内存路径同一约束:变换候选连同元数据开销必须比 baseline 更小才采用,
270+ // 因此变换命中时两个候选都要生成再比较。
271271 phaseStartedAt = performance . now ( ) ;
272- await diffStreamFn ( oldFile , newFile , patchFile ) ;
273- const patchBytes = ( await fs . stat ( patchFile ) ) . size ;
272+ const rawPatchFile = path . join ( tempRoot , 'patch-raw.bin' ) ;
273+ await diffStreamFn ( rawOldFile , rawNewFile , rawPatchFile ) ;
274+ let patchFile = rawPatchFile ;
275+ let patchBytes = ( await fs . stat ( rawPatchFile ) ) . size ;
276+ let chosenPair : typeof pair = null ;
277+ if ( pair ) {
278+ const transformedPatchFile = path . join ( tempRoot , 'patch-transformed.bin' ) ;
279+ await diffStreamFn (
280+ transformedOldFile ,
281+ transformedNewFile ,
282+ transformedPatchFile ,
283+ ) ;
284+ const transformedPatchBytes = ( await fs . stat ( transformedPatchFile ) ) . size ;
285+ const metaOverhead = Buffer . byteLength ( JSON . stringify ( pair . meta ) ) ;
286+ if ( transformedPatchBytes + metaOverhead < patchBytes ) {
287+ patchFile = transformedPatchFile ;
288+ patchBytes = transformedPatchBytes ;
289+ chosenPair = pair ;
290+ }
291+ }
274292 reportDiffPhase ( options , {
275293 phase : 'diff' ,
276294 durationMs : performance . now ( ) - phaseStartedAt ,
@@ -280,13 +298,21 @@ async function buildStreamBundlePatch(
280298
281299 // 变换路径必须验证 T⁻¹ 和元数据;未声明自校验的自定义 diff 也保留
282300 // round-trip。node-hdiffpatch native 普通路径已经在返回前完整验证。
283- if ( pair || ! options . streamOutputVerified ) {
301+ if ( chosenPair || ! options . streamOutputVerified ) {
284302 phaseStartedAt = performance . now ( ) ;
285303 const restoredFile = path . join ( tempRoot , 'restored.bin' ) ;
286- await patchStreamFn ( oldFile , patchFile , restoredFile ) ;
287- if ( pair ) {
304+ await patchStreamFn (
305+ chosenPair ? transformedOldFile : rawOldFile ,
306+ patchFile ,
307+ restoredFile ,
308+ ) ;
309+ if ( chosenPair ) {
288310 const restoredRaw = await fs . readFile ( restoredFile ) ;
289- const restored = transformHbcWithLayout ( restoredRaw , pair . layout , true ) ;
311+ const restored = transformHbcWithLayout (
312+ restoredRaw ,
313+ chosenPair . layout ,
314+ true ,
315+ ) ;
290316 if (
291317 ! restored ||
292318 ! newBuffer ||
@@ -315,7 +341,7 @@ async function buildStreamBundlePatch(
315341
316342 return {
317343 patch : { kind : 'file' , path : patchFile } ,
318- ...( pair ? { hbcTransform : pair . meta } : { } ) ,
344+ ...( chosenPair ? { hbcTransform : chosenPair . meta } : { } ) ,
319345 } ;
320346}
321347
0 commit comments