Skip to content

Commit ac860ac

Browse files
committed
fix: sync error in onceOutputLine callback should cause async rejection
1 parent 8aa626a commit ac860ac

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/createReadyContext.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { promisify } from "util";
22
import stream from "stream";
33
import { ReadyContext } from "./interfaces/ReadyContext";
44
import { onceTcpPortUsed } from "./util/onceTcpPortUsed";
5+
import { onceOutputLine } from "./util/onceOutputLine";
56

67
const delay = promisify(setTimeout);
78

@@ -14,15 +15,3 @@ export function createReadyContext(output: stream.Readable): ReadyContext {
1415
onceDelay: milliseconds => delay(milliseconds),
1516
};
1617
}
17-
18-
function onceOutputLine(output: stream.Readable, test: (line: string) => boolean): Promise<void> {
19-
return new Promise<void>(resolve => {
20-
const handler = (line: string) => {
21-
if (test(line)) {
22-
output.off("data", handler);
23-
resolve();
24-
}
25-
};
26-
output.on("data", handler);
27-
});
28-
}

src/util/onceOutputLine.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import stream from "stream";
2+
3+
export function onceOutputLine(
4+
output: stream.Readable,
5+
test: (line: string) => boolean,
6+
): Promise<void> {
7+
return new Promise<void>((resolve, reject) => {
8+
const handler = (line: string) => {
9+
let testResult = false;
10+
try {
11+
testResult = test(line);
12+
} catch (error) {
13+
cleanup();
14+
reject(error);
15+
return;
16+
}
17+
if (testResult) {
18+
cleanup();
19+
resolve();
20+
}
21+
};
22+
output.on("data", handler);
23+
const cleanup = () => output.off("data", handler);
24+
});
25+
}

0 commit comments

Comments
 (0)