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

Commit 441a3f7

Browse files
committed
fix: code review
1 parent a94d80b commit 441a3f7

File tree

3 files changed

+106
-93
lines changed

3 files changed

+106
-93
lines changed

Diff for: js/src/name/publish.js

+47-40
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

23-
before(function (done) {
20+
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
})

Diff for: js/src/name/resolve.js

+56-45
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,80 +13,68 @@ module.exports = (createCommon, options) => {
1313
const common = createCommon()
1414

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

2321
before(function (done) {
22+
// CI takes longer to instantiate the daemon, so we need to increase the
23+
// timeout for the before step
2424
this.timeout(60 * 1000)
2525

2626
common.setup((err, factory) => {
2727
expect(err).to.not.exist()
2828

29-
factory.spawnNode((err, node) => {
29+
spawnNodeWithId(factory, (err, node) => {
3030
expect(err).to.not.exist()
3131

3232
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')
33+
nodeId = node.peerId.id
4334

44-
keyId = key.id
45-
populate()
46-
})
47-
})
35+
ipfs.files.add(fixture.data, { pin: false }, done)
4836
})
4937
})
50-
51-
function populate () {
52-
each(fixtures.files, (file, cb) => {
53-
ipfs.files.add(file.data, { pin: false }, cb)
54-
}, done)
55-
}
5638
})
5739

5840
after((done) => common.teardown(done))
5941

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

63-
const value = fixtures.files[0].cid
45+
const value = fixture.cid
6446

65-
ipfs.name.publish(value, true, '1m', '10s', 'self', (err, res) => {
47+
ipfs.name.publish(value, (err, res) => {
6648
expect(err).to.not.exist()
6749
expect(res).to.exist()
6850

69-
ipfs.name.resolve(nodeId, false, false, (err, res) => {
51+
ipfs.name.resolve(nodeId, (err, res) => {
7052
expect(err).to.not.exist()
7153
expect(res).to.exist()
72-
expect(res).to.equal(`/ipfs/${value}`)
54+
expect(res.Path).to.equal(`/ipfs/${value}`)
7355

7456
done()
7557
})
7658
})
7759
})
7860

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

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

84-
ipfs.name.publish(value, true, '10ns', '10s', 'self', (err, res) => {
72+
ipfs.name.publish(value, publishOptions, (err, res) => {
8573
expect(err).to.not.exist()
8674
expect(res).to.exist()
8775

8876
setTimeout(function () {
89-
ipfs.name.resolve(nodeId, false, false, (err, res) => {
77+
ipfs.name.resolve(nodeId, (err, res) => {
9078
expect(err).to.exist()
9179
expect(res).to.not.exist()
9280

@@ -96,25 +84,48 @@ module.exports = (createCommon, options) => {
9684
})
9785
})
9886

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

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

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

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

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

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

Diff for: js/src/name/utils.js

+3-8
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)