Created
June 1, 2025 20:05
-
-
Save fabiospampinato/9670b7fe91f9f77ac2610f8dc6e80b00 to your computer and use it in GitHub Desktop.
Transparent worker pool demo, with WorkTank and Esbuild.
This file contains hidden or 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
/* IMPORT */ | |
import esbuild from 'esbuild'; | |
import worktank from 'worktank-esbuild-plugin'; | |
/* MAIN */ | |
esbuild.build ({ | |
bundle: true, | |
minify: true, | |
format: 'esm', | |
platform: 'node', | |
target: 'es2018', | |
entryPoints: ['example.ts'], | |
outdir: 'dist', | |
plugins: [ | |
worktank ({ | |
filter: /\.worker\.(js|ts)$/ | |
}) | |
] | |
}); |
This file contains hidden or 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
// Instructions: | |
// Install dependencies: npm install | |
// Compile the example with esbuild: node build.js | |
// Run the compiled example: node dist/example.js | |
/* IMPORT */ | |
import {id, sum} from './example.worker'; | |
import process from 'node:process'; | |
/* MAIN */ | |
const test = async (): Promise<void> => { | |
console.assert ( await sum ( 10, 5 ) === 15 ); | |
const ids = await Promise.all ([ id (), id (), id () ]); | |
console.log ( 'IDs:', ids ); | |
process.exit (); | |
}; | |
/* RUN */ | |
test (); |
This file contains hidden or 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
/* IMPORT */ | |
import {threadId} from 'worker_threads'; | |
/* MAIN */ | |
const id = async (): Promise<number> => threadId; | |
const sum = async ( a: number, b: number ) => a + b; | |
/* EXPORT */ | |
export {id, sum}; |
This file contains hidden or 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
{ | |
"private": true, | |
"name": "test", | |
"type": "module", | |
"scripts": { | |
"compile": "node build.js" | |
}, | |
"dependencies": { | |
"worktank": "^3.0.2" | |
}, | |
"devDependencies": { | |
"@types/node": "^22.15.29", | |
"esbuild": "^0.25.5", | |
"worktank-esbuild-plugin": "^2.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment