Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: switch to protobuf.js #18

Closed
wants to merge 4 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/unixfs.proto.js
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -33,3 +33,6 @@ node_modules
.node_repl_history

dist

package-lock.json
yarn.lock
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -3,12 +3,10 @@ language: node_js

matrix:
include:
- node_js: 4
env: CXX=g++-4.8
- node_js: 6
env:
- CXX=g++-4.8
- node_js: stable
- node_js: 8
env: CXX=g++-4.8

# Make sure we have new NPM.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
"test": "aegir-test",
"test:node": "aegir-test node",
"test:browser": "aegir-test browser",
"build-proto": "pbjs --wrap commonjs --target static-module src/unixfs.proto > src/unixfs.proto.js",
"build": "aegir-build",
"lint": "aegir-lint",
"release": "aegir-release",
@@ -43,7 +44,7 @@
"safe-buffer": "^5.1.0"
},
"dependencies": {
"protocol-buffers": "^3.2.1"
"protobufjs": "^6.8.0"
},
"pre-commit": [
"lint",
@@ -57,4 +58,4 @@
"Pedro Teixeira <i@pgte.me>",
"Richard Littauer <richard.littauer@gmail.com>"
]
}
}
14 changes: 9 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const protobuf = require('protocol-buffers')
const pb = protobuf(require('./unixfs.proto'))
const pb = require('./unixfs.proto.js')
// encode/decode
const unixfsData = pb.Data
// const unixfsMetadata = pb.MetaData // encode/decode
@@ -77,25 +76,30 @@ function Data (type, data) {
fileSize = undefined
}

return unixfsData.encode({
const msg = unixfsData.create({
Type: type,
Data: this.data,
filesize: fileSize,
blocksizes: this.blockSizes.length > 0 ? this.blockSizes : undefined,
hashType: this.hashType,
fanout: this.fanout
})

return unixfsData.encode(msg).finish()
}
}

// decode from protobuf https://github.com/ipfs/go-ipfs/blob/master/unixfs/format.go#L24
Data.unmarshal = (marsheled) => {
const decoded = unixfsData.decode(marsheled)
if (!decoded.Data) {
if (!decoded.Data || decoded.Data.length === 0) {
decoded.Data = undefined
}

const obj = new Data(types[decoded.Type], decoded.Data)
obj.blockSizes = decoded.blocksizes
obj.blockSizes = (decoded.blocksizes || []).map((s) => {
return typeof s.toNumber === 'function' ? s.toNumber() : s
})
return obj
}

22 changes: 22 additions & 0 deletions src/unixfs.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
message Data {
enum DataType {
Raw = 0;
Directory = 1;
File = 2;
Metadata = 3;
Symlink = 4;
HAMTShard = 5;
}

required DataType Type = 1;
optional bytes Data = 2;
optional uint64 filesize = 3;
repeated uint64 blocksizes = 4;

optional uint64 hashType = 5;
optional uint64 fanout = 6;
}

message Metadata {
required string MimeType = 1;
}
650 changes: 626 additions & 24 deletions src/unixfs.proto.js

Large diffs are not rendered by default.