Skip to content

Commit 5ca09af

Browse files
committed
refactor: add missing log cmds and modularize log api(ipfs-inactive#544)
1 parent 6ff0ea7 commit 5ca09af

File tree

8 files changed

+148
-22
lines changed

8 files changed

+148
-22
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,12 @@ This means:
224224
- [`ipfs.id([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#id)
225225
- [`ipfs.version([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#version)
226226
- [`ipfs.ping()`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#ping)
227-
- [`ipfs.log()`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#log)
227+
228+
#### [log](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic)
229+
230+
- [`ipfs.log.ls([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic)
231+
- [`ipfs.log.tail([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic)
232+
- [`ipfs.log.level(subsystem, level, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic)
228233

229234
#### [key](https://github.com/ipfs/interface-ipfs-core/tree/master/API/key)
230235

src/api/log.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/api/log/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
3+
module.exports = (arg) => {
4+
return {
5+
tail: require('./tail')(arg),
6+
ls: require('./ls')(arg),
7+
level: require('./level')(arg)
8+
}
9+
}

src/api/log/level.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict'
2+
3+
const promisify = require('promisify-es6')
4+
const moduleConfig = require('../../module-config')
5+
6+
module.exports = (arg) => {
7+
const send = moduleConfig(arg)
8+
9+
return promisify((subsystem, level, opts, callback) => {
10+
if (typeof opts === 'function') {
11+
callback = opts
12+
opts = {}
13+
}
14+
if (typeof subsystem !== 'string') {
15+
return callback(new Error('Invalid subsystem type'))
16+
}
17+
18+
if (typeof level !== 'string') {
19+
return callback(new Error('Invalid level type'))
20+
}
21+
22+
send({
23+
path: 'log/level',
24+
args: [subsystem, level],
25+
qs: opts,
26+
files: undefined,
27+
buffer: true
28+
}, callback)
29+
})
30+
}

src/api/log/ls.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
const promisify = require('promisify-es6')
4+
const moduleConfig = require('../../module-config')
5+
6+
module.exports = (arg) => {
7+
const send = moduleConfig(arg)
8+
9+
return promisify((callback) => {
10+
send({
11+
path: 'log/ls'
12+
}, (err, result) => {
13+
if (err) {
14+
return callback(err)
15+
}
16+
17+
callback(null, result.Strings)
18+
})
19+
})
20+
}

src/api/log/tail.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
const promisify = require('promisify-es6')
4+
const pump = require('pump')
5+
const ndjson = require('ndjson')
6+
const moduleConfig = require('../../module-config')
7+
8+
module.exports = (arg) => {
9+
const send = moduleConfig(arg)
10+
11+
return promisify((callback) => {
12+
return send({
13+
path: 'log/tail'
14+
}, (err, response) => {
15+
if (err) {
16+
return callback(err)
17+
}
18+
const outputStream = pump(response, ndjson.parse())
19+
callback(null, outputStream)
20+
})
21+
})
22+
}

test/log.spec.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ describe('.log', () => {
3636
})
3737
})
3838
})
39+
40+
it('.log.ls', (done) => {
41+
ipfs.log.ls((err, res) => {
42+
expect(err).to.not.exist()
43+
expect(res).to.exist()
44+
45+
expect(res).to.be.an('array')
46+
47+
done()
48+
})
49+
})
50+
51+
it('.log.level', (done) => {
52+
ipfs.log.level('all', 'error', (err, res) => {
53+
expect(err).to.not.exist()
54+
expect(res).to.exist()
55+
56+
expect(res).to.be.an('object')
57+
expect(res).to.not.have.property('Error')
58+
expect(res).to.have.property('Message')
59+
60+
done()
61+
})
62+
})
3963
})
4064

4165
describe('Promise API', () => {
@@ -47,5 +71,25 @@ describe('.log', () => {
4771
})
4872
})
4973
})
74+
75+
it('.log.ls', () => {
76+
return ipfs.log.ls()
77+
.then((res) => {
78+
expect(res).to.exist()
79+
80+
expect(res).to.be.an('array')
81+
})
82+
})
83+
84+
it('.log.level', () => {
85+
return ipfs.log.level('all', 'error')
86+
.then((res) => {
87+
expect(res).to.exist()
88+
89+
expect(res).to.be.an('object')
90+
expect(res).to.not.have.property('Error')
91+
expect(res).to.have.property('Message')
92+
})
93+
})
5094
})
5195
})

test/sub-modules.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,21 @@ describe('submodules', () => {
127127
expect(ping).to.be.a('function')
128128
})
129129
})
130+
131+
describe('log', () => {
132+
it('.ls', () => {
133+
const ls = require('../src/api/log/ls')
134+
expect(ls).to.be.a('function')
135+
})
136+
137+
it('.tail', () => {
138+
const tail = require('../src/api/log/tail')
139+
expect(tail).to.be.a('function')
140+
})
141+
142+
it('.level', () => {
143+
const level = require('../src/api/log/level')
144+
expect(level).to.be.a('function')
145+
})
146+
})
130147
})

0 commit comments

Comments
 (0)