Skip to content

Commit

Permalink
Use split again instead of regex to catch target/input
Browse files Browse the repository at this point in the history
Command names can consist of any character, which can easily be caught with the split
  • Loading branch information
paescuj committed Jun 9, 2023
1 parent 5913018 commit a74765e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
4 changes: 1 addition & 3 deletions src/flow-control/input-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ it('forwards input stream to target index specified in input', () => {
controller.handle(commands);

inputStream.write('1:something');
inputStream.write(' 1:ignore_leading_whitespace');
inputStream.write('1:multi\nline\n');

expect(commands[0].stdin?.write).not.toHaveBeenCalled();
expect(commands[1].stdin?.write).toHaveBeenCalledTimes(3);
expect(commands[1].stdin?.write).toHaveBeenCalledTimes(2);
expect(commands[1].stdin?.write).toHaveBeenCalledWith('something');
expect(commands[1].stdin?.write).toHaveBeenCalledWith('ignore_leading_whitespace');
expect(commands[1].stdin?.write).toHaveBeenCalledWith('multi\nline\n');
});

Expand Down
24 changes: 11 additions & 13 deletions src/flow-control/input-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import * as defaults from '../defaults';
import { Logger } from '../logger';
import { FlowController } from './flow-controller';

const LINE_COMMAND_REGEX = /^\s*([\w-]+):(.+)$/s;

/**
* Sends input from concurrently through to commands.
*
Expand Down Expand Up @@ -59,26 +57,26 @@ export class InputHandler implements FlowController {
Rx.fromEvent(inputStream, 'data')
.pipe(map((data) => String(data)))
.subscribe((data) => {
let command: Command | undefined, targetId: string, input: string;
let command: Command | undefined, input: string;

const dataMatch = data.match(LINE_COMMAND_REGEX);
if (dataMatch && (command = commandsMap.get(dataMatch[1]))) {
targetId = dataMatch[1];
input = dataMatch[2];
} else {
// if the `targetId` matched by the Regex does not match a registered command,
// don't log an error, just fallback to `defaultInputTarget`
const dataParts = data.split(/:(.+)/s);
let target = dataParts[0];

targetId = this.defaultInputTarget.toString();
if (dataParts.length > 1 && (command = commandsMap.get(target))) {
input = dataParts[1];
} else {
// If `target` does not match a registered command,
// fallback to `defaultInputTarget` and forward the whole input data
target = this.defaultInputTarget.toString();
command = commandsMap.get(target);
input = data;
command = commandsMap.get(targetId);
}

if (command && command.stdin) {
command.stdin.write(input);
} else {
this.logger.logGlobalEvent(
`Unable to find command "${targetId}", or it has no stdin open\n`
`Unable to find command "${target}", or it has no stdin open\n`
);
}
});
Expand Down

0 comments on commit a74765e

Please sign in to comment.