From ac26c91cba94554c4634ba8cd6db1830d8dd0787 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Tue, 11 Jun 2019 12:59:06 +0100 Subject: [PATCH] Fix content uploads for modern browsers Modern browsers now expose a `stream` function on the Blob and File interfaces. This conflicts with an older style of passing data to the `uploadContent` SDK method, which supported supplying the data to upload in the `stream` property of an object. Since this old style is still in active use in the Matrix JS ecosystem, we preserve the backwards compatibility for now by checking whether `stream` is a function. This fix has been tested in Firefox Nightly (69), Firefox Release (67), Chrome Canary (77), and Chrome Stable (75). Fixes https://github.com/vector-im/riot-web/issues/9913 Fixes https://github.com/matrix-org/matrix-js-sdk/issues/949 --- src/http-api.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/http-api.js b/src/http-api.js index 6ec936155..0c42f765e 100644 --- a/src/http-api.js +++ b/src/http-api.js @@ -165,9 +165,21 @@ module.exports.MatrixHttpApi.prototype = { const contentType = opts.type || file.type || 'application/octet-stream'; const fileName = opts.name || file.name; - // we used to recommend setting file.stream to the thing to upload on - // nodejs. - const body = file.stream ? file.stream : file; + // We used to recommend setting file.stream to the thing to upload on + // Node.js. As of 2019-06-11, this is still in widespread use in various + // clients, so we should preserve this for simple objects used in + // Node.js. File API objects (via either the File or Blob interfaces) in + // the browser now define a `stream` method, which leads to trouble + // here, so we also check the type of `stream`. + let body = file; + if (body.stream && typeof body.stream !== "function") { + logger.warn( + "Using `file.stream` as the content to upload. Future " + + "versions of the js-sdk will change this to expect `file` to " + + "be the content directly.", + ); + body = body.stream; + } // backwards-compatibility hacks where we used to do different things // between browser and node.