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 23, 2024
1 parent 2b1818d commit 45aa628
Show file tree
Hide file tree
Showing 19 changed files with 287 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "05-readabl-flowing",
"name": "05-readable-flowing",
"version": "1.0.0",
"description": "This example shows how to consume a readable stream in flowing mode.",
"type": "module",
Expand Down
23 changes: 23 additions & 0 deletions 06-coding-with-streams/06-readable-async-iterator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 06-readable-async-iterator

This examples shows how to consume a readable stream using the async iterator
interface.

## Run

To run the example you can use:

```bash
node read-stdin.js
```

Now write in the standard input and use `ctrl+d` (unix) or `ctrl+z` (windows) to
end the input stream.

Alternatively:

```bash
cat <path_of_file> | node read-stdin.js
```

to consume an arbitrary file.
15 changes: 15 additions & 0 deletions 06-coding-with-streams/06-readable-async-iterator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "06-readable-async-iterator",
"version": "1.0.0",
"description": "This examples shows how to consume a readable stream using the async iterator interface.",
"type": "module",
"scripts": {},
"engines": {
"node": ">=22"
},
"engineStrict": true,
"keywords": [],
"author": "Luciano Mammino and Mario Casciaro",
"license": "MIT",
"dependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
for await (const chunk of process.stdin) {
console.log('New data available')
console.log(`Chunk read (${chunk.length} bytes): "${chunk.toString()}"`)
}

console.log('End of stream')
25 changes: 25 additions & 0 deletions 06-coding-with-streams/07-custom-readable/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 07-custom-readable

This example shows how to implement a custom Readable stream.

## Dependencies

Install all necessary dependencies with:

```bash
npm install
```

## Run

To run the example you can run:

```bash
node index.js
```

or:

```bash
node simplified-construction.js
```
10 changes: 10 additions & 0 deletions 06-coding-with-streams/07-custom-readable/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { RandomStream } from './random-stream.js'

const randomStream = new RandomStream()
randomStream
.on('data', chunk => {
console.log(`Chunk received (${chunk.length} bytes): ${chunk.toString()}`)
})
.on('end', () => {
console.log(`Produced ${randomStream.emittedBytes} bytes of random data`)
})
17 changes: 17 additions & 0 deletions 06-coding-with-streams/07-custom-readable/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "07-custom-readable",
"version": "1.0.0",
"description": "This example shows how to implement a custom Readable stream.",
"type": "module",
"scripts": {},
"engines": {
"node": ">=22"
},
"engineStrict": true,
"keywords": [],
"author": "Luciano Mammino and Mario Casciaro",
"license": "MIT",
"dependencies": {
"chance": "^1.1.12"
}
}
22 changes: 22 additions & 0 deletions 06-coding-with-streams/07-custom-readable/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions 06-coding-with-streams/07-custom-readable/random-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Readable } from 'node:stream'
import Chance from 'chance' // v1.1.12

const chance = new Chance()

export class RandomStream extends Readable {
constructor(options) {
super(options)
this.emittedBytes = 0
}

_read(size) {
const chunk = chance.string({ length: size })
this.push(chunk, 'utf8')
this.emittedBytes += chunk.length
if (chance.bool({ likelihood: 5 })) {
this.push(null)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Readable } from 'node:stream'
import Chance from 'chance' // v1.1.12

const chance = new Chance()
let emittedBytes = 0

const randomStream = new Readable({
read(size) {
const chunk = chance.string({ length: size })
this.push(chunk, 'utf8')
emittedBytes += chunk.length
if (chance.bool({ likelihood: 5 })) {
this.push(null)
}
},
})

randomStream
.on('data', chunk => {
console.log(`Chunk received (${chunk.length} bytes): ${chunk.toString()}`)
})
.on('end', () => {
console.log(`Produced ${emittedBytes} bytes of random data`)
})
12 changes: 12 additions & 0 deletions 06-coding-with-streams/08-custom-readable-from-iterable/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 08-custom-readable-from-iterable

This example shows how to implement a custom Readable stream using
`Readable.from`.

## Run

To run the example you can run:

```bash
node index.js
```
14 changes: 14 additions & 0 deletions 06-coding-with-streams/08-custom-readable-from-iterable/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Readable } from 'node:stream'

const mountains = [
{ name: 'Everest', height: 8848 },
{ name: 'K2', height: 8611 },
{ name: 'Kangchenjunga', height: 8586 },
{ name: 'Lhotse', height: 8516 },
{ name: 'Makalu', height: 8481 },
]

const mountainsStream = Readable.from(mountains)
mountainsStream.on('data', mountain => {
console.log(`${mountain.name.padStart(14)}\t${mountain.height}m`)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "08-custom-readable-from-iterable",
"version": "1.0.0",
"description": "This example shows how to implement a custom Readable stream using `Readable.from`.",
"type": "module",
"scripts": {},
"engines": {
"node": ">=22"
},
"engineStrict": true,
"keywords": [],
"author": "Luciano Mammino and Mario Casciaro",
"license": "MIT",
"dependencies": {}
}
27 changes: 27 additions & 0 deletions 06-coding-with-streams/09-writable-http-entropy-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 09-writable-http-entropy-server

This example shows how to write into a readable stream, specifically an HTTP
response sent by a server.

## Dependencies

Install all necessary dependencies with:

```bash
npm install
```

## Run

To run the server:

```bash
node entropy-server.js
```

Now you can make requests to the server by pointing your browser to
[http://localhost:3000] or with curl as follows:

```bash
curl -i -vvv http://localhost:3000/
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createServer } from 'node:http'
import Chance from 'chance' // 1.1.12

const chance = new Chance()

const server = createServer((_req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
do {
res.write(`${chance.string()}\n`)
} while (chance.bool({ likelihood: 95 }))
res.end('\n\n')
res.on('finish', () => console.log('All data sent'))
})

server.listen(3000, () => {
console.log('listening on http://localhost:3000')
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "09-writable-http-entropy-server",
"version": "1.0.0",
"description": "This example shows how to write into a readable stream, specifically an HTTP response sent by a server.",
"type": "module",
"scripts": {},
"engines": {
"node": ">=22"
},
"engineStrict": true,
"keywords": [],
"author": "Luciano Mammino and Mario Casciaro",
"license": "MIT",
"dependencies": {
"chance": "^1.1.12"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 45aa628

Please sign in to comment.