corestore 6.0.1-alpha.7 → 6.0.1-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +12 -5
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -123,7 +123,7 @@ module.exports = class Corestore extends EventEmitter {
123
123
  })
124
124
 
125
125
  this.cores.set(id, core)
126
- for (const stream of this._replicationStreams) {
126
+ for (const { stream } of this._replicationStreams) {
127
127
  core.replicate(stream)
128
128
  }
129
129
  core.once('close', () => {
@@ -144,6 +144,7 @@ module.exports = class Corestore extends EventEmitter {
144
144
  }
145
145
 
146
146
  replicate (isInitiator, opts) {
147
+ const isExternal = isStream(isInitiator) || !!(opts && opts.stream)
147
148
  const stream = Hypercore.createProtocolStream(isInitiator, opts)
148
149
  for (const core of this.cores.values()) {
149
150
  core.replicate(stream)
@@ -156,9 +157,10 @@ module.exports = class Corestore extends EventEmitter {
156
157
  stream.close(discoveryKey)
157
158
  })
158
159
  })
159
- this._replicationStreams.push(stream)
160
+ const streamRecord = { stream, isExternal }
161
+ this._replicationStreams.push(streamRecord)
160
162
  stream.once('close', () => {
161
- this._replicationStreams.splice(this._replicationStreams.indexOf(stream), 1)
163
+ this._replicationStreams.splice(this._replicationStreams.indexOf(streamRecord), 1)
162
164
  })
163
165
  return stream
164
166
  }
@@ -182,8 +184,9 @@ module.exports = class Corestore extends EventEmitter {
182
184
  closePromises.push(core.close())
183
185
  }
184
186
  await Promise.allSettled(closePromises)
185
- for (const stream of this._replicationStreams) {
186
- stream.destroy()
187
+ for (const { stream, isExternal } of this._replicationStreams) {
188
+ // Only close streams that were created by the Corestore
189
+ if (!isExternal) stream.destroy()
187
190
  }
188
191
  await this.keys.close()
189
192
  }
@@ -226,4 +229,8 @@ function generateNamespace (first, second) {
226
229
  return out
227
230
  }
228
231
 
232
+ function isStream (s) {
233
+ return typeof s === 'object' && s && typeof s.pipe === 'function'
234
+ }
235
+
229
236
  function noop () {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.0.1-alpha.7",
3
+ "version": "6.0.1-alpha.8",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "scripts": {