Skip to content

Commit ce3b829

Browse files
committedMay 4, 2024·
Many fixes
1 parent a9dd365 commit ce3b829

File tree

8 files changed

+98
-74
lines changed

8 files changed

+98
-74
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ _For detailed documentation, visit [pup.56k.guru](https://pup.56k.guru)._
2828
To install Pup, make sure you run the latest version of Deno (`deno upgrade`), then open your terminal and execute the following command:
2929

3030
```bash
31-
deno run -Ar jsr:@pup/pup@1.0.0-rc.37 setup --channel prerelease
31+
deno run -Ar jsr:@pup/pup@1.0.0-rc.39 setup --channel prerelease
3232
```
3333

3434
This command downloads the latest version of Pup and installs it on your system. The `--channel prerelease` option is included as there is no stable version of Pup yet. Read more abour release

‎application.meta.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
const Application = {
2323
name: "pup",
24-
version: "1.0.0-rc.38",
24+
version: "1.0.0-rc.39",
2525
url: "jsr:@pup/pup@$VERSION",
2626
canary_url: "https://raw.githubusercontent.com/Hexagon/pup/main/pup.ts",
2727
deno: null, /* Minimum stable version of Deno required to run Pup (without --unstable-* flags) */

‎deno.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pup/pup",
3-
"version": "1.0.0-rc.38",
3+
"version": "1.0.0-rc.39",
44

55
"exports": {
66
".": "./pup.ts",
@@ -41,14 +41,14 @@
4141
"@cross/deepmerge": "jsr:@cross/deepmerge@^1.0.0",
4242
"@cross/env": "jsr:@cross/env@^1.0.2",
4343
"@cross/fs": "jsr:@cross/fs@^0.1.11",
44-
"@cross/jwt": "jsr:@cross/jwt@^0.4.6",
44+
"@cross/jwt": "jsr:@cross/jwt@^0.4.7",
4545
"@cross/runtime": "jsr:@cross/runtime@^1.0.0",
4646
"@cross/service": "jsr:@cross/service@^1.0.3",
4747
"@cross/test": "jsr:@cross/test@^0.0.9",
4848
"@cross/utils": "jsr:@cross/utils@^0.12.0",
4949
"@hexagon/croner": "jsr:@hexagon/croner@^8.0.2",
5050
"@oak/oak": "jsr:@oak/oak@^15.0.0",
51-
"@pup/api-client": "jsr:@pup/api-client@^1.0.4",
51+
"@pup/api-client": "jsr:@pup/api-client@^1.0.6",
5252
"@pup/api-definitions": "jsr:@pup/api-definitions@^1.0.2",
5353
"@pup/common": "jsr:@pup/common@^1.0.3",
5454
"@pup/plugin": "jsr:@pup/plugin@^1.0.1",
@@ -58,11 +58,11 @@
5858
"@std/io": "jsr:@std/io@^0.224.0",
5959
"@std/path": "jsr:@std/path@^0.224.0",
6060
"@std/semver": "jsr:@std/semver@^0.224.0",
61-
"dax-sh": "npm:dax-sh@^0.40.0",
61+
"dax-sh": "npm:dax-sh@^0.41.0",
6262
"filesize": "npm:filesize@^10.1.1",
6363
"json5": "npm:json5@^2.2.3",
6464
"timeago.js": "npm:timeago.js@^4.0.2",
65-
"zod": "npm:zod@^3.22.4",
66-
"zod-to-json-schema": "npm:zod-to-json-schema@^3.22.5"
65+
"zod": "npm:zod@^3.23.6",
66+
"zod-to-json-schema": "npm:zod-to-json-schema@^3.23.0"
6767
}
6868
}

‎docs/src/_data.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"description": "Universal Process Manager"
77
},
88
"substitute": {
9-
"$PUP_VERSION": "1.0.0-rc.38"
9+
"$PUP_VERSION": "1.0.0-rc.39"
1010
},
1111
"top_links": [
1212
{

‎docs/src/changelog.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@ nav_order: 13
99

1010
All notable changes to this project will be documented in this section.
1111

12-
## [1.0.0-rc.39] - Unstable
12+
## [1.0.0-rc.39] - 2024-05-04
1313

1414
- fix(core): Clustered processes were duplicated in API and `pup status`
15+
- fix(core): Fix regression with `pup monitor`
16+
- fix(core): Add API connection timeout
17+
- chore(deps): Full dependency update
18+
- fix(api): Log host and port when starting the Rest API Server
19+
- fix(api): Clarify error messages
20+
- fix(api): Remove unused (commented out) code
21+
- fix(cli): Require `--expire-in` when generating tokens on the CLI
1522

1623
## [1.0.0-rc.38] - 2024-05-01
1724

‎lib/cli/main.ts

+64-50
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import { PupRestClient } from "@pup/api-client"
3535
import { CurrentRuntime, Runtime } from "@cross/runtime"
3636
import { Prop } from "../common/prop.ts"
3737
import { encodeBase64 } from "@std/encoding/base64"
38+
import { ApiLogItem } from "@pup/api-definitions"
39+
import { EventHandler } from "@pup/common/eventemitter"
3840

3941
/**
4042
* Define the main entry point of the CLI application
@@ -222,7 +224,7 @@ async function main() {
222224

223225
// Send api request
224226
const apiBaseUrl = `http://${configuration.api?.hostname || DEFAULT_REST_API_HOSTNAME}:${port}`
225-
client = new PupRestClient(apiBaseUrl, token!)
227+
client = new PupRestClient(apiBaseUrl, token!, baseArgument === "monitor", 3000, 1)
226228
} catch (_e) {
227229
/* Ignore */
228230
}
@@ -240,6 +242,9 @@ async function main() {
240242
const expiresInSeconds = checkedArgs.get("expire-in")
241243
if (expiresInSeconds) {
242244
expiresAt = Date.now() + (parseInt(expiresInSeconds, 10) * 1000)
245+
} else {
246+
console.error("Error: You need to specify expiry time using --expire-in <seconds>")
247+
exit(1)
243248
}
244249
const token = await GenerateToken(
245250
secret,
@@ -360,59 +365,64 @@ async function main() {
360365
* using websockets, and prints all received messages
361366
*/
362367
if (baseArgument === "monitor") {
363-
const apiHostname = configuration.api?.hostname || DEFAULT_REST_API_HOSTNAME
364-
const apiPort = port
365-
const wsUrl = `ws://${apiHostname}:${apiPort}/wss`
366-
const wss = new WebSocketStream(wsUrl, {
367-
headers: {
368-
"Authorization": `Bearer ${token}`,
369-
},
370-
})
371-
const { readable } = await wss.opened
372-
const reader = readable.getReader()
373-
while (true) {
374-
const { value, done } = await reader.read()
375-
if (done) {
376-
break
377-
}
368+
const logHandler = (e: ApiLogItem) => {
378369
try {
379-
const v = JSON.parse(value.toString())
380-
if (v.t === "log") {
381-
const logWithColors = configuration!.logger?.colors ?? true
382-
383-
const { processId, severity, category, timeStamp, text } = v.d
384-
385-
const severityFilter = !checkedArgs.get("severity") || checkedArgs.get("severity") === "" || checkedArgs.get("severity")!.toLowerCase() === severity.toLowerCase()
386-
const processFilter = !checkedArgs.get("id") || checkedArgs.get("id") === "" || !processId || checkedArgs.get("id")!.toLowerCase() === processId.toLowerCase()
387-
388-
if (!severityFilter) continue
389-
if (!processFilter) continue
390-
391-
const isStdErr = severity === "error" || category === "stderr"
392-
const decoratedLogText = `${new Date(timeStamp).toISOString()} [${severity.toUpperCase()}] [${processId || "core"}:${category}] ${text}`
393-
let color = null
394-
// Apply coloring rules
395-
if (logWithColors) {
396-
if (processId === "core") color = "gray"
397-
if (category === "starting") color = "green"
398-
if (category === "finished") color = "yellow"
399-
if (isStdErr) color = "red"
400-
}
401-
let logFn = console.log
402-
if (severity === "warn") logFn = console.warn
403-
if (severity === "info") logFn = console.info
404-
if (severity === "error") logFn = console.error
405-
if (color !== null) {
406-
logFn(`%c${decoratedLogText}`, `color: ${color}`)
407-
} else {
408-
logFn(decoratedLogText)
409-
}
370+
const logWithColors = configuration!.logger?.colors ?? true
371+
const { processId, severity, category, timeStamp, text } = e
372+
const severityFilter = !checkedArgs.get("severity") || checkedArgs.get("severity") === "" || checkedArgs.get("severity")!.toLowerCase() === severity.toLowerCase()
373+
const processFilter = !checkedArgs.get("id") || checkedArgs.get("id") === "" || !processId || checkedArgs.get("id")!.toLowerCase() === processId.toLowerCase()
374+
375+
if (!severityFilter) return
376+
if (!processFilter) return
377+
378+
const isStdErr = severity === "error" || category === "stderr"
379+
const decoratedLogText = `${new Date(timeStamp).toISOString()} [${severity.toUpperCase()}] [${processId || "core"}:${category}] ${text}`
380+
let color = null
381+
// Apply coloring rules
382+
if (logWithColors) {
383+
if (processId === "core") color = "gray"
384+
if (category === "starting") color = "green"
385+
if (category === "finished") color = "yellow"
386+
if (isStdErr) color = "red"
387+
}
388+
let logFn = console.log
389+
if (severity === "warn") logFn = console.warn
390+
if (severity === "info") logFn = console.info
391+
if (severity === "error") logFn = console.error
392+
if (color !== null) {
393+
logFn(`%c${decoratedLogText}`, `color: ${color}`)
394+
} else {
395+
logFn(decoratedLogText)
410396
}
411397
} catch (_e) {
412398
console.error("Error in log streamer: " + _e)
413399
}
414400
}
415-
return
401+
402+
// Test the client
403+
let responseState
404+
try {
405+
responseState = await client?.getState()
406+
if (!responseState?.data) {
407+
console.error("Could not contact the Pup instance.")
408+
exit(1)
409+
}
410+
} catch (_e) {
411+
console.error("Action failed: Could not contact the Pup instance.")
412+
exit(1)
413+
}
414+
415+
// Output status
416+
console.log(`Connected to Pup, streaming logs. Abort with CTRL+C.`)
417+
418+
// Start streaming logs
419+
client?.on("log", logHandler as EventHandler<unknown>)
420+
421+
// Wait a year or so
422+
await new Promise((resolve) => setTimeout(resolve, 365 * 24 * 60 * 60 * 1000))
423+
424+
// Exit
425+
exit(0)
416426
}
417427

418428
/**
@@ -537,7 +547,7 @@ async function main() {
537547
return exit(1)
538548
}
539549
} catch (e) {
540-
console.error("Action failed:", e)
550+
console.error("Action failed:", e.name)
541551
return exit(1)
542552
}
543553
}
@@ -556,7 +566,11 @@ async function main() {
556566
console.warn(`Pup already running. Exiting.`)
557567
exit(1)
558568
}
559-
} catch (_e) { /* Expected! ^*/ }
569+
} catch (_e) {
570+
/* Expected! ^*/
571+
} finally {
572+
client?.close()
573+
}
560574

561575
/**
562576
* Error handling: Require at least one configured process

‎lib/core/rest.ts

+3-14
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ const generateAuthMiddleware = (key: CryptoKey, revoked?: string[]) => {
5858
if (payload.data?.consumer) {
5959
if (revoked && revoked.find((r) => r.toLowerCase().trim() === payload.data?.consumer.toLowerCase().trim())) {
6060
ctx.response.status = Status.Unauthorized
61-
ctx.response.body = { error: "Invalid token" }
61+
ctx.response.body = { error: "Revoked token" }
6262
} else {
6363
ctx.state.consumer = payload.data?.consumer
6464
await next()
6565
}
6666
} else {
6767
ctx.response.status = Status.Unauthorized
68-
ctx.response.body = { error: "Invalid token" }
68+
ctx.response.body = { error: "Token missing consumer" }
6969
}
7070
} else {
7171
ctx.response.status = Status.Unauthorized
@@ -117,12 +117,6 @@ export class RestApi {
117117
return ctx.throw(501)
118118
}
119119

120-
// Handle incoming message, but y tho?
121-
/*ws.onmessage = (m) => {
122-
this.pupApi.events.on(evtName, proxyFnFactory(evtName))
123-
ws.send(m.data as string)
124-
}*/
125-
126120
// Expose events to the API Consumer
127121
const proxyFns: Record<string, string | EventHandler<unknown>>[] = []
128122
const proxyFnFactory = (evtName: string) => {
@@ -409,17 +403,12 @@ export class RestApi {
409403
)
410404
this.app.use(this.router.routes())
411405
this.app.use(this.router.allowedMethods())
412-
this.pupApi.log("info", "rest", `Starting the Rest API`)
406+
this.pupApi.log("info", "rest", `Starting the Rest API on ${this.hostname}:${this.port}`)
413407
await this.app.listen({
414408
port: this.port,
415409
hostname: this.hostname,
416410
signal: this.appAbortController.signal,
417411
})
418-
this.pupApi.log(
419-
"info",
420-
"rest",
421-
`Rest API running, available on ${this.hostname}:${this.port}`,
422-
)
423412
return this.port
424413
}
425414

‎versions.json

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22
"canary_url": "https://raw.githubusercontent.com/Hexagon/pup/main/pup.ts",
33
"stable": [],
44
"prerelease": [
5+
{
6+
"version": "1.0.0-rc.39",
7+
"url": "jsr:@pup/pup@1.0.0-rc.39",
8+
"deno": null,
9+
"deno_unstable": "1.43.0",
10+
"default_permissions": [
11+
"--allow-env",
12+
"--allow-read",
13+
"--allow-write",
14+
"--allow-sys=loadavg,systemMemoryInfo,osUptime,osRelease,uid,gid",
15+
"--allow-net",
16+
"--allow-run"
17+
]
18+
},
519
{
620
"version": "1.0.0-rc.38",
721
"url": "jsr:@pup/pup@1.0.0-rc.38",

0 commit comments

Comments
 (0)
Please sign in to comment.