@react-native-ohos/cookies 6.3.0-rc.2 → 6.3.1-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.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Use these variables when you tailor your ArkTS code. They must be of the const type.
|
|
3
3
|
*/
|
|
4
|
-
export const HAR_VERSION = '6.3.
|
|
4
|
+
export const HAR_VERSION = '6.3.1-bate.1';
|
|
5
5
|
export const BUILD_MODE_NAME = 'debug';
|
|
6
6
|
export const DEBUG = true;
|
|
7
7
|
export const TARGET_NAME = 'default';
|
|
@@ -9,9 +9,9 @@ export const TARGET_NAME = 'default';
|
|
|
9
9
|
/**
|
|
10
10
|
* BuildProfile Class is used only for compatibility purposes.
|
|
11
11
|
*/
|
|
12
|
-
export default class BuildProfile {
|
|
12
|
+
export default class BuildProfile {
|
|
13
13
|
static readonly HAR_VERSION = HAR_VERSION;
|
|
14
14
|
static readonly BUILD_MODE_NAME = BUILD_MODE_NAME;
|
|
15
15
|
static readonly DEBUG = DEBUG;
|
|
16
16
|
static readonly TARGET_NAME = TARGET_NAME;
|
|
17
|
-
}
|
|
17
|
+
}
|
|
@@ -23,11 +23,7 @@ export interface Cookies {
|
|
|
23
23
|
export class CookiesModule extends TurboModule {
|
|
24
24
|
clearAll(useWebKit?: boolean): Promise<boolean> {
|
|
25
25
|
try {
|
|
26
|
-
|
|
27
|
-
web_webview.WebCookieManager.clearAllCookies();
|
|
28
|
-
} else {
|
|
29
|
-
web_webview.WebCookieManager.clearAllCookiesSync();
|
|
30
|
-
}
|
|
26
|
+
web_webview.WebCookieManager.clearAllCookiesSync();
|
|
31
27
|
return new Promise((resolve) => {
|
|
32
28
|
resolve(true);
|
|
33
29
|
});
|
|
@@ -40,125 +36,151 @@ export class CookiesModule extends TurboModule {
|
|
|
40
36
|
|
|
41
37
|
async get(url: string, useWebKit?: boolean): Promise<Cookies> {
|
|
42
38
|
if ((url === '') || (url === undefined) || (url === null)) {
|
|
43
|
-
return new
|
|
44
|
-
let cookies: Cookies = { 'cookies': {name: 'error', value: 'url有误'} };
|
|
45
|
-
resolve(cookies);
|
|
46
|
-
});
|
|
39
|
+
return Promise.reject(new Error('url is invalid'));
|
|
47
40
|
}
|
|
48
41
|
try {
|
|
49
42
|
let result: string = '';
|
|
50
43
|
if (useWebKit) {
|
|
51
44
|
result = await web_webview.WebCookieManager.fetchCookie(url);
|
|
52
45
|
} else {
|
|
53
|
-
result =
|
|
46
|
+
result = web_webview.WebCookieManager.fetchCookieSync(url);
|
|
54
47
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
resolve(cookies);
|
|
48
|
+
if (result === '') {
|
|
49
|
+
console.warn('未查询到cookie');
|
|
50
|
+
return {}
|
|
51
|
+
}
|
|
52
|
+
const cookies: Cookies = {};
|
|
53
|
+
const pairs = result.split(';');
|
|
54
|
+
for (let i = 0; i < pairs.length; i++) {
|
|
55
|
+
const trimmed = pairs[i].trim();
|
|
56
|
+
const eqIndex = trimmed.indexOf('=');
|
|
57
|
+
if (eqIndex <= 0) {
|
|
58
|
+
continue;
|
|
68
59
|
}
|
|
69
|
-
|
|
60
|
+
const name = trimmed.substring(0, eqIndex).trim();
|
|
61
|
+
const value = trimmed.substring(eqIndex + 1);
|
|
62
|
+
if (name.length === 0) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
cookies[name] = { name: name, value: value } as Cookie;
|
|
66
|
+
}
|
|
67
|
+
return cookies;
|
|
70
68
|
} catch (error) {
|
|
71
|
-
return
|
|
72
|
-
let cookies: Cookies = { 'cookie': {name: 'error', value: JSON.stringify(error)} }
|
|
73
|
-
resolve(cookies);
|
|
74
|
-
});
|
|
69
|
+
return Promise.reject(error);
|
|
75
70
|
}
|
|
76
71
|
}
|
|
77
72
|
|
|
78
|
-
isEmpty(value:string) {
|
|
73
|
+
isEmpty(value: string | undefined): boolean {
|
|
79
74
|
return value == null || value.length === 0;
|
|
80
75
|
}
|
|
81
76
|
|
|
82
|
-
|
|
83
|
-
|
|
77
|
+
/**
|
|
78
|
+
* Build a Set-Cookie value string per RFC 6265 (aligned with Android toRFC6265string):
|
|
79
|
+
* name=value; expires=...; domain=...; path=...; secure; httponly
|
|
80
|
+
*/
|
|
81
|
+
buildRFC6265CookieString(url: string, cookie: Cookie): string {
|
|
82
|
+
const urlMatch = url.match(/^https?:\/\/([^/?#]+)(?:[/?#]|$)/i);
|
|
83
|
+
if (!urlMatch) {
|
|
84
|
+
throw new Error('Invalid URL: It may be missing a protocol (ex. http:// or https://).');
|
|
85
|
+
}
|
|
86
|
+
const topLevelDomain = urlMatch[1];
|
|
87
|
+
if (this.isEmpty(topLevelDomain)) {
|
|
88
|
+
throw new Error('Invalid URL: It may be missing a protocol (ex. http:// or https://).');
|
|
89
|
+
}
|
|
84
90
|
|
|
85
|
-
|
|
86
|
-
Object.keys(cookie).forEach((key)=>{
|
|
87
|
-
if(key !== "name" && key !== "value" && key !== "domain"){
|
|
88
|
-
cookieBuilder+= `; ${key}=${cookie[key]}`
|
|
89
|
-
}
|
|
90
|
-
})
|
|
91
|
+
let cookieBuilder = `${cookie.name}=${cookie.value}`;
|
|
91
92
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
93
|
+
if (!this.isEmpty(cookie.expires)) {
|
|
94
|
+
cookieBuilder += `; expires=${cookie.expires}`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
let domain: string;
|
|
98
|
+
if (!this.isEmpty(cookie.domain)) {
|
|
99
|
+
domain = cookie.domain!;
|
|
100
|
+
if (domain.startsWith('.')) {
|
|
101
|
+
domain = domain.substring(1);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const isDomainMatch = topLevelDomain === domain || topLevelDomain.endsWith('.' + domain);
|
|
105
|
+
if (!isDomainMatch) {
|
|
106
|
+
throw new Error(`Cookie URL host ${topLevelDomain} and domain ${domain} mismatched. The cookie won't set correctly.`);
|
|
104
107
|
}
|
|
108
|
+
} else {
|
|
109
|
+
domain = topLevelDomain;
|
|
110
|
+
}
|
|
111
|
+
cookieBuilder += `; domain=${domain}`;
|
|
112
|
+
|
|
113
|
+
if (!this.isEmpty(cookie.path)) {
|
|
114
|
+
cookieBuilder += `; path=${cookie.path}`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (cookie.secure === true) {
|
|
118
|
+
cookieBuilder += '; secure';
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (cookie.httpOnly === true) {
|
|
122
|
+
cookieBuilder += '; httponly';
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return cookieBuilder;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async set(url: string, cookie: Cookie, useWebKit?: boolean): Promise<boolean> {
|
|
129
|
+
try {
|
|
130
|
+
const cookieString = this.buildRFC6265CookieString(url, cookie);
|
|
105
131
|
if (useWebKit) {
|
|
106
|
-
await web_webview.WebCookieManager.configCookie(url,
|
|
132
|
+
await web_webview.WebCookieManager.configCookie(url, cookieString);
|
|
107
133
|
} else {
|
|
108
|
-
web_webview.WebCookieManager.configCookieSync(url,
|
|
134
|
+
web_webview.WebCookieManager.configCookieSync(url, cookieString, false);
|
|
109
135
|
}
|
|
110
|
-
return
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return new Promise((resolve) => {
|
|
115
|
-
resolve(false);
|
|
116
|
-
});
|
|
136
|
+
return Promise.resolve(true);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
console.warn('set() failed:', error);
|
|
139
|
+
return Promise.resolve(false);
|
|
117
140
|
}
|
|
118
141
|
}
|
|
119
142
|
|
|
120
|
-
async clearByName(url: string, name: string, useWebKit?: boolean): Promise<boolean>{
|
|
121
|
-
if ((url
|
|
122
|
-
return
|
|
123
|
-
resolve(false);
|
|
124
|
-
});
|
|
143
|
+
async clearByName(url: string, name: string, useWebKit?: boolean): Promise<boolean> {
|
|
144
|
+
if (this.isEmpty(url) || this.isEmpty(name)) {
|
|
145
|
+
return Promise.resolve(false);
|
|
125
146
|
}
|
|
126
147
|
try {
|
|
127
|
-
let
|
|
128
|
-
let cookieStr: string = '';
|
|
129
|
-
let cookies: Array<string> = [];
|
|
148
|
+
let cookieStr = '';
|
|
130
149
|
if (useWebKit) {
|
|
131
|
-
|
|
150
|
+
cookieStr = await web_webview.WebCookieManager.fetchCookie(url);
|
|
132
151
|
} else {
|
|
133
152
|
cookieStr = web_webview.WebCookieManager.fetchCookieSync(url);
|
|
134
153
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
154
|
+
|
|
155
|
+
const cookieExist = cookieStr.split(';').some((pair) => {
|
|
156
|
+
const trimmed = pair.trim();
|
|
157
|
+
const eqIndex = trimmed.indexOf('=');
|
|
158
|
+
if (eqIndex <= 0) {
|
|
159
|
+
return false;
|
|
140
160
|
}
|
|
141
|
-
|
|
161
|
+
return trimmed.substring(0, eqIndex).trim() === name;
|
|
162
|
+
});
|
|
163
|
+
|
|
142
164
|
if (!cookieExist) {
|
|
143
|
-
return
|
|
144
|
-
resolve(true);
|
|
145
|
-
});
|
|
165
|
+
return Promise.resolve(true);
|
|
146
166
|
}
|
|
167
|
+
|
|
168
|
+
// Delete by overwriting with an expired cookie (RFC 6265). Omit path so the platform derives it from the URL.
|
|
169
|
+
const expiredCookieString = this.buildRFC6265CookieString(url, {
|
|
170
|
+
name: name,
|
|
171
|
+
value: '',
|
|
172
|
+
expires: 'Thu, 01 Jan 1970 00:00:00 GMT',
|
|
173
|
+
});
|
|
174
|
+
|
|
147
175
|
if (useWebKit) {
|
|
148
|
-
await web_webview.WebCookieManager.
|
|
176
|
+
await web_webview.WebCookieManager.configCookie(url, expiredCookieString);
|
|
149
177
|
} else {
|
|
150
|
-
web_webview.WebCookieManager.
|
|
178
|
+
web_webview.WebCookieManager.configCookieSync(url, expiredCookieString, false);
|
|
151
179
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
return
|
|
156
|
-
resolve(true);
|
|
157
|
-
});
|
|
158
|
-
} catch(error) {
|
|
159
|
-
return new Promise((resolve) => {
|
|
160
|
-
resolve(false);
|
|
161
|
-
});
|
|
180
|
+
return Promise.resolve(true);
|
|
181
|
+
} catch (error) {
|
|
182
|
+
console.warn('clearByName() failed:', error);
|
|
183
|
+
return Promise.resolve(false);
|
|
162
184
|
}
|
|
163
185
|
}
|
|
164
186
|
|
package/harmony/rn_cookies.har
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-native-ohos/cookies",
|
|
3
|
-
"version": "6.3.
|
|
3
|
+
"version": "6.3.1-beta.1",
|
|
4
4
|
"description": "Cookie Manager for React Native",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -52,4 +52,4 @@
|
|
|
52
52
|
"harmony": {
|
|
53
53
|
"alias": "@react-native-cookies/cookies"
|
|
54
54
|
}
|
|
55
|
-
}
|
|
55
|
+
}
|