Skip to content

Commit

Permalink
Refine data piping to PassThrough for stream and optimize highWaterMa…
Browse files Browse the repository at this point in the history
…rk for client streams #47
  • Loading branch information
Lillifee committed Mar 20, 2023
1 parent c127c0c commit 4778b51
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
8 changes: 0 additions & 8 deletions src/server/common.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
// import { dirname } from 'path';
// import { fileURLToPath } from 'url';

// // TODO To start the server with ts-node (ts-node-esm, we have to use the import.meta.url)
// // For webpack we have to use __dirname, else the path is bundled into the server.js file.
// // For esbuild we have to define the format to esm with other problems.
// export const curDirName = dirname(fileURLToPath(import.meta.url));

export const curDirName = __dirname;
6 changes: 4 additions & 2 deletions src/server/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const createRaspiControl = (settingsHelper: SettingsHelper): RaspiControl
const actionProcess = spawnProcess();
const streamProcess = spawnProcess({
stdioOptions: ['ignore', 'pipe', 'inherit'],
resolveOnData: true,
stream: true,
});

streamProcess.stream.on('data', (chunk: unknown) =>
Expand Down Expand Up @@ -59,7 +59,9 @@ export const createRaspiControl = (settingsHelper: SettingsHelper): RaspiControl
};

const getStream = () => {
const clientStream: ClientStream = { stream: new PassThrough() };
// TODO adjustable highwatermark
const clientStream: ClientStream = { stream: new PassThrough({ highWaterMark: 128 * 1024 }) };

clientStream.stream.once('close', () => {
streams = streams.filter((x) => x != clientStream);
});
Expand Down
16 changes: 11 additions & 5 deletions src/server/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface SpawnProcess {
*/
export const spawnProcess = (options?: {
stdioOptions?: StdioOptions;
resolveOnData?: boolean;
stream?: boolean;
}): SpawnProcess => {
let process: ChildProcess | undefined;
const stream = new PassThrough();
Expand All @@ -54,14 +54,20 @@ export const spawnProcess = (options?: {
logger.log(command, spawnArgs.join(' '));

process = spawn(command, spawnArgs, { stdio: options?.stdioOptions });
if (options?.resolveOnData) {
process.stdout?.once('data', () => resolve());
}
process.on('error', (e) => reject(e));
process.on('exit', () => resolve());

process.stdout?.pipe(stream, { end: false });
process.stderr?.on('data', (d) => logger.log(removeNewlines(d)));

if (options?.stream) {
process.stdout?.once('data', () => resolve());
process.stdout?.pipe(stream, { end: false });
} else {
process.stdout?.on('data', logger.info);
}
});

return { start, stop, running, stream };
};

const removeNewlines = (data: unknown) => String(data).replace(/^\s+|\s+$/g, '');

0 comments on commit 4778b51

Please sign in to comment.