From 4406b1645589733bea62c4ff6ca305f64347634f Mon Sep 17 00:00:00 2001 From: Etienne Prothon Date: Mon, 15 Jun 2020 14:18:58 +0200 Subject: [PATCH 1/4] transmit cookies on redirect --- src/index.mjs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/index.mjs b/src/index.mjs index eb341ed..cb68e65 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -1,9 +1,12 @@ import _fetch from "node-fetch"; import CookieJar from "./cookie-jar.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]); async function fetch(cookieJars, url, options) { + let cookies = ""; const addValidFromJars = jars => { // since multiple cookie jars can be passed, filter duplicates by using a set of cookie names @@ -32,6 +35,11 @@ async function fetch(cookieJars, url, options) { options.headers = {}; options.headers.cookie = cookies.slice(0, -2); } + + let wantFollow = options && options.redirect && options.redirect === 'follow' + if (wantFollow) { + options.redirect = 'manual' + } const result = await _fetch(url, options); // I cannot use headers.get() here because it joins the cookies to a string cookies = result.headers[Object.getOwnPropertySymbols(result.headers)[0]]["set-cookie"]; @@ -44,6 +52,11 @@ async function fetch(cookieJars, url, options) { else if(cookieJars instanceof CookieJar && cookieJars.flags.includes("w")) cookies.forEach(c => cookieJars.addCookie(c, url)); } + if (wantFollow && redirectStatus.has(result.status)) { + const location = result.headers.get('Location') + options.redirect = 'follow' + await fetch(cookieJars, location, options) + } return result; } From 389d5fd426906e8a9e770d774c09ae7e38f465a4 Mon Sep 17 00:00:00 2001 From: Etienne Prothon Date: Mon, 15 Jun 2020 14:48:22 +0200 Subject: [PATCH 2/4] return the last result --- src/index.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.mjs b/src/index.mjs index cb68e65..b1b5087 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -40,7 +40,7 @@ async function fetch(cookieJars, url, options) { if (wantFollow) { options.redirect = 'manual' } - const result = await _fetch(url, options); + let result = await _fetch(url, options); // I cannot use headers.get() here because it joins the cookies to a string cookies = result.headers[Object.getOwnPropertySymbols(result.headers)[0]]["set-cookie"]; if(cookies && cookieJars) { @@ -55,7 +55,7 @@ async function fetch(cookieJars, url, options) { if (wantFollow && redirectStatus.has(result.status)) { const location = result.headers.get('Location') options.redirect = 'follow' - await fetch(cookieJars, location, options) + result = await fetch(cookieJars, location, options) } return result; } From 648f12a1eb628aae75205338244311f873a89b82 Mon Sep 17 00:00:00 2001 From: Etienne Prothon Date: Tue, 16 Jun 2020 00:23:55 +0200 Subject: [PATCH 3/4] fix typo and improve as asked --- src/index.mjs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/index.mjs b/src/index.mjs index b1b5087..6e63ab2 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -1,12 +1,12 @@ import _fetch from "node-fetch"; import CookieJar from "./cookie-jar.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]); async function fetch(cookieJars, url, options) { - + let cookies = ""; const addValidFromJars = jars => { // 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.cookie = cookies.slice(0, -2); } - - let wantFollow = options && options.redirect && options.redirect === 'follow' - if (wantFollow) { - options.redirect = 'manual' + + const wantFollow = !options || !options.redirect || options.redirect === 'follow'; + if(wantFollow) { + 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 cookies = result.headers[Object.getOwnPropertySymbols(result.headers)[0]]["set-cookie"]; if(cookies && cookieJars) { @@ -52,10 +52,10 @@ async function fetch(cookieJars, url, options) { else if(cookieJars instanceof CookieJar && cookieJars.flags.includes("w")) cookies.forEach(c => cookieJars.addCookie(c, url)); } - if (wantFollow && redirectStatus.has(result.status)) { - const location = result.headers.get('Location') - options.redirect = 'follow' - result = await fetch(cookieJars, location, options) + if(wantFollow && redirectStatus.has(result.status)) { + const location = result.headers.get('Location'); + options.redirect = 'follow'; + return fetch(cookieJars, location, options); } return result; } From ab5ee770b8d4c2f68df034bf859ed6cfaea5b07e Mon Sep 17 00:00:00 2001 From: Etienne Prothon Date: Tue, 16 Jun 2020 15:02:27 +0200 Subject: [PATCH 4/4] set option if not defined --- src/index.mjs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.mjs b/src/index.mjs index 6e63ab2..c55a460 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -38,6 +38,7 @@ async function fetch(cookieJars, url, options) { const wantFollow = !options || !options.redirect || options.redirect === 'follow'; if(wantFollow) { + if (!options) options = {}; options.redirect = 'manual'; } const result = await _fetch(url, options);