// Walk a directory tree with a recursive async generator and hash every file // using map() with bounded concurrency. Results stream back in completion order. // Requires Node v24+. // // Run: node examples/bounded-map/main.ts [dir] import { readdir } from 'node:fs/promises'; import { join } from 'node:path'; import { workers, map, inert } from '../../src/index.ts'; import type { Task } from '../../src/index.ts'; import { hashFile, type FileHash } from './hash-file.ts'; const root = process.argv[2] ?? './src'; { using run = workers(); const start = performance.now(); let count = 0; for await (const { path, hash } of map(run, walk(root), { concurrency: 4 })) { console.log(`${hash.slice(0, 12)} ${path}`); count++; } console.log(`\nhashed ${count} files in ${(performance.now() - start).toFixed(0)}ms`); } async function* walk(dir: string): AsyncGenerator> { for (const entry of await readdir(dir, { withFileTypes: true })) { const p = join(dir, entry.name); if (entry.isDirectory()) { yield* walk(p); } else { yield inert(hashFile(p)); } } }