Skip to content

Commit

Permalink
chore: optimize BigInt references
Browse files Browse the repository at this point in the history
  • Loading branch information
jayree committed Jun 29, 2023
1 parent f9a29ed commit 75e8760
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
14 changes: 9 additions & 5 deletions src/models/FileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,15 @@ export class FileSystem {
try {
const stats = await this._lstat(filename)
// For non-browser (local) scenarios regular git 'add' command might be used to write the index. Therefore we need to have the exact nanoseconds (see. normalizeStats.js SecondsNanoseconds ).
try {
const statsNs = await fs.promises.lstat(filename, { bigint: true })
stats.mtimeNs = statsNs.mtimeNs
stats.ctimeNs = statsNs.ctimeNs
} catch (error) {}
if (!process.browser) {
try {
const statsNs = await fs.promises.lstat(filename, { bigint: true })
stats.ctimeSeconds = Number(statsNs.ctimeNs / BigInt(1e9))
stats.ctimeNanoseconds = Number(statsNs.ctimeNs % BigInt(1e9))
stats.mtimeSeconds = Number(statsNs.mtimeNs / BigInt(1e9))
stats.mtimeNanoseconds = Number(statsNs.mtimeNs % BigInt(1e9))
} catch (error) {}
}
return stats
} catch (err) {
if (err.code === 'ENOENT') {
Expand Down
18 changes: 4 additions & 14 deletions src/utils/normalizeStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,17 @@ const MAX_UINT32 = 2 ** 32
function SecondsNanoseconds(
givenSeconds,
givenNanoseconds,
nanoseconds,
milliseconds,
date
) {
// For non-browser (local) scenarios conversions happens in FileSystem
if (givenSeconds !== undefined && givenNanoseconds !== undefined) {
return [givenSeconds, givenNanoseconds]
}
let seconds
// For browser scenarios isomorphic-git 'add' will be used to write the index. Reading and writing are handled using normalizeStats ( see FileSystem.js lstat() ).
if (nanoseconds === undefined) {
milliseconds = milliseconds || date.valueOf()
seconds = Math.trunc(milliseconds / 1000)
nanoseconds = (milliseconds - seconds * 1000) * 1000000 // nanoseconds with millisecond precision
}
// For non-browser (local) scenarios
else {
seconds = Number(nanoseconds / BigInt(1e9))
nanoseconds = Number(nanoseconds % BigInt(1e9))
}
milliseconds = milliseconds || date.valueOf()
const seconds = Math.trunc(milliseconds / 1000)
const nanoseconds = (milliseconds - seconds * 1000) * 1000000 // nanoseconds with millisecond precision

return [seconds, nanoseconds]
}
Expand All @@ -32,14 +24,12 @@ export function normalizeStats(e) {
const [ctimeSeconds, ctimeNanoseconds] = SecondsNanoseconds(
e.ctimeSeconds,
e.ctimeNanoseconds,
e.ctimeNs,
e.ctimeMs,
e.ctime
)
const [mtimeSeconds, mtimeNanoseconds] = SecondsNanoseconds(
e.mtimeSeconds,
e.mtimeNanoseconds,
e.mtimeNs,
e.mtimeMs,
e.mtime
)
Expand Down

0 comments on commit 75e8760

Please sign in to comment.