From 0732751ea3dc688dd0e7517b66a853f55e3d80d2 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 5 Dec 2024 12:10:35 -0800 Subject: [PATCH] Optimize library_nodepath.js. NFC (#23086) --- src/closure-externs/node-externs.js | 12 ++++++++++++ src/library_nodepath.js | 20 ++++++++++---------- src/library_noderawfs.js | 2 +- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/closure-externs/node-externs.js b/src/closure-externs/node-externs.js index ce995014c3179..b7a0e6049fa9f 100644 --- a/src/closure-externs/node-externs.js +++ b/src/closure-externs/node-externs.js @@ -119,3 +119,15 @@ fs.Stats.prototype.mtimeMs; * @type {number} */ fs.Stats.prototype.ctimeMs; + +/** + * @param {string} p + * @return {boolean} + * @nosideeffects + */ +path.isAbsolute; + +/** + * @type {Object.} + */ +path.posix; diff --git a/src/library_nodepath.js b/src/library_nodepath.js index c42820f36e289..09d4c48a4662a 100644 --- a/src/library_nodepath.js +++ b/src/library_nodepath.js @@ -12,14 +12,14 @@ // operations. Hence, using `nodePath` should be safe here. addToLibrary({ - $PATH: { - isAbs: (path) => nodePath['isAbsolute'](path), - normalize: (path) => nodePath['normalize'](path), - dirname: (path) => nodePath['dirname'](path), - basename: (path) => nodePath['basename'](path), - join: (...args) => nodePath['join'](...args), - join2: (l, r) => nodePath['join'](l, r), - }, + $PATH: `{ + isAbs: nodePath.isAbsolute, + normalize: nodePath.normalize, + dirname: nodePath.dirname, + basename: nodePath.basename, + join: nodePath.join, + join2: nodePath.join, + }`, // The FS-using parts are split out into a separate object, so simple path // usage does not require the FS. $PATH_FS__deps: ['$FS'], @@ -27,8 +27,8 @@ addToLibrary({ $PATH_FS: { resolve: (...paths) => { paths.unshift(FS.cwd()); - return nodePath['posix']['resolve'](...paths); + return nodePath.posix.resolve(...paths); }, - relative: (from, to) => nodePath['posix']['relative'](from || FS.cwd(), to || FS.cwd()), + relative: (from, to) => nodePath.posix.relative(from || FS.cwd(), to || FS.cwd()), } }); diff --git a/src/library_noderawfs.js b/src/library_noderawfs.js index 6c622d15078e5..81ab16962a89c 100644 --- a/src/library_noderawfs.js +++ b/src/library_noderawfs.js @@ -40,7 +40,7 @@ addToLibrary({ }, lookupPath(path, opts = {}) { if (opts.parent) { - path = nodePath.dirname(path); + path = PATH.dirname(path); } var st = fs.lstatSync(path); var mode = NODEFS.getMode(path);