From 75e876070c932f29ca1d652581c3bede786cbdb9 Mon Sep 17 00:00:00 2001 From: jayree Date: Tue, 27 Jun 2023 20:58:23 +0200 Subject: [PATCH] chore: optimize BigInt references --- src/models/FileSystem.js | 14 +++++++++----- src/utils/normalizeStats.js | 18 ++++-------------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/models/FileSystem.js b/src/models/FileSystem.js index 95867c905..2a23e89af 100644 --- a/src/models/FileSystem.js +++ b/src/models/FileSystem.js @@ -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') { diff --git a/src/utils/normalizeStats.js b/src/utils/normalizeStats.js index 815a5b394..a3f57b120 100644 --- a/src/utils/normalizeStats.js +++ b/src/utils/normalizeStats.js @@ -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] } @@ -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 )