velocious 1.0.1 → 1.0.3

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 (157) hide show
  1. package/README.md +36 -0
  2. package/bin/velocious.mjs +8 -0
  3. package/index.mjs +21 -0
  4. package/package.json +15 -7
  5. package/peak_flow.yml +12 -5
  6. package/spec/cli/commands/db/create-spec.mjs +25 -0
  7. package/spec/cli/commands/db/migrate-spec.mjs +37 -0
  8. package/spec/cli/commands/destroy/migration-spec.mjs +15 -0
  9. package/spec/cli/commands/generate/migration-spec.mjs +18 -0
  10. package/spec/cli/commands/init-spec.mjs +19 -0
  11. package/spec/cli/commands/test/test-files-finder-spec.mjs +12 -0
  12. package/spec/database/connection/drivers/mysql/{query-parser-spec.cjs → query-parser-spec.mjs} +12 -8
  13. package/spec/database/drivers/mysql/connection-spec.mjs +21 -0
  14. package/spec/database/record/create-spec.mjs +14 -0
  15. package/spec/database/record/destroy-spec.mjs +17 -0
  16. package/spec/database/record/find-spec.mjs +29 -0
  17. package/spec/database/record/update-spec.mjs +15 -0
  18. package/spec/dummy/dummy-directory.mjs +11 -0
  19. package/spec/dummy/index.mjs +63 -0
  20. package/spec/dummy/src/config/configuration.example.mjs +19 -0
  21. package/spec/dummy/src/config/configuration.peakflow.mjs +20 -0
  22. package/spec/dummy/src/{routes.cjs → config/routes.mjs} +3 -2
  23. package/spec/dummy/src/database/migrations/20230728075328-create-projects.mjs +11 -0
  24. package/spec/dummy/src/database/migrations/20230728075329-create-tasks.mjs +13 -0
  25. package/spec/dummy/src/models/task.mjs +4 -0
  26. package/spec/dummy/src/routes/tasks/controller.mjs +26 -0
  27. package/spec/http-server/{client-spec.cjs → client-spec.mjs} +7 -10
  28. package/spec/http-server/{get-spec.cjs → get-spec.mjs} +2 -2
  29. package/spec/http-server/post-spec.mjs +72 -0
  30. package/spec/support/jasmine.json +4 -3
  31. package/src/application.mjs +50 -0
  32. package/src/cli/base-command.mjs +11 -0
  33. package/src/cli/commands/db/create.mjs +50 -0
  34. package/src/cli/commands/db/migrate.mjs +58 -0
  35. package/src/cli/commands/destroy/migration.mjs +35 -0
  36. package/src/cli/commands/generate/migration.mjs +36 -0
  37. package/src/cli/commands/init.mjs +60 -0
  38. package/src/cli/commands/test/index.mjs +14 -0
  39. package/src/cli/commands/test/test-files-finder.mjs +99 -0
  40. package/src/cli/commands/test/test-runner.mjs +19 -0
  41. package/src/cli/index.mjs +59 -0
  42. package/src/configuration-resolver.mjs +26 -0
  43. package/src/configuration.mjs +49 -0
  44. package/src/{controller.cjs → controller.mjs} +21 -4
  45. package/src/database/drivers/base.mjs +17 -0
  46. package/src/database/drivers/index.mjs +5 -0
  47. package/src/database/drivers/mysql/connect-connection.mjs +12 -0
  48. package/src/database/drivers/mysql/index.mjs +102 -0
  49. package/src/database/drivers/mysql/options.mjs +17 -0
  50. package/src/database/drivers/mysql/query-parser.mjs +25 -0
  51. package/src/database/drivers/mysql/query.mjs +26 -0
  52. package/src/database/drivers/mysql/sql/create-database.mjs +4 -0
  53. package/src/database/drivers/mysql/sql/create-table.mjs +4 -0
  54. package/src/database/drivers/mysql/sql/delete.mjs +19 -0
  55. package/src/database/drivers/mysql/sql/insert.mjs +29 -0
  56. package/src/database/drivers/mysql/sql/update.mjs +31 -0
  57. package/src/database/drivers/sqlite/options.mjs +17 -0
  58. package/src/database/drivers/sqlite/query-parser.mjs +25 -0
  59. package/src/database/drivers/sqlite/sql/create-database.mjs +4 -0
  60. package/src/database/drivers/sqlite/sql/create-table.mjs +4 -0
  61. package/src/database/drivers/sqlite/sql/delete.mjs +19 -0
  62. package/src/database/drivers/sqlite/sql/insert.mjs +29 -0
  63. package/src/database/drivers/sqlite/sql/update.mjs +31 -0
  64. package/src/database/drivers/sqlite-expo/index.mjs +100 -0
  65. package/src/database/drivers/sqlite-expo/query.mjs +9 -0
  66. package/src/database/handler.mjs +7 -0
  67. package/src/database/index.mjs +15 -0
  68. package/src/database/migration/index.mjs +18 -0
  69. package/src/database/migrator/index.mjs +15 -0
  70. package/src/database/pool/index.mjs +112 -0
  71. package/src/database/query/base.mjs +11 -0
  72. package/src/database/query/create-database-base.mjs +20 -0
  73. package/src/database/query/create-table-base.mjs +69 -0
  74. package/src/database/query/delete-base.mjs +9 -0
  75. package/src/database/query/from-base.mjs +9 -0
  76. package/src/database/query/from-plain.mjs +10 -0
  77. package/src/database/query/from-table.mjs +12 -0
  78. package/src/database/query/index.mjs +144 -0
  79. package/src/database/query/insert-base.mjs +15 -0
  80. package/src/database/query/join-base.mjs +9 -0
  81. package/src/database/query/join-plain.mjs +12 -0
  82. package/src/database/query/order-base.mjs +9 -0
  83. package/src/database/query/order-plain.mjs +21 -0
  84. package/src/database/query/select-base.mjs +9 -0
  85. package/src/database/query/select-plain.mjs +12 -0
  86. package/src/database/query/{select-table-and-column.cjs → select-table-and-column.mjs} +4 -4
  87. package/src/database/query/update-base.mjs +16 -0
  88. package/src/database/query-parser/{from-parser.cjs → from-parser.mjs} +3 -6
  89. package/src/database/query-parser/{joins-parser.cjs → joins-parser.mjs} +3 -6
  90. package/src/database/query-parser/{options.cjs → options.mjs} +13 -2
  91. package/src/database/query-parser/{select-parser.cjs → select-parser.mjs} +7 -6
  92. package/src/database/record/index.mjs +187 -0
  93. package/src/database/record/record-not-found-error.mjs +1 -0
  94. package/src/database/table-data/index.mjs +83 -0
  95. package/src/{error-logger.js → error-logger.mjs} +1 -1
  96. package/src/http-server/client/{index.cjs → index.mjs} +10 -11
  97. package/src/http-server/client/params-to-object.mjs +68 -0
  98. package/src/http-server/client/request-buffer/form-data-part.mjs +42 -0
  99. package/src/http-server/client/request-buffer/header.mjs +7 -0
  100. package/src/http-server/client/request-buffer/index.mjs +229 -0
  101. package/src/http-server/client/request-parser.mjs +47 -0
  102. package/src/http-server/client/{request-runner.cjs → request-runner.mjs} +5 -5
  103. package/src/http-server/client/request.mjs +15 -0
  104. package/src/http-server/client/{response.cjs → response.mjs} +1 -1
  105. package/src/http-server/index.mjs +137 -0
  106. package/src/http-server/server-client.mjs +47 -0
  107. package/src/http-server/worker-handler/index.mjs +79 -0
  108. package/src/http-server/worker-handler/worker-script.mjs +4 -0
  109. package/src/http-server/worker-handler/worker-thread.mjs +65 -0
  110. package/src/{logger.cjs → logger.mjs} +2 -2
  111. package/src/routes/base-route.mjs +34 -0
  112. package/src/routes/{get-route.cjs → get-route.mjs} +5 -3
  113. package/src/routes/index.mjs +9 -0
  114. package/src/routes/{resolver.cjs → resolver.mjs} +17 -9
  115. package/src/routes/{resource-route.cjs → resource-route.mjs} +9 -5
  116. package/src/routes/root-route.mjs +6 -0
  117. package/src/spec/index.mjs +5 -0
  118. package/src/templates/configuration.mjs +17 -0
  119. package/src/templates/generate-migration.mjs +11 -0
  120. package/src/templates/routes.mjs +11 -0
  121. package/src/utils/file-exists.mjs +13 -0
  122. package/bin/velocious +0 -14
  123. package/index.cjs +0 -13
  124. package/spec/dummy/config/databases.example.json +0 -10
  125. package/spec/dummy/config/databases.json +0 -0
  126. package/spec/dummy/config/databases.peakflow.json +0 -11
  127. package/spec/dummy/index.cjs +0 -40
  128. package/spec/dummy/src/models/task.cjs +0 -4
  129. package/spec/dummy/src/routes/tasks/controller.cjs +0 -18
  130. package/src/application.cjs +0 -36
  131. package/src/configuration.cjs +0 -14
  132. package/src/database/connection/drivers/mysql/index.cjs +0 -5
  133. package/src/database/connection/drivers/mysql/options.cjs +0 -7
  134. package/src/database/connection/drivers/mysql/query-parser.cjs +0 -26
  135. package/src/database/connection/index.cjs +0 -2
  136. package/src/database/handler.cjs +0 -5
  137. package/src/database/index.cjs +0 -9
  138. package/src/database/pool/index.cjs +0 -2
  139. package/src/database/query/from-base.cjs +0 -15
  140. package/src/database/query/from-plain.cjs +0 -12
  141. package/src/database/query/from-table.cjs +0 -12
  142. package/src/database/query/index.cjs +0 -59
  143. package/src/database/query/join-base.cjs +0 -15
  144. package/src/database/query/join-plain.cjs +0 -12
  145. package/src/database/query/select-base.cjs +0 -15
  146. package/src/database/query/select-plain.cjs +0 -12
  147. package/src/database/record/index.cjs +0 -5
  148. package/src/http-server/client/request-parser.cjs +0 -92
  149. package/src/http-server/client/request.cjs +0 -25
  150. package/src/http-server/index.cjs +0 -78
  151. package/src/http-server/worker-handler/index.cjs +0 -78
  152. package/src/http-server/worker-handler/socket-handler.cjs +0 -35
  153. package/src/http-server/worker-handler/worker-script.cjs +0 -4
  154. package/src/http-server/worker-handler/worker-thread.cjs +0 -49
  155. package/src/routes/base-route.cjs +0 -25
  156. package/src/routes/index.cjs +0 -9
  157. package/src/routes/root-route.cjs +0 -4
