@platformatic/node 2.0.0-alpha.7 → 2.0.0-alpha.9

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 (43) hide show
  1. package/config.d.ts +15 -5
  2. package/index.js +140 -34
  3. package/lib/schema.js +21 -7
  4. package/package.json +11 -10
  5. package/schema.json +42 -13
  6. package/test/express.test.js +0 -54
  7. package/test/fastify.test.js +0 -54
  8. package/test/fixtures/express/no-build/index.js +0 -16
  9. package/test/fixtures/express/no-build/package.json +0 -7
  10. package/test/fixtures/express/no-build/platformatic.application.json +0 -3
  11. package/test/fixtures/express/no-build/platformatic.as-entrypoint.runtime.json +0 -19
  12. package/test/fixtures/express/no-build/platformatic.no-entrypoint.runtime.json +0 -16
  13. package/test/fixtures/express/with-build/index.js +0 -17
  14. package/test/fixtures/express/with-build/package.json +0 -7
  15. package/test/fixtures/express/with-build/platformatic.application.json +0 -3
  16. package/test/fixtures/express/with-build/platformatic.as-entrypoint.runtime.json +0 -19
  17. package/test/fixtures/express/with-build/platformatic.no-entrypoint.runtime.json +0 -16
  18. package/test/fixtures/fastify/no-build/index.js +0 -14
  19. package/test/fixtures/fastify/no-build/package.json +0 -7
  20. package/test/fixtures/fastify/no-build/platformatic.application.json +0 -3
  21. package/test/fixtures/fastify/no-build/platformatic.as-entrypoint.runtime.json +0 -19
  22. package/test/fixtures/fastify/no-build/platformatic.no-entrypoint.runtime.json +0 -16
  23. package/test/fixtures/fastify/with-build/index.js +0 -15
  24. package/test/fixtures/fastify/with-build/package.json +0 -7
  25. package/test/fixtures/fastify/with-build/platformatic.application.json +0 -3
  26. package/test/fixtures/fastify/with-build/platformatic.as-entrypoint.runtime.json +0 -19
  27. package/test/fixtures/fastify/with-build/platformatic.no-entrypoint.runtime.json +0 -16
  28. package/test/fixtures/nodejs/no-build/index.js +0 -21
  29. package/test/fixtures/nodejs/no-build/package.json +0 -7
  30. package/test/fixtures/nodejs/no-build/platformatic.application.json +0 -3
  31. package/test/fixtures/nodejs/no-build/platformatic.as-entrypoint.runtime.json +0 -19
  32. package/test/fixtures/nodejs/no-build/platformatic.no-entrypoint.runtime.json +0 -16
  33. package/test/fixtures/nodejs/no-configuration/index.js +0 -21
  34. package/test/fixtures/nodejs/no-configuration/platformatic.as-entrypoint.runtime.json +0 -18
  35. package/test/fixtures/nodejs/no-configuration/platformatic.no-entrypoint.runtime.json +0 -16
  36. package/test/fixtures/nodejs/with-build/index.js +0 -20
  37. package/test/fixtures/nodejs/with-build/package.json +0 -7
  38. package/test/fixtures/nodejs/with-build/platformatic.application.json +0 -3
  39. package/test/fixtures/nodejs/with-build/platformatic.as-entrypoint.runtime.json +0 -19
  40. package/test/fixtures/nodejs/with-build/platformatic.no-entrypoint.runtime.json +0 -16
  41. package/test/fixtures/platformatic-service/platformatic.service.json +0 -15
  42. package/test/fixtures/platformatic-service/plugin.js +0 -12
  43. package/test/node.test.js +0 -97
package/config.d.ts CHANGED
@@ -62,6 +62,9 @@ export interface PlatformaticNodeJsStackable {
62
62
  };
63
63
  [k: string]: unknown;
64
64
  };
