Skip to content

Commit b7a9f63

Browse files
committed
add input/output events
1 parent fec2051 commit b7a9f63

3 files changed

Lines changed: 9 additions & 1 deletion

File tree

.github/workflows/ci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ on:
33
push:
44
branches:
55
- master
6+
- dev
67
jobs:
78
build:
89
runs-on: ubuntu-latest

lib/syncline.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ const LF = Buffer.from( [ 0x0A]);
148148

149149
const EVENT_STDOUT = 'stdout';
150150
const EVENT_STDERR = 'stderr';
151+
const EVENT_INPUT = 'input';
152+
const EVENT_OUTPUT = 'output';
151153
const EVENT_EXIT = 'exit';
152154

153155
const TRACE_LEVEL_OFF = 0;
@@ -257,6 +259,7 @@ export class Syncline extends EventEmitter {
257259
console.log( `${ performance.now()}: main: ${ this.#control[ INDEX_LENGTH]}B written, setting timeout=${ Math.max( 0, timeout)}ms, state=${ STATE_NAMES[ STATE_INPUT_COMPLETE]}`);
258260
Atomics.store( this.#control, INDEX_STATE, STATE_INPUT_COMPLETE);
259261
Atomics.notify( this.#control, INDEX_STATE, 1);
262+
this.emit( EVENT_INPUT, inputLine);
260263

261264
// step 2. receive output
262265
Atomics.wait( this.#control, INDEX_STATE, STATE_INPUT_COMPLETE);
@@ -277,7 +280,9 @@ export class Syncline extends EventEmitter {
277280
if( this.#traceLevel >= TRACE_LEVEL_PROTOCOL)
278281
console.log( `${ performance.now()}: main: state changed to ${ STATE_NAMES[ outputState]}, reading final ${ length}B and returning`);
279282
buffer.push( Buffer.from( this.#shared, OFFSET_DATA, length));
280-
return Buffer.concat( buffer).toString( 'utf-8');
283+
const outputLine = Buffer.concat( buffer).toString( 'utf-8');
284+
this.emit( EVENT_OUTPUT, outputLine);
285+
return outputLine;
281286
case STATE_ERROR:
282287
if( this.#traceLevel >= TRACE_LEVEL_PROTOCOL)
283288
console.log( `${ performance.now()}: main: state changed to ${ STATE_NAMES[ outputState]}, reading ${ length}B and throwing`);

test/test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { Syncline} from '@arcticnotes/syncline';
44

55
TEST( 'smoke test', async() => {
66
const syncline = await Syncline.spawn( 'stdbuf', [ '-oL', 'tr', 'a-z', 'A-Z'], { trace: 3});
7+
syncline.on( 'input', line => console.log( `input (length=${ line.length}):`, line));
8+
syncline.on( 'output', line => console.log( `output (length=${ line.length}):`, line));
79
ASSERT.equal( syncline.exchange( 'Hello, world!'), 'HELLO, WORLD!');
810
await new Promise( resolve => setTimeout( resolve, 500));
911
await syncline.close();

0 commit comments

Comments
 (0)