From 00e5ea0567b4a3441b0b908b252de3eeac099805 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 12 Dec 2019 16:02:21 +0000 Subject: [PATCH 01/10] fix: allow mtime and mode to be optional Uses protobuf messages to enable having mode and mtime as actually optional values. --- src/index.js | 40 +++++++++++++----------- src/unixfs.proto.js | 12 ++++++-- test/unixfs-format.spec.js | 63 ++++++++++++++++++++++++-------------- 3 files changed, 72 insertions(+), 43 deletions(-) diff --git a/src/index.js b/src/index.js index 00dbf124..a325c7d3 100644 --- a/src/index.js +++ b/src/index.js @@ -32,14 +32,6 @@ function Data (type, data) { this.data = data this.blockSizes = [] - if (this.type === 'file') { - this.mode = parseInt('0644', 8) - } - - if (this.type === 'directory' || this.type === 'hamt-sharded-directory') { - this.mode = parseInt('0755', 8) - } - this.addBlockSize = (size) => { this.blockSizes.push(size) } @@ -92,12 +84,22 @@ function Data (type, data) { blockSizes = undefined } - if ((this.type === 'directory' || this.type === 'hamt-sharded-directory') && this.mode === parseInt('0755', 8)) { - delete this.mode + let mode + + if (this.mode != null && this.mode >= 0) { + mode = { + value: this.mode + } } - if (this.type === 'file' && this.mode === parseInt('0644', 8)) { - delete this.mode + let mtime + + if (this.mtime != null && this.mtime >= 0) { + mtime = { + value: [ + this.mtime + ] + } } return unixfsData.encode({ @@ -107,8 +109,8 @@ function Data (type, data) { blocksizes: blockSizes, hashType: this.hashType, fanout: this.fanout, - mode: this.mode, - mtime: this.mtime + mode: mode, + mtime: mtime }) } } @@ -116,18 +118,20 @@ function Data (type, data) { // 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) { decoded.Data = undefined } + const obj = new Data(types[decoded.Type], decoded.Data) obj.blockSizes = decoded.blocksizes - if (decoded.mode) { - obj.mode = decoded.mode + if (decoded.mode != null) { + obj.mode = decoded.mode.value } - if (decoded.mtime) { - obj.mtime = decoded.mtime + if (decoded.mtime != null) { + obj.mtime = decoded.mtime.value[0] } return obj diff --git a/src/unixfs.proto.js b/src/unixfs.proto.js index b8d4fd3c..3bfc9f6a 100644 --- a/src/unixfs.proto.js +++ b/src/unixfs.proto.js @@ -16,10 +16,18 @@ module.exports = `message Data { repeated uint64 blocksizes = 4; optional uint64 hashType = 5; optional uint64 fanout = 6; - optional uint32 mode = 7; - optional int64 mtime = 8; + optional Mode mode = 7; + optional Mtime mtime = 8; } message Metadata { optional string MimeType = 1; +} + +message Mode { + optional uint32 value = 1; +} + +message Mtime { + repeated int64 value = 1; }` diff --git a/test/unixfs-format.spec.js b/test/unixfs-format.spec.js index 0f352e76..89e06beb 100644 --- a/test/unixfs-format.spec.js +++ b/test/unixfs-format.spec.js @@ -79,37 +79,33 @@ describe('unixfs-format', () => { expect(data.blockSizes).to.not.deep.equal(unmarshalled.blockSizes) }) - it('default mode for files', () => { + it('mode', () => { + const mode = parseInt('0555', 8) const data = new UnixFS('file') - expect(data.mode).to.equal(parseInt('0644', 8)) - const marshalled = data.marshal() - const unmarshalled = UnixFS.unmarshal(marshalled) - expect(unmarshalled.mode).to.equal(parseInt('0644', 8)) - }) + data.mode = mode - it('default mode for directories', () => { - const data = new UnixFS('directory') - expect(data.mode).to.equal(parseInt('0755', 8)) - const marshalled = data.marshal() - const unmarshalled = UnixFS.unmarshal(marshalled) - expect(unmarshalled.mode).to.equal(parseInt('0755', 8)) + expect(UnixFS.unmarshal(data.marshal())).to.have.property('mode', mode) }) - it('default mode for hamt-sharded-directories', () => { - const data = new UnixFS('hamt-sharded-directory') - expect(data.mode).to.equal(parseInt('0755', 8)) - const marshalled = data.marshal() - const unmarshalled = UnixFS.unmarshal(marshalled) - expect(unmarshalled.mode).to.equal(parseInt('0755', 8)) + it('removes mode', () => { + const mode = parseInt('0555', 8) + const data = new UnixFS('file') + data.mode = mode + + const unmarshalled = UnixFS.unmarshal(data.marshal()) + expect(unmarshalled).to.have.property('mode', mode) + + delete unmarshalled.mode + + expect(UnixFS.unmarshal(unmarshalled.marshal())).to.not.have.property('mode') }) - it('mode', () => { - const mode = parseInt('0555', 8) + it('sets mode to 0', () => { + const mode = 0 const data = new UnixFS('file') data.mode = mode - const marshalled = data.marshal() - const unmarshalled = UnixFS.unmarshal(marshalled) - expect(unmarshalled.mode).to.equal(mode) + + expect(UnixFS.unmarshal(data.marshal())).to.have.property('mode', mode) }) it('mtime', () => { @@ -121,6 +117,27 @@ describe('unixfs-format', () => { expect(unmarshalled.mtime).to.equal(mtime) }) + it('removes mtime', () => { + const mtime = parseInt(Date.now() / 1000) + const data = new UnixFS('file') + data.mtime = mtime + + const unmarshalled = UnixFS.unmarshal(data.marshal()) + expect(unmarshalled).to.have.property('mtime', mtime) + + delete unmarshalled.mtime + + expect(UnixFS.unmarshal(unmarshalled.marshal())).to.not.have.property('mtime') + }) + + it('sets mtime to 0', () => { + const mtime = 0 + const data = new UnixFS('file') + data.mtime = mtime + + expect(UnixFS.unmarshal(data.marshal())).to.have.property('mtime', mtime) + }) + // figuring out what is this metadata for https://github.com/ipfs/js-ipfs-data-importing/issues/3#issuecomment-182336526 it.skip('metadata', () => {}) From 3a86a0b5b2801b7429c88b986c96f92c89baf694 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 12 Dec 2019 16:17:10 +0000 Subject: [PATCH 02/10] fix: values are required, containing types are not --- src/index.js | 15 +++++++-------- src/unixfs.proto.js | 6 +++--- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index a325c7d3..28f6672f 100644 --- a/src/index.js +++ b/src/index.js @@ -86,7 +86,7 @@ function Data (type, data) { let mode - if (this.mode != null && this.mode >= 0) { + if (!isNaN(this.mode)) { mode = { value: this.mode } @@ -94,11 +94,10 @@ function Data (type, data) { let mtime - if (this.mtime != null && this.mtime >= 0) { + if (!isNaN(this.mtime)) { mtime = { - value: [ - this.mtime - ] + value: this.mtime, + hrValue: [] } } @@ -126,12 +125,12 @@ Data.unmarshal = (marsheled) => { const obj = new Data(types[decoded.Type], decoded.Data) obj.blockSizes = decoded.blocksizes - if (decoded.mode != null) { + if (decoded.mode) { obj.mode = decoded.mode.value } - if (decoded.mtime != null) { - obj.mtime = decoded.mtime.value[0] + if (decoded.mtime) { + obj.mtime = decoded.mtime.value } return obj diff --git a/src/unixfs.proto.js b/src/unixfs.proto.js index 3bfc9f6a..54169fe8 100644 --- a/src/unixfs.proto.js +++ b/src/unixfs.proto.js @@ -21,13 +21,13 @@ module.exports = `message Data { } message Metadata { - optional string MimeType = 1; + required string MimeType = 1; } message Mode { - optional uint32 value = 1; + required uint32 value = 1; } message Mtime { - repeated int64 value = 1; + required int64 value = 1; }` From 04ea7a1fc61b0b6ce5035d65253675e2b4908b33 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 12 Dec 2019 16:17:43 +0000 Subject: [PATCH 03/10] fix: unsaved file buffer --- src/unixfs.proto.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/unixfs.proto.js b/src/unixfs.proto.js index 54169fe8..f3de815f 100644 --- a/src/unixfs.proto.js +++ b/src/unixfs.proto.js @@ -30,4 +30,5 @@ message Mode { message Mtime { required int64 value = 1; + repeated int64 hrValue = 1; }` From 397931ee0bd0e28055c51f006234788b2e2b6d57 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 12 Dec 2019 16:29:40 +0000 Subject: [PATCH 04/10] fix: use correct field index --- src/unixfs.proto.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unixfs.proto.js b/src/unixfs.proto.js index f3de815f..5700d541 100644 --- a/src/unixfs.proto.js +++ b/src/unixfs.proto.js @@ -30,5 +30,5 @@ message Mode { message Mtime { required int64 value = 1; - repeated int64 hrValue = 1; + repeated int64 hrValue = 2; }` From 1ee93a14c29b315860b34c1727ec4ba3a787ad55 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 12 Dec 2019 16:40:07 +0000 Subject: [PATCH 05/10] refactor: call seconds seconds --- src/index.js | 4 ++-- src/unixfs.proto.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 28f6672f..00a1a4e4 100644 --- a/src/index.js +++ b/src/index.js @@ -96,7 +96,7 @@ function Data (type, data) { if (!isNaN(this.mtime)) { mtime = { - value: this.mtime, + seconds: this.mtime, hrValue: [] } } @@ -130,7 +130,7 @@ Data.unmarshal = (marsheled) => { } if (decoded.mtime) { - obj.mtime = decoded.mtime.value + obj.mtime = decoded.mtime.seconds } return obj diff --git a/src/unixfs.proto.js b/src/unixfs.proto.js index 5700d541..da86a6e7 100644 --- a/src/unixfs.proto.js +++ b/src/unixfs.proto.js @@ -29,6 +29,6 @@ message Mode { } message Mtime { - required int64 value = 1; + required int64 seconds = 1; repeated int64 hrValue = 2; }` From a6c4208566632b6e718b0bd3b9a9999cab0e3dc2 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 12 Dec 2019 17:46:14 +0000 Subject: [PATCH 06/10] feat: return mtime as Date object --- src/index.js | 11 ++- src/unixfs.proto.js | 1 - test/unixfs-format.spec.js | 154 ++++++++++++++++++------------------- 3 files changed, 82 insertions(+), 84 deletions(-) diff --git a/src/index.js b/src/index.js index 00a1a4e4..e93d60c0 100644 --- a/src/index.js +++ b/src/index.js @@ -94,10 +94,9 @@ function Data (type, data) { let mtime - if (!isNaN(this.mtime)) { + if (this.mtime) { mtime = { - seconds: this.mtime, - hrValue: [] + seconds: Math.round(this.mtime.getTime() / 1000) } } @@ -115,8 +114,8 @@ function Data (type, data) { } // decode from protobuf https://github.com/ipfs/go-ipfs/blob/master/unixfs/format.go#L24 -Data.unmarshal = (marsheled) => { - const decoded = unixfsData.decode(marsheled) +Data.unmarshal = (marshaled) => { + const decoded = unixfsData.decode(marshaled) if (!decoded.Data) { decoded.Data = undefined @@ -130,7 +129,7 @@ Data.unmarshal = (marsheled) => { } if (decoded.mtime) { - obj.mtime = decoded.mtime.seconds + obj.mtime = new Date(decoded.mtime.seconds * 1000) } return obj diff --git a/src/unixfs.proto.js b/src/unixfs.proto.js index da86a6e7..60c201c2 100644 --- a/src/unixfs.proto.js +++ b/src/unixfs.proto.js @@ -30,5 +30,4 @@ message Mode { message Mtime { required int64 seconds = 1; - repeated int64 hrValue = 2; }` diff --git a/test/unixfs-format.spec.js b/test/unixfs-format.spec.js index 89e06beb..cb335489 100644 --- a/test/unixfs-format.spec.js +++ b/test/unixfs-format.spec.js @@ -17,66 +17,66 @@ const Buffer = require('safe-buffer').Buffer describe('unixfs-format', () => { it('raw', () => { const data = new UnixFS('raw', Buffer.from('bananas')) - const marshalled = data.marshal() - const unmarshalled = UnixFS.unmarshal(marshalled) - expect(data.type).to.equal(unmarshalled.type) - expect(data.data).to.deep.equal(unmarshalled.data) - expect(data.blockSizes).to.deep.equal(unmarshalled.blockSizes) - expect(data.fileSize()).to.deep.equal(unmarshalled.fileSize()) + const marshaled = data.marshal() + const unmarshaled = UnixFS.unmarshal(marshaled) + expect(data.type).to.equal(unmarshaled.type) + expect(data.data).to.deep.equal(unmarshaled.data) + expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes) + expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize()) }) it('directory', () => { const data = new UnixFS('directory') - const marshalled = data.marshal() - const unmarshalled = UnixFS.unmarshal(marshalled) - expect(data.type).to.equal(unmarshalled.type) - expect(data.data).to.deep.equal(unmarshalled.data) - expect(data.blockSizes).to.deep.equal(unmarshalled.blockSizes) - expect(data.fileSize()).to.deep.equal(unmarshalled.fileSize()) + const marshaled = data.marshal() + const unmarshaled = UnixFS.unmarshal(marshaled) + expect(data.type).to.equal(unmarshaled.type) + expect(data.data).to.deep.equal(unmarshaled.data) + expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes) + expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize()) }) it('hamt-sharded-directory', () => { const data = new UnixFS('hamt-sharded-directory') - const marshalled = data.marshal() - const unmarshalled = UnixFS.unmarshal(marshalled) - expect(data.type).to.equal(unmarshalled.type) - expect(data.data).to.deep.equal(unmarshalled.data) - expect(data.blockSizes).to.deep.equal(unmarshalled.blockSizes) - expect(data.fileSize()).to.deep.equal(unmarshalled.fileSize()) + const marshaled = data.marshal() + const unmarshaled = UnixFS.unmarshal(marshaled) + expect(data.type).to.equal(unmarshaled.type) + expect(data.data).to.deep.equal(unmarshaled.data) + expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes) + expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize()) }) it('file', () => { const data = new UnixFS('file', Buffer.from('batata')) - const marshalled = data.marshal() - const unmarshalled = UnixFS.unmarshal(marshalled) - expect(data.type).to.equal(unmarshalled.type) - expect(data.data).to.deep.equal(unmarshalled.data) - expect(data.blockSizes).to.deep.equal(unmarshalled.blockSizes) - expect(data.fileSize()).to.deep.equal(unmarshalled.fileSize()) + const marshaled = data.marshal() + const unmarshaled = UnixFS.unmarshal(marshaled) + expect(data.type).to.equal(unmarshaled.type) + expect(data.data).to.deep.equal(unmarshaled.data) + expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes) + expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize()) }) it('file add blocksize', () => { const data = new UnixFS('file') data.addBlockSize(256) - const marshalled = data.marshal() - const unmarshalled = UnixFS.unmarshal(marshalled) - expect(data.type).to.equal(unmarshalled.type) - expect(data.data).to.deep.equal(unmarshalled.data) - expect(data.blockSizes).to.deep.equal(unmarshalled.blockSizes) - expect(data.fileSize()).to.deep.equal(unmarshalled.fileSize()) + const marshaled = data.marshal() + const unmarshaled = UnixFS.unmarshal(marshaled) + expect(data.type).to.equal(unmarshaled.type) + expect(data.data).to.deep.equal(unmarshaled.data) + expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes) + expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize()) }) it('file add and remove blocksize', () => { const data = new UnixFS('file') data.addBlockSize(256) - const marshalled = data.marshal() - const unmarshalled = UnixFS.unmarshal(marshalled) - expect(data.type).to.equal(unmarshalled.type) - expect(data.data).to.deep.equal(unmarshalled.data) - expect(data.blockSizes).to.deep.equal(unmarshalled.blockSizes) - expect(data.fileSize()).to.deep.equal(unmarshalled.fileSize()) - unmarshalled.removeBlockSize(0) - expect(data.blockSizes).to.not.deep.equal(unmarshalled.blockSizes) + const marshaled = data.marshal() + const unmarshaled = UnixFS.unmarshal(marshaled) + expect(data.type).to.equal(unmarshaled.type) + expect(data.data).to.deep.equal(unmarshaled.data) + expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes) + expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize()) + unmarshaled.removeBlockSize(0) + expect(data.blockSizes).to.not.deep.equal(unmarshaled.blockSizes) }) it('mode', () => { @@ -92,12 +92,12 @@ describe('unixfs-format', () => { const data = new UnixFS('file') data.mode = mode - const unmarshalled = UnixFS.unmarshal(data.marshal()) - expect(unmarshalled).to.have.property('mode', mode) + const unmarshaled = UnixFS.unmarshal(data.marshal()) + expect(unmarshaled).to.have.property('mode', mode) - delete unmarshalled.mode + delete unmarshaled.mode - expect(UnixFS.unmarshal(unmarshalled.marshal())).to.not.have.property('mode') + expect(UnixFS.unmarshal(unmarshaled.marshal())).to.not.have.property('mode') }) it('sets mode to 0', () => { @@ -109,33 +109,33 @@ describe('unixfs-format', () => { }) it('mtime', () => { - const mtime = parseInt(Date.now() / 1000) + const mtime = new Date() const data = new UnixFS('file') data.mtime = mtime - const marshalled = data.marshal() - const unmarshalled = UnixFS.unmarshal(marshalled) - expect(unmarshalled.mtime).to.equal(mtime) + const marshaled = data.marshal() + const unmarshaled = UnixFS.unmarshal(marshaled) + expect(unmarshaled.mtime).to.deep.equal(new Date(Math.round(mtime.getTime() / 1000) * 1000)) }) it('removes mtime', () => { - const mtime = parseInt(Date.now() / 1000) + const mtime = new Date() const data = new UnixFS('file') data.mtime = mtime - const unmarshalled = UnixFS.unmarshal(data.marshal()) - expect(unmarshalled).to.have.property('mtime', mtime) + const unmarshaled = UnixFS.unmarshal(data.marshal()) + expect(unmarshaled).to.have.deep.property('mtime', new Date(Math.round(mtime.getTime() / 1000) * 1000)) - delete unmarshalled.mtime + delete unmarshaled.mtime - expect(UnixFS.unmarshal(unmarshalled.marshal())).to.not.have.property('mtime') + expect(UnixFS.unmarshal(unmarshaled.marshal())).to.not.have.property('mtime') }) it('sets mtime to 0', () => { - const mtime = 0 + const mtime = new Date(0) const data = new UnixFS('file') data.mtime = mtime - expect(UnixFS.unmarshal(data.marshal())).to.have.property('mtime', mtime) + expect(UnixFS.unmarshal(data.marshal())).to.have.deep.property('mtime', new Date(Math.round(mtime.getTime() / 1000) * 1000)) }) // figuring out what is this metadata for https://github.com/ipfs/js-ipfs-data-importing/issues/3#issuecomment-182336526 @@ -143,12 +143,12 @@ describe('unixfs-format', () => { it('symlink', () => { const data = new UnixFS('symlink') - const marshalled = data.marshal() - const unmarshalled = UnixFS.unmarshal(marshalled) - expect(data.type).to.equal(unmarshalled.type) - expect(data.data).to.deep.equal(unmarshalled.data) - expect(data.blockSizes).to.deep.equal(unmarshalled.blockSizes) - expect(data.fileSize()).to.deep.equal(unmarshalled.fileSize()) + const marshaled = data.marshal() + const unmarshaled = UnixFS.unmarshal(marshaled) + expect(data.type).to.equal(unmarshaled.type) + expect(data.data).to.deep.equal(unmarshaled.data) + expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes) + expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize()) }) it('wrong type', (done) => { let data @@ -163,42 +163,42 @@ describe('unixfs-format', () => { describe('interop', () => { it('raw', () => { - const unmarshalled = UnixFS.unmarshal(raw) - expect(unmarshalled.data).to.eql(Buffer.from('Hello UnixFS\n')) - expect(unmarshalled.type).to.equal('file') - expect(unmarshalled.marshal()).to.deep.equal(raw) + const unmarshaled = UnixFS.unmarshal(raw) + expect(unmarshaled.data).to.eql(Buffer.from('Hello UnixFS\n')) + expect(unmarshaled.type).to.equal('file') + expect(unmarshaled.marshal()).to.deep.equal(raw) }) it('directory', () => { - const unmarshalled = UnixFS.unmarshal(directory) - expect(unmarshalled.data).to.deep.equal(undefined) - expect(unmarshalled.type).to.equal('directory') - expect(unmarshalled.marshal()).to.deep.equal(directory) + const unmarshaled = UnixFS.unmarshal(directory) + expect(unmarshaled.data).to.deep.equal(undefined) + expect(unmarshaled.type).to.equal('directory') + expect(unmarshaled.marshal()).to.deep.equal(directory) }) it('file', () => { - const unmarshalled = UnixFS.unmarshal(file) - expect(unmarshalled.data).to.deep.equal(Buffer.from('Hello UnixFS\n')) - expect(unmarshalled.type).to.equal('file') - expect(unmarshalled.marshal()).to.deep.equal(file) + const unmarshaled = UnixFS.unmarshal(file) + expect(unmarshaled.data).to.deep.equal(Buffer.from('Hello UnixFS\n')) + expect(unmarshaled.type).to.equal('file') + expect(unmarshaled.marshal()).to.deep.equal(file) }) it.skip('metadata', () => { }) it('symlink', () => { - const unmarshalled = UnixFS.unmarshal(symlink) - expect(unmarshalled.data).to.deep.equal(Buffer.from('file.txt')) - expect(unmarshalled.type).to.equal('symlink') + const unmarshaled = UnixFS.unmarshal(symlink) + expect(unmarshaled.data).to.deep.equal(Buffer.from('file.txt')) + expect(unmarshaled.type).to.equal('symlink') // TODO: waiting on https://github.com/ipfs/js-ipfs-data-importing/issues/3#issuecomment-182440079 - // expect(unmarshalled.marshal()).to.deep.equal(symlink) + // expect(unmarshaled.marshal()).to.deep.equal(symlink) }) }) it('empty', () => { const data = new UnixFS('file') - const marshalled = data.marshal() + const marshaled = data.marshal() - expect(marshalled).to.deep.equal(Buffer.from([0x08, 0x02, 0x18, 0x00])) + expect(marshaled).to.deep.equal(Buffer.from([0x08, 0x02, 0x18, 0x00])) }) }) From e232acf8f1f2c45c6eae791468c56c844f185d82 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 17 Dec 2019 10:48:28 +0000 Subject: [PATCH 07/10] fix: update protons to latest version --- package.json | 2 +- src/index.js | 28 ++++++++++------------------ src/unixfs.proto.js | 18 +++++++----------- 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 03bea065..1ef36602 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "safe-buffer": "^5.1.2" }, "dependencies": { - "protons": "^1.0.1" + "protons": "^1.1.0" }, "contributors": [ "David Dias ", diff --git a/src/index.js b/src/index.js index e93d60c0..3da490b2 100644 --- a/src/index.js +++ b/src/index.js @@ -51,8 +51,8 @@ function Data (type, data) { this.blockSizes.forEach((size) => { sum += size }) - if (data) { - sum += data.length + if (this.data) { + sum += this.data.length } return sum } @@ -87,17 +87,13 @@ function Data (type, data) { let mode if (!isNaN(this.mode)) { - mode = { - value: this.mode - } + mode = this.mode } let mtime if (this.mtime) { - mtime = { - seconds: Math.round(this.mtime.getTime() / 1000) - } + mtime = Math.round(this.mtime.getTime() / 1000) } return unixfsData.encode({ @@ -116,20 +112,16 @@ function Data (type, data) { // decode from protobuf https://github.com/ipfs/go-ipfs/blob/master/unixfs/format.go#L24 Data.unmarshal = (marshaled) => { const decoded = unixfsData.decode(marshaled) - - if (!decoded.Data) { - decoded.Data = undefined - } - - const obj = new Data(types[decoded.Type], decoded.Data) + const data = decoded.hasData() ? decoded.Data : undefined + const obj = new Data(types[decoded.Type], data) obj.blockSizes = decoded.blocksizes - if (decoded.mode) { - obj.mode = decoded.mode.value + if (decoded.hasMode()) { + obj.mode = decoded.mode } - if (decoded.mtime) { - obj.mtime = new Date(decoded.mtime.seconds * 1000) + if (decoded.hasMtime()) { + obj.mtime = new Date(decoded.mtime * 1000) } return obj diff --git a/src/unixfs.proto.js b/src/unixfs.proto.js index 60c201c2..248423d5 100644 --- a/src/unixfs.proto.js +++ b/src/unixfs.proto.js @@ -1,6 +1,9 @@ 'use strict' -module.exports = `message Data { +module.exports = ` +syntax = "proto2"; + +message Data { enum DataType { Raw = 0; Directory = 1; @@ -16,18 +19,11 @@ module.exports = `message Data { repeated uint64 blocksizes = 4; optional uint64 hashType = 5; optional uint64 fanout = 6; - optional Mode mode = 7; - optional Mtime mtime = 8; + optional uint32 mode = 7; + optional int64 mtime = 8; } message Metadata { required string MimeType = 1; } - -message Mode { - required uint32 value = 1; -} - -message Mtime { - required int64 seconds = 1; -}` +` From 9517501c325887fa80db8deca6ee8cb967473314 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 17 Dec 2019 12:27:44 +0000 Subject: [PATCH 08/10] fix: remove boolean trap constructor and update readme --- README.md | 78 +++++++++++++++++------------- src/index.js | 66 ++++++++++++++++++++------ test/unixfs-format.spec.js | 97 +++++++++++++++++++++----------------- 3 files changed, 151 insertions(+), 90 deletions(-) diff --git a/README.md b/README.md index 8e882df5..01b6c13b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# ipfs-unixfs JavaScript Implementation +# ipfs-unixfs JavaScript Implementation [![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/) @@ -14,32 +14,29 @@ [The unixfs spec can be found inside the ipfs/specs repository](http://github.com/ipfs/specs) -## Lead Maintainer +## Lead Maintainer [Alex Potsides](https://github.com/achingbrain) -## Table of Contents - -- [ipfs-unixfs JavaScript Implementation](#ipfs-unixfs-javascript-implementation) - - [Lead Maintainer](#lead-maintainer) - - [Table of Contents](#table-of-contents) - - [Install](#install) - - [npm](#npm) - - [Use in Node.js](#use-in-nodejs) - - [Use in a browser with browserify, webpack or any other bundler](#use-in-a-browser-with-browserify--webpack-or-any-other-bundler) - - [Use in a browser Using a script tag](#use-in-a-browser-using-a-script-tag) - - [Usage](#usage) - - [Examples](#examples) - - [Create a file composed by several blocks](#create-a-file-composed-by-several-blocks) - - [Create a directory that contains several files](#create-a-directory-that-contains-several-files) - - [API](#api) - - [unixfs Data Structure](#unixfs-data-structure) - - [create an unixfs Data element](#create-an-unixfs-data-element) - - [add and remove a block size to the block size list](#add-and-remove-a-block-size-to-the-block-size-list) - - [get total fileSize](#get-total-filesize) - - [marshal and unmarshal](#marshal-and-unmarshal) - - [Contribute](#contribute) - - [License](#license) +## Table of Contents + +- [Install](#install) + - [npm](#npm) + - [Use in Node.js](#use-in-nodejs) + - [Use in a browser with browserify, webpack or any other bundler](#use-in-a-browser-with-browserify-webpack-or-any-other-bundler) + - [Use in a browser Using a script tag](#use-in-a-browser-using-a-script-tag) +- [Usage](#usage) + - [Examples](#examples) + - [Create a file composed by several blocks](#create-a-file-composed-by-several-blocks) + - [Create a directory that contains several files](#create-a-directory-that-contains-several-files) +- [API](#api) + - [unixfs Data Structure](#unixfs-data-structure) + - [create an unixfs Data element](#create-an-unixfs-data-element) + - [add and remove a block size to the block size list](#add-and-remove-a-block-size-to-the-block-size-list) + - [get total fileSize](#get-total-filesize) + - [marshal and unmarshal](#marshal-and-unmarshal) +- [Contribute](#contribute) +- [License](#license) ## Install @@ -80,7 +77,7 @@ Loading this module through a script tag will make the `Unixfs` obj available in #### Create a file composed by several blocks ```JavaScript -var data = new Unixfs('file') +const data = new Unixfs({ type: 'file' }) data.addBlockSize(256) // add the size of each block data.addBlockSize(256) // ... @@ -91,7 +88,7 @@ data.addBlockSize(256) Creating a directory that contains several files is achieve by creating a unixfs element that identifies a MerkleDAG node as a directory. The links of that MerkleDAG node are the files that are contained in this directory. ```JavaScript -var data = new Unixfs('directory') +const data = new Unixfs({ type: 'directory' }) ``` ## API @@ -99,6 +96,8 @@ var data = new Unixfs('directory') #### unixfs Data Structure ```protobuf +syntax = "proto2"; + message Data { enum DataType { Raw = 0; @@ -113,23 +112,36 @@ message Data { optional bytes Data = 2; optional uint64 filesize = 3; repeated uint64 blocksizes = 4; - optional uint64 hashType = 5; optional uint64 fanout = 6; + optional uint32 mode = 7; + optional int64 mtime = 8; } message Metadata { - optional string MimeType = 1; + required string MimeType = 1; } ``` #### create an unixfs Data element ```JavaScript -var data = new UnixFS(, []) +const data = new UnixFS([options]) ``` -Type can be: `['raw', 'directory', 'file', 'metadata', 'symlink', 'hamt-sharded-directory']` +`options` is an optional object argument that might include the following keys: + +- type (string, default `file`): The type of UnixFS node. Can be: + - `raw` + - `directory` + - `file` + - `metadata` + - `symlink` + - `hamt-sharded-directory` +- data (Buffer): The optional data field for this node +- blockSizes (Array, default: `[]`): If this is a `file` node that is made up of multiple blocks, `blockSizes` is a list numbers that represent the size of the file chunks stored in each child node. It is used to calculate the total file size. +- mode (Number, default `0644` for files, `0755` for directories/hamt-sharded-directories) file mode +- mtime (Date, default `0`): The modification time of this node #### add and remove a block size to the block size list @@ -149,9 +161,9 @@ data.fileSize() // => size in bytes #### marshal and unmarshal -``` -var marshaled = data.marshal() -var unmarshaled = Unixfs.unmarshal(marshaled) +```javascript +const marshaled = data.marshal() +const unmarshaled = Unixfs.unmarshal(marshaled) ``` ## Contribute diff --git a/src/index.js b/src/index.js index 3da490b2..ba0ebf5c 100644 --- a/src/index.js +++ b/src/index.js @@ -20,17 +20,46 @@ const dirTypes = [ 'hamt-sharded-directory' ] -function Data (type, data) { +const DEFAULT_FILE_MODE = parseInt('0644', 8) +const DEFAULT_DIRECTORY_MODE = parseInt('0755', 8) + +function Data (arg1, arg2) { if (!(this instanceof Data)) { - return new Data(type, data) + return new Data(arg1, arg2) + } + + if (arg1 == null) { + arg1 = { + type: 'file' + } + } + + let { type, data, blockSizes, mtime, mode } = arg1 + + if (typeof arg1 === 'string' || arg1 instanceof String) { + // support old-style constructor + type = arg1 + data = arg2 } + if (types.indexOf(type) === -1) { throw new Error('Type: ' + type + ' is not valid') } this.type = type this.data = data - this.blockSizes = [] + this.blockSizes = blockSizes || [] + + this.mtime = mtime || new Date(0) + this.mode = mode + + if (this.mode === undefined && type === 'file') { + this.mode = DEFAULT_FILE_MODE + } + + if (this.mode === undefined && type.includes('directory')) { + this.mode = DEFAULT_DIRECTORY_MODE + } this.addBlockSize = (size) => { this.blockSizes.push(size) @@ -88,12 +117,24 @@ function Data (type, data) { if (!isNaN(this.mode)) { mode = this.mode + + if (mode === DEFAULT_FILE_MODE && this.type === 'file') { + mode = undefined + } + + if (mode === DEFAULT_DIRECTORY_MODE && this.type.includes('directory')) { + mode = undefined + } } let mtime if (this.mtime) { mtime = Math.round(this.mtime.getTime() / 1000) + + if (mtime === 0) { + mtime = undefined + } } return unixfsData.encode({ @@ -112,19 +153,14 @@ function Data (type, data) { // decode from protobuf https://github.com/ipfs/go-ipfs/blob/master/unixfs/format.go#L24 Data.unmarshal = (marshaled) => { const decoded = unixfsData.decode(marshaled) - const data = decoded.hasData() ? decoded.Data : undefined - const obj = new Data(types[decoded.Type], data) - obj.blockSizes = decoded.blocksizes - - if (decoded.hasMode()) { - obj.mode = decoded.mode - } - - if (decoded.hasMtime()) { - obj.mtime = new Date(decoded.mtime * 1000) - } - return obj + return new Data({ + type: types[decoded.Type], + data: decoded.hasData() ? decoded.Data : undefined, + blockSizes: decoded.blocksizes, + mode: decoded.hasMode() ? decoded.mode : undefined, + mtime: decoded.hasMtime() ? new Date(decoded.mtime * 1000) : undefined + }) } exports = module.exports = Data diff --git a/test/unixfs-format.spec.js b/test/unixfs-format.spec.js index cb335489..00d37251 100644 --- a/test/unixfs-format.spec.js +++ b/test/unixfs-format.spec.js @@ -15,8 +15,22 @@ const symlink = loadFixture('test/fixtures/symlink.txt.unixfs') const Buffer = require('safe-buffer').Buffer describe('unixfs-format', () => { + it('defaults to file', () => { + const data = new UnixFS() + expect(data.type).to.equal('file') + const marshaled = data.marshal() + const unmarshaled = UnixFS.unmarshal(marshaled) + expect(data.type).to.equal(unmarshaled.type) + expect(data.data).to.deep.equal(unmarshaled.data) + expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes) + expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize()) + }) + it('raw', () => { - const data = new UnixFS('raw', Buffer.from('bananas')) + const data = new UnixFS({ + type: 'raw', + data: Buffer.from('bananas') + }) const marshaled = data.marshal() const unmarshaled = UnixFS.unmarshal(marshaled) expect(data.type).to.equal(unmarshaled.type) @@ -26,7 +40,9 @@ describe('unixfs-format', () => { }) it('directory', () => { - const data = new UnixFS('directory') + const data = new UnixFS({ + type: 'directory' + }) const marshaled = data.marshal() const unmarshaled = UnixFS.unmarshal(marshaled) expect(data.type).to.equal(unmarshaled.type) @@ -36,7 +52,9 @@ describe('unixfs-format', () => { }) it('hamt-sharded-directory', () => { - const data = new UnixFS('hamt-sharded-directory') + const data = new UnixFS({ + type: 'hamt-sharded-directory' + }) const marshaled = data.marshal() const unmarshaled = UnixFS.unmarshal(marshaled) expect(data.type).to.equal(unmarshaled.type) @@ -46,7 +64,10 @@ describe('unixfs-format', () => { }) it('file', () => { - const data = new UnixFS('file', Buffer.from('batata')) + const data = new UnixFS({ + type: 'file', + data: Buffer.from('batata') + }) const marshaled = data.marshal() const unmarshaled = UnixFS.unmarshal(marshaled) expect(data.type).to.equal(unmarshaled.type) @@ -56,7 +77,9 @@ describe('unixfs-format', () => { }) it('file add blocksize', () => { - const data = new UnixFS('file') + const data = new UnixFS({ + type: 'file' + }) data.addBlockSize(256) const marshaled = data.marshal() const unmarshaled = UnixFS.unmarshal(marshaled) @@ -67,7 +90,9 @@ describe('unixfs-format', () => { }) it('file add and remove blocksize', () => { - const data = new UnixFS('file') + const data = new UnixFS({ + type: 'file' + }) data.addBlockSize(256) const marshaled = data.marshal() const unmarshaled = UnixFS.unmarshal(marshaled) @@ -81,28 +106,19 @@ describe('unixfs-format', () => { it('mode', () => { const mode = parseInt('0555', 8) - const data = new UnixFS('file') + const data = new UnixFS({ + type: 'file' + }) data.mode = mode expect(UnixFS.unmarshal(data.marshal())).to.have.property('mode', mode) }) - it('removes mode', () => { - const mode = parseInt('0555', 8) - const data = new UnixFS('file') - data.mode = mode - - const unmarshaled = UnixFS.unmarshal(data.marshal()) - expect(unmarshaled).to.have.property('mode', mode) - - delete unmarshaled.mode - - expect(UnixFS.unmarshal(unmarshaled.marshal())).to.not.have.property('mode') - }) - it('sets mode to 0', () => { const mode = 0 - const data = new UnixFS('file') + const data = new UnixFS({ + type: 'file' + }) data.mode = mode expect(UnixFS.unmarshal(data.marshal())).to.have.property('mode', mode) @@ -110,31 +126,22 @@ describe('unixfs-format', () => { it('mtime', () => { const mtime = new Date() - const data = new UnixFS('file') - data.mtime = mtime + const data = new UnixFS({ + type: 'file', + mtime + }) + const marshaled = data.marshal() const unmarshaled = UnixFS.unmarshal(marshaled) expect(unmarshaled.mtime).to.deep.equal(new Date(Math.round(mtime.getTime() / 1000) * 1000)) }) - it('removes mtime', () => { - const mtime = new Date() - const data = new UnixFS('file') - data.mtime = mtime - - const unmarshaled = UnixFS.unmarshal(data.marshal()) - expect(unmarshaled).to.have.deep.property('mtime', new Date(Math.round(mtime.getTime() / 1000) * 1000)) - - delete unmarshaled.mtime - - expect(UnixFS.unmarshal(unmarshaled.marshal())).to.not.have.property('mtime') - }) - it('sets mtime to 0', () => { const mtime = new Date(0) - const data = new UnixFS('file') - data.mtime = mtime - + const data = new UnixFS({ + type: 'file', + mtime + }) expect(UnixFS.unmarshal(data.marshal())).to.have.deep.property('mtime', new Date(Math.round(mtime.getTime() / 1000) * 1000)) }) @@ -142,7 +149,9 @@ describe('unixfs-format', () => { it.skip('metadata', () => {}) it('symlink', () => { - const data = new UnixFS('symlink') + const data = new UnixFS({ + type: 'symlink' + }) const marshaled = data.marshal() const unmarshaled = UnixFS.unmarshal(marshaled) expect(data.type).to.equal(unmarshaled.type) @@ -153,7 +162,9 @@ describe('unixfs-format', () => { it('wrong type', (done) => { let data try { - data = new UnixFS('bananas') + data = new UnixFS({ + type: 'bananas' + }) } catch (err) { expect(err).to.exist() expect(data).to.not.exist() @@ -196,7 +207,9 @@ describe('unixfs-format', () => { }) it('empty', () => { - const data = new UnixFS('file') + const data = new UnixFS({ + type: 'file' + }) const marshaled = data.marshal() expect(marshaled).to.deep.equal(Buffer.from([0x08, 0x02, 0x18, 0x00])) From 45b0b30427ed7471a9df681c72528a54243534b5 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 18 Dec 2019 08:15:37 +0000 Subject: [PATCH 09/10] fix: address PR comments, refactor to modern class --- README.md | 33 +++++++---- package.json | 3 +- src/index.js | 117 ++++++++++++++++++++++--------------- src/unixfs.proto.js | 2 +- test/unixfs-format.spec.js | 2 +- 5 files changed, 95 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 01b6c13b..5e7a445a 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,9 @@ ![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square) ![](https://img.shields.io/badge/Node.js-%3E%3D8.0.0-orange.svg?style=flat-square) -> JavaScript implementation of IPFS' unixfs (a Unix FileSystem files representation on top of a MerkleDAG) +> JavaScript implementation of IPFS' UnixFS (a Unix FileSystem files representation on top of a MerkleDAG) -[The unixfs spec can be found inside the ipfs/specs repository](http://github.com/ipfs/specs) +The UnixFS spec can be found inside the [ipfs/specs repository](http://github.com/ipfs/specs) ## Lead Maintainer @@ -30,11 +30,12 @@ - [Create a file composed by several blocks](#create-a-file-composed-by-several-blocks) - [Create a directory that contains several files](#create-a-directory-that-contains-several-files) - [API](#api) - - [unixfs Data Structure](#unixfs-data-structure) + - [UnixFS Data Structure](#unixfs-data-structure) - [create an unixfs Data element](#create-an-unixfs-data-element) - [add and remove a block size to the block size list](#add-and-remove-a-block-size-to-the-block-size-list) - [get total fileSize](#get-total-filesize) - [marshal and unmarshal](#marshal-and-unmarshal) + - [is this UnixFS entry a directory?](#is-this-unixfs-entry-a-directory) - [Contribute](#contribute) - [License](#license) @@ -49,7 +50,7 @@ ### Use in Node.js ```JavaScript -var Unixfs = require('ipfs-unixfs') +var UnixFS = require('ipfs-unixfs') ``` ### Use in a browser with browserify, webpack or any other bundler @@ -57,12 +58,12 @@ var Unixfs = require('ipfs-unixfs') The code published to npm that gets loaded on require is in fact a ES5 transpiled version with the right shims added. This means that you can require it and use with your favourite bundler without having to adjust asset management process. ```JavaScript -var Unixfs = require('ipfs-unixfs') +var UnixFS = require('ipfs-unixfs') ``` ### Use in a browser Using a script tag -Loading this module through a script tag will make the `Unixfs` obj available in the global namespace. +Loading this module through a script tag will make the `UnixFS` obj available in the global namespace. ```html @@ -77,7 +78,7 @@ Loading this module through a script tag will make the `Unixfs` obj available in #### Create a file composed by several blocks ```JavaScript -const data = new Unixfs({ type: 'file' }) +const data = new UnixFS({ type: 'file' }) data.addBlockSize(256) // add the size of each block data.addBlockSize(256) // ... @@ -88,12 +89,12 @@ data.addBlockSize(256) Creating a directory that contains several files is achieve by creating a unixfs element that identifies a MerkleDAG node as a directory. The links of that MerkleDAG node are the files that are contained in this directory. ```JavaScript -const data = new Unixfs({ type: 'directory' }) +const data = new UnixFS({ type: 'directory' }) ``` ## API -#### unixfs Data Structure +#### UnixFS Data Structure ```protobuf syntax = "proto2"; @@ -119,7 +120,7 @@ message Data { } message Metadata { - required string MimeType = 1; + optional string MimeType = 1; } ``` @@ -131,7 +132,7 @@ const data = new UnixFS([options]) `options` is an optional object argument that might include the following keys: -- type (string, default `file`): The type of UnixFS node. Can be: +- type (string, default `file`): The type of UnixFS entry. Can be: - `raw` - `directory` - `file` @@ -166,6 +167,16 @@ const marshaled = data.marshal() const unmarshaled = Unixfs.unmarshal(marshaled) ``` +#### is this UnixFS entry a directory? + +```JavaScript +const dir = new Data({ type: 'directory' }) +dir.isDirectory() // true + +const file = new Data({ type: 'file' }) +file.isDirectory() // false +``` + ## Contribute Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/js-ipfs-unixfs/issues)! diff --git a/package.json b/package.json index 1ef36602..f00d3084 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,7 @@ "devDependencies": { "aegir": "^20.4.1", "chai": "^4.2.0", - "dirty-chai": "^2.0.1", - "safe-buffer": "^5.1.2" + "dirty-chai": "^2.0.1" }, "dependencies": { "protons": "^1.1.0" diff --git a/src/index.js b/src/index.js index ba0ebf5c..1847100c 100644 --- a/src/index.js +++ b/src/index.js @@ -2,9 +2,7 @@ const protons = require('protons') const pb = protons(require('./unixfs.proto')) -// encode/decode const unixfsData = pb.Data -// const unixfsMetadata = pb.MetaData // encode/decode const types = [ 'raw', @@ -23,55 +21,91 @@ const dirTypes = [ const DEFAULT_FILE_MODE = parseInt('0644', 8) const DEFAULT_DIRECTORY_MODE = parseInt('0755', 8) -function Data (arg1, arg2) { - if (!(this instanceof Data)) { - return new Data(arg1, arg2) - } - - if (arg1 == null) { - arg1 = { +function parseArgs (args) { + if (args.length === 0) { + return { type: 'file' } } - let { type, data, blockSizes, mtime, mode } = arg1 - - if (typeof arg1 === 'string' || arg1 instanceof String) { + if (args.length === 2) { // support old-style constructor - type = arg1 - data = arg2 + return { + type: args[0], + data: args[1] + } } - if (types.indexOf(type) === -1) { - throw new Error('Type: ' + type + ' is not valid') + if (typeof args[0] === 'string' || args[0] instanceof String) { + return { + type: args[0] + } + } + + return args[0] +} + +class Data { + // decode from protobuf https://github.com/ipfs/specs/blob/master/UNIXFS.md + static unmarshal (marshaled) { + const decoded = unixfsData.decode(marshaled) + + return new Data({ + type: types[decoded.Type], + data: decoded.hasData() ? decoded.Data : undefined, + blockSizes: decoded.blocksizes, + mode: decoded.hasMode() ? decoded.mode : undefined, + mtime: decoded.hasMtime() ? new Date(decoded.mtime * 1000) : undefined + }) } - this.type = type - this.data = data - this.blockSizes = blockSizes || [] + constructor (...args) { + const { + type, + data, + blockSizes, + hashType, + fanout, + mtime, + mode + } = parseArgs(args) + + if (!types.includes(type)) { + throw new Error('Type: ' + type + ' is not valid') + } - this.mtime = mtime || new Date(0) - this.mode = mode + this.type = type + this.data = data + this.hashType = hashType + this.fanout = fanout + this.blockSizes = blockSizes || [] + this.mtime = mtime || new Date(0) + this.mode = mode - if (this.mode === undefined && type === 'file') { - this.mode = DEFAULT_FILE_MODE + if (this.mode === undefined && type === 'file') { + this.mode = DEFAULT_FILE_MODE + } + + if (this.mode === undefined && this.isDirectory()) { + this.mode = DEFAULT_DIRECTORY_MODE + } } - if (this.mode === undefined && type.includes('directory')) { - this.mode = DEFAULT_DIRECTORY_MODE + isDirectory () { + return dirTypes.includes(this.type) } - this.addBlockSize = (size) => { + addBlockSize (size) { this.blockSizes.push(size) } - this.removeBlockSize = (index) => { + removeBlockSize (index) { this.blockSizes.splice(index, 1) } // data.length + blockSizes - this.fileSize = () => { - if (dirTypes.indexOf(this.type) >= 0) { + fileSize () { + if (this.isDirectory()) { // dirs don't have file size return undefined } @@ -80,14 +114,16 @@ function Data (arg1, arg2) { this.blockSizes.forEach((size) => { sum += size }) + if (this.data) { sum += this.data.length } + return sum } // encode to protobuf - this.marshal = () => { + marshal () { let type switch (this.type) { @@ -115,7 +151,7 @@ function Data (arg1, arg2) { let mode - if (!isNaN(this.mode)) { + if (!isNaN(parseInt(this.mode))) { mode = this.mode if (mode === DEFAULT_FILE_MODE && this.type === 'file') { @@ -144,23 +180,10 @@ function Data (arg1, arg2) { blocksizes: blockSizes, hashType: this.hashType, fanout: this.fanout, - mode: mode, - mtime: mtime + mode, + mtime }) } } -// decode from protobuf https://github.com/ipfs/go-ipfs/blob/master/unixfs/format.go#L24 -Data.unmarshal = (marshaled) => { - const decoded = unixfsData.decode(marshaled) - - return new Data({ - type: types[decoded.Type], - data: decoded.hasData() ? decoded.Data : undefined, - blockSizes: decoded.blocksizes, - mode: decoded.hasMode() ? decoded.mode : undefined, - mtime: decoded.hasMtime() ? new Date(decoded.mtime * 1000) : undefined - }) -} - -exports = module.exports = Data +module.exports = Data diff --git a/src/unixfs.proto.js b/src/unixfs.proto.js index 248423d5..fcc8931d 100644 --- a/src/unixfs.proto.js +++ b/src/unixfs.proto.js @@ -24,6 +24,6 @@ message Data { } message Metadata { - required string MimeType = 1; + optional string MimeType = 1; } ` diff --git a/test/unixfs-format.spec.js b/test/unixfs-format.spec.js index 00d37251..766d99be 100644 --- a/test/unixfs-format.spec.js +++ b/test/unixfs-format.spec.js @@ -12,7 +12,7 @@ const raw = loadFixture('test/fixtures/raw.unixfs') const directory = loadFixture('test/fixtures/directory.unixfs') const file = loadFixture('test/fixtures/file.txt.unixfs') const symlink = loadFixture('test/fixtures/symlink.txt.unixfs') -const Buffer = require('safe-buffer').Buffer +const { Buffer } = require('buffer') describe('unixfs-format', () => { it('defaults to file', () => { From d93b32be2bb03bfb9534e1c2ce04a603b3b47e73 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Wed, 18 Dec 2019 11:01:08 +0000 Subject: [PATCH 10/10] chore: pr comments Co-Authored-By: Alan Shaw --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 1847100c..5e0c9bb8 100644 --- a/src/index.js +++ b/src/index.js @@ -158,7 +158,7 @@ class Data { mode = undefined } - if (mode === DEFAULT_DIRECTORY_MODE && this.type.includes('directory')) { + if (mode === DEFAULT_DIRECTORY_MODE && this.isDirectory()) { mode = undefined } }