toon-memory 3.7.0 → 4.1.1

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.
@@ -18,8 +18,22 @@ function openBrowser(url: string): void {
18
18
  }
19
19
  }
20
20
 
21
+ function resolvePort(portArg?: string): number {
22
+ const raw = portArg ?? process.env.PORT
23
+ if (raw === undefined || raw === "") return 3000
24
+ const port = Number(raw)
25
+ if (!Number.isInteger(port) || port < 1 || port > 65535) {
26
+ console.error(`Invalid port: '${raw}'. Expected an integer between 1 and 65535.`)
27
+ process.exit(1)
28
+ }
29
+ return port
30
+ }
31
+
32
+ const MAX_PORT_ATTEMPTS = 20
33
+
21
34
  export function viewer(portArg?: string): void {
22
- const PORT = parseInt(portArg || process.env.PORT || "3000", 10)
35
+ const startPort = resolvePort(portArg)
36
+ let port = startPort
23
37
  let currentHtml = generateHtml(buildViewerData())
24
38
 
25
39
  const server = createServer((_req: IncomingMessage, res: ServerResponse) => {
@@ -27,15 +41,31 @@ export function viewer(portArg?: string): void {
27
41
  res.end(currentHtml)
28
42
  })
29
43
 
30
- server.listen(PORT, "127.0.0.1", () => {
31
- const url = `http://127.0.0.1:${PORT}`
44
+ server.on("error", (err: NodeJS.ErrnoException) => {
45
+ if (err.code === "EADDRINUSE" && port < startPort + MAX_PORT_ATTEMPTS) {
46
+ port += 1
47
+ console.log(`Port ${port - 1} in use, trying ${port}...`)
48
+ server.listen(port, "127.0.0.1", onListen)
49
+ } else if (err.code === "EADDRINUSE") {
50
+ console.error(`Port ${port} already in use. Try: toon-memory viewer --port ${port + 1} (or PORT=${port + 1} toon-memory viewer)`)
51
+ process.exit(1)
52
+ } else {
53
+ console.error(`Server error: ${err.message}`)
54
+ process.exit(1)
55
+ }
56
+ })
57
+
58
+ function onListen(): void {
59
+ const url = `http://127.0.0.1:${port}`
32
60
  const data = buildViewerData()
33
61
  console.log(`\ntoon-memory viewer`)
34
62
  console.log(` Entries: ${data.totalEntries} | Edges: ${data.edges.length}`)
35
63
  console.log(` Serving at: ${url}`)
36
64
  console.log(` Press 'r' in terminal to reload | Ctrl+C to stop\n`)
37
65
  openBrowser(url)
38
- })
66
+ }
67
+
68
+ server.listen(startPort, "127.0.0.1", onListen)
39
69
 
40
70
  if (process.stdin.isTTY) {
41
71
  process.stdin.setRawMode?.(true)
@@ -50,15 +80,6 @@ export function viewer(portArg?: string): void {
50
80
  if (key === "\u0003") process.exit(0)
51
81
  })
52
82
  }
53
-
54
- server.on("error", (err: NodeJS.ErrnoException) => {
55
- if (err.code === "EADDRINUSE") {
56
- console.error(`Port ${PORT} already in use. Try: PORT=3001 toon-memory viewer`)
57
- } else {
58
- console.error(`Server error: ${err.message}`)
59
- }
60
- process.exit(1)
61
- })
62
83
  }
63
84
 
64
85
  export function viewerExport(): void {