Repository
Current version released
4 years ago
Dependencies
std
deno-pipeline
Configurable data pipeline in Deno.
Data processing defined by Handler
s like Unix pipeline.
export type 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 {
buildHandler(conf?: Map<string, any>): 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
HandlerBuilder
and set it to handlerBuilders
Define a import {
Handler,
Res,
Status,
HandlerBuilder,
buildLine
} from "https://deno.land/x/pipeline/mod.ts";
class BuilderSquare implements HandlerBuilder {
buildHandler(conf?: Map<string, any>): Handler {
return {
async handle(res: Res): Promise<Res> {
res.data = res.data * res.data;
return res;
}
};
}
}
const testBuilders = {
getBuilder(name: string): HandlerBuilder {
const builder = new Map<string, HandlerBuilder>([
["square", HandlerBuilderSquare],
]).get(name);
if (!builder) {
throw Error("builder not found");
}
return builder;
},
};
Line
with config
Build a builderName
: key inTestBuilders
builderConf
: pass toHandlerBuilder
to build aHandler
let conf = [
{
builderName: "square",
timeout: 100,
required: true
}
];
let line = buildLine(conf, testBuilders);
line
.handle({ status: Status.New, data: 2 })
.then(res => console.log("data:", res.data))
.catch(res => console.error(res.message));
// Output:
// data: 4
Line
contains a referenced Handler
Build a refName
: the name of exsitingHandler
in thehandlers
const handlers = {
getHandler(name: string): Handler {
return builders.getBuilder("square").buildHandler();
},
};
let refConf = [
{
refName: "my_square",
timeout: 100,
required: true
},
{
builderName: "square",
timeout: 100,
required: true
}
];
let refLine = buildLine(refConf, testBuilders, 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
Line
contains parallel pipes
Build a Array Item will act as a Parallel
, return a list of data returned by every pipe.
let parallelConf = [
{
refName: "my_square",
timeout: 100,
required: true
},
[
{
builderName: "square",
timeout: 100,
required: true
},
{
builderName: "square",
timeout: 100,
required: true
}
]
];
let parallelLine = buildLine(parallelConf, testBuilders, 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 ]
handleVerbosely
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 ]