fix typo and improve as asked
This commit is contained in:
parent
389d5fd426
commit
648f12a1eb
|
@ -1,12 +1,12 @@
|
||||||
import _fetch from "node-fetch";
|
import _fetch from "node-fetch";
|
||||||
import CookieJar from "./cookie-jar.mjs";
|
import CookieJar from "./cookie-jar.mjs";
|
||||||
import Cookie from "./cookie.mjs";
|
import Cookie from "./cookie.mjs";
|
||||||
import { paramError, CookieParseError } from "./errors.mjs"
|
import {paramError, CookieParseError} from "./errors.mjs";
|
||||||
|
|
||||||
const redirectStatus = new Set([301, 302, 303, 307, 308]);
|
const redirectStatus = new Set([301, 302, 303, 307, 308]);
|
||||||
|
|
||||||
async function fetch(cookieJars, url, options) {
|
async function fetch(cookieJars, url, options) {
|
||||||
|
|
||||||
let cookies = "";
|
let cookies = "";
|
||||||
const addValidFromJars = jars => {
|
const addValidFromJars = jars => {
|
||||||
// since multiple cookie jars can be passed, filter duplicates by using a set of cookie names
|
// since multiple cookie jars can be passed, filter duplicates by using a set of cookie names
|
||||||
|
@ -35,12 +35,12 @@ async function fetch(cookieJars, url, options) {
|
||||||
options.headers = {};
|
options.headers = {};
|
||||||
options.headers.cookie = cookies.slice(0, -2);
|
options.headers.cookie = cookies.slice(0, -2);
|
||||||
}
|
}
|
||||||
|
|
||||||
let wantFollow = options && options.redirect && options.redirect === 'follow'
|
const wantFollow = !options || !options.redirect || options.redirect === 'follow';
|
||||||
if (wantFollow) {
|
if(wantFollow) {
|
||||||
options.redirect = 'manual'
|
options.redirect = 'manual';
|
||||||
}
|
}
|
||||||
let result = await _fetch(url, options);
|
const result = await _fetch(url, options);
|
||||||
// I cannot use headers.get() here because it joins the cookies to a string
|
// I cannot use headers.get() here because it joins the cookies to a string
|
||||||
cookies = result.headers[Object.getOwnPropertySymbols(result.headers)[0]]["set-cookie"];
|
cookies = result.headers[Object.getOwnPropertySymbols(result.headers)[0]]["set-cookie"];
|
||||||
if(cookies && cookieJars) {
|
if(cookies && cookieJars) {
|
||||||
|
@ -52,10 +52,10 @@ async function fetch(cookieJars, url, options) {
|
||||||
else if(cookieJars instanceof CookieJar && cookieJars.flags.includes("w"))
|
else if(cookieJars instanceof CookieJar && cookieJars.flags.includes("w"))
|
||||||
cookies.forEach(c => cookieJars.addCookie(c, url));
|
cookies.forEach(c => cookieJars.addCookie(c, url));
|
||||||
}
|
}
|
||||||
if (wantFollow && redirectStatus.has(result.status)) {
|
if(wantFollow && redirectStatus.has(result.status)) {
|
||||||
const location = result.headers.get('Location')
|
const location = result.headers.get('Location');
|
||||||
options.redirect = 'follow'
|
options.redirect = 'follow';
|
||||||
result = await fetch(cookieJars, location, options)
|
return fetch(cookieJars, location, options);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user