How to use the libp2p-record.Record function in libp2p-record

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-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-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