@platformatic/basic 2.25.0 → 2.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +22 -17
- package/lib/base.js +41 -14
- package/lib/worker/child-process.js +21 -1
- package/package.json +6 -6
- package/schema.json +1 -1
package/index.js
CHANGED
|
@@ -37,7 +37,7 @@ function isImportFailedError (error, pkg) {
|
|
|
37
37
|
return match?.[1] === pkg || error.requireStack?.[0].endsWith(importStackablePackageMarker)
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
async function importStackablePackage (
|
|
40
|
+
async function importStackablePackage (directory, pkg) {
|
|
41
41
|
try {
|
|
42
42
|
try {
|
|
43
43
|
// Try regular import
|
|
@@ -48,7 +48,7 @@ async function importStackablePackage (opts, pkg) {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
// Scope to the service
|
|
51
|
-
const require = createRequire(resolve(
|
|
51
|
+
const require = createRequire(resolve(directory, importStackablePackageMarker))
|
|
52
52
|
const imported = require.resolve(pkg)
|
|
53
53
|
return await importFile(imported)
|
|
54
54
|
}
|
|
@@ -57,16 +57,15 @@ async function importStackablePackage (opts, pkg) {
|
|
|
57
57
|
throw e
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
const serviceDirectory = relative(workerData.dirname,
|
|
60
|
+
const serviceDirectory = workerData ? relative(workerData.dirname, directory) : directory
|
|
61
61
|
throw new Error(
|
|
62
62
|
`Unable to import package '${pkg}'. Please add it as a dependency in the package.json file in the folder ${serviceDirectory}.`
|
|
63
63
|
)
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
async function
|
|
68
|
-
|
|
69
|
-
let toImport = '@platformatic/node'
|
|
67
|
+
export async function importStackableAndConfig (root, config) {
|
|
68
|
+
let moduleName = '@platformatic/node'
|
|
70
69
|
let autodetectDescription = 'is using a generic Node.js application'
|
|
71
70
|
|
|
72
71
|
let rootPackageJson
|
|
@@ -76,14 +75,12 @@ async function buildStackable (opts) {
|
|
|
76
75
|
rootPackageJson = {}
|
|
77
76
|
}
|
|
78
77
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if (!hadConfig) {
|
|
78
|
+
if (!config) {
|
|
82
79
|
for (const candidate of configCandidates) {
|
|
83
80
|
const candidatePath = resolve(root, candidate)
|
|
84
81
|
|
|
85
82
|
if (existsSync(candidatePath)) {
|
|
86
|
-
|
|
83
|
+
config = candidatePath
|
|
87
84
|
break
|
|
88
85
|
}
|
|
89
86
|
}
|
|
@@ -93,19 +90,27 @@ async function buildStackable (opts) {
|
|
|
93
90
|
|
|
94
91
|
if (dependencies?.next || devDependencies?.next) {
|
|
95
92
|
autodetectDescription = 'is using Next.js'
|
|
96
|
-
|
|
93
|
+
moduleName = '@platformatic/next'
|
|
97
94
|
} else if (dependencies?.['@remix-run/dev'] || devDependencies?.['@remix-run/dev']) {
|
|
98
95
|
autodetectDescription = 'is using Remix'
|
|
99
|
-
|
|
96
|
+
moduleName = '@platformatic/remix'
|
|
100
97
|
} else if (dependencies?.vite || devDependencies?.vite) {
|
|
101
98
|
autodetectDescription = 'is using Vite'
|
|
102
|
-
|
|
99
|
+
moduleName = '@platformatic/vite'
|
|
103
100
|
} else if (dependencies?.astro || devDependencies?.astro) {
|
|
104
101
|
autodetectDescription = 'is using Astro'
|
|
105
|
-
|
|
102
|
+
moduleName = '@platformatic/astro'
|
|
106
103
|
}
|
|
107
104
|
|
|
108
|
-
const
|
|
105
|
+
const stackable = await importStackablePackage(root, moduleName)
|
|
106
|
+
|
|
107
|
+
return { stackable, config, autodetectDescription, moduleName }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function buildStackable (opts) {
|
|
111
|
+
const hadConfig = !!opts.config
|
|
112
|
+
const { stackable, config, autodetectDescription, moduleName } = await importStackableAndConfig(opts.context.directory, opts.config)
|
|
113
|
+
opts.config = config
|
|
109
114
|
|
|
110
115
|
const serviceRoot = relative(process.cwd(), opts.context.directory)
|
|
111
116
|
if (
|
|
@@ -122,12 +127,12 @@ async function buildStackable (opts) {
|
|
|
122
127
|
[
|
|
123
128
|
`Platformatic has auto-detected that service "${opts.context.serviceId}" ${autodetectDescription}.\n`,
|
|
124
129
|
`We suggest you create a platformatic.json or watt.json file in the folder ${serviceRoot} with the "$schema" `,
|
|
125
|
-
`property set to "https://schemas.platformatic.dev/${
|
|
130
|
+
`property set to "https://schemas.platformatic.dev/${moduleName}/${packageJson.version}.json".`
|
|
126
131
|
].join('')
|
|
127
132
|
)
|
|
128
133
|
}
|
|
129
134
|
|
|
130
|
-
return
|
|
135
|
+
return stackable.buildStackable(opts)
|
|
131
136
|
}
|
|
132
137
|
|
|
133
138
|
/* c8 ignore next 3 */
|
package/lib/base.js
CHANGED
|
@@ -160,25 +160,31 @@ export class BaseStackable {
|
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
-
async buildWithCommand (command, basePath,
|
|
163
|
+
async buildWithCommand (command, basePath, opts = {}) {
|
|
164
|
+
const { loader, scripts, context, disableChildManager } = opts
|
|
165
|
+
|
|
164
166
|
if (Array.isArray(command)) {
|
|
165
167
|
command = command.join(' ')
|
|
166
168
|
}
|
|
167
169
|
|
|
168
170
|
this.logger.debug(`Executing "${command}" ...`)
|
|
169
171
|
|
|
170
|
-
const
|
|
171
|
-
this.childManager =
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
172
|
+
const baseContext = await this.getChildManagerContext(basePath)
|
|
173
|
+
this.childManager = disableChildManager
|
|
174
|
+
? null
|
|
175
|
+
: new ChildManager({
|
|
176
|
+
logger: this.logger,
|
|
177
|
+
loader,
|
|
178
|
+
scripts,
|
|
179
|
+
context: { ...baseContext, isBuilding: true, ...context }
|
|
180
|
+
})
|
|
177
181
|
|
|
178
182
|
try {
|
|
179
|
-
await this.childManager
|
|
183
|
+
await this.childManager?.inject()
|
|
180
184
|
|
|
181
185
|
const subprocess = this.spawn(command)
|
|
186
|
+
subprocess.stdout.setEncoding('utf8')
|
|
187
|
+
subprocess.stderr.setEncoding('utf8')
|
|
182
188
|
|
|
183
189
|
// Wait for the process to be started
|
|
184
190
|
await new Promise((resolve, reject) => {
|
|
@@ -205,8 +211,8 @@ export class BaseStackable {
|
|
|
205
211
|
throw error
|
|
206
212
|
}
|
|
207
213
|
} finally {
|
|
208
|
-
await this.childManager
|
|
209
|
-
await this.childManager
|
|
214
|
+
await this.childManager?.eject()
|
|
215
|
+
await this.childManager?.close()
|
|
210
216
|
}
|
|
211
217
|
}
|
|
212
218
|
|
|
@@ -214,7 +220,7 @@ export class BaseStackable {
|
|
|
214
220
|
const config = this.configManager.current
|
|
215
221
|
const basePath = config.application?.basePath ? cleanBasePath(config.application?.basePath) : ''
|
|
216
222
|
|
|
217
|
-
const context = await this
|
|
223
|
+
const context = await this.getChildManagerContext(basePath)
|
|
218
224
|
this.childManager = new ChildManager({
|
|
219
225
|
logger: this.logger,
|
|
220
226
|
loader,
|
|
@@ -335,6 +341,7 @@ export class BaseStackable {
|
|
|
335
341
|
|
|
336
342
|
this.#metricsCollected = true
|
|
337
343
|
await this.#collectMetrics()
|
|
344
|
+
this.#setHttpCacheMetrics()
|
|
338
345
|
}
|
|
339
346
|
|
|
340
347
|
async #collectMetrics () {
|
|
@@ -367,6 +374,25 @@ export class BaseStackable {
|
|
|
367
374
|
}
|
|
368
375
|
}
|
|
369
376
|
|
|
377
|
+
#setHttpCacheMetrics () {
|
|
378
|
+
const { client, registry } = globalThis.platformatic.prometheus
|
|
379
|
+
|
|
380
|
+
const cacheHitMetric = new client.Counter({
|
|
381
|
+
name: 'http_cache_hit_count',
|
|
382
|
+
help: 'Number of http cache hits',
|
|
383
|
+
registers: [registry]
|
|
384
|
+
})
|
|
385
|
+
|
|
386
|
+
const cacheMissMetric = new client.Counter({
|
|
387
|
+
name: 'http_cache_miss_count',
|
|
388
|
+
help: 'Number of http cache misses',
|
|
389
|
+
registers: [registry]
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
globalThis.platformatic.onHttpCacheHit = () => { cacheHitMetric.inc() }
|
|
393
|
+
globalThis.platformatic.onHttpCacheMiss = () => { cacheMissMetric.inc() }
|
|
394
|
+
}
|
|
395
|
+
|
|
370
396
|
async getMetrics ({ format } = {}) {
|
|
371
397
|
if (this.childManager && this.clientWs) {
|
|
372
398
|
return this.childManager.send(this.clientWs, 'getMetrics', { format })
|
|
@@ -387,7 +413,7 @@ export class BaseStackable {
|
|
|
387
413
|
}
|
|
388
414
|
}
|
|
389
415
|
|
|
390
|
-
async
|
|
416
|
+
async getChildManagerContext (basePath) {
|
|
391
417
|
const meta = await this.getMeta()
|
|
392
418
|
|
|
393
419
|
return {
|
|
@@ -405,7 +431,8 @@ export class BaseStackable {
|
|
|
405
431
|
/* c8 ignore next 2 */
|
|
406
432
|
port: (this.isEntrypoint ? this.serverConfig?.port || 0 : undefined) ?? true,
|
|
407
433
|
host: (this.isEntrypoint ? this.serverConfig?.hostname : undefined) ?? true,
|
|
408
|
-
telemetryConfig: this.telemetryConfig
|
|
434
|
+
telemetryConfig: this.telemetryConfig,
|
|
435
|
+
interceptLogging: typeof workerData?.loggingPort !== 'undefined'
|
|
409
436
|
}
|
|
410
437
|
}
|
|
411
438
|
}
|
|
@@ -180,6 +180,26 @@ export class ChildProcess extends ITC {
|
|
|
180
180
|
|
|
181
181
|
async #collectMetrics ({ serviceId, workerId, metricsConfig }) {
|
|
182
182
|
await collectMetrics(serviceId, workerId, metricsConfig, this.#metricsRegistry)
|
|
183
|
+
this.#setHttpCacheMetrics()
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
#setHttpCacheMetrics () {
|
|
187
|
+
const { client, registry } = globalThis.platformatic.prometheus
|
|
188
|
+
|
|
189
|
+
const cacheHitMetric = new client.Counter({
|
|
190
|
+
name: 'http_cache_hit_count',
|
|
191
|
+
help: 'Number of http cache hits',
|
|
192
|
+
registers: [registry]
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
const cacheMissMetric = new client.Counter({
|
|
196
|
+
name: 'http_cache_miss_count',
|
|
197
|
+
help: 'Number of http cache misses',
|
|
198
|
+
registers: [registry]
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
globalThis.platformatic.onHttpCacheHit = () => { cacheHitMetric.inc() }
|
|
202
|
+
globalThis.platformatic.onHttpCacheMiss = () => { cacheMissMetric.inc() }
|
|
183
203
|
}
|
|
184
204
|
|
|
185
205
|
async #getMetrics ({ format } = {}) {
|
|
@@ -205,7 +225,7 @@ export class ChildProcess extends ITC {
|
|
|
205
225
|
}
|
|
206
226
|
}
|
|
207
227
|
|
|
208
|
-
if (isMainThread) {
|
|
228
|
+
if (isMainThread && globalThis.platformatic.interceptLogging) {
|
|
209
229
|
pinoOptions.transport = {
|
|
210
230
|
target: new URL('./child-transport.js', import.meta.url).toString()
|
|
211
231
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/basic",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.26.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -23,11 +23,11 @@
|
|
|
23
23
|
"split2": "^4.2.0",
|
|
24
24
|
"undici": "^7.0.0",
|
|
25
25
|
"ws": "^8.18.0",
|
|
26
|
-
"@platformatic/config": "2.
|
|
27
|
-
"@platformatic/
|
|
28
|
-
"@platformatic/
|
|
29
|
-
"@platformatic/
|
|
30
|
-
"@platformatic/
|
|
26
|
+
"@platformatic/config": "2.26.0",
|
|
27
|
+
"@platformatic/itc": "2.26.0",
|
|
28
|
+
"@platformatic/metrics": "2.26.0",
|
|
29
|
+
"@platformatic/utils": "2.26.0",
|
|
30
|
+
"@platformatic/telemetry": "2.26.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"borp": "^0.19.0",
|
package/schema.json
CHANGED