@mherod/get-cookie 2.0.0-beta.9 → 2.0.0-rc.2

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.
@@ -4,7 +4,7 @@ import { fetch } from "cross-fetch";
4
4
  import { merge } from "lodash";
5
5
  // noinspection SpellCheckingInspection
6
6
  import destr from "destr";
7
- import FetchResponse from "./fetchResponse";
7
+ import FetchResponse from "./FetchResponse";
8
8
  import { getGroupedRenderedCookies } from "./getGroupedRenderedCookies";
9
9
 
10
10
  export async function fetchWithCookies(
@@ -13,9 +13,10 @@ export async function fetchWithCookies(
13
13
  ): Promise<FetchResponse> {
14
14
  const defaultOptions: RequestInit = {
15
15
  headers: {
16
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
16
+ "User-Agent":
17
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
17
18
  },
18
- redirect: "manual"
19
+ redirect: "manual",
19
20
  };
20
21
  const url2: string = `${url}`;
21
22
  const url1: URL = new URL(url2);
@@ -24,18 +25,18 @@ export async function fetchWithCookies(
24
25
  });
25
26
  const cookies: string[] = await getGroupedRenderedCookies({
26
27
  name: "%",
27
- domain: domain
28
- });
28
+ domain: domain,
29
+ }).catch(() => []);
29
30
  const cookie = cookies.pop();
30
31
  const newOptions1: RequestInit = merge(defaultOptions, options, {
31
32
  headers: {
32
- Cookie: cookie
33
- }
33
+ Cookie: cookie,
34
+ },
34
35
  });
