@platformatic/runtime 2.0.0-alpha.2 → 2.0.0-alpha.4
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 +285 -0
- package/eslint.config.js +8 -0
- package/fixtures/botched-start/platformatic.runtime.json +1 -1
- package/fixtures/botched-start/services/a/platformatic.service.json +1 -1
- package/fixtures/composerApp/platformatic.composer.json +1 -1
- package/fixtures/configs/invalid-autoload-with-services.json +1 -1
- package/fixtures/configs/invalid-entrypoint.json +1 -1
- package/fixtures/configs/invalid-schema-type.config.json +1 -1
- package/fixtures/configs/missing-property.config.json +1 -1
- package/fixtures/configs/missing-service-config.json +1 -1
- package/fixtures/configs/monorepo-composer-no-autoload.json +2 -2
- package/fixtures/configs/monorepo-composer.json +2 -2
- package/fixtures/configs/monorepo-create-cycle.json +2 -2
- package/fixtures/configs/monorepo-missing-dependencies.json +2 -2
- package/fixtures/configs/monorepo-no-cycles.json +2 -2
- package/fixtures/configs/monorepo-openapi.json +2 -2
- package/fixtures/configs/{monorepo-hotreload-env.json → monorepo-watch-env.json} +2 -2
- package/fixtures/configs/monorepo-watch-single.json +12 -0
- package/fixtures/configs/monorepo-watch.json +26 -9
- package/fixtures/configs/monorepo-with-dependencies.json +2 -2
- package/fixtures/configs/monorepo-with-management-api-without-metrics.json +21 -0
- package/fixtures/configs/monorepo-with-management-api.json +2 -2
- package/fixtures/configs/{monorepo-hotreload.json → monorepo-with-metrics.json} +5 -4
- package/fixtures/configs/monorepo.json +2 -2
- package/fixtures/configs/no-services.config.json +1 -1
- package/fixtures/configs/no-sources.config.json +1 -1
- package/fixtures/configs/service-throws-on-start.json +1 -1
- package/fixtures/configs/service-with-env-port.json +2 -2
- package/fixtures/configs/service-with-stdio.json +12 -0
- package/fixtures/configs/{hotreload.json → watch.json} +2 -2
- package/fixtures/crash-on-bootstrap/platformatic.runtime.json +15 -0
- package/fixtures/crash-on-bootstrap/services/service-1/platformatic.service.json +14 -0
- package/fixtures/crash-on-bootstrap/services/service-1/plugin.js +5 -0
- package/fixtures/crash-on-bootstrap/services/service-2/platformatic.service.json +14 -0
- package/fixtures/crash-on-bootstrap/services/service-2/plugin.js +5 -0
- package/fixtures/dbApp/platformatic.db.json +1 -1
- package/fixtures/dbAppNoName/platformatic.db.json +1 -1
- package/fixtures/dbAppNoPackageJson/platformatic.db.json +1 -1
- package/fixtures/dbAppWithMigrationError/platformatic.db.json +1 -1
- package/fixtures/do-not-reload-dependencies/platformatic.service.json +1 -1
- package/fixtures/do-not-restart-on-crash/platformatic.runtime.json +3 -2
- package/fixtures/do-not-restart-on-crash/services/a/platformatic.service.json +1 -1
- package/fixtures/express/platformatic.runtime.json +1 -1
- package/fixtures/express/services/a/platformatic.service.json +1 -1
- package/fixtures/express/services/b/platformatic.service.json +1 -1
- package/fixtures/external-client/platformatic.service.json +1 -1
- package/fixtures/interceptors/idp.js +2 -2
- package/fixtures/interceptors/platformatic.runtime.json +1 -1
- package/fixtures/interceptors/services/a/platformatic.service.json +1 -1
- package/fixtures/interceptors-2/platformatic.runtime.json +1 -1
- package/fixtures/interceptors-2/services/a/platformatic.service.json +1 -1
- package/fixtures/leven/platformatic.runtime.json +2 -2
- package/fixtures/leven/services/deeply-spittle/platformatic.service.json +1 -1
- package/fixtures/leven/services/rainy-empire/platformatic.composer.json +1 -1
- package/fixtures/management-api/platformatic.json +3 -3
- package/fixtures/management-api/services/service-1/platformatic.json +1 -1
- package/fixtures/management-api/services/service-1/plugin.js +4 -3
- package/fixtures/management-api/services/service-2/platformatic.json +1 -1
- package/fixtures/management-api/services/service-db/platformatic.db.json +1 -1
- package/fixtures/management-api-custom-labels/platformatic.json +2 -2
- package/fixtures/management-api-custom-labels/services/service-1/platformatic.json +1 -1
- package/fixtures/management-api-custom-labels/services/service-1/plugin.js +4 -3
- package/fixtures/management-api-custom-labels/services/service-2/platformatic.json +1 -1
- package/fixtures/management-api-custom-labels/services/service-db/platformatic.db.json +1 -1
- package/fixtures/management-api-without-metrics/platformatic.json +3 -2
- package/fixtures/management-api-without-metrics/services/service-1/platformatic.json +1 -1
- package/fixtures/monorepo/composerApp/platformatic.composer.json +1 -1
- package/fixtures/monorepo/dbApp/platformatic.db.json +1 -1
- package/fixtures/monorepo/serviceApp/platformatic.service.json +3 -2
- package/fixtures/monorepo/serviceApp/with-logger/with-logger.cjs +2 -2
- package/fixtures/monorepo/serviceApp/with-logger/with-logger.d.ts +7 -7
- package/fixtures/monorepo/serviceAppWithLogger/platformatic.service.json +1 -1
- package/fixtures/monorepo/serviceAppWithLogger/plugin.js +12 -0
- package/fixtures/monorepo/serviceAppWithMultiplePlugins/platformatic.service.json +3 -2
- package/fixtures/monorepo-missing-dependencies/composer/platformatic.json +1 -1
- package/fixtures/monorepo-openapi/serviceAppWithoutOpenapi/platformatic.service.json +1 -1
- package/fixtures/monorepo-watch/service1/platformatic.service.json +1 -1
- package/fixtures/monorepo-with-dependencies/main/platformatic.json +1 -1
- package/fixtures/monorepo-with-dependencies/service-1/platformatic.json +1 -1
- package/fixtures/monorepo-with-dependencies/service-2/platformatic.json +1 -1
- package/fixtures/no-env.service.json +1 -1
- package/fixtures/preload/platformatic.runtime.json +1 -1
- package/fixtures/preload/services/a/platformatic.service.json +1 -1
- package/fixtures/prom-server/platformatic.json +2 -2
- package/fixtures/prom-server/services/service-1/platformatic.json +1 -1
- package/fixtures/prom-server/services/service-2/platformatic.json +1 -1
- package/fixtures/restart-on-crash/platformatic.runtime.json +1 -1
- package/fixtures/restart-on-crash/services/a/platformatic.service.json +1 -1
- package/fixtures/sample-runtime/package.json +1 -1
- package/fixtures/sample-runtime/platformatic.json +2 -2
- package/fixtures/sample-runtime/services/rival/package.json +1 -1
- package/fixtures/sample-runtime/services/rival/platformatic.json +1 -1
- package/fixtures/sample-runtime-with-2-services/package.json +1 -1
- package/fixtures/sample-runtime-with-2-services/platformatic.json +2 -2
- package/fixtures/sample-runtime-with-2-services/services/foobar/package.json +1 -1
- package/fixtures/sample-runtime-with-2-services/services/foobar/platformatic.json +1 -1
- package/fixtures/sample-runtime-with-2-services/services/rival/package.json +1 -1
- package/fixtures/sample-runtime-with-2-services/services/rival/platformatic.json +1 -1
- package/fixtures/server/logger-transport/platformatic.runtime.json +2 -2
- package/fixtures/server/logger-transport/services/echo/platformatic.service.json +1 -1
- package/fixtures/server/overrides-service/platformatic.runtime.json +2 -2
- package/fixtures/server/overrides-service/services/echo/platformatic.service.json +1 -1
- package/fixtures/server/runtime-server/platformatic.runtime.json +2 -2
- package/fixtures/server/runtime-server/services/echo/platformatic.service.json +1 -1
- package/fixtures/serviceAppThrowsOnStart/platformatic.service.json +1 -1
- package/fixtures/stackables/node_modules/foo/foo.js +2 -1
- package/fixtures/start-command-in-runtime.js +1 -1
- package/fixtures/stdio/platformatic.service.json +6 -0
- package/fixtures/stdio/plugin.js +24 -0
- package/fixtures/telemetry/platformatic.runtime.json +2 -2
- package/fixtures/telemetry/services/echo/platformatic.service.json +1 -1
- package/fixtures/telemetry/services/echo/routes/span.js +16 -2
- package/fixtures/telemetry/services/service-1/platformatic.service.json +19 -0
- package/fixtures/telemetry/services/service-1/routes/echo.js +7 -0
- package/fixtures/typescript/platformatic.runtime.json +2 -2
- package/fixtures/typescript/services/composer/platformatic.composer.json +1 -1
- package/fixtures/typescript/services/movies/global.d.ts +2 -3
- package/fixtures/typescript/services/movies/platformatic.db.json +1 -1
- package/fixtures/typescript/services/movies/types/Movie.d.ts +3 -3
- package/fixtures/typescript/services/movies/types/index.d.ts +6 -6
- package/fixtures/typescript/services/titles/client/client.d.ts +35 -35
- package/fixtures/typescript/services/titles/platformatic.service.json +1 -1
- package/fixtures/typescript-custom-flags/platformatic.runtime.json +2 -2
- package/fixtures/typescript-custom-flags/services/composer/platformatic.composer.json +1 -1
- package/fixtures/typescript-custom-flags/services/movies/global.d.ts +2 -3
- package/fixtures/typescript-custom-flags/services/movies/platformatic.db.json +1 -1
- package/fixtures/typescript-custom-flags/services/movies/types/Movie.d.ts +3 -3
- package/fixtures/typescript-custom-flags/services/movies/types/index.d.ts +6 -6
- package/fixtures/typescript-custom-flags/services/titles/client/client.d.ts +35 -35
- package/fixtures/typescript-custom-flags/services/titles/platformatic.service.json +1 -1
- package/fixtures/typescript-no-env/platformatic.runtime.json +2 -2
- package/fixtures/typescript-no-env/services/composer/platformatic.composer.json +1 -1
- package/fixtures/typescript-no-env/services/movies/global.d.ts +2 -3
- package/fixtures/typescript-no-env/services/movies/platformatic.db.json +1 -1
- package/fixtures/typescript-no-env/services/movies/types/Movie.d.ts +3 -3
- package/fixtures/typescript-no-env/services/movies/types/index.d.ts +6 -6
- package/fixtures/typescript-no-env/services/titles/client/client.d.ts +35 -35
- package/fixtures/typescript-no-env/services/titles/platformatic.service.json +1 -1
- package/index.d.ts +7 -8
- package/index.js +14 -10
- package/index.test-d.ts +10 -12
- package/lib/build-server.js +5 -11
- package/lib/compile.js +11 -10
- package/lib/config.js +21 -14
- package/lib/dependencies.js +2 -1
- package/lib/errors.js +3 -2
- package/lib/generator/errors.js +1 -1
- package/lib/generator/runtime-generator.d.ts +15 -15
- package/lib/generator/runtime-generator.js +92 -63
- package/lib/logger.js +55 -0
- package/lib/management-api.js +29 -44
- package/lib/prom-server.js +5 -9
- package/lib/runtime.js +955 -0
- package/lib/schema.js +79 -76
- package/lib/start.js +35 -113
- package/lib/upgrade.js +4 -3
- package/lib/utils.js +49 -1
- package/lib/versions/v1.36.0.js +1 -1
- package/lib/versions/v1.5.0.js +1 -1
- package/lib/versions/v2.0.0.js +17 -0
- package/lib/worker/app.js +250 -0
- package/lib/worker/default-stackable.js +27 -0
- package/lib/worker/itc.js +128 -0
- package/lib/worker/main.js +127 -0
- package/lib/worker/symbols.js +7 -0
- package/package.json +25 -25
- package/runtime.mjs +4 -4
- package/schema.json +824 -0
- package/lib/api-client.js +0 -500
- package/lib/api.js +0 -420
- package/lib/app.js +0 -397
- package/lib/load-config.js +0 -12
- package/lib/loader.mjs +0 -103
- package/lib/message-port-writable.js +0 -50
- package/lib/worker.js +0 -182
- /package/lib/{interceptors.js → worker/interceptors.js} +0 -0
package/lib/api.js
DELETED
|
@@ -1,420 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { once } = require('node:events')
|
|
4
|
-
const { getGlobalDispatcher, setGlobalDispatcher } = require('undici')
|
|
5
|
-
const { createFastifyInterceptor } = require('fastify-undici-dispatcher')
|
|
6
|
-
const { PlatformaticApp } = require('./app')
|
|
7
|
-
const errors = require('./errors')
|
|
8
|
-
const { checkDependencies, topologicalSort } = require('./dependencies')
|
|
9
|
-
const { printSchema } = require('graphql')
|
|
10
|
-
|
|
11
|
-
class RuntimeApi {
|
|
12
|
-
#services
|
|
13
|
-
#dispatcher
|
|
14
|
-
#interceptor
|
|
15
|
-
#logger
|
|
16
|
-
#config
|
|
17
|
-
#bootstrapDependenciesResolved
|
|
18
|
-
|
|
19
|
-
constructor (config, logger, loaderPort, composedInterceptors = []) {
|
|
20
|
-
this.#config = config
|
|
21
|
-
this.#services = new Map()
|
|
22
|
-
this.#logger = logger
|
|
23
|
-
|
|
24
|
-
const telemetryConfig = config.telemetry
|
|
25
|
-
const metricsConfig = config.metrics
|
|
26
|
-
|
|
27
|
-
for (let i = 0; i < config.services.length; ++i) {
|
|
28
|
-
const service = config.services[i]
|
|
29
|
-
const serviceTelemetryConfig = telemetryConfig
|
|
30
|
-
? {
|
|
31
|
-
...telemetryConfig,
|
|
32
|
-
serviceName: `${telemetryConfig.serviceName}-${service.id}`
|
|
33
|
-
}
|
|
34
|
-
: null
|
|
35
|
-
|
|
36
|
-
// If the service is an entrypoint and runtime server config is defined, use it.
|
|
37
|
-
let serverConfig = null
|
|
38
|
-
if (config.server && service.entrypoint) {
|
|
39
|
-
serverConfig = config.server
|
|
40
|
-
} else if (service.useHttp) {
|
|
41
|
-
serverConfig = {
|
|
42
|
-
port: 0,
|
|
43
|
-
host: '127.0.0.1',
|
|
44
|
-
keepAliveTimeout: 5000
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const app = new PlatformaticApp(service, loaderPort, logger, serviceTelemetryConfig, serverConfig, !!config.managementApi, metricsConfig)
|
|
49
|
-
|
|
50
|
-
this.#services.set(service.id, app)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
this.#interceptor = createFastifyInterceptor({
|
|
54
|
-
// setting the domain here allows for fail-fast scenarios
|
|
55
|
-
domain: '.plt.local'
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
composedInterceptors.unshift(this.#interceptor)
|
|
59
|
-
|
|
60
|
-
this.#dispatcher = getGlobalDispatcher().compose(composedInterceptors)
|
|
61
|
-
setGlobalDispatcher(this.#dispatcher)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async startListening (parentPort) {
|
|
65
|
-
parentPort.on('message', async (message) => {
|
|
66
|
-
const command = message?.command
|
|
67
|
-
if (command) {
|
|
68
|
-
if (command === 'plt:close') {
|
|
69
|
-
// We close everything because they might be using
|
|
70
|
-
// a FinalizationRegistry and it may stuck us in an infinite loop.
|
|
71
|
-
// This is a workaround for
|
|
72
|
-
// https://github.com/nodejs/node/issues/47748
|
|
73
|
-
// https://github.com/nodejs/node/issues/49344
|
|
74
|
-
// Remove once https://github.com/nodejs/node/pull/51290 is released
|
|
75
|
-
// on all lines.
|
|
76
|
-
// Likely to be removed when we drop support for Node.js 18.
|
|
77
|
-
if (this.#dispatcher) {
|
|
78
|
-
await this.#dispatcher.close()
|
|
79
|
-
}
|
|
80
|
-
setImmediate(process.exit) // Exit the worker thread.
|
|
81
|
-
return
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const res = await this.#executeCommand(message)
|
|
85
|
-
parentPort.postMessage(res)
|
|
86
|
-
return
|
|
87
|
-
}
|
|
88
|
-
await this.#handleProcessLevelEvent(message)
|
|
89
|
-
})
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async #handleProcessLevelEvent (message) {
|
|
93
|
-
const services = [...this.#services.values()]
|
|
94
|
-
await Promise.allSettled(services.map(async (service) => {
|
|
95
|
-
await service.handleProcessLevelEvent(message)
|
|
96
|
-
}))
|
|
97
|
-
|
|
98
|
-
for (const service of services) {
|
|
99
|
-
const serviceStatus = service.getStatus()
|
|
100
|
-
if (
|
|
101
|
-
serviceStatus === 'starting' ||
|
|
102
|
-
serviceStatus === 'started'
|
|
103
|
-
) return
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (this.#dispatcher) {
|
|
107
|
-
await this.#dispatcher.close()
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
process.exit() // Exit the worker thread if all services are stopped
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
async #executeCommand (message) {
|
|
114
|
-
const { operationId, command, params } = message
|
|
115
|
-
try {
|
|
116
|
-
const res = await this.#runCommandHandler(command, params)
|
|
117
|
-
return { operationId, error: null, data: JSON.stringify(res || null) }
|
|
118
|
-
} catch (err) {
|
|
119
|
-
return { operationId, error: err.message, code: err.code }
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
async #runCommandHandler (command, params) {
|
|
124
|
-
switch (command) {
|
|
125
|
-
case 'plt:start-services':
|
|
126
|
-
return this.startServices(params)
|
|
127
|
-
case 'plt:stop-services':
|
|
128
|
-
return this.stopServices(params)
|
|
129
|
-
case 'plt:restart-services':
|
|
130
|
-
return this.#restartServices(params)
|
|
131
|
-
case 'plt:get-entrypoint-details':
|
|
132
|
-
return this.#getEntrypointDetails(params)
|
|
133
|
-
case 'plt:get-services':
|
|
134
|
-
return this.#getServices(params)
|
|
135
|
-
case 'plt:get-service-details':
|
|
136
|
-
return this.#getServiceDetails(params)
|
|
137
|
-
case 'plt:get-service-config':
|
|
138
|
-
return this.#getServiceConfig(params)
|
|
139
|
-
case 'plt:get-service-openapi-schema':
|
|
140
|
-
return this.#getServiceOpenapiSchema(params)
|
|
141
|
-
case 'plt:get-service-graphql-schema':
|
|
142
|
-
return this.#getServiceGraphqlSchema(params)
|
|
143
|
-
case 'plt:get-metrics':
|
|
144
|
-
return this.#getMetrics(params)
|
|
145
|
-
case 'plt:start-service':
|
|
146
|
-
return this.#startService(params)
|
|
147
|
-
case 'plt:stop-service':
|
|
148
|
-
return this.#stopService(params)
|
|
149
|
-
case 'plt:inject':
|
|
150
|
-
return this.#inject(params)
|
|
151
|
-
/* c8 ignore next 2 */
|
|
152
|
-
default:
|
|
153
|
-
throw new errors.UnknownRuntimeAPICommandError(command)
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
async startServices () {
|
|
158
|
-
if (!this.#bootstrapDependenciesResolved) {
|
|
159
|
-
await this._resolveBootstrapDependencies()
|
|
160
|
-
this.#bootstrapDependenciesResolved = true
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
let entrypoint
|
|
164
|
-
for (const service of this.#services.values()) {
|
|
165
|
-
await service.start()
|
|
166
|
-
|
|
167
|
-
if (service.appConfig.entrypoint) {
|
|
168
|
-
entrypoint = service
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
const serviceUrl = new URL(service.appConfig.localUrl)
|
|
172
|
-
this.#interceptor.route(serviceUrl.host, service.server)
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
await entrypoint?.listen()
|
|
176
|
-
return entrypoint?.server.url
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
async stopServices () {
|
|
180
|
-
const stopServiceReqs = []
|
|
181
|
-
for (const service of this.#services.values()) {
|
|
182
|
-
const serviceStatus = service.getStatus()
|
|
183
|
-
if (serviceStatus === 'starting') {
|
|
184
|
-
stopServiceReqs.push(
|
|
185
|
-
once(service, 'start').then(() => service.stop())
|
|
186
|
-
)
|
|
187
|
-
}
|
|
188
|
-
if (serviceStatus === 'started') {
|
|
189
|
-
stopServiceReqs.push(service.stop())
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
await Promise.all(stopServiceReqs)
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
async _resolveBootstrapDependencies () {
|
|
197
|
-
if (this.#config.autoload) {
|
|
198
|
-
for (const service of this.#config.services) {
|
|
199
|
-
const dependencies = await this.#services.get(service.id).getBootstrapDependencies()
|
|
200
|
-
service.dependencies = dependencies
|
|
201
|
-
|
|
202
|
-
for (const { envVar, url } of dependencies) {
|
|
203
|
-
if (envVar) {
|
|
204
|
-
service.localServiceEnvVars.set(envVar, url)
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
checkDependencies(this.#config.services)
|
|
210
|
-
this.#services = topologicalSort(this.#services, this.#config)
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
return this.#services
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
async #restartServices () {
|
|
217
|
-
let entrypoint
|
|
218
|
-
for (const service of this.#services.values()) {
|
|
219
|
-
const serviceStatus = service.getStatus()
|
|
220
|
-
|
|
221
|
-
if (service.appConfig.entrypoint) {
|
|
222
|
-
entrypoint = service
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
if (serviceStatus === 'starting') {
|
|
226
|
-
await once(service, 'start')
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
await service.restart()
|
|
230
|
-
|
|
231
|
-
const serviceUrl = new URL(service.appConfig.localUrl)
|
|
232
|
-
this.#interceptor.route(serviceUrl.host, service.server)
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
return entrypoint?.server.url
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
#getEntrypointDetails () {
|
|
239
|
-
for (const service of this.#services.values()) {
|
|
240
|
-
if (service.appConfig.entrypoint) {
|
|
241
|
-
return this.#getServiceDetails({ id: service.appConfig.id })
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
return null
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
#getServices () {
|
|
248
|
-
const services = { services: [] }
|
|
249
|
-
|
|
250
|
-
for (const service of this.#services.values()) {
|
|
251
|
-
const serviceId = service.appConfig.id
|
|
252
|
-
const serviceDetails = this.#getServiceDetails({ id: serviceId })
|
|
253
|
-
if (serviceDetails.entrypoint) {
|
|
254
|
-
services.entrypoint = serviceId
|
|
255
|
-
}
|
|
256
|
-
services.services.push(serviceDetails)
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
return services
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
#getServiceById (id) {
|
|
263
|
-
const service = this.#services.get(id)
|
|
264
|
-
|
|
265
|
-
if (!service) {
|
|
266
|
-
const listOfServices = this.#getServices().services.map(svc => svc.id).join(', ')
|
|
267
|
-
throw new errors.ServiceNotFoundError(listOfServices)
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
return service
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
#getServiceDetails ({ id }) {
|
|
274
|
-
const service = this.#getServiceById(id)
|
|
275
|
-
const status = service.getStatus()
|
|
276
|
-
|
|
277
|
-
const type = service.config?.configType
|
|
278
|
-
const version = service.config?.app?.configManagerConfig.version ?? null
|
|
279
|
-
const { entrypoint, dependencies, localUrl } = service.appConfig
|
|
280
|
-
|
|
281
|
-
const serviceDetails = {
|
|
282
|
-
id,
|
|
283
|
-
type,
|
|
284
|
-
status,
|
|
285
|
-
version,
|
|
286
|
-
localUrl,
|
|
287
|
-
entrypoint,
|
|
288
|
-
dependencies
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
if (entrypoint) {
|
|
292
|
-
serviceDetails.url = status === 'started' ? service.server.url : null
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
return serviceDetails
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
#getServiceConfig ({ id }) {
|
|
299
|
-
const service = this.#getServiceById(id)
|
|
300
|
-
|
|
301
|
-
const serviceStatus = service.getStatus()
|
|
302
|
-
if (serviceStatus !== 'started') {
|
|
303
|
-
throw new errors.ServiceNotStartedError(id)
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
return service.config.configManager.current
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
async #getServiceOpenapiSchema ({ id }) {
|
|
310
|
-
const service = this.#getServiceById(id)
|
|
311
|
-
|
|
312
|
-
const serviceStatus = service.getStatus()
|
|
313
|
-
if (serviceStatus !== 'started') {
|
|
314
|
-
throw new errors.ServiceNotStartedError(id)
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
if (typeof service.server.swagger !== 'function') {
|
|
318
|
-
return null
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
try {
|
|
322
|
-
await service.server.ready()
|
|
323
|
-
const openapiSchema = service.server.swagger()
|
|
324
|
-
return openapiSchema
|
|
325
|
-
} catch (err) {
|
|
326
|
-
throw new errors.FailedToRetrieveOpenAPISchemaError(id, err.message)
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
async #getServiceGraphqlSchema ({ id }) {
|
|
331
|
-
const service = this.#getServiceById(id)
|
|
332
|
-
|
|
333
|
-
const serviceStatus = service.getStatus()
|
|
334
|
-
if (serviceStatus !== 'started') {
|
|
335
|
-
throw new errors.ServiceNotStartedError(id)
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
if (typeof service.server.graphql !== 'function') {
|
|
339
|
-
return null
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
try {
|
|
343
|
-
await service.server.ready()
|
|
344
|
-
const graphqlSchema = printSchema(service.server.graphql.schema)
|
|
345
|
-
return graphqlSchema
|
|
346
|
-
} catch (err) {
|
|
347
|
-
throw new errors.FailedToRetrieveGraphQLSchemaError(id, err.message)
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
async #getMetrics ({ format }) {
|
|
352
|
-
let metrics = null
|
|
353
|
-
|
|
354
|
-
for (const service of this.#services.values()) {
|
|
355
|
-
const serviceId = service.appConfig.id
|
|
356
|
-
const serviceStatus = service.getStatus()
|
|
357
|
-
if (serviceStatus !== 'started') {
|
|
358
|
-
throw new errors.ServiceNotStartedError(serviceId)
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
const promRegister = service.server.metrics?.client?.register
|
|
362
|
-
|
|
363
|
-
if (promRegister) {
|
|
364
|
-
if (metrics === null) {
|
|
365
|
-
metrics = format === 'json' ? [] : ''
|
|
366
|
-
}
|
|
367
|
-
if (format === 'json') {
|
|
368
|
-
metrics.push(...await promRegister.getMetricsAsJSON())
|
|
369
|
-
} else {
|
|
370
|
-
const serviceMetrics = await promRegister.metrics()
|
|
371
|
-
metrics += serviceMetrics
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
return { metrics }
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
async #startService ({ id }) {
|
|
380
|
-
const service = this.#getServiceById(id)
|
|
381
|
-
const serviceStatus = service.getStatus()
|
|
382
|
-
|
|
383
|
-
if (serviceStatus === 'starting') {
|
|
384
|
-
await once(service, 'start')
|
|
385
|
-
} else {
|
|
386
|
-
await service.start()
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
async #stopService ({ id }) {
|
|
391
|
-
const service = this.#getServiceById(id)
|
|
392
|
-
const serviceStatus = service.getStatus()
|
|
393
|
-
|
|
394
|
-
if (serviceStatus === 'starting') {
|
|
395
|
-
await once(service, 'start')
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
await service.stop()
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
async #inject ({ id, injectParams }) {
|
|
402
|
-
const service = this.#getServiceById(id)
|
|
403
|
-
|
|
404
|
-
const serviceStatus = service.getStatus()
|
|
405
|
-
if (serviceStatus !== 'started') {
|
|
406
|
-
throw new errors.ServiceNotStartedError(id)
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
const res = await service.server.inject(injectParams)
|
|
410
|
-
// Return only serializable properties.
|
|
411
|
-
return {
|
|
412
|
-
statusCode: res.statusCode,
|
|
413
|
-
statusMessage: res.statusMessage,
|
|
414
|
-
headers: res.headers,
|
|
415
|
-
body: res.body
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
module.exports = RuntimeApi
|