Skip to content

Commit 1d59345

Browse files
KhafraDevRafaelGSS
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 e45cc2a commit 1d59345

File tree

8 files changed

+81
-5
lines changed

8 files changed

+81
-5
lines changed

lib/internal/blob.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
ObjectDefineProperties,
88
ObjectDefineProperty,
99
ObjectSetPrototypeOf,
10+
PromisePrototypeThen,
1011
PromiseReject,
1112
RegExpPrototypeExec,
1213
RegExpPrototypeSymbolReplace,
@@ -311,6 +312,15 @@ class Blob {
311312
return dec.decode(await this.arrayBuffer());
312313
}
313314

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

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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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
Lines changed: 45 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 12 additions & 1 deletion
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

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

test/fixtures/wpt/versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"path": "fetch/data-urls/resources"
2929
},
3030
"FileAPI": {
31-
"commit": "e36dbb6f00fb59f9fc792f509194432e9e6d0b6d",
31+
"commit": "cceaf3628da950621004d9b5d8c1d1f367073347",
3232
"path": "FileAPI"
3333
},
3434
"hr-time": {

0 commit comments

Comments
 (0)