@@ -365,54 +365,38 @@ inline sqlite3* DatabaseSync::Connection() {
365
365
std::optional<std::string> ValidateDatabasePath (Environment* env,
366
366
Local<Value> path,
367
367
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;
370
370
};
371
- std::string location;
372
371
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 () ;
376
375
}
377
- }
378
-
379
- if (path->IsUint8Array ()) {
376
+ } else if (path->IsUint8Array ()) {
380
377
Local<Uint8Array> buffer = path.As <Uint8Array>();
381
378
size_t byteOffset = buffer->ByteOffset ();
382
379
size_t byteLength = buffer->ByteLength ();
383
380
auto data =
384
381
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);
394
384
}
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>();
400
387
Local<Value> href;
401
- Local<Value> protocol;
402
388
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 ();
407
392
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:" )) {
411
395
THROW_ERR_INVALID_URL_SCHEME (env->isolate ());
412
396
return std::nullopt;
413
397
}
414
398
415
- return location ;
399
+ return location_value. ToString () ;
416
400
}
417
401
}
418
402
}
0 commit comments