Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Commit c4477da

Browse files
authored
Handle binary request body using buffer: intends to fix #143 (#145)
Handle binary request body using buffer: intends to fix #143
2 parents 6c236d5 + 3d6aa30 commit c4477da

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/utils/serve-functions.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,25 @@ function createHandler(dir) {
174174
return;
175175
}
176176

177-
const isBase64 =
178-
request.body &&
179-
!(request.headers["content-type"] || "").match(
180-
/text|application|multipart\/form-data/
181-
);
177+
var isBase64Encoded = false;
178+
var body = request.body;
179+
180+
if (body instanceof Buffer) {
181+
isBase64Encoded = true;
182+
body = body.toString("base64");
183+
} else if(typeof(body) === "string") {
184+
// body is already processed as string
185+
} else {
186+
body = "";
187+
}
188+
182189
const lambdaRequest = {
183190
path: request.path,
184191
httpMethod: request.method,
185192
queryStringParameters: queryString.parse(request.url.split(/\?(.+)/)[1]),
186193
headers: request.headers,
187-
body: isBase64
188-
? Buffer.from(request.body.toString(), "utf8").toString("base64")
189-
: request.body,
190-
isBase64Encoded: isBase64
194+
body: body,
195+
isBase64Encoded: isBase64Encoded
191196
};
192197

193198
const callback = createCallback(response);
@@ -207,8 +212,8 @@ async function serveFunctions(settings) {
207212
port: assignLoudly(settings.port, defaultPort)
208213
});
209214

210-
app.use(bodyParser.raw({ limit: "6mb" }));
211-
app.use(bodyParser.text({ limit: "6mb", type: "*/*" }));
215+
app.use(bodyParser.text({ limit: "6mb", type: ["text/*", "application/json", "multipart/form-data"] }));
216+
app.use(bodyParser.raw({ limit: "6mb", type: "*/*" }));
212217
app.use(
213218
expressLogging(console, {
214219
blacklist: ["/favicon.ico"]

0 commit comments

Comments
 (0)