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

feat: adds metadata to unixfs #36

Merged
merged 1 commit into from
Nov 18, 2019
Merged
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"fs": false
},
"scripts": {
"test": "aegir test -t node -t browser",
"test": "aegir test -t node -t browser -t webworker",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser",
"test:webworker": "aegir test -t webworker",
Expand Down Expand Up @@ -36,7 +36,7 @@
},
"homepage": "https://github.com/ipfs/js-ipfs-unixfs#readme",
"devDependencies": {
"aegir": "^18.0.0",
"aegir": "^20.4.1",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"safe-buffer": "^5.1.2"
Expand Down
32 changes: 29 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ 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)
}
Expand Down Expand Up @@ -71,7 +79,6 @@ function Data (type, data) {
default:
throw new Error(`Unkown type: "${this.type}"`)
}
let fileSize = this.fileSize()

let data = this.data

Expand All @@ -85,13 +92,23 @@ function Data (type, data) {
blockSizes = undefined
}

if ((this.type === 'directory' || this.type === 'hamt-sharded-directory') && this.mode === parseInt('0755', 8)) {
delete this.mode
}

if (this.type === 'file' && this.mode === parseInt('0644', 8)) {
delete this.mode
}

return unixfsData.encode({
Type: type,
Data: data,
filesize: fileSize,
filesize: this.fileSize(),
blocksizes: blockSizes,
hashType: this.hashType,
fanout: this.fanout
fanout: this.fanout,
mode: this.mode,
mtime: this.mtime
})
}
}
Expand All @@ -104,6 +121,15 @@ Data.unmarshal = (marsheled) => {
}
const obj = new Data(types[decoded.Type], decoded.Data)
obj.blockSizes = decoded.blocksizes

if (decoded.mode) {
obj.mode = decoded.mode
}

if (decoded.mtime) {
obj.mtime = decoded.mtime
}

return obj
}

Expand Down
3 changes: 2 additions & 1 deletion src/unixfs.proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ module.exports = `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 {
Expand Down
42 changes: 42 additions & 0 deletions test/unixfs-format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,48 @@ describe('unixfs-format', () => {
expect(data.blockSizes).to.not.deep.equal(unmarshalled.blockSizes)
})

it('default mode for files', () => {
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))
})

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))
})

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('mode', () => {
const mode = parseInt('0555', 8)
const data = new UnixFS('file')
data.mode = mode
const marshalled = data.marshal()
const unmarshalled = UnixFS.unmarshal(marshalled)
expect(unmarshalled.mode).to.equal(mode)
})

it('mtime', () => {
const mtime = parseInt(Date.now() / 1000)
const data = new UnixFS('file')
data.mtime = mtime
const marshalled = data.marshal()
const unmarshalled = UnixFS.unmarshal(marshalled)
expect(unmarshalled.mtime).to.equal(mtime)
})

// figuring out what is this metadata for https://github.com/ipfs/js-ipfs-data-importing/issues/3#issuecomment-182336526
it.skip('metadata', () => {})

Expand Down