velocious 1.0.41 → 1.0.42

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.41",
6
+ "version": "1.0.42",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "test": "jasmine",
@@ -28,35 +28,7 @@ afterEach(async ({testArgs}) => {
28
28
  }
29
29
 
30
30
  if (truncate) {
31
- await db.withDisabledForeignKeys(async () => {
32
- let tries = 0
33
-
34
- while(true) {
35
- tries++
36
-
37
- const tables = await db.getTables()
38
- const truncateErrors = []
39
-
40
- for (const table of tables) {
41
- if (table.getName() != "schema_migrations") {
42
- try {
43
- await table.truncate({cascade: true})
44
- } catch (error) {
45
- console.error(error)
46
- truncateErrors.push(error)
47
- }
48
- }
49
- }
50
-
51
- if (truncateErrors.length == 0) {
52
- break
53
- } else if (tries <= 5) {
54
- // Retry
55
- } else {
56
- throw truncateErrors[0]
57
- }
58
- }
59
- })
31
+ await db.truncateAllTables()
60
32
  }
61
33
  }
62
34
  })
@@ -77,9 +77,7 @@ export default class VelociousConfiguration {
77
77
  return this._directory
78
78
  }
79
79
 
80
- getEnvironment() {
81
- return digg(this, "_environment")
82
- }
80
+ getEnvironment() { return digg(this, "_environment") }
83
81
 
84
82
  getLocaleFallbacks = () => this.localeFallbacks
