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

ss-media-library

v1.0.0

Published

A TypeScript media library for uploading and downloading public/private files, supporting local and Google Cloud Storage (GCS) backends.

Readme

SS Media Library

A TypeScript media library for uploading and downloading public/private files, supporting local and Google Cloud Storage (GCS) backends. Designed with Clean Architecture, CQRS, and SOLID principles.

Features

  • Upload public and private files
  • Download files as stream or buffer
  • Pluggable storage providers (local, GCS)
  • Token-based access for private files (JWT)
  • Rich file metadata

Installation

npm install ss-media-library

Usage Examples

1. Setup Storage Providers

Local Storage

import { LocalStorageProvider } from '@infrastructure/LocalStorageProvider';
const storageProvider = new LocalStorageProvider();

Google Cloud Storage (GCS)

import { Storage } from '@google-cloud/storage';
import { GCSStorageProvider } from '@infrastructure/GCSStorageProvider';

const gcs = new Storage({ /* app-specific config here */ });
const storageProvider = new GCSStorageProvider(gcs);

2. Upload Public File

const uploadPublic = new UploadPublicFile(storageProvider);
const publicMetadata = await uploadPublic.execute({
  file: Buffer.from('hello world'),
  bucket: 'public-bucket',
  path: 'uploads/',
  filename: 'public-uuid.txt',
  mimetype: 'text/plain',
});
console.log(publicMetadata);

3. Upload Private File (with ownerId and tokenPayload)

const uploadPrivate = new UploadPrivateFile(storageProvider, tokenService);
const privateMetadata = await uploadPrivate.execute({
  file: Buffer.from('secret'),
  bucket: 'private-bucket',
  path: 'user-files/2025/06/',
  filename: 'user-1234-uuidv4.png',
  mimetype: 'image/png',
  ownerId: 'user-1234',
  tokenPayload: {
    fileId: 'user-1234-uuidv4',
    ownerId: 'user-1234',
    appId: 'my-api-app',
    exp: Math.floor(Date.now() / 1000) + 3600 // 1 hour expiry
  }
});
console.log(privateMetadata);

4. Upload Private File (minimal)

const privateMetadata = await uploadPrivate.execute({
  file: Buffer.from('secret'),
  bucket: 'private-bucket',
  path: 'user-files/2025/06/',
  filename: 'user-1234-uuidv4.png',
  mimetype: 'image/png'
  // ownerId and tokenPayload are omitted
});
console.log(privateMetadata);

File Metadata Returned

All upload methods return a FileMetadata object with fields like:

{
  "id": "user-1234-uuidv4",
  "filename": "user-1234-uuidv4.png",
  "originFilename": "original.png",
  "mimetype": "image/png",
  "filesize": 123456,
  "bucket": "private-bucket",
  "path": "user-files/2025/06/",
  "storageType": "local",
  "isPrivate": true,
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "checksum": "abc123...",
  "createdAt": "2025-06-23T12:00:00Z",
  "updatedAt": "2025-06-23T12:00:00Z",
  "expiresAt": "2025-06-24T12:00:00Z",
  "ownerId": "user-1234"
}
  • token is only present for private files.
  • ownerId, expiresAt, and checksum are optional and depend on your usage.

Download Examples

1. Download Public File

const downloadPublic = new DownloadPublicFile(storageProvider);

// As a stream (recommended for large files)
const stream = await downloadPublic.asStream({
  bucket: 'public-bucket',
  path: 'uploads/',
  filename: 'public-uuid.txt',
});
// Use the stream as needed (e.g., pipe to response)

// As a buffer (for small files)
const buffer = await downloadPublic.asBuffer({
  bucket: 'public-bucket',
  path: 'uploads/',
  filename: 'public-uuid.txt',
});
console.log(buffer.toString());

2. Download Private File

const downloadPrivate = new DownloadPrivateFile(storageProvider, tokenService);

// As a stream (requires token)
const privateStream = await downloadPrivate.asStream({
  bucket: 'private-bucket',
  path: 'user-files/2025/06/',
  filename: 'user-1234-uuidv4.png',
  token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...' // from metadata.token
});
// Use the stream as needed

// As a buffer (requires token)
const privateBuffer = await downloadPrivate.asBuffer({
  bucket: 'private-bucket',
  path: 'user-files/2025/06/',
  filename: 'user-1234-uuidv4.png',
  token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
});
console.log(privateBuffer.toString());

Error Handling Example

Download Private File – Invalid Token

If you provide an invalid or expired token when downloading a private file, the library will throw an error.
You should handle this in your code, for example:

try {
  const privateBuffer = await downloadPrivate.asBuffer({
    bucket: 'private-bucket',
    path: 'user-files/2025/06/',
    filename: 'user-1234-uuidv4.png',
    token: 'invalid-or-expired-token'
  });
} catch (error) {
  // Example error handling
  console.error('Failed to download private file:', error.message);
  // error.message might be: "Invalid or expired token"
}

Typical error response:

{
  "error": "Invalid or expired token"
}
  • Make sure to catch and handle errors when working with private file downloads.
  • The exact error message may vary depending on your TokenService implementation.

For more details, see the inline documentation and source files.