| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1683,6 +1683,9 @@ changes: | |||
| 1683 | 1683 | * `options` {Object} | |
| 1684 | 1684 | * `bigint` {boolean} Whether the numeric values in the returned | |
| 1685 | 1685 | {fs.Stats} object should be `bigint`. **Default:** `false`. | |
| 1686 | + * `throwIfNoEntry` {boolean} Whether an exception will be thrown | ||
| 1687 | + if no file system entry exists, rather than returning `undefined`. | ||
| 1688 | + **Default:** `true`. | ||
| 1686 | 1689 | * Returns: {Promise} Fulfills with the {fs.Stats} object for the | |
| 1687 | 1690 | given `path`. | |
| 1688 | 1691 | ||
@@ -4393,6 +4396,9 @@ changes: | |||
| 4393 | 4396 | * `options` {Object} | |
| 4394 | 4397 | * `bigint` {boolean} Whether the numeric values in the returned | |
| 4395 | 4398 | {fs.Stats} object should be `bigint`. **Default:** `false`. | |
| 4399 | + * `throwIfNoEntry` {boolean} Whether an exception will be thrown | ||
| 4400 | + if no file system entry exists, rather than returning `undefined`. | ||
| 4401 | + **Default:** `true`. | ||
| 4396 | 4402 | * `callback` {Function} | |
| 4397 | 4403 | * `err` {Error} | |
| 4398 | 4404 | * `stats` {fs.Stats} | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -194,6 +194,7 @@ function makeStatsCallback(cb) { | |||
| 194 | 194 | ||
| 195 | 195 | return (err, stats) => { | |
| 196 | 196 | if (err) return cb(err); | |
| 197 | + if (stats === undefined && err === null) return cb(null, undefined); | ||
| 197 | 198 | cb(err, getStatsFromBinding(stats)); | |
| 198 | 199 | }; | |
| 199 | 200 | } | |
@@ -1658,7 +1659,7 @@ function lstat(path, options = { bigint: false }, callback) { | |||
| 1658 | 1659 | * ) => any} callback | |
| 1659 | 1660 | * @returns {void} | |
| 1660 | 1661 | */ | |
| 1661 | - function stat(path, options = { bigint: false }, callback) { | ||
| 1662 | + function stat(path, options = { bigint: false, throwIfNoEntry: true }, callback) { | ||
| 1662 | 1663 | if (typeof options === 'function') { | |
| 1663 | 1664 | callback = options; | |
| 1664 | 1665 | options = kEmptyObject; | |
@@ -1667,7 +1668,7 @@ function stat(path, options = { bigint: false }, callback) { | |||
| 1667 | 1668 | ||
| 1668 | 1669 | const req = new FSReqCallback(options.bigint); | |
| 1669 | 1670 | req.oncomplete = callback; | |
| 1670 | - binding.stat(getValidatedPath(path), options.bigint, req); | ||
| 1671 | + binding.stat(getValidatedPath(path), options.bigint, req, options.throwIfNoEntry); | ||
| 1671 | 1672 | } | |
| 1672 | 1673 | ||
| 1673 | 1674 | function statfs(path, options = { bigint: false }, callback) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1033,12 +1033,16 @@ async function lstat(path, options = { bigint: false }) { | |||
| 1033 | 1033 | return getStatsFromBinding(result); | |
| 1034 | 1034 | } | |
| 1035 | 1035 | ||
| 1036 | - async function stat(path, options = { bigint: false }) { | ||
| 1036 | + async function stat(path, options = { bigint: false, throwIfNoEntry: true }) { | ||
| 1037 | 1037 | const result = await PromisePrototypeThen( | |
| 1038 | - binding.stat(getValidatedPath(path), options.bigint, kUsePromises), | ||
| 1038 | + binding.stat(getValidatedPath(path), options.bigint, kUsePromises, options.throwIfNoEntry), | ||
| 1039 | 1039 | undefined, | |
| 1040 | 1040 | handleErrorFromBinding, | |
| 1041 | 1041 | ); | |
| 1042 | + | ||
| 1043 | + // Binding will resolve undefined if UV_ENOENT or UV_ENOTDIR and throwIfNoEntry is false | ||
| 1044 | + if (!options.throwIfNoEntry && result === undefined) return undefined; | ||
| 1045 | + | ||
| 1042 | 1046 | return getStatsFromBinding(result); | |
| 1043 | 1047 | } | |
| 1044 | 1048 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -794,6 +794,22 @@ void AfterStat(uv_fs_t* req) { | |||
| 794 | 794 | } | |
| 795 | 795 | } | |
| 796 | 796 | ||
| 797 | + void AfterStatNoThrowIfNoEntry(uv_fs_t* req) { | ||
| 798 | + FSReqBase* req_wrap = FSReqBase::from_req(req); | ||
| 799 | + FSReqAfterScope after(req_wrap, req); | ||
| 800 | + | ||
| 801 | + FS_ASYNC_TRACE_END1( | ||
| 802 | + req->fs_type, req_wrap, "result", static_cast<int>(req->result)) | ||
| 803 | + if (req->result == UV_ENOENT || req->result == UV_ENOTDIR) { | ||
| 804 | + req_wrap->Resolve(Undefined(req_wrap->env()->isolate())); | ||
| 805 | + return; | ||
| 806 | + } | ||
| 807 | + | ||
| 808 | + if (after.Proceed()) { | ||
| 809 | + req_wrap->ResolveStat(&req->statbuf); | ||
| 810 | + } | ||
| 811 | + } | ||
| 812 | + | ||
| 797 | 813 | void AfterStatFs(uv_fs_t* req) { | |
| 798 | 814 | FSReqBase* req_wrap = FSReqBase::from_req(req); | |
| 799 | 815 | FSReqAfterScope after(req_wrap, req); | |
@@ -1087,7 +1103,9 @@ static void Stat(const FunctionCallbackInfo<Value>& args) { | |||
| 1087 | 1103 | ToNamespacedPath(env, &path); | |
| 1088 | 1104 | ||
| 1089 | 1105 | bool use_bigint = args[1]->IsTrue(); | |
| 1090 | - if (!args[2]->IsUndefined()) { // stat(path, use_bigint, req) | ||
| 1106 | + if (!args[2]->IsUndefined()) { // stat(path, use_bigint, req, | ||
| 1107 | + // do_not_throw_if_no_entry) | ||
| 1108 | + bool do_not_throw_if_no_entry = args[3]->IsFalse(); | ||
| 1091 | 1109 | FSReqBase* req_wrap_async = GetReqWrap(args, 2, use_bigint); | |
| 1092 | 1110 | CHECK_NOT_NULL(req_wrap_async); | |
| 1093 | 1111 | ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS( | |
@@ -1097,8 +1115,25 @@ static void Stat(const FunctionCallbackInfo<Value>& args) { | |||
| 1097 | 1115 | path.ToStringView()); | |
| 1098 | 1116 | FS_ASYNC_TRACE_BEGIN1( | |
| 1099 | 1117 | UV_FS_STAT, req_wrap_async, "path", TRACE_STR_COPY(*path)) | |
| 1100 | - AsyncCall(env, req_wrap_async, args, "stat", UTF8, AfterStat, | ||
| 1101 | - uv_fs_stat, *path); | ||
| 1118 | + if (do_not_throw_if_no_entry) { | ||
| 1119 | + AsyncCall(env, | ||
| 1120 | + req_wrap_async, | ||
| 1121 | + args, | ||
| 1122 | + "stat", | ||
| 1123 | + UTF8, | ||
| 1124 | + AfterStatNoThrowIfNoEntry, | ||
| 1125 | + uv_fs_stat, | ||
| 1126 | + *path); | ||
| 1127 | + } else { | ||
| 1128 | + AsyncCall(env, | ||
| 1129 | + req_wrap_async, | ||
| 1130 | + args, | ||
| 1131 | + "stat", | ||
| 1132 | + UTF8, | ||
| 1133 | + AfterStat, | ||
| 1134 | + uv_fs_stat, | ||
| 1135 | + *path); | ||
| 1136 | + } | ||
| 1102 | 1137 | } else { // stat(path, use_bigint, undefined, do_not_throw_if_no_entry) | |
| 1103 | 1138 | THROW_IF_INSUFFICIENT_PERMISSIONS( | |
| 1104 | 1139 | env, permission::PermissionScope::kFileSystemRead, path.ToStringView()); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -152,6 +152,12 @@ async function executeOnHandle(dest, func) { | |||
| 152 | 152 | }); | |
| 153 | 153 | } | |
| 154 | 154 | ||
| 155 | + // File stats throwIfNoEntry: false | ||
| 156 | + { | ||
| 157 | + const stats = await stat('meow.js', { throwIfNoEntry: false }); | ||
| 158 | + assert.strictEqual(stats, undefined); | ||
| 159 | + } | ||
| 160 | + | ||
| 155 | 161 | // File system stats | |
| 156 | 162 | { | |
| 157 | 163 | const statFs = await statfs(dest); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -224,5 +224,9 @@ fs.lstat(__filename, undefined, common.mustCall()); | |||
| 224 | 224 | ||
| 225 | 225 | { | |
| 226 | 226 | // Test that the throwIfNoEntry option works and returns undefined | |
| 227 | - assert.ok(!(fs.statSync('./wont_exists', { throwIfNoEntry: false }))); | ||
| 227 | + const opts = { throwIfNoEntry: false }; | ||
| 228 | + assert.ok(!(fs.statSync('./wont_exists', opts))); | ||
| 229 | + fs.stat('./wont_exists', opts, common.mustSucceed((err, stats) => { | ||
| 230 | + assert.strictEqual(stats, undefined); | ||
| 231 | + })); | ||
| 228 | 232 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments