Add withBody.

This commit is contained in:
Dillon Kearns 2021-12-22 15:28:19 -08:00
parent 6df0ce9987
commit f43e57e265
3 changed files with 85 additions and 59 deletions

View File

@ -205,6 +205,7 @@ function reqToJson(req) {
port: 80, // TODO port: 80, // TODO
protocol: "https", // TODO protocol: "https", // TODO
rawUrl: "", // TODO rawUrl: "", // TODO
body: req.body,
}; };
} }
`; `;

View File

@ -346,8 +346,19 @@ async function start(options) {
return; return;
} }
let body = "";
req.on("data", function (data) {
body += data;
// Too much POST data, kill the connection!
// 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
if (body.length > 1e6) req.connection.destroy();
});
req.on("end", async function () {
await runRenderThread( await runRenderThread(
reqToJson(req), reqToJson(req, body),
pathname, pathname,
function (renderResult) { function (renderResult) {
const is404 = renderResult.is404; const is404 = renderResult.is404;
@ -369,7 +380,10 @@ async function start(options) {
case "api-response": { case "api-response": {
if (renderResult.body.kind === "server-response") { if (renderResult.body.kind === "server-response") {
const serverResponse = renderResult.body; const serverResponse = renderResult.body;
res.writeHead(serverResponse.statusCode, serverResponse.headers); res.writeHead(
serverResponse.statusCode,
serverResponse.headers
);
res.end(serverResponse.body); res.end(serverResponse.body);
} else if (renderResult.body.kind === "static-file") { } else if (renderResult.body.kind === "static-file") {
let mimeType = serveStatic.mime.lookup(pathname || "text/html"); let mimeType = serveStatic.mime.lookup(pathname || "text/html");
@ -405,6 +419,7 @@ async function start(options) {
} }
} }
); );
});
} }
/** /**
@ -579,7 +594,7 @@ async function ensureRequiredExecutables() {
} }
} }
function reqToJson(req) { function reqToJson(req, body) {
const url = new URL(req.url, "http://localhost:1234"); const url = new URL(req.url, "http://localhost:1234");
return { return {
method: req.method, method: req.method,
@ -591,6 +606,7 @@ function reqToJson(req) {
port: url.port, port: url.port,
protocol: url.protocol, protocol: url.protocol,
rawUrl: req.url, rawUrl: req.url,
body: body,
}; };
} }

View File

@ -1,7 +1,7 @@
module DataSource.ServerRequest exposing module DataSource.ServerRequest exposing
( IsAvailable ( IsAvailable
, ServerRequest, expectHeader, init, optionalHeader, staticData, toDataSource , ServerRequest, expectHeader, init, optionalHeader, staticData, toDataSource
, Method(..), withAllHeaders, withHost, withMethod, withProtocol, withQueryParams , Method(..), withAllHeaders, withBody, withHost, withMethod, withProtocol, withQueryParams
) )
{-| {-|
@ -143,6 +143,15 @@ optionalHeader headerName (ServerRequest decoder) =
|> ServerRequest |> ServerRequest
{-| -}
withBody : ServerRequest (Maybe String -> value) -> ServerRequest value
withBody (ServerRequest decoder) =
decoder
|> OptimizedDecoder.andMap
(OptimizedDecoder.optionalField "body" OptimizedDecoder.string)
|> ServerRequest
type Method type Method
= Connect = Connect
| Delete | Delete