@platformatic/runtime 2.0.0-alpha.15 → 2.0.0-alpha.16

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/schema.js CHANGED
@@ -3,10 +3,37 @@
3
3
 
4
4
  const telemetry = require('@platformatic/telemetry').schema
5
5
  const {
6
- schemaComponents: { server }
6
+ schemaComponents: { server, logger }
7
7
  } = require('@platformatic/utils')
8
8
 
9
+ const services = {
10
+ type: 'array',
11
+ items: {
12
+ type: 'object',
13
+ anyOf: [{ required: ['id', 'path'] }, { required: ['id', 'url'] }],
14
+ properties: {
15
+ id: {
16
+ type: 'string'
17
+ },
18
+ path: {
19
+ type: 'string',
20
+ resolvePath: true
21
+ },
22
+ config: {
23
+ type: 'string'
24
+ },
25
+ url: {
26
+ type: 'string'
27
+ },
28
+ useHttp: {
29
+ type: 'boolean'
30
+ }
31
+ }
32
+ }
33
+ }
34
+
9
35
  const pkg = require('../package.json')
36
+
10
37
  const platformaticRuntimeSchema = {
11
38
  $id: `https://schemas.platformatic.dev/@platformatic/runtime/${pkg.version}.json`,
12
39
  $schema: 'http://json-schema.org/draft-07/schema#',
@@ -19,6 +46,9 @@ const platformaticRuntimeSchema = {
19
46
  type: 'string',
20
47
  resolvePath: true
21
48
  },
49
+ entrypoint: {
50
+ type: 'string'
51
+ },
22
52
  autoload: {
23
53
  type: 'object',
24
54
  additionalProperties: false,
@@ -56,38 +86,20 @@ const platformaticRuntimeSchema = {
56
86
  }
57
87
  }
58
88
  },
59
- telemetry,
89
+ services,
90
+ web: services,
91
+ logger,
60
92
  server,
