Skip to content
This repository was archived by the owner on Dec 28, 2022. It is now read-only.

Commit 47ffc24

Browse files
authored
use random-access-file 3 (#118)
* use random-access-file 3 * force bump * move ram to latest also * fix typo
1 parent 6b92267 commit 47ffc24

File tree

10 files changed

+72
-71
lines changed

10 files changed

+72
-71
lines changed

index.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { EventEmitter } = require('events')
2-
const raf = require('random-access-file')
2+
const RAF = require('random-access-file')
33
const isOptions = require('is-options')
44
const hypercoreCrypto = require('hypercore-crypto')
55
const c = require('compact-encoding')
@@ -9,8 +9,6 @@ const NoiseSecretStream = require('@hyperswarm/secret-stream')
99
const Protomux = require('protomux')
1010
const codecs = require('codecs')
1111

12-
const fsctl = requireMaybe('fsctl') || { lock: noop, sparse: noop }
13-
1412
const Replicator = require('./lib/replicator')
1513
const Core = require('./lib/core')
1614
const BlockEncryption = require('./lib/block-encryption')
@@ -162,14 +160,25 @@ module.exports = class Hypercore extends EventEmitter {
162160
}
163161

164162
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+
166169
const directory = storage
167170
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)
173182
}
174183
}
175184

@@ -852,12 +861,8 @@ function isStream (s) {
852861
return typeof s === 'object' && s && typeof s.pipe === 'function'
853862
}
854863

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')
861866
}
862867

863868
function toHex (buf) {

package.json

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"hypercore-crypto": "^3.2.1",
4444
"is-options": "^1.0.1",
4545
"protomux": "^3.2.0",
46-
"random-access-file": "^2.1.4",
46+
"random-access-file": "^3.0.1",
4747
"random-array-iterator": "^1.0.0",
4848
"safety-catch": "^1.0.1",
4949
"sodium-universal": "^3.0.4",
@@ -52,13 +52,10 @@
5252
},
5353
"devDependencies": {
5454
"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",
5858
"standard": "^16.0.3",
5959
"tmp-promise": "^3.0.2"
60-
},
61-
"optionalDependencies": {
62-
"fsctl": "^1.0.0"
6360
}
6461
}

test/auth.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const test = require('brittle')
2-
const ram = require('random-access-memory')
2+
const RAM = require('random-access-memory')
33
const crypto = require('hypercore-crypto')
44
const sodium = require('sodium-universal')
55
const b4a = require('b4a')
@@ -29,14 +29,14 @@ test('multisig hypercore', async function (t) {
2929
}
3030
}
3131

