This project demonstrates the completion of several tasks on Node.js basics.
The project was completed as part of the RS School NodeJS 2024 Q3 course.
The project contains several nested folders inside src, with the necessary functionality implemented within them.
The output results are displayed in the terminal, with highlights in color and formatted for easy readability.
During the testing and execution of functions, the content of the project changes; some files are renamed, deleted, or their content is modified. A script has been added to the project to restore it to its original state.
- Use 22.x.x version (22.9.0 or upper) of Node.js.
- For convenient function execution, npm-scripts are defined in
package.jsonto run each subtask. - To return the project to its original state, run the
restore.jsscript.
Implemented several functions in dedicated files:
create.js- Function that creates a new filefresh.txtwith contentI am fresh and younginside thefilesfolder. If the file already exists, anErrorwith the messageFS operation failedis thrown.copy.js- Function that copies thefilesfolder with all its content into thefiles_copyfolder at the same level. If thefilesfolder doesn't exist orfiles_copyhas already been created, anErrorwith the messageFS operation failedis thrown.rename.js- Function that renames the filewrongFilename.txttoproperFilename.md. If there's no filewrongFilename.txtorproperFilename.mdalready exists, anErrorwith the messageFS operation failedis thrown.delete.js- Function that deletes the filefileToRemove.txt. If there's no filefileToRemove.txt, anErrorwith the messageFS operation failedis thrown.list.js- Function that prints an array of filenames from thefilesfolder into the console. If thefilesfolder doesn't exist, anErrorwith the messageFS operation failedis thrown.read.js- Function that prints the content of thefileToRead.txtinto the console. If there's no filefileToRead.txt, anErrorwith the messageFS operation failedis thrown.
Implemented several functions in dedicated files:
env.js- Function that parses environment variables with the prefixRSS_and prints them to the console in the formatRSS_name1=value1; RSS_name2=value2.args.js- Function that parses command line arguments (given in the format--propName value --prop2Name value2) and prints them to the console in the formatpropName is value, prop2Name is value2.
Refactored file cjsToEsm.cjs and renamed to esm.mjs:
esm.mjs- Rewritten to its equivalent ofcjsToEsm.cjsin ECMAScript notation.
Implemented several functions in dedicated files:
calcHash.js- Function that calculates the SHA256 hash for the filefileToCalculateHashFor.txtand logs it into the console ashexusing the Streams API.
Implemented several functions in dedicated files:
read.js- Function that reads the content of the filefileToRead.txtusing a Readable Stream and prints its content intoprocess.stdout.write.js- Function that writesprocess.stdindata into the filefileToWrite.txtusing a Writable Stream.transform.js- Function that reads data fromprocess.stdin, reverses the text using a Transform Stream, and then writes it intoprocess.stdout.
Implemented several functions in dedicated files:
compress.js- Function that compresses the filefileToCompress.txttoarchive.gzusingzliband the Streams API.decompress.js- Function that decompressesarchive.gzback tofileToCompress.txtwith the same content as before compression usingzliband the Streams API.
Implemented several functions in dedicated files:
worker.js- Extended the given function to work with data received from the main thread and implemented a function that sends the result of the computation to the main thread.main.js- Function that creates a number of worker threads (equal to the number of host machine logical CPU cores) from the fileworker.jsand is able to send data to those threads and receive the result of the computation from them. Incremental numbers starting from10are sent to eachworker. For example, on a host machine with 4 cores, 4 workers are created and10is sent to the firstworker,11to the secondworker,12to the thirdworker, and13to the fourthworker. After all workers finish, the function logs an array of results into the console. The results are an array of objects with 2 properties:status-'resolved'in case of successfully received value fromworkeror'error'in case of error inworker.data- value fromworkerin case of success ornullin case of error in the worker.
The results in the array are in the same order that the workers were created.
Implemented several functions in dedicated files:
cp.js- FunctionspawnChildProcessthat receives an array of argumentsargsand creates a child process from the filescript.js, passing theseargsto it. This function creates an IPC-channel betweenstdinandstdoutof the master process and the child process:- The child process
stdinreceives input from the master processstdin. - The child process
stdoutsends data to the master processstdout.
- The child process