Skip to content

Commit c178628

Browse files
committed
feat: Add the stdout and stderr streams to the worker API
- Also add the ability to configure whether to log stdout and stderr by default
1 parent 81072c3 commit c178628

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

src/DenoWorker.ts

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
Transferrable,
1616
} from './MessageTarget';
1717
import { MessagePort } from './MessageChannel';
18+
import { Stream, Readable, Duplex } from 'stream';
1819

1920
const DEFAULT_DENO_BOOTSTRAP_SCRIPT_PATH = __dirname.endsWith('src')
2021
? resolve(__dirname, '../deno/index.ts')
@@ -41,6 +42,18 @@ export interface DenoWorkerOptions {
4142
*/
4243
reload: boolean | string[];
4344

45+
/**
46+
* Whether to log stdout from the worker.
47+
* Defaults to true.
48+
*/
49+
logStdout: boolean;
50+
51+
/**
52+
* Whether to log stderr from the worker.
53+
* Defaults to true.
54+
*/
55+
logStderr: boolean;
56+
4457
/**
4558
* The permissions that the Deno worker should use.
4659
*/
@@ -115,6 +128,8 @@ export class DenoWorker {
115128
private _options: DenoWorkerOptions;
116129
private _ports: Map<number | string, MessagePortData>;
117130
private _terminated: boolean;
131+
private _stdout: Duplex;
132+
private _stderr: Duplex;
118133

119134
/**
120135
* Creates a new DenoWorker instance and injects the given script.
@@ -124,15 +139,34 @@ export class DenoWorker {
124139
this._onmessageListeners = [];
125140
this._pendingMessages = [];
126141
this._available = false;
142+
this._stdout = new Duplex();
143+
this._stdout.setEncoding('utf-8');
144+
this._stderr = new Duplex();
145+
this._stdout.setEncoding('utf-8');
146+
this._stderr.setEncoding('utf-8');
127147
this._options = Object.assign(
128148
{
129149
denoExecutable: 'deno',
130150
denoBootstrapScriptPath: DEFAULT_DENO_BOOTSTRAP_SCRIPT_PATH,
131151
reload: process.env.NODE_ENV !== 'production',
152+
logStdout: true,
153+
logStderr: true,
132154
permissions: {},
133155
},
134156
options || {}
135157
);
158+
159+
if (this._options.logStdout) {
160+
this.stdout.on('data', (data) => {
161+
console.log('[deno]', data);
162+
});
163+
}
164+
if (this._options.logStderr) {
165+
this.stderr.on('data', (data) => {
166+
console.log('[deno]', data);
167+
});
168+
}
169+
136170
this._ports = new Map();
137171
this._httpServer = createServer();
138172
this._server = new WSServer({
@@ -273,17 +307,19 @@ export class DenoWorker {
273307
...scriptArgs,
274308
]);
275309

276-
this._process.stdout.setEncoding('utf8');
277-
this._process.stderr.setEncoding('utf8');
278-
this._process.stdout.on('data', (data) => {
279-
console.log('[deno]', data);
280-
});
281-
this._process.stderr.on('data', (data) => {
282-
console.log('[deno]', data);
283-
});
310+
this._process.stdout?.pipe(this._stdout);
311+
this._process.stderr?.pipe(this._stderr);
284312
});
285313
}
286314

315+
get stdout() {
316+
return this._stdout;
317+
}
318+
319+
get stderr() {
320+
return this._stderr;
321+
}
322+
287323
/**
288324
* Represents an event handler for the "message" event, that is a function to be called when a message is recieved from the worker.
289325
*/

0 commit comments

Comments
 (0)