Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Move nock to dev deps and load it lazily

Fang-Pen Lin8c3699b43766
diff --git a/backend/src/server/routes/v1/bdd-nock-router.ts b/backend/src/server/routes/v1/bdd-nock-router.ts
index 6a32cac20a..15981ddd32 100644
--- a/backend/src/server/routes/v1/bdd-nock-router.ts
+++ b/backend/src/server/routes/v1/bdd-nock-router.ts
@@ -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 { ForbiddenRequestError } from "@app/lib/errors";
-// import { logger } from "@app/lib/logger";
-// import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
-// import { AuthMode } from "@app/services/auth/auth-type";
+import { getConfig } from "@app/lib/config/env";
+import { ForbiddenRequestError, InternalServerError } from "@app/lib/errors";
+import { logger } from "@app/lib/logger";
+import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
+import { AuthMode } from "@app/services/auth/auth-type";
-// export const registerBddNockRouter = async (server: FastifyZodProvider) => {
-// 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" });
-// }
-// };
+export const registerBddNockRouter = async (server: FastifyZodProvider) => {
+ const importNock = async () => {
+ // Notice: it seems like importing nock somehow increase memory usage a lots, let's import it lazily.
+ const nock = await import("nock");
+ if (!nock) {
+ throw new InternalServerError({ message: "Failed to import nock" });
+ }
+ 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({
// method: "POST",
@@ -42,46 +52,49 @@
// } as Definition;
// });
-// nock.define(processedDefinitions);
-// // Ensure we are activating the nocks, because we could have called `nock.restore()` before this call.
-// if (!nock.isActive()) {
-// nock.activate();
-// }
-// return { status: "ok" };
-// }
-// });
+ const nock = await importNock();
+ nock.define(processedDefinitions);
+ // Ensure we are activating the nocks, because we could have called `nock.restore()` before this call.
+ if (!nock.isActive()) {
+ nock.activate();
+ }
+ return { status: "ok" };
+ }
+ });
-// server.route({
-// method: "POST",
-// url: "/clean-all",
-// schema: {
-// response: {
-// 200: z.object({ status: z.string() })
-// }
-// },
-// onRequest: verifyAuth([AuthMode.JWT]),
-// handler: async () => {
-// checkIfBddNockApiEnabled();
-// logger.info("Cleaning all nocks");
-// nock.cleanAll();
-// return { status: "ok" };
-// }
-// });
+ server.route({
+ method: "POST",
+ url: "/clean-all",
+ schema: {
+ response: {
+ 200: z.object({ status: z.string() })
+ }
+ },
+ onRequest: verifyAuth([AuthMode.JWT]),
+ handler: async () => {
+ checkIfBddNockApiEnabled();
+ logger.info("Cleaning all nocks");
+ const nock = await importNock();
+ nock.cleanAll();
+ return { status: "ok" };
+ }
+ });
-// server.route({
-// method: "POST",
-// url: "/restore",
-// schema: {
-// response: {
-// 200: z.object({ status: z.string() })
-// }
-// },
-// onRequest: verifyAuth([AuthMode.JWT]),
-// handler: async () => {
-// checkIfBddNockApiEnabled();
-// logger.info("Restore network requests from nock");
-// nock.restore();
-// return { status: "ok" };
-// }
-// });
-// };
+ server.route({
+ method: "POST",
+ url: "/restore",
+ schema: {
+ response: {
+ 200: z.object({ status: z.string() })
+ }
+ },
+ onRequest: verifyAuth([AuthMode.JWT]),
+ handler: async () => {
+ checkIfBddNockApiEnabled();
+ logger.info("Restore network requests from nock");
+ const nock = await importNock();
+ nock.restore();
+ return { status: "ok" };
+ }
+ });
+};