1
0
Fork 0
mirror of https://github.com/sbrl/terrain50-cli.git synced 2024-06-29 10:54:56 +00:00
terrain50-cli/src/Helpers/GzipChildProcess.mjs
Starbeamrainbowlabs d1b6e9e641
Use GzipChildStream again
....spawn-stream appears to be causing issues in the new nimrod data 
downloader
2020-10-05 18:58:11 +01:00

62 lines
1.5 KiB
JavaScript

"use strict";
import EventEmitter from 'events';
import child_process from 'child_process';
import { end_safe } from '../../helpers/StreamHelpers.mjs';
/**
* Spawns and manages a gzip child process.
* @deprecated Use spawn-stream instead
* @extends EventEmitter
*/
class GzipChildProcess extends EventEmitter {
get stdin() { return this.child_process.stdin; }
get stdout() { return this.child_process.stdout; }
get stderr() { return this.child_process.stderr; }
constructor(auto_start = true) {
super();
this.child_process = null;
this.has_exited = false;
if(auto_start)
this.start();
}
start() {
if(this.child_process != null)
throw new Error("Invalid Operation: Can't start the child process, since it's already been started.");
this.child_process = child_process.spawn(
"gzip", [], {
// Pipe stdin + stdout; send error to the parent process
stdio: [ "pipe", "pipe", "inherit" ]
}
);
this.child_process.on("exit", () => {
this.has_exited = true;
this.emit("exit");
});
// FUTURE: Perhaps just throwing the error would be a better choice?
this.child_process.on("error", (error) => {
this.emit("error", error);
});
}
/**
* Returns a Promise that resolves when the gzip process exits.
* If the gzip child process has already exited, then it resolves immediately.
* @return {Promise}
*/
async end_gracefully() {
await end_safe(this.stdin);
if(this.has_exited) return;
await EventEmitter.once(this, "exit");
}
}
export default GzipChildProcess;