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

Commit a94d80b

Browse files
committed
feat: ipns working locally
1 parent 34c3fc6 commit a94d80b

File tree

5 files changed

+242
-0
lines changed

5 files changed

+242
-0
lines changed

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')

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)

js/src/name/publish.js

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const each = require('async/each')
5+
const hat = require('hat')
6+
7+
const { fixtures } = require('./utils')
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+
this.timeout(50 * 1000)
17+
18+
const keyName = hat()
19+
let ipfs
20+
let nodeId
21+
let keyId
22+
23+
before(function (done) {
24+
this.timeout(60 * 1000)
25+
26+
common.setup((err, factory) => {
27+
expect(err).to.not.exist()
28+
29+
factory.spawnNode((err, node) => {
30+
expect(err).to.not.exist()
31+
32+
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')
43+
44+
keyId = key.id
45+
populate()
46+
})
47+
})
48+
})
49+
})
50+
51+
function populate () {
52+
each(fixtures.files, (file, cb) => {
53+
ipfs.files.add(file.data, { pin: false }, cb)
54+
}, done)
55+
}
56+
})
57+
58+
after((done) => common.teardown(done))
59+
60+
it('name publish should publish correctly', (done) => {
61+
const value = fixtures.files[0].cid
62+
63+
ipfs.name.publish(value, true, '1m', '10s', 'self', (err, res) => {
64+
expect(err).to.not.exist()
65+
expect(res).to.exist()
66+
expect(res).to.equal(nodeId)
67+
68+
done()
69+
})
70+
})
71+
72+
it('name publish should publish correctly when the file was not added but resolve is disabled', (done) => {
73+
const value = 'QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'
74+
75+
ipfs.name.publish(value, false, '1m', '10s', 'self', (err, res) => {
76+
expect(err).to.not.exist()
77+
expect(res).to.exist()
78+
expect(res).to.equal(nodeId)
79+
80+
done()
81+
})
82+
})
83+
84+
it('name publish should publish correctly when a new key is used', (done) => {
85+
const value = fixtures.files[0].cid
86+
87+
ipfs.name.publish(value, false, '24h', '10s', keyName, (err, res) => {
88+
expect(err).to.not.exist()
89+
expect(res).to.exist()
90+
expect(res).to.equal(keyId)
91+
92+
done()
93+
})
94+
})
95+
})
96+
}

js/src/name/resolve.js

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const each = require('async/each')
5+
const hat = require('hat')
6+
7+
const { fixtures } = require('./utils')
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.resolve', function () {
16+
this.timeout(50 * 1000)
17+
18+
const keyName = hat()
19+
let ipfs
20+
let nodeId
21+
let keyId
22+
23+
before(function (done) {
24+
this.timeout(60 * 1000)
25+
26+
common.setup((err, factory) => {
27+
expect(err).to.not.exist()
28+
29+
factory.spawnNode((err, node) => {
30+
expect(err).to.not.exist()
31+
32+
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')
43+
44+
keyId = key.id
45+
populate()
46+
})
47+
})
48+
})
49+
})
50+
51+
function populate () {
52+
each(fixtures.files, (file, cb) => {
53+
ipfs.files.add(file.data, { pin: false }, cb)
54+
}, done)
55+
}
56+
})
57+
58+
after((done) => common.teardown(done))
59+
60+
it('name resolve should resolve correctly after a publish', (done) => {
61+
this.timeout(60 * 1000)
62+
63+
const value = fixtures.files[0].cid
64+
65+
ipfs.name.publish(value, true, '1m', '10s', 'self', (err, res) => {
66+
expect(err).to.not.exist()
67+
expect(res).to.exist()
68+
69+
ipfs.name.resolve(nodeId, false, false, (err, res) => {
70+
expect(err).to.not.exist()
71+
expect(res).to.exist()
72+
expect(res).to.equal(`/ipfs/${value}`)
73+
74+
done()
75+
})
76+
})
77+
})
78+
79+
it('name resolve should not get the entry correctly if its validity time expired', (done) => {
80+
this.timeout(60 * 1000)
81+
82+
const value = fixtures.files[0].cid
83+
84+
ipfs.name.publish(value, true, '10ns', '10s', 'self', (err, res) => {
85+
expect(err).to.not.exist()
86+
expect(res).to.exist()
87+
88+
setTimeout(function () {
89+
ipfs.name.resolve(nodeId, false, false, (err, res) => {
90+
expect(err).to.exist()
91+
expect(res).to.not.exist()
92+
93+
done()
94+
})
95+
}, 1)
96+
})
97+
})
98+
99+
it('name resolve should should go recursively until finding an ipfs hash', (done) => {
100+
this.timeout(60 * 1000)
101+
102+
const value = fixtures.files[0].cid
103+
104+
ipfs.name.publish(value, true, '24h', '10s', 'self', (err, res) => {
105+
expect(err).to.not.exist()
106+
expect(res).to.exist()
107+
108+
ipfs.name.publish(`/ipns/${nodeId}`, true, '24h', '10s', keyName, (err, res) => {
109+
expect(err).to.not.exist()
110+
expect(res).to.exist()
111+
112+
ipfs.name.resolve(keyId, false, true, (err, res) => {
113+
expect(err).to.not.exist()
114+
expect(res).to.exist()
115+
expect(res).to.equal(`/ipfs/${value}`)
116+
117+
done()
118+
})
119+
})
120+
})
121+
})
122+
})
123+
}

js/src/name/utils.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
const loadFixture = require('aegir/fixtures')
4+
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+
})])
13+
})

0 commit comments

Comments
 (0)