@adonisjs/core 5.4.2 → 5.5.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/LICENSE.md +1 -1
- package/README.md +1 -1
- package/build/adonis-typings/ace.d.ts +5 -0
- package/build/adonis-typings/ace.js +8 -0
- package/build/adonis-typings/container.d.ts +4 -0
- package/build/adonis-typings/index.d.ts +2 -0
- package/build/adonis-typings/index.js +2 -0
- package/build/adonis-typings/test-utils.d.ts +23 -0
- package/build/adonis-typings/test-utils.js +8 -0
- package/build/providers/AppProvider.d.ts +10 -0
- package/build/providers/AppProvider.js +24 -0
- package/build/src/Ignitor/Ace/App/index.d.ts +8 -29
- package/build/src/Ignitor/Ace/App/index.js +49 -112
- package/build/src/Ignitor/Ace/GenerateManifest/index.d.ts +1 -1
- package/build/src/Ignitor/Ace/GenerateManifest/index.js +8 -15
- package/build/src/Ignitor/HttpServer/index.d.ts +11 -33
- package/build/src/Ignitor/HttpServer/index.js +20 -84
- package/build/src/Ignitor/Kernel/index.d.ts +56 -0
- package/build/src/Ignitor/Kernel/index.js +123 -0
- package/build/src/Ignitor/SignalsListener/index.js +14 -11
- package/build/src/Ignitor/index.d.ts +6 -0
- package/build/src/Ignitor/index.js +12 -1
- package/build/src/TestUtils/Ace/index.d.ts +0 -0
- package/build/src/TestUtils/Ace/index.js +8 -0
- package/build/src/TestUtils/HttpServer/index.d.ts +27 -0
- package/build/src/TestUtils/HttpServer/index.js +56 -0
- package/build/src/TestUtils/index.d.ts +24 -0
- package/build/src/TestUtils/index.js +43 -0
- package/build/src/utils/index.d.ts +13 -6
- package/build/src/utils/index.js +57 -10
- package/build/templates/contracts/env.txt +3 -3
- package/build/templates/env.txt +5 -5
- package/build/templates/tests/bootstrap.txt +71 -0
- package/package.json +78 -31
- package/build/src/Ignitor/Ace/ErrorHandler/index.d.ts +0 -16
- package/build/src/Ignitor/Ace/ErrorHandler/index.js +0 -47
- package/build/src/Ignitor/HttpServer/ErrorHandler/index.d.ts +0 -16
- package/build/src/Ignitor/HttpServer/ErrorHandler/index.js +0 -44
|
@@ -9,61 +9,24 @@
|
|
|
9
9
|
*/
|
|
10
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
11
|
exports.HttpServer = void 0;
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const ErrorHandler_1 = require("./ErrorHandler");
|
|
15
|
-
const SignalsListener_1 = require("../SignalsListener");
|
|
12
|
+
const Kernel_1 = require("../Kernel");
|
|
13
|
+
const utils_1 = require("../../utils");
|
|
16
14
|
/**
|
|
17
15
|
* Exposes the API to setup the application for starting the HTTP
|
|
18
16
|
* server.
|
|
17
|
+
*
|
|
18
|
+
* - Calling "kill" explicitly exists the process.
|
|
19
|
+
* - The "error" event emitted on the server instance explicitly exists the process.
|
|
20
|
+
* - SIGINT and in some case SIGTERM explicitly exists the process.
|
|
19
21
|
*/
|
|
20
22
|
class HttpServer {
|
|
21
23
|
constructor(appRoot) {
|
|
22
24
|
this.appRoot = appRoot;
|
|
23
25
|
/**
|
|
24
|
-
*
|
|
25
|
-
*/
|
|
26
|
-
this.wired = false;
|
|
27
|
-
/**
|
|
28
|
-
* Reference to the application.
|
|
29
|
-
*/
|
|
30
|
-
this.application = new application_1.Application(this.appRoot, 'web');
|
|
31
|
-
/**
|
|
32
|
-
* Listens for unix signals to kill long running
|
|
33
|
-
* processes.
|
|
34
|
-
*/
|
|
35
|
-
this.signalsListener = new SignalsListener_1.SignalsListener(this.application);
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Wires up everything, so that we are ready to kick start
|
|
39
|
-
* the HTTP server.
|
|
40
|
-
*/
|
|
41
|
-
async wire() {
|
|
42
|
-
if (this.wired) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Setting up the application.
|
|
47
|
-
*/
|
|
48
|
-
await this.application.setup();
|
|
49
|
-
/**
|
|
50
|
-
* Registering providers
|
|
51
|
-
*/
|
|
52
|
-
await this.application.registerProviders();
|
|
53
|
-
/**
|
|
54
|
-
* Booting providers
|
|
55
|
-
*/
|
|
56
|
-
await this.application.bootProviders();
|
|
57
|
-
/**
|
|
58
|
-
* Importing preloaded files
|
|
26
|
+
* Reference to the app kernel
|
|
59
27
|
*/
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Sets the server reference
|
|
64
|
-
*/
|
|
65
|
-
setServer() {
|
|
66
|
-
this.server = this.application.container.use('Adonis/Core/Server');
|
|
28
|
+
this.kernel = new Kernel_1.AppKernel(this.appRoot, 'web');
|
|
29
|
+
this.application = this.kernel.application;
|
|
67
30
|
}
|
|
68
31
|
/**
|
|
69
32
|
* Closes the underlying HTTP server
|
|
@@ -73,7 +36,7 @@ class HttpServer {
|
|
|
73
36
|
}
|
|
74
37
|
/**
|
|
75
38
|
* Monitors the HTTP server for close and error events, so that
|
|
76
|
-
* we can perform a graceful shutdown
|
|
39
|
+
* we can perform a graceful shutdown.
|
|
77
40
|
*/
|
|
78
41
|
monitorHttpServer() {
|
|
79
42
|
this.server.instance.on('close', async () => {
|
|
@@ -82,42 +45,17 @@ class HttpServer {
|
|
|
82
45
|
this.application.isShuttingDown = true;
|
|
83
46
|
});
|
|
84
47
|
this.server.instance.on('error', async (error) => {
|
|
85
|
-
|
|
86
|
-
this.application.logger.error('Port in use, closing server');
|
|
87
|
-
process.exitCode = 1;
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
48
|
+
this.application.logger.error(error, error.message);
|
|
90
49
|
await this.kill(3000);
|
|
91
50
|
});
|
|
92
51
|
}
|
|
93
|
-
/**
|
|
94
|
-
* Notify server is ready
|
|
95
|
-
*/
|
|
96
|
-
notifyServerReady(host, port) {
|
|
97
|
-
this.application.logger.info('started server on %s:%s', host, port);
|
|
98
|
-
if (process.send) {
|
|
99
|
-
process.send({ origin: 'adonis-http-server', ready: true, port: port, host: host });
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
52
|
/**
|
|
103
53
|
* Creates the HTTP server to handle incoming requests. The server is just
|
|
104
54
|
* created but not listening on any port.
|
|
105
55
|
*/
|
|
106
56
|
createServer(serverCallback) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
*/
|
|
110
|
-
this.application.logger.trace('optimizing http server handler');
|
|
111
|
-
this.server.optimize();
|
|
112
|
-
/**
|
|
113
|
-
* Bind exception handler to handle exceptions occured during HTTP requests.
|
|
114
|
-
*/
|
|
115
|
-
if (this.application.exceptionHandlerNamespace) {
|
|
116
|
-
this.application.logger.trace('binding %s exception handler', this.application.exceptionHandlerNamespace);
|
|
117
|
-
this.server.errorHandler(this.application.exceptionHandlerNamespace);
|
|
118
|
-
}
|
|
119
|
-
const handler = this.server.handle.bind(this.server);
|
|
120
|
-
this.server.instance = serverCallback ? serverCallback(handler) : (0, http_1.createServer)(handler);
|
|
57
|
+
this.server = this.application.container.use('Adonis/Core/Server');
|
|
58
|
+
(0, utils_1.createHttpServer)(this.application, this.server, serverCallback);
|
|
121
59
|
}
|
|
122
60
|
/**
|
|
123
61
|
* Starts the http server a given host and port
|
|
@@ -125,11 +63,12 @@ class HttpServer {
|
|
|
125
63
|
listen() {
|
|
126
64
|
return new Promise(async (resolve, reject) => {
|
|
127
65
|
try {
|
|
128
|
-
await this.
|
|
66
|
+
await this.kernel.start(() => this.close());
|
|
129
67
|
const host = this.application.env.get('HOST', '0.0.0.0');
|
|
130
68
|
const port = Number(this.application.env.get('PORT', '3333'));
|
|
131
69
|
this.server.instance.listen(port, host, () => {
|
|
132
|
-
this.
|
|
70
|
+
this.application.logger.info('started server on %s:%s', host, port);
|
|
71
|
+
this.kernel.ready({ port: port, host: host });
|
|
133
72
|
resolve();
|
|
134
73
|
});
|
|
135
74
|
}
|
|
@@ -143,15 +82,13 @@ class HttpServer {
|
|
|
143
82
|
*/
|
|
144
83
|
async start(serverCallback) {
|
|
145
84
|
try {
|
|
146
|
-
await this.
|
|
147
|
-
this.setServer();
|
|
85
|
+
await this.kernel.boot();
|
|
148
86
|
this.createServer(serverCallback);
|
|
149
87
|
this.monitorHttpServer();
|
|
150
88
|
await this.listen();
|
|
151
|
-
this.signalsListener.listen(() => this.close());
|
|
152
89
|
}
|
|
153
90
|
catch (error) {
|
|
154
|
-
await
|
|
91
|
+
await this.kernel.handleError(error);
|
|
155
92
|
}
|
|
156
93
|
}
|
|
157
94
|
/**
|
|
@@ -164,8 +101,7 @@ class HttpServer {
|
|
|
164
101
|
* we are not accepting any new request during cool off.
|
|
165
102
|
*/
|
|
166
103
|
await this.closeHttpServer();
|
|
167
|
-
this.
|
|
168
|
-
await this.application.shutdown();
|
|
104
|
+
await this.kernel.close();
|
|
169
105
|
}
|
|
170
106
|
/**
|
|
171
107
|
* Kills the http server process by attempting to perform a graceful
|
|
@@ -184,7 +120,7 @@ class HttpServer {
|
|
|
184
120
|
process.exit(0);
|
|
185
121
|
}
|
|
186
122
|
catch (error) {
|
|
187
|
-
|
|
123
|
+
await this.kernel.handleError(error).finally(() => process.exit(1));
|
|
188
124
|
}
|
|
189
125
|
}
|
|
190
126
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Application } from '@adonisjs/application';
|
|
2
|
+
import { AppEnvironments } from '@ioc:Adonis/Core/Application';
|
|
3
|
+
import { SignalsListener } from '../SignalsListener';
|
|
4
|
+
/**
|
|
5
|
+
* Kernel to manage application state
|
|
6
|
+
*/
|
|
7
|
+
export declare class AppKernel {
|
|
8
|
+
/**
|
|
9
|
+
* Reference to application instance
|
|
10
|
+
*/
|
|
11
|
+
application: Application;
|
|
12
|
+
/**
|
|
13
|
+
* Reference to signals listener
|
|
14
|
+
*/
|
|
15
|
+
signalsListener: SignalsListener;
|
|
16
|
+
/**
|
|
17
|
+
* Find if the application has been booted or not
|
|
18
|
+
*/
|
|
19
|
+
hasBooted: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Avoiding duplicate registeration of ts compiler hook
|
|
22
|
+
*/
|
|
23
|
+
private hasRegisteredTsHook;
|
|
24
|
+
constructor(appRoot: string, environment: AppEnvironments);
|
|
25
|
+
/**
|
|
26
|
+
* Pretty prints a given error on the terminal
|
|
27
|
+
*/
|
|
28
|
+
private prettyPrintError;
|
|
29
|
+
/**
|
|
30
|
+
* Boot the application. The process for booting the app remains the
|
|
31
|
+
* same regardless of the process environment.
|
|
32
|
+
*/
|
|
33
|
+
boot(): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Registers the TS compiler to hook into node require and
|
|
36
|
+
* process TypeScript files
|
|
37
|
+
*/
|
|
38
|
+
registerTsCompilerHook(): void;
|
|
39
|
+
/**
|
|
40
|
+
* Start the application. The callback is required to listen
|
|
41
|
+
* for close signals
|
|
42
|
+
*/
|
|
43
|
+
start(shutdownCallback?: () => Promise<void>): Promise<() => Promise<void>>;
|
|
44
|
+
/**
|
|
45
|
+
* Notify that process is ready
|
|
46
|
+
*/
|
|
47
|
+
ready(eventPayload: Record<string, any>): void;
|
|
48
|
+
/**
|
|
49
|
+
* Prepare application for shutdown
|
|
50
|
+
*/
|
|
51
|
+
close(): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Handles ignitor boot errors
|
|
54
|
+
*/
|
|
55
|
+
handleError(error: any): Promise<void>;
|
|
56
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* @adonisjs/core
|
|
4
|
+
*
|
|
5
|
+
* (c) Harminder Virk <virk@adonisjs.com>
|
|
6
|
+
*
|
|
7
|
+
* For the full copyright and license information, please view the LICENSE
|
|
8
|
+
* file that was distributed with this source code.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.AppKernel = void 0;
|
|
12
|
+
const application_1 = require("@adonisjs/application");
|
|
13
|
+
const utils_1 = require("../../utils");
|
|
14
|
+
const SignalsListener_1 = require("../SignalsListener");
|
|
15
|
+
/**
|
|
16
|
+
* Kernel to manage application state
|
|
17
|
+
*/
|
|
18
|
+
class AppKernel {
|
|
19
|
+
constructor(appRoot, environment) {
|
|
20
|
+
/**
|
|
21
|
+
* Find if the application has been booted or not
|
|
22
|
+
*/
|
|
23
|
+
this.hasBooted = false;
|
|
24
|
+
/**
|
|
25
|
+
* Avoiding duplicate registeration of ts compiler hook
|
|
26
|
+
*/
|
|
27
|
+
this.hasRegisteredTsHook = false;
|
|
28
|
+
this.application = new application_1.Application(appRoot, environment);
|
|
29
|
+
this.signalsListener = new SignalsListener_1.SignalsListener(this.application);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Pretty prints a given error on the terminal
|
|
33
|
+
*/
|
|
34
|
+
async prettyPrintError(error) {
|
|
35
|
+
try {
|
|
36
|
+
const Youch = require('youch');
|
|
37
|
+
const output = await new Youch(error, {}).toJSON();
|
|
38
|
+
console.log(require('youch-terminal')(output));
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
console.log(error.stack);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Boot the application. The process for booting the app remains the
|
|
46
|
+
* same regardless of the process environment.
|
|
47
|
+
*/
|
|
48
|
+
async boot() {
|
|
49
|
+
if (this.hasBooted) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Setting up the application.
|
|
54
|
+
*/
|
|
55
|
+
await this.application.setup();
|
|
56
|
+
/**
|
|
57
|
+
* Registering providers
|
|
58
|
+
*/
|
|
59
|
+
await this.application.registerProviders();
|
|
60
|
+
/**
|
|
61
|
+
* Booting providers
|
|
62
|
+
*/
|
|
63
|
+
await this.application.bootProviders();
|
|
64
|
+
/**
|
|
65
|
+
* Importing preloaded files
|
|
66
|
+
*/
|
|
67
|
+
await this.application.requirePreloads();
|
|
68
|
+
this.hasBooted = true;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Registers the TS compiler to hook into node require and
|
|
72
|
+
* process TypeScript files
|
|
73
|
+
*/
|
|
74
|
+
registerTsCompilerHook() {
|
|
75
|
+
/**
|
|
76
|
+
* Register ts hook when running typescript code directly
|
|
77
|
+
*/
|
|
78
|
+
if (this.application.rcFile.typescript && !this.hasRegisteredTsHook) {
|
|
79
|
+
this.hasRegisteredTsHook = true;
|
|
80
|
+
(0, utils_1.registerTsHook)(this.application.appRoot);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Start the application. The callback is required to listen
|
|
85
|
+
* for close signals
|
|
86
|
+
*/
|
|
87
|
+
async start(shutdownCallback) {
|
|
88
|
+
await this.application.start();
|
|
89
|
+
this.signalsListener.listen(shutdownCallback || (() => this.close()));
|
|
90
|
+
return () => this.close();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Notify that process is ready
|
|
94
|
+
*/
|
|
95
|
+
ready(eventPayload) {
|
|
96
|
+
if (process.send) {
|
|
97
|
+
process.send('ready');
|
|
98
|
+
process.send({ isAdonisJS: true, environment: this.application.environment, ...eventPayload });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Prepare application for shutdown
|
|
103
|
+
*/
|
|
104
|
+
async close() {
|
|
105
|
+
this.signalsListener.cleanup();
|
|
106
|
+
await this.application.shutdown();
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Handles ignitor boot errors
|
|
110
|
+
*/
|
|
111
|
+
async handleError(error) {
|
|
112
|
+
if (typeof error.handle === 'function') {
|
|
113
|
+
await error.handle(error);
|
|
114
|
+
}
|
|
115
|
+
else if (this.application.inDev) {
|
|
116
|
+
await this.prettyPrintError(error);
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
console.error(error.stack);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
exports.AppKernel = AppKernel;
|
|
@@ -21,7 +21,12 @@ class SignalsListener {
|
|
|
21
21
|
*/
|
|
22
22
|
this.kill = async function () {
|
|
23
23
|
try {
|
|
24
|
-
await
|
|
24
|
+
await Promise.race([
|
|
25
|
+
this.onCloseCallback(),
|
|
26
|
+
new Promise((resolve) => {
|
|
27
|
+
setTimeout(resolve, 3000);
|
|
28
|
+
}),
|
|
29
|
+
]);
|
|
25
30
|
process.exit(0);
|
|
26
31
|
}
|
|
27
32
|
catch (error) {
|
|
@@ -35,20 +40,18 @@ class SignalsListener {
|
|
|
35
40
|
*/
|
|
36
41
|
listen(callback) {
|
|
37
42
|
this.onCloseCallback = callback;
|
|
43
|
+
/**
|
|
44
|
+
* Close on SIGINT AND SIGTERM SIGNALS
|
|
45
|
+
*/
|
|
38
46
|
if (process.env.pm_id) {
|
|
39
|
-
process.
|
|
47
|
+
process.on('SIGINT', this.kill);
|
|
40
48
|
}
|
|
41
|
-
process.
|
|
49
|
+
process.on('SIGTERM', this.kill);
|
|
42
50
|
/**
|
|
43
|
-
*
|
|
51
|
+
* Notify about uncaught exceptions
|
|
44
52
|
*/
|
|
45
|
-
process.
|
|
46
|
-
|
|
47
|
-
this.application.logger.fatal(error, '"uncaughtException" detected');
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
this.application.logger.fatal(error, '"uncaughtException" detected. Process will shutdown');
|
|
51
|
-
process.exit(1);
|
|
53
|
+
process.on('uncaughtExceptionMonitor', (error) => {
|
|
54
|
+
this.application.logger.fatal(error, '"uncaughtException" detected');
|
|
52
55
|
});
|
|
53
56
|
}
|
|
54
57
|
/**
|
|
@@ -2,6 +2,7 @@ import { Application } from '@adonisjs/application';
|
|
|
2
2
|
import { AppEnvironments } from '@ioc:Adonis/Core/Application';
|
|
3
3
|
import { Ace } from './Ace';
|
|
4
4
|
import { HttpServer } from './HttpServer';
|
|
5
|
+
import { AppKernel } from './Kernel';
|
|
5
6
|
/**
|
|
6
7
|
* Ignitor is used to wireup different pieces of AdonisJs to bootstrap
|
|
7
8
|
* the application.
|
|
@@ -18,6 +19,11 @@ export declare class Ignitor {
|
|
|
18
19
|
* the HTTP server
|
|
19
20
|
*/
|
|
20
21
|
httpServer(): HttpServer;
|
|
22
|
+
/**
|
|
23
|
+
* Returns instance of server to start
|
|
24
|
+
* the HTTP server
|
|
25
|
+
*/
|
|
26
|
+
kernel(environment: AppEnvironments): AppKernel;
|
|
21
27
|
/**
|
|
22
28
|
* Returns instance of ace to handle console
|
|
23
29
|
* commands
|
|
@@ -14,13 +14,17 @@ const url_1 = require("url");
|
|
|
14
14
|
const application_1 = require("@adonisjs/application");
|
|
15
15
|
const Ace_1 = require("./Ace");
|
|
16
16
|
const HttpServer_1 = require("./HttpServer");
|
|
17
|
+
const Kernel_1 = require("./Kernel");
|
|
17
18
|
/**
|
|
18
19
|
* Ignitor is used to wireup different pieces of AdonisJs to bootstrap
|
|
19
20
|
* the application.
|
|
20
21
|
*/
|
|
21
22
|
class Ignitor {
|
|
22
23
|
constructor(appRoot) {
|
|
23
|
-
|
|
24
|
+
/**
|
|
25
|
+
* In ESM, ignitor is constructed with `import.meta.url`. Normalize
|
|
26
|
+
* the file URL to an absolute directory path.
|
|
27
|
+
*/
|
|
24
28
|
this.appRoot = appRoot.startsWith('file:') ? (0, path_1.dirname)((0, url_1.fileURLToPath)(appRoot)) : appRoot;
|
|
25
29
|
}
|
|
26
30
|
/**
|
|
@@ -36,6 +40,13 @@ class Ignitor {
|
|
|
36
40
|
httpServer() {
|
|
37
41
|
return new HttpServer_1.HttpServer(this.appRoot);
|
|
38
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Returns instance of server to start
|
|
45
|
+
* the HTTP server
|
|
46
|
+
*/
|
|
47
|
+
kernel(environment) {
|
|
48
|
+
return new Kernel_1.AppKernel(this.appRoot, environment);
|
|
49
|
+
}
|
|
39
50
|
/**
|
|
40
51
|
* Returns instance of ace to handle console
|
|
41
52
|
* commands
|
|
File without changes
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ApplicationContract } from '@ioc:Adonis/Core/Application';
|
|
2
|
+
import { CustomServerCallback } from '@ioc:Adonis/Core/TestUtils';
|
|
3
|
+
/**
|
|
4
|
+
* The test HTTP server to handle and serve HTTP request
|
|
5
|
+
*/
|
|
6
|
+
export declare class TestHttpServer {
|
|
7
|
+
application: ApplicationContract;
|
|
8
|
+
private server;
|
|
9
|
+
constructor(application: ApplicationContract);
|
|
10
|
+
/**
|
|
11
|
+
* Creates the HTTP server to handle incoming requests. The server is just
|
|
12
|
+
* created but not listening on any port.
|
|
13
|
+
*/
|
|
14
|
+
private create;
|
|
15
|
+
/**
|
|
16
|
+
* Start the HTTP server to listen for incoming requests
|
|
17
|
+
*/
|
|
18
|
+
private listen;
|
|
19
|
+
/**
|
|
20
|
+
* Close HTTP server from listening to any requests
|
|
21
|
+
*/
|
|
22
|
+
close(): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Start the test HTTP server
|
|
25
|
+
*/
|
|
26
|
+
start(serverCallback?: CustomServerCallback): Promise<() => Promise<void>>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* @adonisjs/core
|
|
4
|
+
*
|
|
5
|
+
* (c) Harminder Virk <virk@adonisjs.com>
|
|
6
|
+
*
|
|
7
|
+
* For the full copyright and license information, please view the LICENSE
|
|
8
|
+
* file that was distributed with this source code.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.TestHttpServer = void 0;
|
|
12
|
+
const utils_1 = require("../../utils");
|
|
13
|
+
/**
|
|
14
|
+
* The test HTTP server to handle and serve HTTP request
|
|
15
|
+
*/
|
|
16
|
+
class TestHttpServer {
|
|
17
|
+
constructor(application) {
|
|
18
|
+
this.application = application;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates the HTTP server to handle incoming requests. The server is just
|
|
22
|
+
* created but not listening on any port.
|
|
23
|
+
*/
|
|
24
|
+
create(serverCallback) {
|
|
25
|
+
this.server = this.application.container.use('Adonis/Core/Server');
|
|
26
|
+
(0, utils_1.createHttpServer)(this.application, this.server, serverCallback);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Start the HTTP server to listen for incoming requests
|
|
30
|
+
*/
|
|
31
|
+
listen() {
|
|
32
|
+
const host = this.application.env.get('HOST', '0.0.0.0');
|
|
33
|
+
const port = Number(this.application.env.get('PORT', '3333'));
|
|
34
|
+
return new Promise((resolve) => {
|
|
35
|
+
this.server.instance.listen(port, host, () => {
|
|
36
|
+
this.application.logger.info('started server on %s:%s', host, port);
|
|
37
|
+
resolve();
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Close HTTP server from listening to any requests
|
|
43
|
+
*/
|
|
44
|
+
close() {
|
|
45
|
+
return new Promise((resolve) => this.server.instance.close(() => resolve()));
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Start the test HTTP server
|
|
49
|
+
*/
|
|
50
|
+
async start(serverCallback) {
|
|
51
|
+
this.create(serverCallback);
|
|
52
|
+
await this.listen();
|
|
53
|
+
return () => this.close();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.TestHttpServer = TestHttpServer;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Macroable } from 'macroable';
|
|
2
|
+
import { TestUtilsContract } from '@ioc:Adonis/Core/TestUtils';
|
|
3
|
+
import { ApplicationContract } from '@ioc:Adonis/Core/Application';
|
|
4
|
+
import { TestHttpServer } from './HttpServer';
|
|
5
|
+
/**
|
|
6
|
+
* Test utils module is meant to be extended to add custom
|
|
7
|
+
* utilities required for testing AdonisJS applications.
|
|
8
|
+
*/
|
|
9
|
+
export declare class TestUtils extends Macroable implements Omit<TestUtilsContract, 'constructor'> {
|
|
10
|
+
application: ApplicationContract;
|
|
11
|
+
static macros: {};
|
|
12
|
+
static getters: {};
|
|
13
|
+
constructor(application: ApplicationContract);
|
|
14
|
+
/**
|
|
15
|
+
* Utilities for ace
|
|
16
|
+
*/
|
|
17
|
+
ace(): {
|
|
18
|
+
loadCommands: () => Promise<void>;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Utilities for http server
|
|
22
|
+
*/
|
|
23
|
+
httpServer(): TestHttpServer;
|
|
24
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* @adonisjs/core
|
|
4
|
+
*
|
|
5
|
+
* (c) Harminder Virk <virk@adonisjs.com>
|
|
6
|
+
*
|
|
7
|
+
* For the full copyright and license information, please view the LICENSE
|
|
8
|
+
* file that was distributed with this source code.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.TestUtils = void 0;
|
|
12
|
+
const macroable_1 = require("macroable");
|
|
13
|
+
const utils_1 = require("../utils");
|
|
14
|
+
const HttpServer_1 = require("./HttpServer");
|
|
15
|
+
/**
|
|
16
|
+
* Test utils module is meant to be extended to add custom
|
|
17
|
+
* utilities required for testing AdonisJS applications.
|
|
18
|
+
*/
|
|
19
|
+
class TestUtils extends macroable_1.Macroable {
|
|
20
|
+
constructor(application) {
|
|
21
|
+
super();
|
|
22
|
+
this.application = application;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Utilities for ace
|
|
26
|
+
*/
|
|
27
|
+
ace() {
|
|
28
|
+
return {
|
|
29
|
+
loadCommands: () => {
|
|
30
|
+
return (0, utils_1.loadAceCommands)(this.application, this.application.container.resolveBinding('Adonis/Core/Ace'));
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Utilities for http server
|
|
36
|
+
*/
|
|
37
|
+
httpServer() {
|
|
38
|
+
return new HttpServer_1.TestHttpServer(this.application);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.TestUtils = TestUtils;
|
|
42
|
+
TestUtils.macros = {};
|
|
43
|
+
TestUtils.getters = {};
|
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*/
|
|
6
|
-
export declare function isMissingModuleError(error: NodeJS.ErrnoException): boolean;
|
|
1
|
+
import { Kernel } from '@adonisjs/ace';
|
|
2
|
+
import { ServerContract } from '@ioc:Adonis/Core/Server';
|
|
3
|
+
import { CustomServerCallback } from '@ioc:Adonis/Core/TestUtils';
|
|
4
|
+
import { ApplicationContract } from '@ioc:Adonis/Core/Application';
|
|
7
5
|
/**
|
|
8
6
|
* Registers the ts hook to compile typescript code within the memory
|
|
9
7
|
*/
|
|
10
8
|
export declare function registerTsHook(appRoot: string): void;
|
|
9
|
+
/**
|
|
10
|
+
* Creates the AdonisJS HTTP server. The method is abstracted to be used by
|
|
11
|
+
* test utils and the HTTP server process both.
|
|
12
|
+
*/
|
|
13
|
+
export declare function createHttpServer(application: ApplicationContract, server: ServerContract, callback?: CustomServerCallback): void;
|
|
14
|
+
/**
|
|
15
|
+
* Loads ace commands from the assembler manifest and the app manifest files
|
|
16
|
+
*/
|
|
17
|
+
export declare function loadAceCommands(application: ApplicationContract, ace: Kernel): Promise<void>;
|