This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathcat.js
157 lines (113 loc) · 4.83 KB
/
cat.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* eslint-env mocha */
'use strict'
const { fixtures } = require('./utils')
const CID = require('cids')
const concat = require('it-concat')
const all = require('it-all')
const { getDescribe, getIt, expect } = require('./utils/mocha')
/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
* @param {Factory} common
* @param {Object} options
*/
module.exports = (common, options) => {
const describe = getDescribe(options)
const it = getIt(options)
describe('.cat', function () {
this.timeout(40 * 1000)
let ipfs
before(async () => { ipfs = (await common.spawn()).api })
after(() => common.clean())
before(() => Promise.all([
all(ipfs.add(fixtures.smallFile.data)),
all(ipfs.add(fixtures.bigFile.data))
]))
it('should cat with a base58 string encoded multihash', async () => {
const data = await concat(ipfs.cat(fixtures.smallFile.cid))
expect(data.toString()).to.contain('Plz add me!')
})
it('should cat with a Buffer multihash', async () => {
const cid = new CID(fixtures.smallFile.cid).multihash
const data = await concat(ipfs.cat(cid))
expect(data.toString()).to.contain('Plz add me!')
})
it('should cat with a CID object', async () => {
const cid = new CID(fixtures.smallFile.cid)
const data = await concat(ipfs.cat(cid))
expect(data.toString()).to.contain('Plz add me!')
})
it('should cat a file added as CIDv0 with a CIDv1', async () => {
const input = Buffer.from(`TEST${Date.now()}`)
const res = await all(ipfs.add(input, { cidVersion: 0 }))
const cidv0 = res[0].cid
expect(cidv0.version).to.equal(0)
const cidv1 = cidv0.toV1()
const output = await concat(ipfs.cat(cidv1))
expect(output.slice()).to.eql(input)
})
it('should cat a file added as CIDv1 with a CIDv0', async () => {
const input = Buffer.from(`TEST${Date.now()}`)
const res = await all(ipfs.add(input, { cidVersion: 1, rawLeaves: false }))
const cidv1 = res[0].cid
expect(cidv1.version).to.equal(1)
const cidv0 = cidv1.toV0()
const output = await concat(ipfs.cat(cidv0))
expect(output.slice()).to.eql(input)
})
it('should cat a BIG file', async () => {
const data = await concat(ipfs.cat(fixtures.bigFile.cid))
expect(data.length).to.equal(fixtures.bigFile.data.length)
expect(data.slice()).to.eql(fixtures.bigFile.data)
})
it('should cat with IPFS path', async () => {
const ipfsPath = '/ipfs/' + fixtures.smallFile.cid
const data = await concat(ipfs.cat(ipfsPath))
expect(data.toString()).to.contain('Plz add me!')
})
it('should cat with IPFS path, nested value', async () => {
const fileToAdd = { path: 'a/testfile.txt', content: fixtures.smallFile.data }
const filesAdded = await all(ipfs.add([fileToAdd]))
const file = await filesAdded.find((f) => f.path === 'a')
expect(file).to.exist()
const data = await concat(ipfs.cat(`/ipfs/${file.cid}/testfile.txt`))
expect(data.toString()).to.contain('Plz add me!')
})
it('should cat with IPFS path, deeply nested value', async () => {
const fileToAdd = { path: 'a/b/testfile.txt', content: fixtures.smallFile.data }
const filesAdded = await all(ipfs.add([fileToAdd]))
const file = filesAdded.find((f) => f.path === 'a')
expect(file).to.exist()
const data = await concat(ipfs.cat(`/ipfs/${file.cid}/b/testfile.txt`))
expect(data.toString()).to.contain('Plz add me!')
})
it('should error on invalid key', () => {
const invalidCid = 'somethingNotMultihash'
return expect(concat(ipfs.cat(invalidCid))).to.eventually.be.rejected()
})
it('should error on unknown path', () => {
return expect(concat(ipfs.cat(fixtures.smallFile.cid + '/does-not-exist'))).to.eventually.be.rejected()
.and.be.an.instanceOf(Error)
.and.to.have.property('message')
.to.be.oneOf([
'file does not exist',
'no link named "does-not-exist" under Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
])
})
it('should error on dir path', async () => {
const file = { path: 'dir/testfile.txt', content: fixtures.smallFile.data }
const filesAdded = await all(ipfs.add([file]))
expect(filesAdded.length).to.equal(2)
const files = filesAdded.filter((file) => file.path === 'dir')
expect(files.length).to.equal(1)
const dir = files[0]
const err = await expect(concat(ipfs.cat(dir.cid))).to.be.rejected()
expect(err.message).to.contain('this dag node is a directory')
})
it('should export a chunk of a file', async () => {
const offset = 1
const length = 3
const data = await concat(ipfs.cat(fixtures.smallFile.cid, { offset, length }))
expect(data.toString()).to.equal('lz ')
})
})
}