@xkwdstore/agent 2.0.5 → 2.0.7
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/README.md +4 -6
- package/package.json +1 -1
- package/src/actions/comment.js +153 -138
- package/src/actions/confirmation.js +129 -0
- package/src/actions/follow.js +179 -83
- package/src/actions/tweet.js +107 -91
- package/src/cli.js +6 -1
- package/src/enrollmentClient.js +43 -2
- package/src/sessionHandler.js +304 -581
package/src/actions/follow.js
CHANGED
|
@@ -1,83 +1,179 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Follow action — follow a user on X.
|
|
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
|
-
if (
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Follow action — follow a user on X.
|
|
5
|
+
*
|
|
6
|
+
* Hardened 2.0.7:
|
|
7
|
+
* - the clicked control is PROVEN to belong to the target profile (bound by
|
|
8
|
+
* the target handle in the aria-label or a handle-based testid). A
|
|
9
|
+
* recommendation/suggestion Follow button can never satisfy the selector.
|
|
10
|
+
* - the numeric X user-id testid form (`<digits>-follow` / `<digits>-unfollow`)
|
|
11
|
+
* that current X renders is supported via the target-bound aria/state.
|
|
12
|
+
* - pre-action state is classified: ALREADY_FOLLOWING returns success WITHOUT
|
|
13
|
+
* clicking; FOLLOWABLE_TARGET is clicked EXACTLY ONCE; an ambiguous control
|
|
14
|
+
* fails safely BEFORE any mutation.
|
|
15
|
+
* - post-click confirmation is READ-ONLY and reuses FOLLOWING_LABELS (not
|
|
16
|
+
* English-only). Bounded, validated window (default 15 s).
|
|
17
|
+
* - a successful click that cannot be confirmed is UNKNOWN_OUTCOME, never a
|
|
18
|
+
* false success and never an auto-retry; the click is never repeated.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const { sleep, humanDelay, randDelay, naturalNavigate, humanPreAction, humanClick, detectStage } = require('./helpers');
|
|
22
|
+
const { boundedIntEnv } = require('./confirmation');
|
|
23
|
+
|
|
24
|
+
// Follow button labels in multiple languages
|
|
25
|
+
const FOLLOW_LABELS = [
|
|
26
|
+
'follow', 'seguir', 'suivre', 'folgen', 'تابع', 'フォロー',
|
|
27
|
+
'segui', 'volgen', 'śledź', 'seguir', '关注', '팔로우',
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
const FOLLOWING_LABELS = [
|
|
31
|
+
'following', 'siguiendo', 'abonné', 'folgt', 'يتابعك', 'متابعة', 'フォロー中',
|
|
32
|
+
'following', 'volgend', 'obserwujesz', 'seguindo', '已关注', '팔로잉',
|
|
33
|
+
'unfollow', 'dejar de seguir', 'ne plus suivre', 'entfolgen', 'إلغاء المتابعة',
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
// Bounded and comfortably below runAction's server budget (180000 ms).
|
|
37
|
+
const FOLLOW_CONFIRM_TIMEOUT_MS = boundedIntEnv('LOCAL_AGENT_FOLLOW_CONFIRM_TIMEOUT_MS', 15000, 2000, 60000);
|
|
38
|
+
const FOLLOW_CONFIRM_POLL_MS = boundedIntEnv('LOCAL_AGENT_FOLLOW_CONFIRM_POLL_MS', 400, 50, 2000);
|
|
39
|
+
|
|
40
|
+
function norm(value) { return String(value == null ? '' : value).toLowerCase().replace(/\s+/g, ' ').trim(); }
|
|
41
|
+
function escapeRe(s) { return String(s).replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }
|
|
42
|
+
|
|
43
|
+
function mentionsHandle(aria, username) { return norm(aria).includes('@' + norm(username)); }
|
|
44
|
+
function testidMatchesHandle(testid, username) {
|
|
45
|
+
return new RegExp(`^${escapeRe(String(username))}-(follow|unfollow)$`, 'i').test(String(testid || ''));
|
|
46
|
+
}
|
|
47
|
+
function isUnfollowTestid(testid) { return /-unfollow$/i.test(String(testid || '')); }
|
|
48
|
+
function isFollowTestid(testid) { return /-follow$/i.test(String(testid || '')); }
|
|
49
|
+
function textIndicatesFollowing(text) { const n = norm(text); return FOLLOWING_LABELS.some((l) => n.includes(norm(l))); }
|
|
50
|
+
function textIndicatesFollow(text) { const n = norm(text); return FOLLOW_LABELS.some((l) => n.includes(norm(l))); }
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* PURE: pick the control that is PROVEN to belong to the target profile.
|
|
54
|
+
* Target-bound = the aria-label mentions the target handle, or the testid is the
|
|
55
|
+
* handle-based follow/unfollow form. Controls inside a UserCell (suggestion) are
|
|
56
|
+
* rejected outright, and a bare numeric testid is never target-bound on its own.
|
|
57
|
+
*/
|
|
58
|
+
function selectTargetControl(controls, username) {
|
|
59
|
+
const list = Array.isArray(controls) ? controls : [];
|
|
60
|
+
for (const c of list) {
|
|
61
|
+
if (!c || c.visible === false || c.inUserCell) continue;
|
|
62
|
+
if (mentionsHandle(c.aria, username) || testidMatchesHandle(c.testid, username)) return c;
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** PURE: classify the target control's pre-action state. */
|
|
68
|
+
function classifyControl(control) {
|
|
69
|
+
if (!control) return 'unknown';
|
|
70
|
+
const combined = `${control.text || ''} ${control.aria || ''}`;
|
|
71
|
+
if (isUnfollowTestid(control.testid) || textIndicatesFollowing(combined)) return 'following';
|
|
72
|
+
if (isFollowTestid(control.testid)) return 'followable';
|
|
73
|
+
if (textIndicatesFollow(combined)) return 'followable';
|
|
74
|
+
return 'unknown';
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Read-only: descriptors for every button/[role=button], in DOM order. */
|
|
78
|
+
async function readActionControls(page) {
|
|
79
|
+
return await page.evaluate(() => {
|
|
80
|
+
const els = Array.from(document.querySelectorAll('button, [role="button"]'));
|
|
81
|
+
return els.map((el, i) => ({
|
|
82
|
+
i,
|
|
83
|
+
text: (el.innerText || '').trim().slice(0, 80),
|
|
84
|
+
aria: el.getAttribute('aria-label') || '',
|
|
85
|
+
testid: el.getAttribute('data-testid') || '',
|
|
86
|
+
inUserCell: !!el.closest('[data-testid="UserCell"]'),
|
|
87
|
+
visible: !!(el.offsetParent !== null || el.getClientRects().length > 0),
|
|
88
|
+
}));
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function controlLocator(page, index) {
|
|
93
|
+
return page.locator('button, [role="button"]').nth(index);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* READ-ONLY wait until the TARGET control reports a followed state.
|
|
98
|
+
* Never clicks. Bounded.
|
|
99
|
+
*/
|
|
100
|
+
async function confirmFollow(page, username, timeoutMs = FOLLOW_CONFIRM_TIMEOUT_MS) {
|
|
101
|
+
const deadline = Date.now() + timeoutMs;
|
|
102
|
+
while (Date.now() < deadline) {
|
|
103
|
+
const control = selectTargetControl(await readActionControls(page), username);
|
|
104
|
+
if (control && classifyControl(control) === 'following') return true;
|
|
105
|
+
await sleep(FOLLOW_CONFIRM_POLL_MS);
|
|
106
|
+
}
|
|
107
|
+
const control = selectTargetControl(await readActionControls(page), username);
|
|
108
|
+
return !!(control && classifyControl(control) === 'following');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function follow(page, payload) {
|
|
112
|
+
const { username } = payload;
|
|
113
|
+
if (!username) throw new Error('username required');
|
|
114
|
+
|
|
115
|
+
const cleanUsername = String(username).replace(/^@/, '').replace(/^.*x\.com\//, '');
|
|
116
|
+
await naturalNavigate(page, `https://x.com/${cleanUsername}`);
|
|
117
|
+
await humanPreAction(page);
|
|
118
|
+
|
|
119
|
+
if (page.url().includes('/login') || page.url().includes('/i/flow')) {
|
|
120
|
+
throw new Error('Not logged in — open browser and login first');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Check for suspended/not-found
|
|
124
|
+
const title = await page.title().catch(() => '');
|
|
125
|
+
const bodyText = await page.evaluate(() => document.body.innerText).catch(() => '');
|
|
126
|
+
if (/account suspended|doesn't exist|page doesn't exist/i.test(title + bodyText)) {
|
|
127
|
+
throw new Error(`Account @${cleanUsername} is suspended or not found`);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ── Pre-action: resolve the TARGET profile control and its state. ─────────
|
|
131
|
+
const controls = await readActionControls(page);
|
|
132
|
+
const control = selectTargetControl(controls, cleanUsername);
|
|
133
|
+
if (!control) {
|
|
134
|
+
// No target-bound control → nothing to click. Known pre-action failure (no
|
|
135
|
+
// mutation happened). A suggestion's Follow button can never satisfy this.
|
|
136
|
+
throw new Error(`Could not find the target follow control for @${cleanUsername}`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const state = classifyControl(control);
|
|
140
|
+
if (state === 'following') {
|
|
141
|
+
return { success: true, message: `Already following @${cleanUsername}`, status: 'already_following' };
|
|
142
|
+
}
|
|
143
|
+
if (state !== 'followable') {
|
|
144
|
+
// Ambiguous target control → fail safely BEFORE any mutation.
|
|
145
|
+
throw new Error(`Target follow control for @${cleanUsername} is in an ambiguous state`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// ── EXACTLY ONE target-bound click. ───────────────────────────────────────
|
|
149
|
+
await humanClick(page, controlLocator(page, control.i));
|
|
150
|
+
await sleep(1000);
|
|
151
|
+
|
|
152
|
+
// ── READ-ONLY confirmation. Never clicks again. ───────────────────────────
|
|
153
|
+
if (await confirmFollow(page, cleanUsername, FOLLOW_CONFIRM_TIMEOUT_MS)) {
|
|
154
|
+
return { success: true, message: `Following @${cleanUsername}` };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
success: false,
|
|
159
|
+
code: 'UNKNOWN_OUTCOME',
|
|
160
|
+
outcome: 'unknown',
|
|
161
|
+
error: `Follow click for @${cleanUsername} could not be confirmed; outcome unknown`,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
module.exports = {
|
|
166
|
+
follow,
|
|
167
|
+
_internals: {
|
|
168
|
+
norm,
|
|
169
|
+
mentionsHandle,
|
|
170
|
+
testidMatchesHandle,
|
|
171
|
+
isUnfollowTestid,
|
|
172
|
+
isFollowTestid,
|
|
173
|
+
textIndicatesFollowing,
|
|
174
|
+
textIndicatesFollow,
|
|
175
|
+
selectTargetControl,
|
|
176
|
+
classifyControl,
|
|
177
|
+
FOLLOW_CONFIRM_TIMEOUT_MS,
|
|
178
|
+
},
|
|
179
|
+
};
|
package/src/actions/tweet.js
CHANGED
|
@@ -1,91 +1,107 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Tweet action — compose and post a tweet.
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
if (
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const
|
|
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
|
-
await page.keyboard.press('Control+Enter');
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Tweet action — compose and post a tweet.
|
|
5
|
+
*
|
|
6
|
+
* NON-IDEMPOTENT. The submit mutation is dispatched AT MOST ONCE. The outcome
|
|
7
|
+
* is then established by PASSIVE, read-only confirmation bound to the exact
|
|
8
|
+
* compose surface used; if success cannot be proven the result is
|
|
9
|
+
* UNKNOWN_OUTCOME (never a second Ctrl+Enter, never a false success).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const { sleep, humanDelay, randDelay, naturalNavigate, humanPreAction, humanType, humanClick, detectStage, waitForLoggedIn, injectCookies } = require('./helpers');
|
|
13
|
+
const { captureComposeSurface, countArticlesWithText, confirmSubmission, SUBMIT_CONFIRM_TIMEOUT_MS } = require('./confirmation');
|
|
14
|
+
|
|
15
|
+
const FAILURE_TOAST = /something went wrong|try again|failed/i;
|
|
16
|
+
const COMPOSE_URL_MARKER = '/compose/post';
|
|
17
|
+
|
|
18
|
+
async function tweet(page, payload) {
|
|
19
|
+
const { text } = payload;
|
|
20
|
+
if (!text) throw new Error('Tweet text required');
|
|
21
|
+
|
|
22
|
+
// Navigate to compose page
|
|
23
|
+
await page.goto('https://x.com/compose/post', { waitUntil: 'domcontentloaded', timeout: 30000 }).catch(() => {});
|
|
24
|
+
await randDelay(2000, 4000);
|
|
25
|
+
|
|
26
|
+
// Check if logged in
|
|
27
|
+
const stage = await detectStage(page);
|
|
28
|
+
if (stage === 'login_required' || stage === 'login_username_step' || stage === 'login_password_step') {
|
|
29
|
+
throw new Error('Not logged in — open browser and login first');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Wait for the compose textarea
|
|
33
|
+
const editorSelector = '[data-testid="tweetTextarea_0"]';
|
|
34
|
+
await page.waitForSelector(editorSelector, { timeout: 15000 });
|
|
35
|
+
const editor = page.locator(editorSelector).first();
|
|
36
|
+
await editor.click();
|
|
37
|
+
await humanDelay(300, 600);
|
|
38
|
+
|
|
39
|
+
// Detect RTL (Arabic) text
|
|
40
|
+
const isRTL = /[\u0600-\u06FF\u0750-\u077F]/.test(text);
|
|
41
|
+
if (isRTL) {
|
|
42
|
+
await page.evaluate((t) => {
|
|
43
|
+
const el = document.querySelector('[data-testid="tweetTextarea_0"]');
|
|
44
|
+
if (el) { el.setAttribute('dir', 'rtl'); }
|
|
45
|
+
}, text);
|
|
46
|
+
const words = text.split(' ');
|
|
47
|
+
for (const word of words) {
|
|
48
|
+
await page.evaluate((w) => {
|
|
49
|
+
document.execCommand('insertText', false, w + ' ');
|
|
50
|
+
}, word);
|
|
51
|
+
await humanDelay(50, 120);
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
await humanType(page, text, { delayMin: 50, delayMax: 150 });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
await humanDelay(500, 1000);
|
|
58
|
+
|
|
59
|
+
// ── Bind confirmation to the EXACT compose surface used ──────────────────
|
|
60
|
+
const surface = await captureComposeSurface(page);
|
|
61
|
+
const matchesBefore = (await countArticlesWithText(page, text)) || 0;
|
|
62
|
+
|
|
63
|
+
// ── ONE submission. Exactly one of {post-button click, Ctrl+Enter}. ──────
|
|
64
|
+
const postSelectors = [
|
|
65
|
+
'[data-testid="tweetButton"]',
|
|
66
|
+
'[data-testid="tweetButtonInline"]',
|
|
67
|
+
];
|
|
68
|
+
let submitted = false;
|
|
69
|
+
for (const sel of postSelectors) {
|
|
70
|
+
const btn = page.locator(sel).first();
|
|
71
|
+
if (await btn.isVisible({ timeout: 3000 }).catch(() => false)) {
|
|
72
|
+
await humanClick(page, btn);
|
|
73
|
+
submitted = true;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (!submitted) {
|
|
78
|
+
await page.keyboard.press('Control+Enter');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ── PASSIVE confirmation. NEVER submit again. ────────────────────────────
|
|
82
|
+
const confirmed = await confirmSubmission(page, {
|
|
83
|
+
surface,
|
|
84
|
+
text,
|
|
85
|
+
matchesBefore,
|
|
86
|
+
timeoutMs: SUBMIT_CONFIRM_TIMEOUT_MS,
|
|
87
|
+
// The composer leaves /compose/post on a successful post (read-only).
|
|
88
|
+
extraProbe: async () => !page.url().includes(COMPOSE_URL_MARKER),
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
if (confirmed) return { success: true, message: 'Tweet posted' };
|
|
92
|
+
|
|
93
|
+
// Not confirmed: a clear X failure toast is a KNOWN failure (never resubmit).
|
|
94
|
+
const bodyText = await page.evaluate(() => document.body.innerText).catch(() => '');
|
|
95
|
+
if (FAILURE_TOAST.test(bodyText)) {
|
|
96
|
+
throw new Error('Tweet submit reported a failure toast');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
success: false,
|
|
101
|
+
code: 'UNKNOWN_OUTCOME',
|
|
102
|
+
outcome: 'unknown',
|
|
103
|
+
error: 'Tweet submit could not be confirmed; outcome unknown',
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
module.exports = { tweet };
|
package/src/cli.js
CHANGED
|
@@ -24,7 +24,7 @@ const crypto = require('crypto');
|
|
|
24
24
|
const os = require('os');
|
|
25
25
|
const path = require('path');
|
|
26
26
|
|
|
27
|
-
const CLI_VERSION = '2.0.
|
|
27
|
+
const CLI_VERSION = '2.0.7';
|
|
28
28
|
const PROTOCOL_VERSION = 2;
|
|
29
29
|
|
|
30
30
|
// ── Command parsing ──────────────────────────────────────────────────────────
|
|
@@ -120,6 +120,11 @@ async function start(serverOrigin, flags) {
|
|
|
120
120
|
console.error('The agent will not start enrollment until the server is compatible.');
|
|
121
121
|
process.exit(1);
|
|
122
122
|
}
|
|
123
|
+
// NOTE: the server's declared `capabilities` are INFORMATIONAL. They are
|
|
124
|
+
// never a reason to refuse a server — a server that does not advertise the
|
|
125
|
+
// profile-identity capability may still supply the identity tuple, and every
|
|
126
|
+
// currently-deployed backend omits the field. A missing capability must not
|
|
127
|
+
// block startup (see validateCompatibilityResponse).
|
|
123
128
|
// compat.ok — enforce minimum agent version
|
|
124
129
|
const minVer = compat.body.minimumAgentVersion;
|
|
125
130
|
const recVer = compat.body.recommendedAgentVersion;
|
package/src/enrollmentClient.js
CHANGED
|
@@ -299,6 +299,26 @@ function _isHtmlResponse(data, contentType) {
|
|
|
299
299
|
return body.startsWith('<!doctype html>') || body.startsWith('<html');
|
|
300
300
|
}
|
|
301
301
|
|
|
302
|
+
/**
|
|
303
|
+
* The server capability that indicates the backend can send the complete
|
|
304
|
+
* immutable profile-identity tuple inside startSession.options.
|
|
305
|
+
*
|
|
306
|
+
* THIS IS INFORMATIONAL ONLY — IT MUST NEVER GATE COMPATIBILITY.
|
|
307
|
+
* ------------------------------------------------------------
|
|
308
|
+
* An earlier revision made this a hard requirement of
|
|
309
|
+
* validateCompatibilityResponse. That was WRONG and would have been an outage:
|
|
310
|
+
* every currently-deployed server (including the live production backend) and
|
|
311
|
+
* every currently-published agent (2.0.4, 2.0.5) treat `capabilities` as an
|
|
312
|
+
* unknown/extra field, so requiring it would have REFUSED servers that in fact
|
|
313
|
+
* work correctly. Availability has to be able to report a capability that a
|
|
314
|
+
* server does not yet advertise without breaking that server.
|
|
315
|
+
*
|
|
316
|
+
* The field is therefore parsed and surfaced (see `capabilities` on the returned
|
|
317
|
+
* object) so operator tooling can DISPLAY the difference, but its absence never
|
|
318
|
+
* produces { ok: false }.
|
|
319
|
+
*/
|
|
320
|
+
const PROFILE_IDENTITY_CAPABILITY = 'profile-identity-v1';
|
|
321
|
+
|
|
302
322
|
/**
|
|
303
323
|
* Shared strict compatibility validator (Protocol-v2 contract).
|
|
304
324
|
*
|
|
@@ -311,12 +331,20 @@ function _isHtmlResponse(data, contentType) {
|
|
|
311
331
|
* - non-empty minimumAgentVersion
|
|
312
332
|
* - non-empty recommendedAgentVersion
|
|
313
333
|
*
|
|
334
|
+
* OPTIONAL (never a failure condition):
|
|
335
|
+
* - capabilities[] — parsed and returned when present, so a caller can show
|
|
336
|
+
* whether the server advertises the profile-identity contract. A missing or
|
|
337
|
+
* ill-typed value is IGNORED, not rejected: the declared capability set is
|
|
338
|
+
* metadata about the server, and treating absent metadata as an
|
|
339
|
+
* incompatibility would break servers that are perfectly usable.
|
|
340
|
+
*
|
|
314
341
|
* Never prints the HTML body or undefined values. For HTML/SPA responses it
|
|
315
342
|
* returns { ok: false, html: true } and the caller prints the exact required
|
|
316
343
|
* message.
|
|
317
344
|
*
|
|
318
345
|
* @param {object} resp — { status, body, contentType }
|
|
319
|
-
* @returns {object} { ok: boolean, html?: boolean, reason?: string, body?: object
|
|
346
|
+
* @returns {object} { ok: boolean, html?: boolean, reason?: string, body?: object,
|
|
347
|
+
* capabilities: string[], hasProfileIdentityCapability: boolean }
|
|
320
348
|
*/
|
|
321
349
|
function validateCompatibilityResponse(resp) {
|
|
322
350
|
if (!resp || typeof resp !== 'object') {
|
|
@@ -348,7 +376,19 @@ function validateCompatibilityResponse(resp) {
|
|
|
348
376
|
if (!body.recommendedAgentVersion || typeof body.recommendedAgentVersion !== 'string') {
|
|
349
377
|
return { ok: false, reason: 'missing recommendedAgentVersion' };
|
|
350
378
|
}
|
|
351
|
-
|
|
379
|
+
// OPTIONAL capabilities: read for DISPLAY only. An absent, empty, or
|
|
380
|
+
// ill-typed value must NOT fail compatibility — a server that does not
|
|
381
|
+
// advertise capabilities can still be fully usable (every deployed server
|
|
382
|
+
// today omits it). Only well-formed string entries are surfaced.
|
|
383
|
+
const capabilities = Array.isArray(body.capabilities)
|
|
384
|
+
? body.capabilities.filter((c) => typeof c === 'string' && c.trim()).map((c) => c.trim())
|
|
385
|
+
: [];
|
|
386
|
+
return {
|
|
387
|
+
ok: true,
|
|
388
|
+
body,
|
|
389
|
+
capabilities,
|
|
390
|
+
hasProfileIdentityCapability: capabilities.includes(PROFILE_IDENTITY_CAPABILITY),
|
|
391
|
+
};
|
|
352
392
|
}
|
|
353
393
|
|
|
354
394
|
/**
|
|
@@ -586,4 +626,5 @@ module.exports = {
|
|
|
586
626
|
_isHtmlResponse,
|
|
587
627
|
validateCompatibilityResponse,
|
|
588
628
|
COMPAT_HTML_MESSAGE,
|
|
629
|
+
PROFILE_IDENTITY_CAPABILITY,
|
|
589
630
|
};
|