How to use libp2p-record - 10 common examples

To help you get started, we’ve selected a few libp2p-record 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-datastore-pubsub / test / index.spec.js View on Github external
it('should get the second record if the selector selects it as the newest one', async () => {
    const customValidator = {
      validate: () => {
        return true
      },
      select: () => {
        return 1 // current record is the newer
      }
    }

    const newValue = 'new value'
    const record = new Record(key, Buffer.from(newValue))
    const newSerializedRecord = record.serialize()

    const dsPubsubA = new DatastorePubsub(pubsubA, datastoreA, peerIdA, smoothValidator)
    const dsPubsubB = new DatastorePubsub(pubsubB, datastoreB, peerIdB, customValidator)
    const subsTopic = keyToTopic(`/${keyRef}`)
    let receivedMessage = false

    function messageHandler () {
      receivedMessage = true
    }

    // causes pubsub b to become subscribed to the topic
    await dsPubsubB.get(key)
      .then(() => expect.fail('Should have failed to fetch key'), (err) => {
        // not locally stored record
        expect(err.code).to.equal('ERR_NOT_FOUND')
github ipfs / js-datastore-pubsub / test / index.spec.js View on Github external
// not locally stored record
        expect(err.code).to.equal('ERR_NOT_FOUND')
      })

    await waitForPeerToSubscribe(subsTopic, peerIdB, pubsubA)

    // subscribe in order to understand when the message arrive to the node
    await pubsubB.subscribe(subsTopic, messageHandler)
    await dsPubsubA.put(key, serializedRecord)

    // wait until message arrives
    await waitFor(() => receivedMessage === true)

    // get from datastore
    const result = await dsPubsubB.get(keyNew)
    const receivedRecord = Record.deserialize(result)

    expect(receivedRecord.value.toString()).to.equal(value)
  })
github ipfs / js-datastore-pubsub / test / index.spec.js View on Github external
validate: (data) => {
        const receivedRecord = Record.deserialize(data)

        expect(receivedRecord.value.toString()).to.equal(value) // validator should deserialize correctly

        return receivedRecord.value.toString() === value
      },
      select: () => {
github ipfs / js-ipfs / src / core / ipns / routing / offline-datastore.js View on Github external
if (!Buffer.isBuffer(value)) {
      throw errcode(new Error('Offline datastore value must be a buffer'), 'ERR_INVALID_VALUE')
    }

    let routingKey

    try {
      routingKey = this._routingKey(key)
    } catch (err) {
      log.error(err)
      throw errcode(new Error('Not possible to generate the routing key'), 'ERR_GENERATING_ROUTING_KEY')
    }

    // Marshal to libp2p record as the DHT does
    const record = new Record(key, value)

    return this._repo.datastore.put(routingKey, record.serialize())
  }
github ipfs / js-ipfs / src / core / ipns / routing / offline-datastore.js View on Github external
let routingKey

    try {
      routingKey = this._routingKey(key)
    } catch (err) {
      log.error(err)
      throw errcode(new Error('Not possible to generate the routing key'), 'ERR_GENERATING_ROUTING_KEY')
    }

    const res = await this._repo.datastore.get(routingKey)

    // Unmarshal libp2p record as the DHT does
    let record
    try {
      record = Record.deserialize(res)
    } catch (err) {
      log.error(err)
      throw (err)
    }

    return record.value
  }
github libp2p / js-libp2p-distributed-record-store / src / index.js View on Github external
self.put = function (key, recordSignature, callback) {
    // 1. check if valid
    // 2. do the mapping
    // 3. add to other recordStores if any

    var recordSignatureMH = multihashing(ipld.marshal(recordSignature), 'sha2-256')
    var isValid = iprs.validator(recordSignatureMH, self.mdagStore)

    if (!isValid) {
      return callback(new Error('record is not valid'))
    }

    if (!self.mapping[key]) {
      self.mapping[key] = []
    }
    self.mapping[key].push(recordSignature)

    callback()
  }
}
github ipfs / js-datastore-pubsub / test / index.spec.js View on Github external
beforeEach(() => {
    keyRef = `key${testCounter}`
    key = (new Key(keyRef)).toBuffer()
    record = new Record(key, Buffer.from(value))

    serializedRecord = record.serialize()
  })
github ipfs / js-ipfs / src / core / ipns / routing / experimental / utils.js View on Github external
const result = await ky
    .get(url, {
      searchParams: {
        dns: buf.toString('base64')
      },
      headers: {
        accept: 'application/dns-message'
      }
    })
    .arrayBuffer()

  const data = dnsPacket.decode(Buffer.from(result))
  if (!data || data.answers.length < 1) {
    throw errcode(new Error('Record not found'), 'ERR_NOT_FOUND')
  }
  const record = new Record(key, Buffer.from(Buffer.concat(data.answers[0].data).toString(), 'base64'))
  console.log(`Resolved ${keyStr}.${domain} in ${(Date.now() - start)}ms`)

  return record.value
}
github ipfs / js-ipfs / src / core / ipns / routing / experimental / workers-datastore.js View on Github external
if (!Buffer.isBuffer(key)) {
      throw errcode(new Error(`Workers datastore key must be a buffer`), 'ERR_INVALID_KEY')
    }

    const keyStr = keyToBase32(key)

    const data = await ky
      .get('https://workers.ipns.dev', {
        searchParams: {
          key: keyStr
        }
      })
      .text()

    const record = new Record(key, Buffer.from(data, 'base64'))
    console.log(`Resolved ${keyStr} with workers in: ${(Date.now() - start)}ms`)

    return record.value
  }
}
github ipfs / js-ipfs / src / core / ipns / routing / dns-datastore.js View on Github external
.then(data => {
      data = dnsPacket.decode(Buffer.from(data))
      console.log('TCL: dohBinary -> data', data)

      if (!data && data.answers.length < 1) {
        throw errcode(new Error('Record not found'), 'ERR_NOT_FOUND')
      }
      console.log('TCL: doh -> data', data)
      const record = new Record(key, Buffer.from(Buffer.concat(data.answers[0].data).toString(), 'base64'))
      setImmediate(() => callback(null, record.value))
    })
    .catch(err => {

libp2p-record

libp2p record implementation

MIT
Latest version published 3 years ago

Package Health Score

48 / 100
Full package analysis