@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.
@@ -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
- })