Skip to content

Commit

Permalink
chore: working on chapter 6
Browse files Browse the repository at this point in the history
  • Loading branch information
lmammino committed Nov 27, 2024
1 parent ecf72aa commit 101b7db
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 13 deletions.

This file was deleted.

12 changes: 12 additions & 0 deletions 06-coding-with-streams/26-readable-stream-utilities/README.md
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 06-coding-with-streams/26-readable-stream-utilities/index.js
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 06-coding-with-streams/26-readable-stream-utilities/package.json
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": {}
}
13 changes: 13 additions & 0 deletions 06-coding-with-streams/27-web-streams/README.md
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
```
34 changes: 34 additions & 0 deletions 06-coding-with-streams/27-web-streams/node-to-web.js
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))
15 changes: 15 additions & 0 deletions 06-coding-with-streams/27-web-streams/package.json
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": {}
}
35 changes: 35 additions & 0 deletions 06-coding-with-streams/27-web-streams/web-to-node.js
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)
13 changes: 13 additions & 0 deletions 06-coding-with-streams/28-stream-consumers/README.md
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
```
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()
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 06-coding-with-streams/28-stream-consumers/accumulate-http.js
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()
15 changes: 15 additions & 0 deletions 06-coding-with-streams/28-stream-consumers/package.json
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": {}
}

0 comments on commit 101b7db

Please sign in to comment.