velocious 1.0.81 → 1.0.83

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.
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "velocious": "bin/velocious.js"
4
4
  },
5
5
  "name": "velocious",
6
- "version": "1.0.81",
6
+ "version": "1.0.83",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "test": "VELOCIOUS_TEST_DIR=../ cd spec/dummy && npx velocious test",
package/src/controller.js CHANGED
@@ -86,7 +86,7 @@ export default class VelociousController {
86
86
  renderJsonArg(json) {
87
87
  const body = JSON.stringify(json)
88
88
 
89
- this._response.addHeader("Content-Type", "application/json")
89
+ this._response.setHeader("Content-Type", "application/json; charset=UTF-8")
90
90
  this._response.setBody(body)
91
91
  }
92
92
 
@@ -100,7 +100,7 @@ export default class VelociousController {
100
100
  if (err) {
101
101
  reject(err)
102
102
  } else {
103
- this._response.addHeader("Content-Type", "text/html")
103
+ this._response.setHeader("Content-Type", "text/html; charset=UTF-8")
104
104
  this._response.setBody(str)
105
105
 
106
106
  resolve()
@@ -87,16 +87,16 @@ export default class VeoliciousHttpServerClient {
87
87
  this.logger.debug("sendResponse", {clientCount: this.clientCount, connectionHeader, httpVersion})
88
88
 
89
89
  if (httpVersion == "1.0" && connectionHeader == "keep-alive") {
90
- response.addHeader("Connection", "Keep-Alive")
90
+ response.setHeader("Connection", "Keep-Alive")
91
91
  } else {
92
- response.addHeader("Connection", "Close")
92
+ response.setHeader("Connection", "Close")
93
93
  }
94
94
 
95
- const textEncoded = new TextEncoder().encode(body)
95
+ const contentLength = new TextEncoder().encode(body).length
96
96
 
97
- response.addHeader("Content-Length", textEncoded.length)
98
- response.addHeader("Date", date.toUTCString())
99
- response.addHeader("Server", "Velocious")
97
+ response.setHeader("Content-Length", contentLength)
98
+ response.setHeader("Date", date.toUTCString())
99
+ response.setHeader("Server", "Velocious")
100
100
 
101
101
  let headers = ""
102
102
 
@@ -14,6 +14,10 @@ export default class VelociousHttpServerClientResponse {
14
14
  this.headers[key].push(value)
15
15
  }
16
16
 
17
+ setHeader(key, value) {
18
+ this.headers[key] = [value]
19
+ }
20
+
17
21
  getBody() {
18
22
  if (this.body !== undefined) {
19
23
  return this.body
@@ -35,8 +39,8 @@ export default class VelociousHttpServerClientResponse {
35
39
  }
36
40
 
37
41
  setErrorBody(error) {
38
- this.body = `${error.message}\n\n${error.stack}`
39
- this.addHeader("Content-Type", "text/plain")
42
+ this.setHeader("Content-Type", "text/plain; charset=UTF-8")
43
+ this.setBody(`${error.message}\n\n${error.stack}`)
40
44
  }
41
45
 
42
46
  setStatus(status) {
@@ -52,11 +52,15 @@ export default class VelociousHttpServer {
52
52
  }
53
53
 
54
54
  async stopClients() {
55
+ const promises = []
56
+
55
57
  for (const clientCount in this.clients) {
56
58
  const client = this.clients[clientCount]
57
59
 
58
- await client.close()
60
+ promises.push(client.end())
59
61
  }
62
+
63
+ await Promise.all(promises)
60
64
  }
61
65
 
62
66
  stopServer() {
@@ -17,9 +17,11 @@ export default class ServerClient {
17
17
 
18
18
  listen = () => this.socket.on("data", this.onSocketData)
19
19
 
20
- close() {
21
- this.socket.destroy()
22
- this.events.emit("close", this)
20
+ end() {
21
+ return new Promise((resolve) => {
22
+ this.socket.once("close", () => resolve())
23
+ this.socket.end()
24
+ })
23
25
  }
24
26
 
25
27
  onSocketData = (chunk) => {
@@ -37,8 +39,12 @@ export default class ServerClient {
37
39
  this.events.emit("close", this)
38
40
  }
39
41
 
40
- send(data) {
41
- this.logger.debug("Send", data)
42
- this.socket.write(data)
42
+ async send(data) {
43
+ return new Promise((resolve) => {
44
+ this.logger.debug("Send", data)
45
+ this.socket.write(data, () => {
46
+ resolve()
47
+ })
48
+ })
43
49
  }
44
50
  }
@@ -78,7 +78,7 @@ export default class VelociousHttpServerWorker {
78
78
  } else if (command == "clientClose") {
79
79
  const {clientCount} = digs(data, "clientCount")
80
80
 
81
- this.clients[clientCount]?.close()
81
+ this.clients[clientCount]?.end()
82
82
  } else {
83
83
  throw new Error(`Unknown command: ${command}`)
84
84
  }