hypercore 10.0.0-alpha.14 → 10.0.0-alpha.15

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 (3) hide show
  1. package/README.md +3 -0
  2. package/index.js +11 -3
  3. package/package.json +2 -1
package/README.md CHANGED
@@ -63,6 +63,7 @@ Note that `tree`, `data`, and `bitfield` are normally heavily sparse files.
63
63
  createIfMissing: true, // create a new Hypercore key pair if none was present in storage
64
64
  overwrite: false, // overwrite any old Hypercore that might already exist
65
65
  valueEncoding: 'json' | 'utf-8' | 'binary', // defaults to binary
66
+ encodeBatch: batch => { ... }, // optionally apply an encoding to complete batches
66
67
  keyPair: kp, // optionally pass the public key and secret key as a key pair
67
68
  encryptionKey: k // optionally pass an encryption key to enable block encryption
68
69
  }
@@ -70,6 +71,8 @@ Note that `tree`, `data`, and `bitfield` are normally heavily sparse files.
70
71
 
71
72
  You can also set valueEncoding to any [abstract-encoding](https://github.com/mafintosh/abstract-encoding) or [compact-encoding](https://github.com/compact-encoding) instance.
72
73
 
74
+ valueEncodings will be applied to individually blocks, even if you append batches. If you want to control encoding at the batch-level, you can use the `encodeBatch` option, which is a function that takes a batch and returns a binary-encoded batch. If you provide a custom valueEncoding, it will not be applied prior to `encodeBatch`.
75
+
73
76
  #### `const seq = await core.append(block)`
74
77
 
75
78
  Append a block of data (or an array of blocks) to the core.
package/index.js CHANGED
@@ -55,6 +55,8 @@ module.exports = class Hypercore extends EventEmitter {
55
55
  this.cache = opts.cache === true ? new Xache({ maxSize: 65536, maxAge: 0 }) : (opts.cache || null)
56
56
 
57
57
  this.valueEncoding = null
58
+ this.encodeBatch = null
59
+
58
60
  this.key = key || null
59
61
  this.discoveryKey = null
60
62
  this.readable = true
@@ -192,6 +194,9 @@ module.exports = class Hypercore extends EventEmitter {
192
194
  if (opts.valueEncoding) {
193
195
  this.valueEncoding = c.from(codecs(opts.valueEncoding))
194
196
  }
197
+ if (opts.encodeBatch) {
198
+ this.encodeBatch = opts.encodeBatch
199
+ }
195
200
 
196
201
  // This is a hidden option that's only used by Corestore.
197
202
  // It's required so that corestore can load a name from userData before 'ready' is emitted.
@@ -480,10 +485,13 @@ module.exports = class Hypercore extends EventEmitter {
480
485
  blocks = Array.isArray(blocks) ? blocks : [blocks]
481
486
 
482
487
  const preappend = this.encryption && this._preappend
483
- const buffers = new Array(blocks.length)
484
488
 
485
- for (let i = 0; i < blocks.length; i++) {
486
- buffers[i] = this._encode(this.valueEncoding, blocks[i])
489
+ const buffers = this.encodeBatch !== null ? this.encodeBatch(blocks) : new Array(blocks.length)
490
+
491
+ if (this.encodeBatch === null) {
492
+ for (let i = 0; i < blocks.length; i++) {
493
+ buffers[i] = this._encode(this.valueEncoding, blocks[i])
494
+ }
487
495
  }
488
496
 
489
497
  return await this.core.append(buffers, this.sign, { preappend })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.0.0-alpha.14",
3
+ "version": "10.0.0-alpha.15",
4
4
  "description": "Hypercore 10",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -38,6 +38,7 @@
38
38
  "codecs": "^2.2.0",
39
39
  "compact-encoding": "^2.5.0",
40
40
  "crc32-universal": "^1.0.1",
41
+ "events": "^3.3.0",
41
42
  "flat-tree": "^1.9.0",
42
43
  "hypercore-crypto": "^3.1.0",
43
44
  "is-options": "^1.0.1",