-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Make blob: URLs cross‑realm and allow file‑backed Blob cloning #63103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,10 @@ | |
| #include "v8.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cstdlib> | ||
| #include <mutex> | ||
| #include <optional> | ||
| #include <unordered_map> | ||
|
|
||
| namespace node { | ||
|
|
||
|
|
@@ -103,6 +107,48 @@ void Concat(const FunctionCallbackInfo<Value>& args) { | |
| args.GetReturnValue().Set(ArrayBuffer::New(isolate, std::move(store))); | ||
| } | ||
|
|
||
| struct BlobURLEntry { | ||
| std::shared_ptr<DataQueue> data_queue; | ||
| size_t length; | ||
| std::string type; | ||
| }; | ||
|
|
||
| static std::mutex blob_url_registry_mutex; | ||
| static std::unordered_map<std::string, BlobURLEntry> blob_url_registry; | ||
|
|
||
| static void RevokeBlobURLEntry(const std::string& uuid); | ||
|
|
||
| static void BlobURLCleanupHook(void* arg) { | ||
| std::string* uuid = static_cast<std::string*>(arg); | ||
| RevokeBlobURLEntry(*uuid); | ||
| delete uuid; | ||
| } | ||
|
|
||
| static void StoreBlobURLEntry(const std::string& uuid, | ||
| std::shared_ptr<DataQueue> data_queue, | ||
| size_t length, | ||
| std::string type, | ||
| Environment* env) { | ||
| std::lock_guard<std::mutex> lock(blob_url_registry_mutex); | ||
| blob_url_registry[uuid] = BlobURLEntry{std::move(data_queue), length, | ||
| std::move(type)}; | ||
|
|
||
| std::string* uuid_copy = new std::string(uuid); | ||
| env->AddCleanupHook(BlobURLCleanupHook, uuid_copy); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really the right lifetime? i.e. a blob URL becomes inaccessible once its creating environment exits? That would mean, for example, that when a worker exits (and that could also be e.g. a crash due to an uncaught exception, i.e. something unpredictable), its parent environment would not be able to use the URL anymore |
||
| } | ||
|
|
||
| static std::optional<BlobURLEntry> GetBlobURLEntry(const std::string& uuid) { | ||
| std::lock_guard<std::mutex> lock(blob_url_registry_mutex); | ||
| auto it = blob_url_registry.find(uuid); | ||
| if (it == blob_url_registry.end()) return std::nullopt; | ||
| return it->second; | ||
| } | ||
|
|
||
| static void RevokeBlobURLEntry(const std::string& uuid) { | ||
| std::lock_guard<std::mutex> lock(blob_url_registry_mutex); | ||
| blob_url_registry.erase(uuid); | ||
| } | ||
|
|
||
| void BlobFromFilePath(const FunctionCallbackInfo<Value>& args) { | ||
| Environment* env = Environment::GetCurrent(args); | ||
| BufferValue path(env->isolate(), args[0]); | ||
|
|
@@ -302,7 +348,7 @@ Blob::Reader::Reader(Environment* env, | |
| Local<Object> obj, | ||
| BaseObjectPtr<Blob> strong_ptr) | ||
| : AsyncWrap(env, obj, AsyncWrap::PROVIDER_BLOBREADER), | ||
| inner_(strong_ptr->data_queue_->get_reader()), | ||
| inner_(strong_ptr->data_queue_->get_reader(env)), | ||
| strong_ptr_(std::move(strong_ptr)) { | ||
| MakeWeak(); | ||
| } | ||
|
|
@@ -453,7 +499,6 @@ void Blob::StoreDataObject(const FunctionCallbackInfo<Value>& args) { | |
| CHECK(args[2]->IsUint32()); // Length | ||
| CHECK(args[3]->IsString()); // Type | ||
|
|
||
| BlobBindingData* binding_data = realm->GetBindingData<BlobBindingData>(); | ||
| Isolate* isolate = realm->isolate(); | ||
|
|
||
| Utf8Value key(isolate, args[0]); | ||
|
|
@@ -463,10 +508,8 @@ void Blob::StoreDataObject(const FunctionCallbackInfo<Value>& args) { | |
| size_t length = args[2].As<Uint32>()->Value(); | ||
| Utf8Value type(isolate, args[3]); | ||
|
|
||
| binding_data->store_data_object( | ||
| key.ToString(), | ||
| BlobBindingData::StoredDataObject( | ||
| BaseObjectPtr<Blob>(blob), length, type.ToString())); | ||
| StoreBlobURLEntry(key.ToString(), blob->data_queue_, length, | ||
| type.ToString(), realm->env()); | ||
| } | ||
|
|
||
| // Note: applying the V8 Fast API to the following function does not produce | ||
|
|
@@ -475,7 +518,6 @@ void Blob::RevokeObjectURL(const FunctionCallbackInfo<Value>& args) { | |
| CHECK_GE(args.Length(), 1); | ||
| CHECK(args[0]->IsString()); | ||
| Realm* realm = Realm::GetCurrent(args); | ||
| BlobBindingData* binding_data = realm->GetBindingData<BlobBindingData>(); | ||
| Isolate* isolate = realm->isolate(); | ||
|
|
||
| Utf8Value input(isolate, args[0].As<String>()); | ||
|
|
@@ -492,37 +534,38 @@ void Blob::RevokeObjectURL(const FunctionCallbackInfo<Value>& args) { | |
| 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); | ||
| RevokeBlobURLEntry(id); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void Blob::GetDataObject(const FunctionCallbackInfo<Value>& args) { | ||
| CHECK(args[0]->IsString()); | ||
| Realm* realm = Realm::GetCurrent(args); | ||
| BlobBindingData* binding_data = realm->GetBindingData<BlobBindingData>(); | ||
| Isolate* isolate = realm->isolate(); | ||
|
|
||
| Utf8Value key(isolate, args[0]); | ||
| std::optional<BlobURLEntry> stored = GetBlobURLEntry(key.ToString()); | ||
| if (!stored.has_value()) return; | ||
|
|
||
| Environment* env = realm->env(); | ||
| BaseObjectPtr<Blob> blob = Blob::Create(env, stored->data_queue); | ||
| if (!blob) return; | ||
|
|
||
| Local<Value> type; | ||
| if (!String::NewFromUtf8(isolate, | ||
| stored->type.c_str(), | ||
| NewStringType::kNormal, | ||
| static_cast<int>(stored->type.length())) | ||
| .ToLocal(&type)) { | ||
| return; | ||
| } | ||
|
|
||
| BlobBindingData::StoredDataObject stored = | ||
| binding_data->get_data_object(key.ToString()); | ||
| if (stored.blob) { | ||
| Local<Value> type; | ||
| if (!String::NewFromUtf8(isolate, | ||
| stored.type.c_str(), | ||
| NewStringType::kNormal, | ||
| static_cast<int>(stored.type.length())) | ||
| .ToLocal(&type)) { | ||
| return; | ||
| } | ||
|
|
||
| Local<Value> values[] = {stored.blob->object(), | ||
| Uint32::NewFromUnsigned(isolate, stored.length), | ||
| type}; | ||
| Local<Value> values[] = {blob->object(), | ||
| Uint32::NewFromUnsigned(isolate, stored->length), | ||
| type}; | ||
|
|
||
| args.GetReturnValue().Set(Array::New(isolate, values, arraysize(values))); | ||
| } | ||
| args.GetReturnValue().Set(Array::New(isolate, values, arraysize(values))); | ||
| } | ||
|
|
||
| void BlobBindingData::StoredDataObject::MemoryInfo( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const { isMainThread, parentPort, Worker } = require('worker_threads'); | ||
| const { Blob } = require('buffer'); | ||
|
|
||
| if (isMainThread) { | ||
| const blob = new Blob(['hello world']); | ||
| const url = URL.createObjectURL(blob); | ||
|
|
||
| const worker = new Worker(__filename); | ||
| worker.once('message', common.mustCall((value) => { | ||
| assert.deepStrictEqual(value, { size: 11, type: '', text: 'hello world' }); | ||
| worker.terminate(); | ||
| })); | ||
| worker.once('error', common.mustNotCall()); | ||
| worker.once('exit', common.mustCall((code) => { | ||
| assert.strictEqual(code, 0); | ||
| })); | ||
|
|
||
| worker.postMessage(url); | ||
| } else { | ||
| const { resolveObjectURL } = require('buffer'); | ||
| parentPort.once('message', async (url) => { | ||
| const blob = resolveObjectURL(url); | ||
| const text = await blob.text(); | ||
| parentPort.postMessage({ size: blob.size, type: blob.type, text }); | ||
| }); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be a good use case for
ExclusiveAccessfromnode_mutex.h