npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

media-pipeline

v1.6.1

Published

A storage-agnostic file processing pipeline for Node.js

Downloads

522

Readme

Media Pipeline

Version: 1.5.11 Runtime: Node.js Purpose: Storage-agnostic, extensible file validation, transformation, and storage pipeline.


Overview

Media Pipeline is a modular file processing system where files flow through a structured pipeline:

Input → Validators → Processors → Storage → Output

Each stage is fully customizable and extensible via plugins.


Installation

npm install media-pipeline

Basic Usage

import {
  createPipeline,
  localStorage,
  maxSize,
  allowedMimeTypes
} from 'media-pipeline';

const pipeline = createPipeline({
  validators: [
    maxSize(5 * 1024 * 1024),
    allowedMimeTypes(['image/jpeg'])
  ],
  storage: localStorage('./uploads')
});

const file = {
  buffer: Buffer.from('data'),
  filename: 'image.jpg',
  mimeType: 'image/jpeg',
  size: 1024
};

const result = await pipeline.process(file);
console.log(result);

Core Concepts

Pipeline Flow

  1. Input: Raw file object
  2. Validators: Ensure file meets requirements
  3. Processors: Transform file
  4. Storage: Persist file
  5. Output: Result with metadata and trace

API Reference

createPipeline(config)

Creates a new pipeline instance.

Config

type PipelineConfig = {
  validators?: Validator[];
  processors?: Processor[];
  storage: Storage;
  hooks?: PipelineHooks;
};

Built-in Utilities

localStorage(basePath)

Stores files locally.

maxSize(limit)

Validates file size.

allowedMimeTypes(types)

Validates MIME type.

identityProcessor()

No-op processor.


Types

PipelineFile

{
  buffer: Buffer;
  filename: string;
  mimeType: string;
  size: number;
}

PipelineResult

{
  url: string;
  path: string;
  size: number;
  metadata: Record<string, any>;
  meta: {
    plugins: PluginMeta[];
    trace: TraceEvent[];
  };
}

PipelineContext

{
  file: PipelineFile;
  metadata: Record<string, any>;
  meta: PipelineMeta;
}

Validators

Validators check conditions and throw errors if invalid.

type Validator = (ctx: PipelineContext) => void | Promise<void>;

Example

function imageOnlyValidator(ctx) {
  if (!ctx.file.mimeType.startsWith('image/')) {
    throw new Error('Invalid file type');
  }
}

Processors

Processors transform files.

type Processor = (ctx: PipelineContext) => PipelineContext | Promise<PipelineContext>;

Example

function renameProcessor(ctx) {
  return {
    ...ctx,
    file: {
      ...ctx.file,
      filename: `processed-${ctx.file.filename}`
    }
  };
}

Storage

Storage persists files.

type Storage = {
  save(file: PipelineFile): Promise<PipelineResult>;
};

Hooks

Hooks allow lifecycle customization.

type PipelineHooks = {
  onStart?: (ctx) => void | Promise<void>;
  afterValidate?: (ctx) => void | Promise<void>;
  afterProcess?: (ctx) => void | Promise<void>;
  onError?: (error, ctx) => void | Promise<void>;
  onFinish?: (result, ctx) => void | Promise<void>;
};

Plugin System

Plugins extend functionality.

Plugin Types

Object Plugin

{
  name: string;
  version?: string;
  setup(builder: PipelineBuilder): void;
}

Function Plugin

(builder: PipelineBuilder) => void;

Builder API

builder.addValidator(fn);
builder.addProcessor(fn);
builder.mergeHooks(hooks);
builder.setStorage(storage);

Example Plugin (Image Resize)

const sharpPlugin = {
  name: 'sharp-resize',
  setup(builder) {
    builder.addProcessor(async (ctx) => {
      if (ctx.file.mimeType.startsWith('image/')) {
        const sharp = require('sharp');
        const buffer = await sharp(ctx.file.buffer)
          .resize(800, 800)
          .toBuffer();

        return {
          ...ctx,
          file: {
            ...ctx.file,
            buffer,
            size: buffer.length
          }
        };
      }
      return ctx;
    });
  }
};

Execution Flow

  1. onStart
  2. Validators
  3. afterValidate
  4. Processors
  5. afterProcess
  6. Storage
  7. onFinish

On error:

  • onError is called
  • Error is re-thrown

Tracing

Each step is recorded.

{
  plugin: string;
  stage: 'validator' | 'processor' | 'hook' | 'storage';
  message: string;
  duration?: number;
  timestamp: number;
}

Errors

Classes

  • PipelineError
  • ValidationError
  • ProcessorError
  • StorageError

Example Handling

try {
  await pipeline.process(file);
} catch (err) {
  if (err instanceof ValidationError) {
    console.log(err.details);
  }
}

Best Practices

  • Use named functions for better tracing
  • Always handle errors
  • Keep processors pure
  • Use plugins for reusable logic

Limitations

  • No streaming support
  • Sequential execution only
  • Trace accumulation across runs
  • No automatic error wrapping

Advanced Usage

Using Plugins

pipeline.use(sharpPlugin);

Custom Storage Example

const memoryStorage = {
  async save(file) {
    return {
      url: 'memory://file',
      path: file.filename,
      size: file.size,
      metadata: {}
    };
  }
};

Design Principles

  • Separation of concerns
  • Composability
  • Extensibility
  • Minimal core

Future Improvements

  • Streaming support
  • Parallel processing
  • Queue system
  • Plugin marketplace

License

MIT