Skip to content

Commit c0ff738

Browse files
committed
make unixfs be just unixfs, continue the importing on data-importing
1 parent f9ad25c commit c0ff738

File tree

4 files changed

+106
-105
lines changed

4 files changed

+106
-105
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ ipfs-unixfs
99
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
1010

1111
> JavaScript implementation of IPFS' unixfs (a Unix FileSystem representation on top of a MerkleDAG)
12+
13+
# Usage

src/index.js

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,86 @@
1-
exports.format = require('./unixfs-format.js')
1+
const fs = require('fs')
2+
const path = require('path')
3+
const protobuf = require('protocol-buffers')
4+
const schema = fs.readFileSync(path.resolve(__dirname, 'unixfs.proto'))
5+
const pb = protobuf(schema)
6+
const unixfsData = pb.Data // encode/decode
7+
// const unixfsMetadata = pb.MetaData // encode/decode
8+
9+
const types = [
10+
'raw',
11+
'directory',
12+
'file',
13+
'metadata',
14+
'symlink'
15+
]
16+
17+
exports = module.exports = Data
18+
19+
function Data (type, data) {
20+
if (!(this instanceof Data)) {
21+
return new Data(type, data)
22+
}
23+
if (types.indexOf(type) === -1) {
24+
throw new Error('Type: ' + type + ' is not valid')
25+
}
26+
27+
this.type = type
28+
this.data = data
29+
this.blockSizes = []
30+
31+
this.addBlockSize = (size) => {
32+
this.blockSizes.push(size)
33+
}
34+
35+
this.removeBlockSize = (index) => {
36+
this.blockSizes.splice(index, 1)
37+
}
38+
39+
// data.length + blockSizes
40+
this.fileSize = () => {
41+
var sum = 0
42+
this.blockSizes.forEach((size) => {
43+
sum += size
44+
})
45+
if (data) {
46+
sum += data.length
47+
}
48+
return sum
49+
}
50+
51+
// encode to protobuf
52+
this.marshal = () => {
53+
var type
54+
55+
switch (this.type) {
56+
case 'raw': type = unixfsData.DataType.Raw; break
57+
case 'directory': type = unixfsData.DataType.Directory; break
58+
case 'file': type = unixfsData.DataType.File; break
59+
case 'metadata': type = unixfsData.DataType.Metadata; break
60+
case 'symlink': type = unixfsData.DataType.Symlink; break
61+
}
62+
var fileSize = this.fileSize()
63+
64+
if (fileSize === 0) {
65+
fileSize = undefined
66+
}
67+
68+
return unixfsData.encode({
69+
Type: type,
70+
Data: this.data,
71+
filesize: fileSize,
72+
blocksizes: this.blockSizes.length > 0 ? this.blockSizes : undefined
73+
})
74+
}
75+
}
76+
77+
// decode from protobuf https://github.com/ipfs/go-ipfs/blob/master/unixfs/format.go#L24
78+
exports.unmarshal = (marsheled) => {
79+
const decoded = unixfsData.decode(marsheled)
80+
if (!decoded.Data) {
81+
decoded.Data = undefined
82+
}
83+
const obj = new Data(types[decoded.Type], decoded.Data)
84+
obj.blockSizes = decoded.blocksizes
85+
return obj
86+
}

src/unixfs-format.js

Lines changed: 0 additions & 86 deletions
This file was deleted.

tests/test-unixfs-format.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
const expect = require('chai').expect
44
const fs = require('fs')
55

6-
const UnixfsFormat = require('../src').format
6+
const UnixFS = require('../src')
77

