node-csfd-api
Version:
ČSFD API in JavaScript. Amazing NPM library for scrapping csfd.cz :)
46 lines (45 loc) • 1.22 kB
JavaScript
import { isAnubisChallenge, passChallenge } from "./challenge.js";
//#region src/anubis/client.ts
/**
* An Anubis-aware cookie holder. Each client keeps its own cookie, so callers
* with separate egress IPs (Anubis binds the cookie to one) can hold one each.
*/
const createAnubisClient = (options = {}) => {
const doFetch = options.fetch ?? ((input, init) => globalThis.fetch(input, init));
const timeBudgetMs = options.timeBudgetMs;
let cookie = null;
let platformCookieJar = false;
let pending = null;
return {
isChallenge: isAnubisChallenge,
getCookie: () => cookie,
setCookie: (value) => {
cookie = value;
},
reset: () => {
cookie = null;
pending = null;
},
usesPlatformCookieJar: () => platformCookieJar,
pass: async (html, headers, url, requestHeaders) => {
if (!pending) pending = passChallenge({
html,
headers,
url,
requestHeaders,
fetch: doFetch,
timeBudgetMs
}).finally(() => {
pending = null;
});
const result = await pending;
if (!result) return false;
cookie = result.cookie;
platformCookieJar = result.platformCookieJar;
return true;
}
};
};
//#endregion
export { createAnubisClient };
//# sourceMappingURL=client.js.map