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

@bklarjs/upload

v1.0.0

Published

Multipart file upload middleware for Bklar.

Downloads

173

Readme

@bklarjs/upload 📁

NPM Version License: MIT

Simple, high-performance file upload middleware for the bklar framework.

This package simplifies handling multipart/form-data requests. It validates files, saves them to disk using Bun's optimized I/O, and organizes them neatly in your application context.


✨ Features

  • Native Performance: Uses Bun.write for the fastest possible file I/O.
  • 🛡️ Built-in Validation: Easily restrict uploads by file size and MIME type.
  • 💾 Automatic Storage: Automatically saves files to a destination folder, or keeps them in memory for custom processing.
  • 🎲 Smart Renaming: Auto-generates UUIDs for filenames to prevent collisions, or lets you define custom naming logic.
  • 🧩 Type Safe: Fully typed ctx.state.files for great developer experience.

📦 Installation

This package is designed to work with bklar.

bun add bklar @bklarjs/upload

🚀 Usage

1. Basic: Save to Disk

This example saves all uploaded files to the ./uploads directory.

import { Bklar } from "bklar";
import { upload } from "@bklarjs/upload";

const app = Bklar();

// Create the upload middleware
const uploadMiddleware = upload({
  dest: "./uploads",
  maxSize: 5 * 1024 * 1024, // 5MB limit
  types: ["image/png", "image/jpeg"], // Only allow images
});

app.post(
  "/upload",
  (ctx) => {
    // Regular form fields are in ctx.body
    const { username } = ctx.body;

    // Files are in ctx.state.files
    const avatar = ctx.state.files?.avatar;

    if (!avatar) {
      return ctx.json({ error: "Avatar is required" }, 400);
    }

    // If 'dest' is set, avatar is an UploadedFile object
    return ctx.json({
      message: "File uploaded!",
      file: avatar,
      // avatar.path -> "uploads/f47ac10b-58cc-4372-a567-0e02b2c3d479.png"
    });
  },
  {
    middlewares: [uploadMiddleware],
  }
);

app.listen(3000);

2. Advanced: In-Memory / Custom Handling

If you don't provide a dest, files are passed as native Bun.File objects. This is useful if you want to upload them directly to S3 or process them before saving.

const memoryUpload = upload({
  types: /^image\//, // Allow all images
});

app.post(
  "/s3-upload",
  async (ctx) => {
    const file = ctx.state.files?.doc;

    if (file && file instanceof File) {
      // It's a native File object. You can read it as an ArrayBuffer, etc.
      const buffer = await file.arrayBuffer();

      // ... logic to upload buffer to S3 ...

      return ctx.json({ size: file.size });
    }
  },
  { middlewares: [memoryUpload] }
);

⚙️ Configuration Options

| Option | Type | Default | Description | | :---------- | :----------------------- | :---------- | :--------------------------------------------------------------------------- | | dest | string | undefined | Folder to save files. If omitted, files are not saved to disk automatically. | | maxSize | number | Infinity | Max file size in bytes. Returns 413 if exceeded. | | types | string[] | RegExp | undefined | Allowed MIME types. Returns 415 if invalid. | | randomize | boolean | true | If true, renames files to a UUID (preserves extension). | | filename | (file: File) => string | undefined | Custom function to generate filenames. |

🧩 TypeScript

The package uses module augmentation to add files to ctx.state.

import type { UploadedFile } from "@bklarjs/upload";

// If dest is SET:
const file = ctx.state.files["fieldname"] as UploadedFile;
console.log(file.path);

// If dest is NOT SET:
const file = ctx.state.files["fieldname"] as File;
console.log(file.name);

🤝 Contributing

Contributions are welcome! Please open an issue or submit a Pull Request to the main bklar repository.

📄 License

This project is licensed under the MIT License.