@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
@@ -1,289 +0,0 @@
1
- import os from 'os'
2
- import { join, basename } from 'path'
3
- import { writeFile, mkdtemp } from 'fs/promises'
4
- import t, { test } from 'tap'
5
- import { request } from 'undici'
6
- import { setTimeout as sleep } from 'timers/promises'
7
- import { start } from './helper.mjs'
8
-
9
- t.jobs = 5
10
-
11
- function createLoggingPlugin (text, reloaded = false) {
12
- return `\
13
- module.exports = async (app) => {
14
- app.log.info({ reloaded: ${reloaded}, text: '${text}' }, 'debugme')
15
- if (${reloaded}) {
16
- app.log.info('RELOADED')
17
- }
18
- app.get('/version', () => '${text}')
19
- }
20
- `
21
- }
22
-
23
- test('should watch js files by default', async ({ equal, teardown, comment }) => {
24
- const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-'))
25
- comment(`using ${tmpDir}`)
26
- const pluginFilePath = join(tmpDir, 'plugin.js')
27
- const configFilePath = join(tmpDir, 'platformatic.service.json')
28
-
29
- const defaultConfig = {
30
- server: {
31
- logger: {
32
- level: 'info'
33
- },
34
- hostname: '127.0.0.1',
35
- port: 0
36
- },
37
- watch: true,
38
- plugins: {
39
- paths: [pluginFilePath]
40
- }
41
- }
42
-
43
- await Promise.all([
44
- writeFile(configFilePath, JSON.stringify(defaultConfig)),
45
- writeFile(pluginFilePath, createLoggingPlugin('v1'))
46
- ])
47
-
48
- const { child, url } = await start(['-c', configFilePath])
49
- teardown(() => child.kill('SIGINT'))
50
-
51
- await writeFile(pluginFilePath, createLoggingPlugin('v2', true))
52
-
53
- for await (const log of child.ndj) {
54
- if (log.msg === 'RELOADED') break
55
- }
56
-
57
- const res = await request(`${url}/version`)
58
- const version = await res.body.text()
59
- equal(version, 'v2')
60
- })
61
-
62
- test('should watch allowed file', async ({ comment, teardown }) => {
63
- const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-'))
64
- const jsonFilePath = join(tmpDir, 'plugin-config.json')
65
- const pluginFilePath = join(tmpDir, 'plugin.js')
66
- const configFilePath = join(tmpDir, 'platformatic.service.json')
67
-
68
- const config = {
69
- server: {
70
- logger: {
71
- level: 'info'
72
- },
73
- hostname: '127.0.0.1',
74
- port: 0
75
- },
76
- watch: {
77
- allow: ['*.js', '*.json']
78
- },
79
- plugins: {
80
- paths: [pluginFilePath]
81
- }
82
- }
83
-
84
- const pluginCode = `\
85
- const readFileSync = require('fs').readFileSync
86
- const json = readFileSync(${JSON.stringify(jsonFilePath)}, 'utf8')
87
-
88
- module.exports = async function (app) {
89
- if (json === 'RESTARTED') {
90
- app.log.info('RESTARTED')
91
- }
92
- }`
93
-
94
- await Promise.all([
95
- writeFile(configFilePath, JSON.stringify(config)),
96
- writeFile(jsonFilePath, 'INITIAL'),
97
- writeFile(pluginFilePath, pluginCode)
98
- ])
99
-
100
- const { child } = await start(['-c', configFilePath])
101
- teardown(() => child.kill('SIGINT'))
102
-
103
- writeFile(jsonFilePath, 'RESTARTED')
104
- for await (const log of child.ndj) {
105
- if (log.msg === 'RESTARTED') break
106
- }
107
- })
108
-
109
- test('should not watch ignored file', async ({ teardown, equal }) => {
110
- const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-'))
111
- const pluginFilePath = join(tmpDir, 'plugin.js')
112
- const configFilePath = join(tmpDir, 'platformatic.service.json')
113
-
114
- const config = {
115
- server: {
116
- logger: {
117
- level: 'info'
118
- },
119
- hostname: '127.0.0.1',
120
- port: 0
121
- },
122
- watch: {
123
- ignore: [basename(pluginFilePath)]
124
- },
125
- plugins: {
126
- paths: [pluginFilePath]
127
- }
128
- }
129
-
130
- await Promise.all([
131
- writeFile(configFilePath, JSON.stringify(config)),
132
- writeFile(pluginFilePath, createLoggingPlugin('v1'))
133
- ])
134
-
135
- const { child, url } = await start(['-c', configFilePath])
136
- teardown(() => child.kill('SIGINT'))
137
-
138
- await writeFile(pluginFilePath, createLoggingPlugin('v2', true))
139
-
140
- await sleep(5000)
141
-
142
- const res = await request(`${url}/version`)
143
- const version = await res.body.text()
144
- equal(version, 'v1')
145
- })
146
-
147
- test('should not loop forever when doing ESM', async ({ comment, fail }) => {
148
- const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-esm-'))
149
- const pluginFilePath = join(tmpDir, 'plugin.mjs')
150
- const configFilePath = join(tmpDir, 'platformatic.service.json')
151
-
152
- const config = {
153
- server: {
154
- logger: {
155
- level: 'info'
156
- },
157
- hostname: '127.0.0.1',
158
- port: 0
159
- },
160
- watch: {
161
- ignore: [basename(pluginFilePath)]
162
- },
163
- plugins: {
164
- paths: [pluginFilePath]
165
- }
166
- }
167
-
168
- await Promise.all([
169
- writeFile(configFilePath, JSON.stringify(config)),
170
- writeFile(pluginFilePath, 'export default async (app) => {}')
171
- ])
172
-
173
- const { child } = await start(['-c', configFilePath])
174
-
175
- await sleep(1000)
176
-
177
- child.kill('SIGINT')
178
-
179
- let linesCounter = 0
180
- for await (const line of child.ndj) {
181
- // lines will have a series of "config changed"
182
- // messages without an ignore
183
- comment(line.msg)
184
- if (++linesCounter > 2) {
185
- fail()
186
- break
187
- }
188
- }
189
- })
190
-
191
- test('should watch config file', async ({ comment, teardown }) => {
192
- const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-config-'))
193
- const pluginFilePath = join(tmpDir, 'plugin.js')
194
- const configFilePath = join(tmpDir, 'platformatic.service.json')
195
-
196
- const config = {
197
- server: {
198
- logger: {
199
- level: 'info'
200
- },
201
- hostname: '127.0.0.1',
202
- port: 0
203
- },
204
- watch: {
205
- allow: ['*.js', '*.json']
206
- },
207
- plugins: {
208
- paths: [pluginFilePath]
209
- }
210
- }
211
-
212
- const config2 = {
213
- server: {
214
- logger: {
215
- level: 'info'
216
- },
217
- hostname: '127.0.0.1',
218
- port: 0
219
- },
220
- plugins: {
221
- paths: [{
222
- path: pluginFilePath,
223
- options: {
224
- log: true
225
- }
226
- }]
227
- }
228
- }
229
-
230
- const pluginCode = `\
231
- module.exports = async function (app, opts) {
232
- if (opts.log) {
233
- app.log.info('RESTARTED')
234
- }
235
- }`
236
-
237
- await Promise.all([
238
- writeFile(configFilePath, JSON.stringify(config)),
239
- writeFile(pluginFilePath, pluginCode)
240
- ])
241
-
242
- const { child } = await start(['-c', configFilePath])
243
- teardown(() => child.kill('SIGINT'))
244
-
245
- // We do not await
246
- writeFile(configFilePath, JSON.stringify(config2))
247
- for await (const log of child.ndj) {
248
- if (log.msg === 'RESTARTED') break
249
- }
250
- })
251
-
252
- test('should not fail when updating wrong config', async ({ equal, teardown, comment }) => {
253
- const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-'))
254
- comment(`using ${tmpDir}`)
255
- const pluginFilePath = join(tmpDir, 'plugin.js')
256
- const configFilePath = join(tmpDir, 'platformatic.service.json')
257
-
258
- const defaultConfig = {
259
- server: {
260
- logger: {
261
- level: 'info'
262
- },
263
- hostname: '127.0.0.1',
264
- port: 0
265
- },
266
- plugins: {
267
- paths: [pluginFilePath]
268
- },
269
- watch: true
270
- }
271
-
272
- await Promise.all([
273
- writeFile(configFilePath, JSON.stringify(defaultConfig)),
274
- writeFile(pluginFilePath, createLoggingPlugin('v1', true))
275
- ])
276
-
277
- const { child, url } = await start(['-c', configFilePath])
278
- teardown(() => child.kill('SIGINT'))
279
-
280
- writeFile(configFilePath, 'this is not a valid config')
281
-
282
- for await (const log of child.ndj) {
283
- if (log.msg === 'failed to reload server') break
284
- }
285
-
286
- const res = await request(`${url}/version`)
287
- const version = await res.body.text()
288
- equal(version, 'v1')
289
- })
@@ -1,248 +0,0 @@
1
- 'use strict'
2
-
3
- const { join } = require('path')
4
- const { tmpdir } = require('os')
5
- const { writeFile, mkdtemp, rm } = require('fs/promises')
6
-
7
- const { test } = require('tap')
8
- const { request } = require('undici')
9
-
10
- const { buildServer } = require('..')
11
- require('./helper')
12
-
13
- test('should stop watching files after disabling watch option', async ({ teardown, equal, pass, same }) => {
14
- const tmpDir = await mkdtemp(join(tmpdir(), 'platformatic.service.test-'))
15
- const pathToPlugin = join(tmpDir, 'plugin.js')
16
- const pathToConfig = join(tmpDir, 'platformatic.service.json')
17
-
18
- teardown(async () => {
19
- await rm(tmpDir, { recursive: true, force: true })
20
- })
21
-
22
- await writeFile(pathToPlugin, `
23
- module.exports = async function plugin (app) {
24
- app.get('/test', {}, async function (request, response) {
25
- return { res: "plugin, version 1"}
26
- })
27
- }`)
28
-
29
- const config = {
30
- server: {
31
- hostname: '127.0.0.1',
32
- port: 0
33
- },
34
- plugins: {
35
- paths: [pathToPlugin],
36
- stopTimeout: 1000,
37
- hotReload: true
38
- },
39
- watch: true,
40
- metrics: false
41
- }
42
-
43
- await writeFile(pathToConfig, JSON.stringify(config, null, 2))
44
- const app = await buildServer(pathToConfig)
45
-
46
- teardown(async () => {
47
- await app.close()
48
- })
49
- await app.start()
50
-
51
- {
52
- const res = await request(`${app.url}/test`, {
53
- method: 'GET'
54
- })
55
- same(await res.body.json(), { res: 'plugin, version 1' }, 'get rest plugin')
56
- }
57
-
58
- equal(app.fileWatcher.isWatching, true)
59
-
60
- await app.platformatic.configManager.update({ ...config, watch: false })
61
- await app.restart()
62
-
63
- await writeFile(pathToPlugin, `
64
- module.exports = async function plugin (app) {
65
- app.get('/test', {}, async function (request, response) {
66
- return { res: "plugin, version 2"}
67
- })
68
- }`)
69
-
70
- // wait to be sure that app is not watching files anymore
71
- await new Promise((resolve) => setTimeout(resolve, 5000))
72
-
73
- equal(app.fileWatcher, undefined)
74
-
75
- {
76
- const res = await request(`${app.url}/test`, {
77
- method: 'GET'
78
- })
79
- equal(res.statusCode, 200)
80
- // must be unchanged
81
- same(await res.body.json(), { res: 'plugin, version 1' }, 'get rest plugin')
82
- }
83
- })
84
-
85
- test('should stop watching typescript files after disabling watch option', async ({ teardown, equal, ok, same }) => {
86
- const tmpDir = await mkdtemp(join(tmpdir(), 'platformatic.service.test-'))
87
- const pathToPlugin = join(tmpDir, 'plugin.ts')
88
- const pathToConfig = join(tmpDir, 'platformatic.service.json')
89
- const pathToTsConfig = join(tmpDir, 'tsconfig.json')
90
-
91
- teardown(async () => {
92
- await rm(tmpDir, { recursive: true, force: true })
93
- })
94
-
95
- const tsConfig = {
96
- compilerOptions: {
97
- module: 'commonjs',
98
- esModuleInterop: true,
99
- target: 'es6',
100
- moduleResolution: 'node',
101
- sourceMap: true,
102
- pretty: true,
103
- noEmitOnError: true,
104
- outDir: 'dist'
105
- },
106
- watchOptions: {
107
- watchFile: 'fixedPollingInterval',
108
- watchDirectory: 'fixedPollingInterval',
109
- fallbackPolling: 'dynamicPriority',
110
- synchronousWatchDirectory: true,
111
- excludeDirectories: [
112
- '**/node_modules',
113
- 'dist'
114
- ]
115
- }
116
- }
117
-
118
- await writeFile(pathToPlugin, `
119
- export default async function plugin (app) {
120
- app.get('/test', {}, async function (request, response) {
121
- return { res: "plugin, version 1"}
122
- })
123
- }`)
124
-
125
- const config = {
126
- server: {
127
- hostname: '127.0.0.1',
128
- port: 0
129
- },
130
- plugins: {
131
- paths: [pathToPlugin],
132
- stopTimeout: 1000,
133
- hotReload: true,
134
- typescript: true
135
- },
136
- watch: true,
137
- metrics: false
138
- }
139
-
140
- await writeFile(pathToConfig, JSON.stringify(config, null, 2))
141
- await writeFile(pathToTsConfig, JSON.stringify(tsConfig, null, 2))
142
-
143
- const app = await buildServer(pathToConfig)
144
-
145
- teardown(async () => {
146
- await app.close()
147
- })
148
- await app.start()
149
-
150
- {
151
- const res = await request(`${app.url}/test`, {
152
- method: 'GET'
153
- })
154
- same(await res.body.json(), { res: 'plugin, version 1' }, 'get rest plugin')
155
- }
156
-
157
- ok(app.tsCompilerWatcher)
158
-
159
- await app.platformatic.configManager.update({ ...config, watch: false })
160
- await app.restart()
161
-
162
- await writeFile(pathToPlugin, `
163
- export default async function plugin (app) {
164
- app.get('/test', {}, async function (request, response) {
165
- return { res: "plugin, version 2"}
166
- })
167
- }`)
168
-
169
- // wait to be sure that app is not watching files anymore
170
- await new Promise((resolve) => setTimeout(resolve, 10000))
171
-
172
- equal(app.tsCompilerWatcher, undefined)
173
-
174
- {
175
- const res = await request(`${app.url}/test`, {
176
- method: 'GET'
177
- })
178
- equal(res.statusCode, 200)
179
- // must be unchanged
180
- same(await res.body.json(), { res: 'plugin, version 1' }, 'get rest plugin')
181
- }
182
- })
183
-
184
- test('should watch files by default', async ({ teardown, equal, pass, same }) => {
185
- const tmpDir = await mkdtemp(join(tmpdir(), 'platformatic.service.test-'))
186
- const pathToPlugin = join(tmpDir, 'plugin.js')
187
- const pathToConfig = join(tmpDir, 'platformatic.service.json')
188
-
189
- teardown(async () => {
190
- await rm(tmpDir, { recursive: true, force: true })
191
- })
192
-
193
- await writeFile(pathToPlugin, `
194
- module.exports = async function plugin (app) {
195
- app.get('/test', {}, async function (request, response) {
196
- return { res: "plugin, version 1"}
197
- })
198
- }`)
199
-
200
- const config = {
201
- server: {
202
- hostname: '127.0.0.1',
203
- port: 0
204
- },
205
- plugins: {
206
- paths: [pathToPlugin],
207
- stopTimeout: 1000,
208
- hotReload: true
209
- },
210
- metrics: false
211
- }
212
-
213
- await writeFile(pathToConfig, JSON.stringify(config, null, 2))
214
- const app = await buildServer(pathToConfig)
215
-
216
- teardown(async () => {
217
- await app.close()
218
- })
219
- await app.start()
220
-
221
- {
222
- const res = await request(`${app.url}/test`, {
223
- method: 'GET'
224
- })
225
- same(await res.body.json(), { res: 'plugin, version 1' }, 'get rest plugin')
226
- }
227
-
228
- equal(app.fileWatcher.isWatching, true)
229
-
230
- await writeFile(pathToPlugin, `
231
- module.exports = async function plugin (app) {
232
- app.get('/test', {}, async function (request, response) {
233
- return { res: "plugin, version 2"}
234
- })
235
- }`)
236
-
237
- // wait to be sure that app is not watching files anymore
238
- await new Promise((resolve) => setTimeout(resolve, 5000))
239
-
240
- {
241
- const res = await request(`${app.url}/test`, {
242
- method: 'GET'
243
- })
244
- equal(res.statusCode, 200)
245
- // must be unchanged
246
- same(await res.body.json(), { res: 'plugin, version 2' })
247
- }
248
- })