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

Commit ad1df5a

Browse files
authored
feat: handle raw nodes in mfs (#48)
Right now we assume that all nodes are `dag-pb` nodes and that they only contain `dag-pb` nodes. This PR allows us to handle `raw` nodes when `stat`ing and `ls`ing IPFS paths.
1 parent 4a36b42 commit ad1df5a

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

src/core/ls-pull-stream.js

+9
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ module.exports = (context) => {
109109
return cb(err)
110110
}
111111

112+
if (Buffer.isBuffer(result.node)) {
113+
return cb(null, {
114+
name: file.name,
115+
type: 0,
116+
hash: formatCid(file.cid, options.cidBase),
117+
size: result.node.length
118+
})
119+
}
120+
112121
const meta = UnixFs.unmarshal(result.node.data)
113122

114123
cb(null, {

src/core/stat.js

+13
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ module.exports = (context) => {
6363
node, cid
6464
} = result
6565

66+
if (Buffer.isBuffer(node)) {
67+
return cb(null, {
68+
hash: formatCid(cid, options.cidBase),
69+
size: node.length,
70+
cumulativeSize: node.length,
71+
blocks: 0,
72+
type: 'file', // really?
73+
local: undefined,
74+
sizeLocal: undefined,
75+
withLocality: false
76+
})
77+
}
78+
6679
const meta = unmarshal(node.data)
6780
let blocks = node.links.length
6881

test/ls.spec.js

+58
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ chai.use(require('dirty-chai'))
66
const expect = chai.expect
77
const pull = require('pull-stream/pull')
88
const collect = require('pull-stream/sinks/collect')
9+
const randomBytes = require('./helpers/random-bytes')
10+
const CID = require('cids')
911
const {
1012
FILE_TYPES
1113
} = require('../src')
@@ -17,6 +19,7 @@ const {
1719

1820
describe('ls', () => {
1921
let mfs
22+
let largeFile = randomBytes(490668)
2023

2124
before(async () => {
2225
mfs = await createMfs()
@@ -216,6 +219,61 @@ describe('ls', () => {
216219
}
217220
})
218221

222+
it('lists a raw node', async () => {
223+
const filePath = '/stat/large-file.txt'
224+
225+
await mfs.write(filePath, largeFile, {
226+
create: true,
227+
parents: true,
228+
rawLeaves: true
229+
})
230+
231+
const stats = await mfs.stat(filePath)
232+
const result = await mfs.ipld.get(new CID(stats.hash))
233+
const node = result.value
234+
const child = node.links[0]
235+
236+
expect(child.cid.codec).to.equal('raw')
237+
238+
const rawNodeContents = await mfs.ls(`/ipfs/${child.cid}/`, {
239+
long: true
240+
})
241+
242+
expect(rawNodeContents[0].type).to.equal(0) // this is what go does
243+
expect(rawNodeContents[0].hash).to.equal(child.cid.toBaseEncodedString())
244+
})
245+
246+
it('lists a raw node in an mfs directory', async () => {
247+
const filePath = '/stat/large-file.txt'
248+
249+
await mfs.write(filePath, largeFile, {
250+
create: true,
251+
parents: true,
252+
rawLeaves: true
253+
})
254+
255+
const stats = await mfs.stat(filePath)
256+
const cid = new CID(stats.hash)
257+
const result = await mfs.ipld.get(cid)
258+
const node = result.value
259+
const child = node.links[0]
260+
261+
expect(child.cid.codec).to.equal('raw')
262+
263+
const dir = `/dir-with-raw-${Date.now()}`
264+
const path = `${dir}/raw-${Date.now()}`
265+
266+
await mfs.mkdir(dir)
267+
await mfs.cp(`/ipfs/${child.cid.toBaseEncodedString()}`, path)
268+
269+
const rawNodeContents = await mfs.ls(path, {
270+
long: true
271+
})
272+
273+
expect(rawNodeContents[0].type).to.equal(0) // this is what go does
274+
expect(rawNodeContents[0].hash).to.equal(child.cid.toBaseEncodedString())
275+
})
276+
219277
it('lists a sharded directory contents', async () => {
220278
const shardSplitThreshold = 10
221279
const fileCount = 11

test/stat.spec.js

+51
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const chai = require('chai')
55
chai.use(require('dirty-chai'))
66
const expect = chai.expect
77
const randomBytes = require('./helpers/random-bytes')
8+
const CID = require('cids')
89

910
const {
1011
createMfs,
@@ -153,6 +154,56 @@ describe('stat', () => {
153154
expect(stats.type).to.equal('file')
154155
})
155156

157+
it('stats a raw node', async () => {
158+
const filePath = '/stat/large-file.txt'
159+
160+
await mfs.write(filePath, largeFile, {
161+
create: true,
162+
parents: true,
163+
rawLeaves: true
164+
})
165+
166+
const stats = await mfs.stat(filePath)
167+
const result = await mfs.ipld.get(new CID(stats.hash))
168+
const node = result.value
169+
const child = node.links[0]
170+
171+
expect(child.cid.codec).to.equal('raw')
172+
173+
const rawNodeStats = await mfs.stat(`/ipfs/${child.cid.toBaseEncodedString()}`)
174+
175+
expect(rawNodeStats.hash).to.equal(child.cid.toBaseEncodedString())
176+
expect(rawNodeStats.type).to.equal('file') // this is what go does
177+
})
178+
179+
it('stats a raw node in an mfs directory', async () => {
180+
const filePath = '/stat/large-file.txt'
181+
182+
await mfs.write(filePath, largeFile, {
183+
create: true,
184+
parents: true,
185+
rawLeaves: true
186+
})
187+
188+
const stats = await mfs.stat(filePath)
189+
const result = await mfs.ipld.get(new CID(stats.hash))
190+
const node = result.value
191+
const child = node.links[0]
192+
193+
expect(child.cid.codec).to.equal('raw')
194+
195+
const dir = `/dir-with-raw-${Date.now()}`
196+
const path = `${dir}/raw-${Date.now()}`
197+
198+
await mfs.mkdir(dir)
199+
await mfs.cp(`/ipfs/${child.cid.toBaseEncodedString()}`, path)
200+
201+
const rawNodeStats = await mfs.stat(path)
202+
203+
expect(rawNodeStats.hash).to.equal(child.cid.toBaseEncodedString())
204+
expect(rawNodeStats.type).to.equal('file') // this is what go does
205+
})
206+
156207
it('stats a sharded directory', async () => {
157208
const shardedDirPath = await createShardedDirectory(mfs)
158209

0 commit comments

Comments
 (0)