MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1const { Transform } = require('node:stream');
2
3async function main() {
4 let output = '';
5
6 const tr = new Transform({
7 transform(chunk, encoding, cb) {
8 Promise.resolve().then(() => {
9 cb(null, chunk.toString('utf8').toUpperCase());
10 });
11 },
12 });
13
14 tr.on('data', (chunk) => {
15 output += chunk.toString('utf8');
16 });
17
18 const finished = new Promise((resolve, reject) => {
19 tr.on('finish', resolve);
20 tr.on('error', reject);
21 });
22
23 tr.write('abc');
24 tr.end();
25 await finished;
26
27 if (output !== 'ABC') {
28 throw new Error(`expected "ABC", got ${JSON.stringify(output)}`);
29 }
30
31 console.log('stream Transform async callback preserves receiver state');
32}
33
34main().catch((err) => {
35 console.error(err && err.stack ? err.stack : String(err));
36 process.exit(1);
37});