Create a program, which reads from a .env file and a function, which parses the variables in the .env file:
import { randomBytes } from "node:crypto";
import { logger } from "./logger.js";
import z from 'zod';
export const envSchema = z.object({
PORT: z.coerce.number({ invalid_type_error: "The Port must be a number." }).max(65535, { message: "This port is not available for use, please reconfigure Oxygen to use a port between 1 and 65535." }).min(1, { message: "This port is not available for use, please reconfigure Oxygen to use a port between 1 and 65535." }).default(9495),
JWTSECRET: z.string({ invalid_type_error: "The Secret must be string" }).min(256, { message: "Please reconfigure Oxygen to use a secret which is longer than 256 characters." }),
NODE_ENV: z.enum(["development", "production", "test"]).default("development")
});
export async function parseEnv(envPath: string): Promise<Zod.ZodError | void> {
if(await (Bun.file(envPath)).exists() === false && process.env.NODE_ENV !== "test") {
logger.error("Cannot access .env configuration file, creating a new one with default configuration...");
let defaultJWTSecret = randomBytes(512).toString("hex");
await Bun.write(envPath, # The string, with which all of the JSONWebTokens are encrypted\n# Defaults to 512 random bytes if left blank\n# WARNING: If this variable is changed all API Keys will be invalidated and you will have to reset the database (-c)\nJWTSECRET=${defaultJWTSecret}\n# Set this to valid Port (1-65535)\nPORT=9495);
process.env.PORT = 9495;
process.env.JWTSECRET = defaultJWTSecret;
logger.info("Done!");
}
What version of Bun is running?
1.0.11+f7f6233ea
What platform is your computer?
Darwin 23.1.0 arm64 arm
What steps can reproduce the bug?
import { randomBytes } from "node:crypto"; import { logger } from "./logger.js"; import z from 'zod';
export const envSchema = z.object({ PORT: z.coerce.number({ invalid_type_error: "The Port must be a number." }).max(65535, { message: "This port is not available for use, please reconfigure Oxygen to use a port between 1 and 65535." }).min(1, { message: "This port is not available for use, please reconfigure Oxygen to use a port between 1 and 65535." }).default(9495), JWTSECRET: z.string({ invalid_type_error: "The Secret must be string" }).min(256, { message: "Please reconfigure Oxygen to use a secret which is longer than 256 characters." }), NODE_ENV: z.enum(["development", "production", "test"]).default("development") });
export async function parseEnv(envPath: string): Promise<Zod.ZodError | void> { if(await (Bun.file(envPath)).exists() === false && process.env.NODE_ENV !== "test") { logger.error("Cannot access .env configuration file, creating a new one with default configuration..."); let defaultJWTSecret = randomBytes(512).toString("hex"); await Bun.write(envPath,
# The string, with which all of the JSONWebTokens are encrypted\n# Defaults to 512 random bytes if left blank\n# WARNING: If this variable is changed all API Keys will be invalidated and you will have to reset the database (-c)\nJWTSECRET=${defaultJWTSecret}\n# Set this to valid Port (1-65535)\nPORT=9495
); process.env.PORT = 9495; process.env.JWTSECRET = defaultJWTSecret; logger.info("Done!"); }}
process.env.PORT = portToTest;
????????What is the expected behavior?
It works like in NodeJS
What do you see instead?
It doesn't work
Additional information
No response