Configurable data pipeline in Deno.
Data processing defined by Handler
s like Unix pipeline.
export interface Res {
status: Status;
meta?: object;
data?: any;
message?: string;
}
export interface Handler {
handle(res: Res): Promise<Res>;
handleVerbosely?(res: Res): Promise<Res[]>;
}
Based on the Handler
, we need to make some tools:
HandlerBuilder
: build aHandler
with config.
export interface HandlerBuilder {
build(conf?: object): Handler;
}
-
Pipe
:- wraps a
Handler
with options:timeout
/required
/defaultValue
. - A Pipe can build with a
HandlerBuilder
or referenced by a existingHandler
. - Throws error when
Handler
timeout or failed. - Use the
defaultValue
when a notrequired
Handler
failed or timout.
- wraps a
-
Parallel
:- contains a list of
Pipe
, handles the pipes concurrently. - A
Parallel
is aHandler
.
- contains a list of
-
Line
:- contains a list of
Pipe
, handles the pipes sequentially. - A
Line
is aHandler
.
- contains a list of
import {
Handler,
Res,
Status,
HandlerBuilder,
handelrBuilders,
buildLine
} from "https://deno.land/x/pipeline/mod.ts";
class BuilderSquare implements HandlerBuilder {
build(conf?: Map<string, any>): Handler {
return {
async handle(res: Res): Promise<Res> {
res.data = res.data * res.data;
return res;
}
};
}
}
handelrBuilders.set("square", new BuilderSquare())
builderName
: key inhandlerBuilders
builderConf
: pass toHandlerBuilder
to build aHandler
let conf = [
{
builderName: "square",
timeout: 100,
required: true
}
];
let line = buildLine(conf);
line
.handle({ status: Status.New, data: 2 })
.then(res => console.log("data:", res.data))
.catch(res => console.error(res.message));
// Output:
// data: 4
refId
: exsiting key ofHandler
in thehandlers
let handlers = new Map<string, Handler>([["my_square", line]]);
let refConf = [
{
refId: "my_square",
timeout: 100,
required: true
},
{
builderName: "square",
timeout: 100,
required: true
}
];
let refLine = buildLine(refConf, handlers);
refLine
.handle({ status: Status.New, data: 2 })
.then(res => console.log("ref line data:", res.data))
.catch(res => console.error(res.message));
// Output:
// ref line data: 16
Array Item will act as a Parallel
, return a list of data returned by every pipe.
let parallelConf = [
{
refId: "my_square",
timeout: 100,
required: true
},
[
{
builderName: "square",
timeout: 100,
required: true
},
{
builderName: "square",
timeout: 100,
required: true
}
]
];
let parallelLine = buildLine(parallelConf, handlers);
parallelLine
.handle({ status: Status.New, data: 2 })
.then(res => console.log("parallel line data:", res.data))
.catch(res => console.error(res.message))
// Output:
// parallel line data: [ 16, 16 ]
Returns a list of Res
returned by every pipe;
parallelLine
.handleVerbosely({ status: Status.New, data: 2 })
.then(reses =>
reses.forEach((res, i) => console.log(i, "verbosely data:", res.data))
)
.catch(reses => console.error(reses));
// Output:
// 0 verbosely data: 4
// 1 verbosely data: [ 16, 16 ]