85
83
  setLocaleFallbacks(newLocaleFallbacks) {
package/src/controller.js CHANGED
@@ -32,17 +32,10 @@ export default class VelociousController {
32
32
  this._viewPath = viewPath
33
33
  }
34
34
 
35
- getAction() {
36
- return this._action
37
- }
38
-
39
- getParams() {
40
- return this._params
41
- }
42
-
43
- getRequest() {
44
- return this._request
45
- }
35
+ getAction() { return this._action }
36
+ getConfiguration() { return this._configuration }
37
+ getParams() { return this._params }
38
+ getRequest() { return this._request }
46
39
 
47
40
  async _runBeforeCallbacks() {
48
41
  await this.logger.debug("_runBeforeCallbacks")
@@ -74,7 +67,7 @@ export default class VelociousController {
74
67
  await this.logger.debug("After runBeforeCallbacks")
75
68
  }
76
69
 
77
- params = () => this._params
70
+ params() { return this._params }
78
71
 
79
72
  render({json, status, ...restArgs} = {}) {
80
73
  restArgsError(restArgs)
@@ -120,11 +113,6 @@ export default class VelociousController {
120
113
  throw new Error("renderText stub")
121
114
  }
122
115
 
123
- request() {
124
- return this._request
125
- }
126
-
127
- response() {
128
- return this._response
129
- }
116
+ request() { return this._request }
117
+ response() { return this._response }
130
118
  }
@@ -211,6 +211,38 @@ export default class VelociousDatabaseDriversBase {
211
211
  await this.query(`ROLLBACK TO SAVEPOINT ${savePointName}`)
212
212
  }
213
213
 
214
+ async truncateAllTables() {
215
+ await this.withDisabledForeignKeys(async () => {
216
+ let tries = 0
217
+
218
+ while(tries <= 5) {
219
+ tries++
220
+
221
+ const tables = await this.getTables()
222
+ const truncateErrors = []
223
+
224
+ for (const table of tables) {
225
+ if (table.getName() != "schema_migrations") {
226
+ try {
227
+ await table.truncate({cascade: true})
228
+ } catch (error) {
229
+ console.error(error)
230
+ truncateErrors.push(error)
231
+ }
232
+ }
233
+ }
234
+
235
+ if (truncateErrors.length == 0) {
236
+ break
237
+ } else if (tries <= 5) {
238
+ // Retry
239
+ } else {
240
+ throw truncateErrors[0]
241
+ }
242
+ }
243
+ })
244
+ }
245
+
214
246
  async update(...args) {
215
247
  const sql = this.updateSql(...args)
216
248
 
@@ -166,12 +166,16 @@ export default class RequestBuffer {
166
166
  this.multiPartyFormData = true
167
167
  this.setState("multi-part-form-data")
168
168
  } else {
169
- if (!this.contentLength) throw new Error("Content length hasn't been set")
170
-
171
- this.postBodyBuffer = new ArrayBuffer(this.contentLength)
172
- this.postBodyChars = new Uint8Array(this.postBodyBuffer)
169
+ if (this.contentLength === 0) {
170
+ this.completeRequest()
171
+ } else if (!this.contentLength) {
172
+ throw new Error("Content length hasn't been set")
173
+ } else {
174
+ this.postBodyBuffer = new ArrayBuffer(this.contentLength)
175
+ this.postBodyChars = new Uint8Array(this.postBodyBuffer)
173
176
 
174
- this.setState("post-body")
177
+ this.setState("post-body")
178
+ }
175
179
  }
176
180
  } else {
177
181
  throw new Error(`Unknown HTTP method: ${this.httpMethod}`)
@@ -7,13 +7,13 @@ export default class VelociousHttpServerClientRequest {
7
7
  this.requestParser = new RequestParser({configuration})
8
8
  }
9
9
 
10
- baseURL = () => `${this.protocol()}://${this.hostWithPort()}`
11
- feed = (data) => this.requestParser.feed(data)
12
- header = (headerName) => this.requestParser.requestBuffer.getHeader(headerName)?.value
13
- httpMethod = () => this.requestParser.getHttpMethod()
14
- host = () => this.requestParser.getHost()
10
+ baseURL() { return `${this.protocol()}://${this.hostWithPort()}` }
11
+ feed(data) { return this.requestParser.feed(data) }
12
+ header(headerName) { return this.requestParser.requestBuffer.getHeader(headerName)?.value }
13
+ httpMethod() { return this.requestParser.getHttpMethod() }
14
+ host() { return this.requestParser.getHost() }
15
15
 
16
- hostWithPort = () => {
16
+ hostWithPort() {
17
17
  const port = this.port()
18
18
  const protocol = this.protocol()
19
19
  let hostWithPort = `${this.host()}`
@@ -1,4 +1,5 @@
1
1
  import GetRoute from "./get-route.js"
2
+ import NamespaceRoute from "./namespace-route.js"
2
3
  import PostRoute from "./post-route.js"
3
4
  import ResourceRoute from "./resource-route.js"
4
5
 
@@ -20,6 +21,16 @@ export function initBaseRoute() {
20
21
  throw new Error(`No 'matchWithPath' implemented on ${this.constructor.name}`)
21
22
  }
22
23
 
24
+ namespace(name, callback) {
25
+ const route = new NamespaceRoute({name})
26
+
27
+ this.routes.push(route)
28
+
29
+ if (callback) {
30
+ callback(route)
31
+ }
32
+ }
33
+
23
34
  post(name, args) {
24
35
  const route = new PostRoute({name, args})
25
36
 
@@ -0,0 +1,24 @@
1
+ import BaseRoute, {initBaseRoute} from "./base-route.js"
2
+ import escapeStringRegexp from "escape-string-regexp"
3
+
4
+ initBaseRoute()
5
+
6
+ export default class VelociousRouteNamespaceRoute extends BaseRoute {
7
+ constructor({name}) {
8
+ super()
9
+ this.name = name
10
+ this.regExp = new RegExp(`^(${escapeStringRegexp(name)})(.*)$`)
11
+ }
12
+
13
+ matchWithPath({params, path}) {
14
+ const match = path.match(this.regExp)
15
+
16
+ if (match) {
17
+ const [_beginnigSlash, _matchedName, restPath] = match
18
+
19
+ params.controller = this.name
20
+
21
+ return {restPath}
22
+ }
23
+ }
24
+ }