corestore 6.0.3 → 6.0.6

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.
package/index.js CHANGED
@@ -26,6 +26,8 @@ module.exports = class Corestore extends EventEmitter {
26
26
  this._keyStorage = null
27
27
  this._primaryKey = opts.primaryKey
28
28
  this._namespace = opts.namespace || DEFAULT_NAMESPACE
29
+
30
+ this._root = opts._root || this
29
31
  this._replicationStreams = opts._streams || []
30
32
  this._overwrite = opts.overwrite === true
31
33
 
@@ -37,6 +39,7 @@ module.exports = class Corestore extends EventEmitter {
37
39
 
38
40
  if (this._namespace.byteLength !== 32) throw new Error('Namespace must be a 32-byte Buffer or Uint8Array')
39
41
 
42
+ this._closing = null
40
43
  this._opening = opts._opening ? opts._opening.then(() => this._open()) : this._open()
41
44
  this._opening.catch(safetyCatch)
42
45
  this.ready = () => this._opening
@@ -178,7 +181,7 @@ module.exports = class Corestore extends EventEmitter {
178
181
  userData,
179
182
  auth,
180
183
  cache: opts.cache,
181
- createIfMissing: !opts._discoveryKey,
184
+ createIfMissing: opts.createIfMissing === false ? false : !opts._discoveryKey,
182
185
  keyPair: keyPair && keyPair.publicKey
183
186
  ? {
184
187
  publicKey: keyPair.publicKey,
@@ -187,6 +190,7 @@ module.exports = class Corestore extends EventEmitter {
187
190
  : null
188
191
  })
189
192
 
193
+ if (this._root._closing) throw new Error('The corestore is closed')
190
194
  this.cores.set(id, core)
191
195
  core.ready().then(() => {
192
196
  for (const { stream } of this._replicationStreams) {
@@ -226,6 +230,7 @@ module.exports = class Corestore extends EventEmitter {
226
230
  }
227
231
 
228
232
  get (opts = {}) {
233
+ if (this._root._closing) throw new Error('The corestore is closed')
229
234
  opts = validateGetOptions(opts)
230
235
 
231
236
  if (opts.cache !== false) {
@@ -288,6 +293,7 @@ module.exports = class Corestore extends EventEmitter {
288
293
  primaryKey: this._opening.then(() => this.primaryKey),
289
294
  namespace: generateNamespace(this._namespace, name),
290
295
  cache: this.cache,
296
+ _root: this._root,
291
297
  _opening: this._opening,
292
298
  _cores: this.cores,
293
299
  _streams: this._replicationStreams,
@@ -295,23 +301,25 @@ module.exports = class Corestore extends EventEmitter {
295
301
  })
296
302
  }
297
303
 
298
- async _close () {
299
- await this._opening
300
- if (!b4a.equals(this._namespace, DEFAULT_NAMESPACE)) {
301
- // namespaces should not release resources on close
302
- // TODO: Refactor the namespace close logic to actually close sessions with ref counting
303
- return
304
- }
304
+ _closeNamespace () {
305
305
  const closePromises = []
306
- for (const core of this.cores.values()) {
307
- closePromises.push(core.close())
306
+ for (const session of this._sessions) {
307
+ closePromises.push(session.close())
308
308
  }
309
- await Promise.allSettled(closePromises)
309
+ return Promise.allSettled(closePromises)
310
+ }
311
+
312
+ async _closePrimaryNamespace () {
313
+ const closePromises = []
314
+ // At this point, the primary namespace is closing.
310
315
  for (const { stream, isExternal } of this._replicationStreams) {
311
316
  // Only close streams that were created by the Corestore
312
317
  if (!isExternal) stream.destroy()
313
318
  }
314
- if (!this._keyStorage) return
319
+ for (const core of this.cores.values()) {
320
+ closePromises.push(forceClose(core))
321
+ }
322
+ await Promise.allSettled(closePromises)
315
323
  await new Promise((resolve, reject) => {
316
324
  this._keyStorage.close(err => {
317
325
  if (err) return reject(err)
@@ -320,10 +328,17 @@ module.exports = class Corestore extends EventEmitter {
320
328
  })
321
329
  }
322
330
 
331
+ async _close () {
332
+ await this._opening
333
+ await this._closeNamespace()
334
+ if (this._root === this) {
335
+ await this._closePrimaryNamespace()
336
+ }
337
+ }
338
+
323
339
  close () {
324
340
  if (this._closing) return this._closing
325
341
  this._closing = this._close()
326
- this._closing.catch(safetyCatch)
327
342
  return this._closing
328
343
  }
329
344
  }
@@ -371,3 +386,7 @@ function defaultCache () {
371
386
  function isStream (s) {
372
387
  return typeof s === 'object' && s && typeof s.pipe === 'function'
373
388
  }
389
+
390
+ function forceClose (core) {
391
+ return Promise.all(core.sessions.map(s => s.close()))
392
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.0.3",
3
+ "version": "6.0.6",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -19,6 +19,10 @@
19
19
  "url": "https://github.com/hypercore-protocol/corestore/issues"
20
20
  },
21
21
  "homepage": "https://github.com/hypercore-protocol/corestore#readme",
22
+ "files": [
23
+ "index.js",
24
+ "lib/**.js"
25
+ ],
22
26
  "devDependencies": {
23
27
  "brittle": "^3.0.0",
24
28
  "random-access-memory": "^5.0.1",
@@ -1,23 +0,0 @@
1
- name: Build Status
2
- on:
3
- push:
4
- branches:
5
- - master
6
- pull_request:
7
- branches:
8
- - master
9
- jobs:
10
- build:
11
- strategy:
12
- matrix:
13
- node-version: [lts/*]
14
- os: [ubuntu-latest, macos-latest, windows-latest]
15
- runs-on: ${{ matrix.os }}
16
- steps:
17
- - uses: actions/checkout@v2
18
- - name: Use Node.js ${{ matrix.node-version }}
19
- uses: actions/setup-node@v2
20
- with:
21
- node-version: ${{ matrix.node-version }}
22
- - run: npm install
23
- - run: npm test
package/test/all.js DELETED
@@ -1,334 +0,0 @@
1
- const test = require('brittle')
2
- const crypto = require('hypercore-crypto')
3
- const ram = require('random-access-memory')
4
- const os = require('os')
5
- const path = require('path')
6
- const b4a = require('b4a')
7
- const sodium = require('sodium-universal')
8
-
9
- const Corestore = require('..')
10
-
11
- test('basic get with caching', async function (t) {
12
- const store = new Corestore(ram)
13
- const core1a = store.get({ name: 'core-1' })
14
- const core1b = store.get({ name: 'core-1' })
15
- const core2 = store.get({ name: 'core-2' })
16
-
17
- await Promise.all([core1a.ready(), core1b.ready(), core2.ready()])
18
-
19
- t.alike(core1a.key, core1b.key)
20
- t.unlike(core1a.key, core2.key)
21
-
22
- t.ok(core1a.writable)
23
- t.ok(core1b.writable)
24
-
25
- t.is(store.cores.size, 2)
26
- })
27
-
28
- test('basic get with custom keypair', async function (t) {
29
- const store = new Corestore(ram)
30
- const kp1 = crypto.keyPair()
31
- const kp2 = crypto.keyPair()
32
-
33
- const core1 = store.get(kp1)
34
- const core2 = store.get(kp2)
35
- await Promise.all([core1.ready(), core2.ready()])
36
-
37
- t.alike(core1.key, kp1.publicKey)
38
- t.alike(core2.key, kp2.publicKey)
39
- t.ok(core1.writable)
40
- t.ok(core2.writable)
41
- })
42
-
43
- test('basic namespaces', async function (t) {
44
- const store = new Corestore(ram)
45
- const ns1 = store.namespace('ns1')
46
- const ns2 = store.namespace('ns2')
47
- const ns3 = store.namespace('ns1') // Duplicate namespace
48
-
49
- const core1 = ns1.get({ name: 'main' })
50
- const core2 = ns2.get({ name: 'main' })
51
- const core3 = ns3.get({ name: 'main' })
52
- await Promise.all([core1.ready(), core2.ready(), core3.ready()])
53
-
54
- t.absent(core1.key.equals(core2.key))
55
- t.ok(core1.key.equals(core3.key))
56
- t.ok(core1.writable)
57
- t.ok(core2.writable)
58
- t.ok(core3.writable)
59
- t.is(store.cores.size, 2)
60
-
61
- t.end()
62
- })
63
-
64
- test('basic replication', async function (t) {
65
- const store1 = new Corestore(ram)
66
- const store2 = new Corestore(ram)
67
-
68
- const core1 = store1.get({ name: 'core-1' })
69
- const core2 = store1.get({ name: 'core-2' })
70
- await core1.append('hello')
71
- await core2.append('world')
72
-
73
- const core3 = store2.get({ key: core1.key })
74
- const core4 = store2.get({ key: core2.key })
75
-
76
- const s = store1.replicate(true)
77
- s.pipe(store2.replicate(false)).pipe(s)
78
-
79
- t.alike(await core3.get(0), Buffer.from('hello'))
80
- t.alike(await core4.get(0), Buffer.from('world'))
81
- })
82
-
83
- test('replicating cores created after replication begins', async function (t) {
84
- const store1 = new Corestore(ram)
85
- const store2 = new Corestore(ram)
86
-
87
- const s = store1.replicate(true, { live: true })
88
- s.pipe(store2.replicate(false, { live: true })).pipe(s)
89
-
90
- const core1 = store1.get({ name: 'core-1' })
91
- const core2 = store1.get({ name: 'core-2' })
92
- await core1.append('hello')
93
- await core2.append('world')
94
-
95
- const core3 = store2.get({ key: core1.key })
96
- const core4 = store2.get({ key: core2.key })
97
-
98
- t.alike(await core3.get(0), Buffer.from('hello'))
99
- t.alike(await core4.get(0), Buffer.from('world'))
100
- })
101
-
102
- test('replicating cores using discovery key hook', async function (t) {
103
- const dir = tmpdir()
104
- let store1 = new Corestore(dir)
105
- const store2 = new Corestore(ram)
106
-
107
- const core = store1.get({ name: 'main' })
108
- await core.append('hello')
109
- const key = core.key
110
-
111
- await store1.close()
112
- store1 = new Corestore(dir)
113
-
114
- const s = store1.replicate(true, { live: true })
115
- s.pipe(store2.replicate(false, { live: true })).pipe(s)
116
-
117
- const core2 = store2.get(key)
118
- t.alike(await core2.get(0), Buffer.from('hello'))
119
- })
120
-
121
- test('nested namespaces', async function (t) {
122
- const store = new Corestore(ram)
123
- const ns1a = store.namespace('ns1').namespace('a')
124
- const ns1b = store.namespace('ns1').namespace('b')
125
-
126
- const core1 = ns1a.get({ name: 'main' })
127
- const core2 = ns1b.get({ name: 'main' })
128
- await Promise.all([core1.ready(), core2.ready()])
129
-
130
- t.not(core1.key.equals(core2.key))
131
- t.ok(core1.writable)
132
- t.ok(core2.writable)
133
- t.is(store.cores.size, 2)
134
- })
135
-
136
- test('core uncached when all sessions close', async function (t) {
137
- const store = new Corestore(ram)
138
- const core1 = store.get({ name: 'main' })
139
- await core1.ready()
140
- t.is(store.cores.size, 1)
141
- await core1.close()
142
- t.is(store.cores.size, 0)
143
- })
144
-
145
- test('writable core loaded from name userData', async function (t) {
146
- const dir = tmpdir()
147
-
148
- let store = new Corestore(dir)
149
- let core = store.get({ name: 'main' })
150
- await core.ready()
151
- const key = core.key
152
-
153
- t.ok(core.writable)
154
- await core.append('hello')
155
- t.is(core.length, 1)
156
-
157
- await store.close()
158
- store = new Corestore(dir)
159
- core = store.get(key)
160
- await core.ready()
161
-
162
- t.ok(core.writable)
163
- await core.append('world')
164
- t.is(core.length, 2)
165
- t.alike(await core.get(0), Buffer.from('hello'))
166
- t.alike(await core.get(1), Buffer.from('world'))
167
- })
168
-
169
- test('writable core loaded from name and namespace userData', async function (t) {
170
- const dir = tmpdir()
171
-
172
- let store = new Corestore(dir)
173
- let core = store.namespace('ns1').get({ name: 'main' })
174
- await core.ready()
175
- const key = core.key
176
-
177
- t.ok(core.writable)
178
- await core.append('hello')
179
- t.is(core.length, 1)
180
-
181
- await store.close()
182
- store = new Corestore(dir)
183
- core = store.get(key)
184
- await core.ready()
185
-
186
- t.ok(core.writable)
187
- await core.append('world')
188
- t.is(core.length, 2)
189
- t.alike(await core.get(0), Buffer.from('hello'))
190
- t.alike(await core.get(1), Buffer.from('world'))
191
- })
192
-
193
- test('storage locking', async function (t) {
194
- const dir = tmpdir()
195
-
196
- const store1 = new Corestore(dir)
197
- await store1.ready()
198
-
199
- const store2 = new Corestore(dir)
200
- try {
201
- await store2.ready()
202
- t.fail('dir should have been locked')
203
- } catch {
204
- t.pass('dir was locked')
205
- }
206
- })
207
-
208
- test('closing a namespace does not close cores', async function (t) {
209
- const store = new Corestore(ram)
210
- const ns1 = store.namespace('ns1')
211
- const core1 = ns1.get({ name: 'core-1' })
212
- const core2 = ns1.get({ name: 'core-2' })
213
- await Promise.all([core1.ready(), core2.ready()])
214
-
215
- await ns1.close()
216
-
217
- t.is(store.cores.size, 2)
218
- t.not(core1.closed)
219
- t.not(core1.closed)
220
-
221
- await store.close()
222
-
223
- t.is(store.cores.size, 0)
224
- t.ok(core1.closed)
225
- t.ok(core2.closed)
226
- })
227
-
228
- test('findingPeers', async function (t) {
229
- t.plan(6)
230
-
231
- const store = new Corestore(ram)
232
-
233
- const ns1 = store.namespace('ns1')
234
- const ns2 = store.namespace('ns2')
235
-
236
- const a = ns1.get(Buffer.alloc(32).fill('a'))
237
- const b = ns2.get(Buffer.alloc(32).fill('b'))
238
-
239
- const done = ns1.findingPeers()
240
-
241
- let aUpdated = false
242
- let bUpdated = false
243
- let cUpdated = false
244
-
245
- const c = ns1.get(Buffer.alloc(32).fill('c'))
246
-
247
- a.update().then(function (bool) {
248
- aUpdated = true
249
- })
250
-
251
- b.update().then(function (bool) {
252
- bUpdated = true
253
- })
254
-
255
- c.update().then(function (bool) {
256
- cUpdated = true
257
- })
258
-
259
- await new Promise(resolve => setImmediate(resolve))
260
-
261
- t.is(aUpdated, false)
262
- t.is(bUpdated, true)
263
- t.is(cUpdated, false)
264
-
265
- done()
266
-
267
- await new Promise(resolve => setImmediate(resolve))
268
-
269
- t.is(aUpdated, true)
270
- t.is(bUpdated, true)
271
- t.is(cUpdated, true)
272
- })
273
-
274
- test('different primary keys yield different keypairs', async function (t) {
275
- const pk1 = randomBytes(32)
276
- const pk2 = randomBytes(32)
277
- t.unlike(pk1, pk2)
278
-
279
- const store1 = new Corestore(ram, { primaryKey: pk1 })
280
- const store2 = new Corestore(ram, { primaryKey: pk2 })
281
-
282
- const kp1 = await store1.createKeyPair('hello')
283
- const kp2 = await store2.createKeyPair('hello')
284
-
285
- t.unlike(kp1.publicKey, kp2.publicKey)
286
- })
287
-
288
- test('keypair auth sign', async function (t) {
289
- const store = new Corestore(ram)
290
- const keyPair = await store.createKeyPair('foo')
291
- const message = b4a.from('hello world')
292
-
293
- const sig = keyPair.auth.sign(message)
294
-
295
- t.is(sig.length, 64)
296
- t.ok(crypto.verify(message, sig, keyPair.publicKey))
297
- t.absent(crypto.verify(message, b4a.alloc(64), keyPair.publicKey))
298
- })
299
-
300
- test('keypair auth verify', async function (t) {
301
- const store = new Corestore(ram)
302
- const keyPair = await store.createKeyPair('foo')
303
- const message = b4a.from('hello world')
304
-
305
- const sig = crypto.sign(message, keyPair.secretKey)
306
-
307
- t.is(sig.length, 64)
308
- t.ok(keyPair.auth.verify(message, sig))
309
- t.absent(keyPair.auth.verify(message, b4a.alloc(64)))
310
- })
311
-
312
- test('core caching after reopen regression', async function (t) {
313
- const store = new Corestore(ram)
314
- const core = store.get({ name: 'test-core' })
315
- await core.ready()
316
-
317
- core.close()
318
- await core.opening
319
-
320
- const core2 = store.get({ name: 'test-core' })
321
- await core2.ready()
322
-
323
- t.pass('did not infinite loop')
324
- })
325
-
326
- function tmpdir () {
327
- return path.join(os.tmpdir(), 'corestore-' + Math.random().toString(16).slice(2))
328
- }
329
-
330
- function randomBytes (n) {
331
- const buf = b4a.allocUnsafe(n)
332
- sodium.randombytes_buf(buf)
333
- return buf
334
- }
package/test/cache.js DELETED
@@ -1,46 +0,0 @@
1
- const test = require('brittle')
2
- const RAM = require('random-access-memory')
3
-
4
- const Corestore = require('..')
5
-
6
- test('core cache', async function (t) {
7
- const store = new Corestore(RAM, { cache: true })
8
-
9
- const core = store.get({ name: 'core' })
10
- await core.append(['a', 'b', 'c'])
11
-
12
- const p = core.get(0)
13
- const q = core.get(0)
14
-
15
- t.is(await p, await q)
16
- })
17
-
18
- test('clear cache on truncate', async function (t) {
19
- const store = new Corestore(RAM, { cache: true })
20
-
21
- const core = store.get({ name: 'core' })
22
- await core.append(['a', 'b', 'c'])
23
-
24
- const p = core.get(0)
25
-
26
- await core.truncate(0)
27
- await core.append('d')
28
-
29
- const q = core.get(0)
30
-
31
- t.alike(await p, Buffer.from('a'))
32
- t.alike(await q, Buffer.from('d'))
33
- })
34
-
35
- test('core cache on namespace', async function (t) {
36
- const store = new Corestore(RAM, { cache: true })
37
- const ns1 = store.namespace('test-namespace-1')
38
-
39
- const c1 = store.get({ name: 'test-core' })
40
- const c2 = ns1.get({ name: 'test-core' })
41
-
42
- await Promise.all([c1.ready(), c2.ready()])
43
-
44
- t.ok(c1.cache)
45
- t.ok(c2.cache)
46
- })
@@ -1,19 +0,0 @@
1
- const fs = require('fs').promises
2
-
3
- async function cleanup (dirs) {
4
- return Promise.allSettled(dirs.map(dir => fs.rmdir(dir, { recursive: true })))
5
- }
6
-
7
- function delay (ms, cb) {
8
- return new Promise(resolve => {
9
- setTimeout(() => {
10
- if (cb) cb()
11
- resolve()
12
- }, ms)
13
- })
14
- }
15
-
16
- module.exports = {
17
- delay,
18
- cleanup
19
- }