Skip to content

Commit

Permalink
fix: setContentTypeParser
Browse files Browse the repository at this point in the history
This fixes "Internal Server Error" caused by the server crashing being unable to add a contentTypeParser to one that already exists while the instance is up
  • Loading branch information
yamcodes committed Mar 30, 2024
1 parent d304379 commit 788b0e9
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 112 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ yarn-error.log*

#vite
dist
*.log
*.log

# isolate-package
isolate
7 changes: 4 additions & 3 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log",
"clean": "rimraf node_modules dist"
"clean": "rimraf node_modules dist isolate"
},
"engines": {
"node": "20"
},
"main": "dist/index.js",
"dependencies": {
"utilities": "workspace:*",
"@faker-js/faker": "^8.0.1",
"express": "^4.18.2",
"fastify": "^4.26.2",
"firebase-admin": "^12.0.0",
"firebase-functions": "^4.8.1"
"firebase-functions": "^4.8.1",
"utilities": "workspace:*"
},
"devDependencies": {
"@types/rollup-plugin-generate-package-json": "^3.2.0",
Expand All @@ -45,6 +45,7 @@
"firebase-functions-test": "^3.1.0",
"rimraf": "^5.0.1",
"rollup-plugin-generate-package-json": "^3.2.0",
"@google-cloud/functions-framework": "^3.3.0",
"typescript": "^5.0.0",
"vite": "^5.0.0"
},
Expand Down
22 changes: 20 additions & 2 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,32 @@

import { onRequest } from 'firebase-functions/v2/https';
import { fastify } from 'fastify';
import { registerRoutes } from './utils';
import { registerRoutes, setContentTypeParser } from './utils';

const app = fastify({
logger: true,
});

// Register the content type parser before starting the server
setContentTypeParser(
app,
'application/json',
{ parseAs: 'string' },
(request, incomingMessage, done) => {
// useful to include the request's raw body on the `req` object that will
// later be available in your other routes so you can calculate the HMAC
// if needed
// @ts-expect-error - taken from Fastify docs
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- taken from Fastify docs
request.rawBody = incomingMessage.rawBody;
// @ts-expect-error - taken from Fastify docs
done(null, incomingMessage.body);
}
);

registerRoutes(app);

export const server = onRequest(async (request, reply) => {
registerRoutes(app);
await app.ready();
app.server.emit('request', request, reply);
});
27 changes: 8 additions & 19 deletions apps/api/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
import { iLikeTurtles } from 'utilities';
import { faker } from '@faker-js/faker';
import { type FastifyInstance, type FastifyRequest } from 'fastify';
import type { AddContentTypeParser, FastifyInstance } from 'fastify';

export interface Request extends FastifyRequest {
/** The wire format representation of the request body. */
rawBody: Buffer;
}
export const setContentTypeParser = (
app: FastifyInstance,
...[contentType, opts, parser]: Parameters<AddContentTypeParser>
) => {
app.removeContentTypeParser(contentType);
app.addContentTypeParser(contentType, opts, parser);
};

export const registerRoutes = (fastify: FastifyInstance) => {
fastify.addContentTypeParser('application/json', {}, (req, payload, done) => {
// useful to include the request's raw body on the `req` object that will
// later be available in your other routes so you can calculate the HMAC
// if needed
// @ts-expect-error - taken from Fastify docs
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- taken from Fastify docs
req.rawBody = payload.rawBody;

// payload.body is already the parsed JSON so we just fire the done callback
// with it
// @ts-expect-error - taken from Fastify docs
done(null, payload.body);
});

// define your endpoints here...
fastify.post('/some-route-here', (_request) => {
return { message: 'Hello World!!' };
Expand Down
3 changes: 2 additions & 1 deletion apps/api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
},
"outDir": "dist"
},
"extends": "tsconfig/functions.json",
"include": ["."],
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@
"scripts": {
"start": "turbo run dev emulators",
"dev": "turbo run dev",
"emulate": "turbo run root:emulate",
"emulate": "turbo run build && firebase emulators:start",
"clean": "turbo run root:clean clean",
"build": "turbo run build",
"deploy": "turbo run build --parallel && turbo run deploy --filter=web --filter=api",
"deploy": "turbo run deploy",
"lint": "pnpm format:check && turbo run lint",
"lint:fix": "pnpm format && turbo run lint -- --fix",
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"format:check": "prettier --check \"**/*.{ts,tsx,md}\"",
"root:clean": "rimraf node_modules",
"root:emulate": "firebase emulators:start"
"root:clean": "rimraf node_modules"
},
"devDependencies": {
"eslint-config-custom": "workspace:*",
Expand Down
Loading

0 comments on commit 788b0e9

Please sign in to comment.