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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@flystorage/file-storage

v0.1.5

Published

File-storage abstraction: multiple filesystems, one API.

Downloads

6,991

Readme

Flystorage

Flystorage is a file storage abstraction for NodeJS and TypeScript. It is an 80/20 solution that is built around a set of goals:

  • Provide a straight-forward API that is easy to use.
  • Allow application code to be unaware WHERE files are stored.
  • Pragmatically smooth over underlying storage differences.
  • Expose an async/await based API, promises all the way.
  • Abstract over file permissions using "visibility".
  • Actually tested using real integrations, mocks are not welcome.
  • Stand on the shoulders of giants, use official vendor packages when possible.

What is Flystorage NOT:

Flystorage is meant to be used in cases for generic file storage use-cases. It's not an API for any specific filesystem. It's a generalised solution and will not implement feature only specific to one particular storage implementation. There will be use-cases that are not catered to, simply because they cannot be abstracted over in a reasonable manner.

Capabilities

Implemented

  • [x] Write files using string | buffer | readable/stream
  • [x] Read files as stream, string, or Uint8Array
  • [x] Set permissions using abstracted visibility
  • [x] List the contents of a directory/prefix, (shallow and deep).
  • [x] Delete files without failing when they don't exist.
  • [x] Delete directories (and any files it contains)
  • [x] Generate public URLs.
  • [x] Generate temporary (signed) URLs.
  • [x] Expose or calculate checksums for files.
  • [x] Mime-type resolving
  • [x] Last modified fetching
  • [x] File size
  • [x] Moving files
  • [x] Copying files

Implementations / Adapters

Implemented

Planned

  • [ ] FTP (using basic-ftp)
  • [ ] SFTP (?)

Usage

Install the main package and any adapters you might need:

npm i -S @flystorage/file-storage

# for using AWS S3
npm i -S @flystorage/aws-s3

# for using the local filesystem
npm i -S @flystorage/local-fs

Local Usage

import {resolve} from 'node:path';
import {createReadStream} from 'node:fs';
import {FileStorage, Visibility} from '@flystorage/file-storage';
import {LocalStorageAdapter} from '@flystorage/local-fs';

/**
 * SETUP
 **/

const rootDirectory = resolve(process.cwd(), 'my-files');
const storage = new FileStorage(new LocalStorageAdapter(rootDirectory));

/**
 * USAGE
 **/

// Write using a string
await storage.write('write-from-a-string.txt', 'file contents');

// Write using a stream
const stream = createReadStream(resolve(process.cwd(), 'test-files/picture.png'));
await storage.write('picture.png', stream);

// Write with visibility (permissions).
await storage.write('public.txt', 'debug', {
    visibility: Visibility.PUBLIC, // mode: 0o644
});
await storage.write('private.txt', 'debug', {
    visibility: Visibility.PRIVATE, // mode: 0o600
});

// List directory contents
const contentsAsAsyncGenerator = storage.list('', {deep: true});

for await (const item of contentsAsAsyncGenerator) {
    console.log(item.path);

    if (item.isFile) {
        // do something with the file
    } else if (item.isDirectory) {
        // do something with the directory
    }
}

// Delete a file
await storage.deleteFile('some-file.txt');

// Delete a directory (with all contents)
await storage.deleteDirectory('some-directory');

Author

Flystorage is built by the maintainer of Flysystem, a filesystem abstraction for PHP. This brings along over a decade of filesystem abstraction experience.