How to use cids - 10 common examples

To help you get started, we’ve selected a few cids examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ipfs / js-ipfs / test / core / pin.js View on Github external
.then(result => {
        expect(result.pinned).to.eql(pinned)
        if (type === pinTypes.indirect) {
          // indirect pins return a CID of recursively pinned root instead of 'indirect' string
          expect(CID.isCID(result.reason)).to.be.true()
        } else if (type !== pinTypes.all) {
          expect(result.reason).to.eql(type)
        }
      })
  }
github ipfs / js-ipfs-http-client / src / utils / clean-cid.js View on Github external
module.exports = function (cid) {
  if (Buffer.isBuffer(cid)) {
    return new CID(cid).toString()
  }
  if (CID.isCID(cid)) {
    return cid.toString()
  }
  if (typeof cid !== 'string') {
    throw new Error('unexpected cid type: ' + typeof cid)
  }
  new CID(cid.split('/')[0]) // eslint-disable-line
  return cid
}
github ipfs / js-ipfs / src / core / components / block.js View on Github external
// blocks are exclusive operations
    const release = await self._gcLock.writeLock()

    try {
      for (let cid of cids) {
        cid = cleanCid(cid)

        const result = {
          hash: cid.toString()
        }

        try {
          const pinResult = await self.pin.pinManager.isPinnedWithType(cid, PinTypes.all)

          if (pinResult.pinned) {
            if (CID.isCID(pinResult.reason)) { // eslint-disable-line max-depth
              throw errCode(new Error(`pinned via ${pinResult.reason}`))
            }

            throw errCode(new Error(`pinned: ${pinResult.reason}`))
          }

          // remove has check when https://github.com/ipfs/js-ipfs-block-service/pull/88 is merged
          const has = await self._blockService._repo.blocks.has(cid)

          if (!has) {
            throw errCode(new Error('block not found'), 'ERR_BLOCK_NOT_FOUND')
          }

          await self._blockService.delete(cid)
        } catch (err) {
          if (!options.force) {
github ipfs / js-ipfs / src / core / components / block / rm.js View on Github external
parallelMap(BLOCK_RM_CONCURRENCY, async cid => {
          cid = cleanCid(cid)

          const result = { hash: cid.toString() }

          try {
            const pinResult = await pinManager.isPinnedWithType(cid, PinTypes.all)

            if (pinResult.pinned) {
              if (CID.isCID(pinResult.reason)) { // eslint-disable-line max-depth
                throw errCode(new Error(`pinned via ${pinResult.reason}`))
              }

              throw errCode(new Error(`pinned: ${pinResult.reason}`))
            }

            // remove has check when https://github.com/ipfs/js-ipfs-block-service/pull/88 is merged
            const has = await blockService._repo.blocks.has(cid)

            if (!has) {
              throw errCode(new Error('block not found'), 'ERR_BLOCK_NOT_FOUND')
            }

            await blockService.delete(cid)
          } catch (err) {
            if (!options.force) {
github ipfs / js-ipfs / src / core / components / pin / pin-manager.js View on Github external
async _walkDag ({ cid, preload = false, onCid = () => {} }) {
    if (!CID.isCID(cid)) {
      cid = new CID(cid)
    }

    const walk = (cid) => {
      return async () => {
        const { value: node } = await this.dag.get(cid, { preload })

        onCid(cid)

        if (cid.codec === 'dag-pb') {
          queue.addAll(
            node.Links.map(link => walk(link.Hash))
          )
        } else if (cid.codec === 'dag-cbor') {
          for (const [_, childCid] of dagCborLinks(node)) { // eslint-disable-line no-unused-vars
            queue.add(walk(childCid))
github rvagg / iamap / examples / level-backed.js View on Github external
async function createMap (id) {
  if (id) { // existing
    if (!CID.isCID(id)) {
      id = new CID(id)
    }
    return iamap.load(store, id)
  }
  // new map with default options, our hasher and custom store
  return iamap.create(store, { hashAlg: 'murmur3-32' })
}
github ipld / js-ipld / src / index.js View on Github external
resolve (cid, path) {
    if (!CID.isCID(cid)) {
      throw new Error('`cid` argument must be a CID')
    }
    if (typeof path !== 'string') {
      throw new Error('`path` argument must be a string')
    }

    const generator = async function * () {
      // End iteration if there isn't a CID to follow anymore
      while (cid !== null) {
        const format = await this._getFormat(cid.codec)

        // get block
        // use local resolver
        // update path value
        const block = await promisify(this.bs.get.bind(this.bs))(cid)
        const result = format.resolver.resolve(block.data, path)
github cloudflare / ipfs-ext / node_modules / ipld-dag-pb / src / resolver.js View on Github external
const traverse = function * (node, path) {
  // Traverse only objects and arrays
  if (Buffer.isBuffer(node) || CID.isCID(node) || typeof node === 'string' ||
      node === null) {
    return
  }
  for (const item of Object.keys(node)) {
    const nextpath = path === undefined ? item : path + '/' + item
    yield nextpath
    yield * traverse(node[item], nextpath)
  }
}
github ipld / js-ipld-dag-cbor / src / resolver.js View on Github external
const traverse = function * (node, path) {
  // Traverse only objects and arrays
  if (Buffer.isBuffer(node) || CID.isCID(node) || typeof node === 'string' ||
      node === null) {
    return
  }
  for (const item of Object.keys(node)) {
    const nextpath = path === undefined ? item : path + '/' + item
    yield nextpath
    yield * traverse(node[item], nextpath)
  }
}
github ipld / js-ipld-ethereum / util / createResolver.js View on Github external
const _traverse = function * (node, path) {
    // Traverse only objects and arrays
    if (Buffer.isBuffer(node) || CID.isCID(node) || typeof node === 'string' ||
        node === null) {
      return
    }
    for (const item of Object.keys(node)) {
      const nextpath = path === undefined ? item : path + '/' + item
      yield nextpath
      yield * _traverse(node[item], nextpath)
    }
  }

cids

CID Implementation in JavaScript

MIT
Latest version published 3 years ago

Package Health Score

56 / 100
Full package analysis

Popular cids functions