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

cardex

v3.0.2

Published

Indexes for CARs.

Downloads

388

Readme

cardex

Build JavaScript Style Guide npm bundle size

Indexes for CARs.

Implementations of CARv2 indexes in JavaScript. Status:

Install

npm install cardex

Usage

Write index

import fs from 'fs'
import { Readable } from 'stream'
import { CarIndexer } from '@ipld/car/indexer'
import { IndexSortedWriter } from 'cardex'

const carStream = fs.createReadStream('my.car')
const indexer = await CarIndexer.fromIterable(carStream)

const { readable, writable } = new TransformStream()
const writer = IndexSortedWriter.createWriter({ writer: writable.getWriter() })

readable.pipeTo(Readable.toWeb(fs.createWriteStream('my.car.idx')))

for await (const { cid, offset } of indexer) {
  await writer.add(cid, offset)
}
await writer.close()

Read index

import fs from 'fs'
import { Readable } from 'stream'
import { IndexSortedReader } from 'cardex'

const carStream = fs.createReadStream('my.car.idx')
const reader = IndexSortedReader.createReader({ reader: Readable.toWeb(carStream).getReader() })

while (true) {
  const { done, value } = await reader.read()
  if (done) break
  console.log(`${Buffer.from(value.digest).toString('hex')} @ ${value.offset}`)
}

Multi-index index

The multi-index index is a custom index allowing multiple CAR indexes to be grouped together in a single index.

Write multi-index

import { MultihashIndexSortedWriter } from 'cardex'
import { MultiIndexWriter } from 'cardex/multi-index'

const { readable, writable } = new TransformStream()
const writer = MultiIndexWriter.createWriter({ writer: writable.getWriter() })

readable.pipeTo(new WritableStream()) // destination

writer.add(carCID0, async ({ writer }) => {
  const index0 = MultihashIndexSortedWriter.createWriter({ writer })
  index0.add(cid, offset)
  await index0.close()
})

writer.add(carCID1, async ({ writer }) => {
  const index1 = MultihashIndexSortedWriter.createWriter({ writer })
  index1.add(cid, offset)
  await index1.close()
})

await writer.close()

Read multi-index

import { MultihashIndexSortedReader, IndexSortedReader } from 'cardex'
import { MultiIndexReader } from 'cardex/multi-index'

const readable = new ReadableStream() // reader of a multi-index bytes
const reader = MultiIndexReader.createReader({ reader: readable.getReader() })

// add readers to the multi-index reader (to allow reading different index types)
reader.add(MultihashIndexSortedReader)
reader.add(IndexSortedReader)

while (true) {
  const { done, value } = await reader.read()
  if (done) break

  const { origin, multihash, digest, offset } = value // (origin is a CAR CID)
  console.log(`${origin} -> ${CID.createV1(raw.code, multihash)} @ ${offset}`)
}

Universal reader

The universal reader is for when you don't know what type of CARv2 index you're reading. The universal reader automatically instantiates the correct reader for a given index based on the codec:

import fs from 'fs'
import { Readable } from 'stream'
import { UniversalReader } from 'cardex/universal'

const carStream = fs.createReadStream('my.car.idx')
const reader = UniversalReader.createReader({ reader: Readable.toWeb(carStream).getReader() })

while (true) {
  const { done, value } = await reader.read()
  if (done) break
  console.log(`${Buffer.from(value.digest).toString('hex')} @ ${value.offset}`)
  // Note: `value` might have `multihash` if reading from MultihashIndexSorted
  // and it might have `origin` if reading from `MultiIndex`.
}

Contributing

Feel free to join in. All welcome. Open an issue!

License

Dual-licensed under MIT + Apache 2.0