Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implementation of MultipartFormData streaming encoder #200

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions builtins/web/fetch/request-response.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "request-response.h"

#include "../blob.h"
#include "../form-data/form-data.h"
#include "../form-data/form-data-encoder.h"
#include "../streams/native-stream-source.h"
#include "../streams/transform-stream.h"
#include "../url.h"
Expand All @@ -27,8 +29,6 @@
#include "js/experimental/TypedData.h"
#pragma clang diagnostic pop

using builtins::web::blob::Blob;

namespace builtins::web::streams {

bool NativeStreamSource::stream_is_body(JSContext *cx, JS::HandleObject stream) {
Expand All @@ -41,6 +41,10 @@ bool NativeStreamSource::stream_is_body(JSContext *cx, JS::HandleObject stream)

namespace builtins::web::fetch {

using blob::Blob;
using form_data::FormData;
using form_data::MultipartFormData;

static api::Engine *ENGINE;

bool error_stream_controller_with_pending_exception(JSContext *cx, HandleObject stream) {
Expand Down Expand Up @@ -293,6 +297,7 @@ bool RequestOrResponse::extract_body(JSContext *cx, JS::HandleObject self,
// - byte sequence
// - buffer source
// - Blob
// - FormData
// - USV strings
// - URLSearchParams
// - ReadableStream
Expand Down Expand Up @@ -320,6 +325,24 @@ bool RequestOrResponse::extract_body(JSContext *cx, JS::HandleObject self,
MOZ_ASSERT(host_type_str);
content_type = host_type_str.ptr.get();
}
} else if (FormData::is_instance(body_obj)) {
RootedObject encoder(cx, MultipartFormData::create(cx, body_obj));
if (!encoder) {
return false;
}

RootedObject stream(cx, MultipartFormData::encode_stream(cx, encoder));
if (!stream) {
return false;
}

auto boundary = MultipartFormData::boundary(encoder);
auto type = "multipart/form-data; boundary=" + boundary;
host_type_str = type.c_str();
content_type = host_type_str.ptr.get();

RootedValue stream_val(cx, JS::ObjectValue(*stream));
JS_SetReservedSlot(self, static_cast<uint32_t>(RequestOrResponse::Slots::BodyStream), stream_val);
} else if (body_obj && JS::IsReadableStream(body_obj)) {
if (RequestOrResponse::body_unusable(cx, body_obj)) {
return api::throw_error(cx, FetchErrors::BodyStreamUnusable);
Expand Down
5 changes: 5 additions & 0 deletions builtins/web/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ bool File::lastModified_get(JSContext *cx, unsigned argc, JS::Value *vp) {
return true;
}

JSString *File::name(JSObject *self) {
MOZ_ASSERT(is_instance(self));
return JS::GetReservedSlot(self, static_cast<size_t>(Slots::Name)).toString();
}

// https://w3c.github.io/FileAPI/#file-constructor
bool File::init(JSContext *cx, HandleObject self, HandleValue fileBits, HandleValue fileName,
HandleValue opts) {
Expand Down
2 changes: 2 additions & 0 deletions builtins/web/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class File : public BuiltinImpl<File> {
static const JSFunctionSpec methods[];
static const JSPropertySpec properties[];

static JSString *name(JSObject *self);

static JSObject *create(JSContext *cx, HandleValue fileBits, HandleValue fileName, HandleValue opts);
static bool init(JSContext *cx, HandleObject self, HandleValue fileBits, HandleValue fileName, HandleValue opts);
static bool init_class(JSContext *cx, HandleObject global);
Expand Down
Loading
Loading