velocious 1.0.24 → 1.0.25

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/bin/velocious.js CHANGED
@@ -3,7 +3,23 @@
3
3
  import Cli from "../src/cli/index.js"
4
4
 
5
5
  const processArgs = process.argv.slice(2)
6
- const cli = new Cli({processArgs})
6
+ const parsedProcessArgs = {}
7
+
8
+ for (let i = 0; i < processArgs.length; i++) {
9
+ const processArg = processArgs[i]
10
+ const singleLetterArgMatch = processArg.match(/^-([a-z])$/)
11
+ const multiLetterArgMatch = processArg.match(/^--([a-z]+)$/)
12
+
13
+ if (singleLetterArgMatch) {
14
+ parsedProcessArgs[singleLetterArgMatch[1]] = processArgs[i + 1]
15
+ i++
16
+ } else if (multiLetterArgMatch) {
17
+ parsedProcessArgs[multiLetterArgMatch[1]] = processArgs[i + 1]
18
+ i++
19
+ }
20
+ }
21
+
22
+ const cli = new Cli({parsedProcessArgs, processArgs})
7
23
 
8
24
  await cli.execute()
9
25
 
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "velocious": "bin/velocious.js"
4
4
  },
5
5
  "name": "velocious",
6
- "version": "1.0.24",
6
+ "version": "1.0.25",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "test": "jasmine",
@@ -7,7 +7,20 @@ describe("HttpServer", () => {
7
7
  const response = await fetch("http://localhost:3006/tasks")
8
8
  const text = await response.text()
9
9
 
10
+ expect(response.status).toEqual(200)
11
+ expect(response.statusText).toEqual("OK")
10
12
  expect(text).toEqual("1, 2, 3, 4, 5\n")
11
13
  })
12
14
  })
15
+
16
+ it("returns a 404 error when a collection action isnt found", async () => {
17
+ await Dummy.run(async () => {
18
+ const response = await fetch("http://localhost:3006/tasks/doesnt-exist")
19
+ const text = await response.text()
20
+
21
+ expect(response.status).toEqual(404)
22
+ expect(response.statusText).toEqual("Not Found")
23
+ expect(text).toEqual("Not found!\n")
24
+ })
25
+ })
13
26
  })
@@ -55,7 +55,7 @@ export default class VelociousApplication {
55
55
  }
56
56
 
