"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(); }; };