@platformatic/composer 2.71.0-alpha.1 → 2.71.1-alpha.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/lib/metrics.js ADDED
@@ -0,0 +1,12 @@
1
+ export function initMetrics (prometheus) {
2
+ if (!prometheus?.registry || !prometheus?.client) return null
3
+ const { client, registry } = prometheus
4
+
5
+ return {
6
+ activeWsConnections: new client.Gauge({
7
+ name: 'active_ws_composer_connections',
8
+ help: 'Active Websocket composer connections in "@platformatic/composer"',
9
+ registers: [registry]
10
+ })
11
+ }
12
+ }
package/lib/proxy.js CHANGED
@@ -5,6 +5,7 @@ const { ensureLoggableError } = require('@platformatic/utils')
5
5
  const fp = require('fastify-plugin')
6
6
  const { workerData } = require('node:worker_threads')
7
7
  const { getGlobalDispatcher } = require('undici')
8
+ const { initMetrics } = require('./metrics')
8
9
 
9
10
  const kITC = Symbol.for('plt.runtime.itc')
10
11
  const kProxyRoute = Symbol('plt.composer.proxy.route')
@@ -45,13 +46,15 @@ async function resolveServiceProxyParameters (service) {
45
46
  prefix,
46
47
  rewritePrefix,
47
48
  internalRewriteLocationHeader,
48
- needsRootRedirect: meta.needsRootRedirect,
49
+ needsRootTrailingSlash: meta.needsRootTrailingSlash,
49
50
  needsRefererBasedRedirect: meta.needsRefererBasedRedirect,
50
51
  upstream: service.proxy?.upstream,
51
52
  ws: service.proxy?.ws
52
53
  }
53
54
  }
54
55
 
