(sync)
| 30 | runTests(true); |
| 31 | |
| 32 | function runTests(sync) { |
| 33 | // Test 1: Partial write splitting a 3-byte UTF-8 character (CJK) |
| 34 | // "abc中def" where "中" is 3 bytes (E4 B8 AD) |
| 35 | // Simulate partial write of 4 bytes: "abc" (3 bytes) + first byte of "中" |
| 36 | // The remaining buffer should be "中def" (not "def") |
| 37 | { |
| 38 | const dest = getTempFile(); |
| 39 | const fd = openSync(dest, 'w'); |
| 40 | |
| 41 | let firstWrite = true; |
| 42 | const writtenChunks = []; |
| 43 | const fsOverride = {}; |
| 44 | |
| 45 | if (sync) { |
| 46 | fsOverride.writeSync = common.mustCall((...args) => { |
| 47 | const data = args[1]; |
| 48 | writtenChunks.push(typeof data === 'string' ? data : data.toString()); |
| 49 | if (firstWrite) { |
| 50 | firstWrite = false; |
| 51 | // Simulate partial write: only 4 bytes written out of 9 |
| 52 | // This splits the 3-byte "中" character |
| 53 | return 4; |
| 54 | } |
| 55 | return writeSync(...args); |
| 56 | }, 2); |
| 57 | } else { |
| 58 | fsOverride.write = common.mustCall((...args) => { |
| 59 | const data = args[1]; |
| 60 | writtenChunks.push(typeof data === 'string' ? data : data.toString()); |
| 61 | const callback = args[args.length - 1]; |
| 62 | if (firstWrite) { |
| 63 | firstWrite = false; |
| 64 | // Simulate partial write: only 4 bytes written out of 9 |
| 65 | process.nextTick(callback, null, 4); |
| 66 | return; |
| 67 | } |
| 68 | return write(...args); |
| 69 | }, 2); |
| 70 | } |
| 71 | |
| 72 | const stream = new Utf8Stream({ |
| 73 | fd, |
| 74 | sync, |
| 75 | minLength: 0, |
| 76 | fs: fsOverride, |
| 77 | }); |
| 78 | |
| 79 | stream.on('ready', common.mustCall(() => { |
| 80 | stream.write('abc中def'); |
| 81 | stream.end(); |
| 82 | |
| 83 | stream.on('finish', common.mustCall(() => { |
| 84 | // Verify the second chunk contains the preserved CJK character |
| 85 | assert.strictEqual(writtenChunks.length, 2); |
| 86 | assert.strictEqual(writtenChunks[0], 'abc中def'); // First attempt |
| 87 | assert.strictEqual(writtenChunks[1], '中def'); // Retry with preserved char |
| 88 | })); |
| 89 | })); |
no test coverage detected
searching dependent graphs…