package/index.cjs DELETED
@@ -1,13 +0,0 @@
1
- const Application = require("./src/application.cjs")
2
- const Controller = require("./src/controller.cjs")
3
- const Database = require("./src/database/index.cjs")
4
- const HttpServer = require("./src/http-server/index.cjs")
5
- const Routes = require("./src/routes/index.cjs")
6
-
7
- module.exports = {
8
- Application,
9
- Controller,
10
- Database,
11
- HttpServer,
12
- Routes
13
- }
@@ -1,10 +0,0 @@
1
- {
2
- "default": {
3
- "master": {
4
- "type": "mysql",
5
- "host": "mysql",
6
- "username": "username",
7
- "password": "password"
8
- }
9
- }
10
- }
File without changes
@@ -1,11 +0,0 @@
1
- {
2
- "default": {
3
- "master": {
4
- "type": "mysql",
5
- "host": "mysql",
6
- "username": "peakflow",
7
- "password": "password",
8
- "database": "velocious_test"
9
- }
10
- }
11
- }
@@ -1,40 +0,0 @@
1
- const {Application} = require("../../index.cjs")
2
-
3
- module.exports = class Dummy {
4
- static async run(callback) {
5
- const dummy = new Dummy()
6
-
7
- await dummy.run(callback)
8
- }
9
-
10
- async run(callback) {
11
- await this.start()
12
-
13
- try {
14
- await callback()
15
- } finally {
16
- this.stop()
17
- }
18
- }
19
-
20
- async start() {
21
- this.application = new Application({
22
- databases: {
23
- default: {
24
- host: "mysql",
25
- username: "user",
26
- password: ""
27
- }
28
- },
29
- debug: false,
30
- directory: __dirname,
31
- httpServer: {port: 3006}
32
- })
33
-
34
- await this.application.start()
35
- }
36
-
37
- stop() {
38
- this.application.stop()
39
- }
40
- }
@@ -1,4 +0,0 @@
1
- const Record = require("../../../../src/database/record/index.cjs")
2
-
3
- module.exports = class Task extends Record {
4
- }
@@ -1,18 +0,0 @@
1
- const Controller = require("../../../../../src/controller.cjs")
2
- const {digg} = require("diggerize")
3
- const Task = require("../../models/task.cjs")
4
-
5
- module.exports = class TasksController extends Controller {
6
- index() {
7
- this.viewParams.numbers = [1, 2, 3, 4, 5]
8
- this.render()
9
- }
10
-
11
- async show() {
12
- const taskId = digg(params, "id")
13
- const task = await Task.find(taskId)
14
-
15
- this.viewParams.task = task
16
- this.render()
17
- }
18
- }
@@ -1,36 +0,0 @@
1
- const {digs} = require("diggerize")
2
- const Configuration = require("./configuration.cjs")
3
- const logger = require("./logger.cjs")
4
- const HttpServer = require("./http-server/index.cjs")
5
-
6
- module.exports = class VelociousApplication {
7
- constructor({debug, directory, httpServer}) {
8
- this.configuration = new Configuration({debug, directory})
9
- this.httpServerConfiguration = httpServer ?? {}
10
- }
11
-
12
- async run(callback) {
13
- await this.start()
14
-
15
- try {
16
- await callback()
17
- } finally {
18
- this.stop()
19
- }
20
- }
21
-
22
- async start() {
23
- const {configuration, httpServerConfiguration} = digs(this, "configuration", "httpServerConfiguration")
24
- const port = httpServerConfiguration.port || 3006
25
-
26
- logger(this, `Starting server on port ${port}`)
27
-
28
- this.httpServer = new HttpServer({configuration, port})
29
-
30
- await this.httpServer.start()
31
- }
32
-
33
- stop() {
34
- this.httpServer.stop()
35
- }
36
- }
@@ -1,14 +0,0 @@
1
- const Routes = require("./routes/index.cjs")
2
-
3
- module.exports = class VelociousConfiguration {
4
- constructor({debug, directory}) {
5
- if (!directory) throw new Error("No directory given")
6
-
7
- // Every client need to make their own routes because they probably can't be shared across different worker threads
8
- const {routes} = require(`${directory}/src/config/routes.cjs`)
9
-
10
- this.debug = debug
11
- this.directory = directory
12
- this.routes = routes
13
- }
14
- }
@@ -1,5 +0,0 @@
1
- module.exports = class VelociousDatabaseConnectionDriversMysql {
2
- constructor(args) {
3
- this.args = args
4
- }
5
- }
@@ -1,7 +0,0 @@
1
- const QueryParserOptions = require("../../../query-parser/options.cjs")
2
- const queryParserOptions = new QueryParserOptions({
3
- columnQuote: "`",
4
- tableQuote: "`"
5
- })
6
-
7
- module.exports = queryParserOptions
@@ -1,26 +0,0 @@
1
- const {digs} = require("diggerize")
2
- const FromParser = require("../../../query-parser/from-parser.cjs")
3
- const JoinsParser = require("../../../query-parser/joins-parser.cjs")
4
- const queryParserOptions = require("./options.cjs")
5
- const SelectParser = require("../../../query-parser/select-parser.cjs")
6
-
7
- module.exports = class VelociousDatabaseConnectionDriversMysqlQueryParser {
8
- constructor({pretty, query}) {
9
- if (!query) throw new Error("No query given")
10
-
11
- this.pretty = pretty
12
- this.query = query
13
- }
14
-
15
- toSql() {
16
- const {pretty, query} = digs(this, "pretty", "query")
17
-
18
- let sql = ""
19
-
20
- sql += new SelectParser({pretty, query, queryParserOptions}).toSql()
21
- sql += new FromParser({pretty, query, queryParserOptions}).toSql()
22
- sql += new JoinsParser({pretty, query, queryParserOptions}).toSql()
23
-
24
- return sql
25
- }
26
- }
@@ -1,2 +0,0 @@
1
- module.exports = class VelociousDatabaseConnection {
2
- }
@@ -1,5 +0,0 @@
1
- module.exports = class VelociousDatabaseHandler {
2
- constructor() {
3
- console.log("stub")
4
- }
5
- }
@@ -1,9 +0,0 @@
1
- const Handler = require("./handler.cjs")
2
- const Query = require("./query/index.cjs")
3
- const Record = require("./record/index.cjs")
4
-
5
- module.exports = {
6
- Handler,
7
- Query,
8
- Record
9
- }
@@ -1,2 +0,0 @@
1
- module.exports = class VelociousDatabasePool {
2
- }
@@ -1,15 +0,0 @@
1
- module.exports = class VelociousDatabaseQueryFromBase {
2
- getOptions() {
3
- if (!this._options) throw new Error("Options hasn't been set")
4
-
5
- return this._options
6
- }
7
-
8
- setOptions(options) {
9
- this._options = options
10
- }
11
-
12
- toSql() {
13
- throw new Error("'toSql' wasn't implemented")
14
- }
15
- }
@@ -1,12 +0,0 @@
1
- const FromBase = require("./from-base.cjs")
2
-
3
- module.exports = class VelociousDatabaseQueryFromPlain extends FromBase {
4
- constructor({plain}) {
5
- super()
6
- this.plain = plain
7
- }
8
-
9
- toSql() {
10
- return this.plain
11
- }
12
- }
@@ -1,12 +0,0 @@
1
- const FromBase = require("./from-base.cjs")
2
-
3
- module.exports = class VelociousDatabaseQueryFromTable extends FromBase {
4
- constructor({tableName}) {
5
- super()
6
- this.tableName = tableName
7
- }
8
-
9
- toSql() {
10
- return this.getOptions().quoteTableName(this.tableName)
11
- }
12
- }
@@ -1,59 +0,0 @@
1
- const FromPlain = require("./from-plain.cjs")
2
- const JoinPlain = require("./join-plain.cjs")
3
- const SelectPlain = require("./select-plain.cjs")
4
-
5
- module.exports = class VelociousDatabaseQuery {
6
- constructor({handler}) {
7
- if (!handler) throw new Error("No handler given")
8
-
9
- this._froms = []
10
- this._joins = []
11
- this._orders = []
12
- this._selects = []
13
- }
14
-
15
- getOptions() {
16
- if (!this._options) throw new Error("Options not set")
17
- return this._options
18
- }
19
-
20
- from(from) {
21
- if (typeof from == "string") from = new FromPlain({plain: from})
22
-
23
- this._froms.push(from)
24
- return this
25
- }
26
-
27
- joins(join) {
28
- if (typeof join == "string") join = new JoinPlain({plain: join})
29
-
30
- this._joins.push(join)
31
- return this
32
- }
33
-
34
- order(order) {
35
- if (typeof order == "string") order = new OrderPlain({plain: order})
36
-
37
- this._orders.push(order)
38
- return this
39
- }
40
-
41
- select(select) {
42
- if (Array.isArray(select)) {
43
- for (const selectInArray of select) {
44
- this.select(selectInArray)
45
- }
46
-
47
- return this
48
- }
49
-
50
- if (typeof select == "string") select = new SelectPlain({plain: select})
51
-
52
- this._selects.push(select)
53
- return this
54
- }
55
-
56
- toSql() {
57
- throw new Error("stub")
58
- }
59
- }
@@ -1,15 +0,0 @@
1
- module.exports = class VelociousDatabaseQueryJoinBase {
2
- getOptions() {
3
- if (!this._options) throw new Error("Options hasn't been set")
4
-
5
- return this._options
6
- }
7
-
8
- setOptions(options) {
9
- this._options = options
10
- }
11
-
12
- toSql() {
13
- throw new Error("'toSql' wasn't implemented")
14
- }
15
- }
@@ -1,12 +0,0 @@
1
- const JoinBase = require("./join-base.cjs")
2
-
3
- module.exports = class VelociousDatabaseQueryJoinPlain extends JoinBase {
4
- constructor({plain}) {
5
- super()
6
- this.plain = plain
7
- }
8
-
9
- toSql() {
10
- return this.plain
11
- }
12
- }
@@ -1,15 +0,0 @@
1
- module.exports = class VelociousDatabaseQuerySelectBase {
2
- getOptions() {
3
- if (!this._options) throw new Error("Options hasn't been set")
4
-
5
- return this._options
6
- }
7
-
8
- setOptions(options) {
9
- this._options = options
10
- }
11
-
12
- toSql() {
13
- throw new Error("'toSql' wasn't implemented")
14
- }
15
- }
@@ -1,12 +0,0 @@
1
- const SelectBase = require("./select-base.cjs")
2
-
3
- module.exports = class VelociousDatabaseQuerySelectPlain extends SelectBase {
4
- constructor({plain}) {
5
- super()
6
- this.plain = plain
7
- }
8
-
9
- toSql() {
10
- return this.plain
11
- }
12
- }
@@ -1,5 +0,0 @@
1
- module.exports = class VelociousDatabaseRecord {
2
- static find(recordId) {
3
- throw new Error("stub")
4
- }
5
- }
@@ -1,92 +0,0 @@
1
- const {EventEmitter} = require("events")
2
- const logger = require("../../logger.cjs")
3
-
4
- module.exports = class VelociousHttpServerClientRequestParser {
5
- constructor({configuration}) {
6
- if (!configuration) throw new Error("No configuration given")
7
-
8
- this.configuration = configuration
9
- this.data = []
10
- this.events = new EventEmitter()
11
- this.headers = []
12
- this.headersByName = {}
13
- this.state = "status"
14
- }
15
-
16
- addHeader(name, value) {
17
- logger(this, "addHeader", {name, value})
18
-
19
- this.headers.push({name, value})
20
-
21
- const formattedName = name.toLowerCase().trim()
22
-
23
- this.headersByName[formattedName] = value
24
- }
25
-
26
- feed(data) {
27
- if (this.state == "status" || this.state == "headers") {
28
- for (const char of data) {
29
- this.data.push(char)
30
-
31
- if (char == 10) {
32
- const line = String.fromCharCode.apply(null, this.data)
33
-
34
- this.data = []
35
- this.parse(line)
36
- }
37
- }
38
- }
39
- }
40
-
41
- matchAndRemove(regex) {
42
- const match = this.data.match(regex)
43
-
44
- if (!match) {
45
- return null
46
- }
47
-
48
- this.data = this.data.replace(regex, "")
49
-
50
- return match
51
- }
52
-
53
- parse(line) {
54
- if (this.state == "status") {
55
- this.parseStatusLine(line)
56
- } else if (this.state == "headers") {
57
- this.parseHeader(line)
58
- } else {
59
- throw new Error(`Unknown state: ${this.state}`)
60
- }
61
- }
62
-
63
- parseHeader(line) {
64
- let match
65
-
66
- if (match = line.match(/^(.+): (.+)\r\n/)) {
67
- const name = match[1]
68
- const value = match[2]
69
-
70
- this.addHeader(name, value)
71
- } else if (line == "\r\n") {
72
- if (this.httpMethod.toUpperCase() == "GET") {
73
- this.state = "done"
74
- this.events.emit("done")
75
- } else {
76
- throw new Error(`Unknown HTTP method: ${this.httpMethod}`)
77
- }
78
- }
79
- }
80
-
81
- parseStatusLine(line) {
82
- const match = line.match(/^(GET|POST) (.+?) HTTP\/1\.1\r\n/)
83
-
84
- if (!match) {
85
- throw new Error(`Couldn't match status line from: ${line}`)
86
- }
87
-
88
- this.httpMethod = match[1]
89
- this.path = match[2]
90
- this.state = "headers"
91
- }
92
- }
@@ -1,25 +0,0 @@
1
- const {digg} = require("diggerize")
2
- const RequestParser = require("./request-parser.cjs")
3
-
4
- module.exports = class VelociousHttpServerClientRequest {
5
- constructor({configuration}) {
6
- this.configuration = configuration
7
- this.requestParser = new RequestParser({configuration})
8
- }
9
-
10
- feed(data) {
11
- this.requestParser.feed(data)
12
- }
13
-
14
- httpMethod() {
15
- return digg(this, "requestParser", "httpMethod")
16
- }
17
-
18
- host() {
19
- return digg(this, "requestParser", "headersByName", "host")
20
- }
21
-
22
- path() {
23
- return digg(this, "requestParser", "path")
24
- }
25
- }
@@ -1,78 +0,0 @@
1
- const logger = require("../logger.cjs")
2
- const Net = require("net")
3
- const WorkerHandler = require("./worker-handler/index.cjs")
4
-
5
- module.exports = class VelociousHttpServer {
6
- constructor({configuration, host, maxWorkers, port}) {
7
- this.configuration = configuration
8
- this.host = host
9
- this.port = port
10
- this.clientCount = 0
11
- this.maxWorkers = maxWorkers || 16
12
- this.workerCount = 0
13
- this.workerHandlers = []
14
- }
15
-
16
- async start() {
17
- // We need at least one worker to handle requests
18
- if (this.workerHandlers.length == 0) {
19
- await this.spawnWorker()
20
- }
21
-
22
- this.netServer = new Net.Server()
23
- this.netServer.on("connection", (socket) => this.onConnection(socket))
24
- this.netServer.listen(this.port, () => {
25
- logger(this, `Velocious listening on ${this.host}:${this.port}`)
26
- })
27
- }
28
-
29
- stop() {
30
- return new Promise((resolve) => {
31
- this.netServer?.close(() => resolve())
32
- })
33
- }
34
-
35
- onConnection(socket) {
36
- const clientCount = this.clientCount
37
-
38
- logger(this, `New client ${clientCount}`)
39
-
40
- this.clientCount++
41
-
42
- const workerHandler = this.workerHandlerToUse()
43
-
44
- logger(this, `Gave client ${clientCount} to worker ${workerHandler.workerCount}`)
45
-
46
- workerHandler.addSocketConnection({
47
- socket,
48
- clientCount: this.clientCount
49
- })
50
- }
51
-
52
- async spawnWorker() {
53
- const workerCount = this.workerCount
54
-
55
- this.workerCount++
56
-
57
- const workerHandler = new WorkerHandler({
58
- configuration: this.configuration,
59
- workerCount
60
- })
61
-
62
- await workerHandler.start()
63
- this.workerHandlers.push(workerHandler)
64
- }
65
-
66
- workerHandlerToUse() {
67
- logger(this, `Worker handlers length: ${this.workerHandlers.length}`)
68
-
69
- const randomWorkerNumber = parseInt(Math.random() * this.workerHandlers.length)
70
- const workerHandler = this.workerHandlers[randomWorkerNumber]
71
-
72
- if (!workerHandler) {
73
- throw new Error(`No workerHandler by that number: ${randomWorkerNumber}`)
74
- }
75
-
76
- return workerHandler
77
- }
78
- }
@@ -1,78 +0,0 @@
1
- const {digs} = require("diggerize")
2
- const logger = require("../../logger.cjs")
3
- const SocketHandler = require("./socket-handler.cjs")
4
- const {Worker} = require("worker_threads")
5
-
6
- module.exports = class VelociousHttpServerWorker {
7
- constructor({configuration, workerCount}) {
8
- this.configuration = configuration
9
- this.socketHandlers = {}
10
- this.workerCount = workerCount
11
- }
12
-
13
- async start() {
14
- return new Promise((resolve) => {
15
- const {debug, directory} = digs(this.configuration, "debug", "directory")
16
-
17
- this.onStartCallback = resolve
18
- this.worker = new Worker("./src/http-server/worker-handler/worker-script.cjs", {
19
- workerData: {
20
- debug,
21
- directory,
22
- workerCount: this.workerCount
23
- }
24
- })
25
- this.worker.on("error", (error) => this.onWorkerError(error))
26
- this.worker.on("exit", (code) => this.onWorkerExit(code))
27
- this.worker.on("message", (message) => this.onWorkerMessage(message))
28
- })
29
- }
30
-
31
- addSocketConnection({socket, clientCount}) {
32
- socket.on("end", () => {
33
- logger(this, `Removing ${clientCount} from socketHandlers`)
34
- delete this.socketHandlers[clientCount]
35
- })
36
-
37
- const socketHandler = new SocketHandler({
38
- configuration: this.configuration,
39
- socket,
40
- clientCount,
41
- worker: this.worker
42
- })
43
-
44
- this.socketHandlers[clientCount] = socketHandler
45
- this.worker.postMessage({command: "newClient", clientCount})
46
- }
47
-
48
- onWorkerError(error) {
49
- throw new Error(`Worker error: ${error}`)
50
- }
51
-
52
- onWorkerExit(code) {
53
- if (code !== 0) {
54
- throw new Error(`Client worker stopped with exit code ${code}`)
55
- }
56
- }
57
-
58
- onWorkerMessage(data) {
59
- logger(this, `Worker message`, data)
60
-
61
- const {command} = digs(data, "command")
62
-
63
- if (command == "started") {
64
- this.onStartCallback()
65
- this.onStartCallback = null
66
- } else if (command == "clientOutput") {
67
- logger(this, "CLIENT OUTPUT", data)
68
-
69
- const {clientCount, output} = digs(data, "clientCount", "output")
70
-
71
- logger(this, "CLIENT OUTPUT", data)
72
-
73
- this.socketHandlers[clientCount].send(output)
74
- } else {
75
- throw new Error(`Unknown command: ${command}`)
76
- }
77
- }
78
- }