@platformatic/node 3.33.0-alpha.3 → 3.34.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/config.d.ts CHANGED
@@ -231,6 +231,10 @@ export interface PlatformaticNodeJsConfig {
231
231
  gracefulShutdown?: {
232
232
  runtime: number | string;
233
233
  application: number | string;
234
+ /**
235
+ * Add Connection: close header to HTTP responses during graceful shutdown
236
+ */
237
+ closeConnections?: boolean;
234
238
  };
235
239
  health?: {
236
240
  enabled?: boolean | string;
@@ -394,6 +398,23 @@ export interface PlatformaticNodeJsConfig {
394
398
  */
395
399
  serviceVersion?: string;
396
400
  };
401
+ /**
402
+ * Custom labels to add to HTTP metrics (http_request_all_duration_seconds). Each label extracts its value from an HTTP request header.
403
+ */
404
+ httpCustomLabels?: {
405
+ /**
406
+ * The label name to use in metrics
407
+ */
408
+ name: string;
409
+ /**
410
+ * The HTTP request header to extract the value from
411
+ */
412
+ header: string;
413
+ /**
414
+ * Default value when header is missing (defaults to "unknown")
415
+ */
416
+ default?: string;
417
+ }[];
397
418
  };
398
419
  telemetry?: {
399
420
  enabled?: boolean | string;
package/lib/capability.js CHANGED
@@ -36,10 +36,18 @@ const validFilesBasenames = ['index', 'main', 'app', 'application', 'server', 's
36
36
 
37
37
  // Paolo: This is kinda hackish but there is no better way. I apologize.
38
38
  function isFastify (app) {
39
+ if (!app) {
40
+ return false
41
+ }
42
+
39
43
  return Object.getOwnPropertySymbols(app).some(s => s.description === 'fastify.state')
40
44
  }
41
45
 
42
46
  function isKoa (app) {
47
+ if (!app) {
48
+ return false
49
+ }
50
+
43
51
  return typeof app.callback === 'function'
44
52
  }
45
53
 
@@ -221,6 +229,30 @@ export class NodeCapability extends BaseCapability {
221
229
  return this.config.node?.hasServer !== false && this.#module?.hasServer !== false
222
230
  }
223
231
 
232
+ setClosing () {
233
+ super.setClosing()
234
+
235
+ if (!this.#server) return
236
+
237
+ const closeConnections = this.runtimeConfig?.gracefulShutdown?.closeConnections !== false
238
+ if (!closeConnections) return
239
+
240
+ // For non-Fastify raw HTTP servers, add request listener to set Connection: close
241
+ if (!this.#isFastify) {
242
+ const self = this
243
+ this.#server.on('request', (req, res) => {
244
+ if (self.closing && !res.headersSent && req.httpVersionMajor !== 2) {
245
+ res.setHeader('Connection', 'close')
246
+ }
247
+ })
248
+ }
249
+
250
+ // For HTTP/2, send GOAWAY frames
251
+ if (this.#server.closeHttp2Sessions) {
252
+ this.#server.closeHttp2Sessions()
253
+ }
254
+ }
255
+
224
256
  async stop () {
225
257
  await super.stop()
226
258
 
@@ -345,6 +377,14 @@ export class NodeCapability extends BaseCapability {
345
377
  return this.getDispatchFunc()
346
378
  }
347
379
 
380
+ async getDispatchFunc () {
381
+ if (!this.#hasServer()) {
382
+ return this.#backgroundServiceInject.bind(this)
383
+ }
384
+
385
+ return super.getDispatchFunc()
386
+ }
387
+
348
388
  async _listen () {
349
389
  // Make this idempotent
350
390
  /* c8 ignore next 3 */
@@ -451,4 +491,8 @@ export class NodeCapability extends BaseCapability {
451
491
  ignore
452
492
  }
453
493
  }
494
+
495
+ #backgroundServiceInject (_, res) {
496
+ res.destroy(new Error('Background services cannot receive HTTP requests via the mesh network.'))
497
+ }
454
498
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/node",
3
- "version": "3.33.0-alpha.3",
3
+ "version": "3.34.0",
4
4
  "description": "Platformatic Node.js Capability",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -18,9 +18,9 @@
18
18
  "@watchable/unpromise": "^1.0.2",
19
19
  "json5": "^2.2.3",
20
20
  "light-my-request": "^6.0.0",
21
- "@platformatic/basic": "3.33.0-alpha.3",
22
- "@platformatic/foundation": "3.33.0-alpha.3",
23
- "@platformatic/generators": "3.33.0-alpha.3"
21
+ "@platformatic/basic": "3.34.0",
22
+ "@platformatic/foundation": "3.34.0",
23
+ "@platformatic/generators": "3.34.0"
24
24
  },
25
25
  "devDependencies": {
26
26
  "cleaner-spec-reporter": "^0.5.0",
@@ -32,8 +32,8 @@
32
32
  "neostandard": "^0.12.0",
33
33
  "tsx": "^4.19.0",
34
34
  "typescript": "^5.5.4",
35
- "@platformatic/gateway": "3.33.0-alpha.3",
36
- "@platformatic/service": "3.33.0-alpha.3"
35
+ "@platformatic/gateway": "3.34.0",
36
+ "@platformatic/service": "3.34.0"
37
37
  },
38
38
  "engines": {
39
39
  "node": ">=22.19.0"
package/schema.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "$id": "https://schemas.platformatic.dev/@platformatic/node/3.33.0-alpha.3.json",
2
+ "$id": "https://schemas.platformatic.dev/@platformatic/node/3.34.0.json",
3
3
  "$schema": "http://json-schema.org/draft-07/schema#",
4
4
  "title": "Platformatic Node.js Config",
5
5
  "type": "object",
@@ -1111,6 +1111,11 @@
1111
1111
  }
1112
1112
  ],
1113
1113
  "default": 10000
1114
+ },
1115
+ "closeConnections": {
1116
+ "type": "boolean",
1117
+ "default": true,
1118
+ "description": "Add Connection: close header to HTTP responses during graceful shutdown"
1114
1119
  }
1115
1120
  },
1116
1121
  "default": {},
@@ -1651,6 +1656,32 @@
1651
1656
  "endpoint"
1652
1657
  ],
1653
1658
  "additionalProperties": false
1659
+ },
1660
+ "httpCustomLabels": {
1661
+ "type": "array",
1662
+ "description": "Custom labels to add to HTTP metrics (http_request_all_duration_seconds). Each label extracts its value from an HTTP request header.",
1663
+ "items": {
1664
+ "type": "object",
1665
+ "properties": {
1666
+ "name": {
1667
+ "type": "string",
1668
+ "description": "The label name to use in metrics"
1669
+ },
1670
+ "header": {
1671
+ "type": "string",
1672
+ "description": "The HTTP request header to extract the value from"
1673
+ },
1674
+ "default": {
1675
+ "type": "string",
1676
+ "description": "Default value when header is missing (defaults to \"unknown\")"
1677
+ }
1678
+ },
1679
+ "required": [
1680
+ "name",
1681
+ "header"
1682
+ ],
1683
+ "additionalProperties": false
1684
+ }
1654
1685
  }
1655
1686
  },
1656
1687
  "additionalProperties": false