@platformatic/basic 2.0.0-alpha.4 → 2.0.0-alpha.5

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.
Files changed (103) hide show
  1. package/NOTICE +13 -0
  2. package/README.md +13 -0
  3. package/config.d.ts +1 -149
  4. package/index.js +60 -87
  5. package/lib/base.js +4 -4
  6. package/lib/schema.js +26 -33
  7. package/lib/utils.js +1 -5
  8. package/lib/worker/child-manager.js +12 -0
  9. package/lib/worker/child-process.js +66 -0
  10. package/package.json +6 -13
  11. package/schema.json +2 -512
  12. package/test/helper.js +38 -9
  13. package/lib/next.js +0 -105
  14. package/lib/server.js +0 -134
  15. package/lib/vite.js +0 -105
  16. package/lib/worker/next-loader.js +0 -137
  17. package/test/express.test.js +0 -34
  18. package/test/fastify.test.js +0 -34
  19. package/test/fixtures/express/no-build/index.js +0 -16
  20. package/test/fixtures/express/no-build/package.json +0 -7
  21. package/test/fixtures/express/no-build/platformatic.app.json +0 -3
  22. package/test/fixtures/express/no-build/platformatic.as-entrypoint.runtime.json +0 -19
  23. package/test/fixtures/express/no-build/platformatic.no-entrypoint.runtime.json +0 -16
  24. package/test/fixtures/express/with-build/index.js +0 -17
  25. package/test/fixtures/express/with-build/package.json +0 -7
  26. package/test/fixtures/express/with-build/platformatic.app.json +0 -3
  27. package/test/fixtures/express/with-build/platformatic.as-entrypoint.runtime.json +0 -19
  28. package/test/fixtures/express/with-build/platformatic.no-entrypoint.runtime.json +0 -16
  29. package/test/fixtures/fastify/no-build/index.js +0 -14
  30. package/test/fixtures/fastify/no-build/package.json +0 -7
  31. package/test/fixtures/fastify/no-build/platformatic.app.json +0 -3
  32. package/test/fixtures/fastify/no-build/platformatic.as-entrypoint.runtime.json +0 -19
  33. package/test/fixtures/fastify/no-build/platformatic.no-entrypoint.runtime.json +0 -16
  34. package/test/fixtures/fastify/with-build/index.js +0 -15
  35. package/test/fixtures/fastify/with-build/package.json +0 -7
  36. package/test/fixtures/fastify/with-build/platformatic.app.json +0 -3
  37. package/test/fixtures/fastify/with-build/platformatic.as-entrypoint.runtime.json +0 -19
  38. package/test/fixtures/fastify/with-build/platformatic.no-entrypoint.runtime.json +0 -16
  39. package/test/fixtures/next/composer-autodetect-prefix/next.config.mjs +0 -5
  40. package/test/fixtures/next/composer-autodetect-prefix/package.json +0 -15
  41. package/test/fixtures/next/composer-autodetect-prefix/platformatic.application.json +0 -8
  42. package/test/fixtures/next/composer-autodetect-prefix/platformatic.runtime.json +0 -21
  43. package/test/fixtures/next/composer-autodetect-prefix/src/app/layout.js +0 -7
  44. package/test/fixtures/next/composer-autodetect-prefix/src/app/page.js +0 -5
  45. package/test/fixtures/next/composer-with-prefix/next.config.js +0 -1
  46. package/test/fixtures/next/composer-with-prefix/package.json +0 -15
  47. package/test/fixtures/next/composer-with-prefix/platformatic.application.json +0 -11
  48. package/test/fixtures/next/composer-with-prefix/platformatic.runtime.json +0 -21
  49. package/test/fixtures/next/composer-with-prefix/src/app/layout.js +0 -7
  50. package/test/fixtures/next/composer-with-prefix/src/app/page.js +0 -5
  51. package/test/fixtures/next/composer-without-prefix/next.config.mjs +0 -3
  52. package/test/fixtures/next/composer-without-prefix/package.json +0 -15
  53. package/test/fixtures/next/composer-without-prefix/platformatic.application.json +0 -8
  54. package/test/fixtures/next/composer-without-prefix/platformatic.runtime.json +0 -21
  55. package/test/fixtures/next/composer-without-prefix/src/app/layout.js +0 -7
  56. package/test/fixtures/next/composer-without-prefix/src/app/page.js +0 -5
  57. package/test/fixtures/next/standalone/next.config.mjs +0 -3
  58. package/test/fixtures/next/standalone/package.json +0 -15
  59. package/test/fixtures/next/standalone/platformatic.runtime.json +0 -18
  60. package/test/fixtures/next/standalone/src/app/layout.js +0 -7
  61. package/test/fixtures/next/standalone/src/app/page.js +0 -5
  62. package/test/fixtures/nodejs/no-build/index.js +0 -21
  63. package/test/fixtures/nodejs/no-build/package.json +0 -7
  64. package/test/fixtures/nodejs/no-build/platformatic.app.json +0 -3
  65. package/test/fixtures/nodejs/no-build/platformatic.as-entrypoint.runtime.json +0 -19
  66. package/test/fixtures/nodejs/no-build/platformatic.no-entrypoint.runtime.json +0 -16
  67. package/test/fixtures/nodejs/no-configuration/index.js +0 -21
  68. package/test/fixtures/nodejs/no-configuration/platformatic.as-entrypoint.runtime.json +0 -18
  69. package/test/fixtures/nodejs/no-configuration/platformatic.no-entrypoint.runtime.json +0 -16
  70. package/test/fixtures/nodejs/with-build/index.js +0 -20
  71. package/test/fixtures/nodejs/with-build/package.json +0 -7
  72. package/test/fixtures/nodejs/with-build/platformatic.app.json +0 -3
  73. package/test/fixtures/nodejs/with-build/platformatic.as-entrypoint.runtime.json +0 -19
  74. package/test/fixtures/nodejs/with-build/platformatic.no-entrypoint.runtime.json +0 -16
  75. package/test/fixtures/platformatic-composer/platformatic.composer.json +0 -20
  76. package/test/fixtures/platformatic-composer/platformatic.no-prefix.composer.json +0 -23
  77. package/test/fixtures/platformatic-composer/plugin.js +0 -9
  78. package/test/fixtures/platformatic-service/platformatic.service.json +0 -15
  79. package/test/fixtures/platformatic-service/plugin.js +0 -12
  80. package/test/fixtures/vite/composer-autodetect-prefix/custom.vite.config.js +0 -3
  81. package/test/fixtures/vite/composer-autodetect-prefix/index.html +0 -13
  82. package/test/fixtures/vite/composer-autodetect-prefix/main.js +0 -3
  83. package/test/fixtures/vite/composer-autodetect-prefix/package.json +0 -14
  84. package/test/fixtures/vite/composer-autodetect-prefix/platformatic.application.json +0 -11
  85. package/test/fixtures/vite/composer-autodetect-prefix/platformatic.runtime.json +0 -21
  86. package/test/fixtures/vite/composer-with-prefix/index.html +0 -13
  87. package/test/fixtures/vite/composer-with-prefix/main.js +0 -3
  88. package/test/fixtures/vite/composer-with-prefix/package.json +0 -14
  89. package/test/fixtures/vite/composer-with-prefix/platformatic.application.json +0 -11
  90. package/test/fixtures/vite/composer-with-prefix/platformatic.runtime.json +0 -21
  91. package/test/fixtures/vite/composer-without-prefix/index.html +0 -13
  92. package/test/fixtures/vite/composer-without-prefix/main.js +0 -3
  93. package/test/fixtures/vite/composer-without-prefix/package.json +0 -14
  94. package/test/fixtures/vite/composer-without-prefix/platformatic.application.json +0 -8
  95. package/test/fixtures/vite/composer-without-prefix/platformatic.runtime.json +0 -21
  96. package/test/fixtures/vite/standalone/custom.vite.config.js +0 -3
  97. package/test/fixtures/vite/standalone/index.html +0 -13
  98. package/test/fixtures/vite/standalone/main.js +0 -3
  99. package/test/fixtures/vite/standalone/package.json +0 -14
  100. package/test/fixtures/vite/standalone/platformatic.runtime.json +0 -18
  101. package/test/next.test.js +0 -85
  102. package/test/node.test.js +0 -66
  103. package/test/vite.test.js +0 -89