35
36
  try {
36
- const res = await fetch(url2, newOptions1);
37
+ const res: Response = await fetch(url2, newOptions1);
37
38
  const newUrl = res.headers.get("location") as string;
38
- if (res.redirected || newUrl && newUrl !== url2) {
39
+ if (res.redirected || (newUrl && newUrl !== url2)) {
39
40
  return fetchWithCookies(newUrl, newOptions1);
40
41
  }
41
42
  const arrayBuffer1 = res.arrayBuffer();
@@ -43,7 +44,8 @@ export async function fetchWithCookies(
43
44
  const buffer = async () => arrayBuffer().then(Buffer.from);
44
45
  const text = async () => buffer().then((buffer) => buffer.toString("utf8"));
45
46
  const json = async () => text().then(destr);
46
- const formData = async () => text().then((text) => new URLSearchParams(text));
47
+ const formData = async () =>
48
+ text().then((text) => new URLSearchParams(text));
47
49
  const source2 = {
48
50
  status: res.status,
49
51
  statusText: res.statusText,
@@ -52,7 +54,7 @@ export async function fetchWithCookies(
52
54
  buffer,
53
55
  text,
54
56
  json,
55
- formData
57
+ formData,
56
58
  //
57
59
  };
58
60
  return merge({}, res, source2);
@@ -1,18 +1,16 @@
1
1
  import { env } from "./global";
2
2
  import * as fs from "fs";
3
3
 
4
- export async function findAllFiles(
5
- {
6
- path,
7
- name,
8
- maxDepth = 2
9
- //
10
- }: {
11
- path: string;
12
- name: string;
13
- maxDepth?: number;
14
- }
15
- ): Promise<string[]> {
4
+ export async function findAllFiles({
5
+ path,
6
+ name,
7
+ maxDepth = 2,
8
+ }: //
9
+ {
10
+ path: string;
11
+ name: string;
12
+ maxDepth?: number;
13
+ }): Promise<string[]> {
16
14
  const rootSegments = path.split("/").length;
17
15
 
18
16
  if (env.VERBOSE) {
@@ -45,7 +43,7 @@ export async function findAllFiles(
45
43
  const subFiles = await findAllFiles({
46
44
  path: filePath,
47
45
  name: name,
48
- maxDepth: 2
46
+ maxDepth: 2,
49
47
  });
50
48
  files.push(...subFiles);
51
49
  } catch (e) {
@@ -2,18 +2,18 @@ import { queryCookies } from "./queryCookies";
2
2
  import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
3
3
  import { groupBy } from "lodash";
4
4
  import { resultsRendered } from "./resultsRendered";
5
- import { CookieRequest } from "./CookieRequest";
5
+ import { CookieSpec } from "./CookieSpec";
6
6
  import { ExportedCookie } from "./ExportedCookie";
7
7
 
8
8
  export async function getGroupedRenderedCookies(
9
9
  //
10
10
  {
11
11
  name,
12
- domain
13
- //
14
- }: CookieRequest
15
- //
16
- ): Promise<string[]> {
12
+ domain,
13
+ }: //
14
+ CookieSpec
15
+ ): //
16
+ Promise<string[]> {
17
17
  const cookies: ExportedCookie[] = await queryCookies(
18
18
  { name, domain },
19
19
  new CompositeCookieQueryStrategy()
package/src/global.ts CHANGED
@@ -1,4 +1,5 @@
1
- const { merge } = require("lodash");
1
+ import { merge } from "lodash";
2
+
2
3
  export const env: any = {};
3
4
  merge(env, process.env);
4
5
  export const HOME: string = env["HOME"];
package/src/index.ts CHANGED
@@ -5,12 +5,14 @@ import { queryCookies } from "./queryCookies";
5
5
  import FirefoxCookieQueryStrategy from "./browsers/FirefoxCookieQueryStrategy";
6
6
  import ChromeCookieQueryStrategy from "./browsers/ChromeCookieQueryStrategy";
7
7
  import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
8
- import { CookieRequest } from "./CookieRequest";
8
+ import { CookieSpec } from "./CookieSpec";
9
9
  import { ExportedCookie } from "./ExportedCookie";
10
10
  import { getGroupedRenderedCookies } from "./getGroupedRenderedCookies";
11
11
  import { fetchWithCookies } from "./fetchWithCookies";
12
12
 
13
- export async function getCookie(params: CookieRequest): Promise<ExportedCookie | undefined> {
13
+ export async function getCookie(
14
+ params: CookieSpec
15
+ ): Promise<ExportedCookie | undefined> {
14
16
  const cookies = await queryCookies(
15
17
  params,
16
18
  new CompositeCookieQueryStrategy()
@@ -23,7 +25,9 @@ export async function getCookie(params: CookieRequest): Promise<ExportedCookie |
23
25
  }
24
26
  }
25
27
 
26
- export async function getFirefoxCookie(params: CookieRequest): Promise<ExportedCookie | undefined> {
28
+ export async function getFirefoxCookie(
29
+ params: CookieSpec
30
+ ): Promise<ExportedCookie | undefined> {
27
31
  const cookies = await queryCookies(
28
32
  params,
29
33
  new FirefoxCookieQueryStrategy()
@@ -36,7 +40,9 @@ export async function getFirefoxCookie(params: CookieRequest): Promise<ExportedC
36
40
  }
37
41
  }
38
42
 
39
- export async function getChromeCookie(params: CookieRequest): Promise<ExportedCookie | undefined> {
43
+ export async function getChromeCookie(
44
+ params: CookieSpec
45
+ ): Promise<ExportedCookie | undefined> {
40
46
  const cookies = await queryCookies(
41
47
  params,
42
48
  new ChromeCookieQueryStrategy()
@@ -49,11 +55,8 @@ export async function getChromeCookie(params: CookieRequest): Promise<ExportedCo
49
55
  }
50
56
  }
51
57
 
52
- export {
53
- getGroupedRenderedCookies,
54
- fetchWithCookies,
55
- };
58
+ export { getGroupedRenderedCookies, fetchWithCookies };
56
59
 
57
- export * from "./CookieRequest";
60
+ export * from "./CookieSpec";
58
61
  export * from "./CookieRow";
59
62
  export * from "./ExportedCookie";
package/src/isValidJwt.ts CHANGED
@@ -1,4 +1,4 @@
1
- import jsonwebtoken from "jsonwebtoken";
1
+ import jsonwebtoken, { JwtPayload } from "jsonwebtoken";
2
2
  import { env } from "./global";
3
3
 
4
4
  export default function isValidJwt(token: any) {
@@ -7,6 +7,16 @@ export default function isValidJwt(token: any) {
7
7
  if (env.VERBOSE) {
8
8
  console.log(result);
9
9
  }
10
+ const payload: JwtPayload = result?.payload as JwtPayload;
11
+ if (payload) {
12
+ const exp = payload.exp;
13
+ if (exp) {
14
+ const now = new Date().getTime() / 1000;
15
+ if (now > exp) {
16
+ return false;
17
+ }
18
+ }
19
+ }
10
20
  return true;
11
21
  } catch (err) {
12
22
  return false;
@@ -3,14 +3,11 @@ import { uniqBy } from "lodash";
3
3
  import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
4
4
  import CookieQueryStrategy from "./browsers/CookieQueryStrategy";
5
5
  import isValidJwt from "./isValidJwt";
6
- import { CookieRequest } from "./CookieRequest";
6
+ import { CookieSpec } from "./CookieSpec";
7
7
  import { ExportedCookie } from "./ExportedCookie";
8
8
 
9
9
  export async function queryCookies(
10
- {
11
- name,
12
- domain
13
- }: CookieRequest,
10
+ { name, domain }: CookieSpec,
14
11
  strategy: CookieQueryStrategy = new CompositeCookieQueryStrategy()
15
12
  ) {
16
13
  const results: ExportedCookie[] = await strategy.queryCookies(name, domain);
@@ -0,0 +1,13 @@
1
+ export function unpackHeaders(headerArgs: string[] | string) {
2
+ const headers: any = {};
3
+ if (Array.isArray(headerArgs)) {
4
+ for (const h of headerArgs) {
5
+ const [key, value] = h.split("=");
6
+ headers[key] = value;
7
+ }
8
+ } else {
9
+ const [key, value] = headerArgs.split("=");
10
+ headers[key] = value;
11
+ }
12
+ return headers;
13
+ }
package/src/utils.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  // noinspection JSUnusedGlobalSymbols
2
2
 
3
- import {exec, ExecException} from "child_process";
3
+ import { exec, ExecException } from "child_process";
4
4
 
5
5
  export async function execSimple(command: string): Promise<string> {
6
6
  return new Promise((resolve, reject) => {
@@ -24,11 +24,6 @@ export async function execSimple(command: string): Promise<string> {
24
24
  });
25
25
  }
26
26
 
27
- /**
28
- *
29
- * @param result
30
- * @returns {string|null}
31
- */
32
27
  export function toStringOrNull(result: any) {
33
28
  if (result == null) {
34
29
  return null;
@@ -50,7 +45,6 @@ export function invalidString(input: any): boolean {
50
45
  return typeof input !== "string" || input.length === 0;
51
46
  }
52
47
 
53
-
54
48
  export function validString(input: any): input is string {
55
49
  return typeof input == "string" && input.length > 0;
56
50
  }