This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathtouch.js
187 lines (149 loc) · 4.9 KB
/
touch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* eslint-env mocha */
'use strict'
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
const { concat: uint8ArrayConcat } = require('uint8arrays/concat')
const { nanoid } = require('nanoid')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const delay = require('delay')
const all = require('it-all')
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {Object} options
*/
module.exports = (factory, options) => {
const describe = getDescribe(options)
const it = getIt(options)
describe('.files.touch', function () {
this.timeout(120 * 1000)
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
/**
* @param {import('ipfs-unixfs').MtimeLike} mtime
* @param {import('ipfs-unixfs').MtimeLike} expectedMtime
*/
async function testMtime (mtime, expectedMtime) {
const testPath = `/test-${nanoid()}`
await ipfs.files.write(testPath, uint8ArrayFromString('Hello, world!'), {
create: true
})
const stat = await ipfs.files.stat(testPath)
expect(stat).to.not.have.deep.property('mtime', expectedMtime)
await ipfs.files.touch(testPath, {
mtime
})
const stat2 = await ipfs.files.stat(testPath)
expect(stat2).to.have.deep.nested.property('mtime', expectedMtime)
}
before(async () => { ipfs = (await factory.spawn()).api })
after(() => factory.clean())
it('should have default mtime', async function () {
// @ts-ignore this is mocha
this.slow(5 * 1000)
const testPath = `/test-${nanoid()}`
await ipfs.files.write(testPath, uint8ArrayFromString('Hello, world!'), {
create: true
})
const stat = await ipfs.files.stat(testPath)
expect(stat).to.not.have.property('mtime')
await ipfs.files.touch(testPath)
const stat2 = await ipfs.files.stat(testPath)
expect(stat2).to.have.property('mtime').that.does.not.deep.equal({
secs: 0,
nsecs: 0
})
})
it('should update file mtime', async function () {
// @ts-ignore this is mocha
this.slow(5 * 1000)
const testPath = `/test-${nanoid()}`
const mtime = new Date()
const seconds = Math.floor(mtime.getTime() / 1000)
await ipfs.files.write(testPath, uint8ArrayFromString('Hello, world!'), {
create: true,
mtime
})
await delay(2000)
await ipfs.files.touch(testPath)
const stat = await ipfs.files.stat(testPath)
expect(stat).to.have.nested.property('mtime.secs').that.is.greaterThan(seconds)
})
it('should update directory mtime', async function () {
// @ts-ignore this is mocha
this.slow(5 * 1000)
const testPath = `/test-${nanoid()}`
const mtime = new Date()
const seconds = Math.floor(mtime.getTime() / 1000)
await ipfs.files.mkdir(testPath, {
create: true,
mtime
})
await delay(2000)
await ipfs.files.touch(testPath)
const stat2 = await ipfs.files.stat(testPath)
expect(stat2).to.have.nested.property('mtime.secs').that.is.greaterThan(seconds)
})
it('should update the mtime for a hamt-sharded-directory', async () => {
const path = `/foo-${Math.random()}`
await ipfs.files.mkdir(path, {
mtime: new Date()
})
await ipfs.files.write(`${path}/foo.txt`, uint8ArrayFromString('Hello world'), {
create: true,
shardSplitThreshold: 0
})
const originalMtime = (await ipfs.files.stat(path)).mtime
if (!originalMtime) {
throw new Error('No originalMtime found')
}
await delay(1000)
await ipfs.files.touch(path, {
flush: true
})
const updatedMtime = (await ipfs.files.stat(path)).mtime
if (!updatedMtime) {
throw new Error('No updatedMtime found')
}
expect(updatedMtime.secs).to.be.greaterThan(originalMtime.secs)
})
it('should create an empty file', async () => {
const path = `/foo-${Math.random()}`
await ipfs.files.touch(path, {
flush: true
})
const bytes = uint8ArrayConcat(await all(ipfs.files.read(path)))
expect(bytes.slice()).to.deep.equal(Uint8Array.from([]))
})
it('should set mtime as Date', async function () {
await testMtime(new Date(5000), {
secs: 5,
nsecs: 0
})
})
it('should set mtime as { nsecs, secs }', async function () {
const mtime = {
secs: 5,
nsecs: 0
}
await testMtime(mtime, mtime)
})
it('should set mtime as timespec', async function () {
await testMtime({
Seconds: 5,
FractionalNanoseconds: 0
}, {
secs: 5,
nsecs: 0
})
})
it('should set mtime as hrtime', async function () {
const mtime = process.hrtime()
await testMtime(mtime, {
secs: mtime[0],
nsecs: mtime[1]
})
})
})
}