-
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
19 changed files
with
287 additions
and
1 deletion.
There are no files selected for viewing
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...h-streams/05-readabl-flowing/package.json → ...-streams/05-readable-flowing/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
File renamed without changes.
23 changes: 23 additions & 0 deletions
23
06-coding-with-streams/06-readable-async-iterator/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,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
15
06-coding-with-streams/06-readable-async-iterator/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": "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": {} | ||
} |
6 changes: 6 additions & 0 deletions
6
06-coding-with-streams/06-readable-async-iterator/read-stdin.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,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') |
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,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 | ||
``` |
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,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`) | ||
}) |
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,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" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
06-coding-with-streams/07-custom-readable/random-stream.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,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) | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
06-coding-with-streams/07-custom-readable/simplified-construction.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,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
12
06-coding-with-streams/08-custom-readable-from-iterable/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 @@ | ||
# 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
14
06-coding-with-streams/08-custom-readable-from-iterable/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,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`) | ||
}) |
15 changes: 15 additions & 0 deletions
15
06-coding-with-streams/08-custom-readable-from-iterable/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": "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
27
06-coding-with-streams/09-writable-http-entropy-server/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,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/ | ||
``` |
17 changes: 17 additions & 0 deletions
17
06-coding-with-streams/09-writable-http-entropy-server/entropy-server.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,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') | ||
}) |
17 changes: 17 additions & 0 deletions
17
06-coding-with-streams/09-writable-http-entropy-server/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,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" | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
06-coding-with-streams/09-writable-http-entropy-server/pnpm-lock.yaml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.