read a directory and write the output into a file using streams
Hello,
As a small personal challenge i am trying to use streams for:
1.read a directory (__dirname in here)
2.transform the output as upperCase
3.write the transfomed output into a file
I found this solution, but i doubt that it is the best practice... May someone confirm ? or correct ?
const fs = require('fs')
const { Transform, pipeline, Readable } = require('stream')
const streamUpper = () => {
return new Transform({
transform(chunk, enc, next) {
next(null, chunk.toString().toUpperCase())
}
})
}
async function readDirStream() {
const dir = await fs.promises.opendir(__dirname)
for await (const dirent of dir) {
await require('util').promisify(pipeline)(
Readable.from(dirent.name+'\n'),
streamUpper(),
fs.createWriteStream('./fileList.txt', {flags:'a'})
)
}
}
readDirStream().catch(e => console.error(e))
Comments
-
I just notice that the Readable.from() (line 17) is useless, that is enought
await require('util').promisify(pipeline)( dirent.name+'\n', streamUpper(), fs.createWriteStream('./fileList.txt', {flags:'a'}) )So it makes me try the course exemple "Reading Directories (.cont)", as there is the same Readable.from() consuming the output from opendir(). And it works without (Readable.from()) as well. I guess it makes sense as opendir() return already a stream, am i right ?
exemple from the course without Readable.from()
opendir(__dirname, (err, dir) => { if (err) { res.statusCode = 500 res.end('Server Error') return } // const dirStream = Readable.from(dir) const entryStream = createEntryStream() res.setHeader('Content-Type', 'application/json') pipeline(dir, entryStream, res, (err) => { if (err) console.error(err) }) })0 -
- Avoid doing inline
require- always putrequireat the top Readable.fromis a convenient way to turn a data structure (string, array) into a readable stream- It's not needed in your directory processing example, and really neither is the transform stream
- A better example to use with your transform stream would be a TCP socket:
const { Transform, pipeline } = require('stream') const streamUpper = () => { return new Transform({ transform(chunk, enc, next) { next(null, chunk.toString().toUpperCase()) } }) } const { createServer } = require('net') createServer((socket) => { pipeline(socket, streamUpper(), socket, (err) => { if (err) console.error(err) else console.log('socket closed') }) }).listen(9999)Readable.fromwould be useful for testing your stream:const { Transform, Readable } = require('stream') const assert = require('assert').strict const streamUpper = () => { return new Transform({ transform(chunk, enc, next) { next(null, chunk.toString().toUpperCase()) } }) } const upper = streamUpper() Readable.from(['test-a', 'test-b']).pipe(upper) upper.once('data', (first) => { assert.equal(first, 'TEST-A') upper.once('data', (second) { assert.equal(first, 'TEST-B') }) })1 - Avoid doing inline
-
Hello David,
Thank you for the answer, i will work on that. Thank you.
1
Categories
- All Categories
- 175 LFX Mentorship
- 175 LFX Mentorship: Linux Kernel
- 745 Linux Foundation IT Professional Programs
- 372 Cloud Engineer IT Professional Program
- 168 Advanced Cloud Engineer IT Professional Program
- 73 DevOps IT Professional Program - Discontinued
- 3 DevOps & GitOps IT Professional Program
- 98 Cloud Native Developer IT Professional Program
- 7.6K Training Courses & Learning Paths
- AI & ML Training
- Blockchain & Decentralized Identity Training
- Cloud & Containers Training
- Cybersecurity Training
- DevOps & Site-Reliability Training
- Linux Kernel Development Training
- Networking Training
- Open Source Best Practice Training
- System Administration Training
- System Engineering Training
- Web & Application Development Training
- 2 LFD103-JP クラス フォーラム
- 4 LFD210-CN Class Forum
- 764 LFD259 Class Forum
- 681 LFS101 Class Forum
- 2 LFS158-JP クラス フォーラム
- 162 LFS207 Class Forum
- 3 LFS207-DE-Klassenforum
- 4 LFS207-JP クラス フォーラム
- 61 LFS241 Class Forum
- 52 LFS242 Class Forum
- 42 LFS243 Class Forum
- 19 LFS244 Class Forum
- 4 LFS250-JP クラス フォーラム
- 166 LFS253 Class Forum
- 19 LFS256 Class Forum
- 1.4K LFS258 Class Forum
- 165 LFS261 Class Forum
- 26 LFS267 Class Forum
- 792 Hardware
- 202 Drivers
- 68 I/O Devices
- 37 Monitors
- 95 Multimedia
- 173 Networking
- 91 Printers & Scanners
- 87 Storage
- 768 Linux Distributions
- 81 Debian
- 67 Fedora
- 22 Linux Mint
- 13 Mageia
- 24 openSUSE
- 150 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 465 Linux System Administration
- 31 Cloud Computing
- 73 Command Line/Scripting
- Github systems admin projects
- 98 Linux Security
- 78 Network Management
- 101 System Management
- 46 Web Management
- 106 Mobile Computing
- 18 Android
- 73 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 392 Off Topic
- 121 Introductions
- 181 Small Talk
- 29 Study Material
- 944 Programming and Development
- 310 Kernel Development
- 616 Software Development
- 977 Software
- 369 Applications
- 182 Command Line
- 5 Compiling/Installing
- 68 Games
- 317 Installation
- Archived
- 2 LFD140 Class Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)