package/NOTICE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2024 Platformatic
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # @platformatic/basic
2
+
3
+ Check out the full documentation for Platformatic on [our website](https://docs.platformatic.dev).
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install @platformatic/basic
9
+ ```
10
+
11
+ ## License
12
+
13
+ Apache 2.0
package/config.d.ts CHANGED
@@ -7,153 +7,5 @@
7
7
 
8
8
  export interface PlatformaticStackable {
9
9
  $schema?: string;
10
- server?: {
11
- hostname?: string;
12
- port?: number | string;
13
- pluginTimeout?: number;
14
- healthCheck?:
15
- | boolean
16
- | {
17
- enabled?: boolean;
18
- interval?: number;
19
- [k: string]: unknown;
20
- };
21
- ignoreTrailingSlash?: boolean;
22
- ignoreDuplicateSlashes?: boolean;
23
- connectionTimeout?: number;
24
- keepAliveTimeout?: number;
25
- maxRequestsPerSocket?: number;
26
- forceCloseConnections?: boolean | string;
27
- requestTimeout?: number;
28
- bodyLimit?: number;
29
- maxParamLength?: number;
30
- disableRequestLogging?: boolean;
31
- exposeHeadRoutes?: boolean;
32
- logger?:
33
- | boolean
34
- | {
35
- level?: string;
36
- transport?:
37
- | {
38
- target?: string;
39
- options?: {
40
- [k: string]: unknown;
41
- };
42
- }
43
- | {
44
- targets?: {
45
- target?: string;
46
- options?: {
47
- [k: string]: unknown;
48
- };
49
- level?: string;
50
- additionalProperties?: never;
51
- [k: string]: unknown;
52
- }[];
53
- options?: {
54
- [k: string]: unknown;
55
- };
56
- };
57
- pipeline?: {
58
- target?: string;
59
- options?: {
60
- [k: string]: unknown;
61
- };
62
- };
63
- [k: string]: unknown;
64
- };
65
- serializerOpts?: {
66
- schema?: {
67
- [k: string]: unknown;
68
- };
69
- ajv?: {
70
- [k: string]: unknown;
71
- };
72
- rounding?: "floor" | "ceil" | "round" | "trunc";
73
- debugMode?: boolean;
74
- mode?: "debug" | "standalone";
75
- largeArraySize?: number | string;
76
- largeArrayMechanism?: "default" | "json-stringify";
77
- [k: string]: unknown;
78
- };
79
- caseSensitive?: boolean;
80
- requestIdHeader?: string | false;
81
- requestIdLogLabel?: string;
82
- jsonShorthand?: boolean;
83
- trustProxy?: boolean | string | string[] | number;
84
- http2?: boolean;
85
- https?: {
86
- allowHTTP1?: boolean;
87
- key:
88
- | string
89
- | {
90
- path?: string;
91
- }
92
- | (
93
- | string
94
- | {
95
- path?: string;
96
- }
97
- )[];
98
- cert:
99
- | string
100
- | {
101
- path?: string;
102
- }
103
- | (
104
- | string
105
- | {
106
- path?: string;
107
- }
108
- )[];
109
- requestCert?: boolean;
110
- rejectUnauthorized?: boolean;
111
- };
112
- cors?: {
113
- origin?:
114
- | boolean
115
- | string
116
- | (
117
- | string
118
- | {
119
- regexp: string;
120
- [k: string]: unknown;
121
- }
122
- )[]
123
- | {
124
- regexp: string;
125
- [k: string]: unknown;
126
- };
127
- methods?: string[];
128
- /**
129
- * Comma separated string of allowed headers.
130
- */
131
- allowedHeaders?: string;
132
- exposedHeaders?: string[] | string;
133
- credentials?: boolean;
134
- maxAge?: number;
135
- preflightContinue?: boolean;
136
- optionsSuccessStatus?: number;
137
- preflight?: boolean;
138
- strictPreflight?: boolean;
139
- hideOptionsRoute?: boolean;
140
- };
141
- };
142
- watch?:
143
- | {
144
- enabled?: boolean | string;
145
- /**
146
- * @minItems 1
147
- */
148
- allow?: [string, ...string[]];
149
- ignore?: string[];
150
- }
151
- | boolean
152
- | string;
153
- application?: {
154
- base?: string;
155
- };
156
- vite?: {
157
- configFile?: string | boolean;
158
- };
10
+ [k: string]: unknown;
159
11
  }
package/index.js CHANGED
@@ -1,118 +1,91 @@
1
- 'use strict'
2
-
3
1
  import { existsSync } from 'node:fs'
4
2
  import { readFile } from 'node:fs/promises'
5
- import { resolve } from 'node:path'
6
-
7
- import { ConfigManager } from '@platformatic/config'
8
-
9
- import { NextStackable } from './lib/next.js'
3
+ import { createRequire } from 'node:module'
4
+ import { relative, resolve } from 'node:path'
5
+ import { workerData } from 'node:worker_threads'
10
6
  import { packageJson, schema } from './lib/schema.js'
11
- import { ServerStackable } from './lib/server.js'
12
- import { ViteStackable } from './lib/vite.js'
13
-
14
- const validFields = [
15
- 'main',
16
- 'exports',
17
- 'exports',
18
- 'exports#node',
19
- 'exports#import',
20
- 'exports#require',
21
- 'exports#default',
22
- 'exports#.#node',
23
- 'exports#.#import',
24
- 'exports#.#require',
25
- 'exports#.#default',
26
- ]
27
-
28
- const validFilesBasenames = ['index', 'main', 'app', 'application', 'server', 'start', 'bundle', 'run', 'entrypoint']
29
-
30
- async function parsePackageJson (root) {
31
- let entrypoint
32
- let packageJson
33
- let hadEntrypointField = false
7
+ import { importFile } from './lib/utils.js'
34
8
 
9
+ async function importStackablePackage (opts, pkg, autodetectDescription) {
35
10
  try {
36
- packageJson = JSON.parse(await readFile(resolve(root, 'package.json'), 'utf-8'))
37
- } catch {
38
- // No package.json, we only load the index.js file
39
- packageJson = {}
40
- }
11
+ try {
12
+ // Try regular import
13
+ return await import(pkg)
14
+ } catch (e) {
15
+ // Scope to the service
16
+ const require = createRequire(resolve(opts.context.directory, 'index.js'))
17
+ const imported = require.resolve(pkg)
18
+ return await importFile(imported)
19
+ }
20
+ } catch (e) {
21
+ const rootFolder = relative(process.cwd(), workerData.dirname)
41
22
 
42
- for (const field of validFields) {
43
- let current = packageJson
44
- const sequence = field.split('#')
23
+ let errorMessage = `Unable to import package, "${pkg}". Please add it as a dependency `
45
24
 
46
- while (current && sequence.length && typeof current !== 'string') {
47
- current = current[sequence.shift()]
25
+ if (rootFolder) {
26
+ errorMessage += `in the package.json file in the folder ${rootFolder}.`
27
+ } else {
28
+ errorMessage += 'in the root package.json file.'
48
29
  }
49
30
 
50
- if (typeof current === 'string') {
51
- entrypoint = current
52
- hadEntrypointField = true
53
- break
31
+ if (!opts.config) {
32
+ const serviceRoot = relative(process.cwd(), opts.context.directory)
33
+ errorMessage += [
34
+ '', // Do not remove this
35
+ `Platformatic has auto-detected that service ${opts.context.serviceId} ${autodetectDescription}.`,
36
+ `We suggest you create a platformatic.application.json file in the folder ${serviceRoot} with the "$schema" property set to "https://schemas.platformatic.dev/${pkg}/${packageJson.version}.json".`,
37
+ ].join('\n')
54
38
  }
55
- }
56
-
57
- if (!entrypoint) {
58
- for (const basename of validFilesBasenames) {
59
- for (const ext of ['js', 'mjs', 'cjs']) {
60
- const file = `${basename}.${ext}`
61
-
62
- if (existsSync(resolve(root, file))) {
63
- entrypoint = file
64
- break
65
- }
66
- }
67
39
 
68
- if (entrypoint) {
69
- break
70
- }
71
- }
40
+ throw new Error(errorMessage)
72
41
  }
73
-
74
- return { packageJson, entrypoint, hadEntrypointField }
75
42
  }
76
43
 
77
- function transformConfig () {
78
- if (this.current.watch === undefined) {
79
- this.current.watch = { enabled: false }
80
- }
44
+ async function buildStackable (opts) {
45
+ const root = opts.context.directory
46
+ let toImport = '@platformatic/node'
47
+ let autodetectDescription = 'is using a generic Node.js application'
81
48
 
82
- if (typeof this.current.watch !== 'object') {
83
- this.current.watch = { enabled: this.current.watch || false }
49
+ let rootPackageJson
50
+ try {
51
+ rootPackageJson = JSON.parse(await readFile(resolve(root, 'package.json'), 'utf-8'))
52
+ } catch {
53
+ rootPackageJson = {}
84
54
  }
85
- }
86
55
 
87
- export async function buildStackable (opts) {
88
- const root = opts.context.directory
89
- const {
90
- entrypoint,
91
- hadEntrypointField,
92
- packageJson: { dependencies, devDependencies },
93
- } = await parsePackageJson(root)
56
+ if (!opts.config) {
57
+ const candidate = resolve(root, 'platformatic.application.json')
58
+
59
+ if (existsSync(candidate)) {
60
+ opts.config = candidate
61
+ }
62
+ }
94
63
 
95
- const configManager = new ConfigManager({ schema, source: opts.config ?? {} })
96
- await configManager.parseAndValidate()
64
+ const { dependencies, devDependencies } = rootPackageJson
97
65
 
98
- let stackable
99
66
  if (dependencies?.next || devDependencies?.next) {
100
- stackable = new NextStackable(opts, root, configManager)
67
+ autodetectDescription = 'is using Next.js'
68
+ toImport = '@platformatic/next'
101
69
  } else if (dependencies?.vite || devDependencies?.vite) {
102
- stackable = new ViteStackable(opts, root, configManager)
103
- } else {
104
- stackable = new ServerStackable(opts, root, configManager, entrypoint, hadEntrypointField)
70
+ autodetectDescription = 'is using Vite'
71
+ toImport = '@platformatic/vite'
105
72
  }
106
73
 
107
- return stackable
74
+ const imported = await importStackablePackage(opts, toImport, autodetectDescription)
75
+ return imported.buildStackable(opts)
108
76
  }
109
77
 
110
78
  export default {
111
79
  configType: 'nodejs',
112
- configManagerConfig: {
113
- transformConfig,
114
- },
80
+ configManagerConfig: {},
115
81
  buildStackable,
116
82
  schema,
117
83
  version: packageJson.version,
118
84
  }
85
+
86
+ export * from './lib/base.js'
87
+ export * as errors from './lib/errors.js'
88
+ export { schema, schemaComponents } from './lib/schema.js'
89
+ export * from './lib/utils.js'
90
+ export * from './lib/worker/child-manager.js'
91
+ export * from './lib/worker/server-listener.js'
package/lib/base.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import pino from 'pino'
2
- import { packageJson } from './schema.js'
3
2
 
4
3
  export class BaseStackable {
5
- constructor (options, root, configManager) {
6
- this.type = 'nodejs'
4
+ constructor (type, version, options, root, configManager) {
5
+ this.type = type
6
+ this.version = version
7
7
  this.id = options.context.serviceId
8
8
  this.root = root
9
9
  this.configManager = configManager
@@ -50,7 +50,7 @@ export class BaseStackable {
50
50
  }
51
51
 
52
52
  async getInfo () {
53
- return { type: this.type, version: packageJson.version }
53
+ return { type: this.type, version: this.version }
54
54
  }
55
55
 
56
56
  getDispatchFunc () {
package/lib/schema.js CHANGED
@@ -1,8 +1,31 @@
1
1
  import { schemas } from '@platformatic/utils'
2
2
  import { readFileSync } from 'node:fs'
3
- import { fileURLToPath } from 'node:url'
4
3
 
5
- export const packageJson = JSON.parse(readFileSync(fileURLToPath(new URL('../package.json', import.meta.url))))
4
+ export const packageJson = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf-8'))
5
+
6
+ const application = {
7
+ type: 'object',
8
+ properties: {
9
+ basePath: {
10
+ type: 'string',
11
+ },
12
+ },
13
+ additionalProperties: false,
14
+ }
15
+
16
+ const watch = {
17
+ anyOf: [
18
+ schemas.watch,
19
+ {
20
+ type: 'boolean',
21
+ },
22
+ {
23
+ type: 'string',
24
+ },
25
+ ],
26
+ }
27
+
28
+ export const schemaComponents = { application, watch }
6
29
 
7
30
  export const schema = {
8
31
  $id: `https://schemas.platformatic.dev/@platformatic/basic/${packageJson.version}.json`,
@@ -13,38 +36,8 @@ export const schema = {
13
36
  $schema: {
14
37
  type: 'string',
15
38
  },
16
- server: schemas.server,
17
- watch: {
18
- anyOf: [
19
- schemas.watch,
20
- {
21
- type: 'boolean',
22
- },
23
- {
24
- type: 'string',
25
- },
26
- ],
27
- },
28
- application: {
29
- type: 'object',
30
- properties: {
31
- base: {
32
- type: 'string',
33
- },
34
- },
35
- additionalProperties: false,
36
- },
37
- vite: {
38
- type: 'object',
39
- properties: {
40
- configFile: {
41
- oneOf: [{ type: 'string' }, { type: 'boolean' }],
42
- },
43
- },
44
- additionalProperties: false,
45
- },
46
39
  },
47
- additionalProperties: false,
40
+ additionalProperties: true,
48
41
  }
