system-testing 1.0.33 → 1.0.35

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,77 +0,0 @@
1
- // @ts-check
2
-
3
- import fs from "node:fs/promises"
4
- import http from "node:http"
5
- import mime from "mime"
6
- import url from "url"
7
-
8
- export default class SystemTestHttpServer {
9
- /** @returns {void} */
10
- close() {
11
- if (!this.httpServer) {
12
- throw new Error("HTTP server is not initialized")
13
- }
14
-
15
- this.httpServer.close()
16
- }
17
-
18
- /**
19
- * @param {http.IncomingMessage} request
20
- * @param {http.ServerResponse} response
21
- * @returns {Promise<void>}
22
- */
23
- onHttpServerRequest = async (request, response) => {
24
- if (!request.url) {
25
- response.statusCode = 400
26
- response.end("Bad Request")
27
- return
28
- }
29
-
30
- const parsedUrl = url.parse(request.url)
31
- let filePath = `${process.cwd()}/dist${parsedUrl.pathname}`
32
-
33
- if (filePath.endsWith("/")) {
34
- filePath += "index.html"
35
- }
36
-
37
- let fileExists
38
-
39
- try {
40
- await fs.stat(filePath)
41
- fileExists = true
42
- } catch (_error) { // eslint-disable-line no-unused-vars
43
- fileExists = false
44
- }
45
-
46
- if (!fileExists) {
47
- filePath = `${process.cwd()}/dist/index.html`
48
- }
49
-
50
- const fileContent = await fs.readFile(filePath)
51
- const mimeType = mime.getType(filePath)
52
-
53
- response.statusCode = 200
54
-
55
- if (mimeType) {
56
- response.setHeader("Content-Type", mimeType)
57
- }
58
-
59
- response.end(fileContent)
60
- }
61
-
62
- /** @returns {Promise<void>} */
63
- async start() {
64
- this.basePath = await fs.realpath(`${__dirname}/../..`)
65
- await this.startHttpServer()
66
- }
67
-
68
- /** @returns {Promise<void>} */
69
- startHttpServer() {
70
- return new Promise((resolve) => {
71
- this.httpServer = http.createServer(this.onHttpServerRequest)
72
- this.httpServer.listen(1984, "localhost", () => {
73
- resolve()
74
- })
75
- })
76
- }
77
- }