mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
buffer: add .bytes() method to Blob
PR-URL: https://github.com/nodejs/node/pull/53221 Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
This commit is contained in:
@@ -7,6 +7,7 @@ const {
|
||||
ObjectDefineProperties,
|
||||
ObjectDefineProperty,
|
||||
ObjectSetPrototypeOf,
|
||||
PromisePrototypeThen,
|
||||
PromiseReject,
|
||||
RegExpPrototypeExec,
|
||||
RegExpPrototypeSymbolReplace,
|
||||
@@ -311,6 +312,15 @@ class Blob {
|
||||
return dec.decode(await this.arrayBuffer());
|
||||
}
|
||||
|
||||
bytes() {
|
||||
if (!isBlob(this))
|
||||
throw new ERR_INVALID_THIS('Blob');
|
||||
|
||||
return PromisePrototypeThen(
|
||||
this.arrayBuffer(),
|
||||
(buffer) => new Uint8Array(buffer));
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {ReadableStream}
|
||||
*/
|
||||
|
||||
@@ -27,6 +27,7 @@ promise_test(async () => {
|
||||
|
||||
assert_equals(await slicedBlob.text(), "oo");
|
||||
assert_equals(charCodeBufferToString(await slicedBlob.arrayBuffer()), "oo");
|
||||
assert_equals(charCodeArrayToString(await slicedBlob.bytes()), "oo");
|
||||
|
||||
const reader = slicedBlob.stream().getReader();
|
||||
const { value } = await reader.read();
|
||||
@@ -48,6 +49,14 @@ promise_test(async () => {
|
||||
assert_equals(charCodeBufferToString(charCodeBuffer), "bar");
|
||||
}, "arrayBuffer()");
|
||||
|
||||
promise_test(async () => {
|
||||
const { bytes } = await BlobPrototypeFromDetachedFramePromise;
|
||||
const blob = new Blob(["bar"]);
|
||||
|
||||
const charCodeBytes = await bytes.call(blob);
|
||||
assert_equals(charCodeArrayToString(charCodeBytes), "bar");
|
||||
}, "bytes()");
|
||||
|
||||
promise_test(async () => {
|
||||
const { stream } = await BlobPrototypeFromDetachedFramePromise;
|
||||
const blob = new Blob(["baz"]);
|
||||
|
||||
2
test/fixtures/wpt/FileAPI/META.yml
vendored
2
test/fixtures/wpt/FileAPI/META.yml
vendored
@@ -1,6 +1,6 @@
|
||||
spec: https://w3c.github.io/FileAPI/
|
||||
suggested_reviewers:
|
||||
- inexorabletash
|
||||
- zqzhang
|
||||
- jdm
|
||||
- mkruisselbrink
|
||||
- annevk
|
||||
|
||||
45
test/fixtures/wpt/FileAPI/blob/Blob-bytes.any.js
vendored
Normal file
45
test/fixtures/wpt/FileAPI/blob/Blob-bytes.any.js
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
// META: title=Blob bytes()
|
||||
// META: script=../support/Blob.js
|
||||
'use strict';
|
||||
|
||||
promise_test(async () => {
|
||||
const input_arr = new TextEncoder().encode("PASS");
|
||||
const blob = new Blob([input_arr]);
|
||||
const uint8array = await blob.bytes();
|
||||
assert_true(uint8array instanceof Uint8Array);
|
||||
assert_equals_typed_array(uint8array, input_arr);
|
||||
}, "Blob.bytes()")
|
||||
|
||||
promise_test(async () => {
|
||||
const input_arr = new TextEncoder().encode("");
|
||||
const blob = new Blob([input_arr]);
|
||||
const uint8array = await blob.bytes();
|
||||
assert_true(uint8array instanceof Uint8Array);
|
||||
assert_equals_typed_array(uint8array, input_arr);
|
||||
}, "Blob.bytes() empty Blob data")
|
||||
|
||||
promise_test(async () => {
|
||||
const input_arr = new TextEncoder().encode("\u08B8\u000a");
|
||||
const blob = new Blob([input_arr]);
|
||||
const uint8array = await blob.bytes();
|
||||
assert_equals_typed_array(uint8array, input_arr);
|
||||
}, "Blob.bytes() non-ascii input")
|
||||
|
||||
promise_test(async () => {
|
||||
const input_arr = [8, 241, 48, 123, 151];
|
||||
const typed_arr = new Uint8Array(input_arr);
|
||||
const blob = new Blob([typed_arr]);
|
||||
const uint8array = await blob.bytes();
|
||||
assert_equals_typed_array(uint8array, typed_arr);
|
||||
}, "Blob.bytes() non-unicode input")
|
||||
|
||||
promise_test(async () => {
|
||||
const input_arr = new TextEncoder().encode("PASS");
|
||||
const blob = new Blob([input_arr]);
|
||||
const uint8array_results = await Promise.all([blob.bytes(),
|
||||
blob.bytes(), blob.bytes()]);
|
||||
for (let uint8array of uint8array_results) {
|
||||
assert_true(uint8array instanceof Uint8Array);
|
||||
assert_equals_typed_array(uint8array, input_arr);
|
||||
}
|
||||
}, "Blob.bytes() concurrent reads")
|
||||
@@ -290,10 +290,11 @@ test_blob(function() {
|
||||
new Int16Array([0x4150, 0x5353]),
|
||||
new Uint32Array([0x53534150]),
|
||||
new Int32Array([0x53534150]),
|
||||
new Float16Array([2.65625, 58.59375]),
|
||||
new Float32Array([0xD341500000])
|
||||
]);
|
||||
}, {
|
||||
expected: "PASSPASSPASSPASSPASSPASSPASS",
|
||||
expected: "PASSPASSPASSPASSPASSPASSPASSPASS",
|
||||
type: "",
|
||||
desc: "Passing typed arrays as elements of the blobParts array should work."
|
||||
});
|
||||
|
||||
@@ -70,7 +70,18 @@ promise_test(async() => {
|
||||
await garbageCollect();
|
||||
const chunks = await read_all_chunks(stream, { perform_gc: true });
|
||||
assert_array_equals(chunks, input_arr);
|
||||
}, "Blob.stream() garbage collection of blob shouldn't break stream" +
|
||||
}, "Blob.stream() garbage collection of blob shouldn't break stream " +
|
||||
"consumption")
|
||||
|
||||
promise_test(async() => {
|
||||
const input_arr = [8, 241, 48, 123, 151];
|
||||
const typed_arr = new Uint8Array(input_arr);
|
||||
let blob = new Blob([typed_arr]);
|
||||
const chunksPromise = read_all_chunks(blob.stream());
|
||||
// It somehow matters to do GC here instead of doing `perform_gc: true`
|
||||
await garbageCollect();
|
||||
assert_array_equals(await chunksPromise, input_arr);
|
||||
}, "Blob.stream() garbage collection of stream shouldn't break stream " +
|
||||
"consumption")
|
||||
|
||||
promise_test(async () => {
|
||||
|
||||
2
test/fixtures/wpt/README.md
vendored
2
test/fixtures/wpt/README.md
vendored
@@ -17,7 +17,7 @@ Last update:
|
||||
- dom/events: https://github.com/web-platform-tests/wpt/tree/ab8999891c/dom/events
|
||||
- encoding: https://github.com/web-platform-tests/wpt/tree/a58bbf6d8c/encoding
|
||||
- fetch/data-urls/resources: https://github.com/web-platform-tests/wpt/tree/7c79d998ff/fetch/data-urls/resources
|
||||
- FileAPI: https://github.com/web-platform-tests/wpt/tree/e36dbb6f00/FileAPI
|
||||
- FileAPI: https://github.com/web-platform-tests/wpt/tree/cceaf3628d/FileAPI
|
||||
- hr-time: https://github.com/web-platform-tests/wpt/tree/34cafd797e/hr-time
|
||||
- html/webappapis/atob: https://github.com/web-platform-tests/wpt/tree/f267e1dca6/html/webappapis/atob
|
||||
- html/webappapis/microtask-queuing: https://github.com/web-platform-tests/wpt/tree/2c5c3c4c27/html/webappapis/microtask-queuing
|
||||
|
||||
2
test/fixtures/wpt/versions.json
vendored
2
test/fixtures/wpt/versions.json
vendored
@@ -28,7 +28,7 @@
|
||||
"path": "fetch/data-urls/resources"
|
||||
},
|
||||
"FileAPI": {
|
||||
"commit": "e36dbb6f00fb59f9fc792f509194432e9e6d0b6d",
|
||||
"commit": "cceaf3628da950621004d9b5d8c1d1f367073347",
|
||||
"path": "FileAPI"
|
||||
},
|
||||
"hr-time": {
|
||||
|
||||
Reference in New Issue
Block a user