88
describe('unixfs-format', () => {
99
it('raw', (done) => {
10-
const data = new UnixfsFormat('raw', new Buffer('bananas'))
10+
const data = new UnixFS('raw', new Buffer('bananas'))
1111
const marsheled = data.marshal()
12-
const unmarsheled = UnixfsFormat.unmarshal(marsheled)
12+
const unmarsheled = UnixFS.unmarshal(marsheled)
1313
expect(data.type).to.equal(unmarsheled.type)
1414
expect(data.data).to.deep.equal(unmarsheled.data)
1515
expect(data.blockSizes).to.deep.equal(unmarsheled.blockSizes)
@@ -18,9 +18,9 @@ describe('unixfs-format', () => {
1818
})
1919

2020
it('directory', (done) => {
21-
const data = new UnixfsFormat('directory')
21+
const data = new UnixFS('directory')
2222
const marsheled = data.marshal()
23-
const unmarsheled = UnixfsFormat.unmarshal(marsheled)
23+
const unmarsheled = UnixFS.unmarshal(marsheled)
2424
expect(data.type).to.equal(unmarsheled.type)
2525
expect(data.data).to.deep.equal(unmarsheled.data)
2626
expect(data.blockSizes).to.deep.equal(unmarsheled.blockSizes)
@@ -29,9 +29,9 @@ describe('unixfs-format', () => {
2929
})
3030

3131
it('file', (done) => {
32-
const data = new UnixfsFormat('file', new Buffer('batata'))
32+
const data = new UnixFS('file', new Buffer('batata'))
3333
const marsheled = data.marshal()
34-
const unmarsheled = UnixfsFormat.unmarshal(marsheled)
34+
const unmarsheled = UnixFS.unmarshal(marsheled)
3535
expect(data.type).to.equal(unmarsheled.type)
3636
expect(data.data).to.deep.equal(unmarsheled.data)
3737
expect(data.blockSizes).to.deep.equal(unmarsheled.blockSizes)
@@ -40,10 +40,10 @@ describe('unixfs-format', () => {
4040
})
4141

4242
it('file add blocksize', (done) => {
43-
const data = new UnixfsFormat('file')
43+
const data = new UnixFS('file')
4444
data.addBlockSize(256)
4545
const marsheled = data.marshal()
46-
const unmarsheled = UnixfsFormat.unmarshal(marsheled)
46+
const unmarsheled = UnixFS.unmarshal(marsheled)
4747
expect(data.type).to.equal(unmarsheled.type)
4848
expect(data.data).to.deep.equal(unmarsheled.data)
4949
expect(data.blockSizes).to.deep.equal(unmarsheled.blockSizes)
@@ -52,10 +52,10 @@ describe('unixfs-format', () => {
5252
})
5353

5454
it('file add and remove blocksize', (done) => {
55-
const data = new UnixfsFormat('file')
55+
const data = new UnixFS('file')
5656
data.addBlockSize(256)
5757
const marsheled = data.marshal()
58-
const unmarsheled = UnixfsFormat.unmarshal(marsheled)
58+
const unmarsheled = UnixFS.unmarshal(marsheled)
5959
expect(data.type).to.equal(unmarsheled.type)
6060
expect(data.data).to.deep.equal(unmarsheled.data)
6161
expect(data.blockSizes).to.deep.equal(unmarsheled.blockSizes)
@@ -69,9 +69,9 @@ describe('unixfs-format', () => {
6969
it.skip('metadata', (done) => {})
7070

7171
it('symlink', (done) => {
72-
const data = new UnixfsFormat('symlink')
72+
const data = new UnixFS('symlink')
7373
const marsheled = data.marshal()
74-
const unmarsheled = UnixfsFormat.unmarshal(marsheled)
74+
const unmarsheled = UnixFS.unmarshal(marsheled)
7575
expect(data.type).to.equal(unmarsheled.type)
7676
expect(data.data).to.deep.equal(unmarsheled.data)
7777
expect(data.blockSizes).to.deep.equal(unmarsheled.blockSizes)
@@ -81,7 +81,7 @@ describe('unixfs-format', () => {
8181
it('wrong type', (done) => {
8282
var data
8383
try {
84-
data = new UnixfsFormat('bananas')
84+
data = new UnixFS('bananas')
8585
} catch (err) {
8686
expect(err).to.exist
8787
expect(data).to.not.exist
@@ -92,7 +92,7 @@ describe('unixfs-format', () => {
9292
describe('interop', () => {
9393
it('raw', (done) => {
9494
var raw = fs.readFileSync(__dirname + '/test-data/raw.unixfs')
95-
const unmarsheled = UnixfsFormat.unmarshal(raw)
95+
const unmarsheled = UnixFS.unmarshal(raw)
9696
expect(unmarsheled.data).to.deep.equal(new Buffer('Hello UnixFS\n'))
9797
expect(unmarsheled.type).to.equal('file')
9898
expect(unmarsheled.marshal()).to.deep.equal(raw)
@@ -101,7 +101,7 @@ describe('unixfs-format', () => {
101101

102102
it('directory', (done) => {
103103
var raw = fs.readFileSync(__dirname + '/test-data/directory.unixfs')
104-
const unmarsheled = UnixfsFormat.unmarshal(raw)
104+
const unmarsheled = UnixFS.unmarshal(raw)
105105
expect(unmarsheled.data).to.deep.equal(undefined)
106106
expect(unmarsheled.type).to.equal('directory')
107107
expect(unmarsheled.marshal()).to.deep.equal(raw)
@@ -110,7 +110,7 @@ describe('unixfs-format', () => {
110110

111111
it('file', (done) => {
112112
var raw = fs.readFileSync(__dirname + '/test-data/file.txt.unixfs')
113-
const unmarsheled = UnixfsFormat.unmarshal(raw)
113+
const unmarsheled = UnixFS.unmarshal(raw)
114114
expect(unmarsheled.data).to.deep.equal(new Buffer('Hello UnixFS\n'))
115115
expect(unmarsheled.type).to.equal('file')
116116
expect(unmarsheled.marshal()).to.deep.equal(raw)
@@ -122,7 +122,7 @@ describe('unixfs-format', () => {
122122

123123
it('symlink', (done) => {
124124
var raw = fs.readFileSync(__dirname + '/test-data/symlink.txt.unixfs')
125-
const unmarsheled = UnixfsFormat.unmarshal(raw)
125+
const unmarsheled = UnixFS.unmarshal(raw)
126126
expect(unmarsheled.data).to.deep.equal(new Buffer('file.txt'))
127127
expect(unmarsheled.type).to.equal('symlink')
128128
// TODO: waiting on https://github.com/ipfs/js-ipfs-data-importing/issues/3#issuecomment-182440079

0 commit comments

Comments
 (0)