Command Palette

Search for a command to run...

Back to skills
skill № 41366

nodejs-patterns

Use when the user asks about Node.js streams and backpressure, stream.pipeline, worker threads, typed EventEmitter, AbortController, bounded Promise…

FreeNo executable scripts
j4flmaoby j4flmao★ 47 GitHub stars

Install nodejs-patterns in Claude Code & Claude Desktop

Sign up to install this skill

Create a free account to reveal the install command and save the skill to your library.

  • Reveal the one-line install command
  • Save skills to your synced library
  • Get notified when skills update
Sign up freeI already have an account

About this skill

Node.js Patterns

Purpose

Implement production-grade Node.js patterns — streams with backpressure, typed event emitters, worker threads for CPU-bound work, DI containers, error classification, and async flow control.

Agent Protocol

Trigger

User request includes: Node.js stream, Node.js event emitter, Node.js worker thread, Node.js DI, Node.js error pattern, Node.js async, Node.js backpressure, Node.js event loop.

Input Context

  • Node.js version (18+, 20+, 22+)
  • Pattern needed (Streams, Workers, DI, Events, Errors)
  • Runtime constraints (memory, CPU, concurrency)

Output Artifact

Code examples for the requested pattern. Compress output — no preamble, no postamble.

Response Format

Pattern: {streams | worker pool | DI | typed events | error hierarchy | async flow}
Why: {constraint it addresses}
Code: {minimal complete TypeScript}
Pitfalls: {backpressure, leaks, unhandled rejections}

Completion Criteria

  • Stream pipeline with backpressure handling
  • Typed event emitter with TypeScript generics
  • Worker thread pool with message protocol
  • DI container with scoped lifetimes
  • Error classification hierarchy with Zod validation

Max Response Length

4096 tokens

Architecture Decision Trees

Streams vs Buffers vs Generators

Criterion Streams Buffers Async Generators
Memory Constant (chunked) Full dataset Constant (lazy)
Backpressure Built-in Manual Pull-based (automatic)
Piping .pipe() or pipeline() Manual buffer copy for await...of
Error handling pipeline() with callback try/catch try/catch in loop
Use case Large files, network IO Small payloads, transforms Lazy iteration

Decision: Large data processing → Streams. In-memory transforms → Buffers. Pull-based iteration → Async Generators.

Worker Threads vs Cluster vs Child Process

Criterion Worker Threads Cluster Child Process
Shared memory SharedArrayBuffer No No
IPC Message passing Built-in HTTP round-robin stdio/pipe
Use case CPU-bound tasks HTTP load balancing System commands
Module isolation Same module Same code Separate binary
Overhead Low Medium High

Decision: CPU-bound parallel computation → Worker Threads. Multi-core HTTP serving → Cluster. Running external commands → Child Process.

Workflow

Step 1: Stream Pipeline with Backpressure

// src/streams/transform.ts
import { pipeline, Transform, Writable, Readable } from 'node:stream';
import { promisify } from 'node:util';

const pipelineAsync = promisify(pipeline);

// Backpressure-aware transform
class JsonlParser extends Transform {
  private buffer = '';

  _transform(chunk: Buffer, _encoding: string, callback: Function) {
    this.buffer += chunk.toString();
    const lines = this.buffer.split('\n');
    this.buffer = lines.pop() || '';

    for (const line of lines) {
      if (line.trim()) {
        try {
          const parsed = JSON.parse(line);
          this.push(parsed);
        } catch { /* skip invalid lines */ }
      }
    }
    callback();
  }

  _flush(callback: Function) {
    if (this.buffer.trim()) {
      try {
        this.push(JSON.parse(this.buffer));
      } catch { /* skip */ }
    }
    callback();
  }
}

// Usage with backpressure
async function processLargeFile(inputPath: string, outputPath: string) {
  await pipelineAsync(
    fs.createReadStream(inputPath, { highWaterMark: 64 * 1024 }),
    new JsonlParser(),
    new Transform({
      objectMode: true,
      transform(chunk, _encoding, callback) {
        callback(null, transformRecord(chunk));
      },
    }),
    fs.createWriteStream(outputPath),
  );
}

Step 2: Typed Event Emitter

// src/events/typed-emitter.ts
type EventMap = {
  'user:created': { id: string; email: string };
  'user:updated': { id: string; changes: string[] };
  'order:placed': { orderId: string; amount: number };
  'error': { code: string; message: string };
};

export class TypedEventEmitter {
  private emitter = new EventEmitter();

  on<K extends keyof EventMap>(event: K, listener: (payload: EventMap[K]) => void): this {
    this.emitter.on(event, listener);
    return this;
  }

  emit<K extends keyof EventMap>(event: K, payload: EventMap[K]): boolean {
    return this.emitter.emit(event, payload);
  }

  once<K extends keyof EventMap>(event: K, listener: (payload: EventMap[K]) => void): this {
    this.emitter.once(event, listener);
    return this;
  }

  off<K extends keyof EventMap>(event: K, listener: (payload: EventMap[K]) => void): this {
    this.emitter.off(event, listener);
    return this;
  }
}

// Usage
const events = new TypedEventEmitter();
events.on('user:created', ({ id, email }) => {
  sendWelcomeEmail(email);
  trackAnalytics('user_signup', id);
});

Step 3: Worker Thread Pool

``

Full SKILL.md on GitHub

Allowed tools

Tools this skill is permitted to call.

No restriction — this skill can use any tool.

Bundled files

  • references/code-organization.md
  • references/express-patterns.md
  • references/node-testing.md
  • references/nodejs-async-patterns.md
  • references/nodejs-di.md
  • references/nodejs-error-handling.md
  • references/nodejs-streams-patterns.md
  • references/nodejs-streams.md

FAQ

What does the nodejs-patterns skill do?

Use when the user asks about Node.js streams and backpressure, stream.pipeline, worker threads, typed EventEmitter, AbortController, bounded Promise concurrency or DI with awilix. Produces Node runtime pattern code. Not for project structure (use nodejs-architecture).

How do I install the nodejs-patterns skill?

Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.

Does the nodejs-patterns skill run scripts?

No, this skill is instructions only (SKILL.md) with no executable scripts.

Related skills

Compare nodejs-patterns with