32-
const a = new Hypercore(ram, null, {
32+
const a = new Hypercore(RAM, null, {
3333
valueEncoding: 'utf-8',
3434
auth
3535
})
3636

3737
await a.ready()
3838

39-
const b = new Hypercore(ram, a.key, {
39+
const b = new Hypercore(RAM, a.key, {
4040
valueEncoding: 'utf-8',
4141
auth
4242
})
@@ -110,14 +110,14 @@ test('multisig hypercore with instance and extension', async function (t) {
110110
const aKey = crypto.keyPair()
111111
const bKey = crypto.keyPair()
112112

113-
const a = new Hypercore(ram, null, {
113+
const a = new Hypercore(RAM, null, {
114114
valueEncoding: 'utf-8',
115115
auth: new MultiSigAuth(aKey.publicKey, bKey.publicKey, { keyPair: aKey })
116116
})
117117

118118
await a.ready()
119119

120-
const b = new Hypercore(ram, a.key, {
120+
const b = new Hypercore(RAM, a.key, {
121121
valueEncoding: 'utf-8',
122122
auth: new MultiSigAuth(bKey.publicKey, aKey.publicKey, { keyPair: bKey })
123123
})
@@ -193,14 +193,14 @@ test('proof-of-work hypercore', async function (t) {
193193
}
194194
}
195195

196-
const a = new Hypercore(ram, null, {
196+
const a = new Hypercore(RAM, null, {
197197
valueEncoding: 'utf-8',
198198
auth
199199
})
200200

201201
await a.ready()
202202

203-
const b = new Hypercore(ram, a.key, {
203+
const b = new Hypercore(RAM, a.key, {
204204
valueEncoding: 'utf-8',
205205
auth
206206
})
@@ -224,7 +224,7 @@ test('core using custom sign fn', async function (t) {
224224

225225
const keyPair = crypto.keyPair()
226226

227-
const a = new Hypercore(ram, null, {
227+
const a = new Hypercore(RAM, null, {
228228
valueEncoding: 'utf-8',
229229
sign: (signable) => crypto.sign(signable, keyPair.secretKey),
230230
keyPair: {
@@ -234,7 +234,7 @@ test('core using custom sign fn', async function (t) {
234234

235235
await a.ready()
236236

237-
const b = new Hypercore(ram, a.key, { valueEncoding: 'utf-8' })
237+
const b = new Hypercore(RAM, a.key, { valueEncoding: 'utf-8' })
238238
await b.ready()
239239

240240
await a.append(['a', 'b', 'c', 'd', 'e'])

test/basic.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const test = require('brittle')
2-
const ram = require('random-access-memory')
2+
const RAM = require('random-access-memory')
33

44
const Hypercore = require('../')
55
const { create, eventFlush } = require('./helpers')
@@ -71,7 +71,7 @@ test('close multiple', async function (t) {
7171
})
7272

7373
test('storage options', async function (t) {
74-
const core = new Hypercore({ storage: ram })
74+
const core = new Hypercore({ storage: RAM })
7575
await core.append('hello')
7676
t.alike(await core.get(0), Buffer.from('hello'))
7777
t.end()
@@ -82,15 +82,15 @@ test(
8282
function (t) {
8383
const key = Buffer.alloc(33).fill('a')
8484

85-
const core = new Hypercore(ram, key, { crypto: {} })
85+
const core = new Hypercore(RAM, key, { crypto: {} })
8686

8787
t.is(core.key, key)
8888
t.pass('creating a core with more than 32 byteLength key did not throw')
8989
}
9090
)
9191

9292
test('createIfMissing', async function (t) {
93-
const core = new Hypercore(ram, { createIfMissing: false })
93+
const core = new Hypercore(RAM, { createIfMissing: false })
9494

9595
t.exception(core.ready())
9696
})
@@ -114,15 +114,15 @@ test('reopen and overwrite', async function (t) {
114114

115115
function open (name) {
116116
if (st[name]) return st[name]
117-
st[name] = ram()
117+
st[name] = new RAM()
118118
return st[name]
119119
}
120120
})
121121

122122
test('truncate event has truncated-length and fork', async function (t) {
123123
t.plan(2)
124124

125-
const core = new Hypercore(ram)
125+
const core = new Hypercore(RAM)
126126

127127
core.on('truncate', function (length, fork) {
128128
t.is(length, 2)
@@ -134,7 +134,7 @@ test('truncate event has truncated-length and fork', async function (t) {
134134
})
135135

136136
test('treeHash gets the tree hash at a given core length', async function (t) {
137-
const core = new Hypercore(ram)
137+
const core = new Hypercore(RAM)
138138
await core.ready()
139139

140140
const { core: { tree } } = core
@@ -152,7 +152,7 @@ test('treeHash gets the tree hash at a given core length', async function (t) {
152152
})
153153

154154
test('snapshot locks the state', async function (t) {
155-
const core = new Hypercore(ram)
155+
const core = new Hypercore(RAM)
156156
await core.ready()
157157

158158
const a = core.snapshot()
@@ -173,7 +173,7 @@ test('snapshot locks the state', async function (t) {
173173
test('downloading local range', async function (t) {
174174
t.plan(1)
175175

176-
const core = new Hypercore(ram)
176+
const core = new Hypercore(RAM)
177177

178178
await core.append('a')
179179

@@ -189,7 +189,7 @@ test('downloading local range', async function (t) {
189189
test('read ahead', async function (t) {
190190
t.plan(1)
191191

192-
const core = new Hypercore(ram, { valueEncoding: 'utf-8' })
192+
const core = new Hypercore(RAM, { valueEncoding: 'utf-8' })
193193

194194
await core.append('a')
195195

test/bitfield.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const test = require('brittle')
2-
const ram = require('random-access-memory')
2+
const RAM = require('random-access-memory')
33
const Bitfield = require('../lib/bitfield')
44

55
test('bitfield - set and get', async function (t) {
6-
const b = await Bitfield.open(ram())
6+
const b = await Bitfield.open(new RAM())
77

88
t.absent(b.get(42))
99
b.set(42, true)
@@ -21,7 +21,7 @@ test('bitfield - set and get', async function (t) {
2121
})
2222

2323
test('bitfield - random set and gets', async function (t) {
24-
const b = await Bitfield.open(ram())
24+
const b = await Bitfield.open(new RAM())
2525
const set = new Set()
2626

2727
for (let i = 0; i < 200; i++) {
@@ -52,7 +52,7 @@ test('bitfield - random set and gets', async function (t) {
5252
})
5353

5454
test('bitfield - reload', async function (t) {
55-
const s = ram()
55+
const s = new RAM()
5656

5757
{
5858
const b = await Bitfield.open(s)

test/helpers/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const Hypercore = require('../../')
2-
const ram = require('random-access-memory')
2+
const RAM = require('random-access-memory')
33

44
exports.create = async function create (...args) {
5-
const core = new Hypercore(ram, ...args)
5+
const core = new Hypercore(RAM, ...args)
66
await core.ready()
77
return core
88
}
@@ -16,7 +16,7 @@ exports.createStored = function createStored () {
1616

1717
function storage (name) {
1818
if (files.has(name)) return files.get(name).clone()
19-
const st = ram()
19+
const st = new RAM()
2020
files.set(name, st)
2121
return st
2222
}

test/merkle-tree.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const test = require('brittle')
22
const Tree = require('../lib/merkle-tree')
3-
const ram = require('random-access-memory')
3+
const RAM = require('random-access-memory')
44

55
test('nodes', async function (t) {
66
const tree = await create()
@@ -590,7 +590,7 @@ async function reorg (local, remote) {
590590
}
591591

592592
async function create (length = 0) {
593-
const tree = await Tree.open(ram())
593+
const tree = await Tree.open(new RAM())
594594
const b = tree.batch()
595595
for (let i = 0; i < length; i++) {
596596
b.append(Buffer.from('#' + i))

test/oplog.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const p = require('path')
22
const fs = require('fs')
33
const test = require('brittle')
4-
const fsctl = require('fsctl')
5-
const raf = require('random-access-file')
4+
const RAF = require('random-access-file')
65
const c = require('compact-encoding')
76

87
const Oplog = require('../lib/oplog')
@@ -370,12 +369,12 @@ test('oplog - multi append is atomic', async function (t) {
370369
})
371370

372371
function testStorage () {
373-
return raf(STORAGE_FILE_NAME, { directory: __dirname, lock: fsctl.lock })
372+
return new RAF(STORAGE_FILE_NAME, { directory: __dirname, lock: true })
374373
}
375374

376375
function failingOffsetStorage (offset) {
377376
let shouldError = false
378-
const storage = raf(STORAGE_FILE_NAME, { directory: __dirname, lock: fsctl.lock })
377+
const storage = new RAF(STORAGE_FILE_NAME, { directory: __dirname, lock: true })
379378
const write = storage.write.bind(storage)
380379

381380
storage.write = (off, data, cb) => {

test/preload.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const crypto = require('hypercore-crypto')
22
const test = require('brittle')
3-
const ram = require('random-access-memory')
3+
const RAM = require('random-access-memory')
44
const Hypercore = require('../')
55

66
test('preload - storage', async function (t) {
77
const core = new Hypercore(null, {
88
preload: () => {
9-
return { storage: ram }
9+
return { storage: RAM }
1010
}
1111
})
1212
await core.ready()
@@ -21,7 +21,7 @@ test('preload - storage', async function (t) {
2121
test('preload - from another core', async function (t) {
2222
t.plan(2)
2323

24-
const first = new Hypercore(ram)
24+
const first = new Hypercore(RAM)
2525
await first.ready()
2626

2727
const second = new Hypercore(null, {
@@ -37,7 +37,7 @@ test('preload - from another core', async function (t) {
3737

3838
test('preload - custom keypair', async function (t) {
3939
const keyPair = crypto.keyPair()
40-
const core = new Hypercore(ram, keyPair.publicKey, {
40+
const core = new Hypercore(RAM, keyPair.publicKey, {
4141
preload: () => {
4242
return { keyPair }
4343
}
@@ -56,7 +56,7 @@ test('preload - sign/storage', async function (t) {
5656
valueEncoding: 'utf-8',
5757
preload: () => {
5858
return {
59-
storage: ram,
59+
storage: RAM,
6060
auth: {
6161
sign: signable => crypto.sign(signable, keyPair.secretKey),
6262
verify: (signable, signature) => crypto.verify(signable, signature, keyPair.publicKey)

0 commit comments

Comments
 (0)