hypercore 10.13.0 → 10.14.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 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
- this.storage = Hypercore.defaultStorage(opts.storage || storage)
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,
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()
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.13.0",
3
+ "version": "10.14.0",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {