https://github.com/gajus/output-interceptor
Intercepts stdout/ stderr.
https://github.com/gajus/output-interceptor
async interceptor stderr stdout
Last synced: 6 months ago
JSON representation
Intercepts stdout/ stderr.
- Host: GitHub
- URL: https://github.com/gajus/output-interceptor
- Owner: gajus
- License: other
- Created: 2019-01-10T01:23:06.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-01-30T17:11:16.000Z (over 5 years ago)
- Last Synced: 2025-03-29T01:34:33.047Z (6 months ago)
- Topics: async, interceptor, stderr, stdout
- Language: JavaScript
- Size: 26.4 KB
- Stars: 13
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# output-interceptor
[](https://gitspo.com/mentions/gajus/slonik)
[](https://travis-ci.org/gajus/output-interceptor)
[](https://coveralls.io/github/gajus/output-interceptor)
[](https://www.npmjs.org/package/output-interceptor)
[](https://github.com/gajus/canonical)
[](https://twitter.com/kuizinas)Intercepts stdout/ stderr.
## Implementation
This module uses [`domain`](https://nodejs.org/api/domain.html) to capture asynchronous function output.
Read [Capturing stdout/ stderr in Node.js using Domain module](https://medium.com/@gajus/capturing-stdout-stderr-in-node-js-using-domain-module-3c86f5b1536d).
## Usage
```js
import {
createOutputInterceptor
} from 'output-interceptor';const interceptOutput = createOutputInterceptor();
const main = async () => {
const result = await interceptOutput(() => {
console.log('foo');
console.error('bar');return Promise.resolve('baz');
});result === 'baz';
interceptOutput.output === 'foo\nbar\n';
};main();
```
### Singleton or dependency injection pattern
It is recommended that you only create one instance of output-interceptor per entire project, e.g.
Create `./routines.js` file with contents:
```js
import {
createOutputInterceptor
} from 'output-interceptor';export const interceptOutput = createOutputInterceptor();
```
Then just import the `{interceptOutput}` routine from elsewhere in your codebase.
Alternatively, create an instance of output-interceptor at the top of the program and pass it down using dependency injection.
The benefit of this approach is that you do not create unnecessary wrappers around `process.stderr.write` and `process.stdout.write`.
## API
```js
/**
* @property interceptStderr Default: true.
* @property interceptStdout Default: true.
* @property stripAnsi Default: true.
*/
export type OutputInterceptorUserConfigurationType = {|
+interceptStderr?: boolean,
+interceptStdout?: boolean,
+stripAnsi?: boolean
|};/**
* @returns Intercepted output.
*/
type FlushType = () => string;/**
* @property output Output produced during the executing of the `routine`.
*/
export type OutputInterceptorType = {|
(routine: () => Promise | T): Promise,
output: ''
|};createOutputInterceptor(userConfiguration?: OutputInterceptorUserConfigurationType): OutputInterceptorType;
```