|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Regression test: after a multi-byte UTF-8 chunk is *fully* written, the |
| 4 | +// stream must keep flushing the remaining buffered chunks instead of stalling. |
| 5 | +// |
| 6 | +// `releaseWritingBuf()` tracks the buffered length in characters, but on a full |
| 7 | +// write it used to subtract the number of *bytes* reported by fs.write instead |
| 8 | +// of the number of *characters*. For multi-byte data this drove the internal |
| 9 | +// length to zero, so the stream emitted 'drain' and went idle while queued |
| 10 | +// chunks were left unwritten. |
| 11 | + |
| 12 | +constcommon=require('../common'); |
| 13 | +constassert=require('node:assert'); |
| 14 | +const{ Utf8Stream }=require('node:fs'); |
| 15 | + |
| 16 | +// "€" is a single JS character that encodes to three UTF-8 bytes, so the byte |
| 17 | +// count and character count differ - which is exactly what triggered the bug. |
| 18 | +constCHAR='€'; |
| 19 | +constCOUNT=3; |
| 20 | + |
| 21 | +constchunks=[]; |
| 22 | +constfsOverride={ |
| 23 | +// Always report a full (successful) write. |
| 24 | +write: common.mustCallAtLeast((fd,data,enc,cb)=>{ |
| 25 | +chunks.push(data); |
| 26 | +process.nextTick(cb,null,Buffer.byteLength(data)); |
| 27 | +},COUNT), |
| 28 | +writeSync(){thrownewError('writeSync should not be used in async mode');}, |
| 29 | +fsync(fd,cb){cb();}, |
| 30 | +fsyncSync(){}, |
| 31 | +close(fd,cb){cb();}, |
| 32 | +open(path,flags,mode,cb){cb(null,42);}, |
| 33 | +mkdir(path,opts,cb){cb();}, |
| 34 | +mkdirSync(){}, |
| 35 | +}; |
| 36 | + |
| 37 | +conststream=newUtf8Stream({ |
| 38 | +fd: 42, |
| 39 | +sync: false, |
| 40 | +minLength: 0, |
| 41 | +// Force each character into its own buffered chunk so that, while the first |
| 42 | +// write is in flight, the remaining characters stay queued. |
| 43 | +maxWrite: 1, |
| 44 | +fs: fsOverride, |
| 45 | +}); |
| 46 | + |
| 47 | +stream.on('ready',common.mustCall(()=>{ |
| 48 | +for(leti=0;i<COUNT;i++){ |
| 49 | +stream.write(CHAR); |
| 50 | +} |
| 51 | + |
| 52 | +// Without calling end(): the stream must flush everything on its own. |
| 53 | +setTimeout(common.mustCall(()=>{ |
| 54 | +assert.strictEqual(chunks.length,COUNT, |
| 55 | +`expected ${COUNT} writes, got ${chunks.length}`); |
| 56 | +assert.strictEqual(chunks.join(''),CHAR.repeat(COUNT)); |
| 57 | +stream.destroy(); |
| 58 | +}),common.platformTimeout(100)); |
| 59 | +})); |
0 commit comments