49
42
 
50
43
  /* c8 ignore next 3 */
package/lib/utils.js CHANGED
@@ -7,11 +7,6 @@ export function getServerUrl (server) {
7
7
  return new URL(family === 'IPv6' ? `http://[${address}]:${port}` : `http://${address}:${port}`).origin
8
8
  }
9
9
 
10
- // Paolo: This is kinda hackish but there is no better way. I apologize.
11
- export function isFastify (app) {
12
- return Object.getOwnPropertySymbols(app).some(s => s.description === 'fastify.state')
13
- }
14
-
15
10
  export async function injectViaRequest (baseUrl, injectParams, onInject) {
16
11
  const url = new URL(injectParams.url, baseUrl).href
17
12
  const requestParams = { method: injectParams.method, headers: injectParams.headers }
@@ -43,6 +38,7 @@ export async function injectViaRequest (baseUrl, injectParams, onInject) {
43
38
  }
44
39
  }
45
40
 
41
+ // This is to avoid common path/URL problems on Windows
46
42
  export function importFile (path) {
47
43
  return import(pathToFileURL(path))
48
44
  }
@@ -2,6 +2,7 @@ import { ITC } from '@platformatic/itc'
2
2
  import { subscribe, unsubscribe } from 'node:diagnostics_channel'
