Skip to content

Commit 774968f

Browse files
committed
Porting complete, with build and docs. WIP continues with tests
1 parent 25e86f4 commit 774968f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+4912
-337
lines changed

bin/cjs/index.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"use strict";
2+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3+
if (k2 === undefined) k2 = k;
4+
var desc = Object.getOwnPropertyDescriptor(m, k);
5+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6+
desc = { enumerable: true, get: function() { return m[k]; } };
7+
}
8+
Object.defineProperty(o, k2, desc);
9+
}) : (function(o, m, k, k2) {
10+
if (k2 === undefined) k2 = k;
11+
o[k2] = m[k];
12+
}));
13+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
14+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15+
};
16+
Object.defineProperty(exports, "__esModule", { value: true });
17+
__exportStar(require("./types/enum"), exports);
18+
__exportStar(require("./types/pipe"), exports);
19+
__exportStar(require("./types/message"), exports);
20+
__exportStar(require("./plumbing"), exports);
21+
/*
22+
export {
23+
JunctionType,
24+
PipeMessageType,
25+
MessagePriority,
26+
QueueControlMessageType,
27+
FilterControlMessageType,
28+
JunctionMediatorNotification,
29+
} from "./types/enum";
30+
31+
export type {
32+
PipeMessage,
33+
FilterControlMessage,
34+
QueueControlMessage,
35+
} from "./types/message";
36+
37+
export type {
38+
IPipeFitting,
39+
IPipeAware,
40+
PipeListenerCallback,
41+
FilterControlFunction,
42+
} from "./types/pipe";
43+
44+
export { Pipe } from "./plumbing/Pipe";
45+
export { Queue } from "./plumbing/Queue";
46+
export { Filter } from "./plumbing/Filter";
47+
export { Junction } from "./plumbing/Junction";
48+
export { TeeMerge } from "./plumbing/TeeMerge";
49+
export { TeeSplit } from "./plumbing/TeeSplit";
50+
export { PipeListener } from "./plumbing/PipeListener";
51+
export { JunctionMediator } from "./plumbing/JunctionMediator";
52+
*/

bin/cjs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"type": "commonjs"}

