scrollport 0.1.0
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 +147 -0
- package/dist/args.d.ts +21 -0
- package/dist/args.d.ts.map +1 -0
- package/dist/args.js +122 -0
- package/dist/args.js.map +1 -0
- package/dist/browser.d.ts +38 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +88 -0
- package/dist/browser.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +122 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/logout.d.ts +15 -0
- package/dist/commands/logout.d.ts.map +1 -0
- package/dist/commands/logout.js +25 -0
- package/dist/commands/logout.js.map +1 -0
- package/dist/commands/setup.d.ts +18 -0
- package/dist/commands/setup.d.ts.map +1 -0
- package/dist/commands/setup.js +171 -0
- package/dist/commands/setup.js.map +1 -0
- package/dist/commands/status.d.ts +21 -0
- package/dist/commands/status.d.ts.map +1 -0
- package/dist/commands/status.js +85 -0
- package/dist/commands/status.js.map +1 -0
- package/dist/credentials.d.ts +54 -0
- package/dist/credentials.d.ts.map +1 -0
- package/dist/credentials.js +197 -0
- package/dist/credentials.js.map +1 -0
- package/dist/device-client.d.ts +24 -0
- package/dist/device-client.d.ts.map +1 -0
- package/dist/device-client.js +82 -0
- package/dist/device-client.js.map +1 -0
- package/dist/device-flow.d.ts +70 -0
- package/dist/device-flow.d.ts.map +1 -0
- package/dist/device-flow.js +128 -0
- package/dist/device-flow.js.map +1 -0
- package/dist/errors.d.ts +58 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +61 -0
- package/dist/errors.js.map +1 -0
- package/dist/http.d.ts +76 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +163 -0
- package/dist/http.js.map +1 -0
- package/dist/output.d.ts +31 -0
- package/dist/output.d.ts.map +1 -0
- package/dist/output.js +27 -0
- package/dist/output.js.map +1 -0
- package/dist/verify.d.ts +4 -0
- package/dist/verify.d.ts.map +1 -0
- package/dist/verify.js +27 -0
- package/dist/verify.js.map +1 -0
- package/package.json +31 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { isTeachingError, toCliError } from "./http.js";
|
|
2
|
+
import { isCliError } from "./errors.js";
|
|
3
|
+
/** Shown on the consent screen so the human can see what they are approving. */
|
|
4
|
+
export const CLIENT_NAME = "scrollport CLI";
|
|
5
|
+
/**
|
|
6
|
+
* `POST /v1/auth/device`. Returns both plaintext codes — the only time they
|
|
7
|
+
* exist. The device code is a credential and is never displayed; the user code
|
|
8
|
+
* is displayed precisely so the human can compare it with the consent screen.
|
|
9
|
+
*/
|
|
10
|
+
export async function startDeviceAuthorization(client, opts = {}) {
|
|
11
|
+
// Typed as the shared contract's own response so a drift between this CLI and
|
|
12
|
+
// packages/shared/src/api.ts fails the build rather than a user's setup.
|
|
13
|
+
const body = await client.request("POST", "/auth/device", {
|
|
14
|
+
body: {
|
|
15
|
+
client_name: opts.clientName ?? CLIENT_NAME,
|
|
16
|
+
...(opts.scopes ? { scopes: opts.scopes } : {}),
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
return body;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* One poll of `POST /v1/auth/token`.
|
|
23
|
+
*
|
|
24
|
+
* Never throws for an expected outcome — pending, slow_down, denial, expiry and
|
|
25
|
+
* "the network is having a moment" are all values. Only a genuinely unexpected
|
|
26
|
+
* response (a 400 we do not recognise, a 401) throws, because that is a bug
|
|
27
|
+
* rather than a state, and the API's own teaching error is the best thing to
|
|
28
|
+
* show for it.
|
|
29
|
+
*/
|
|
30
|
+
export async function pollDeviceToken(client, deviceCode) {
|
|
31
|
+
let res;
|
|
32
|
+
try {
|
|
33
|
+
res = await client.send("POST", "/auth/token", {
|
|
34
|
+
body: {
|
|
35
|
+
device_code: deviceCode,
|
|
36
|
+
// Accepted and ignored by our server; sent because it is what RFC 8628
|
|
37
|
+
// clients send and costs nothing to be correct about.
|
|
38
|
+
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
43
|
+
// A dropped connection mid-flow is not a reason to abandon an approval the
|
|
44
|
+
// human may be seconds away from clicking.
|
|
45
|
+
if (isCliError(err) && err.code === "api_unreachable") {
|
|
46
|
+
return { kind: "transient", reason: err.message };
|
|
47
|
+
}
|
|
48
|
+
throw err;
|
|
49
|
+
}
|
|
50
|
+
if (res.ok) {
|
|
51
|
+
const granted = res.body;
|
|
52
|
+
if (granted && typeof granted.api_key === "string" && typeof granted.account_id === "string") {
|
|
53
|
+
return { kind: "granted", apiKey: granted.api_key, accountId: granted.account_id };
|
|
54
|
+
}
|
|
55
|
+
return { kind: "transient", reason: "the API returned 200 with no api_key" };
|
|
56
|
+
}
|
|
57
|
+
// Rate limiting and server errors: retryable, and the loop backs off.
|
|
58
|
+
if (res.status === 429 || res.status >= 500) {
|
|
59
|
+
return { kind: "transient", reason: `HTTP ${res.status}` };
|
|
60
|
+
}
|
|
61
|
+
if (isTeachingError(res.body)) {
|
|
62
|
+
const interval = readInterval(res.body);
|
|
63
|
+
switch (res.body.error) {
|
|
64
|
+
case "authorization_pending":
|
|
65
|
+
return { kind: "pending", ...(interval === null ? {} : { interval }) };
|
|
66
|
+
case "slow_down":
|
|
67
|
+
return { kind: "slow_down", ...(interval === null ? {} : { interval }) };
|
|
68
|
+
case "access_denied":
|
|
69
|
+
return { kind: "denied" };
|
|
70
|
+
case "expired_token":
|
|
71
|
+
return { kind: "expired" };
|
|
72
|
+
default:
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
throw toCliError(res);
|
|
77
|
+
}
|
|
78
|
+
function readInterval(body) {
|
|
79
|
+
const value = body["interval"];
|
|
80
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=device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,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,CAAC;YAC3E,KAAK,eAAe;gBAClB,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;YAC5B,KAAK,eAAe;gBAClB,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;YAC7B;gBACE,MAAM;QACV,CAAC;IACH,CAAC;IAED,MAAM,UAAU,CAAC,GAAG,CAAC,CAAC;AACxB,CAAC;AAED,SAAS,YAAY,CAAC,IAAa;IACjC,MAAM,KAAK,GAAI,IAAgC,CAAC,UAAU,CAAC,CAAC;IAC5D,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;AAC5E,CAAC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/** Floor on the poll interval, whatever the server says. */
|
|
2
|
+
export declare const MIN_INTERVAL_SECONDS = 1;
|
|
3
|
+
/** Ceiling, so a bad `interval` cannot park us for an hour inside a 10-minute window. */
|
|
4
|
+
export declare const MAX_INTERVAL_SECONDS = 60;
|
|
5
|
+
/** Applied when the server says `slow_down` without saying how much. RFC 8628 §3.5. */
|
|
6
|
+
export declare const SLOW_DOWN_FALLBACK_BACKOFF_SECONDS = 5;
|
|
7
|
+
/** Consecutive transport failures tolerated before we stop and say so. */
|
|
8
|
+
export declare const MAX_CONSECUTIVE_TRANSIENT_FAILURES = 5;
|
|
9
|
+
/** Ceiling on the exponential backoff applied to transport failures. */
|
|
10
|
+
export declare const MAX_TRANSIENT_BACKOFF_SECONDS = 30;
|
|
11
|
+
/** The start-authorization response we need to run the loop (a subset of `DeviceAuthResponse`). */
|
|
12
|
+
export interface DeviceAuthorizationStart {
|
|
13
|
+
device_code: string;
|
|
14
|
+
user_code: string;
|
|
15
|
+
verification_uri: string;
|
|
16
|
+
verification_uri_complete: string;
|
|
17
|
+
expires_in: number;
|
|
18
|
+
interval: number;
|
|
19
|
+
}
|
|
20
|
+
/** One poll's meaning, with the HTTP already stripped off. */
|
|
21
|
+
export type PollOutcome = {
|
|
22
|
+
kind: "granted";
|
|
23
|
+
apiKey: string;
|
|
24
|
+
accountId: string;
|
|
25
|
+
} | {
|
|
26
|
+
kind: "pending";
|
|
27
|
+
interval?: number;
|
|
28
|
+
} | {
|
|
29
|
+
kind: "slow_down";
|
|
30
|
+
interval?: number;
|
|
31
|
+
} | {
|
|
32
|
+
kind: "denied";
|
|
33
|
+
} | {
|
|
34
|
+
kind: "expired";
|
|
35
|
+
}
|
|
36
|
+
/** The API could not be reached or answered 5xx — retry, do not give up. */
|
|
37
|
+
| {
|
|
38
|
+
kind: "transient";
|
|
39
|
+
reason: string;
|
|
40
|
+
};
|
|
41
|
+
export interface WaitEvent {
|
|
42
|
+
reason: "pending" | "slow_down" | "transient";
|
|
43
|
+
/** Seconds we are about to wait. */
|
|
44
|
+
waitSeconds: number;
|
|
45
|
+
/** Seconds left before both codes die. */
|
|
46
|
+
secondsRemaining: number;
|
|
47
|
+
/** Set on `transient`: how many failures in a row so far. */
|
|
48
|
+
failures?: number;
|
|
49
|
+
}
|
|
50
|
+
export interface AwaitDeps {
|
|
51
|
+
poll: (deviceCode: string) => Promise<PollOutcome>;
|
|
52
|
+
sleep: (ms: number) => Promise<void>;
|
|
53
|
+
now: () => number;
|
|
54
|
+
onWait?: (event: WaitEvent) => void;
|
|
55
|
+
}
|
|
56
|
+
export interface DeviceGrantResult {
|
|
57
|
+
apiKey: string;
|
|
58
|
+
accountId: string;
|
|
59
|
+
}
|
|
60
|
+
/** Keep an interval sane whatever the wire said. NaN and 0 are both hostile. */
|
|
61
|
+
export declare function clampInterval(seconds: number): number;
|
|
62
|
+
/**
|
|
63
|
+
* Poll until the human decides, or until the codes expire.
|
|
64
|
+
*
|
|
65
|
+
* Resolves with the key. Every other ending throws a CliError whose message says
|
|
66
|
+
* what a person should do next — a device flow fails in four distinct ways and
|
|
67
|
+
* "authorization failed" is the wrong answer to all of them.
|
|
68
|
+
*/
|
|
69
|
+
export declare function awaitAuthorization(auth: DeviceAuthorizationStart, deps: AwaitDeps): Promise<DeviceGrantResult>;
|
|
70
|
+
//# sourceMappingURL=device-flow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"device-flow.d.ts","sourceRoot":"","sources":["../src/device-flow.ts"],"names":[],"mappings":"AAyBA,4DAA4D;AAC5D,eAAO,MAAM,oBAAoB,IAAI,CAAC;AACtC,yFAAyF;AACzF,eAAO,MAAM,oBAAoB,KAAK,CAAC;AACvC,uFAAuF;AACvF,eAAO,MAAM,kCAAkC,IAAI,CAAC;AACpD,0EAA0E;AAC1E,eAAO,MAAM,kCAAkC,IAAI,CAAC;AACpD,wEAAwE;AACxE,eAAO,MAAM,6BAA6B,KAAK,CAAC;AAEhD,mGAAmG;AACnG,MAAM,WAAW,wBAAwB;IACvC,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB,EAAE,MAAM,CAAC;IACzB,yBAAyB,EAAE,MAAM,CAAC;IAClC,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,8DAA8D;AAC9D,MAAM,MAAM,WAAW,GACnB;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GACtD;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,GACtC;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,GACxC;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAClB;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE;AACrB,4EAA4E;GAC1E;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAE1C,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,SAAS,GAAG,WAAW,GAAG,WAAW,CAAC;IAC9C,oCAAoC;IACpC,WAAW,EAAE,MAAM,CAAC;IACpB,0CAA0C;IAC1C,gBAAgB,EAAE,MAAM,CAAC;IACzB,6DAA6D;IAC7D,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC,WAAW,CAAC,CAAC;IACnD,KAAK,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACrC,GAAG,EAAE,MAAM,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,CAAC;CACrC;AAED,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,gFAAgF;AAChF,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAGrD;AAED;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CACtC,IAAI,EAAE,wBAAwB,EAC9B,IAAI,EAAE,SAAS,GACd,OAAO,CAAC,iBAAiB,CAAC,CA8F5B"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The client half of the RFC 8628 device grant: wait for a human to approve,
|
|
3
|
+
* without hammering the endpoint.
|
|
4
|
+
*
|
|
5
|
+
* This module is deliberately free of HTTP, clocks and I/O — `poll`, `sleep` and
|
|
6
|
+
* `now` are injected. That is not ceremony: the interesting behaviour here is
|
|
7
|
+
* *timing* (did we respect the interval? did we back off? did we stop?), and
|
|
8
|
+
* timing is only testable when the clock is a parameter. The tests are hermetic;
|
|
9
|
+
* no server is stood up.
|
|
10
|
+
*
|
|
11
|
+
* The server's poll semantics (apps/api/src/auth/device-flow.ts, PR #45) that
|
|
12
|
+
* this is built against:
|
|
13
|
+
* • `authorization_pending` and `slow_down` both arrive as **HTTP 400** with
|
|
14
|
+
* an `interval` field — per RFC 8628 §3.5, waiting is not an error but is
|
|
15
|
+
* spelled like one.
|
|
16
|
+
* • the rate check is `last_polled_at is not null and …`, so the **first**
|
|
17
|
+
* poll can never be too soon. We use that: poll immediately, then sleep.
|
|
18
|
+
* • `slow_down` *grows* the server's stored interval by 5s and returns the new
|
|
19
|
+
* value. Adopting the returned number is mandatory — ignoring it means every
|
|
20
|
+
* subsequent poll is also too fast, and the interval keeps climbing.
|
|
21
|
+
* • `expired_token` covers unknown, expired and already-redeemed codes with
|
|
22
|
+
* one answer. All three mean the same thing to us: start over.
|
|
23
|
+
*/
|
|
24
|
+
import { CliError, EXIT_DENIED, EXIT_EXPIRED, EXIT_NETWORK } from "./errors.js";
|
|
25
|
+
/** Floor on the poll interval, whatever the server says. */
|
|
26
|
+
export const MIN_INTERVAL_SECONDS = 1;
|
|
27
|
+
/** Ceiling, so a bad `interval` cannot park us for an hour inside a 10-minute window. */
|
|
28
|
+
export const MAX_INTERVAL_SECONDS = 60;
|
|
29
|
+
/** Applied when the server says `slow_down` without saying how much. RFC 8628 §3.5. */
|
|
30
|
+
export const SLOW_DOWN_FALLBACK_BACKOFF_SECONDS = 5;
|
|
31
|
+
/** Consecutive transport failures tolerated before we stop and say so. */
|
|
32
|
+
export const MAX_CONSECUTIVE_TRANSIENT_FAILURES = 5;
|
|
33
|
+
/** Ceiling on the exponential backoff applied to transport failures. */
|
|
34
|
+
export const MAX_TRANSIENT_BACKOFF_SECONDS = 30;
|
|
35
|
+
/** Keep an interval sane whatever the wire said. NaN and 0 are both hostile. */
|
|
36
|
+
export function clampInterval(seconds) {
|
|
37
|
+
if (!Number.isFinite(seconds))
|
|
38
|
+
return MIN_INTERVAL_SECONDS;
|
|
39
|
+
return Math.min(Math.max(Math.round(seconds), MIN_INTERVAL_SECONDS), MAX_INTERVAL_SECONDS);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Poll until the human decides, or until the codes expire.
|
|
43
|
+
*
|
|
44
|
+
* Resolves with the key. Every other ending throws a CliError whose message says
|
|
45
|
+
* what a person should do next — a device flow fails in four distinct ways and
|
|
46
|
+
* "authorization failed" is the wrong answer to all of them.
|
|
47
|
+
*/
|
|
48
|
+
export async function awaitAuthorization(auth, deps) {
|
|
49
|
+
const deadline = deps.now() + Math.max(auth.expires_in, 0) * 1000;
|
|
50
|
+
let interval = clampInterval(auth.interval);
|
|
51
|
+
let consecutiveFailures = 0;
|
|
52
|
+
for (;;) {
|
|
53
|
+
const outcome = await deps.poll(auth.device_code);
|
|
54
|
+
let reason;
|
|
55
|
+
let waitSeconds;
|
|
56
|
+
switch (outcome.kind) {
|
|
57
|
+
case "granted":
|
|
58
|
+
return { apiKey: outcome.apiKey, accountId: outcome.accountId };
|
|
59
|
+
case "denied":
|
|
60
|
+
throw new CliError("access_denied", "The approval was declined on the consent screen.", {
|
|
61
|
+
hint: "Nothing was provisioned and no wallet was charged. If that was a mistake, run `scrollport setup` again for a fresh code.",
|
|
62
|
+
exitCode: EXIT_DENIED,
|
|
63
|
+
});
|
|
64
|
+
case "expired":
|
|
65
|
+
// Unknown, expired and already-redeemed all land here — the server
|
|
66
|
+
// answers all three identically on purpose, so we must not guess which.
|
|
67
|
+
throw new CliError("expired_token", "This device code is no longer valid.", {
|
|
68
|
+
hint: "Codes are short-lived and issue a key exactly once. Run `scrollport setup` again to get a fresh one.",
|
|
69
|
+
exitCode: EXIT_EXPIRED,
|
|
70
|
+
});
|
|
71
|
+
case "pending":
|
|
72
|
+
consecutiveFailures = 0;
|
|
73
|
+
interval = clampInterval(outcome.interval ?? interval);
|
|
74
|
+
reason = "pending";
|
|
75
|
+
waitSeconds = interval;
|
|
76
|
+
break;
|
|
77
|
+
case "slow_down":
|
|
78
|
+
consecutiveFailures = 0;
|
|
79
|
+
// The server has already grown its own interval and told us the new one.
|
|
80
|
+
// Adopting it is the whole point; the fallback is only for a server that
|
|
81
|
+
// omits the field.
|
|
82
|
+
interval = clampInterval(outcome.interval ?? interval + SLOW_DOWN_FALLBACK_BACKOFF_SECONDS);
|
|
83
|
+
reason = "slow_down";
|
|
84
|
+
waitSeconds = interval;
|
|
85
|
+
break;
|
|
86
|
+
case "transient": {
|
|
87
|
+
consecutiveFailures += 1;
|
|
88
|
+
if (consecutiveFailures >= MAX_CONSECUTIVE_TRANSIENT_FAILURES) {
|
|
89
|
+
throw new CliError("api_unreachable", `The scrollport API failed ${consecutiveFailures} times in a row while waiting for approval (${outcome.reason}).`, {
|
|
90
|
+
hint: "Your approval may still have gone through. Check your connection and run `scrollport setup` again — approving twice is harmless.",
|
|
91
|
+
exitCode: EXIT_NETWORK,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
// Exponential, from the current interval: a blip should not cost us the
|
|
95
|
+
// approval window, and a sustained outage should not cost the API a
|
|
96
|
+
// request per second.
|
|
97
|
+
waitSeconds = Math.min(interval * 2 ** (consecutiveFailures - 1), MAX_TRANSIENT_BACKOFF_SECONDS);
|
|
98
|
+
reason = "transient";
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const remainingMs = deadline - deps.now();
|
|
103
|
+
if (remainingMs <= 0) {
|
|
104
|
+
throw new CliError("expired_token", "The approval window closed before anyone approved this device.", {
|
|
105
|
+
hint: `The link is only valid for about ${describeWindow(auth.expires_in)}. Run \`scrollport setup\` again when you are ready to click.`,
|
|
106
|
+
exitCode: EXIT_EXPIRED,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
// Never sleep past the deadline: waking up to poll a code we know is dead
|
|
110
|
+
// gets us the server's own `expired_token`, which is the message we want,
|
|
111
|
+
// and it happens as soon as it is true rather than a full interval later.
|
|
112
|
+
const waitMs = Math.min(waitSeconds * 1000, remainingMs);
|
|
113
|
+
deps.onWait?.({
|
|
114
|
+
reason,
|
|
115
|
+
waitSeconds: Math.round(waitMs / 1000),
|
|
116
|
+
secondsRemaining: Math.round(remainingMs / 1000),
|
|
117
|
+
...(reason === "transient" ? { failures: consecutiveFailures } : {}),
|
|
118
|
+
});
|
|
119
|
+
await deps.sleep(waitMs);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** "10 minutes" / "45 seconds". Never "0 minutes", which reads as a bug. */
|
|
123
|
+
function describeWindow(seconds) {
|
|
124
|
+
if (seconds >= 90)
|
|
125
|
+
return `${Math.round(seconds / 60)} minutes`;
|
|
126
|
+
return `${Math.max(Math.round(seconds), 1)} seconds`;
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=device-flow.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"device-flow.js","sourceRoot":"","sources":["../src/device-flow.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhF,4DAA4D;AAC5D,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,CAAC;AACtC,yFAAyF;AACzF,MAAM,CAAC,MAAM,oBAAoB,GAAG,EAAE,CAAC;AACvC,uFAAuF;AACvF,MAAM,CAAC,MAAM,kCAAkC,GAAG,CAAC,CAAC;AACpD,0EAA0E;AAC1E,MAAM,CAAC,MAAM,kCAAkC,GAAG,CAAC,CAAC;AACpD,wEAAwE;AACxE,MAAM,CAAC,MAAM,6BAA6B,GAAG,EAAE,CAAC;AA4ChD,gFAAgF;AAChF,MAAM,UAAU,aAAa,CAAC,OAAe;IAC3C,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC;QAAE,OAAO,oBAAoB,CAAC;IAC3D,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,oBAAoB,CAAC,EAAE,oBAAoB,CAA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CAAC;YACZ,MAAM;YACN,WAAW,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC;YACtC,gBAAgB,EAAE,IAAI,CAAC,KAAK,CAAC,WAAW,GAAG,IAAI,CAAC;YAChD,GAAG,CAAC,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,mBAAmB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SACrE,CAAC,CAAC;QACH,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAC3B,CAAC;AACH,CAAC;AAED,4EAA4E;AAC5E,SAAS,cAAc,CAAC,OAAe;IACrC,IAAI,OAAO,IAAI,EAAE;QAAE,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,EAAE,CAAC,UAAU,CAAC;IAChE,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,UAAU,CAAC;AACvD,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Failures, and the exit codes that carry them.
|
|
3
|
+
*
|
|
4
|
+
* Agents parse this CLI (issue #12), so an exit code is part of the contract:
|
|
5
|
+
* it has to say *which* kind of thing went wrong without the caller having to
|
|
6
|
+
* regex a message. Every code below is documented in README.md and must stay
|
|
7
|
+
* stable.
|
|
8
|
+
*
|
|
9
|
+
* CLAUDE.md > Invariant 5 applies to the terminal exactly as it applies to the
|
|
10
|
+
* API: `{ error, message, hint }`, where the hint carries the corrected command
|
|
11
|
+
* wherever one exists. A CLI that prints "Error: request failed" has taught the
|
|
12
|
+
* user nothing and cost them a support round-trip.
|
|
13
|
+
*/
|
|
14
|
+
/** Success. */
|
|
15
|
+
export declare const EXIT_OK = 0;
|
|
16
|
+
/** Something we did not anticipate. The catch-all, deliberately not reused. */
|
|
17
|
+
export declare const EXIT_ERROR = 1;
|
|
18
|
+
/** The command line itself was wrong: unknown command, bad flag, missing value. */
|
|
19
|
+
export declare const EXIT_USAGE = 2;
|
|
20
|
+
/** A human saw the consent screen and said no. */
|
|
21
|
+
export declare const EXIT_DENIED = 3;
|
|
22
|
+
/** The approval window closed — nobody approved in time, or the code was spent. */
|
|
23
|
+
export declare const EXIT_EXPIRED = 4;
|
|
24
|
+
/** The API could not be reached, or kept failing. Retryable; not the user's fault. */
|
|
25
|
+
export declare const EXIT_NETWORK = 5;
|
|
26
|
+
/** No usable key. Always fixed by `scrollport setup`. */
|
|
27
|
+
export declare const EXIT_UNAUTHENTICATED = 6;
|
|
28
|
+
/** Ctrl-C. The conventional 128 + SIGINT(2). */
|
|
29
|
+
export declare const EXIT_INTERRUPTED = 130;
|
|
30
|
+
/** The API's error envelope (packages/shared `ApiError`), mirrored for the terminal. */
|
|
31
|
+
export interface TeachingError {
|
|
32
|
+
error: string;
|
|
33
|
+
message: string;
|
|
34
|
+
hint?: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A failure we chose, with the exit code and the next action attached.
|
|
38
|
+
*
|
|
39
|
+
* Anything thrown that is *not* a CliError is a bug in this CLI and surfaces as
|
|
40
|
+
* EXIT_ERROR with its stack — deliberately noisier than a handled failure, so
|
|
41
|
+
* the two are never confused in a log.
|
|
42
|
+
*/
|
|
43
|
+
export declare class CliError extends Error {
|
|
44
|
+
readonly code: string;
|
|
45
|
+
readonly hint: string | undefined;
|
|
46
|
+
readonly exitCode: number;
|
|
47
|
+
constructor(code: string, message: string, opts?: {
|
|
48
|
+
hint?: string;
|
|
49
|
+
exitCode?: number;
|
|
50
|
+
cause?: unknown;
|
|
51
|
+
});
|
|
52
|
+
toJSON(): TeachingError & {
|
|
53
|
+
exit_code: number;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/** Narrow an unknown catch binding without swallowing real bugs. */
|
|
57
|
+
export declare function isCliError(err: unknown): err is CliError;
|
|
58
|
+
//# sourceMapping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|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Failures, and the exit codes that carry them.
|
|
3
|
+
*
|
|
4
|
+
* Agents parse this CLI (issue #12), so an exit code is part of the contract:
|
|
5
|
+
* it has to say *which* kind of thing went wrong without the caller having to
|
|
6
|
+
* regex a message. Every code below is documented in README.md and must stay
|
|
7
|
+
* stable.
|
|
8
|
+
*
|
|
9
|
+
* CLAUDE.md > Invariant 5 applies to the terminal exactly as it applies to the
|
|
10
|
+
* API: `{ error, message, hint }`, where the hint carries the corrected command
|
|
11
|
+
* wherever one exists. A CLI that prints "Error: request failed" has taught the
|
|
12
|
+
* user nothing and cost them a support round-trip.
|
|
13
|
+
*/
|
|
14
|
+
/** Success. */
|
|
15
|
+
export const EXIT_OK = 0;
|
|
16
|
+
/** Something we did not anticipate. The catch-all, deliberately not reused. */
|
|
17
|
+
export const EXIT_ERROR = 1;
|
|
18
|
+
/** The command line itself was wrong: unknown command, bad flag, missing value. */
|
|
19
|
+
export const EXIT_USAGE = 2;
|
|
20
|
+
/** A human saw the consent screen and said no. */
|
|
21
|
+
export const EXIT_DENIED = 3;
|
|
22
|
+
/** The approval window closed — nobody approved in time, or the code was spent. */
|
|
23
|
+
export const EXIT_EXPIRED = 4;
|
|
24
|
+
/** The API could not be reached, or kept failing. Retryable; not the user's fault. */
|
|
25
|
+
export const EXIT_NETWORK = 5;
|
|
26
|
+
/** No usable key. Always fixed by `scrollport setup`. */
|
|
27
|
+
export const EXIT_UNAUTHENTICATED = 6;
|
|
28
|
+
/** Ctrl-C. The conventional 128 + SIGINT(2). */
|
|
29
|
+
export const EXIT_INTERRUPTED = 130;
|
|
30
|
+
/**
|
|
31
|
+
* A failure we chose, with the exit code and the next action attached.
|
|
32
|
+
*
|
|
33
|
+
* Anything thrown that is *not* a CliError is a bug in this CLI and surfaces as
|
|
34
|
+
* EXIT_ERROR with its stack — deliberately noisier than a handled failure, so
|
|
35
|
+
* the two are never confused in a log.
|
|
36
|
+
*/
|
|
37
|
+
export class CliError extends Error {
|
|
38
|
+
code;
|
|
39
|
+
hint;
|
|
40
|
+
exitCode;
|
|
41
|
+
constructor(code, message, opts = {}) {
|
|
42
|
+
super(message, opts.cause === undefined ? undefined : { cause: opts.cause });
|
|
43
|
+
this.name = "CliError";
|
|
44
|
+
this.code = code;
|
|
45
|
+
this.hint = opts.hint;
|
|
46
|
+
this.exitCode = opts.exitCode ?? EXIT_ERROR;
|
|
47
|
+
}
|
|
48
|
+
toJSON() {
|
|
49
|
+
return {
|
|
50
|
+
error: this.code,
|
|
51
|
+
message: this.message,
|
|
52
|
+
...(this.hint === undefined ? {} : { hint: this.hint }),
|
|
53
|
+
exit_code: this.exitCode,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** Narrow an unknown catch binding without swallowing real bugs. */
|
|
58
|
+
export function isCliError(err) {
|
|
59
|
+
return err instanceof CliError;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,eAAe;AACf,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,CAAC;AACzB,+EAA+E;AAC/E,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,CAAC;AAC5B,mFAAmF;AACnF,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,CAAC;AAC5B,kDAAkD;AAClD,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAC;AAC7B,mFAAmF;AACnF,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,CAAC;AAC9B,sFAAsF;AACtF,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,CAAC;AAC9B,yDAAyD;AACzD,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,CAAC;AACtC,gDAAgD;AAChD,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,CAAC;AASpC;;;;;;GAMG;AACH,MAAM,OAAO,QAAS,SAAQ,KAAK;IACxB,IAAI,CAAS;IACb,IAAI,CAAqB;IACzB,QAAQ,CAAS;IAE1B,YACE,IAAY,EACZ,OAAe,EACf,OAA8D,EAAE;QAEhE,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;QAC7E,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACtB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,IAAI,UAAU,CAAC;IAC9C,CAAC;IAED,MAAM;QACJ,OAAO;YACL,KAAK,EAAE,IAAI,CAAC,IAAI;YAChB,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,GAAG,CAAC,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC;YACvD,SAAS,EAAE,IAAI,CAAC,QAAQ;SACzB,CAAC;IACJ,CAAC;CACF;AAED,oEAAoE;AACpE,MAAM,UAAU,UAAU,CAAC,GAAY;IACrC,OAAO,GAAG,YAAY,QAAQ,CAAC;AACjC,CAAC"}
|
package/dist/http.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The thinnest possible API client: one request method, no business logic
|
|
3
|
+
* (issue #12 — "thin pure API client"). Everything it knows about the contract
|
|
4
|
+
* is the base path and the `{ error, message, hint }` error envelope.
|
|
5
|
+
*
|
|
6
|
+
* No HTTP library. Node ≥22 has `fetch`, and a CLI people reach through `npx`
|
|
7
|
+
* pays for every dependency in start-up latency against a 60-second budget
|
|
8
|
+
* (issue #9's acceptance criterion).
|
|
9
|
+
*/
|
|
10
|
+
import { CliError, type TeachingError } from "./errors.js";
|
|
11
|
+
/**
|
|
12
|
+
* The versioned prefix every endpoint sits under. Mirrors `API_BASE_PATH` from
|
|
13
|
+
* `@scrollport/shared`, which is the executable contract; duplicated as a
|
|
14
|
+
* literal only so this package carries no runtime dependency on a private
|
|
15
|
+
* workspace package. `http.test.ts` asserts the two agree, so the duplication
|
|
16
|
+
* cannot drift silently.
|
|
17
|
+
*/
|
|
18
|
+
export declare const API_BASE_PATH = "/v1";
|
|
19
|
+
/** Where the API lives when nothing says otherwise. */
|
|
20
|
+
export declare const DEFAULT_API_URL = "https://api.scrollport.com";
|
|
21
|
+
/** Per-request ceiling. Generous: a cold Railway container can be slow. */
|
|
22
|
+
export declare const DEFAULT_TIMEOUT_MS = 30000;
|
|
23
|
+
export type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
|
|
24
|
+
/** A raw response, decoded but not judged. Callers decide what a status means. */
|
|
25
|
+
export interface ApiResponse {
|
|
26
|
+
status: number;
|
|
27
|
+
ok: boolean;
|
|
28
|
+
/** Parsed JSON body, or `undefined` when the body was empty or not JSON. */
|
|
29
|
+
body: unknown;
|
|
30
|
+
}
|
|
31
|
+
export interface ApiClientOptions {
|
|
32
|
+
fetch?: FetchLike;
|
|
33
|
+
timeoutMs?: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Normalize whatever the user typed into an origin we can append paths to.
|
|
37
|
+
*
|
|
38
|
+
* Accepts `api.scrollport.com`, `https://api.scrollport.com/`, and
|
|
39
|
+
* `http://localhost:8787/v1` alike — the trailing `/v1` is stripped because we
|
|
40
|
+
* add it ourselves, and getting `/v1/v1/wallet` for being helpful is a bad
|
|
41
|
+
* first-run experience.
|
|
42
|
+
*/
|
|
43
|
+
export declare function normalizeApiUrl(raw: string): string;
|
|
44
|
+
export declare class ApiClient {
|
|
45
|
+
readonly baseUrl: string;
|
|
46
|
+
private readonly fetchImpl;
|
|
47
|
+
private readonly timeoutMs;
|
|
48
|
+
constructor(baseUrl: string, opts?: ApiClientOptions);
|
|
49
|
+
/**
|
|
50
|
+
* Send one request. Returns non-2xx responses rather than throwing: the
|
|
51
|
+
* device-flow poll spends its whole life reading HTTP 400s that are not
|
|
52
|
+
* failures (RFC 8628 §3.5), so "not ok" cannot mean "throw" here.
|
|
53
|
+
*
|
|
54
|
+
* Only a *transport* failure throws, and it throws a CliError that names the
|
|
55
|
+
* URL — "connection refused" with no address is the least useful message in
|
|
56
|
+
* computing.
|
|
57
|
+
*/
|
|
58
|
+
send(method: string, path: string, opts?: {
|
|
59
|
+
body?: unknown;
|
|
60
|
+
apiKey?: string;
|
|
61
|
+
}): Promise<ApiResponse>;
|
|
62
|
+
/** `send`, but a non-2xx is a failure and the API's own teaching error is kept. */
|
|
63
|
+
request<T>(method: string, path: string, opts?: {
|
|
64
|
+
body?: unknown;
|
|
65
|
+
apiKey?: string;
|
|
66
|
+
}): Promise<T>;
|
|
67
|
+
}
|
|
68
|
+
/** True when a body carries the API's `{ error, message }` envelope. */
|
|
69
|
+
export declare function isTeachingError(body: unknown): body is TeachingError;
|
|
70
|
+
/**
|
|
71
|
+
* Turn a failed response into a CliError, preserving the API's message and hint
|
|
72
|
+
* verbatim. The server already wrote a teaching error (CLAUDE.md > Invariant 5);
|
|
73
|
+
* rewording it here would only lose information.
|
|
74
|
+
*/
|
|
75
|
+
export declare function toCliError(res: ApiResponse): CliError;
|
|
76
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,QAAQ,EAAgB,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AAEzE;;;;;;GAMG;AACH,eAAO,MAAM,aAAa,QAAQ,C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