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

browser-image-validator

v0.1.1

Published

Validate browser image files before upload by MIME type, file size, and dimensions.

Readme

browser-image-validator

npm version CI TypeScript License: MIT

Validate browser image files before upload by MIME type, file size, and dimensions.

Installation

npm install browser-image-validator

Why this package

  • Validate image files before upload on the client side
  • Keep validation logic framework-agnostic
  • Return typed, machine-readable error codes
  • Avoid loading image dimensions unless dimension checks are enabled

Requirements

Browser only — uses the browser File API and Image constructor. Not intended for Node.js or server-side use.

TypeScript 5+ is recommended for full type inference.

Quick start

import { validateImage } from 'browser-image-validator';

const result = await validateImage(file, {
    allowedMimeTypes: ['image/jpeg', 'image/png', 'image/webp'],
    maxFileSizeBytes: 5 * 1024 * 1024, // 5 MB
    dimensions: {
        maxWidth: 4096,
        maxHeight: 4096,
    },
});

if (result.valid) {
    console.log('Image is valid:', result.image);
    // { mimeType, sizeBytes, dimensions?: { width, height } }
} else {
    console.error('Validation failed:', result.errors);
    // Array<{ code: ImageValidationErrorCode }>
}

See docs/API.md for the full API reference including all types, options, error codes, and behavior details.

Features

  • Validates MIME type
  • Validates file size
  • Validates image width and height (via browser image decoding)
  • Fully typed — results are discriminated unions
  • Framework-agnostic

Error codes

  • INVALID_FILE_TYPE
  • FILE_TOO_LARGE
  • IMAGE_WIDTH_TOO_LARGE
  • IMAGE_HEIGHT_TOO_LARGE
  • IMAGE_LOAD_FAILED

Common use case

Use validateImage before starting an upload flow to reject unsupported files early and return consistent error codes for your UI layer.

Out of scope

  • image resize / crop / preview generation
  • drag-and-drop
  • localization
  • multiple files validation / async queue
  • EXIF / aspect ratio / content-based MIME validation
  • server-side validation / upload logic / UI error messages
  • React, Vue, or framework-specific integrations

Limitations

  • MIME type validation relies on the browser-provided file.type
  • The package does not inspect actual file contents
  • Non-image files can still pass if you do not enable MIME or dimension checks
  • ESM package intended for browser runtimes

Contributing

Requires Node.js ≥ 22 (development/build toolchain only).

npm ci
npm run test:run    # run tests once
npm run test        # run tests in watch mode
npm run typecheck   # TypeScript type check
npm run lint        # ESLint
npm run format      # Prettier
npm run build       # build to dist/

License

MIT