Skip to content

Commit d356276

Browse files
committed
Add bytes() to Blob and PushMessageData
https://bugs.webkit.org/show_bug.cgi?id=274119 rdar://128418858 Reviewed by Youenn Fablet. This implements w3c/FileAPI#198 and w3c/push-api#370 creating parity for these APIs with Fetch's Body. This incorporates the tests from web-platform-tests/wpt#46232 modulo a typo fix. PushMessageData test coverage is done through a local test that would be good to upstream at some point. * LayoutTests/http/wpt/push-api/pushEvent.any.js: (test): * LayoutTests/imported/w3c/web-platform-tests/FileAPI/Blob-methods-from-detached-frame-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/FileAPI/Blob-methods-from-detached-frame.html: * LayoutTests/imported/w3c/web-platform-tests/FileAPI/blob/Blob-bytes.any-expected.txt: Added. * LayoutTests/imported/w3c/web-platform-tests/FileAPI/blob/Blob-bytes.any.html: Added. * LayoutTests/imported/w3c/web-platform-tests/FileAPI/blob/Blob-bytes.any.js: Added. (string_appeared_here.promise_test.async const): * LayoutTests/imported/w3c/web-platform-tests/FileAPI/blob/Blob-bytes.any.worker-expected.txt: Added. * LayoutTests/imported/w3c/web-platform-tests/FileAPI/blob/Blob-bytes.any.worker.html: Added. * LayoutTests/imported/w3c/web-platform-tests/FileAPI/blob/w3c-import.log: * Source/WebCore/Modules/push-api/PushMessageData.cpp: (WebCore::PushMessageData::arrayBuffer): (WebCore::PushMessageData::bytes): * Source/WebCore/Modules/push-api/PushMessageData.h: * Source/WebCore/Modules/push-api/PushMessageData.idl: * Source/WebCore/fileapi/Blob.cpp: (WebCore::Blob::arrayBuffer): (WebCore::Blob::bytes): * Source/WebCore/fileapi/Blob.h: * Source/WebCore/fileapi/Blob.idl: Canonical link: https://commits.webkit.org/279263@main
1 parent 083dc3e commit d356276

15 files changed

+123
-15
lines changed

LayoutTests/http/wpt/push-api/pushEvent.any.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ test(() => {
2727
assert_true(data instanceof PushMessageData);
2828
assert_equals(data.text(), stringValue);
2929
assert_true(data.blob() instanceof Blob);
30+
assert_true(data.bytes() instanceof Uint8Array);
3031
assert_equals(data.json().test, 1);
3132
assert_equals(data.arrayBuffer().byteLength, stringValue.length);
33+
assert_equals(data.bytes().length, stringValue.length);
34+
assert_not_equals(data.bytes().buffer, data.arrayBuffer());
35+
assert_not_equals(data.bytes(), data.bytes());
3236
}
3337
}, "PushEvent with data");
3438

LayoutTests/imported/w3c/web-platform-tests/FileAPI/Blob-methods-from-detached-frame-expected.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
PASS slice()
33
PASS text()
44
PASS arrayBuffer()
5+
PASS bytes()
56
PASS stream()
67

LayoutTests/imported/w3c/web-platform-tests/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"]);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
PASS Blob.bytes()
3+
PASS Blob.bytes() empty Blob data
4+
PASS Blob.bytes() non-ascii input
5+
PASS Blob.bytes() non-unicode input
6+
PASS Blob.bytes() concurrent reads
7+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- This file is required for WebKit test infrastructure to run the templated test -->
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")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
PASS Blob.bytes()
3+
PASS Blob.bytes() empty Blob data
4+
PASS Blob.bytes() non-ascii input
5+
PASS Blob.bytes() non-unicode input
6+
PASS Blob.bytes() concurrent reads
7+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- This file is required for WebKit test infrastructure to run the templated test -->

LayoutTests/imported/w3c/web-platform-tests/FileAPI/blob/w3c-import.log

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ None
1515
------------------------------------------------------------------------
1616
List of files:
1717
/LayoutTests/imported/w3c/web-platform-tests/FileAPI/blob/Blob-array-buffer.any.js
18+
/LayoutTests/imported/w3c/web-platform-tests/FileAPI/blob/Blob-bytes.any.js
1819
/LayoutTests/imported/w3c/web-platform-tests/FileAPI/blob/Blob-constructor-dom.window.js
1920
/LayoutTests/imported/w3c/web-platform-tests/FileAPI/blob/Blob-constructor-endings.html
2021
/LayoutTests/imported/w3c/web-platform-tests/FileAPI/blob/Blob-constructor.any.js

Source/WebCore/Modules/push-api/PushMessageData.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 Apple Inc. All rights reserved.
2+
* Copyright (C) 2021-2024 Apple Inc. All rights reserved.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -40,7 +40,7 @@ WTF_MAKE_ISO_ALLOCATED_IMPL(PushMessageData);
4040

4141
ExceptionOr<RefPtr<JSC::ArrayBuffer>> PushMessageData::arrayBuffer()
4242
{
43-
auto buffer = ArrayBuffer::tryCreate(m_data.data(), m_data.size());
43+
auto buffer = ArrayBuffer::tryCreate(m_data.span());
4444
if (!buffer)
4545
return Exception { ExceptionCode::OutOfMemoryError };
4646
return buffer;
@@ -51,6 +51,14 @@ RefPtr<Blob> PushMessageData::blob(ScriptExecutionContext& context)
5151
return Blob::create(&context, Vector<uint8_t> { m_data }, { });
5252
}
5353

54+
ExceptionOr<RefPtr<JSC::Uint8Array>> PushMessageData::bytes()
55+
{
56+
auto view = Uint8Array::tryCreate(m_data.span());
57+
if (!view)
58+
return Exception { ExceptionCode::OutOfMemoryError };
59+
return view;
60+
}
61+
5462
ExceptionOr<JSC::JSValue> PushMessageData::json(JSDOMGlobalObject& globalObject)
5563
{
5664
JSC::JSLockHolder lock(&globalObject);

0 commit comments

Comments
 (0)