@platformatic/runtime 3.6.0 → 3.7.1

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/worker/itc.js CHANGED
@@ -70,12 +70,14 @@ export async function waitEventFromITC (worker, event) {
70
70
  }
71
71
 
72
72
  export function setupITC (controller, application, dispatcher, sharedContext) {
73
- const messaging = new MessagingITC(controller.appConfig.id, workerData.config)
73
+ const logger = globalThis.platformatic.logger
74
+ const messaging = new MessagingITC(controller.appConfig.id, workerData.config, logger)
74
75
 
75
76
  Object.assign(globalThis.platformatic ?? {}, {
76
77
  messaging: {
77
78
  handle: messaging.handle.bind(messaging),
78
- send: messaging.send.bind(messaging)
79
+ send: messaging.send.bind(messaging),
80
+ notify: messaging.notify.bind(messaging)
79
81
  }
80
82
  })
81
83
 
@@ -1,5 +1,5 @@
1
- import { executeWithTimeout, kTimeout } from '@platformatic/foundation'
2
- import { ITC, generateResponse, sanitize } from '@platformatic/itc'
1
+ import { executeWithTimeout, ensureLoggableError, kTimeout } from '@platformatic/foundation'
2
+ import { ITC, parseRequest, generateRequest, generateResponse, sanitize, errors } from '@platformatic/itc'
3
3
  import { MessagingError } from '../errors.js'
4
4
  import { RoundRobinMap } from './round-robin-map.js'
5
5
  import { kITC, kWorkersBroadcast } from './symbols.js'
@@ -11,10 +11,12 @@ export class MessagingITC extends ITC {
11
11
  #listener
12
12
  #closeResolvers
13
13
  #broadcastChannel
14
+ #notificationsChannels
14
15
  #workers
15
16
  #sources
17
+ #logger
16
18
 
17
- constructor (id, runtimeConfig) {
19
+ constructor (id, runtimeConfig, logger) {
18
20
  super({
19
21
  throwOnMissingHandler: true,
20
22
  name: `${id}-messaging`
@@ -28,6 +30,14 @@ export class MessagingITC extends ITC {
28
30
  this.#broadcastChannel = new BroadcastChannel(kWorkersBroadcast)
29
31
  this.#broadcastChannel.onmessage = this.#updateWorkers.bind(this)
30
32
 
33
+ this.#notificationsChannels = new Map()
34
+
35
+ const notificationsChannel = new BroadcastChannel(`plt.messaging.notifications-${id}`)
36
+ notificationsChannel.onmessage = this.#handleNotification.bind(this)
37
+ this.#notificationsChannels.set(id, notificationsChannel)
38
+
39
+ this.#logger = logger
40
+
31
41
  this.listen()
32
42
  }
33
43
 
@@ -87,6 +97,18 @@ export class MessagingITC extends ITC {
87
97
  return response
88
98
  }
89
99
 
100
+ notify (application, name, message) {
101
+ const request = generateRequest(name, message)
102
+
103
+ let channel = this.#notificationsChannels.get(application)
104
+ if (!channel) {
105
+ channel = new BroadcastChannel(`plt.messaging.notifications-${application}`)
106
+ this.#notificationsChannels.set(application, channel)
107
+ }
108
+
109
+ channel.postMessage(sanitize(request))
110
+ }
111
+
90
112
  async addSource (channel) {
91
113
  this.#sources.add(channel)
92
114
  this.#setupChannel(channel)
@@ -119,6 +141,10 @@ export class MessagingITC extends ITC {
119
141
  this.#closeResolvers.resolve()
120
142
  this.#broadcastChannel.close()
121
143
 
144
+ for (const channel of this.#notificationsChannels.values()) {
145
+ channel.close()
146
+ }
147
+
122
148
  for (const source of this.#sources) {
123
149
  source.close()
124
150
  }
@@ -166,6 +192,27 @@ export class MessagingITC extends ITC {
166
192
  this.#workers.configure(instances)
167
193
  }
168
194
 
195
+ async #handleNotification (messageEvent) {
196
+ let request
197
+ try {
198
+ request = parseRequest(messageEvent.data)
199
+ } catch (err) {
200
+ this.#logger.error({ err: ensureLoggableError(err) }, 'Failed to parse the notification message.')
201
+ return
202
+ }
203
+
204
+ try {
205
+ const handler = this.getHandler(request.name)
206
+ if (!handler) {
207
+ throw new errors.HandlerNotFoundError(request.name)
208
+ }
209
+
210
+ await handler(request.data)
211
+ } catch (error) {
212
+ this.#logger.error({ error }, `"Handler for the "${request.name}" message failed.`)
213
+ }
214
+ }
215
+
169
216
  #handlePendingResponse (channel) {
170
217
  for (const { application, request } of channel[kPendingResponses].values()) {
171
218
  this._emitResponse(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/runtime",
3
- "version": "3.6.0",
3
+ "version": "3.7.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -34,14 +34,14 @@
34
34
  "typescript": "^5.5.4",
35
35
  "undici-oidc-interceptor": "^0.5.0",
36
36
  "why-is-node-running": "^2.2.2",
37
- "@platformatic/composer": "3.6.0",
38
- "@platformatic/db": "3.6.0",
39
- "@platformatic/gateway": "3.6.0",
40
- "@platformatic/node": "3.6.0",
41
- "@platformatic/service": "3.6.0",
42
- "@platformatic/sql-mapper": "3.6.0",
43
- "@platformatic/wattpm-pprof-capture": "3.6.0",
44
- "@platformatic/sql-graphql": "3.6.0"
37
+ "@platformatic/composer": "3.7.1",
38
+ "@platformatic/db": "3.7.1",
39
+ "@platformatic/gateway": "3.7.1",
40
+ "@platformatic/node": "3.7.1",
41
+ "@platformatic/service": "3.7.1",
42
+ "@platformatic/sql-graphql": "3.7.1",
43
+ "@platformatic/sql-mapper": "3.7.1",
44
+ "@platformatic/wattpm-pprof-capture": "3.7.1"
45
45
  },
46
46
  "dependencies": {
47
47
  "@fastify/accepts": "^5.0.0",
@@ -71,12 +71,12 @@
71
71
  "undici": "^7.0.0",
72
72
  "undici-thread-interceptor": "^0.14.0",
73
73
  "ws": "^8.16.0",
74
- "@platformatic/basic": "3.6.0",
75
- "@platformatic/foundation": "3.6.0",
76
- "@platformatic/generators": "3.6.0",
77
- "@platformatic/metrics": "3.6.0",
78
- "@platformatic/itc": "3.6.0",
79
- "@platformatic/telemetry": "3.6.0"
74
+ "@platformatic/foundation": "3.7.1",
75
+ "@platformatic/basic": "3.7.1",
76
+ "@platformatic/itc": "3.7.1",
77
+ "@platformatic/generators": "3.7.1",
78
+ "@platformatic/metrics": "3.7.1",
79
+ "@platformatic/telemetry": "3.7.1"
80
80
  },
81
81
  "engines": {
82
82
  "node": ">=22.19.0"
package/schema.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "$id": "https://schemas.platformatic.dev/@platformatic/runtime/3.6.0.json",
2
+ "$id": "https://schemas.platformatic.dev/@platformatic/runtime/3.7.1.json",
3
3
  "$schema": "http://json-schema.org/draft-07/schema#",
4
4
  "title": "Platformatic Runtime Config",
5
5
  "type": "object",