@mojojs/core 0.19.1 → 0.21.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/lib/app.d.ts +7 -0
- package/lib/app.js +13 -2
- package/lib/app.js.map +1 -1
- package/lib/cache.d.ts +23 -0
- package/lib/cache.js +45 -0
- package/lib/cache.js.map +1 -0
- package/lib/cli/create-full-app.js +2 -2
- package/lib/logger.d.ts +69 -0
- package/lib/logger.js +63 -0
- package/lib/logger.js.map +1 -1
- package/lib/plugins/mt-engine.js +4 -5
- package/lib/plugins/mt-engine.js.map +1 -1
- package/lib/plugins/view-helpers.js +2 -1
- package/lib/plugins/view-helpers.js.map +1 -1
- package/lib/renderer.d.ts +38 -0
- package/lib/renderer.js +38 -0
- package/lib/renderer.js.map +1 -1
- package/lib/router/pattern.d.ts +33 -0
- package/lib/router/pattern.js +24 -0
- package/lib/router/pattern.js.map +1 -1
- package/lib/router/route.d.ts +83 -0
- package/lib/router/route.js +83 -0
- package/lib/router/route.js.map +1 -1
- package/lib/router.d.ts +38 -2
- package/lib/router.js +38 -2
- package/lib/router.js.map +1 -1
- package/lib/session.d.ts +35 -0
- package/lib/session.js +35 -0
- package/lib/session.js.map +1 -1
- package/lib/static.d.ts +22 -0
- package/lib/static.js +22 -0
- package/lib/static.js.map +1 -1
- package/lib/user-agent/test.d.ts +115 -2
- package/lib/user-agent/test.js +123 -10
- package/lib/user-agent/test.js.map +1 -1
- package/package.json +3 -5
package/lib/app.d.ts
CHANGED
|
@@ -34,6 +34,10 @@ export declare class App {
|
|
|
34
34
|
* Application config.
|
|
35
35
|
*/
|
|
36
36
|
config: Record<string, any>;
|
|
37
|
+
/**
|
|
38
|
+
* Default stash values.
|
|
39
|
+
*/
|
|
40
|
+
defaults: Record<string, any>;
|
|
37
41
|
/**
|
|
38
42
|
* Detect if the application has been imported and disable the command line interface if it has.
|
|
39
43
|
*/
|
|
@@ -153,6 +157,9 @@ export declare class App {
|
|
|
153
157
|
* Generate route matching only `PATCH` requests.
|
|
154
158
|
*/
|
|
155
159
|
patch(...args: RouteArguments): Route;
|
|
160
|
+
/**
|
|
161
|
+
* Register plugin.
|
|
162
|
+
*/
|
|
156
163
|
plugin(plugin: Plugin, options?: Record<string, any>): any;
|
|
157
164
|
/**
|
|
158
165
|
* Generate route matching only `POST` requests.
|
package/lib/app.js
CHANGED
|
@@ -30,6 +30,10 @@ export class App {
|
|
|
30
30
|
* Command line interface.
|
|
31
31
|
*/
|
|
32
32
|
this.cli = new CLI(this);
|
|
33
|
+
/**
|
|
34
|
+
* Default stash values.
|
|
35
|
+
*/
|
|
36
|
+
this.defaults = {};
|
|
33
37
|
/**
|
|
34
38
|
* Application hooks.
|
|
35
39
|
*/
|
|
@@ -166,14 +170,18 @@ export class App {
|
|
|
166
170
|
* Create a context for application.
|
|
167
171
|
*/
|
|
168
172
|
newContext(req, res, options) {
|
|
169
|
-
|
|
173
|
+
const ctx = new this._contextClass(this, req, res, options);
|
|
174
|
+
Object.assign(ctx.stash, this.defaults);
|
|
175
|
+
return ctx;
|
|
170
176
|
}
|
|
171
177
|
/**
|
|
172
178
|
* Create a mock context for application. Very useful for testing helpers.
|
|
173
179
|
*/
|
|
174
180
|
newMockContext(options) {
|
|
175
181
|
const req = new MockRequest(options);
|
|
176
|
-
|
|
182
|
+
const ctx = new this._contextClass(this, req, new MockResponse(req), { isReverseProxy: false, isWebSocket: false });
|
|
183
|
+
Object.assign(ctx.stash, this.defaults);
|
|
184
|
+
return ctx;
|
|
177
185
|
}
|
|
178
186
|
/**
|
|
179
187
|
* Create a new mock user agent for application.
|
|
@@ -199,6 +207,9 @@ export class App {
|
|
|
199
207
|
patch(...args) {
|
|
200
208
|
return this.router.patch(...args);
|
|
201
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* Register plugin.
|
|
212
|
+
*/
|
|
202
213
|
plugin(plugin, options = {}) {
|
|
203
214
|
return plugin(this, options);
|
|
204
215
|
}
|
package/lib/app.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.js","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAaA,OAAO,EAAC,GAAG,EAAC,MAAM,UAAU,CAAC;AAC7B,OAAO,EAAC,OAAO,EAAC,MAAM,cAAc,CAAC;AACrC,OAAO,EAAC,KAAK,EAAC,MAAM,YAAY,CAAC;AACjC,OAAO,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AACnC,OAAO,EAAC,IAAI,EAAC,MAAM,WAAW,CAAC;AAC/B,OAAO,EAAC,WAAW,EAAC,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAC,YAAY,EAAC,MAAM,oBAAoB,CAAC;AAChD,OAAO,oBAAoB,MAAM,8BAA8B,CAAC;AAChE,OAAO,sBAAsB,MAAM,gCAAgC,CAAC;AACpE,OAAO,sBAAsB,MAAM,gCAAgC,CAAC;AACpE,OAAO,cAAc,MAAM,wBAAwB,CAAC;AACpD,OAAO,iBAAiB,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAC,QAAQ,EAAC,MAAM,eAAe,CAAC;AACvC,OAAO,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AACnC,OAAO,EAAC,OAAO,EAAC,MAAM,cAAc,CAAC;AACrC,OAAO,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AACnC,OAAO,EAAC,SAAS,EAAC,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAC,aAAa,EAAC,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAC,aAAa,EAAC,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAC,SAAS,EAAC,MAAM,gBAAgB,CAAC;AACzC,OAAO,IAAI,MAAM,cAAc,CAAC;AAEhC,MAAM,cAAc,GAAG,KAAM,SAAQ,OAAO;CAAG,CAAC;AAShD;;GAEG;AACH,MAAM,OAAO,GAAG;
|
|
1
|
+
{"version":3,"file":"app.js","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mapp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
+
/**
|
|
6
|
+
* Maximum number of values in cache, defaults to `100`.
|
|
7
|
+
*/
|
|
8
|
+
max: number;
|
|
9
|
+
_cache: Record<string, T>;
|
|
10
|
+
_queue: string[];
|
|
11
|
+
/**
|
|
12
|
+
* Get cached value.
|
|
13
|
+
*/
|
|
14
|
+
get(key: string): T | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Add value to the cache.
|
|
17
|
+
*/
|
|
18
|
+
set(key: string, value: T): void;
|
|
19
|
+
/**
|
|
20
|
+
* Current size of the cache.
|
|
21
|
+
*/
|
|
22
|
+
get size(): number;
|
|
23
|
+
}
|
package/lib/cache.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache class.
|
|
3
|
+
*/
|
|
4
|
+
export class Cache {
|
|
5
|
+
constructor() {
|
|
6
|
+
/**
|
|
7
|
+
* Maximum number of values in cache, defaults to `100`.
|
|
8
|
+
*/
|
|
9
|
+
this.max = 100;
|
|
10
|
+
this._cache = {};
|
|
11
|
+
this._queue = [];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Get cached value.
|
|
15
|
+
*/
|
|
16
|
+
get(key) {
|
|
17
|
+
return this._cache[key];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Add value to the cache.
|
|
21
|
+
*/
|
|
22
|
+
set(key, value) {
|
|
23
|
+
const max = this.max;
|
|
24
|
+
if (max <= 0)
|
|
25
|
+
return;
|
|
26
|
+
const cache = this._cache;
|
|
27
|
+
const queue = this._queue;
|
|
28
|
+
while (queue.length >= max) {
|
|
29
|
+
const key = queue.shift();
|
|
30
|
+
if (key !== undefined)
|
|
31
|
+
delete cache[key];
|
|
32
|
+
}
|
|
33
|
+
if (cache[key] === undefined) {
|
|
34
|
+
queue.push(key);
|
|
35
|
+
cache[key] = value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Current size of the cache.
|
|
40
|
+
*/
|
|
41
|
+
get size() {
|
|
42
|
+
return this._queue.length;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=cache.js.map
|
package/lib/cache.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,OAAO,KAAK;IAAlB;QACE;;WAEG;QACH,QAAG,GAAG,GAAG,CAAC;QAEV,WAAM,GAAsB,EAAE,CAAC;QAC/B,WAAM,GAAa,EAAE,CAAC;IAmCxB,CAAC;IAjCC;;OAEG;IACH,GAAG,CAAC,GAAW;QACb,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,GAAW,EAAE,KAAQ;QACvB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC;QACrB,IAAI,GAAG,IAAI,CAAC;YAAE,OAAO;QAErB,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,OAAO,KAAK,CAAC,MAAM,IAAI,GAAG,EAAE;YAC1B,MAAM,GAAG,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC;YAC1B,IAAI,GAAG,KAAK,SAAS;gBAAE,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC;SAC1C;QAED,IAAI,KAAK,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE;YAC5B,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAChB,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;SACpB;IACH,CAAC;IAED;;OAEG;IACH,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;IAC5B,CAAC;CACF"}
|
|
@@ -56,13 +56,13 @@ const jsController = `export default class Controller {
|
|
|
56
56
|
const mtLayout = `<!DOCTYPE html>
|
|
57
57
|
<html>
|
|
58
58
|
<head>
|
|
59
|
-
|
|
59
|
+
%%= ctx.mojoFaviconTag()
|
|
60
60
|
<title>Welcome</title>
|
|
61
61
|
</head>
|
|
62
62
|
<body><%%== view.content %></body>
|
|
63
63
|
</html>
|
|
64
64
|
`;
|
|
65
|
-
const mtView =
|
|
65
|
+
const mtView = `%% view.layout = 'default';
|
|
66
66
|
<h2><%%= msg %></h2>
|
|
67
67
|
<p>
|
|
68
68
|
This page was generated from the template "views/example/welcome.html.mt" and the layout
|
package/lib/logger.d.ts
CHANGED
|
@@ -11,9 +11,21 @@ interface LogEvent extends LogContext {
|
|
|
11
11
|
msg: string;
|
|
12
12
|
}
|
|
13
13
|
declare type LogFormatter = (data: LogEvent) => string;
|
|
14
|
+
/**
|
|
15
|
+
* A simple logger class.
|
|
16
|
+
*/
|
|
14
17
|
export declare class Logger {
|
|
18
|
+
/**
|
|
19
|
+
* Log destination stream.
|
|
20
|
+
*/
|
|
15
21
|
destination: NodeJS.WritableStream;
|
|
22
|
+
/**
|
|
23
|
+
* Log formatter.
|
|
24
|
+
*/
|
|
16
25
|
formatter: LogFormatter;
|
|
26
|
+
/**
|
|
27
|
+
* The last few logged messages.
|
|
28
|
+
*/
|
|
17
29
|
history: LogEvent[];
|
|
18
30
|
_historySize: number;
|
|
19
31
|
_level: number;
|
|
@@ -23,30 +35,87 @@ export declare class Logger {
|
|
|
23
35
|
historySize?: number;
|
|
24
36
|
level?: string;
|
|
25
37
|
});
|
|
38
|
+
/**
|
|
39
|
+
* Create a child logger that will include context information with every log message.
|
|
40
|
+
*/
|
|
26
41
|
child(context: LogContext): ChildLogger;
|
|
42
|
+
/**
|
|
43
|
+
* Log formatter with color highlighting.
|
|
44
|
+
*/
|
|
27
45
|
static colorFormatter(data: LogEvent): string;
|
|
46
|
+
/**
|
|
47
|
+
* Log `debug` message.
|
|
48
|
+
*/
|
|
28
49
|
debug(msg: string, context?: LogContext): void;
|
|
50
|
+
/**
|
|
51
|
+
* Log formatter without color highlighting.
|
|
52
|
+
*/
|
|
29
53
|
static stringFormatter(data: LogEvent): string;
|
|
54
|
+
/**
|
|
55
|
+
* Log formatter for systemd.
|
|
56
|
+
*/
|
|
30
57
|
static systemdFormatter(data: LogEvent): string;
|
|
58
|
+
/**
|
|
59
|
+
* Log `error` message.
|
|
60
|
+
*/
|
|
31
61
|
error(msg: string, context?: LogContext): void;
|
|
62
|
+
/**
|
|
63
|
+
* Log `fatal` message.
|
|
64
|
+
*/
|
|
32
65
|
fatal(msg: string, context?: LogContext): void;
|
|
66
|
+
/**
|
|
67
|
+
* Log `infor` message.
|
|
68
|
+
*/
|
|
33
69
|
info(msg: string, context?: LogContext): void;
|
|
70
|
+
/**
|
|
71
|
+
* JSON log formatter.
|
|
72
|
+
*/
|
|
34
73
|
static jsonFormatter(data: LogEvent): string;
|
|
74
|
+
/**
|
|
75
|
+
* Currently active log level.
|
|
76
|
+
*/
|
|
35
77
|
get level(): string;
|
|
36
78
|
set level(level: string);
|
|
79
|
+
/**
|
|
80
|
+
* Log `trace` message.
|
|
81
|
+
*/
|
|
37
82
|
trace(msg: string, context?: LogContext): void;
|
|
83
|
+
/**
|
|
84
|
+
* Log `warn` message.
|
|
85
|
+
*/
|
|
38
86
|
warn(msg: string, context?: LogContext): void;
|
|
39
87
|
_log(level: string, msg: string, context?: LogContext): void;
|
|
40
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Child logger class.
|
|
91
|
+
*/
|
|
41
92
|
export declare class ChildLogger {
|
|
42
93
|
parent: Logger;
|
|
43
94
|
context: LogContext;
|
|
44
95
|
constructor(parent: Logger, context: LogContext);
|
|
96
|
+
/**
|
|
97
|
+
* Log `debug` message.
|
|
98
|
+
*/
|
|
45
99
|
debug(msg: string): void;
|
|
100
|
+
/**
|
|
101
|
+
* Log `error` message.
|
|
102
|
+
*/
|
|
46
103
|
error(msg: string): void;
|
|
104
|
+
/**
|
|
105
|
+
* Log `fatal` message.
|
|
106
|
+
*/
|
|
47
107
|
fatal(msg: string): void;
|
|
108
|
+
/**
|
|
109
|
+
* Log `info` message.
|
|
110
|
+
*/
|
|
48
111
|
info(msg: string): void;
|
|
112
|
+
/**
|
|
113
|
+
* Log `trace` message.
|
|
114
|
+
*/
|
|
49
115
|
trace(msg: string): void;
|
|
116
|
+
/**
|
|
117
|
+
* Log `warn` message.
|
|
118
|
+
*/
|
|
50
119
|
warn(msg: string): void;
|
|
51
120
|
}
|
|
52
121
|
export {};
|
package/lib/logger.js
CHANGED
|
@@ -8,8 +8,14 @@ const LEVEL = {
|
|
|
8
8
|
debug: 6,
|
|
9
9
|
trace: 7
|
|
10
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* A simple logger class.
|
|
13
|
+
*/
|
|
11
14
|
export class Logger {
|
|
12
15
|
constructor(options = {}) {
|
|
16
|
+
/**
|
|
17
|
+
* The last few logged messages.
|
|
18
|
+
*/
|
|
13
19
|
this.history = [];
|
|
14
20
|
this._level = 7;
|
|
15
21
|
this.destination = options.destination ?? process.stderr;
|
|
@@ -17,9 +23,15 @@ export class Logger {
|
|
|
17
23
|
this.level = process.env.MOJO_LOG_LEVEL ?? options.level ?? 'trace';
|
|
18
24
|
this._historySize = options.historySize ?? 0;
|
|
19
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Create a child logger that will include context information with every log message.
|
|
28
|
+
*/
|
|
20
29
|
child(context) {
|
|
21
30
|
return new ChildLogger(this, context);
|
|
22
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Log formatter with color highlighting.
|
|
34
|
+
*/
|
|
23
35
|
static colorFormatter(data) {
|
|
24
36
|
const formatted = Logger.stringFormatter(data);
|
|
25
37
|
if (data.level === 'error' || data.level === 'fatal')
|
|
@@ -28,35 +40,59 @@ export class Logger {
|
|
|
28
40
|
return chalk.yellow(formatted);
|
|
29
41
|
return formatted;
|
|
30
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Log `debug` message.
|
|
45
|
+
*/
|
|
31
46
|
debug(msg, context) {
|
|
32
47
|
if (this._level >= LEVEL.debug)
|
|
33
48
|
this._log('debug', msg, context);
|
|
34
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Log formatter without color highlighting.
|
|
52
|
+
*/
|
|
35
53
|
static stringFormatter(data) {
|
|
36
54
|
if (data.requestId !== undefined)
|
|
37
55
|
return `[${data.time}] [${data.level}] [${data.requestId}] ${data.msg}\n`;
|
|
38
56
|
return `[${data.time}] [${data.level}] ${data.msg}\n`;
|
|
39
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Log formatter for systemd.
|
|
60
|
+
*/
|
|
40
61
|
static systemdFormatter(data) {
|
|
41
62
|
if (data.requestId === undefined)
|
|
42
63
|
return `<${LEVEL[data.level]}>[${data.level.substring(0, 1)}] ${data.msg}\n`;
|
|
43
64
|
return `<${LEVEL[data.level]}>[${data.level.substring(0, 1)}] [${data.requestId}] ${data.msg}\n`;
|
|
44
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Log `error` message.
|
|
68
|
+
*/
|
|
45
69
|
error(msg, context) {
|
|
46
70
|
if (this._level >= LEVEL.error)
|
|
47
71
|
this._log('error', msg, context);
|
|
48
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Log `fatal` message.
|
|
75
|
+
*/
|
|
49
76
|
fatal(msg, context) {
|
|
50
77
|
if (this._level >= LEVEL.fatal)
|
|
51
78
|
this._log('fatal', msg, context);
|
|
52
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Log `infor` message.
|
|
82
|
+
*/
|
|
53
83
|
info(msg, context) {
|
|
54
84
|
if (this._level >= LEVEL.info)
|
|
55
85
|
this._log('info', msg, context);
|
|
56
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* JSON log formatter.
|
|
89
|
+
*/
|
|
57
90
|
static jsonFormatter(data) {
|
|
58
91
|
return JSON.stringify(data);
|
|
59
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Currently active log level.
|
|
95
|
+
*/
|
|
60
96
|
get level() {
|
|
61
97
|
return Object.keys(LEVEL).find(key => LEVEL[key] === this._level);
|
|
62
98
|
}
|
|
@@ -64,10 +100,16 @@ export class Logger {
|
|
|
64
100
|
assert(LEVEL[level] !== undefined, `Unsupported log level "${level}"`);
|
|
65
101
|
this._level = LEVEL[level];
|
|
66
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* Log `trace` message.
|
|
105
|
+
*/
|
|
67
106
|
trace(msg, context) {
|
|
68
107
|
if (this._level >= LEVEL.trace)
|
|
69
108
|
this._log('trace', msg, context);
|
|
70
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Log `warn` message.
|
|
112
|
+
*/
|
|
71
113
|
warn(msg, context) {
|
|
72
114
|
if (this._level >= LEVEL.warn)
|
|
73
115
|
this._log('warn', msg, context);
|
|
@@ -88,26 +130,47 @@ export class Logger {
|
|
|
88
130
|
}
|
|
89
131
|
}
|
|
90
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* Child logger class.
|
|
135
|
+
*/
|
|
91
136
|
export class ChildLogger {
|
|
92
137
|
constructor(parent, context) {
|
|
93
138
|
this.parent = parent;
|
|
94
139
|
this.context = context;
|
|
95
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Log `debug` message.
|
|
143
|
+
*/
|
|
96
144
|
debug(msg) {
|
|
97
145
|
this.parent.debug(msg, this.context);
|
|
98
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Log `error` message.
|
|
149
|
+
*/
|
|
99
150
|
error(msg) {
|
|
100
151
|
this.parent.error(msg, this.context);
|
|
101
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Log `fatal` message.
|
|
155
|
+
*/
|
|
102
156
|
fatal(msg) {
|
|
103
157
|
this.parent.fatal(msg, this.context);
|
|
104
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Log `info` message.
|
|
161
|
+
*/
|
|
105
162
|
info(msg) {
|
|
106
163
|
this.parent.info(msg, this.context);
|
|
107
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* Log `trace` message.
|
|
167
|
+
*/
|
|
108
168
|
trace(msg) {
|
|
109
169
|
this.parent.trace(msg, this.context);
|
|
110
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* Log `warn` message.
|
|
173
|
+
*/
|
|
111
174
|
warn(msg) {
|
|
112
175
|
this.parent.warn(msg, this.context);
|
|
113
176
|
}
|
package/lib/logger.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAEA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,KAAK,MAAM,OAAO,CAAC;AAa1B,MAAM,KAAK,GAA2B;IACpC,KAAK,EAAE,CAAC;IACR,KAAK,EAAE,CAAC;IACR,IAAI,EAAE,CAAC;IACP,IAAI,EAAE,CAAC;IACP,KAAK,EAAE,CAAC;IACR,KAAK,EAAE,CAAC;CACT,CAAC;AAEF,MAAM,OAAO,MAAM;
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAEA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,KAAK,MAAM,OAAO,CAAC;AAa1B,MAAM,KAAK,GAA2B;IACpC,KAAK,EAAE,CAAC;IACR,KAAK,EAAE,CAAC;IACR,IAAI,EAAE,CAAC;IACP,IAAI,EAAE,CAAC;IACP,KAAK,EAAE,CAAC;IACR,KAAK,EAAE,CAAC;CACT,CAAC;AAEF;;GAEG;AACH,MAAM,OAAO,MAAM;IAiBjB,YACE,UAAuG,EAAE;QAT3G;;WAEG;QACH,YAAO,GAAe,EAAE,CAAC;QAGzB,WAAM,GAAG,CAAC,CAAC;QAKT,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC;QACzD,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,MAAM,CA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,CAAC,IAAI,CAAC,CAAC,CAAC;SAC9C;IACH,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,WAAW;IAItB,YAAY,MAAc,EAAE,OAAmB;QAC7C,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,GAAW;QACf,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,GAAW;QACf,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,GAAW;QACf,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,IAAI,CAAC,GAAW;QACd,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACtC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,GAAW;QACf,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,IAAI,CAAC,GAAW;QACd,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACtC,CAAC;CACF"}
|
package/lib/plugins/mt-engine.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { createHash } from 'crypto';
|
|
2
|
-
import {
|
|
2
|
+
import { Cache } from '../cache.js';
|
|
3
3
|
import Path from '@mojojs/path';
|
|
4
4
|
import Template from '@mojojs/template';
|
|
5
|
-
import LRU from 'lru-cache';
|
|
6
5
|
/**
|
|
7
6
|
* `@mojojs/template` engine plugin.
|
|
8
7
|
*/
|
|
@@ -11,7 +10,7 @@ export default function mtEnginePlugin(app) {
|
|
|
11
10
|
}
|
|
12
11
|
class MTEngine {
|
|
13
12
|
constructor() {
|
|
14
|
-
this.cache = new
|
|
13
|
+
this.cache = new Cache();
|
|
15
14
|
}
|
|
16
15
|
async render(ctx, options) {
|
|
17
16
|
let template;
|
|
@@ -19,7 +18,7 @@ class MTEngine {
|
|
|
19
18
|
const checksum = createHash('md5').update(options.inline).digest('hex');
|
|
20
19
|
template = this.cache.get(checksum);
|
|
21
20
|
if (template === undefined) {
|
|
22
|
-
template = new Template(options.inline
|
|
21
|
+
template = new Template(options.inline).compile();
|
|
23
22
|
this.cache.set(checksum, template);
|
|
24
23
|
}
|
|
25
24
|
}
|
|
@@ -29,7 +28,7 @@ class MTEngine {
|
|
|
29
28
|
if (options.viewPath === undefined)
|
|
30
29
|
throw new Error('viewPath is not defined for mtEngine');
|
|
31
30
|
const source = await new Path(options.viewPath).readFile('utf8');
|
|
32
|
-
template = new Template(source.toString(), {
|
|
31
|
+
template = new Template(source.toString(), { name: options.viewPath }).compile();
|
|
33
32
|
this.cache.set(options.viewPath, template);
|
|
34
33
|
}
|
|
35
34
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mt-engine.js","sourceRoot":"","sources":["../../src/plugins/mt-engine.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,UAAU,EAAC,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAC,
|
|
1
|
+
{"version":3,"file":"mt-engine.js","sourceRoot":"","sources":["../../src/plugins/mt-engine.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,UAAU,EAAC,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAC,KAAK,EAAC,MAAM,aAAa,CAAC;AAClC,OAAO,IAAI,MAAM,cAAc,CAAC;AAChC,OAAO,QAAQ,MAAM,kBAAkB,CAAC;AAExC;;GAEG;AACH,M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OAAO,EAAE,CAAC;gBAC/E,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;aAC5C;SACF;QAED,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,QAAQ,CAAC,EAAC,GAAG,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,OAAO,EAAC,CAAC,CAAC,CAAC;IAC3F,CAAC;CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"view-helpers.js","sourceRoot":"","sources":["../../src/plugins/view-helpers.ts"],"names":[],"mappings":"AACA,OAAO,GAAG,
|
|
1
|
+
{"version":3,"file":"view-helpers.js","sourceRoot":"","sources":["../../src/plugins/view-helpers.ts"],"names":[],"mappings":"AACA,OAAO,GAAG,MAAM,aAAa,CAAC;AAC9B,OAAO,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAE5C;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,GAAY;IACpD,GAAG,CAAC,SAAS,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAElC,GAAG,CAAC,SAAS,CAAC,gBAAgB,EAAE,cAAc,CAAC,CAAC;IAChD,GAAG,CAAC,SAAS,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;IACtC,GAAG,CAAC,SAAS,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;IACpC,GAAG,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;AAC5B,CAAC;AAED,KAAK,UAAU,OAAO,CAAC,GAAgB,EAAE,OAAsB,EAAE,KAA0B;IACzF,OAAO,MAAM,GAAG,CAAC,cAAc,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;AAClD,CAAC;AAED,SAAS,cAAc,CAAC,GAAgB;IACtC,OAAO,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,EAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,CAAC,UAAU,CAAC,mBAAmB,CAAC,EAAC,CAAC,CAAC;AACnF,CAAC;AAED,SAAS,SAAS,CAAC,GAAgB,EAAE,MAAc;IACjD,OAAO,GAAG,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAC,GAAG,EAAE,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,EAAC,CAAC,CAAC;AAC1D,CAAC;AAED,SAAS,QAAQ,CAAC,GAAgB,EAAE,MAAc;IAChD,OAAO,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,EAAC,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,EAAC,CAAC,CAAC;AAC5E,CAAC;AAED,SAAS,GAAG,CACV,GAAgB,EAChB,IAAY,EACZ,QAAgC,EAAE,EAClC,UAA+B,EAAE;IAEjC,OAAO,IAAI,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;AACrE,CAAC"}
|
package/lib/renderer.d.ts
CHANGED
|
@@ -14,20 +14,58 @@ declare type ViewIndex = Record<string, ViewSuggestion[]>;
|
|
|
14
14
|
interface ViewEngine {
|
|
15
15
|
render: (ctx: MojoContext, options: RenderOptions) => Promise<Buffer>;
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Renderer class.
|
|
19
|
+
*/
|
|
17
20
|
export declare class Renderer {
|
|
21
|
+
/**
|
|
22
|
+
* Try to negotiate compression for dynamically generated response content and gzip compress it automatically.
|
|
23
|
+
*/
|
|
18
24
|
autoCompress: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* The default template engine to use for rendering in cases where auto-detection doesn't work, like for inline
|
|
27
|
+
* templates, defaults to `mt`.
|
|
28
|
+
*/
|
|
19
29
|
defaultEngine: string;
|
|
30
|
+
/**
|
|
31
|
+
* The default format to render if format is not set, defaults to `html`. Note that changing the default away from
|
|
32
|
+
* `html` is not recommended, as it has the potential to break, for example, plugins with bundled templates.
|
|
33
|
+
*/
|
|
20
34
|
defaultFormat: string;
|
|
35
|
+
/**
|
|
36
|
+
* Template engines.
|
|
37
|
+
*/
|
|
21
38
|
engines: Record<string, ViewEngine>;
|
|
39
|
+
/**
|
|
40
|
+
* Minimum output size in bytes required for compression to be used if enabled, defaults to `860`.
|
|
41
|
+
*/
|
|
22
42
|
minCompressSize: number;
|
|
43
|
+
/**
|
|
44
|
+
* Directories to look for templates in, first one has the highest precedence.
|
|
45
|
+
*/
|
|
23
46
|
viewPaths: string[];
|
|
24
47
|
_viewIndex: ViewIndex | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* Register a template engine.
|
|
50
|
+
*/
|
|
25
51
|
addEngine(name: string, engine: ViewEngine): this;
|
|
52
|
+
/**
|
|
53
|
+
* Find a view for render parameters.
|
|
54
|
+
*/
|
|
26
55
|
findView(options: RenderOptions): ViewSuggestion | null;
|
|
56
|
+
/**
|
|
57
|
+
* Render output through one of the template engines.
|
|
58
|
+
*/
|
|
27
59
|
render(ctx: MojoContext, options: RenderOptions): Promise<EngineResult | null>;
|
|
60
|
+
/**
|
|
61
|
+
* Finalize dynamically generated response content and compress it if possible.
|
|
62
|
+
*/
|
|
28
63
|
respond(ctx: MojoContext, result: EngineResult, options: {
|
|
29
64
|
status?: number;
|
|
30
65
|
}): Promise<boolean>;
|
|
66
|
+
/**
|
|
67
|
+
* Prepare views for rendering.
|
|
68
|
+
*/
|
|
31
69
|
warmup(): Promise<void>;
|
|
32
70
|
_renderInline(ctx: MojoContext, options: RenderOptions): Promise<EngineResult | null>;
|
|
33
71
|
_renderView(ctx: MojoContext, options: RenderOptions): Promise<EngineResult | null>;
|
package/lib/renderer.js
CHANGED
|
@@ -3,20 +3,49 @@ import { gzip } from 'zlib';
|
|
|
3
3
|
import Path from '@mojojs/path';
|
|
4
4
|
import yaml from 'js-yaml';
|
|
5
5
|
const gzipPromise = promisify(gzip);
|
|
6
|
+
/**
|
|
7
|
+
* Renderer class.
|
|
8
|
+
*/
|
|
6
9
|
export class Renderer {
|
|
7
10
|
constructor() {
|
|
11
|
+
/**
|
|
12
|
+
* Try to negotiate compression for dynamically generated response content and gzip compress it automatically.
|
|
13
|
+
*/
|
|
8
14
|
this.autoCompress = true;
|
|
15
|
+
/**
|
|
16
|
+
* The default template engine to use for rendering in cases where auto-detection doesn't work, like for inline
|
|
17
|
+
* templates, defaults to `mt`.
|
|
18
|
+
*/
|
|
9
19
|
this.defaultEngine = 'mt';
|
|
20
|
+
/**
|
|
21
|
+
* The default format to render if format is not set, defaults to `html`. Note that changing the default away from
|
|
22
|
+
* `html` is not recommended, as it has the potential to break, for example, plugins with bundled templates.
|
|
23
|
+
*/
|
|
10
24
|
this.defaultFormat = 'html';
|
|
25
|
+
/**
|
|
26
|
+
* Template engines.
|
|
27
|
+
*/
|
|
11
28
|
this.engines = {};
|
|
29
|
+
/**
|
|
30
|
+
* Minimum output size in bytes required for compression to be used if enabled, defaults to `860`.
|
|
31
|
+
*/
|
|
12
32
|
this.minCompressSize = 860;
|
|
33
|
+
/**
|
|
34
|
+
* Directories to look for templates in, first one has the highest precedence.
|
|
35
|
+
*/
|
|
13
36
|
this.viewPaths = [Path.currentFile().sibling('..', 'vendor', 'views').toString()];
|
|
14
37
|
this._viewIndex = undefined;
|
|
15
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Register a template engine.
|
|
41
|
+
*/
|
|
16
42
|
addEngine(name, engine) {
|
|
17
43
|
this.engines[name] = engine;
|
|
18
44
|
return this;
|
|
19
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Find a view for render parameters.
|
|
48
|
+
*/
|
|
20
49
|
findView(options) {
|
|
21
50
|
const view = options.view;
|
|
22
51
|
const index = this._viewIndex;
|
|
@@ -36,6 +65,9 @@ export class Renderer {
|
|
|
36
65
|
}
|
|
37
66
|
return fallback ?? views[0];
|
|
38
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Render output through one of the template engines.
|
|
70
|
+
*/
|
|
39
71
|
async render(ctx, options) {
|
|
40
72
|
const log = ctx.log;
|
|
41
73
|
if (options.text !== undefined) {
|
|
@@ -94,6 +126,9 @@ export class Renderer {
|
|
|
94
126
|
}
|
|
95
127
|
return null;
|
|
96
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Finalize dynamically generated response content and compress it if possible.
|
|
131
|
+
*/
|
|
97
132
|
async respond(ctx, result, options) {
|
|
98
133
|
const res = ctx.res;
|
|
99
134
|
if (res.isSent)
|
|
@@ -113,6 +148,9 @@ export class Renderer {
|
|
|
113
148
|
await res.type(type).send(output);
|
|
114
149
|
return true;
|
|
115
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Prepare views for rendering.
|
|
153
|
+
*/
|
|
116
154
|
async warmup() {
|
|
117
155
|
const viewIndex = (this._viewIndex = {});
|
|
118
156
|
for (const dir of this.viewPaths.map(path => new Path(path))) {
|