1 #ifndef SRC_NODE_BLOB_H_ 2 #define SRC_NODE_BLOB_H_ 3 4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 5 6 #include "async_wrap.h" 7 #include "base_object.h" 8 #include "env.h" 9 #include "memory_tracker.h" 10 #include "node_internals.h" 11 #include "node_worker.h" 12 #include "v8.h" 13 14 #include <vector> 15 16 namespace node { 17 18 struct BlobEntry { 19 std::shared_ptr<v8::BackingStore> store; 20 size_t length; 21 size_t offset; 22 }; 23 24 class Blob : public BaseObject { 25 public: 26 static void Initialize(Environment* env, v8::Local<v8::Object> target); 27 28 static void New(const v8::FunctionCallbackInfo<v8::Value>& args); 29 static void ToArrayBuffer(const v8::FunctionCallbackInfo<v8::Value>& args); 30 static void ToSlice(const v8::FunctionCallbackInfo<v8::Value>& args); 31 32 static v8::Local<v8::FunctionTemplate> GetConstructorTemplate( 33 Environment* env); 34 35 static BaseObjectPtr<Blob> Create( 36 Environment* env, 37 const std::vector<BlobEntry> store, 38 size_t length); 39 40 static bool HasInstance(Environment* env, v8::Local<v8::Value> object); 41 entries()42 const std::vector<BlobEntry> entries() const { 43 return store_; 44 } 45 46 void MemoryInfo(MemoryTracker* tracker) const override; 47 SET_MEMORY_INFO_NAME(Blob) 48 SET_SELF_SIZE(Blob) 49 50 // Copies the contents of the Blob into an ArrayBuffer. 51 v8::MaybeLocal<v8::Value> GetArrayBuffer(Environment* env); 52 53 BaseObjectPtr<Blob> Slice(Environment* env, size_t start, size_t end); 54 length()55 inline size_t length() const { return length_; } 56 57 class BlobTransferData : public worker::TransferData { 58 public: BlobTransferData(const std::vector<BlobEntry> & store,size_t length)59 explicit BlobTransferData( 60 const std::vector<BlobEntry>& store, 61 size_t length) 62 : store_(store), 63 length_(length) {} 64 65 BaseObjectPtr<BaseObject> Deserialize( 66 Environment* env, 67 v8::Local<v8::Context> context, 68 std::unique_ptr<worker::TransferData> self) override; 69 70 SET_MEMORY_INFO_NAME(BlobTransferData) 71 SET_SELF_SIZE(BlobTransferData) 72 SET_NO_MEMORY_INFO() 73 74 private: 75 std::vector<BlobEntry> store_; 76 size_t length_ = 0; 77 }; 78 79 BaseObject::TransferMode GetTransferMode() const override; 80 std::unique_ptr<worker::TransferData> CloneForMessaging() const override; 81 82 Blob( 83 Environment* env, 84 v8::Local<v8::Object> obj, 85 const std::vector<BlobEntry>& store, 86 size_t length); 87 88 private: 89 std::vector<BlobEntry> store_; 90 size_t length_ = 0; 91 }; 92 93 class FixedSizeBlobCopyJob : public AsyncWrap, public ThreadPoolWork { 94 public: 95 enum class Mode { 96 SYNC, 97 ASYNC 98 }; 99 100 static void Initialize(Environment* env, v8::Local<v8::Object> target); 101 static void New(const v8::FunctionCallbackInfo<v8::Value>& args); 102 static void Run(const v8::FunctionCallbackInfo<v8::Value>& args); 103 IsNotIndicativeOfMemoryLeakAtExit()104 bool IsNotIndicativeOfMemoryLeakAtExit() const override { 105 return true; 106 } 107 108 void DoThreadPoolWork() override; 109 void AfterThreadPoolWork(int status) override; 110 mode()111 Mode mode() const { return mode_; } 112 113 void MemoryInfo(MemoryTracker* tracker) const override; 114 SET_MEMORY_INFO_NAME(FixedSizeBlobCopyJob) 115 SET_SELF_SIZE(FixedSizeBlobCopyJob) 116 117 private: 118 FixedSizeBlobCopyJob( 119 Environment* env, 120 v8::Local<v8::Object> object, 121 Blob* blob, 122 Mode mode = Mode::ASYNC); 123 124 Mode mode_; 125 std::vector<BlobEntry> source_; 126 std::shared_ptr<v8::BackingStore> destination_; 127 size_t length_ = 0; 128 }; 129 130 } // namespace node 131 132 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 133 #endif // SRC_NODE_BLOB_H_ 134