@valkyrjaio/valkyrja 26.3.0 → 26.4.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@valkyrjaio/valkyrja",
3
- "version": "26.3.0",
3
+ "version": "26.4.0",
4
4
  "description": "The Valkyrja Framework.",
5
5
  "keywords": [
6
6
  "framework",
@@ -8,8 +8,8 @@
8
8
  */
9
9
 
10
10
  export class ApplicationInfo {
11
- static readonly VERSION = '26.3.0' as const;
12
- static readonly VERSION_BUILD_DATE_TIME = 'July 25 2026 20:40:59 MST' as const;
11
+ static readonly VERSION = '26.4.0' as const;
12
+ static readonly VERSION_BUILD_DATE_TIME = 'July 27 2026 22:05:07 MST' as const;
13
13
  static readonly ASCII = `
14
14
  _ _ _
15
15
  /\\ /\\__ _| | | ___ _ _ __(_) __ _
@@ -29,6 +29,8 @@ export class Cli extends App {
29
29
  }
30
30
 
31
31
  static getInput(config: CliConfigContract): InputContract {
32
- return InputFactory.fromGlobals(process.argv, config.applicationName, config.defaultCommandName);
32
+ // process.argv leads with the interpreter path, so drop it: the factory expects the vector
33
+ // to start with the caller, followed by the command name and the rest of the args.
34
+ return InputFactory.fromGlobals(process.argv.slice(1), config.applicationName, config.defaultCommandName);
33
35
  }
34
36
  }
@@ -8,7 +8,7 @@
8
8
  */
9
9
 
10
10
  import { createServer } from 'node:http';
11
- import type { IncomingMessage } from 'node:http';
11
+ import type { IncomingMessage, Server, ServerResponse } from 'node:http';
12
12
 
13
13
  import { App } from './Abstract/App.ts';
14
14
  import { HttpServerServiceId } from '../../Http/Server/Constant/HttpServerServiceId.ts';
@@ -20,6 +20,14 @@ import type { RequestHandlerContract } from '../../Http/Server/Handler/Contract/
20
20
 
21
21
  export class Http extends App {
22
22
  static run(config: HttpConfigContract, port: number = 3000): void {
23
+ const server = this.createServer((req, res) => {
24
+ this.handle(config, req, res);
25
+ });
26
+
27
+ this.listen(server, port);
28
+ }
29
+
30
+ static handle(config: HttpConfigContract, nodeRequest: IncomingMessage, nodeResponse: ServerResponse): void {
23
31
  const app = this.start(config);
24
32
  const container = app.getContainer();
25
33
 
@@ -27,15 +35,37 @@ export class Http extends App {
27
35
 
28
36
  const handler = container.getSingleton<RequestHandlerContract>(HttpServerServiceId.RequestHandlerContract);
29
37
 
30
- const server = createServer((req, res) => {
31
- const request = this.getRequest(req);
32
- handler.run(request, res);
33
- });
34
-
35
- server.listen(port);
38
+ handler.run(this.getRequest(nodeRequest), nodeResponse);
36
39
  }
37
40
 
38
41
  static getRequest(nodeRequest: IncomingMessage): ServerRequestContract {
39
42
  return RequestFactory.fromNodeRequest(nodeRequest);
40
43
  }
44
+
45
+ /**
46
+ * Create the native HTTP server.
47
+ *
48
+ * Overridable runtime seam wrapping Node's socket-creating `createServer`, so
49
+ * tests can substitute a double without opening a real socket. Ignored for
50
+ * coverage because it is irreducible runtime I/O that only runs against a live
51
+ * server.
52
+ */
53
+ /* v8 ignore start */
54
+ static createServer(handler: (request: IncomingMessage, response: ServerResponse) => void): Server {
55
+ return createServer(handler);
56
+ }
57
+ /* v8 ignore stop */
58
+
59
+ /**
60
+ * Bind the server to the given port.
61
+ *
62
+ * Overridable runtime seam wrapping Node's socket-binding `listen`, so tests
63
+ * can substitute a double without binding a real port. Ignored for coverage
64
+ * because it is irreducible runtime I/O that only runs against a live server.
65
+ */
66
+ /* v8 ignore start */
67
+ static listen(server: Server, port: number): void {
68
+ server.listen(port);
69
+ }
70
+ /* v8 ignore stop */
41
71
  }
@@ -0,0 +1,122 @@
1
+ /*
2
+ * This file is part of the Valkyrja package.
3
+ *
4
+ * (c) Melech Mizrachi <melechmizrachi@gmail.com>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+
10
+ import { createServer } from 'node:http';
11
+ import type { IncomingMessage, Server, ServerResponse } from 'node:http';
12
+
13
+ import { ChildApplication } from '../Kernel/ChildApplication.ts';
14
+ import { ChildContainer } from '../../Container/Manager/ChildContainer.ts';
15
+ import { ApplicationServiceId } from '../Constant/ApplicationServiceId.ts';
16
+ import { ContainerServiceId } from '../../Container/Constant/ContainerServiceId.ts';
17
+ import { HttpServerServiceId } from '../../Http/Server/Constant/HttpServerServiceId.ts';
18
+ import { HttpRoutingServiceId } from '../../Http/Routing/Constant/HttpRoutingServiceId.ts';
19
+ import { RequestFactory } from '../../Http/Message/Request/Factory/RequestFactory.ts';
20
+ import { App } from './Abstract/App.ts';
21
+
22
+ import type { ApplicationContract } from '../Kernel/Contract/ApplicationContract.ts';
23
+ import type { ContainerContract } from '../../Container/Manager/Contract/ContainerContract.ts';
24
+ import type { ContainerData } from '../../Container/Data/ContainerData.ts';
25
+ import type { HttpConfigContract } from '../Data/Contract/HttpConfigContract.ts';
26
+ import type { ServerRequestContract } from '../../Http/Message/Request/Contract/ServerRequestContract.ts';
27
+ import type { RequestHandlerContract } from '../../Http/Server/Handler/Contract/RequestHandlerContract.ts';
28
+ import type { RouteCollectionContract } from '../../Http/Routing/Collection/Contract/RouteCollectionContract.ts';
29
+
30
+ export class WorkerHttp extends App {
31
+ static run(config: HttpConfigContract, port: number = 3000): void {
32
+ const app = this.bootstrap(config);
33
+ const data = app.getContainer().getData();
34
+
35
+ const server = this.createServer((req, res) => {
36
+ this.handle(app, data, req, res);
37
+ });
38
+
39
+ this.listen(server, port);
40
+ }
41
+
42
+ static bootstrap(config: HttpConfigContract): ApplicationContract {
43
+ const app = this.start(config);
44
+ const container = app.getContainer();
45
+
46
+ this.bootstrapThrowableHandler(app, container);
47
+ this.bootstrapParentServices(app);
48
+
49
+ return app;
50
+ }
51
+
52
+ static handle(
53
+ app: ApplicationContract,
54
+ data: ContainerData,
55
+ nodeRequest: IncomingMessage,
56
+ nodeResponse: ServerResponse,
57
+ ): void {
58
+ const request = this.getRequest(nodeRequest);
59
+ const childContainer = this.getChildContainer(app, data);
60
+ const childApp = this.getChildApplication(app, childContainer);
61
+
62
+ this.bootstrapChildContainer(childApp, childContainer);
63
+ this.handleRequest(childContainer, request, nodeResponse);
64
+ }
65
+
66
+ static getChildApplication(app: ApplicationContract, container: ContainerContract): ApplicationContract {
67
+ return new ChildApplication(app, container);
68
+ }
69
+
70
+ static getChildContainer(app: ApplicationContract, data: ContainerData): ContainerContract {
71
+ return new ChildContainer(app.getContainer(), data);
72
+ }
73
+
74
+ static bootstrapChildContainer(app: ApplicationContract, container: ContainerContract): void {
75
+ container.setSingleton(ApplicationServiceId.ApplicationContract, app);
76
+ container.setSingleton(ContainerServiceId.Contract, container);
77
+ }
78
+
79
+ static handleRequest(
80
+ container: ContainerContract,
81
+ request: ServerRequestContract,
82
+ nodeResponse: ServerResponse,
83
+ ): void {
84
+ const handler = container.getSingleton<RequestHandlerContract>(HttpServerServiceId.RequestHandlerContract);
85
+ handler.run(request, nodeResponse);
86
+ }
87
+
88
+ static getRequest(nodeRequest: IncomingMessage): ServerRequestContract {
89
+ return RequestFactory.fromNodeRequest(nodeRequest);
90
+ }
91
+
92
+ static bootstrapParentServices(app: ApplicationContract): void {
93
+ app.getContainer().getSingleton<RouteCollectionContract>(HttpRoutingServiceId.RouteCollectionContract);
94
+ }
95
+
96
+ /**
97
+ * Create the native HTTP server.
98
+ *
99
+ * Overridable runtime seam wrapping Node's socket-creating `createServer`, so
100
+ * tests can substitute a double without opening a real socket. Ignored for
101
+ * coverage because it is irreducible runtime I/O that only runs against a live
102
+ * server.
103
+ */
104
+ /* v8 ignore start */
105
+ static createServer(handler: (request: IncomingMessage, response: ServerResponse) => void): Server {
106
+ return createServer(handler);
107
+ }
108
+ /* v8 ignore stop */
109
+
110
+ /**
111
+ * Bind the server to the given port.
112
+ *
113
+ * Overridable runtime seam wrapping Node's socket-binding `listen`, so tests
114
+ * can substitute a double without binding a real port. Ignored for coverage
115
+ * because it is irreducible runtime I/O that only runs against a live server.
116
+ */
117
+ /* v8 ignore start */
118
+ static listen(server: Server, port: number): void {
119
+ server.listen(port);
120
+ }
121
+ /* v8 ignore stop */
122
+ }
@@ -25,11 +25,18 @@ export abstract class InputFactory {
25
25
  ): InputContract {
26
26
  const arguments_ = [];
27
27
  const options = [];
28
+ let endOfOptions = false;
28
29
 
29
30
  for (const [key, arg] of args.entries()) {
30
31
  if (key === 0) {
31
32
  applicationName = arg;
32
- } else if (arg.startsWith('-')) {
33
+ } else if (!endOfOptions && arg === '--') {
34
+ // POSIX end-of-options marker: the `--` itself is consumed, and every arg after
35
+ // it is an operand — never an option, however many dashes it starts with. A
36
+ // second `--` is therefore an ordinary operand.
37
+ endOfOptions = true;
38
+ } else if (!endOfOptions && arg !== '-' && arg.startsWith('-')) {
39
+ // A lone `-` is an operand by convention (it names standard input), not an option.
33
40
  options.push(...OptionFactory.fromArg(arg));
34
41
  } else if (key === 1) {
35
42
  commandName = arg;
@@ -18,12 +18,11 @@ export abstract class OptionFactory {
18
18
  OptionFactory.validateArgIsOption(arg);
19
19
 
20
20
  const type = OptionFactory.getOptionType(arg);
21
- // The name is everything before the first '='; the value is the segment after it (if any).
22
- const name = arg
23
- .replace(/=[\s\S]*$/, '')
24
- .replace(/^-+/, '')
25
- .trim();
26
- const value = arg.split('=')[1] ?? '';
21
+ // Split on the first `=` only, so a value that itself contains one survives intact
22
+ // (`--expr=a=b` yields `a=b`, not `a`).
23
+ const separatorIndex = arg.indexOf('=');
24
+ const name = (separatorIndex === -1 ? arg : arg.slice(0, separatorIndex)).replace(/^-+/, '').trim();
25
+ const value = separatorIndex === -1 ? '' : arg.slice(separatorIndex + 1);
27
26
 
28
27
  OptionFactory.validateNonEmptyName(name);
29
28
 
@@ -176,6 +176,14 @@ export class OptionParameter extends Parameter implements OptionParameterContrac
176
176
  valid = valid && this.options.length <= 1;
177
177
  }
178
178
 
179
+ if (this.validValues.length > 0) {
180
+ for (const option of this.options) {
181
+ if (!this.validValues.includes(option.getValue())) {
182
+ return false;
183
+ }
184
+ }
185
+ }
186
+
179
187
  return valid;
180
188
  }
181
189
 
@@ -101,10 +101,28 @@ export class HeaderCollection implements HeaderCollectionContract {
101
101
  withAddedHeaders(...headers: HeaderContract[]): this {
102
102
  const clone = ObjectFactory.clone(this);
103
103
  clone.headers = { ...this.headers };
104
- this.setHeadersOnCollection(clone, ...headers);
104
+ for (const header of headers) {
105
+ clone.addHeader(header);
106
+ }
105
107
  return clone;
106
108
  }
107
109
 
110
+ /**
111
+ * Add a header, merging its values into an existing header of the same name.
112
+ */
113
+ protected addHeader(header: HeaderContract): void {
114
+ const name = header.getNormalizedName();
115
+ const existing = this.headers[name];
116
+
117
+ if (existing === undefined) {
118
+ this.headers[name] = header;
119
+
120
+ return;
121
+ }
122
+
123
+ this.headers[name] = existing.withAddedValues(...header.getValues());
124
+ }
125
+
108
126
  protected setHeaders(...headers: HeaderContract[]): void {
109
127
  this.setHeadersOnCollection(this, ...headers);
110
128
  }
@@ -42,8 +42,8 @@ export class Regex {
42
42
  static readonly ALPHA_NUM = '[a-zA-Z0-9]+';
43
43
  static readonly ALPHA_NUM_UNDERSCORE = '\\w+';
44
44
 
45
- static readonly START = '/^';
46
- static readonly END = '$/';
45
+ static readonly START = '^';
46
+ static readonly END = '$';
47
47
 
48
48
  static readonly START_CAPTURE_GROUP = '(';
49
49
  static readonly START_NON_CAPTURE_GROUP = '(?:';
@@ -1,83 +0,0 @@
1
- /*
2
- * This file is part of the Valkyrja package.
3
- *
4
- * (c) Melech Mizrachi <melechmizrachi@gmail.com>
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
-
10
- import type { IncomingMessage, ServerResponse } from 'node:http';
11
-
12
- import { ChildApplication } from '../../Kernel/ChildApplication.ts';
13
- import { ChildContainer } from '../../../Container/Manager/ChildContainer.ts';
14
- import { ApplicationServiceId } from '../../Constant/ApplicationServiceId.ts';
15
- import { ContainerServiceId } from '../../../Container/Constant/ContainerServiceId.ts';
16
- import { HttpServerServiceId } from '../../../Http/Server/Constant/HttpServerServiceId.ts';
17
- import { HttpRoutingServiceId } from '../../../Http/Routing/Constant/HttpRoutingServiceId.ts';
18
- import { RequestFactory } from '../../../Http/Message/Request/Factory/RequestFactory.ts';
19
- import { App } from './App.ts';
20
-
21
- import type { ApplicationContract } from '../../Kernel/Contract/ApplicationContract.ts';
22
- import type { ContainerContract } from '../../../Container/Manager/Contract/ContainerContract.ts';
23
- import type { ContainerData } from '../../../Container/Data/ContainerData.ts';
24
- import type { HttpConfigContract } from '../../Data/Contract/HttpConfigContract.ts';
25
- import type { ServerRequestContract } from '../../../Http/Message/Request/Contract/ServerRequestContract.ts';
26
- import type { RequestHandlerContract } from '../../../Http/Server/Handler/Contract/RequestHandlerContract.ts';
27
- import type { RouteCollectionContract } from '../../../Http/Routing/Collection/Contract/RouteCollectionContract.ts';
28
-
29
- export abstract class WorkerHttp extends App {
30
- static bootstrap(config: HttpConfigContract): ApplicationContract {
31
- const app = this.start(config);
32
- const container = app.getContainer();
33
-
34
- this.bootstrapThrowableHandler(app, container);
35
- this.bootstrapParentServices(app);
36
-
37
- return app;
38
- }
39
-
40
- static handle(
41
- app: ApplicationContract,
42
- data: ContainerData,
43
- nodeRequest: IncomingMessage,
44
- nodeResponse: ServerResponse,
45
- ): void {
46
- const request = this.getRequest(nodeRequest);
47
- const childContainer = this.getChildContainer(app, data);
48
- const childApp = this.getChildApplication(app, childContainer);
49
-
50
- this.bootstrapChildContainer(childApp, childContainer);
51
- this.handleRequest(childContainer, request, nodeResponse);
52
- }
53
-
54
- static getChildApplication(app: ApplicationContract, container: ContainerContract): ApplicationContract {
55
- return new ChildApplication(app, container);
56
- }
57
-
58
- static getChildContainer(app: ApplicationContract, data: ContainerData): ContainerContract {
59
- return new ChildContainer(app.getContainer(), data);
60
- }
61
-
62
- static bootstrapChildContainer(app: ApplicationContract, container: ContainerContract): void {
63
- container.setSingleton(ApplicationServiceId.ApplicationContract, app);
64
- container.setSingleton(ContainerServiceId.Contract, container);
65
- }
66
-
67
- static handleRequest(
68
- container: ContainerContract,
69
- request: ServerRequestContract,
70
- nodeResponse: ServerResponse,
71
- ): void {
72
- const handler = container.getSingleton<RequestHandlerContract>(HttpServerServiceId.RequestHandlerContract);
73
- handler.run(request, nodeResponse);
74
- }
75
-
76
- static getRequest(nodeRequest: IncomingMessage): ServerRequestContract {
77
- return RequestFactory.fromNodeRequest(nodeRequest);
78
- }
79
-
80
- static bootstrapParentServices(app: ApplicationContract): void {
81
- app.getContainer().getSingleton<RouteCollectionContract>(HttpRoutingServiceId.RouteCollectionContract);
82
- }
83
- }