Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2021-01-06 02:39:54 +0300
committerJames M Snell <jasnell@gmail.com>2021-01-19 00:55:25 +0300
commitd4bea09a0a9812d28780b8afc99a2b1fed2c3015 (patch)
tree352038723a4bdb96a6bb028b8bc506ffa7322d14 /src/node_blob.h
parent3dce4fb85f604441a12cf453af715f3bf2259474 (diff)
buffer: introduce Blob
The `Blob` object is an immutable data buffer. This is a first step towards alignment with the `Blob` Web API. Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: https://github.com/nodejs/node/pull/36811 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Diffstat (limited to 'src/node_blob.h')
-rw-r--r--src/node_blob.h137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/node_blob.h b/src/node_blob.h
new file mode 100644
index 00000000000..965f65390bd
--- /dev/null
+++ b/src/node_blob.h
@@ -0,0 +1,137 @@
+#ifndef SRC_NODE_BLOB_H_
+#define SRC_NODE_BLOB_H_
+
+#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
+
+#include "async_wrap.h"
+#include "base_object.h"
+#include "env.h"
+#include "memory_tracker.h"
+#include "node_internals.h"
+#include "node_worker.h"
+#include "v8.h"
+
+#include <vector>
+
+namespace node {
+
+struct BlobEntry {
+ std::shared_ptr<v8::BackingStore> store;
+ size_t length;
+ size_t offset;
+};
+
+class Blob : public BaseObject {
+ public:
+ static void RegisterExternalReferences(
+ ExternalReferenceRegistry* registry);
+ static void Initialize(Environment* env, v8::Local<v8::Object> target);
+
+ static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
+ static void ToArrayBuffer(const v8::FunctionCallbackInfo<v8::Value>& args);
+ static void ToSlice(const v8::FunctionCallbackInfo<v8::Value>& args);
+
+ static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
+ Environment* env);
+
+ static BaseObjectPtr<Blob> Create(
+ Environment* env,
+ const std::vector<BlobEntry> store,
+ size_t length);
+
+ static bool HasInstance(Environment* env, v8::Local<v8::Value> object);
+
+ const std::vector<BlobEntry> entries() const {
+ return store_;
+ }
+
+ void MemoryInfo(MemoryTracker* tracker) const override;
+ SET_MEMORY_INFO_NAME(Blob);
+ SET_SELF_SIZE(Blob);
+
+ // Copies the contents of the Blob into an ArrayBuffer.
+ v8::MaybeLocal<v8::Value> GetArrayBuffer(Environment* env);
+
+ BaseObjectPtr<Blob> Slice(Environment* env, size_t start, size_t end);
+
+ inline size_t length() const { return length_; }
+
+ class BlobTransferData : public worker::TransferData {
+ public:
+ explicit BlobTransferData(
+ const std::vector<BlobEntry>& store,
+ size_t length)
+ : store_(store),
+ length_(length) {}
+
+ BaseObjectPtr<BaseObject> Deserialize(
+ Environment* env,
+ v8::Local<v8::Context> context,
+ std::unique_ptr<worker::TransferData> self) override;
+
+ SET_MEMORY_INFO_NAME(BlobTransferData)
+ SET_SELF_SIZE(BlobTransferData)
+ SET_NO_MEMORY_INFO()
+
+ private:
+ std::vector<BlobEntry> store_;
+ size_t length_ = 0;
+ };
+
+ BaseObject::TransferMode GetTransferMode() const override;
+ std::unique_ptr<worker::TransferData> CloneForMessaging() const override;
+
+ Blob(
+ Environment* env,
+ v8::Local<v8::Object> obj,
+ const std::vector<BlobEntry>& store,
+ size_t length);
+
+ private:
+ std::vector<BlobEntry> store_;
+ size_t length_ = 0;
+};
+
+class FixedSizeBlobCopyJob : public AsyncWrap, public ThreadPoolWork {
+ public:
+ enum class Mode {
+ SYNC,
+ ASYNC
+ };
+
+ static void RegisterExternalReferences(
+ ExternalReferenceRegistry* registry);
+ static void Initialize(Environment* env, v8::Local<v8::Object> target);
+ static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
+ static void Run(const v8::FunctionCallbackInfo<v8::Value>& args);
+
+ bool IsNotIndicativeOfMemoryLeakAtExit() const override {
+ return true;
+ }
+
+ void DoThreadPoolWork() override;
+ void AfterThreadPoolWork(int status) override;
+
+ Mode mode() const { return mode_; }
+
+ void MemoryInfo(MemoryTracker* tracker) const override;
+ SET_MEMORY_INFO_NAME(FixedSizeBlobCopyJob)
+ SET_SELF_SIZE(FixedSizeBlobCopyJob)
+
+ private:
+ FixedSizeBlobCopyJob(
+ Environment* env,
+ v8::Local<v8::Object> object,
+ Blob* blob,
+ Mode mode = Mode::ASYNC);
+
+ Mode mode_;
+ std::vector<BlobEntry> source_;
+ std::shared_ptr<v8::BackingStore> destination_;
+ size_t length_ = 0;
+};
+
+} // namespace node
+
+#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
+#endif // SRC_NODE_BLOB_H_