forked from nodejs/node
-
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.
stream: add abort signal for ReadableStream and WritableStream
Refs: nodejs#39316
- Loading branch information
1 parent
23effb2
commit 3eaadf3
Showing
5 changed files
with
104 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { finished, addAbortSignal } = require('stream'); | ||
const { ReadableStream, WritableStream } = require('stream/web'); | ||
const assert = require('assert'); | ||
|
||
{ | ||
const rs = new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue('hello'); | ||
controller.close(); | ||
} | ||
}); | ||
|
||
const reader = rs.getReader(); | ||
|
||
const ac = new AbortController(); | ||
|
||
addAbortSignal(ac.signal, rs); | ||
|
||
finished(rs, common.mustCall((err) => { | ||
assert.strictEqual(err.name, 'AbortError'); | ||
})); | ||
|
||
ac.abort(); | ||
|
||
assert.rejects(reader.read(), 'AbortError: The operation was aborted.'); | ||
} | ||
|
||
{ | ||
const rs = new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue('a'); | ||
controller.enqueue('b'); | ||
controller.enqueue('c'); | ||
controller.close(); | ||
} | ||
}); | ||
|
||
const ac = new AbortController(); | ||
|
||
addAbortSignal(ac.signal, rs); | ||
|
||
assert.rejects((async () => { | ||
for await (const chunk of rs) { | ||
if (chunk === 'b') { | ||
ac.abort(); | ||
} | ||
} | ||
})(), /AbortError/); | ||
} | ||
|
||
{ | ||
const values = []; | ||
const ws = new WritableStream({ | ||
write(chunk) { | ||
values.push(chunk); | ||
} | ||
}); | ||
|
||
finished(ws, common.mustCall((err) => { | ||
assert.strictEqual(err.name, 'AbortError'); | ||
assert.deepStrictEqual(values, ['a']); | ||
})); | ||
|
||
const ac = new AbortController(); | ||
|
||
addAbortSignal(ac.signal, ws); | ||
|
||
const writer = ws.getWriter(); | ||
|
||
writer.write('a').then(() => { | ||
ac.abort(); | ||
}); | ||
} |