Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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')
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())
}
beforeEach(() => {
keyRef = `key${testCounter}`
key = (new Key(keyRef)).toBuffer()
record = new Record(key, Buffer.from(value))
serializedRecord = record.serialize()
})
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
}
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
}
}
.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 => {