@platformatic/service 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.taprc CHANGED
@@ -1,2 +1,2 @@
1
- timeout: 80
1
+ timeout: 160
2
2
  coverage: false
package/index.js CHANGED
@@ -33,6 +33,19 @@ async function platformaticService (app, opts, toLoad = []) {
33
33
  app.decorate('platformatic', {})
34
34
  }
35
35
 
36
+ {
37
+ const fileWatcher = opts.fileWatcher
38
+ const configManager = opts.configManager
39
+ /* c8 ignore next 4 */
40
+ if (fileWatcher !== undefined) {
41
+ app.platformatic.fileWatcher = fileWatcher
42
+ }
43
+ if (configManager !== undefined) {
44
+ app.platformatic.configManager = configManager
45
+ app.platformatic.config = configManager.current
46
+ }
47
+ }
48
+
36
49
  if (opts.plugin) {
37
50
  let pluginOptions = opts.plugin
38
51
  /* c8 ignore next 4 */
@@ -48,7 +61,7 @@ async function platformaticService (app, opts, toLoad = []) {
48
61
  /* c8 ignore next */
49
62
  const hotReload = opts.plugin.watchOptions?.hotReload !== false
50
63
  const isWatchEnabled = opts.plugin.watch !== false
51
-
64
+ /* c8 ignore next 13 */
52
65
  if (isWatchEnabled && hotReload) {
53
66
  await app.register(sandbox, {
54
67
  ...pluginOptions,
@@ -116,7 +129,13 @@ async function buildServer (options, app = platformaticService) {
116
129
 
117
130
  const _restart = handler.restart
118
131
 
119
- handler.restart = async (opts) => {
132
+ let debounce = null
133
+ handler.restart = (opts) => {
134
+ /* c8 ignore next 3 */
135
+ if (debounce) {
136
+ return debounce
137
+ }
138
+
120
139
  addLoggerToTheConfig(opts)
121
140
 
122
141
  // Ignore because not tested on Windows
@@ -124,26 +143,20 @@ async function buildServer (options, app = platformaticService) {
124
143
  // this on Windows
125
144
  /* c8 ignore start */
126
145
  if (opts) {
127
- opts = createServerConfig(opts)
128
- opts.app = app
129
-
130
146
  const fileWatcher = handler.app.platformatic.fileWatcher
131
147
  const configManager = handler.app.platformatic.configManager
132
-
133
- await _restart(opts)
134
-
135
- if (fileWatcher !== undefined) {
136
- handler.app.platformatic.fileWatcher = fileWatcher
137
- }
138
- if (configManager !== undefined) {
139
- handler.app.platformatic.configManager = configManager
140
- handler.app.platformatic.config = configManager.current
141
- }
142
-
143
- return handler
148
+ opts.fileWatcher = fileWatcher
149
+ opts.configManager = configManager
150
+ opts = createServerConfig(opts)
151
+ opts.app = app
144
152
  }
153
+ debounce = _restart(opts).then(() => {
154
+ handler.app.log.info('restarted')
155
+ }).finally(() => {
156
+ debounce = null
157
+ })
145
158
  /* c8 ignore stop */
146
- return _restart()
159
+ return debounce
147
160
  }
148
161
 
149
162
  return handler
package/lib/schema.js CHANGED
@@ -125,6 +125,9 @@ const plugin = {
125
125
  watch: {
126
126
  type: 'boolean'
127
127
  },
128
+ fallback: {
129
+ type: 'boolean'
130
+ },
128
131
  watchOptions: {
129
132
  type: 'object',
130
133
  properties: {
@@ -156,6 +159,7 @@ const plugin = {
156
159
  type: 'object'
157
160
  }
158
161
  },
162
+ additionalProperties: false,
159
163
  required: ['path']
160
164
  }
161
165
 
package/lib/start.mjs CHANGED
@@ -34,9 +34,9 @@ async function start (_args) {
34
34
  configManager
35
35
  })
36
36
 
37
- configManager.on('update', (newConfig) => onConfigUpdated(newConfig, server))
38
37
  server.app.platformatic.configManager = configManager
39
38
  server.app.platformatic.config = config
39
+ configManager.on('update', (newConfig) => onConfigUpdated(newConfig, server))
40
40
 
41
41
  if (
42
42
  config.plugin !== undefined &&
@@ -58,9 +58,6 @@ async function start (_args) {
58
58
  process.on('SIGUSR2', function () {
59
59
  server.app.log.info('reloading configuration')
60
60
  server.restart()
61
- .then(() => {
62
- server.app.log.info('restarted')
63
- })
64
61
  .catch((err) => {
65
62
  server.app.log.error({
66
63
  err: {
@@ -103,7 +100,7 @@ async function startFileWatching (server) {
103
100
  })
104
101
  fileWatcher.startWatching()
105
102
 
106
- server.app.log.info('start watching files')
103
+ server.app.log.debug('start watching files')
107
104
  server.app.platformatic.fileWatcher = fileWatcher
108
105
  }
109
106
 
@@ -112,7 +109,7 @@ async function stopFileWatching (server) {
112
109
  if (fileWatcher !== undefined) {
113
110
  await fileWatcher.stopWatching()
114
111
 
115
- server.app.log.info('stop watching files')
112
+ server.app.log.debug('stop watching files')
116
113
  server.app.platformatic.fileWatcher = undefined
117
114
  }
118
115
  }
@@ -120,18 +117,11 @@ async function stopFileWatching (server) {
120
117
  async function onConfigUpdated (newConfig, server) {
121
118
  try {
122
119
  server.app.platformatic.config = newConfig
123
- server.app.log.info('config changed')
120
+ server.app.log.debug('config changed')
124
121
  server.app.log.trace({ newConfig }, 'new config')
125
122
 
126
123
  await stopFileWatching(server)
127
124
 
128
- if (
129
- newConfig.plugin !== undefined &&
130
- newConfig.plugin.watch !== false
131
- ) {
132
- await startFileWatching(server)
133
- }
134
-
135
125
  await server.restart(newConfig)
136
126
  } catch (err) {
137
127
  // TODO: test this
@@ -141,14 +131,24 @@ async function onConfigUpdated (newConfig, server) {
141
131
  stack: err.stack
142
132
  }
143
133
  }, 'failed to reload config')
134
+ } finally {
135
+ if (
136
+ newConfig.plugin !== undefined &&
137
+ newConfig.plugin.watch !== false
138
+ ) {
139
+ await startFileWatching(server)
140
+ }
144
141
  }
145
142
  }
146
143
 
147
144
  async function onFilesUpdated (server) {
145
+ // Reload the config as well, otherwise we will have problems
146
+ // in case the files watcher triggers the config watcher too
147
+ const configManager = server.app.platformatic.configManager
148
148
  try {
149
- const config = server.app.platformatic.config
150
- server.app.log.info('files changed')
151
- await server.restart(config)
149
+ server.app.log.debug('files changed')
150
+ await configManager.parse()
151
+ await server.restart(configManager.current)
152
152
  } catch (err) {
153
153
  // TODO: test this
154
154
  server.app.log.error({
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@platformatic/service",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Matteo Collina <hello@matteocollina.com>",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "git+https://github.com/plaformatic/platformatic.git"
9
+ "url": "git+https://github.com/platformatic/platformatic.git"
10
10
  },
11
11
  "license": "Apache-2.0",
12
12
  "bugs": {
13
- "url": "https://github.com/plaformatic/platformatic/issues"
13
+ "url": "https://github.com/platformatic/platformatic/issues"
14
14
  },
15
- "homepage": "https://github.com/plaformatic/platformatic#readme",
15
+ "homepage": "https://github.com/platformatic/platformatic#readme",
16
16
  "devDependencies": {
17
17
  "c8": "^7.11.0",
18
18
  "snazzy": "^9.0.0",
@@ -34,8 +34,8 @@
34
34
  "@fastify/static": "^6.5.0",
35
35
  "@fastify/swagger": "^8.0.0",
36
36
  "@fastify/under-pressure": "^8.0.0",
37
- "@platformatic/config": "0.6.0",
38
- "@platformatic/utils": "0.6.0",
37
+ "@platformatic/config": "0.7.0",
38
+ "@platformatic/utils": "0.7.0",
39
39
  "close-with-grace": "^1.1.0",
40
40
  "commist": "^3.1.2",
41
41
  "desm": "^1.2.0",
@@ -38,7 +38,7 @@ export async function start (...args) {
38
38
 
39
39
  const errorTimeout = setTimeout(() => {
40
40
  throw new Error('Couldn\'t start server')
41
- }, 10000)
41
+ }, 30000)
42
42
 
43
43
  for await (const messages of on(output, 'data')) {
44
44
  for (const message of messages) {
@@ -0,0 +1,244 @@
1
+ import os from 'os'
2
+ import { join, basename } from 'path'
3
+ import { writeFile, mkdtemp } from 'fs/promises'
4
+ import { setTimeout as sleep } from 'timers/promises'
5
+ import t, { test } from 'tap'
6
+ import { request } from 'undici'
7
+ import { start } from './helper.mjs'
8
+
9
+ t.jobs = 5
10
+
11
+ function createLoggingPlugin (text) {
12
+ return `\
13
+ module.exports = async (app) => {
14
+ app.get('/version', () => '${text}')
15
+ }
16
+ `
17
+ }
18
+
19
+ test('should watch js files by default', async ({ equal, teardown }) => {
20
+ const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-'))
21
+ const pluginFilePath = join(tmpDir, 'plugin.js')
22
+ const configFilePath = join(tmpDir, 'platformatic.service.json')
23
+
24
+ const defaultConfig = {
25
+ server: {
26
+ logger: {
27
+ level: 'info'
28
+ },
29
+ hostname: '127.0.0.1',
30
+ port: 0
31
+ },
32
+ plugin: {
33
+ path: pluginFilePath,
34
+ watch: true
35
+ }
36
+ }
37
+
38
+ await Promise.all([
39
+ writeFile(configFilePath, JSON.stringify(defaultConfig)),
40
+ writeFile(pluginFilePath, createLoggingPlugin('v1'))
41
+ ])
42
+
43
+ const { child, url } = await start('-c', configFilePath)
44
+ teardown(() => child.kill('SIGINT'))
45
+
46
+ await writeFile(pluginFilePath, createLoggingPlugin('v2'))
47
+
48
+ await sleep(5000)
49
+
50
+ const res = await request(`${url}/version`)
51
+ const version = await res.body.text()
52
+ equal(version, 'v2')
53
+ })
54
+
55
+ test('should watch allowed file', async ({ comment, teardown }) => {
56
+ const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-'))
57
+ const jsonFilePath = join(tmpDir, 'plugin-config.json')
58
+ const pluginFilePath = join(tmpDir, 'plugin.js')
59
+ const configFilePath = join(tmpDir, 'platformatic.service.json')
60
+
61
+ const config = {
62
+ server: {
63
+ logger: {
64
+ level: 'info'
65
+ },
66
+ hostname: '127.0.0.1',
67
+ port: 0
68
+ },
69
+ plugin: {
70
+ path: pluginFilePath,
71
+ watch: true,
72
+ watchOptions: {
73
+ allow: ['*.js', '*.json']
74
+ }
75
+ }
76
+ }
77
+
78
+ const pluginCode = `\
79
+ const readFileSync = require('fs').readFileSync
80
+ const json = readFileSync(${JSON.stringify(jsonFilePath)}, 'utf8')
81
+
82
+ module.exports = async function (app) {
83
+ if (json === 'RESTARTED') {
84
+ app.log.info('RESTARTED')
85
+ }
86
+ }`
87
+
88
+ await Promise.all([
89
+ writeFile(configFilePath, JSON.stringify(config)),
90
+ writeFile(jsonFilePath, 'INITIAL'),
91
+ writeFile(pluginFilePath, pluginCode)
92
+ ])
93
+
94
+ const { child } = await start('-c', configFilePath)
95
+ teardown(() => child.kill('SIGINT'))
96
+
97
+ writeFile(jsonFilePath, 'RESTARTED')
98
+ for await (const log of child.ndj) {
99
+ if (log.msg === 'RESTARTED') break
100
+ }
101
+ })
102
+
103
+ test('should not watch ignored file', async ({ teardown, equal }) => {
104
+ const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-'))
105
+ const pluginFilePath = join(tmpDir, 'plugin.js')
106
+ const configFilePath = join(tmpDir, 'platformatic.service.json')
107
+
108
+ const config = {
109
+ server: {
110
+ logger: {
111
+ level: 'info'
112
+ },
113
+ hostname: '127.0.0.1',
114
+ port: 0
115
+ },
116
+ plugin: {
117
+ path: pluginFilePath,
118
+ watch: true,
119
+ watchOptions: {
120
+ ignore: [basename(pluginFilePath)]
121
+ }
122
+ }
123
+ }
124
+
125
+ await Promise.all([
126
+ writeFile(configFilePath, JSON.stringify(config)),
127
+ writeFile(pluginFilePath, createLoggingPlugin('v1'))
128
+ ])
129
+
130
+ const { child, url } = await start('-c', configFilePath)
131
+ teardown(() => child.kill('SIGINT'))
132
+
133
+ await writeFile(pluginFilePath, createLoggingPlugin('v2'))
134
+ await sleep(5000)
135
+
136
+ const res = await request(`${url}/version`)
137
+ const version = await res.body.text()
138
+ equal(version, 'v1')
139
+ })
140
+
141
+ test('should not loop forever when doing ESM', { skip: true }, async ({ comment, fail }) => {
142
+ const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-esm-'))
143
+ const pluginFilePath = join(tmpDir, 'plugin.mjs')
144
+ const configFilePath = join(tmpDir, 'platformatic.service.json')
145
+
146
+ const config = {
147
+ server: {
148
+ logger: {
149
+ level: 'info'
150
+ },
151
+ hostname: '127.0.0.1',
152
+ port: 0
153
+ },
154
+ plugin: {
155
+ path: pluginFilePath,
156
+ watch: true,
157
+ watchOptions: {
158
+ ignore: [basename(pluginFilePath)]
159
+ }
160
+ }
161
+ }
162
+
163
+ await Promise.all([
164
+ writeFile(configFilePath, JSON.stringify(config)),
165
+ writeFile(pluginFilePath, 'export default async (app) => {}')
166
+ ])
167
+
168
+ const { child } = await start('-c', configFilePath)
169
+
170
+ await sleep(1000)
171
+
172
+ child.kill('SIGINT')
173
+
174
+ let linesCounter = 0
175
+ for await (const line of child.ndj) {
176
+ // lines will have a series of "config changed"
177
+ // messages without an ignore
178
+ comment(line.msg)
179
+ if (++linesCounter > 2) {
180
+ fail()
181
+ break
182
+ }
183
+ }
184
+ })
185
+
186
+ test('should watch config file', async ({ comment, teardown }) => {
187
+ const tmpDir = await mkdtemp(join(os.tmpdir(), 'watch-config-'))
188
+ const pluginFilePath = join(tmpDir, 'plugin.js')
189
+ const configFilePath = join(tmpDir, 'platformatic.service.json')
190
+
191
+ const config = {
192
+ server: {
193
+ logger: {
194
+ level: 'info'
195
+ },
196
+ hostname: '127.0.0.1',
197
+ port: 0
198
+ },
199
+ plugin: {
200
+ path: pluginFilePath,
201
+ watch: true,
202
+ watchOptions: {
203
+ allow: ['*.js', '*.json']
204
+ }
205
+ }
206
+ }
207
+
208
+ const config2 = {
209
+ server: {
210
+ logger: {
211
+ level: 'info'
212
+ },
213
+ hostname: '127.0.0.1',
214
+ port: 0
215
+ },
216
+ plugin: {
217
+ path: pluginFilePath,
218
+ options: {
219
+ log: true
220
+ }
221
+ }
222
+ }
223
+
224
+ const pluginCode = `\
225
+ module.exports = async function (app, opts) {
226
+ if (opts.log) {
227
+ app.log.info('RESTARTED')
228
+ }
229
+ }`
230
+
231
+ await Promise.all([
232
+ writeFile(configFilePath, JSON.stringify(config)),
233
+ writeFile(pluginFilePath, pluginCode)
234
+ ])
235
+
236
+ const { child } = await start('-c', configFilePath)
237
+ teardown(() => child.kill('SIGINT'))
238
+
239
+ // We do not await
240
+ writeFile(configFilePath, JSON.stringify(config2))
241
+ for await (const log of child.ndj) {
242
+ if (log.msg === 'RESTARTED') break
243
+ }
244
+ })
@@ -201,7 +201,7 @@ test('load and reload with the fallback', async ({ teardown, equal, pass, same }
201
201
  }
202
202
  })
203
203
 
204
- test('load and reload ESM', async ({ teardown, equal, pass, same }) => {
204
+ test('load and reload ESM', { skip: true }, async ({ teardown, equal, pass, same }) => {
205
205
  const file = join(os.tmpdir(), `some-plugin-${process.pid}.mjs`)
206
206
 
207
207
  await writeFile(file, `