Update processDocuments() signature for allow passing headers

Also:

- Actually use the cookie sandbox passed to processDocuments()

For zotero/translation-server#16, we want to include Accept-Language
(and maybe other headers) from the client request in upstream requests,
which requires passing it to both non-translate processDocuments().
translation-server's non-translate processDocuments() is defined in that
repo, but it's called by the cross-repo translate processDocuments() in
utilities_translate.js, so the signature needs to be changed in both
repos.

We also apparently weren't using the cookieSandbox in client
processDocuments() calls, though I think that only would've affected
translator testing.
This commit is contained in:
Dan Stillman 2018-11-30 00:30:01 -07:00
parent 223f582aa7
commit a6fb0b35c3
3 changed files with 21 additions and 5 deletions

View file

@ -421,7 +421,9 @@ Zotero_TranslatorTester.prototype.fetchPageAndRunTest = function (test, testDone
testDoneCallback(obj, test, status, message);
});
},
this._cookieSandbox,
{
cookieSandbox: this._cookieSandbox
}
)
.catch(function (e) {
testDoneCallback(this, test, "failed", "Translation failed to initialize: " + e);

View file

@ -903,10 +903,12 @@ Zotero.HTTP = new function() {
* @param {String|String[]} urls URL(s) of documents to load
* @param {Function} processor - Callback to be executed for each document loaded; if function returns
* a promise, it's waited for before continuing
* @param {Zotero.CookieSandbox} [cookieSandbox] Cookie sandbox object
* @param {Object} [options]
* @param {Zotero.CookieSandbox} [options.cookieSandbox] - Cookie sandbox object
* @param {Object} [options.headers] - Headers to include in the request
* @return {Promise<Array>} - A promise for an array of results from the processor runs
*/
this.processDocuments = async function (urls, processor, cookieSandbox) {
this.processDocuments = async function (urls, processor, options = {}) {
// Handle old signature: urls, processor, onDone, onError, dontDelete, cookieSandbox
if (arguments.length > 3) {
Zotero.debug("Zotero.HTTP.processDocuments() now takes only 3 arguments -- update your code");
@ -914,6 +916,14 @@ Zotero.HTTP = new function() {
var onError = arguments[3];
var cookieSandbox = arguments[5];
}
else if (options instanceof Zotero.CookieSandbox) {
Zotero.debug("Zotero.HTTP.processDocuments() now takes an 'options' object for its third parameter -- update your code");
var cookieSandbox = options;
}
else {
var cookieSandbox = options.cookieSandbox;
var headers = options.headers;
}
if (typeof urls == "string") urls = [urls];
var funcs = urls.map(url => () => {
@ -921,7 +931,9 @@ Zotero.HTTP = new function() {
"GET",
url,
{
responseType: 'document'
responseType: 'document',
cookieSandbox,
headers
}
)
.then((xhr) => {

View file

@ -261,7 +261,9 @@ Zotero.Utilities.Translate.prototype.processDocuments = async function (urls, pr
if (!processor) return;
return processDoc(doc);
},
translate.cookieSandbox
{
cookieSandbox: translate.cookieSandbox
}
)
);
}