Skip to content

Commit

Permalink
Merge pull request #3097 from cloudflare/jsnell/blob-use-buffersource…
Browse files Browse the repository at this point in the history
…-for-reads
  • Loading branch information
jasnell authored Nov 12, 2024
2 parents f117326 + 56c6529 commit 88eab86
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/workerd/api/blob.c++
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,23 @@ jsg::Ref<Blob> Blob::slice(
JSG_THIS, data.slice(start, end), normalizeType(kj::mv(type).orDefault(nullptr)));
}

jsg::Promise<kj::Array<kj::byte>> Blob::arrayBuffer(jsg::Lock& js) {
// TODO(perf): Find a way to avoid the copy.
jsg::Promise<jsg::BufferSource> Blob::arrayBuffer(jsg::Lock& js) {
FeatureObserver::maybeRecordUse(FeatureObserver::Feature::BLOB_AS_ARRAY_BUFFER);
return js.resolvedPromise(kj::heapArray<byte>(data));
// We use BufferSource here instead of kj::Array<kj::byte> to ensure that the
// resulting backing store is associated with the isolate, which is necessary
// for when we start making use of v8 sandboxing.
auto backing = jsg::BackingStore::alloc<v8::ArrayBuffer>(js, data.size());
backing.asArrayPtr().copyFrom(data);
return js.resolvedPromise(jsg::BufferSource(js, kj::mv(backing)));
}

jsg::Promise<jsg::BufferSource> Blob::bytes(jsg::Lock& js) {
return js.resolvedPromise(js.bytes(kj::heapArray<byte>(data)));
// We use BufferSource here instead of kj::Array<kj::byte> to ensure that the
// resulting backing store is associated with the isolate, which is necessary
// for when we start making use of v8 sandboxing.
auto backing = jsg::BackingStore::alloc<v8::Uint8Array>(js, data.size());
backing.asArrayPtr().copyFrom(data);
return js.resolvedPromise(jsg::BufferSource(js, kj::mv(backing)));
}

jsg::Promise<kj::String> Blob::text(jsg::Lock& js) {
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Blob: public jsg::Object {
jsg::Ref<Blob> slice(
jsg::Optional<int> start, jsg::Optional<int> end, jsg::Optional<kj::String> type);

jsg::Promise<kj::Array<kj::byte>> arrayBuffer(jsg::Lock& js);
jsg::Promise<jsg::BufferSource> arrayBuffer(jsg::Lock& js);
jsg::Promise<jsg::BufferSource> bytes(jsg::Lock& js);
jsg::Promise<kj::String> text(jsg::Lock& js);
jsg::Ref<ReadableStream> stream();
Expand Down

0 comments on commit 88eab86

Please sign in to comment.