fetch-cookie
Advanced tools
Comparing version 2.2.0 to 3.0.0
@@ -9,2 +9,9 @@ # Changelog | ||
## [3.0.0] - 2024-02-21 | ||
* Export `FetchCookieImpl` interface. ([#81]) | ||
* **Breaking:** move away from CJS `module.exports = fetchCookie` style, | ||
instead use more standard and future-proof `exports.default = fetchCookie`. | ||
When using CJS, you now need to `const fetchCookie = require('fetch-cookie').default` | ||
instead of `const fetchCookie = require('fetch-cookie')` previously. | ||
## [2.2.0] - 2024-02-01 | ||
@@ -11,0 +18,0 @@ * Export `CookieJar` interface. ([#81]) |
201
cjs/index.js
@@ -0,25 +1,8 @@ | ||
"use strict"; | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __defProps = Object.defineProperties; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); | ||
var __export = (target, all) => { | ||
@@ -29,38 +12,19 @@ for (var name in all) | ||
}; | ||
var __reExport = (target, module2, copyDefault, desc) => { | ||
if (module2 && typeof module2 === "object" || typeof module2 === "function") { | ||
for (let key of __getOwnPropNames(module2)) | ||
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default")) | ||
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable }); | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return target; | ||
return to; | ||
}; | ||
var __toESM = (module2, isNodeMode) => { | ||
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", !isNodeMode && module2 && module2.__esModule ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2); | ||
}; | ||
var __toCommonJS = /* @__PURE__ */ ((cache) => { | ||
return (module2, temp) => { | ||
return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp); | ||
}; | ||
})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0); | ||
var __async = (__this, __arguments, generator) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator = generator.apply(__this, __arguments)).next()); | ||
}); | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
// If the importer is in node compatibility mode or this is not an ESM | ||
// file that has been converted to a CommonJS file using a Babel- | ||
// compatible transform (i.e. "__esModule" has not been set), then set | ||
// "default" to the CommonJS "module.exports" for node compatibility. | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var src_exports = {}; | ||
@@ -70,2 +34,3 @@ __export(src_exports, { | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
var tough = __toESM(require("tough-cookie"), 1); | ||
@@ -125,50 +90,48 @@ var import_set_cookie_parser = require("set-cookie-parser"); | ||
} | ||
function handleRedirect(fetchImpl, init, response) { | ||
return __async(this, null, function* () { | ||
var _a, _b, _c; | ||
switch ((_a = init.redirect) != null ? _a : "follow") { | ||
case "error": | ||
throw new TypeError(`URI requested responded with a redirect and redirect mode is set to error: ${response.url}`); | ||
case "manual": | ||
return response; | ||
case "follow": | ||
break; | ||
default: | ||
throw new TypeError(`Invalid redirect option: ${init.redirect}`); | ||
} | ||
const locationUrl = response.headers.get("location"); | ||
if (locationUrl === null) { | ||
async function handleRedirect(fetchImpl, init, response) { | ||
switch (init.redirect ?? "follow") { | ||
case "error": | ||
throw new TypeError(`URI requested responded with a redirect and redirect mode is set to error: ${response.url}`); | ||
case "manual": | ||
return response; | ||
case "follow": | ||
break; | ||
default: | ||
throw new TypeError(`Invalid redirect option: ${init.redirect}`); | ||
} | ||
const locationUrl = response.headers.get("location"); | ||
if (locationUrl === null) { | ||
return response; | ||
} | ||
const requestUrl = response.url; | ||
const redirectUrl = new URL(locationUrl, requestUrl).toString(); | ||
const redirectCount = init.redirectCount ?? 0; | ||
const maxRedirect = init.maxRedirect ?? 20; | ||
if (redirectCount >= maxRedirect) { | ||
throw new TypeError(`Reached maximum redirect of ${maxRedirect} for URL: ${requestUrl}`); | ||
} | ||
init = { | ||
...init, | ||
redirectCount: redirectCount + 1 | ||
}; | ||
const deleteHeader = identifyDeleteHeader(init); | ||
if (!isDomainOrSubdomain(requestUrl, redirectUrl)) { | ||
for (const name of ["authorization", "www-authenticate", "cookie", "cookie2"]) { | ||
deleteHeader(init, name); | ||
} | ||
const requestUrl = response.url; | ||
const redirectUrl = new URL(locationUrl, requestUrl).toString(); | ||
const redirectCount = (_b = init.redirectCount) != null ? _b : 0; | ||
const maxRedirect = (_c = init.maxRedirect) != null ? _c : 20; | ||
if (redirectCount >= maxRedirect) { | ||
throw new TypeError(`Reached maximum redirect of ${maxRedirect} for URL: ${requestUrl}`); | ||
} | ||
init = __spreadProps(__spreadValues({}, init), { | ||
redirectCount: redirectCount + 1 | ||
}); | ||
const deleteHeader = identifyDeleteHeader(init); | ||
if (!isDomainOrSubdomain(requestUrl, redirectUrl)) { | ||
for (const name of ["authorization", "www-authenticate", "cookie", "cookie2"]) { | ||
deleteHeader(init, name); | ||
} | ||
} | ||
const maybeNodeStreamBody = init.body; | ||
const maybeStreamBody = init.body; | ||
if (response.status !== 303 && init.body != null && (typeof maybeNodeStreamBody.pipe === "function" || typeof maybeStreamBody.pipeTo === "function")) { | ||
throw new TypeError("Cannot follow redirect with body being a readable stream"); | ||
} | ||
if (response.status === 303 || (response.status === 301 || response.status === 302) && init.method === "POST") { | ||
init.method = "GET"; | ||
init.body = void 0; | ||
deleteHeader(init, "content-length"); | ||
} | ||
if (response.headers.has("referrer-policy")) { | ||
init.referrerPolicy = parseReferrerPolicy(response.headers.get("referrer-policy")); | ||
} | ||
return yield fetchImpl(redirectUrl, init); | ||
}); | ||
} | ||
const maybeNodeStreamBody = init.body; | ||
const maybeStreamBody = init.body; | ||
if (response.status !== 303 && init.body != null && (typeof maybeNodeStreamBody.pipe === "function" || typeof maybeStreamBody.pipeTo === "function")) { | ||
throw new TypeError("Cannot follow redirect with body being a readable stream"); | ||
} | ||
if (response.status === 303 || (response.status === 301 || response.status === 302) && init.method === "POST") { | ||
init.method = "GET"; | ||
init.body = void 0; | ||
deleteHeader(init, "content-length"); | ||
} | ||
if (response.headers.has("referrer-policy")) { | ||
init.referrerPolicy = parseReferrerPolicy(response.headers.get("referrer-policy")); | ||
} | ||
return await fetchImpl(redirectUrl, init); | ||
} | ||
@@ -186,3 +149,3 @@ function addCookiesToRequest(input, init, cookie) { | ||
} else { | ||
init = __spreadProps(__spreadValues({}, init), { headers: __spreadProps(__spreadValues({}, init.headers), { cookie }) }); | ||
init = { ...init, headers: { ...init.headers, cookie } }; | ||
} | ||
@@ -211,24 +174,19 @@ return init; | ||
const actualFetch = fetch; | ||
const actualJar = jar != null ? jar : new tough.CookieJar(); | ||
function fetchCookieWrapper(input, init) { | ||
return __async(this, null, function* () { | ||
var _a, _b; | ||
const originalInit = init != null ? init : {}; | ||
init = __spreadProps(__spreadValues({}, init), { redirect: "manual" }); | ||
const requestUrl = typeof input === "string" ? input : (_a = input.url) != null ? _a : input.href; | ||
const cookie = yield actualJar.getCookieString(requestUrl); | ||
init = addCookiesToRequest(input, init, cookie); | ||
const response = yield actualFetch(input, init); | ||
const cookies = getCookiesFromResponse(response); | ||
yield Promise.all(cookies.map((cookie2) => __async(this, null, function* () { | ||
return yield actualJar.setCookie(cookie2, response.url, { ignoreError }); | ||
}))); | ||
if (((_b = init.redirectCount) != null ? _b : 0) > 0) { | ||
Object.defineProperty(response, "redirected", { value: true }); | ||
} | ||
if (!isRedirect(response.status)) { | ||
return response; | ||
} | ||
return yield handleRedirect(fetchCookieWrapper, originalInit, response); | ||
}); | ||
const actualJar = jar ?? new tough.CookieJar(); | ||
async function fetchCookieWrapper(input, init) { | ||
const originalInit = init ?? {}; | ||
init = { ...init, redirect: "manual" }; | ||
const requestUrl = typeof input === "string" ? input : input.url ?? input.href; | ||
const cookie = await actualJar.getCookieString(requestUrl); | ||
init = addCookiesToRequest(input, init, cookie); | ||
const response = await actualFetch(input, init); | ||
const cookies = getCookiesFromResponse(response); | ||
await Promise.all(cookies.map(async (cookie2) => await actualJar.setCookie(cookie2, response.url, { ignoreError }))); | ||
if ((init.redirectCount ?? 0) > 0) { | ||
Object.defineProperty(response, "redirected", { value: true }); | ||
} | ||
if (!isRedirect(response.status)) { | ||
return response; | ||
} | ||
return await handleRedirect(fetchCookieWrapper, originalInit, response); | ||
} | ||
@@ -239,2 +197,1 @@ fetchCookieWrapper.toughCookie = tough; | ||
fetchCookie.toughCookie = tough; | ||
module.exports = __toCommonJS(src_exports); |
170
esm/index.js
@@ -1,40 +0,1 @@ | ||
var __defProp = Object.defineProperty; | ||
var __defProps = Object.defineProperties; | ||
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __async = (__this, __arguments, generator) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator = generator.apply(__this, __arguments)).next()); | ||
}); | ||
}; | ||
import * as tough from "tough-cookie"; | ||
@@ -94,50 +55,48 @@ import { splitCookiesString } from "set-cookie-parser"; | ||
} | ||
function handleRedirect(fetchImpl, init, response) { | ||
return __async(this, null, function* () { | ||
var _a, _b, _c; | ||
switch ((_a = init.redirect) != null ? _a : "follow") { | ||
case "error": | ||
throw new TypeError(`URI requested responded with a redirect and redirect mode is set to error: ${response.url}`); | ||
case "manual": | ||
return response; | ||
case "follow": | ||
break; | ||
default: | ||
throw new TypeError(`Invalid redirect option: ${init.redirect}`); | ||
} | ||
const locationUrl = response.headers.get("location"); | ||
if (locationUrl === null) { | ||
async function handleRedirect(fetchImpl, init, response) { | ||
switch (init.redirect ?? "follow") { | ||
case "error": | ||
throw new TypeError(`URI requested responded with a redirect and redirect mode is set to error: ${response.url}`); | ||
case "manual": | ||
return response; | ||
case "follow": | ||
break; | ||
default: | ||
throw new TypeError(`Invalid redirect option: ${init.redirect}`); | ||
} | ||
const locationUrl = response.headers.get("location"); | ||
if (locationUrl === null) { | ||
return response; | ||
} | ||
const requestUrl = response.url; | ||
const redirectUrl = new URL(locationUrl, requestUrl).toString(); | ||
const redirectCount = init.redirectCount ?? 0; | ||
const maxRedirect = init.maxRedirect ?? 20; | ||
if (redirectCount >= maxRedirect) { | ||
throw new TypeError(`Reached maximum redirect of ${maxRedirect} for URL: ${requestUrl}`); | ||
} | ||
init = { | ||
...init, | ||
redirectCount: redirectCount + 1 | ||
}; | ||
const deleteHeader = identifyDeleteHeader(init); | ||
if (!isDomainOrSubdomain(requestUrl, redirectUrl)) { | ||
for (const name of ["authorization", "www-authenticate", "cookie", "cookie2"]) { | ||
deleteHeader(init, name); | ||
} | ||
const requestUrl = response.url; | ||
const redirectUrl = new URL(locationUrl, requestUrl).toString(); | ||
const redirectCount = (_b = init.redirectCount) != null ? _b : 0; | ||
const maxRedirect = (_c = init.maxRedirect) != null ? _c : 20; | ||
if (redirectCount >= maxRedirect) { | ||
throw new TypeError(`Reached maximum redirect of ${maxRedirect} for URL: ${requestUrl}`); | ||
} | ||
init = __spreadProps(__spreadValues({}, init), { | ||
redirectCount: redirectCount + 1 | ||
}); | ||
const deleteHeader = identifyDeleteHeader(init); | ||
if (!isDomainOrSubdomain(requestUrl, redirectUrl)) { | ||
for (const name of ["authorization", "www-authenticate", "cookie", "cookie2"]) { | ||
deleteHeader(init, name); | ||
} | ||
} | ||
const maybeNodeStreamBody = init.body; | ||
const maybeStreamBody = init.body; | ||
if (response.status !== 303 && init.body != null && (typeof maybeNodeStreamBody.pipe === "function" || typeof maybeStreamBody.pipeTo === "function")) { | ||
throw new TypeError("Cannot follow redirect with body being a readable stream"); | ||
} | ||
if (response.status === 303 || (response.status === 301 || response.status === 302) && init.method === "POST") { | ||
init.method = "GET"; | ||
init.body = void 0; | ||
deleteHeader(init, "content-length"); | ||
} | ||
if (response.headers.has("referrer-policy")) { | ||
init.referrerPolicy = parseReferrerPolicy(response.headers.get("referrer-policy")); | ||
} | ||
return yield fetchImpl(redirectUrl, init); | ||
}); | ||
} | ||
const maybeNodeStreamBody = init.body; | ||
const maybeStreamBody = init.body; | ||
if (response.status !== 303 && init.body != null && (typeof maybeNodeStreamBody.pipe === "function" || typeof maybeStreamBody.pipeTo === "function")) { | ||
throw new TypeError("Cannot follow redirect with body being a readable stream"); | ||
} | ||
if (response.status === 303 || (response.status === 301 || response.status === 302) && init.method === "POST") { | ||
init.method = "GET"; | ||
init.body = void 0; | ||
deleteHeader(init, "content-length"); | ||
} | ||
if (response.headers.has("referrer-policy")) { | ||
init.referrerPolicy = parseReferrerPolicy(response.headers.get("referrer-policy")); | ||
} | ||
return await fetchImpl(redirectUrl, init); | ||
} | ||
@@ -155,3 +114,3 @@ function addCookiesToRequest(input, init, cookie) { | ||
} else { | ||
init = __spreadProps(__spreadValues({}, init), { headers: __spreadProps(__spreadValues({}, init.headers), { cookie }) }); | ||
init = { ...init, headers: { ...init.headers, cookie } }; | ||
} | ||
@@ -180,24 +139,19 @@ return init; | ||
const actualFetch = fetch; | ||
const actualJar = jar != null ? jar : new tough.CookieJar(); | ||
function fetchCookieWrapper(input, init) { | ||
return __async(this, null, function* () { | ||
var _a, _b; | ||
const originalInit = init != null ? init : {}; | ||
init = __spreadProps(__spreadValues({}, init), { redirect: "manual" }); | ||
const requestUrl = typeof input === "string" ? input : (_a = input.url) != null ? _a : input.href; | ||
const cookie = yield actualJar.getCookieString(requestUrl); | ||
init = addCookiesToRequest(input, init, cookie); | ||
const response = yield actualFetch(input, init); | ||
const cookies = getCookiesFromResponse(response); | ||
yield Promise.all(cookies.map((cookie2) => __async(this, null, function* () { | ||
return yield actualJar.setCookie(cookie2, response.url, { ignoreError }); | ||
}))); | ||
if (((_b = init.redirectCount) != null ? _b : 0) > 0) { | ||
Object.defineProperty(response, "redirected", { value: true }); | ||
} | ||
if (!isRedirect(response.status)) { | ||
return response; | ||
} | ||
return yield handleRedirect(fetchCookieWrapper, originalInit, response); | ||
}); | ||
const actualJar = jar ?? new tough.CookieJar(); | ||
async function fetchCookieWrapper(input, init) { | ||
const originalInit = init ?? {}; | ||
init = { ...init, redirect: "manual" }; | ||
const requestUrl = typeof input === "string" ? input : input.url ?? input.href; | ||
const cookie = await actualJar.getCookieString(requestUrl); | ||
init = addCookiesToRequest(input, init, cookie); | ||
const response = await actualFetch(input, init); | ||
const cookies = getCookiesFromResponse(response); | ||
await Promise.all(cookies.map(async (cookie2) => await actualJar.setCookie(cookie2, response.url, { ignoreError }))); | ||
if ((init.redirectCount ?? 0) > 0) { | ||
Object.defineProperty(response, "redirected", { value: true }); | ||
} | ||
if (!isRedirect(response.status)) { | ||
return response; | ||
} | ||
return await handleRedirect(fetchCookieWrapper, originalInit, response); | ||
} | ||
@@ -204,0 +158,0 @@ fetchCookieWrapper.toughCookie = tough; |
{ | ||
"name": "fetch-cookie", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"description": "Decorator for a `fetch` function to support automatic cookies.", | ||
@@ -8,3 +8,3 @@ "license": "Unlicense", | ||
"type": "module", | ||
"main": "./cjs/index-wrapper.js", | ||
"main": "./cjs/index.js", | ||
"module": "./esm/index.js", | ||
@@ -15,12 +15,10 @@ "exports": { | ||
"import": { | ||
"types": "./esm/index.d.ts", | ||
"default": "./esm/index.js" | ||
}, | ||
"require": { | ||
"types": "./cjs/index.d.ts", | ||
"default": "./cjs/index-wrapper.js" | ||
"default": "./cjs/index.js" | ||
} | ||
} | ||
}, | ||
"types": "esm/index.d.ts", | ||
"types": "index.d.ts", | ||
"repository": "valeriangalliat/fetch-cookie", | ||
@@ -36,3 +34,3 @@ "scripts": { | ||
"type-check": "tsc -noEmit", | ||
"type-declarations": "tsc --project tsconfig.build.json --declaration --emitDeclarationOnly --outDir esm && cat esm/index.d.ts | sed 's/^export default /export = /' > cjs/index.d.ts" | ||
"type-declarations": "tsc --project tsconfig.build.json --declaration --emitDeclarationOnly --outDir ." | ||
}, | ||
@@ -53,3 +51,3 @@ "dependencies": { | ||
"chai": "^4.3.6", | ||
"esbuild": "^0.14.21", | ||
"esbuild": "^0.20.0", | ||
"express": "^4.17.3", | ||
@@ -56,0 +54,0 @@ "mocha": "^9.2.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32194
10
400