61
- entrypoint: {
62
- type: 'string'
63
- },
64
- watch: {
93
+ restartOnError: {
94
+ default: true,
65
95
  anyOf: [
96
+ { type: 'boolean' },
66
97
  {
67
- type: 'boolean'
68
- },
69
- {
70
- type: 'string'
98
+ type: 'number',
99
+ minimum: 100
71
100
  }
72
101
  ]
73
102
  },
74
- inspectorOptions: {
75
- type: 'object',
76
- properties: {
77
- host: {
78
- type: 'string'
79
- },
80
- port: {
81
- type: 'number'
82
- },
83
- breakFirstLine: {
84
- type: 'boolean'
85
- },
86
- watchDisabled: {
87
- type: 'boolean'
88
- }
89
- }
90
- },
91
103
  undici: {
92
104
  type: 'object',
93
105
  properties: {
@@ -130,6 +142,16 @@ const platformaticRuntimeSchema = {
130
142
  }
131
143
  }
132
144
  },
145
+ watch: {
146
+ anyOf: [
147
+ {
148
+ type: 'boolean'
149
+ },
150
+ {
151
+ type: 'string'
152
+ }
153
+ ]
154
+ },
133
155
  managementApi: {
134
156
  anyOf: [
135
157
  { type: 'boolean' },
@@ -138,11 +160,15 @@ const platformaticRuntimeSchema = {
138
160
  type: 'object',
139
161
  properties: {
140
162
  logs: {
141
- maxSize: {
142
- type: 'number',
143
- minimum: 5,
144
- default: 200
145
- }
163
+ type: 'object',
164
+ properties: {
165
+ maxSize: {
166
+ type: 'number',
167
+ minimum: 5,
168
+ default: 200
169
+ }
170
+ },
171
+ additionalProperties: false
146
172
  }
147
173
  },
148
174
  additionalProperties: false
@@ -179,43 +205,30 @@ const platformaticRuntimeSchema = {
179
205
  }
180
206
  ]
181
207
  },
182
- restartOnError: {
183
- default: true,
184
- anyOf: [
185
- { type: 'boolean' },
186
- {
187
- type: 'number',
188
- minimum: 100
189
- }
190
- ]
191
- },
192
- services: {
193
- type: 'array',
194
- items: {
195
- type: 'object',
196
- anyOf: [{ required: ['id', 'path'] }, { required: ['id', 'url'] }],
197
- properties: {
198
- id: {
199
- type: 'string'
200
- },
201
- path: {
202
- type: 'string',
203
- resolvePath: true
204
- },
205
- config: {
206
- type: 'string'
207
- },
208
- url: {
209
- type: 'string'
210
- },
211
- useHttp: {
212
- type: 'boolean'
213
- }
208
+ telemetry,
209
+ inspectorOptions: {
210
+ type: 'object',
211
+ properties: {
212
+ host: {
213
+ type: 'string'
214
+ },
215
+ port: {
216
+ type: 'number'
217
+ },
218
+ breakFirstLine: {
219
+ type: 'boolean'
220
+ },
221
+ watchDisabled: {
222
+ type: 'boolean'
214
223
  }
215
224
  }
216
225
  }
217
226
  },
218
- anyOf: [{ required: ['autoload', 'entrypoint'] }, { required: ['services', 'entrypoint'] }],
227
+ anyOf: [
228
+ { required: ['autoload', 'entrypoint'] },
229
+ { required: ['services', 'entrypoint'] },
230
+ { required: ['web', 'entrypoint'] }
231
+ ],
219
232
  additionalProperties: false,
220
233
  $defs: {
221
234
  undiciInterceptor: {
package/lib/start.js CHANGED
@@ -45,7 +45,13 @@ async function buildRuntime (configManager, env) {
45
45
  }
46
46
  })
47
47
 
48
- await runtime.init()
48
+ try {
49
+ await runtime.init()
50
+ } catch (e) {
51
+ await runtime.close()
52
+ throw e
53
+ }
54
+
49
55
  return runtime
50
56
  }
51
57
 
@@ -108,7 +114,7 @@ async function setupAndStartRuntime (config) {
108
114
  return { address, runtime }
109
115
  }
110
116
 
111
- async function startCommand (args) {
117
+ async function startCommand (args, throwAllErrors = false) {
112
118
  try {
113
119
  const config = await loadConfig(
114
120
  {
@@ -134,6 +140,10 @@ async function startCommand (args) {
134
140
 
135
141
  return res
136
142
  } catch (err) {
143
+ if (throwAllErrors) {
144
+ throw err
145
+ }
146
+
137
147
  if (err.code === 'PLT_CONFIG_NO_CONFIG_FILE_FOUND' && args.length === 1) {
138
148
  const config = {
139
149
  $schema: `https://schemas.platformatic.dev/@platformatic/service/${pkg.version}.json`,
@@ -12,6 +12,11 @@ module.exports = {
12
12
  delete config.hotReload
13
13
  }
14
14
 
15
+ if (config.server?.logger) {
16
+ config.logger = config.server.logger
17
+ delete config.server.logger
18
+ }
19
+
15
20
  return config
16
- },
21
+ }
17
22
  }
package/lib/worker/app.js CHANGED
@@ -20,7 +20,7 @@ class PlatformaticApp extends EventEmitter {
20
20
  #debouncedRestart
21
21
  #context
22
22
 
23
- constructor (appConfig, telemetryConfig, serverConfig, hasManagementApi, watch, metricsConfig) {
23
+ constructor (appConfig, telemetryConfig, loggerConfig, serverConfig, metricsConfig, hasManagementApi, watch) {
24
24
  super()
25
25
  this.appConfig = appConfig
26
26
  this.#watch = watch
@@ -38,6 +38,7 @@ class PlatformaticApp extends EventEmitter {
38
38
  isProduction: this.appConfig.isProduction,
39
39
  telemetryConfig,
40
40
  metricsConfig,
41
+ loggerConfig,
41
42
  serverConfig,
42
43
  hasManagementApi: !!hasManagementApi,
43
44
  localServiceEnvVars: this.appConfig.localServiceEnvVars
@@ -102,11 +103,7 @@ class PlatformaticApp extends EventEmitter {
102
103
 
103
104
  const metricsConfig = this.#context.metricsConfig
104
105
  if (metricsConfig !== false) {
105
- this.#metricsRegistry = await collectMetrics(
106
- this.stackable,
107
- this.appConfig.id,
108
- metricsConfig
109
- )
106
+ this.#metricsRegistry = await collectMetrics(this.stackable, this.appConfig.id, metricsConfig)
110
107
  }
111
108
 
112
109
  this.#updateDispatcher()
@@ -183,9 +180,7 @@ class PlatformaticApp extends EventEmitter {
183
180
  async getMetrics ({ format }) {
184
181
  if (!this.#metricsRegistry) return null
185
182
 
186
- return format === 'json'
187
- ? this.#metricsRegistry.getMetricsAsJSON()
188
- : this.#metricsRegistry.metrics()
183
+ return format === 'json' ? this.#metricsRegistry.getMetricsAsJSON() : this.#metricsRegistry.metrics()
189
184
  }
190
185
 
191
186
  #fetchServiceUrl (key, { parent, context: service }) {
@@ -10,6 +10,7 @@ const defaultStackable = {
10
10
  getUrl: () => null,
11
11
  updateContext: () => {},
12
12
  getConfig: () => null,
13
+ getEnv: () => null,
13
14
  getInfo: () => null,
14
15
  getDispatchFunc: () => null,
15
16
  getOpenapiSchema: () => null,
@@ -17,7 +18,7 @@ const defaultStackable = {
17
18
  getMeta: () => ({}),
18
19
  collectMetrics: () => ({
19
20
  defaultMetrics: true,
20
- httpMetrics: true,
21
+ httpMetrics: true
21
22
  }),
22
23
  inject: () => {
23
24
  throw new Error('Stackable inject not implemented')
@@ -26,7 +27,7 @@ const defaultStackable = {
26
27
  console.log(message)
27
28
  },
28
29
  getBootstrapDependencies: () => [],
29
- getWatchConfig: () => ({ enabled: false }),
30
+ getWatchConfig: () => ({ enabled: false })
30
31
  }
31
32
 
32
33
  module.exports = defaultStackable
package/lib/worker/itc.js CHANGED
@@ -9,14 +9,14 @@ const { Unpromise } = require('@watchable/unpromise')
9
9
  const errors = require('../errors')
10
10
  const { kITC, kId } = require('./symbols')
11
11
 
12
- async function sendViaITC (worker, name, message) {
12
+ async function safeHandleInITC (worker, fn) {
13
13
  try {
14
14
  // Make sure to catch when the worker exits, otherwise we're stuck forever
15
15
  const ac = new AbortController()
16
16
  let exitCode
17
17
 
18
18
  const response = await Unpromise.race([
19
- worker[kITC].send(name, message),
19
+ fn(),
20
20
  once(worker, 'exit', { signal: ac.signal }).then(([code]) => {
21
21
  exitCode = code
22
22
  })
@@ -42,6 +42,14 @@ async function sendViaITC (worker, name, message) {
42
42
  }
43
43
  }
44
44
 
45
+ async function sendViaITC (worker, name, message) {
46
+ return safeHandleInITC(worker, () => worker[kITC].send(name, message))
47
+ }
48
+
49
+ async function waitEventFromITC (worker, event) {
50
+ return safeHandleInITC(worker, () => once(worker[kITC], event))
51
+ }
52
+
45
53
  function setupITC (app, service, dispatcher) {
46
54
  const itc = new ITC({
47
55
  name: app.appConfig.id + '-worker',
@@ -101,6 +109,11 @@ function setupITC (app, service, dispatcher) {
101
109
  return JSON.parse(JSON.stringify(current))
102
110
  },
103
111
 
112
+ async getServiceEnv () {
113
+ // Remove all undefined keys from the config
114
+ return JSON.parse(JSON.stringify({ ...process.env, ...(await app.stackable.getEnv()) }))
115
+ },
116
+
104
117
  async getServiceOpenAPISchema () {
105
118
  try {
106
119
  return await app.stackable.getOpenapiSchema()
@@ -146,4 +159,4 @@ function setupITC (app, service, dispatcher) {
146
159
  return itc
147
160
  }
148
161
 
149
- module.exports = { sendViaITC, setupITC }
162
+ module.exports = { sendViaITC, setupITC, waitEventFromITC }
@@ -12,7 +12,12 @@ const { wire } = require('undici-thread-interceptor')
12
12
  const { PlatformaticApp } = require('./app')
13
13
  const { setupITC } = require('./itc')
14
14
  const loadInterceptors = require('./interceptors')
15
- const { MessagePortWritable, createPinoWritable, executeWithTimeout, errors } = require('@platformatic/utils')
15
+ const {
16
+ MessagePortWritable,
17
+ createPinoWritable,
18
+ executeWithTimeout,
19
+ ensureLoggableError
20
+ } = require('@platformatic/utils')
16
21
  const { kId, kITC } = require('./symbols')
17
22
 
18
23
  process.on('uncaughtException', handleUnhandled.bind(null, 'uncaught exception'))
@@ -27,7 +32,7 @@ globalThis.platformatic = Object.assign(globalThis.platformatic ?? {}, { logger:
27
32
 
28
33
  function handleUnhandled (type, err) {
29
34
  globalThis.platformatic.logger.error(
30
- { err: errors.ensureLoggableError(err) },
35
+ { err: ensureLoggableError(err) },
31
36
  `Service ${workerData.serviceConfig.id} threw an ${type}.`
32
37
  )
33
38
 
@@ -106,10 +111,11 @@ async function main () {
106
111
  app = new PlatformaticApp(
107
112
  service,
108
113
  telemetryConfig,
114
+ config.logger,
109
115
  serverConfig,
116
+ config.metrics,
110
117
  !!config.managementApi,
111
- !!config.watch,
112
- config.metrics
118
+ !!config.watch
113
119
  )
114
120
 
115
121
  await app.init()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/runtime",
3
- "version": "2.0.0-alpha.15",
3
+ "version": "2.0.0-alpha.16",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -34,11 +34,11 @@
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": "2.0.0-alpha.15",
38
- "@platformatic/db": "2.0.0-alpha.15",
39
- "@platformatic/sql-graphql": "2.0.0-alpha.15",
40
- "@platformatic/service": "2.0.0-alpha.15",
41
- "@platformatic/sql-mapper": "2.0.0-alpha.15"
37
+ "@platformatic/composer": "2.0.0-alpha.16",
38
+ "@platformatic/db": "2.0.0-alpha.16",
39
+ "@platformatic/service": "2.0.0-alpha.16",
40
+ "@platformatic/sql-graphql": "2.0.0-alpha.16",
41
+ "@platformatic/sql-mapper": "2.0.0-alpha.16"
42
42
  },
43
43
  "dependencies": {
44
44
  "@fastify/error": "^4.0.0",
@@ -68,17 +68,17 @@
68
68
  "undici": "^6.9.0",
69
69
  "undici-thread-interceptor": "^0.6.1",
70
70
  "ws": "^8.16.0",
71
- "@platformatic/basic": "2.0.0-alpha.15",
72
- "@platformatic/generators": "2.0.0-alpha.15",
73
- "@platformatic/config": "2.0.0-alpha.15",
74
- "@platformatic/itc": "2.0.0-alpha.15",
75
- "@platformatic/telemetry": "2.0.0-alpha.15",
76
- "@platformatic/ts-compiler": "2.0.0-alpha.15",
77
- "@platformatic/utils": "2.0.0-alpha.15"
71
+ "@platformatic/basic": "2.0.0-alpha.16",
72
+ "@platformatic/config": "2.0.0-alpha.16",
73
+ "@platformatic/generators": "2.0.0-alpha.16",
74
+ "@platformatic/itc": "2.0.0-alpha.16",
75
+ "@platformatic/telemetry": "2.0.0-alpha.16",
76
+ "@platformatic/utils": "2.0.0-alpha.16",
77
+ "@platformatic/ts-compiler": "2.0.0-alpha.16"
78
78
  },
79
79
  "scripts": {
80
80
  "test": "npm run lint && borp --concurrency=1 --timeout=180000 && tsd",
81
- "coverage": "npm run lint && borp -X=fixtures -X=test -C --concurrency=1 --timeout=180000 && tsd",
81
+ "coverage": "npm run lint && borp -X fixtures -X test -C --concurrency=1 --timeout=180000 && tsd",
82
82
  "gen-schema": "node lib/schema.js > schema.json",
83
83
  "gen-types": "json2ts > config.d.ts < schema.json",
84
84
  "build": "pnpm run gen-schema && pnpm run gen-types",