@platformatic/basic 2.35.0 → 2.36.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/utils.js CHANGED
@@ -62,8 +62,10 @@ export function importFile (path) {
62
62
  /* c8 ignore next 6 */
63
63
  export function resolvePackage (root, pkg) {
64
64
  const require = createRequire(root)
65
-
66
- return require.resolve(pkg, { paths: [root, ...require.main.paths] })
65
+ // We need to add the main module paths to the require.resolve call
66
+ // Note that `require.main` is not defined in `next` if we set sthe instrumentation hook reequired for ESM applications.
67
+ // see: https://github.com/open-telemetry/opentelemetry-js/blob/main/doc/esm-support.md#instrumentation-hook-required-for-esm
68
+ return require.resolve(pkg, { paths: [root, ...require.main?.paths || []] })
67
69
  }
68
70
 
69
71
  export function cleanBasePath (basePath) {
@@ -68,9 +68,6 @@ export class ChildManager extends ITC {
68
68
  const logs = Array.isArray(message.logs) ? message.logs : [message.logs]
69
69
  this._forwardLogs(logs)
70
70
  },
71
- fetch: request => {
72
- return this.#fetch(request)
73
- },
74
71
  ...itcOpts.handlers
75
72
  }
76
73
  })
@@ -81,7 +78,7 @@ export class ChildManager extends ITC {
81
78
  this.#scripts = scripts
82
79
  this.#originalNodeOptions = process.env.NODE_OPTIONS
83
80
  this.#logger = globalThis.platformatic.logger
84
- this.#server = createServer()
81
+ this.#server = createServer(this.#childProcessFetchHandler.bind(this))
85
82
  this.#socketPath ??= getSocketPath(this.#id)
86
83
  this.#clients = new Set()
87
84
  this.#requests = new Map()
@@ -246,13 +243,28 @@ export class ChildManager extends ITC {
246
243
  workerData.loggingPort.postMessage({ logs: logs.map(m => JSON.stringify(m)) })
247
244
  }
248
245
 
249
- async #fetch (opts) {
250
- const { statusCode, headers, body } = await request(opts)
246
+ async #childProcessFetchHandler (req, res) {
247
+ const { url, headers } = req
248
+
249
+ const requestOptions = { method: req.method, headers }
251
250
 
252
- const rawPayload = Buffer.from(await body.arrayBuffer())
253
- const payload = rawPayload.toString()
251
+ if (req.method !== 'GET' && req.method !== 'HEAD') {
252
+ requestOptions.body = req
253
+ }
254
254
 
255
- return { statusCode, headers, body: payload, payload, rawPayload }
255
+ try {
256
+ const {
257
+ statusCode,
258
+ headers: responseHeaders,
259
+ body
260
+ } = await request(`http://${headers.host}${url ?? '/'}`, requestOptions)
261
+
262
+ res.writeHead(statusCode, responseHeaders)
263
+ body.pipe(res)
264
+ } catch (error) {
265
+ res.writeHead(502, { 'content-type': 'application/json' })
266
+ res.end(JSON.stringify(ensureLoggableError(error)))
267
+ }
256
268
  }
257
269
 
258
270
  #handleUnexpectedError (error, message, exitCode) {
@@ -11,7 +11,7 @@ import { basename, resolve } from 'node:path'
11
11
  import { fileURLToPath } from 'node:url'
12
12
  import { isMainThread } from 'node:worker_threads'
13
13
  import pino from 'pino'
14
- import { Agent, setGlobalDispatcher } from 'undici'
14
+ import { Agent, Pool, setGlobalDispatcher } from 'undici'
15
15
  import { WebSocket } from 'ws'
16
16
  import { exitCodes } from '../errors.js'
17
17
  import { importFile } from '../utils.js'
@@ -19,9 +19,21 @@ import { getSocketPath } from './child-manager.js'
19
19
 
20
20
  const windowsNpmExecutables = ['npm-prefix.js', 'npm-cli.js']
21
21
 
