mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-28 06:05:31 +03:00
Add withBody.
This commit is contained in:
parent
6df0ce9987
commit
f43e57e265
@ -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,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
@ -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,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user