@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/app.js
DELETED
|
@@ -1,397 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { dirname } = require('node:path')
|
|
4
|
-
const { EventEmitter, once } = require('node:events')
|
|
5
|
-
const { FileWatcher } = require('@platformatic/utils')
|
|
6
|
-
const debounce = require('debounce')
|
|
7
|
-
const { snakeCase } = require('change-case-all')
|
|
8
|
-
const { buildServer } = require('./build-server')
|
|
9
|
-
const { loadConfig } = require('./load-config')
|
|
10
|
-
const errors = require('./errors')
|
|
11
|
-
|
|
12
|
-
class PlatformaticApp extends EventEmitter {
|
|
13
|
-
#hotReload
|
|
14
|
-
#loaderPort
|
|
15
|
-
#starting
|
|
16
|
-
#started
|
|
17
|
-
#originalWatch
|
|
18
|
-
#fileWatcher
|
|
19
|
-
#logger
|
|
20
|
-
#telemetryConfig
|
|
21
|
-
#serverConfig
|
|
22
|
-
#debouncedRestart
|
|
23
|
-
#hasManagementApi
|
|
24
|
-
#metricsConfig
|
|
25
|
-
|
|
26
|
-
constructor (appConfig, loaderPort, logger, telemetryConfig, serverConfig, hasManagementApi, metricsConfig) {
|
|
27
|
-
super()
|
|
28
|
-
this.appConfig = appConfig
|
|
29
|
-
this.config = null
|
|
30
|
-
this.#hotReload = false
|
|
31
|
-
this.#loaderPort = loaderPort
|
|
32
|
-
this.#starting = false
|
|
33
|
-
this.#started = false
|
|
34
|
-
this.server = null
|
|
35
|
-
this.#originalWatch = null
|
|
36
|
-
this.#fileWatcher = null
|
|
37
|
-
this.#hasManagementApi = !!hasManagementApi
|
|
38
|
-
this.#logger = logger.child({
|
|
39
|
-
name: this.appConfig.id
|
|
40
|
-
})
|
|
41
|
-
this.#telemetryConfig = telemetryConfig
|
|
42
|
-
this.#metricsConfig = metricsConfig
|
|
43
|
-
this.#serverConfig = serverConfig
|
|
44
|
-
|
|
45
|
-
/* c8 ignore next 4 */
|
|
46
|
-
this.#debouncedRestart = debounce(() => {
|
|
47
|
-
this.server.log.info('files changed')
|
|
48
|
-
this.restart()
|
|
49
|
-
}, 100) // debounce restart for 100ms
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
getStatus () {
|
|
53
|
-
if (this.#starting) return 'starting'
|
|
54
|
-
if (this.#started) return 'started'
|
|
55
|
-
return 'stopped'
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
async restart () {
|
|
59
|
-
if (this.#starting || !this.#started) {
|
|
60
|
-
return
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
this.#starting = true
|
|
64
|
-
this.#started = false
|
|
65
|
-
|
|
66
|
-
// The CJS cache should not be cleared from the loader because v20 moved
|
|
67
|
-
// the loader to a different thread with a different CJS cache.
|
|
68
|
-
clearCjsCache()
|
|
69
|
-
|
|
70
|
-
/* c8 ignore next 4 - tests may not pass in a MessagePort. */
|
|
71
|
-
if (this.#loaderPort) {
|
|
72
|
-
this.#loaderPort.postMessage('plt:clear-cache')
|
|
73
|
-
await once(this.#loaderPort, 'message')
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
try {
|
|
77
|
-
await this.config.configManager.parseAndValidate()
|
|
78
|
-
await this.#updateConfig()
|
|
79
|
-
|
|
80
|
-
this.#setuplogger(this.config.configManager)
|
|
81
|
-
await this.server.restart()
|
|
82
|
-
} catch (err) {
|
|
83
|
-
// The restart failed. Log the error and
|
|
84
|
-
// wait for another event.
|
|
85
|
-
// The old app is still available
|
|
86
|
-
this.#logger.error({ err })
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
this.#started = true
|
|
90
|
-
this.#starting = false
|
|
91
|
-
this.emit('start')
|
|
92
|
-
this.emit('restart')
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
async getBootstrapDependencies () {
|
|
96
|
-
await this.#loadConfig()
|
|
97
|
-
const resolver = this.config.app.getBootstrapDependencies
|
|
98
|
-
if (typeof resolver === 'function') {
|
|
99
|
-
return resolver(this.appConfig, this.config.configManager)
|
|
100
|
-
}
|
|
101
|
-
return []
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
async start () {
|
|
105
|
-
if (this.#starting || this.#started) {
|
|
106
|
-
throw new errors.ApplicationAlreadyStartedError()
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
this.#starting = true
|
|
110
|
-
|
|
111
|
-
await this.#applyConfig()
|
|
112
|
-
await this.#updateConfig()
|
|
113
|
-
|
|
114
|
-
const configManager = this.config.configManager
|
|
115
|
-
const config = configManager.current
|
|
116
|
-
|
|
117
|
-
this.#setuplogger(configManager)
|
|
118
|
-
|
|
119
|
-
try {
|
|
120
|
-
// If this is a restart, have the fastify server restart itself. If this
|
|
121
|
-
// is not a restart, then create a new server.
|
|
122
|
-
this.server = await buildServer({
|
|
123
|
-
app: this.config.app,
|
|
124
|
-
...config,
|
|
125
|
-
id: this.appConfig.id,
|
|
126
|
-
configManager
|
|
127
|
-
})
|
|
128
|
-
} catch (err) {
|
|
129
|
-
this.#logAndExit(err)
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (
|
|
133
|
-
(config.plugins !== undefined) &&
|
|
134
|
-
this.#originalWatch?.enabled !== false
|
|
135
|
-
) {
|
|
136
|
-
this.#startFileWatching()
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (this.appConfig.useHttp) {
|
|
140
|
-
try {
|
|
141
|
-
await this.server.start()
|
|
142
|
-
/* c8 ignore next 5 */
|
|
143
|
-
} catch (err) {
|
|
144
|
-
this.server.log.error({ err })
|
|
145
|
-
this.#starting = false
|
|
146
|
-
throw err
|
|
147
|
-
}
|
|
148
|
-
} else {
|
|
149
|
-
// Make sure the server has run all the onReady hooks before returning.
|
|
150
|
-
await this.server.ready()
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
this.#started = true
|
|
154
|
-
this.#starting = false
|
|
155
|
-
this.emit('start')
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
async stop () {
|
|
159
|
-
if (!this.#started || this.#starting) {
|
|
160
|
-
throw new errors.ApplicationNotStartedError()
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
await this.#stopFileWatching()
|
|
164
|
-
await this.server.close()
|
|
165
|
-
|
|
166
|
-
this.#started = false
|
|
167
|
-
this.#starting = false
|
|
168
|
-
this.emit('stop')
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
async listen () {
|
|
172
|
-
// This server is not an entrypoint or already listened in start. Behave as no-op.
|
|
173
|
-
if (!this.appConfig.entrypoint || this.appConfig.useHttp) {
|
|
174
|
-
return
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
await this.server.start()
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
async handleProcessLevelEvent ({ signal, err }) {
|
|
181
|
-
/* c8 ignore next 3 */
|
|
182
|
-
if (!this.server) {
|
|
183
|
-
return false
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (signal === 'SIGUSR2') {
|
|
187
|
-
this.server.log.info('reloading configuration')
|
|
188
|
-
await this.restart()
|
|
189
|
-
return false
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
if (err) {
|
|
193
|
-
this.server.log.error({
|
|
194
|
-
err: {
|
|
195
|
-
message: err?.message,
|
|
196
|
-
stack: err?.stack
|
|
197
|
-
}
|
|
198
|
-
}, 'exiting')
|
|
199
|
-
} else if (signal) {
|
|
200
|
-
this.server.log.info({ signal }, 'received signal')
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
if (this.#starting) {
|
|
204
|
-
await once(this, 'start')
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (this.#started) {
|
|
208
|
-
await this.stop()
|
|
209
|
-
this.server.log.info('server stopped')
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
async #loadConfig () {
|
|
214
|
-
const appConfig = this.appConfig
|
|
215
|
-
|
|
216
|
-
let _config
|
|
217
|
-
try {
|
|
218
|
-
_config = await loadConfig({}, ['-c', appConfig.config], {
|
|
219
|
-
onMissingEnv: this.#fetchServiceUrl,
|
|
220
|
-
context: appConfig
|
|
221
|
-
}, true, this.#logger)
|
|
222
|
-
} catch (err) {
|
|
223
|
-
this.#logAndExit(err)
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
this.config = _config
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
async #applyConfig () {
|
|
230
|
-
if (!this.config) {
|
|
231
|
-
await this.#loadConfig()
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const appConfig = this.appConfig
|
|
235
|
-
const { configManager } = this.config
|
|
236
|
-
|
|
237
|
-
function applyOverrides () {
|
|
238
|
-
if (appConfig._configOverrides instanceof Map) {
|
|
239
|
-
appConfig._configOverrides.forEach((value, key) => {
|
|
240
|
-
if (typeof key !== 'string') {
|
|
241
|
-
throw new errors.ConfigPathMustBeStringError()
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
const parts = key.split('.')
|
|
245
|
-
let next = configManager.current
|
|
246
|
-
let obj
|
|
247
|
-
let i
|
|
248
|
-
|
|
249
|
-
for (i = 0; next !== undefined && i < parts.length; ++i) {
|
|
250
|
-
obj = next
|
|
251
|
-
next = obj[parts[i]]
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
if (i === parts.length) {
|
|
255
|
-
obj[parts.at(-1)] = value
|
|
256
|
-
}
|
|
257
|
-
})
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
applyOverrides()
|
|
262
|
-
|
|
263
|
-
this.#hotReload = this.appConfig.hotReload
|
|
264
|
-
|
|
265
|
-
configManager.on('error', (err) => {
|
|
266
|
-
/* c8 ignore next */
|
|
267
|
-
this.server.log.error({ err }, 'error reloading the configuration')
|
|
268
|
-
})
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
async #updateConfig () {
|
|
272
|
-
this.#originalWatch = this.config.configManager.current.watch
|
|
273
|
-
this.config.configManager.current.watch = { enabled: false }
|
|
274
|
-
|
|
275
|
-
const { configManager } = this.config
|
|
276
|
-
configManager.update({
|
|
277
|
-
...configManager.current,
|
|
278
|
-
telemetry: this.#telemetryConfig,
|
|
279
|
-
metrics: this.#metricsConfig
|
|
280
|
-
})
|
|
281
|
-
|
|
282
|
-
if (configManager.current.metrics !== false) {
|
|
283
|
-
configManager.update({
|
|
284
|
-
...configManager.current,
|
|
285
|
-
metrics: this.#metricsConfig
|
|
286
|
-
})
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
if (this.#serverConfig) {
|
|
290
|
-
configManager.update({
|
|
291
|
-
...configManager.current,
|
|
292
|
-
server: this.#serverConfig
|
|
293
|
-
})
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
if (
|
|
297
|
-
(this.#hasManagementApi && configManager.current.metrics === undefined) ||
|
|
298
|
-
configManager.current.metrics
|
|
299
|
-
) {
|
|
300
|
-
configManager.update({
|
|
301
|
-
...configManager.current,
|
|
302
|
-
metrics: {
|
|
303
|
-
server: 'hide',
|
|
304
|
-
defaultMetrics: { enabled: this.appConfig.entrypoint },
|
|
305
|
-
prefix: snakeCase(this.appConfig.id) + '_',
|
|
306
|
-
...configManager.current.metrics
|
|
307
|
-
}
|
|
308
|
-
})
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
if (!this.appConfig.entrypoint) {
|
|
312
|
-
configManager.update({
|
|
313
|
-
...configManager.current,
|
|
314
|
-
server: {
|
|
315
|
-
...(configManager.current.server || {}),
|
|
316
|
-
trustProxy: true
|
|
317
|
-
}
|
|
318
|
-
})
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
#setuplogger (configManager) {
|
|
323
|
-
configManager.current.server = configManager.current.server || {}
|
|
324
|
-
const level = configManager.current.server.logger?.level
|
|
325
|
-
|
|
326
|
-
configManager.current.server.logger = level
|
|
327
|
-
? this.#logger.child({ level })
|
|
328
|
-
: this.#logger
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
#fetchServiceUrl (key, { parent, context: service }) {
|
|
332
|
-
if (service.localServiceEnvVars.has(key)) {
|
|
333
|
-
return service.localServiceEnvVars.get(key)
|
|
334
|
-
} else if (!key.endsWith('_URL') || !parent.serviceId) {
|
|
335
|
-
return null
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
return getServiceUrl(parent.serviceId)
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
#startFileWatching () {
|
|
342
|
-
if (this.#fileWatcher) {
|
|
343
|
-
return
|
|
344
|
-
}
|
|
345
|
-
const server = this.server
|
|
346
|
-
const { configManager } = server.platformatic
|
|
347
|
-
const fileWatcher = new FileWatcher({
|
|
348
|
-
path: dirname(configManager.fullPath),
|
|
349
|
-
/* c8 ignore next 2 */
|
|
350
|
-
allowToWatch: this.#originalWatch?.allow,
|
|
351
|
-
watchIgnore: this.#originalWatch?.ignore || []
|
|
352
|
-
})
|
|
353
|
-
|
|
354
|
-
fileWatcher.on('update', this.#debouncedRestart)
|
|
355
|
-
|
|
356
|
-
fileWatcher.startWatching()
|
|
357
|
-
server.log.debug('start watching files')
|
|
358
|
-
server.platformatic.fileWatcher = fileWatcher
|
|
359
|
-
this.#fileWatcher = fileWatcher
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
async #stopFileWatching () {
|
|
363
|
-
const watcher = this.#fileWatcher
|
|
364
|
-
if (watcher) {
|
|
365
|
-
this.server.log.debug('stop watching files')
|
|
366
|
-
await watcher.stopWatching()
|
|
367
|
-
this.server.platformatic.fileWatcher = undefined
|
|
368
|
-
this.#fileWatcher = null
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
#logAndExit (err) {
|
|
373
|
-
this.#logger.error({ err })
|
|
374
|
-
process.exit(1)
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
/* c8 ignore next 11 - c8 upgrade marked many existing things as uncovered */
|
|
379
|
-
function clearCjsCache () {
|
|
380
|
-
// This evicts all of the modules from the require() cache.
|
|
381
|
-
// Note: This does not clean up children references to the deleted module.
|
|
382
|
-
// It's likely not a big deal for most cases, but it is a leak. The child
|
|
383
|
-
// references can be cleaned up, but it is expensive and involves walking
|
|
384
|
-
// the entire require() cache. See the DEP0144 documentation for how to do
|
|
385
|
-
// it.
|
|
386
|
-
Object.keys(require.cache).forEach((key) => {
|
|
387
|
-
if (!key.match(/node_modules/)) {
|
|
388
|
-
delete require.cache[key]
|
|
389
|
-
}
|
|
390
|
-
})
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
function getServiceUrl (id) {
|
|
394
|
-
return `http://${id}.plt.local`
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
module.exports = { PlatformaticApp }
|
package/lib/load-config.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { Store, loadConfig } = require('@platformatic/config')
|
|
4
|
-
const { platformaticRuntime } = require('./config')
|
|
5
|
-
|
|
6
|
-
function _loadConfig (minimistConfig, args, overrides, replaceEnv = true) {
|
|
7
|
-
const store = new Store()
|
|
8
|
-
store.add(platformaticRuntime)
|
|
9
|
-
return loadConfig(minimistConfig, args, store, overrides, replaceEnv)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
module.exports = { loadConfig: _loadConfig }
|
package/lib/loader.mjs
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { createRequire, isBuiltin } from 'node:module'
|
|
2
|
-
import { dirname, isAbsolute, resolve as pathResolve } from 'node:path'
|
|
3
|
-
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
4
|
-
import errors from './errors.js'
|
|
5
|
-
const isWindows = process.platform === 'win32'
|
|
6
|
-
let timestamp = process.hrtime.bigint()
|
|
7
|
-
let port
|
|
8
|
-
|
|
9
|
-
/* c8 ignore next 3 - c8 upgrade marked many existing things as uncovered */
|
|
10
|
-
function bustEsmCache () {
|
|
11
|
-
timestamp = process.hrtime.bigint()
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function isRelativePath (p) {
|
|
15
|
-
// This function is extracted from Node core, so it should work.
|
|
16
|
-
/* c8 ignore next - c8 upgrade marked many existing things as uncovered */
|
|
17
|
-
return p.charAt(0) === '.' &&
|
|
18
|
-
/* c8 ignore next 9 */
|
|
19
|
-
(
|
|
20
|
-
p.length === 1 ||
|
|
21
|
-
p.charAt(1) === '/' ||
|
|
22
|
-
(isWindows && p.charAt(1) === '\\') ||
|
|
23
|
-
(p.charAt(1) === '.' && ((
|
|
24
|
-
p.length === 2 ||
|
|
25
|
-
p.charAt(2) === '/') ||
|
|
26
|
-
(isWindows && p.charAt(2) === '\\')))
|
|
27
|
-
)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function specifierToFileUrl (specifier, referencingModuleId) {
|
|
31
|
-
// Convert the specifier into an absolute path URL if possible. If the
|
|
32
|
-
// specifier cannot be converted to a path (for example for a core module),
|
|
33
|
-
// then return null.
|
|
34
|
-
try {
|
|
35
|
-
const url = new URL(specifier)
|
|
36
|
-
|
|
37
|
-
if (url.protocol === 'file:') {
|
|
38
|
-
return url.href
|
|
39
|
-
} else {
|
|
40
|
-
return null
|
|
41
|
-
}
|
|
42
|
-
} catch {
|
|
43
|
-
// Ignore error.
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/* c8 ignore next 3 - c8 upgrade marked many existing things as uncovered */
|
|
47
|
-
if (isBuiltin(specifier)) {
|
|
48
|
-
return null
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/* c8 ignore next 3 */
|
|
52
|
-
if (isAbsolute(specifier)) {
|
|
53
|
-
return pathToFileURL(specifier).href
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/* c8 ignore next 3 */
|
|
57
|
-
if (!referencingModuleId) {
|
|
58
|
-
throw new errors.CannotMapSpecifierToAbsolutePathError(specifier)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/* c8 ignore next 5 - c8 upgrade marked many existing things as uncovered */
|
|
62
|
-
if (isRelativePath(specifier)) {
|
|
63
|
-
return pathToFileURL(
|
|
64
|
-
pathResolve(dirname(fileURLToPath(referencingModuleId)), specifier)
|
|
65
|
-
).href
|
|
66
|
-
} else {
|
|
67
|
-
// The specifier is something in node_modules/.
|
|
68
|
-
const req = createRequire(referencingModuleId)
|
|
69
|
-
|
|
70
|
-
return pathToFileURL(req.resolve(specifier)).href
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export async function resolve (specifier, context, nextResolve) {
|
|
75
|
-
const url = specifierToFileUrl(specifier, context.parentURL)
|
|
76
|
-
|
|
77
|
-
// If the specifier could not be mapped to a file, or the path is this file,
|
|
78
|
-
// then don't do anything.
|
|
79
|
-
if (typeof url !== 'string' || url === import.meta.url || url.match(/node_modules/)) {
|
|
80
|
-
return nextResolve(specifier, context)
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return nextResolve(`${url}?ts=${timestamp}`, context)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export function globalPreload (context) {
|
|
87
|
-
port = context.port
|
|
88
|
-
port.on('message', () => {
|
|
89
|
-
/* c8 ignore next 3 - c8 upgrade marked many existing things as uncovered */
|
|
90
|
-
bustEsmCache()
|
|
91
|
-
port.postMessage('plt:cache-cleared')
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
return 'globalThis.LOADER_PORT = port;'
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export async function initialize (data) {
|
|
98
|
-
port = data.port
|
|
99
|
-
port.on('message', () => {
|
|
100
|
-
bustEsmCache()
|
|
101
|
-
port.postMessage('plt:cache-cleared')
|
|
102
|
-
})
|
|
103
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
const assert = require('node:assert')
|
|
3
|
-
const { Writable } = require('node:stream')
|
|
4
|
-
|
|
5
|
-
class MessagePortWritable extends Writable {
|
|
6
|
-
constructor (options) {
|
|
7
|
-
const opts = { ...options, objectMode: true }
|
|
8
|
-
|
|
9
|
-
super(opts)
|
|
10
|
-
this.port = opts.port
|
|
11
|
-
this.metadata = opts.metadata
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
_write (chunk, encoding, callback) {
|
|
15
|
-
this.port.postMessage({
|
|
16
|
-
metadata: this.metadata,
|
|
17
|
-
logs: [chunk.toString().trim()]
|
|
18
|
-
})
|
|
19
|
-
process.nextTick(callback)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
_writev (chunks, callback) {
|
|
23
|
-
// Process the logs here before trying to send them across the thread
|
|
24
|
-
// boundary. Sometimes the chunks have an undocumented method on them
|
|
25
|
-
// which will prevent sending the chunk itself across threads.
|
|
26
|
-
const logs = chunks.map((chunk) => {
|
|
27
|
-
// pino should always produce a string here.
|
|
28
|
-
assert(typeof chunk.chunk === 'string')
|
|
29
|
-
return chunk.chunk.trim()
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
this.port.postMessage({
|
|
33
|
-
metadata: this.metadata,
|
|
34
|
-
logs
|
|
35
|
-
})
|
|
36
|
-
setImmediate(callback)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
_final (callback) {
|
|
40
|
-
this.port.close()
|
|
41
|
-
callback()
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
_destroy (err, callback) {
|
|
45
|
-
this.port.close()
|
|
46
|
-
callback(err)
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
module.exports = { MessagePortWritable }
|