Skip to content

Commit

Permalink
fix(firebase): issue with body-parser internally in firebase environment
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Dec 14, 2022
1 parent db3e6f0 commit 3ec9a6f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
35 changes: 28 additions & 7 deletions src/handlers/firebase/http-firebase.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { IncomingMessage, ServerResponse } from 'http';
import { https } from 'firebase-functions';
import { FrameworkContract, HandlerContract } from '../../contracts';
import { getEventBodyAsBuffer, getFlattenedHeadersMap } from '../../core';
import { ServerlessRequest } from '../../network';

//#endregion
Expand Down Expand Up @@ -30,19 +31,39 @@ export class HttpFirebaseHandler<TApp>
): (req: IncomingMessage, res: ServerResponse) => void | Promise<void> {
return https.onRequest(
(request: IncomingMessage, response: ServerResponse) => {
const serverlessRequest = request as ServerlessRequest;
const expressRequestParsed = request as unknown as {
body: object | Buffer;
};

const headers = getFlattenedHeadersMap(request.headers, ',', true);
const remoteAddress = headers['x-forwarded-for'];

let body: Buffer | undefined;

if (
serverlessRequest.body &&
typeof serverlessRequest.body === 'object'
expressRequestParsed.body &&
typeof expressRequestParsed.body === 'object'
) {
serverlessRequest.body = Buffer.from(
JSON.stringify(serverlessRequest.body),
'utf-8',
const jsonContent = JSON.stringify(expressRequestParsed.body);

const [bufferBody, contentLength] = getEventBodyAsBuffer(
jsonContent,
false,
);

body = bufferBody;
headers['content-length'] = String(contentLength);
}

return framework.sendRequest(app, serverlessRequest, response);
const customRequest = new ServerlessRequest({
method: request.method!,
url: request.url!,
body,
headers,
remoteAddress,
});

return framework.sendRequest(app, customRequest, response);
},
);
}
Expand Down
32 changes: 20 additions & 12 deletions test/handlers/http-firebase.handler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import {
AdapterContract,
FrameworkContract,
createDefaultLogger,
} from '../../lib';
import {
ServerlessRequest,
ServerlessResponse,
waitForStreamComplete,
Expand Down Expand Up @@ -88,19 +84,31 @@ describe(HttpFirebaseHandler.name, () => {
});

const framework: FrameworkContract<unknown> = {
sendRequest: jest.fn(),
sendRequest: jest.fn(
async (
app: null,
req: ServerlessRequest,
res: ServerlessResponse,
) => {
expect(req.body?.toString()).toEqual(JSON.stringify(option));
expect(req.headers['content-length']).toEqual(
Buffer.byteLength(JSON.stringify(option)).toString(),
);

req.pipe(res);

await waitForStreamComplete(res);

expect(ServerlessResponse.body(res).toString()).toEqual(
JSON.stringify(option),
);
},
),
};

const handler = handlerFactory.getHandler(null, framework);

handler(request, response);

// eslint-disable-next-line @typescript-eslint/unbound-method
expect(framework.sendRequest).toHaveBeenCalledWith(
null,
expect.objectContaining({ body: Buffer.from(JSON.stringify(option)) }),
response,
);
}
});
});

0 comments on commit 3ec9a6f

Please sign in to comment.