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

Use js-ipfs-unixfs in a browser environment #6

Closed
wants to merge 5 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
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ node_modules
.node_repl_history

dist
lib
lib

# IntelliJ IDEA
.idea
js-ipfs-unixfs.iml
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"devDependencies": {
"chai": "^3.5.0",
"dignified.js": "github:dignifiedquire/dignified.js",
"pre-commit": "^1.1.2"
"pre-commit": "^1.1.2",
"webpack": "^2.1.0-beta.4"
},
"dependencies": {
"protocol-buffers": "^3.1.5"
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
const fs = require('fs')
const path = require('path')
const protobuf = require('protocol-buffers')
const schema = fs.readFileSync(path.resolve(__dirname, '../protos/unixfs.proto'))

const isNode = !global.window
const schema = isNode ? fs.readFileSync(path.resolve(__dirname, '../protos/unixfs.proto')) : protobuf(require('buffer!'+path.resolve(__dirname, '../protos/unixfs.proto')))
const pb = protobuf(schema)
// encode/decode
const unixfsData = pb.Data
Expand Down
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.
18 changes: 18 additions & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-env mocha */

describe('IPFS-UnixFS Tests on the Browser', function () {
this.timeout(10000)

it('', () => {
const testsContext = require.context('.', true, /.*?spec.js$/)
console.log('KEYS', testsContext.keys())
testsContext
.keys()
.filter((key) => {
return !key.endsWith('-node.spec.js')
})
.forEach((key) => {
testsContext(key)
})
})
})
53 changes: 53 additions & 0 deletions test/unixfs-format-node.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* eslint-env mocha */
'use strict'

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

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

describe('IPFS-UnixFS Tests on NodeJS', () => {

describe('interop', () => {
it('raw', (done) => {
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')
expect(unmarsheled.marshal()).to.deep.equal(raw)
done()
})

it('directory', (done) => {
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')
expect(unmarsheled.marshal()).to.deep.equal(raw)
done()
})

it('file', (done) => {
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')
expect(unmarsheled.marshal()).to.deep.equal(raw)
done()
})

it.skip('metadata', (done) => {
})

it('symlink', (done) => {
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')
// TODO: waiting on https://github.com/ipfs/js-ipfs-data-importing/issues/3#issuecomment-182440079
// expect(unmarsheled.marshal()).to.deep.equal(raw)
done()
})
})
})
45 changes: 2 additions & 43 deletions test/unixfs-format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
'use strict'

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

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

Expand Down Expand Up @@ -91,45 +91,4 @@ describe('unixfs-format', () => {
}
})

describe('interop', () => {
it('raw', (done) => {
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')
expect(unmarsheled.marshal()).to.deep.equal(raw)
done()
})

it('directory', (done) => {
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')
expect(unmarsheled.marshal()).to.deep.equal(raw)
done()
})

it('file', (done) => {
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')
expect(unmarsheled.marshal()).to.deep.equal(raw)
done()
})

it.skip('metadata', (done) => {
})

it('symlink', (done) => {
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')
// TODO: waiting on https://github.com/ipfs/js-ipfs-data-importing/issues/3#issuecomment-182440079
// expect(unmarsheled.marshal()).to.deep.equal(raw)
done()
})
})
})