Skip to content

Commit ee73112

Browse files
fix(p2-shim): incorrect ns magnitude
1 parent c9eb516 commit ee73112

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

packages/preview2-shim/lib/nodejs/filesystem.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const symbolDispose = Symbol.dispose || Symbol.for("dispose");
3939
const isWindows = platform === "win32";
4040
const isMac = platform === "darwin";
4141

42-
const nsMagnitude = 1_000_000_000_000n;
42+
const nsMagnitude = 1_000_000_000n;
4343
function nsToDateTime(ns) {
4444
const seconds = ns / nsMagnitude;
4545
const nanoseconds = Number(ns % nsMagnitude);
@@ -328,8 +328,10 @@ class Descriptor {
328328
if (!pathFlags.symlinkFollow && !lutimesSync) {
329329
throw new Error("Changing the timestamps of symlinks isn't supported");
330330
}
331+
332+
let metadataSetFn = pathFlags.symlinkFollow ? lutimesSync : utimesSync;
331333
try {
332-
(pathFlags.symlinkFollow ? utimesSync : lutimesSync)(fullPath, atime, mtime);
334+
metadataSetFn(fullPath, atime / 1000, mtime / 1000);
333335
} catch (e) {
334336
throw convertFsError(e);
335337
}
@@ -814,5 +816,14 @@ function convertFsError(e) {
814816
}
815817

816818
function timestampToMs(timestamp) {
817-
return Number(timestamp.seconds) * 1000 + timestamp.nanoseconds / 1e9;
819+
let secondsInMillis = timestamp.seconds * 1000n;
820+
if (secondsInMillis > Number.MAX_SAFE_INTEGER) {
821+
throw new Error(`cannot represent millisecond amount as Number [${{ nanosInMillis }}]`);
822+
}
823+
if (timestamp.nanoseconds > Number.MAX_SAFE_INTEGER) {
824+
throw new Error(`cannot represent millisecond amount as Number [${{ nanosInMillis }}]`);
825+
}
826+
let remainderFractionalMillis = timestamp.nanoseconds / 1_000_000;
827+
const res = Number(secondsInMillis) + remainderFractionalMillis;
828+
return res;
818829
}

0 commit comments

Comments
 (0)