diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 54c7d5d6b2f48d..900188fa5fddf5 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -593,54 +593,38 @@ bool DatabaseSync::ShouldIgnoreSQLiteError() { std::optional ValidateDatabasePath(Environment* env, Local path, const std::string& field_name) { - auto has_null_bytes = [](const std::string& str) { - return str.find('\0') != std::string::npos; + constexpr auto has_null_bytes = [](std::string_view str) { + return str.find('\0') != std::string_view::npos; }; - std::string location; if (path->IsString()) { - location = Utf8Value(env->isolate(), path.As()).ToString(); - if (!has_null_bytes(location)) { - return location; + Utf8Value location(env->isolate(), path.As()); + if (!has_null_bytes(location.ToStringView())) { + return location.ToString(); } - } - - if (path->IsUint8Array()) { + } else if (path->IsUint8Array()) { Local buffer = path.As(); size_t byteOffset = buffer->ByteOffset(); size_t byteLength = buffer->ByteLength(); auto data = static_cast(buffer->Buffer()->Data()) + byteOffset; - if (!(std::find(data, data + byteLength, 0) != data + byteLength)) { - Local out; - if (String::NewFromUtf8(env->isolate(), - reinterpret_cast(data), - NewStringType::kNormal, - static_cast(byteLength)) - .ToLocal(&out)) { - return Utf8Value(env->isolate(), out.As()).ToString(); - } + if (std::find(data, data + byteLength, 0) == data + byteLength) { + return std::string(reinterpret_cast(data), byteLength); } - } - - // When is URL - if (path->IsObject()) { - Local url = path.As(); + } else if (path->IsObject()) { // When is URL + auto url = path.As(); Local href; - Local protocol; if (url->Get(env->context(), env->href_string()).ToLocal(&href) && - href->IsString() && - url->Get(env->context(), env->protocol_string()).ToLocal(&protocol) && - protocol->IsString()) { - location = Utf8Value(env->isolate(), href.As()).ToString(); + href->IsString()) { + Utf8Value location_value(env->isolate(), href.As()); + auto location = location_value.ToStringView(); if (!has_null_bytes(location)) { - auto file_url = ada::parse(location); - CHECK(file_url); - if (file_url->type != ada::scheme::FILE) { + CHECK(ada::can_parse(location)); + if (!location.starts_with("file:")) { THROW_ERR_INVALID_URL_SCHEME(env->isolate()); return std::nullopt; } - return location; + return location_value.ToString(); } } }