Skip to content

Commit 75aa830

Browse files
committed
add lint
1 parent f11a61c commit 75aa830

File tree

15 files changed

+114
-54
lines changed

15 files changed

+114
-54
lines changed

.github/workflows/test-node.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
with:
2626
node-version: ${{ matrix.node-version }}
2727
- run: npm install
28+
- run: npm run lint
2829
- run: npm test
2930
- run: npm -g install bare
3031
- run: npm run test:bare

index.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ class Hypercore extends EventEmitter {
104104
static DefaultEncryption = DefaultEncryption
105105

106106
static key(manifest, { compat, version, namespace } = {}) {
107-
if (b4a.isBuffer(manifest))
107+
if (b4a.isBuffer(manifest)) {
108108
manifest = { version, signers: [{ publicKey: manifest, namespace }] }
109+
}
109110
return compat ? manifest.signers[0].publicKey : manifestHash(createManifest(manifest))
110111
}
111112

@@ -353,13 +354,15 @@ class Hypercore extends EventEmitter {
353354
}
354355

355356
if (this.state && checkout !== -1) {
356-
if (!opts.name && !opts.atom)
357+
if (!opts.name && !opts.atom) {
357358
throw ASSERTION('Checkouts must be named or atomized', this.discoveryKey)
358-
if (checkout > this.state.length)
359+
}
360+
if (checkout > this.state.length) {
359361
throw ASSERTION(
360362
`Invalid checkout ${checkout} for ${opts.name}, length is ${this.state.length}`,
361363
this.discoveryKey
362364
)
365+
}
363366
if (this.state.prologue && checkout < this.state.prologue.length) {
364367
throw ASSERTION(
365368
`Invalid checkout ${checkout} for ${opts.name}, prologue length is ${this.state.prologue.length}`,
@@ -704,8 +707,9 @@ class Hypercore extends EventEmitter {
704707
const offset = await s.update()
705708
if (offset) return offset
706709

707-
if (this.closing !== null)
710+
if (this.closing !== null) {
708711
throw SESSION_CLOSED('cannot seek on a closed session', this.discoveryKey)
712+
}
709713

710714
if (!this._shouldWait(opts, this.wait)) return null
711715

@@ -724,8 +728,9 @@ class Hypercore extends EventEmitter {
724728

725729
async has(start, end = start + 1) {
726730
if (this.opened === false) await this.opening
727-
if (!isValidIndex(start) || !isValidIndex(end))
731+
if (!isValidIndex(start) || !isValidIndex(end)) {
728732
throw ASSERTION('has range is invalid', this.discoveryKey)
733+
}
729734

730735
if (this.state.isDefault()) {
731736
if (end === start + 1) return this.core.bitfield.get(start)
@@ -757,8 +762,9 @@ class Hypercore extends EventEmitter {
757762
if (this.opened === false) await this.opening
758763
if (!isValidIndex(index)) throw ASSERTION('block index is invalid', this.discoveryKey)
759764

760-
if (this.closing !== null)
765+
if (this.closing !== null) {
761766
throw SESSION_CLOSED('cannot get on a closed session', this.discoveryKey)
767+
}
762768

763769
const encoding =
764770
(opts && opts.valueEncoding && c.from(opts.valueEncoding)) || this.valueEncoding
@@ -784,16 +790,18 @@ class Hypercore extends EventEmitter {
784790

785791
async clear(start, end = start + 1, opts) {
786792
if (this.opened === false) await this.opening
787-
if (this.closing !== null)
793+
if (this.closing !== null) {
788794
throw SESSION_CLOSED('cannot clear on a closed session', this.discoveryKey)
795+
}
789796

790797
if (typeof end === 'object') {
791798
opts = end
792799
end = start + 1
793800
}
794801

795-
if (!isValidIndex(start) || !isValidIndex(end))
802+
if (!isValidIndex(start) || !isValidIndex(end)) {
796803
throw ASSERTION('clear range is invalid', this.discoveryKey)
804+
}
797805

798806
const cleared = opts && opts.diff ? { blocks: 0 } : null
799807

@@ -815,8 +823,9 @@ class Hypercore extends EventEmitter {
815823

816824
if (block !== null) return block
817825

818-
if (this.closing !== null)
826+
if (this.closing !== null) {
819827
throw SESSION_CLOSED('cannot get on a closed session', this.discoveryKey)
828+
}
820829

821830
// snapshot should check if core has block
822831
if (this._snapshot !== null) {
@@ -906,8 +915,9 @@ class Hypercore extends EventEmitter {
906915

907916
const isDefault = this.state === this.core.state
908917
const writable = !this._readonly && !!(signature || (keyPair && keyPair.secretKey))
909-
if (isDefault && writable === false && (newLength > 0 || fork !== this.state.fork))
918+
if (isDefault && writable === false && (newLength > 0 || fork !== this.state.fork)) {
910919
throw SESSION_NOT_WRITABLE('cannot append to a non-writable core', this.discoveryKey)
920+
}
911921

912922
await this.state.truncate(newLength, fork, { keyPair, signature })
913923

@@ -925,8 +935,9 @@ class Hypercore extends EventEmitter {
925935
const writable =
926936
!isDefault || !!signature || !!(keyPair && keyPair.secretKey) || opts.writable === true
927937

928-
if (this._readonly || writable === false)
938+
if (this._readonly || writable === false) {
929939
throw SESSION_NOT_WRITABLE('cannot append to a readonly core', this.discoveryKey)
940+
}
930941

931942
blocks = Array.isArray(blocks) ? blocks : [blocks]
932943

@@ -1108,11 +1119,12 @@ function maybeUnslab(block) {
11081119
}
11091120

11101121
function checkSnapshot(snapshot, index) {
1111-
if (index >= snapshot.state.snapshotCompatLength)
1122+
if (index >= snapshot.state.snapshotCompatLength) {
11121123
throw SNAPSHOT_NOT_AVAILABLE(
11131124
`snapshot at index ${index} not available (max compat length ${snapshot.state.snapshotCompatLength})`,
11141125
snapshot.discoveryKey
11151126
)
1127+
}
11161128
}
11171129

11181130
function readBlock(rx, index) {

lib/core.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ module.exports = class Core {
140140
async _tryOpen(opts) {
141141
if (opts.preopen) await opts.preopen // just a hook to allow exclusive access here...
142142

143-
let storage = await this.db.resume(this.discoveryKey)
143+
let storage = await this.db.resumeCore(this.discoveryKey)
144144

145145
let overwrite = opts.overwrite === true
146146

@@ -203,7 +203,7 @@ module.exports = class Core {
203203

204204
const discoveryKey = opts.discoveryKey || crypto.discoveryKey(header.key)
205205

206-
storage = await this.db.create({
206+
storage = await this.db.createCore({
207207
key: header.key,
208208
manifest,
209209
keyPair,
@@ -330,8 +330,9 @@ module.exports = class Core {
330330

331331
try {
332332
if (manifest && this.header.manifest === null) {
333-
if (!Verifier.isValidManifest(this.header.key, manifest))
333+
if (!Verifier.isValidManifest(this.header.key, manifest)) {
334334
throw INVALID_CHECKSUM('Manifest hash does not match', this.discoveryKey)
335+
}
335336

336337
const tx = this.state.createWriteBatch()
337338
this._setManifest(tx, Verifier.createManifest(manifest), null)
@@ -343,8 +344,9 @@ module.exports = class Core {
343344
}
344345

345346
_setManifest(tx, manifest, keyPair) {
346-
if (!manifest && b4a.equals(keyPair.publicKey, this.header.key))
347+
if (!manifest && b4a.equals(keyPair.publicKey, this.header.key)) {
347348
manifest = Verifier.defaultSignerManifest(this.header.key)
349+
}
348350
if (!manifest) return
349351

350352
const verifier = new Verifier(this.header.key, manifest, { legacy: this._legacy })
@@ -499,8 +501,9 @@ module.exports = class Core {
499501

500502
// if we got a manifest AND its strictly a non compat one, lets store it
501503
if (manifest && this.header.manifest === null) {
502-
if (!Verifier.isValidManifest(this.header.key, manifest))
504+
if (!Verifier.isValidManifest(this.header.key, manifest)) {
503505
throw INVALID_CHECKSUM('Manifest hash does not match', this.discoveryKey)
506+
}
504507
this._setManifest(tx, manifest, null)
505508
}
506509

lib/default-encryption.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ module.exports = class DefaultEncryption {
2525
const blindingKey = subKeys.subarray(sodium.crypto_stream_KEYBYTES)
2626

2727
if (!block) {
28-
if (compat) sodium.crypto_generichash_batch(blockKey, [encryptionKey], hypercoreKey)
29-
else
28+
if (compat) {
29+
sodium.crypto_generichash_batch(blockKey, [encryptionKey], hypercoreKey)
30+
} else {
3031
sodium.crypto_generichash_batch(blockKey, [DEFAULT_ENCRYPTION, hypercoreKey, encryptionKey])
32+
}
3133
}
3234

3335
sodium.crypto_generichash(blindingKey, blockKey)

lib/fully-remote-proof.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async function verify(storage, buffer, { referrer = null } = {}) {
3939
block: null
4040
}
4141

42-
const core = await storage.resume(discoveryKey)
42+
const core = await storage.resumeCore(discoveryKey)
4343
if (core === null) return null
4444

4545
let rx = core.read()

lib/merkle-tree.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,9 @@ class MerkleTreeBatch {
231231

232232
commit(tx) {
233233
if (tx === undefined) throw INVALID_OPERATION('No database batch was passed')
234-
if (!this.commitable())
234+
if (!this.commitable()) {
235235
throw INVALID_OPERATION('Tree was modified during batch, refusing to commit')
236+
}
236237

237238
if (this.upgraded) this._commitUpgrade(tx)
238239

@@ -696,8 +697,9 @@ class MerkleTree {
696697
// TODO: we could prop use a read batch here and do this in blocks of X for perf
697698
while (!ite.contains(head) && !(await hasTreeNode(session.storage, ite.index))) {
698699
cnt++
699-
if (cnt >= 1024)
700+
if (cnt >= 1024) {
700701
throw ASSERTION('Bad arguments to missingNodes index=' + index + ' at length=' + length)
702+
}
701703
ite.parent()
702704
}
703705

@@ -1023,8 +1025,9 @@ function blockAndSeekProof(session, rx, node, seek, seekRoot, root, p) {
10231025
if (!node.value) p.node.push(getTreeNodeOrError(rx, ite.index))
10241026

10251027
while (ite.index !== root) {
1026-
if (++cnt >= 1024)
1028+
if (++cnt >= 1024) {
10271029
throw ASSERTION('Bad blockAndSeekProof seekRoot=' + seekRoot + ', root=' + root)
1030+
}
10281031
ite.sibling()
10291032

10301033
if (seek && ite.contains(seekRoot) && ite.index !== seekRoot) {
@@ -1105,10 +1108,11 @@ function additionalUpgradeProof(session, rx, from, to, p) {
11051108
ite.seek(target)
11061109

11071110
while (ite.index !== root) {
1108-
if (++cnt >= 1024)
1111+
if (++cnt >= 1024) {
11091112
throw ASSERTION(
11101113
'Bad arguments to additionalUpgradeProof root=' + root + ' target=' + target
11111114
)
1115+
}
11121116
ite.sibling()
11131117
if (ite.index > target) {
11141118
p.additionalUpgrade.push(getTreeNodeOrError(rx, ite.index))
@@ -1171,22 +1175,30 @@ function log2(n) {
11711175
}
11721176

11731177
function normalizeIndexed(block, hash) {
1174-
if (block)
1175-
return { value: true, index: block.index * 2, nodes: block.nodes, lastIndex: block.index }
1176-
if (hash)
1178+
if (block) {
1179+
return {
1180+
value: true,
1181+
index: block.index * 2,
1182+
nodes: block.nodes,
1183+
lastIndex: block.index
1184+
}
1185+
}
1186+
if (hash) {
11771187
return {
11781188
value: false,
11791189
index: hash.index,
11801190
nodes: hash.nodes,
11811191
lastIndex: flat.rightSpan(hash.index) / 2
11821192
}
1193+
}
11831194
return null
11841195
}
11851196

11861197
async function getTreeNodeOrError(rx, index) {
11871198
const node = await rx.getTreeNode(index)
1188-
if (node === null)
1199+
if (node === null) {
11891200
throw INVALID_OPERATION('Expected tree node ' + index + ' from storage, got (nil)')
1201+
}
11901202
return node
11911203
}
11921204

lib/multisig.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ async function partialSignature(
3131
if (from > core.state.length) return null
3232
const nodes = to <= from ? null : await upgradeNodes(core, from, to)
3333

34-
if (signature.byteLength !== 64)
34+
if (signature.byteLength !== 64) {
3535
signature = c.decode(multiSignature, signature).proofs[0].signature
36+
}
3637

3738
return {
3839
signer,

lib/mutex.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ module.exports = class Mutex {
1414
}
1515

1616
lock() {
17-
if (this.destroyed)
17+
if (this.destroyed) {
1818
return Promise.reject(this._destroyError || new Error('Mutex has been destroyed'))
19+
}
1920
if (this.locked) return new Promise(this._enqueue)
2021
this.locked = true
2122
return Promise.resolve()
@@ -30,8 +31,9 @@ module.exports = class Mutex {
3031
}
3132

3233
destroy(err) {
33-
if (!this._destroying)
34+
if (!this._destroying) {
3435
this._destroying = this.locked ? this.lock().catch(() => {}) : Promise.resolve()
36+
}
3537

3638
this.destroyed = true
3739
if (err) this._destroyError = err

0 commit comments

Comments
 (0)