bin/cjs/plumbing/Filter.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Filter = void 0;
4+
const Pipe_1 = require("./Pipe");
5+
const index_1 = require("../index");
6+
/**
7+
* Pipe Filter.
8+
*
9+
* Filters may modify the contents of messages before writing them to
10+
* their output pipe fitting. They may also have their parameters and
11+
* filter function passed to them by control message, as well as having
12+
* their Bypass/Filter operation mode toggled via control message.
13+
*/
14+
class Filter extends Pipe_1.Pipe {
15+
/**
16+
* Constructor.
17+
*
18+
* Optionally connect the output and set the parameters.
19+
*/
20+
constructor({ name, output, filter, params, }) {
21+
super(output);
22+
this.mode = index_1.FilterControlMessageType.FILTER;
23+
this.params = undefined;
24+
this.name = name;
25+
this.filter = filter;
26+
this.params = params;
27+
}
28+
/**
29+
* Handle the incoming message.
30+
*
31+
* If message type is normal, filter the message (unless in bypass mode)
32+
* and write the result to the output pipe fitting if the filter
33+
* operation is successful.
34+
*
35+
* The `FilterControlMessage.SET_PARAMS` message type tells the `Filter`
36+
* that the message class is FilterControlMessage, which it
37+
* casts the message to in order to retrieve the filter parameters
38+
* object if the message is addressed to this filter.
39+
*
40+
*
41+
* The `FilterControlMessage.SET_FILTER` message type tells the `Filter`
42+
* that the message class is FilterControlMessage, which it
43+
* casts the message to in order to retrieve the filter function.
44+
*
45+
*
46+
* The `FilterControlMessageType.BYPASS` message type tells the `Filter`
47+
* that it should go into Bypass mode operation, passing all normal
48+
* messages through unfiltered.
49+
*
50+
*
51+
* The `FilterControlMessage.FILTER` message type tells the `Filter`
52+
* that it should go into Filtering mode operation, filtering all
53+
* normal messages before writing out. This is the default
54+
* mode of operation and so this message type need only be sent to
55+
* cancel a previous BYPASS message.
56+
*
57+
*
58+
* The Filter only acts on the control message if it is targeted
59+
* to this named filter instance. Otherwise, it writes through to the
60+
* output.
61+
*
62+
* @return boolean True if the filter process does not throw an error and subsequent operations in the pipeline succeed.
63+
*/
64+
write(message) {
65+
var _a, _b, _c, _d, _e;
66+
let outputMessage;
67+
let success = true;
68+
// Filter normal messages
69+
switch (message.type) {
70+
case index_1.PipeMessageType.NORMAL:
71+
try {
72+
if (this.mode === index_1.FilterControlMessageType.FILTER) {
73+
outputMessage = this.applyFilter(message);
74+
}
75+
else {
76+
outputMessage = message;
77+
}
78+
success = ((_a = this.output) === null || _a === void 0 ? void 0 : _a.write(outputMessage)) || false;
79+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
80+
}
81+
catch (e) {
82+
success = false;
83+
}
84+
break;
85+
// Accept parameters from control message
86+
case index_1.FilterControlMessageType.SET_PARAMS:
87+
if (this.isTarget(message)) {
88+
this.params = message.params;
89+
}
90+
else {
91+
success = ((_b = this.output) === null || _b === void 0 ? void 0 : _b.write(message)) || false;
92+
}
93+
break;
94+
// Accept filter function from control message
95+
case index_1.FilterControlMessageType.SET_FILTER:
96+
if (this.isTarget(message)) {
97+
this.filter = message.filter;
98+
}
99+
else {
100+
success = ((_c = this.output) === null || _c === void 0 ? void 0 : _c.write(message)) || false;
101+
}
102+
break;
103+
// Toggle between Filter or Bypass operational modes
104+
case index_1.FilterControlMessageType.BYPASS:
105+
case index_1.FilterControlMessageType.FILTER:
106+
if (this.isTarget(message)) {
107+
this.mode = message.type;
108+
}
109+
else {
110+
success = ((_d = this.output) === null || _d === void 0 ? void 0 : _d.write(message)) || false;
111+
}
112+
break;
113+
// Write control messages for other fittings through
114+
default:
115+
success = ((_e = this.output) === null || _e === void 0 ? void 0 : _e.write(message)) || false;
116+
}
117+
return success;
118+
}
119+
/**
120+
* Is the message directed at this filter instance?
121+
*/
122+
isTarget(message) {
123+
return message.name === this.name;
124+
}
125+
/**
126+
* Filter the message.
127+
*/
128+
applyFilter(message) {
129+
this.filter(message, this.params);
130+
return message;
131+
}
132+
}
133+
exports.Filter = Filter;

