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

@universal-packages/storage

v1.11.9

Published

Simple storage organizer

Downloads

3,220

Readme

Storage

npm version Testing codecov

Simple storage organizer.

Install

npm install @universal-packages/storage

Storage

Storage is the main class interface to start storing blobs of data under a key.

import { Storage } from '@universal-packages/storage'

const storage = new Storage()

const key = await storage.storage({ data: new Buffer('example') })

const myBlob = await storage.retrieve(key)

console.log(myData)

// > <Buffer example>

By default a registry uses a local engine to store blobs, this is suitable for most cases but you can also use a custom engine to store the data in a database or any other storage system. In testing environments the registry will use a memory engine to store blobs.

Options

  • engine Engine default: local Instance of the engine to be used to store the blobs.
  • engineOptions Object Options to pass to the engine if resolved as adapter.

Instance methods

prepare() async

Initialize the internal engine in case it needs preparation.

release() async

Releases the engine resources in case they need to be disposed before finishing the process.

generateKey(md5?: string)

Generates a new key using the provided md5 or a random one if not provided.

generateVersionKey(key: string, descriptor: Object)

  • descriptor VersionBlobDescriptor
    • name String
    • mimetype String
    • width Number
    • height Number
    • fit contain | cover | fill | inside | outside

Generates the version key of a key using the provided descriptor.

parseVersionSlug(slug: string)

Gets the version descriptor from a version slug.

serializeVersionBlobDescriptor(descriptor: Object)

  • descriptor VersionBlobDescriptor
    • name String
    • mimetype String
    • width Number
    • height Number
    • fit contain | cover | fill | inside | outside

Serializes a version blob descriptor into a version slug.

store(descriptor: Object, engineOptions?: Object)

store(key: string, descriptor: Object, engineOptions?: Object)

  • descriptor BlobDescriptor
    • data Buffer
    • name String
    • mimetype String
    • md5 String
    • size Number

Stores a blob under a newly generated key or by using a provided one, engine options can optionally be passed in case the engine needs configuration per blob.

storeVersion(key: string, descriptor: Object, engineOptions?: Object)

  • descriptor VersionBlobDescriptor
    • name String
    • mimetype String
    • width Number
    • height Number
    • fit contain | cover | fill | inside | outside

Stores a new version of a, image blob by using a provided key, engine options can optionally be passed in case the engine needs configuration per blob.

retrieve(key: String)

Returns the blob stored under the provided key.

retrieveVersion(key: string, descriptor: Object, engineOptions?: Object)

  • descriptor VersionBlobDescriptor
    • name String
    • mimetype String
    • width Number
    • height Number
    • fit contain | cover | fill | inside | outside

Retrieves a version blob if the version was stored previously for the given key and descriptor.

retrieveStream(key: String)

Returns a stream of the blob stored under the provided key.

retrieveVersionStream(key: string, descriptor: Object, engineOptions?: Object)

  • descriptor VersionBlobDescriptor
    • name String
    • mimetype String
    • width Number
    • height Number
    • fit contain | cover | fill | inside | outside

Retrieves a version stream if the version was stored previously for the given key and descriptor.

retrieveUri(key: String, engineOptions?: Object)

Returns the uri of the blob stored under the provided key without retrieving the blob, engine options can optionally be passed in case the engine can generate a special uri.

retrieveVersionUri(key: string, descriptor: Object, engineOptions?: Object)

  • descriptor VersionBlobDescriptor
    • name String
    • mimetype String
    • width Number
    • height Number
    • fit contain | cover | fill | inside | outside

Returns the uri of the version blob stored under the provided key and descriptor, without retrieving the blob, engine options can optionally be passed in case the engine can generate a special uri.

dispose(key: String)

Removes the blob stored under the provided key so it's no longer retrievable.

disposeVersion(key: string, descriptor: Object)

  • descriptor VersionBlobDescriptor
    • name String
    • mimetype String
    • width Number
    • height Number
    • fit contain | cover | fill | inside | outside

Removes the version blob stored under the provided key and descriptor so it's no longer retrievable.

Events

Storage will emit events regarding blob tasks

storage.on('*', ({ event, key, engine, descriptor }) => console.log(event, key, engine, descriptor))
storage.on('store:start', ({ key, engine, descriptor }) => console.log(key, engine, descriptor))
storage.on('store:finish', ({ key, engine, descriptor }) => console.log(key, engine, descriptor))
storage.on('store-version:start', ({ key, engine, descriptor }) => console.log(key, engine, descriptor))
storage.on('store-version:finish', ({ key, engine, descriptor }) => console.log(key, engine, descriptor))
storage.on('retrieve:start', ({ key, engine }) => console.log(key, engine))
storage.on('retrieve:finish', ({ key, engine }) => console.log(key, engine))
storage.on('retrieve-version:start', ({ key, engine, descriptor }) => console.log(key, engine, descriptor))
storage.on('retrieve-version:finish', ({ key, engine, descriptor }) => console.log(key, engine, descriptor))
storage.on('retrieve-stream:start', ({ key, engine }) => console.log(key, engine))
storage.on('retrieve-stream:finish', ({ key, engine }) => console.log(key, engine))
storage.on('retrieve-version-stream:start', ({ key, engine, descriptor }) => console.log(key, engine, descriptor))
storage.on('retrieve-version-stream:finish', ({ key, engine, descriptor }) => console.log(key, engine, descriptor))
storage.on('retrieve-uri:start', ({ key, engine }) => console.log(key, engine))
storage.on('retrieve-uri:finish', ({ key, engine }) => console.log(key, engine))
storage.on('retrieve-version-uri:start', ({ key, engine, descriptor }) => console.log(key, engine, descriptor))
storage.on('retrieve-version-uri:finish', ({ key, engine, descriptor }) => console.log(key, engine, descriptor))
storage.on('dispose:start', ({ key, engine }) => console.log(key, engine))
storage.on('dispose:finish', ({ key, engine }) => console.log(key, engine))
storage.on('dispose-version:start', ({ key, engine, descriptor }) => console.log(key, engine, descriptor))
storage.on('dispose-version:finish', ({ key, engine, descriptor }) => console.log(key, engine, descriptor))

Engine

To create an engine that suits your requirements you just need to implement a new class as the following:

import MyEngine from './MyEngine'

const storage = new Storage({ engine: new MyEngine() })
export default class MyEngine {
  constructor(options) {
    // Options passed through the adapters sub system
  }

  prepare() {
    // Initialize any connection using options
  }

  release() {
    // Release any resources or close any connection
  }

  store(key, data) {
    // Store the blob using the key as key
  }

  retrieve(key) {
    return // retrieve the subject from your engine using the key
  }

  dispose(key) {
    // dispose the blob from your engine using the key
  }

  disposeDirectory(key) {
    // dispose a directory of blobs (usually the versions directory)
  }
}

Engine Interface

If you are using TypeScript just can implement the EngineInterface to ensure the right implementation.

import { EngineInterface } from '@universal-packages/storage'

export default class MyEngine implements EngineInterface {}

Typescript

This library is developed in TypeScript and shipped fully typed.

Contributing

The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.

License

MIT licensed.

Sharp

This project uses the Sharp library, which is licensed under the Apache License 2.0. Copyright 2013 Lovell Fuller and others.

The source code for the Sharp library can be found at https://github.com/lovell/sharp.