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 all 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
15 changes: 2 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,20 @@ 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.
before_install:
- npm install -g npm@4

script:
- npm run lint
- npm test
- npm run coverage
- make test
- npm run test

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start

after_success:
- npm run coverage-publish

addons:
firefox: 'latest'
apt:
Expand Down
4 changes: 4 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ machine:
node:
version: stable

post:
test:
- npm run coverage -- --upload

dependencies:
pre:
- google-chrome --version
Expand Down
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
"fs": false
},
"scripts": {
"test": "aegir-test",
"test:node": "aegir-test node",
"test:browser": "aegir-test browser",
"build": "aegir-build",
"lint": "aegir-lint",
"release": "aegir-release",
"release-minor": "aegir-release --minor",
"release-major": "aegir-release --major",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
"test": "aegir test",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser -t webworker",
"build-proto": "pbjs --wrap commonjs --target static-module src/unixfs.proto > src/unixfs.proto.js",
"build": "aegir build",
"lint": "aegir lint",
"release": "aegir release",
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major",
"coverage": "aegir coverage --ignore src/unixfs.proto.js"
},
"repository": {
"type": "git",
Expand All @@ -36,14 +36,14 @@
},
"homepage": "https://github.com/ipfs/js-ipfs-unixfs#readme",
"devDependencies": {
"aegir": "^11.0.2",
"chai": "^4.0.2",
"dirty-chai": "^2.0.0",
"aegir": "^12.0.4",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"pre-commit": "^1.2.2",
"safe-buffer": "^5.1.0"
"safe-buffer": "^5.1.1"
},
"dependencies": {
"protocol-buffers": "^3.2.1"
"protobufjs": "^6.8.0"
},
"pre-commit": [
"lint",
Expand All @@ -57,4 +57,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