Skip to content
Closed
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
5 changes: 5 additions & 0 deletions src/jsc/FetchHeaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ unsafe extern "C" {
// safe: `FetchHeaders` is an `opaque_ffi!` ZST handle; `&mut` is ABI-identical
// to a non-null `*mut` and the C++ refcount decrement is interior to the cell.
safe fn WebCore__FetchHeaders__deref(arg0: &mut FetchHeaders);
safe fn WebCore__FetchHeaders__ref(arg0: &mut FetchHeaders);
safe fn WebCore__FetchHeaders__fastGet_(arg0: &FetchHeaders, arg1: u8, arg2: &mut EncodedSlice);
safe fn WebCore__FetchHeaders__fastHas_(arg0: &FetchHeaders, arg1: u8) -> bool;
safe fn WebCore__FetchHeaders__fastRemove_(arg0: &FetchHeaders, arg1: u8);
Expand Down Expand Up @@ -293,6 +294,10 @@ impl FetchHeaders {
})
}

pub fn ref_(&mut self) {
WebCore__FetchHeaders__ref(self)
}

pub fn deref(&mut self) {
WebCore__FetchHeaders__deref(self)
}
Expand Down
18 changes: 14 additions & 4 deletions src/jsc/JSGlobalObject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,8 +1144,15 @@ impl JSGlobalObject {
crate::call_zero_is_throw(self, || ZigGlobalObject__readableStreamToJSON(self, value))
}

