Skip to content

Commit 6827dbb

Browse files
authored
url: reduce revokeObjectURL cpp calls
PR-URL: #47728 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
1 parent 0a3f6a9 commit 6827dbb

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

lib/internal/url.js

+3-16
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const {
2424
StringPrototypeIncludes,
2525
StringPrototypeIndexOf,
2626
StringPrototypeSlice,
27-
StringPrototypeSplit,
2827
StringPrototypeStartsWith,
2928
Symbol,
3029
SymbolIterator,
@@ -1024,10 +1023,7 @@ ObjectDefineProperties(URL, {
10241023
});
10251024

10261025
function installObjectURLMethods() {
1027-
const {
1028-
storeDataObject,
1029-
revokeDataObject,
1030-
} = internalBinding('blob');
1026+
const bindingBlob = internalBinding('blob');
10311027

10321028
function createObjectURL(obj) {
10331029
const cryptoRandom = lazyCryptoRandom();
@@ -1040,22 +1036,13 @@ function installObjectURLMethods() {
10401036

10411037
const id = cryptoRandom.randomUUID();
10421038

1043-
storeDataObject(id, obj[blob.kHandle], obj.size, obj.type);
1039+
bindingBlob.storeDataObject(id, obj[blob.kHandle], obj.size, obj.type);
10441040

10451041
return `blob:nodedata:${id}`;
10461042
}
10471043

10481044
function revokeObjectURL(url) {
1049-
url = `${url}`;
1050-
try {
1051-
// TODO(@anonrig): Remove this try/catch by calling `parse` directly.
1052-
const parsed = new URL(url);
1053-
const split = StringPrototypeSplit(parsed.pathname, ':');
1054-
if (split.length === 2)
1055-
revokeDataObject(split[1]);
1056-
} catch {
1057-
// If there's an error, it's ignored.
1058-
}
1045+
bindingBlob.revokeObjectURL(`${url}`);
10591046
}
10601047

10611048
ObjectDefineProperties(URL, {

src/node_blob.cc

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "node_blob.h"
2+
#include "ada.h"
23
#include "async_wrap-inl.h"
34
#include "base_object-inl.h"
45
#include "env-inl.h"
@@ -7,6 +8,7 @@
78
#include "node_errors.h"
89
#include "node_external_reference.h"
910
#include "node_file.h"
11+
#include "util.h"
1012
#include "v8.h"
1113

1214
#include <algorithm>
@@ -119,7 +121,7 @@ void Blob::Initialize(
119121
SetMethod(context, target, "createBlob", New);
120122
SetMethod(context, target, "storeDataObject", StoreDataObject);
121123
SetMethod(context, target, "getDataObject", GetDataObject);
122-
SetMethod(context, target, "revokeDataObject", RevokeDataObject);
124+
SetMethod(context, target, "revokeObjectURL", RevokeObjectURL);
123125
SetMethod(context, target, "concat", Concat);
124126
SetMethod(context, target, "createBlobFromFilePath", BlobFromFilePath);
125127
}
@@ -414,15 +416,29 @@ void Blob::StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
414416
std::string(*type, type.length())));
415417
}
416418

417-
void Blob::RevokeDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
419+
// TODO(@anonrig): Add V8 Fast API to the following function
420+
void Blob::RevokeObjectURL(const FunctionCallbackInfo<Value>& args) {
421+
CHECK_GE(args.Length(), 1);
422+
CHECK(args[0]->IsString());
418423
BlobBindingData* binding_data = Realm::GetBindingData<BlobBindingData>(args);
419-
420424
Environment* env = Environment::GetCurrent(args);
421-
CHECK(args[0]->IsString()); // ID key
425+
Utf8Value input(env->isolate(), args[0].As<String>());
426+
auto out = ada::parse<ada::url_aggregator>(input.ToStringView());
422427

423-
Utf8Value key(env->isolate(), args[0]);
428+
if (!out) {
429+
return;
430+
}
431+
432+
auto pathname = out->get_pathname();
433+
auto start_index = pathname.find(':');
424434

425-
binding_data->revoke_data_object(std::string(*key, key.length()));
435+
if (start_index != std::string_view::npos && start_index != pathname.size()) {
436+
auto end_index = pathname.find(':', start_index + 1);
437+
if (end_index == std::string_view::npos) {
438+
auto id = std::string(pathname.substr(start_index + 1));
439+
binding_data->revoke_data_object(id);
440+
}
441+
}
426442
}
427443

428444
void Blob::GetDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
@@ -538,7 +554,7 @@ void Blob::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
538554
registry->Register(Blob::ToSlice);
539555
registry->Register(Blob::StoreDataObject);
540556
registry->Register(Blob::GetDataObject);
541-
registry->Register(Blob::RevokeDataObject);
557+
registry->Register(Blob::RevokeObjectURL);
542558
registry->Register(Blob::Reader::Pull);
543559
registry->Register(Concat);
544560
registry->Register(BlobFromFilePath);

src/node_blob.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Blob : public BaseObject {
3737
static void ToSlice(const v8::FunctionCallbackInfo<v8::Value>& args);
3838
static void StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args);
3939
static void GetDataObject(const v8::FunctionCallbackInfo<v8::Value>& args);
40-
static void RevokeDataObject(const v8::FunctionCallbackInfo<v8::Value>& args);
40+
static void RevokeObjectURL(const v8::FunctionCallbackInfo<v8::Value>& args);
4141

4242
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
4343
Environment* env);

0 commit comments

Comments
 (0)