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 25, 2024
1 parent d06c0ef commit 71bd828
Show file tree
Hide file tree
Showing 12 changed files with 364 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
results.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 20-unordered-limited-parallel-execution

This example shows how to create a limited parallel execution flow using
streams.

## Run

To run the example:

```bash
node check-urls.js urls.txt
```

Then check the content of the results file with:

```bash
cat results.txt
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createReadStream, createWriteStream } from 'node:fs'
import { createInterface } from 'node:readline'
import { pipeline } from 'node:stream/promises'
import { LimitedParallelStream } from './limited-parallel-stream.js'

const inputFile = createReadStream(process.argv[2])
const fileLines = createInterface({
input: inputFile,
})
const checkUrls = new LimitedParallelStream(
8,
async (url, _enc, push, done) => {
if (!url) {
return done()
}
try {
await fetch(url, { method: 'HEAD', timeout: 5 * 1000 })
push(`${url} is up\n`)
} catch (err) {
push(`${url} is down: ${err}\n`)
}
done()
}
)
const outputFile = createWriteStream('results.txt')

await pipeline(fileLines, checkUrls, outputFile)

console.log('All urls have been checked')
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Transform } from 'node:stream'

export class LimitedParallelStream extends Transform {
constructor(concurrency, userTransform, opts) {
super({ objectMode: true, ...opts })
this.concurrency = concurrency
this.userTransform = userTransform
this.running = 0
this.continueCb = null
this.terminateCb = null
}

_transform(chunk, enc, done) {
this.running++
this.userTransform(
chunk,
enc,
this.push.bind(this),
this._onComplete.bind(this)
)
if (this.running < this.concurrency) {
done()
} else {
this.continueCb = done
}
}

_flush(done) {
if (this.running > 0) {
this.terminateCb = done
} else {
done()
}
}

_onComplete(err) {
this.running--
if (err) {
return this.emit('error', err)
}
const tmpCb = this.continueCb
this.continueCb = null
tmpCb?.()
if (this.running === 0) {
this.terminateCb?.()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "20-unordered-limited-parallel-execution",
"version": "1.0.0",
"description": "This example shows how to create a limited parallel execution flow using streams.",
"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,50 @@
https://mario.fyi
https://loige.co
http://thiswillbedownforsure.com
https://google.com
https://youtube.com
https://facebook.com
https://twitter.com
https://instagram.com
https://linkedin.com
https://wikipedia.org
https://amazon.com
https://apple.com
https://whatsapp.com
https://tiktok.com
https://yahoo.com
https://microsoft.com
https://reddit.com
https://netflix.com
https://zoom.us
https://ebay.com
https://baidu.com
https://vk.com
https://aliexpress.com
https://bing.com
https://nytimes.com
https://cnn.com
https://bbc.com
https://spotify.com
https://adobe.com
https://wordpress.com
https://weather.com
https://tripadvisor.com
https://pinterest.com
https://github.com
https://salesforce.com
https://medium.com
https://etsy.com
https://shopify.com
https://canva.com
https://fandom.com
https://patreon.com
https://playstation.com
https://yelp.com
https://snapchat.com
https://telegram.org
https://dropbox.com
https://stackoverflow.com
https://cloudflare.com
https://weibo.com
https://duckduckgo.com
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
results.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 21-ordered-parallel-execution

This example shows how to create a limited parallel execution flow using
streams.

## Dependencies

Install all necessary dependencies with:

```bash
pnpm install
```

## Run

To run the example:

```bash
node check-urls.js urls.txt
```

For a long list of urls you can alternatively use the `500urls.txt` file!

Then check the content of the results file with:

```bash
cat results.txt
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createReadStream, createWriteStream } from 'node:fs'
import { createInterface } from 'node:readline'
import { pipeline } from 'node:stream/promises'
import parallelTransform from 'parallel-transform' // v1.2.0

const inputFile = createReadStream(process.argv[2])
const fileLines = createInterface({
input: inputFile,
})
const checkUrls = parallelTransform(8, async function (url, done) {
if (!url) {
return done()
}
try {
await fetch(url, { method: 'HEAD', timeout: 5 * 1000 })
this.push(`${url} is up\n`)
} catch (err) {
this.push(`${url} is down: ${err}\n`)
}
done()
})
const outputFile = createWriteStream('results.txt')

await pipeline(fileLines, checkUrls, outputFile)

console.log('All urls have been checked')
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "21-ordered-parallel-execution",
"version": "1.0.0",
"description": "This example shows how to create a limited parallel execution flow using streams.",
"type": "module",
"scripts": {},
"engines": {
"node": ">=22"
},
"engineStrict": true,
"keywords": [],
"author": "Luciano Mammino and Mario Casciaro",
"license": "MIT",
"dependencies": {
"parallel-transform": "^1.2.0"
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
https://mario.fyi
https://loige.co
http://thiswillbedownforsure.com
https://google.com
https://youtube.com
https://facebook.com
https://twitter.com
https://instagram.com
https://linkedin.com
https://wikipedia.org
https://amazon.com
https://apple.com
https://whatsapp.com
https://tiktok.com
https://yahoo.com
https://microsoft.com
https://reddit.com
https://netflix.com
https://zoom.us
https://ebay.com
https://baidu.com
https://vk.com
https://aliexpress.com
https://bing.com
https://nytimes.com
https://cnn.com
https://bbc.com
https://spotify.com
https://adobe.com
https://wordpress.com
https://weather.com
https://tripadvisor.com
https://pinterest.com
https://github.com
https://salesforce.com
https://medium.com
https://etsy.com
https://shopify.com
https://canva.com
https://fandom.com
https://patreon.com
https://playstation.com
https://yelp.com
https://snapchat.com
https://telegram.org
https://dropbox.com
https://stackoverflow.com
https://cloudflare.com
https://weibo.com
https://duckduckgo.com

0 comments on commit 71bd828

Please sign in to comment.