This repository was archived by the owner on Jul 30, 2023. It is now read-only.
forked from nativescript-community/https
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcookie.ts
More file actions
94 lines (75 loc) · 2.73 KB
/
cookie.ts
File metadata and controls
94 lines (75 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Headers } from 'tns-core-modules/http';
import { HttpsResponse } from './https.common';
import { isAndroid } from 'tns-core-modules/platform';
import * as AppSettings from 'tns-core-modules/application-settings';
const MobileStorageCookieStore = require('tough-cookie-mobile-storage-store');
import { CookieJar } from 'tough-cookie-no-native';
const STORE_KEY = 'NS_COOKIE_STORE';
class NSStorageWrapper {
getItem = (key: string) => AppSettings.getString(key);
setItem = (key: string, value: string) => AppSettings.setString(key, value);
}
const store = new MobileStorageCookieStore(new NSStorageWrapper(), STORE_KEY);
export const cookieJar = new CookieJar(store);
export function resolveCookieStringAndroid(headers: okhttp3.Headers): string[] {
const searchReg = /^set-cookie$/i;
const length: number = headers.size();
let result: string[] = [];
for (let i = 0; i < length; i++) {
const key = headers.name(i);
if (key.match(searchReg)) {
const value = headers.value(i);
result = [...result, value];
}
}
return result;
}
export function resolveCookieStringIOS(headers: NSDictionary<any, any>): string[] {
const searchReg = /^set-cookie$/i;
let result: string[] = [];
headers.enumerateKeysAndObjectsUsingBlock((key, value) => {
if (key.match(searchReg)) {
result = [...result, value];
}
});
return result;
}
export function handleCookie(url: string, headers: okhttp3.Headers | NSDictionary<any, any>): void {
const cookies = isAndroid
? resolveCookieStringAndroid(<okhttp3.Headers>headers)
: resolveCookieStringIOS(<NSDictionary<any, any>>headers);
console.log('received cookie');
try {
console.dir(JSON.stringify(cookies));
} catch (error) {
console.log(error.message);
}
return (
cookies &&
cookies.length &&
cookies.forEach(cookie => cookieJar.setCookieSync(cookie, url, { ignoreError: true }))
);
}
function resolvePassedCookieString(headers: Headers = {}): string {
const searchReg = /^cookie$/i;
const key = Object.keys(headers).find(key => Boolean(key.match(searchReg)));
const cookie = headers[key];
return Array.isArray(cookie) ? cookie.reduce((s1, s2) => `${s1}; ${s2}`, '') : cookie;
}
export function mergeRequestHeaders(url: string, headers = {}): { [key: string]: string } {
const cookies = resolvePassedCookieString(headers);
const existingCookies = cookieJar.getCookieStringSync(url);
console.log('existing cookie');
try {
console.dir(JSON.stringify(existingCookies));
} catch (error) {
console.log(error.message);
}
const mergedCookies = [cookies, existingCookies]
.filter(str => str)
.reduce((s1, s2) => `${s1}; ${s2}`, '');
return mergedCookies ? { ...headers, Cookie: mergedCookies } : headers;
}
export function clearCookies(): void {
return AppSettings.remove(STORE_KEY);
}