From 99b4f42ee0c0ec2ad1a12f99345f691fb57e65fe Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Sat, 2 May 2026 18:26:05 +0200 Subject: [PATCH] allow marking the active request array as dead --- index.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 48858621..bd26e1b9 100644 --- a/index.js +++ b/index.js @@ -34,7 +34,8 @@ const { SESSION_MOVED, SESSION_NOT_WRITABLE, SNAPSHOT_NOT_AVAILABLE, - DECODING_ERROR + DECODING_ERROR, + REQUEST_CANCELLED } = require('hypercore-errors') // Hypercore actually does not have any notion of max/min block sizes @@ -736,6 +737,8 @@ class Hypercore extends EventEmitter { if (!upgraded && remoteWait) { const activeRequests = (opts && opts.activeRequests) || this.activeRequests + if (isRequestsDestroyed(activeRequests)) throw REQUEST_CANCELLED() + const req = this.core.replicator.addUpgrade(activeRequests) try { @@ -755,6 +758,7 @@ class Hypercore extends EventEmitter { if (!isValidIndex(bytes)) throw ASSERTION('seek is invalid', this.discoveryKey) const activeRequests = (opts && opts.activeRequests) || this.activeRequests + if (isRequestsDestroyed(activeRequests)) throw REQUEST_CANCELLED() if (this.encryption && !this.core.manifest) { const req = this.replicator.addUpgrade(activeRequests) @@ -777,6 +781,7 @@ class Hypercore extends EventEmitter { if (!this._shouldWait(opts, this.wait)) return null + if (isRequestsDestroyed(activeRequests)) throw REQUEST_CANCELLED() const req = this.core.replicator.addSeek(activeRequests, s) const timeout = opts && opts.timeout !== undefined ? opts.timeout : this.timeout @@ -916,6 +921,7 @@ class Hypercore extends EventEmitter { if (this.onwait) this.onwait(index, this) const activeRequests = (opts && opts.activeRequests) || this.activeRequests + if (isRequestsDestroyed(activeRequests)) throw REQUEST_CANCELLED() const force = opts ? opts.force === true : false const req = this.core.replicator.addBlock(activeRequests, index, force) @@ -1393,3 +1399,8 @@ function createDiscoveryKeyHandler(fn) { return fn(id) } } + +function isRequestsDestroyed(activeRequests) { + // TODO: move this to an object instead so we can store a property in next major + return activeRequests.length > 0 && activeRequests[0] === null +}