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

@n7e/storage

v0.2.0

Published

A simple file storage abstraction.

Downloads

5

Readme

Storage

A simple file storage abstraction.

For further insights, read on.

Installation

To install this library use your favorite package manager. No additional steps are required to start using the library.

npm install @n7e/storage

This library is implemented in TypeScript but can be used with JavaScript without any additional steps.

Storage Entry

A storage entry represents either a file of a directory in a storage medium. If a storage entry represents a directory the content stream won't generate any data.

import type { StorageEntry } from "@n7e/storage";

function doSomethingWith(entry: StorageEntry): void {
    // ...
}

StorageEntry is just an interface describing the functionality of a storage entry. To obtain a storage entry use a storage client.

Locations

Storage entries are located using URLs. Any supported or preferred URI schemes are up to the implementation to communicate. Examples in this documentation will use the file scheme.

Storage Client

All operations to storage entries are done through a storage client.

import type { StorageClient } from "@n7e/storage";

function doSomethingWith(client: StorageClient): void {
    // ...
}

List Entries

To list entries in a given location use the list method:

const entries = await client.list(new URL("file://some/directory"));

The location is optional and if omitted implementations should list entries in the "root directory". What "root directory" really means is ultimately up to the implementation.

const entries = await client.list();

If the given location points to an existing file, storage clients should throw an InvalidLocation error.

const enties = await client.list(new URL("file://some/file.txt"));

Retrieve Entries

To retrieve a single entry at a given location use the get method:

const file = await client.get(new URL("file://some/file.txt"));
const directory = await client.get(new URL("file://some/directory"));

Note that the content stream of a directory won't produce any data.

Create Entries

To create a new entry or update an existing entry at a given location use the put method:

await client.put(new URL("file://some/file.txt"), generateContentStream());

Note that there's currently no explicit way to create directories. It is up to the implementation to either create directories or simulate directories in some fashion when needed.

If attempting to create a file where a directory already exists, storage clients should throw an InvalidLocation error.

await client.put(new URL("file://some/directory"), generateContentStream());

Remove Entries

To remove an entry use the delete method:

await client.delete(new URL("file://some/file.txt"));

When removing a directory, storage clients may choose to recursively remove any entries in the given directory or throw an InvalidLocation error. It's up to the implementation to define the behaviour.

Storage Client Builder

Storage clients can be created using the builder pattern. Storage client builders should implement the StorageClientBuilder interface to be used in storage client builder registries and/or storage client factories. Any other builder configuration is up to the implementation.

import type { StorageClientBuilder } from "@n7e/storage";

function doSomethingWith(storageClientBuilder: StorageClientBuilder): void {
    const client = storageClientBuilder.build();
    
    // ...
}

Storage Client Builder Registry

A storage client builder registry is used to register a set of storage client builder that should be available in an application. This allows specific implementations to be provided where needed, retrieved by name.

import type { StorageClientBuilderRegistry } from "@n7e/storage";

function registerStorageClient(clientBuilders: StorageClientBuilderRegistry): void {
    clientBuilders.register("images", someStorageClientBuilder);
}

async function retrieveStorageClient(clientBuilders: StorageClientBuilderRegistry): Promise<void> {
    const client = clientBuilders.retrieve("images").build();
    
    // ...
}

StorageClientBuilderRegistry is just an interface describing the functionality of a storage client builder registry. To create a storage client builder registry use a specific implementation.

This paradigm enables an application to store files on different storage mediums with a single API. It makes it easy to use different storage mediums between application environments. For example storing files in memory when developing or running tests.

To further abstract the use of storage client builders use the storage client factory.

Default Storage Client Builder Registry

This library provides a StorageClientBuilderRegistry implementation. To use it simply import it and create an instance:

import { DefaultStorageClientBuilderRegistry } from "@n7e/storage";

const storageClientBuilders = new DefaultStorageClientBuilderRegistry();

Storage Client Factory

To abstract away the use of storage client builders from the user a StorageClientFactory can be used to create storage clients.

import type { StorageClientFactory } from "@n7e/storage";

function doSomethingWith(storageClientFactory: StorageClientFactory): void {
    const client = storageClientFactory.createClient("images");
    
    // ...
}

StorageClientFactory is just an interface describing the functionality of a storage client factory. To create a storage client factory use a specific implementation.

Default Storage Client Factory

This library provides a StorageClientFactory implementation. It uses a StorageClientBuilderRegistry to retrieve named storage clients.

import { DefaultStorageClientFactory } from "@n7e/storage";

const storageClientFactory = new DefaultStorageClientFactory(someStorageClientBuilderRegistry);