| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 60e83d9 commit 9a83b5d
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -818,7 +818,7 @@ class Utf8Stream extends EventEmitter { | |||
| 818 | 818 | bufs.length === 0 || | |
| 819 | 819 | lens[lens.length - 1] + data.length > this.#maxWrite | |
| 820 | 820 | ) { | |
| 821 | - ArrayPrototypePush(bufs, []); | ||
| 821 | + ArrayPrototypePush(bufs, [data]); | ||
| 822 | 822 | ArrayPrototypePush(lens, data.length); | |
| 823 | 823 | } else { | |
| 824 | 824 | ArrayPrototypePush(bufs[bufs.length - 1], data); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,72 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + // Regression test for buffer content mode: the first buffer of every | ||
| 4 | + // batch used to be dropped, corrupting the output and eventually | ||
| 5 | + // crashing in mergeBuf(). Refs: https://github.com/nodejs/node/pull/58897 | ||
| 6 | + | ||
| 7 | + const common = require('../common'); | ||
| 8 | + const tmpdir = require('../common/tmpdir'); | ||
| 9 | + const assert = require('node:assert'); | ||
| 10 | + const { | ||
| 11 | + readFile, | ||
| 12 | + Utf8Stream, | ||
| 13 | + } = require('node:fs'); | ||
| 14 | + const { join } = require('node:path'); | ||
| 15 | + | ||
| 16 | + tmpdir.refresh(); | ||
| 17 | + let fileCounter = 0; | ||
| 18 | + | ||
| 19 | + function getTempFile() { | ||
| 20 | + return join(tmpdir.path, `fastutf8stream-${process.pid}-${Date.now()}-${fileCounter++}.log`); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + runTests(false); | ||
| 24 | + runTests(true); | ||
| 25 | + | ||
| 26 | + function runTests(sync) { | ||
| 27 | + { | ||
| 28 | + // A single buffer write must end up in the file. | ||
| 29 | + const dest = getTempFile(); | ||
| 30 | + const stream = new Utf8Stream({ dest, sync, contentMode: 'buffer' }); | ||
| 31 | + | ||
| 32 | + stream.on('ready', common.mustCall(() => { | ||
| 33 | + assert.ok(stream.write(Buffer.from('hello world\n'))); | ||
| 34 | + stream.end(); | ||
| 35 | + | ||
| 36 | + stream.on('finish', common.mustCall(() => { | ||
| 37 | + readFile(dest, 'utf8', common.mustSucceed((data) => { | ||
| 38 | + assert.strictEqual(data, 'hello world\n'); | ||
| 39 | + })); | ||
| 40 | + })); | ||
| 41 | + })); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + { | ||
| 45 | + // Writes that exceed maxWrite start a new batch; data must survive | ||
| 46 | + // the batch boundary and be written in order. | ||
| 47 | + const dest = getTempFile(); | ||
| 48 | + const stream = new Utf8Stream({ | ||
| 49 | + dest, | ||
| 50 | + sync, | ||
| 51 | + contentMode: 'buffer', | ||
| 52 | + minLength: 60, | ||
| 53 | + maxWrite: 64, | ||
| 54 | + }); | ||
| 55 | + | ||
| 56 | + stream.on('ready', common.mustCall(() => { | ||
| 57 | + stream.write(Buffer.from('a'.repeat(40))); | ||
| 58 | + stream.write(Buffer.from('b'.repeat(40))); | ||
| 59 | + stream.write(Buffer.from('c'.repeat(40))); | ||
| 60 | + stream.end(); | ||
| 61 | + | ||
| 62 | + stream.on('finish', common.mustCall(() => { | ||
| 63 | + readFile(dest, 'utf8', common.mustSucceed((data) => { | ||
| 64 | + assert.strictEqual( | ||
| 65 | + data, | ||
| 66 | + 'a'.repeat(40) + 'b'.repeat(40) + 'c'.repeat(40), | ||
| 67 | + ); | ||
| 68 | + })); | ||
| 69 | + })); | ||
| 70 | + })); | ||
| 71 | + } | ||
| 72 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments