Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hypercore

Package Overview
Dependencies
Maintainers
1
Versions
498
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hypercore - npm Package Compare versions

Comparing version 1.3.1 to 1.4.0

5

index.js

@@ -9,2 +9,3 @@ var subleveldown = require('subleveldown')

var util = require('util')
var crypto = require('crypto')

@@ -20,3 +21,3 @@ module.exports = Hypercore

this.db = db
this.cache = subleveldown(db, 'cache', {valueEncoding: 'binary'})
this.id = opts.id || crypto.randomBytes(32)
this._hashes = subleveldown(db, 'hashes', {valueEncoding: 'binary'})

@@ -26,2 +27,3 @@ this._blocks = subleveldown(db, 'blocks', {valueEncoding: 'binary'})

this._feeds = subleveldown(db, 'feeds', {valueEncoding: messages.Feed})
this._cache = subleveldown(db, 'data', {valueEncoding: 'binary'})
this._storage = opts.storage || null

@@ -62,2 +64,3 @@ this._opened = {}

fd = this._opened[id] = feed(this, link, opts)
if (this.swarm.joined[id]) this.swarm.joined[id].open(fd)
this.emit('interested', fd.id)

@@ -64,0 +67,0 @@ return fd

44

lib/feed.js

@@ -35,2 +35,4 @@ var flat = require('flat-tree')

this.want = []
this.cache = null
this.options = opts

@@ -62,2 +64,3 @@ this._core = core

self.pointer = opts.pointer || crypto.randomBytes(32)
self.cache = new Cache(self.pointer, self._core._cache)
self._prefix = prefix(self.pointer.toString('hex'), '')

@@ -74,2 +77,3 @@ return cb()

}
self.cache = new Cache(self.pointer, self._core._cache)
self._prefix = prefix(self.pointer.toString('hex'), '')

@@ -153,14 +157,23 @@ self._core._bitfields.get(self.id.toString('hex'), function (_, buf) {

var self = this
this.flush(function (err) {
if (this._storage && this._storage.finalize) this._storage.finalize(done)
else done()
function done (err) {
if (err) return cb(err)
if (!self.blocks || self.id) return cb(null)
self.id = hash.root(self._generator.roots)
self.bitfield = fullBitfield(self.blocks)
self._sync(true, function (err) {
self.flush(function (err) {
if (err) return cb(err)
self._core.swarm.join(self.id)
self.emit('finalize')
cb()
if (!self.blocks || self.id) return cb(null)
self.id = hash.root(self._generator.roots)
self.bitfield = fullBitfield(self.blocks)
self._sync(true, function (err) {
if (err) return cb(err)
self._core._opened[self.id.toString('hex')] = self
self._core.swarm.join(self.id)
self.emit('finalize')
cb()
})
})
})
}
}

@@ -501,1 +514,14 @@

}
function Cache (id, db) {
this.prefix = prefix(id.toString('hex'), '')
this.db = db
}
Cache.prototype.get = function (key, opts, cb) {
this.db.get(this.prefix + key, opts, cb)
}
Cache.prototype.put = function (key, val, opts, cb) {
this.db.put(this.prefix + key, val, opts, cb)
}

@@ -46,33 +46,50 @@ var protocol = require('./protocol')

id: id,
feed: this.core.get(link),
feed: null,
link: link,
peers: [],
fetch: fetch
fetch: fetch,
open: open
}
self.prioritized += subswarm.feed.want.length
if (this.core._opened[id]) open(this.core.get(link))
this.joined[id] = subswarm
return subswarm
subswarm.feed.on('want', function (block) {
self.prioritized++
debug('prioritizing block %d (%d)', block, self.prioritized)
subswarm.fetch()
})
function open (feed) {
if (subswarm.feed) return
subswarm.feed = feed
self.prioritized += subswarm.feed.want.length
subswarm.feed.on('unwant', function (block) {
self.prioritized--
debug('deprioritizing block %d (%d)', block, self.prioritized)
if (!self.prioritized) subswarm.fetch()
})
subswarm.feed.on('want', function (block) {
self.prioritized++
debug('prioritizing block %d (%d)', block, self.prioritized)
subswarm.fetch()
})
subswarm.feed.on('put', function (block) {
for (var i = 0; i < subswarm.peers.length; i++) {
subswarm.peers[i].have(block)
}
})
subswarm.feed.on('unwant', function (block) {
self.prioritized--
debug('deprioritizing block %d (%d)', block, self.prioritized)
if (!self.prioritized) subswarm.fetch()
})
this.joined[id] = subswarm
return subswarm
subswarm.feed.on('put', function (block) {
for (var i = 0; i < subswarm.peers.length; i++) {
subswarm.peers[i].have(block)
}
})
subswarm.peers.forEach(function (ch) {
subswarm.feed.open(function (err) { // TODO: hackish for now
if (err) return ch.leave(err)
ch.bitfield(subswarm.feed.bitfield)
ch.unpause() // for now always unpause. #yolo
process.nextTick(function () {
subswarm.fetch(ch)
})
})
})
}
function fetch (peer) {
if (!subswarm.feed.opened) return
if (!subswarm.feed || !subswarm.feed.opened) return
debug('should try to fetch')

@@ -99,8 +116,11 @@

// so we wont fetch the same data from multiple peers
if (!subswarm.feed) return -3 // feed half open
var len = peer.remoteBitfield.buffer.length * 8
var block = -1
var critical = false
for (var j = 0; j < subswarm.feed.want.length; j++) {
block = subswarm.feed.want[j].block
if (subswarm.feed.want[j].critical) critical = true
if (peer.amRequesting.get(block)) continue

@@ -113,6 +133,11 @@ if (peer.remoteBitfield.get(block) && !subswarm.feed.bitfield.get(block)) {

if (critical) {
debug('not downloading any non-critical blocks')
return -4
}
// TODO: there might be a starvation convern here. should only return *if* there are peers that
// that could satisfy the want list. this is just a quick "hack" for realtime prioritization
// when dealing with multiple files
if (self.prioritized && !subswarm.feed.want.length) {
if (self.prioritized > 0 && !subswarm.feed.want.length) { // self.prioritized < 0 sometimes. that's a bug though :/
debug('not downloading to yield to prioritized downloading')

@@ -157,3 +182,3 @@ return -1

var self = this
var peer = protocol({extensions: self.extensions})
var peer = protocol({id: this.core.id, extensions: self.extensions})

@@ -196,2 +221,3 @@ debug('new peer stream')

debug('[channel %s] rcvd response #%d (%d bytes, proof contained %d hashes)', name, block, data.length, proof.length)
if (!subswarm.feed) return
subswarm.fetch(ch)

@@ -206,2 +232,3 @@ subswarm.feed.put(block, data, proof, function (err) {

debug('[channel %s] rcvd request #%d', name, block)
if (!subswarm.feed) return
subswarm.feed.get(block, function (err, data) {

@@ -225,2 +252,3 @@ if (err) return ch.leave(err)

if (!subswarm.feed) return
subswarm.feed.open(function (err) {

@@ -230,3 +258,5 @@ if (err) return ch.leave(err)

ch.unpause() // for now always unpause. #yolo
subswarm.fetch(ch)
process.nextTick(function () {
subswarm.fetch(ch)
})
})

@@ -233,0 +263,0 @@ }

{
"name": "hypercore",
"version": "1.3.1",
"version": "1.4.0",
"description": "Hypercore is a protocol and network for distributing and replicating static feeds of binary data.",

@@ -5,0 +5,0 @@ "main": "index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc