-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
199 additions
and
13 deletions.
There are no files selected for viewing
13 changes: 0 additions & 13 deletions
13
06-coding-with-streams/13-transform-filter-reduce/package-lock.json
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
06-coding-with-streams/26-readable-stream-utilities/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
Binary file not shown.
21 changes: 21 additions & 0 deletions
21
06-coding-with-streams/26-readable-stream-utilities/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
15 changes: 15 additions & 0 deletions
15
06-coding-with-streams/26-readable-stream-utilities/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
11 changes: 11 additions & 0 deletions
11
06-coding-with-streams/28-stream-consumers/accumulate-http-consumers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
3 changes: 3 additions & 0 deletions
3
06-coding-with-streams/28-stream-consumers/accumulate-http-fetch.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const res = await fetch('https://jsonplaceholder.typicode.com/todos/1') | ||
const data = await res.json() | ||
console.log(data) |
12 changes: 12 additions & 0 deletions
12
06-coding-with-streams/28-stream-consumers/accumulate-http.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": {} | ||
} |