1 | /**
|
2 | * Cached process.env
|
3 | *
|
4 | * @type {Record<string, string>}
|
5 | */
|
6 | export let environment = {};
|
7 |
|
8 | /**
|
9 | * This should only be necessary when you or a package mutates the environment variables.
|
10 | * The `mainFn` / `mainTestFn` / `mainBenchFn` / ... will call this function by default
|
11 | * after loading your `.env` file.
|
12 | *
|
13 | * Accessing an environment variable via `process.env.XXXX` is relatively slow compared
|
14 | * to direct property access. As can be seen in the following benchmark:
|
15 | *
|
16 | * ```txt
|
17 | * property access 500000000 iterations 0 ns/op
|
18 | * process.env access 5000000 iterations 246 ns/op
|
19 | * ```
|
20 | *
|
21 | * See this thread: https://github.com/nodejs/node/issues/3104 for more information.
|
22 | *
|
23 | * @since 0.1.0
|
24 | * @summary Repopulate the cached environment copy.
|
25 | *
|
26 | * @returns {void}
|
27 | */
|
28 | export function refreshEnvironmentCache() {
|
29 | environment = JSON.parse(JSON.stringify(process.env));
|
30 | }
|
31 |
|
32 | /**
|
33 | * Returns true when the `NODE_ENV` variable is not set, or when it does not equal to
|
34 | * `development`. This allows for a 'safe by default' experience.
|
35 | *
|
36 | * @since 0.1.0
|
37 | *
|
38 | * @returns {boolean}
|
39 | */
|
40 | export function isProduction() {
|
41 | return environment.NODE_ENV !== "development";
|
42 | }
|
43 |
|
44 | /**
|
45 | * Returns true when `NODE_ENV` is explicitly set to 'development' or when the
|
46 | * environment variable `IS_STAGING` is explicitly set to 'true'.
|
47 | *
|
48 | * @since 0.1.0
|
49 | *
|
50 | * @returns {boolean}
|
51 | */
|
52 | export function isStaging() {
|
53 | return (
|
54 | environment.NODE_ENV === "development" || environment.IS_STAGING === "true"
|
55 | );
|
56 | }
|
57 |
|
58 | /**
|
59 | * Try to calculate the CORS_URL environment variable from the APP_URL environment
|
60 | * variable. Assumes the APP_URL is in the following format: http(s)://api.xxx.xx.com and
|
61 | * generates the following CORS_URL value: http(s)://xxx.xx.com.
|
62 | * If the APP_URL host only contains xxx.com the CORS_URL value will be equivalent.
|
63 | *
|
64 | * Refreshing the environment cache via `refreshEnvironmentCache` is not necessary.
|
65 | *
|
66 | * @since 0.1.0
|
67 | *
|
68 | * @returns {void}
|
69 | */
|
70 | export function calculateCorsUrlFromAppUrl() {
|
71 | const appUrl = new URL(environment.APP_URL);
|
72 |
|
73 | const hostParts = appUrl.host.split(".");
|
74 | const protocol = appUrl.protocol;
|
75 |
|
76 | let corsUrl = "";
|
77 | if (hostParts.length === 2) {
|
78 | corsUrl = `${protocol}//${appUrl.host}`;
|
79 | } else {
|
80 | corsUrl = `${protocol}//${hostParts.slice(1).join(".")}`;
|
81 | }
|
82 |
|
83 | environment.CORS_URL = corsUrl;
|
84 | process.env.CORS_URL = corsUrl;
|
85 | }
|
86 |
|
87 | /**
|
88 | * Try to calculate the COOKIE_URL environment variable from the APP_URL environment
|
89 | * variable. Assumes the APP_URL is in the following format: http(s)://api.xxx.xx.com and
|
90 | * generates the following COOKIE_URL value: xxx.xx.com.
|
91 | * If the APP_URL host only contains xxx.com the CORS_URL value will be equivalent.
|
92 | *
|
93 | * Refreshing the environment cache via `refreshEnvironmentCache` is not necessary.
|
94 | *
|
95 | * @since 0.1.0
|
96 | *
|
97 | * @returns {void}
|
98 | */
|
99 | export function calculateCookieUrlFromAppUrl() {
|
100 | const appUrl = new URL(environment.APP_URL);
|
101 |
|
102 | const hostParts = appUrl.host.split(".");
|
103 |
|
104 | let cookieUrl = appUrl.host;
|
105 | if (hostParts.length !== 2) {
|
106 | cookieUrl = hostParts.slice(1).join(".");
|
107 | }
|
108 |
|
109 | environment.COOKIE_URL = cookieUrl;
|
110 | process.env.COOKIE_URL = cookieUrl;
|
111 | }
|