@open-dy/interest-card-cli 0.1.0-beta.27 → 0.1.0-beta.28
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.
|
@@ -74,12 +74,29 @@ export async function handleCreatorDeviceLogin(parsed, options, deps = {}) {
|
|
|
74
74
|
// The status file is shared by every config in the directory: a request waiting for another
|
|
75
75
|
// config file, site or lane is not this login's to hand out again or to report as its failure.
|
|
76
76
|
const stored = await readCreatorLoginStatusFile(statusPath);
|
|
77
|
-
|
|
77
|
+
let previous = isSameLogin(stored, options, lane) ? stored : undefined;
|
|
78
78
|
if (detach && !parsed.options.force) {
|
|
79
|
-
const
|
|
79
|
+
const isAlive = deps.isAlive ?? isProcessAlive;
|
|
80
|
+
const pending = pendingRequest(previous, now(), isAlive);
|
|
80
81
|
if (pending) {
|
|
81
82
|
return handoff(options, pending, now(), json, lane);
|
|
82
83
|
}
|
|
84
|
+
// Some agent sandboxes kill the background worker when the command returns. The creator may
|
|
85
|
+
// still have confirmed the link, so this command asks for the outcome itself before it would
|
|
86
|
+
// hand out a new link.
|
|
87
|
+
const orphan = orphanedRequest(previous, now(), isAlive);
|
|
88
|
+
if (orphan) {
|
|
89
|
+
const outcome = await settleOrphanedRequest(options, orphan);
|
|
90
|
+
if (outcome.status === 'authorized') {
|
|
91
|
+
return outcome.result;
|
|
92
|
+
}
|
|
93
|
+
if (outcome.status === 'pending') {
|
|
94
|
+
await startWorker(parsed, options, deps, orphan, lane);
|
|
95
|
+
return handoff(options, orphan, now(), json, lane);
|
|
96
|
+
}
|
|
97
|
+
const settled = await readCreatorLoginStatusFile(statusPath);
|
|
98
|
+
previous = isSameLogin(settled, options, lane) ? settled : undefined;
|
|
99
|
+
}
|
|
83
100
|
}
|
|
84
101
|
const previousAttempt = failedLinkAttempt(previous);
|
|
85
102
|
if (previousAttempt?.code === NOT_ALLOWED_CODE) {
|
|
@@ -97,6 +114,8 @@ export async function handleCreatorDeviceLogin(parsed, options, deps = {}) {
|
|
|
97
114
|
verifyUrl: authorization.verifyUrl,
|
|
98
115
|
userCode: authorization.userCode,
|
|
99
116
|
expiresAtMs: now() + authorization.expiresInSeconds * 1000,
|
|
117
|
+
deviceCode: authorization.deviceCode,
|
|
118
|
+
intervalSeconds: authorization.intervalSeconds,
|
|
100
119
|
};
|
|
101
120
|
if (!detach) {
|
|
102
121
|
printText(linkInstructions(request, lane, previousAttempt));
|
|
@@ -108,6 +127,11 @@ export async function handleCreatorDeviceLogin(parsed, options, deps = {}) {
|
|
|
108
127
|
}, deps);
|
|
109
128
|
return saveLogin(options, config, false);
|
|
110
129
|
}
|
|
130
|
+
await startWorker(parsed, options, deps, request, lane);
|
|
131
|
+
return handoff(options, request, now(), json, lane, previousAttempt);
|
|
132
|
+
}
|
|
133
|
+
/** Starts the detached worker and writes the status file that hands it the request. */
|
|
134
|
+
async function startWorker(parsed, options, deps, request, lane) {
|
|
111
135
|
const child = spawnWorker(parsed, options, deps);
|
|
112
136
|
try {
|
|
113
137
|
if (typeof child.pid !== 'number') {
|
|
@@ -118,8 +142,8 @@ export async function handleCreatorDeviceLogin(parsed, options, deps = {}) {
|
|
|
118
142
|
verifyUrl: request.verifyUrl,
|
|
119
143
|
userCode: request.userCode,
|
|
120
144
|
expiresAt: new Date(request.expiresAtMs).toISOString(),
|
|
121
|
-
intervalSeconds:
|
|
122
|
-
deviceCode:
|
|
145
|
+
intervalSeconds: request.intervalSeconds,
|
|
146
|
+
deviceCode: request.deviceCode,
|
|
123
147
|
pid: child.pid,
|
|
124
148
|
baseUrl: options.baseUrl,
|
|
125
149
|
lane: lane ?? '',
|
|
@@ -134,7 +158,45 @@ export async function handleCreatorDeviceLogin(parsed, options, deps = {}) {
|
|
|
134
158
|
}
|
|
135
159
|
throw error;
|
|
136
160
|
}
|
|
137
|
-
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Asks the server once how a request whose worker is gone ended. A confirmed request is saved
|
|
164
|
+
* like the worker would have saved it; a refused or expired one is recorded so the new link says
|
|
165
|
+
* why; one nobody decided yet stays as it is.
|
|
166
|
+
*/
|
|
167
|
+
async function settleOrphanedRequest(options, request) {
|
|
168
|
+
const statusPath = creatorLoginStatusPath(options.configPath);
|
|
169
|
+
// `logout` or a newer login may have taken the status file while the server was asked.
|
|
170
|
+
const stillOurs = async () => {
|
|
171
|
+
const current = await readCreatorLoginStatusFile(statusPath);
|
|
172
|
+
return (current?.phase === 'awaiting_authorization' && current.deviceCode === request.deviceCode);
|
|
173
|
+
};
|
|
174
|
+
try {
|
|
175
|
+
const poll = await exchangeDeviceCodeForToken(request.deviceCode, {
|
|
176
|
+
base_url: options.baseUrl,
|
|
177
|
+
user: LINK_LOGIN_USER,
|
|
178
|
+
});
|
|
179
|
+
if (poll.status !== 'authorized') {
|
|
180
|
+
return { status: 'pending' };
|
|
181
|
+
}
|
|
182
|
+
if (!(await stillOurs())) {
|
|
183
|
+
await revokeCardToken(poll.config);
|
|
184
|
+
return { status: 'failed' };
|
|
185
|
+
}
|
|
186
|
+
const result = await saveLogin(options, poll.config, true);
|
|
187
|
+
await publishCreatorLoginStatus(options.configPath, { phase: 'succeeded' }).catch(() => fs.rm(statusPath, { force: true }).catch(() => undefined));
|
|
188
|
+
return { status: 'authorized', result };
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
if (await stillOurs()) {
|
|
192
|
+
await publishCreatorLoginStatus(options.configPath, {
|
|
193
|
+
phase: 'failed',
|
|
194
|
+
message: error instanceof Error ? error.message : String(error),
|
|
195
|
+
code: error instanceof CliError ? error.code : 'DEVICE_AUTH_FAILED',
|
|
196
|
+
}).catch(() => fs.rm(statusPath, { force: true }).catch(() => undefined));
|
|
197
|
+
}
|
|
198
|
+
return { status: 'failed' };
|
|
199
|
+
}
|
|
138
200
|
}
|
|
139
201
|
/**
|
|
140
202
|
* `login --wait`: blocks while a link authorization started by an earlier `login` is waiting for
|
|
@@ -357,6 +419,30 @@ function pendingRequest(status, now, isAlive) {
|
|
|
357
419
|
}
|
|
358
420
|
return { verifyUrl: status.verifyUrl, userCode: status.userCode, expiresAtMs };
|
|
359
421
|
}
|
|
422
|
+
/**
|
|
423
|
+
* A request that can still be confirmed but whose worker is gone, so nobody polls for it. The
|
|
424
|
+
* status file still holds what a poll needs.
|
|
425
|
+
*/
|
|
426
|
+
function orphanedRequest(status, now, isAlive) {
|
|
427
|
+
if (status?.phase !== 'awaiting_authorization' ||
|
|
428
|
+
!status.verifyUrl ||
|
|
429
|
+
!status.userCode ||
|
|
430
|
+
!status.deviceCode ||
|
|
431
|
+
typeof status.pid !== 'number') {
|
|
432
|
+
return undefined;
|
|
433
|
+
}
|
|
434
|
+
const expiresAtMs = Date.parse(status.expiresAt || '');
|
|
435
|
+
if (!Number.isFinite(expiresAtMs) || expiresAtMs <= now || isAlive(status.pid)) {
|
|
436
|
+
return undefined;
|
|
437
|
+
}
|
|
438
|
+
return {
|
|
439
|
+
verifyUrl: status.verifyUrl,
|
|
440
|
+
userCode: status.userCode,
|
|
441
|
+
expiresAtMs,
|
|
442
|
+
deviceCode: status.deviceCode,
|
|
443
|
+
intervalSeconds: status.intervalSeconds || 3,
|
|
444
|
+
};
|
|
445
|
+
}
|
|
360
446
|
function failedLinkAttempt(status) {
|
|
361
447
|
if (status?.phase !== 'failed' || !status.code || !status.message) {
|
|
362
448
|
return undefined;
|