Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from "fs";
import {pipeline} from "stream/promises";
import {dirname} from "../utils/dir.js";

const read = async () => {
// Write your code here
const __dirname = dirname(import.meta.url);
const readStream = fs.createReadStream(`${__dirname}/files/fileToRead.txt`);

try {
await pipeline(
readStream,
process.stdout
);
} catch (error) {
console.log('Stream error occurred');
}
};

await read();
25 changes: 24 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import { Transform } from 'stream';
import { pipeline } from 'stream/promises';

const transform = async () => {
// Write your code here
const transformStream = new Transform({
transform(chunk, encoding, callback) {
const reversedStr = chunk.toString()
.trim()
.split('')
.reverse()
.join('');

callback(null, `${reversedStr}\n`);
}
});

try {
await pipeline(
process.stdin,
transformStream,
process.stdout
);
} catch (error) {
console.log('Stream error occurred');
}
};

await transform();
16 changes: 15 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from "fs";
import {pipeline} from "stream/promises";
import {dirname} from "../utils/dir.js";

const write = async () => {
// Write your code here
const __dirname = dirname(import.meta.url);
const writeStream = fs.createWriteStream(`${__dirname}/files/fileToWrite.txt`);

try {
await pipeline(
process.stdin,
writeStream
);
} catch (error) {
console.log('Stream error occurred');
}
};

await write();