| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5109092 commit fc80ff5
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -274,6 +274,12 @@ class BroadcastImpl { | |||
| 274 | 274 | [kWrite](chunk) { | |
| 275 | 275 | if (this.#ended || this.#cancelled) return false; | |
| 276 | 276 | ||
| 277 | + const batchSize = this.#batchByteSize(chunk); | ||
| 278 | + | ||
| 279 | + // Skip empty chunks -- zero-byte writes would accumulate infinitely | ||
| 280 | + // without ever triggering backpressure under a byte-budget model. | ||
| 281 | + if (batchSize === 0) return true; | ||
| 282 | + | ||
| 277 | 283 | if (this.#bufferedBytes >= this.#options.budget) { | |
| 278 | 284 | switch (this.#options.backpressure) { | |
| 279 | 285 | case 'strict': | |
@@ -299,7 +305,6 @@ class BroadcastImpl { | |||
| 299 | 305 | } | |
| 300 | 306 | } | |
| 301 | 307 | ||
| 302 | - const batchSize = this.#batchByteSize(chunk); | ||
| 303 | 308 | this.#buffer.push(chunk); | |
| 304 | 309 | this.#bufferedBytes += batchSize; | |
| 305 | 310 | this.#notifyConsumers(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -147,6 +147,26 @@ async function testWritevAsync() { | |||
| 147 | 147 | assert.strictEqual(data, 'hello world'); | |
| 148 | 148 | } | |
| 149 | 149 | ||
| 150 | + // Zero-byte writes do not consume buffer entries. | ||
| 151 | + async function testZeroByteWrites() { | ||
| 152 | + const { writer, broadcast: bc } = broadcast({ budget: 16384 }); | ||
| 153 | + const consumer = bc.push(); | ||
| 154 | + | ||
| 155 | + for (let i = 0; i < 1000; i++) { | ||
| 156 | + assert.strictEqual(writer.writeSync(''), true); | ||
| 157 | + assert.strictEqual(writer.writevSync([]), true); | ||
| 158 | + } | ||
| 159 | + await writer.write(''); | ||
| 160 | + await writer.writev([]); | ||
| 161 | + assert.strictEqual(writer.canWrite, true); | ||
| 162 | + writer.endSync(); | ||
| 163 | + | ||
| 164 | + let entries = 0; | ||
| 165 | + const iterator = consumer[Symbol.asyncIterator](); | ||
| 166 | + while (!(await iterator.next()).done) entries++; | ||
| 167 | + assert.strictEqual(entries, 0); | ||
| 168 | + } | ||
| 169 | + | ||
| 150 | 170 | // endSync returns the total byte count | |
| 151 | 171 | async function testEndSyncReturnValue() { | |
| 152 | 172 | const { writer, broadcast: bc } = broadcast({ budget: 16384 }); | |
@@ -165,5 +185,6 @@ Promise.all([ | |||
| 165 | 185 | testBlockBackpressureContent(), | |
| 166 | 186 | testStrictBackpressureOverflow(), | |
| 167 | 187 | testWritevAsync(), | |
| 188 | + testZeroByteWrites(), | ||
| 168 | 189 | testEndSyncReturnValue(), | |
| 169 | 190 | ]).then(common.mustCall()); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments