velocious 1.0.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 (60) hide show
  1. package/.github/dependabot.yml +9 -0
  2. package/bin/velocious +14 -0
  3. package/index.cjs +11 -0
  4. package/package.json +29 -0
  5. package/peak_flow.yml +17 -0
  6. package/spec/database/connection/drivers/mysql/query-parser-spec.cjs +34 -0
  7. package/spec/dummy/config/databases.example.json +10 -0
  8. package/spec/dummy/config/databases.json +0 -0
  9. package/spec/dummy/config/databases.peakflow.json +11 -0
  10. package/spec/dummy/index.cjs +40 -0
  11. package/spec/dummy/src/models/task.cjs +4 -0
  12. package/spec/dummy/src/routes/tasks/controller.cjs +18 -0
  13. package/spec/dummy/src/routes/tasks/index.ejs +1 -0
  14. package/spec/dummy/src/routes/tasks/show.ejs +0 -0
  15. package/spec/dummy/src/routes.cjs +10 -0
  16. package/spec/http-server/client-spec.cjs +35 -0
  17. package/spec/http-server/get-spec.cjs +13 -0
  18. package/spec/support/jasmine.json +11 -0
  19. package/src/application.cjs +36 -0
  20. package/src/configuration.cjs +14 -0
  21. package/src/controller.cjs +50 -0
  22. package/src/database/connection/drivers/mysql/index.cjs +5 -0
  23. package/src/database/connection/drivers/mysql/options.cjs +7 -0
  24. package/src/database/connection/drivers/mysql/query-parser.cjs +26 -0
  25. package/src/database/connection/index.cjs +2 -0
  26. package/src/database/handler.cjs +5 -0
  27. package/src/database/index.cjs +9 -0
  28. package/src/database/pool/index.cjs +2 -0
  29. package/src/database/query/from-base.cjs +15 -0
  30. package/src/database/query/from-plain.cjs +12 -0
  31. package/src/database/query/from-table.cjs +12 -0
  32. package/src/database/query/index.cjs +59 -0
  33. package/src/database/query/join-base.cjs +15 -0
  34. package/src/database/query/join-plain.cjs +12 -0
  35. package/src/database/query/select-base.cjs +15 -0
  36. package/src/database/query/select-plain.cjs +12 -0
  37. package/src/database/query/select-table-and-column.cjs +21 -0
  38. package/src/database/query-parser/from-parser.cjs +35 -0
  39. package/src/database/query-parser/joins-parser.cjs +33 -0
  40. package/src/database/query-parser/options.cjs +20 -0
  41. package/src/database/query-parser/select-parser.cjs +42 -0
  42. package/src/database/record/index.cjs +5 -0
  43. package/src/error-logger.js +18 -0
  44. package/src/http-server/client/index.cjs +75 -0
  45. package/src/http-server/client/request-parser.cjs +92 -0
  46. package/src/http-server/client/request-runner.cjs +32 -0
  47. package/src/http-server/client/request.cjs +25 -0
  48. package/src/http-server/client/response.cjs +28 -0
  49. package/src/http-server/index.cjs +78 -0
  50. package/src/http-server/worker-handler/index.cjs +78 -0
  51. package/src/http-server/worker-handler/socket-handler.cjs +35 -0
  52. package/src/http-server/worker-handler/worker-script.cjs +4 -0
  53. package/src/http-server/worker-handler/worker-thread.cjs +49 -0
  54. package/src/logger.cjs +11 -0
  55. package/src/routes/base-route.cjs +25 -0
  56. package/src/routes/get-route.cjs +18 -0
  57. package/src/routes/index.cjs +9 -0
  58. package/src/routes/resolver.cjs +61 -0
  59. package/src/routes/resource-route.cjs +39 -0
  60. package/src/routes/root-route.cjs +4 -0
