Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fast path for string responses
  • Loading branch information
bengl committed Jul 12, 2019
1 parent fc12ec6 commit 2a839bb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
12 changes: 6 additions & 6 deletions js/bootstrap/index.js
Expand Up @@ -5,6 +5,8 @@
delete self._startResponse;
const writeResponse = self._writeResponse;
delete self._writeResponse;
const stringResponse = self._stringResponse;
delete self._stringResponse;
const setTimeout = self._setTimeout;
delete self._setTimeout;
const setInterval = self._setInterval;
Expand Down Expand Up @@ -263,11 +265,7 @@
response = await fn(request, generateContextObject(url));
switch (typeof response) {
case 'string': {
const headers = new Headers({
'Content-Type': 'text/plain'
});

response = new Response(response, { headers });
// handle it in native code
break;
}
case 'object': {
Expand Down Expand Up @@ -304,7 +302,9 @@
response = new Response('', { status: 500 });
}

if (response.body) {
if (typeof response === 'string') {
stringResponse(response, reqId);
} else if (response.body) {
startResponse(response, reqId);
let stream =
response.body instanceof TransformStream
Expand Down
1 change: 1 addition & 0 deletions osgood-v8/src/wrapper/local.rs
Expand Up @@ -128,6 +128,7 @@ persistent!(
persistent!(V8::Module, persistent_from_module, persistent_to_module);
persistent!(V8::Message, persistent_from_message, persistent_to_message);
each_valuable_type!(V8::Object);
each_valuable_type!(V8::Map);
each_valuable_type!(V8::Array);
each_valuable_type!(V8::String);
each_valuable_type!(V8::Primitive);
Expand Down
1 change: 1 addition & 0 deletions src/worker.rs
Expand Up @@ -222,6 +222,7 @@ fn make_globals(mut context: Local<Context>, route: &str) {
global.set("_route", route);
global.set_extern_method(context, "_startResponse", inbound::start_response);
global.set_extern_method(context, "_writeResponse", inbound::write_response);
global.set_extern_method(context, "_stringResponse", inbound::string_response);
global.set_extern_method(context, "_setFetchHandler", fetch::set_fetch_handler);
global.set_extern_method(context, "_setTimerHandler", timers::set_timer_handler);
global.set_extern_method(
Expand Down
9 changes: 9 additions & 0 deletions src/worker/inbound.rs
@@ -1,4 +1,5 @@
use super::*;
use hyper::header::HeaderValue;

enum ResponseHolder {
Resp(body::Sender),
Expand Down Expand Up @@ -97,6 +98,14 @@ pub fn call_inbound_req_body_handler(mut context: Local<V8::Context>, args: Vec<
.call(context, &null, args);
}

#[v8_fn]
pub fn string_response(args: FunctionCallbackInfo) {
let req_id = args.get(1).unwrap().to_number().value() as i32;
let mut response = Response::new(args.get(0).unwrap().as_rust_string().into());
(*response.headers_mut()).insert("Content-Type", HeaderValue::from_str("text/plain").unwrap());
send_response!(&req_id, Ok(response));
}

#[v8_fn]
pub fn start_response(args: FunctionCallbackInfo) {
let context = get_context();
Expand Down

0 comments on commit 2a839bb

Please sign in to comment.