hypercore 10.0.0-alpha.52 → 10.0.0-alpha.53

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 +20 -15
  2. package/package.json +5 -8
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const { EventEmitter } = require('events')
2
- const raf = require('random-access-file')
2
+ const RAF = require('random-access-file')
3
3
  const isOptions = require('is-options')
4
4
  const hypercoreCrypto = require('hypercore-crypto')
5
5
  const c = require('compact-encoding')
@@ -9,8 +9,6 @@ const NoiseSecretStream = require('@hyperswarm/secret-stream')
9
9
  const Protomux = require('protomux')
10
10
  const codecs = require('codecs')
11
11
 
12
- const fsctl = requireMaybe('fsctl') || { lock: noop, sparse: noop }
13
-
14
12
  const Replicator = require('./lib/replicator')
15
13
  const Core = require('./lib/core')
16
14
  const BlockEncryption = require('./lib/block-encryption')
@@ -162,14 +160,25 @@ module.exports = class Hypercore extends EventEmitter {
162
160
  }
163
161
 
164
162
  static defaultStorage (storage, opts = {}) {
165
- if (typeof storage !== 'string') return storage
163
+ if (typeof storage !== 'string') {
164
+ if (!isRandomAccessClass(storage)) return storage
165
+ const Cls = storage // just to satisfy standard...
166
+ return name => new Cls(name)
167
+ }
168
+
166
169
  const directory = storage
167
170
  const toLock = opts.lock || 'oplog'
168
- return function createFile (name) {
169
- const locked = name === toLock || name.endsWith('/' + toLock)
170
- const lock = locked ? fsctl.lock : null
171
- const sparse = locked ? null : null // fsctl.sparse, disable sparse on windows - seems to fail for some people. TODO: investigate
172
- return raf(name, { directory, lock, sparse })
171
+
172
+ return createFile
173
+
174
+ function createFile (name) {
175
+ const lock = isFile(name, toLock)
176
+ const sparse = isFile(name, 'data') || isFile(name, 'bitfield') || isFile(name, 'tree')
177
+ return new RAF(name, { directory, lock, sparse })
178
+ }
179
+
180
+ function isFile (name, n) {
181
+ return name === n || name.endsWith('/' + n)
173
182
  }
174
183
  }
175
184
 
@@ -852,12 +861,8 @@ function isStream (s) {
852
861
  return typeof s === 'object' && s && typeof s.pipe === 'function'
853
862
  }
854
863
 
855
- function requireMaybe (name) {
856
- try {
857
- return require(name)
858
- } catch (_) {
859
- return null
860
- }
864
+ function isRandomAccessClass (fn) {
865
+ return !!(typeof fn === 'function' && fn.prototype && typeof fn.prototype.open === 'function')
861
866
  }
862
867
 
863
868
  function toHex (buf) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.0.0-alpha.52",
3
+ "version": "10.0.0-alpha.53",
4
4
  "description": "Hypercore 10",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -43,7 +43,7 @@
43
43
  "hypercore-crypto": "^3.2.1",
44
44
  "is-options": "^1.0.1",
45
45
  "protomux": "^3.2.0",
46
- "random-access-file": "^2.1.4",
46
+ "random-access-file": "^3.0.1",
47
47
  "random-array-iterator": "^1.0.0",
48
48
  "safety-catch": "^1.0.1",
49
49
  "sodium-universal": "^3.0.4",
@@ -52,13 +52,10 @@
52
52
  },
53
53
  "devDependencies": {
54
54
  "brittle": "^2.0.0",
55
- "hyperswarm": "next",
56
- "random-access-memory": "^4.1.0",
57
- "random-access-memory-overlay": "^1.0.0",
55
+ "hyperswarm": "^4.1.1",
56
+ "random-access-memory": "^5.0.0",
57
+ "random-access-memory-overlay": "^2.0.0",
58
58
  "standard": "^16.0.3",
59
59
  "tmp-promise": "^3.0.2"
60
- },
61
- "optionalDependencies": {
62
- "fsctl": "^1.0.0"
63
60
  }
64
61
  }