@mherod/get-cookie 2.0.0-rc.15 → 2.0.0-rc.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -789,7 +789,11 @@ function $2d84054eb5f070a6$export$f266452a5050185d(url) {
789
789
 
790
790
 
791
791
  const $cfc07ae4aa2c7e44$var$userAgent = new (0, ($parcel$interopDefault($7oI3p$useragents)))().toString();
792
- async function $cfc07ae4aa2c7e44$export$b24a545a0b39f491(url, options = {}, fetch = (0, $7oI3p$crossfetch.fetch)) {
792
+ async function $cfc07ae4aa2c7e44$export$b24a545a0b39f491(url, options = {}, fetch = (0, $7oI3p$crossfetch.fetch), originalRequest) {
793
+ const originalRequest1 = originalRequest || {
794
+ url: url,
795
+ options: options
796
+ };
793
797
  const headers = {
794
798
  "User-Agent": $cfc07ae4aa2c7e44$var$userAgent
795
799
  };
@@ -816,20 +820,36 @@ async function $cfc07ae4aa2c7e44$export$b24a545a0b39f491(url, options = {}, fetc
816
820
  });
817
821
  for (const [key, value] of headers1)if (key === "set-cookie") {
818
822
  await (0, $53a3bcc99a05dfcf$export$928a906645f9d018).setCookie(value, url2);
819
- if ((0, $ef3d5dc2fd8022f5$export$c348dfaf65040d9b).verbose) console.log((0, $7oI3p$colorette.blue)(`Set-Cookie: ${(0, $7oI3p$colorette.yellow)(value)} ${(0, $7oI3p$colorette.yellow)(url2)}`));
820
- // const cookie = tough.parse(value);
821
- // if (cookie instanceof Cookie) {
822
- // await memoryCookieStore.putCookie(cookie);
823
- // }
823
+ if ((0, $ef3d5dc2fd8022f5$export$c348dfaf65040d9b).verbose) console.log((0, $7oI3p$colorette.blue)(`Set-Cookie:`), (0, $7oI3p$colorette.yellow)(value), (0, $7oI3p$colorette.yellow)(url2));
824
824
  }
825
- const newUrl = res.headers.get("location");
826
- if (res.status == 301 || res.status == 302) // const sameHost = new URL(newUrl).host === url1.host;
825
+ const newUrl = res.headers.get("location") ?? res.url;
826
+ const sameHost = new URL(newUrl).host === url1.host;
827
+ if (res.status == 301 || res.status == 302) // follow the redirect
827
828
  {
828
829
  if (newUrl && newUrl !== url2) {
829
830
  if ((0, $ef3d5dc2fd8022f5$export$c348dfaf65040d9b).verbose) console.log((0, $7oI3p$colorette.blue)(`Redirected to `), (0, $7oI3p$colorette.yellow)(newUrl));
830
831
  return $cfc07ae4aa2c7e44$export$b24a545a0b39f491(//
831
- newUrl, newOptions1, fetch);
832
+ newUrl, newOptions1, fetch, originalRequest1);
833
+ }
834
+ }
835
+ if (res.status == 303 && newUrl && newUrl !== url2) {
836
+ // follow the redirect with GET
837
+ let newOptions2 = {};
838
+ switch(newOptions1.method){
839
+ case "POST":
840
+ case "PUT":
841
+ case "DELETE":
842
+ (0, $7oI3p$lodash.merge)(newOptions2, newOptions1, {
843
+ method: "GET"
844
+ });
845
+ break;
846
+ default:
847
+ (0, $7oI3p$lodash.merge)(newOptions2, newOptions1);
848
+ break;
832
849
  }
850
+ if ((0, $ef3d5dc2fd8022f5$export$c348dfaf65040d9b).verbose) console.log((0, $7oI3p$colorette.blue)(`Redirected to `), (0, $7oI3p$colorette.yellow)(newUrl));
851
+ return $cfc07ae4aa2c7e44$export$b24a545a0b39f491(//
852
+ newUrl, newOptions2, fetch, originalRequest1);
833
853
  }
834
854
  const arrayBuffer1 = res.arrayBuffer();
835
855
  async function arrayBuffer() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mherod/get-cookie",
3
- "version": "2.0.0-rc.15",
3
+ "version": "2.0.0-rc.17",
4
4
  "description": "Node.js module for querying a local user's Chrome cookie",
5
5
  "source": "src/index.ts",
6
6
  "bin": "dist/cli.js",
@@ -14,11 +14,18 @@ import CookieSpec from "./CookieSpec";
14
14
 
15
15
  const userAgent = new UserAgent().toString();
16
16
 
17
+ interface FetchRequestInit {
18
+ url: RequestInfo | URL | string;
19
+ options?: RequestInit;
20
+ }
21
+
17
22
  export async function fetchWithCookies(
18
- url: RequestInfo | URL,
23
+ url: RequestInfo | URL | string,
19
24
  options: RequestInit | undefined = {},
20
- fetch: Function = fetchImpl
25
+ fetch: Function = fetchImpl,
26
+ originalRequest: FetchRequestInit | undefined = undefined
21
27
  ): Promise<Response> {
28
+ const originalRequest1 = originalRequest || { url, options };
22
29
  const headers: HeadersInit = {
23
30
  "User-Agent": userAgent,
24
31
  };
@@ -46,18 +53,15 @@ export async function fetchWithCookies(
46
53
  if (key === "set-cookie") {
47
54
  await cookieJar.setCookie(value, url2);
48
55
  if (parsedArgs.verbose) {
49
- console.log(blue(`Set-Cookie: ${yellow(value)} ${yellow(url2)}`));
56
+ console.log(blue(`Set-Cookie:`), yellow(value), yellow(url2));
50
57
  }
51
- // const cookie = tough.parse(value);
52
- // if (cookie instanceof Cookie) {
53
- // await memoryCookieStore.putCookie(cookie);
54
- // }
55
58
  }
56
59
  }
57
60
 
58
- const newUrl: string = res.headers.get("location") as string;
61
+ const newUrl: string = res.headers.get("location") ?? res.url;
62
+ const sameHost = new URL(newUrl).host === url1.host;
59
63
  if (res.status == 301 || res.status == 302) {
60
- // const sameHost = new URL(newUrl).host === url1.host;
64
+ // follow the redirect
61
65
  if (newUrl && newUrl !== url2) {
62
66
  if (parsedArgs.verbose) {
63
67
  console.log(blue(`Redirected to `), yellow(newUrl));
@@ -66,11 +70,37 @@ export async function fetchWithCookies(
66
70
  //
67
71
  newUrl,
68
72
  newOptions1,
69
- fetch
73
+ fetch,
74
+ originalRequest1
70
75
  //
71
76
  );
72
77
  }
73
78
  }
79
+ if (res.status == 303 && newUrl && newUrl !== url2) {
80
+ // follow the redirect with GET
81
+ let newOptions2: RequestInit = {};
82
+ switch (newOptions1.method) {
83
+ case "POST":
84
+ case "PUT":
85
+ case "DELETE":
86
+ merge(newOptions2, newOptions1, { method: "GET" });
87
+ break;
88
+ default:
89
+ merge(newOptions2, newOptions1);
90
+ break;
91
+ }
92
+ if (parsedArgs.verbose) {
93
+ console.log(blue(`Redirected to `), yellow(newUrl));
94
+ }
95
+ return fetchWithCookies(
96
+ //
97
+ newUrl,
98
+ newOptions2,
99
+ fetch,
100
+ originalRequest1
101
+ //
102
+ );
103
+ }
74
104
 
75
105
  const arrayBuffer1: Promise<ArrayBuffer> = res.arrayBuffer();
76
106
 
@@ -83,11 +113,11 @@ export async function fetchWithCookies(
83
113
  }
84
114
 
85
115
  async function text(): Promise<string> {
86
- return buffer().then((buffer) => buffer.toString("utf8"));
116
+ return buffer().then((buffer: Buffer) => buffer.toString("utf8"));
87
117
  }
88
118
 
89
119
  async function json(): Promise<any> {
90
- return text().then((text) => destr(text));
120
+ return text().then((text: string) => destr(text));
91
121
  }
92
122
 
93
123
  async function formData(): Promise<FormData> {