url: reduce revokeObjectURL cpp calls
PR-URL: https://github.com/nodejs/node/pull/47728 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
This commit is contained in:
parent
0a3f6a9d07
commit
6827dbbfde
@ -24,7 +24,6 @@ const {
|
|||||||
StringPrototypeIncludes,
|
StringPrototypeIncludes,
|
||||||
StringPrototypeIndexOf,
|
StringPrototypeIndexOf,
|
||||||
StringPrototypeSlice,
|
StringPrototypeSlice,
|
||||||
StringPrototypeSplit,
|
|
||||||
StringPrototypeStartsWith,
|
StringPrototypeStartsWith,
|
||||||
Symbol,
|
Symbol,
|
||||||
SymbolIterator,
|
SymbolIterator,
|
||||||
@ -1024,10 +1023,7 @@ ObjectDefineProperties(URL, {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function installObjectURLMethods() {
|
function installObjectURLMethods() {
|
||||||
const {
|
const bindingBlob = internalBinding('blob');
|
||||||
storeDataObject,
|
|
||||||
revokeDataObject,
|
|
||||||
} = internalBinding('blob');
|
|
||||||
|
|
||||||
function createObjectURL(obj) {
|
function createObjectURL(obj) {
|
||||||
const cryptoRandom = lazyCryptoRandom();
|
const cryptoRandom = lazyCryptoRandom();
|
||||||
@ -1040,22 +1036,13 @@ function installObjectURLMethods() {
|
|||||||
|
|
||||||
const id = cryptoRandom.randomUUID();
|
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}`;
|
return `blob:nodedata:${id}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function revokeObjectURL(url) {
|
function revokeObjectURL(url) {
|
||||||
url = `${url}`;
|
bindingBlob.revokeObjectURL(`${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.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectDefineProperties(URL, {
|
ObjectDefineProperties(URL, {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include "node_blob.h"
|
#include "node_blob.h"
|
||||||
|
#include "ada.h"
|
||||||
#include "async_wrap-inl.h"
|
#include "async_wrap-inl.h"
|
||||||
#include "base_object-inl.h"
|
#include "base_object-inl.h"
|
||||||
#include "env-inl.h"
|
#include "env-inl.h"
|
||||||
@ -7,6 +8,7 @@
|
|||||||
#include "node_errors.h"
|
#include "node_errors.h"
|
||||||
#include "node_external_reference.h"
|
#include "node_external_reference.h"
|
||||||
#include "node_file.h"
|
#include "node_file.h"
|
||||||
|
#include "util.h"
|
||||||
#include "v8.h"
|
#include "v8.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@ -119,7 +121,7 @@ void Blob::Initialize(
|
|||||||
SetMethod(context, target, "createBlob", New);
|
SetMethod(context, target, "createBlob", New);
|
||||||
SetMethod(context, target, "storeDataObject", StoreDataObject);
|
SetMethod(context, target, "storeDataObject", StoreDataObject);
|
||||||
SetMethod(context, target, "getDataObject", GetDataObject);
|
SetMethod(context, target, "getDataObject", GetDataObject);
|
||||||
SetMethod(context, target, "revokeDataObject", RevokeDataObject);
|
SetMethod(context, target, "revokeObjectURL", RevokeObjectURL);
|
||||||
SetMethod(context, target, "concat", Concat);
|
SetMethod(context, target, "concat", Concat);
|
||||||
SetMethod(context, target, "createBlobFromFilePath", BlobFromFilePath);
|
SetMethod(context, target, "createBlobFromFilePath", BlobFromFilePath);
|
||||||
}
|
}
|
||||||
@ -414,15 +416,29 @@ void Blob::StoreDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|||||||
std::string(*type, type.length())));
|
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);
|
BlobBindingData* binding_data = Realm::GetBindingData<BlobBindingData>(args);
|
||||||
|
|
||||||
Environment* env = Environment::GetCurrent(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;
|
||||||
|
}
|
||||||
|
|
||||||
binding_data->revoke_data_object(std::string(*key, key.length()));
|
auto pathname = out->get_pathname();
|
||||||
|
auto start_index = pathname.find(':');
|
||||||
|
|
||||||
|
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) {
|
void Blob::GetDataObject(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||||
@ -538,7 +554,7 @@ void Blob::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
|
|||||||
registry->Register(Blob::ToSlice);
|
registry->Register(Blob::ToSlice);
|
||||||
registry->Register(Blob::StoreDataObject);
|
registry->Register(Blob::StoreDataObject);
|
||||||
registry->Register(Blob::GetDataObject);
|
registry->Register(Blob::GetDataObject);
|
||||||
registry->Register(Blob::RevokeDataObject);
|
registry->Register(Blob::RevokeObjectURL);
|
||||||
registry->Register(Blob::Reader::Pull);
|
registry->Register(Blob::Reader::Pull);
|
||||||
registry->Register(Concat);
|
registry->Register(Concat);
|
||||||
registry->Register(BlobFromFilePath);
|
registry->Register(BlobFromFilePath);
|
||||||
|
@ -37,7 +37,7 @@ class Blob : public BaseObject {
|
|||||||
static void ToSlice(const v8::FunctionCallbackInfo<v8::Value>& args);
|
static void ToSlice(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||||
static void StoreDataObject(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 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(
|
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
|
||||||
Environment* env);
|
Environment* env);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user