@platformatic/runtime 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/config.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * and run json-schema-to-typescript to regenerate this file.
6
6
  */
7
7
 
8
- export type HttpsSchemasPlatformaticDevPlatformaticRuntime2710Alpha1Json = {
8
+ export type HttpsSchemasPlatformaticDevPlatformaticRuntime2711Alpha0Json = {
9
9
  [k: string]: unknown;
10
10
  } & {
11
11
  $schema?: string;
@@ -12,7 +12,7 @@ const { getServiceTemplateFromSchemaUrl } = require('@platformatic/generators/li
12
12
  const { DotEnvTool } = require('dotenv-tool')
13
13
  const { getArrayDifference } = require('../utils')
14
14
  const { pathToFileURL } = require('node:url')
15
- const { safeRemove, generateDashedName } = require('@platformatic/utils')
15
+ const { safeRemove, generateDashedName, DEFAULT_PACKAGE_MANAGER } = require('@platformatic/utils')
16
16
  const { createRequire } = require('node:module')
17
17
 
18
18
  const wrappableProperties = {
@@ -50,6 +50,7 @@ class RuntimeGenerator extends BaseGenerator {
50
50
  this.services = []
51
51
  this.existingServices = []
52
52
  this.entryPoint = null
53
+ this.packageManager = opts.packageManager ?? DEFAULT_PACKAGE_MANAGER
53
54
  }
54
55
 
55
56
  async addService (service, name) {
@@ -85,7 +86,6 @@ class RuntimeGenerator extends BaseGenerator {
85
86
  async generatePackageJson () {
86
87
  const template = {
87
88
  name: `${this.runtimeName}`,
88
- workspaces: [this.servicesFolder + '/*'],
89
89
  scripts: {
90
90
  dev: this.config.devCommand,
91
91
  build: this.config.buildCommand,
@@ -103,6 +103,11 @@ class RuntimeGenerator extends BaseGenerator {
103
103
  },
104
104
  engines
105
105
  }
106
+
107
+ if (this.packageManager === 'npm' || this.packageManager === 'yarn') {
108
+ template.workspaces = [this.servicesFolder + '/*']
109
+ }
110
+
106
111
  if (this.config.typescript) {
107
112
  const typescriptVersion = JSON.parse(await readFile(join(__dirname, '..', '..', 'package.json'), 'utf-8'))
108
113
  .devDependencies.typescript
package/lib/runtime.js CHANGED
@@ -83,6 +83,7 @@ class Runtime extends EventEmitter {
83
83
  servicesConfigsPatches
84
84
  #scheduler
85
85
  #stdio
86
+ #sharedContext
86
87
 
87
88
  constructor (configManager, runtimeLogsDir, env) {
88
89
  super()
@@ -120,8 +121,11 @@ class Runtime extends EventEmitter {
120
121
  getHttpCacheValue: this.#getHttpCacheValue.bind(this),
121
122
  setHttpCacheValue: this.#setHttpCacheValue.bind(this),
122
123
  deleteHttpCacheValue: this.#deleteHttpCacheValue.bind(this),
123
- invalidateHttpCache: this.invalidateHttpCache.bind(this)
124
+ invalidateHttpCache: this.invalidateHttpCache.bind(this),
125
+ updateSharedContext: this.updateSharedContext.bind(this),
126
+ getSharedContext: this.getSharedContext.bind(this)
124
127
  }
128
+ this.#sharedContext = {}
125
129
  }
126
130
 
127
131
  async init () {
@@ -1081,6 +1085,35 @@ class Runtime extends EventEmitter {
1081
1085
  return super.emit(event, payload)
1082
1086
  }
1083
1087
 
1088
+ async updateSharedContext (options = {}) {
1089
+ const { context, overwrite = false } = options
1090
+
1091
+ const sharedContext = overwrite ? {} : this.#sharedContext
1092
+ Object.assign(sharedContext, context)
1093
+
1094
+ this.#sharedContext = sharedContext
1095
+
1096
+ const promises = []
1097
+ for (const worker of this.#workers.values()) {
1098
+ promises.push(
1099
+ sendViaITC(worker, 'setSharedContext', sharedContext)
1100
+ )
1101
+ }
1102
+
1103
+ const results = await Promise.allSettled(promises)
1104
+ for (const result of results) {
1105
+ if (result.status === 'rejected') {
1106
+ this.logger.error({ err: result.reason }, 'Cannot update shared context')
1107
+ }
1108
+ }
1109
+
1110
+ return sharedContext
1111
+ }
1112
+
1113
+ getSharedContext () {
1114
+ return this.#sharedContext
1115
+ }
1116
+
1084
1117
  async #setDispatcher (undiciConfig) {
1085
1118
  const config = this.#configManager.current
1086
1119
 
@@ -1786,7 +1819,11 @@ class Runtime extends EventEmitter {
1786
1819
  })
1787
1820
  }
1788
1821
 
1789
- this.#workersBroadcastChannel.postMessage(workers)
1822
+ try {
1823
+ this.#workersBroadcastChannel.postMessage(workers)
1824
+ } catch (err) {
1825
+ this.logger?.error({ err }, 'Error when broadcasting workers')
1826
+ }
1790
1827
  }
1791
1828
 
1792
1829
  async #getWorkerMessagingChannel ({ service, worker }, context) {
package/lib/start.js CHANGED
@@ -98,7 +98,7 @@ async function setupAndStartRuntime (config) {
98
98
  try {
99
99
  address = await runtime.start()
100
100
  } catch (err) {
101
- if (err.code === 'EADDRINUSE') {
101
+ if (err.code === 'EADDRINUSE' || err.code === 'EACCES') {
102
102
  // Get the actual port from the error message if original port was 0
103
103
  if (!port) {
104
104
  const mo = err.message.match(/ address already in use (.+)/)
package/lib/worker/app.js CHANGED
@@ -207,6 +207,18 @@ class PlatformaticApp extends EventEmitter {
207
207
  }
208
208
 
209
209
  async getMetrics ({ format }) {
210
+ const dispatcher = getGlobalDispatcher()
211
+ if (globalThis.platformatic?.onHttpStatsFree && dispatcher?.stats) {
212
+ for (const url in dispatcher.stats) {
213
+ const { free, connected, pending, queued, running, size } = dispatcher.stats[url]
214
+ globalThis.platformatic.onHttpStatsFree(url, free || 0)
215
+ globalThis.platformatic.onHttpStatsConnected(url, connected || 0)
216
+ globalThis.platformatic.onHttpStatsPending(url, pending || 0)
217
+ globalThis.platformatic.onHttpStatsQueued(url, queued || 0)
218
+ globalThis.platformatic.onHttpStatsRunning(url, running || 0)
219
+ globalThis.platformatic.onHttpStatsSize(url, size || 0)
220
+ }
221
+ }
210
222
  return this.stackable.getMetrics({ format })
211
223
  }
212
224
 
package/lib/worker/itc.js CHANGED
@@ -56,7 +56,7 @@ async function waitEventFromITC (worker, event) {
56
56
  return safeHandleInITC(worker, () => once(worker[kITC], event))
57
57
  }
58
58
 
59
- function setupITC (app, service, dispatcher) {
59
+ function setupITC (app, service, dispatcher, sharedContext) {
60
60
  const messaging = new MessagingITC(app.appConfig.id, workerData.config)
61
61
 
62
62
  Object.assign(globalThis.platformatic ?? {}, {
@@ -210,6 +210,10 @@ function setupITC (app, service, dispatcher) {
210
210
  }
211
211
  },
212
212
 
213
+ setSharedContext (context) {
214
+ sharedContext._set(context)
215
+ },
216
+
213
217
  saveMessagingChannel (channel) {
214
218
  messaging.addSource(channel)
215
219
  }
@@ -23,6 +23,7 @@ const pino = require('pino')
23
23
  const { fetch } = require('undici')
24
24
 
25
25
  const { PlatformaticApp } = require('./app')
26
+ const { SharedContext } = require('./shared-context')
26
27
  const { setupITC } = require('./itc')
27
28
  const { setDispatcher } = require('./interceptors')
28
29
  const { kId, kITC, kStderrMarker } = require('./symbols')
@@ -189,8 +190,15 @@ async function main () {
189
190
  }
190
191
  }
191
192
 
193
+ const sharedContext = new SharedContext()
194
+ // Limit the amount of methods a user can call
195
+ globalThis.platformatic.sharedContext = {
196
+ get: () => sharedContext.get(),
197
+ update: (...args) => sharedContext.update(...args)
198
+ }
199
+
192
200
  // Setup interaction with parent port
193
- const itc = setupITC(app, service, threadDispatcher)
201
+ const itc = setupITC(app, service, threadDispatcher, sharedContext)
194
202
  globalThis[kITC] = itc
195
203
 
196
204
  // Get the dependencies
@@ -0,0 +1,26 @@
1
+ 'use strict'
2
+
3
+ const { kITC } = require('./symbols')
4
+
5
+ class SharedContext {
6
+ constructor () {
7
+ this.sharedContext = null
8
+ }
9
+
10
+ update (context, options = {}) {
11
+ return globalThis[kITC].send('updateSharedContext', { ...options, context })
12
+ }
13
+
14
+ get () {
15
+ if (this.sharedContext === null) {
16
+ this.sharedContext = globalThis[kITC].send('getSharedContext')
17
+ }
18
+ return this.sharedContext
19
+ }
20
+
21
+ _set (context) {
22
+ this.sharedContext = context
23
+ }
24
+ }
25
+
26
+ module.exports = { SharedContext }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/runtime",
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": {
@@ -37,12 +37,12 @@
37
37
  "typescript": "^5.5.4",
38
38
  "undici-oidc-interceptor": "^0.5.0",
39
39
  "why-is-node-running": "^2.2.2",
40
- "@platformatic/db": "2.71.0-alpha.1",
41
- "@platformatic/node": "2.71.0-alpha.1",
42
- "@platformatic/service": "2.71.0-alpha.1",
43
- "@platformatic/sql-graphql": "2.71.0-alpha.1",
44
- "@platformatic/composer": "2.71.0-alpha.1",
45
- "@platformatic/sql-mapper": "2.71.0-alpha.1"
40
+ "@platformatic/db": "2.71.1-alpha.0",
41
+ "@platformatic/node": "2.71.1-alpha.0",
42
+ "@platformatic/sql-graphql": "2.71.1-alpha.0",
43
+ "@platformatic/sql-mapper": "2.71.1-alpha.0",
44
+ "@platformatic/composer": "2.71.1-alpha.0",
45
+ "@platformatic/service": "2.71.1-alpha.0"
46
46
  },
47
47
  "dependencies": {
48
48
  "@fastify/accepts": "^5.0.0",
@@ -76,14 +76,14 @@
76
76
  "undici": "^7.0.0",
77
77
  "undici-thread-interceptor": "^0.14.0",
78
78
  "ws": "^8.16.0",
79
- "@platformatic/basic": "2.71.0-alpha.1",
80
- "@platformatic/config": "2.71.0-alpha.1",
81
- "@platformatic/generators": "2.71.0-alpha.1",
82
- "@platformatic/metrics": "2.71.0-alpha.1",
83
- "@platformatic/telemetry": "2.71.0-alpha.1",
84
- "@platformatic/itc": "2.71.0-alpha.1",
85
- "@platformatic/ts-compiler": "2.71.0-alpha.1",
86
- "@platformatic/utils": "2.71.0-alpha.1"
79
+ "@platformatic/basic": "2.71.1-alpha.0",
80
+ "@platformatic/config": "2.71.1-alpha.0",
81
+ "@platformatic/generators": "2.71.1-alpha.0",
82
+ "@platformatic/metrics": "2.71.1-alpha.0",
83
+ "@platformatic/itc": "2.71.1-alpha.0",
84
+ "@platformatic/telemetry": "2.71.1-alpha.0",
85
+ "@platformatic/utils": "2.71.1-alpha.0",
86
+ "@platformatic/ts-compiler": "2.71.1-alpha.0"
87
87
  },
88
88
  "scripts": {
89
89
  "test": "pnpm run lint && borp --concurrency=1 --timeout=1200000 && tsd",
package/schema.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "$id": "https://schemas.platformatic.dev/@platformatic/runtime/2.71.0-alpha.1.json",
2
+ "$id": "https://schemas.platformatic.dev/@platformatic/runtime/2.71.1-alpha.0.json",
3
3
  "$schema": "http://json-schema.org/draft-07/schema#",
4
4
  "type": "object",
5
5
  "properties": {