-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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 <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
- Loading branch information
Showing
5 changed files
with
112 additions
and
19 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
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 |
---|---|---|
@@ -1,24 +1,104 @@ | ||
'use strict'; | ||
require('../common'); | ||
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: () => {} | ||
read() { | ||
this.push(null); | ||
} | ||
}); | ||
check(readable, 0, () => { | ||
readable.read(); | ||
}); | ||
} | ||
|
||
assert.strictEqual(readable.readableDidRead, false); | ||
readable.read(); | ||
assert.strictEqual(readable.readableDidRead, true); | ||
{ | ||
const readable = new Readable({ | ||
read() { | ||
this.push(null); | ||
} | ||
}); | ||
check(readable, 0, () => { | ||
readable.resume(); | ||
}); | ||
} | ||
|
||
{ | ||
const readable = new Readable({ | ||
read: () => {} | ||
read() { | ||
this.push(null); | ||
} | ||
}); | ||
check(readable, -2, () => { | ||
readable.destroy(); | ||
}); | ||
} | ||
|
||
assert.strictEqual(readable.readableDidRead, false); | ||
readable.resume(); | ||
assert.strictEqual(readable.readableDidRead, true); | ||
{ | ||
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); | ||
}); | ||
} |