forked from mscdex/dicer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: use
Writable#_final
for 'finish' tracking
Instead overriding `emit` method and using `this._realFinish` to detect unexpected end of data - implement `_final` method and track the logic there. The only behavior change from the use of `_final` is that we would no longer emit `finish` when the input data is terminated early. Instead we would emit `error` as before and stop the processing. Note that this is the behavior provided by `stream.Writable`, and it is thus conformant with the specification. Additionally, replace uses of private `_events` property with either straight `emit()` with return value check, or `listenerCount()` in the situations where there might be an overhead from the constructed event arguments. Fix: mscdex#26
- Loading branch information
1 parent
c64ada8
commit 0ab918d
Showing
3 changed files
with
72 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { inspect } = require('util'); | ||
const { Readable } = require('stream'); | ||
const { pipeline } = require('stream/promises'); | ||
const Dicer = require('..'); | ||
|
||
async function main() { | ||
const r = new Readable({ read() {} }); | ||
const d = new Dicer({ boundary: 'a' }); | ||
|
||
d.on('part', async (part) => { | ||
part.resume(); | ||
}); | ||
|
||
r.push(`--a\r\nA: 1\r\nB: 1\r\n\r\n123\r\n--a\r\n\r\n456\r\n--a--\r\n`); | ||
setImmediate(() => { | ||
r.push(null); | ||
}); | ||
|
||
const timer = setTimeout(() => { | ||
throw new Error('Should be canceled'); | ||
}, 2000); | ||
|
||
await pipeline(r, d); | ||
|
||
clearTimeout(timer); | ||
} | ||
|
||
main().catch((err) => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |