diff --git a/src/shared/message_handler.js b/src/shared/message_handler.js index c0adeb460..2a3178255 100644 --- a/src/shared/message_handler.js +++ b/src/shared/message_handler.js @@ -31,7 +31,13 @@ const StreamKind = { }; function wrapReason(reason) { - if (typeof reason !== 'object') { + if (typeof PDFJSDev === 'undefined' || + PDFJSDev.test('!PRODUCTION || TESTING')) { + assert(reason instanceof Error || + (typeof reason === 'object' && reason !== null), + 'wrapReason: Expected "reason" to be a (possibly cloned) Error.'); + } + if (typeof reason !== 'object' || reason === null) { return reason; } switch (reason.name) { @@ -334,20 +340,6 @@ MessageHandler.prototype = { const streamId = data.streamId; const comObj = this.comObj; - let deleteStreamController = () => { - // Delete the `streamController` only when the start, pull, and cancel - // capabilities have settled, to prevent `TypeError`s. - Promise.all([ - this.streamControllers[streamId].startCall, - this.streamControllers[streamId].pullCall, - this.streamControllers[streamId].cancelCall - ].map(function(capability) { - return capability && capability.promise.catch(function() { }); - })).then(() => { - delete this.streamControllers[streamId]; - }); - }; - switch (data.stream) { case StreamKind.START_COMPLETE: if (data.success) { @@ -423,14 +415,14 @@ MessageHandler.prototype = { } this.streamControllers[streamId].isClosed = true; this.streamControllers[streamId].controller.close(); - deleteStreamController(); + this._deleteStreamController(streamId); break; case StreamKind.ERROR: assert(this.streamControllers[streamId], 'error should have stream controller'); this.streamControllers[streamId].controller.error( wrapReason(data.reason)); - deleteStreamController(); + this._deleteStreamController(streamId); break; case StreamKind.CANCEL_COMPLETE: if (data.success) { @@ -439,7 +431,7 @@ MessageHandler.prototype = { this.streamControllers[streamId].cancelCall.reject( wrapReason(data.reason)); } - deleteStreamController(); + this._deleteStreamController(streamId); break; case StreamKind.CANCEL: if (!this.streamSinks[streamId]) { @@ -475,6 +467,19 @@ MessageHandler.prototype = { } }, + async _deleteStreamController(streamId) { + // Delete the `streamController` only when the start, pull, and cancel + // capabilities have settled, to prevent `TypeError`s. + await Promise.all([ + this.streamControllers[streamId].startCall, + this.streamControllers[streamId].pullCall, + this.streamControllers[streamId].cancelCall + ].map(function(capability) { + return capability && capability.promise.catch(function() { }); + })); + delete this.streamControllers[streamId]; + }, + /** * Sends raw message to the comObj. * @private