Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 29d9e48

Browse files
committed
fix: code review
1 parent a94d80b commit 29d9e48

File tree

3 files changed

+106
-92
lines changed

3 files changed

+106
-92
lines changed

js/src/name/publish.js

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const each = require('async/each')
54
const hat = require('hat')
65

7-
const { fixtures } = require('./utils')
6+
const { fixture } = require('./utils')
7+
const { spawnNodeWithId } = require('../utils/spawn')
88
const { getDescribe, getIt, expect } = require('../utils/mocha')
99

1010
module.exports = (createCommon, options) => {
@@ -13,83 +13,90 @@ module.exports = (createCommon, options) => {
1313
const common = createCommon()
1414

1515
describe('.name.publish', function () {
16-
this.timeout(50 * 1000)
17-
1816
const keyName = hat()
1917
let ipfs
2018
let nodeId
21-
let keyId
2219

2320
before(function (done) {
21+
// CI takes longer to instantiate the daemon, so we need to increase the
22+
// timeout for the before step
2423
this.timeout(60 * 1000)
2524

2625
common.setup((err, factory) => {
2726
expect(err).to.not.exist()
2827

29-
factory.spawnNode((err, node) => {
28+
spawnNodeWithId(factory, (err, node) => {
3029
expect(err).to.not.exist()
3130

3231
ipfs = node
33-
ipfs.id().then((res) => {
34-
expect(res.id).to.exist()
35-
36-
nodeId = res.id
32+
nodeId = node.peerId.id
3733

38-
ipfs.key.gen(keyName, { type: 'rsa', size: 2048 }, (err, key) => {
39-
expect(err).to.not.exist()
40-
expect(key).to.exist()
41-
expect(key).to.have.property('name', keyName)
42-
expect(key).to.have.property('id')
43-
44-
keyId = key.id
45-
populate()
46-
})
47-
})
34+
ipfs.files.add(fixture.data, { pin: false }, done)
4835
})
4936
})
50-
51-
function populate () {
52-
each(fixtures.files, (file, cb) => {
53-
ipfs.files.add(file.data, { pin: false }, cb)
54-
}, done)
55-
}
5637
})
5738

5839
after((done) => common.teardown(done))
5940

60-
it('name publish should publish correctly', (done) => {
61-
const value = fixtures.files[0].cid
41+
it('should publish an IPNS record with the default params', (done) => {
42+
this.timeout(50 * 1000)
43+
44+
const value = fixture.cid
6245

63-
ipfs.name.publish(value, true, '1m', '10s', 'self', (err, res) => {
46+
ipfs.name.publish(value, (err, res) => {
6447
expect(err).to.not.exist()
6548
expect(res).to.exist()
66-
expect(res).to.equal(nodeId)
49+
expect(res.Name).to.equal(nodeId)
50+
expect(res.Value).to.equal(`/ipfs/${value}`)
6751

6852
done()
6953
})
7054
})
7155

72-
it('name publish should publish correctly when the file was not added but resolve is disabled', (done) => {
56+
it('should publish correctly when the file was not added but resolve is disabled', (done) => {
57+
this.timeout(50 * 1000)
58+
7359
const value = 'QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'
7460

75-
ipfs.name.publish(value, false, '1m', '10s', 'self', (err, res) => {
61+
const options = {
62+
resolve: false,
63+
lifetime: '1m',
64+
ttl: '10s',
65+
key: 'self'
66+
}
67+
68+
ipfs.name.publish(value, options, (err, res) => {
7669
expect(err).to.not.exist()
7770
expect(res).to.exist()
78-
expect(res).to.equal(nodeId)
71+
expect(res.Name).to.equal(nodeId)
72+
expect(res.Value).to.equal(`/ipfs/${value}`)
7973

8074
done()
8175
})
8276
})
8377

84-
it('name publish should publish correctly when a new key is used', (done) => {
85-
const value = fixtures.files[0].cid
78+
it('should recursively resolve to an IPFS hash', (done) => {
79+
this.timeout(90 * 1000)
80+
81+
const value = fixture.cid
82+
const options = {
83+
resolve: false,
84+
lifetime: '24h',
85+
ttl: '10s',
86+
key: keyName
87+
}
8688

87-
ipfs.name.publish(value, false, '24h', '10s', keyName, (err, res) => {
89+
ipfs.key.gen(keyName, { type: 'rsa', size: 2048 }, (err, key) => {
8890
expect(err).to.not.exist()
89-
expect(res).to.exist()
90-
expect(res).to.equal(keyId)
9191

92-
done()
92+
ipfs.name.publish(value, options, (err, res) => {
93+
expect(err).to.not.exist()
94+
expect(res).to.exist()
95+
expect(res.Name).to.equal(key.id)
96+
expect(res.Value).to.equal(`/ipfs/${value}`)
97+
98+
done()
99+
})
93100
})
94101
})
95102
})

js/src/name/resolve.js

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
/* eslint max-nested-callbacks: ["error", 6] */
12
/* eslint-env mocha */
23
'use strict'
34

4-
const each = require('async/each')
55
const hat = require('hat')
66

7-
const { fixtures } = require('./utils')
7+
const { fixture } = require('./utils')
8+
const { spawnNodeWithId } = require('../utils/spawn')
89
const { getDescribe, getIt, expect } = require('../utils/mocha')
910

1011
module.exports = (createCommon, options) => {
@@ -13,80 +14,68 @@ module.exports = (createCommon, options) => {
1314
const common = createCommon()
1415

1516
describe('.name.resolve', function () {
16-
this.timeout(50 * 1000)
17-
1817
const keyName = hat()
1918
let ipfs
2019
let nodeId
2120
let keyId
2221

2322
before(function (done) {
23+
// CI takes longer to instantiate the daemon, so we need to increase the
24+
// timeout for the before step
2425
this.timeout(60 * 1000)
2526

2627
common.setup((err, factory) => {
2728
expect(err).to.not.exist()
2829

29-
factory.spawnNode((err, node) => {
30+
spawnNodeWithId(factory, (err, node) => {
3031
expect(err).to.not.exist()
3132

3233
ipfs = node
33-
ipfs.id().then((res) => {
34-
expect(res.id).to.exist()
35-
36-
nodeId = res.id
37-
38-
ipfs.key.gen(keyName, { type: 'rsa', size: 2048 }, (err, key) => {
39-
expect(err).to.not.exist()
40-
expect(key).to.exist()
41-
expect(key).to.have.property('name', keyName)
42-
expect(key).to.have.property('id')
34+
nodeId = node.peerId.id
4335

44-
keyId = key.id
45-
populate()
46-
})
47-
})
36+
ipfs.files.add(fixture.data, { pin: false }, done)
4837
})
4938
})
50-
51-
function populate () {
52-
each(fixtures.files, (file, cb) => {
53-
ipfs.files.add(file.data, { pin: false }, cb)
54-
}, done)
55-
}
5639
})
5740

5841
after((done) => common.teardown(done))
5942

60-
it('name resolve should resolve correctly after a publish', (done) => {
61-
this.timeout(60 * 1000)
43+
it('should resolve a record with the default params after a publish', (done) => {
44+
this.timeout(50 * 1000)
6245

63-
const value = fixtures.files[0].cid
46+
const value = fixture.cid
6447

65-
ipfs.name.publish(value, true, '1m', '10s', 'self', (err, res) => {
48+
ipfs.name.publish(value, (err, res) => {
6649
expect(err).to.not.exist()
6750
expect(res).to.exist()
6851

69-
ipfs.name.resolve(nodeId, false, false, (err, res) => {
52+
ipfs.name.resolve(nodeId, (err, res) => {
7053
expect(err).to.not.exist()
7154
expect(res).to.exist()
72-
expect(res).to.equal(`/ipfs/${value}`)
55+
expect(res.Path).to.equal(`/ipfs/${value}`)
7356

7457
done()
7558
})
7659
})
7760
})
7861

79-
it('name resolve should not get the entry correctly if its validity time expired', (done) => {
80-
this.timeout(60 * 1000)
62+
it('should not get the entry if its validity time expired', (done) => {
63+
this.timeout(50 * 1000)
8164

82-
const value = fixtures.files[0].cid
65+
const value = fixture.cid
66+
const publishOptions = {
67+
resolve: true,
68+
lifetime: '1ms',
69+
ttl: '10s',
70+
key: 'self'
71+
}
8372

84-
ipfs.name.publish(value, true, '10ns', '10s', 'self', (err, res) => {
73+
ipfs.name.publish(value, publishOptions, (err, res) => {
8574
expect(err).to.not.exist()
8675
expect(res).to.exist()
8776

8877
setTimeout(function () {
89-
ipfs.name.resolve(nodeId, false, false, (err, res) => {
78+
ipfs.name.resolve(nodeId, (err, res) => {
9079
expect(err).to.exist()
9180
expect(res).to.not.exist()
9281

@@ -96,25 +85,48 @@ module.exports = (createCommon, options) => {
9685
})
9786
})
9887

99-
it('name resolve should should go recursively until finding an ipfs hash', (done) => {
100-
this.timeout(60 * 1000)
88+
it('should recursively resolve to an IPFS hash', (done) => {
89+
this.timeout(100 * 1000)
10190

102-
const value = fixtures.files[0].cid
91+
const value = fixture.cid
92+
const publishOptions = {
93+
resolve: true,
94+
lifetime: '24h',
95+
ttl: '10s',
96+
key: 'self'
97+
}
10398

104-
ipfs.name.publish(value, true, '24h', '10s', 'self', (err, res) => {
99+
// Generate new key
100+
ipfs.key.gen(keyName, { type: 'rsa', size: 2048 }, (err, key) => {
105101
expect(err).to.not.exist()
106-
expect(res).to.exist()
107102

108-
ipfs.name.publish(`/ipns/${nodeId}`, true, '24h', '10s', keyName, (err, res) => {
103+
keyId = key.id
104+
105+
// publish ipfs
106+
ipfs.name.publish(value, publishOptions, (err, res) => {
109107
expect(err).to.not.exist()
110108
expect(res).to.exist()
111109

112-
ipfs.name.resolve(keyId, false, true, (err, res) => {
110+
publishOptions.key = keyName
111+
112+
// publish ipns with the generated key
113+
ipfs.name.publish(`/ipns/${nodeId}`, publishOptions, (err, res) => {
113114
expect(err).to.not.exist()
114115
expect(res).to.exist()
115-
expect(res).to.equal(`/ipfs/${value}`)
116116

117-
done()
117+
const resolveOptions = {
118+
nocache: false,
119+
recursive: true
120+
}
121+
122+
// recursive resolve (will get ipns first, and will resolve again to find the ipfs)
123+
ipfs.name.resolve(keyId, resolveOptions, (err, res) => {
124+
expect(err).to.not.exist()
125+
expect(res).to.exist()
126+
expect(res.Path).to.equal(`/ipfs/${value}`)
127+
128+
done()
129+
})
118130
})
119131
})
120132
})

js/src/name/utils.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
const loadFixture = require('aegir/fixtures')
44

5-
exports.fixtures = Object.freeze({
6-
files: Object.freeze([Object.freeze({
7-
data: loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core'),
8-
cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
9-
}), Object.freeze({
10-
data: loadFixture('js/test/fixtures/test-folder/files/hello.txt', 'interface-ipfs-core'),
11-
cid: 'QmY9cxiHqTFoWamkQVkpmmqzBrY3hCBEL2XNu3NtX74Fuu'
12-
})])
5+
exports.fixture = Object.freeze({
6+
data: loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core'),
7+
cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
138
})

0 commit comments

Comments
 (0)