3
3
  import { once } from 'node:events'
4
4
  import { workerData } from 'node:worker_threads'
5
+ import { request } from 'undici'
5
6
 
6
7
  export const childProcessWorkerFile = new URL('./child-process.js', import.meta.url)
7
8
 
@@ -31,6 +32,8 @@ export class ChildManager extends ITC {
31
32
  workerData.loggingPort.postMessage(JSON.parse(message))
32
33
  })
33
34
 
35
+ this.handle('fetch', this.#fetch.bind(this))
36
+
34
37
  this.#prepareChildEnvironment(loader, context)
35
38
  }
36
39
 
@@ -75,4 +78,13 @@ export class ChildManager extends ITC {
75
78
  process.env.NODE_OPTIONS ?? '',
76
79
  ].join(' ')
77
80
  }
81
+
82
+ async #fetch (opts) {
83
+ const { statusCode, headers, body } = await request(opts)
84
+
85
+ const rawPayload = Buffer.from(await body.arrayBuffer())
86
+ const payload = rawPayload.toString()
87
+
88
+ return { statusCode, headers, body: payload, payload, rawPayload }
89
+ }
78
90
  }
@@ -2,6 +2,67 @@ import { ITC } from '@platformatic/itc'
2
2
  import { createPinoWritable, DestinationWritable, withResolvers } from '@platformatic/utils'
