@@ -365,54 +365,38 @@ inline sqlite3* DatabaseSync::Connection() {
365365std::optional<std::string> ValidateDatabasePath (Environment* env,
366366 Local<Value> path,
367367 const std::string& field_name) {
368- auto has_null_bytes = [](const std::string& str) {
369- return str.find (' \0 ' ) != std::string ::npos;
368+ constexpr auto has_null_bytes = [](std::string_view str) {
369+ return str.find (' \0 ' ) != std::string_view ::npos;
370370 };
371- std::string location;
372371 if (path->IsString ()) {
373- location = Utf8Value (env->isolate (), path.As <String>()). ToString ( );
374- if (!has_null_bytes (location)) {
375- return location;
372+ Utf8Value location (env->isolate (), path.As <String>());
373+ if (!has_null_bytes (location. ToStringView () )) {
374+ return location. ToString () ;
376375 }
377- }
378-
379- if (path->IsUint8Array ()) {
376+ } else if (path->IsUint8Array ()) {
380377 Local<Uint8Array> buffer = path.As <Uint8Array>();
381378 size_t byteOffset = buffer->ByteOffset ();
382379 size_t byteLength = buffer->ByteLength ();
383380 auto data =
384381 static_cast <const uint8_t *>(buffer->Buffer ()->Data ()) + byteOffset;
385- if (!(std::find (data, data + byteLength, 0 ) != data + byteLength)) {
386- Local<Value> out;
387- if (String::NewFromUtf8 (env->isolate (),
388- reinterpret_cast <const char *>(data),
389- NewStringType::kNormal ,
390- static_cast <int >(byteLength))
391- .ToLocal (&out)) {
392- return Utf8Value (env->isolate (), out.As <String>()).ToString ();
393- }
382+ if (std::find (data, data + byteLength, 0 ) == data + byteLength) {
383+ return std::string (reinterpret_cast <const char *>(data), byteLength);
394384 }
395- }
396-
397- // When is URL
398- if (path->IsObject ()) {
399- Local<Object> url = path.As <Object>();
385+ } else if (path->IsObject ()) { // When is URL
386+ auto url = path.As <Object>();
400387 Local<Value> href;
401- Local<Value> protocol;
402388 if (url->Get (env->context (), env->href_string ()).ToLocal (&href) &&
403- href->IsString () &&
404- url->Get (env->context (), env->protocol_string ()).ToLocal (&protocol) &&
405- protocol->IsString ()) {
406- location = Utf8Value (env->isolate (), href.As <String>()).ToString ();
389+ href->IsString ()) {
390+ Utf8Value location_value (env->isolate (), href.As <String>());
391+ auto location = location_value.ToStringView ();
407392 if (!has_null_bytes (location)) {
408- auto file_url = ada::parse (location);
409- CHECK (file_url);
410- if (file_url->type != ada::scheme::FILE) {
393+ CHECK (ada::can_parse (location));
394+ if (!location.starts_with (" file:" )) {
411395 THROW_ERR_INVALID_URL_SCHEME (env->isolate ());
412396 return std::nullopt ;
413397 }
414398
415- return location ;
399+ return location_value. ToString () ;
416400 }
417401 }
418402 }
0 commit comments