@mherod/get-cookie 1.3.0 → 2.0.0-beta.1

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.
@@ -0,0 +1,17 @@
1
+ import jsonwebtoken from "jsonwebtoken";
2
+ import { env } from "./global";
3
+
4
+ export default function isValidJwt(token) {
5
+ if (typeof token !== "string") {
6
+ return false;
7
+ }
8
+ try {
9
+ const result = jsonwebtoken.decode(token, { complete: true });
10
+ if (env.VERBOSE) {
11
+ console.log(result);
12
+ }
13
+ return true;
14
+ } catch (err) {
15
+ return false;
16
+ }
17
+ }
@@ -0,0 +1,28 @@
1
+ import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
2
+ import { uniq } from "lodash";
3
+ import { env } from "./global";
4
+ import isValidJwt from "./isValidJwt";
5
+
6
+ export async function queryCookies(
7
+ { name, domain },
8
+ strategy = new CompositeCookieQueryStrategy()
9
+ ) {
10
+ const results = await strategy.queryCookies(name, domain);
11
+ const results1 = uniq(results).map((cookie) => {
12
+ return {
13
+ name: name,
14
+ domain: domain,
15
+ value: cookie,
16
+ };
17
+ });
18
+ const jwtCookies = [];
19
+ for (const result of results1) {
20
+ const value = result.value;
21
+ if (isValidJwt(value)) {
22
+ jwtCookies.push(result);
23
+ }
24
+ }
25
+ const results2 = env.REQUIRE_JWT ? jwtCookies : results1;
26
+ const resultsUniq = uniq(results2).map((cookie) => cookie.value);
27
+ return env.SINGLE ? [resultsUniq[0]] : resultsUniq;
28
+ }
package/src/utils.js CHANGED
@@ -1,9 +1,10 @@
1
1
  // noinspection JSUnusedGlobalSymbols
2
2
 
3
- const { exec } = require("child_process");
4
- const fs = require("fs");
3
+ import { exec } from "child_process";
5
4
 
6
- async function execSimple(command) {
5
+ import fs from "fs";
6
+
7
+ export async function execSimple(command) {
7
8
  if (typeof command !== "string") {
8
9
  throw new TypeError("execSimple: command must be a string");
9
10
  }
@@ -31,7 +32,7 @@ async function execSimple(command) {
31
32
  });
32
33
  }
33
34
 
34
- async function execAsBuffer(command) {
35
+ export async function execAsBuffer(command) {
35
36
  if (typeof command !== "string") {
36
37
  throw new TypeError("execAsBuffer: command must be a string");
37
38
  }
@@ -64,7 +65,7 @@ async function execAsBuffer(command) {
64
65
  });
65
66
  }
66
67
 
67
- function toStringValue(r) {
68
+ export function toStringValue(r) {
68
69
  if (process.env.VERBOSE) {
69
70
  console.log("Printing value", r);
70
71
  }
@@ -78,7 +79,7 @@ function toStringValue(r) {
78
79
  }
79
80
  }
80
81
 
81
- function printStringValue(r) {
82
+ export function printStringValue(r) {
82
83
  if (process.env.VERBOSE) {
83
84
  console.log("Printing value", r);
84
85
  }
@@ -97,7 +98,7 @@ function printStringValue(r) {
97
98
  * @param result
98
99
  * @returns {string|null}
99
100
  */
100
- function toStringOrNull(result) {
101
+ export function toStringOrNull(result) {
101
102
  if (result == null) {
102
103
  return null;
103
104
  }
@@ -120,7 +121,7 @@ function toStringOrNull(result) {
120
121
  * @param {string} sql
121
122
  * @returns {Promise<Buffer[]>}
122
123
  */
123
- async function doSqliteQuery1(file, sql) {
124
+ export async function doSqliteQuery1(file, sql) {
124
125
  if (typeof sql !== "string") {
125
126
  throw new TypeError("doSqliteQuery1: sql must be a string");
126
127
  }
@@ -175,11 +176,6 @@ async function doSqliteQuery1(file, sql) {
175
176
  });
176
177
  }
177
178
 
178
- module.exports = {
179
- execSimple: execSimple,
180
- execAsBuffer: execAsBuffer,
181
- printStringValue: printStringValue,
182
- toStringValue,
183
- toStringOrNull: toStringOrNull,
184
- doSqliteQuery1,
185
- };
179
+ export function invalidString(input) {
180
+ return typeof input !== "string" || input.length === 0;
181
+ }