diff --git a/lib/error.js b/lib/error.js index 14cb8bf..fdfa89f 100644 --- a/lib/error.js +++ b/lib/error.js @@ -27,7 +27,8 @@ const ErrorHandler = class { 6: 'MPV is already running', 7: 'Could not send IPC message', 8: 'MPV is not running', - 9: 'Unsupported protocol' + 9: 'Unsupported protocol', + 10: 'IPC connection error' } } diff --git a/lib/ipcInterface/_events.js b/lib/ipcInterface/_events.js index 5598464..5a3b81a 100644 --- a/lib/ipcInterface/_events.js +++ b/lib/ipcInterface/_events.js @@ -55,7 +55,8 @@ const events = { if(message.length > 0){ const JSONmessage = JSON.parse(message); // if there was a request_id it was a request message - if(JSONmessage.request_id && JSONmessage.request_id !== 0){ + if(JSONmessage.request_id && JSONmessage.request_id !== 0 + && this.ipcRequests[JSONmessage.request_id] !== undefined){ // resolve promise if(JSONmessage.error === 'success'){ // resolve the request diff --git a/lib/ipcInterface/ipcInterface.js b/lib/ipcInterface/ipcInterface.js index a0c5756..83536c4 100644 --- a/lib/ipcInterface/ipcInterface.js +++ b/lib/ipcInterface/ipcInterface.js @@ -190,7 +190,8 @@ ipcInterface.prototype = Object.assign({ return new Promise((resolve, reject) => { // reject the promise if the socket is not running, this is only the case if the mpv player is not running - if (this.socket.destroyed){ + // reject also if the socket is not writable + if (this.socket.destroyed || !this.socket.writable){ return reject(this.errorHandler.errorMessage(8, util.getCaller())); } diff --git a/lib/mpv/_controls.js b/lib/mpv/_controls.js index 07b2de3..3901bde 100644 --- a/lib/mpv/_controls.js +++ b/lib/mpv/_controls.js @@ -29,6 +29,10 @@ const controls = { let started = false; // socket to observe if the file is being played back const observeSocket = new net.Socket(); + // catch EventEmitter error to avoid crash if socket is no longer available + observeSocket.on('error', (error) => { + return reject(this.errorHandler.errorMessage(10, error.message, 'play()')); + }); observeSocket.connect({path: this.options.socket}, async () => { // this starts the playback when mpv is idle but songs are queued in the playlist await this.setProperty('playlist-pos', 0); @@ -112,6 +116,10 @@ const controls = { // tracks if the seek event has been emitted let seek_event_started = false; const observeSocket = new net.Socket(); + // catch EventEmitter error to avoid crash if socket is no longer available + observeSocket.on('error', (error) => { + return reject(this.errorHandler.errorMessage(10, error.message, 'seek()')); + }); observeSocket.connect({path: this.options.socket}, async () => { await this.command('seek', [seconds, mode, 'exact']); }).on('data', (data) => { diff --git a/lib/mpv/_events.js b/lib/mpv/_events.js index 15c34dc..a37e932 100644 --- a/lib/mpv/_events.js +++ b/lib/mpv/_events.js @@ -144,6 +144,10 @@ const events = { let timeout = 0; // promise to watch the socket output new Promise((resolve, reject) => { + // catch EventEmitter error to avoid crash if socket is no longer available + observeSocket.on('error', (error) => { + return reject(this.errorHandler.errorMessage(10, error.message, 'messageHandler()')); + }); // connect a tempoary socket to the mpv player observeSocket.connect({path: this.options.socket}, () =>{ // receive the data diff --git a/lib/mpv/_playlist.js b/lib/mpv/_playlist.js index 0a6247d..276fe8c 100644 --- a/lib/mpv/_playlist.js +++ b/lib/mpv/_playlist.js @@ -42,6 +42,10 @@ const playlist = { .then(() => { return new Promise((resolve, reject) => { // socket to observe the command let observeSocket = net.Socket(); + // catch EventEmitter error to avoid crash if socket is no longer available + observeSocket.on('error', (error) => { + return reject(this.errorHandler.errorMessage(10, error.message, 'loadPlaylist()')); + }); observeSocket.connect({path: this.options.socket}, () =>{ // send the command to mpv this.command('loadlist', [playlist, mode]); @@ -161,6 +165,10 @@ const playlist = { return new Promise((resolve, reject) => { // socket to observe the command const observeSocket = net.Socket(); + // catch EventEmitter error to avoid crash if socket is no longer available + observeSocket.on('error', (error) => { + return reject(this.errorHandler.errorMessage(10, error.message, 'append()')); + }); observeSocket.connect({path: this.options.socket}, async () =>{ // send the skip command to mpv await this.command('playlist-next', [mode]); @@ -249,6 +257,10 @@ const playlist = { return new Promise((resolve, reject) => { // socket to observe the command const observeSocket = net.Socket(); + // catch EventEmitter error to avoid crash if socket is no longer available + observeSocket.on('error', (error) => { + return reject(this.errorHandler.errorMessage(10, error.message, 'prev()')); + }); observeSocket.connect({path: this.options.socket}, async () =>{ // send the prev command to mpv await this.command('playlist-prev', [mode]); @@ -318,6 +330,10 @@ const playlist = { return new Promise((resolve, reject) => { // socket to observe the command const observeSocket = net.Socket(); + // catch EventEmitter error to avoid crash if socket is no longer available + observeSocket.on('error', (error) => { + return reject(this.errorHandler.errorMessage(10, error.message, 'jump()')); + }); observeSocket.connect({path: this.options.socket}, async () =>{ // send the command to jump in the playlist to mpv await this.setProperty('playlist-pos', position); diff --git a/lib/mpv/_startStop.js b/lib/mpv/_startStop.js index f598d06..5a0108c 100644 --- a/lib/mpv/_startStop.js +++ b/lib/mpv/_startStop.js @@ -118,6 +118,10 @@ const startStop = { // socket to check for the idle event to check if mpv fully loaded and // actually running const observeSocket = net.Socket(); + // catch EventEmitter error to avoid crash if socket is no longer available + observeSocket.on('error', (error) => { + return reject(this.errorHandler.errorMessage(10, error.message, 'start()')); + }); observeSocket.connect({path: this.options.socket}, () => { // send any message to see if there's a MPV instance responding observeSocket.write(JSON.stringify({ diff --git a/lib/mpv/mpv.js b/lib/mpv/mpv.js index d5a7e9e..4ee6ec4 100644 --- a/lib/mpv/mpv.js +++ b/lib/mpv/mpv.js @@ -113,6 +113,10 @@ mpv.prototype = Object.assign({ await new Promise ((resolve, reject) => { // socket to observe the command const observeSocket = net.Socket(); + // catch EventEmitter error to avoid crash if socket is no longer available + observeSocket.on('error', (error) => { + return reject(this.errorHandler.errorMessage(10, error.message, 'load()')); + }); observeSocket.connect({path: this.options.socket}, async () =>{ // send the command to mpv await this.command('loadfile', options ? [source, mode].concat(util.formatOptions(options)) : [source, mode]);