diff --git a/src/auth.js b/src/auth.js index 59990f5b..fdd5b40f 100644 --- a/src/auth.js +++ b/src/auth.js @@ -45,17 +45,18 @@ const IncludeGrantedScopes = ['none', 'user', 'team']; export default class DropboxAuth { constructor(options) { options = options || {}; + fetch = options.fetch; if (isBrowserEnv()) { - fetch = window.fetch.bind(window); + fetch = fetch || window.fetch.bind(window); crypto = window.crypto || window.msCrypto; // for IE11 } else if (isWorkerEnv()) { /* eslint-disable no-restricted-globals */ - fetch = self.fetch.bind(self); + fetch = fetch || self.fetch.bind(self); crypto = self.crypto; /* eslint-enable no-restricted-globals */ } else { - fetch = require('node-fetch'); // eslint-disable-line global-require + fetch = fetch || global.fetch || require('node-fetch'); // eslint-disable-line global-require crypto = require('crypto'); // eslint-disable-line global-require } @@ -65,7 +66,7 @@ export default class DropboxAuth { Encoder = TextEncoder; } - this.fetch = options.fetch || fetch; + this.fetch = fetch; this.accessToken = options.accessToken; this.accessTokenExpiresAt = options.accessTokenExpiresAt; this.refreshToken = options.refreshToken; @@ -340,7 +341,7 @@ export default class DropboxAuth { checkAndRefreshAccessToken() { const canRefresh = this.getRefreshToken() && this.getClientId(); const needsRefresh = !this.getAccessTokenExpiresAt() - || (new Date(Date.now() + TokenExpirationBuffer)) >= this.getAccessTokenExpiresAt(); + || (new Date(Date.now() + TokenExpirationBuffer)) >= this.getAccessTokenExpiresAt(); const needsToken = !this.getAccessToken(); if ((needsRefresh || needsToken) && canRefresh) { return this.refreshAccessToken(); diff --git a/src/dropbox.js b/src/dropbox.js index fa33f58b..283ef915 100644 --- a/src/dropbox.js +++ b/src/dropbox.js @@ -57,7 +57,7 @@ export default class Dropbox { this.auth = new DropboxAuth(options); } - this.fetch = options.fetch || this.auth.fetch; + this.fetch = this.auth.fetch; this.selectUser = options.selectUser; this.selectAdmin = options.selectAdmin; this.pathRoot = options.pathRoot; diff --git a/src/response.js b/src/response.js index 2753bb7b..c46bbb1e 100644 --- a/src/response.js +++ b/src/response.js @@ -48,7 +48,7 @@ export function parseDownloadResponse(res) { if (isWindowOrWorker()) { res.blob().then((data) => resolve(data)); } else { - res.buffer().then((data) => resolve(data)); + res.arrayBuffer().then((data) => resolve(Buffer.from(data))); } }).then((data) => { const result = JSON.parse(res.headers.get('dropbox-api-result'));