-
Notifications
You must be signed in to change notification settings - Fork 938
Expand file tree
/
Copy pathicloud-utils.js
More file actions
192 lines (170 loc) · 6.54 KB
/
icloud-utils.js
File metadata and controls
192 lines (170 loc) · 6.54 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
(function icloudUtilsModule(root, factory) {
if (typeof module !== 'undefined' && module.exports) {
module.exports = factory();
return;
}
root.IcloudUtils = factory();
})(typeof self !== 'undefined' ? self : globalThis, function createIcloudUtils() {
function normalizeIcloudHost(rawHost) {
let host = String(rawHost || '').trim().toLowerCase();
if (!host) return '';
try {
if (host.includes('://')) {
host = new URL(host).hostname.toLowerCase();
}
} catch {}
host = host.split('/')[0].split('?')[0].split('#')[0];
host = host.replace(/:\d+$/, '').replace(/\.$/, '');
if (host === 'icloud.com' || host.endsWith('.icloud.com')) return 'icloud.com';
if (host === 'icloud.com.cn' || host.endsWith('.icloud.com.cn')) return 'icloud.com.cn';
return '';
}
function getConfiguredIcloudHostPreference(stateOrValue = '') {
const preference = typeof stateOrValue === 'object'
? String(stateOrValue?.icloudHostPreference || '').trim().toLowerCase()
: String(stateOrValue || '').trim().toLowerCase();
if (!preference || preference === 'auto') return '';
return normalizeIcloudHost(preference);
}
function getIcloudLoginUrlForHost(host) {
const normalizedHost = normalizeIcloudHost(host);
if (normalizedHost === 'icloud.com') return 'https://www.icloud.com/';
if (normalizedHost === 'icloud.com.cn') return 'https://www.icloud.com.cn/';
return '';
}
function getIcloudMailUrlForHost(host) {
const normalizedHost = normalizeIcloudHost(host);
if (normalizedHost === 'icloud.com') return 'https://www.icloud.com/mail/';
if (normalizedHost === 'icloud.com.cn') return 'https://www.icloud.com.cn/mail/';
return '';
}
function getIcloudSetupUrlForHost(host) {
const normalizedHost = normalizeIcloudHost(host);
if (normalizedHost === 'icloud.com') return 'https://setup.icloud.com/setup/ws/1';
if (normalizedHost === 'icloud.com.cn') return 'https://setup.icloud.com.cn/setup/ws/1';
return '';
}
function getIcloudHostHintFromMessage(message) {
const lower = String(message || '').toLowerCase();
if (lower.includes('setup.icloud.com.cn') || lower.includes('www.icloud.com.cn') || lower.includes('icloud.com.cn')) {
return 'icloud.com.cn';
}
if (lower.includes('setup.icloud.com') || lower.includes('www.icloud.com') || lower.includes('icloud.com')) {
return 'icloud.com';
}
return '';
}
function normalizeBooleanMap(rawValue = {}) {
if (!rawValue || typeof rawValue !== 'object' || Array.isArray(rawValue)) {
return {};
}
return Object.entries(rawValue).reduce((result, [key, value]) => {
const normalizedKey = String(key || '').trim().toLowerCase();
if (!normalizedKey) {
return result;
}
result[normalizedKey] = Boolean(value);
return result;
}, {});
}
function toNormalizedEmailSet(values = []) {
if (values instanceof Set) {
return new Set(Array.from(values, (item) => String(item || '').trim().toLowerCase()).filter(Boolean));
}
if (Array.isArray(values)) {
return new Set(values.map((item) => String(item || '').trim().toLowerCase()).filter(Boolean));
}
if (values && typeof values === 'object') {
const normalizedMap = normalizeBooleanMap(values);
return new Set(Object.entries(normalizedMap)
.filter(([, value]) => value)
.map(([email]) => email));
}
return new Set();
}
function findIcloudAliasArray(node, depth = 0) {
if (!node || depth > 4) return null;
if (Array.isArray(node)) {
return node.some((item) => item && typeof item === 'object') ? node : null;
}
if (typeof node !== 'object') return null;
const priorityKeys = ['hmeEmails', 'hmeEmailList', 'hmeList', 'hmes', 'aliases', 'items'];
for (const key of priorityKeys) {
if (Array.isArray(node[key])) return node[key];
}
for (const value of Object.values(node)) {
const nested = findIcloudAliasArray(value, depth + 1);
if (nested) return nested;
}
return null;
}
function normalizeIcloudAliasRecord(raw, options = {}) {
const usedEmails = toNormalizedEmailSet(options.usedEmails);
const preservedEmails = toNormalizedEmailSet(options.preservedEmails);
const anonymousId = String(raw?.anonymousId || raw?.id || '').trim();
const email = String(
raw?.hme
|| raw?.email
|| raw?.alias
|| raw?.address
|| raw?.metaData?.hme
|| ''
).trim().toLowerCase();
if (!email || !email.includes('@')) return null;
const label = String(raw?.label || raw?.metaData?.label || '').trim();
const note = String(raw?.note || raw?.metaData?.note || '').trim();
const state = String(raw?.state || raw?.status || '').trim().toLowerCase();
const createdAt = raw?.createTimestamp
|| raw?.createTime
|| raw?.createdAt
|| raw?.createdDate
|| null;
return {
anonymousId,
email,
label,
note,
state,
active: raw?.active !== false && raw?.isActive !== false && state !== 'inactive' && state !== 'deleted',
used: usedEmails.has(email),
preserved: preservedEmails.has(email),
createdAt,
};
}
function normalizeIcloudAliasList(response, options = {}) {
const aliases = findIcloudAliasArray(response);
if (!aliases) return [];
return aliases
.map((alias) => normalizeIcloudAliasRecord(alias, options))
.filter(Boolean)
.sort((left, right) => {
if (left.active !== right.active) return left.active ? -1 : 1;
if (left.used !== right.used) return left.used ? 1 : -1;
return String(left.email).localeCompare(String(right.email));
});
}
function pickReusableIcloudAlias(aliases = []) {
return (Array.isArray(aliases) ? aliases : []).find((alias) => alias?.active && !alias?.used) || null;
}
function findIcloudAliasByEmail(aliases = [], email = '') {
const normalizedEmail = String(email || '').trim().toLowerCase();
if (!normalizedEmail) return null;
return (Array.isArray(aliases) ? aliases : [])
.find((alias) => String(alias?.email || '').trim().toLowerCase() === normalizedEmail) || null;
}
return {
findIcloudAliasArray,
findIcloudAliasByEmail,
getConfiguredIcloudHostPreference,
getIcloudHostHintFromMessage,
getIcloudLoginUrlForHost,
getIcloudMailUrlForHost,
getIcloudSetupUrlForHost,
normalizeBooleanMap,
normalizeIcloudAliasList,
normalizeIcloudAliasRecord,
normalizeIcloudHost,
pickReusableIcloudAlias,
toNormalizedEmailSet,
};
});