Skip to content

Commit accc9eb

Browse files
committed
move inspect to sep file
1 parent b821ef7 commit accc9eb

File tree

2 files changed

+56
-98
lines changed

2 files changed

+56
-98
lines changed

index.js

Lines changed: 6 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const id = require('hypercore-id-encoding')
1010
const safetyCatch = require('safety-catch')
1111
const unslab = require('unslab')
1212

13+
const inspect = require('./lib/inspect')
1314
const Core = require('./lib/core')
1415
const Info = require('./lib/info')
1516
const Download = require('./lib/download')
@@ -29,7 +30,7 @@ const {
2930
DECODING_ERROR
3031
} = require('hypercore-errors')
3132

32-
const inspect = Symbol.for('nodejs.util.inspect.custom')
33+
const inspectSymbol = Symbol.for('nodejs.util.inspect.custom')
3334

3435
// Hypercore actually does not have any notion of max/min block sizes
3536
// but we enforce 15mb to ensure smooth replication (each block is transmitted atomically)
@@ -96,92 +97,8 @@ class Hypercore extends EventEmitter {
9697
this.on('newListener', maybeAddMonitor)
9798
}
9899

99-
[inspect](depth, opts) {
100-
let indent = ''
101-
if (typeof opts.indentationLvl === 'number') {
102-
while (indent.length < opts.indentationLvl) indent += ' '
103-
}
104-
105-
let peers = ''
106-
const min = Math.min(this.peers.length, 5)
107-
108-
for (let i = 0; i < min; i++) {
109-
const peer = this.peers[i]
110-
111-
peers += indent + ' Peer(\n'
112-
peers +=
113-
indent +
114-
' remotePublicKey: ' +
115-
opts.stylize(toHex(peer.remotePublicKey), 'string') +
116-
'\n'
117-
peers += indent + ' remoteLength: ' + opts.stylize(peer.remoteLength, 'number') + '\n'
118-
peers += indent + ' remoteFork: ' + opts.stylize(peer.remoteFork, 'number') + '\n'
119-
peers +=
120-
indent + ' remoteCanUpgrade: ' + opts.stylize(peer.remoteCanUpgrade, 'boolean') + '\n'
121-
peers += indent + ' )' + '\n'
122-
}
123-
124-
if (this.peers.length > 5) {
125-
peers += indent + ' ... and ' + (this.peers.length - 5) + ' more\n'
126-
}
127-
128-
if (peers) peers = '[\n' + peers + indent + ' ]'
129-
else peers = '[ ' + opts.stylize(0, 'number') + ' ]'
130-
131-
return (
132-
this.constructor.name +
133-
'(\n' +
134-
indent +
135-
' id: ' +
136-
opts.stylize(this.id, 'string') +
137-
'\n' +
138-
indent +
139-
' key: ' +
140-
opts.stylize(toHex(this.key), 'string') +
141-
'\n' +
142-
indent +
143-
' discoveryKey: ' +
144-
opts.stylize(toHex(this.discoveryKey), 'string') +
145-
'\n' +
146-
indent +
147-
' opened: ' +
148-
opts.stylize(this.opened, 'boolean') +
149-
'\n' +
150-
indent +
151-
' closed: ' +
152-
opts.stylize(this.closed, 'boolean') +
153-
'\n' +
154-
indent +
155-
' snapshotted: ' +
156-
opts.stylize(this.snapshotted, 'boolean') +
157-
'\n' +
158-
indent +
159-
' writable: ' +
160-
opts.stylize(this.writable, 'boolean') +
161-
'\n' +
162-
indent +
163-
' length: ' +
164-
opts.stylize(this.length, 'number') +
165-
'\n' +
166-
indent +
167-
' fork: ' +
168-
opts.stylize(this.fork, 'number') +
169-
'\n' +
170-
indent +
171-
' sessions: [ ' +
172-
opts.stylize(this.sessions.length, 'number') +
173-
' ]\n' +
174-
indent +
175-
' activeRequests: [ ' +
176-
opts.stylize(this.activeRequests.length, 'number') +
177-
' ]\n' +
178-
indent +
179-
' peers: ' +
180-
peers +
181-
'\n' +
182-
indent +
183-
')'
184-
)
100+
[inspectSymbol](depth, opts) {
101+
return inspect(this, depth, opts)
185102
}
186103

187104
static MAX_SUGGESTED_BLOCK_SIZE = MAX_SUGGESTED_BLOCK_SIZE
@@ -442,17 +359,12 @@ class Hypercore extends EventEmitter {
442359
throw ASSERTION('Checkouts must be named or atomized', this.discoveryKey)
443360
if (checkout > this.state.length)
444361
throw ASSERTION(
445-
'Invalid checkout ' + checkout + ' for ' + opts.name + ', length is ' + this.state.length,
362+
`Invalid checkout ${checkout} for ${opts.name}, length is ${this.state.length}`,
446363
this.discoveryKey
447364
)
448365
if (this.state.prologue && checkout < this.state.prologue.length) {
449366
throw ASSERTION(
450-
'Invalid checkout ' +
451-
checkout +
452-
' for ' +
453-
opts.name +
454-
', prologue length is ' +
455-
this.state.prologue.length,
367+
`Invalid checkout ${checkout} for ${opts.name}, prologue length is ${this.state.prologue.length}`,
456368
this.discoveryKey
457369
)
458370
}
@@ -1165,10 +1077,6 @@ function isStream(s) {
11651077
return typeof s === 'object' && s && typeof s.pipe === 'function'
11661078
}
11671079

1168-
function toHex(buf) {
1169-
return buf && b4a.toString(buf, 'hex')
1170-
}
1171-
11721080
async function preappend(blocks) {
11731081
const offset = this.state.length
11741082
const fork = this.state.encryptionFork

lib/inspect.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const b4a = require('b4a')
2+
3+
module.exports = function (core, depth, opts) {
4+
let indent = ''
5+
if (typeof opts.indentationLvl === 'number') {
6+
while (indent.length < opts.indentationLvl) indent += ' '
7+
}
8+
9+
let peers = ''
10+
const min = Math.min(core.peers.length, 5)
11+
12+
for (let i = 0; i < min; i++) {
13+
const peer = core.peers[i]
14+
15+
peers += `${indent} Peer(\n`
16+
peers += `${indent} remotePublicKey: ${opts.stylize(toHex(peer.remotePublicKey), 'string')}\n`
17+
peers += `${indent} remoteLength: ${opts.stylize(peer.remoteLength, 'number')}\n`
18+
peers += `${indent} remoteFork: ${opts.stylize(peer.remoteFork, 'number')}\n`
19+
peers += `${indent} remoteCanUpgrade: ${opts.stylize(peer.remoteCanUpgrade, 'boolean')}\n`
20+
peers += `${indent} )\n`
21+
}
22+
23+
if (core.peers.length > 5) {
24+
peers += `${indent} ... and ${core.peers.length - 5} more\n`
25+
}
26+
27+
if (peers) peers = `[\n${peers}${indent} ]`
28+
else peers = `[ ${opts.stylize(0, 'number')} ]`
29+
30+
return (
31+
`${core.constructor.name}(\n` +
32+
`${indent} id: ${opts.stylize(core.id, 'string')}\n` +
33+
`${indent} key: ${opts.stylize(toHex(core.key), 'string')}\n` +
34+
`${indent} discoveryKey: ${opts.stylize(toHex(core.discoveryKey), 'string')}\n` +
35+
`${indent} opened: ${opts.stylize(core.opened, 'boolean')}\n` +
36+
`${indent} closed: ${opts.stylize(core.closed, 'boolean')}\n` +
37+
`${indent} snapshotted: ${opts.stylize(core.snapshotted, 'boolean')}\n` +
38+
`${indent} writable: ${opts.stylize(core.writable, 'boolean')}\n` +
39+
`${indent} length: ${opts.stylize(core.length, 'number')}\n` +
40+
`${indent} fork: ${opts.stylize(core.fork, 'number')}\n` +
41+
`${indent} sessions: [ ${opts.stylize(core.sessions.length, 'number')} ]\n` +
42+
`${indent} activeRequests: [ ${opts.stylize(core.activeRequests.length, 'number')} ]\n` +
43+
`${indent} peers: ${peers}\n` +
44+
`${indent})'`
45+
)
46+
}
47+
48+
function toHex(buf) {
49+
return buf && b4a.toString(buf, 'hex')
50+
}

0 commit comments

Comments
 (0)