3
3
  import { tracingChannel } from 'node:diagnostics_channel'
4
4
  import pino from 'pino'
5
+ import { getGlobalDispatcher, setGlobalDispatcher } from 'undici'
6
+
7
+ function createInterceptor (itc) {
8
+ return function (dispatch) {
9
+ return async (opts, handler) => {
10
+ let url = opts.origin
11
+ if (!(url instanceof URL)) {
12
+ url = new URL(opts.path, url)
13
+ }
14
+
15
+ // Other URLs are handled normally
16
+ if (!url.hostname.endsWith('.plt.local')) {
17
+ return dispatch(opts, handler)
18
+ }
19
+
20
+ const headers = {
21
+ ...opts?.headers,
22
+ }
23
+
24
+ delete headers.connection
25
+ delete headers['transfer-encoding']
26
+ headers.host = url.host
27
+
28
+ const requestOpts = {
29
+ ...opts,
30
+ headers,
31
+ }
32
+ delete requestOpts.dispatcher
33
+
34
+ itc
35
+ .send('fetch', requestOpts)
36
+ .then(res => {
37
+ if (res.rawPayload && !Buffer.isBuffer(res.rawPayload)) {
38
+ res.rawPayload = Buffer.from(res.rawPayload.data)
39
+ }
40
+
41
+ const headers = []
42
+ for (const [key, value] of Object.entries(res.headers)) {
43
+ if (Array.isArray(value)) {
44
+ for (const v of value) {
45
+ headers.push(key)
46
+ headers.push(v)
47
+ }
48
+ } else {
49
+ headers.push(key)
50
+ headers.push(value)
51
+ }
52
+ }
53
+
54
+ handler.onHeaders(res.statusCode, headers, () => {}, res.statusMessage)
55
+ handler.onData(res.rawPayload)
56
+ handler.onComplete([])
57
+ })
58
+ .catch(e => {
59
+ handler.onError(new Error(e.message))
60
+ })
61
+
62
+ return true
63
+ }
64
+ }
65
+ }
5
66
 
