@platformatic/runtime 1.53.3 → 2.0.0-alpha.10
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 +284 -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/{monorepo-hotreload.json → invalid-autoload-with-services.json} +8 -8
- 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 +32 -0
- package/fixtures/configs/monorepo-composer.json +2 -2
- package/fixtures/configs/monorepo-create-cycle.json +2 -3
- package/fixtures/configs/monorepo-missing-dependencies.json +9 -0
- package/fixtures/configs/monorepo-no-cycles.json +2 -3
- package/fixtures/configs/monorepo-openapi.json +2 -2
- package/fixtures/configs/{hotreload.json → monorepo-watch-env.json} +2 -3
- 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-client-without-id.json → monorepo-with-management-api-without-metrics.json} +5 -8
- package/fixtures/configs/monorepo-with-management-api.json +2 -3
- package/fixtures/configs/monorepo-with-metrics.json +1 -2
- package/fixtures/configs/monorepo.json +2 -3
- 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 -3
- package/fixtures/configs/service-with-stdio.json +12 -0
- package/fixtures/configs/{monorepo-hotreload-env.json → watch.json} +2 -3
- 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 -3
- 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 +8 -4
- 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 -3
- 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 -3
- package/fixtures/management-api-without-metrics/services/service-1/platformatic.json +1 -1
- package/fixtures/monorepo/composerApp/platformatic.composer.json +1 -2
- 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 +24 -0
- 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 -3
- 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 -3
- 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 -3
- 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 -3
- package/fixtures/server/logger-transport/services/echo/platformatic.service.json +1 -1
- package/fixtures/server/overrides-service/platformatic.runtime.json +2 -3
- package/fixtures/server/overrides-service/services/echo/platformatic.service.json +1 -1
- package/fixtures/server/runtime-server/platformatic.runtime.json +2 -3
- 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 -3
- 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 -3
- 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 -3
- 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 -3
- 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 +15 -10
- package/index.test-d.ts +10 -12
- package/lib/build-server.js +23 -15
- package/lib/compile.js +56 -13
- package/lib/config.js +27 -139
- package/lib/dependencies.js +59 -0
- package/lib/errors.js +6 -2
- package/lib/generator/errors.js +1 -1
- package/lib/generator/runtime-generator.d.ts +15 -15
- package/lib/generator/runtime-generator.js +81 -66
- package/lib/logger.js +55 -0
- package/lib/management-api.js +29 -44
- package/lib/prom-server.js +5 -9
- package/lib/runtime.js +1005 -0
- package/lib/schema.js +46 -44
- package/lib/start.js +49 -117
- 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 +266 -0
- package/lib/worker/default-stackable.js +32 -0
- package/lib/worker/itc.js +149 -0
- package/lib/worker/main.js +129 -0
- package/lib/worker/metrics.js +106 -0
- package/lib/worker/symbols.js +7 -0
- package/package.json +44 -44
- package/runtime.mjs +11 -6
- package/schema.json +25 -13
- package/fixtures/monorepo/serviceApp/platformatic.service-client-without-id.json +0 -21
- package/lib/api-client.js +0 -497
- package/lib/api.js +0 -397
- package/lib/app.js +0 -365
- package/lib/load-config.js +0 -20
- 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/config.js
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
1
|
'use strict'
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
const { readdir } = require('node:fs/promises')
|
|
3
4
|
const { join, resolve: pathResolve } = require('node:path')
|
|
4
|
-
|
|
5
|
-
const Topo = require('@hapi/topo')
|
|
5
|
+
|
|
6
6
|
const ConfigManager = require('@platformatic/config')
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
const errors = require('./errors')
|
|
9
|
+
const { schema } = require('./schema')
|
|
9
10
|
const upgrade = require('./upgrade')
|
|
10
11
|
|
|
12
|
+
const kServicesAutoloaded = Symbol('plt.servicesAutoloaded')
|
|
13
|
+
|
|
11
14
|
async function _transformConfig (configManager) {
|
|
12
15
|
const config = configManager.current
|
|
13
16
|
const services = config.services ?? []
|
|
14
17
|
|
|
15
18
|
if (config.autoload) {
|
|
19
|
+
if (config.services && !config.services[kServicesAutoloaded]) {
|
|
20
|
+
throw new errors.InvalidAutoloadWithServicesError()
|
|
21
|
+
}
|
|
22
|
+
|
|
16
23
|
const { exclude = [], mappings = {} } = config.autoload
|
|
17
24
|
let { path } = config.autoload
|
|
18
25
|
|
|
@@ -53,8 +60,6 @@ async function _transformConfig (configManager) {
|
|
|
53
60
|
}
|
|
54
61
|
}
|
|
55
62
|
|
|
56
|
-
configManager.current.allowCycles = !!configManager.current.allowCycles
|
|
57
|
-
|
|
58
63
|
configManager.current.serviceMap = new Map()
|
|
59
64
|
configManager.current.inspectorOptions = undefined
|
|
60
65
|
|
|
@@ -63,13 +68,12 @@ async function _transformConfig (configManager) {
|
|
|
63
68
|
for (let i = 0; i < services.length; ++i) {
|
|
64
69
|
const service = services[i]
|
|
65
70
|
|
|
66
|
-
if (configManager._fixPaths) {
|
|
71
|
+
if (configManager._fixPaths && service.config) {
|
|
67
72
|
service.config = pathResolve(service.path, service.config)
|
|
68
73
|
}
|
|
69
74
|
service.entrypoint = service.id === config.entrypoint
|
|
70
|
-
service.
|
|
75
|
+
service.watch = !!config.watch
|
|
71
76
|
service.dependencies = []
|
|
72
|
-
service.dependents = []
|
|
73
77
|
service.localServiceEnvVars = new Map()
|
|
74
78
|
service.localUrl = `http://${service.id}.plt.local`
|
|
75
79
|
|
|
@@ -85,126 +89,11 @@ async function _transformConfig (configManager) {
|
|
|
85
89
|
}
|
|
86
90
|
|
|
87
91
|
configManager.current.services = services
|
|
92
|
+
configManager.current.services[kServicesAutoloaded] = true
|
|
88
93
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (!configManager.current.allowCycles) {
|
|
92
|
-
topologicalSort(configManager)
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function missingDependencyErrorMessage (clientName, service, configManager) {
|
|
97
|
-
const closestName = closest(clientName, [...configManager.current.serviceMap.keys()])
|
|
98
|
-
let errorMsg = `service '${service.id}' has unknown dependency: '${clientName}'.`
|
|
99
|
-
if (closestName) {
|
|
100
|
-
errorMsg += ` Did you mean '${closestName}'?`
|
|
101
|
-
}
|
|
102
|
-
return errorMsg
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
async function parseClientsAndComposer (configManager) {
|
|
106
|
-
for (const service of configManager.current.services) {
|
|
107
|
-
const cm = new ConfigManager({ source: service.config })
|
|
108
|
-
const configString = await cm.load()
|
|
109
|
-
const serviceConfig = cm._parser(configString)
|
|
110
|
-
|
|
111
|
-
async function parseConfigUrl (urlString) {
|
|
112
|
-
if (!urlString) {
|
|
113
|
-
return { url: null, envVar: null }
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
try {
|
|
117
|
-
const url = await cm.replaceEnv(urlString)
|
|
118
|
-
return { url, envVar: null }
|
|
119
|
-
} catch (err) {
|
|
120
|
-
// The MissingValueError is an error coming from pupa
|
|
121
|
-
// https://github.com/sindresorhus/pupa#missingvalueerror
|
|
122
|
-
// All other errors are simply re-thrown.
|
|
123
|
-
if (err.name !== 'MissingValueError' || urlString !== `{${err.key}}`) {
|
|
124
|
-
throw err
|
|
125
|
-
}
|
|
126
|
-
return { url: null, envVar: err.key }
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
async function addServiceDependency (service, dependencyId, urlString) {
|
|
131
|
-
let { url, envVar } = await parseConfigUrl(urlString)
|
|
132
|
-
if (url !== null) {
|
|
133
|
-
service.dependencies.push({ id: dependencyId, url, local: false })
|
|
134
|
-
return
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const depService = configManager.current.serviceMap.get(dependencyId)
|
|
138
|
-
if (depService === undefined) {
|
|
139
|
-
throw new errors.MissingDependencyError(
|
|
140
|
-
missingDependencyErrorMessage(dependencyId, service, configManager)
|
|
141
|
-
)
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
url = `http://${dependencyId}.plt.local`
|
|
145
|
-
|
|
146
|
-
if (envVar !== null) {
|
|
147
|
-
service.localServiceEnvVars.set(envVar, url)
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
depService.dependents.push(service.id)
|
|
151
|
-
service.dependencies.push({ id: dependencyId, url, local: true })
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const composedServices = serviceConfig.composer?.services
|
|
155
|
-
if (Array.isArray(composedServices)) {
|
|
156
|
-
await Promise.all(
|
|
157
|
-
composedServices.map(async (composedService) =>
|
|
158
|
-
addServiceDependency(
|
|
159
|
-
service,
|
|
160
|
-
composedService.id,
|
|
161
|
-
composedService.origin
|
|
162
|
-
)
|
|
163
|
-
)
|
|
164
|
-
)
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (Array.isArray(serviceConfig.clients)) {
|
|
168
|
-
await Promise.all(
|
|
169
|
-
serviceConfig.clients.map(async (client) => {
|
|
170
|
-
let clientServiceId = client.serviceId
|
|
171
|
-
if (!clientServiceId) {
|
|
172
|
-
try {
|
|
173
|
-
const clientPath = pathResolve(service.path, client.path)
|
|
174
|
-
const clientPackageJsonPath = join(clientPath, 'package.json')
|
|
175
|
-
const clientPackageJSONFile = await readFile(clientPackageJsonPath, 'utf8')
|
|
176
|
-
const clientPackageJSON = JSON.parse(clientPackageJSONFile)
|
|
177
|
-
clientServiceId = clientPackageJSON.name ?? ''
|
|
178
|
-
} catch (err) {
|
|
179
|
-
if (client.url === undefined || client.name === undefined) {
|
|
180
|
-
throw err
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
await addServiceDependency(service, clientServiceId, client.url)
|
|
185
|
-
})
|
|
186
|
-
)
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
function topologicalSort (configManager) {
|
|
192
|
-
const { services } = configManager.current
|
|
193
|
-
const topo = new Topo.Sorter()
|
|
194
|
-
|
|
195
|
-
for (const service of services) {
|
|
196
|
-
const localDependencyIds = service.dependencies
|
|
197
|
-
.filter(dep => dep.local)
|
|
198
|
-
.map(dep => dep.id)
|
|
199
|
-
|
|
200
|
-
topo.add(service, {
|
|
201
|
-
group: service.id,
|
|
202
|
-
after: localDependencyIds,
|
|
203
|
-
manual: true
|
|
204
|
-
})
|
|
94
|
+
if (configManager.current.restartOnError === true) {
|
|
95
|
+
configManager.current.restartOnError = 5000
|
|
205
96
|
}
|
|
206
|
-
|
|
207
|
-
configManager.current.services = topo.sort()
|
|
208
97
|
}
|
|
209
98
|
|
|
210
99
|
async function platformaticRuntime () {
|
|
@@ -222,12 +111,12 @@ platformaticRuntime.configManagerConfig = {
|
|
|
222
111
|
useDefaults: true,
|
|
223
112
|
coerceTypes: true,
|
|
224
113
|
allErrors: true,
|
|
225
|
-
strict: false
|
|
114
|
+
strict: false,
|
|
226
115
|
},
|
|
227
116
|
async transformConfig () {
|
|
228
117
|
await _transformConfig(this)
|
|
229
118
|
},
|
|
230
|
-
upgrade
|
|
119
|
+
upgrade,
|
|
231
120
|
}
|
|
232
121
|
|
|
233
122
|
async function wrapConfigInRuntimeConfig ({ configManager, args }) {
|
|
@@ -246,15 +135,14 @@ async function wrapConfigInRuntimeConfig ({ configManager, args }) {
|
|
|
246
135
|
const wrapperConfig = {
|
|
247
136
|
$schema: schema.$id,
|
|
248
137
|
entrypoint: serviceId,
|
|
249
|
-
|
|
250
|
-
hotReload: true,
|
|
138
|
+
watch: true,
|
|
251
139
|
services: [
|
|
252
140
|
{
|
|
253
141
|
id: serviceId,
|
|
254
142
|
path: configManager.dirname,
|
|
255
|
-
config: configManager.fullPath
|
|
256
|
-
}
|
|
257
|
-
]
|
|
143
|
+
config: configManager.fullPath,
|
|
144
|
+
},
|
|
145
|
+
],
|
|
258
146
|
}
|
|
259
147
|
const cm = new ConfigManager({
|
|
260
148
|
source: wrapperConfig,
|
|
@@ -263,9 +151,9 @@ async function wrapConfigInRuntimeConfig ({ configManager, args }) {
|
|
|
263
151
|
useDefaults: true,
|
|
264
152
|
coerceTypes: true,
|
|
265
153
|
allErrors: true,
|
|
266
|
-
strict: false
|
|
154
|
+
strict: false,
|
|
267
155
|
},
|
|
268
|
-
transformConfig () { return _transformConfig(this) }
|
|
156
|
+
transformConfig () { return _transformConfig(this) },
|
|
269
157
|
})
|
|
270
158
|
|
|
271
159
|
await cm.parseAndValidate()
|
|
@@ -317,15 +205,15 @@ function parseInspectorOptions (configManager) {
|
|
|
317
205
|
host,
|
|
318
206
|
port,
|
|
319
207
|
breakFirstLine: hasInspectBrk,
|
|
320
|
-
|
|
208
|
+
watchDisabled: !!current.watch,
|
|
321
209
|
}
|
|
322
210
|
|
|
323
|
-
current.
|
|
211
|
+
current.watch = false
|
|
324
212
|
}
|
|
325
213
|
}
|
|
326
214
|
|
|
327
215
|
module.exports = {
|
|
328
216
|
parseInspectorOptions,
|
|
329
217
|
platformaticRuntime,
|
|
330
|
-
wrapConfigInRuntimeConfig
|
|
218
|
+
wrapConfigInRuntimeConfig,
|
|
331
219
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Topo = require('@hapi/topo')
|
|
4
|
+
const { closest } = require('fastest-levenshtein')
|
|
5
|
+
|
|
6
|
+
const errors = require('./errors')
|
|
7
|
+
|
|
8
|
+
function missingDependencyErrorMessage (clientName, service, services) {
|
|
9
|
+
const closestName = closest(clientName, [...services.keys()])
|
|
10
|
+
let errorMsg = `service '${service.id}' has unknown dependency: '${clientName}'.`
|
|
11
|
+
if (closestName) {
|
|
12
|
+
errorMsg += ` Did you mean '${closestName}'?`
|
|
13
|
+
}
|
|
14
|
+
return errorMsg
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function checkDependencies (services) {
|
|
18
|
+
const allServices = new Set(services.map(s => s.id))
|
|
19
|
+
|
|
20
|
+
for (const service of services) {
|
|
21
|
+
for (const dependency of service.dependencies) {
|
|
22
|
+
if (dependency.local && !allServices.has(dependency.id)) {
|
|
23
|
+
throw new errors.MissingDependencyError(
|
|
24
|
+
missingDependencyErrorMessage(dependency.id, service, services)
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function topologicalSort (services, config) {
|
|
32
|
+
const topo = new Topo.Sorter()
|
|
33
|
+
|
|
34
|
+
for (const service of config.services) {
|
|
35
|
+
const localDependencyIds = Array.from(service.dependencies)
|
|
36
|
+
.filter(dep => dep.local)
|
|
37
|
+
.map(dep => dep.id)
|
|
38
|
+
|
|
39
|
+
topo.add(service, {
|
|
40
|
+
group: service.id,
|
|
41
|
+
after: localDependencyIds,
|
|
42
|
+
manual: true,
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
config.services = topo.sort()
|
|
47
|
+
|
|
48
|
+
return new Map(Array.from(services.entries()).sort((a, b) => {
|
|
49
|
+
if (a[0] === b[0]) {
|
|
50
|
+
return 0
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const aIndex = config.services.findIndex(s => s.id === a[0])
|
|
54
|
+
const bIndex = config.services.findIndex(s => s.id === b[0])
|
|
55
|
+
return aIndex - bIndex
|
|
56
|
+
}))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = { checkDependencies, topologicalSort }
|
package/lib/errors.js
CHANGED
|
@@ -7,16 +7,20 @@ const ERROR_PREFIX = 'PLT_RUNTIME'
|
|
|
7
7
|
module.exports = {
|
|
8
8
|
AddressInUseError: createError(`${ERROR_PREFIX}_EADDR_IN_USE`, 'The current port is in use by another application'),
|
|
9
9
|
RuntimeExitedError: createError(`${ERROR_PREFIX}_RUNTIME_EXIT`, 'The runtime exited before the operation completed'),
|
|
10
|
+
ServiceExitedError: createError(`${ERROR_PREFIX}_SERVICE_EXIT`, 'The service %s exited prematurely with error code %d'),
|
|
10
11
|
UnknownRuntimeAPICommandError: createError(`${ERROR_PREFIX}_UNKNOWN_RUNTIME_API_COMMAND`, 'Unknown Runtime API command "%s"'),
|
|
11
|
-
ServiceNotFoundError: createError(`${ERROR_PREFIX}_SERVICE_NOT_FOUND`, 'Service not found. Available services are: %s'),
|
|
12
|
+
ServiceNotFoundError: createError(`${ERROR_PREFIX}_SERVICE_NOT_FOUND`, 'Service %s not found. Available services are: %s'),
|
|
12
13
|
ServiceNotStartedError: createError(`${ERROR_PREFIX}_SERVICE_NOT_STARTED`, "Service with id '%s' is not started"),
|
|
13
14
|
FailedToRetrieveOpenAPISchemaError: createError(`${ERROR_PREFIX}_FAILED_TO_RETRIEVE_OPENAPI_SCHEMA`, 'Failed to retrieve OpenAPI schema for service with id "%s": %s'),
|
|
14
15
|
FailedToRetrieveGraphQLSchemaError: createError(`${ERROR_PREFIX}_FAILED_TO_RETRIEVE_GRAPHQL_SCHEMA`, 'Failed to retrieve GraphQL schema for service with id "%s": %s'),
|
|
16
|
+
FailedToRetrieveMetaError: createError(`${ERROR_PREFIX}_FAILED_TO_RETRIEVE_META`, 'Failed to retrieve metadata for service with id "%s": %s'),
|
|
17
|
+
FailedToRetrieveMetricsError: createError(`${ERROR_PREFIX}_FAILED_TO_RETRIEVE_METRICS`, 'Failed to retrieve metrics for service with id "%s": %s'),
|
|
15
18
|
ApplicationAlreadyStartedError: createError(`${ERROR_PREFIX}_APPLICATION_ALREADY_STARTED`, 'Application is already started'),
|
|
16
19
|
ApplicationNotStartedError: createError(`${ERROR_PREFIX}_APPLICATION_NOT_STARTED`, 'Application has not been started'),
|
|
17
20
|
ConfigPathMustBeStringError: createError(`${ERROR_PREFIX}_CONFIG_PATH_MUST_BE_STRING`, 'Config path must be a string'),
|
|
18
21
|
NoConfigFileFoundError: createError(`${ERROR_PREFIX}_NO_CONFIG_FILE_FOUND`, "No config file found for service '%s'"),
|
|
19
22
|
InvalidEntrypointError: createError(`${ERROR_PREFIX}_INVALID_ENTRYPOINT`, "Invalid entrypoint: '%s' does not exist"),
|
|
23
|
+
InvalidAutoloadWithServicesError: createError(`${ERROR_PREFIX}_INVALID_AUTOLOAD_WITH_SERVICES`, 'Autoload cannot be used when services is defined'),
|
|
20
24
|
MissingDependencyError: createError(`${ERROR_PREFIX}_MISSING_DEPENDENCY`, 'Missing dependency: "%s"'),
|
|
21
25
|
InspectAndInspectBrkError: createError(`${ERROR_PREFIX}_INSPECT_AND_INSPECT_BRK`, '--inspect and --inspect-brk cannot be used together'),
|
|
22
26
|
InspectorPortError: createError(`${ERROR_PREFIX}_INSPECTOR_PORT`, 'Inspector port must be 0 or in range 1024 to 65535'),
|
|
@@ -28,5 +32,5 @@ module.exports = {
|
|
|
28
32
|
WorkerIsRequired: createError(`${ERROR_PREFIX}_REQUIRED_WORKER`, 'The worker parameter is required'),
|
|
29
33
|
|
|
30
34
|
// TODO: should remove next one as it's not used anymore
|
|
31
|
-
CannotRemoveServiceOnUpdateError: createError(`${ERROR_PREFIX}_CANNOT_REMOVE_SERVICE_ON_UPDATE`, 'Cannot remove service "%s" when updating a Runtime')
|
|
35
|
+
CannotRemoveServiceOnUpdateError: createError(`${ERROR_PREFIX}_CANNOT_REMOVE_SERVICE_ON_UPDATE`, 'Cannot remove service "%s" when updating a Runtime'),
|
|
32
36
|
}
|
package/lib/generator/errors.js
CHANGED
|
@@ -6,5 +6,5 @@ const ERROR_PREFIX = 'PLT_RUNTIME_GEN'
|
|
|
6
6
|
|
|
7
7
|
module.exports = {
|
|
8
8
|
NoServiceNamedError: createError(`${ERROR_PREFIX}_NO_SERVICE_FOUND`, 'No service named \'%s\' has been added to this runtime.'),
|
|
9
|
-
NoEntryPointError: createError(`${ERROR_PREFIX}_NO_ENTRYPOINT`, 'No entrypoint had been defined.')
|
|
9
|
+
NoEntryPointError: createError(`${ERROR_PREFIX}_NO_ENTRYPOINT`, 'No entrypoint had been defined.'),
|
|
10
10
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BaseGenerator } from
|
|
2
|
-
import { FileGenerator } from
|
|
1
|
+
import { BaseGenerator } from '@platformatic/generators'
|
|
2
|
+
import { FileGenerator } from '@platformatic/generators/lib/file-generator'
|
|
3
3
|
|
|
4
4
|
type Service = {
|
|
5
5
|
config: FileGenerator.FileGenerator | BaseGenerator.BaseGenerator
|
|
@@ -13,7 +13,7 @@ type KeyValue = {
|
|
|
13
13
|
[key: string]: string
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
type RuntimeGeneratorOptions =
|
|
16
|
+
type RuntimeGeneratorOptions = BaseGenerator.BaseGeneratorOptions & {
|
|
17
17
|
logLevel: string
|
|
18
18
|
}
|
|
19
19
|
|
|
@@ -21,17 +21,17 @@ export namespace RuntimeGenerator {
|
|
|
21
21
|
export class RuntimeGenerator extends BaseGenerator.BaseGenerator {
|
|
22
22
|
services: Service[]
|
|
23
23
|
entryPoint: Service
|
|
24
|
-
constructor(opts?: RuntimeGeneratorOptions)
|
|
25
|
-
|
|
26
|
-
addService(service: Service, name: string): Promise<void>
|
|
27
|
-
|
|
28
|
-
setEntryPoint(entryPoint: string): void
|
|
29
|
-
|
|
30
|
-
setServicesDirectory(): void
|
|
31
|
-
|
|
32
|
-
setServicesConfig(configToOverride: object): void
|
|
33
|
-
|
|
34
|
-
getRuntimeEnv(): KeyValue
|
|
35
|
-
writeServicesFiles(): Promise<GeneratorMetadata>
|
|
24
|
+
constructor (opts?: RuntimeGeneratorOptions)
|
|
25
|
+
|
|
26
|
+
addService (service: Service, name: string): Promise<void>
|
|
27
|
+
|
|
28
|
+
setEntryPoint (entryPoint: string): void
|
|
29
|
+
|
|
30
|
+
setServicesDirectory (): void
|
|
31
|
+
|
|
32
|
+
setServicesConfig (configToOverride: object): void
|
|
33
|
+
|
|
34
|
+
getRuntimeEnv (): KeyValue
|
|
35
|
+
writeServicesFiles (): Promise<GeneratorMetadata>
|
|
36
36
|
}
|
|
37
37
|
}
|