Skip to content

Commit

Permalink
Add FormData streaming encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiltd committed Jan 23, 2025
1 parent ca8fee8 commit 655b57d
Show file tree
Hide file tree
Showing 11 changed files with 669 additions and 8 deletions.
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 = std::string_view(type);
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

0 comments on commit 655b57d

Please sign in to comment.