6
67
  class ChildProcessWritable extends DestinationWritable {
7
68
  #itc
@@ -29,6 +90,7 @@ class ChildProcess extends ITC {
29
90
  this.listen()
30
91
  this.#setupLogger()
31
92
  this.#setupServer()
93
+ this.#setupInterceptors()
32
94
  }
33
95
 
34
96
  _setupListener (listener) {
@@ -79,6 +141,10 @@ class ChildProcess extends ITC {
79
141
 
80
142
  tracingChannel('net.server.listen').subscribe(subscribers)
81
143
  }
144
+
145
+ #setupInterceptors () {
146
+ setGlobalDispatcher(getGlobalDispatcher().compose(createInterceptor(this)))
147
+ }
82
148
  }
83
149
 
84
150
  globalThis[Symbol.for('plt.children.itc')] = new ChildProcess()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/basic",
3
- "version": "2.0.0-alpha.4",
3
+ "version": "2.0.0-alpha.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -15,17 +15,13 @@
15
15
  },
16
16
  "homepage": "https://github.com/platformatic/platformatic#readme",
17
17
  "dependencies": {
18
- "@babel/generator": "^7.25.0",
19
- "@babel/parser": "^7.25.3",
20
- "@babel/traverse": "^7.25.3",
21
- "@babel/types": "^7.25.2",
22
18
  "@fastify/error": "^4.0.0",
23
- "light-my-request": "^5.13.0",
24
19
  "pino": "^9.3.2",
25
20
  "semver": "^7.6.3",
26
21
  "undici": "^6.19.5",
27
- "@platformatic/config": "2.0.0-alpha.4",
28
- "@platformatic/itc": "2.0.0-alpha.4"
22
+ "@platformatic/config": "2.0.0-alpha.5",
23
+ "@platformatic/utils": "2.0.0-alpha.5",
24
+ "@platformatic/itc": "2.0.0-alpha.5"
29
25
  },
30
26
  "devDependencies": {
31
27
  "borp": "^0.17.0",
@@ -40,13 +36,10 @@
40
36
  "typescript": "^5.5.4",
41
37
  "vite": "^5.4.0",
42
38
  "ws": "^8.18.0",
43
- "@platformatic/composer": "2.0.0-alpha.4",
44
- "@platformatic/utils": "2.0.0-alpha.4",
45
- "@platformatic/service": "2.0.0-alpha.4"
39
+ "@platformatic/composer": "2.0.0-alpha.5",
40
+ "@platformatic/service": "2.0.0-alpha.5"
46
41
  },
47
42
  "scripts": {
48
- "test": "npm run lint && borp --concurrency=1 --timeout=180000",
49
- "coverage": "npm run lint && borp -C -X test -X test/fixtures --concurrency=1 --timeout=180000",
50
43
  "gen-schema": "node lib/schema.js > schema.json",
51
44
  "gen-types": "json2ts > config.d.ts < schema.json",
52
45
  "build": "pnpm run gen-schema && pnpm run gen-types",