Skip to content

Commit

Permalink
url: reduce revokeObjectURL cpp calls
Browse files Browse the repository at this point in the history
PR-URL: #47728
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Debadree Chatterjee <[email protected]>
  • Loading branch information
anonrig authored Apr 30, 2023
1 parent 0a3f6a9 commit 6827dbb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
19 changes: 3 additions & 16 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const {
StringPrototypeIncludes,
StringPrototypeIndexOf,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
Symbol,
SymbolIterator,
Expand Down Expand Up @@ -1024,10 +1023,7 @@ ObjectDefineProperties(URL, {
});

function installObjectURLMethods() {
const {
storeDataObject,
revokeDataObject,
} = internalBinding('blob');
const bindingBlob = internalBinding('blob');

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

const id = cryptoRandom.randomUUID();

storeDataObject(id, obj[blob.kHandle], obj.size, obj.type);
bindingBlob.storeDataObject(id, obj[blob.kHandle], obj.size, obj.type);

return `blob:nodedata:${id}`;
}

function revokeObjectURL(url) {
url = `${url}`;
try {
// TODO(@anonrig): Remove this try/catch by calling `parse` directly.
const parsed = new URL(url);
const split = StringPrototypeSplit(parsed.pathname, ':');
if (split.length === 2)
revokeDataObject(split[1]);
} catch {
// If there's an error, it's ignored.
}
bindingBlob.revokeObjectURL(`${url}`);
}

ObjectDefineProperties(URL, {
Expand Down
30 changes: 23 additions & 7 deletions src/node_blob.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "node_blob.h"
#include "ada.h"
#include "async_wrap-inl.h"
#include "base_object-inl.h"
#include "env-inl.h"
Expand All @@ -7,6 +8,7 @@
#include "node_errors.h"
#include "node_external_reference.h"
#include "node_file.h"
#include "util.h"
#include "v8.h"

#include <algorithm>
Expand Down Expand Up @@ -119,7 +121,7 @@ void Blob::Initialize(
SetMethod(context, target, "createBlob", New);
SetMethod(context, target, "storeDataObject", StoreDataObject);
SetMethod(context, target, "getDataObject", GetDataObject);
SetMethod(context, target, "revokeDataObject", RevokeDataObject);
SetMethod(context, target, "revokeObjectURL", RevokeObjectURL);
SetMethod(context, target, "concat", Concat);
SetMethod(context, target, "createBlobFromFilePath", BlobFromFilePath);
}
Expand Down Expand Up @@ -414,15 +416,29 @@ void Blob::StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
std::string(*type, type.length())));
}

void Blob::RevokeDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
// TODO(@anonrig): Add V8 Fast API to the following function
void Blob::RevokeObjectURL(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 1);
CHECK(args[0]->IsString());
BlobBindingData* binding_data = Realm::GetBindingData<BlobBindingData>(args);

Environment* env = Environment::GetCurrent(args);
CHECK(args[0]->IsString()); // ID key
Utf8Value input(env->isolate(), args[0].As<String>());
auto out = ada::parse<ada::url_aggregator>(input.ToStringView());

Utf8Value key(env->isolate(), args[0]);
if (!out) {
return;
}

auto pathname = out->get_pathname();
auto start_index = pathname.find(':');

binding_data->revoke_data_object(std::string(*key, key.length()));
if (start_index != std::string_view::npos && start_index != pathname.size()) {
auto end_index = pathname.find(':', start_index + 1);
if (end_index == std::string_view::npos) {
auto id = std::string(pathname.substr(start_index + 1));
binding_data->revoke_data_object(id);
}
}
}

void Blob::GetDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
Expand Down Expand Up @@ -538,7 +554,7 @@ void Blob::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(Blob::ToSlice);
registry->Register(Blob::StoreDataObject);
registry->Register(Blob::GetDataObject);
registry->Register(Blob::RevokeDataObject);
registry->Register(Blob::RevokeObjectURL);
registry->Register(Blob::Reader::Pull);
registry->Register(Concat);
registry->Register(BlobFromFilePath);
Expand Down
2 changes: 1 addition & 1 deletion src/node_blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Blob : public BaseObject {
static void ToSlice(const v8::FunctionCallbackInfo<v8::Value>& args);
static void StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetDataObject(const v8::FunctionCallbackInfo<v8::Value>& args);
static void RevokeDataObject(const v8::FunctionCallbackInfo<v8::Value>& args);
static void RevokeObjectURL(const v8::FunctionCallbackInfo<v8::Value>& args);

static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
Environment* env);
Expand Down

0 comments on commit 6827dbb

Please sign in to comment.