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

@ipld/block

v6.0.4

Published

IPLD Block interface

Downloads

101

Readme

Block API

The Block API is the single endpoint for authoring IPLD data structures. Unless you're implementing a new codec you can get everything you need from the Block API: encoding, decoding, cid creation w/ hashing.

import Block from '@ipld/block/defaults'

const b1 = Block.encoder({ hello: 'world' }, 'dag-cbor')

// link between blocks
const b2 = Block.encoder({ head: await b1.cid() }, 'dag-cbor')

// write to a standard key value store
for (const block of [b1, b2]) {
  const cid = await block.cid()
  await put(cid.toString('base64'), block.encode())
}

// write to a store that understands the Block interface
await Promise.all([put(b1), put(b2)])

Block.encoder(object, codec, algorithm = 'sha2-256')

Create a Block instance from a native object.

The cid as well as encoding will not happen until requested and then will be cached when appropriate.

let block = Block.encoder({hello: 'world'}, 'dag-cbor')

Returns a Block instance.

Block.decoder(binary, codec, algorithm = 'sha2-256')

Create a Block instance from an existing binary encoded block

The cid as well as decoding will not happen until requested and then will be cached when appropriate.

let block = Block.decoder(someBuffer, 'dag-cbor')

Returns a Block instance.

Block.create(binary, cid)

Create a new block from the raw binary data and cid.

cid can be an instance of CID or a base encoded string of a cid.

Returns a Block instance.

Block(opts)

Once a block instance is created the information represented in the block is considered immutable.

block.decode()

Returns decoded block data.

A new object is returned on every call that is copied from a cached version of the decoded block. This means you are free to mutate the return value and that we'll never do more than one actual block decode in the codec.

block.decodeUnsafe()

Returns the cached block data directly.

Waring: If you mutate the return value bad things will happen.

This operation is very fast and useful as long as you can ensure the return value will not be mutated.

block.cid()

Promise that resolves to a cid instance. Cached after creation.

block.encode()

Returns a Buffer instance encoded from the source input.

The first time the block is encoded it is cached indefinitely. This method returns a new copied buffer that you are free to mutate.

block.encodeUnsafe()

Returns a Buffer instance encoded from the source input.

Warning: If you mutate the return value bad things will happen.

This returns the internal cached versoin of the block encode. It's very fast and useful if you are certain the buffer won't be mutated.

block.reader()

Returns an instance of Reader() from the codec implementation.

block.validate()

Returns true/false if the CID's multihash matches the given block.

If a CID hasn't been created yet it will return true since we know the hash will match in our eventually created CID.