Skip to content

Commit f9ad25c

Browse files
committed
Merge pull request #1 from ipfs/feat/unixfs-format
feat/unixfs format
2 parents 056beac + e2ac02b commit f9ad25c

File tree

12 files changed

+251
-1
lines changed

12 files changed

+251
-1
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "JavaScript implementation of IPFS' unixfs (a Unix FileSystem representation on top of a MerkleDAG)",
55
"main": "src/index.js",
66
"scripts": {
7-
"test": "mocha tests/*-test.js"
7+
"test": "mocha tests/test-*.js"
88
},
99
"repository": {
1010
"type": "git",
@@ -24,5 +24,8 @@
2424
"mocha": "^2.4.5",
2525
"pre-commit": "^1.1.2",
2626
"standard": "^6.0.4"
27+
},
28+
"dependencies": {
29+
"protocol-buffers": "^3.1.5"
2730
}
2831
}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.format = require('./unixfs-format.js')

src/unixfs-format.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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.proto

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
message Data {
2+
enum DataType {
3+
Raw = 0;
4+
Directory = 1;
5+
File = 2;
6+
Metadata = 3;
7+
Symlink = 4;
8+
}
9+
10+
required DataType Type = 1;
11+
optional bytes Data = 2;
12+
optional uint64 filesize = 3;
13+
repeated uint64 blocksizes = 4;
14+
}
15+
16+
message Metadata {
17+
required string MimeType = 1;
18+
}

tests/test-data/directory.unixfs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+


tests/test-data/directory/file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello UnixFS

tests/test-data/file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello UnixFS

tests/test-data/file.txt.unixfs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello UnixFS
2+


tests/test-data/raw.unixfs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello UnixFS
2+


tests/test-data/symlink.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file.txt

0 commit comments

Comments
 (0)