@mherod/get-cookie 2.0.0-rc.19 → 2.0.0-rc.20

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/module.js ADDED
@@ -0,0 +1,996 @@
1
+ #!/usr/bin/env node
2
+ import {uniqBy as $6B8p2$uniqBy, merge as $6B8p2$merge, groupBy as $6B8p2$groupBy, orderBy as $6B8p2$orderBy} from "lodash";
3
+ import $6B8p2$minimist from "minimist";
4
+ import $6B8p2$lrucache from "lru-cache";
5
+ import {red as $6B8p2$red, redBright as $6B8p2$redBright, blue as $6B8p2$blue, yellow as $6B8p2$yellow} from "colorette";
6
+ import $6B8p2$fs, {existsSync as $6B8p2$existsSync, readdirSync as $6B8p2$readdirSync, statSync as $6B8p2$statSync, readFileSync as $6B8p2$readFileSync, writeFile as $6B8p2$writeFile} from "fs";
7
+ import {pbkdf2 as $6B8p2$pbkdf2, createDecipheriv as $6B8p2$createDecipheriv} from "crypto";
8
+ import {join as $6B8p2$join} from "path";
9
+ import {Database as $6B8p2$Database} from "sqlite3";
10
+ import {exec as $6B8p2$exec} from "child_process";
11
+ import {MemoryCookieStore as $6B8p2$MemoryCookieStore, CookieJar as $6B8p2$CookieJar, Cookie as $6B8p2$Cookie, Store as $6B8p2$Store} from "tough-cookie";
12
+ import $6B8p2$destr from "destr";
13
+ import $6B8p2$jsonwebtoken from "jsonwebtoken";
14
+ import {fetch as $6B8p2$fetch} from "cross-fetch";
15
+ import $6B8p2$useragents from "user-agents";
16
+
17
+
18
+
19
+
20
+ const $105ddf345d82788c$export$6f4ae1207c703045 = process.argv ?? [];
21
+ const $105ddf345d82788c$var$minimistArgs = (0, $6B8p2$minimist)($105ddf345d82788c$export$6f4ae1207c703045.slice(2));
22
+ const $105ddf345d82788c$var$defaultOptions = {
23
+ verbose: false
24
+ };
25
+ const $105ddf345d82788c$export$c348dfaf65040d9b = (0, $6B8p2$merge)($105ddf345d82788c$var$defaultOptions, $105ddf345d82788c$var$minimistArgs);
26
+
27
+
28
+
29
+ async function $5487dc2a77024e5b$export$8d71c74f97c0202e(command) {
30
+ return new Promise((resolve, reject)=>{
31
+ (0, $6B8p2$exec)(command, {
32
+ encoding: "binary",
33
+ maxBuffer: 5120
34
+ }, (error, stdout, stderr)=>{
35
+ if (error) {
36
+ reject(error);
37
+ return;
38
+ }
39
+ if (stderr) {
40
+ reject(error);
41
+ return;
42
+ }
43
+ if (stdout) resolve(stdout.trim());
44
+ });
45
+ });
46
+ }
47
+
48
+
49
+
50
+ const $27811734c054cd51$export$a7b6bc01c63cdfc3 = {};
51
+ (0, $6B8p2$merge)($27811734c054cd51$export$a7b6bc01c63cdfc3, process.env);
52
+ const $27811734c054cd51$export$a7c5707626fc90f = $27811734c054cd51$export$a7b6bc01c63cdfc3["HOME"];
53
+ if (!$27811734c054cd51$export$a7c5707626fc90f) throw new Error("HOME environment variable is not set");
54
+
55
+
56
+
57
+
58
+
59
+ async function $5ddc6ac5caa391cf$export$2c9faf86071156ae({ path: path , name: name , maxDepth: maxDepth = 2 }) {
60
+ const rootSegments = path.split("/").length;
61
+ const files = [];
62
+ let readdirSync;
63
+ try {
64
+ readdirSync = $6B8p2$readdirSync(path);
65
+ } catch (e) {
66
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log(`Error reading ${path}`, e);
67
+ return [];
68
+ }
69
+ for (const file of readdirSync){
70
+ const filePath = path + "/" + file;
71
+ let stat;
72
+ try {
73
+ stat = $6B8p2$statSync(filePath);
74
+ } catch (e1) {
75
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.error(`Error getting stat for ${filePath}`, e1);
76
+ continue;
77
+ }
78
+ if (stat.isDirectory()) {
79
+ if (filePath.split("/").length < rootSegments + maxDepth) try {
80
+ const subFiles = await $5ddc6ac5caa391cf$export$2c9faf86071156ae({
81
+ path: filePath,
82
+ name: name,
83
+ maxDepth: 2
84
+ });
85
+ files.push(...subFiles);
86
+ } catch (e2) {
87
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.error(e2);
88
+ }
89
+ } else if (file === name) files.push(filePath);
90
+ }
91
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) {
92
+ if (files.length > 0) {
93
+ console.log(`Found ${files.length} ${name} files`);
94
+ console.log(files);
95
+ }
96
+ }
97
+ return files;
98
+ }
99
+
100
+
101
+
102
+
103
+
104
+ function $0128ed33064d0b33$export$bc926c43858bd3cb(obj) {
105
+ return obj.domain !== undefined && obj.name !== undefined && obj.value !== undefined;
106
+ }
107
+
108
+
109
+ function $954720edc83b2219$export$963c8d651337e4a4(obj) {
110
+ return obj.domain !== undefined && obj.name !== undefined && obj.value !== undefined;
111
+ }
112
+
113
+
114
+ function $8a525b77a45adb9e$export$4eadcf0bf35a2ddb(s) {
115
+ const s1 = s.replaceAll(/\./gi, ".");
116
+ const s2 = s1.replace(/%/g, ".*");
117
+ const s3 = s2.replace(/\*/g, ".*");
118
+ return new RegExp(s3);
119
+ }
120
+
121
+
122
+
123
+
124
+ class $245d30db16a07938$export$2e2bcd8739ae039 {
125
+ browserName = "Chrome";
126
+ async queryCookies(name, domain) {
127
+ if (process.platform !== "darwin") throw new Error("This only works on macOS");
128
+ if ((0, $27811734c054cd51$export$a7b6bc01c63cdfc3).FIREFOX_ONLY) return [];
129
+ return $245d30db16a07938$var$getChromeCookies({
130
+ requireJwt: false,
131
+ name: name,
132
+ domain: domain
133
+ });
134
+ }
135
+ }
136
+ async function $245d30db16a07938$var$getPromise1(name, domain, file) {
137
+ try {
138
+ return await $245d30db16a07938$var$getEncryptedChromeCookie({
139
+ name: name,
140
+ domain: domain,
141
+ file: file
142
+ });
143
+ } catch (e) {
144
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log("Error getting encrypted cookie", e);
145
+ return [];
146
+ }
147
+ }
148
+ async function $245d30db16a07938$var$getPromise(name, domain) {
149
+ try {
150
+ const files = await (0, $5ddc6ac5caa391cf$export$2c9faf86071156ae)({
151
+ path: $245d30db16a07938$var$chromeLocal,
152
+ name: "Cookies"
153
+ });
154
+ const promises = files.map((file)=>$245d30db16a07938$var$getPromise1(name, domain, file));
155
+ const results1 = await Promise.all(promises);
156
+ return results1.flat().filter((0, $0128ed33064d0b33$export$bc926c43858bd3cb));
157
+ } catch (error) {
158
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log("error", error);
159
+ return [];
160
+ }
161
+ }
162
+ async function $245d30db16a07938$var$decryptValue(password, encryptedValue) {
163
+ let d;
164
+ try {
165
+ d = await $245d30db16a07938$var$decrypt(password, encryptedValue);
166
+ } catch (e) {
167
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log("Error decrypting cookie", e);
168
+ d = null;
169
+ }
170
+ return d ?? encryptedValue.toString("utf-8");
171
+ }
172
+ async function $245d30db16a07938$var$getChromeCookies({ name: name , domain: domain = "%" , requireJwt: requireJwt = false }) {
173
+ const encryptedDataItems = await $245d30db16a07938$var$getPromise(name, domain);
174
+ const password = await $245d30db16a07938$var$getChromePassword();
175
+ const decrypted = encryptedDataItems.filter(({ value: value })=>value != null && value.length > 0).map(async (cookieRow)=>{
176
+ const encryptedValue = cookieRow.value;
177
+ const decryptedValue = await $245d30db16a07938$var$decryptValue(password, encryptedValue);
178
+ const meta = {};
179
+ (0, $6B8p2$merge)(meta, cookieRow.meta ?? {});
180
+ const exportedCookie = {
181
+ domain: cookieRow.domain,
182
+ name: cookieRow.name,
183
+ value: decryptedValue,
184
+ meta: meta
185
+ };
186
+ const expiry = cookieRow.expiry;
187
+ const mergeExpiry = expiry != null && expiry > 0 ? {
188
+ expiry: new Date(expiry)
189
+ } : {
190
+ expiry: "Infinity"
191
+ };
192
+ (0, $6B8p2$merge)(exportedCookie, mergeExpiry);
193
+ return exportedCookie;
194
+ });
195
+ const results = (await Promise.all(decrypted)).filter((0, $954720edc83b2219$export$963c8d651337e4a4));
196
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log("results", results);
197
+ return results;
198
+ }
199
+ const $245d30db16a07938$var$chromeLocal = $6B8p2$join((0, $27811734c054cd51$export$a7c5707626fc90f), "Library", "Application Support", "Google", "Chrome");
200
+ async function $245d30db16a07938$var$getEncryptedChromeCookie({ name: name , domain: domain , file: file = $6B8p2$join($245d30db16a07938$var$chromeLocal, "Default", "Cookies") }) {
201
+ if (!(0, $6B8p2$existsSync)(file)) throw new Error(`File ${file} does not exist`);
202
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) {
203
+ const s = file.split("/").slice(-3).join("/");
204
+ console.log(`Trying Chrome (at ${s}) cookie ${name} for domain ${domain}`);
205
+ }
206
+ let sql;
207
+ //language=SQL
208
+ sql = "SELECT * FROM cookies";
209
+ // sql = "SELECT encrypted_value, name, host_key FROM cookies";
210
+ const wildcardRegexp = /^([*%])$/i;
211
+ const specifiedName = name.match(wildcardRegexp) == null;
212
+ const specifiedDomain = domain.match(wildcardRegexp) == null;
213
+ const wildcardDomain = domain.match(/[%*]/) != null;
214
+ const queryDomain = specifiedDomain && !wildcardDomain;
215
+ // if we have a wildcard domain, we need to use a regexp
216
+ if (specifiedName || queryDomain) {
217
+ sql += ` WHERE `;
218
+ if (specifiedName) {
219
+ sql += `name = '${name}'`;
220
+ if (queryDomain) sql += ` AND `;
221
+ }
222
+ if (queryDomain) {
223
+ // leading dot replaced with % to match subdomains
224
+ const sqlEmbedDomain = domain.replace(/^[.%]?/, "%");
225
+ sql += `host_key LIKE '${sqlEmbedDomain}';`;
226
+ }
227
+ }
228
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log("sql", sql);
229
+ const domainRegexp = (0, $8a525b77a45adb9e$export$4eadcf0bf35a2ddb)(domain);
230
+ const sqliteQuery1 = await $245d30db16a07938$export$bd52b72df8d0d261({
231
+ file: file,
232
+ sql: sql,
233
+ rowFilter: (row)=>{
234
+ return row["host_key"].match(domainRegexp) != null;
235
+ },
236
+ rowTransform: (row)=>{
237
+ const cookieRow = {
238
+ expiry: (row["expires_utc"] / 1000000 - 11644473600) * 1000,
239
+ domain: row["host_key"],
240
+ name: row["name"],
241
+ value: row["encrypted_value"]
242
+ };
243
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log("CookieRow", cookieRow);
244
+ return cookieRow;
245
+ }
246
+ });
247
+ return sqliteQuery1.filter((row)=>{
248
+ return row.domain.match(domainRegexp) != null;
249
+ });
250
+ }
251
+ async function $245d30db16a07938$var$getChromePassword() {
252
+ return (0, $5487dc2a77024e5b$export$8d71c74f97c0202e)('security find-generic-password -w -s "Chrome Safe Storage"');
253
+ }
254
+ async function $245d30db16a07938$var$decrypt(password, encryptedData) {
255
+ if (typeof password !== "string") throw new Error("password must be a string: " + password);
256
+ let encryptedData1;
257
+ encryptedData1 = encryptedData;
258
+ if (encryptedData1 == null || typeof encryptedData1 !== "object") throw new Error("encryptedData must be a object: " + encryptedData1);
259
+ if (!(encryptedData1 instanceof Buffer)) {
260
+ if (Array.isArray(encryptedData1) && encryptedData1[0] instanceof Buffer) {
261
+ [encryptedData1] = encryptedData1;
262
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log(`encryptedData is an array of buffers, selected first: ${encryptedData1}`);
263
+ } else throw new Error("encryptedData must be a Buffer: " + encryptedData1);
264
+ encryptedData1 = Buffer.from(encryptedData1);
265
+ }
266
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log(`Trying to decrypt with password ${password}`);
267
+ return new Promise((resolve, reject)=>{
268
+ $6B8p2$pbkdf2(password, "saltysalt", 1003, 16, "sha1", (error, buffer)=>{
269
+ try {
270
+ if (error) {
271
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log("Error doing pbkdf2", error);
272
+ reject(error);
273
+ return;
274
+ }
275
+ if (buffer.length !== 16) {
276
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log("Error doing pbkdf2, buffer length is not 16", buffer.length);
277
+ reject(new Error("Buffer length is not 16"));
278
+ return;
279
+ }
280
+ const str = new Array(17).join(" ");
281
+ const iv = Buffer.from(str, "binary");
282
+ const decipher = $6B8p2$createDecipheriv("aes-128-cbc", buffer, iv);
283
+ decipher.setAutoPadding(false);
284
+ if (encryptedData1 && encryptedData1.slice) encryptedData1 = encryptedData1.slice(3);
285
+ if (encryptedData1.length % 16 !== 0) {
286
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log("Error doing pbkdf2, encryptedData length is not a multiple of 16", encryptedData1.length);
287
+ reject(new Error("encryptedData length is not a multiple of 16"));
288
+ return;
289
+ }
290
+ let decoded = decipher.update(encryptedData1);
291
+ try {
292
+ decipher.final("utf-8");
293
+ } catch (e) {
294
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log("Error doing decipher.final()", e);
295
+ reject(e);
296
+ return;
297
+ }
298
+ const padding = decoded[decoded.length - 1];
299
+ if (padding) decoded = decoded.slice(0, 0 - padding);
300
+ // noinspection JSCheckFunctionSignatures
301
+ const decodedString = decoded.toString("utf8");
302
+ resolve(decodedString);
303
+ } catch (e1) {
304
+ reject(e1);
305
+ }
306
+ });
307
+ });
308
+ }
309
+ async function $245d30db16a07938$export$bd52b72df8d0d261({ file: file , sql: sql , rowFilter: rowFilter = ()=>true , rowTransform: rowTransform }) {
310
+ if (!file || file && !(0, $6B8p2$fs).existsSync(file)) throw new Error(`doSqliteQuery1: file ${file} does not exist`);
311
+ const db = new $6B8p2$Database(file);
312
+ return new Promise((resolve, reject)=>{
313
+ db.all(sql, (err, rows)=>{
314
+ if (err) {
315
+ reject(err);
316
+ return;
317
+ }
318
+ const rows1 = rows;
319
+ if (rows1 == null || rows1.length === 0) {
320
+ resolve([]);
321
+ return;
322
+ }
323
+ if (Array.isArray(rows1)) {
324
+ const cookieRows = rows1.filter(rowFilter).map((row)=>{
325
+ const newVar = {
326
+ meta: {
327
+ file: file
328
+ }
329
+ };
330
+ const cookieRow = rowTransform(row);
331
+ return (0, $6B8p2$merge)(newVar, cookieRow);
332
+ });
333
+ resolve(cookieRows);
334
+ return;
335
+ }
336
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log(`doSqliteQuery1: rows ${JSON.stringify(rows1)}`);
337
+ resolve([
338
+ rows1
339
+ ]);
340
+ });
341
+ });
342
+ }
343
+
344
+
345
+
346
+
347
+
348
+
349
+ function $043534331646fe99$export$da22813e5a2ec500({ name: name , domain: domain }) {
350
+ const wildcardRegexp = /^([*%])$/i;
351
+ return {
352
+ specifiedName: name.match(wildcardRegexp) == null,
353
+ specifiedDomain: domain.match(wildcardRegexp) == null
354
+ };
355
+ }
356
+
357
+
358
+
359
+
360
+
361
+ async function $ded29c0fa27637d3$export$f19f611d1fa7c6f3({ file: file , sql: sql , rowTransform: rowTransform }) {
362
+ if (!file || file && !(0, $6B8p2$fs).existsSync(file)) throw new Error(`doSqliteQuery1: file ${file} does not exist`);
363
+ const db = new $6B8p2$Database(file);
364
+ return new Promise((resolve, reject)=>{
365
+ db.all(sql, (err, rows)=>{
366
+ if (err) {
367
+ reject(err);
368
+ return;
369
+ }
370
+ const rows1 = rows;
371
+ if (rows1 == null || rows1.length === 0) {
372
+ resolve([]);
373
+ return;
374
+ }
375
+ if (Array.isArray(rows1)) {
376
+ const cookieRows = rows1.map((row)=>{
377
+ const newVar = {
378
+ meta: {
379
+ file: file
380
+ }
381
+ };
382
+ const cookieRow = rowTransform(row);
383
+ return (0, $6B8p2$merge)(newVar, cookieRow);
384
+ });
385
+ resolve(cookieRows);
386
+ return;
387
+ }
388
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log(`doSqliteQuery1: rows ${JSON.stringify(rows1)}`);
389
+ resolve([
390
+ rows1
391
+ ]);
392
+ });
393
+ });
394
+ }
395
+ class $ded29c0fa27637d3$export$2e2bcd8739ae039 {
396
+ browserName = "Firefox";
397
+ async queryCookies(name, domain) {
398
+ if (process.platform !== "darwin") // TODO: implement
399
+ return [];
400
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).browser !== "firefox") return [];
401
+ const cookies = await this.#getFirefoxCookie({
402
+ name: name,
403
+ domain: domain
404
+ });
405
+ if (Array.isArray(cookies)) return cookies.map((cookie)=>{
406
+ return {
407
+ domain: cookie.domain,
408
+ name: cookie.name,
409
+ value: cookie.value.toString("utf8")
410
+ };
411
+ });
412
+ else return [];
413
+ }
414
+ async #getFirefoxCookie({ name: name , domain: domain } //
415
+ ) {
416
+ const files = await (0, $5ddc6ac5caa391cf$export$2c9faf86071156ae)({
417
+ path: $6B8p2$join((0, $27811734c054cd51$export$a7c5707626fc90f), "Library", "Application Support", "Firefox", "Profiles"),
418
+ name: "cookies.sqlite"
419
+ });
420
+ const fn = async (file)=>{
421
+ return await this.#queryCookiesDb(file, name, domain);
422
+ };
423
+ const all = await Promise.all(files.map(fn));
424
+ // flatten
425
+ return all.flat();
426
+ }
427
+ async #queryCookiesDb(file, name1, domain1) {
428
+ if (file && !(0, $6B8p2$existsSync)(file)) throw new Error(`File ${file} does not exist`);
429
+ let sql;
430
+ //language=SQL
431
+ sql = "SELECT value, name, host FROM moz_cookies";
432
+ const { specifiedName: specifiedName , specifiedDomain: specifiedDomain } = (0, $043534331646fe99$export$da22813e5a2ec500)({
433
+ name: name1,
434
+ domain: domain1
435
+ });
436
+ if (specifiedName || specifiedDomain) {
437
+ sql += ` WHERE `;
438
+ if (specifiedName) {
439
+ sql += `name = '${name1}'`;
440
+ if (specifiedDomain) sql += ` AND `;
441
+ }
442
+ if (specifiedDomain) sql += `host LIKE '${domain1}';`;
443
+ }
444
+ const rowTransform = (row)=>{
445
+ // row is object key by column name
446
+ const value = row.value;
447
+ return {
448
+ domain: row.domain,
449
+ name: row.name,
450
+ value: Buffer.from(value, "utf8")
451
+ };
452
+ };
453
+ try {
454
+ return await $ded29c0fa27637d3$export$f19f611d1fa7c6f3({
455
+ file: file,
456
+ sql: sql,
457
+ rowTransform: rowTransform
458
+ });
459
+ } catch (e) {
460
+ console.error(`Error querying ${file}`, e);
461
+ return [];
462
+ }
463
+ }
464
+ }
465
+
466
+
467
+ class $68ef79184bd02203$export$2e2bcd8739ae039 {
468
+ browserName = "Safari";
469
+ async queryCookies(name, domain) {
470
+ // TODO: implement
471
+ return [];
472
+ }
473
+ }
474
+
475
+
476
+
477
+
478
+
479
+
480
+
481
+
482
+
483
+
484
+ class $703b18edcf62c952$export$55374e421ca433de extends (0, $6B8p2$Store) {
485
+ synchronous = false;
486
+ tasks = [];
487
+ constructor(filePath, internalStore = new (0, $6B8p2$MemoryCookieStore)()){
488
+ super();
489
+ this.filePath = filePath;
490
+ this.internalStore = internalStore ?? new (0, $6B8p2$MemoryCookieStore)();
491
+ console.log("Importing cookies from", filePath, internalStore);
492
+ this.tasks.push(this.importSaved(filePath, internalStore).then(console.log, console.error));
493
+ }
494
+ async importSaved(filePath, store) {
495
+ const cookies = [];
496
+ if ((0, $6B8p2$existsSync)(filePath)) {
497
+ const data = (0, $6B8p2$readFileSync)(filePath, "utf8");
498
+ const cookies1 = Object.values((0, $6B8p2$destr)(data)).flatMap((domainCookies)=>Object.values(domainCookies)).flatMap((domainCookies)=>Object.values(domainCookies));
499
+ console.log("Importing", cookies1.length, "cookies");
500
+ const array = Array.isArray(cookies1) ? cookies1 : Object.values(cookies1);
501
+ const put = await Promise.all(array.map((cookie)=>{
502
+ const fromJSON = (0, $6B8p2$Cookie).fromJSON(cookie);
503
+ if (fromJSON) return this.putCookieInternal(fromJSON, store);
504
+ }));
505
+ cookies1.push(...put);
506
+ }
507
+ return cookies;
508
+ }
509
+ findCookie(domain, path, key, cb) {
510
+ this.waitUntilIdle().finally(()=>{
511
+ this.internalStore.findCookie(domain, path, key, cb);
512
+ });
513
+ }
514
+ findCookies(domain, path, allowSpecialUseDomain, cb) {
515
+ this.waitUntilIdle().finally(()=>{
516
+ this.internalStore.findCookies(domain, path, allowSpecialUseDomain, cb);
517
+ });
518
+ }
519
+ putCookie(cookie, cb) {
520
+ this.internalStore.putCookie(cookie, cb);
521
+ this.internalStore.getAllCookies((err, cookies)=>{
522
+ this.serializeCallback(err, cookies);
523
+ });
524
+ }
525
+ updateCookie(oldCookie, newCookie, cb) {
526
+ this.internalStore.updateCookie(oldCookie, newCookie, cb);
527
+ this.internalStore.getAllCookies((err, cookies)=>{
528
+ this.serializeCallback(err, cookies);
529
+ });
530
+ }
531
+ removeCookie(domain, path, key, cb) {
532
+ this.internalStore.removeCookie(domain, path, key, cb);
533
+ this.internalStore.getAllCookies((err, cookies)=>{
534
+ this.serializeCallback(err, cookies);
535
+ });
536
+ }
537
+ removeCookies(domain, path, cb) {
538
+ this.internalStore.removeCookies(domain, path, cb);
539
+ this.internalStore.getAllCookies((err, cookies)=>{
540
+ this.serializeCallback(err, cookies);
541
+ });
542
+ }
543
+ getAllCookies(cb) {
544
+ this.waitUntilIdle().finally(()=>{
545
+ this.internalStore.getAllCookies(cb);
546
+ });
547
+ }
548
+ waitUntilIdle() {
549
+ return Promise.allSettled(this.tasks).catch(console.error);
550
+ }
551
+ async putCookieInternal(cookie, store) {
552
+ await this.waitUntilIdle();
553
+ return await new Promise((resolve, reject)=>{
554
+ store.putCookie(cookie, (err)=>{
555
+ if (err) reject(err);
556
+ else resolve(cookie);
557
+ });
558
+ });
559
+ }
560
+ serializeCallback(err, cookies) {
561
+ if (err) {
562
+ console.error(err);
563
+ return;
564
+ }
565
+ const cookieObjs = cookies.map((cookie)=>cookie);
566
+ const idx = {};
567
+ for (const cookie of cookieObjs){
568
+ const domain = cookie.domain;
569
+ if (typeof domain === "string") {
570
+ const path = cookie.path ?? "/";
571
+ const key = cookie.key;
572
+ (0, $6B8p2$merge)(idx, {
573
+ [domain]: {
574
+ [path]: {
575
+ [key]: cookie.toJSON()
576
+ }
577
+ }
578
+ });
579
+ }
580
+ }
581
+ const stringify = JSON.stringify(idx, null, 2);
582
+ (0, $6B8p2$writeFile)(this.filePath, stringify, (err)=>{
583
+ if (err) console.error(err);
584
+ });
585
+ }
586
+ }
587
+
588
+
589
+ const $7e7374355a590069$var$memoryCookieStore = new (0, $6B8p2$MemoryCookieStore)();
590
+ async function $7e7374355a590069$var$getCookieStore() {
591
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b)["cs"] == "memory") return $7e7374355a590069$var$memoryCookieStore;
592
+ const fileCookieStore = new (0, $703b18edcf62c952$export$55374e421ca433de)(`${(0, $27811734c054cd51$export$a7b6bc01c63cdfc3)["HOME"]}/cookie-star.json`, $7e7374355a590069$var$memoryCookieStore);
593
+ await fileCookieStore.waitUntilIdle();
594
+ return fileCookieStore;
595
+ }
596
+ const $7e7374355a590069$export$2c043f03eceff4e8 = new Promise((resolve)=>{
597
+ $7e7374355a590069$var$getCookieStore().then(resolve);
598
+ });
599
+ const $7e7374355a590069$export$eced8873dedbb4c0 = new Promise((resolve)=>{
600
+ $7e7374355a590069$export$2c043f03eceff4e8.then((store)=>new (0, $6B8p2$CookieJar)(store)).then(resolve);
601
+ });
602
+
603
+
604
+
605
+ class $6f21bbb0b106370d$export$2e2bcd8739ae039 {
606
+ browserName = "internal";
607
+ async queryCookies(name, domain) {
608
+ const exportedCookies = [];
609
+ // if (name.match(/[%*]/) || domain.match(/[%*]/)) {
610
+ const cookies = await this.#getAllCookies();
611
+ const allExportedCookies = cookies.map((cookie)=>{
612
+ return this.#extracted(cookie, {
613
+ name: name,
614
+ domain: domain
615
+ });
616
+ });
617
+ exportedCookies.push(...allExportedCookies);
618
+ // }
619
+ const wildcardRegexp = /^([*%])$/i;
620
+ if (name.match(wildcardRegexp) && domain.match(wildcardRegexp)) return exportedCookies;
621
+ const path = "/";
622
+ if (domain != "%") {
623
+ const domain1 = domain.match(/(\w+.+\w+)/gi)?.pop() ?? domain;
624
+ const url = new URL("https://" + domain1);
625
+ url.pathname = path;
626
+ const cookies1 = await this.#getCookies(url.href);
627
+ const domainCookies = cookies1.map((cookie)=>{
628
+ return this.#extracted(cookie, {
629
+ domain: domain,
630
+ name: name
631
+ });
632
+ });
633
+ exportedCookies.push(...domainCookies);
634
+ }
635
+ if (name == "%") return exportedCookies.filter((cookie)=>{
636
+ return cookie.domain.match((0, $8a525b77a45adb9e$export$4eadcf0bf35a2ddb)(domain));
637
+ });
638
+ return exportedCookies.filter((cookie)=>{
639
+ return cookie.name.match((0, $8a525b77a45adb9e$export$4eadcf0bf35a2ddb)(name)) && cookie.domain.match((0, $8a525b77a45adb9e$export$4eadcf0bf35a2ddb)(domain));
640
+ });
641
+ }
642
+ #extracted(cookie, cookieSpec) {
643
+ return {
644
+ domain: cookie.domain ?? cookieSpec.domain,
645
+ name: cookie.key ?? cookieSpec.name,
646
+ value: cookie.value,
647
+ expiry: cookie.expires ?? "Infinity",
648
+ meta: {
649
+ file: "tough-cookie"
650
+ }
651
+ };
652
+ }
653
+ async #getCookies(url) {
654
+ const cookieJar = await (0, $7e7374355a590069$export$eced8873dedbb4c0);
655
+ return new Promise((resolve, reject)=>{
656
+ return cookieJar.getCookies(url, (err, cookies)=>{
657
+ if (err) reject(err);
658
+ else resolve(cookies ?? []);
659
+ });
660
+ });
661
+ }
662
+ async #getAllCookies() {
663
+ const cookieStore = await (0, $7e7374355a590069$export$2c043f03eceff4e8);
664
+ return new Promise((resolve, reject)=>{
665
+ return cookieStore.getAllCookies((err, cookies)=>{
666
+ if (err) reject(err);
667
+ else resolve(cookies ?? []);
668
+ });
669
+ });
670
+ }
671
+ }
672
+
673
+
674
+
675
+
676
+
677
+ const $4ad2c96545f22a92$var$cache = new (0, $6B8p2$lrucache)({
678
+ ttl: 2000,
679
+ max: 10
680
+ });
681
+ class $4ad2c96545f22a92$export$2e2bcd8739ae039 {
682
+ browserName = "all";
683
+ #strategies;
684
+ constructor(){
685
+ this.#strategies = [
686
+ (0, $6f21bbb0b106370d$export$2e2bcd8739ae039),
687
+ (0, $245d30db16a07938$export$2e2bcd8739ae039),
688
+ (0, $ded29c0fa27637d3$export$2e2bcd8739ae039),
689
+ (0, $68ef79184bd02203$export$2e2bcd8739ae039),
690
+ ].map((strategy)=>{
691
+ return new strategy();
692
+ });
693
+ }
694
+ async queryCookies(name, domain) {
695
+ // domain = domain.match(/(\w+.+\w+)/gi)?.pop() ?? domain;
696
+ const key = `${name}:${domain}`;
697
+ const cached = $4ad2c96545f22a92$var$cache.get(key);
698
+ if (cached) return cached;
699
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log("Querying cookies:", name, domain);
700
+ const results = await Promise.all(this.#strategies.map(async (strategy)=>{
701
+ // @ts-ignore
702
+ return strategy.queryCookies(name, domain).then((cookies)=>{
703
+ return cookies.map((cookie)=>{
704
+ return (0, $6B8p2$merge)(cookie, {
705
+ meta: {
706
+ browser: strategy.browserName
707
+ }
708
+ });
709
+ });
710
+ }).catch((e)=>{
711
+ console.log((0, $6B8p2$red)(`Error querying ${strategy.browserName} cookies`), e);
712
+ return [];
713
+ });
714
+ }));
715
+ const flat = results.flat();
716
+ $4ad2c96545f22a92$var$cache.set(`${name}:${domain}`, flat);
717
+ return flat;
718
+ }
719
+ }
720
+
721
+
722
+
723
+
724
+ function $382bb98116581c98$export$2e2bcd8739ae039(token) {
725
+ try {
726
+ const result = (0, $6B8p2$jsonwebtoken).decode(token, {
727
+ complete: true
728
+ });
729
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose && result) console.log(result);
730
+ const payload = result?.payload;
731
+ if (payload) {
732
+ const exp = payload.exp;
733
+ if (exp) {
734
+ const now = new Date().getTime() / 1000;
735
+ if (now > exp) return false;
736
+ }
737
+ }
738
+ return true;
739
+ } catch (err) {
740
+ return false;
741
+ }
742
+ }
743
+
744
+
745
+ async function $894f0031f9c297b7$export$e6d428437cf2cacb({ name: name , domain: domain }, strategy = new (0, $4ad2c96545f22a92$export$2e2bcd8739ae039)()) {
746
+ const results = await strategy.queryCookies(name, domain);
747
+ const allCookies = (0, $6B8p2$uniqBy)(results, JSON.stringify);
748
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b)["require-jwt"]) {
749
+ const jwtCookies = [];
750
+ for (const result of allCookies){
751
+ const value = result.value;
752
+ if ((0, $382bb98116581c98$export$2e2bcd8739ae039)(value)) jwtCookies.push(result);
753
+ }
754
+ return (0, $105ddf345d82788c$export$c348dfaf65040d9b)["single"] ? [
755
+ jwtCookies[0]
756
+ ] : jwtCookies;
757
+ } else return (0, $105ddf345d82788c$export$c348dfaf65040d9b)["single"] ? [
758
+ allCookies[0]
759
+ ] : allCookies;
760
+ }
761
+
762
+
763
+
764
+ async function $ee90d6e46afee441$export$4be65e66cfa2648a(params) {
765
+ const cookies = await (0, $894f0031f9c297b7$export$e6d428437cf2cacb)(params, new (0, $4ad2c96545f22a92$export$2e2bcd8739ae039)());
766
+ if (Array.isArray(cookies) && cookies.length > 0) return cookies.find((cookie)=>cookie != null);
767
+ else throw new Error("Cookie not found");
768
+ }
769
+
770
+
771
+
772
+
773
+
774
+ async function $acf32070bfa1e556$export$e5637297e9eb6f2e(params) {
775
+ const cookies = await (0, $894f0031f9c297b7$export$e6d428437cf2cacb)(params, new (0, $245d30db16a07938$export$2e2bcd8739ae039)());
776
+ if (cookies.length == 0) throw new Error("Cookie not found");
777
+ return cookies.find((0, $954720edc83b2219$export$963c8d651337e4a4));
778
+ }
779
+
780
+
781
+
782
+
783
+ async function $d3544dba87d45eaf$export$c44c5fdef43f0941(params) {
784
+ const cookies = await (0, $894f0031f9c297b7$export$e6d428437cf2cacb)(params, new (0, $ded29c0fa27637d3$export$2e2bcd8739ae039)());
785
+ if (Array.isArray(cookies) && cookies.length > 0) return cookies.find((cookie)=>cookie != null);
786
+ else throw new Error("Cookie not found");
787
+ }
788
+
789
+
790
+
791
+
792
+ function $c874cfe31b9a966d$export$9abc0a414b0d8b8f(results) {
793
+ // sort by name and expiry descending
794
+ // takes the latest expiry for each name
795
+ const orderedResults = (0, $6B8p2$orderBy)(results, [
796
+ "name",
797
+ "expiry"
798
+ ], [
799
+ "asc",
800
+ "desc"
801
+ ]);
802
+ return (0, $6B8p2$uniqBy)(orderedResults, (r)=>r.name).map((r)=>r.name + "=" + r.value).join("; ");
803
+ }
804
+
805
+
806
+
807
+
808
+ async function $0452c1feb4ebc7da$export$a65902517ae87ba8(cookieSpec) {
809
+ const cookies = [];
810
+ if (Array.isArray(cookieSpec)) {
811
+ const results = await Promise.all(cookieSpec.map((cs)=>{
812
+ return (0, $894f0031f9c297b7$export$e6d428437cf2cacb)(cs);
813
+ }));
814
+ for (const exportedCookie of results.flat())cookies.push(exportedCookie);
815
+ } else {
816
+ const singleQuery = await (0, $894f0031f9c297b7$export$e6d428437cf2cacb)(cookieSpec);
817
+ cookies.push(...singleQuery);
818
+ }
819
+ return (0, $6B8p2$uniqBy)(cookies, JSON.stringify);
820
+ }
821
+
822
+
823
+ async function $4f706a2b35459230$export$4d2e9997e2c23417(cookieSpec) {
824
+ const cookies = await (0, $0452c1feb4ebc7da$export$a65902517ae87ba8)(cookieSpec);
825
+ if (cookies.length == 0) throw new Error("Cookie not found");
826
+ const groupedByFile = (0, $6B8p2$groupBy)(cookies, (r)=>r.meta?.file);
827
+ return Object.keys(groupedByFile).map((file)=>{
828
+ const results = groupedByFile[file];
829
+ return (0, $c874cfe31b9a966d$export$9abc0a414b0d8b8f)(results);
830
+ });
831
+ }
832
+
833
+
834
+
835
+
836
+ async function $dc47e52699aadc0c$export$cfc0723de5712e57(cookieSpec) {
837
+ const cookies = await (0, $0452c1feb4ebc7da$export$a65902517ae87ba8)(cookieSpec);
838
+ if (cookies.length > 0) return (0, $c874cfe31b9a966d$export$9abc0a414b0d8b8f)(cookies);
839
+ return "";
840
+ }
841
+
842
+
843
+
844
+
845
+
846
+
847
+
848
+
849
+
850
+
851
+
852
+ function $2fa260a31891b0ed$export$f266452a5050185d(url) {
853
+ const url1 = typeof url == "string" ? new URL(url) : url;
854
+ const cookieSpecs = [];
855
+ const splits = url1.hostname.split(".");
856
+ const tld = splits.slice(-2).join(".");
857
+ const cookieSpec = {
858
+ name: "%",
859
+ domain: "%." + tld
860
+ };
861
+ cookieSpecs.push(cookieSpec);
862
+ const cookieSpec1 = {
863
+ name: "%",
864
+ domain: url1.hostname
865
+ };
866
+ cookieSpecs.push(cookieSpec1);
867
+ // const isWww = splits.slice(-3)[0] === "www";
868
+ const cookieSpec2 = {
869
+ name: "%",
870
+ domain: tld
871
+ };
872
+ cookieSpecs.push(cookieSpec2);
873
+ return (0, $6B8p2$uniqBy)(cookieSpecs, JSON.stringify);
874
+ }
875
+
876
+
877
+ const $a1c21070f1cf1867$var$userAgent = new (0, $6B8p2$useragents)().toString();
878
+ async function $a1c21070f1cf1867$export$b24a545a0b39f491(url, options = {}, fetch = (0, $6B8p2$fetch), originalRequest) {
879
+ const originalRequest1 = originalRequest || {
880
+ url: url,
881
+ options: options
882
+ };
883
+ const headers = {
884
+ "User-Agent": $a1c21070f1cf1867$var$userAgent
885
+ };
886
+ const defaultOptions = {
887
+ headers: headers,
888
+ redirect: "manual"
889
+ };
890
+ const url2 = `${url}`;
891
+ const url1 = new URL(url2);
892
+ const cookieSpecs = (0, $2fa260a31891b0ed$export$f266452a5050185d)(url1);
893
+ headers["Cookie"] = await (0, $dc47e52699aadc0c$export$cfc0723de5712e57)(cookieSpecs).catch(()=>"");
894
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b)["dump-request-headers"]) {
895
+ console.log((0, $6B8p2$redBright)("Request URL:"), url1.href);
896
+ console.log((0, $6B8p2$blue)("Request headers:"), headers);
897
+ }
898
+ const newOptions1 = (0, $6B8p2$merge)(defaultOptions, {
899
+ headers: headers
900
+ }, options);
901
+ try {
902
+ const res = await fetch(url1, newOptions1);
903
+ const headers1 = [];
904
+ res.headers.forEach((value, key)=>{
905
+ headers1.push([
906
+ key,
907
+ value
908
+ ]);
909
+ });
910
+ for (const [key, value] of headers1)if (key === "set-cookie") {
911
+ const cookieJar1 = await (0, $7e7374355a590069$export$eced8873dedbb4c0);
912
+ await cookieJar1.setCookie(value, url2);
913
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log((0, $6B8p2$blue)(`Set-Cookie:`), (0, $6B8p2$yellow)(value), (0, $6B8p2$yellow)(url2));
914
+ }
915
+ const newUrl = res.headers.get("location") ?? res.url;
916
+ // const sameHost = new URL(newUrl).host === url1.host;
917
+ if (res.status == 301 || res.status == 302) // follow the redirect
918
+ {
919
+ if (newUrl && newUrl !== url2) {
920
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose || (0, $105ddf345d82788c$export$c348dfaf65040d9b)["dump-response-headers"]) console.log((0, $6B8p2$blue)(`Redirected to `), (0, $6B8p2$yellow)(newUrl));
921
+ return $a1c21070f1cf1867$export$b24a545a0b39f491(//
922
+ newUrl, newOptions1, fetch, originalRequest1);
923
+ }
924
+ }
925
+ if (res.status == 303 && newUrl && newUrl !== url2) {
926
+ // follow the redirect with GET
927
+ let newOptions2 = {};
928
+ switch(newOptions1.method){
929
+ case "POST":
930
+ case "PUT":
931
+ case "DELETE":
932
+ (0, $6B8p2$merge)(newOptions2, newOptions1, {
933
+ method: "GET"
934
+ });
935
+ newOptions2.body = undefined; // TODO: is this needed?
936
+ break;
937
+ case "HEAD":
938
+ case "GET":
939
+ (0, $6B8p2$merge)(newOptions2, newOptions1);
940
+ newOptions2.body = undefined; // TODO: is this needed?
941
+ break;
942
+ default:
943
+ (0, $6B8p2$merge)(newOptions2, newOptions1);
944
+ break;
945
+ }
946
+ if ((0, $105ddf345d82788c$export$c348dfaf65040d9b).verbose) console.log((0, $6B8p2$blue)(`Redirected to `), (0, $6B8p2$yellow)(newUrl));
947
+ return $a1c21070f1cf1867$export$b24a545a0b39f491(//
948
+ newUrl, newOptions2, fetch, originalRequest1);
949
+ }
950
+ const arrayBuffer1 = res.arrayBuffer();
951
+ async function arrayBuffer() {
952
+ return arrayBuffer1;
953
+ }
954
+ async function buffer() {
955
+ return arrayBuffer().then(Buffer.from);
956
+ }
957
+ async function text() {
958
+ return buffer().then((buffer)=>buffer.toString("utf8"));
959
+ }
960
+ async function json() {
961
+ return text().then((text)=>(0, $6B8p2$destr)(text));
962
+ }
963
+ async function formData() {
964
+ const urlSearchParams = await text().then((text)=>new URLSearchParams(text));
965
+ const formData = new FormData();
966
+ for (const [key, value] of urlSearchParams.entries())formData.append(key, value);
967
+ return formData;
968
+ }
969
+ const res1 = res;
970
+ const source2 = {
971
+ arrayBuffer: arrayBuffer,
972
+ text: text,
973
+ json: json,
974
+ buffer: buffer,
975
+ formData: formData
976
+ };
977
+ return (0, $6B8p2$merge)(res1, source2);
978
+ } catch (e) {
979
+ throw e;
980
+ }
981
+ }
982
+
983
+
984
+ var $a37c3808491b8fab$exports = {};
985
+
986
+
987
+ var $801bca016ea3ef3e$exports = {};
988
+
989
+
990
+ var $e3cac67a6cf22070$exports = {};
991
+
992
+
993
+
994
+
995
+ export {$ee90d6e46afee441$export$4be65e66cfa2648a as getCookie, $acf32070bfa1e556$export$e5637297e9eb6f2e as getChromeCookie, $d3544dba87d45eaf$export$c44c5fdef43f0941 as getFirefoxCookie, $dc47e52699aadc0c$export$cfc0723de5712e57 as getMergedRenderedCookies, $4f706a2b35459230$export$4d2e9997e2c23417 as getGroupedRenderedCookies, $a1c21070f1cf1867$export$b24a545a0b39f491 as fetchWithCookies};
996
+ //# sourceMappingURL=module.js.map