hypercore
Advanced tools
Comparing version 10.37.0 to 10.37.1
@@ -28,2 +28,3 @@ const { BLOCK_NOT_AVAILABLE, SESSION_CLOSED } = require('hypercore-errors') | ||
this._sessionBatch = null | ||
this._cachedBatch = null | ||
this._flushing = null | ||
@@ -211,17 +212,29 @@ this._clear = clear | ||
createTreeBatch (length, blocks = []) { | ||
_catchupBatch (clone) { | ||
if (this._cachedBatch === null) this._cachedBatch = this._sessionBatch.clone() | ||
if (this.length > this._cachedBatch.length) { | ||
const offset = this._cachedBatch.length - this._sessionBatch.length | ||
for (let i = offset; i < this._appendsActual.length; i++) { | ||
this._cachedBatch.append(this._appendsActual[i]) | ||
} | ||
} | ||
return clone ? this._cachedBatch.clone() : this._cachedBatch | ||
} | ||
createTreeBatch (length, opts = {}) { | ||
if (Array.isArray(opts)) opts = { blocks: opts } | ||
const { blocks = [], clone = true } = opts | ||
if (!length && length !== 0) length = this.length + blocks.length | ||
const maxLength = this.length + blocks.length | ||
const b = this._sessionBatch.clone() | ||
const b = this._catchupBatch(clone || (blocks.length > 0 || length !== this.length)) | ||
const len = Math.min(length, this.length) | ||
if (len < this._sessionLength || length > maxLength) return null | ||
if (len < b.length) b.checkout(len, this._sessionBatch.roots) | ||
for (let i = 0; i < len - this._sessionLength; i++) { | ||
b.append(this._appendsActual[i]) | ||
} | ||
if (len < this.length) return b | ||
for (let i = 0; i < length - len; i++) { | ||
@@ -244,2 +257,4 @@ b.append(this._appendsActual === this._appends ? blocks[i] : this._encrypt(b.length, blocks[i])) | ||
this._cachedBatch = null | ||
const length = this._sessionLength | ||
@@ -386,2 +401,4 @@ if (newLength < length) { | ||
if (this._cachedBatch !== null) this._cachedBatch.prune(info.length) | ||
const same = this._appends === this._appendsActual | ||
@@ -388,0 +405,0 @@ |
@@ -60,2 +60,64 @@ const flat = require('flat-tree') | ||
checkout (length, additionalRoots) { | ||
const roots = [] | ||
let r = 0 | ||
const head = 2 * length - 2 | ||
const gaps = new Set() | ||
const all = new Map() | ||
// additional roots is so the original roots can be passed (we mutate the array in appendRoot) | ||
if (additionalRoots) { | ||
for (const node of additionalRoots) all.set(node.index, node) | ||
} | ||
for (const node of this.nodes) all.set(node.index, node) | ||
for (const index of flat.fullRoots(head + 2)) { | ||
const left = flat.leftSpan(index) | ||
if (left !== 0) gaps.add(left - 1) | ||
if (r < this.roots.length && this.roots[r].index === index) { | ||
roots.push(this.roots[r++]) | ||
continue | ||
} | ||
const node = all.get(index) | ||
if (!node) throw new BAD_ARGUMENT('root missing for given length') | ||
roots.push(node) | ||
} | ||
this.roots = roots | ||
this.length = length | ||
this.byteLength = totalSize(roots) | ||
this.hashCached = null | ||
this.signature = null | ||
for (let i = 0; i < this.nodes.length; i++) { | ||
const index = this.nodes[i].index | ||
if (index <= head && !gaps.has(index)) continue | ||
const last = this.nodes.pop() | ||
if (i < this.nodes.length) this.nodes[i--] = last | ||
} | ||
} | ||
prune (length) { | ||
if (length === 0) return | ||
const head = 2 * length - 2 | ||
const gaps = new Set() | ||
// TODO: make a function for this in flat-tree | ||
for (const index of flat.fullRoots(head + 2)) { | ||
const left = flat.leftSpan(index) | ||
if (left !== 0) gaps.add(left - 1) | ||
} | ||
for (let i = 0; i < this.nodes.length; i++) { | ||
const index = this.nodes[i].index | ||
if (index > head || gaps.has(index)) continue | ||
const last = this.nodes.pop() | ||
if (i < this.nodes.length) this.nodes[i--] = last | ||
} | ||
} | ||
clone () { | ||
@@ -62,0 +124,0 @@ const b = new MerkleTreeBatch(this.tree) |
@@ -233,3 +233,7 @@ const BigSparseArray = require('big-sparse-array') | ||
return val ? -1 : this._maxSegments * BITS_PER_SEGMENT | ||
// For the val === false case, we always return at least | ||
// the 'position', also if nothing was found | ||
return val | ||
? -1 | ||
: Math.max(position, this._maxSegments * BITS_PER_SEGMENT) | ||
} | ||
@@ -236,0 +240,0 @@ |
{ | ||
"name": "hypercore", | ||
"version": "10.37.0", | ||
"version": "10.37.1", | ||
"description": "Hypercore is a secure, distributed append-only log", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
271797
7615