pub fn readable_stream_to_blob(&self, value: JSValue) -> JsResult<JSValue> {
crate::call_zero_is_throw(self, || ZigGlobalObject__readableStreamToBlob(self, value))
/// `content_type`: a JS string for the resulting Blob's `type`, or `undefined`.
pub fn readable_stream_to_blob(
&self,
value: JSValue,
content_type: JSValue,
) -> JsResult<JSValue> {
crate::call_zero_is_throw(self, || {
ZigGlobalObject__readableStreamToBlob(self, value, content_type)
})
}

pub fn readable_stream_to_form_data(
Expand Down Expand Up @@ -1565,8 +1572,11 @@ unsafe extern "C" {
value: JSValue,
content_type: JSValue,
) -> JSValue;
safe fn ZigGlobalObject__readableStreamToBlob(this: &JSGlobalObject, value: JSValue)
-> JSValue;
safe fn ZigGlobalObject__readableStreamToBlob(
this: &JSGlobalObject,
value: JSValue,
content_type: JSValue,
) -> JSValue;

safe fn ZigGlobalObject__makeNapiEnvForFFI(this: &JSGlobalObject) -> *mut c_void;

Expand Down
2 changes: 1 addition & 1 deletion src/jsc/bindings/ZigGlobalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,6 @@ extern "C" JSC::EncodedJSValue ZigGlobalObject__readableStreamToText(Zig::Global
extern "C" JSC::EncodedJSValue ZigGlobalObject__readableStreamToArrayBuffer(Zig::GlobalObject* globalObject, JSC::EncodedJSValue readableStreamValue);
extern "C" JSC::EncodedJSValue ZigGlobalObject__readableStreamToBytes(Zig::GlobalObject* globalObject, JSC::EncodedJSValue readableStreamValue);
extern "C" JSC::EncodedJSValue ZigGlobalObject__readableStreamToJSON(Zig::GlobalObject* globalObject, JSC::EncodedJSValue readableStreamValue);
extern "C" JSC::EncodedJSValue ZigGlobalObject__readableStreamToBlob(Zig::GlobalObject* globalObject, JSC::EncodedJSValue readableStreamValue);
extern "C" JSC::EncodedJSValue ZigGlobalObject__readableStreamToBlob(Zig::GlobalObject* globalObject, JSC::EncodedJSValue readableStreamValue, JSC::EncodedJSValue contentType);

#endif
4 changes: 4 additions & 0 deletions src/jsc/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2595,6 +2595,10 @@ void WebCore__FetchHeaders__deref(WebCore::FetchHeaders* arg0)
{
arg0->deref();
}
void WebCore__FetchHeaders__ref(WebCore::FetchHeaders* arg0)
{
arg0->ref();
}

WebCore::FetchHeaders* WebCore__FetchHeaders__createValueNotJS(JSC::JSGlobalObject* arg0, StringPointer* arg1, StringPointer* arg2, const EncodedSlice* arg3, uint32_t count)
{
Expand Down
1 change: 1 addition & 0 deletions src/jsc/bindings/headers.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 49 additions & 7 deletions src/jsc/bindings/webcore/streams/BunStreamConsumers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
#include <wtf/Vector.h>
#include <wtf/text/StringBuilder.h>

// Body.rs
extern "C" void Body__setBlobContentType(JSC::EncodedJSValue blob, const uint8_t* contentType, size_t length);

namespace WebCore {

using namespace JSC;
Expand Down Expand Up @@ -1272,28 +1275,41 @@ JSValue readableStreamToJSON(JSGlobalObject* globalObject, WebCore::JSReadableSt
return derived;
}

JSValue readableStreamToBlob(JSGlobalObject* globalObject, WebCore::JSReadableStream* stream)
JSValue readableStreamToBlob(JSGlobalObject* globalObject, WebCore::JSReadableStream* stream, JSValue contentType)
{
auto& vm = getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
if (isReadableStreamLocked(stream))
RELEASE_AND_RETURN(scope, promiseRejectedWith(globalObject, createLockedError(globalObject)));
if (stream->m_disturbed)
RELEASE_AND_RETURN(scope, promiseRejectedWith(globalObject, createAlreadyUsedError(globalObject)));
if (!contentType.isString() || !asString(contentType)->length())
contentType = jsUndefined();
auto* runtime = JSStreamsRuntime::from(globalObject);
JSValue fastPath = tryUseReadableStreamBufferedFastPath(globalObject, stream, builtinNames(vm).blobPublicName());
RETURN_IF_EXCEPTION(scope, {});
if (fastPath)
return fastPath;
if (fastPath) {
if (contentType.isUndefined())
return fastPath;
// The native handle built the Blob without knowing the body's Content-Type.
auto* blobPromise = dynamicDowncast<JSPromise>(fastPath);
if (!blobPromise) [[unlikely]] {
blobPromise = promiseFulfilledWith(globalObject, fastPath);
RETURN_IF_EXCEPTION(scope, {});
}
auto* derived = JSPromise::create(vm, globalObject->promiseStructure());
blobPromise->performPromiseThenWithContext(vm, globalObject, runtime->onReadableStreamToBlobSetType(), jsUndefined(), derived, contentType);
return derived;
}
JSValue arrayResult = readableStreamToArray(globalObject, stream);
RETURN_IF_EXCEPTION(scope, {});
auto* arrayPromise = dynamicDowncast<JSPromise>(arrayResult);
if (!arrayPromise) [[unlikely]] {
arrayPromise = promiseFulfilledWith(globalObject, arrayResult);
RETURN_IF_EXCEPTION(scope, {});
}
auto* runtime = JSStreamsRuntime::from(globalObject);
auto* derived = JSPromise::create(vm, globalObject->promiseStructure());
arrayPromise->performPromiseThenWithContext(vm, globalObject, runtime->onReadableStreamToBlobFulfilled(), jsUndefined(), derived, jsUndefined());
arrayPromise->performPromiseThenWithContext(vm, globalObject, runtime->onReadableStreamToBlobFulfilled(), jsUndefined(), derived, contentType);
return derived;
}

Expand All @@ -1305,7 +1321,8 @@ JSValue readableStreamToFormData(JSGlobalObject* globalObject, WebCore::JSReadab
RELEASE_AND_RETURN(scope, promiseRejectedWith(globalObject, createLockedError(globalObject)));
if (stream->m_disturbed)
RELEASE_AND_RETURN(scope, promiseRejectedWith(globalObject, createAlreadyUsedError(globalObject)));
JSValue blobResult = readableStreamToBlob(globalObject, stream);
// FormData.from() below takes the content type itself; the intermediate Blob does not need it.
JSValue blobResult = readableStreamToBlob(globalObject, stream, jsUndefined());
RETURN_IF_EXCEPTION(scope, {});
auto* blobPromise = dynamicDowncast<JSPromise>(blobResult);
if (!blobPromise) [[unlikely]] {
Expand Down Expand Up @@ -1391,7 +1408,7 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionReadableStreamToBlob, (JSGlobalObject * globa
auto* stream = dynamicDowncast<JSReadableStream>(streamValue);
if (!stream) [[unlikely]]
return Bun::ERR::INVALID_ARG_TYPE(scope, globalObject, "stream"_s, "ReadableStream"_s, streamValue);
RELEASE_AND_RETURN(scope, JSValue::encode(Bun::WebStreams::readableStreamToBlob(globalObject, stream)));
RELEASE_AND_RETURN(scope, JSValue::encode(Bun::WebStreams::readableStreamToBlob(globalObject, stream, jsUndefined())));
}

JSC_DEFINE_HOST_FUNCTION(jsFunctionReadableStreamToFormData, (JSGlobalObject * globalObject, CallFrame* callFrame))
Expand Down Expand Up @@ -1489,6 +1506,19 @@ JSC_DEFINE_HOST_FUNCTION(jsWebStreamsHandler_onReadableStreamToJSONFulfilled, (J
RELEASE_AND_RETURN(scope, JSValue::encode(JSONParseWithException(globalObject, text)));
}

// Through Body.rs, not `new Blob([], { type })`: same MIME rules as the other body readers.
static void setBodyBlobContentType(JSGlobalObject* globalObject, JSValue blob, JSValue contentType)
{
auto& vm = getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
if (!contentType.isString())
return;
auto string = contentType.toWTFString(globalObject);
RETURN_IF_EXCEPTION(scope, );
auto utf8 = string.utf8();
Body__setBlobContentType(JSValue::encode(blob), reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
}

JSC_DEFINE_HOST_FUNCTION(jsWebStreamsHandler_onReadableStreamToBlobFulfilled, (JSGlobalObject * globalObject, CallFrame* callFrame))
{
auto& vm = getVM(globalObject);
Expand All @@ -1497,11 +1527,23 @@ JSC_DEFINE_HOST_FUNCTION(jsWebStreamsHandler_onReadableStreamToBlobFulfilled, (J
arguments.append(callFrame->argument(0));
JSObject* blob = JSC::construct(globalObject, defaultGlobalObject(globalObject)->JSBlobConstructor(), arguments, "Blob is not constructible"_s);
RETURN_IF_EXCEPTION(scope, {});
setBodyBlobContentType(globalObject, blob, callFrame->argument(1));
RETURN_IF_EXCEPTION(scope, {});
Bun::WebStreams::releaseInternalChunkArray(globalObject, callFrame->argument(0));
RETURN_IF_EXCEPTION(scope, {});
return JSValue::encode(blob);
}

JSC_DEFINE_HOST_FUNCTION(jsWebStreamsHandler_onReadableStreamToBlobSetType, (JSGlobalObject * globalObject, CallFrame* callFrame))
{
auto& vm = getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue blob = callFrame->argument(0);
setBodyBlobContentType(globalObject, blob, callFrame->argument(1));
RETURN_IF_EXCEPTION(scope, {});
return JSValue::encode(blob);
}

JSC_DEFINE_HOST_FUNCTION(jsWebStreamsHandler_onReadableStreamToFormDataFulfilled, (JSGlobalObject * globalObject, CallFrame* callFrame))
{
auto& vm = getVM(globalObject);
Expand Down
2 changes: 1 addition & 1 deletion src/jsc/bindings/webcore/streams/JSReadableStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ JSC_DEFINE_HOST_FUNCTION(jsReadableStreamPrototypeFunction_blob, (JSGlobalObject
auto* stream = dynamicDowncast<JSReadableStream>(callFrame->thisValue());
if (!stream) [[unlikely]]
return Bun::ERR::INVALID_THIS(scope, lexicalGlobalObject, "ReadableStream"_s);
RELEASE_AND_RETURN(scope, JSValue::encode(readableStreamToBlob(lexicalGlobalObject, stream)));
RELEASE_AND_RETURN(scope, JSValue::encode(readableStreamToBlob(lexicalGlobalObject, stream, jsUndefined())));
}

// Bun private-name accessor ($bunNativePtr).
Expand Down
2 changes: 2 additions & 0 deletions src/jsc/bindings/webcore/streams/JSStreamsRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ namespace WebCore {
// onReadableStreamTo*Fulfilled: the generic-path promise chains
// (toArrayBuffer/toBytes/toBlob: value = the chunk array; toJSON: value = the text;
// toFormData: value = the Blob, context = the contentType JSString).
// onReadableStreamToBlob{Fulfilled,SetType}: context = the body's contentType JSString or undefined.
// onIntoArrayReadMany*: readableStreamIntoArray's readMany() continuation (readMany may
// return a Promise); context = an InternalFieldTuple{reader, resultArray}.
// onDirectConsumeLoopRead*: the readableStreamTo{Text,Array}Direct read loop;
Expand All @@ -205,6 +206,7 @@ namespace WebCore {
V(onReadableStreamToTextChunksFulfilled) \
V(onReadableStreamToJSONFulfilled) \
V(onReadableStreamToBlobFulfilled) \
V(onReadableStreamToBlobSetType) /* buffered fast path: give the native Blob the body's contentType */ \
V(onReadableStreamToFormDataFulfilled) \
V(onIntoArrayReadManyFulfilled) /* append value; !done => readMany() again; done => release + resolve */ \
V(onIntoArrayReadManyRejected) /* release the reader, reject the result promise */ \
Expand Down
4 changes: 2 additions & 2 deletions src/jsc/bindings/webcore/streams/WebStreamsExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,13 @@ extern "C" JSC::EncodedJSValue ZigGlobalObject__readableStreamToJSON(Zig::Global
RELEASE_AND_RETURN(scope, JSValue::encode(readableStreamToJSON(globalObject, stream)));
}

extern "C" JSC::EncodedJSValue ZigGlobalObject__readableStreamToBlob(Zig::GlobalObject* globalObject, JSC::EncodedJSValue streamValue)
extern "C" JSC::EncodedJSValue ZigGlobalObject__readableStreamToBlob(Zig::GlobalObject* globalObject, JSC::EncodedJSValue streamValue, JSC::EncodedJSValue contentType)
{
auto& vm = JSC::getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
auto* stream = toReadableStream(globalObject, scope, streamValue);
RETURN_IF_EXCEPTION(scope, {});
RELEASE_AND_RETURN(scope, JSValue::encode(readableStreamToBlob(globalObject, stream)));
RELEASE_AND_RETURN(scope, JSValue::encode(readableStreamToBlob(globalObject, stream, JSValue::decode(contentType))));
}

extern "C" JSC::EncodedJSValue ZigGlobalObject__readableStreamToFormData(Zig::GlobalObject* globalObject, JSC::EncodedJSValue streamValue, JSC::EncodedJSValue contentType)
Expand Down
5 changes: 3 additions & 2 deletions src/jsc/bindings/webcore/streams/WebStreamsInternals.h
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,8 @@ JSC::JSValue readableStreamToArray(JSC::JSGlobalObject*, JSReadableStream*); //
JSC::JSValue readableStreamToArrayBuffer(JSC::JSGlobalObject*, JSReadableStream*); // userJS: yes — BunStreamConsumers.cpp
JSC::JSValue readableStreamToBytes(JSC::JSGlobalObject*, JSReadableStream*); // userJS: yes — BunStreamConsumers.cpp
JSC::JSValue readableStreamToJSON(JSC::JSGlobalObject*, JSReadableStream*); // userJS: yes — BunStreamConsumers.cpp
JSC::JSValue readableStreamToBlob(JSC::JSGlobalObject*, JSReadableStream*); // userJS: yes — BunStreamConsumers.cpp
// `contentType`: a JSString to use as the resulting Blob's `type`, or undefined.
JSC::JSValue readableStreamToBlob(JSC::JSGlobalObject*, JSReadableStream*, JSC::JSValue contentType); // userJS: yes — BunStreamConsumers.cpp
JSC::JSValue readableStreamToFormData(JSC::JSGlobalObject*, JSReadableStream*, JSC::JSValue contentType); // userJS: yes — BunStreamConsumers.cpp

// The buffered fast path: returns the native handle's own .text()/.arrayBuffer()/... promise,
Expand Down Expand Up @@ -727,7 +728,7 @@ JSC::EncodedJSValue ZigGlobalObject__readableStreamToArrayBuffer(Zig::GlobalObje
JSC::EncodedJSValue ZigGlobalObject__readableStreamToBytes(Zig::GlobalObject*, JSC::EncodedJSValue stream); // userJS: yes
JSC::EncodedJSValue ZigGlobalObject__readableStreamToText(Zig::GlobalObject*, JSC::EncodedJSValue stream); // userJS: yes
JSC::EncodedJSValue ZigGlobalObject__readableStreamToJSON(Zig::GlobalObject*, JSC::EncodedJSValue stream); // userJS: yes
JSC::EncodedJSValue ZigGlobalObject__readableStreamToBlob(Zig::GlobalObject*, JSC::EncodedJSValue stream); // userJS: yes
JSC::EncodedJSValue ZigGlobalObject__readableStreamToBlob(Zig::GlobalObject*, JSC::EncodedJSValue stream, JSC::EncodedJSValue contentType); // userJS: yes
JSC::EncodedJSValue ZigGlobalObject__readableStreamToFormData(Zig::GlobalObject*, JSC::EncodedJSValue stream, JSC::EncodedJSValue contentType); // userJS: yes

} // extern "C"
4 changes: 1 addition & 3 deletions src/runtime/api/html_rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1554,8 +1554,6 @@ impl RewriterPipe {
let Some(response) = self.response.get().as_deref() else {
return;
};
// For a waiting `.blob()`'s content type.
let headers = response.get_fetch_headers().map(NonNull::from);
let body_value = response.get_body_value();
let bytes = self.output_buffer.replace(Vec::new());
let mut prev_value = core::mem::replace(
Expand All @@ -1565,7 +1563,7 @@ impl RewriterPipe {
was_string: false,
}),
);
let _ = webcore::body::Value::resolve(&mut prev_value, body_value, &self.global, headers);
let _ = webcore::body::Value::resolve(&mut prev_value, body_value, &self.global);
}

/// Feed the accumulated `pending_input` once unblocked, then maybe end,
Expand Down
7 changes: 0 additions & 7 deletions src/runtime/server/RangeRequest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,6 @@ pub(crate) fn from_request(req: &AnyRequest, total: u64) -> Result {
parse(h, total)
}

pub(crate) fn raw_from_request(req: &AnyRequest) -> Raw {
let Some(h) = req.header(b"range") else {
return Raw::None;
};
parse_raw(h)
}

/// Max bytes a `Content-Range: bytes ...` value can occupy: `"bytes "` (6) +
/// three `u64::MAX` (20 each) + `'-'` + `'/'` = 68. 96 leaves slack.
pub(crate) const CONTENT_RANGE_BUF: usize = 96;
Expand Down
Loading
Loading