@lbu/stdlib
Advanced tools
Comparing version 0.0.91 to 0.0.92
@@ -313,2 +313,23 @@ import { Logger } from "@lbu/insight"; | ||
/** | ||
* Cached environment, set by `refreshEnvironmentCache()` | ||
*/ | ||
export const environment: typeof process.env; | ||
/** | ||
* Repopulate the cached environment copy. | ||
* This should only be necessary when you or a sub package mutates the environment. | ||
* The `mainFn` / `mainTestFn` / `mainBenchFn` / ... will call this function by default | ||
* after loading your `.env` file. | ||
* | ||
* Accessing process.env.XXX is relatively slow in Node.js. | ||
* Benchmark of a plain object property access and accessing process.env.NODE_ENV: | ||
* | ||
* property access 500000000 iterations 0 ns/op | ||
* process.env access 5000000 iterations 246 ns/op | ||
* | ||
* See this thread: https://github.com/nodejs/node/issues/3104 | ||
*/ | ||
export function refreshEnvironmentCache(): void; | ||
/** | ||
* Returns whether NODE_ENV === "production" | ||
@@ -315,0 +336,0 @@ */ |
13
index.js
export { uuid } from "./src/datatypes.js"; | ||
export { AppError } from "./src/error.js"; | ||
export { | ||
isProduction, | ||
isStaging, | ||
environment, | ||
refreshEnvironmentCache, | ||
} from "./src/env.js"; | ||
export { | ||
isNil, | ||
@@ -11,2 +20,3 @@ isPlainObject, | ||
} from "./src/lodash.js"; | ||
export { | ||
@@ -20,2 +30,3 @@ exec, | ||
} from "./src/node.js"; | ||
export { | ||
@@ -28,4 +39,2 @@ getSecondsSinceEpoch, | ||
dirnameForModule, | ||
isProduction, | ||
isStaging, | ||
} from "./src/utils.js"; |
{ | ||
"name": "@lbu/stdlib", | ||
"version": "0.0.91", | ||
"version": "0.0.92", | ||
"description": "All kinds of utility functions", | ||
@@ -17,4 +17,4 @@ "main": "./index.js", | ||
"dependencies": { | ||
"@lbu/insight": "0.0.91", | ||
"@types/node": "14.14.5", | ||
"@lbu/insight": "0.0.92", | ||
"@types/node": "14.14.6", | ||
"dotenv": "8.2.0", | ||
@@ -44,3 +44,3 @@ "lodash.merge": "4.6.2" | ||
}, | ||
"gitHead": "241012842c62b2eb255e9a503da837e32bbef548" | ||
"gitHead": "68be0c5c1f7177797820b1cf2ef061542004dfd3" | ||
} |
@@ -8,2 +8,3 @@ import { lstatSync, realpathSync } from "fs"; | ||
import dotenv from "dotenv"; | ||
import { isProduction, refreshEnvironmentCache } from "./env.js"; | ||
import { AppError } from "./error.js"; | ||
@@ -51,36 +52,38 @@ import { isNil } from "./lodash.js"; | ||
const { isMainFn, name } = isMainFnAndReturnName(meta); | ||
if (isMainFn) { | ||
dotenv.config(); | ||
const logger = newLogger({ | ||
ctx: { type: name }, | ||
}); | ||
if (!isMainFn) { | ||
return; | ||
} | ||
const unhandled = (error) => { | ||
logger.error(error); | ||
process.exit(1); | ||
}; | ||
dotenv.config(); | ||
refreshEnvironmentCache(); | ||
// Just kill the process | ||
process.on("unhandledRejection", (reason, promise) => | ||
unhandled({ | ||
reason: AppError.format(reason), | ||
promise: AppError.format(promise), | ||
}), | ||
); | ||
const logger = newLogger({ | ||
ctx: { type: name }, | ||
pretty: !isProduction(), | ||
}); | ||
// Node.js by default will kill the process, we just make sure to log correctly | ||
process.on("uncaughtExceptionMonitor", (error, origin) => | ||
logger.error({ | ||
error: AppError.format(error), | ||
origin, | ||
}), | ||
); | ||
// Log full warnings as well, no need for exiting | ||
process.on("warning", (warn) => logger.error(AppError.format(warn))); | ||
const unhandled = (error) => { | ||
logger.error(error); | ||
process.exit(1); | ||
}; | ||
// Handle async errors from the provided callback as `unhandledRejections` | ||
Promise.resolve(cb(logger)).catch((e) => { | ||
unhandled(AppError.format(e)); | ||
}); | ||
} | ||
process.on("unhandledRejection", (reason, promise) => | ||
unhandled({ | ||
reason: AppError.format(reason), | ||
promise: AppError.format(promise), | ||
}), | ||
); | ||
process.on("uncaughtExceptionMonitor", (error, origin) => | ||
logger.error({ | ||
error: AppError.format(error), | ||
origin, | ||
}), | ||
); | ||
process.on("warning", (warn) => logger.error(AppError.format(warn))); | ||
Promise.resolve(cb(logger)).catch((e) => { | ||
unhandled(AppError.format(e)); | ||
}); | ||
} | ||
@@ -142,17 +145,1 @@ | ||
} | ||
/** | ||
* @returns {boolean} | ||
*/ | ||
export function isProduction() { | ||
return process.env.NODE_ENV === "production"; | ||
} | ||
/** | ||
* @returns {boolean} | ||
*/ | ||
export function isStaging() { | ||
return ( | ||
process.env.NODE_ENV !== "production" || process.env.IS_STAGING === "true" | ||
); | ||
} |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
30252
14
922
5
183
+ Added@lbu/insight@0.0.92(transitive)
+ Added@types/node@14.14.6(transitive)
- Removed@lbu/insight@0.0.91(transitive)
- Removed@types/node@14.14.5(transitive)
Updated@lbu/insight@0.0.92
Updated@types/node@14.14.6