@platformatic/service 0.27.0 → 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.
@@ -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
  })
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)
@@ -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
- })