Skip to content

Commit 09e2191

Browse files
KhafraDevmarco-ippolito
authored andcommitted
buffer: add .bytes() method to Blob
PR-URL: #53221 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Filip Skokan <[email protected]>
1 parent 0f23553 commit 09e2191

File tree

8 files changed

+81
-5
lines changed

8 files changed

+81
-5
lines changed

lib/internal/blob.js

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
MathMin,
77
ObjectDefineProperties,
88
ObjectDefineProperty,
9+
PromisePrototypeThen,
910
PromiseReject,
1011
ReflectConstruct,
1112
RegExpPrototypeExec,
@@ -312,6 +313,15 @@ class Blob {
312313
return dec.decode(await this.arrayBuffer());
313314
}
314315

316+
bytes() {
317+
if (!isBlob(this))
318+
throw new ERR_INVALID_THIS('Blob');
319+
320+
return PromisePrototypeThen(
321+
this.arrayBuffer(),
322+
(buffer) => new Uint8Array(buffer));
323+
}
324+
315325
/**
316326
* @returns {ReadableStream}
317327
*/

test/fixtures/wpt/FileAPI/Blob-methods-from-detached-frame.html

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
assert_equals(await slicedBlob.text(), "oo");
2929
assert_equals(charCodeBufferToString(await slicedBlob.arrayBuffer()), "oo");
30+
assert_equals(charCodeArrayToString(await slicedBlob.bytes()), "oo");
3031

3132
const reader = slicedBlob.stream().getReader();
3233
const { value } = await reader.read();
@@ -48,6 +49,14 @@
4849
assert_equals(charCodeBufferToString(charCodeBuffer), "bar");
4950
}, "arrayBuffer()");
5051

52+
promise_test(async () => {
53+
const { bytes } = await BlobPrototypeFromDetachedFramePromise;
54+
const blob = new Blob(["bar"]);
55+
56+
const charCodeBytes = await bytes.call(blob);
57+
assert_equals(charCodeArrayToString(charCodeBytes), "bar");
58+
}, "bytes()");
59+
5160
promise_test(async () => {
5261
const { stream } = await BlobPrototypeFromDetachedFramePromise;
5362
const blob = new Blob(["baz"]);

test/fixtures/wpt/FileAPI/META.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
spec: https://w3c.github.io/FileAPI/
22
suggested_reviewers:
33
- inexorabletash
4-
- zqzhang
54
- jdm
65
- mkruisselbrink
6+
- annevk
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// META: title=Blob bytes()
2+
// META: script=../support/Blob.js
3+
'use strict';
4+
5+
promise_test(async () => {
6+
const input_arr = new TextEncoder().encode("PASS");
7+
const blob = new Blob([input_arr]);
8+
const uint8array = await blob.bytes();
9+
assert_true(uint8array instanceof Uint8Array);
10+
assert_equals_typed_array(uint8array, input_arr);
11+
}, "Blob.bytes()")
12+
13+
promise_test(async () => {
14+
const input_arr = new TextEncoder().encode("");
15+
const blob = new Blob([input_arr]);
16+
const uint8array = await blob.bytes();
17+
assert_true(uint8array instanceof Uint8Array);
18+
assert_equals_typed_array(uint8array, input_arr);
19+
}, "Blob.bytes() empty Blob data")
20+
21+
promise_test(async () => {
22+
const input_arr = new TextEncoder().encode("\u08B8\u000a");
23+
const blob = new Blob([input_arr]);
24+
const uint8array = await blob.bytes();
25+
assert_equals_typed_array(uint8array, input_arr);
26+
}, "Blob.bytes() non-ascii input")
27+
28+
promise_test(async () => {
29+
const input_arr = [8, 241, 48, 123, 151];
30+
const typed_arr = new Uint8Array(input_arr);
31+
const blob = new Blob([typed_arr]);
32+
const uint8array = await blob.bytes();
33+
assert_equals_typed_array(uint8array, typed_arr);
34+
}, "Blob.bytes() non-unicode input")
35+
36+
promise_test(async () => {
37+
const input_arr = new TextEncoder().encode("PASS");
38+
const blob = new Blob([input_arr]);
39+
const uint8array_results = await Promise.all([blob.bytes(),
40+
blob.bytes(), blob.bytes()]);
41+
for (let uint8array of uint8array_results) {
42+
assert_true(uint8array instanceof Uint8Array);
43+
assert_equals_typed_array(uint8array, input_arr);
44+
}
45+
}, "Blob.bytes() concurrent reads")

test/fixtures/wpt/FileAPI/blob/Blob-constructor.any.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,11 @@ test_blob(function() {
290290
new Int16Array([0x4150, 0x5353]),
291291
new Uint32Array([0x53534150]),
292292
new Int32Array([0x53534150]),
293+
new Float16Array([2.65625, 58.59375]),
293294
new Float32Array([0xD341500000])
294295
]);
295296
}, {
296-
expected: "PASSPASSPASSPASSPASSPASSPASS",
297+
expected: "PASSPASSPASSPASSPASSPASSPASSPASS",
297298
type: "",
298299
desc: "Passing typed arrays as elements of the blobParts array should work."
299300
});

test/fixtures/wpt/FileAPI/blob/Blob-stream.any.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,18 @@ promise_test(async() => {
7070
await garbageCollect();
7171
const chunks = await read_all_chunks(stream, { perform_gc: true });
7272
assert_array_equals(chunks, input_arr);
73-
}, "Blob.stream() garbage collection of blob shouldn't break stream" +
73+
}, "Blob.stream() garbage collection of blob shouldn't break stream " +
74+
"consumption")
75+
76+
promise_test(async() => {
77+
const input_arr = [8, 241, 48, 123, 151];
78+
const typed_arr = new Uint8Array(input_arr);
79+
let blob = new Blob([typed_arr]);
80+
const chunksPromise = read_all_chunks(blob.stream());
81+
// It somehow matters to do GC here instead of doing `perform_gc: true`
82+
await garbageCollect();
83+
assert_array_equals(await chunksPromise, input_arr);
84+
}, "Blob.stream() garbage collection of stream shouldn't break stream " +
7485
"consumption")
7586

7687
promise_test(async () => {

test/fixtures/wpt/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Last update:
1616
- dom/events: https://github.com/web-platform-tests/wpt/tree/ab8999891c/dom/events
1717
- encoding: https://github.com/web-platform-tests/wpt/tree/a58bbf6d8c/encoding
1818
- fetch/data-urls/resources: https://github.com/web-platform-tests/wpt/tree/7c79d998ff/fetch/data-urls/resources
19-
- FileAPI: https://github.com/web-platform-tests/wpt/tree/e36dbb6f00/FileAPI
19+
- FileAPI: https://github.com/web-platform-tests/wpt/tree/cceaf3628d/FileAPI
2020
- hr-time: https://github.com/web-platform-tests/wpt/tree/34cafd797e/hr-time
2121
- html/webappapis/atob: https://github.com/web-platform-tests/wpt/tree/f267e1dca6/html/webappapis/atob
2222
- html/webappapis/microtask-queuing: https://github.com/web-platform-tests/wpt/tree/2c5c3c4c27/html/webappapis/microtask-queuing

test/fixtures/wpt/versions.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"path": "fetch/data-urls/resources"
2525
},
2626
"FileAPI": {
27-
"commit": "e36dbb6f00fb59f9fc792f509194432e9e6d0b6d",
27+
"commit": "cceaf3628da950621004d9b5d8c1d1f367073347",
2828
"path": "FileAPI"
2929
},
3030
"hr-time": {

0 commit comments

Comments
 (0)