22
- function createInterceptor (itc) {
22
+ function createInterceptor () {
23
+ const pool = new Pool(
24
+ {
25
+ hostname: 'localhost',
26
+ protocol: 'http:'
27
+ },
28
+ {
29
+ socketPath: getSocketPath(process.env.PLT_MANAGER_ID),
30
+ keepAliveTimeout: 10,
31
+ keepAliveMaxTimeout: 10
32
+ }
33
+ )
34
+
23
35
  return function (dispatch) {
24
- return async (opts, handler) => {
36
+ return (opts, handler) => {
25
37
  let url = opts.origin
26
38
  if (!(url instanceof URL)) {
27
39
  url = new URL(opts.path, url)
@@ -32,49 +44,17 @@ function createInterceptor (itc) {
32
44
  return dispatch(opts, handler)
33
45
  }
34
46
 
35
- const headers = {
36
- ...opts?.headers
37
- }
38
-
39
- delete headers.connection
40
- delete headers['transfer-encoding']
41
- headers.host = url.host
42
-
43
- const requestOpts = {
44
- ...opts,
45
- headers
46
- }
47
- delete requestOpts.dispatcher
48
-
49
- itc
50
- .send('fetch', requestOpts)
51
- .then(res => {
52
- if (res.rawPayload && !Buffer.isBuffer(res.rawPayload)) {
53
- res.rawPayload = Buffer.from(res.rawPayload.data)
47
+ // Route the call via the UNIX socket
48
+ return pool.dispatch(
49
+ {
50
+ ...opts,
51
+ headers: {
52
+ host: url.host,
53
+ ...opts?.headers
54
54
  }
55
-
56
- const headers = []
57
- for (const [key, value] of Object.entries(res.headers)) {
58
- if (Array.isArray(value)) {
59
- for (const v of value) {
60
- headers.push(key)
61
- headers.push(v)
62
- }
63
- } else {
64
- headers.push(key)
65
- headers.push(value)
66
- }
67
- }
68
-
69
- handler.onHeaders(res.statusCode, headers, () => {}, res.statusMessage)
70
- handler.onData(res.rawPayload)
71
- handler.onComplete([])
72
- })
73
- .catch(e => {
74
- handler.onError(new Error(e.message))
75
- })
76
-
77
- return true
55
+ },
56
+ handler
57
+ )
78
58
  }
79
59
  }
80
60
  }
@@ -198,8 +178,12 @@ export class ChildProcess extends ITC {
198
178
  registers: [registry]
199
179
  })
200
180
 
201
- globalThis.platformatic.onHttpCacheHit = () => { cacheHitMetric.inc() }
202
- globalThis.platformatic.onHttpCacheMiss = () => { cacheMissMetric.inc() }
181
+ globalThis.platformatic.onHttpCacheHit = () => {
182
+ cacheHitMetric.inc()
183
+ }
184
+ globalThis.platformatic.onHttpCacheMiss = () => {
185
+ cacheMissMetric.inc()
186
+ }
203
187
  }
204
188
 
205
189
  async #getMetrics ({ format } = {}) {
@@ -232,8 +216,8 @@ export class ChildProcess extends ITC {
232
216
 
233
217
  this.#logger = pino(pinoOptions)
234
218
 
235
- Reflect.defineProperty(process, 'stdout', { value: createPinoWritable(this.#logger, 'info') })
236
- Reflect.defineProperty(process, 'stderr', { value: createPinoWritable(this.#logger, 'error', true) })
219
+ Reflect.defineProperty(process, 'stdout', { value: createPinoWritable(this.#logger, 'info', false, 'STDOUT') })
220
+ Reflect.defineProperty(process, 'stderr', { value: createPinoWritable(this.#logger, 'error', true, 'STDERR') })
237
221
  } else {
238
222
  this.#logger = pino(pinoOptions)
239
223
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/basic",
3
- "version": "2.35.0",
3
+ "version": "2.36.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -24,11 +24,11 @@
24
24
  "split2": "^4.2.0",
25
25
  "undici": "^7.0.0",
26
26
  "ws": "^8.18.0",
27
- "@platformatic/config": "2.35.0",
28
- "@platformatic/itc": "2.35.0",
29
- "@platformatic/metrics": "2.35.0",
30
- "@platformatic/telemetry": "2.35.0",
31
- "@platformatic/utils": "2.35.0"
27
+ "@platformatic/config": "2.36.1",
28
+ "@platformatic/itc": "2.36.1",
29
+ "@platformatic/metrics": "2.36.1",
30
+ "@platformatic/utils": "2.36.1",
31
+ "@platformatic/telemetry": "2.36.1"
32
32
  },
33
33
  "devDependencies": {
34
34
  "borp": "^0.19.0",
package/schema.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "$id": "https://schemas.platformatic.dev/@platformatic/basic/2.35.0.json",
2
+ "$id": "https://schemas.platformatic.dev/@platformatic/basic/2.36.1.json",
3
3
  "$schema": "http://json-schema.org/draft-07/schema#",
4
4
  "title": "Platformatic Stackable",
5
5
  "type": "object",