This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpublish.js
111 lines (90 loc) · 3.36 KB
/
publish.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* eslint-env mocha */
'use strict'
const { nanoid } = require('nanoid')
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
const { fixture } = require('./utils')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const last = require('it-last')
const PeerId = require('peer-id')
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {Object} options
*/
module.exports = (factory, options) => {
const describe = getDescribe(options)
const it = getIt(options)
describe('.name.publish offline', () => {
const keyName = nanoid()
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
/** @type {string} */
let nodeId
before(async () => {
ipfs = (await factory.spawn()).api
const peerInfo = await ipfs.id()
nodeId = peerInfo.id
await ipfs.add(fixture.data, { pin: false })
})
after(() => factory.clean())
it('should publish an IPNS record with the default params', async function () {
// @ts-ignore this is mocha
this.timeout(50 * 1000)
const value = fixture.cid
const keys = await ipfs.key.list()
const self = keys.find(key => key.name === 'self')
if (!self) {
throw new Error('No self key found')
}
const res = await ipfs.name.publish(value, { allowOffline: true })
expect(res).to.exist()
expect(PeerId.parse(res.name).toString()).to.equal(PeerId.parse(self.id).toString())
expect(res.value).to.equal(`/ipfs/${value}`)
})
it('should publish correctly with the lifetime option and resolve', async () => {
const { path } = await ipfs.add(uint8ArrayFromString('should publish correctly with the lifetime option and resolve'))
await ipfs.name.publish(path, { allowOffline: true, resolve: false, lifetime: '2h' })
expect(await last(ipfs.name.resolve(`/ipns/${nodeId}`))).to.eq(`/ipfs/${path}`)
})
it('should publish correctly when the file was not added but resolve is disabled', async function () {
// @ts-ignore this is mocha
this.timeout(50 * 1000)
const value = 'QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'
const keys = await ipfs.key.list()
const self = keys.find(key => key.name === 'self')
if (!self) {
throw new Error('No self key found')
}
const options = {
resolve: false,
lifetime: '1m',
ttl: '10s',
key: 'self',
allowOffline: true
}
const res = await ipfs.name.publish(value, options)
expect(res).to.exist()
expect(PeerId.parse(res.name).toString()).to.equal(PeerId.parse(self.id).toString())
expect(res.value).to.equal(`/ipfs/${value}`)
})
it('should publish with a key received as param, instead of using the key of the node', async function () {
// @ts-ignore this is mocha
this.timeout(90 * 1000)
const value = fixture.cid
const options = {
resolve: false,
lifetime: '24h',
ttl: '10s',
key: keyName,
allowOffline: true
}
const key = await ipfs.key.gen(keyName, { type: 'rsa', size: 2048 })
const res = await ipfs.name.publish(value, options)
expect(res).to.exist()
expect(PeerId.parse(res.name).toString()).to.equal(PeerId.parse(key.id).toString())
expect(res.value).to.equal(`/ipfs/${value}`)
})
})
}