@sentio/cli 4.4.0-rc.1 → 4.4.0-rc.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.
package/dist/index.js CHANGED
@@ -46183,10 +46183,18 @@ function isAccessTokenExpired(expiresAt) {
46183
46183
  }
46184
46184
  //#endregion
46185
46185
  //#region src/commands/login-server.ts
46186
+ function loginResultPage(message) {
46187
+ return `<!doctype html>
46188
+ <html lang="en">
46189
+ <head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Sentio CLI</title></head>
46190
+ <body><p>${message}</p></body>
46191
+ </html>`;
46192
+ }
46186
46193
  function startServer(params) {
46187
46194
  return new Promise((resolve, reject) => {
46188
46195
  const app = (0, import_express.default)();
46189
- let redeeming = false;
46196
+ let redemption;
46197
+ let responses = 0;
46190
46198
  let settled = false;
46191
46199
  const controller = new AbortController();
46192
46200
  const finish = (error) => {
@@ -46205,26 +46213,31 @@ function startServer(params) {
46205
46213
  process.once("SIGINT", interrupted);
46206
46214
  app.get("/callback", async (req, res) => {
46207
46215
  res.setHeader("Cache-Control", "no-store");
46216
+ res.type("html");
46208
46217
  if (typeof req.query.state !== "string" || req.query.state !== params.state) {
46209
- res.status(400).end("Invalid authorization state");
46210
- return;
46211
- }
46212
- if (redeeming) {
46213
- res.status(409).end("Authorization already in progress");
46218
+ res.status(400).end(loginResultPage("Invalid authorization state"));
46214
46219
  return;
46215
46220
  }
46216
- redeeming = true;
46221
+ responses++;
46217
46222
  const respond = (message, error) => {
46218
- const complete = () => finish(error);
46223
+ let completed = false;
46224
+ const complete = () => {
46225
+ if (completed) return;
46226
+ completed = true;
46227
+ if (--responses === 0) finish(error);
46228
+ };
46219
46229
  if (res.destroyed) return complete();
46220
46230
  res.once("close", complete);
46221
- res.status(error ? 400 : 200).end(message, complete);
46231
+ res.status(error ? 400 : 200).end(loginResultPage(message), complete);
46222
46232
  };
46223
46233
  try {
46224
46234
  if (req.query.error) throw new Error("Authorization was not completed");
46225
46235
  if (typeof req.query.code !== "string" || !req.query.code) throw new Error("Missing authorization code");
46226
- const username = await exchangeCodeAndSave(getFinalizedHost(params.sentioHost), req.query.code, params.codeVerifier, params.authConfig, controller.signal);
46227
- console.log(chalk.green(`Login success with ${username}`));
46236
+ redemption ??= exchangeCodeAndSave(getFinalizedHost(params.sentioHost), req.query.code, params.codeVerifier, params.authConfig, controller.signal).then((username) => {
46237
+ console.log(chalk.green(`Login success with ${username}`));
46238
+ return username;
46239
+ });
46240
+ await redemption;
46228
46241
  respond("Login success, please go back to CLI to continue");
46229
46242
  } catch (error) {
46230
46243
  respond("Login failed. Check the terminal and try again.", error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sentio/cli",
3
- "version": "4.4.0-rc.1",
3
+ "version": "4.4.0-rc.3",
4
4
  "license": "Apache-2.0",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,10 +17,20 @@ interface AuthParams {
17
17
  timeoutMs?: number
18
18
  }
19
19
 
20
+ // Only fixed messages are rendered; OAuth parameters never appear in the page.
21
+ function loginResultPage(message: string): string {
22
+ return `<!doctype html>
23
+ <html lang="en">
24
+ <head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Sentio CLI</title></head>
25
+ <body><p>${message}</p></body>
26
+ </html>`
27
+ }
28
+
20
29
  export function startServer(params: AuthParams): Promise<void> {
21
30
  return new Promise((resolve, reject) => {
22
31
  const app = express()
23
- let redeeming = false
32
+ let redemption: Promise<string> | undefined
33
+ let responses = 0
24
34
  let settled = false
25
35
  const controller = new AbortController()
26
36
  const finish = (error?: Error) => {
@@ -42,34 +52,39 @@ export function startServer(params: AuthParams): Promise<void> {
42
52
  process.once('SIGINT', interrupted)
43
53
  app.get('/callback', async (req, res) => {
44
54
  res.setHeader('Cache-Control', 'no-store')
55
+ res.type('html')
45
56
  if (typeof req.query.state !== 'string' || req.query.state !== params.state) {
46
- res.status(400).end('Invalid authorization state')
47
- return
48
- }
49
- if (redeeming) {
50
- res.status(409).end('Authorization already in progress')
57
+ res.status(400).end(loginResultPage('Invalid authorization state'))
51
58
  return
52
59
  }
53
- redeeming = true
60
+ responses++
54
61
  const respond = (message: string, error?: Error) => {
55
- // A browser can close the tab while the token request is still running.
56
- // Completing login must not depend on delivering a response to that tab.
57
- const complete = () => finish(error)
62
+ // Browsers may interrupt and retry the navigation during redemption.
63
+ // Flush every waiting response before closing the shared listener.
64
+ let completed = false
65
+ const complete = () => {
66
+ if (completed) return
67
+ completed = true
68
+ if (--responses === 0) finish(error)
69
+ }
58
70
  if (res.destroyed) return complete()
59
71
  res.once('close', complete)
60
- res.status(error ? 400 : 200).end(message, complete)
72
+ res.status(error ? 400 : 200).end(loginResultPage(message), complete)
61
73
  }
62
74
  try {
63
75
  if (req.query.error) throw new Error('Authorization was not completed')
64
76
  if (typeof req.query.code !== 'string' || !req.query.code) throw new Error('Missing authorization code')
65
- const username = await exchangeCodeAndSave(
77
+ redemption ??= exchangeCodeAndSave(
66
78
  getFinalizedHost(params.sentioHost),
67
79
  req.query.code,
68
80
  params.codeVerifier,
69
81
  params.authConfig,
70
82
  controller.signal
71
- )
72
- console.log(chalk.green(`Login success with ${username}`))
83
+ ).then((username) => {
84
+ console.log(chalk.green(`Login success with ${username}`))
85
+ return username
86
+ })
87
+ await redemption
73
88
  respond('Login success, please go back to CLI to continue')
74
89
  } catch (error) {
75
90
  respond('Login failed. Check the terminal and try again.', error as Error)