Skip to content

Commit 0b485ec

Browse files
committed
url: reduce revokeObjectURL cpp calls
1 parent 2e44a14 commit 0b485ec

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

lib/internal/url.js

Lines changed: 3 additions & 16 deletions
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

Lines changed: 18 additions & 7 deletions
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,24 @@ 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+
}
424431

425-
binding_data->revoke_data_object(std::string(*key, key.length()));
432+
auto path = std::string(out->get_pathname());
433+
auto sections = SplitString(path, ':', false);
434+
if (sections.size() == 2) {
435+
binding_data->revoke_data_object(sections.at(1));
436+
}
426437
}
427438

428439
void Blob::GetDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
@@ -538,7 +549,7 @@ void Blob::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
538549
registry->Register(Blob::ToSlice);
539550
registry->Register(Blob::StoreDataObject);
540551
registry->Register(Blob::GetDataObject);
541-
registry->Register(Blob::RevokeDataObject);
552+
registry->Register(Blob::RevokeObjectURL);
542553
registry->Register(Blob::Reader::Pull);
543554
registry->Register(Concat);
544555
registry->Register(BlobFromFilePath);

src/node_blob.h

Lines changed: 1 addition & 1 deletion
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)