pinokiod 3.261.0 → 3.263.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.
@@ -1,4 +1,7 @@
1
+ const fs = require('fs')
1
2
  const path = require('path')
3
+
4
+ const { detectCommandLineTools } = require('./xcode-tools')
2
5
  class Brew {
3
6
  description = "Wait for an install pop-up, then approve."
4
7
  async install(req, ondata) {
@@ -19,64 +22,24 @@ class Brew {
19
22
  await this.kernel.bin.rm("Homebrew.zip", ondata)
20
23
 
21
24
  if (this.kernel.platform === "darwin") {
22
- // command line tools
25
+ const checkingMsg = "> checking xcode command line tools...\r\n"
26
+ console.log(checkingMsg)
27
+ ondata({ raw: checkingMsg })
23
28
 
24
- // try to get the contents
25
- let result = await this.kernel.bin.exec({ message: "ls -m $(xcode-select -p)" }, (stream) => {
26
- ondata(stream)
29
+ const cltStatus = await detectCommandLineTools({
30
+ exec: (params) => this.kernel.bin.exec(params, () => {})
27
31
  })
28
- let e5 = result && result.stdout && /.*Library.*/g.test(result.stdout) && /.*SDKs.*/g.test(result.stdout) && /.*usr.*/g.test(result.stdout)
29
- if (e5) {
30
- const msg = "> xcode-select command line tools is installed. checking the version...\r\n"
32
+
33
+ if (cltStatus.valid) {
34
+ const msg = `> command line tools detected at ${cltStatus.path} (pkg ${cltStatus.pkgVersion}, xcode-select ${cltStatus.xcodeSelectVersion}). skipping...\r\n`
31
35
  console.log(msg)
32
36
  ondata({ raw: msg })
33
- // check the version.
34
- // if it's not valid, install the latest
35
- // if it's valid, skip
36
- let e4;
37
- let result = await this.kernel.bin.exec({ message: "xcode-select --version" }, (stream) => {
38
- ondata(stream)
39
- })
40
- if (result && result.stdout) {
41
- e4 = /xcode-select version ([0-9]+)/gi.exec(result.stdout)
42
- if (e4.length > 1) {
43
- let version = Number(e4[1])
44
- console.log("xcode-select version", version)
45
- if (version >= 2349) {
46
- e4 = true
47
- } else {
48
- e4 = false
49
- }
50
- } else {
51
- e4 = false
52
- }
53
- } else {
54
- e4 = false
55
- }
56
- console.log("> e4", e4)
57
-
58
-
59
- // valid version installed => skip
60
- if (e4) {
61
- const msg = "> a valid version command line tools already installed. skipping...\r\n"
62
- console.log(msg)
63
- ondata({ raw: msg })
64
- } else {
65
- const msg = "> valid version command line tools NOT installed.\r\n"
66
- console.log(msg)
67
- ondata({ raw: msg })
68
- await this._install(req, ondata)
69
- }
70
37
  } else {
71
- // not installed. install
72
- const msg = "> command line tools not installed yet. install the latest xcode build tools...\r\n"
38
+ const msg = `> ${cltStatus.reason || "command line tools not installed yet."} install the latest xcode build tools...\r\n`
73
39
  console.log(msg)
74
40
  ondata({ raw: msg })
75
41
  await this._install(req, ondata)
76
42
  }
77
-
78
- //ondata({ raw: "Setting CommandLineTools path...\r\n" })
79
- //await this.kernel.bin.exec({ sudo: true, message: "xcode-select -switch /Library/Developer/CommandLineTools" }, (stream) => { ondata(stream) })
80
43
  }
81
44
  //
82
45
  ondata({ raw: "installing gettext\r\n" })
@@ -21,6 +21,7 @@ const LLVM = require('./llvm')
21
21
  const VS = require("./vs")
22
22
  const Cuda = require("./cuda")
23
23
  const Torch = require("./torch")
24
+ const { detectCommandLineTools } = require('./xcode-tools')
24
25
  const { glob } = require('glob')
25
26
  const fakeUa = require('fake-useragent');
26
27
  const fse = require('fs-extra')
@@ -461,36 +462,11 @@ class Bin {
461
462
 
462
463
  // check brew_installed
463
464
  let e = await this.kernel.bin.exists("homebrew")
464
- let { stdout }= await this.exec({ message: "xcode-select -p", conda: { skip: true } }, (stream) => { })
465
- let e2 = /(.*Library.*Developer.*CommandLineTools.*|.*Xcode.*Developer.*)/gi.test(stdout)
466
- let e3 = await this.kernel.exists("/Library/Developer/CommandLineTools")
467
-
468
- // if xcode-select version exists
469
- // - if version is greater thatn 2349 => yes
470
- // - if version lower than 2349 => no
471
- // if xcode-select version doesn't match
472
- // - no
473
-
474
- let e4;
475
- let result = await this.exec({ message: "xcode-select --version", conda: { skip: true } }, (stream) => { })
476
- if (result && result.stdout) {
477
- e4 = /xcode-select version ([0-9]+)/gi.exec(result.stdout)
478
- if (e4 && e4.length > 1) {
479
- let version = Number(e4[1])
480
- // console.log("xcode-select version", version)
481
- if (version >= 2349) {
482
- e4 = true
483
- } else {
484
- e4 = false
485
- }
486
- } else {
487
- e4 = false
488
- }
489
- } else {
490
- e4 = false
491
- }
492
- console.log("BREW CHECK", { e, e2, e3, e4 })
493
- this.brew_installed = e && e2 && e3 && e4
465
+ const cltStatus = await detectCommandLineTools({
466
+ exec: (params) => this.exec(params, () => {})
467
+ })
468
+ console.log("BREW CHECK", { homebrew: e, cltStatus })
469
+ this.brew_installed = e && cltStatus.valid
494
470
 
495
471
  }
496
472
 
@@ -0,0 +1,136 @@
1
+ const fs = require('fs')
2
+ const path = require('path')
3
+
4
+ const MIN_XCODESELECT_VERSION = 2349
5
+ const REQUIRED_BINARIES = [
6
+ ["usr", "bin", "clang"],
7
+ ["usr", "bin", "git"]
8
+ ]
9
+ const CLT_PACKAGE_IDS = [
10
+ "com.apple.pkg.CLTools_Executables",
11
+ "com.apple.pkg.DeveloperToolsCLI"
12
+ ]
13
+
14
+ async function detectCommandLineTools({ exec }) {
15
+ if (typeof exec !== 'function') {
16
+ throw new Error('detectCommandLineTools requires an exec function')
17
+ }
18
+
19
+ const run = (message) => exec({ message, conda: { skip: true } })
20
+
21
+ const status = { valid: false }
22
+ let pathResult
23
+
24
+ try {
25
+ pathResult = await run('xcode-select -p')
26
+ } catch (err) {
27
+ status.reason = 'xcode-select -p failed'
28
+ return status
29
+ }
30
+
31
+ const developerPath = extractDeveloperPath(pathResult && pathResult.stdout)
32
+ if (!developerPath) {
33
+ status.rawPathOutput = pathResult ? pathResult.stdout : ''
34
+ status.reason = 'unable to parse developer path from xcode-select output'
35
+ return status
36
+ }
37
+ status.path = developerPath
38
+
39
+ try {
40
+ const stat = await fs.promises.stat(developerPath)
41
+ if (!stat.isDirectory()) {
42
+ status.reason = `${developerPath} is not a directory`
43
+ return status
44
+ }
45
+ } catch (err) {
46
+ status.reason = `developer path ${developerPath} is not accessible`
47
+ return status
48
+ }
49
+
50
+ try {
51
+ for (const rel of REQUIRED_BINARIES) {
52
+ const binaryPath = path.join(developerPath, ...rel)
53
+ await fs.promises.access(binaryPath, fs.constants.X_OK)
54
+ }
55
+ } catch (err) {
56
+ status.reason = 'required developer binaries are missing'
57
+ return status
58
+ }
59
+
60
+ const pkgInfo = await readCommandLineToolsPkgVersion(run)
61
+ if (!pkgInfo) {
62
+ status.reason = 'unable to read command line tools package info'
63
+ return status
64
+ }
65
+ status.pkgVersion = pkgInfo.version
66
+
67
+ const selectInfo = await readXcodeSelectVersion(run)
68
+ status.xcodeSelectVersion = selectInfo.version
69
+ if (!selectInfo.valid) {
70
+ status.reason = selectInfo.reason || 'xcode-select version below minimum'
71
+ return status
72
+ }
73
+
74
+ status.valid = true
75
+ return status
76
+ }
77
+
78
+ async function readCommandLineToolsPkgVersion(exec) {
79
+ for (const pkgId of CLT_PACKAGE_IDS) {
80
+ try {
81
+ const result = await exec(`pkgutil --pkg-info=${pkgId}`)
82
+ if (result && result.stdout) {
83
+ const match = /version:\s*([^\n]+)/i.exec(result.stdout)
84
+ if (match) {
85
+ return { pkgId, version: match[1].trim() }
86
+ }
87
+ }
88
+ } catch (err) {
89
+ // pkg not installed, try next id
90
+ }
91
+ }
92
+ return null
93
+ }
94
+
95
+ async function readXcodeSelectVersion(exec) {
96
+ let result
97
+ try {
98
+ result = await exec('xcode-select --version')
99
+ } catch (err) {
100
+ return { valid: false, reason: 'xcode-select --version failed' }
101
+ }
102
+
103
+ const match = result && result.stdout && /xcode-select version\s+(\d+)/i.exec(result.stdout)
104
+ if (!match) {
105
+ return { valid: false, reason: 'unable to parse xcode-select version' }
106
+ }
107
+
108
+ const numericVersion = Number(match[1])
109
+ return {
110
+ valid: numericVersion >= MIN_XCODESELECT_VERSION,
111
+ version: numericVersion
112
+ }
113
+ }
114
+
115
+ module.exports = {
116
+ detectCommandLineTools,
117
+ MIN_XCODESELECT_VERSION
118
+ }
119
+
120
+ function extractDeveloperPath(stdout) {
121
+ if (!stdout) {
122
+ return null
123
+ }
124
+
125
+ const lines = stdout.split(/\r?\n/)
126
+ for (const raw of lines) {
127
+ const line = raw.trim()
128
+ if (!line) {
129
+ continue
130
+ }
131
+ if (line.startsWith('/')) {
132
+ return line
133
+ }
134
+ }
135
+ return null
136
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pinokiod",
3
- "version": "3.261.0",
3
+ "version": "3.263.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -52,9 +52,25 @@ body.dark .tab-link-popover .tab-link-popover-header {
52
52
  background: transparent;
53
53
  cursor: pointer;
54
54
  }
55
- .tab-link-popover .tab-link-popover-item.qr-inline { flex-direction: row; align-items: center; gap: 10px; }
56
- .tab-link-popover .tab-link-popover-item.qr-inline .textcol { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1 1 auto; }
57
- .tab-link-popover .tab-link-popover-item.qr-inline .qr { width: 64px; height: 64px; image-rendering: pixelated; flex: 0 0 auto; margin-left: auto; }
55
+ .tab-link-popover .tab-link-popover-item.qr-inline {
56
+ flex-direction: row;
57
+ align-items: flex-start;
58
+ gap: 12px;
59
+ }
60
+ .tab-link-popover .tab-link-popover-item.qr-inline .textcol {
61
+ display: flex;
62
+ flex-direction: column;
63
+ gap: 2px;
64
+ min-width: 0;
65
+ flex: 1 1 auto;
66
+ }
67
+ .tab-link-popover .tab-link-popover-item.qr-inline .qr {
68
+ width: 128px;
69
+ height: 128px;
70
+ image-rendering: pixelated;
71
+ flex: 0 0 auto;
72
+ margin-left: auto;
73
+ }
58
74
  .tab-link-popover .tab-link-popover-item:hover,
59
75
  .tab-link-popover .tab-link-popover-item:focus-visible {
60
76
  background: rgba(15, 23, 42, 0.06);
@@ -272,8 +272,8 @@
272
272
  const parsed = new URL(value, location.origin)
273
273
  const host = parsed.host
274
274
  const pathname = parsed.pathname || "/"
275
- const search = parsed.search || ""
276
- return `${host}${pathname}${search}`
275
+ const hash = parsed.hash || ""
276
+ return `${host}${pathname}${hash}`
277
277
  } catch (_) {
278
278
  return value
279
279
  }
@@ -1237,22 +1237,32 @@
1237
1237
  popover.style.display = "flex"
1238
1238
  popover.classList.add("visible")
1239
1239
  popover.style.visibility = "hidden"
1240
+ popover.style.maxHeight = ""
1241
+ popover.style.overflowY = ""
1240
1242
 
1241
1243
  const popoverWidth = popover.offsetWidth
1242
- const popoverHeight = popover.offsetHeight
1244
+ let popoverHeight = popover.offsetHeight
1245
+ const viewportPadding = 12
1246
+ const availableHeight = Math.max(80, window.innerHeight - viewportPadding * 2)
1247
+
1248
+ if (popoverHeight > availableHeight) {
1249
+ popover.style.maxHeight = `${Math.round(availableHeight)}px`
1250
+ popover.style.overflowY = "auto"
1251
+ popoverHeight = Math.min(availableHeight, popover.offsetHeight)
1252
+ }
1243
1253
 
1244
1254
  let left = rect.left
1245
1255
  let top = rect.bottom + 8
1246
1256
 
1247
- if (left + popoverWidth > window.innerWidth - 12) {
1248
- left = window.innerWidth - popoverWidth - 12
1257
+ if (left + popoverWidth > window.innerWidth - viewportPadding) {
1258
+ left = window.innerWidth - popoverWidth - viewportPadding
1249
1259
  }
1250
- if (left < 12) {
1251
- left = 12
1260
+ if (left < viewportPadding) {
1261
+ left = viewportPadding
1252
1262
  }
1253
1263
 
1254
- if (top + popoverHeight > window.innerHeight - 12) {
1255
- top = Math.max(12, rect.top - popoverHeight - 8)
1264
+ if (top + popoverHeight > window.innerHeight - viewportPadding) {
1265
+ top = Math.max(viewportPadding, rect.top - popoverHeight - 8)
1256
1266
  }
1257
1267
 
1258
1268
  popover.style.left = `${Math.round(left)}px`
@@ -1521,6 +1531,7 @@
1521
1531
  const valueSpan = document.createElement("span")
1522
1532
  valueSpan.className = "value"
1523
1533
  valueSpan.textContent = entry.display
1534
+ valueSpan.title = entry.url
1524
1535
 
1525
1536
  if (entry.type === 'http' && entry.qr === true) {
1526
1537
  item.className = "tab-link-popover-item qr-inline"
@@ -1608,10 +1619,14 @@
1608
1619
  hideTabLinkPopover({ immediate: true })
1609
1620
  }
1610
1621
 
1611
- window.addEventListener("scroll", () => {
1612
- if (tabLinkPopoverEl && tabLinkPopoverEl.classList.contains("visible")) {
1613
- hideTabLinkPopover({ immediate: true })
1622
+ window.addEventListener("scroll", (event) => {
1623
+ if (!tabLinkPopoverEl || !tabLinkPopoverEl.classList.contains("visible")) {
1624
+ return
1625
+ }
1626
+ if (event && event.target && tabLinkPopoverEl.contains(event.target)) {
1627
+ return
1614
1628
  }
1629
+ hideTabLinkPopover({ immediate: true })
1615
1630
  }, true)
1616
1631
 
1617
1632
  window.addEventListener("resize", () => {
@@ -7,9 +7,6 @@
7
7
  <style>
8
8
  body {
9
9
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
10
- max-width: 600px;
11
- margin: 50px auto;
12
- padding: 20px;
13
10
  background: #f8fafc;
14
11
  }
15
12
  .container {
@@ -4,16 +4,20 @@
4
4
  <meta charset="UTF-8">
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
6
  <title><%=name%> OAuth</title>
7
+ <link href="/xterm.min.css" rel="stylesheet" />
7
8
  <link href="/css/fontawesome.min.css" rel="stylesheet">
8
9
  <link href="/css/solid.min.css" rel="stylesheet">
9
10
  <link href="/css/regular.min.css" rel="stylesheet">
10
11
  <link href="/css/brands.min.css" rel="stylesheet">
12
+ <link href="/markdown.css" rel="stylesheet"/>
13
+ <link href="/noty.css" rel="stylesheet"/>
14
+ <link href="/style.css" rel="stylesheet"/>
15
+ <% if (agent === "electron") { %>
16
+ <link href="/electron.css" rel="stylesheet"/>
17
+ <% } %>
11
18
  <style>
12
19
  body {
13
20
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
14
- max-width: 600px;
15
- margin: 50px auto;
16
- padding: 20px;
17
21
  background: #f8fafc;
18
22
  }
19
23
  /*
@@ -24,6 +28,14 @@
24
28
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
25
29
  }
26
30
  */
31
+ .container {
32
+ background: white;
33
+ max-width: 600px;
34
+ margin: 0 auto;
35
+ padding: 30px;
36
+ border-radius: 10px;
37
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
38
+ }
27
39
  h1 {
28
40
  text-transform: capitalize;
29
41
  color: #1f2937;
@@ -79,6 +91,47 @@
79
91
  .status.error { background: #fee2e2; color: #991b1b; }
80
92
  .status.warning { background: #fef3c7; color: #92400e; }
81
93
  .hidden { display: none; }
94
+ .loader-overlay {
95
+ position: fixed;
96
+ top: 0;
97
+ left: 0;
98
+ width: 100%;
99
+ height: 100%;
100
+ background: rgba(248, 250, 252, 0.95);
101
+ display: flex;
102
+ flex-direction: column;
103
+ align-items: center;
104
+ justify-content: center;
105
+ gap: 16px;
106
+ z-index: 999;
107
+ }
108
+ .loader-overlay.hidden {
109
+ display: none;
110
+ }
111
+ .loader-spinner {
112
+ width: 48px;
113
+ height: 48px;
114
+ border: 4px solid #e5e7eb;
115
+ border-top: 4px solid #ff6b35;
116
+ border-radius: 50%;
117
+ animation: spin 1s linear infinite;
118
+ }
119
+ @keyframes spin {
120
+ to { transform: rotate(360deg); }
121
+ }
122
+ .loader-message {
123
+ font-size: 16px;
124
+ color: #374151;
125
+ text-align: center;
126
+ }
127
+ .loader-cancel {
128
+ background: transparent;
129
+ border: 1px solid #6b7280;
130
+ color: #374151;
131
+ padding: 8px 16px;
132
+ border-radius: 6px;
133
+ cursor: pointer;
134
+ }
82
135
  .profile {
83
136
  width: 400px;
84
137
  display: flex;
@@ -96,9 +149,17 @@
96
149
  }
97
150
  header {
98
151
  text-align: center;
99
- padding: 50px;
100
152
  letter-spacing: -1px;
101
153
  }
154
+ header.head {
155
+ max-width: 600px;
156
+ margin: 0 auto;
157
+ padding: 50px;
158
+ text-align: center;
159
+ }
160
+ header.head h1 {
161
+ justify-content: center;
162
+ }
102
163
  .logos {
103
164
  display: flex;
104
165
  justify-content: center;
@@ -118,9 +179,40 @@
118
179
  font-size: 14px;
119
180
  }
120
181
  </style>
182
+ <script src="/popper.min.js"></script>
183
+ <script src="/tippy-bundle.umd.min.js"></script>
184
+ <script src="/hotkeys.min.js"></script>
185
+ <script src="/sweetalert2.js"></script>
186
+ <script src="/nav.js"></script>
121
187
  </head>
122
- <body>
123
- <header>
188
+ <body class='<%=theme%>' data-agent="<%=agent%>">
189
+ <header class="navheader grabbable">
190
+ <h1>
191
+ <a class="home" href="/home"><img class="icon" src="/pinokio-black.png"></a>
192
+ <button class="btn2" id="minimize-header" data-tippy-content="fullscreen" title="fullscreen">
193
+ <div><i class="fa-solid fa-expand"></i></div>
194
+ </button>
195
+ <button class="btn2" id="back" data-tippy-content="back"><div><i class="fa-solid fa-chevron-left"></i></div></button>
196
+ <button class="btn2" id="forward" data-tippy-content="forward"><div><i class="fa-solid fa-chevron-right"></i></div></button>
197
+ <button class="btn2" id="refresh-page" data-tippy-content="refresh"><div><i class="fa-solid fa-rotate-right"></i></div></button>
198
+ <button class="btn2" id="screenshot" data-tippy-content="screen capture"><i class="fa-solid fa-camera"></i></button>
199
+ <button class="btn2" id="inspector" data-tippy-content="X-ray mode"><i class="fa-solid fa-eye"></i></button>
200
+ <div class="flexible"></div>
201
+ <a class="btn2" href="/columns" data-tippy-content="split into 2 columns">
202
+ <div><i class="fa-solid fa-table-columns"></i></div>
203
+ </a>
204
+ <a class="btn2" href="/rows" data-tippy-content="split into 2 rows">
205
+ <div><i class="fa-solid fa-table-columns fa-rotate-270"></i></div>
206
+ </a>
207
+ <button class="btn2" id="new-window" data-tippy-content="open a new window" title="open a new window" data-agent="web">
208
+ <div><i class="fa-solid fa-plus"></i></div>
209
+ </button>
210
+ <button class="btn2 hidden" id="close-window" data-tippy-content="close this section">
211
+ <div><i class="fa-solid fa-xmark"></i></div>
212
+ </button>
213
+ </h1>
214
+ </header>
215
+ <header class='head'>
124
216
  <div class='logos'>
125
217
  <img class='logo' src="/pinokio-black.png"/>
126
218
  <div>+</div>
@@ -135,33 +227,228 @@
135
227
  <h1><%=name%> Connect</h1>
136
228
  </header>
137
229
  <div class="container">
138
- <% if (protocol === "https") { %>
139
- <div id="status" class="status warning">
140
- Checking authentication status...
141
- </div>
142
-
143
- <!-- Login Section -->
144
- <div id="login-section">
145
- <p>Click below to authenticate with <%=name%>:</p>
146
- <button class="btn" onclick="login()">Login</button>
147
- </div>
148
-
149
- <!-- User Section -->
150
- <div id="user-section" class="hidden">
151
- <div class="user-info">
152
- <h3>Logged In</h3>
153
- <div id="user-details"></div>
154
- <button class="btn secondary" onclick="logout()">Logout</button>
155
- </div>
156
- </div>
157
- <% } else { %>
158
- <a class='btn' href="https://pinokio.localhost/connect/<%=name%>">Get started<a>
159
- <% } %>
160
-
230
+ <div id="status" class="status hidden">
231
+ </div>
232
+
233
+ <div id="login-section" class="hidden">
234
+ <p>Click below to authenticate with <%=name%>:</p>
235
+ <button class="btn" id="login-button">Login</button>
236
+ </div>
237
+
238
+ <div id="http-section" class="hidden">
239
+ <p>Click below to open the secure connection flow:</p>
240
+ <button class='btn' id="get-started-button">Get started</button>
241
+ </div>
242
+
243
+ <div id="user-section" class="hidden">
244
+ <div class="user-info">
245
+ <h3>Logged In</h3>
246
+ <div id="user-details"></div>
247
+ <button class="btn secondary" onclick="logout()">Logout</button>
248
+ </div>
249
+ </div>
250
+
251
+ </div>
252
+
253
+ <div id="connect-loader" class="loader-overlay hidden">
254
+ <div class="loader-spinner"></div>
255
+ <div id="loader-message" class="loader-message">Connecting...</div>
256
+ <button id="loader-cancel" class="loader-cancel hidden">Cancel</button>
161
257
  </div>
162
258
 
163
- <% if (protocol === "https") { %>
164
259
  <script>
260
+ const CONNECT_NAME = "<%=name%>"
261
+ const isSecureConnectContext = window.location.protocol === 'https:' && window.location.hostname === 'pinokio.localhost'
262
+ const statusElement = document.getElementById('status')
263
+ const loaderElement = document.getElementById('connect-loader')
264
+ const loaderMessageElement = document.getElementById('loader-message')
265
+ const loaderCancelButton = document.getElementById('loader-cancel')
266
+ const loginSection = document.getElementById('login-section')
267
+ const loginButton = document.getElementById('login-button')
268
+ const httpSection = document.getElementById('http-section')
269
+ const userSection = document.getElementById('user-section')
270
+ const userDetailsElement = document.getElementById('user-details')
271
+ const CONNECT_PROFILE_URL = `/connect/${CONNECT_NAME}/profile`
272
+ const CONNECT_LOGOUT_URL = `/connect/${CONNECT_NAME}/logout`
273
+ let loaderCancelHandler = null
274
+
275
+ function setStatus(message, type = 'warning') {
276
+ if (!statusElement) return
277
+ if (type === 'hidden') {
278
+ statusElement.textContent = message || ''
279
+ statusElement.className = 'status hidden'
280
+ return
281
+ }
282
+ statusElement.textContent = message
283
+ statusElement.className = `status ${type}`
284
+ statusElement.classList.remove('hidden')
285
+ }
286
+
287
+ function showLoader(message = 'Connecting...', options = {}) {
288
+ if (!loaderElement) return
289
+ loaderElement.classList.remove('hidden')
290
+ if (loaderMessageElement) {
291
+ loaderMessageElement.textContent = message
292
+ }
293
+ if (loaderCancelButton) {
294
+ if (options.cancellable) {
295
+ loaderCancelHandler = typeof options.onCancel === 'function' ? options.onCancel : null
296
+ loaderCancelButton.classList.remove('hidden')
297
+ loaderCancelButton.onclick = () => {
298
+ if (loaderCancelHandler) {
299
+ loaderCancelHandler()
300
+ } else {
301
+ hideLoader()
302
+ }
303
+ }
304
+ } else {
305
+ loaderCancelButton.classList.add('hidden')
306
+ loaderCancelButton.onclick = null
307
+ loaderCancelHandler = null
308
+ }
309
+ }
310
+ }
311
+
312
+ function hideLoader() {
313
+ if (!loaderElement) return
314
+ loaderElement.classList.add('hidden')
315
+ if (loaderCancelButton) {
316
+ loaderCancelButton.classList.add('hidden')
317
+ loaderCancelButton.onclick = null
318
+ }
319
+ loaderCancelHandler = null
320
+ }
321
+
322
+ function configureInitialView() {
323
+ if (isSecureConnectContext) {
324
+ if (loginSection) {
325
+ loginSection.classList.remove('hidden')
326
+ }
327
+ if (httpSection) {
328
+ httpSection.classList.add('hidden')
329
+ }
330
+ setStatus('Checking authentication status...', 'warning')
331
+ } else {
332
+ if (loginSection) {
333
+ loginSection.classList.add('hidden')
334
+ }
335
+ if (httpSection) {
336
+ httpSection.classList.remove('hidden')
337
+ }
338
+ setStatus('', 'hidden')
339
+ }
340
+ }
341
+
342
+ async function ensureValidToken() {
343
+ try {
344
+ const res = await fetch(`/connect/${CONNECT_NAME}/keys`, {
345
+ method: 'POST',
346
+ headers: { 'Content-Type': 'application/json' },
347
+ body: JSON.stringify({})
348
+ })
349
+ const json = await res.json()
350
+ if (json && json.access_token && !json.error) {
351
+ return json.access_token
352
+ }
353
+ return null
354
+ } catch (err) {
355
+ console.error('Failed to check connect status', err)
356
+ return null
357
+ }
358
+ }
359
+
360
+ async function fetchUserInfo(existingToken) {
361
+ try {
362
+ const token = existingToken || await ensureValidToken()
363
+ if (!token) {
364
+ return false
365
+ }
366
+ await displayUserInfo()
367
+ return true
368
+ } catch (err) {
369
+ console.error('Failed to fetch user info', err)
370
+ setStatus('Failed to fetch user info: ' + err.message, 'error')
371
+ hideLoader()
372
+ return false
373
+ }
374
+ }
375
+
376
+ async function displayUserInfo() {
377
+ if (!userSection || !userDetailsElement) {
378
+ return
379
+ }
380
+ const res = await fetch(CONNECT_PROFILE_URL)
381
+ if (!res.ok) {
382
+ throw new Error('Profile fetch failed')
383
+ }
384
+ const profile = await res.json()
385
+ const rows = profile.items.map((row) => {
386
+ return `<tr><td>${row.key}</td><td>${row.val}</td></tr>`
387
+ }).join("")
388
+ userDetailsElement.innerHTML = `<div class="profile">
389
+ <img src="${profile.image}">
390
+ <div class='profile-column'>
391
+ <table>${rows}</table>
392
+ </div>
393
+ </div>`
394
+ if (loginSection) {
395
+ loginSection.classList.add('hidden')
396
+ }
397
+ if (httpSection) {
398
+ httpSection.classList.add('hidden')
399
+ }
400
+ userSection.classList.remove('hidden')
401
+ setStatus('', 'hidden')
402
+ hideLoader()
403
+ }
404
+
405
+ async function logout() {
406
+ try {
407
+ showLoader('Disconnecting...', { cancellable: false })
408
+ const res = await fetch(CONNECT_LOGOUT_URL, {
409
+ method: 'POST',
410
+ headers: { 'Content-Type': 'application/json' },
411
+ body: JSON.stringify({})
412
+ })
413
+ await res.json()
414
+ setStatus('Logged out', 'warning')
415
+ } catch (err) {
416
+ console.error('Failed to logout', err)
417
+ setStatus('Failed to logout: ' + err.message, 'error')
418
+ } finally {
419
+ hideLoader()
420
+ if (loginSection) {
421
+ loginSection.classList.remove('hidden')
422
+ }
423
+ if (httpSection) {
424
+ httpSection.classList.remove('hidden')
425
+ }
426
+ if (userSection) {
427
+ userSection.classList.add('hidden')
428
+ }
429
+ }
430
+ }
431
+
432
+ configureInitialView()
433
+
434
+ if (loginButton) {
435
+ loginButton.addEventListener('click', () => {
436
+ if (typeof window.startOAuthLogin === 'function') {
437
+ window.startOAuthLogin()
438
+ }
439
+ })
440
+ }
441
+
442
+ window.ensureValidToken = ensureValidToken
443
+ window.fetchUserInfo = fetchUserInfo
444
+ window.logout = logout
445
+ window.startOAuthLogin = () => {
446
+ console.warn('Login flow not available in this context')
447
+ }
448
+ </script>
449
+
450
+ <script>
451
+ if (isSecureConnectContext) {
165
452
 
166
453
  // Configuration
167
454
 
@@ -173,13 +460,6 @@
173
460
  const SCOPE = "<%=config.SCOPE%>"
174
461
  const name = "<%=name%>"
175
462
 
176
- // Utility functions
177
- function setStatus(message, type) {
178
- const status = document.getElementById('status');
179
- status.textContent = message;
180
- status.className = `status ${type}`;
181
- }
182
-
183
463
  function generateRandomString(length) {
184
464
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
185
465
  let result = '';
@@ -210,25 +490,11 @@
210
490
  .replace(/=/g, '');
211
491
  }
212
492
 
213
- // Token management with automatic refresh
214
- async function ensureValidToken() {
215
- const res = await fetch(`/connect/${name}/keys`, {
216
- method: 'POST',
217
- headers: { 'Content-Type': 'application/json' },
218
- body: JSON.stringify({})
219
- });
220
- const json = await res.json();
221
- console.log({ json })
222
- if (json && json.access_token) {
223
- return json.access_token
224
- } else {
225
- return null
226
- }
227
- }
228
-
229
493
  // OAuth functions
230
494
  async function login() {
231
495
  try {
496
+ showLoader('Connecting...');
497
+ setStatus(`Redirecting to ${name}...`, 'warning');
232
498
  // Clear existing data
233
499
  localStorage.removeItem('oauth_state');
234
500
  localStorage.removeItem('code_verifier');
@@ -260,11 +526,13 @@
260
526
 
261
527
  } catch (error) {
262
528
  console.error('Login error:', error);
529
+ hideLoader();
263
530
  setStatus('Failed to start login', 'error');
264
531
  }
265
532
  }
266
533
 
267
534
  async function handleOAuthCallback(code) {
535
+ showLoader('Finishing connection...');
268
536
  try {
269
537
  // Verify state
270
538
  const urlParams = new URLSearchParams(window.location.search);
@@ -303,63 +571,19 @@
303
571
 
304
572
  // Get user info
305
573
  await fetchUserInfo();
574
+ setStatus('Connection complete. Refreshing...', 'success');
575
+ window.location.reload();
306
576
  } else {
307
577
  throw new Error('No access token received');
308
578
  }
309
579
 
310
580
  } catch (error) {
311
581
  console.error('OAuth callback error:', error);
582
+ hideLoader();
312
583
  setStatus('Authentication failed: ' + error.message, 'error');
313
584
  }
314
585
  }
315
586
 
316
- async function fetchUserInfo() {
317
- try {
318
- // Use ensureValidToken to automatically refresh if needed
319
- const token = await ensureValidToken();
320
- if (!token) {
321
- throw new Error('No valid token available');
322
- }
323
- console.log({ token })
324
- await displayUserInfo()
325
- } catch (error) {
326
- console.error('Error fetching user info:', error);
327
- setStatus('Failed to fetch user info: ' + error.message, 'error');
328
- // logout();
329
- }
330
- }
331
-
332
- async function displayUserInfo() {
333
- const res = await fetch(`/connect/${name}/profile`)
334
- const profile = await res.json();
335
- const userDetails = document.getElementById('user-details');
336
- let rows = profile.items.map((row) => {
337
- return `<tr><td>${row.key}</td><td>${row.val}</td></tr>`
338
- }).join("")
339
- userDetails.innerHTML = `<div class="profile">
340
- <img src="${profile.image}">
341
- <div class='profile-column'>
342
- <table>${rows}</table>
343
- </div>
344
- </div>`
345
- document.getElementById('login-section').className = 'hidden';
346
- document.getElementById('user-section').className = '';
347
- setStatus("", "hidden")
348
- }
349
-
350
- async function logout() {
351
- document.getElementById('login-section').className = '';
352
- document.getElementById('user-section').className = 'hidden';
353
- setStatus('Logged out', 'warning');
354
- const res = await fetch('/connect/<%=name%>/logout', {
355
- method: 'POST',
356
- headers: { 'Content-Type': 'application/json' },
357
- body: JSON.stringify({})
358
- });
359
- const json = await res.json();
360
- location.href = location.href
361
- }
362
-
363
587
  // Utility function for making authenticated API calls with automatic refresh
364
588
  async function makeAuthenticatedRequest(url, options = {}) {
365
589
  const token = await ensureValidToken();
@@ -395,7 +619,7 @@
395
619
  // Check existing session with automatic refresh
396
620
  const token = await ensureValidToken();
397
621
  if (token) {
398
- await fetchUserInfo();
622
+ await fetchUserInfo(token);
399
623
  } else {
400
624
  setStatus('Not authenticated', 'warning');
401
625
  }
@@ -404,7 +628,80 @@
404
628
  // Export makeAuthenticatedRequest for external use
405
629
  window.makeAuthenticatedRequest = makeAuthenticatedRequest;
406
630
  window.ensureValidToken = ensureValidToken;
631
+ window.startOAuthLogin = login;
632
+ }
633
+ </script>
634
+
635
+ <script>
636
+ if (!isSecureConnectContext) {
637
+ const SECURE_CONNECT_URL = 'https://pinokio.localhost/connect/<%=name%>'
638
+ const CONNECT_POLL_INTERVAL = 4000
639
+ const CONNECT_TIMEOUT = 120000
640
+ let connectPollTimer = null
641
+ let connectTimeoutHandle = null
642
+
643
+ function stopConnectPolling(message, type = 'warning') {
644
+ if (connectPollTimer) {
645
+ clearInterval(connectPollTimer)
646
+ connectPollTimer = null
647
+ }
648
+ if (connectTimeoutHandle) {
649
+ clearTimeout(connectTimeoutHandle)
650
+ connectTimeoutHandle = null
651
+ }
652
+ hideLoader()
653
+ if (message) {
654
+ setStatus(message, type)
655
+ }
656
+ }
657
+
658
+ async function pollConnectStatus() {
659
+ const token = await ensureValidToken()
660
+ if (token) {
661
+ if (connectPollTimer) {
662
+ clearInterval(connectPollTimer)
663
+ connectPollTimer = null
664
+ }
665
+ if (connectTimeoutHandle) {
666
+ clearTimeout(connectTimeoutHandle)
667
+ connectTimeoutHandle = null
668
+ }
669
+ await fetchUserInfo(token)
670
+ setStatus('Connected.', 'success')
671
+ }
672
+ }
673
+
674
+ function startHttpConnect() {
675
+ stopConnectPolling()
676
+ showLoader('Connecting... Complete the login in the secure window.', {
677
+ cancellable: true,
678
+ onCancel: () => stopConnectPolling('Connection cancelled.', 'warning')
679
+ })
680
+ setStatus('Waiting for connection...', 'warning')
681
+ const secureWindow = window.open(SECURE_CONNECT_URL, '_blank')
682
+ if (!secureWindow) {
683
+ window.location.href = SECURE_CONNECT_URL
684
+ return
685
+ }
686
+ pollConnectStatus()
687
+ connectPollTimer = setInterval(pollConnectStatus, CONNECT_POLL_INTERVAL)
688
+ connectTimeoutHandle = setTimeout(() => {
689
+ stopConnectPolling('Timed out waiting for connection. Please try again.', 'error')
690
+ }, CONNECT_TIMEOUT)
691
+ }
692
+
693
+ const getStartedButton = document.getElementById('get-started-button')
694
+ if (getStartedButton) {
695
+ getStartedButton.addEventListener('click', startHttpConnect)
696
+ }
697
+
698
+ window.addEventListener('load', async () => {
699
+ const token = await ensureValidToken()
700
+ if (token) {
701
+ await fetchUserInfo(token)
702
+ }
703
+ })
704
+ }
407
705
  </script>
408
- <% } %>
409
706
  </body>
410
707
  </html>
@@ -856,7 +856,7 @@ document.addEventListener('DOMContentLoaded', function() {
856
856
  <div class='tab-content'>
857
857
  <% items.forEach((item) => { %>
858
858
  <% if (item.profile) { %>
859
- <a href="<%=item.url%>" class="tab connected" target="_blank">
859
+ <a href="<%=item.url%>" class="tab connected">
860
860
  <div class='tab'>
861
861
  <% if (item.image) { %>
862
862
  <img class="app-icon" src="<%=item.image%>"/>
@@ -887,7 +887,7 @@ document.addEventListener('DOMContentLoaded', function() {
887
887
  </div>
888
888
  </a>
889
889
  <% } else { %>
890
- <a href="<%=item.url%>" class="tab" target="_blank">
890
+ <a href="<%=item.url%>" class="tab">
891
891
  <% if (item.image) { %>
892
892
  <img class="icon" src="<%=item.image%>"/>
893
893
  <% } else if (item.icon) { %>