@platformatic/service 0.26.1 → 0.28.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.
Files changed (37) hide show
  1. package/fixtures/hello/platformatic.service.json +1 -2
  2. package/fixtures/hello/warn-log.service.json +1 -2
  3. package/fixtures/hello-client/platformatic.service.json +2 -3
  4. package/fixtures/hello-client-ts/platformatic.service.json +2 -3
  5. package/index.d.ts +17 -11
  6. package/index.js +4 -21
  7. package/index.test-d.ts +14 -22
  8. package/lib/compile.js +14 -38
  9. package/lib/gen-schema.js +1 -0
  10. package/lib/load-config.js +1 -2
  11. package/lib/plugins/openapi.js +1 -0
  12. package/lib/plugins/plugins.js +1 -19
  13. package/lib/plugins/typescript.js +1 -31
  14. package/lib/root-endpoint/public/background_frame.svg +614 -0
  15. package/lib/root-endpoint/public/background_polygon_14.svg +3 -0
  16. package/lib/root-endpoint/public/background_polygon_28.svg +3 -0
  17. package/lib/root-endpoint/public/dark_mode.svg +3 -0
  18. package/lib/root-endpoint/public/index.html +174 -35
  19. package/lib/root-endpoint/public/light_mode.svg +11 -0
  20. package/lib/root-endpoint/public/platformatic-logo-dark.svg +8 -0
  21. package/lib/root-endpoint/public/platformatic-logo-light.svg +8 -0
  22. package/lib/schema.js +3 -10
  23. package/lib/start.js +2 -26
  24. package/lib/utils.js +8 -0
  25. package/package.json +28 -20
  26. package/test/autoload.test.js +0 -182
  27. package/test/cli/compile.test.mjs +1 -1
  28. package/test/cli/helper.mjs +3 -2
  29. package/test/fixtures/bad-typescript-plugin/platformatic.service.json +1 -1
  30. package/test/fixtures/nested-directories/modules/inventory/routes/product.js +0 -1
  31. package/test/load-and-reload-files.test.js +7 -278
  32. package/test/utils.test.js +7 -0
  33. package/tsconfig.json +12 -0
  34. package/lib/plugins/file-watcher.js +0 -44
  35. package/lib/root-endpoint/public/logo-512x512.png +0 -0
  36. package/test/cli/watch.test.mjs +0 -289
  37. package/test/watch.test.js +0 -248
@@ -47,99 +47,6 @@ test('autoload & filesystem based routing / watch disabled', async ({ teardown,
47
47
  }
48
48
  })
49
49
 
