UNPKG

3.96 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.applyDefaultMiddleware = exports.accessorMiddleware = exports.strictProxyMiddleware = void 0;
4var strictProxyMiddleware = function (envObj, rawEnv, options) {
5 if (options === void 0) { options = {}; }
6 var _a = options.extraInspectables, extraInspectables = _a === void 0 ? [] : _a;
7 var inspectables = [
8 'length',
9 'inspect',
10 'hasOwnProperty',
11 'toJSON',
12 Symbol.toStringTag,
13 Symbol.iterator,
14 // For jest
15 'asymmetricMatch',
16 'nodeType',
17 // For react-refresh, see #150
18 '$$typeof',
19 // For libs that use `then` checks to see if objects are Promises (see #74):
20 'then',
21 // For usage with TypeScript esModuleInterop flag
22 '__esModule',
23 ];
24 var inspectSymbolStrings = ['Symbol(util.inspect.custom)', 'Symbol(nodejs.util.inspect.custom)'];
25 return new Proxy(envObj, {
26 get: function (target, name) {
27 var _a;
28 // These checks are needed because calling console.log on a
29 // proxy that throws crashes the entire process. This permits access on
30 // the necessary properties for `console.log(envObj)`, `envObj.length`,
31 // `envObj.hasOwnProperty('string')` to work.
32 if (inspectables.includes(name) ||
33 inspectSymbolStrings.includes(name.toString()) ||
34 extraInspectables.includes(name)) {
35 // @ts-expect-error TS doesn't like symbol types as indexers
36 return target[name];
37 }
38 var varExists = target.hasOwnProperty(name);
39 if (!varExists) {
40 if (typeof rawEnv === 'object' && ((_a = rawEnv === null || rawEnv === void 0 ? void 0 : rawEnv.hasOwnProperty) === null || _a === void 0 ? void 0 : _a.call(rawEnv, name))) {
41 throw new ReferenceError("[envalid] Env var ".concat(name, " was accessed but not validated. This var is set in the environment; please add an envalid validator for it."));
42 }
43 throw new ReferenceError("[envalid] Env var not found: ".concat(name));
44 }
45 return target[name];
46 },
47 set: function (_target, name) {
48 throw new TypeError("[envalid] Attempt to mutate environment value: ".concat(name));
49 },
50 });
51};
52exports.strictProxyMiddleware = strictProxyMiddleware;
53var accessorMiddleware = function (envObj, rawEnv) {
54 // Attach is{Prod/Dev/Test} properties for more readable NODE_ENV checks
55 // Note that isDev and isProd are just aliases to isDevelopment and isProduction
56 // @ts-ignore attempt to read NODE_ENV even if it's not in the spec
57 var computedNodeEnv = envObj.NODE_ENV || rawEnv.NODE_ENV;
58 // If NODE_ENV is not set, assume production
59 var isProd = !computedNodeEnv || computedNodeEnv === 'production';
60 Object.defineProperties(envObj, {
61 isDevelopment: { value: computedNodeEnv === 'development' },
62 isDev: { value: computedNodeEnv === 'development' },
63 isProduction: { value: isProd },
64 isProd: { value: isProd },
65 isTest: { value: computedNodeEnv === 'test' },
66 });
67 return envObj;
68};
69exports.accessorMiddleware = accessorMiddleware;
70var applyDefaultMiddleware = function (cleanedEnv, rawEnv) {
71 // Note: Ideally we would declare the default middlewares in an array and apply them in series with
72 // a generic pipe() function. However, a generically typed variadic pipe() appears to not be possible
73 // in TypeScript as of 4.x, so we just manually apply them below. See
74 // https://github.com/microsoft/TypeScript/pull/39094#issuecomment-647042984
75 return (0, exports.strictProxyMiddleware)((0, exports.accessorMiddleware)(cleanedEnv, rawEnv), rawEnv);
76};
77exports.applyDefaultMiddleware = applyDefaultMiddleware;
78//# sourceMappingURL=middleware.js.map
\No newline at end of file