From 401ae386363b97fb820c15de5233633988f9ddd7 Mon Sep 17 00:00:00 2001 From: ZYSzys <17367077526@163.com> Date: Thu, 13 Sep 2018 12:24:58 +0800 Subject: [PATCH] lib: destructuring assignment `path` module in fs --- lib/fs.js | 106 ++++++++++++++++++------------------ lib/internal/fs/promises.js | 42 +++++++------- lib/internal/fs/utils.js | 10 ++-- 3 files changed, 79 insertions(+), 79 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 3302cfe0bf756a..278b66ce8d6e5a 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -40,7 +40,7 @@ const { } = constants; const { _extend } = require('util'); -const pathModule = require('path'); +const { _makeLong, toNamespacedPath, resolve } = require('path'); const { isArrayBufferView } = require('internal/util/types'); const binding = process.binding('fs'); const { Buffer, kMaxLength } = require('buffer'); @@ -183,7 +183,7 @@ function access(path, mode, callback) { mode = mode | 0; const req = new FSReqCallback(); req.oncomplete = makeCallback(callback); - binding.access(pathModule.toNamespacedPath(path), mode, req); + binding.access(toNamespacedPath(path), mode, req); } function accessSync(path, mode) { @@ -196,7 +196,7 @@ function accessSync(path, mode) { mode = mode | 0; const ctx = { path }; - binding.access(pathModule.toNamespacedPath(path), mode, undefined, ctx); + binding.access(toNamespacedPath(path), mode, undefined, ctx); handleErrorFromBinding(ctx); } @@ -299,7 +299,7 @@ function readFile(path, options, callback) { path = toPathIfFileURL(path); validatePath(path); - binding.open(pathModule.toNamespacedPath(path), + binding.open(toNamespacedPath(path), stringToFlags(options.flag || 'r'), 0o666, req); @@ -423,7 +423,7 @@ function open(path, flags, mode, callback) { const req = new FSReqCallback(); req.oncomplete = callback; - binding.open(pathModule.toNamespacedPath(path), + binding.open(toNamespacedPath(path), flagsNumber, mode, req); @@ -437,7 +437,7 @@ function openSync(path, flags, mode) { mode = validateMode(mode, 'mode', 0o666); const ctx = { path }; - const result = binding.open(pathModule.toNamespacedPath(path), + const result = binding.open(toNamespacedPath(path), flagsNumber, mode, undefined, ctx); handleErrorFromBinding(ctx); @@ -593,8 +593,8 @@ function rename(oldPath, newPath, callback) { validatePath(newPath, 'newPath'); const req = new FSReqCallback(); req.oncomplete = callback; - binding.rename(pathModule.toNamespacedPath(oldPath), - pathModule.toNamespacedPath(newPath), + binding.rename(toNamespacedPath(oldPath), + toNamespacedPath(newPath), req); } @@ -604,8 +604,8 @@ function renameSync(oldPath, newPath) { newPath = toPathIfFileURL(newPath); validatePath(newPath, 'newPath'); const ctx = { path: oldPath, dest: newPath }; - binding.rename(pathModule.toNamespacedPath(oldPath), - pathModule.toNamespacedPath(newPath), undefined, ctx); + binding.rename(toNamespacedPath(oldPath), + toNamespacedPath(newPath), undefined, ctx); handleErrorFromBinding(ctx); } @@ -684,14 +684,14 @@ function rmdir(path, callback) { validatePath(path); const req = new FSReqCallback(); req.oncomplete = callback; - binding.rmdir(pathModule.toNamespacedPath(path), req); + binding.rmdir(toNamespacedPath(path), req); } function rmdirSync(path) { path = toPathIfFileURL(path); validatePath(path); const ctx = { path }; - binding.rmdir(pathModule.toNamespacedPath(path), undefined, ctx); + binding.rmdir(toNamespacedPath(path), undefined, ctx); handleErrorFromBinding(ctx); } @@ -743,7 +743,7 @@ function mkdir(path, options, callback) { const req = new FSReqCallback(); req.oncomplete = callback; - binding.mkdir(pathModule.toNamespacedPath(path), + binding.mkdir(toNamespacedPath(path), validateMode(mode, 'mode', 0o777), recursive, req); } @@ -762,7 +762,7 @@ function mkdirSync(path, options) { throw new ERR_INVALID_ARG_TYPE('recursive', 'boolean', recursive); const ctx = { path }; - binding.mkdir(pathModule.toNamespacedPath(path), + binding.mkdir(toNamespacedPath(path), validateMode(mode, 'mode', 0o777), recursive, undefined, ctx); handleErrorFromBinding(ctx); @@ -786,7 +786,7 @@ function readdir(path, options, callback) { getDirents(path, result, callback); }; } - binding.readdir(pathModule.toNamespacedPath(path), options.encoding, + binding.readdir(toNamespacedPath(path), options.encoding, !!options.withFileTypes, req); } @@ -795,7 +795,7 @@ function readdirSync(path, options) { path = toPathIfFileURL(path); validatePath(path); const ctx = { path }; - const result = binding.readdir(pathModule.toNamespacedPath(path), + const result = binding.readdir(toNamespacedPath(path), options.encoding, !!options.withFileTypes, undefined, ctx); handleErrorFromBinding(ctx); @@ -823,7 +823,7 @@ function lstat(path, options, callback) { validatePath(path); const req = new FSReqCallback(options.bigint); req.oncomplete = callback; - binding.lstat(pathModule.toNamespacedPath(path), options.bigint, req); + binding.lstat(toNamespacedPath(path), options.bigint, req); } function stat(path, options, callback) { @@ -836,7 +836,7 @@ function stat(path, options, callback) { validatePath(path); const req = new FSReqCallback(options.bigint); req.oncomplete = callback; - binding.stat(pathModule.toNamespacedPath(path), options.bigint, req); + binding.stat(toNamespacedPath(path), options.bigint, req); } function fstatSync(fd, options = {}) { @@ -851,7 +851,7 @@ function lstatSync(path, options = {}) { path = toPathIfFileURL(path); validatePath(path); const ctx = { path }; - const stats = binding.lstat(pathModule.toNamespacedPath(path), + const stats = binding.lstat(toNamespacedPath(path), options.bigint, undefined, ctx); handleErrorFromBinding(ctx); return getStatsFromBinding(stats); @@ -861,7 +861,7 @@ function statSync(path, options = {}) { path = toPathIfFileURL(path); validatePath(path); const ctx = { path }; - const stats = binding.stat(pathModule.toNamespacedPath(path), + const stats = binding.stat(toNamespacedPath(path), options.bigint, undefined, ctx); handleErrorFromBinding(ctx); return getStatsFromBinding(stats); @@ -874,7 +874,7 @@ function readlink(path, options, callback) { validatePath(path, 'oldPath'); const req = new FSReqCallback(); req.oncomplete = callback; - binding.readlink(pathModule.toNamespacedPath(path), options.encoding, req); + binding.readlink(toNamespacedPath(path), options.encoding, req); } function readlinkSync(path, options) { @@ -882,7 +882,7 @@ function readlinkSync(path, options) { path = toPathIfFileURL(path); validatePath(path, 'oldPath'); const ctx = { path }; - const result = binding.readlink(pathModule.toNamespacedPath(path), + const result = binding.readlink(toNamespacedPath(path), options.encoding, undefined, ctx); handleErrorFromBinding(ctx); return result; @@ -902,7 +902,7 @@ function symlink(target, path, type_, callback_) { req.oncomplete = callback; binding.symlink(preprocessSymlinkDestination(target, type, path), - pathModule.toNamespacedPath(path), flags, req); + toNamespacedPath(path), flags, req); } function symlinkSync(target, path, type) { @@ -915,7 +915,7 @@ function symlinkSync(target, path, type) { const ctx = { path: target, dest: path }; binding.symlink(preprocessSymlinkDestination(target, type, path), - pathModule.toNamespacedPath(path), flags, undefined, ctx); + toNamespacedPath(path), flags, undefined, ctx); handleErrorFromBinding(ctx); } @@ -931,8 +931,8 @@ function link(existingPath, newPath, callback) { const req = new FSReqCallback(); req.oncomplete = callback; - binding.link(pathModule.toNamespacedPath(existingPath), - pathModule.toNamespacedPath(newPath), + binding.link(toNamespacedPath(existingPath), + toNamespacedPath(newPath), req); } @@ -943,8 +943,8 @@ function linkSync(existingPath, newPath) { validatePath(newPath, 'newPath'); const ctx = { path: existingPath, dest: newPath }; - const result = binding.link(pathModule.toNamespacedPath(existingPath), - pathModule.toNamespacedPath(newPath), + const result = binding.link(toNamespacedPath(existingPath), + toNamespacedPath(newPath), undefined, ctx); handleErrorFromBinding(ctx); return result; @@ -956,14 +956,14 @@ function unlink(path, callback) { validatePath(path); const req = new FSReqCallback(); req.oncomplete = callback; - binding.unlink(pathModule.toNamespacedPath(path), req); + binding.unlink(toNamespacedPath(path), req); } function unlinkSync(path) { path = toPathIfFileURL(path); validatePath(path); const ctx = { path }; - binding.unlink(pathModule.toNamespacedPath(path), undefined, ctx); + binding.unlink(toNamespacedPath(path), undefined, ctx); handleErrorFromBinding(ctx); } @@ -1025,7 +1025,7 @@ function chmod(path, mode, callback) { const req = new FSReqCallback(); req.oncomplete = callback; - binding.chmod(pathModule.toNamespacedPath(path), mode, req); + binding.chmod(toNamespacedPath(path), mode, req); } function chmodSync(path, mode) { @@ -1034,7 +1034,7 @@ function chmodSync(path, mode) { mode = validateMode(mode, 'mode'); const ctx = { path }; - binding.chmod(pathModule.toNamespacedPath(path), mode, undefined, ctx); + binding.chmod(toNamespacedPath(path), mode, undefined, ctx); handleErrorFromBinding(ctx); } @@ -1046,7 +1046,7 @@ function lchown(path, uid, gid, callback) { validateUint32(gid, 'gid'); const req = new FSReqCallback(); req.oncomplete = callback; - binding.lchown(pathModule.toNamespacedPath(path), uid, gid, req); + binding.lchown(toNamespacedPath(path), uid, gid, req); } function lchownSync(path, uid, gid) { @@ -1055,7 +1055,7 @@ function lchownSync(path, uid, gid) { validateUint32(uid, 'uid'); validateUint32(gid, 'gid'); const ctx = { path }; - binding.lchown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx); + binding.lchown(toNamespacedPath(path), uid, gid, undefined, ctx); handleErrorFromBinding(ctx); } @@ -1088,7 +1088,7 @@ function chown(path, uid, gid, callback) { const req = new FSReqCallback(); req.oncomplete = callback; - binding.chown(pathModule.toNamespacedPath(path), uid, gid, req); + binding.chown(toNamespacedPath(path), uid, gid, req); } function chownSync(path, uid, gid) { @@ -1097,7 +1097,7 @@ function chownSync(path, uid, gid) { validateUint32(uid, 'uid'); validateUint32(gid, 'gid'); const ctx = { path }; - binding.chown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx); + binding.chown(toNamespacedPath(path), uid, gid, undefined, ctx); handleErrorFromBinding(ctx); } @@ -1108,7 +1108,7 @@ function utimes(path, atime, mtime, callback) { const req = new FSReqCallback(); req.oncomplete = callback; - binding.utimes(pathModule.toNamespacedPath(path), + binding.utimes(toNamespacedPath(path), toUnixTimestamp(atime), toUnixTimestamp(mtime), req); @@ -1118,7 +1118,7 @@ function utimesSync(path, atime, mtime) { path = toPathIfFileURL(path); validatePath(path); const ctx = { path }; - binding.utimes(pathModule.toNamespacedPath(path), + binding.utimes(toNamespacedPath(path), toUnixTimestamp(atime), toUnixTimestamp(mtime), undefined, ctx); handleErrorFromBinding(ctx); @@ -1284,7 +1284,7 @@ const statWatchers = new Map(); function watchFile(filename, options, listener) { filename = toPathIfFileURL(filename); validatePath(filename); - filename = pathModule.resolve(filename); + filename = resolve(filename); let stat; const defaults = { @@ -1323,7 +1323,7 @@ function watchFile(filename, options, listener) { function unwatchFile(filename, listener) { filename = toPathIfFileURL(filename); validatePath(filename); - filename = pathModule.resolve(filename); + filename = resolve(filename); const stat = statWatchers.get(filename); if (stat === undefined) return; @@ -1398,7 +1398,7 @@ function realpathSync(p, options) { p += ''; } validatePath(p); - p = pathModule.resolve(p); + p = resolve(p); const cache = options[realpathCacheKey]; const maybeCachedResult = cache && cache.get(p); @@ -1426,7 +1426,7 @@ function realpathSync(p, options) { // On windows, check that the root exists. On unix there is no need. if (isWindows && !knownHard[base]) { const ctx = { path: base }; - binding.lstat(pathModule.toNamespacedPath(base), false, undefined, ctx); + binding.lstat(toNamespacedPath(base), false, undefined, ctx); handleErrorFromBinding(ctx); knownHard[base] = true; } @@ -1466,7 +1466,7 @@ function realpathSync(p, options) { // Use stats array directly to avoid creating an fs.Stats instance just // for our internal use. - const baseLong = pathModule.toNamespacedPath(base); + const baseLong = toNamespacedPath(base); const ctx = { path: base }; const stats = binding.lstat(baseLong, false, undefined, ctx); handleErrorFromBinding(ctx); @@ -1496,14 +1496,14 @@ function realpathSync(p, options) { linkTarget = binding.readlink(baseLong, undefined, undefined, ctx); handleErrorFromBinding(ctx); } - resolvedLink = pathModule.resolve(previous, linkTarget); + resolvedLink = resolve(previous, linkTarget); if (cache) cache.set(base, resolvedLink); if (!isWindows) seenLinks[id] = linkTarget; } // resolve the link, then start over - p = pathModule.resolve(resolvedLink, p.slice(pos)); + p = resolve(resolvedLink, p.slice(pos)); // Skip over roots current = base = splitRoot(p); @@ -1512,7 +1512,7 @@ function realpathSync(p, options) { // On windows, check that the root exists. On unix there is no need. if (isWindows && !knownHard[base]) { const ctx = { path: base }; - binding.lstat(pathModule.toNamespacedPath(base), false, undefined, ctx); + binding.lstat(toNamespacedPath(base), false, undefined, ctx); handleErrorFromBinding(ctx); knownHard[base] = true; } @@ -1545,7 +1545,7 @@ function realpath(p, options, callback) { p += ''; } validatePath(p); - p = pathModule.resolve(p); + p = resolve(p); const seenLinks = Object.create(null); const knownHard = Object.create(null); @@ -1641,12 +1641,12 @@ function realpath(p, options, callback) { function gotTarget(err, target, base) { if (err) return callback(err); - gotResolvedLink(pathModule.resolve(previous, target)); + gotResolvedLink(resolve(previous, target)); } function gotResolvedLink(resolvedLink) { // resolve the link, then start over - p = pathModule.resolve(resolvedLink, p.slice(pos)); + p = resolve(resolvedLink, p.slice(pos)); current = base = splitRoot(p); pos = current.length; @@ -1715,8 +1715,8 @@ function copyFile(src, dest, flags, callback) { validatePath(src, 'src'); validatePath(dest, 'dest'); - src = pathModule._makeLong(src); - dest = pathModule._makeLong(dest); + src = _makeLong(src); + dest = _makeLong(dest); flags = flags | 0; const req = new FSReqCallback(); req.oncomplete = makeCallback(callback); @@ -1732,8 +1732,8 @@ function copyFileSync(src, dest, flags) { const ctx = { path: src, dest }; // non-prefixed - src = pathModule._makeLong(src); - dest = pathModule._makeLong(dest); + src = _makeLong(src); + dest = _makeLong(dest); flags = flags | 0; binding.copyFile(src, dest, flags, undefined, ctx); handleErrorFromBinding(ctx); diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 0aa26ba2f97daa..4eaddcff7f2b56 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -37,7 +37,7 @@ const { validateInteger, validateUint32 } = require('internal/validators'); -const pathModule = require('path'); +const { toNamespacedPath } = require('path'); const { promisify } = require('internal/util'); const kHandle = Symbol('handle'); @@ -176,7 +176,7 @@ async function access(path, mode = F_OK) { validatePath(path); mode = mode | 0; - return binding.access(pathModule.toNamespacedPath(path), mode, + return binding.access(toNamespacedPath(path), mode, kUsePromises); } @@ -186,8 +186,8 @@ async function copyFile(src, dest, flags) { validatePath(src, 'src'); validatePath(dest, 'dest'); flags = flags | 0; - return binding.copyFile(pathModule.toNamespacedPath(src), - pathModule.toNamespacedPath(dest), + return binding.copyFile(toNamespacedPath(src), + toNamespacedPath(dest), flags, kUsePromises); } @@ -198,7 +198,7 @@ async function open(path, flags, mode) { validatePath(path); mode = validateMode(mode, 'mode', 0o666); return new FileHandle( - await binding.openFileHandle(pathModule.toNamespacedPath(path), + await binding.openFileHandle(toNamespacedPath(path), stringToFlags(flags), mode, kUsePromises)); } @@ -261,8 +261,8 @@ async function rename(oldPath, newPath) { newPath = toPathIfFileURL(newPath); validatePath(oldPath, 'oldPath'); validatePath(newPath, 'newPath'); - return binding.rename(pathModule.toNamespacedPath(oldPath), - pathModule.toNamespacedPath(newPath), + return binding.rename(toNamespacedPath(oldPath), + toNamespacedPath(newPath), kUsePromises); } @@ -280,7 +280,7 @@ async function ftruncate(handle, len = 0) { async function rmdir(path) { path = toPathIfFileURL(path); validatePath(path); - return binding.rmdir(pathModule.toNamespacedPath(path), kUsePromises); + return binding.rmdir(toNamespacedPath(path), kUsePromises); } async function fdatasync(handle) { @@ -307,7 +307,7 @@ async function mkdir(path, options) { if (typeof recursive !== 'boolean') throw new ERR_INVALID_ARG_TYPE('recursive', 'boolean', recursive); - return binding.mkdir(pathModule.toNamespacedPath(path), + return binding.mkdir(toNamespacedPath(path), validateMode(mode, 'mode', 0o777), recursive, kUsePromises); } @@ -316,7 +316,7 @@ async function readdir(path, options) { options = getOptions(options, {}); path = toPathIfFileURL(path); validatePath(path); - const result = await binding.readdir(pathModule.toNamespacedPath(path), + const result = await binding.readdir(toNamespacedPath(path), options.encoding, !!options.withTypes, kUsePromises); return options.withFileTypes ? @@ -328,7 +328,7 @@ async function readlink(path, options) { options = getOptions(options, {}); path = toPathIfFileURL(path); validatePath(path, 'oldPath'); - return binding.readlink(pathModule.toNamespacedPath(path), + return binding.readlink(toNamespacedPath(path), options.encoding, kUsePromises); } @@ -339,7 +339,7 @@ async function symlink(target, path, type_) { validatePath(target, 'target'); validatePath(path); return binding.symlink(preprocessSymlinkDestination(target, type, path), - pathModule.toNamespacedPath(path), + toNamespacedPath(path), stringToSymlinkType(type), kUsePromises); } @@ -353,7 +353,7 @@ async function fstat(handle, options = { bigint: false }) { async function lstat(path, options = { bigint: false }) { path = toPathIfFileURL(path); validatePath(path); - const result = await binding.lstat(pathModule.toNamespacedPath(path), + const result = await binding.lstat(toNamespacedPath(path), options.bigint, kUsePromises); return getStatsFromBinding(result); } @@ -361,7 +361,7 @@ async function lstat(path, options = { bigint: false }) { async function stat(path, options = { bigint: false }) { path = toPathIfFileURL(path); validatePath(path); - const result = await binding.stat(pathModule.toNamespacedPath(path), + const result = await binding.stat(toNamespacedPath(path), options.bigint, kUsePromises); return getStatsFromBinding(result); } @@ -371,15 +371,15 @@ async function link(existingPath, newPath) { newPath = toPathIfFileURL(newPath); validatePath(existingPath, 'existingPath'); validatePath(newPath, 'newPath'); - return binding.link(pathModule.toNamespacedPath(existingPath), - pathModule.toNamespacedPath(newPath), + return binding.link(toNamespacedPath(existingPath), + toNamespacedPath(newPath), kUsePromises); } async function unlink(path) { path = toPathIfFileURL(path); validatePath(path); - return binding.unlink(pathModule.toNamespacedPath(path), kUsePromises); + return binding.unlink(toNamespacedPath(path), kUsePromises); } async function fchmod(handle, mode) { @@ -392,7 +392,7 @@ async function chmod(path, mode) { path = toPathIfFileURL(path); validatePath(path); mode = validateMode(mode, 'mode'); - return binding.chmod(pathModule.toNamespacedPath(path), mode, kUsePromises); + return binding.chmod(toNamespacedPath(path), mode, kUsePromises); } async function lchmod(path, mode) { @@ -408,7 +408,7 @@ async function lchown(path, uid, gid) { validatePath(path); validateUint32(uid, 'uid'); validateUint32(gid, 'gid'); - return binding.lchown(pathModule.toNamespacedPath(path), + return binding.lchown(toNamespacedPath(path), uid, gid, kUsePromises); } @@ -424,14 +424,14 @@ async function chown(path, uid, gid) { validatePath(path); validateUint32(uid, 'uid'); validateUint32(gid, 'gid'); - return binding.chown(pathModule.toNamespacedPath(path), + return binding.chown(toNamespacedPath(path), uid, gid, kUsePromises); } async function utimes(path, atime, mtime) { path = toPathIfFileURL(path); validatePath(path); - return binding.utimes(pathModule.toNamespacedPath(path), + return binding.utimes(toNamespacedPath(path), toUnixTimestamp(atime), toUnixTimestamp(mtime), kUsePromises); diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index acc566e37bb4bf..1d7f9dfd7e5fb3 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -10,7 +10,7 @@ const { ERR_OUT_OF_RANGE } = require('internal/errors').codes; const { isUint8Array, isArrayBufferView } = require('internal/util/types'); -const pathModule = require('path'); +const { resolve, toNamespacedPath } = require('path'); const util = require('util'); const kType = Symbol('type'); const kStats = Symbol('stats'); @@ -129,7 +129,7 @@ function getDirents(path, [names, types], callback) { const name = names[i]; const idx = i; toFinish++; - lazyLoadFs().stat(pathModule.resolve(path, name), (err, stats) => { + lazyLoadFs().stat(resolve(path, name), (err, stats) => { if (err) { callback(err); return; @@ -152,7 +152,7 @@ function getDirents(path, [names, types], callback) { const type = types[i]; if (type === UV_DIRENT_UNKNOWN) { const name = names[i]; - const stats = lazyLoadFs().statSync(pathModule.resolve(path, name)); + const stats = lazyLoadFs().statSync(resolve(path, name)); names[i] = new DirentFromStats(name, stats); } else { names[i] = new Dirent(names[i], types[i]); @@ -218,8 +218,8 @@ function preprocessSymlinkDestination(path, type, linkPath) { } else if (type === 'junction') { // Junctions paths need to be absolute and \\?\-prefixed. // A relative target is relative to the link's parent directory. - path = pathModule.resolve(linkPath, '..', path); - return pathModule.toNamespacedPath(path); + path = resolve(linkPath, '..', path); + return toNamespacedPath(path); } else { // Windows symlinks don't tolerate forward slashes. return ('' + path).replace(/\//g, '\\');