Skip to content

Commit

Permalink
Merge pull request #4 from dignifiedquire/dignified
Browse files Browse the repository at this point in the history
Use dignified.js
  • Loading branch information
daviddias committed Apr 2, 2016
2 parents 42c29e1 + 2db367f commit b20d7e2
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ node_modules

# Optional REPL history
.node_repl_history

dist
lib
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: node_js
node_js:
- '4'
- '5'
- stable

addons:
firefox: 'latest'

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

script:
- npm run lint
- npm run test
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
"name": "ipfs-unixfs",
"version": "0.1.1",
"description": "JavaScript implementation of IPFS' unixfs (a Unix FileSystem representation on top of a MerkleDAG)",
"main": "src/index.js",
"main": "lib/index.js",
"jsnext:main": "src/index.js",
"scripts": {
"test": "mocha tests/test-*.js"
"test": "dignified-test",
"test:node": "dignified-test node",
"test:browser": "dignified-test browser",
"build": "dignified-build",
"lint": "dignified-lint",
"release": "dignified-release"
},
"repository": {
"type": "git",
Expand All @@ -21,9 +27,8 @@
"homepage": "https://github.com/ipfs/js-ipfs-unixfs#readme",
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.4.5",
"pre-commit": "^1.1.2",
"standard": "^6.0.4"
"dignified.js": "github:dignifiedquire/dignified.js",
"pre-commit": "^1.1.2"
},
"dependencies": {
"protocol-buffers": "^3.1.5"
Expand Down
File renamed without changes.
21 changes: 13 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict'

const fs = require('fs')
const path = require('path')
const protobuf = require('protocol-buffers')
const schema = fs.readFileSync(path.resolve(__dirname, 'unixfs.proto'))
const schema = fs.readFileSync(path.resolve(__dirname, '../protos/unixfs.proto'))
const pb = protobuf(schema)
const unixfsData = pb.Data // encode/decode
// encode/decode
const unixfsData = pb.Data
// const unixfsMetadata = pb.MetaData // encode/decode

const types = [
Expand All @@ -14,8 +17,6 @@ const types = [
'symlink'
]

exports = module.exports = Data

function Data (type, data) {
if (!(this instanceof Data)) {
return new Data(type, data)
Expand All @@ -38,7 +39,7 @@ function Data (type, data) {

// data.length + blockSizes
this.fileSize = () => {
var sum = 0
let sum = 0
this.blockSizes.forEach((size) => {
sum += size
})
Expand All @@ -50,16 +51,18 @@ function Data (type, data) {

// encode to protobuf
this.marshal = () => {
var type
let type

switch (this.type) {
case 'raw': type = unixfsData.DataType.Raw; break
case 'directory': type = unixfsData.DataType.Directory; break
case 'file': type = unixfsData.DataType.File; break
case 'metadata': type = unixfsData.DataType.Metadata; break
case 'symlink': type = unixfsData.DataType.Symlink; break
default:
throw new Error(`Unkown type: "${this.type}"`)
}
var fileSize = this.fileSize()
let fileSize = this.fileSize()

if (fileSize === 0) {
fileSize = undefined
Expand All @@ -75,7 +78,7 @@ function Data (type, data) {
}

// decode from protobuf https://github.com/ipfs/go-ipfs/blob/master/unixfs/format.go#L24
exports.unmarshal = (marsheled) => {
Data.unmarshal = (marsheled) => {
const decoded = unixfsData.decode(marsheled)
if (!decoded.Data) {
decoded.Data = undefined
Expand All @@ -84,3 +87,5 @@ exports.unmarshal = (marsheled) => {
obj.blockSizes = decoded.blocksizes
return obj
}

exports = module.exports = Data
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 8 additions & 6 deletions tests/test-unixfs-format.js → test/unixfs-format.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* globals describe, it */
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const fs = require('fs')
const path = require('path')

const UnixFS = require('../src')

Expand Down Expand Up @@ -79,7 +81,7 @@ describe('unixfs-format', () => {
done()
})
it('wrong type', (done) => {
var data
let data
try {
data = new UnixFS('bananas')
} catch (err) {
Expand All @@ -91,7 +93,7 @@ describe('unixfs-format', () => {

describe('interop', () => {
it('raw', (done) => {
var raw = fs.readFileSync(__dirname + '/test-data/raw.unixfs')
const raw = fs.readFileSync(path.join(__dirname, '/test-data/raw.unixfs'))
const unmarsheled = UnixFS.unmarshal(raw)
expect(unmarsheled.data).to.deep.equal(new Buffer('Hello UnixFS\n'))
expect(unmarsheled.type).to.equal('file')
Expand All @@ -100,7 +102,7 @@ describe('unixfs-format', () => {
})

it('directory', (done) => {
var raw = fs.readFileSync(__dirname + '/test-data/directory.unixfs')
const raw = fs.readFileSync(path.join(__dirname, '/test-data/directory.unixfs'))
const unmarsheled = UnixFS.unmarshal(raw)
expect(unmarsheled.data).to.deep.equal(undefined)
expect(unmarsheled.type).to.equal('directory')
Expand All @@ -109,7 +111,7 @@ describe('unixfs-format', () => {
})

it('file', (done) => {
var raw = fs.readFileSync(__dirname + '/test-data/file.txt.unixfs')
const raw = fs.readFileSync(path.join(__dirname, '/test-data/file.txt.unixfs'))
const unmarsheled = UnixFS.unmarshal(raw)
expect(unmarsheled.data).to.deep.equal(new Buffer('Hello UnixFS\n'))
expect(unmarsheled.type).to.equal('file')
Expand All @@ -121,7 +123,7 @@ describe('unixfs-format', () => {
})

it('symlink', (done) => {
var raw = fs.readFileSync(__dirname + '/test-data/symlink.txt.unixfs')
const raw = fs.readFileSync(path.join(__dirname, '/test-data/symlink.txt.unixfs'))
const unmarsheled = UnixFS.unmarshal(raw)
expect(unmarsheled.data).to.deep.equal(new Buffer('file.txt'))
expect(unmarsheled.type).to.equal('symlink')
Expand Down

0 comments on commit b20d7e2

Please sign in to comment.