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

content-store

v1.0.0

Published

Content server with file upload, automatic hashing and hash based naming scheme.

Downloads

10

Readme

content-store

An HTTP content server with file upload, automatic hashing and hash based naming scheme.

Or, put it another way: a content addressable storage server based on Restify, Multiparty and user defined hashing algorithm.

Setup

npm install content-store

Usage

const ContentStore = require('content-store')
const PORT = 8001
const storageDir = 'data'

// hash function implementation example
const { MetroHash128 } = require('metrohash')
function createHash () {
  const SEED = 0 // hard code the seed, can be any integer
  return new MetroHash128(SEED)
}

async function start () {
  const server = await ContentStore({ storageDir }, createHash)

  server.listen(PORT, (err) => {
    if (err) {
      return console.log('something bad happened', err)
    }

    console.log(`server is listening on ${server.url}`)
  })
}

start()
.catch(console.error)

Now one can upload a file:

curl -F '[email protected]' http://localhost:8001/upload

// with an response like this:

{"result":"upload OK","files":[["sample.txt","ba089843d132af3231990d405f2ac3c0"]]}

The -F option of cURL means we are sending data as multipart/form-data, which is a standard way of uploading files over http.

Download it:

curl -O http://localhost:8001/ba089843d132af3231990d405f2ac3c0

Delete it:

curl -X DELETE http://localhost:8001/ba089843d132af3231990d405f2ac3c0

Sample dockerized application

that uses this content-store as a backend microservice can be found here

Configuration

The ContentStore constructor function returns a server promise. The first parameter is options object with following defaults:

{
  name: 'content-store',
  storageDir: 'data'
}

The specified storage directory is to be resolved against the process.cwd() - the directory of current process. If you need an absolute path, then set it here and it will remain as is.

The second parameter is createHash function with desired implementation. This function should have no arguments and return a hash object, which should support two methods: hash.update(chunk) and hash.digest(format) with the same logics as in node crypto module.

In the example above we used createHash function based on metrohash module implementation, which is one of well known non-cryptographic hashing algorithm out there.

Any other suitable hashing algorithm would do, be it cryptographic or non-cryptographic. Here is as example of createHash based on sha256 algorithm from node's crypto:

const crypto = require('crypto')
function createHash () {
  return crypto.createHash('sha256')
}

which illustrates the ease of adopting other hashing algorithms.

Idea

Imagine a server able to upload files and storing each uploaded file under the name based on hash digest of its content.

In this way the entity identification on the server side is entirely based on content's hash, so we are safe to consider such a server a content store, as opposed to file store, because from external point of view it essentially operates on contents rather then on files.

One consequence of such an approach is that any two uploaded files with the same content are always stored under same name (and absolute path), so there is no way for file duplication on backend side.

Another consequence of the server being a content store is that it only supports 3 out of 4 CRUD operations:

POST /upload
GET /:hash  
DELETE /:hash

There is no much sense in updating a content. Just like as it is in GIT where updating a file leads to two really unrelated (from GIT point of view) operations: deleting old content entry and creating new content entry.