diff --git a/06-coding-with-streams/13-transform-filter-reduce/package-lock.json b/06-coding-with-streams/13-transform-filter-reduce/package-lock.json deleted file mode 100644 index d05d627..0000000 --- a/06-coding-with-streams/13-transform-filter-reduce/package-lock.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "13-transform-filter-reduce", - "version": "1.0.0", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "csv-parse": { - "version": "4.10.1", - "resolved": "https://registry.npmjs.org/csv-parse/-/csv-parse-4.10.1.tgz", - "integrity": "sha512-gdDJVchi0oSLIcYXz1H/VSgLE6htHDqJyFsRU/vTkQgmVOZ3S0IR2LXnNbWUYG7VD76dYVwdfBLyx8AX9+An8A==" - } - } -} diff --git a/06-coding-with-streams/26-readable-stream-utilities/README.md b/06-coding-with-streams/26-readable-stream-utilities/README.md new file mode 100644 index 0000000..95ba38b --- /dev/null +++ b/06-coding-with-streams/26-readable-stream-utilities/README.md @@ -0,0 +1,12 @@ +# 26-readable-stream-utilities + +This example shows how to use Readable stream util methods such as `.map()`, +`.filter()`, and `reduce()`. + +## Run + +To run the example: + +```bash +node index.js +``` diff --git a/06-coding-with-streams/26-readable-stream-utilities/data.csv.gz b/06-coding-with-streams/26-readable-stream-utilities/data.csv.gz new file mode 100644 index 0000000..6fb81d6 Binary files /dev/null and b/06-coding-with-streams/26-readable-stream-utilities/data.csv.gz differ diff --git a/06-coding-with-streams/26-readable-stream-utilities/index.js b/06-coding-with-streams/26-readable-stream-utilities/index.js new file mode 100644 index 0000000..f5180f9 --- /dev/null +++ b/06-coding-with-streams/26-readable-stream-utilities/index.js @@ -0,0 +1,21 @@ +import { createReadStream } from 'node:fs' +import { createInterface } from 'node:readline' +import { Readable, compose } from 'node:stream' +import { createGunzip } from 'node:zlib' + +const uncompressedData = compose( + createReadStream('data.csv.gz'), + createGunzip() +) +const byLine = Readable.from(createInterface({ input: uncompressedData })) + +const totalProfit = await byLine + .drop(1) + .map(chunk => { + const [type, country, profit] = chunk.toString().split(',') + return { type, country, profit: Number.parseFloat(profit) } + }) + .filter(record => record.country === 'Italy') + .reduce((acc, record) => acc + record.profit, 0) + +console.log(totalProfit) diff --git a/06-coding-with-streams/26-readable-stream-utilities/package.json b/06-coding-with-streams/26-readable-stream-utilities/package.json new file mode 100644 index 0000000..ffd9b8b --- /dev/null +++ b/06-coding-with-streams/26-readable-stream-utilities/package.json @@ -0,0 +1,15 @@ +{ + "name": "26-readable-stream-utilities", + "version": "1.0.0", + "description": "This example shows how to use Readable stream util methods such as `.map()`, `.filter()`, and `reduce()`", + "type": "module", + "scripts": {}, + "engines": { + "node": ">=22" + }, + "engineStrict": true, + "keywords": [], + "author": "Luciano Mammino and Mario Casciaro", + "license": "MIT", + "dependencies": {} +} diff --git a/06-coding-with-streams/27-web-streams/README.md b/06-coding-with-streams/27-web-streams/README.md new file mode 100644 index 0000000..837d0b9 --- /dev/null +++ b/06-coding-with-streams/27-web-streams/README.md @@ -0,0 +1,13 @@ +# 27-web-streams + +This example demonstrates interoperability between Node.js streams and Web +Streams. + +## Run + +To run the examples: + +```bash +node node-to-web.js +node web-to-node.js +``` diff --git a/06-coding-with-streams/27-web-streams/node-to-web.js b/06-coding-with-streams/27-web-streams/node-to-web.js new file mode 100644 index 0000000..0da7bad --- /dev/null +++ b/06-coding-with-streams/27-web-streams/node-to-web.js @@ -0,0 +1,34 @@ +import { Readable, Transform, Writable } from 'node:stream' + +const nodeReadable = new Readable({ + read() { + this.push('Hello, ') + this.push('world!') + this.push(null) + }, +}) + +const webReadable = Readable.toWeb(nodeReadable) +console.log(webReadable) + +const nodeWritable = new Writable({ + write(chunk, _enc, cb) { + console.log(chunk.toString()) + cb() + }, +}) + +const webWritable = Writable.toWeb(nodeWritable) +console.log(webWritable) + +const nodeTransform = new Transform({ + transform(chunk, _enc, cb) { + cb(null, chunk.toString().toUpperCase()) + }, +}) + +const webTransform = Transform.toWeb(nodeTransform) +console.log(webTransform) + +nodeReadable.pipe(process.stdout) +webReadable.pipeTo(Writable.toWeb(process.stdout)) diff --git a/06-coding-with-streams/27-web-streams/package.json b/06-coding-with-streams/27-web-streams/package.json new file mode 100644 index 0000000..6904328 --- /dev/null +++ b/06-coding-with-streams/27-web-streams/package.json @@ -0,0 +1,15 @@ +{ + "name": "27-web-streams", + "version": "1.0.0", + "description": "This example demonstrates interoperability between Node.js streams and Web Streams", + "type": "module", + "scripts": {}, + "engines": { + "node": ">=22" + }, + "engineStrict": true, + "keywords": [], + "author": "Luciano Mammino and Mario Casciaro", + "license": "MIT", + "dependencies": {} +} diff --git a/06-coding-with-streams/27-web-streams/web-to-node.js b/06-coding-with-streams/27-web-streams/web-to-node.js new file mode 100644 index 0000000..c4a0f8a --- /dev/null +++ b/06-coding-with-streams/27-web-streams/web-to-node.js @@ -0,0 +1,35 @@ +import { Readable, Transform, Writable } from 'node:stream' +import { + ReadableStream, + TransformStream, + WritableStream, +} from 'node:stream/web' + +const webReadable = new ReadableStream({ + start(controller) { + controller.enqueue('Hello, ') + controller.enqueue('world!') + controller.close() + }, +}) + +const nodeReadable = Readable.fromWeb(webReadable) +console.log(nodeReadable) + +const webWritable = new WritableStream({ + write(chunk) { + console.log(chunk) + }, +}) + +const nodeWritable = Writable.fromWeb(webWritable) +console.log(nodeWritable) + +const webTransform = new TransformStream({ + transform(chunk, controller) { + controller.enqueue(chunk.toString().toUpperCase()) + }, +}) + +const nodeTransform = Transform.fromWeb(webTransform) +console.log(nodeTransform) diff --git a/06-coding-with-streams/28-stream-consumers/README.md b/06-coding-with-streams/28-stream-consumers/README.md new file mode 100644 index 0000000..770ed7c --- /dev/null +++ b/06-coding-with-streams/28-stream-consumers/README.md @@ -0,0 +1,13 @@ +# 28-stream-consumers + +This example demonstrates how to use Node.js stream consumers. + +## Run + +To run the examples: + +```bash +node accumulate-http.js +node accumulate-http-consumers.js +node accumulate-http-fetch.js +``` diff --git a/06-coding-with-streams/28-stream-consumers/accumulate-http-consumers.js b/06-coding-with-streams/28-stream-consumers/accumulate-http-consumers.js new file mode 100644 index 0000000..0f00df9 --- /dev/null +++ b/06-coding-with-streams/28-stream-consumers/accumulate-http-consumers.js @@ -0,0 +1,11 @@ +import { request } from 'node:https' +import consumers from 'node:stream/consumers' + +const req = request( + 'https://jsonplaceholder.typicode.com/todos/1', + async res => { + const data = await consumers.json(res) + console.log(data) + } +) +req.end() diff --git a/06-coding-with-streams/28-stream-consumers/accumulate-http-fetch.js b/06-coding-with-streams/28-stream-consumers/accumulate-http-fetch.js new file mode 100644 index 0000000..f5ab2b1 --- /dev/null +++ b/06-coding-with-streams/28-stream-consumers/accumulate-http-fetch.js @@ -0,0 +1,3 @@ +const res = await fetch('https://jsonplaceholder.typicode.com/todos/1') +const data = await res.json() +console.log(data) diff --git a/06-coding-with-streams/28-stream-consumers/accumulate-http.js b/06-coding-with-streams/28-stream-consumers/accumulate-http.js new file mode 100644 index 0000000..b63852d --- /dev/null +++ b/06-coding-with-streams/28-stream-consumers/accumulate-http.js @@ -0,0 +1,12 @@ +import { request } from 'node:https' + +const req = request('https://jsonplaceholder.typicode.com/todos/1', res => { + let data = '' + res.on('data', chunk => { + data += chunk + }) + res.on('end', () => { + console.log(JSON.parse(data)) + }) +}) +req.end() diff --git a/06-coding-with-streams/28-stream-consumers/package.json b/06-coding-with-streams/28-stream-consumers/package.json new file mode 100644 index 0000000..64d7541 --- /dev/null +++ b/06-coding-with-streams/28-stream-consumers/package.json @@ -0,0 +1,15 @@ +{ + "name": "28-stream-consumers", + "version": "1.0.0", + "description": "This example demonstrates how to use Node.js stream consumers", + "type": "module", + "scripts": {}, + "engines": { + "node": ">=22" + }, + "engineStrict": true, + "keywords": [], + "author": "Luciano Mammino and Mario Casciaro", + "license": "MIT", + "dependencies": {} +}