You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
763 B
JavaScript

"use strict";
// FIXME: This is a bit of a hack to persist the CSRF token across calls. There is probably a better solution for this, but that sort of state management needs to be handled on a scraping-server level, probably.
let token;
module.exports = function (state) {
const getCsrfToken = require("./get-csrf-token")(state);
return async function withCSRFToken(callback) {
async function obtainToken() {
token = await getCsrfToken();
}
async function attemptCallback() {
let response = await callback(token);
if (response.statusCode === 419) {
await obtainToken();
return attemptCallback();
} else {
return response;
}
}
if (token == null) {
await obtainToken();
}
return attemptCallback();
};
};