bin/cjs/plumbing/Junction.js

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Junction = void 0;
4+
const index_1 = require("../index");
5+
const enum_1 = require("../types/enum");
6+
/**
7+
* Pipe Junction.
8+
*
9+
* Manages Pipes for a Module.
10+
*
11+
* When you register a Pipe with a Junction, it is
12+
* declared as being an INPUT pipe or an OUTPUT pipe.
13+
*
14+
* You can retrieve or remove a registered Pipe by name,
15+
* check to see if a Pipe with a given name exists,or if
16+
* it exists AND is an INPUT or an OUTPUT Pipe.
17+
*
18+
* You can send an `PipeMessage` on a named INPUT Pipe
19+
* or add a `PipeListener` to registered INPUT Pipe.
20+
*/
21+
class Junction {
22+
constructor() {
23+
/**
24+
* The names of the INPUT pipes
25+
*/
26+
this.inputPipes = new Set();
27+
/**
28+
* The names of the OUTPUT pipes
29+
*/
30+
this.outputPipes = new Set();
31+
/**
32+
* The map of pipe names to their pipes
33+
*/
34+
this.pipesMap = new Map();
35+
/**
36+
* The map of pipe names to their types
37+
* @type {Map<string, JunctionType.INPUT | JunctionType.OUTPUT>}
38+
*/
39+
this.pipeTypesMap = new Map();
40+
}
41+
/**
42+
* Register a pipe with the junction.
43+
*
44+
* Pipes are registered by unique name and type,
45+
* which must be either `Junction.INPUT`
46+
* or `Junction.OUTPUT`.
47+
*
48+
* NOTE: You cannot have an INPUT pipe and an OUTPUT
49+
* pipe registered with the same name. All pipe names
50+
* must be unique regardless of type.
51+
*
52+
* @return boolean true if successfully registered. false if another pipe exists by that name.
53+
*/
54+
registerPipe({ name, type, pipe, }) {
55+
if (!name)
56+
return false;
57+
let success = true;
58+
if (!this.pipesMap.get(name)) {
59+
this.pipesMap.set(name, pipe);
60+
this.pipeTypesMap.set(name, type);
61+
switch (type) {
62+
case enum_1.JunctionType.INPUT:
63+
this.inputPipes.add(name);
64+
break;
65+
case enum_1.JunctionType.OUTPUT:
66+
this.outputPipes.add(name);
67+
break;
68+
default:
69+
success = false;
70+
}
71+
}
72+
else {
73+
success = false;
74+
}
75+
return success;
76+
}
77+
/**
78+
* Does this junction have a pipe by this name?
79+
*
80+
* @param name the pipe to check for
81+
* @return boolean whether as pipe is registered with that name.
82+
*/
83+
hasPipe(name) {
84+
return this.pipesMap.get(name) !== null;
85+
}
86+
/**
87+
* Does this junction have an INPUT pipe by this name?
88+
*
89+
* @param name the pipe to check for
90+
* @return boolean whether an INPUT pipe is registered with that name.
91+
*/
92+
hasInputPipe(name) {
93+
return (this.hasPipe(name) && this.pipeTypesMap.get(name) === enum_1.JunctionType.INPUT);
94+
}
95+
/**
96+
* Does this junction have an OUTPUT pipe by this name?
97+
*
98+
* @param name the pipe to check for
99+
* @return boolean whether an OUTPUT pipe is registered with that name.
100+
*/
101+
hasOutputPipe(name) {
102+
return (this.hasPipe(name) && this.pipeTypesMap.get(name) === enum_1.JunctionType.OUTPUT);
103+
}
104+
/**
105+
* Remove the pipe with this name if it is registered.
106+
*
107+
* NOTE: You cannot have an INPUT pipe and an OUTPUT
108+
* pipe registered with the same name. All pipe names
109+
* must be unique regardless of type.
110+
*
111+
* @param name the pipe to remove
112+
*/
113+
removePipe(name) {
114+
if (this.hasPipe(name)) {
115+
let type = this.pipeTypesMap.get(name);
116+
let pipesList;
117+
switch (type) {
118+
case enum_1.JunctionType.INPUT:
119+
pipesList = this.inputPipes;
120+
break;
121+
case enum_1.JunctionType.OUTPUT:
122+
pipesList = this.outputPipes;
123+
break;
124+
default:
125+
pipesList = new Set();
126+
break;
127+
}
128+
for (const item of pipesList) {
129+
if (item === name) {
130+
pipesList.delete(item);
131+
break;
132+
}
133+
}
134+
this.pipesMap.delete(name);
135+
this.pipeTypesMap.delete(name);
136+
}
137+
}
138+
/**
139+
* Retrieve the named pipe.
140+
*
141+
* @param name the pipe to retrieve
142+
* @return IPipeFitting the pipe registered by the given name if it exists
143+
*/
144+
retrievePipe(name) {
145+
return this.pipesMap.get(name);
146+
}
147+
/**
148+
* Add a PipeListener to an INPUT pipe.
149+
*
150+
* NOTE: there can only be one PipeListener per pipe,
151+
* and the listener function must accept an PipeMessage
152+
* as its sole argument.
153+
*
154+
* @param inputPipeName the INPUT pipe to add a PipeListener to
155+
* @param listener the callback function to invoke
156+
*/
157+
addPipeListener(inputPipeName, listener) {
158+
let success = false;
159+
if (this.hasInputPipe(inputPipeName)) {
160+
let pipe = this.pipesMap.get(inputPipeName);
161+
success = (pipe === null || pipe === void 0 ? void 0 : pipe.connect(new index_1.PipeListener(listener))) || false;
162+
}
163+
return success;
164+
}
165+
/**
166+
* Send a message on an OUTPUT pipe.
167+
*
168+
* @param outputPipeName the OUTPUT pipe to send the message on
169+
* @param message the PipeMessage to send
170+
*/
171+
sendMessage(outputPipeName, message) {
172+
let success = false;
173+
if (this.hasOutputPipe(outputPipeName)) {
174+
let pipe = this.pipesMap.get(outputPipeName);
175+
success = (pipe === null || pipe === void 0 ? void 0 : pipe.write(message)) || false;
176+
}
177+
return success;
178+
}
179+
}
180+
exports.Junction = Junction;

0 commit comments

Comments
 (0)