65
+ loggerInstance?: {
66
+ [k: string]: unknown;
67
+ };
65
68
  serializerOpts?: {
66
69
  schema?: {
67
70
  [k: string]: unknown;
@@ -150,11 +153,18 @@ export interface PlatformaticNodeJsStackable {
150
153
  }
151
154
  | boolean
152
155
  | string;
153
- deploy?: {
156
+ application?: {
157
+ basePath?: string;
158
+ outputDirectory?: string;
154
159
  include?: string[];
155
- buildCommand?: string;
156
- installCommand?: string;
157
- startCommand?: string;
158
- [k: string]: unknown;
160
+ commands?: {
161
+ install?: string;
162
+ build?: string;
163
+ development?: string;
164
+ production?: string;
165
+ };
166
+ };
167
+ node?: {
168
+ entrypoint?: string;
159
169
  };
160
170
  }
package/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import {
2
2
  BaseStackable,
3
+ cleanBasePath,
3
4
  createServerListener,
5
+ ensureTrailingSlash,
4
6
  getServerUrl,
5
7
  importFile,
6
8
  injectViaRequest,
@@ -13,6 +15,7 @@ import { existsSync } from 'node:fs'
13
15
  import { readFile } from 'node:fs/promises'
14
16
  import { Server } from 'node:http'
15
17
  import { resolve as pathResolve, resolve } from 'node:path'
18
+ import { pathToFileURL } from 'url'
16
19
  import { packageJson, schema } from './lib/schema.js'
17
20
 
18
21
  const validFields = [
@@ -37,18 +40,14 @@ function isFastify (app) {
37
40
  }
38
41
 
39
42
  export class NodeStackable extends BaseStackable {
40
- #entrypoint
41
- #hadEntrypointField
42
43
  #module
43
44
  #app
44
45
  #server
45
46
  #dispatcher
46
47
  #isFastify
47
48
 
48
- constructor (options, root, configManager, entrypoint, hadEntrypointField) {
49
+ constructor (options, root, configManager) {
49
50
  super('nodejs', packageJson.version, options, root, configManager)
50
- this.#entrypoint = entrypoint
51
- this.#hadEntrypointField = hadEntrypointField
52
51
  }
53
52
 
54
53
  async start ({ listen }) {
@@ -63,17 +62,36 @@ export class NodeStackable extends BaseStackable {
63
62
  return this.url
64
63
  }
65
64
 
66
- // Require the application
67
- if (!this.#hadEntrypointField) {
68
- this.logger.warn(
69
- `The service ${this.id} had no valid entrypoint defined in the package.json file. Falling back to the file ${this.#entrypoint}.`
70
- )
65
+ const config = this.configManager.current
66
+ const command = config.application.commands[this.isProduction ? 'production' : 'development']
67
+
68
+ if (command) {
69
+ return this.startWithCommand(command)
71
70
  }
72
71
 
72
+ // Resolve the entrypoint
73
+ // The priority is platformatic.application.json, then package.json and finally autodetect.
74
+ // Only when autodetecting we eventually search in the dist folder when in production mode
75
+ const finalEntrypoint = await this._findEntrypoint()
76
+
77
+ // Require the application
78
+ const basePath = config.application?.basePath
79
+ ? ensureTrailingSlash(cleanBasePath(config.application?.basePath))
80
+ : undefined
81
+
82
+ this.registerGlobals({
83
+ // Always use URL to avoid serialization problem in Windows
84
+ id: this.id,
85
+ root: pathToFileURL(this.root).toString(),
86
+ basePath,
87
+ logLevel: this.logger.level
88
+ })
89
+
73
90
  // The server promise must be created before requiring the entrypoint even if it's not going to be used
74
91
  // at all. Otherwise there is chance we miss the listen event.
75
- const serverPromise = createServerListener()
76
- this.#module = await importFile(pathResolve(this.root, this.#entrypoint))
92
+ const serverOptions = this.serverConfig
93
+ const serverPromise = createServerListener((this.isEntrypoint ? serverOptions?.port : undefined) ?? true)
94
+ this.#module = await importFile(finalEntrypoint)
77
95
  this.#module = this.#module.default || this.#module
78
96
 
79
97
  // Deal with application
@@ -100,37 +118,60 @@ export class NodeStackable extends BaseStackable {
100
118
  }
101
119
 
102
120
  async stop () {
121
+ if (this.subprocess) {
122
+ return this.stopCommand()
123
+ }
124
+
103
125
  if (this.#isFastify) {
104
126
  return this.#app.close()
105
127
  }
106
128
 
107
- if (this.#server) {
108
- /* c8 ignore next 3 */
109
- if (!this.#server.listening) {
110
- return
111
- }
129
+ /* c8 ignore next 3 */
130
+ if (!this.#server?.listening) {
131
+ return
132
+ }
112
133
 
113
- return new Promise((resolve, reject) => {
114
- this.#server.close(error => {
115
- /* c8 ignore next 3 */
116
- if (error) {
117
- return reject(error)
118
- }
134
+ return new Promise((resolve, reject) => {
135
+ this.#server.close(error => {
136
+ /* c8 ignore next 3 */
137
+ if (error) {
138
+ return reject(error)
139
+ }
119
140
 
120
- resolve(error)
121
- })
141
+ resolve()
122
142
  })
143
+ })
144
+ }
145
+
146
+ async build () {
147
+ const command = this.configManager.current.application.commands.build
148
+
149
+ if (command) {
150
+ return this.buildWithCommand(command, null)
123
151
  }
152
+
153
+ // If no command was specified, we try to see if there is a build script defined in package.json.
154
+ const hasBuildScript = await this.#hasBuildScript()
155
+
156
+ if (!hasBuildScript) {
157
+ this.logger.warn(
158
+ 'No "application.commands.build" configuration value specified and no build script found in package.json. Skipping build ...'
159
+ )
160
+ return
161
+ }
162
+
163
+ return this.buildWithCommand('npm run build', null)
124
164
  }
125
165
 
126
166
  async inject (injectParams, onInject) {
127
167
  let res
128
- if (this.#isFastify) {
168
+
169
+ if (this.url) {
170
+ res = await injectViaRequest(this.url, injectParams, onInject)
171
+ } else if (this.#isFastify) {
129
172
  res = await this.#app.inject(injectParams, onInject)
130
- } else if (this.#dispatcher) {
131
- res = await inject(this.#dispatcher, injectParams, onInject)
132
173
  } else {
133
- res = await injectViaRequest(this.url, injectParams, onInject)
174
+ res = await inject(this.#dispatcher ?? this.#app, injectParams, onInject)
134
175
  }
135
176
 
136
177
  /* c8 ignore next 3 */
@@ -145,9 +186,22 @@ export class NodeStackable extends BaseStackable {
145
186
  }
146
187
 
147
188
  getMeta () {
148
- return {
149
- deploy: this.configManager.current.deploy
189
+ const config = this.configManager.current
190
+ let composer = { prefix: this.servicePrefix, wantsAbsoluteUrls: true, needsRootRedirect: true }
191
+
192
+ if (this.url) {
193
+ composer = {
194
+ tcp: true,
195
+ url: this.url,
196
+ prefix: config.application?.basePath
197
+ ? ensureTrailingSlash(cleanBasePath(config.application?.basePath))
198
+ : this.servicePrefix,
199
+ wantsAbsoluteUrls: true,
200
+ needsRootRedirect: true
201
+ }
150
202
  }
203
+
204
+ return { composer }
151
205
  }
152
206
 
153
207
  async _listen () {
@@ -175,6 +229,58 @@ export class NodeStackable extends BaseStackable {
175
229
  _getApplication () {
176
230
  return this.#app
177
231
  }
232
+
233
+ async _findEntrypoint () {
234
+ const config = this.configManager.current
235
+ const outputRoot = resolve(this.root, config.application.outputDirectory)
236
+
237
+ if (config.node.entrypoint) {
238
+ return pathResolve(this.root, config.node.entrypoint)
239
+ }
240
+
241
+ const { entrypoint, hadEntrypointField } = await getEntrypointInformation(this.root)
242
+
243
+ if (!entrypoint) {
244
+ this.logger.error(
245
+ `The service ${this.id} had no valid entrypoint defined in the package.json file and no valid entrypoint file was found.`
246
+ )
247
+
248
+ process.exit(1)
249
+ }
250
+
251
+ if (!hadEntrypointField) {
252
+ this.logger.warn(
253
+ `The service ${this.id} had no valid entrypoint defined in the package.json file. Falling back to the file "${entrypoint}".`
254
+ )
255
+ }
256
+
257
+ let root = this.root
258
+
259
+ if (this.isProduction) {
260
+ const hasCommand = this.configManager.current.application.commands.build
261
+ const hasBuildScript = await this.#hasBuildScript()
262
+
263
+ if (hasCommand || hasBuildScript) {
264
+ this.verifyOutputDirectory(outputRoot)
265
+ root = outputRoot
266
+ }
267
+ }
268
+
269
+ return pathResolve(root, entrypoint)
270
+ }
271
+
272
+ async #hasBuildScript () {
273
+ // If no command was specified, we try to see if there is a build script defined in package.json.
274
+ let hasBuildScript
275
+ try {
276
+ const packageJson = JSON.parse(await readFile(resolve(this.root, 'package.json'), 'utf-8'))
277
+ hasBuildScript = typeof packageJson.scripts.build === 'string' && packageJson.scripts.build
278
+ } catch (e) {
279
+ // No-op
280
+ }
281
+
282
+ return hasBuildScript
283
+ }
178
284
  }
179
285
 
180
286
  async function getEntrypointInformation (root) {
@@ -227,14 +333,14 @@ async function getEntrypointInformation (root) {
227
333
  export async function buildStackable (opts) {
228
334
  const root = opts.context.directory
229
335
 
230
- const { entrypoint, hadEntrypointField } = await getEntrypointInformation(root)
231
-
232
336
  const configManager = new ConfigManager({ schema, source: opts.config ?? {}, schemaOptions, transformConfig })
233
337
  await configManager.parseAndValidate()
234
338
 
235
- return new NodeStackable(opts, root, configManager, entrypoint, hadEntrypointField)
339
+ return new NodeStackable(opts, root, configManager)
236
340
  }
237
341
 
342
+ export { schema, schemaComponents } from './lib/schema.js'
343
+
238
344
  export default {
239
345
  configType: 'nodejs',
240
346
  configManagerConfig: {
package/lib/schema.js CHANGED
@@ -1,9 +1,22 @@
1
- import { schemaComponents } from '@platformatic/basic'
2
- import { schemas as utilsSchema } from '@platformatic/utils'
1
+ import { schemaComponents as basicSchemaComponents } from '@platformatic/basic'
2
+ import { schemaComponents as utilsSchemaComponents } from '@platformatic/utils'
3
3
  import { readFileSync } from 'node:fs'
4
4
 
5
5
  export const packageJson = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf-8'))
6
6
 
7
+ const node = {
8
+ type: 'object',
9
+ properties: {
10
+ entrypoint: {
11
+ type: 'string'
12
+ }
13
+ },
14
+ default: {},
15
+ additionalProperties: false
16
+ }
17
+
18
+ export const schemaComponents = { node }
19
+
7
20
  export const schema = {
8
21
  $id: `https://schemas.platformatic.dev/@platformatic/vite/${packageJson.version}.json`,
9
22
  $schema: 'http://json-schema.org/draft-07/schema#',
@@ -11,13 +24,14 @@ export const schema = {
11
24
  type: 'object',
12
25
  properties: {
13
26
  $schema: {
14
- type: 'string',
27
+ type: 'string'
15
28
  },
16
- server: utilsSchema.server,
17
- watch: schemaComponents.watch,
18
- deploy: schemaComponents.deploy
29
+ server: utilsSchemaComponents.server,
30
+ watch: basicSchemaComponents.watch,
31
+ application: basicSchemaComponents.application,
32
+ node
19
33
  },
20
- additionalProperties: false,
34
+ additionalProperties: false
21
35
  }
22
36
 
23
37
  /* c8 ignore next 3 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/node",
3
- "version": "2.0.0-alpha.7",
3
+ "version": "2.0.0-alpha.9",
4
4
  "description": "Platformatic Node.js Stackable",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -15,25 +15,26 @@
15
15
  },
16
16
  "homepage": "https://github.com/platformatic/platformatic#readme",
17
17
  "dependencies": {
18
- "light-my-request": "^5.13.0",
19
- "@platformatic/basic": "2.0.0-alpha.7",
20
- "@platformatic/utils": "2.0.0-alpha.7",
21
- "@platformatic/config": "2.0.0-alpha.7"
18
+ "light-my-request": "^6.0.0",
19
+ "@platformatic/basic": "2.0.0-alpha.9",
20
+ "@platformatic/utils": "2.0.0-alpha.9",
21
+ "@platformatic/config": "2.0.0-alpha.9"
22
22
  },
23
23
  "devDependencies": {
24
24
  "borp": "^0.17.0",
25
25
  "express": "^4.19.2",
26
26
  "eslint": "9",
27
- "fastify": "^4.28.1",
27
+ "fastify": "5.0.0-alpha.4",
28
28
  "json-schema-to-typescript": "^15.0.1",
29
29
  "neostandard": "^0.11.1",
30
+ "tsx": "^4.19.0",
30
31
  "typescript": "^5.5.4",
31
- "@platformatic/composer": "2.0.0-alpha.7",
32
- "@platformatic/service": "2.0.0-alpha.7"
32
+ "@platformatic/composer": "2.0.0-alpha.9",
33
+ "@platformatic/service": "2.0.0-alpha.9"
33
34
  },
34
35
  "scripts": {
35
- "test": "npm run lint && borp --concurrency=1 --timeout=180000",
36
- "coverage": "npm run lint && borp -C -X test -X test/fixtures --concurrency=1 --timeout=180000",
36
+ "test": "npm run lint && borp --concurrency=1 --no-timeout",
37
+ "coverage": "npm run lint && borp -C -X test -X test/fixtures --concurrency=1 --no-timeout",
37
38
  "gen-schema": "node lib/schema.js > schema.json",
38
39
  "gen-types": "json2ts > config.d.ts < schema.json",
39
40
  "build": "pnpm run gen-schema && pnpm run gen-types",
package/schema.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "$id": "https://schemas.platformatic.dev/@platformatic/vite/2.0.0-alpha.7.json",
2
+ "$id": "https://schemas.platformatic.dev/@platformatic/vite/2.0.0-alpha.9.json",
3
3
  "$schema": "http://json-schema.org/draft-07/schema#",
4
4
  "title": "Platformatic Node.js Stackable",
5
5
  "type": "object",
@@ -161,6 +161,9 @@
161
161
  }
162
162
  ]
163
163
  },
164
+ "loggerInstance": {
165
+ "type": "object"
166
+ },
164
167
  "serializerOpts": {
165
168
  "type": "object",
166
169
  "properties": {
@@ -492,9 +495,16 @@
492
495
  }
493
496
  ]
494
497
  },
495
- "deploy": {
498
+ "application": {
496
499
  "type": "object",
497
500
  "properties": {
501
+ "basePath": {
502
+ "type": "string"
503
+ },
504
+ "outputDirectory": {
505
+ "type": "string",
506
+ "default": "dist"
507
+ },
498
508
  "include": {
499
509
  "type": "array",
500
510
  "items": {
@@ -504,20 +514,39 @@
504
514
  "dist"
505
515
  ]
506
516
  },
507
- "buildCommand": {
508
- "type": "string",
509
- "default": "npm run build"
510
- },
511
- "installCommand": {
512
- "type": "string",
513
- "default": "npm ci --omit-dev"
514
- },
515
- "startCommand": {
516
- "type": "string",
517
- "default": "npm run start"
517
+ "commands": {
518
+ "type": "object",
519
+ "properties": {
520
+ "install": {
521
+ "type": "string",
522
+ "default": "npm ci --omit-dev"
523
+ },
524
+ "build": {
525
+ "type": "string"
526
+ },
527
+ "development": {
528
+ "type": "string"
529
+ },
530
+ "production": {
531
+ "type": "string"
532
+ }
533
+ },
534
+ "default": {},
535
+ "additionalProperties": false
518
536
  }
519
537
  },
538
+ "additionalProperties": false,
520
539
  "default": {}
540
+ },
541
+ "node": {
542
+ "type": "object",
543
+ "properties": {
544
+ "entrypoint": {
545
+ "type": "string"
546
+ }
547
+ },
548
+ "default": {},
549
+ "additionalProperties": false
521
550
  }
522
551
  },
523
552
  "additionalProperties": false
@@ -1,54 +0,0 @@
1
- import { ifError } from 'node:assert'
2
- import { resolve } from 'node:path'
3
- import { test } from 'node:test'
4
- import { createRuntime, setFixturesDir, verifyJSONViaHTTP, verifyJSONViaInject } from '../../basic/test/helper.js'
5
-
6
- const packageRoot = resolve(import.meta.dirname, '..')
7
- setFixturesDir(resolve(import.meta.dirname, './fixtures'))
8
-
9
- test('can detect and start an express application with no build function defined', async t => {
10
- const { runtime, url } = await createRuntime(
11
- t,
12
- 'express/no-build/platformatic.as-entrypoint.runtime.json',
13
- packageRoot
14
- )
15
-
16
- await verifyJSONViaHTTP(url, '/direct', 200, { ok: true })
17
- await verifyJSONViaInject(runtime, 'main', 'GET', '/direct', 200, { ok: true })
18
- })
19
-
20
- test('can detect and start an express application with no build function and when not the entrypoint', async t => {
21
- const { runtime, url } = await createRuntime(
22
- t,
23
- 'express/no-build/platformatic.no-entrypoint.runtime.json',
24
- packageRoot
25
- )
26
-
27
- await verifyJSONViaHTTP(url, '/mesh', 200, { ok: true })
28
- await verifyJSONViaInject(runtime, 'main', 'GET', '/mesh', 200, { ok: true })
29
- })
30
-
31
- test('can detect and start an express application with build function defined', async t => {
32
- const { runtime, url } = await createRuntime(
33
- t,
34
- 'express/with-build/platformatic.as-entrypoint.runtime.json',
35
- packageRoot
36
- )
37
-
38
- await verifyJSONViaHTTP(url, '/direct', 200, { ok: true })
39
- await verifyJSONViaInject(runtime, 'main', 'GET', '/direct', 200, { ok: true })
40
- })
41
-
42
- test('can detect and start an express applicationwith build function defined and when not the entrypoint', async t => {
43
- const { runtime, url } = await createRuntime(
44
- t,
45
- 'express/no-build/platformatic.no-entrypoint.runtime.json',
46
- packageRoot
47
- )
48
-
49
- await verifyJSONViaHTTP(url, '/mesh', 200, { ok: true })
50
- await verifyJSONViaInject(runtime, 'main', 'GET', '/mesh', 200, { ok: true })
51
-
52
- const details = await runtime.getServiceDetails('internal')
53
- ifError(details.url)
54
- })
@@ -1,54 +0,0 @@
1
- import { ifError } from 'node:assert'
2
- import { resolve } from 'node:path'
3
- import { test } from 'node:test'
4
- import { createRuntime, setFixturesDir, verifyJSONViaHTTP, verifyJSONViaInject } from '../../basic/test/helper.js'
5
-
6
- const packageRoot = resolve(import.meta.dirname, '..')
7
- setFixturesDir(resolve(import.meta.dirname, './fixtures'))
8
-
9
- test('can detect and start a fastify application with no build function defined', async t => {
10
- const { runtime, url } = await createRuntime(
11
- t,
12
- 'fastify/no-build/platformatic.as-entrypoint.runtime.json',
13
- packageRoot
14
- )
15
-
16
- await verifyJSONViaHTTP(url, '/direct', 200, { ok: true })
17
- await verifyJSONViaInject(runtime, 'main', 'GET', '/direct', 200, { ok: true })
18
- })
19
-
20
- test('can detect and start a fastify application with no build function and when not the entrypoint', async t => {
21
- const { runtime, url } = await createRuntime(
22
- t,
23
- 'fastify/no-build/platformatic.no-entrypoint.runtime.json',
24
- packageRoot
25
- )
26
-
27
- await verifyJSONViaHTTP(url, '/mesh', 200, { ok: true })
28
- await verifyJSONViaInject(runtime, 'main', 'GET', '/mesh', 200, { ok: true })
29
- })
30
-
31
- test('can detect and start a fastify application with build function defined', async t => {
32
- const { runtime, url } = await createRuntime(
33
- t,
34
- 'fastify/with-build/platformatic.as-entrypoint.runtime.json',
35
- packageRoot
36
- )
37
-
38
- await verifyJSONViaHTTP(url, '/direct', 200, { ok: true })
39
- await verifyJSONViaInject(runtime, 'main', 'GET', '/direct', 200, { ok: true })
40
- })
41
-
42
- test('can detect and start a fastify application with build function defined and when not the entrypoint', async t => {
43
- const { runtime, url } = await createRuntime(
44
- t,
45
- 'fastify/no-build/platformatic.no-entrypoint.runtime.json',
46
- packageRoot
47
- )
48
-
49
- await verifyJSONViaHTTP(url, '/mesh', 200, { ok: true })
50
- await verifyJSONViaInject(runtime, 'main', 'GET', '/mesh', 200, { ok: true })
51
-
52
- const details = await runtime.getServiceDetails('internal')
53
- ifError(details.url)
54
- })
@@ -1,16 +0,0 @@
1
- import express from 'express'
2
-
3
- const app = express()
4
-
5
- app.get('/direct', (req, res) => {
6
- res.send({ ok: true })
7
- })
8
-
9
- app.get('/internal', (req, res) => {
10
- fetch('http://main.plt.local/direct')
11
- .then(response => response.json())
12
- .then(res.json.bind(res))
13
- })
14
-
15
- // This would likely fail if our code doesn't work
16
- app.listen(1)
@@ -1,7 +0,0 @@
1
- {
2
- "main": "index.js",
3
- "type": "module",
4
- "dependencies": {
5
- "express": "^4.19.2"
6
- }
7
- }
@@ -1,3 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/basic/1.52.0.json"
3
- }
@@ -1,19 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/1.52.0.json",
3
- "entrypoint": "main",
4
- "watch": false,
5
- "managementApi": false,
6
- "metrics": false,
7
- "server": {
8
- "logger": {
9
- "level": "error"
10
- }
11
- },
12
- "services": [
13
- {
14
- "id": "main",
15
- "path": ".",
16
- "config": "platformatic.application.json"
17
- }
18
- ]
19
- }
@@ -1,16 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/1.52.0.json",
3
- "entrypoint": "main",
4
- "watch": false,
5
- "managementApi": false,
6
- "metrics": false,
7
- "server": {
8
- "logger": {
9
- "level": "error"
10
- }
11
- },
12
- "services": [
13
- { "id": "main", "path": "../../platformatic-service", "config": "platformatic.service.json" },
14
- { "id": "internal", "path": ".", "config": "platformatic.application.json" }
15
- ]
16
- }
@@ -1,17 +0,0 @@
1
- import express from 'express'
2
-
3
- export function build () {
4
- const app = express()
5
-
6
- app.get('/direct', (req, res) => {
7
- res.send({ ok: true })
8
- })
9
-
10
- app.get('/internal', (req, res) => {
11
- fetch('http://main.plt.local/direct')
12
- .then(response => response.json())
13
- .then(res.json.bind(res))
14
- })
15
-
16
- return app
17
- }
@@ -1,7 +0,0 @@
1
- {
2
- "main": "index.js",
3
- "type": "module",
4
- "dependencies": {
5
- "express": "^4.19.2"
6
- }
7
- }
@@ -1,3 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/basic/1.52.0.json"
3
- }
@@ -1,19 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/1.52.0.json",
3
- "entrypoint": "main",
4
- "watch": false,
5
- "managementApi": false,
6
- "metrics": false,
7
- "server": {
8
- "logger": {
9
- "level": "error"
10
- }
11
- },
12
- "services": [
13
- {
14
- "id": "main",
15
- "path": ".",
16
- "config": "platformatic.application.json"
17
- }
18
- ]
19
- }
@@ -1,16 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/1.52.0.json",
3
- "entrypoint": "main",
4
- "watch": false,
5
- "managementApi": false,
6
- "metrics": false,
7
- "server": {
8
- "logger": {
9
- "level": "error"
10
- }
11
- },
12
- "services": [
13
- { "id": "main", "path": "../../platformatic-service", "config": "platformatic.service.json" },
14
- { "id": "internal", "path": ".", "config": "platformatic.application.json" }
15
- ]
16
- }
@@ -1,14 +0,0 @@
1
- import fastify from 'fastify'
2
-
3
- const app = fastify()
4
-
5
- app.get('/direct', async () => {
6
- return { ok: true }
7
- })
8
-
9
- app.get('/internal', () => {
10
- return fetch('http://main.plt.local/direct').then(response => response.json())
11
- })
12
-
13
- // This would likely fail if our code doesn't work
14
- app.listen({ port: 1 })
@@ -1,7 +0,0 @@
1
- {
2
- "main": "index.js",
3
- "type": "module",
4
- "dependencies": {
5
- "fastify": "^4.28.1"
6
- }
7
- }
@@ -1,3 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/basic/1.52.0.json"
3
- }
@@ -1,19 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/1.52.0.json",
3
- "entrypoint": "main",
4
- "watch": false,
5
- "managementApi": false,
6
- "metrics": false,
7
- "server": {
8
- "logger": {
9
- "level": "error"
10
- }
11
- },
12
- "services": [
13
- {
14
- "id": "main",
15
- "path": ".",
16
- "config": "platformatic.application.json"
17
- }
18
- ]
19
- }
@@ -1,16 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/1.52.0.json",
3
- "entrypoint": "main",
4
- "watch": false,
5
- "managementApi": false,
6
- "metrics": false,
7
- "server": {
8
- "logger": {
9
- "level": "error"
10
- }
11
- },
12
- "services": [
13
- { "id": "main", "path": "../../platformatic-service", "config": "platformatic.service.json" },
14
- { "id": "internal", "path": ".", "config": "platformatic.application.json" }
15
- ]
16
- }
@@ -1,15 +0,0 @@
1
- import fastify from 'fastify'
2
-
3
- export function build () {
4
- const app = fastify()
5
-
6
- app.get('/direct', async () => {
7
- return { ok: true }
8
- })
9
-
10
- app.get('/internal', () => {
11
- return fetch('http://main.plt.local/direct').then(response => response.json())
12
- })
13
-
14
- return app
15
- }
@@ -1,7 +0,0 @@
1
- {
2
- "main": "index.js",
3
- "type": "module",
4
- "dependencies": {
5
- "fastify": "^4.28.1"
6
- }
7
- }
@@ -1,3 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/basic/1.52.0.json"
3
- }
@@ -1,19 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/1.52.0.json",
3
- "entrypoint": "main",
4
- "watch": false,
5
- "managementApi": false,
6
- "metrics": false,
7
- "server": {
8
- "logger": {
9
- "level": "error"
10
- }
11
- },
12
- "services": [
13
- {
14
- "id": "main",
15
- "path": ".",
16
- "config": "platformatic.application.json"
17
- }
18
- ]
19
- }
@@ -1,16 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/1.52.0.json",
3
- "entrypoint": "main",
4
- "watch": false,
5
- "managementApi": false,
6
- "metrics": false,
7
- "server": {
8
- "logger": {
9
- "level": "error"
10
- }
11
- },
12
- "services": [
13
- { "id": "main", "path": "../../platformatic-service", "config": "platformatic.service.json" },
14
- { "id": "internal", "path": ".", "config": "platformatic.application.json" }
15
- ]
16
- }
@@ -1,21 +0,0 @@
1
- import { createServer } from 'node:http'
2
-
3
- const server = createServer((req, res) => {
4
- if (req.url === '/direct') {
5
- res.writeHead(200, { 'content-type': 'application/json', connection: 'close' })
6
- res.end(JSON.stringify({ ok: true }))
7
- } else if (req.url === '/mesh') {
8
- fetch('http://main.plt.local/direct')
9
- .then(response => response.json())
10
- .then(json => {
11
- res.writeHead(200, { 'content-type': 'application/json', connection: 'close' })
12
- res.end(JSON.stringify({ ok: true }))
13
- })
14
- } else {
15
- res.writeHead(404, { 'content-type': 'application/json', connection: 'close' })
16
- res.end(JSON.stringify({ ok: false }))
17
- }
18
- })
19
-
20
- // This would likely fail if our code doesn't work
21
- server.listen(1)
@@ -1,7 +0,0 @@
1
- {
2
- "main": "index.js",
3
- "type": "module",
4
- "dependencies": {
5
- "express": "^4.19.2"
6
- }
7
- }
@@ -1,3 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/basic/1.52.0.json"
3
- }
@@ -1,19 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/1.52.0.json",
3
- "entrypoint": "main",
4
- "watch": false,
5
- "managementApi": true,
6
- "metrics": false,
7
- "server": {
8
- "logger": {
9
- "level": "warn"
10
- }
11
- },
12
- "services": [
13
- {
14
- "id": "main",
15
- "path": ".",
16
- "config": "platformatic.application.json"
17
- }
18
- ]
19
- }
@@ -1,16 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/1.52.0.json",
3
- "entrypoint": "main",
4
- "watch": false,
5
- "managementApi": true,
6
- "metrics": false,
7
- "server": {
8
- "logger": {
9
- "level": "warn"
10
- }
11
- },
12
- "services": [
13
- { "id": "main", "path": "../../platformatic-service", "config": "platformatic.service.json" },
14
- { "id": "internal", "path": ".", "config": "platformatic.application.json" }
15
- ]
16
- }
@@ -1,21 +0,0 @@
1
- import { createServer } from 'node:http'
2
-
3
- const server = createServer((req, res) => {
4
- if (req.url === '/direct') {
5
- res.writeHead(200, { 'content-type': 'application/json', connection: 'close' })
6
- res.end(JSON.stringify({ ok: true }))
7
- } else if (req.url === '/mesh') {
8
- fetch('http://main.plt.local/direct')
9
- .then(response => response.json())
10
- .then(json => {
11
- res.writeHead(200, { 'content-type': 'application/json', connection: 'close' })
12
- res.end(JSON.stringify({ ok: true }))
13
- })
14
- } else {
15
- res.writeHead(404, { 'content-type': 'application/json', connection: 'close' })
16
- res.end(JSON.stringify({ ok: false }))
17
- }
18
- })
19
-
20
- // This would likely fail if our code doesn't work
21
- server.listen(1)
@@ -1,18 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/1.52.0.json",
3
- "entrypoint": "main",
4
- "watch": false,
5
- "managementApi": true,
6
- "metrics": false,
7
- "server": {
8
- "logger": {
9
- "level": "warn"
10
- }
11
- },
12
- "services": [
13
- {
14
- "id": "main",
15
- "path": "."
16
- }
17
- ]
18
- }
@@ -1,16 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/1.52.0.json",
3
- "entrypoint": "main",
4
- "watch": false,
5
- "managementApi": true,
6
- "metrics": false,
7
- "server": {
8
- "logger": {
9
- "level": "warn"
10
- }
11
- },
12
- "services": [
13
- { "id": "main", "path": "../../platformatic-service", "config": "platformatic.service.json" },
14
- { "id": "internal", "path": "." }
15
- ]
16
- }
@@ -1,20 +0,0 @@
1
- import { createServer } from 'node:http'
2
-
3
- export function build () {
4
- return createServer((req, res) => {
5
- if (req.url === '/direct') {
6
- res.writeHead(200, { 'content-type': 'application/json', connection: 'close' })
7
- res.end(JSON.stringify({ ok: true }))
8
- } else if (req.url === '/mesh') {
9
- fetch('http://main.plt.local/direct')
10
- .then(response => response.json())
11
- .then(json => {
12
- res.writeHead(200, { 'content-type': 'application/json', connection: 'close' })
13
- res.end(JSON.stringify({ ok: true }))
14
- })
15
- } else {
16
- res.writeHead(404, { 'content-type': 'application/json', connection: 'close' })
17
- res.end(JSON.stringify({ ok: false }))
18
- }
19
- })
20
- }
@@ -1,7 +0,0 @@
1
- {
2
- "main": "index.js",
3
- "type": "module",
4
- "dependencies": {
5
- "express": "^4.19.2"
6
- }
7
- }
@@ -1,3 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/basic/1.52.0.json"
3
- }
@@ -1,19 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/1.52.0.json",
3
- "entrypoint": "main",
4
- "watch": false,
5
- "managementApi": true,
6
- "metrics": false,
7
- "server": {
8
- "logger": {
9
- "level": "warn"
10
- }
11
- },
12
- "services": [
13
- {
14
- "id": "main",
15
- "path": ".",
16
- "config": "platformatic.application.json"
17
- }
18
- ]
19
- }
@@ -1,16 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/1.52.0.json",
3
- "entrypoint": "main",
4
- "watch": false,
5
- "managementApi": true,
6
- "metrics": false,
7
- "server": {
8
- "logger": {
9
- "level": "warn"
10
- }
11
- },
12
- "services": [
13
- { "id": "main", "path": "../../platformatic-service", "config": "platformatic.service.json" },
14
- { "id": "internal", "path": ".", "config": "platformatic.application.json" }
15
- ]
16
- }
@@ -1,15 +0,0 @@
1
- {
2
- "$schema": "https://schemas.platformatic.dev/@platformatic/service/1.52.0.json",
3
- "server": {
4
- "logger": {
5
- "level": "error"
6
- }
7
- },
8
- "plugins": {
9
- "paths": [
10
- {
11
- "path": "./plugin.js"
12
- }
13
- ]
14
- }
15
- }
@@ -1,12 +0,0 @@
1
- 'use strict'
2
-
3
- export default async function (app) {
4
- app.get('/mesh', async () => {
5
- const response = await fetch('http://internal.plt.local/direct')
6
- return response.json()
7
- })
8
-
9
- app.get('/direct', async () => {
10
- return { ok: true }
11
- })
12
- }
package/test/node.test.js DELETED
@@ -1,97 +0,0 @@
1
- import { deepStrictEqual, ifError, ok } from 'node:assert'
2
- import { resolve } from 'node:path'
3
- import { test } from 'node:test'
4
- import {
5
- createRuntime,
6
- getLogs,
7
- setFixturesDir,
8
- verifyJSONViaHTTP,
9
- verifyJSONViaInject
10
- } from '../../basic/test/helper.js'
11
-
12
- const packageRoot = resolve(import.meta.dirname, '..')
13
- setFixturesDir(resolve(import.meta.dirname, './fixtures'))
14
-
15
- test('can detect and start a Node.js application with no configuration files', async t => {
16
- const { runtime, url } = await createRuntime(
17
- t,
18
- 'nodejs/no-configuration/platformatic.as-entrypoint.runtime.json',
19
- packageRoot
20
- )
21
-
22
- await verifyJSONViaHTTP(url, '/direct', 200, { ok: true })
23
- await verifyJSONViaInject(runtime, 'main', 'GET', '/direct', 200, { ok: true })
24
-
25
- const missingConfigurationMessage =
26
- 'The service main had no valid entrypoint defined in the package.json file. Falling back to the file index.js.'
27
-
28
- const logs = await getLogs(runtime)
29
- ok(logs.map(m => m.msg).includes(missingConfigurationMessage))
30
- })
31
-
32
- test('can detect and start a Node.js application with no configuration files and when not the entrypoint', async t => {
33
- const { runtime, url } = await createRuntime(
34
- t,
35
- 'nodejs/no-configuration/platformatic.no-entrypoint.runtime.json',
36
- packageRoot
37
- )
38
-
39
- await verifyJSONViaHTTP(url, '/mesh', 200, { ok: true })
40
- await verifyJSONViaInject(runtime, 'main', 'GET', '/mesh', 200, { ok: true })
41
-
42
- const missingConfigurationMessage =
43
- 'The service internal had no valid entrypoint defined in the package.json file. Falling back to the file index.js.'
44
-
45
- const logs = await getLogs(runtime)
46
- ok(logs.map(m => m.msg).includes(missingConfigurationMessage))
47
- })
48
-
49
- test('can detect and start a Node.js application with no build function defined', async t => {
50
- const { runtime, url } = await createRuntime(
51
- t,
52
- 'nodejs/no-build/platformatic.as-entrypoint.runtime.json',
53
- packageRoot
54
- )
55
-
56
- await verifyJSONViaHTTP(url, '/direct', 200, { ok: true })
57
- await verifyJSONViaInject(runtime, 'main', 'GET', '/direct', 200, { ok: true })
58
-
59
- const logs = await getLogs(runtime)
60
- deepStrictEqual(logs, [])
61
- })
62
-
63
- test('can detect and start a Node.js application with no build function and when not the entrypoint', async t => {
64
- const { runtime, url } = await createRuntime(
65
- t,
66
- 'nodejs/no-build/platformatic.no-entrypoint.runtime.json',
67
- packageRoot
68
- )
69
-
70
- await verifyJSONViaHTTP(url, '/mesh', 200, { ok: true })
71
- await verifyJSONViaInject(runtime, 'main', 'GET', '/mesh', 200, { ok: true })
72
- })
73
-
74
- test('can detect and start a Node.js application with build function defined', async t => {
75
- const { runtime, url } = await createRuntime(
76
- t,
77
- 'nodejs/with-build/platformatic.as-entrypoint.runtime.json',
78
- packageRoot
79
- )
80
-
81
- await verifyJSONViaHTTP(url, '/direct', 200, { ok: true })
82
- await verifyJSONViaInject(runtime, 'main', 'GET', '/direct', 200, { ok: true })
83
- })
84
-
85
- test('can detect and start a Node.js application with build function defined and when not the entrypoint', async t => {
86
- const { runtime, url } = await createRuntime(
87
- t,
88
- 'nodejs/no-build/platformatic.no-entrypoint.runtime.json',
89
- packageRoot
90
- )
91
-
92
- await verifyJSONViaHTTP(url, '/mesh', 200, { ok: true })
93
- await verifyJSONViaInject(runtime, 'main', 'GET', '/mesh', 200, { ok: true })
94
-
95
- const details = await runtime.getServiceDetails('internal')
96
- ifError(details.url)
97
- })