diff --git a/.gitignore b/.gitignore index 8cb3dc06..e706b2ae 100644 --- a/.gitignore +++ b/.gitignore @@ -33,4 +33,8 @@ node_modules .node_repl_history dist -lib \ No newline at end of file +lib + +# IntelliJ IDEA +.idea +js-ipfs-unixfs.iml \ No newline at end of file diff --git a/package.json b/package.json index 8eeba7f8..ed7c3de2 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/index.js b/src/index.js index b3b93e36..ecd8b2dd 100644 --- a/src/index.js +++ b/src/index.js @@ -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 diff --git a/test/test-data/directory.unixfs b/test-data/directory.unixfs similarity index 100% rename from test/test-data/directory.unixfs rename to test-data/directory.unixfs diff --git a/test/test-data/directory/file.txt b/test-data/directory/file.txt similarity index 100% rename from test/test-data/directory/file.txt rename to test-data/directory/file.txt diff --git a/test/test-data/file.txt b/test-data/file.txt similarity index 100% rename from test/test-data/file.txt rename to test-data/file.txt diff --git a/test/test-data/file.txt.unixfs b/test-data/file.txt.unixfs similarity index 100% rename from test/test-data/file.txt.unixfs rename to test-data/file.txt.unixfs diff --git a/test/test-data/raw.unixfs b/test-data/raw.unixfs similarity index 100% rename from test/test-data/raw.unixfs rename to test-data/raw.unixfs diff --git a/test/test-data/symlink.txt b/test-data/symlink.txt similarity index 100% rename from test/test-data/symlink.txt rename to test-data/symlink.txt diff --git a/test/test-data/symlink.txt.unixfs b/test-data/symlink.txt.unixfs similarity index 100% rename from test/test-data/symlink.txt.unixfs rename to test-data/symlink.txt.unixfs diff --git a/test/browser.js b/test/browser.js new file mode 100644 index 00000000..af94ca56 --- /dev/null +++ b/test/browser.js @@ -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) + }) + }) +}) diff --git a/test/unixfs-format-node.spec.js b/test/unixfs-format-node.spec.js new file mode 100644 index 00000000..6b0d8170 --- /dev/null +++ b/test/unixfs-format-node.spec.js @@ -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() + }) + }) +}) diff --git a/test/unixfs-format.spec.js b/test/unixfs-format.spec.js index f4a12e37..826d60ff 100644 --- a/test/unixfs-format.spec.js +++ b/test/unixfs-format.spec.js @@ -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') @@ -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() - }) - }) })