@platformatic/service 0.46.1 → 0.47.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.
Files changed (125) hide show
  1. package/.c8rc +6 -0
  2. package/config.d.ts +0 -1
  3. package/index.d.ts +25 -0
  4. package/index.js +1 -0
  5. package/index.test-d.ts +24 -1
  6. package/lib/schema.js +0 -3
  7. package/package.json +9 -7
  8. package/fixtures/hello/no-server-logger.json +0 -13
  9. package/fixtures/hello/platformatic.service.json +0 -18
  10. package/fixtures/hello/plugin.js +0 -7
  11. package/fixtures/hello/warn-log.service.json +0 -18
  12. package/fixtures/hello-client/hello/hello.cjs +0 -21
  13. package/fixtures/hello-client/hello/hello.d.ts +0 -34
  14. package/fixtures/hello-client/hello/hello.openapi.json +0 -22
  15. package/fixtures/hello-client/hello/package.json +0 -5
  16. package/fixtures/hello-client/platformatic.service.json +0 -19
  17. package/fixtures/hello-client/plugin.js +0 -8
  18. package/fixtures/hello-client-ts/hello/hello.cjs +0 -21
  19. package/fixtures/hello-client-ts/hello/hello.d.ts +0 -34
  20. package/fixtures/hello-client-ts/hello/hello.openapi.json +0 -22
  21. package/fixtures/hello-client-ts/hello/package.json +0 -5
  22. package/fixtures/hello-client-ts/platformatic.service.json +0 -21
  23. package/fixtures/hello-client-ts/plugin.ts +0 -8
  24. package/fixtures/hello-client-ts/tsconfig.json +0 -23
  25. package/fixtures/hello-client-ts-without-deps/hello.d.ts +0 -34
  26. package/fixtures/hello-client-ts-without-deps/hello.openapi.json +0 -22
  27. package/fixtures/hello-client-ts-without-deps/platformatic.service.json +0 -23
  28. package/fixtures/hello-client-ts-without-deps/plugin.ts +0 -8
  29. package/fixtures/hello-client-ts-without-deps/tsconfig.json +0 -22
  30. package/fixtures/hello-client-without-deps/hello.d.ts +0 -34
  31. package/fixtures/hello-client-without-deps/hello.openapi.json +0 -22
  32. package/fixtures/hello-client-without-deps/platformatic.service.json +0 -21
  33. package/fixtures/hello-client-without-deps/plugin.js +0 -8
  34. package/fixtures/https/embedded-pem.json +0 -20
  35. package/fixtures/https/https.crt +0 -25
  36. package/fixtures/https/https.key +0 -27
  37. package/fixtures/https/pem-path.json +0 -20
  38. package/fixtures/https/plugin.js +0 -7
  39. package/fixtures/options/platformatic.service.yml +0 -9
  40. package/fixtures/options/plugin.js +0 -7
  41. package/test/autoload.test.js +0 -486
  42. package/test/cli/compile-1.test.mjs +0 -352
  43. package/test/cli/compile-2.test.mjs +0 -310
  44. package/test/cli/gen-schema.test.mjs +0 -44
  45. package/test/cli/gen-types.test.mjs +0 -30
  46. package/test/cli/helper.mjs +0 -67
  47. package/test/cli/start.test.mjs +0 -126
  48. package/test/cli/tap-parallel-not-ok +0 -0
  49. package/test/cli/validations.test.mjs +0 -33
  50. package/test/clients.test.js +0 -87
  51. package/test/config.test.js +0 -316
  52. package/test/cors.test.js +0 -291
  53. package/test/fixtures/bad-typescript-plugin/platformatic.service.json +0 -16
  54. package/test/fixtures/bad-typescript-plugin/plugin.ts +0 -5
  55. package/test/fixtures/bad-typescript-plugin/tsconfig.json +0 -22
  56. package/test/fixtures/custom-port-placeholder.json +0 -10
  57. package/test/fixtures/default-env-var-names.json +0 -7
  58. package/test/fixtures/directories/platformatic.service.json +0 -13
  59. package/test/fixtures/directories/plugins/decorator.js +0 -7
  60. package/test/fixtures/directories/routes/foo/bar.js +0 -11
  61. package/test/fixtures/directories/routes/foo/baz/index.js +0 -7
  62. package/test/fixtures/directories/routes/foo/baz/index2.js +0 -11
  63. package/test/fixtures/directories/routes/oof/auto.hooks.js +0 -5
  64. package/test/fixtures/directories/routes/oof/index.js +0 -7
  65. package/test/fixtures/directories/routes/root.js +0 -7
  66. package/test/fixtures/hello-ts-with-config/global.d.ts +0 -8
  67. package/test/fixtures/hello-ts-with-config/platformatic.service.json +0 -16
  68. package/test/fixtures/hello-ts-with-config/plugin.ts +0 -8
  69. package/test/fixtures/hello-ts-with-config/tsconfig.json +0 -23
  70. package/test/fixtures/hello-world-resolver.js +0 -16
  71. package/test/fixtures/missing-property.config.json +0 -1
  72. package/test/fixtures/nested-directories/modules/catalogue/index.js +0 -11
  73. package/test/fixtures/nested-directories/modules/catalogue/routes/products.js +0 -14
  74. package/test/fixtures/nested-directories/modules/inventory/index.js +0 -33
  75. package/test/fixtures/nested-directories/modules/inventory/routes/product.js +0 -15
  76. package/test/fixtures/nested-directories/package.json +0 -3
  77. package/test/fixtures/nested-directories/platformatic.service.json +0 -23
  78. package/test/fixtures/nested-directories/plugins/decorator.js +0 -3
  79. package/test/fixtures/nested-directories/plugins/handlers.js +0 -17
  80. package/test/fixtures/not-load.js +0 -8
  81. package/test/fixtures/not-load.service.json +0 -13
  82. package/test/fixtures/other-side.js +0 -9
  83. package/test/fixtures/request-id.js +0 -9
  84. package/test/fixtures/root-endpoint-plugin.js +0 -8
  85. package/test/fixtures/throw-resolver.js +0 -16
  86. package/test/fixtures/typescript-autoload/platformatic.service.json +0 -16
  87. package/test/fixtures/typescript-autoload/routes/plugin.ts +0 -5
  88. package/test/fixtures/typescript-autoload/tsconfig.json +0 -22
  89. package/test/fixtures/typescript-compiled/compiled/plugin.js +0 -6
  90. package/test/fixtures/typescript-compiled/platformatic.service.json +0 -18
  91. package/test/fixtures/typescript-plugin/platformatic.service.json +0 -16
  92. package/test/fixtures/typescript-plugin/plugin.ts +0 -5
  93. package/test/fixtures/typescript-plugin/tsconfig.json +0 -22
  94. package/test/fixtures/typescript-plugin-custom-flags/platformatic.service.json +0 -18
  95. package/test/fixtures/typescript-plugin-custom-flags/plugin.ts +0 -5
  96. package/test/fixtures/typescript-plugin-custom-flags/tsconfig.json +0 -22
  97. package/test/fixtures/typescript-plugin-custom-tsconfig/a-config-for-ts.json +0 -22
  98. package/test/fixtures/typescript-plugin-custom-tsconfig/platformatic.service.json +0 -18
  99. package/test/fixtures/typescript-plugin-custom-tsconfig/plugin.ts +0 -5
  100. package/test/fixtures/typescript-plugin-nocompile/platformatic.service.json +0 -16
  101. package/test/fixtures/typescript-plugin-nocompile/plugin.ts +0 -5
  102. package/test/fixtures/typescript-plugin-nocompile/tsconfig.json +0 -22
  103. package/test/fixtures/typescript-plugin-nocompile-enabled/platformatic.service.json +0 -18
  104. package/test/fixtures/typescript-plugin-nocompile-enabled/plugin.ts +0 -5
  105. package/test/fixtures/typescript-plugin-nocompile-enabled/tsconfig.json +0 -22
  106. package/test/fixtures/typescript-plugin-nocompile-string/platformatic.service.json +0 -16
  107. package/test/fixtures/typescript-plugin-nocompile-string/plugin.ts +0 -5
  108. package/test/fixtures/typescript-plugin-nocompile-string/tsconfig.json +0 -22
  109. package/test/fixtures/typescript-plugin-string/platformatic.service.json +0 -16
  110. package/test/fixtures/typescript-plugin-string/plugin.ts +0 -5
  111. package/test/fixtures/typescript-plugin-string/tsconfig.json +0 -22
  112. package/test/fixtures/undici-plugin.js +0 -15
  113. package/test/graphql.test.js +0 -219
  114. package/test/healthcheck.test.js +0 -92
  115. package/test/helper.js +0 -30
  116. package/test/https.test.js +0 -64
  117. package/test/lambda.test.js +0 -103
  118. package/test/load-and-reload-files.test.js +0 -180
  119. package/test/load-plugin.test.js +0 -67
  120. package/test/metrics.test.js +0 -220
  121. package/test/routes.test.js +0 -230
  122. package/test/schema.test.js +0 -12
  123. package/test/tap-parallel-ok +0 -0
  124. package/test/telemetry.test.js +0 -87
  125. package/test/utils.test.js +0 -37
