Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
MultiFeed.prototype._addFeed = function (key) {
if (key.equals(this.local.key)) {
if (this.feeds.indexOf(this.local) > -1) return false
this.feeds.push(this.local)
this.emit('add-feed', this.local)
return true
}
for (var i = 0; i < this.feeds.length; i++) {
if (this.feeds[i].key.equals(key)) return false
}
var dk = hypercore.discoveryKey(key).toString('hex')
var feed = this._createFeed('feeds/' + dk.slice(0, 2) + '/' + dk.slice(2, 4) + '/' + dk.slice(4), key)
this.feeds.push(feed)
this.emit('add-feed', feed)
return true
}
Archiver.prototype.replicate = function (opts) {
if (!opts) opts = {}
if (opts.discoveryKey) opts.discoveryKey = toBuffer(opts.discoveryKey, 'hex')
if (opts.key) opts.discoveryKey = hypercore.discoveryKey(toBuffer(opts.key, 'hex'))
var stream = protocol({live: true, id: this.changes.id, encrypt: opts.encrypt})
var self = this
stream.on('feed', add)
if (opts.channel || opts.discoveryKey) add(opts.channel || opts.discoveryKey)
function add (dk) {
self.ready(function (err) {
if (err) return stream.destroy(err)
if (stream.destroyed) return
var hex = dk.toString('hex')
var changesHex = self.changes.discoveryKey.toString('hex')
var archive = self.archives[hex]
Archiver.prototype.replicate = function (opts) {
if (!opts) opts = {}
if (opts.discoveryKey) opts.discoveryKey = toBuffer(opts.discoveryKey, 'hex')
if (opts.key) opts.discoveryKey = hypercore.discoveryKey(toBuffer(opts.key, 'hex'))
const protocolOpts = {
live: true,
id: this.changes.id,
encrypt: opts.encrypt
}
if (opts.userData) {
protocolOpts.userData = opts.userData
}
if (opts.timeout) {
protocolOpts.timeout = opts.timeout
}
var stream = protocol(protocolOpts)
var self = this
stream.on('feed', add)
function storage (key) {
var dk = hypercore.discoveryKey(key).toString('hex')
var prefix = dk.slice(0, 2) + '/' + dk.slice(2, 4) + '/' + dk.slice(4) + '/'
return function (name) {
return self.storage.feeds(prefix + name)
}
}
}
function storage (key) {
var dk = hypercore.discoveryKey(key).toString('hex')
var prefix = dk.slice(0, 2) + '/' + dk.slice(2, 4) + '/' + dk.slice(4) + '/'
return function (name) {
return self.storage.feeds(prefix + name)
}
}
}
replicate(opts) {
this._ensureReady()
log('replicate')
if (!opts) {
opts = {}
}
if (opts.discoveryKey) {
opts.discoveryKey = toBuffer(opts.discoveryKey, 'hex')
}
if (opts.key) {
opts.discoveryKey = Hypercore.discoveryKey(toBuffer(opts.key, 'hex'))
}
const { archiver } = this
const stream = HypercoreProtocol({
live: true,
id: archiver.changes.id,
encrypt: opts.encrypt,
extensions: ['hypermerge']
})
stream.on('feed', add)
if (opts.channel || opts.discoveryKey) {
add(opts.channel || opts.discoveryKey)
}
_feedStorage(key) {
const dk = Hypercore.discoveryKey(key).toString("hex")
const prefix = `${dk.slice(0, 2)}/${dk.slice(2, 4)}/${dk.slice(4)}/`
return name => this.archiver.storage.feeds(prefix + name)
}
_feedStorage(key) {
const dk = Hypercore.discoveryKey(key).toString('hex')
const prefix = `${dk.slice(0, 2)}/${dk.slice(2, 4)}/${dk.slice(4)}/`
return (name) => this.archiver.storage.feeds(prefix + name)
}
export function discoveryKey(buf: Buffer): Buffer {
return _hypercore.discoveryKey(buf)
}
function HyperDB (storage, key, opts) {
if (!(this instanceof HyperDB)) return new HyperDB(storage, key, opts)
events.EventEmitter.call(this)
if (isOptions(key)) {
opts = key
key = null
}
opts = Object.assign({}, opts)
if (opts.firstNode) opts.reduce = reduceFirst
var checkout = opts.checkout
this.key = typeof key === 'string' ? Buffer.from(key, 'hex') : key
this.discoveryKey = this.key ? hypercore.discoveryKey(this.key) : null
this.source = checkout ? checkout.source : null
this.local = checkout ? checkout.local : null
this.localContent = checkout ? checkout.localContent : null
this.feeds = checkout ? checkout.feeds : []
this.contentFeeds = checkout ? checkout.contentFeeds : (opts.contentFeed ? [] : null)
this.ready = thunky(this._ready.bind(this))
this.opened = false
this.sparse = !!opts.sparse
this.sparseContent = opts.sparseContent !== undefined ? !!opts.sparseContent : this.sparse
this.id = Buffer.alloc(32)
sodium.randombytes_buf(this.id)
this._storage = createStorage(storage)
this._contentStorage = typeof opts.contentFeed === 'function'
? opts.contentFeed
: opts.contentFeed ? this._storage : null