admin管理员组文章数量:1303412
I want to wrap a array, e.g. an Uint8Array
, into a NodeJS.WritableStream
so I can read its content later. How can I do that?
Background: For testing my winston logger, I want to set up a custom transport which just holds all the logged data in a manner I can evaluate it later. The example reads like this:
logger.add(new winston.transports.Stream({
stream: fs.createWriteStream('/dev/null')
/* other options */
}));
But if I create the stream like fs.createWriteStream('/dev/null')
, I cannot see its content, and it won't run under Windows (or at least I'm having issues here atm). And why would I need fs
anyway?
To give another illustration where I'm coming from, let's take this arbitraty example from Go:
buf := bytes.Buffer{}
// zerolog.New() expects an io.Writable, similar to a NodeJS.WritableStream
logger := zerolog.New(&buf)
logger.Info().Msg("Hello World")
// I can access the written data by acessing the underlying buffer
assert.Equal(t, buf.String(), "{\"level\":\"info\",\"message\":\"Hello World\"}\n")
Note this isn't a specific thing for zerolog
, but a illustration on how to set up an io.writer
in manner to access the underlying, "written" data. This is what I hope to achieve with a NodeJS.WritableStream
implementation as well.
I tried to find a way around with an event listener, but didn't suceed yet.
I want to wrap a array, e.g. an Uint8Array
, into a NodeJS.WritableStream
so I can read its content later. How can I do that?
Background: For testing my winston logger, I want to set up a custom transport which just holds all the logged data in a manner I can evaluate it later. The example reads like this:
logger.add(new winston.transports.Stream({
stream: fs.createWriteStream('/dev/null')
/* other options */
}));
But if I create the stream like fs.createWriteStream('/dev/null')
, I cannot see its content, and it won't run under Windows (or at least I'm having issues here atm). And why would I need fs
anyway?
To give another illustration where I'm coming from, let's take this arbitraty example from Go:
buf := bytes.Buffer{}
// zerolog.New() expects an io.Writable, similar to a NodeJS.WritableStream
logger := zerolog.New(&buf)
logger.Info().Msg("Hello World")
// I can access the written data by acessing the underlying buffer
assert.Equal(t, buf.String(), "{\"level\":\"info\",\"message\":\"Hello World\"}\n")
Note this isn't a specific thing for zerolog
, but a illustration on how to set up an io.writer
in manner to access the underlying, "written" data. This is what I hope to achieve with a NodeJS.WritableStream
implementation as well.
I tried to find a way around with an event listener, but didn't suceed yet.
Share Improve this question edited Feb 5 at 10:31 NotX asked Feb 4 at 16:50 NotXNotX 2,4151 gold badge24 silver badges37 bronze badges 14 | Show 9 more comments1 Answer
Reset to default 0For completeness sake, here the solution @Bergi provided.
While it's not possible to set an underlying buffer as in Go oder Java, we work a callback to create a writable stream which can be evaluated. For that we have to create an own implementation of WritableStream
with the help of the (abstract) implementation Writable
:
import { Writable } from 'node:stream';
import * as winston from 'winston';
let logged: string = '';
const writable = new Writable({
write(chunk: Buffer, _encoding, done) {
logged += chunk.toString();
done();
},
});
// for my concrete use case
expect(logged).toEqual(winston.createLogger({transports: new winston.transports.Stream({ stream: writable })}).info('some text'))
本文标签: javascriptHow to create a writable byte stream I can evaluateStack Overflow
版权声明:本文标题:javascript - How to create a writable byte stream I can evaluate? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741752008a2395887.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
_write
, likenew Writable({ write(chunk, encoding, done) { …; done(); } })
. – Bergi Commented Feb 5 at 11:32