-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Provide "ready helpers" as ReadyContext methods
- Loading branch information
Showing
12 changed files
with
67 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { promisify } from 'util' | ||
import stream from 'stream' | ||
import { ReadyContext } from './interfaces/ReadyContext' | ||
import { onceTcpPortUsed } from './util/onceTcpPortUsed' | ||
|
||
const delay = promisify(setTimeout) | ||
|
||
export function createReadyContext(output: stream.Readable): ReadyContext { | ||
return { | ||
onceTcpPortUsed, | ||
onceOutputLineIs: line => onceOutputLine(output, l => l === line), | ||
onceOutputLineIncludes: text => | ||
onceOutputLine(output, l => l.includes(text)), | ||
onceOutputLine: test => onceOutputLine(output, test), | ||
onceDelay: milliseconds => delay(milliseconds), | ||
} | ||
} | ||
|
||
function onceOutputLine( | ||
output: stream.Readable, | ||
test: (line: string) => boolean, | ||
): Promise<void> { | ||
return new Promise<void>(resolve => { | ||
const handler = (line: string) => { | ||
if (test(line)) { | ||
output.off('data', handler) | ||
resolve() | ||
} | ||
} | ||
output.on('data', handler) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,33 @@ | ||
import stream from 'stream' | ||
|
||
/** | ||
* Context object given as argument to each {@link ServiceConfig.ready} function | ||
*/ | ||
export interface ReadyContext { | ||
/** | ||
* Interleaved lines from stdout & stderr of the service. | ||
* | ||
* Each chunk is a utf8 string ending in '\n'. | ||
* Wait until the given TCP `port` (on the given `host`) is accepting connections. | ||
* The `port` is required. | ||
* The `host` defaults to "localhost". | ||
* | ||
* Can be used `as AsyncIterable<string>`. | ||
* Works by trying establish a TCP connection to the given port every 250 milliseconds. | ||
*/ | ||
onceTcpPortUsed: (port: number | string, host?: string) => Promise<void> | ||
|
||
/** | ||
* Wait until a line in the console output passes custom `test` | ||
*/ | ||
onceOutputLine: (test: (line: string) => boolean) => Promise<void> | ||
|
||
/** | ||
* Wait until a certain exact `line` appears in the console output | ||
*/ | ||
onceOutputLineIs: (line: string) => Promise<void> | ||
|
||
/** | ||
* Wait until a line including `text` appears in the console output | ||
*/ | ||
onceOutputLineIncludes: (text: string) => Promise<void> | ||
|
||
/** | ||
* Wait a predetermined length of time | ||
*/ | ||
output: stream.Readable | ||
onceDelay: (milliseconds: number) => Promise<void> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters