@grayspar/cli 0.0.2 → 0.0.3

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.
@@ -41,10 +41,12 @@ async function login(options) {
41
41
  token: session.token,
42
42
  email: session.email,
43
43
  });
44
+ await listener.answerBrowser(true);
44
45
  progress.stop();
45
46
  printResult(options.format, { status: 'ok', email: session.email }, () => receipt(`Signed in as ${session.email}`));
46
47
  }
47
48
  catch (error) {
49
+ await listener.answerBrowser(false);
48
50
  progress.stop();
49
51
  throw error;
50
52
  }
@@ -1,23 +1,32 @@
1
1
  /* Listens on a port the machine picks, catches exactly one redirect, and closes. The browser carries
2
- a code; the verifier that makes it usable never leaves this process. */
2
+ a code; the verifier that makes it usable never leaves this process. The browser hears nothing until
3
+ the CLI has traded the code, so what its page says is true. */
3
4
  import { createServer } from 'node:http';
4
- // The terminal trades the code for its session after this page has gone out, so the page claims nothing about it.
5
- const SIGNED_IN_PAGE = `<!doctype html><meta charset="utf-8"><title>Grayspar CLI</title>
6
- <body style="font:16px/1.5 system-ui;margin:4rem auto;max-width:28rem;text-align:center">
7
- <h1 style="font-size:1.25rem">Back to your terminal</h1>
8
- <p>It finishes signing in there. You can close this window.</p>`;
5
+ function browserPage(mark, markColor, title, detail) {
6
+ return `<!doctype html><meta charset="utf-8"><meta name="color-scheme" content="light dark">
7
+ <title>Grayspar CLI</title>
8
+ <style>body{font:16px/1.5 system-ui;margin:0;min-height:100svh;display:grid;place-content:center;text-align:center;background:light-dark(#fff,#0f1010);color:light-dark(#111,#eee)}
9
+ svg{display:block;width:48px;height:48px;margin:0 auto;color:${markColor}}h1{font-size:1.25rem;font-weight:600;margin:1rem 0 .25rem}p{margin:0;opacity:.7}</style>
10
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="10"/><path d="${mark}"/></svg>
11
+ <h1>${title}</h1><p>${detail}</p>`;
12
+ }
13
+ const SIGNED_IN_PAGE = browserPage('m8 12 3 3 5-6', '#2f9e5b', 'Signed in successfully', 'You can close this window.');
14
+ const NOT_SIGNED_IN_PAGE = browserPage('m9 9 6 6m0-6-6 6', '#d9534f', "The sign-in didn't complete", 'Run grayspar login again.');
9
15
  export async function waitForSignInRedirect(timeoutMs) {
10
16
  let settle = () => undefined;
11
17
  const waitForCode = new Promise((resolve) => {
12
18
  settle = resolve;
13
19
  });
20
+ let browser = null;
21
+ let redirected = false;
14
22
  const server = createServer((request, response) => {
15
23
  const url = new URL(request.url ?? '/', 'http://127.0.0.1');
16
- if (url.pathname !== '/callback') {
24
+ if (url.pathname !== '/callback' || redirected) {
17
25
  response.writeHead(404).end();
18
26
  return;
19
27
  }
20
- response.writeHead(200, { 'content-type': 'text/html; charset=utf-8' }).end(SIGNED_IN_PAGE);
28
+ redirected = true;
29
+ browser = response;
21
30
  settle({ code: url.searchParams.get('code') });
22
31
  });
23
32
  await new Promise((ready) => server.listen(0, '127.0.0.1', ready));
@@ -29,6 +38,16 @@ export async function waitForSignInRedirect(timeoutMs) {
29
38
  return {
30
39
  port: address.port,
31
40
  waitForCode,
41
+ answerBrowser: (signedIn) => new Promise((answered) => {
42
+ if (browser === null) {
43
+ answered();
44
+ return;
45
+ }
46
+ browser
47
+ .writeHead(signedIn ? 200 : 400, { 'content-type': 'text/html; charset=utf-8' })
48
+ .end(signedIn ? SIGNED_IN_PAGE : NOT_SIGNED_IN_PAGE, () => answered());
49
+ browser = null;
50
+ }),
32
51
  close: () => {
33
52
  clearTimeout(timer);
34
53
  server.close();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grayspar/cli",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "The cloud for small software. Deploy your applications with one command, and share them with exactly the people you choose.",
5
5
  "repository": {
6
6
  "type": "git",