Skip to content

Commit

Permalink
fix: nanosecond accuracy for non-browser environments only
Browse files Browse the repository at this point in the history
  • Loading branch information
jayree committed Jun 19, 2023
1 parent ce73521 commit 81095c1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
25 changes: 14 additions & 11 deletions __tests__/test-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,20 @@ describe('status', () => {
* Without the fix, the second write is not detected and the status is 'added'
* instead of '*added'
*/
it('status of a file changed twice within a second', async () => {
// Setup
const { fs, dir } = await makeFixture('test-empty')
const file = 'a.txt'
;(process.browser ? xit : it)(
'status of a file changed twice within a second',
async () => {
// Setup
const { fs, dir } = await makeFixture('test-empty')
const file = 'a.txt'

await fs.write(path.join(dir, file), 'Hi')
await add({ fs, dir, filepath: file })
await fs.write(path.join(dir, file), 'Ho')
await fs.write(path.join(dir, file), 'Hi')
await add({ fs, dir, filepath: file })
await fs.write(path.join(dir, file), 'Ho')

// Test
const a = await status({ fs, dir, filepath: file })
expect(a).toEqual('*added')
})
// Test
const a = await status({ fs, dir, filepath: file })
expect(a).toEqual('*added')
}
)
})
7 changes: 6 additions & 1 deletion src/models/FileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,12 @@ export class FileSystem {
*/
async lstat(filename) {
try {
const stats = await this._lstat(filename, { bigint: true })
const stats = await this._lstat(filename)
if (!process.browser) {
const statsNs = await this._lstat(filename, { bigint: true })
stats.mtimeNs = statsNs.mtimeNs
stats.ctimeNs = statsNs.ctimeNs
}
return stats
} catch (err) {
if (err.code === 'ENOENT') {
Expand Down
6 changes: 4 additions & 2 deletions src/utils/compareStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ export function compareStats(entry, stats) {
e.mode !== s.mode ||
e.mtimeSeconds !== s.mtimeSeconds ||
e.ctimeSeconds !== s.ctimeSeconds ||
e.mtimeNanoseconds !== s.mtimeNanoseconds ||
e.ctimeNanoseconds !== s.ctimeNanoseconds ||
(e.mtimeNanoseconds &&
s.mtimeNanoseconds &&
(e.mtimeNanoseconds !== s.mtimeNanoseconds ||
e.ctimeNanoseconds !== s.ctimeNanoseconds)) ||
e.uid !== s.uid ||
e.gid !== s.gid ||
e.ino !== s.ino ||
Expand Down
22 changes: 13 additions & 9 deletions src/utils/normalizeStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ function SecondsNanoseconds(givenSeconds, givenNanoseconds, nanoseconds, date) {
if (givenSeconds !== undefined && givenNanoseconds !== undefined) {
return [givenSeconds, givenNanoseconds]
}
let seconds
if (nanoseconds === undefined) {
nanoseconds = BigInt(date.valueOf() * 1e6)
const milliseconds = date.valueOf()
seconds = Math.trunc(milliseconds / 1000)
} else {
seconds = Number(nanoseconds / BigInt(1e9))
nanoseconds = Number(nanoseconds % BigInt(1e9))
}
const seconds = Number(nanoseconds / BigInt(1e9))
nanoseconds = Number(nanoseconds % BigInt(1e9))

return [seconds, nanoseconds]
}

Expand All @@ -33,13 +37,13 @@ export function normalizeStats(e) {
ctimeNanoseconds: ctimeNanoseconds % MAX_UINT32,
mtimeSeconds: mtimeSeconds % MAX_UINT32,
mtimeNanoseconds: mtimeNanoseconds % MAX_UINT32,
dev: Number(e.dev) % MAX_UINT32,
ino: Number(e.ino) % MAX_UINT32,
mode: normalizeMode(Number(e.mode) % MAX_UINT32),
uid: Number(e.uid) % MAX_UINT32,
gid: Number(e.gid) % MAX_UINT32,
dev: e.dev % MAX_UINT32,
ino: e.ino % MAX_UINT32,
mode: normalizeMode(e.mode % MAX_UINT32),
uid: e.uid % MAX_UINT32,
gid: e.gid % MAX_UINT32,
// size of -1 happens over a BrowserFS HTTP Backend that doesn't serve Content-Length headers
// (like the Karma webserver) because BrowserFS HTTP Backend uses HTTP HEAD requests to do fs.stat
size: Number(e.size) > -1 ? Number(e.size) % MAX_UINT32 : 0,
size: e.size > -1 ? e.size % MAX_UINT32 : 0,
}
}

0 comments on commit 81095c1

Please sign in to comment.