@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,67 +0,0 @@
1
- 'use strict'
2
-
3
- // setup the undici agent
4
- require('./helper')
5
-
6
- const { test } = require('tap')
7
- const { buildServer, platformaticService } = require('..')
8
- const { request } = require('undici')
9
-
10
- test('customize service', async ({ teardown, equal }) => {
11
- async function myApp (app, opts) {
12
- await platformaticService(app, opts, [async function (app) {
13
- app.get('/', () => 'hello world')
14
- }])
15
- }
16
-
17
- const app = await buildServer({
18
- server: {
19
- hostname: '127.0.0.1',
20
- port: 0
21
- }
22
- }, myApp)
23
-
24
- teardown(async () => {
25
- await app.close()
26
- })
27
- await app.start()
28
-
29
- const res = await (request(app.url))
30
- const body = await res.body.text()
31
- equal(res.statusCode, 200)
32
- equal(body, 'hello world')
33
- })
34
-
35
- test('catch errors from the other side', async ({ teardown, equal, same }) => {
36
- async function myApp (app, opts) {
37
- await platformaticService(app, opts, [async function (app) {
38
- app.get('/', () => 'hello world')
39
- }])
40
- }
41
-
42
- const app = await buildServer({
43
- server: {
44
- hostname: '127.0.0.1',
45
- port: 0
46
- },
47
- plugins: {
48
- paths: [{
49
- path: require.resolve('./fixtures/other-side.js')
50
- }]
51
- }
52
- }, myApp)
53
-
54
- teardown(async () => {
55
- await app.close()
56
- })
57
- await app.start()
58
-
59
- const res = await (request(app.url))
60
- const body = await res.body.json()
61
- equal(res.statusCode, 500)
62
- same(body, {
63
- statusCode: 500,
64
- error: 'Internal Server Error',
65
- message: 'kaboom'
66
- })
67
- })
@@ -1,220 +0,0 @@
1
- 'use strict'
2
-
3
- // setup the undici agent
4
- require('./helper')
5
-
6
- const { test, equal } = require('tap')
7
- const { buildServer } = require('..')
8
- const { request } = require('undici')
9
- const { promisify } = require('util')
10
- const sleep = promisify(setTimeout)
11
-
12
- test('has /metrics endpoint on default prometheus port', async ({ teardown, equal, fail, match }) => {
13
- const app = await buildServer({
14
- server: {
15
- hostname: '127.0.0.1',
16
- port: 0
17
- },
18
- metrics: true
19
- })
20
-
21
- teardown(async () => {
22
- await app.close()
23
- })
24
- await app.start()
25
-
26
- // needed to reach 100% code cov, otherwise the ELU check won't run
27
- await sleep(120)
28
- const res = await (request('http://127.0.0.1:9090/metrics'))
29
- const body = await res.body.text()
30
- equal(res.statusCode, 200)
31
- match(res.headers['content-type'], /^text\/plain/)
32
- testPrometheusOutput(body)
33
- })
34
-
35
- test('has /metrics endpoint with accept application/json', async ({ teardown, equal, fail, match }) => {
36
- const app = await buildServer({
37
- server: {
38
- hostname: '127.0.0.1',
39
- port: 0
40
- },
41
- metrics: true
42
- })
43
-
44
- teardown(async () => {
45
- await app.close()
46
- })
47
- await app.start()
48
-
49
- const res = await (request(
50
- 'http://127.0.0.1:9090/metrics',
51
- {
52
- headers: {
53
- accept: 'application/json'
54
- }
55
- }
56
- ))
57
- match(res.headers['content-type'], /^application\/json/)
58
- const json = await res.body.json()
59
- equal(res.statusCode, 200)
60
- testPrometheusJsonOutput(json)
61
- })
62
-
63
- test('has /metrics endpoint on configured port', async ({ teardown, equal, fail, match }) => {
64
- const app = await buildServer({
65
- server: {
66
- hostname: '127.0.0.1',
67
- port: 0
68
- },
69
- metrics: {
70
- port: 9999
71
- }
72
- })
73
-
74
- teardown(async () => {
75
- await app.close()
76
- })
77
- await app.start()
78
-
79
- const res = await (request('http://127.0.0.1:9999/metrics'))
80
- equal(res.statusCode, 200)
81
- match(res.headers['content-type'], /^text\/plain/)
82
- const body = await res.body.text()
83
- testPrometheusOutput(body)
84
- })
85
-
86
- test('support basic auth', async ({ teardown, equal, fail, match }) => {
87
- const app = await buildServer({
88
- server: {
89
- hostname: '127.0.0.1',
90
- port: 0
91
- },
92
- metrics: {
93
- auth: {
94
- username: 'foo',
95
- password: 'bar'
96
- }
97
- }
98
- })
99
-
100
- teardown(async () => {
101
- await app.close()
102
- })
103
- await app.start()
104
-
105
- {
106
- const res = await (request('http://127.0.0.1:9090/metrics'))
107
- equal(res.statusCode, 401)
108
- match(res.headers['content-type'], /^application\/json/)
109
- }
110
-
111
- {
112
- // wrong credentials
113
- const res = await (request('http://127.0.0.1:9090/metrics', {
114
- headers: {
115
- authorization: `Basic ${Buffer.from('bar:foo').toString('base64')}`
116
- }
117
- }))
118
- equal(res.statusCode, 401)
119
- match(res.headers['content-type'], /^application\/json/)
120
- }
121
-
122
- {
123
- const res = await (request('http://127.0.0.1:9090/metrics', {
124
- headers: {
125
- authorization: `Basic ${Buffer.from('foo:bar').toString('base64')}`
126
- }
127
- }))
128
- equal(res.statusCode, 200)
129
- match(res.headers['content-type'], /^text\/plain/)
130
- const body = await res.body.text()
131
- testPrometheusOutput(body)
132
- }
133
- })
134
-
135
- test('do not error on restart', async ({ teardown, equal, fail, match }) => {
136
- const app = await buildServer({
137
- server: {
138
- hostname: '127.0.0.1',
139
- port: 0
140
- },
141
- metrics: true
142
- })
143
-
144
- teardown(async () => {
145
- await app.close()
146
- })
147
- await app.start()
148
- await app.restart()
149
-
150
- const res = await (request('http://127.0.0.1:9090/metrics'))
151
- equal(res.statusCode, 200)
152
- match(res.headers['content-type'], /^text\/plain/)
153
- const body = await res.body.text()
154
- testPrometheusOutput(body)
155
- })
156
-
157
- test('restarting 10 times does not leak', async ({ teardown, equal, fail, match }) => {
158
- process.on('warning', (warning) => {
159
- fail('warning was raised')
160
- })
161
- const app = await buildServer({
162
- server: {
163
- hostname: '127.0.0.1',
164
- port: 0
165
- },
166
- metrics: true
167
- })
168
-
169
- teardown(async () => {
170
- await app.close()
171
- })
172
-
173
- await app.start()
174
-
175
- for (let i = 0; i < 10; i++) {
176
- await app.restart()
177
- }
178
- })
179
-
180
- function testPrometheusOutput (output) {
181
- let metricBlock = []
182
- const lines = output.split('\n')
183
- for (let i = 0; i < lines.length; i++) {
184
- const line = lines[i]
185
- if (line === '') {
186
- // check this metric set
187
- checkMetricBlock(metricBlock)
188
- metricBlock = []
189
- } else {
190
- metricBlock.push(line)
191
- }
192
- }
193
- }
194
-
195
- function checkMetricBlock (metricBlock) {
196
- if (!metricBlock[0].match(/^# HELP/)) {
197
- throw new Error('First line should be HELP')
198
- }
199
-
200
- if (!metricBlock[1].match(/^# TYPE/)) {
201
- throw new Error('Second line should be TYPE')
202
- }
203
- for (let i = 2; i < metricBlock.length; i++) {
204
- const split = metricBlock[i].split(' ')
205
- if (split.length !== 2) {
206
- throw new Error(`Bad format for metric: ${metricBlock[i]}`)
207
- }
208
- }
209
- return true
210
- }
211
-
212
- function testPrometheusJsonOutput (output) {
213
- for (const metric of output) {
214
- equal(typeof metric.help, 'string', 'metric.help is string')
215
- equal(typeof metric.name, 'string', 'metric.name is string')
216
- equal(typeof metric.type, 'string', 'metric.type is string')
217
- equal(typeof metric.aggregator, 'string', 'metric.aggregator is string')
218
- equal(Array.isArray(metric.values), true, 'metric.values is array')
219
- }
220
- }
@@ -1,230 +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('should respond 200 on root endpoint', 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
- }))
20
-
21
- teardown(async () => {
22
- await app.close()
23
- })
24
- await app.start()
25
-
26
- {
27
- // No browser (i.e. curl)
28
- const res = await (request(`${app.url}/`))
29
- equal(res.statusCode, 200)
30
- const body = await res.body.json()
31
- same(body, { message: 'Welcome to Platformatic! Please visit https://docs.platformatic.dev' })
32
- }
33
-
34
- {
35
- // browser
36
- const res = await (request(`${app.url}/`, {
37
- headers: {
38
- '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'
39
- }
40
- }))
41
-
42
- equal(res.statusCode, 200)
43
- equal(res.headers['content-type'], 'text/html; charset=UTF-8')
44
- }
45
- })
46
-
47
- test('should not overwrite a plugin which define a root endpoint', async ({ teardown, equal, same }) => {
48
- const app = await buildServer(buildConfig({
49
- server: {
50
- hostname: '127.0.0.1',
51
- port: 0,
52
- healthCheck: {
53
- enabled: true,
54
- interval: 2000
55
- }
56
- },
57
- plugins: {
58
- paths: [join(__dirname, 'fixtures', 'root-endpoint-plugin.js')]
59
- }
60
- }))
61
-
62
- teardown(async () => {
63
- await app.close()
64
- })
65
- await app.start()
66
-
67
- const res = await (request(`${app.url}/`))
68
- equal(res.statusCode, 200)
69
- const body = await res.body.json()
70
- same(body, { message: 'Root Plugin' })
71
- })
72
-
73
- test('openapi enabled', async ({ teardown, equal, same }) => {
74
- const app = await buildServer(buildConfig({
75
- server: {
76
- hostname: '127.0.0.1',
77
- port: 0,
78
- healthCheck: {
79
- enabled: true,
80
- interval: 2000
81
- }
82
- },
83
- service: {
84
- openapi: true
85
- },
86
- plugins: {
87
- paths: [join(__dirname, 'fixtures', 'root-endpoint-plugin.js')]
88
- }
89
- }))
90
-
91
- teardown(async () => {
92
- await app.close()
93
- })
94
- await app.start()
95
-
96
- {
97
- // No browser (i.e. curl)
98
- const res = await (request(`${app.url}/documentation/json`))
99
- equal(res.statusCode, 200)
100
- const body = await res.body.json()
101
-
102
- equal(body.openapi, '3.0.3')
103
- equal(body.info.title, 'Platformatic')
104
- equal(body.info.version, '1.0.0')
105
- equal(!!body.paths['/'].get, true)
106
- }
107
- })
108
-
109
- test('openapi config', async ({ teardown, equal, same }) => {
110
- const app = await buildServer(buildConfig({
111
- server: {
112
- hostname: '127.0.0.1',
113
- port: 0
114
- },
115
- service: {
116
- openapi: {
117
- info: {
118
- title: 'My Service',
119
- version: '0.0.42',
120
- description: 'My Service is the best service ever'
121
- }
122
- }
123
- },
124
- plugins: {
125
- paths: [join(__dirname, 'fixtures', 'root-endpoint-plugin.js')]
126
- }
127
- }))
128
-
129
- teardown(async () => {
130
- await app.close()
131
- })
132
- await app.start()
133
-
134
- {
135
- // No browser (i.e. curl)
136
- const res = await (request(`${app.url}/documentation/json`))
137
- equal(res.statusCode, 200)
138
- const body = await res.body.json()
139
-
140
- equal(body.openapi, '3.0.3')
141
- equal(body.info.title, 'My Service')
142
- equal(body.info.version, '0.0.42')
143
- equal(body.info.description, 'My Service is the best service ever')
144
- equal(!!body.paths['/'].get, true)
145
- }
146
- })
147
-
148
- test('openapi disabled', async ({ teardown, equal, same }) => {
149
- const app = await buildServer(buildConfig({
150
- server: {
151
- hostname: '127.0.0.1',
152
- port: 0,
153
- healthCheck: {
154
- enabled: true,
155
- interval: 2000
156
- }
157
- },
158
- service: {
159
- openapi: false
160
- },
161
- plugins: {
162
- paths: [join(__dirname, 'fixtures', 'root-endpoint-plugin.js')]
163
- }
164
- }))
165
-
166
- teardown(async () => {
167
- await app.close()
168
- })
169
- await app.start()
170
-
171
- {
172
- // No browser (i.e. curl)
173
- const res = await (request(`${app.url}/documentation/json`))
174
- equal(res.statusCode, 404)
175
- await res.body.text()
176
- }
177
- })
178
-
179
- test('openapi disabled by default', async ({ teardown, equal, same }) => {
180
- const app = await buildServer(buildConfig({
181
- server: {
182
- hostname: '127.0.0.1',
183
- port: 0,
184
- healthCheck: {
185
- enabled: true,
186
- interval: 2000
187
- }
188
- },
189
- plugins: {
190
- paths: [join(__dirname, 'fixtures', 'root-endpoint-plugin.js')]
191
- }
192
- }))
193
-
194
- teardown(async () => {
195
- await app.close()
196
- })
197
- await app.start()
198
-
199
- {
200
- // No browser (i.e. curl)
201
- const res = await (request(`${app.url}/documentation/json`))
202
- equal(res.statusCode, 404)
203
- await res.body.text()
204
- }
205
- })
206
-
207
- test('request id is a uuid', async ({ teardown, equal, match }) => {
208
- const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
209
- const app = await buildServer({
210
- server: {
211
- hostname: '127.0.0.1',
212
- port: 0
213
- },
214
- plugins: {
215
- paths: [join(__dirname, 'fixtures', 'request-id.js')]
216
- }
217
- })
218
-
219
- teardown(async () => {
220
- await app.close()
221
- })
222
- await app.start()
223
-
224
- const res = await request(`${app.url}/request-id`, {
225
- method: 'GET'
226
- })
227
- equal(res.statusCode, 200)
228
- const json = await res.body.json()
229
- match(json.request_id, UUID_REGEX)
230
- })
@@ -1,12 +0,0 @@
1
- 'use strict'
2
-
3
- const { test } = require('tap')
4
- const { join } = require('path')
5
- const { schema } = require('../lib/schema')
6
-
7
- test('schema output', async (t) => {
8
- const { execa } = await import('execa')
9
- const { stdout } = await execa(process.execPath, [join(__dirname, '..', 'lib', 'schema.js')])
10
-
11
- t.same(stdout, JSON.stringify(schema, null, 2))
12
- })
File without changes
@@ -1,87 +0,0 @@
1
- 'use strict'
2
-
3
- const { buildConfig } = require('./helper')
4
- const { test } = require('tap')
5
- const { buildServer } = require('..')
6
- const { request } = require('undici')
7
- const { join } = require('path')
8
- const os = require('os')
9
- const { writeFile } = require('fs/promises')
10
-
11
- test('should not configure telemetry if not configured', async ({ teardown, equal, pass, same }) => {
12
- const app = await buildServer(buildConfig({
13
- server: {
14
- hostname: '127.0.0.1',
15
- port: 0
16
- }
17
- }))
18
-
19
- teardown(async () => {
20
- await app.close()
21
- })
22
- await app.start()
23
- equal(app.openTelemetry, undefined)
24
- })
25
-
26
- test('should setup telemetry if configured', async ({ teardown, equal, pass, same }) => {
27
- const file = join(os.tmpdir(), `${process.pid}-1.js`)
28
-
29
- await writeFile(file, `
30
- module.exports = async function (app, options) {
31
- app.get('/', () => options.message)
32
- }`)
33
-
34
- const app = await buildServer(buildConfig({
35
- server: {
36
- hostname: '127.0.0.1',
37
- port: 0
38
- },
39
-
40
- telemetry: {
41
- serviceName: 'test-service',
42
- version: '1.0.0',
43
- exporter: {
44
- type: 'memory'
45
- }
46
- },
47
- plugins: {
48
- paths: [{
49
- path: file,
50
- options: {
51
- message: 'hello'
52
- }
53
- }]
54
- }
55
- }))
56
-
57
- teardown(async () => {
58
- await app.close()
59
- })
60
- await app.start()
61
-
62
- const res = await request(`${app.url}/`, {
63
- method: 'GET',
64
- headers: {
65
- 'Content-Type': 'application/json'
66
- },
67
- body: JSON.stringify({
68
- query: `
69
- mutation {
70
- savePage(input: { title: "Hello" }) {
71
- id
72
- title
73
- }
74
- }
75
- `
76
- })
77
- })
78
- equal(res.statusCode, 200, 'savePage status code')
79
- const { exporters } = app.openTelemetry
80
- const finishedSpans = exporters[0].getFinishedSpans()
81
- equal(finishedSpans.length, 1)
82
- const span = finishedSpans[0]
83
- equal(span.name, 'GET /')
84
- equal(span.attributes['http.request.method'], 'GET')
85
- equal(span.attributes['url.path'], '/')
86
- equal(span.attributes['http.response.status_code'], 200)
87
- })
@@ -1,37 +0,0 @@
1
- 'use strict'
2
-
3
- const { test } = require('tap')
4
- const { getJSPluginPath, isFileAccessible } = require('../lib/utils')
5
- const { join, resolve } = require('path')
6
-
7
- test('should get the path of a TS plugin', (t) => {
8
- t.plan(1)
9
-
10
- const result = getJSPluginPath('/something', '/something/plugin.ts', '/something/dist')
11
- const expected = join('/something', 'dist', 'plugin.js')
12
- t.equal(result, expected)
13
- })
14
-
15
- test('should get the path of a JS plugin', (t) => {
16
- t.plan(1)
17
-
18
- const result = getJSPluginPath('/something', '/something/plugin.js', '/something/dist')
19
- t.equal(result, '/something/plugin.js')
20
- })
21
-
22
- test('isFileAccessible with dir', async (t) => {
23
- const dir = resolve(join(__dirname, '..', 'fixtures', 'hello'))
24
- t.equal(await isFileAccessible('platformatic.service.json', dir), true)
25
- })
26
-
27
- test('isFileAccessible no dir', async (t) => {
28
- const file = resolve(join(__dirname, '..', 'fixtures', 'hello', 'platformatic.service.json'))
29
- t.equal(await isFileAccessible(file), true)
30
- })
31
-
32
- test('should return the same plugin folder if it\'s already the compiled one', (t) => {
33
- t.plan(1)
34
-
35
- const result = getJSPluginPath('/something', '/something/dist/plugins', '/something/dist')
36
- t.equal(result, '/something/dist/plugins')
37
- })