Exercises
- 6.1 Data compression efficiency: Write a command-line script that takes a file as input and compresses it using the different algorithms available in the
zlib
module (Brotli, Deflate, Gzip). You want to produce a summary table that compares the algorithm’s compression time and compression efficiency on the given file. Hint: This could be a good use case for the fork pattern, but remember that we made some important performance considerations when we discussed it earlier in this chapter. - 6.2 Stream data processing: On Kaggle, you can find a lot of interesting datasets, such as London Crime Data (nodejsdp.link/london-crime). You can download the data in CSV format and build a stream processing script that analyzes the data and tries to answer the following questions:
- Did the number of crimes go up or down over the years?
- What are the most dangerous areas of London?
- What is the most common crime per area?
- What is the least common crime?
Hint: You can use a combination of Transform
streams and PassThrough
streams to parse and observe the data as it is flowing. Then, you can build in-memory aggregations for the data, which can help you answer the preceding questions. Also, you don’t need to do everything in one pipeline; you could build very specialized pipelines (for example, one per question) and use the fork pattern to distribute the parsed data across them.
- 6.3 File share over TCP: Build a client and a server to transfer files over TCP. Extra points if you add a layer of encryption on top of that and if you can transfer multiple files at once. Once you have your implementation ready, give the client code and your IP address to a friend or a colleague, then ask them to send you some files! Hint: You could use mux/demux to receive multiple files at once.
- 6.4 Animations with
Readable
streams: Did you know you can create amazing terminal animations with justReadable
streams? Well, to understand what we are talking about here, try to runcurl parrot.live
in your terminal and see what happens! If you think that this is cool, why don’t you try to create something similar? Hint: If you need some help with figuring out how to implement this, you can check out the actual source code ofparrot.live
by simply accessing its URL through your browser.