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 🙏

© 2025 – Pkg Stats / Ryan Hefner

universal_media_storage

v1.1.5

Published

A media storage helper, allowing storage to firebase storage, cloudflare r2, aws s2 and more buckets.

Readme

Media Storage Library

A powerful and modular Node.js media storage library that provides a unified interface for managing file uploads, integrity verification, and deletions across multiple providers — including Cloudflare R2, Firebase Storage (GCS), and Google Drive.


🚀 Features

  • 🔁 Unified API for multiple storage providers (R2, Firebase, Drive)
  • 🔒 Automatic integrity verification with Subresource Integrity (SRI)
  • 🧠 Smart caching and checksum validation (sha256)
  • 🧩 Pluggable architecture for extending storage backends
  • ⚙️ Strongly typed (TypeScript)

📦 Installation

npm install media-storage

or with yarn:

yarn add media-storage

🧰 Supported Providers

| Provider | Module | Notes | |-----------|---------|-------| | Cloudflare R2 | CloudFlareR2StorageService | S3-compatible; uses @aws-sdk/client-s3 | | Firebase Storage (GCS) | FirebaseStorageService | Uses @google-cloud/storage | | Google Drive | GoogleDriveStorageService | Uses googleapis Drive v3 |


🧠 Core Concepts

1. Storage Service

Each provider implements a subclass of BaseStorageService with a unified uploadFile, deleteFile, and optional verifyStorage API.

interface UploadParams {
  file: {
    name: string;
    data: Buffer;
    mimetype: string;
  };
  uploadPath?: string;
}

Example:

import { CloudFlareR2StorageService } from 'media-storage';

const svc = new CloudFlareR2StorageService();

const result = await svc.uploadFile({
  file: {
    name: 'example.png',
    mimetype: 'image/png',
    data: fs.readFileSync('example.png'),
  },
  uploadPath: 'assets',
});

console.log(result);

2. Integrity Verification

Every upload generates a sha256 SRI hash that can later be validated using the universal verifier:

import { verifyStorage } from 'media-storage';

const outcome = await verifyStorage(result, { r2: { s3: new S3Client() } });

console.log(outcome);

Sample output:

{
  "exists": true,
  "integrityMatches": true,
  "sizeMatches": true
}

⚙️ Environment Configuration

Environment variables are managed by the built-in EnvironmentRegister class. You can register them at runtime or load from process.env.

import EnvironmentRegister from 'media-storage/register';

const env = EnvironmentRegister.getInstance();
env.loadFromProcessEnv();

Example .env file

R2_ACCOUNT_ID=your-account
R2_BUCKET=media
R2_ACCESS_KEY_ID=xxxx
R2_ACCESS_KEY_SECRET=xxxx
FIREBASE_STORAGE_BUCKET=my-app.appspot.com
GCP_SERVICE_ACCOUNT_KEY_BASE64=...base64...

🧪 Testing

Run the Jest test suite:

npm test

Key tests:

  • cloudflareR2.spec.ts — Verifies R2 upload, integrity, and race conditions
  • firebaseStorage.spec.ts — Validates Firebase metadata and size checks
  • googleDriveStorage.spec.ts — Tests Drive uploads and mock API verification
  • environmentRegister.spec.ts — Ensures correct env registration and immutability
  • baseStorage.spec.ts — Validates integrity computation and result normalization

🧱 Project Structure

media_storage/
 ├── src/
 │   ├── register/                # Environment config
 │   ├── services/                # Provider implementations
 │   │   ├── cloudFlareR2Storage.ts
 │   │   ├── firebaseStorage.ts
 │   │   └── googleDriveStorage.ts
 │   ├── utils/                   # Common utilities
 │   │   ├── encryptions.ts
 │   │   ├── universalIntegrityVerifier.ts
 │   │   └── validate.ts
 │   └── types/                   # Type definitions
 │
 ├── __tests__/                   # Jest test suite
 ├── package.json
 └── README.md

📜 License

MIT License © 2025 [Rookie Players]