-
Notifications
You must be signed in to change notification settings - Fork 2.1k
companion,companion-client: send uppy-versions header to companion #1612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d77180a
f626fc0
3afc90b
592b43b
3d167cd
cabff1b
49539f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,8 @@ module.exports = class RequestClient { | |
| this.uppy = uppy | ||
| this.opts = opts | ||
| this.onReceiveResponse = this.onReceiveResponse.bind(this) | ||
| this.allowedHeaders = ['accept', 'content-type', 'uppy-auth-token'] | ||
| this.preflightDone = false | ||
| } | ||
|
|
||
| get hostname () { | ||
|
|
@@ -25,12 +27,15 @@ module.exports = class RequestClient { | |
| get defaultHeaders () { | ||
| return { | ||
| 'Accept': 'application/json', | ||
| 'Content-Type': 'application/json' | ||
| 'Content-Type': 'application/json', | ||
| 'Uppy-Versions': '@uppy/companion-client=1.0.3' | ||
| } | ||
| } | ||
|
|
||
| headers () { | ||
| return Promise.resolve(Object.assign({}, this.defaultHeaders, this.opts.serverHeaders || {})) | ||
| return Promise.resolve( | ||
| Object.assign({}, this.defaultHeaders, this.opts.serverHeaders || {}) | ||
| ) | ||
| } | ||
|
|
||
| _getPostResponseFunc (skip) { | ||
|
|
@@ -77,9 +82,49 @@ module.exports = class RequestClient { | |
| return res.json() | ||
| } | ||
|
|
||
| preflight (path) { | ||
| return new Promise((resolve, reject) => { | ||
| if (this.preflightDone) { | ||
| return resolve(this.allowedHeaders.slice()) | ||
| } | ||
|
|
||
| fetch(this._getUrl(path), { | ||
| method: 'OPTIONS' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am unable to reproduce the issue locally with firefox. We've always allowed all methods for all endpoints Are you using the companion standalone server when testing? Also are you able to reproduce this with other providers asides S3? Here's my network log when testing with instagram on firefox
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did renee test it against transloadit? Can you? Maybe we're messing something up over there?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh…I tested using the examples/aws-companion example in this repo, which does not use the standalone server, and uses the default configuration of the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Transloadit directly uses the standalone server so I wouldn't think we would be messing anything up there.
Ah alright, it makes sense now. Users who have their own personalized express servers are left to determine what methods they allow on their servers, but maybe we should change this and let the companion pluggable app set the allowed methods for its own endpoints 🤔 Also, should this still be considered a breaking change if it breaks only for users who don't use the standalone server, and happen not to allow the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, the entire upload fails right now if the preflight request fails, maybe we can change it so the preflight request failure is caught, and we use the default allowedHeaders in that case?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's true but it does pass through HAProxy which does/can tamper with headers. Not the issue right now it seems, but I thought i'd add that as a friendly reminder here to save us a possible headache later.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah ok, I thought you once mentioned that it does almost nothing (for the case of companion) but forward requests to companion, so I was working with that.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
will do 👍
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @goto-bus-stop I have made an update. PS: I had to force push because I did a rebase to resolve conflicts |
||
| }) | ||
| .then((response) => { | ||
| if (response.headers.has('access-control-allow-headers')) { | ||
| this.allowedHeaders = response.headers.get('access-control-allow-headers') | ||
| .split(',').map((headerName) => headerName.trim().toLowerCase()) | ||
| } | ||
| this.preflightDone = true | ||
| resolve(this.allowedHeaders.slice()) | ||
| }) | ||
| .catch((err) => { | ||
| this.uppy.log(`[CompanionClient] unable to make preflight request ${err}`, 'warning') | ||
| this.preflightDone = true | ||
| resolve(this.allowedHeaders.slice()) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| preflightAndHeaders (path) { | ||
| return Promise.all([this.preflight(path), this.headers()]) | ||
| .then(([allowedHeaders, headers]) => { | ||
| // filter to keep only allowed Headers | ||
| Object.keys(headers).forEach((header) => { | ||
| if (allowedHeaders.indexOf(header.toLowerCase()) === -1) { | ||
| this.uppy.log(`[CompanionClient] excluding unallowed header ${header}`) | ||
| delete headers[header] | ||
| } | ||
| }) | ||
|
|
||
| return headers | ||
| }) | ||
| } | ||
|
|
||
| get (path, skipPostResponse) { | ||
| return new Promise((resolve, reject) => { | ||
| this.headers().then((headers) => { | ||
| this.preflightAndHeaders(path).then((headers) => { | ||
| fetch(this._getUrl(path), { | ||
| method: 'get', | ||
| headers: headers, | ||
|
|
@@ -91,13 +136,13 @@ module.exports = class RequestClient { | |
| err = err.isAuthError ? err : new Error(`Could not get ${this._getUrl(path)}. ${err}`) | ||
| reject(err) | ||
| }) | ||
| }) | ||
| }).catch(reject) | ||
| }) | ||
| } | ||
|
|
||
| post (path, data, skipPostResponse) { | ||
| return new Promise((resolve, reject) => { | ||
| this.headers().then((headers) => { | ||
| this.preflightAndHeaders(path).then((headers) => { | ||
| fetch(this._getUrl(path), { | ||
| method: 'post', | ||
| headers: headers, | ||
|
|
@@ -110,13 +155,13 @@ module.exports = class RequestClient { | |
| err = err.isAuthError ? err : new Error(`Could not post ${this._getUrl(path)}. ${err}`) | ||
| reject(err) | ||
| }) | ||
| }) | ||
| }).catch(reject) | ||
| }) | ||
| } | ||
|
|
||
| delete (path, data, skipPostResponse) { | ||
| return new Promise((resolve, reject) => { | ||
| this.headers().then((headers) => { | ||
| this.preflightAndHeaders(path).then((headers) => { | ||
| fetch(`${this.hostname}/${path}`, { | ||
| method: 'delete', | ||
| headers: headers, | ||
|
|
@@ -129,7 +174,7 @@ module.exports = class RequestClient { | |
| err = err.isAuthError ? err : new Error(`Could not delete ${this._getUrl(path)}. ${err}`) | ||
| reject(err) | ||
| }) | ||
| }) | ||
| }).catch(reject) | ||
| }) | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| const semver = require('semver') | ||
|
|
||
| /** | ||
| * checks if a version is greater than or equal to | ||
| * @param {string} v1 the LHS version | ||
| * @param {string} v2 the RHS version | ||
| * @returns {boolean} | ||
| */ | ||
| exports.gte = (v1, v2) => { | ||
| v1 = semver.coerce(v1).version | ||
| v2 = semver.coerce(v2).version | ||
|
|
||
| return semver.gte(v1, v2) | ||
| } |




Uh oh!
There was an error while loading. Please reload this page.