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

Commit 49a4827

Browse files
vasco-santosalanshaw
authored andcommitted
feat: ipns working locally (#327)
1 parent a0559e7 commit 49a4827

File tree

5 files changed

+258
-0
lines changed

5 files changed

+258
-0
lines changed

Diff for: js/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ exports.files = require('./files')
1010
exports.key = require('./key')
1111
exports.ls = require('./ls')
1212
exports.miscellaneous = require('./miscellaneous')
13+
exports.name = require('./name')
1314
exports.object = require('./object')
1415
exports.pin = require('./pin')
1516
exports.ping = require('./ping')

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

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
const { createSuite } = require('../utils/suite')
3+
4+
const tests = {
5+
publish: require('./publish'),
6+
resolve: require('./resolve')
7+
}
8+
9+
module.exports = createSuite(tests)

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

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const hat = require('hat')
5+
6+
const { fixture } = require('./utils')
7+
const { spawnNodeWithId } = require('../utils/spawn')
8+
const { getDescribe, getIt, expect } = require('../utils/mocha')
9+
10+
module.exports = (createCommon, options) => {
11+
const describe = getDescribe(options)
12+
const it = getIt(options)
13+
const common = createCommon()
14+
15+
describe('.name.publish', function () {
16+
const keyName = hat()
17+
let ipfs
18+
let nodeId
19+
20+
before(function (done) {
21+
// CI takes longer to instantiate the daemon, so we need to increase the
22+
// timeout for the before step
23+
this.timeout(60 * 1000)
24+
25+
common.setup((err, factory) => {
26+
expect(err).to.not.exist()
27+
28+
spawnNodeWithId(factory, (err, node) => {
29+
expect(err).to.not.exist()
30+
31+
ipfs = node
32+
nodeId = node.peerId.id
33+
34+
ipfs.files.add(fixture.data, { pin: false }, done)
35+
})
36+
})
37+
})
38+
39+
after((done) => common.teardown(done))
40+
41+
it('should publish an IPNS record with the default params', function (done) {
42+
this.timeout(50 * 1000)
43+
44+
const value = fixture.cid
45+
46+
ipfs.name.publish(value, (err, res) => {
47+
expect(err).to.not.exist()
48+
expect(res).to.exist()
49+
expect(res.name).to.equal(nodeId)
50+
expect(res.value).to.equal(`/ipfs/${value}`)
51+
52+
done()
53+
})
54+
})
55+
56+
it('should publish correctly when the file was not added but resolve is disabled', function (done) {
57+
this.timeout(50 * 1000)
58+
59+
const value = 'QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'
60+
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) => {
69+
expect(err).to.not.exist()
70+
expect(res).to.exist()
71+
expect(res.name).to.equal(nodeId)
72+
expect(res.value).to.equal(`/ipfs/${value}`)
73+
74+
done()
75+
})
76+
})
77+
78+
it('should publish with a key received as param, instead of using the key of the node', function (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+
}
88+
89+
ipfs.key.gen(keyName, { type: 'rsa', size: 2048 }, function (err, key) {
90+
expect(err).to.not.exist()
91+
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+
})
100+
})
101+
})
102+
})
103+
}

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

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/* eslint max-nested-callbacks: ["error", 6] */
2+
/* eslint-env mocha */
3+
'use strict'
4+
5+
const hat = require('hat')
6+
7+
const { fixture } = require('./utils')
8+
const { spawnNodeWithId } = require('../utils/spawn')
9+
const { getDescribe, getIt, expect } = require('../utils/mocha')
10+
11+
module.exports = (createCommon, options) => {
12+
const describe = getDescribe(options)
13+
const it = getIt(options)
14+
const common = createCommon()
15+
16+
describe('.name.resolve', function () {
17+
const keyName = hat()
18+
let ipfs
19+
let nodeId
20+
let keyId
21+
22+
before(function (done) {
23+
// CI takes longer to instantiate the daemon, so we need to increase the
24+
// timeout for the before step
25+
this.timeout(60 * 1000)
26+
27+
common.setup((err, factory) => {
28+
expect(err).to.not.exist()
29+
30+
spawnNodeWithId(factory, (err, node) => {
31+
expect(err).to.not.exist()
32+
33+
ipfs = node
34+
nodeId = node.peerId.id
35+
36+
ipfs.files.add(fixture.data, { pin: false }, done)
37+
})
38+
})
39+
})
40+
41+
after((done) => common.teardown(done))
42+
43+
it('should resolve a record with the default params after a publish', function (done) {
44+
this.timeout(50 * 1000)
45+
46+
const value = fixture.cid
47+
48+
ipfs.name.publish(value, (err, res) => {
49+
expect(err).to.not.exist()
50+
expect(res).to.exist()
51+
52+
ipfs.name.resolve(nodeId, (err, res) => {
53+
expect(err).to.not.exist()
54+
expect(res).to.exist()
55+
expect(res.path).to.equal(`/ipfs/${value}`)
56+
57+
done()
58+
})
59+
})
60+
})
61+
62+
it('should not get the entry if its validity time expired', function (done) {
63+
this.timeout(50 * 1000)
64+
65+
const value = fixture.cid
66+
const publishOptions = {
67+
resolve: true,
68+
lifetime: '1ms',
69+
ttl: '10s',
70+
key: 'self'
71+
}
72+
73+
ipfs.name.publish(value, publishOptions, (err, res) => {
74+
expect(err).to.not.exist()
75+
expect(res).to.exist()
76+
77+
// guarantee that the record has an expired validity.
78+
setTimeout(function () {
79+
ipfs.name.resolve(nodeId, (err, res) => {
80+
expect(err).to.exist()
81+
expect(err.message).to.equal('record has expired')
82+
expect(res).to.not.exist()
83+
84+
done()
85+
})
86+
}, 1)
87+
})
88+
})
89+
90+
it('should recursively resolve to an IPFS hash', function (done) {
91+
this.timeout(100 * 1000)
92+
93+
const value = fixture.cid
94+
const publishOptions = {
95+
resolve: false,
96+
lifetime: '24h',
97+
ttl: '10s',
98+
key: 'self'
99+
}
100+
101+
// Generate new key
102+
ipfs.key.gen(keyName, { type: 'rsa', size: 2048 }, (err, key) => {
103+
expect(err).to.not.exist()
104+
105+
keyId = key.id
106+
107+
// publish ipfs
108+
ipfs.name.publish(value, publishOptions, (err, res) => {
109+
expect(err).to.not.exist()
110+
expect(res).to.exist()
111+
112+
publishOptions.key = keyName
113+
114+
// publish ipns with the generated key
115+
ipfs.name.publish(`/ipns/${nodeId}`, publishOptions, (err, res) => {
116+
expect(err).to.not.exist()
117+
expect(res).to.exist()
118+
119+
const resolveOptions = {
120+
nocache: false,
121+
recursive: true
122+
}
123+
124+
// recursive resolve (will get ipns first, and will resolve again to find the ipfs)
125+
ipfs.name.resolve(keyId, resolveOptions, (err, res) => {
126+
expect(err).to.not.exist()
127+
expect(res).to.exist()
128+
expect(res.path).to.equal(`/ipfs/${value}`)
129+
130+
done()
131+
})
132+
})
133+
})
134+
})
135+
})
136+
})
137+
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
const loadFixture = require('aegir/fixtures')
4+
5+
exports.fixture = Object.freeze({
6+
data: loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core'),
7+
cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
8+
})

0 commit comments

Comments
 (0)