Move nock to dev deps and load it lazily

# Conflicts:
#	backend/src/server/routes/v1/bdd-nock-router.ts
This commit is contained in:
Fang-Pen Lin
2025-11-20 08:56:59 -08:00
parent 8eb257b04e
commit 8c3699b437
+69 -56
View File
@@ -1,20 +1,30 @@
// import { z } from "zod"; import type { Definition } from "nock";
import { z } from "zod";
// import { getConfig } from "@app/lib/config/env"; import { getConfig } from "@app/lib/config/env";
// import { ForbiddenRequestError } from "@app/lib/errors"; import { ForbiddenRequestError, InternalServerError } from "@app/lib/errors";
// import { logger } from "@app/lib/logger"; import { logger } from "@app/lib/logger";
// import { verifyAuth } from "@app/server/plugins/auth/verify-auth"; import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
// import { AuthMode } from "@app/services/auth/auth-type"; import { AuthMode } from "@app/services/auth/auth-type";
// export const registerBddNockRouter = async (server: FastifyZodProvider) => { export const registerBddNockRouter = async (server: FastifyZodProvider) => {
// const checkIfBddNockApiEnabled = () => { const importNock = async () => {
// const appCfg = getConfig(); // Notice: it seems like importing nock somehow increase memory usage a lots, let's import it lazily.
// // Note: Please note that this API is only available in development mode and only for BDD tests. const nock = await import("nock");
// // This endpoint should NEVER BE ENABLED IN PRODUCTION! if (!nock) {
// if (appCfg.NODE_ENV !== "development" || !appCfg.isBddNockApiEnabled) { throw new InternalServerError({ message: "Failed to import nock" });
// throw new ForbiddenRequestError({ message: "BDD Nock API is not enabled" }); }
// } return nock;
// }; };
const checkIfBddNockApiEnabled = () => {
const appCfg = getConfig();
// Note: Please note that this API is only available in development mode and only for BDD tests.
// This endpoint should NEVER BE ENABLED IN PRODUCTION!
if (appCfg.NODE_ENV !== "development" || !appCfg.isBddNockApiEnabled) {
throw new ForbiddenRequestError({ message: "BDD Nock API is not enabled" });
}
};
// server.route({ // server.route({
// method: "POST", // method: "POST",
@@ -42,46 +52,49 @@
// } as Definition; // } as Definition;
// }); // });
// nock.define(processedDefinitions); const nock = await importNock();
// // Ensure we are activating the nocks, because we could have called `nock.restore()` before this call. nock.define(processedDefinitions);
// if (!nock.isActive()) { // Ensure we are activating the nocks, because we could have called `nock.restore()` before this call.
// nock.activate(); if (!nock.isActive()) {
// } nock.activate();
// return { status: "ok" }; }
// } return { status: "ok" };
// }); }
});
// server.route({ server.route({
// method: "POST", method: "POST",
// url: "/clean-all", url: "/clean-all",
// schema: { schema: {
// response: { response: {
// 200: z.object({ status: z.string() }) 200: z.object({ status: z.string() })
// } }
// }, },
// onRequest: verifyAuth([AuthMode.JWT]), onRequest: verifyAuth([AuthMode.JWT]),
// handler: async () => { handler: async () => {
// checkIfBddNockApiEnabled(); checkIfBddNockApiEnabled();
// logger.info("Cleaning all nocks"); logger.info("Cleaning all nocks");
// nock.cleanAll(); const nock = await importNock();
// return { status: "ok" }; nock.cleanAll();
// } return { status: "ok" };
// }); }
});
// server.route({ server.route({
// method: "POST", method: "POST",
// url: "/restore", url: "/restore",
// schema: { schema: {
// response: { response: {
// 200: z.object({ status: z.string() }) 200: z.object({ status: z.string() })
// } }
// }, },
// onRequest: verifyAuth([AuthMode.JWT]), onRequest: verifyAuth([AuthMode.JWT]),
// handler: async () => { handler: async () => {
// checkIfBddNockApiEnabled(); checkIfBddNockApiEnabled();
// logger.info("Restore network requests from nock"); logger.info("Restore network requests from nock");
// nock.restore(); const nock = await importNock();
// return { status: "ok" }; nock.restore();
// } return { status: "ok" };
// }); }
// }; });
};