56
+ let metrics
57
+
55
58
  module.exports = fp(async function (app, opts) {
56
59
  const meta = { proxies: {} }
57
60
  const hostnameLessProxies = []
@@ -72,7 +75,7 @@ module.exports = fp(async function (app, opts) {
72
75
  url,
73
76
  rewritePrefix,
74
77
  internalRewriteLocationHeader,
75
- needsRootRedirect,
78
+ needsRootTrailingSlash,
76
79
  needsRefererBasedRedirect,
77
80
  ws
78
81
  } = parameters
@@ -81,34 +84,16 @@ module.exports = fp(async function (app, opts) {
81
84
  const basePath = `/${prefix ?? ''}`.replaceAll(/\/+/g, '/').replace(/\/$/, '')
82
85
  const dispatcher = getGlobalDispatcher()
83
86
 
84
- if (needsRootRedirect) {
85
- app.addHook('preHandler', (req, reply, done) => {
86
- if (req.url === basePath) {
87
- app.inject(
88
- {
89
- method: req.method,
90
- url: `${basePath}/`,
91
- headers: req.headers,
92
- payload: req.body
93
- },
94
- (err, result) => {
95
- if (err) {
96
- done(err)
97
- return
98
- }
99
-
100
- const replyHeaders = result.headers
101
- delete replyHeaders['content-length']
102
- delete replyHeaders['transfer-encoding']
103
-
104
- reply.code(result.statusCode).headers(replyHeaders).send(result.rawPayload)
105
- done()
106
- }
107
- )
108
- } else {
109
- done()
87
+ let preRewrite = null
88
+
89
+ if (needsRootTrailingSlash) {
90
+ preRewrite = function preRewrite (url) {
91
+ if (url === basePath) {
92
+ url += '/'
110
93
  }
111
- })
94
+
95
+ return url
96
+ }
112
97
  }
113
98
 
114
99
  /*
@@ -118,7 +103,7 @@ module.exports = fp(async function (app, opts) {
118
103
  from the Referer header.
119
104
  */
120
105
  if (needsRefererBasedRedirect) {
121
- app.addHook('preHandler', (req, reply, done) => {
106
+ app.addHook('preHandler', function refererBasedRedirectPreHandler (req, reply, done) {
122
107
  // If the URL is already targeted to the service, do nothing
123
108
  if (req.url.startsWith(basePath)) {
124
109
  done()
@@ -163,17 +148,28 @@ module.exports = fp(async function (app, opts) {
163
148
  )
164
149
  : null
165
150
 
151
+ if (!metrics) {
152
+ metrics = initMetrics(globalThis.platformatic?.prometheus)
153
+ }
154
+
166
155
  const proxyOptions = {
167
- websocket: true,
168
156
  prefix,
169
157
  rewritePrefix,
170
158
  upstream: service.proxy?.upstream ?? origin,
159
+ preRewrite,
171
160
 
161
+ websocket: true,
172
162
  wsUpstream: ws?.upstream ?? url ?? origin,
173
163
  wsReconnect: ws?.reconnect,
174
164
  wsHooks: {
175
- onConnect: ws?.hooks?.onConnect,
176
- onDisconnect: ws?.hooks?.onDisconnect,
165
+ onConnect: (...args) => {
166
+ metrics?.activeWsConnections?.inc()
167
+ ws?.hooks?.onConnect(...args)
168
+ },
169
+ onDisconnect: (...args) => {
170
+ metrics?.activeWsConnections?.dec()
171
+ ws?.hooks?.onDisconnect(...args)
172
+ },
177
173
  onReconnect: ws?.hooks?.onReconnect,
178
174
  onPong: ws?.hooks?.onPong,
179
175
  onIncomingMessage: ws?.hooks?.onIncomingMessage,
@@ -185,6 +181,7 @@ module.exports = fp(async function (app, opts) {
185
181
  config: {
186
182
  [kProxyRoute]: true
187
183
  },
184
+
188
185
  internalRewriteLocationHeader: false,
189
186
  replyOptions: {
190
187
  rewriteHeaders: headers => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/composer",
3
- "version": "2.71.0-alpha.1",
3
+ "version": "2.71.1-alpha.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -25,6 +25,7 @@
25
25
  "neostandard": "^0.12.0",
26
26
  "openapi-schema-validator": "^12.1.3",
27
27
  "pino-test": "^1.0.1",
28
+ "prom-client": "^15.1.2",
28
29
  "self-cert": "^2.0.0",
29
30
  "single-user-cache": "^1.0.1",
30
31
  "split2": "^4.2.0",
@@ -32,20 +33,20 @@
32
33
  "typescript": "^5.5.4",
33
34
  "why-is-node-running": "2",
34
35
  "ws": "^8.16.0",
35
- "@platformatic/client": "2.71.0-alpha.1",
36
- "@platformatic/config": "2.71.0-alpha.1",
37
- "@platformatic/db": "2.71.0-alpha.1"
36
+ "@platformatic/client": "2.71.1-alpha.0",
37
+ "@platformatic/config": "2.71.1-alpha.0",
38
+ "@platformatic/db": "2.71.1-alpha.0"
38
39
  },
39
40
  "dependencies": {
40
41
  "@fastify/error": "^4.0.0",
41
- "@fastify/http-proxy": "^11.1.1",
42
+ "@fastify/http-proxy": "^11.3.0",
42
43
  "@fastify/reply-from": "^12.0.0",
43
44
  "@fastify/static": "^8.0.0",
44
45
  "@fastify/swagger": "^9.0.0",
45
46
  "@fastify/view": "^10.0.1",
46
47
  "@platformatic/fastify-openapi-glue": "^5.1.0",
47
48
  "@platformatic/graphql-composer": "^0.10.0",
48
- "@scalar/fastify-api-reference": "1.31.8",
49
+ "@scalar/fastify-api-reference": "1.32.4",
49
50
  "ajv": "^8.12.0",
50
51
  "commist": "^3.2.0",
51
52
  "console-table-printer": "^2.12.0",
@@ -68,12 +69,12 @@
68
69
  "rfdc": "^1.3.1",
69
70
  "semgrator": "^0.3.0",
70
71
  "undici": "^7.0.0",
71
- "@platformatic/config": "2.71.0-alpha.1",
72
- "@platformatic/generators": "2.71.0-alpha.1",
73
- "@platformatic/scalar-theme": "2.71.0-alpha.1",
74
- "@platformatic/telemetry": "2.71.0-alpha.1",
75
- "@platformatic/utils": "^2.71.0-alpha.1",
76
- "@platformatic/service": "2.71.0-alpha.1"
72
+ "@platformatic/generators": "2.71.1-alpha.0",
73
+ "@platformatic/scalar-theme": "2.71.1-alpha.0",
74
+ "@platformatic/utils": "^2.71.1-alpha.0",
75
+ "@platformatic/service": "2.71.1-alpha.0",
76
+ "@platformatic/telemetry": "2.71.1-alpha.0",
77
+ "@platformatic/config": "2.71.1-alpha.0"
77
78
  },
78
79
  "scripts": {
79
80
  "test": "pnpm run lint && borp -T --timeout=1200000 -c 1 && tsd",
package/schema.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "$id": "https://schemas.platformatic.dev/@platformatic/composer/2.71.0-alpha.1.json",
2
+ "$id": "https://schemas.platformatic.dev/@platformatic/composer/2.71.1-alpha.0.json",
3
3
  "$schema": "http://json-schema.org/draft-07/schema#",
4
4
  "title": "Platformatic Composer",
5
5
  "type": "object",