Created
February 18, 2024 05:55
-
-
Save mayankchoubey/61dd1b23ff7d92b0e26fcfd699bd1426 to your computer and use it in GitHub Desktop.
Node.js vs Deno vs Bun : A real world PDF generation service for benchmarking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jwt from "jsonwebtoken"; | |
import { env } from "node:process"; | |
const jwtSecret = env.JWT_SECRET; | |
function getToken(headers) { | |
if ( | |
headers && headers.authorization && | |
headers.authorization.startsWith("Bearer ") | |
) { | |
return headers.authorization.split(" ")[1]; | |
} | |
} | |
export function authorize(hdrs) { | |
const rcvdJwt = getToken(hdrs); | |
if (!rcvdJwt) { | |
return; | |
} | |
let email; | |
try { | |
const payload = jwt.verify(rcvdJwt, jwtSecret); | |
email = payload.email; | |
} catch (e) { | |
} | |
return email; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { env } from "node:process"; | |
import { DataTypes, Sequelize } from "sequelize"; | |
const dbUser = env.dbUser; | |
const dbUserPass = env.dbUserPass; | |
const dbName = env.dbName; | |
const sequelize = new Sequelize( | |
`postgres://${dbUser}:${dbUserPass}@localhost:5432/${dbName}`, | |
{ | |
logging: false, | |
pool: { | |
max: 10, | |
min: 10, | |
}, | |
}, | |
); | |
await sequelize.authenticate(); | |
const User = sequelize.define("user", { | |
email: { | |
type: DataTypes.STRING, | |
primaryKey: true, | |
}, | |
first: DataTypes.STRING, | |
last: DataTypes.STRING, | |
city: DataTypes.STRING, | |
county: DataTypes.STRING, | |
}, { | |
timestamps: false, | |
}); | |
export async function getUser(userEmail) { | |
return await User.findOne({ | |
where: { | |
email: userEmail, | |
}, | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getPdf } from "./service.mjs"; | |
import { authorize } from "./auth.mjs"; | |
export async function handleRequest(ctx) { | |
const userEmail = authorize(ctx.headers); | |
if (!userEmail) { | |
ctx.set.status = 401; | |
return; | |
} | |
const pdfBytes = await getPdf(userEmail); | |
if (!pdfBytes) { | |
ctx.set.status = 500; | |
return { | |
errMsg: "Failed to generate PDF", | |
}; | |
} | |
return new Response(pdfBytes.buffer, { | |
status: 200, | |
headers: { | |
"content-type": "application/pdf", | |
}, | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Elysia } from "elysia"; | |
import { handleRequest } from "./elysiaController.js"; | |
const app = new Elysia(); | |
app.get("/", handleRequest); | |
app.listen({ | |
port: 3000, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getPdf } from "./service.mjs"; | |
import { authorize } from "./auth.mjs"; | |
export async function handleRequest(req, rep) { | |
const userEmail = authorize(req.headers); | |
if (!userEmail) { | |
return rep.code(401).send(); | |
} | |
const pdfBytes = await getPdf(userEmail); | |
if (!pdfBytes) { | |
return rep.code(500).send({ errMsg: "Failed to generate PDF" }); | |
} | |
rep.header("content-type", "application/pdf"); | |
rep.send(pdfBytes); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Fastify from "fastify"; | |
import { handleRequest } from "./fastifyController.mjs"; | |
const app = Fastify({ | |
logger: false, | |
}); | |
app.get("/", handleRequest); | |
app.listen({ port: 3000 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getPdf } from "./service.mjs"; | |
import { authorize } from "./auth.mjs"; | |
import { HTTPException } from "npm:hono/http-exception"; | |
export async function handleRequest(ctx) { | |
const userEmail = authorize(ctx.req.header()); | |
if (!userEmail) { | |
throw new HTTPException(401); | |
} | |
const pdfBytes = await getPdf(userEmail); | |
if (!pdfBytes) { | |
throw new HTTPException(500, { errMsg: "Failed to generate PDF" }); | |
} | |
ctx.status(200); | |
ctx.header("content-type", "application/pdf"); | |
return ctx.body(pdfBytes); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Hono } from "npm:hono"; | |
import { handleRequest } from "./honoController.js"; | |
const app = new Hono(); | |
app.get("/", handleRequest); | |
Deno.serve({ port: 3000 }, app.fetch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PDFDocument } from "pdf-lib"; | |
export async function createPdf(user) { | |
const pdfDoc = await PDFDocument.create(); | |
const page = pdfDoc.addPage(); | |
page.drawText(`Email: ${user.email}`, { x: 50, y: 800, size: 18 }); | |
page.drawText(`First name: ${user.first}`, { x: 50, y: 700, size: 18 }); | |
page.drawText(`Last name: ${user.last}`, { x: 50, y: 600, size: 18 }); | |
page.drawText(`City: ${user.city}`, { x: 50, y: 500, size: 18 }); | |
page.drawText(`County: ${user.county}`, { x: 50, y: 400, size: 18 }); | |
return await pdfDoc.save(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getUser } from "./db.mjs"; | |
import { createPdf } from "./pdf.mjs"; | |
export async function getPdf(userEmail) { | |
const user = await getUser(userEmail); | |
if (!user) { | |
return; | |
} | |
return await createPdf(user); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment