[Fetch API] Add support for consuming a Request ReadableStream body
https://bugs.webkit.org/show_bug.cgi?id=176182
Patch by Youenn Fablet <youenn@apple.com> on 2017-09-01
Reviewed by Alex Christensen.
Source/WebCore:
Tests: http/wpt/fetch/request-stream-empty.html
http/wpt/fetch/request-stream-error.html
Adding a ReadableStreamSink which allows to pipe all ReadableStream data back to C++ code.
This sink only takes BufferSource as the only user is Fetch which has this restriction.
This sink is tied to the readableStreamPipeTo builtin internal function that reads all data from the ReadableStream
and send it to the sink.
Adding a ReadableStreamToSharedBufferSink specialization to get the data as a SharedBuffer.
Use that class in FetchBodyConsumer to get the data back from the ReadableStream and transform it according
the desired body type (text, JSON, ArrayBuffer).
* CMakeLists.txt:
* DerivedSources.make:
* Modules/fetch/FetchBody.cpp:
(WebCore::FetchBody::consume):
(WebCore::FetchBody::loadingSucceeded):
* Modules/fetch/FetchBodyConsumer.cpp:
(WebCore::resolveWithTypeAndData):
(WebCore::FetchBodyConsumer::clean):
(WebCore::FetchBodyConsumer::resolveWithData):
(WebCore::FetchBodyConsumer::resolve):
* Modules/fetch/FetchBodyConsumer.h:
* Modules/fetch/FetchResponse.cpp:
(WebCore::FetchResponse::finishConsumingStream):
* Modules/streams/ReadableStreamInternals.js:
(readableStreamPipeTo):
* Modules/streams/ReadableStreamSink.cpp: Added.
(WebCore::ReadableStreamToSharedBufferSink::ReadableStreamToSharedBufferSink):
(WebCore::ReadableStreamToSharedBufferSink::pipeFrom):
(WebCore::ReadableStreamToSharedBufferSink::enqueue):
(WebCore::ReadableStreamToSharedBufferSink::close):
(WebCore::ReadableStreamToSharedBufferSink::error):
* Modules/streams/ReadableStreamSink.h: Added.
* Modules/streams/ReadableStreamSink.idl: Added.
* WebCore.xcodeproj/project.pbxproj:
* bindings/js/ReadableStream.cpp:
(WebCore::ReadableStream::pipeTo):
* bindings/js/ReadableStream.h:
LayoutTests:
* http/wpt/fetch/request-stream-disturbed-1-expected.txt:
* http/wpt/fetch/request-stream-disturbed-2-expected.txt:
* http/wpt/fetch/request-stream-disturbed-3-expected.txt:
* http/wpt/fetch/request-stream-empty-expected.txt: Added.
* http/wpt/fetch/request-stream-empty.html: Added.
* http/wpt/fetch/request-stream-error-expected.txt: Added.
* http/wpt/fetch/request-stream-error.html: Added.
git-svn-id: http://svn.webkit.org/repository/webkit/trunk@221504 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/bindings/js/ReadableStream.cpp b/Source/WebCore/bindings/js/ReadableStream.cpp
index 722740f..cf48697 100644
--- a/Source/WebCore/bindings/js/ReadableStream.cpp
+++ b/Source/WebCore/bindings/js/ReadableStream.cpp
@@ -27,6 +27,7 @@
#include "ReadableStream.h"
#include "JSDOMConvertSequences.h"
+#include "JSReadableStreamSink.h"
#include "JSReadableStreamSource.h"
#include "WebCoreJSClientData.h"
@@ -54,6 +55,24 @@
return create(globalObject, *newReadableStream);
}
+void ReadableStream::pipeTo(ReadableStreamSink& sink)
+{
+ auto& state = *m_globalObject->globalExec();
+ JSVMClientData* clientData = static_cast<JSVMClientData*>(state.vm().clientData);
+ const Identifier& privateName = clientData->builtinFunctions().readableStreamInternalsBuiltins().readableStreamPipeToPrivateName();
+
+ auto readableStreamPipeTo = m_globalObject->get(&state, privateName);
+ ASSERT(readableStreamPipeTo.isFunction());
+
+ CallData callData;
+ CallType callType = getCallData(readableStreamPipeTo, callData);
+ ASSERT(callType != JSC::CallType::None);
+ MarkedArgumentBuffer arguments;
+ arguments.append(readableStream());
+ arguments.append(toJS(&state, m_globalObject.get(), sink));
+ JSC::call(&state, readableStreamPipeTo, callType, callData, JSC::jsUndefined(), arguments);
+}
+
std::pair<Ref<ReadableStream>, Ref<ReadableStream>> ReadableStream::tee()
{
auto& state = *m_globalObject->globalExec();