57
57
  onHttpServerClose = () => {
58
- console.log("HTTP server closed")
58
+ logger(this, "HTTP server closed")
59
59
 
60
60
  if (this.waitResolve) {
61
61
  this.waitResolve()
@@ -9,8 +9,9 @@ export default class VelociousCliCommandsServer extends BaseCommand{
9
9
 
10
10
  await this.databaseConnection.connect()
11
11
 
12
- const host = "0.0.0.0"
13
- const port = 3006
12
+ const {parsedProcessArgs} = this.args
13
+ const host = parsedProcessArgs.h || parsedProcessArgs.host || "127.0.0.1"
14
+ const port = parsedProcessArgs.p || parsedProcessArgs.port || 3006
14
15
  const application = new Application({
15
16
  configuration: this.configuration,
16
17
  httpServer: {
package/src/controller.js CHANGED
@@ -1,9 +1,16 @@
1
1
  import {digs} from "diggerize"
2
2
  import ejs from "ejs"
3
3
  import * as inflection from "inflection"
4
+ import logger from "./logger.js"
4
5
  import restArgsError from "./utils/rest-args-error.js"
5
6
 
6
7
  export default class VelociousController {
8
+ static beforeAction(methodName) {
9
+ if (!this._beforeActions) this._beforeActions = []
10
+
11
+ this._beforeActions.push(methodName)
12
+ }
13
+
7
14
  constructor({action, configuration, controller, params, request, response, viewPath}) {
8
15
  if (!action) throw new Error("No action given")
9
16
  if (!configuration) throw new Error("No configuration given")
@@ -23,6 +30,36 @@ export default class VelociousController {
23
30
  this._viewPath = viewPath
24
31
  }
25
32
 
33
+ async _runBeforeCallbacks() {
34
+ await logger(this, "_runBeforeCallbacks")
35
+
36
+ let currentControllerClass = this.constructor
37
+
38
+ while (currentControllerClass) {
39
+ await logger(this, `Running callbacks for ${currentControllerClass.name}`)
40
+
41
+ const beforeActions = currentControllerClass._beforeActions
42
+
43
+ if (beforeActions) {
44
+ for (const beforeActionName of beforeActions) {
45
+ const beforeAction = currentControllerClass.prototype[beforeActionName]
46
+
47
+ if (!beforeAction) throw new Error(`No such before action: ${beforeActionName}`)
48
+
49
+ const boundBeforeAction = beforeAction.bind(this)
50
+
51
+ await boundBeforeAction()
52
+ }
53
+ }
54
+
55
+ currentControllerClass = Object.getPrototypeOf(currentControllerClass)
56
+
57
+ if (!currentControllerClass?.name?.endsWith("Controller")) break
58
+ }
59
+
60
+ await logger(this, "After runBeforeCallbacks")
61
+ }
62
+
26
63
  params = () => this._params
27
64
 
28
65
  render({json, status, ...restArgs} = {}) {
@@ -40,6 +40,7 @@ export default class VeoliciousHttpServerClient {
40
40
  this.currentRequest = new Request({
41
41
  configuration: this.configuration
42
42
  })
43
+
43
44
  this.currentRequest.requestParser.events.on("done", this.executeCurrentRequest)
44
45
  this.currentRequest.feed(data)
45
46
  this.state = "requestStarted"
@@ -152,7 +152,7 @@ export default class RequestBuffer {
152
152
 
153
153
  this.events.emit("header", header)
154
154
  } else if (line == "\r\n") {
155
- if (this.httpMethod.toUpperCase() == "GET") {
155
+ if (this.httpMethod.toUpperCase() == "GET" || this.httpMethod.toUpperCase() == "OPTIONS") {
156
156
  this.completeRequest()
157
157
  } else if (this.httpMethod.toUpperCase() == "POST") {
158
158
  this.readingBody = true
@@ -176,7 +176,7 @@ export default class RequestBuffer {
176
176
  }
177
177
 
178
178
  parseStatusLine(line) {
179
- const match = line.match(/^(GET|POST) (.+?) HTTP\/1\.1\r\n/)
179
+ const match = line.match(/^(GET|OPTIONS|POST) (.+?) HTTP\/1\.1\r\n/)
180
180
 
181
181
  if (!match) {
182
182
  throw new Error(`Couldn't match status line from: ${line}`)
@@ -185,6 +185,8 @@ export default class RequestBuffer {
185
185
  this.httpMethod = match[1]
186
186
  this.path = match[2]
187
187
  this.setState("headers")
188
+
189
+ logger(this, () => ["Parsed status line", {httpMethod: this.httpMethod, path: this.path}])
188
190
  }
189
191
 
190
192
  postRequestDone() {
@@ -194,7 +196,7 @@ export default class RequestBuffer {
194
196
  }
195
197
 
196
198
  setState(newState) {
197
- logger(this, `Changing state from ${this.state} to ${newState}`)
199
+ logger(this, () => [`Changing state from ${this.state} to ${newState}`])
198
200
 
199
201
  this.state = newState
200
202
  }
@@ -34,9 +34,46 @@ export default class VelociousHttpServerClientRequestParser {
34
34
  feed = (data) => this.requestBuffer.feed(data)
35
35
  getHeader = (name) => this.requestBuffer.getHeader(name)?.value
36
36
  getHttpMethod = () => digg(this, "requestBuffer", "httpMethod")
37
- getHost = () => this.requestBuffer.getHeader("host")?.value
37
+
38
+ _getHostMatch = () => {
39
+ const rawHost = this.requestBuffer.getHeader("origin")?.value
40
+
41
+ if (!rawHost) return null
42
+
43
+ const match = rawHost.match(/^(.+):\/\/(.+)(|:(\d+))/)
44
+
45
+ if (!match) throw new Error(`Couldn't match host: ${rawHost}`)
46
+
47
+ return {
48
+ protocol: match[1],
49
+ host: match[2],
50
+ port: match[4]
51
+ }
52
+ }
53
+
54
+ getHost() {
55
+ const rawHostSplit = this.requestBuffer.getHeader("host")?.value?.split(":")
56
+
57
+ if (rawHostSplit && rawHostSplit[0]) return rawHostSplit[0]
58
+ }
59
+
38
60
  getPath = () => digg(this, "requestBuffer", "path")
39
61
 
62
+ getPort() {
63
+ const rawHostSplit = this.requestBuffer.getHeader("host")?.value?.split(":")
64
+ const httpMethod = this.getHttpMethod()
65
+
66
+ if (rawHostSplit && rawHostSplit[1]) {
67
+ return parseInt(rawHostSplit[1])
68
+ } else if (httpMethod == "http") {
69
+ return 80
70
+ } else if (httpMethod == "https") {
71
+ return 443
72
+ }
73
+ }
74
+
75
+ getProtocol = () => this._getHostMatch()?.protocol
76
+
40
77
  requestDone = () => {
41
78
  const incorporator = new Incorporator({objects: [this.params, this.requestBuffer.params]})
42
79
 
@@ -19,17 +19,27 @@ export default class VelociousHttpServerClientRequestRunner {
19
19
  getState = () => this.state
20
20
 
21
21
  async run() {
22
- if (!this.request) throw new Error("No request?")
22
+ const {configuration, request, response} = this
23
23
 
24
- logger(this, "Run request")
24
+ if (!request) throw new Error("No request?")
25
25
 
26
- const routesResolver = new RoutesResolver({
27
- configuration: this.configuration,
28
- request: this.request,
29
- response: this.response
30
- })
26
+ try {
27
+ if (request.httpMethod() == "OPTIONS" && request.header("sec-fetch-mode") == "cors") {
28
+ await logger(this, () => ["Run CORS", {httpMethod: request.httpMethod(), secFetchMode: request.header("sec-fetch-mode")}])
29
+ await configuration.cors({request, response})
30
+ } else {
31
+ await logger(this, "Run request")
32
+ const routesResolver = new RoutesResolver({configuration, request, response})
33
+
34
+ await routesResolver.resolve()
35
+ }
36
+ } catch (error) {
37
+ await logger(this, `Error while running request: ${error.message}`)
38
+
39
+ response.setStatus(500)
40
+ response.setErrorBody(error)
41
+ }
31
42
 
32
- await routesResolver.resolve()
33
43
  this.state = "done"
34
44
  this.events.emit("done", this)
35
45
  }
@@ -7,9 +7,31 @@ export default class VelociousHttpServerClientRequest {
7
7
  this.requestParser = new RequestParser({configuration})
8
8
  }
9
9
 
10
+ baseURL = () => `${this.protocol()}://${this.hostWithPort()}`
10
11
  feed = (data) => this.requestParser.feed(data)
12
+ header = (headerName) => this.requestParser.requestBuffer.getHeader(headerName)?.value
11
13
  httpMethod = () => this.requestParser.getHttpMethod()
12
14
  host = () => this.requestParser.getHost()
15
+
16
+ hostWithPort = () => {
17
+ const port = this.port()
18
+ const protocol = this.protocol()
19
+ let hostWithPort = `${this.host()}`
20
+
21
+ if (port == 80 && protocol == "http") {
22
+ // Do nothing
23
+ } else if (port == 443 && protocol == "https") {
24
+ // Do nothing
25
+ } else if (port) {
26
+ hostWithPort += `:${port}`
27
+ }
28
+
29
+ return hostWithPort
30
+ }
31
+
32
+ origin = () => this.header("origin")
13
33
  path = () => this.requestParser.getPath()
14
34
  params = () => digg(this, "requestParser", "params")
35
+ port = () => this.requestParser.getPort()
36
+ protocol = () => this.requestParser.getProtocol()
15
37
  }
@@ -15,7 +15,7 @@ export default class VelociousHttpServerClientResponse {
15
15
  }
16
16
 
17
17
  getBody() {
18
- if (this.body) {
18
+ if (this.body !== undefined) {
19
19
  return this.body
20
20
  }
21
21
 
@@ -34,10 +34,21 @@ export default class VelociousHttpServerClientResponse {
34
34
  this.body = value
35
35
  }
36
36
 
37
+ setErrorBody(error) {
38
+ this.body = `${error.message}\n\n${error.stack}`
39
+ this.addHeader("Content-Type", "text/plain")
40
+ }
41
+
37
42
  setStatus(status) {
38
- if (status == "not-found" || status == 404) {
43
+ if (status == "success" || status == 200) {
44
+ this.statusCode = 200
45
+ this.statusMessage = "OK"
46
+ } else if (status == "not-found" || status == 404) {
39
47
  this.statusCode = 404
40
48
  this.statusMessage = "Not Found"
49
+ } else if (status == "internal-server-error" || status == 500) {
50
+ this.statusCode = 500
51
+ this.statusMessage = "Internal server error"
41
52
  } else {
42
53
  throw new Error(`Unhandled status: ${status}`)
43
54
  }
@@ -25,7 +25,7 @@ export default class ServerClient {
25
25
  }
26
26
 
27
27
  onSocketData = (chunk) => {
28
- logger(this, `Socket ${this.clientCount}: ${chunk}`)
28
+ logger(this, () => [`Socket ${this.clientCount}: ${chunk}`])
29
29
 
30
30
  this.worker.postMessage({
31
31
  command: "clientWrite",
@@ -54,6 +54,8 @@ export default class VelociousHttpServerWorker {
54
54
  onWorkerExit = (code) => {
55
55
  if (code !== 0) {
56
56
  throw new Error(`Client worker stopped with exit code ${code}`)
57
+ } else {
58
+ logger(this, () => [`Client worker stopped with exit code ${code}`])
57
59
  }
58
60
  }
59
61
 
@@ -66,12 +68,10 @@ export default class VelociousHttpServerWorker {
66
68
  this.onStartCallback()
67
69
  this.onStartCallback = null
68
70
  } else if (command == "clientOutput") {
69
- logger(this, "CLIENT OUTPUT", data)
71
+ logger(this, () => ["CLIENT OUTPUT", data])
70
72
 
71
73
  const {clientCount, output} = digs(data, "clientCount", "output")
72
74
 
73
- logger(this, "CLIENT OUTPUT", data)
74
-
75
75
  this.clients[clientCount].send(output)
76
76
  } else {
77
77
  throw new Error(`Unknown command: ${command}`)
@@ -38,10 +38,10 @@ export default class VelociousHttpServerWorkerHandlerWorkerThread {
38
38
  }
39
39
  }
40
40
 
41
- onCommand = (data) => {
42
- logger(this, `Worker ${this.workerCount} received command`, data)
41
+ onCommand = async (data) => {
42
+ await logger(this, () => [`Worker ${this.workerCount} received command`, data])
43
43
 
44
- const {command} = data
44
+ const command = data.command
45
45
 
46
46
  if (command == "newClient") {
47
47
  const {clientCount} = digs(data, "clientCount")
@@ -56,9 +56,13 @@ export default class VelociousHttpServerWorkerHandlerWorkerThread {
56
56
 
57
57
  this.clients[clientCount] = client
58
58
  } else if (command == "clientWrite") {
59
+ await logger(this, "Looking up client")
60
+
59
61
  const {chunk, clientCount} = digs(data, "chunk", "clientCount")
60
62
  const client = digg(this.clients, clientCount)
61
63
 
64
+ await logger(this, `Sending to client ${clientCount}`)
65
+
62
66
  client.onWrite(chunk)
63
67
  } else {
64
68
  throw new Error(`Unknown command: ${command}`)
package/src/logger.js CHANGED
@@ -1,13 +1,51 @@
1
- export default function log(object, ...messages) {
2
- if (!object.configuration) console.error(`No configuration on ${object.constructor.name}`)
1
+ import Configuration from "./configuration.js"
3
2
 
4
- if (object.configuration?.debug) {
5
- if (!object.constructor.name) {
6
- throw new Error(`No constructor name for object`)
7
- }
3
+ function consoleLog(message) {
4
+ return new Promise((resolve) => {
5
+ process.stdout.write(message, "utf8", resolve)
6
+ })
7
+ }
8
+
9
+ export default async function log(object, ...messages) {
10
+ let configuration
11
+
12
+ if (object.configuration) {
13
+ configuration = object.configuration
14
+ } else {
15
+ configuration = Configuration.current()
16
+ }
17
+
18
+ if (configuration?.debug) {
19
+ try {
20
+ if (!object.constructor.name) {
21
+ throw new Error(`No constructor name for object`)
22
+ }
8
23
 
9
- const className = object.constructor.name
24
+ const className = object.constructor.name
10
25
 
11
- console.log(className, ...messages)
26
+ if (messages.length === 1 && typeof messages[0] == "function") {
27
+ messages = messages[0]()
28
+ }
29
+
30
+ let message = ""
31
+
32
+ for (const messagePartIndex in messages) {
33
+ const messagePart = messages[messagePartIndex]
34
+
35
+ if (messagePartIndex > 0) {
36
+ message += " "
37
+ }
38
+
39
+ if (typeof messagePart == "object") {
40
+ message += JSON.stringify(messagePart)
41
+ } else {
42
+ message += messagePart
43
+ }
44
+ }
45
+
46
+ await consoleLog(`${className} ${message}\n`)
47
+ } catch (error) {
48
+ console.error(`ERROR ${error.message}`)
49
+ }
12
50
  }
13
51
  }
@@ -10,7 +10,7 @@ export default class VelociousRouteGetRoute extends BaseRoute {
10
10
  this.regExp = new RegExp(`^(${escapeStringRegexp(name)})(.*)$`)
11
11
  }
12
12
 
13
- matchWithPath(path) {
13
+ matchWithPath({path}) {
14
14
  if (path.match(this.regExp)) {
15
15
  const [_beginnigSlash, _matchedName, restPath] = match
16
16
 
@@ -57,6 +57,7 @@ export default class VelociousRoutesResolver {
57
57
  }
58
58
 
59
59
  await this.configuration.getDatabasePool().withConnection(async () => {
60
+ await controllerInstance._runBeforeCallbacks()
60
61
  await controllerInstance[action]()
61
62
  })
62
63
  }
@@ -20,7 +20,7 @@ export default class VelociousRouteResourceRoute extends BaseRoute {
20
20
  let subRoutesMatchesRestPath = false
21
21
 
22
22
  for (const route of this.routes) {
23
- if (route.matchWithPath(restPath)) {
23
+ if (route.matchWithPath({path: restPath})) {
24
24
  subRoutesMatchesRestPath = true
25
25
  }
26
26
  }