summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-03-06 20:25:58 -0300
committerEuAndreh <eu@euandre.org>2024-03-06 20:26:00 -0300
commit1a15b80316b9cdbe56b0cac48ecc9e09cfc3182d (patch)
treebc71af675af5d6fff78c4efaafe3b600f6e94d6c
parentsrc/hero.mjs: Add minimalistic "ping" action for doing smoke tests in production (diff)
downloadpapod-1a15b80316b9cdbe56b0cac48ecc9e09cfc3182d.tar.gz
papod-1a15b80316b9cdbe56b0cac48ecc9e09cfc3182d.tar.xz
src/hero.mjs: Better handling of default values for arguments
Now one can give a partial specification of the object and still get the other attributes to have their default values.
-rw-r--r--src/hero.mjs30
1 files changed, 10 insertions, 20 deletions
diff --git a/src/hero.mjs b/src/hero.mjs
index 7ac7c19..451198d 100644
--- a/src/hero.mjs
+++ b/src/hero.mjs
@@ -178,12 +178,9 @@ export const makeLogger = (writerFn = console.error) => ({
export const log = makeLogger();
export const interceptorsFn = ({
- uuidFn,
- logger,
-} = {
- uuidFn: crypto.randomUUID,
- logger: log,
-}) => ({
+ uuidFn = crypto.randomUUID,
+ logger = log,
+} = {}) => ({
requestId: (req, next) => next({ ...req, id: uuidFn() }),
logged: async (req, next) => {
const { id, url, method } = req;
@@ -256,10 +253,8 @@ export const wrapHandler = (fn, arr) =>
chainInterceptors(arr.concat([ (req, _next) => fn(req) ]));
export const actionsFn = ({
- logger,
-} = {
- logger: log,
-}) => ({
+ logger = log,
+} = {}) => ({
"toggle-debug-env": action => {
const before = process.env.DEBUG;
if (process.env.DEBUG) {
@@ -291,12 +286,9 @@ export const actionsFn = ({
export const actions = actionsFn();
export const lineHandlerFn = ({
- logger,
- actionsMap,
-} = {
- logger: log,
- actionsMap: actions,
-}) => line => {
+ logger = log,
+ actionsMap = actions,
+} = {}) => line => {
let cmd = null;
try {
cmd = JSON.parse(line);
@@ -359,10 +351,8 @@ export const makeLineEmitter = fn => {
};
export const makePipeReaderFn = ({
- lineFn,
-} = {
- lineFn: lineHandler
-}) => path => {
+ lineFn = lineHandler,
+} = {}) => path => {
mkfifo(path);
fs.createReadStream(path, "UTF-8").on("data", makeLineEmitter(lineFn));
};