@mojojs/core 0.25.0 → 0.28.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/README.md +2 -2
- package/lib/app.d.ts +11 -7
- package/lib/app.js +24 -17
- package/lib/app.js.map +1 -1
- package/lib/body.d.ts +9 -5
- package/lib/body.js +14 -15
- package/lib/body.js.map +1 -1
- package/lib/cli/get.js +3 -25
- package/lib/cli/get.js.map +1 -1
- package/lib/cli/server.js +32 -9
- package/lib/cli/server.js.map +1 -1
- package/lib/context.d.ts +5 -5
- package/lib/context.js +4 -5
- package/lib/context.js.map +1 -1
- package/lib/headers.d.ts +34 -0
- package/lib/headers.js +97 -0
- package/lib/headers.js.map +1 -0
- package/lib/plugins/default-conditions.d.ts +5 -0
- package/lib/plugins/{header-conditions.js → default-conditions.js} +3 -3
- package/lib/plugins/default-conditions.js.map +1 -0
- package/lib/plugins/default-helpers.js +122 -0
- package/lib/plugins/default-helpers.js.map +1 -1
- package/lib/renderer.js +1 -1
- package/lib/renderer.js.map +1 -1
- package/lib/server/request.d.ts +24 -15
- package/lib/server/request.js +20 -29
- package/lib/server/request.js.map +1 -1
- package/lib/server/response.d.ts +23 -9
- package/lib/server/response.js +35 -48
- package/lib/server/response.js.map +1 -1
- package/lib/server.d.ts +22 -8
- package/lib/server.js +66 -6
- package/lib/server.js.map +1 -1
- package/lib/static.js +7 -7
- package/lib/static.js.map +1 -1
- package/lib/types.d.ts +18 -5
- package/lib/user-agent/mock.d.ts +3 -3
- package/lib/user-agent/mock.js +4 -4
- package/lib/user-agent/mock.js.map +1 -1
- package/lib/user-agent/response.d.ts +22 -11
- package/lib/user-agent/response.js +9 -21
- package/lib/user-agent/response.js.map +1 -1
- package/lib/user-agent/test.d.ts +2 -2
- package/lib/user-agent/test.js +5 -5
- package/lib/user-agent/test.js.map +1 -1
- package/lib/user-agent/transport/http.js +21 -1
- package/lib/user-agent/transport/http.js.map +1 -1
- package/lib/user-agent/transport/ws.js +11 -2
- package/lib/user-agent/transport/ws.js.map +1 -1
- package/lib/user-agent.js +5 -5
- package/lib/user-agent.js.map +1 -1
- package/lib/util.d.ts +4 -0
- package/lib/util.js +10 -0
- package/lib/util.js.map +1 -1
- package/lib/websocket.d.ts +1 -1
- package/lib/websocket.js +6 -6
- package/lib/websocket.js.map +1 -1
- package/package.json +1 -1
- package/vendor/views/mojo/debug.html.mt +5 -4
- package/lib/mock/request.d.ts +0 -9
- package/lib/mock/request.js +0 -13
- package/lib/mock/request.js.map +0 -1
- package/lib/mock/response.d.ts +0 -7
- package/lib/mock/response.js +0 -7
- package/lib/mock/response.js.map +0 -1
- package/lib/plugins/exception-helpers.d.ts +0 -5
- package/lib/plugins/exception-helpers.js +0 -101
- package/lib/plugins/exception-helpers.js.map +0 -1
- package/lib/plugins/header-conditions.d.ts +0 -5
- package/lib/plugins/header-conditions.js.map +0 -1
- package/lib/plugins/view-helpers.d.ts +0 -5
- package/lib/plugins/view-helpers.js +0 -33
- package/lib/plugins/view-helpers.js.map +0 -1
package/lib/server/response.js
CHANGED
|
@@ -1,52 +1,59 @@
|
|
|
1
|
-
import
|
|
1
|
+
import EventEmitter from 'events';
|
|
2
|
+
import { Headers } from '../headers.js';
|
|
2
3
|
import { stringifyCookie } from './cookie.js';
|
|
3
4
|
/**
|
|
4
5
|
* Server response class.
|
|
5
6
|
*/
|
|
6
|
-
export class ServerResponse {
|
|
7
|
-
constructor(
|
|
7
|
+
export class ServerResponse extends EventEmitter {
|
|
8
|
+
constructor(sendResponse) {
|
|
9
|
+
super();
|
|
10
|
+
/**
|
|
11
|
+
* Response headers.
|
|
12
|
+
*/
|
|
13
|
+
this.headers = new Headers();
|
|
8
14
|
/**
|
|
9
15
|
* Response has already been sent.
|
|
10
16
|
*/
|
|
11
17
|
this.isSent = false;
|
|
12
18
|
/**
|
|
13
|
-
* Response status.
|
|
19
|
+
* Response status code.
|
|
14
20
|
*/
|
|
15
21
|
this.statusCode = 200;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Response status message.
|
|
24
|
+
*/
|
|
25
|
+
this.statusMessage = null;
|
|
26
|
+
this._ctx = undefined;
|
|
27
|
+
this._sendResponse = sendResponse;
|
|
19
28
|
}
|
|
20
29
|
/**
|
|
21
30
|
* Append header value.
|
|
22
31
|
*/
|
|
23
32
|
append(name, value) {
|
|
24
|
-
|
|
25
|
-
const old = headers[name];
|
|
26
|
-
if (old === undefined) {
|
|
27
|
-
headers[name] = value;
|
|
28
|
-
}
|
|
29
|
-
else if (Array.isArray(old)) {
|
|
30
|
-
old.push(value);
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
headers[name] = [old, value];
|
|
34
|
-
}
|
|
33
|
+
this.headers.append(name, value);
|
|
35
34
|
return this;
|
|
36
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Bind context to response.
|
|
38
|
+
*/
|
|
39
|
+
bindContext(ctx) {
|
|
40
|
+
this._ctx = new WeakRef(ctx);
|
|
41
|
+
}
|
|
37
42
|
/**
|
|
38
43
|
* Set response content length.
|
|
39
44
|
*/
|
|
40
45
|
length(len) {
|
|
41
|
-
this.
|
|
42
|
-
return this;
|
|
46
|
+
return this.set('Content-Length', len.toString());
|
|
43
47
|
}
|
|
44
48
|
/**
|
|
45
49
|
* Send response.
|
|
46
50
|
*/
|
|
47
51
|
async send(body) {
|
|
48
52
|
this.isSent = true;
|
|
49
|
-
const
|
|
53
|
+
const ctxRef = this._ctx;
|
|
54
|
+
if (ctxRef === undefined)
|
|
55
|
+
return;
|
|
56
|
+
const ctx = ctxRef.deref();
|
|
50
57
|
if (ctx === undefined)
|
|
51
58
|
return;
|
|
52
59
|
const app = ctx.app;
|
|
@@ -55,55 +62,35 @@ export class ServerResponse {
|
|
|
55
62
|
body = newBody;
|
|
56
63
|
if (ctx.isSessionActive === true)
|
|
57
64
|
await app.session.store(ctx, await ctx.session());
|
|
58
|
-
|
|
59
|
-
if (typeof body === 'string' || Buffer.isBuffer(body)) {
|
|
60
|
-
this.length(Buffer.byteLength(body));
|
|
61
|
-
raw.writeHead(this.statusCode, this._headers);
|
|
62
|
-
raw.end(body);
|
|
63
|
-
}
|
|
64
|
-
else if (body instanceof Stream) {
|
|
65
|
-
raw.writeHead(this.statusCode, this._headers);
|
|
66
|
-
body.pipe(this._raw);
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
raw.writeHead(this.statusCode, this._headers);
|
|
70
|
-
raw.end();
|
|
71
|
-
}
|
|
65
|
+
this._sendResponse(this, body);
|
|
72
66
|
}
|
|
73
67
|
/**
|
|
74
68
|
* Set HTTP header for response.
|
|
75
69
|
*/
|
|
76
70
|
set(name, value) {
|
|
77
|
-
|
|
78
|
-
let header = this._headers[name];
|
|
79
|
-
if (typeof header === 'string')
|
|
80
|
-
header = this._headers[name] = [header];
|
|
81
|
-
header.push(value);
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
this._headers[name] = value;
|
|
85
|
-
}
|
|
71
|
+
this.headers.set(name, value);
|
|
86
72
|
return this;
|
|
87
73
|
}
|
|
88
74
|
/**
|
|
89
75
|
* Set cookie value.
|
|
90
76
|
*/
|
|
91
77
|
setCookie(name, value, options) {
|
|
92
|
-
return this.
|
|
78
|
+
return this.append('Set-Cookie', stringifyCookie(name, value, options));
|
|
93
79
|
}
|
|
94
80
|
/**
|
|
95
81
|
* Set response status.
|
|
96
82
|
*/
|
|
97
|
-
status(code) {
|
|
83
|
+
status(code, message) {
|
|
98
84
|
this.statusCode = code;
|
|
85
|
+
if (message !== undefined)
|
|
86
|
+
this.statusMessage = message;
|
|
99
87
|
return this;
|
|
100
88
|
}
|
|
101
89
|
/**
|
|
102
90
|
* Set response content type.
|
|
103
91
|
*/
|
|
104
92
|
type(type) {
|
|
105
|
-
this.
|
|
106
|
-
return this;
|
|
93
|
+
return this.set('Content-Type', type);
|
|
107
94
|
}
|
|
108
95
|
}
|
|
109
96
|
//# sourceMappingURL=response.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"response.js","sourceRoot":"","sources":["../../src/server/response.ts"],"names":[],"mappings":"AAGA,OAAO,
|
|
1
|
+
{"version":3,"file":"response.js","sourceRoot":"","sources":["../../src/server/response.ts"],"names":[],"mappings":"AAGA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAC;AACtC,OAAO,EAAC,eAAe,EAAC,MAAM,aAAa,CAAC;AAI5C;;GAEG;AACH,MAAM,OAAO,cAAe,SAAQ,YAAY;IAqB9C,YAAY,YAA0B;QACpC,KAAK,EAAE,CAAC;QArBV;;WAEG;QACH,YAAO,GAAG,IAAI,OAAO,EAAE,CAAC;QACxB;;WAEG;QACH,WAAM,GAAG,KAAK,CAAC;QACf;;WAEG;QACH,eAAU,GAAG,GAAG,CAAC;QACjB;;WAEG;QACH,kBAAa,GAAkB,IAAI,CAAC;QAEpC,SAAI,GAAiC,SAAS,CAAC;QAK7C,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;IACpC,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,IAAY,EAAE,KAAa;QAChC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QACjC,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACH,WAAW,CAAC,GAAY;QACtB,IAAI,CAAC,IAAI,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC;IAC/B,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,GAAW;QAChB,OAAO,IAAI,CAAC,GAAG,CAAC,gBAAgB,EAAE,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;IACpD,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,IAAI,CAAC,IAA+B;QACxC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QAEnB,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC;QACzB,I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/server.d.ts
CHANGED
|
@@ -1,18 +1,13 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import type { App } from './app.js';
|
|
3
|
+
import type { ServerOptions } from './types.js';
|
|
3
4
|
import type { Socket } from 'net';
|
|
4
5
|
import http from 'http';
|
|
5
6
|
import https from 'https';
|
|
6
7
|
import { URL } from 'url';
|
|
8
|
+
import { ServerRequest } from './server/request.js';
|
|
7
9
|
import { WebSocketServer } from 'ws';
|
|
8
10
|
declare type ListenArgs = any[];
|
|
9
|
-
interface ServerOptions {
|
|
10
|
-
cluster?: boolean;
|
|
11
|
-
listen?: string[];
|
|
12
|
-
quiet?: boolean;
|
|
13
|
-
reverseProxy?: boolean;
|
|
14
|
-
workers?: number;
|
|
15
|
-
}
|
|
16
11
|
/**
|
|
17
12
|
* Server class.
|
|
18
13
|
*/
|
|
@@ -21,6 +16,24 @@ export declare class Server {
|
|
|
21
16
|
* Application this server handles.
|
|
22
17
|
*/
|
|
23
18
|
app: App;
|
|
19
|
+
/**
|
|
20
|
+
* Limit the amount of time the parser will wait to receive the complete HTTP headers, defaults to `60000`
|
|
21
|
+
* (60 seconds).
|
|
22
|
+
*/
|
|
23
|
+
headersTimeout: number | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Limit the amount of time of inactivity a server needs to wait for additional incoming data, after it has finished
|
|
26
|
+
* writing the last response, before a socket will be destroyed, defaults to `5000` (5 seconds).
|
|
27
|
+
*/
|
|
28
|
+
keepAliveTimeout: number | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Maximum number of requests socket can handle before closing keep alive connection, defaults to `0`.
|
|
31
|
+
*/
|
|
32
|
+
maxRequestsPerSocket: number | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Limit the amount of time for receiving the entire request from the client, defaults to `300000` (300 seconds).
|
|
35
|
+
*/
|
|
36
|
+
requestTimeout: number | undefined;
|
|
24
37
|
/**
|
|
25
38
|
* Reverse proxy mode.
|
|
26
39
|
*/
|
|
@@ -48,7 +61,8 @@ export declare class Server {
|
|
|
48
61
|
*/
|
|
49
62
|
stop(): Promise<void>;
|
|
50
63
|
_createServer(location: string): Promise<void>;
|
|
51
|
-
_handleRequest(req: http.IncomingMessage,
|
|
64
|
+
_handleRequest(req: http.IncomingMessage, raw: http.ServerResponse): void;
|
|
52
65
|
_handleUpgrade(wss: WebSocketServer, req: http.IncomingMessage, socket: Socket, head: Buffer): void;
|
|
66
|
+
_prepareRequest(req: http.IncomingMessage, socket: Socket, isWebSocket: boolean): ServerRequest;
|
|
53
67
|
}
|
|
54
68
|
export {};
|
package/lib/server.js
CHANGED
|
@@ -2,7 +2,11 @@ import cluster from 'cluster';
|
|
|
2
2
|
import http from 'http';
|
|
3
3
|
import https from 'https';
|
|
4
4
|
import os from 'os';
|
|
5
|
+
import { Stream } from 'stream';
|
|
5
6
|
import { URL } from 'url';
|
|
7
|
+
import { ServerRequest } from './server/request.js';
|
|
8
|
+
import { ServerResponse } from './server/response.js';
|
|
9
|
+
import { termEscape } from './util.js';
|
|
6
10
|
import { WebSocket } from './websocket.js';
|
|
7
11
|
import Path from '@mojojs/path';
|
|
8
12
|
import { WebSocketServer } from 'ws';
|
|
@@ -17,6 +21,10 @@ export class Server {
|
|
|
17
21
|
this.urls = [];
|
|
18
22
|
this._servers = [];
|
|
19
23
|
this.app = app;
|
|
24
|
+
this.headersTimeout = options.headersTimeout;
|
|
25
|
+
this.keepAliveTimeout = options.keepAliveTimeout;
|
|
26
|
+
this.maxRequestsPerSocket = options.maxRequestsPerSocket;
|
|
27
|
+
this.requestTimeout = options.requestTimeout;
|
|
20
28
|
this.reverseProxy = options.reverseProxy ?? false;
|
|
21
29
|
this._cluster = options.cluster ?? false;
|
|
22
30
|
this._listen = options.listen ?? ['http://*:3000'];
|
|
@@ -81,14 +89,22 @@ export class Server {
|
|
|
81
89
|
const wss = new WebSocketServer({ noServer: true });
|
|
82
90
|
const server = (isHttps ? https : http).createServer(options, this._handleRequest.bind(this));
|
|
83
91
|
this._servers.push(server);
|
|
92
|
+
if (this.maxRequestsPerSocket !== undefined)
|
|
93
|
+
server.maxRequestsPerSocket = this.maxRequestsPerSocket;
|
|
94
|
+
if (this.headersTimeout !== undefined)
|
|
95
|
+
server.headersTimeout = this.headersTimeout;
|
|
96
|
+
if (this.keepAliveTimeout !== undefined)
|
|
97
|
+
server.keepAliveTimeout = this.keepAliveTimeout;
|
|
98
|
+
if (this.requestTimeout !== undefined)
|
|
99
|
+
server.requestTimeout = this.requestTimeout;
|
|
84
100
|
server.on('upgrade', this._handleUpgrade.bind(this, wss));
|
|
85
101
|
if (process.env.MOJO_SERVER_DEBUG === '1') {
|
|
86
|
-
server.on('connection', socket => {
|
|
102
|
+
server.on('connection', (socket) => {
|
|
87
103
|
const stderr = process.stderr;
|
|
88
|
-
socket.on('data', (chunk) => stderr.write(`-- Server <<< Client\n${chunk}`));
|
|
104
|
+
socket.on('data', (chunk) => stderr.write(termEscape(`-- Server <<< Client\n${chunk}`)));
|
|
89
105
|
const write = socket.write;
|
|
90
106
|
socket.write = (chunk, cb) => {
|
|
91
|
-
stderr.write(`-- Server >>> Client\n${chunk}`);
|
|
107
|
+
stderr.write(termEscape(`-- Server >>> Client\n${chunk}`));
|
|
92
108
|
return write.apply(socket, [chunk, cb]);
|
|
93
109
|
};
|
|
94
110
|
});
|
|
@@ -113,14 +129,20 @@ export class Server {
|
|
|
113
129
|
});
|
|
114
130
|
});
|
|
115
131
|
}
|
|
116
|
-
_handleRequest(req,
|
|
132
|
+
_handleRequest(req, raw) {
|
|
117
133
|
const app = this.app;
|
|
118
|
-
const
|
|
134
|
+
const socket = req.socket;
|
|
135
|
+
const ctx = app.newContext(this._prepareRequest(req, socket, false), new ServerResponse(function (res, body) {
|
|
136
|
+
_sendResponse(res, body, raw);
|
|
137
|
+
}));
|
|
138
|
+
raw.on('finish', () => ctx.emit('finish'));
|
|
119
139
|
app.handleRequest(ctx).catch(error => ctx.exception(error));
|
|
120
140
|
}
|
|
121
141
|
_handleUpgrade(wss, req, socket, head) {
|
|
122
142
|
const app = this.app;
|
|
123
|
-
const ctx = app.newContext(req,
|
|
143
|
+
const ctx = app.newContext(this._prepareRequest(req, socket, true), new ServerResponse(function (res, body) {
|
|
144
|
+
_sendResponse(res, body, new http.ServerResponse(req));
|
|
145
|
+
}));
|
|
124
146
|
app
|
|
125
147
|
.handleRequest(ctx)
|
|
126
148
|
.then(() => {
|
|
@@ -139,5 +161,43 @@ export class Server {
|
|
|
139
161
|
return ctx.exception(error);
|
|
140
162
|
});
|
|
141
163
|
}
|
|
164
|
+
_prepareRequest(req, socket, isWebSocket) {
|
|
165
|
+
return new ServerRequest({
|
|
166
|
+
body: req,
|
|
167
|
+
headers: req.rawHeaders,
|
|
168
|
+
isSecure: socket.encrypted ?? false,
|
|
169
|
+
isWebSocket: isWebSocket,
|
|
170
|
+
method: req.method,
|
|
171
|
+
remoteAddress: socket.remoteAddress,
|
|
172
|
+
reverseProxy: this.reverseProxy,
|
|
173
|
+
url: req.url
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function _sendHeaders(res, raw) {
|
|
178
|
+
const statusCode = res.statusCode;
|
|
179
|
+
const statusMessage = res.statusMessage;
|
|
180
|
+
const headers = res.headers.toArray();
|
|
181
|
+
if (statusMessage === null) {
|
|
182
|
+
raw.writeHead(statusCode, headers);
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
raw.writeHead(statusCode, statusMessage, headers);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function _sendResponse(res, body, raw) {
|
|
189
|
+
if (typeof body === 'string' || Buffer.isBuffer(body)) {
|
|
190
|
+
res.length(Buffer.byteLength(body));
|
|
191
|
+
_sendHeaders(res, raw);
|
|
192
|
+
raw.end(body);
|
|
193
|
+
}
|
|
194
|
+
else if (body instanceof Stream) {
|
|
195
|
+
_sendHeaders(res, raw);
|
|
196
|
+
body.pipe(raw);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
_sendHeaders(res, raw);
|
|
200
|
+
raw.end();
|
|
201
|
+
}
|
|
142
202
|
}
|
|
143
203
|
//# sourceMappingURL=server.js.map
|
package/lib/server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAGA,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAC,MAAM,EAAC,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAC,GAAG,EAAC,MAAM,KAAK,CAAC;AACxB,OAAO,EAAC,aAAa,EAAC,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAC,cAAc,EAAC,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAC,UAAU,EAAC,MAAM,WAAW,CAAC;AACrC,OAAO,EAAC,SAAS,EAAC,MAAM,gBAAgB,CAAC;AACzC,OAAO,IAAI,MAAM,cAAc,CAAC;AAChC,OAAO,EAAC,eAAe,EAAC,MAAM,IAAI,CAAC;AAMnC;;GAEG;AACH,MAAM,OAAO,MAAM;IAsCjB,YAAY,GAAQ,EAAE,UAAyB,EAAE;QAXjD;;WAEG;QACH,SAAI,GAAU,EAAE,CAAC;QAIjB,aAAQ,GAAsC,EAAE,CAAC;QAK/C,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;QAC7C,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;QACjD,IAAI,CAAC,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC;QACzD,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,cAAc,CAA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AC;IACL,CAAC;CACF;AAED,SAAS,YAAY,CAAC,GAAmB,EAAE,GAAwB;IACjE,MAAM,UAAU,GAAG,GAAG,CAAC,UAAU,CAAC;IAClC,MAAM,aAAa,GAAG,GAAG,CAAC,aAAa,CAAC;IACxC,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;IAEtC,IAAI,aAAa,KAAK,IAAI,EAAE;QAC1B,GAAG,CAAC,SAAS,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;KACpC;SAAM;QACL,GAAG,CAAC,SAAS,CAAC,UAAU,EAAE,aAAa,EAAE,OAAO,CAAC,CAAC;KACnD;AACH,CAAC;AAED,SAAS,aAAa,CAAC,GAAmB,EAAE,IAAkB,EAAE,GAAwB;IACtF,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;QACrD,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC;QACpC,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACvB,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;KACf;SAAM,IAAI,IAAI,YAAY,MAAM,EAAE;QACjC,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACvB,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;KAChB;SAAM;QACL,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACvB,GAAG,CAAC,GAAG,EAAE,CAAC;KACX;AACH,CAAC"}
|
package/lib/static.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import crypto from 'crypto';
|
|
2
|
-
import path from 'path';
|
|
3
2
|
import Path from '@mojojs/path';
|
|
4
3
|
/**
|
|
5
4
|
* Static file server class.
|
|
@@ -26,11 +25,12 @@ export class Static {
|
|
|
26
25
|
const method = req.method;
|
|
27
26
|
if (method !== 'GET' && method !== 'HEAD')
|
|
28
27
|
return false;
|
|
29
|
-
const relative = unsafePath.replace(this.prefix, '').split('/')
|
|
30
|
-
|
|
28
|
+
const relative = new Path(...unsafePath.replace(this.prefix, '').split('/'));
|
|
29
|
+
const relativePath = relative.toString();
|
|
30
|
+
if (relativePath !== relative.normalize().toString() || relativePath.startsWith('..'))
|
|
31
31
|
return false;
|
|
32
32
|
for (const dir of this.publicPaths) {
|
|
33
|
-
const file = new Path(dir,
|
|
33
|
+
const file = new Path(dir, relativePath);
|
|
34
34
|
if ((await file.isReadable()) !== true || (await file.stat()).isDirectory() === true)
|
|
35
35
|
continue;
|
|
36
36
|
await this.serveFile(ctx, file);
|
|
@@ -61,7 +61,7 @@ export class Static {
|
|
|
61
61
|
// If-None-Match
|
|
62
62
|
const req = ctx.req;
|
|
63
63
|
const ifNoneMatch = req.get('If-None-Match');
|
|
64
|
-
if (etag !== undefined && ifNoneMatch !==
|
|
64
|
+
if (etag !== undefined && ifNoneMatch !== null) {
|
|
65
65
|
const etags = ifNoneMatch.split(/,\s+/).map(value => value.replaceAll('"', ''));
|
|
66
66
|
for (const match of etags) {
|
|
67
67
|
if (match === etag)
|
|
@@ -70,7 +70,7 @@ export class Static {
|
|
|
70
70
|
}
|
|
71
71
|
// If-Modified-Since
|
|
72
72
|
const ifModifiedSince = req.get('If-Modified-Since');
|
|
73
|
-
if (options.lastModified !== undefined && ifModifiedSince !==
|
|
73
|
+
if (options.lastModified !== undefined && ifModifiedSince !== null) {
|
|
74
74
|
const epoch = Date.parse(ifModifiedSince);
|
|
75
75
|
if (isNaN(epoch))
|
|
76
76
|
return false;
|
|
@@ -102,7 +102,7 @@ export class Static {
|
|
|
102
102
|
return;
|
|
103
103
|
}
|
|
104
104
|
// Entire file
|
|
105
|
-
if (range ===
|
|
105
|
+
if (range === null) {
|
|
106
106
|
await res.status(200).length(length).type(type).send(file.createReadStream());
|
|
107
107
|
return;
|
|
108
108
|
}
|
package/lib/static.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"static.js","sourceRoot":"","sources":["../src/static.ts"],"names":[],"mappings":"AACA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,IAAI,MAAM,
|
|
1
|
+
{"version":3,"file":"static.js","sourceRoot":"","sources":["../src/static.ts"],"names":[],"mappings":"AACA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,IAAI,MAAM,cAAc,CAAC;AAEhC;;GAEG;AACH,MAAM,OAAO,MAAM;IAAnB;QACE;;WAEG;QACH,WAAM,GAAG,SAAS,CAAC;QACnB;;WAEG;QACH,gBAAW,GAAG,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;IAyHlF,CAAC;IAvHC;;OAEG;IACH,KAAK,CAAC,QAAQ,CAAC,GAAgB;QAC7B,MAAM,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC;QACpB,MAAM,UAAU,GAAG,GAAG,CAAC,IAAI,CAAC;QAC5B,IAAI,UAAU,KAAK,IAAI,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC;YAAE,OAAO,KAAK,CAAC;QAE7E,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC;QAC1B,IAAI,MAAM,KAAK,KAAK,IAAI,MAAM,KAAK,MAAM;YAAE,OAAO,KAAK,CAAC;QAExD,MAAM,QAAQ,GAAG,IAAI,IAAI,CAAC,GAAG,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;QAC7E,MAAM,YAAY,GAAG,QAAQ,CAAC,QAAQ,EAAE,CAAC;QACzC,IAAI,YAAY,KAAK,QAAQ,CAAC,SAAS,EAAE,CAAC,QAAQ,EAAE,IAAI,YAAY,CAAC,UAAU,CAAC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K,CAAC,CAAC,CAAC,CAAC,CAAC;QAC/B,IAAI,KAAK,CAAC,KAAK,CAAC;YAAE,KAAK,GAAG,CAAC,CAAC;QAC5B,IAAI,GAAG,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAC7B,IAAI,KAAK,CAAC,GAAG,CAAC;YAAE,GAAG,GAAG,MAAM,GAAG,CAAC,CAAC;QACjC,IAAI,KAAK,GAAG,GAAG;YAAE,OAAO,MAAM,IAAI,CAAC,oBAAoB,CAAC,GAAG,CAAC,CAAC;QAE7D,QAAQ;QACR,MAAM,GAAG;aACN,MAAM,CAAC,GAAG,CAAC;aACX,MAAM,CAAC,GAAG,GAAG,KAAK,GAAG,CAAC,CAAC;aACvB,IAAI,CAAC,IAAI,CAAC;aACV,GAAG,CAAC,eAAe,EAAE,SAAS,KAAK,IAAI,GAAG,IAAI,MAAM,EAAE,CAAC;aACvD,IAAI,CAAC,IAAI,CAAC,gBAAgB,CAAC,EAAC,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,EAAC,CAAC,CAAC,CAAC;IAC3D,CAAC;IAED,KAAK,CAAC,oBAAoB,CAAC,GAAgB;QACzC,MAAM,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;IACnC,CAAC;CACF"}
|
package/lib/types.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type { App } from './app.js';
|
|
|
4
4
|
import type { Context } from './context.js';
|
|
5
5
|
import type { ValidatorResult } from './validator/result.js';
|
|
6
6
|
import type { Agent } from 'http';
|
|
7
|
+
import type { Readable } from 'stream';
|
|
7
8
|
import type { CookieJar } from 'tough-cookie';
|
|
8
9
|
import type { URL } from 'url';
|
|
9
10
|
export declare type Expand<T> = T extends infer O ? {
|
|
@@ -57,11 +58,6 @@ export interface CookieOptions {
|
|
|
57
58
|
sameSite?: 'lax' | 'strict' | 'none';
|
|
58
59
|
secure?: boolean;
|
|
59
60
|
}
|
|
60
|
-
export interface MockRequestOptions {
|
|
61
|
-
headers?: Record<string, string>;
|
|
62
|
-
method?: string;
|
|
63
|
-
url?: string;
|
|
64
|
-
}
|
|
65
61
|
export interface RenderOptions {
|
|
66
62
|
engine?: string;
|
|
67
63
|
format?: string;
|
|
@@ -77,9 +73,26 @@ export interface RenderOptions {
|
|
|
77
73
|
view?: string;
|
|
78
74
|
[key: string]: any;
|
|
79
75
|
}
|
|
76
|
+
export interface ServerOptions {
|
|
77
|
+
cluster?: boolean;
|
|
78
|
+
headersTimeout?: number;
|
|
79
|
+
keepAliveTimeout?: number;
|
|
80
|
+
listen?: string[];
|
|
81
|
+
maxRequestsPerSocket?: number;
|
|
82
|
+
quiet?: boolean;
|
|
83
|
+
requestTimeout?: number;
|
|
84
|
+
reverseProxy?: boolean;
|
|
85
|
+
workers?: number;
|
|
86
|
+
}
|
|
80
87
|
export interface ServerRequestOptions {
|
|
88
|
+
body: Readable;
|
|
89
|
+
headers: string[];
|
|
90
|
+
isSecure: boolean;
|
|
81
91
|
isWebSocket: boolean;
|
|
92
|
+
method?: string;
|
|
93
|
+
remoteAddress?: string;
|
|
82
94
|
reverseProxy: boolean;
|
|
95
|
+
url?: string;
|
|
83
96
|
}
|
|
84
97
|
export interface UserAgentOptions {
|
|
85
98
|
baseURL?: string | URL;
|
package/lib/user-agent/mock.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { App } from '../app.js';
|
|
2
|
-
import type { UserAgentOptions } from '../types.js';
|
|
2
|
+
import type { ServerOptions, UserAgentOptions } from '../types.js';
|
|
3
3
|
import { Server } from '../server.js';
|
|
4
4
|
import { UserAgent } from '../user-agent.js';
|
|
5
5
|
/**
|
|
@@ -14,11 +14,11 @@ export declare class MockUserAgent extends UserAgent {
|
|
|
14
14
|
/**
|
|
15
15
|
* Create a new mock user agent.
|
|
16
16
|
*/
|
|
17
|
-
static newMockUserAgent(app: App, options?: UserAgentOptions): Promise<MockUserAgent>;
|
|
17
|
+
static newMockUserAgent(app: App, options?: UserAgentOptions, serverOptions?: ServerOptions): Promise<MockUserAgent>;
|
|
18
18
|
/**
|
|
19
19
|
* Start mock server.
|
|
20
20
|
*/
|
|
21
|
-
start(app: App): Promise<this>;
|
|
21
|
+
start(app: App, options?: ServerOptions): Promise<this>;
|
|
22
22
|
/**
|
|
23
23
|
* Stop mock server.
|
|
24
24
|
*/
|
package/lib/user-agent/mock.js
CHANGED
|
@@ -15,14 +15,14 @@ export class MockUserAgent extends UserAgent {
|
|
|
15
15
|
/**
|
|
16
16
|
* Create a new mock user agent.
|
|
17
17
|
*/
|
|
18
|
-
static async newMockUserAgent(app, options) {
|
|
19
|
-
return await new MockUserAgent(options).start(app);
|
|
18
|
+
static async newMockUserAgent(app, options, serverOptions) {
|
|
19
|
+
return await new MockUserAgent(options).start(app, serverOptions);
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
22
22
|
* Start mock server.
|
|
23
23
|
*/
|
|
24
|
-
async start(app) {
|
|
25
|
-
const server = (this.server = new Server(app, { listen: ['http://*'], quiet: true }));
|
|
24
|
+
async start(app, options = {}) {
|
|
25
|
+
const server = (this.server = new Server(app, { ...options, listen: ['http://*'], quiet: true }));
|
|
26
26
|
await server.start();
|
|
27
27
|
if (this.baseURL === undefined)
|
|
28
28
|
this.baseURL = server.urls[0];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mock.js","sourceRoot":"","sources":["../../src/user-agent/mock.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AACpC,OAAO,EAAC,SAAS,EAAC,MAAM,kBAAkB,CAAC;AAE3C;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,SAAS;IAM1C,YAAY,OAA0B;QACpC,KAAK,CAAC,OAAO,CAAC,CAAC;QANjB;;WAEG;QACH,WAAM,GAAuB,SAAS,CAAC;QAKrC,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;IAC1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AAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAClC,CAAC;CACF"}
|
|
@@ -1,12 +1,30 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Readable } from 'stream';
|
|
1
3
|
import { Body } from '../body.js';
|
|
4
|
+
interface UserAgentResponseOptions {
|
|
5
|
+
body: Readable;
|
|
6
|
+
headers: string[];
|
|
7
|
+
httpVersion: string;
|
|
8
|
+
statusCode: number;
|
|
9
|
+
statusMessage: string;
|
|
10
|
+
}
|
|
2
11
|
/**
|
|
3
12
|
* User agent response class.
|
|
4
13
|
*/
|
|
5
14
|
export declare class UserAgentResponse extends Body {
|
|
6
15
|
/**
|
|
7
|
-
*
|
|
16
|
+
* HTTP version.
|
|
17
|
+
*/
|
|
18
|
+
httpVersion: string;
|
|
19
|
+
/**
|
|
20
|
+
* Response status code.
|
|
21
|
+
*/
|
|
22
|
+
statusCode: number;
|
|
23
|
+
/**
|
|
24
|
+
* Response status message.
|
|
8
25
|
*/
|
|
9
|
-
|
|
26
|
+
statusMessage: string;
|
|
27
|
+
constructor(options: UserAgentResponseOptions);
|
|
10
28
|
/**
|
|
11
29
|
* Check if response has a `4xx` response status code.
|
|
12
30
|
*/
|
|
@@ -27,16 +45,9 @@ export declare class UserAgentResponse extends Body {
|
|
|
27
45
|
* Check if response has a `2xx` response status code.
|
|
28
46
|
*/
|
|
29
47
|
get isSuccess(): boolean;
|
|
30
|
-
/**
|
|
31
|
-
* Response status code.
|
|
32
|
-
*/
|
|
33
|
-
get status(): number;
|
|
34
|
-
/**
|
|
35
|
-
* Get response message.
|
|
36
|
-
*/
|
|
37
|
-
get statusMessage(): string | undefined;
|
|
38
48
|
/**
|
|
39
49
|
* Get `Content-Type` header value.
|
|
40
50
|
*/
|
|
41
|
-
get type(): string |
|
|
51
|
+
get type(): string | null;
|
|
42
52
|
}
|
|
53
|
+
export {};
|
|
@@ -3,17 +3,17 @@ import { Body } from '../body.js';
|
|
|
3
3
|
* User agent response class.
|
|
4
4
|
*/
|
|
5
5
|
export class UserAgentResponse extends Body {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
constructor(options) {
|
|
7
|
+
super(options.headers, options.body);
|
|
8
|
+
this.httpVersion = options.httpVersion;
|
|
9
|
+
this.statusCode = options.statusCode;
|
|
10
|
+
this.statusMessage = options.statusMessage;
|
|
11
11
|
}
|
|
12
12
|
/**
|
|
13
13
|
* Check if response has a `4xx` response status code.
|
|
14
14
|
*/
|
|
15
15
|
get isClientError() {
|
|
16
|
-
const statusCode = this.
|
|
16
|
+
const statusCode = this.statusCode;
|
|
17
17
|
return statusCode >= 400 && statusCode <= 499;
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
@@ -26,35 +26,23 @@ export class UserAgentResponse extends Body {
|
|
|
26
26
|
* Check if response has a `3xx` response status code.
|
|
27
27
|
*/
|
|
28
28
|
get isRedirect() {
|
|
29
|
-
const statusCode = this.
|
|
29
|
+
const statusCode = this.statusCode;
|
|
30
30
|
return statusCode >= 300 && statusCode <= 399;
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
33
|
* Check if response has a `5xx` response status code.
|
|
34
34
|
*/
|
|
35
35
|
get isServerError() {
|
|
36
|
-
const statusCode = this.
|
|
36
|
+
const statusCode = this.statusCode;
|
|
37
37
|
return statusCode >= 500 && statusCode <= 599;
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
40
40
|
* Check if response has a `2xx` response status code.
|
|
41
41
|
*/
|
|
42
42
|
get isSuccess() {
|
|
43
|
-
const statusCode = this.
|
|
43
|
+
const statusCode = this.statusCode;
|
|
44
44
|
return statusCode >= 200 && statusCode <= 299;
|
|
45
45
|
}
|
|
46
|
-
/**
|
|
47
|
-
* Response status code.
|
|
48
|
-
*/
|
|
49
|
-
get status() {
|
|
50
|
-
return this._raw.statusCode;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Get response message.
|
|
54
|
-
*/
|
|
55
|
-
get statusMessage() {
|
|
56
|
-
return this._raw.statusMessage;
|
|
57
|
-
}
|
|
58
46
|
/**
|
|
59
47
|
* Get `Content-Type` header value.
|
|
60
48
|
*/
|