Something went wrong. Try again.
Offload functions to worker threads with shared memory primitives for Node.js.
Something went wrong. Try again.
1.1 kB · 35 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536// 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<Task<FileHash>> { 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)); } }}