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

optimag

v0.1.0

Published

Tiny browser-side TypeScript image optimization library for pre-upload compression.

Readme

optimag

Tiny browser-side TypeScript helper for shrinking images before uploading them to object storage.

Install

npm install optimag

Usage

import { optimizeImage } from "optimag";

const input = fileInput.files?.[0];

if (input) {
  const result = await optimizeImage(input, {
    maxWidth: 1920,
    maxHeight: 1920,
    quality: 0.82,
    mimeType: "image/webp",
  });

  await fetch("/upload", {
    method: "POST",
    body: result.file,
  });
}

React Guide

Use the optimizer inside a file input change handler or form submit handler. The library depends on browser APIs, so call it only on the client.

import { ChangeEvent, useState } from "react";
import { optimizeImage } from "optimag";

export function ImageUploader() {
  const [previewUrl, setPreviewUrl] = useState<string | null>(null);
  const [isUploading, setIsUploading] = useState(false);
  const [message, setMessage] = useState<string | null>(null);

  async function handleChange(event: ChangeEvent<HTMLInputElement>) {
    const input = event.target.files?.[0];

    if (!input) {
      return;
    }

    setIsUploading(true);
    setMessage(null);

    try {
      const optimized = await optimizeImage(input, {
        maxWidth: 1600,
        maxHeight: 1600,
        quality: 0.8,
        mimeType: "image/webp",
      });

      const formData = new FormData();
      formData.append("file", optimized.file);

      const response = await fetch("/api/upload", {
        method: "POST",
        body: formData,
      });

      if (!response.ok) {
        throw new Error("Upload failed.");
      }

      setPreviewUrl(URL.createObjectURL(optimized.file));
      setMessage(
        `Saved ${Math.round(optimized.savedBytes / 1024)} KB before upload.`,
      );
    } catch (error) {
      setMessage(error instanceof Error ? error.message : "Upload failed.");
    } finally {
      setIsUploading(false);
    }
  }

  return (
    <div>
      <input
        type="file"
        accept="image/*"
        onChange={handleChange}
        disabled={isUploading}
      />
      {previewUrl ? <img src={previewUrl} alt="Preview" width={240} /> : null}
      {message ? <p>{message}</p> : null}
    </div>
  );
}

React integration notes:

  • Call optimizeImage only in browser event handlers, effects, or other client-only code.
  • In Next.js App Router, put the component behind "use client" because server components cannot access canvas or File.
  • Upload result.file in FormData if your backend expects a standard browser file upload.
  • If you create preview URLs with URL.createObjectURL, revoke old URLs when replacing them in a long-lived component.

Publish To npm

The package is configured for public npm publishing. Before publishing:

npm login
npm run typecheck
npm run build
npm publish

Notes:

  • prepublishOnly already runs typechecking and build during npm publish.
  • The name optimag was not present in the npm registry when checked on March 9, 2026, but npm names can change at any time, so recheck before your final publish.

API

optimizeImage(blob, options) returns:

  • blob: the optimized output blob
  • file: a File wrapper ready for form uploads
  • width / height: output dimensions
  • originalWidth / originalHeight: input dimensions
  • mimeType: output mime type
  • originalSize / outputSize: byte sizes before and after optimization
  • savedBytes: how many bytes were removed

Options:

  • maxWidth: resize down if the source is wider than this
  • maxHeight: resize down if the source is taller than this
  • quality: compression quality from 0 to 1
  • mimeType: output format, typically image/jpeg, image/png, or image/webp
  • keepOriginalIfSmaller: defaults to true; returns the original image if recompression makes it larger
  • fileName: override the generated output filename

Notes

  • Runs entirely in the browser using createImageBitmap and canvas APIs.
  • Keeps aspect ratio automatically.
  • Falls back to the original file when the optimized result is not actually smaller.