@@ -0,0 +1,25 @@
1
+ module.exports = class VelociousBaseRoute {
2
+ routes = []
3
+
4
+ get(name, args) {
5
+ const GetRoute = require("./get-route.cjs")
6
+ const route = new GetRoute({name, args})
7
+
8
+ this.routes.push(route)
9
+ }
10
+
11
+ matchWithPath(_path) {
12
+ throw new Error(`No 'matchWithPath' implemented on ${this.constructor.name}`)
13
+ }
14
+
15
+ resources(name, callback) {
16
+ const ResourceRoute = require("./resource-route.cjs")
17
+ const route = new ResourceRoute({name})
18
+
19
+ this.routes.push(route)
20
+
21
+ if (callback) {
22
+ callback(route)
23
+ }
24
+ }
25
+ }
@@ -0,0 +1,18 @@
1
+ const BaseRoute = require("./base-route.cjs")
2
+ const escapeStringRegexp = require("escape-string-regexp")
3
+
4
+ module.exports = class VelociousRouteGetRoute extends BaseRoute {
5
+ constructor({name}) {
6
+ super()
7
+ this.name = name
8
+ this.regExp = new RegExp(`^(${escapeStringRegexp(name)})(.*)$`)
9
+ }
10
+
11
+ matchWithPath(path) {
12
+ if (path.match(this.regExp)) {
13
+ const [_beginnigSlash, _matchedName, restPath] = match
14
+
15
+ return {restPath}
16
+ }
17
+ }
18
+ }
@@ -0,0 +1,9 @@
1
+ const RootRoute = require("./root-route.cjs")
2
+
3
+ module.exports = class VelociousRoutes {
4
+ rootRoute = new RootRoute()
5
+
6
+ draw(callback) {
7
+ callback(this.rootRoute)
8
+ }
9
+ }
@@ -0,0 +1,61 @@
1
+ const {digg, digs} = require("diggerize")
2
+
3
+ module.exports = class VelociousRoutesResolver {
4
+ constructor({configuration, request, response, routes}) {
5
+ if (!configuration) throw new Error("No configuration given")
6
+ if (!request) throw new Error("No request given")
7
+ if (!response) throw new Error("No response given")
8
+
9
+ this.configuration = configuration
10
+ this.params = {}
11
+ this.request = request
12
+ this.response = response
13
+ }
14
+
15
+ resolve() {
16
+ let currentRoute = digg(this, "configuration", "routes", "rootRoute")
17
+ let currentPath = this.request.path()
18
+
19
+ const matchResult = this.matchPathWithRoutes(currentRoute, currentPath)
20
+
21
+ if (!matchResult) throw new Error(`Couldn't match a route with the given path: ${currentPath}`)
22
+
23
+ if (this.params.action && this.params.controller) {
24
+ const controllerPath = `${digg(this, "configuration", "directory")}/src/routes/${digg(this, "params", "controller")}/controller.cjs`
25
+ const controllerClass = require(controllerPath)
26
+ const controllerInstance = new controllerClass({
27
+ configuration: this.configuration,
28
+ params: this.params,
29
+ request: this.request,
30
+ response: this.response
31
+ })
32
+
33
+ controllerInstance[this.params.action]()
34
+
35
+ return
36
+ }
37
+
38
+ throw new Error(`Matched the route but didn't know what to do with it: ${currentPath}`)
39
+ }
40
+
41
+ matchPathWithRoutes(route, path) {
42
+ const pathWithoutSlash = path.replace(/^\//, "")
43
+
44
+ for (const subRoute of route.routes) {
45
+ const matchResult = subRoute.matchWithPath({
46
+ params: this.params,
47
+ path: pathWithoutSlash
48
+ })
49
+
50
+ if (!matchResult) continue
51
+
52
+ const {restPath} = digs(matchResult, "restPath")
53
+
54
+ if (restPath) {
55
+ return this.matchPathWithRoutes(subRoute, restPath)
56
+ }
57
+
58
+ return matchResult
59
+ }
60
+ }
61
+ }
@@ -0,0 +1,39 @@
1
+ const BaseRoute = require("./base-route.cjs")
2
+ const escapeStringRegexp = require("escape-string-regexp")
3
+
4
+ module.exports = class VelociousRouteResourceRoute extends BaseRoute {
5
+ constructor({name}) {
6
+ super()
7
+ this.name = name
8
+ this.regExp = new RegExp(`^(${escapeStringRegexp(name)})(.*)$`)
9
+ }
10
+
11
+ matchWithPath({params, path}) {
12
+ const match = path.match(this.regExp)
13
+
14
+ if (match) {
15
+ const [_beginnigSlash, _matchedName, restPath] = match
16
+
17
+ let action = "index"
18
+ let subRoutesMatchesRestPath = false
19
+
20
+ for (const route of this.routes) {
21
+ if (route.matchWithPath(restPath)) {
22
+ subRoutesMatchesRestPath = true
23
+ }
24
+ }
25
+
26
+ if (!subRoutesMatchesRestPath) {
27
+ if (restPath.match(/\/(.+)/)) {
28
+ // TODO: This should change the action to "show" and set the "resource_name_id" in params.
29
+ action = "show"
30
+ }
31
+ }
32
+
33
+ params.action = action
34
+ params.controller = this.name
35
+
36
+ return {restPath}
37
+ }
38
+ }
39
+ }
@@ -0,0 +1,4 @@
1
+ const BaseRoute = require("./base-route.cjs")
2
+
3
+ module.exports = class VelociousRootRoute extends BaseRoute {
4
+ }