Skip to content

Commit a6c4208

Browse files
committed
feat: return mtime as Date object
1 parent 1ee93a1 commit a6c4208

File tree

3 files changed

+82
-84
lines changed

3 files changed

+82
-84
lines changed

src/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,9 @@ function Data (type, data) {
9494

9595
let mtime
9696

97-
if (!isNaN(this.mtime)) {
97+
if (this.mtime) {
9898
mtime = {
99-
seconds: this.mtime,
100-
hrValue: []
99+
seconds: Math.round(this.mtime.getTime() / 1000)
101100
}
102101
}
103102

@@ -115,8 +114,8 @@ function Data (type, data) {
115114
}
116115

117116
// decode from protobuf https://github.com/ipfs/go-ipfs/blob/master/unixfs/format.go#L24
118-
Data.unmarshal = (marsheled) => {
119-
const decoded = unixfsData.decode(marsheled)
117+
Data.unmarshal = (marshaled) => {
118+
const decoded = unixfsData.decode(marshaled)
120119

121120
if (!decoded.Data) {
122121
decoded.Data = undefined
@@ -130,7 +129,7 @@ Data.unmarshal = (marsheled) => {
130129
}
131130

132131
if (decoded.mtime) {
133-
obj.mtime = decoded.mtime.seconds
132+
obj.mtime = new Date(decoded.mtime.seconds * 1000)
134133
}
135134

136135
return obj

src/unixfs.proto.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@ message Mode {
3030
3131
message Mtime {
3232
required int64 seconds = 1;
33-
repeated int64 hrValue = 2;
3433
}`

test/unixfs-format.spec.js

Lines changed: 77 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -17,66 +17,66 @@ const Buffer = require('safe-buffer').Buffer
1717
describe('unixfs-format', () => {
1818
it('raw', () => {
1919
const data = new UnixFS('raw', Buffer.from('bananas'))
20-
const marshalled = data.marshal()
21-
const unmarshalled = UnixFS.unmarshal(marshalled)
22-
expect(data.type).to.equal(unmarshalled.type)
23-
expect(data.data).to.deep.equal(unmarshalled.data)
24-
expect(data.blockSizes).to.deep.equal(unmarshalled.blockSizes)
25-
expect(data.fileSize()).to.deep.equal(unmarshalled.fileSize())
20+
const marshaled = data.marshal()
21+
const unmarshaled = UnixFS.unmarshal(marshaled)
22+
expect(data.type).to.equal(unmarshaled.type)
23+
expect(data.data).to.deep.equal(unmarshaled.data)
24+
expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes)
25+
expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize())
2626
})
2727

2828
it('directory', () => {
2929
const data = new UnixFS('directory')
30-
const marshalled = data.marshal()
31-
const unmarshalled = UnixFS.unmarshal(marshalled)
32-
expect(data.type).to.equal(unmarshalled.type)
33-
expect(data.data).to.deep.equal(unmarshalled.data)
34-
expect(data.blockSizes).to.deep.equal(unmarshalled.blockSizes)
35-
expect(data.fileSize()).to.deep.equal(unmarshalled.fileSize())
30+
const marshaled = data.marshal()
31+
const unmarshaled = UnixFS.unmarshal(marshaled)
32+
expect(data.type).to.equal(unmarshaled.type)
33+
expect(data.data).to.deep.equal(unmarshaled.data)
34+
expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes)
35+
expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize())
3636
})
3737

3838
it('hamt-sharded-directory', () => {
3939
const data = new UnixFS('hamt-sharded-directory')
40-
const marshalled = data.marshal()
41-
const unmarshalled = UnixFS.unmarshal(marshalled)
42-
expect(data.type).to.equal(unmarshalled.type)
43-
expect(data.data).to.deep.equal(unmarshalled.data)
44-
expect(data.blockSizes).to.deep.equal(unmarshalled.blockSizes)
45-
expect(data.fileSize()).to.deep.equal(unmarshalled.fileSize())
40+
const marshaled = data.marshal()
41+
const unmarshaled = UnixFS.unmarshal(marshaled)
42+
expect(data.type).to.equal(unmarshaled.type)
43+
expect(data.data).to.deep.equal(unmarshaled.data)
44+
expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes)
45+
expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize())
4646
})
4747

4848
it('file', () => {
4949
const data = new UnixFS('file', Buffer.from('batata'))
50-
const marshalled = data.marshal()
51-
const unmarshalled = UnixFS.unmarshal(marshalled)
52-
expect(data.type).to.equal(unmarshalled.type)
53-
expect(data.data).to.deep.equal(unmarshalled.data)
54-
expect(data.blockSizes).to.deep.equal(unmarshalled.blockSizes)
55-
expect(data.fileSize()).to.deep.equal(unmarshalled.fileSize())
50+
const marshaled = data.marshal()
51+
const unmarshaled = UnixFS.unmarshal(marshaled)
52+
expect(data.type).to.equal(unmarshaled.type)
53+
expect(data.data).to.deep.equal(unmarshaled.data)
54+
expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes)
55+
expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize())
5656
})
5757

5858
it('file add blocksize', () => {
5959
const data = new UnixFS('file')
6060
data.addBlockSize(256)
61-
const marshalled = data.marshal()
62-
const unmarshalled = UnixFS.unmarshal(marshalled)
63-
expect(data.type).to.equal(unmarshalled.type)
64-
expect(data.data).to.deep.equal(unmarshalled.data)
65-
expect(data.blockSizes).to.deep.equal(unmarshalled.blockSizes)
66-
expect(data.fileSize()).to.deep.equal(unmarshalled.fileSize())
61+
const marshaled = data.marshal()
62+
const unmarshaled = UnixFS.unmarshal(marshaled)
63+
expect(data.type).to.equal(unmarshaled.type)
64+
expect(data.data).to.deep.equal(unmarshaled.data)
65+
expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes)
66+
expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize())
6767
})
6868

6969
it('file add and remove blocksize', () => {
7070
const data = new UnixFS('file')
7171
data.addBlockSize(256)
72-
const marshalled = data.marshal()
73-
const unmarshalled = UnixFS.unmarshal(marshalled)
74-
expect(data.type).to.equal(unmarshalled.type)
75-
expect(data.data).to.deep.equal(unmarshalled.data)
76-
expect(data.blockSizes).to.deep.equal(unmarshalled.blockSizes)
77-
expect(data.fileSize()).to.deep.equal(unmarshalled.fileSize())
78-
unmarshalled.removeBlockSize(0)
79-
expect(data.blockSizes).to.not.deep.equal(unmarshalled.blockSizes)
72+
const marshaled = data.marshal()
73+
const unmarshaled = UnixFS.unmarshal(marshaled)
74+
expect(data.type).to.equal(unmarshaled.type)
75+
expect(data.data).to.deep.equal(unmarshaled.data)
76+
expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes)
77+
expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize())
78+
unmarshaled.removeBlockSize(0)
79+
expect(data.blockSizes).to.not.deep.equal(unmarshaled.blockSizes)
8080
})
8181

8282
it('mode', () => {
@@ -92,12 +92,12 @@ describe('unixfs-format', () => {
9292
const data = new UnixFS('file')
9393
data.mode = mode
9494

95-
const unmarshalled = UnixFS.unmarshal(data.marshal())
96-
expect(unmarshalled).to.have.property('mode', mode)
95+
const unmarshaled = UnixFS.unmarshal(data.marshal())
96+
expect(unmarshaled).to.have.property('mode', mode)
9797

98-
delete unmarshalled.mode
98+
delete unmarshaled.mode
9999

100-
expect(UnixFS.unmarshal(unmarshalled.marshal())).to.not.have.property('mode')
100+
expect(UnixFS.unmarshal(unmarshaled.marshal())).to.not.have.property('mode')
101101
})
102102

103103
it('sets mode to 0', () => {
@@ -109,46 +109,46 @@ describe('unixfs-format', () => {
109109
})
110110

111111
it('mtime', () => {
112-
const mtime = parseInt(Date.now() / 1000)
112+
const mtime = new Date()
113113
const data = new UnixFS('file')
114114
data.mtime = mtime
115-
const marshalled = data.marshal()
116-
const unmarshalled = UnixFS.unmarshal(marshalled)
117-
expect(unmarshalled.mtime).to.equal(mtime)
115+
const marshaled = data.marshal()
116+
const unmarshaled = UnixFS.unmarshal(marshaled)
117+
expect(unmarshaled.mtime).to.deep.equal(new Date(Math.round(mtime.getTime() / 1000) * 1000))
118118
})
119119

120120
it('removes mtime', () => {
121-
const mtime = parseInt(Date.now() / 1000)
121+
const mtime = new Date()
122122
const data = new UnixFS('file')
123123
data.mtime = mtime
124124

125-
const unmarshalled = UnixFS.unmarshal(data.marshal())
126-
expect(unmarshalled).to.have.property('mtime', mtime)
125+
const unmarshaled = UnixFS.unmarshal(data.marshal())
126+
expect(unmarshaled).to.have.deep.property('mtime', new Date(Math.round(mtime.getTime() / 1000) * 1000))
127127

128-
delete unmarshalled.mtime
128+
delete unmarshaled.mtime
129129

130-
expect(UnixFS.unmarshal(unmarshalled.marshal())).to.not.have.property('mtime')
130+
expect(UnixFS.unmarshal(unmarshaled.marshal())).to.not.have.property('mtime')
131131
})
132132

133133
it('sets mtime to 0', () => {
134-
const mtime = 0
134+
const mtime = new Date(0)
135135
const data = new UnixFS('file')
136136
data.mtime = mtime
137137

138-
expect(UnixFS.unmarshal(data.marshal())).to.have.property('mtime', mtime)
138+
expect(UnixFS.unmarshal(data.marshal())).to.have.deep.property('mtime', new Date(Math.round(mtime.getTime() / 1000) * 1000))
139139
})
140140

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

144144
it('symlink', () => {
145145
const data = new UnixFS('symlink')
146-
const marshalled = data.marshal()
147-
const unmarshalled = UnixFS.unmarshal(marshalled)
148-
expect(data.type).to.equal(unmarshalled.type)
149-
expect(data.data).to.deep.equal(unmarshalled.data)
150-
expect(data.blockSizes).to.deep.equal(unmarshalled.blockSizes)
151-
expect(data.fileSize()).to.deep.equal(unmarshalled.fileSize())
146+
const marshaled = data.marshal()
147+
const unmarshaled = UnixFS.unmarshal(marshaled)
148+
expect(data.type).to.equal(unmarshaled.type)
149+
expect(data.data).to.deep.equal(unmarshaled.data)
150+
expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes)
151+
expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize())
152152
})
153153
it('wrong type', (done) => {
154154
let data
@@ -163,42 +163,42 @@ describe('unixfs-format', () => {
163163

164164
describe('interop', () => {
165165
it('raw', () => {
166-
const unmarshalled = UnixFS.unmarshal(raw)
167-
expect(unmarshalled.data).to.eql(Buffer.from('Hello UnixFS\n'))
168-
expect(unmarshalled.type).to.equal('file')
169-
expect(unmarshalled.marshal()).to.deep.equal(raw)
166+
const unmarshaled = UnixFS.unmarshal(raw)
167+
expect(unmarshaled.data).to.eql(Buffer.from('Hello UnixFS\n'))
168+
expect(unmarshaled.type).to.equal('file')
169+
expect(unmarshaled.marshal()).to.deep.equal(raw)
170170
})
171171

172172
it('directory', () => {
173-
const unmarshalled = UnixFS.unmarshal(directory)
174-
expect(unmarshalled.data).to.deep.equal(undefined)
175-
expect(unmarshalled.type).to.equal('directory')
176-
expect(unmarshalled.marshal()).to.deep.equal(directory)
173+
const unmarshaled = UnixFS.unmarshal(directory)
174+
expect(unmarshaled.data).to.deep.equal(undefined)
175+
expect(unmarshaled.type).to.equal('directory')
176+
expect(unmarshaled.marshal()).to.deep.equal(directory)
177177
})
178178

179179
it('file', () => {
180-
const unmarshalled = UnixFS.unmarshal(file)
181-
expect(unmarshalled.data).to.deep.equal(Buffer.from('Hello UnixFS\n'))
182-
expect(unmarshalled.type).to.equal('file')
183-
expect(unmarshalled.marshal()).to.deep.equal(file)
180+
const unmarshaled = UnixFS.unmarshal(file)
181+
expect(unmarshaled.data).to.deep.equal(Buffer.from('Hello UnixFS\n'))
182+
expect(unmarshaled.type).to.equal('file')
183+
expect(unmarshaled.marshal()).to.deep.equal(file)
184184
})
185185

186186
it.skip('metadata', () => {
187187
})
188188

189189
it('symlink', () => {
190-
const unmarshalled = UnixFS.unmarshal(symlink)
191-
expect(unmarshalled.data).to.deep.equal(Buffer.from('file.txt'))
192-
expect(unmarshalled.type).to.equal('symlink')
190+
const unmarshaled = UnixFS.unmarshal(symlink)
191+
expect(unmarshaled.data).to.deep.equal(Buffer.from('file.txt'))
192+
expect(unmarshaled.type).to.equal('symlink')
193193
// TODO: waiting on https://github.com/ipfs/js-ipfs-data-importing/issues/3#issuecomment-182440079
194-
// expect(unmarshalled.marshal()).to.deep.equal(symlink)
194+
// expect(unmarshaled.marshal()).to.deep.equal(symlink)
195195
})
196196
})
197197

198198
it('empty', () => {
199199
const data = new UnixFS('file')
200-
const marshalled = data.marshal()
200+
const marshaled = data.marshal()
201201

202-
expect(marshalled).to.deep.equal(Buffer.from([0x08, 0x02, 0x18, 0x00]))
202+
expect(marshaled).to.deep.equal(Buffer.from([0x08, 0x02, 0x18, 0x00]))
203203
})
204204
})

0 commit comments

Comments
 (0)