@platformatic/watt-extra 0.1.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.
- package/README.md +87 -0
- package/app.js +124 -0
- package/cli.js +141 -0
- package/clients/compliance/compliance-types.d.ts +887 -0
- package/clients/compliance/compliance.mjs +1049 -0
- package/clients/compliance/compliance.openapi.json +6127 -0
- package/clients/control-plane/control-plane-types.d.ts +2696 -0
- package/clients/control-plane/control-plane.mjs +3051 -0
- package/clients/control-plane/control-plane.openapi.json +13693 -0
- package/clients/cron/cron-types.d.ts +1479 -0
- package/clients/cron/cron.mjs +872 -0
- package/clients/cron/cron.openapi.json +9330 -0
- package/compliance/index.js +21 -0
- package/compliance/rules/dependencies.js +76 -0
- package/compliance/rules/utils.js +12 -0
- package/eslint.config.js +11 -0
- package/help/start.txt +12 -0
- package/help/watt-extra.txt +12 -0
- package/index.js +45 -0
- package/lib/banner.js +22 -0
- package/lib/errors.js +34 -0
- package/lib/utils.js +34 -0
- package/lib/wattpro.js +580 -0
- package/package.json +50 -0
- package/plugins/alerts.js +115 -0
- package/plugins/auth.js +89 -0
- package/plugins/compliancy.js +70 -0
- package/plugins/env.js +58 -0
- package/plugins/flamegraphs.js +100 -0
- package/plugins/init.js +70 -0
- package/plugins/metadata.js +84 -0
- package/plugins/scheduler.js +48 -0
- package/plugins/update.js +128 -0
- package/renovate.json +6 -0
- package/test/alerts.test.js +607 -0
- package/test/auth.test.js +128 -0
- package/test/auto-cache.test.js +401 -0
- package/test/cli.test.js +75 -0
- package/test/compliancy.test.js +87 -0
- package/test/fixtures/runtime-domains/alpha/package.json +5 -0
- package/test/fixtures/runtime-domains/alpha/platformatic.json +6 -0
- package/test/fixtures/runtime-domains/alpha/plugin.js +16 -0
- package/test/fixtures/runtime-domains/beta/package.json +5 -0
- package/test/fixtures/runtime-domains/beta/platformatic.json +6 -0
- package/test/fixtures/runtime-domains/beta/plugin.js +7 -0
- package/test/fixtures/runtime-domains/composer/package.json +5 -0
- package/test/fixtures/runtime-domains/composer/platformatic.json +19 -0
- package/test/fixtures/runtime-domains/package.json +1 -0
- package/test/fixtures/runtime-domains/platformatic.json +27 -0
- package/test/fixtures/runtime-health/package.json +20 -0
- package/test/fixtures/runtime-health/platformatic.json +16 -0
- package/test/fixtures/runtime-health/services/service-1/package.json +17 -0
- package/test/fixtures/runtime-health/services/service-1/platformatic.json +16 -0
- package/test/fixtures/runtime-health/services/service-1/plugins/example.js +6 -0
- package/test/fixtures/runtime-health/services/service-1/routes/root.cjs +8 -0
- package/test/fixtures/runtime-health/services/service-2/package.json +17 -0
- package/test/fixtures/runtime-health/services/service-2/platformatic.json +16 -0
- package/test/fixtures/runtime-health/services/service-2/plugins/example.js +6 -0
- package/test/fixtures/runtime-health/services/service-2/routes/root.cjs +8 -0
- package/test/fixtures/runtime-next/package.json +5 -0
- package/test/fixtures/runtime-next/platformatic.json +9 -0
- package/test/fixtures/runtime-next/web/next/next.config.js +2 -0
- package/test/fixtures/runtime-next/web/next/package.json +7 -0
- package/test/fixtures/runtime-next/web/next/platformatic.json +9 -0
- package/test/fixtures/runtime-next/web/next/src/app/direct/route.js +3 -0
- package/test/fixtures/runtime-next/web/next/src/app/layout.jsx +7 -0
- package/test/fixtures/runtime-next/web/next/src/app/page.jsx +3 -0
- package/test/fixtures/runtime-scheduler/main/package.json +5 -0
- package/test/fixtures/runtime-scheduler/main/platformatic.json +9 -0
- package/test/fixtures/runtime-scheduler/main/routes/root.cjs +11 -0
- package/test/fixtures/runtime-scheduler/package.json +1 -0
- package/test/fixtures/runtime-scheduler/platformatic.json +27 -0
- package/test/fixtures/runtime-service/main/package.json +5 -0
- package/test/fixtures/runtime-service/main/platformatic.json +12 -0
- package/test/fixtures/runtime-service/main/routes/root.cjs +11 -0
- package/test/fixtures/runtime-service/package.json +1 -0
- package/test/fixtures/runtime-service/platformatic.json +19 -0
- package/test/fixtures/service-1/package.json +7 -0
- package/test/fixtures/service-1/platformatic.json +18 -0
- package/test/fixtures/service-1/routes/root.cjs +48 -0
- package/test/fixtures/service-2/platformatic.json +21 -0
- package/test/fixtures/service-2/routes/root.cjs +5 -0
- package/test/fixtures/service-3/package.json +5 -0
- package/test/fixtures/service-3/platformatic.json +21 -0
- package/test/fixtures/service-3/routes/root.cjs +8 -0
- package/test/health.test.js +44 -0
- package/test/helper.js +274 -0
- package/test/init.test.js +243 -0
- package/test/patch-config.test.js +434 -0
- package/test/scheduler.test.js +71 -0
- package/test/send-to-icc-retry.test.js +138 -0
- package/test/shared-context.test.js +82 -0
- package/test/spawn.test.js +110 -0
- package/test/trigger-flamegraphs.test.js +226 -0
- package/test/update.test.js +519 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import WebSocket from 'ws'
|
|
2
|
+
import { once } from 'node:events'
|
|
3
|
+
import { setTimeout as sleep } from 'node:timers/promises'
|
|
4
|
+
|
|
5
|
+
function createWebSocketUrl (httpUrl, path) {
|
|
6
|
+
const url = new URL(httpUrl)
|
|
7
|
+
url.protocol = url.protocol.replace('http', 'ws')
|
|
8
|
+
const basePath = url.pathname.endsWith('/') ? url.pathname : `${url.pathname}/`
|
|
9
|
+
url.pathname = `${basePath}${path}`
|
|
10
|
+
return url.toString()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async function updatePlugin (app) {
|
|
14
|
+
const reconnectInterval = app.env.PLT_UPDATES_RECONNECT_INTERVAL_SEC * 1000
|
|
15
|
+
|
|
16
|
+
let socket = null
|
|
17
|
+
|
|
18
|
+
async function processMessage (data) {
|
|
19
|
+
try {
|
|
20
|
+
const message = JSON.parse(data)
|
|
21
|
+
const { topic, type, command } = message
|
|
22
|
+
|
|
23
|
+
// Handle trigger-flamegraph command from ICC
|
|
24
|
+
if (command === 'trigger-flamegraph') {
|
|
25
|
+
app.log.info({ command }, 'Received trigger-flamegraph command from ICC')
|
|
26
|
+
await app.sendFlamegraphs()
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Handle updates websocket format: { type: '...', topic: '...', data: {...} }
|
|
31
|
+
if (!topic || !type) {
|
|
32
|
+
app.log.warn({ message }, 'Received invalid message from updates websocket')
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (type === 'config-updated') {
|
|
37
|
+
app.log.info({ topic, type }, 'Received config update from updates websocket')
|
|
38
|
+
await app.updateConfig(message)
|
|
39
|
+
} else {
|
|
40
|
+
app.log.info({ topic, type }, 'Received message, not handled type')
|
|
41
|
+
}
|
|
42
|
+
} catch (err) {
|
|
43
|
+
app.log.error(err, 'Error processing message from updates websocket')
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function connectToUpdates () {
|
|
48
|
+
const applicationId = app.instanceConfig?.applicationId
|
|
49
|
+
if (!applicationId) {
|
|
50
|
+
app.log.warn('No application ID found, cannot connect to updates websocket')
|
|
51
|
+
return null
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const iccUrl = app.env.PLT_ICC_URL
|
|
55
|
+
if (!iccUrl) {
|
|
56
|
+
app.log.warn('No PLT_ICC_URL found in environment, cannot connect to updates websocket')
|
|
57
|
+
return null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const wsUrl = createWebSocketUrl(iccUrl, `api/updates/applications/${applicationId}`)
|
|
61
|
+
app.log.info(`Connecting to updates websocket at ${wsUrl}`)
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
const headers = await app.getAuthorizationHeader()
|
|
65
|
+
|
|
66
|
+
socket = new WebSocket(wsUrl, { headers })
|
|
67
|
+
await once(socket, 'open')
|
|
68
|
+
|
|
69
|
+
app.log.info('Connected to updates websocket')
|
|
70
|
+
// Subscribing, if subscription fails we throw, so the caller can retry
|
|
71
|
+
const subscribeMsg = JSON.stringify({ command: 'subscribe', topic: '/config' })
|
|
72
|
+
socket.send(subscribeMsg)
|
|
73
|
+
|
|
74
|
+
const command = await once(socket, 'message')
|
|
75
|
+
const message = JSON.parse(command[0])
|
|
76
|
+
if (message?.command !== 'ack') {
|
|
77
|
+
app.log.error({ message }, 'Subscription updates failed')
|
|
78
|
+
throw new Error('Subscription updates failed')
|
|
79
|
+
}
|
|
80
|
+
app.log.info('Received subscription acknowledgment from updates websocket')
|
|
81
|
+
|
|
82
|
+
// listen for subsequent messages
|
|
83
|
+
socket.on('message', processMessage)
|
|
84
|
+
|
|
85
|
+
socket.on('error', (err) => {
|
|
86
|
+
app.log.error(err, 'Error in updates websocket connection')
|
|
87
|
+
reconnectToUpdates()
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
socket.on('close', (code, reason) => {
|
|
91
|
+
app.log.info({ code, reason: reason.toString() }, 'Updates websocket connection closed')
|
|
92
|
+
reconnectToUpdates()
|
|
93
|
+
})
|
|
94
|
+
} catch (err) {
|
|
95
|
+
app.log.error(err, 'Failed to connect and subscribe to updates websocket')
|
|
96
|
+
reconnectToUpdates()
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let isReconnecting = false
|
|
101
|
+
let isClosing = false
|
|
102
|
+
|
|
103
|
+
async function reconnectToUpdates () {
|
|
104
|
+
if (isReconnecting || isClosing) return
|
|
105
|
+
isReconnecting = true
|
|
106
|
+
|
|
107
|
+
await sleep(reconnectInterval)
|
|
108
|
+
|
|
109
|
+
isReconnecting = false
|
|
110
|
+
app.log.info('Reconnecting to updates websocket')
|
|
111
|
+
await connectToUpdates()
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
app.updateConfig = async (message) => {
|
|
115
|
+
await app.wattpro.applyIccConfigUpdates(message.data)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
app.connectToUpdates = connectToUpdates
|
|
119
|
+
app.closeUpdates = async () => {
|
|
120
|
+
isClosing = true
|
|
121
|
+
if (socket) {
|
|
122
|
+
socket.close()
|
|
123
|
+
socket = null
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export default updatePlugin
|