50
- test('autoload & filesystem based routing / watch enabled', async ({ teardown, equal }) => {
51
- const config = {
52
- server: {
53
- hostname: '127.0.0.1',
54
- port: 0
55
- },
56
- plugins: {
57
- paths: [join(__dirname, 'fixtures', 'directories', 'routes')]
58
- },
59
- watch: true,
60
- metrics: false
61
- }
62
-
63
- const app = await buildServer(config)
64
- teardown(async () => {
65
- await app.close()
66
- })
67
- await app.start()
68
-
69
- {
70
- const res = await request(`${app.url}/`)
71
- equal(res.statusCode, 200, 'status code')
72
- const body = await res.body.json()
73
- equal(body.hello, 'from root', 'body')
74
- }
75
-
76
- {
77
- const res = await request(`${app.url}/foo/bar`)
78
- equal(res.statusCode, 200, 'status code')
79
- const body = await res.body.json()
80
- equal(body.hello, 'from bar', 'body')
81
- }
82
-
83
- {
84
- const res = await request(`${app.url}/foo/baz`)
85
- equal(res.statusCode, 200, 'status code')
86
- const body = await res.body.json()
87
- equal(body.hello, 'from baz', 'body')
88
- }
89
- })
90
-
91
- test('multiple files', async ({ teardown, equal }) => {
92
- const config = {
93
- server: {
94
- hostname: '127.0.0.1',
95
- port: 0
96
- },
97
- plugins: {
98
- paths: [{
99
- path: join(__dirname, 'fixtures', 'directories', 'plugins')
100
- }, {
101
- path: join(__dirname, 'fixtures', 'directories', 'routes')
102
- }]
103
- },
104
- watch: true,
105
- metrics: false
106
- }
107
-
108
- const app = await buildServer(config)
109
- teardown(async () => {
110
- await app.close()
111
- })
112
- await app.start()
113
-
114
- {
115
- const res = await request(`${app.url}/`)
116
- equal(res.statusCode, 200, 'status code')
117
- const body = await res.body.json()
118
- equal(body.hello, 'from root', 'body')
119
- }
120
-
121
- {
122
- const res = await request(`${app.url}/foo/bar`)
123
- equal(res.statusCode, 200, 'status code')
124
- const body = await res.body.json()
125
- equal(body.hello, 'from bar', 'body')
126
- }
127
-
128
- {
129
- const res = await request(`${app.url}/foo/baz`)
130
- equal(res.statusCode, 200, 'status code')
131
- const body = await res.body.json()
132
- equal(body.hello, 'from baz', 'body')
133
- }
134
-
135
- {
136
- const res = await request(`${app.url}/foo/with-decorator`)
137
- equal(res.statusCode, 200, 'status code')
138
- const body = await res.body.json()
139
- equal(body.hello, 'bar', 'body')
140
- }
141
- })
142
-
143
50
  test('multiple files / watch false', async ({ teardown, equal }) => {
144
51
  const config = {
145
52
  server: {
@@ -233,95 +140,6 @@ test('autoload & filesystem based routing / watch disabled / no object', async (
233
140
  }
234
141
  })
235
142
 
236
- test('autoload & filesystem based routing / watch enabled / no object', async ({ teardown, equal }) => {
237
- const config = {
238
- server: {
239
- hostname: '127.0.0.1',
240
- port: 0
241
- },
242
- plugins: {
243
- paths: [join(__dirname, 'fixtures', 'directories', 'routes')]
244
- },
245
- watch: true,
246
- metrics: false
247
- }
248
-
249
- const app = await buildServer(config)
250
- teardown(async () => {
251
- await app.close()
252
- })
253
- await app.start()
254
-
255
- {
256
- const res = await request(`${app.url}/`)
257
- equal(res.statusCode, 200, 'status code')
258
- const body = await res.body.json()
259
- equal(body.hello, 'from root', 'body')
260
- }
261
-
262
- {
263
- const res = await request(`${app.url}/foo/bar`)
264
- equal(res.statusCode, 200, 'status code')
265
- const body = await res.body.json()
266
- equal(body.hello, 'from bar', 'body')
267
- }
268
-
269
- {
270
- const res = await request(`${app.url}/foo/baz`)
271
- equal(res.statusCode, 200, 'status code')
272
- const body = await res.body.json()
273
- equal(body.hello, 'from baz', 'body')
274
- }
275
- })
276
-
277
- test('multiple files / no object', async ({ teardown, equal }) => {
278
- const config = {
279
- server: {
280
- hostname: '127.0.0.1',
281
- port: 0
282
- },
283
- plugins: {
284
- paths: [join(__dirname, 'fixtures', 'directories', 'plugins'), join(__dirname, 'fixtures', 'directories', 'routes')]
285
- },
286
- watch: true,
287
- metrics: false
288
- }
289
-
290
- const app = await buildServer(config)
291
- teardown(async () => {
292
- await app.close()
293
- })
294
- await app.start()
295
-
296
- {
297
- const res = await request(`${app.url}/`)
298
- equal(res.statusCode, 200, 'status code')
299
- const body = await res.body.json()
300
- equal(body.hello, 'from root', 'body')
301
- }
302
-
303
- {
304
- const res = await request(`${app.url}/foo/bar`)
305
- equal(res.statusCode, 200, 'status code')
306
- const body = await res.body.json()
307
- equal(body.hello, 'from bar', 'body')
308
- }
309
-
310
- {
311
- const res = await request(`${app.url}/foo/baz`)
312
- equal(res.statusCode, 200, 'status code')
313
- const body = await res.body.json()
314
- equal(body.hello, 'from baz', 'body')
315
- }
316
-
317
- {
318
- const res = await request(`${app.url}/foo/with-decorator`)
319
- equal(res.statusCode, 200, 'status code')
320
- const body = await res.body.json()
321
- equal(body.hello, 'bar', 'body')
322
- }
323
- })
324
-
325
143
  test('multiple files / watch false / no object', async ({ teardown, equal }) => {
326
144
  const config = {
327
145
  server: {
@@ -164,7 +164,7 @@ t.test('start command should not compile typescript plugin with errors', async (
164
164
  await childProcess
165
165
  t.fail('should not compile bad typescript plugin')
166
166
  } catch (err) {
167
- if (!err.stderr.includes('Found 1 error')) {
167
+ if (!err.stdout.includes('Found 1 error')) {
168
168
  t.comment(err.stdout)
169
169
  t.comment(err.stderr)
170
170
  t.fail('should throw one ts error')
@@ -41,8 +41,9 @@ export async function start (commandOpts, exacaOpts = {}) {
41
41
 
42
42
  for await (const messages of on(output, 'data')) {
43
43
  for (const message of messages) {
44
- const url = message.url
45
- if (url !== undefined) {
44
+ const text = message.msg
45
+ if (text && text.includes('Server listening at')) {
46
+ const url = text.match(/Server listening at (.*)/)[1]
46
47
  clearTimeout(errorTimeout)
47
48
  return { child, url, output }
48
49
  }
@@ -12,5 +12,5 @@
12
12
  "paths": ["plugin.ts"],
13
13
  "typescript": true
14
14
  },
15
- "watch": true
15
+ "watch": false
16
16
  }
@@ -1,4 +1,3 @@
1
-
2
1
  export default async function (fastify, opts) {
3
2
  fastify.get('/product/:sku', {
4
3
  schema: {
@@ -4,12 +4,13 @@ require('./helper')
4
4
  const { test } = require('tap')
5
5
  const { buildServer } = require('..')
6
6
  const { request, setGlobalDispatcher, getGlobalDispatcher, MockAgent } = require('undici')
7
+ const { randomUUID } = require('crypto')
7
8
  const { join } = require('path')
8
9
  const os = require('os')
9
10
  const { writeFile } = require('fs/promises')
10
11
 
11
12
  test('load and reload', async ({ teardown, equal, pass, same }) => {
12
- const file = join(os.tmpdir(), `some-plugin-${process.pid}.js`)
13
+ const file = join(os.tmpdir(), `some-plugin-${randomUUID()}.js`)
13
14
 
14
15
  await writeFile(file, `
15
16
  module.exports = async function (app) {
@@ -50,12 +51,13 @@ test('load and reload', async ({ teardown, equal, pass, same }) => {
50
51
  {
51
52
  const res = await request(`${app.url}/`)
52
53
  equal(res.statusCode, 200, 'add status code')
53
- same(await res.body.text(), 'hello world', 'response')
54
+ // The plugin is in Node's module cache, so the new value is not seen.
55
+ same(await res.body.json(), { message: 'Welcome to Platformatic! Please visit https://oss.platformatic.dev' })
54
56
  }
55
57
  })
56
58
 
57
59
  test('error', async ({ teardown, equal, pass, match }) => {
58
- const file = join(os.tmpdir(), `some-plugin-${process.pid}.js`)
60
+ const file = join(os.tmpdir(), `some-plugin-${randomUUID()}.js`)
59
61
 
60
62
  await writeFile(file, `
61
63
  module.exports = async function (app) {
@@ -132,52 +134,6 @@ test('mock undici is supported', async ({ teardown, equal, pass, same }) => {
132
134
  })
133
135
  })
134
136
 
135
- test('load and reload with the fallback', async ({ teardown, equal, pass, same }) => {
136
- const file = join(os.tmpdir(), `some-plugin-${process.pid}.js`)
137
-
138
- await writeFile(file, `
139
- module.exports = async function (app) {
140
- }`
141
- )
142
-
143
- const app = await buildServer({
144
- server: {
145
- hostname: '127.0.0.1',
146
- port: 0
147
- },
148
- plugins: {
149
- paths: [file],
150
- stopTimeout: 1000,
151
- fallback: true
152
- }
153
- })
154
-
155
- teardown(async () => {
156
- await app.close()
157
- })
158
- await app.start()
159
-
160
- {
161
- const res = await request(`${app.url}/`)
162
- equal(res.statusCode, 200, 'status code')
163
- const data = await res.body.json()
164
- same(data, { message: 'Welcome to Platformatic! Please visit https://oss.platformatic.dev' })
165
- }
166
-
167
- await writeFile(file, `
168
- module.exports = async function (app) {
169
- app.get('/', () => "hello world" )
170
- }`)
171
-
172
- await app.restart()
173
-
174
- {
175
- const res = await request(`${app.url}/`)
176
- equal(res.statusCode, 200, 'add status code')
177
- same(await res.body.text(), 'hello world', 'response')
178
- }
179
- })
180
-
181
137
  test('load and reload ESM', async ({ teardown, equal, pass, same }) => {
182
138
  const file = join(os.tmpdir(), `some-plugin-${process.pid}.mjs`)
183
139
 
@@ -218,234 +174,7 @@ test('load and reload ESM', async ({ teardown, equal, pass, same }) => {
218
174
  {
219
175
  const res = await request(`${app.url}/`)
220
176
  equal(res.statusCode, 200, 'add status code')
221
- same(await res.body.text(), 'hello world', 'response')
222
- }
223
- })
224
-
225
- test('server should be available after reload a compromised plugin', async ({ teardown, equal, pass, same, rejects }) => {
226
- const file = join(os.tmpdir(), `some-plugin-${process.pid}.js`)
227
-
228
- const workingModule = `
229
- module.exports = async function (app) {
230
- (() => { /* console.log('loaded') */ })()
231
- }`
232
- const compromisedModule = '//console.log(\'loaded but server fails\')'
233
- await writeFile(file, workingModule)
234
-
235
- const config = {
236
- server: {
237
- hostname: '127.0.0.1',
238
- port: 0
239
- },
240
- plugins: {
241
- paths: [file]
242
- }
243
- }
244
-
245
- const app = await buildServer(config)
246
-
247
- teardown(async () => {
248
- await app.close()
249
- })
250
- await app.start()
251
-
252
- await writeFile(file, compromisedModule)
253
- await app.restart().catch(() => {
254
- pass('plugin reload failed')
255
- })
256
-
257
- {
258
- const res = await request(`${app.url}/`, { method: 'GET' })
259
- equal(res.statusCode, 200, 'status code')
260
- const data = await res.body.json()
261
- same(data, { message: 'Welcome to Platformatic! Please visit https://oss.platformatic.dev' })
262
- }
263
-
264
- await writeFile(file, workingModule)
265
- await app.restart()
266
-
267
- {
268
- const res = await request(`${app.url}/`, { method: 'GET' })
269
- equal(res.statusCode, 200, 'add status code')
270
- const data = await res.body.json()
271
- same(data, { message: 'Welcome to Platformatic! Please visit https://oss.platformatic.dev' })
272
- }
273
- })
274
-
275
- test('hot reload disabled, CommonJS', async ({ teardown, equal, pass, same }) => {
276
- const file = join(os.tmpdir(), `some-plugin-hot-rel-test-${process.pid}.js`)
277
-
278
- await writeFile(file, `
279
- module.exports = async function plugin (app) {
280
- app.get('/test', {}, async function (request, response) {
281
- return { res: "plugin, version 1"}
282
- })
283
- }`
284
- )
285
-
286
- const app = await buildServer({
287
- server: {
288
- hostname: '127.0.0.1',
289
- port: 0
290
- },
291
- plugins: {
292
- paths: [file],
293
- hotReload: false
294
- }
295
- })
296
-
297
- teardown(async () => {
298
- await app.close()
299
- })
300
- await app.start()
301
-
302
- {
303
- const res = await request(`${app.url}/test`, {
304
- method: 'GET'
305
- })
306
- equal(res.statusCode, 200)
307
- same(await res.body.json(), { res: 'plugin, version 1' }, 'get rest plugin')
308
- }
309
-
310
- await writeFile(file, `
311
- module.exports = async function plugin (app) {
312
- app.get('/test', {}, async function (request, response) {
313
- return { res: "plugin, version 2"}
314
- })
315
- }`
316
- )
317
-
318
- await app.restart()
319
-
320
- {
321
- const res = await request(`${app.url}/test`, {
322
- method: 'GET'
323
- })
324
- equal(res.statusCode, 200)
325
- // must be unchanged
326
- same(await res.body.json(), { res: 'plugin, version 1' }, 'get rest plugin')
327
- }
328
- })
329
-
330
- test('hot reload disabled, ESM', async ({ teardown, equal, pass, same }) => {
331
- const pathToPlugin = join(os.tmpdir(), `some-plugin-hot-rel-test-${process.pid}.mjs`)
332
- const pathToConfig = join(os.tmpdir(), `platformatic.service.${process.pid}.json`)
333
-
334
- await writeFile(pathToPlugin, `
335
- export default async function (app) {
336
- app.get('/test', {}, async function (request, response) {
337
- return { res: "plugin, version 1"}
338
- })
339
- }`
340
- )
341
-
342
- const config = {
343
- server: {
344
- hostname: '127.0.0.1',
345
- port: 0
346
- },
347
- plugins: {
348
- paths: [pathToPlugin],
349
- stopTimeout: 1000,
350
- hotReload: false
351
- },
352
- watch: true,
353
- metrics: false
354
- }
355
- await writeFile(pathToConfig, JSON.stringify(config, null, 2))
356
- const app = await buildServer(pathToConfig)
357
-
358
- teardown(async () => {
359
- await app.close()
360
- })
361
- await app.start()
362
-
363
- {
364
- const res = await request(`${app.url}/test`, {
365
- method: 'GET'
366
- })
367
- equal(res.statusCode, 200)
368
- same(await res.body.json(), { res: 'plugin, version 1' }, 'get rest plugin')
369
- }
370
-
371
- await writeFile(pathToPlugin, `
372
- export default async function (app) {
373
- app.get('/test', {}, async function (request, response) {
374
- return { res: "plugin, version 2"}
375
- })
376
- }`
377
- )
378
-
379
- await app.restart()
380
-
381
- {
382
- const res = await request(`${app.url}/test`, {
383
- method: 'GET'
384
- })
385
- equal(res.statusCode, 200)
386
- // must be unchanged
387
- same(await res.body.json(), { res: 'plugin, version 1' }, 'get rest plugin')
388
- }
389
- })
390
-
391
- test('hot reload disabled, with default export', async ({ teardown, equal, pass, same }) => {
392
- const pathToPlugin = join(os.tmpdir(), `some-plugin-hot-rel-test-${process.pid}.js`)
393
- const pathToConfig = join(os.tmpdir(), `platformatic.service.${process.pid}.json`)
394
-
395
- await writeFile(pathToPlugin, `
396
- Object.defineProperty(exports, "__esModule", { value: true })
397
- exports.default = async function plugin (app) {
398
- app.get('/test', {}, async function (request, response) {
399
- return { res: "plugin, version 1"}
400
- })
401
- }`)
402
-
403
- const config = {
404
- server: {
405
- hostname: '127.0.0.1',
406
- port: 0
407
- },
408
- plugins: {
409
- paths: [pathToPlugin],
410
- stopTimeout: 1000,
411
- hotReload: false
412
- },
413
- watch: true,
414
- metrics: false
415
- }
416
-
417
- await writeFile(pathToConfig, JSON.stringify(config, null, 2))
418
- const app = await buildServer(pathToConfig)
419
-
420
- teardown(async () => {
421
- await app.close()
422
- })
423
- await app.start()
424
-
425
- {
426
- const res = await request(`${app.url}/test`, {
427
- method: 'GET'
428
- })
429
- same(await res.body.json(), { res: 'plugin, version 1' }, 'get rest plugin')
430
- }
431
-
432
- await writeFile(pathToPlugin, `
433
- Object.defineProperty(exports, "__esModule", { value: true })
434
- exports.default = async function plugin (app) {
435
- app.get('/test', {}, async function (request, response) {
436
- return { res: "plugin, version 2"}
437
- })
438
- }`
439
- )
440
-
441
- await app.restart()
442
-
443
- {
444
- const res = await request(`${app.url}/test`, {
445
- method: 'GET'
446
- })
447
- equal(res.statusCode, 200)
448
- // must be unchanged
449
- same(await res.body.json(), { res: 'plugin, version 1' }, 'get rest plugin')
177
+ // The plugin is in Node's module cache, so the new value is not seen.
178
+ same(await res.body.json(), { message: 'Welcome to Platformatic! Please visit https://oss.platformatic.dev' })
450
179
  }
451
180
  })
@@ -28,3 +28,10 @@ test('isFileAccessible no dir', async (t) => {
28
28
  const file = resolve(join(__dirname, '..', 'fixtures', 'hello', 'platformatic.service.json'))
29
29
  t.equal(await isFileAccessible(file), true)
30
30
  })
31
+
32
+ test('should return the same plugin folder if it\'s already the compiled one', (t) => {
33
+ t.plan(1)
34
+
35
+ const result = getJSPluginPath('/something', '/something/dist/plugins', '/something/dist')
36
+ t.equal(result, '/something/dist/plugins')
37
+ })
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "CommonJS",
4
+ "moduleResolution": "node",
5
+ "esModuleInterop": true,
6
+ "allowSyntheticDefaultImports": true,
7
+ "target": "es2019",
8
+ "outDir": "build",
9
+ "sourceMap": true,
10
+ "strictNullChecks": true
11
+ }
12
+ }
@@ -1,44 +0,0 @@
1
- 'use strict'
2
-
3
- const { FileWatcher } = require('@platformatic/utils')
4
- const fp = require('fastify-plugin')
5
-
6
- async function setupFileWatcher (app, opts) {
7
- // TODO: move params to opts
8
-
9
- const configManager = app.platformatic.configManager
10
- const config = configManager.current
11
-
12
- const isRestartableApp = app.restarted !== undefined
13
-
14
- // to run the plugin without restartable
15
- /* c8 ignore next 1 */
16
- const persistentRef = isRestartableApp ? app.persistentRef : app
17
-
18
- let fileWatcher = persistentRef.fileWatcher
19
- if (!fileWatcher) {
20
- fileWatcher = new FileWatcher({
21
- path: configManager.dirname,
22
- allowToWatch: config.watch?.allow,
23
- watchIgnore: config.watch?.ignore
24
- })
25
-
26
- fileWatcher.on('update', () => {
27
- opts.onFilesUpdated(persistentRef)
28
- })
29
-
30
- app.log.debug('start watching files')
31
- fileWatcher.startWatching()
32
- }
33
-
34
- app.decorate('fileWatcher', fileWatcher)
35
-
36
- app.addHook('onClose', async () => {
37
- if (!isRestartableApp || app.closingRestartable) {
38
- app.fileWatcher.stopWatching()
39
- app.log.debug('stop watching files')
40
- }
41
- })
42
- }
43
-
44
- module.exports = fp(setupFileWatcher)