diff options
author | EuAndreh <eu@euandre.org> | 2024-03-06 12:14:10 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-03-06 12:14:10 -0300 |
commit | f0d33b9b21b3c739ab20d8e2b60c39694350321a (patch) | |
tree | 2a0eeeb6c3e133d43d6f94734d2ba7b642d6603a /src | |
parent | src/utils.mjs: Add first(), rest(), butlast() and last() (diff) | |
download | papod-f0d33b9b21b3c739ab20d8e2b60c39694350321a.tar.gz papod-f0d33b9b21b3c739ab20d8e2b60c39694350321a.tar.xz |
src/hero.mjs: Add makeLineEmitter()
Diffstat (limited to 'src')
-rw-r--r-- | src/hero.mjs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/hero.mjs b/src/hero.mjs index 6b6fa77..8cbbe36 100644 --- a/src/hero.mjs +++ b/src/hero.mjs @@ -338,6 +338,25 @@ export const rmIf = path => { export const mkfifo = path => child_process.execFileSync("mkfifo", [path]); +export const makeLineEmitter = fn => { + let data = ""; + return chunk => { + const segments = chunk.split("\n"); + assert.ok(segments.length > 0); + + if (segments.length === 1) { + data += segments[0]; + return; + } + + [ + data + u.first(segments), + ...u.butlast(u.rest(segments)), + ].forEach(fn); + data = u.last(segments); + }; +}; + export const buildRoutes = (routes, globalInterceptors = []) => routes.reduce( (acc, [methods, path, handlerFn, interceptors = []]) => |