From 331738a0f69e00d12d44f27a717b3b73733b485a Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Wed, 6 Mar 2024 11:57:09 -0300 Subject: src/hero.mjs: Add rmIf() and mkfifo() --- src/hero.mjs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'src/hero.mjs') diff --git a/src/hero.mjs b/src/hero.mjs index 53f3086..6b6fa77 100644 --- a/src/hero.mjs +++ b/src/hero.mjs @@ -1,7 +1,10 @@ -import assert from "node:assert/strict"; -import crypto from "node:crypto"; -import http from "node:http"; -import process from "node:process"; +import assert from "node:assert/strict"; +import child_process from "node:child_process"; +import crypto from "node:crypto"; +import fs from "node:fs"; +import http from "node:http"; +import process from "node:process"; +import util from "node:util"; import * as u from "./utils.mjs"; @@ -79,13 +82,13 @@ export const firstParamMatch = (tree, segments, params) => { const subtree = tree[seg]; if (subtree) { const submatch = firstParamMatch(subtree, nextSegments, params); - // propagation of the end of recursion + /// propagation of the end of recursion if (submatch) { return submatch; } } - // literal matching failed, we now look for patterns that might match + /// literal matching failed, we now look for patterns that might match const paramOptions = Object.keys(tree) .filter(s => s.startsWith(":")) .sort(u.strSortFn); @@ -326,6 +329,15 @@ export const lineHandlerFn = ({ export const lineHandler = lineHandlerFn(); +export const rmIf = path => { + if (fs.existsSync(path)) { + fs.unlinkSync(path); + } +}; + +export const mkfifo = path => + child_process.execFileSync("mkfifo", [path]); + export const buildRoutes = (routes, globalInterceptors = []) => routes.reduce( (acc, [methods, path, handlerFn, interceptors = []]) => -- cgit v1.2.3