You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Utf8Stream writes duplicate bytes when a short write splits a multi-byte character #67679
When fs.write() commits only partial of a string and the cut lands inside a multi byte UTF-8 character, fs.Utf8Stream leaves the already written lead bytes on the fd and then writes the whole character again.
See repro below forces one short write through the fs option. The short write itself is real: 4 of the 9 bytes of 'abc中def' reach the fd, which cuts 中 (e4 b8 ad) after its first byte.
'use strict';constfs=require('node:fs');constos=require('node:os');constpath=require('node:path');constfile=path.join(os.tmpdir(),`utf8stream-${process.pid}.txt`);constfd=fs.openSync(file,'w');letshortWriteDone=false;conststream=newfs.Utf8Stream({
fd,fs: {// Simulate a short write: the first call commits only 4 of 9 bytes,// which cuts '中' (e4 b8 ad) after its first byte.write(fd,data, ...rest){if(shortWriteDone)returnfs.write(fd,data, ...rest);shortWriteDone=true;fs.write(fd,Buffer.from(data).subarray(0,4),rest.at(-1));},},});stream.on('ready',()=>{stream.write('abc中def');stream.end();});stream.on('close',()=>{constactual=fs.readFileSync(file);fs.unlinkSync(file);console.log('expected:',Buffer.from('abc中def').toString('hex'));console.log('actual: ',actual.toString('hex'),JSON.stringify(actual.toString()));});
How often does it reproduce? Is there a required condition?
Always
What is the expected behavior? Why is that the expected behavior?
Activity
jakecastelli commentedon Nov 21, 2026
Same happens with 2-byte characters, so accented Latin text is also affected.
Using the repro above with
café!