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

@ltv/storage

v0.1.6

Published

> Forked from [Flydrive](https://github.com/Slynova-Org/flydrive)

Downloads

1

Readme

NodeJS Storage

Forked from Flydrive

Getting Started

This package is available in the npm registry. It can easily be installed with npm or yarn.

$ npm i @ltv/storage
# or
$ yarn add @ltv/storage

When you require the package in your file, it will give you access to the StorageManager class. This class is a facade for the package and should be instantiated with a configuration object.

const { StorageManager } = require('@ltv/storage');
const storage = new StorageManager(config);

Once you instantiated the manager, you can use the StorageManager#disk() method to retrieve a disk an use it.

storage.disk(); // Returns the default disk (specified in the config)
storage.disk('awsCloud'); // Returns the driver for the disk "s3"
storage.disk('awsCloud', customConfig); // Overwrite the default configuration of the disk

Driver's API

Each driver extends the abstract class Storage. This class will throw an exception for each methods by default. The driver needs to overwrite the methods it supports.

The following method doesn't exist on the LocalFileSystemStorage driver, therefore, it will throw an exception.

// throws "E_METHOD_NOT_SUPPORTED: Method getSignedUrl is not supported for the driver LocalFileSystemStorage"
storage.disk('local').getSignedUrl();

Since we are using TypeScript, you can make use of casting to get the real interface:

import { LocalFileSystemStorage } from '@ltv/storage';

storage.disk<LocalFileSystemStorage>('local');

Response interface

Asynchronous methods will always return a Promise which resolves with a Response object. The response object may contain relevant data in its properties (for example, the ExistsResponse object for the exists method contains a boolean exists property).

All responses additionally have a raw property which is driver-specific and contains the result from the original call made by the driver.

Exceptions

In case of runtime errors, flydrive will try to throw driver-agnostic exceptions.
Exceptions also have a raw property which contains the original error.

Methods

This method will append the content to the file at the location. If the file doesn't exist yet, it will be created.

// Supported drivers: "local"

await storage.disk('local').append('foo.txt', 'bar');
// foo.txt now has the content `${initialContent}bar`

This method will copy a file to another location.

// Supported drivers: "local", "s3", "gcs"

await storage.disk('local').copy('foo.txt', 'bar.txt');
// foo.txt was copied to bar.txt

This method will delete the file at the given location.

// Supported drivers: "local", "s3", "gcs"

const { wasDeleted } = await storage.disk('local').delete('foo.txt');
// If a file named foo.txt has been deleted, wasDeleted is true.

The value returned by this method will have a wasDeleted property that can be either a boolean (true if a file was deleted, false if there was no file to delete) or null (if no information about the file is available).

This method returns the driver used if you need to do anything specific not supported by default.

storage.disk('local').driver(); // Returns the "fs-extra" module.
storage.disk('awsCloud').driver(); // Returns an instance of the AWS S3 client.
storage.disk('googleCloud').driver(); // Returns an instance of the the Google Cloud Storage client.
// ....

This method will determine if a file exists at the given location.

// Supported drivers: "local", "s3", "gcs"

const { exists } = await storage.disk('local').exists('foo.txt');
// exists is true or false

This method will return the file's content as a string for the given location.

// Supported drivers: "local", "s3", "gcs"

const { content } = await storage.disk('local').exists('foo.txt');

This method will return the file's content as a Buffer for the given location.

// Supported drivers: "local", "s3", "gcs"

const { content } = await storage.disk('local').exists('foo.txt');

This method will return the signed url for an existing file.

// Supported drivers: "s3", "gcs"

const { signedUrl } = await storage.disk('awsCloud').getSignedUrl('foo.txt');

This method will return the file's size (in bytes) and last modification date.

// Supported drivers: "local", "s3", "gcs"

const { size, modified } = await storage.disk('local').getStat('foo.txt');

This method will return a Node.js readable stream for the given file.

// Supported drivers: "local", "s3", "gcs"

const stream = storage.disk('local').getStream('foo.txt');

This method will return a public URL for a given file.

// Supported drivers: "s3", "gcs"

const uri = storage.disk('awsCloud').getUrl('foo.txt');

This method will move the file to a new location.

// Supported drivers: "local", "s3", "gcs"

await storage.disk('local').move('foo.txt', 'newFolder/foo.txt');

This method will create a new file with the provided content.

// Supported drivers: "local", "s3", "gcs"

await storage.disk('local').put('bar.txt', 'Foobar');

This method will prepend content to a file.

// Supported drivers: "local"

await storage.disk('local').prepend('foo.txt', 'bar');
// foo.txt now has the content `bar${initialContent}`

This method will return an async iterator over all file names that start with prefix (recursive).

// Supported drivers: "local", "s3", "gcs"

const disk = storage.disk('local');
for await (const filename of disk.flatList('a/b')) {
  console.log(filename);
}

Contribution Guidelines

Any pull requests or discussions are welcome. Note that every pull request providing new feature or correcting a bug should be created with appropriate unit tests.