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.
PR-URL: nodejs#36061 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Robert Nagy <[email protected]>
- Loading branch information
1 parent
5122456
commit 5bd1eec
Showing
10 changed files
with
183 additions
and
13 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,41 @@ | ||
'use strict'; | ||
|
||
const { | ||
AbortError, | ||
codes, | ||
} = require('internal/errors'); | ||
|
||
const eos = require('internal/streams/end-of-stream'); | ||
const { ERR_INVALID_ARG_TYPE } = codes; | ||
|
||
// This method is inlined here for readable-stream | ||
// https://github.com/nodejs/node/pull/36061#discussion_r533718029 | ||
const validateAbortSignal = (signal, name) => { | ||
if (signal !== undefined && | ||
(signal === null || | ||
typeof signal !== 'object' || | ||
!('aborted' in signal))) { | ||
throw new ERR_INVALID_ARG_TYPE(name, 'AbortSignal', signal); | ||
} | ||
}; | ||
|
||
function isStream(obj) { | ||
return !!(obj && typeof obj.pipe === 'function'); | ||
} | ||
|
||
module.exports = function addAbortSignal(signal, stream) { | ||
validateAbortSignal(signal, 'signal'); | ||
if (!isStream(stream)) { | ||
throw new ERR_INVALID_ARG_TYPE('stream', 'stream.Stream', stream); | ||
} | ||
const onAbort = () => { | ||
stream.destroy(new AbortError()); | ||
}; | ||
if (signal.aborted) { | ||
onAbort(); | ||
} else { | ||
signal.addEventListener('abort', onAbort); | ||
eos(stream, () => signal.removeEventListener('abort', onAbort)); | ||
} | ||
return stream; | ||
}; |
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
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