reasonally 0.2.0 → 0.4.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/dist/login.js CHANGED
@@ -13,6 +13,8 @@ export function authorizeUrl(appUrl, params) {
13
13
  state: params.state,
14
14
  device: params.device,
15
15
  });
16
+ if (params.client)
17
+ query.set("client", params.client);
16
18
  return `${appUrl}/cli/authorize?${query}`;
17
19
  }
18
20
  /** Interprets a request to the loopback server. Only `/callback` with our `state` counts. */
@@ -43,10 +45,35 @@ function openBrowser(url) {
43
45
  // same
44
46
  }
45
47
  }
48
+ /** Thrown when the caller's `signal` stops a login that was waiting for the browser. */
49
+ export class LoginCancelled extends Error {
50
+ constructor() {
51
+ super("Login was cancelled.");
52
+ }
53
+ }
54
+ /** Thrown when the user pressed Cancel on the approval page. */
55
+ export class LoginDenied extends Error {
56
+ constructor() {
57
+ super("Login was cancelled in the browser.");
58
+ }
59
+ }
60
+ /** Thrown when nobody approved in the browser in time. */
61
+ export class LoginTimedOut extends Error {
62
+ constructor() {
63
+ super("Timed out waiting for approval in the browser. Run the login again.");
64
+ }
65
+ }
46
66
  /** Waits for the browser to come back to the loopback server with a code (or a refusal). */
47
- function waitForCallback(server, state) {
67
+ function waitForCallback(server, state, { signal, returnTo = "your terminal" } = {}) {
48
68
  return new Promise((resolve, reject) => {
49
- const timer = setTimeout(() => reject(new Error("Timed out waiting for approval in the browser. Run the login again.")), LOGIN_TIMEOUT_MS);
69
+ const timer = setTimeout(() => reject(new LoginTimedOut()), LOGIN_TIMEOUT_MS);
70
+ const onAbort = () => {
71
+ clearTimeout(timer);
72
+ reject(new LoginCancelled());
73
+ };
74
+ if (signal?.aborted)
75
+ return onAbort();
76
+ signal?.addEventListener("abort", onAbort, { once: true });
50
77
  server.on("request", (req, res) => {
51
78
  const result = readCallback(req.url ?? "/", state);
52
79
  const reply = (status, title, body) => {
@@ -56,26 +83,24 @@ function waitForCallback(server, state) {
56
83
  if (result.kind === "ignore")
57
84
  return reply(404, "Not found", "Nothing to see here.");
58
85
  clearTimeout(timer);
86
+ signal?.removeEventListener("abort", onAbort);
59
87
  if (result.kind === "code") {
60
- reply(200, "You're logged in", "You can close this tab and go back to your terminal.");
88
+ reply(200, "You're logged in", `You can close this tab and go back to ${returnTo}.`);
61
89
  resolve(result.code);
62
90
  }
63
91
  else if (result.kind === "denied") {
64
92
  reply(200, "Login cancelled", "You can close this tab.");
65
- reject(new Error("Login was cancelled in the browser."));
93
+ reject(new LoginDenied());
66
94
  }
67
95
  else {
68
- reply(400, "Login failed", "This response didn't match the login in your terminal.");
96
+ reply(400, "Login failed", `This response didn't match the login in ${returnTo}.`);
69
97
  reject(new Error("The browser returned an unexpected response. Run the login again."));
70
98
  }
71
99
  });
72
100
  });
73
101
  }
74
- /**
75
- * Browser login: opens the approval page, receives the code on a one-shot loopback server and
76
- * exchanges it (with the PKCE verifier) for an API key.
77
- */
78
- export async function browserLogin(appUrl, log, { open = true } = {}) {
102
+ export async function browserLogin(appUrl, log, options = {}) {
103
+ const { open = true } = options;
79
104
  const pkce = createPkce();
80
105
  const server = createServer();
81
106
  await new Promise((resolve, reject) => {
@@ -89,21 +114,24 @@ export async function browserLogin(appUrl, log, { open = true } = {}) {
89
114
  redirectUri,
90
115
  challenge: pkce.challenge,
91
116
  state: pkce.state,
92
- device: hostname().replace(/\.local$/, ""),
117
+ device: options.device ?? hostname().replace(/\.local$/, ""),
118
+ client: options.client,
93
119
  });
120
+ options.onUrl?.(url);
94
121
  if (open) {
95
122
  log("Opening your browser to approve this computer…");
96
123
  log(`If it doesn't open, visit:\n\n ${url}\n`);
97
- openBrowser(url);
124
+ (options.openUrl ?? openBrowser)(url);
98
125
  }
99
126
  else {
100
127
  log(`Open this link in the browser where you're signed in, on this computer:\n\n ${url}\n`);
101
128
  }
102
- const code = await waitForCallback(server, pkce.state);
129
+ const code = await waitForCallback(server, pkce.state, options);
103
130
  return await exchangeCode(appUrl, {
104
131
  code,
105
132
  code_verifier: pkce.verifier,
106
133
  redirect_uri: redirectUri,
134
+ client: options.client,
107
135
  });
108
136
  }
109
137
  finally {