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

Commit 08a5b90

Browse files
committed
fix: code review
1 parent 3962673 commit 08a5b90

File tree

3 files changed

+88
-82
lines changed

3 files changed

+88
-82
lines changed

SPEC/NAME.md

+39-41
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Name API
22

33
* [name.publish](#namepublish)
4-
* [name.resolve](#nameresolve)
54
* [name.pubsub.cancel](#namepubsubcancel)
65
* [name.pubsub.state](#namepubsubstate)
76
* [name.pubsub.subs](#namepubsubsubs)
7+
* [name.resolve](#nameresolve)
88

99
#### `name.publish`
1010

@@ -58,41 +58,6 @@ ipfs.name.publish(addr, function (err, res) {
5858

5959
This way, you can republish a new version of your website under the same address. By default, `ipfs.name.publish` will use the Peer ID. If you want to have multiple websites (for example) under the same IPFS module, you can always check the [key API](./KEY.md).
6060

61-
#### `name.resolve`
62-
63-
> Resolve an IPNS name.
64-
65-
##### `Go` **WIP**
66-
67-
##### `JavaScript` - ipfs.name.resolve(value, [options, callback])
68-
69-
`value` is a IPNS address, such as: `/ipns/ipfs.io`.
70-
71-
`options` is an object that may contain:
72-
73-
```JavaScript
74-
{
75-
recursive: // bool - Resolve until the result is not an IPNS name. Default: false.
76-
nocache: // bool - Do not use cached entries. Default: false.
77-
}
78-
```
79-
80-
`callback` must follow `function (err, result) {}` signature, where `err` is an error if the operation was not successful. `result` is an object that contains the resulting path.
81-
82-
If no `callback` is passed, a promise is returned.
83-
84-
**Example:**
85-
86-
```JavaScript
87-
// The IPNS address you want to resolve.
88-
const addr = '/ipns/ipfs.io'
89-
90-
ipfs.name.resolve(addr, function (err, result) {
91-
console.log(result.path)
92-
// /ipfs/QmQrX8hka2BtNHa8N8arAq16TCVx5qHcb46c5yPewRycLm
93-
})
94-
```
95-
9661
#### `name.pubsub.cancel`
9762

9863
> Cancel a name subscription.
@@ -159,12 +124,10 @@ ipfs.name.pubsub.state(function (err, result) {
159124

160125
##### `JavaScript` - ipfs.name.pubsub.subs([callback])
161126

162-
`callback` must follow `function (err, result) {}` signature, where `err` is an error if the operation was not successful. `result` is an object that contains the result of the operation, such as:
127+
`callback` must follow `function (err, result) {}` signature, where `err` is an error if the operation was not successful. `result` is an array of subscriptions, such as:
163128

164129
```JavaScript
165-
{
166-
strings: ['/ipns/QmQrX8hka2BtNHa8N8arAq16TCVx5qHcb46c5yPewRycLm']
167-
}
130+
['/ipns/QmQrX8hka2BtNHa8N8arAq16TCVx5qHcb46c5yPewRycLm']
168131
```
169132

170133
If no `callback` is passed, a promise is returned.
@@ -173,7 +136,42 @@ If no `callback` is passed, a promise is returned.
173136

174137
```JavaScript
175138
ipfs.name.pubsub.subs(function (err, result) {
176-
console.log(result.strings)
139+
console.log(result)
177140
// ['/ipns/QmQrX8hka2BtNHa8N8arAq16TCVx5qHcb46c5yPewRycLm']
178141
})
179142
```
143+
144+
#### `name.resolve`
145+
146+
> Resolve an IPNS name.
147+
148+
##### `Go` **WIP**
149+
150+
##### `JavaScript` - ipfs.name.resolve(value, [options, callback])
151+
152+
`value` is a IPNS address, such as: `/ipns/ipfs.io`.
153+
154+
`options` is an object that may contain:
155+
156+
```JavaScript
157+
{
158+
recursive: // bool - Resolve until the result is not an IPNS name. Default: false.
159+
nocache: // bool - Do not use cached entries. Default: false.
160+
}
161+
```
162+
163+
`callback` must follow `function (err, name) {}` signature, where `err` is an error if the operation was not successful. `name` is a string that contains the IPFS hash.
164+
165+
If no `callback` is passed, a promise is returned.
166+
167+
**Example:**
168+
169+
```JavaScript
170+
// The IPNS address you want to resolve.
171+
const addr = '/ipns/ipfs.io'
172+
173+
ipfs.name.resolve(addr, function (err, name) {
174+
console.log(name)
175+
// /ipfs/QmQrX8hka2BtNHa8N8arAq16TCVx5qHcb46c5yPewRycLm
176+
})
177+
```

js/src/name-pubsub/cancel.js

+26-19
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
/* eslint-env mocha */
33
'use strict'
44

5+
const series = require('async/series')
56
const loadFixture = require('aegir/fixtures')
67

78
const { spawnNodeWithId } = require('../utils/spawn')
89
const { getDescribe, getIt, expect } = require('../utils/mocha')
910

1011
const fixture = Object.freeze({
11-
data: loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core'),
12-
cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
12+
data: loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core')
1313
})
1414

1515
module.exports = (createCommon, options) => {
@@ -20,6 +20,7 @@ module.exports = (createCommon, options) => {
2020
describe('.name.pubsub.cancel', function () {
2121
let ipfs
2222
let nodeId
23+
let value
2324

2425
before(function (done) {
2526
// CI takes longer to instantiate the daemon, so we need to increase the
@@ -35,7 +36,12 @@ module.exports = (createCommon, options) => {
3536
ipfs = node
3637
nodeId = node.peerId.id
3738

38-
ipfs.files.add(fixture.data, { pin: false }, done)
39+
ipfs.files.add(fixture.data, { pin: false }, (err, res) => {
40+
expect(err).to.not.exist()
41+
42+
value = res[0].path
43+
done()
44+
})
3945
})
4046
})
4147
})
@@ -56,24 +62,25 @@ module.exports = (createCommon, options) => {
5662
})
5763

5864
it('should cancel a subscription correctly returning true', function (done) {
59-
this.timeout(140 * 1000)
60-
const value = fixture.cid
61-
62-
ipfs.name.publish(value, { resolve: false }, (err, res) => {
65+
this.timeout(300 * 1000)
66+
const ipnsPath = `/ipns/${nodeId}`
67+
68+
series([
69+
(cb) => ipfs.name.pubsub.subs(cb),
70+
(cb) => ipfs.name.publish(value, { resolve: false }, cb),
71+
(cb) => ipfs.name.resolve(nodeId, cb),
72+
(cb) => ipfs.name.pubsub.subs(cb),
73+
(cb) => ipfs.name.pubsub.cancel(ipnsPath, cb),
74+
(cb) => ipfs.name.pubsub.subs(cb)
75+
], (err, res) => {
6376
expect(err).to.not.exist()
77+
expect(res).to.exist()
78+
expect(res[0]).to.eql([]) // initally empty
79+
expect(res[4]).to.have.property('canceled')
80+
expect(res[4].canceled).to.eql(true)
81+
expect(res[5]).to.be.an('array').that.does.not.include(ipnsPath)
6482

65-
ipfs.name.resolve(nodeId, (err) => {
66-
expect(err).to.not.exist()
67-
68-
ipfs.name.pubsub.cancel(nodeId, (err, res) => {
69-
expect(err).to.not.exist()
70-
expect(res).to.exist()
71-
expect(res).to.have.property('canceled')
72-
expect(res.canceled).to.eql(true)
73-
74-
done()
75-
})
76-
})
83+
done()
7784
})
7885
})
7986
})

js/src/name-pubsub/subs.js

+23-22
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
/* eslint-env mocha */
33
'use strict'
44

5+
const series = require('async/series')
56
const loadFixture = require('aegir/fixtures')
67

78
const { spawnNodeWithId } = require('../utils/spawn')
89
const { getDescribe, getIt, expect } = require('../utils/mocha')
910

1011
const fixture = Object.freeze({
11-
data: loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core'),
12-
cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
12+
data: loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core')
1313
})
1414

1515
module.exports = (createCommon, options) => {
@@ -20,6 +20,7 @@ module.exports = (createCommon, options) => {
2020
describe('.name.pubsub.subs', function () {
2121
let ipfs
2222
let nodeId
23+
let value
2324

2425
before(function (done) {
2526
// CI takes longer to instantiate the daemon, so we need to increase the
@@ -35,45 +36,45 @@ module.exports = (createCommon, options) => {
3536
ipfs = node
3637
nodeId = node.peerId.id
3738

38-
ipfs.files.add(fixture.data, { pin: false }, done)
39+
ipfs.files.add(fixture.data, { pin: false }, (err, res) => {
40+
expect(err).to.not.exist()
41+
42+
value = res[0].path
43+
done()
44+
})
3945
})
4046
})
4147
})
4248

4349
after((done) => common.teardown(done))
4450

45-
it('should get a null result of subscriptions before any resolve', function (done) {
51+
it('should get an empty array as a result of subscriptions before any resolve', function (done) {
4652
this.timeout(60 * 1000)
4753

4854
ipfs.name.pubsub.subs((err, res) => {
4955
expect(err).to.not.exist()
5056
expect(res).to.exist()
51-
expect(res).to.have.property('strings')
52-
expect(res.strings).to.eql(null)
57+
expect(res).to.eql([])
5358

5459
done()
5560
})
5661
})
5762

5863
it('should get the list of subscriptions updated after a resolve', function (done) {
59-
this.timeout(140 * 1000)
60-
const value = fixture.cid
61-
62-
ipfs.name.publish(value, { resolve: false }, (err, res) => {
64+
this.timeout(300 * 1000)
65+
66+
series([
67+
(cb) => ipfs.name.pubsub.subs(cb),
68+
(cb) => ipfs.name.publish(value, { resolve: false }, cb),
69+
(cb) => ipfs.name.resolve(nodeId, cb),
70+
(cb) => ipfs.name.pubsub.subs(cb)
71+
], (err, res) => {
6372
expect(err).to.not.exist()
73+
expect(res).to.exist()
74+
expect(res[0]).to.eql([]) // initally empty
75+
expect(res[3]).to.be.an('array').that.does.include(`/ipns/${nodeId}`)
6476

65-
ipfs.name.resolve(nodeId, (err) => {
66-
expect(err).to.not.exist()
67-
68-
ipfs.name.pubsub.subs((err, res) => {
69-
expect(err).to.not.exist()
70-
expect(res).to.exist()
71-
expect(res).to.have.property('strings')
72-
expect(res.strings).to.be.an('array').that.does.include(`/ipns/${nodeId}`)
73-
74-
done()
75-
})
76-
})
77+
done()
7778
})
7879
})
7980
})

0 commit comments

Comments
 (0)