@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.
Files changed (95) hide show
  1. package/README.md +87 -0
  2. package/app.js +124 -0
  3. package/cli.js +141 -0
  4. package/clients/compliance/compliance-types.d.ts +887 -0
  5. package/clients/compliance/compliance.mjs +1049 -0
  6. package/clients/compliance/compliance.openapi.json +6127 -0
  7. package/clients/control-plane/control-plane-types.d.ts +2696 -0
  8. package/clients/control-plane/control-plane.mjs +3051 -0
  9. package/clients/control-plane/control-plane.openapi.json +13693 -0
  10. package/clients/cron/cron-types.d.ts +1479 -0
  11. package/clients/cron/cron.mjs +872 -0
  12. package/clients/cron/cron.openapi.json +9330 -0
  13. package/compliance/index.js +21 -0
  14. package/compliance/rules/dependencies.js +76 -0
  15. package/compliance/rules/utils.js +12 -0
  16. package/eslint.config.js +11 -0
  17. package/help/start.txt +12 -0
  18. package/help/watt-extra.txt +12 -0
  19. package/index.js +45 -0
  20. package/lib/banner.js +22 -0
  21. package/lib/errors.js +34 -0
  22. package/lib/utils.js +34 -0
  23. package/lib/wattpro.js +580 -0
  24. package/package.json +50 -0
  25. package/plugins/alerts.js +115 -0
  26. package/plugins/auth.js +89 -0
  27. package/plugins/compliancy.js +70 -0
  28. package/plugins/env.js +58 -0
  29. package/plugins/flamegraphs.js +100 -0
  30. package/plugins/init.js +70 -0
  31. package/plugins/metadata.js +84 -0
  32. package/plugins/scheduler.js +48 -0
  33. package/plugins/update.js +128 -0
  34. package/renovate.json +6 -0
  35. package/test/alerts.test.js +607 -0
  36. package/test/auth.test.js +128 -0
  37. package/test/auto-cache.test.js +401 -0
  38. package/test/cli.test.js +75 -0
  39. package/test/compliancy.test.js +87 -0
  40. package/test/fixtures/runtime-domains/alpha/package.json +5 -0
  41. package/test/fixtures/runtime-domains/alpha/platformatic.json +6 -0
  42. package/test/fixtures/runtime-domains/alpha/plugin.js +16 -0
  43. package/test/fixtures/runtime-domains/beta/package.json +5 -0
  44. package/test/fixtures/runtime-domains/beta/platformatic.json +6 -0
  45. package/test/fixtures/runtime-domains/beta/plugin.js +7 -0
  46. package/test/fixtures/runtime-domains/composer/package.json +5 -0
  47. package/test/fixtures/runtime-domains/composer/platformatic.json +19 -0
  48. package/test/fixtures/runtime-domains/package.json +1 -0
  49. package/test/fixtures/runtime-domains/platformatic.json +27 -0
  50. package/test/fixtures/runtime-health/package.json +20 -0
  51. package/test/fixtures/runtime-health/platformatic.json +16 -0
  52. package/test/fixtures/runtime-health/services/service-1/package.json +17 -0
  53. package/test/fixtures/runtime-health/services/service-1/platformatic.json +16 -0
  54. package/test/fixtures/runtime-health/services/service-1/plugins/example.js +6 -0
  55. package/test/fixtures/runtime-health/services/service-1/routes/root.cjs +8 -0
  56. package/test/fixtures/runtime-health/services/service-2/package.json +17 -0
  57. package/test/fixtures/runtime-health/services/service-2/platformatic.json +16 -0
  58. package/test/fixtures/runtime-health/services/service-2/plugins/example.js +6 -0
  59. package/test/fixtures/runtime-health/services/service-2/routes/root.cjs +8 -0
  60. package/test/fixtures/runtime-next/package.json +5 -0
  61. package/test/fixtures/runtime-next/platformatic.json +9 -0
  62. package/test/fixtures/runtime-next/web/next/next.config.js +2 -0
  63. package/test/fixtures/runtime-next/web/next/package.json +7 -0
  64. package/test/fixtures/runtime-next/web/next/platformatic.json +9 -0
  65. package/test/fixtures/runtime-next/web/next/src/app/direct/route.js +3 -0
  66. package/test/fixtures/runtime-next/web/next/src/app/layout.jsx +7 -0
  67. package/test/fixtures/runtime-next/web/next/src/app/page.jsx +3 -0
  68. package/test/fixtures/runtime-scheduler/main/package.json +5 -0
  69. package/test/fixtures/runtime-scheduler/main/platformatic.json +9 -0
  70. package/test/fixtures/runtime-scheduler/main/routes/root.cjs +11 -0
  71. package/test/fixtures/runtime-scheduler/package.json +1 -0
  72. package/test/fixtures/runtime-scheduler/platformatic.json +27 -0
  73. package/test/fixtures/runtime-service/main/package.json +5 -0
  74. package/test/fixtures/runtime-service/main/platformatic.json +12 -0
  75. package/test/fixtures/runtime-service/main/routes/root.cjs +11 -0
  76. package/test/fixtures/runtime-service/package.json +1 -0
  77. package/test/fixtures/runtime-service/platformatic.json +19 -0
  78. package/test/fixtures/service-1/package.json +7 -0
  79. package/test/fixtures/service-1/platformatic.json +18 -0
  80. package/test/fixtures/service-1/routes/root.cjs +48 -0
  81. package/test/fixtures/service-2/platformatic.json +21 -0
  82. package/test/fixtures/service-2/routes/root.cjs +5 -0
  83. package/test/fixtures/service-3/package.json +5 -0
  84. package/test/fixtures/service-3/platformatic.json +21 -0
  85. package/test/fixtures/service-3/routes/root.cjs +8 -0
  86. package/test/health.test.js +44 -0
  87. package/test/helper.js +274 -0
  88. package/test/init.test.js +243 -0
  89. package/test/patch-config.test.js +434 -0
  90. package/test/scheduler.test.js +71 -0
  91. package/test/send-to-icc-retry.test.js +138 -0
  92. package/test/shared-context.test.js +82 -0
  93. package/test/spawn.test.js +110 -0
  94. package/test/trigger-flamegraphs.test.js +226 -0
  95. 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
package/renovate.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "$schema": "https://docs.renovatebot.com/renovate-schema.json",
3
+ "extends": [
4
+ "config:recommended"
5
+ ]
6
+ }