Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion deps/v8/src/strings/unicode-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ bool Utf8::IsValidCharacter(uchar c) {
c != kBadChar);
}

template <>
bool Utf8::IsAsciiOneByteString<uint8_t>(const uint8_t* buffer, size_t size) {
return simdutf::validate_ascii(reinterpret_cast<const char*>(buffer), size);
}

template <>
bool Utf8::IsAsciiOneByteString<uint16_t>(const uint16_t* buffer, size_t size) {
return false;
}

template <typename Char>
Utf8::EncodingResult Utf8::Encode(v8::base::Vector<const Char> string,
char* buffer, size_t capacity,
Expand All @@ -221,8 +231,17 @@ Utf8::EncodingResult Utf8::Encode(v8::base::Vector<const Char> string,
const Char* characters = string.begin();
size_t content_capacity = capacity - write_null;
CHECK_LE(content_capacity, capacity);
uint16_t last = Utf16::kNoPreviousCharacter;
size_t read_index = 0;
if (kSourceIsOneByte) {
size_t writeable = std::min(string.size(), content_capacity);
// Just memcpy when possible.
if (writeable > 0 && Utf8::IsAsciiOneByteString(characters, writeable)) {
memcpy(buffer, characters, writeable);
read_index = writeable;
write_index = writeable;
}
}
uint16_t last = Utf16::kNoPreviousCharacter;
for (; read_index < string.size(); read_index++) {
Char character = characters[read_index];

Expand Down
11 changes: 11 additions & 0 deletions deps/v8/src/strings/unicode.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ class V8_EXPORT_PRIVATE Utf8 {
// - valid code point range.
static bool ValidateEncoding(const uint8_t* str, size_t length);

template <typename Char>
static bool IsAsciiOneByteString(const Char* buffer, size_t size);

// Encode the given characters as Utf8 into the provided output buffer.
struct EncodingResult {
size_t bytes_written;
Expand All @@ -223,6 +226,14 @@ class V8_EXPORT_PRIVATE Utf8 {
bool replace_invalid_utf8);
};

template <>
inline bool Utf8::IsAsciiOneByteString<uint8_t>(const uint8_t* buffer,
size_t size);

template <>
inline bool Utf8::IsAsciiOneByteString<uint16_t>(const uint16_t* buffer,
size_t size);

#if V8_ENABLE_WEBASSEMBLY
class V8_EXPORT_PRIVATE Wtf8 {
public:
Expand Down
6 changes: 6 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,9 @@ changes:
* `options` {Object}
* `bigint` {boolean} Whether the numeric values in the returned
{fs.Stats} object should be `bigint`. **Default:** `false`.
* `throwIfNoEntry` {boolean} Whether an exception will be thrown
if no file system entry exists, rather than returning `undefined`.
**Default:** `true`.
* Returns: {Promise} Fulfills with the {fs.Stats} object for the
given `path`.

Expand Down Expand Up @@ -4436,6 +4439,9 @@ changes:
* `options` {Object}
* `bigint` {boolean} Whether the numeric values in the returned
{fs.Stats} object should be `bigint`. **Default:** `false`.
* `throwIfNoEntry` {boolean} Whether an exception will be thrown
if no file system entry exists, rather than returning `undefined`.
**Default:** `true`.
* `callback` {Function}
* `err` {Error}
* `stats` {fs.Stats}
Expand Down
5 changes: 3 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ function makeStatsCallback(cb) {

return (err, stats) => {
if (err) return cb(err);
if (stats === undefined && err === null) return cb(null, undefined);
cb(err, getStatsFromBinding(stats));
};
}
Expand Down Expand Up @@ -1621,7 +1622,7 @@ function lstat(path, options = { bigint: false }, callback) {
* ) => any} callback
* @returns {void}
*/
function stat(path, options = { bigint: false }, callback) {
function stat(path, options = { bigint: false, throwIfNoEntry: true }, callback) {
if (typeof options === 'function') {
callback = options;
options = kEmptyObject;
Expand All @@ -1630,7 +1631,7 @@ function stat(path, options = { bigint: false }, callback) {

const req = new FSReqCallback(options.bigint);
req.oncomplete = callback;
binding.stat(getValidatedPath(path), options.bigint, req);
binding.stat(getValidatedPath(path), options.bigint, req, options.throwIfNoEntry);
}

function statfs(path, options = { bigint: false }, callback) {
Expand Down
8 changes: 6 additions & 2 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -1026,12 +1026,16 @@ async function lstat(path, options = { bigint: false }) {
return getStatsFromBinding(result);
}

async function stat(path, options = { bigint: false }) {
async function stat(path, options = { bigint: false, throwIfNoEntry: true }) {
const result = await PromisePrototypeThen(
binding.stat(getValidatedPath(path), options.bigint, kUsePromises),
binding.stat(getValidatedPath(path), options.bigint, kUsePromises, options.throwIfNoEntry),
undefined,
handleErrorFromBinding,
);

// Binding will resolve undefined if UV_ENOENT or UV_ENOTDIR and throwIfNoEntry is false
if (!options.throwIfNoEntry && result === undefined) return undefined;

return getStatsFromBinding(result);
}

Expand Down
41 changes: 38 additions & 3 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,22 @@ void AfterStat(uv_fs_t* req) {
}
}

void AfterStatNoThrowIfNoEntry(uv_fs_t* req) {
FSReqBase* req_wrap = FSReqBase::from_req(req);
FSReqAfterScope after(req_wrap, req);

FS_ASYNC_TRACE_END1(
req->fs_type, req_wrap, "result", static_cast<int>(req->result))
if (req->result == UV_ENOENT || req->result == UV_ENOTDIR) {
req_wrap->Resolve(Undefined(req_wrap->env()->isolate()));
return;
}

if (after.Proceed()) {
req_wrap->ResolveStat(&req->statbuf);
}
}

void AfterStatFs(uv_fs_t* req) {
FSReqBase* req_wrap = FSReqBase::from_req(req);
FSReqAfterScope after(req_wrap, req);
Expand Down Expand Up @@ -1105,7 +1121,9 @@ static void Stat(const FunctionCallbackInfo<Value>& args) {
ToNamespacedPath(env, &path);

bool use_bigint = args[1]->IsTrue();
if (!args[2]->IsUndefined()) { // stat(path, use_bigint, req)
if (!args[2]->IsUndefined()) { // stat(path, use_bigint, req,
// do_not_throw_if_no_entry)
bool do_not_throw_if_no_entry = args[3]->IsFalse();
FSReqBase* req_wrap_async = GetReqWrap(args, 2, use_bigint);
CHECK_NOT_NULL(req_wrap_async);
ASYNC_THROW_IF_INSUFFICIENT_PERMISSIONS(
Expand All @@ -1115,8 +1133,25 @@ static void Stat(const FunctionCallbackInfo<Value>& args) {
path.ToStringView());
FS_ASYNC_TRACE_BEGIN1(
UV_FS_STAT, req_wrap_async, "path", TRACE_STR_COPY(*path))
AsyncCall(env, req_wrap_async, args, "stat", UTF8, AfterStat,
uv_fs_stat, *path);
if (do_not_throw_if_no_entry) {
AsyncCall(env,
req_wrap_async,
args,
"stat",
UTF8,
AfterStatNoThrowIfNoEntry,
uv_fs_stat,
*path);
} else {
AsyncCall(env,
req_wrap_async,
args,
"stat",
UTF8,
AfterStat,
uv_fs_stat,
*path);
}
} else { // stat(path, use_bigint, undefined, do_not_throw_if_no_entry)
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());
Expand Down
Loading
Loading