@@ -1,219 +0,0 @@
1
- 'use strict'
2
-
3
- const { test } = require('tap')
4
- const { buildServer } = require('..')
5
- const { buildConfig } = require('./helper')
6
- const { request } = require('undici')
7
- const { join } = require('path')
8
-
9
- test('graphql enabled', async ({ teardown, equal, same }) => {
10
- const app = await buildServer(buildConfig({
11
- server: {
12
- hostname: '127.0.0.1',
13
- port: 0,
14
- healthCheck: {
15
- enabled: true,
16
- interval: 2000
17
- }
18
- },
19
- service: {
20
- graphql: true
21
- },
22
- plugins: {
23
- paths: [join(__dirname, 'fixtures', 'hello-world-resolver.js')]
24
- }
25
- }))
26
-
27
- teardown(async () => {
28
- await app.close()
29
- })
30
- await app.start()
31
-
32
- {
33
- const res = await request(`${app.url}/graphql`, {
34
- method: 'POST',
35
- headers: {
36
- 'content-type': 'application/json'
37
- },
38
- body: JSON.stringify({
39
- query: `
40
- query {
41
- hello
42
- }
43
- `
44
- })
45
- })
46
- equal(res.statusCode, 200, 'hello status code')
47
- same(await res.body.json(), {
48
- data: {
49
- hello: 'world'
50
- }
51
- }, 'hello response')
52
- }
53
-
54
- {
55
- const res = await request(`${app.url}/graphiql`)
56
- equal(res.statusCode, 200, 'graphiql status code')
57
- }
58
- })
59
-
60
- test('graphql disabled', async ({ teardown, equal, fail }) => {
61
- try {
62
- const app = await buildServer(buildConfig({
63
- server: {
64
- hostname: '127.0.0.1',
65
- port: 0,
66
- healthCheck: {
67
- enabled: true,
68
- interval: 2000
69
- }
70
- },
71
- service: {
72
- graphql: false
73
- },
74
- plugins: {
75
- paths: [join(__dirname, 'fixtures', 'hello-world-resolver.js')]
76
- }
77
- }))
78
- await app.close()
79
- fail('should have errored but did not')
80
- } catch (err) {
81
- equal(err.message, 'Cannot read properties of undefined (reading \'extendSchema\')')
82
- }
83
- })
84
-
85
- test('disable graphiql', async ({ teardown, equal, same }) => {
86
- const app = await buildServer(buildConfig({
87
- server: {
88
- hostname: '127.0.0.1',
89
- port: 0,
90
- healthCheck: {
91
- enabled: true,
92
- interval: 2000
93
- }
94
- },
95
- service: {
96
- graphql: {
97
- graphiql: false
98
- }
99
- },
100
- plugins: {
101
- paths: [join(__dirname, 'fixtures', 'hello-world-resolver.js')]
102
- }
103
- }))
104
-
105
- teardown(async () => {
106
- await app.close()
107
- })
108
- await app.start()
109
-
110
- {
111
- const res = await request(`${app.url}/graphql`, {
112
- method: 'POST',
113
- headers: {
114
- 'content-type': 'application/json'
115
- },
116
- body: JSON.stringify({
117
- query: `
118
- query {
119
- hello
120
- }
121
- `
122
- })
123
- })
124
- equal(res.statusCode, 200, 'hello status code')
125
- same(await res.body.json(), {
126
- data: {
127
- hello: 'world'
128
- }
129
- }, 'hello response')
130
- }
131
-
132
- {
133
- const res = await request(`${app.url}/graphiql`)
134
- equal(res.statusCode, 404, 'graphiql status code')
135
- }
136
- })
137
-
138
- test('graphql disabled by default', async ({ teardown, equal, fail }) => {
139
- try {
140
- const app = await buildServer(buildConfig({
141
- server: {
142
- hostname: '127.0.0.1',
143
- port: 0,
144
- healthCheck: {
145
- enabled: true,
146
- interval: 2000
147
- }
148
- },
149
- plugins: {
150
- paths: [join(__dirname, 'fixtures', 'hello-world-resolver.js')]
151
- }
152
- }))
153
- await app.close()
154
- fail('should have errored but did not')
155
- } catch (err) {
156
- equal(err.message, 'Cannot read properties of undefined (reading \'extendSchema\')')
157
- }
158
- })
159
-
160
- test('graphql errors are correctly propagated in custom resolvers', async ({ teardown, equal, same }) => {
161
- const app = await buildServer(buildConfig({
162
- server: {
163
- hostname: '127.0.0.1',
164
- port: 0,
165
- healthCheck: {
166
- enabled: true,
167
- interval: 2000
168
- }
169
- },
170
- service: {
171
- graphql: true
172
- },
173
- plugins: {
174
- paths: [join(__dirname, 'fixtures', 'throw-resolver.js')]
175
- }
176
- }))
177
-
178
- teardown(async () => {
179
- await app.close()
180
- })
181
- await app.start()
182
-
183
- {
184
- const res = await request(`${app.url}/graphql`, {
185
- method: 'POST',
186
- headers: {
187
- 'content-type': 'application/json'
188
- },
189
- body: JSON.stringify({
190
- query: `
191
- query {
192
- hello
193
- }
194
- `
195
- })
196
- })
197
- equal(res.statusCode, 200, 'hello status code')
198
- same(await res.body.json(), {
199
- data: {
200
- hello: null
201
- },
202
- errors: [
203
- {
204
- message: 'Kaboooooom!!!',
205
- locations: [{
206
- line: 3,
207
- column: 13
208
- }],
209
- path: ['hello']
210
- }
211
- ]
212
- }, 'hello response')
213
- }
214
-
215
- {
216
- const res = await request(`${app.url}/graphiql`)
217
- equal(res.statusCode, 200, 'graphiql status code')
218
- }
219
- })
@@ -1,92 +0,0 @@
1
- 'use strict'
2
-
3
- const { test } = require('tap')
4
- const { buildServer } = require('..')
5
- require('./helper')
6
- const { request } = require('undici')
7
-
8
- test('healthcheck route enabled with interval', async ({ teardown, equal, same }) => {
9
- let check = true
10
- const app = await buildServer({
11
- server: {
12
- hostname: '127.0.0.1',
13
- port: 0,
14
- healthCheck: {
15
- interval: 2000,
16
- fn: function () {
17
- return check
18
- }
19
- }
20
- },
21
- metrics: false
22
- })
23
-
24
- teardown(async () => {
25
- await app.close()
26
- })
27
- await app.start()
28
-
29
- {
30
- const res = await (request(`${app.url}/status`))
31
- equal(res.statusCode, 200)
32
- const body = await res.body.json()
33
- same(body, { status: 'ok' })
34
- }
35
-
36
- check = false
37
-
38
- {
39
- const res = await (request(`${app.url}/status`))
40
- equal(res.statusCode, 503)
41
- const body = await res.body.json()
42
- same(body, {
43
- statusCode: 503,
44
- code: 'FST_UNDER_PRESSURE',
45
- error: 'Service Unavailable',
46
- message: 'Service Unavailable'
47
- })
48
- }
49
-
50
- check = true
51
- })
52
-
53
- test('healthcheck route enabled without interval', async ({ teardown, equal, same }) => {
54
- const app = await buildServer({
55
- server: {
56
- hostname: '127.0.0.1',
57
- port: 0,
58
- healthCheck: true
59
- },
60
- metrics: false
61
- })
62
-
63
- teardown(async () => {
64
- await app.close()
65
- })
66
- await app.start()
67
-
68
- {
69
- const res = await (request(`${app.url}/status`))
70
- equal(res.statusCode, 200)
71
- const body = await res.body.json()
72
- same(body, { status: 'ok' })
73
- }
74
- })
75
-
76
- test('healthcheck route disabled', async ({ teardown, equal, same }) => {
77
- const app = await buildServer({
78
- server: {
79
- hostname: '127.0.0.1',
80
- port: 0
81
- },
82
- metrics: false
83
- })
84
-
85
- teardown(async () => {
86
- await app.close()
87
- })
88
- await app.start()
89
-
90
- const res = await (request(`${app.url}/status`))
91
- equal(res.statusCode, 404)
92
- })
package/test/helper.js DELETED
@@ -1,30 +0,0 @@
1
- 'use strict'
2
-
3
- const { Agent, setGlobalDispatcher } = require('undici')
4
- const tap = require('tap')
5
-
6
- const agent = new Agent({
7
- keepAliveTimeout: 10,
8
- keepAliveMaxTimeout: 10,
9
- tls: {
10
- rejectUnauthorized: false
11
- }
12
- })
13
-
14
- setGlobalDispatcher(agent)
15
-
16
- // This should not be needed, but a weird combination
17
- // of node-tap, Windows, c8 and ESM makes this necessary.
18
- tap.teardown(() => {
19
- process.exit(0)
20
- })
21
-
22
- function buildConfig (options) {
23
- const base = {
24
- server: {}
25
- }
26
-
27
- return Object.assign(base, options)
28
- }
29
-
30
- module.exports.buildConfig = buildConfig
@@ -1,64 +0,0 @@
1
- 'use strict'
2
-
3
- const { mkdtemp, writeFile } = require('fs/promises')
4
- const { tmpdir } = require('os')
5
- const { join, relative } = require('path')
6
- const selfCert = require('self-cert')
7
- const { test } = require('tap')
8
- const { Agent, setGlobalDispatcher, request } = require('undici')
9
- const { buildServer } = require('..')
10
- const { buildConfig } = require('./helper')
11
-
12
- test('supports https options', async ({ teardown, equal, same, plan, comment }) => {
13
- plan(6)
14
-
15
- const { certificate, privateKey } = selfCert({})
16
- const localDir = tmpdir()
17
- comment(`system tmpdir ${localDir}`)
18
- const tmpDir = await mkdtemp(join(localDir, 'plt-service-https-test-'))
19
- comment(`writing in ${tmpDir}`)
20
- const privateKeyPath = join(tmpDir, 'plt.key')
21
- const certificatePath = join(tmpDir, 'plt.cert')
22
- const certificateRelativePath = relative(process.cwd(), certificatePath)
23
- comment(`certificate relative path ${certificateRelativePath}`)
24
-
25
- await writeFile(privateKeyPath, privateKey)
26
- await writeFile(certificatePath, certificate)
27
-
28
- setGlobalDispatcher(new Agent({
29
- connect: {
30
- rejectUnauthorized: false
31
- }
32
- }))
33
-
34
- const app = await buildServer(buildConfig({
35
- server: {
36
- hostname: '127.0.0.1',
37
- port: 0,
38
- https: {
39
- key: privateKey,
40
- cert: [{ path: certificateRelativePath }]
41
- }
42
- }
43
- }))
44
-
45
- teardown(async () => {
46
- await app.close()
47
- })
48
-
49
- await app.start()
50
-
51
- equal(app.url.startsWith('https://'), true)
52
- let res = await (request(`${app.url}/`))
53
- equal(res.statusCode, 200)
54
- let body = await res.body.json()
55
- same(body, { message: 'Welcome to Platformatic! Please visit https://docs.platformatic.dev' })
56
-
57
- await app.restart()
58
-
59
- equal(app.url.startsWith('https://'), true)
60
- res = await (request(`${app.url}/`))
61
- equal(res.statusCode, 200)
62
- body = await res.body.json()
63
- same(body, { message: 'Welcome to Platformatic! Please visit https://docs.platformatic.dev' })
64
- })
@@ -1,103 +0,0 @@
1
- 'use strict'
2
-
3
- const { test } = require('tap')
4
- const { buildServer } = require('..')
5
- const { buildConfig } = require('./helper')
6
- const { join } = require('path')
7
- const awsLambdaFastify = require('@fastify/aws-lambda')
8
-
9
- test('should respond 200 on root endpoint', async ({ teardown, equal, same, ok }) => {
10
- const app = await buildServer(buildConfig({
11
- server: {
12
- hostname: '127.0.0.1',
13
- port: 0,
14
- healthCheck: {
15
- enabled: true,
16
- interval: 2000
17
- }
18
- }
19
- }))
20
-
21
- teardown(async () => {
22
- await app.close()
23
- })
24
-
25
- const handler = awsLambdaFastify(app)
26
-
27
- {
28
- // No browser (i.e. curl)
29
- const evt = {
30
- version: '2.0',
31
- httpMethod: 'GET',
32
- path: '/',
33
- headers: {
34
- },
35
- cookies: [],
36
- queryStringParameters: ''
37
- }
38
-
39
- const ret = await handler(evt)
40
-
41
- equal(ret.body, JSON.stringify({ message: 'Welcome to Platformatic! Please visit https://docs.platformatic.dev' }))
42
- equal(ret.isBase64Encoded, false)
43
- ok(ret.headers)
44
- equal(ret.headers['content-type'], 'application/json; charset=utf-8')
45
- equal(ret.headers['content-length'], '81')
46
- ok(ret.headers.date)
47
- equal(ret.headers.connection, 'keep-alive')
48
- }
49
-
50
- {
51
- // browser
52
- const evt = {
53
- version: '2.0',
54
- httpMethod: 'GET',
55
- path: '/',
56
- headers: {
57
- 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
58
- },
59
- cookies: [],
60
- queryStringParameters: ''
61
- }
62
-
63
- const ret = await handler(evt)
64
-
65
- // No browser (i.e. curl)
66
- equal(ret.isBase64Encoded, false)
67
- ok(ret.headers)
68
- equal(ret.headers['content-type'], 'text/html; charset=UTF-8')
69
- ok(ret.headers.date)
70
- equal(ret.headers.connection, 'keep-alive')
71
- }
72
- })
73
-
74
- test('from a config file on disk', async ({ teardown, equal, same, ok }) => {
75
- const app = await buildServer(join(__dirname, '..', 'fixtures', 'hello', 'warn-log.service.json'))
76
-
77
- teardown(async () => {
78
- await app.close()
79
- })
80
-
81
- const handler = awsLambdaFastify(app)
82
-
83
- // No browser (i.e. curl)
84
- const evt = {
85
- version: '2.0',
86
- httpMethod: 'GET',
87
- path: '/',
88
- headers: {
89
- },
90
- cookies: [],
91
- queryStringParameters: ''
92
- }
93
-
94
- const ret = await handler(evt)
95
-
96
- equal(ret.body, JSON.stringify({ hello: 'world' }))
97
- equal(ret.isBase64Encoded, false)
98
- ok(ret.headers)
99
- equal(ret.headers['content-type'], 'application/json; charset=utf-8')
100
- equal(ret.headers['content-length'], '17')
101
- ok(ret.headers.date)
102
- equal(ret.headers.connection, 'keep-alive')
103
- })
@@ -1,180 +0,0 @@
1
- 'use strict'
2
-
3
- require('./helper')
4
- const { test } = require('tap')
5
- const { buildServer } = require('..')
6
- const { request, setGlobalDispatcher, getGlobalDispatcher, MockAgent } = require('undici')
7
- const { randomUUID } = require('crypto')
8
- const { join } = require('path')
9
- const os = require('os')
10
- const { writeFile } = require('fs/promises')
11
-
12
- test('load and reload', async ({ teardown, equal, pass, same }) => {
13
- const file = join(os.tmpdir(), `some-plugin-${randomUUID()}.js`)
14
-
15
- await writeFile(file, `
16
- module.exports = async function (app) {
17
- }`
18
- )
19
-
20
- const app = await buildServer({
21
- server: {
22
- hostname: '127.0.0.1',
23
- port: 0
24
- },
25
- plugins: {
26
- paths: [file]
27
- },
28
- watch: false,
29
- metrics: false
30
- })
31
-
32
- teardown(async () => {
33
- await app.close()
34
- })
35
- await app.start()
36
-
37
- {
38
- const res = await request(`${app.url}/`)
39
- equal(res.statusCode, 200, 'status code')
40
- const data = await res.body.json()
41
- same(data, { message: 'Welcome to Platformatic! Please visit https://docs.platformatic.dev' })
42
- }
43
-
44
- await writeFile(file, `
45
- module.exports = async function (app) {
46
- app.get('/', () => "hello world" )
47
- }`)
48
-
49
- await app.restart()
50
-
51
- {
52
- const res = await request(`${app.url}/`)
53
- equal(res.statusCode, 200, 'add status code')
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://docs.platformatic.dev' })
56
- }
57
- })
58
-
59
- test('error', async ({ teardown, equal, pass, match }) => {
60
- const file = join(os.tmpdir(), `some-plugin-${randomUUID()}.js`)
61
-
62
- await writeFile(file, `
63
- module.exports = async function (app) {
64
- app.get('/', () => {
65
- throw new Error('kaboom')
66
- })
67
- }`)
68
-
69
- const app = await buildServer({
70
- server: {
71
- hostname: '127.0.0.1',
72
- port: 0
73
- },
74
- plugins: {
75
- paths: [file]
76
- },
77
- watch: false,
78
- metrics: false
79
- })
80
-
81
- teardown(async () => {
82
- await app.close()
83
- })
84
- await app.start()
85
-
86
- const res = await request(`${app.url}/`)
87
- equal(res.statusCode, 500, 'add status code')
88
- match(await res.body.json(), {
89
- message: 'kaboom'
90
- })
91
- })
92
-
93
- test('mock undici is supported', async ({ teardown, equal, pass, same }) => {
94
- const previousAgent = getGlobalDispatcher()
95
- teardown(() => setGlobalDispatcher(previousAgent))
96
-
97
- const mockAgent = new MockAgent({
98
- keepAliveTimeout: 10,
99
- keepAliveMaxTimeout: 10
100
- })
101
- setGlobalDispatcher(mockAgent)
102
-
103
- const mockPool = mockAgent.get('http://localhost:42')
104
-
105
- // intercept the request
106
- mockPool.intercept({
107
- path: '/',
108
- method: 'GET'
109
- }).reply(200, {
110
- hello: 'world'
111
- })
112
-
113
- const app = await buildServer({
114
- server: {
115
- hostname: '127.0.0.1',
116
- port: 0
117
- },
118
- plugins: {
119
- paths: [join(__dirname, 'fixtures', 'undici-plugin.js')]
120
- }
121
- })
122
-
123
- teardown(async () => {
124
- await app.close()
125
- })
126
- await app.start()
127
-
128
- const res = await request(`${app.url}/request`, {
129
- method: 'GET'
130
- })
131
- equal(res.statusCode, 200)
132
- same(await res.body.json(), {
133
- hello: 'world'
134
- })
135
- })
136
-
137
- test('load and reload ESM', async ({ teardown, equal, pass, same }) => {
138
- const file = join(os.tmpdir(), `some-plugin-${process.pid}.mjs`)
139
-
140
- await writeFile(file, `
141
- export default async function (app) {
142
- }`
143
- )
144
-
145
- const app = await buildServer({
146
- server: {
147
- hostname: '127.0.0.1',
148
- port: 0
149
- },
150
- plugins: {
151
- paths: [file]
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://docs.platformatic.dev' })
165
- }
166
-
167
- await writeFile(file, `
168
- export default 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
- // 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://docs.platformatic.dev' })
179
- }
180
- })