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

corestore

v6.18.4

Published

A Hypercore factory that simplifies managing collections of cores.

Downloads

8,761

Readme

Corestore

See the full API docs at docs.holepunch.to

Corestore is a Hypercore factory that makes it easier to manage large collections of named Hypercores.

Corestore provides:

  1. Key Derivation - All writable Hypercore keys are derived from a single master key and a user-provided name.
  2. Session Handling - If a single Hypercore is loaded multiple times through the get method, the underlying resources will only be opened once (using Hypercore 10's new session feature). Once all sessions are closed, the resources will be released.
  3. Storage Management - Hypercores can be stored in any random-access-storage instance, where they will be keyed by their discovery keys.
  4. Namespacing - You can share a single Corestore instance between multiple applications or components without worrying about naming collisions by creating "namespaces" (e.g. corestore.namespace('my-app').get({ name: 'main' }))

Installation

npm install corestore

Usage

A corestore instance can be constructed with a random-access-storage module, a function that returns a random-access-storage module given a path, or a string. If a string is specified, it will be assumed to be a path to a local storage directory:

const Corestore = require('corestore')

const store = new Corestore('./my-storage')
const core1 = store.get({ name: 'core-1' })
const core2 = store.get({ name: 'core-2' })

API

const store = new Corestore(storage, opts = {})

Create a new Corestore instance.

storage can be either a random-access-storage module, a string, or a function that takes a path and returns an random-access-storage instance.

Opts include:

{
  inflightRange: null // Advanced option. Forwarded to the Hypercores created by corestore.get(...)
}

const core = store.get(key | { name: 'a-name', exclusive, ...hypercoreOpts})

Loads a Hypercore, either by name (if the name option is provided), or from the provided key (if the first argument is a Buffer or String with hex/z32 key, or if the key options is set).

If that Hypercore has previously been loaded, subsequent calls to get will return a new Hypercore session on the existing core.

If you set the exclusive option and you are opening a writable session it will wait for all other exclusive writable to close before opening the Hypercore effectively meaning any op on the core will wait until its exclusive.

All other options besides name and key and exclusive will be forwarded to the Hypercore constructor.

const stream = store.replicate(optsOrStream)

Creates a replication stream that's capable of replicating all Hypercores that are managed by the Corestore, assuming the remote peer has the correct capabilities.

opts will be forwarded to Hypercore's replicate function.

Corestore replicates in an "all-to-all" fashion, meaning that when replication begins, it will attempt to replicate every Hypercore that's currently loaded and in memory. These attempts will fail if the remote side doesn't have a Hypercore's capability -- Corestore replication does not exchange Hypercore keys.

If the remote side dynamically adds a new Hypercore to the replication stream, Corestore will load and replicate that core if possible.

Using Hyperswarm you can easily replicate corestores

const swarm = new Hyperswarm()

// join the relevant topic
swarm.join(...)

// simply pass the connection stream to corestore
swarm.on('connection', (connection) => store.replicate(connection))

const store = store.namespace(name)

Create a new namespaced Corestore. Namespacing is useful if you're going to be sharing a single Corestore instance between many applications or components, as it prevents name collisions.

Namespaces can be chained:

const ns1 = store.namespace('a')
const ns2 = ns1.namespace('b')
const core1 = ns1.get({ name: 'main' }) // These will load different Hypercores
const core2 = ns2.get({ name: 'main' })

const storeB = storeA.session(opts)

Create a new Corestore that shares resources with the original, like cache, cores, replication streams, and storage, while optionally resetting the namespace, overriding primaryKey. Useful when an application wants to accept an optional Corestore, but needs to maintain a predictable key derivation.

opts are the same as the constructor options:

{
  primaryKey, // Overrides the primaryKey for this session
  namespace, // If set to null it will reset to the DEFAULT_NAMESPACE
  detach: true, // By disabling this, closing the session will also close the store that created the session
  inflightRange: null // Advanced option. Forwarded to the Hypercores created by `session.get(...)
}

await store.close()

Fully close this Corestore instance.

store.on('core-open', core)

Emitted when the first session for a core is opened.

Note: This core may close at any time, so treat it as a weak reference

store.on('core-close', core)

Emitted when the last session for a core is closed.

License

MIT