-
Notifications
You must be signed in to change notification settings - Fork 29.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: add readableDidRead if has been read from
Adds did read accessor used to determine whether a readable has been read from. PR-URL: #39589 Refs: nodejs/undici#907 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
- Loading branch information
Showing
3 changed files
with
133 additions
and
1 deletion.
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,104 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const Readable = require('stream').Readable; | ||
|
||
function noop() {} | ||
|
||
function check(readable, data, fn) { | ||
assert.strictEqual(readable.readableDidRead, false); | ||
if (data === -1) { | ||
readable.on('error', common.mustCall()); | ||
readable.on('data', common.mustNotCall()); | ||
readable.on('end', common.mustNotCall()); | ||
} else { | ||
readable.on('error', common.mustNotCall()); | ||
if (data === -2) { | ||
readable.on('end', common.mustNotCall()); | ||
} else { | ||
readable.on('end', common.mustCall()); | ||
} | ||
if (data > 0) { | ||
readable.on('data', common.mustCallAtLeast(data)); | ||
} else { | ||
readable.on('data', common.mustNotCall()); | ||
} | ||
} | ||
readable.on('close', common.mustCall()); | ||
fn(); | ||
setImmediate(() => { | ||
assert.strictEqual(readable.readableDidRead, true); | ||
}); | ||
} | ||
|
||
{ | ||
const readable = new Readable({ | ||
read() { | ||
this.push(null); | ||
} | ||
}); | ||
check(readable, 0, () => { | ||
readable.read(); | ||
}); | ||
} | ||
|
||
{ | ||
const readable = new Readable({ | ||
read() { | ||
this.push(null); | ||
} | ||
}); | ||
check(readable, 0, () => { | ||
readable.resume(); | ||
}); | ||
} | ||
|
||
{ | ||
const readable = new Readable({ | ||
read() { | ||
this.push(null); | ||
} | ||
}); | ||
check(readable, -2, () => { | ||
readable.destroy(); | ||
}); | ||
} | ||
|
||
{ | ||
const readable = new Readable({ | ||
read() { | ||
this.push(null); | ||
} | ||
}); | ||
|
||
check(readable, -1, () => { | ||
readable.destroy(new Error()); | ||
}); | ||
} | ||
|
||
{ | ||
const readable = new Readable({ | ||
read() { | ||
this.push('data'); | ||
this.push(null); | ||
} | ||
}); | ||
|
||
check(readable, 1, () => { | ||
readable.on('data', noop); | ||
}); | ||
} | ||
|
||
{ | ||
const readable = new Readable({ | ||
read() { | ||
this.push('data'); | ||
this.push(null); | ||
} | ||
}); | ||
|
||
check(readable, 1, () => { | ||
readable.on('data', noop); | ||
readable.off('data', noop); | ||
}); | ||
} |