hypercore 10.13.0 → 10.15.0
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/README.md +10 -0
- package/index.js +7 -4
- package/lib/core.js +4 -3
- package/lib/oplog.js +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -303,6 +303,16 @@ To cancel downloading a range simply destroy the range instance.
|
|
|
303
303
|
range.destroy()
|
|
304
304
|
```
|
|
305
305
|
|
|
306
|
+
#### `const session = await core.session([options])`
|
|
307
|
+
|
|
308
|
+
Creates a new Hypercore instance that shares the same underlying core.
|
|
309
|
+
|
|
310
|
+
You must close any session you make.
|
|
311
|
+
|
|
312
|
+
Options are inherited from the parent instance, unless they are re-set.
|
|
313
|
+
|
|
314
|
+
`options` are the same as in the constructor.
|
|
315
|
+
|
|
306
316
|
#### `const info = await core.info([options])`
|
|
307
317
|
|
|
308
318
|
Get information about this core, such as its total size in bytes.
|
package/index.js
CHANGED
|
@@ -184,13 +184,14 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
184
184
|
const toLock = opts.unlocked ? null : (opts.lock || 'oplog')
|
|
185
185
|
const pool = opts.pool || (opts.poolSize ? RAF.createPool(opts.poolSize) : null)
|
|
186
186
|
const rmdir = !!opts.rmdir
|
|
187
|
+
const writable = opts.writable !== false
|
|
187
188
|
|
|
188
189
|
return createFile
|
|
189
190
|
|
|
190
191
|
function createFile (name) {
|
|
191
192
|
const lock = toLock === null ? false : isFile(name, toLock)
|
|
192
193
|
const sparse = isFile(name, 'data') || isFile(name, 'bitfield') || isFile(name, 'tree')
|
|
193
|
-
return new RAF(name, { directory, lock, sparse, pool: lock ? null : pool, rmdir })
|
|
194
|
+
return new RAF(name, { directory, lock, sparse, pool: lock ? null : pool, rmdir, writable })
|
|
194
195
|
}
|
|
195
196
|
|
|
196
197
|
function isFile (name, n) {
|
|
@@ -331,11 +332,13 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
331
332
|
async _openCapabilities (keyPair, storage, opts) {
|
|
332
333
|
if (opts.from) return this._openFromExisting(opts.from, opts)
|
|
333
334
|
|
|
334
|
-
|
|
335
|
+
const unlocked = !!opts.unlocked
|
|
336
|
+
this.storage = Hypercore.defaultStorage(opts.storage || storage, { unlocked, writable: !unlocked })
|
|
335
337
|
|
|
336
338
|
this.core = await Core.open(this.storage, {
|
|
337
339
|
force: opts.force,
|
|
338
340
|
createIfMissing: opts.createIfMissing,
|
|
341
|
+
readonly: unlocked,
|
|
339
342
|
overwrite: opts.overwrite,
|
|
340
343
|
keyPair,
|
|
341
344
|
crypto: this.crypto,
|
|
@@ -630,9 +633,9 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
630
633
|
}
|
|
631
634
|
}
|
|
632
635
|
|
|
633
|
-
async setUserData (key, value) {
|
|
636
|
+
async setUserData (key, value, { flush = false } = {}) {
|
|
634
637
|
if (this.opened === false) await this.opening
|
|
635
|
-
return this.core.userData(key, value)
|
|
638
|
+
return this.core.userData(key, value, flush)
|
|
636
639
|
}
|
|
637
640
|
|
|
638
641
|
async getUserData (key) {
|
package/lib/core.js
CHANGED
|
@@ -72,7 +72,8 @@ module.exports = class Core {
|
|
|
72
72
|
|
|
73
73
|
const oplog = new Oplog(oplogFile, {
|
|
74
74
|
headerEncoding: m.oplog.header,
|
|
75
|
-
entryEncoding: m.oplog.entry
|
|
75
|
+
entryEncoding: m.oplog.entry,
|
|
76
|
+
headerOnly: opts.readonly
|
|
76
77
|
})
|
|
77
78
|
|
|
78
79
|
let { header, entries } = await oplog.open()
|
|
@@ -191,7 +192,7 @@ module.exports = class Core {
|
|
|
191
192
|
await this.blocks.put(index, value, byteOffset)
|
|
192
193
|
}
|
|
193
194
|
|
|
194
|
-
async userData (key, value) {
|
|
195
|
+
async userData (key, value, flush) {
|
|
195
196
|
// TODO: each oplog append can set user data, so we should have a way
|
|
196
197
|
// to just hitch a ride on one of the other ongoing appends?
|
|
197
198
|
await this._mutex.lock()
|
|
@@ -219,7 +220,7 @@ module.exports = class Core {
|
|
|
219
220
|
|
|
220
221
|
updateUserData(this.header.userData, key, value)
|
|
221
222
|
|
|
222
|
-
if (this._shouldFlush()) await this._flushOplog()
|
|
223
|
+
if (this._shouldFlush() || flush) await this._flushOplog()
|
|
223
224
|
} finally {
|
|
224
225
|
this._mutex.unlock()
|
|
225
226
|
}
|
package/lib/oplog.js
CHANGED
|
@@ -4,10 +4,11 @@ const { crc32 } = require('crc-universal')
|
|
|
4
4
|
const { OPLOG_CORRUPT } = require('./errors')
|
|
5
5
|
|
|
6
6
|
module.exports = class Oplog {
|
|
7
|
-
constructor (storage, { pageSize = 4096, headerEncoding = cenc.raw, entryEncoding = cenc.raw } = {}) {
|
|
7
|
+
constructor (storage, { pageSize = 4096, headerEncoding = cenc.raw, entryEncoding = cenc.raw, headerOnly = false } = {}) {
|
|
8
8
|
this.storage = storage
|
|
9
9
|
this.headerEncoding = headerEncoding
|
|
10
10
|
this.entryEncoding = entryEncoding
|
|
11
|
+
this.headerOnly = headerOnly
|
|
11
12
|
this.flushed = false
|
|
12
13
|
this.byteLength = 0
|
|
13
14
|
this.length = 0
|
|
@@ -101,6 +102,8 @@ module.exports = class Oplog {
|
|
|
101
102
|
|
|
102
103
|
result.header = header ? h2.message : h1.message
|
|
103
104
|
|
|
105
|
+
if (this.headerOnly) return result
|
|
106
|
+
|
|
104
107
|
while (true) {
|
|
105
108
|
const entry = this._decodeEntry(state, this.entryEncoding)
|
|
106
109
|
if (!entry) break
|