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
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -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
Expand Up @@ -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.
Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
"test": "aegir-test",
"test:node": "aegir-test node",
"test:browser": "aegir-test browser",
"build": "aegir-build",
"build-proto": "pbjs --wrap commonjs --target static-module src/unixfs.proto > src/unixfs.proto.js",
"build": "npm run build-proto && aegir-build",
"lint": "aegir-lint",
"release": "aegir-release",
"release-minor": "aegir-release --minor",
"release-major": "aegir-release --major",
"release": "npm run build-proto && aegir-release",
"release-minor": "npm run build-proto && aegir-release --minor",
"release-major": "npm run build-proto && aegir-release --major",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
},
Expand Down Expand Up @@ -43,7 +44,7 @@
"safe-buffer": "^5.1.0"
},
"dependencies": {
"protocol-buffers": "^3.2.1"
"protobufjs": "^6.8.0"
},
"pre-commit": [
"lint",
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
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;
}
Loading