@oxog/spark 1.1.1 → 1.2.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/CHANGELOG.md +632 -315
- package/LICENSE +20 -20
- package/README.md +288 -241
- package/package.json +83 -89
- package/src/core/application.js +1068 -956
- package/src/core/context.js +1098 -830
- package/src/core/middleware.js +303 -217
- package/src/index.js +183 -280
- package/src/middleware/body-parser.js +482 -526
- package/src/middleware/cache.js +177 -170
- package/src/middleware/compression.js +177 -182
- package/src/middleware/cors.js +176 -95
- package/src/middleware/health.js +219 -218
- package/src/middleware/logger.js +195 -194
- package/src/middleware/metrics.js +339 -327
- package/src/middleware/rate-limit.js +367 -319
- package/src/middleware/security.js +442 -332
- package/src/middleware/session.js +647 -529
- package/src/middleware/static.js +404 -299
- package/src/router/layer.js +197 -117
- package/src/router/route.js +149 -123
- package/src/router/router.js +703 -755
- package/src/utils/async-handler.js +145 -147
- package/src/utils/context-pool.js +83 -82
- package/src/utils/regex-validator.js +187 -163
- package/src/utils/safe-json.js +110 -0
- package/types/index.d.ts +536 -291
- package/dist/LICENSE +0 -21
- package/dist/README.md +0 -242
- package/dist/core/application.js +0 -959
- package/dist/core/context.js +0 -833
- package/dist/core/middleware.js +0 -220
- package/dist/core/request.js +0 -335
- package/dist/core/response.js +0 -377
- package/dist/index.js +0 -280
- package/dist/middleware/body-parser.js +0 -528
- package/dist/middleware/cache.js +0 -173
- package/dist/middleware/compression.js +0 -185
- package/dist/middleware/cors.js +0 -97
- package/dist/middleware/health.js +0 -221
- package/dist/middleware/index.js +0 -23
- package/dist/middleware/logger.js +0 -197
- package/dist/middleware/metrics.js +0 -330
- package/dist/middleware/rate-limit.js +0 -322
- package/dist/middleware/security.js +0 -335
- package/dist/middleware/session.js +0 -532
- package/dist/middleware/static.js +0 -301
- package/dist/package.json +0 -62
- package/dist/router/layer.js +0 -119
- package/dist/router/route.js +0 -125
- package/dist/router/router.js +0 -758
- package/dist/types/index.d.ts +0 -291
- package/dist/utils/async-handler.js +0 -150
- package/dist/utils/cache.js +0 -443
- package/dist/utils/context-pool.js +0 -85
- package/dist/utils/error-types.js +0 -134
- package/dist/utils/http.js +0 -415
- package/dist/utils/regex-validator.js +0 -166
- package/src/core/request.js +0 -335
- package/src/core/response.js +0 -377
- package/src/middleware/index.js +0 -23
- package/src/utils/cache.js +0 -443
- package/src/utils/error-types.js +0 -134
- package/src/utils/http.js +0 -415
package/src/core/request.js
DELETED
|
@@ -1,335 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { URL } = require('url');
|
|
4
|
-
const querystring = require('querystring');
|
|
5
|
-
|
|
6
|
-
class Request {
|
|
7
|
-
constructor(req) {
|
|
8
|
-
this.req = req;
|
|
9
|
-
this.method = req.method;
|
|
10
|
-
this.url = req.url;
|
|
11
|
-
this.headers = req.headers;
|
|
12
|
-
this.httpVersion = req.httpVersion;
|
|
13
|
-
this.httpVersionMajor = req.httpVersionMajor;
|
|
14
|
-
this.httpVersionMinor = req.httpVersionMinor;
|
|
15
|
-
this.connection = req.connection;
|
|
16
|
-
this.socket = req.socket;
|
|
17
|
-
this.complete = req.complete;
|
|
18
|
-
this.trailers = req.trailers;
|
|
19
|
-
this.rawTrailers = req.rawTrailers;
|
|
20
|
-
this.aborted = req.aborted;
|
|
21
|
-
this.upgrade = req.upgrade;
|
|
22
|
-
|
|
23
|
-
this.parsedUrl = new URL(this.url, `http://${this.headers.host}`);
|
|
24
|
-
this.pathname = this.parsedUrl.pathname;
|
|
25
|
-
this.query = querystring.parse(this.parsedUrl.search.slice(1));
|
|
26
|
-
this.search = this.parsedUrl.search;
|
|
27
|
-
this.hash = this.parsedUrl.hash;
|
|
28
|
-
|
|
29
|
-
this.body = null;
|
|
30
|
-
this.rawBody = null;
|
|
31
|
-
this.files = {};
|
|
32
|
-
this.params = {};
|
|
33
|
-
|
|
34
|
-
this.parseHeaders();
|
|
35
|
-
this.parseCookies();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
parseHeaders() {
|
|
39
|
-
this.host = this.headers.host;
|
|
40
|
-
this.hostname = this.host ? this.host.split(':')[0] : 'localhost';
|
|
41
|
-
this.protocol = this.connection.encrypted ? 'https' : 'http';
|
|
42
|
-
this.secure = this.connection.encrypted;
|
|
43
|
-
this.ip = this.connection.remoteAddress;
|
|
44
|
-
this.ips = this.getIPs();
|
|
45
|
-
this.userAgent = this.headers['user-agent'];
|
|
46
|
-
this.contentType = this.headers['content-type'];
|
|
47
|
-
this.contentLength = parseInt(this.headers['content-length']) || 0;
|
|
48
|
-
this.accept = this.headers.accept;
|
|
49
|
-
this.authorization = this.headers.authorization;
|
|
50
|
-
this.referer = this.headers.referer || this.headers.referrer;
|
|
51
|
-
this.origin = this.headers.origin;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
parseCookies() {
|
|
55
|
-
this.cookies = {};
|
|
56
|
-
const cookieHeader = this.headers.cookie;
|
|
57
|
-
|
|
58
|
-
if (cookieHeader) {
|
|
59
|
-
cookieHeader.split(';').forEach(cookie => {
|
|
60
|
-
const [name, value] = cookie.trim().split('=');
|
|
61
|
-
if (name && value) {
|
|
62
|
-
this.cookies[name] = decodeURIComponent(value);
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
getIPs() {
|
|
69
|
-
const forwarded = this.headers['x-forwarded-for'];
|
|
70
|
-
if (forwarded) {
|
|
71
|
-
return forwarded.split(',').map(ip => ip.trim());
|
|
72
|
-
}
|
|
73
|
-
return [this.ip];
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
get(headerName) {
|
|
77
|
-
return this.headers[headerName.toLowerCase()];
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
header(headerName) {
|
|
81
|
-
return this.get(headerName);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
is(type) {
|
|
85
|
-
if (!this.contentType) return false;
|
|
86
|
-
|
|
87
|
-
const mimeTypes = {
|
|
88
|
-
json: 'application/json',
|
|
89
|
-
form: 'application/x-www-form-urlencoded',
|
|
90
|
-
multipart: 'multipart/form-data',
|
|
91
|
-
text: 'text/plain',
|
|
92
|
-
html: 'text/html',
|
|
93
|
-
xml: 'application/xml'
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const targetType = mimeTypes[type] || type;
|
|
97
|
-
return this.contentType.includes(targetType);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
accepts(types) {
|
|
101
|
-
if (!this.accept) return false;
|
|
102
|
-
|
|
103
|
-
if (typeof types === 'string') {
|
|
104
|
-
types = [types];
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
for (const type of types) {
|
|
108
|
-
if (this.accept.includes(type) || this.accept.includes('*/*')) {
|
|
109
|
-
return type;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return false;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
acceptsCharsets(charsets) {
|
|
117
|
-
const acceptCharset = this.headers['accept-charset'];
|
|
118
|
-
if (!acceptCharset) return charsets[0];
|
|
119
|
-
|
|
120
|
-
if (typeof charsets === 'string') {
|
|
121
|
-
charsets = [charsets];
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
for (const charset of charsets) {
|
|
125
|
-
if (acceptCharset.includes(charset)) {
|
|
126
|
-
return charset;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
acceptsEncodings(encodings) {
|
|
134
|
-
const acceptEncoding = this.headers['accept-encoding'];
|
|
135
|
-
if (!acceptEncoding) return encodings[0];
|
|
136
|
-
|
|
137
|
-
if (typeof encodings === 'string') {
|
|
138
|
-
encodings = [encodings];
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
for (const encoding of encodings) {
|
|
142
|
-
if (acceptEncoding.includes(encoding)) {
|
|
143
|
-
return encoding;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return false;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
acceptsLanguages(languages) {
|
|
151
|
-
const acceptLanguage = this.headers['accept-language'];
|
|
152
|
-
if (!acceptLanguage) return languages[0];
|
|
153
|
-
|
|
154
|
-
if (typeof languages === 'string') {
|
|
155
|
-
languages = [languages];
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
for (const language of languages) {
|
|
159
|
-
if (acceptLanguage.includes(language)) {
|
|
160
|
-
return language;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
return false;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
range(size) {
|
|
168
|
-
const range = this.headers.range;
|
|
169
|
-
if (!range) return null;
|
|
170
|
-
|
|
171
|
-
const ranges = parseRange(size, range);
|
|
172
|
-
return ranges.length > 0 ? ranges : null;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
param(name, defaultValue) {
|
|
176
|
-
if (this.params.hasOwnProperty(name)) {
|
|
177
|
-
return this.params[name];
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (this.body && this.body.hasOwnProperty(name)) {
|
|
181
|
-
return this.body[name];
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if (this.query.hasOwnProperty(name)) {
|
|
185
|
-
return this.query[name];
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
return defaultValue;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
fresh() {
|
|
192
|
-
const method = this.method;
|
|
193
|
-
const status = this.statusCode;
|
|
194
|
-
|
|
195
|
-
if (method !== 'GET' && method !== 'HEAD') return false;
|
|
196
|
-
if ((status >= 200 && status < 300) || status === 304) return true;
|
|
197
|
-
|
|
198
|
-
return false;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
stale() {
|
|
202
|
-
return !this.fresh();
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
xhr() {
|
|
206
|
-
return this.get('x-requested-with') === 'XMLHttpRequest';
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
subdomains() {
|
|
210
|
-
const parts = this.hostname.split('.');
|
|
211
|
-
return parts.length > 2 ? parts.slice(0, -2).reverse() : [];
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
route() {
|
|
215
|
-
return this.route;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
baseUrl() {
|
|
219
|
-
return `${this.protocol}://${this.host}`;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
originalUrl() {
|
|
223
|
-
return this.url;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
path() {
|
|
227
|
-
return this.pathname;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
async readBody() {
|
|
231
|
-
if (this.body !== null) {
|
|
232
|
-
return this.body;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
return new Promise((resolve, reject) => {
|
|
236
|
-
let body = '';
|
|
237
|
-
this.req.on('data', chunk => {
|
|
238
|
-
body += chunk;
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
this.req.on('end', () => {
|
|
242
|
-
this.rawBody = body;
|
|
243
|
-
try {
|
|
244
|
-
if (this.is('json')) {
|
|
245
|
-
this.body = JSON.parse(body);
|
|
246
|
-
} else if (this.is('form')) {
|
|
247
|
-
this.body = querystring.parse(body);
|
|
248
|
-
} else {
|
|
249
|
-
this.body = body;
|
|
250
|
-
}
|
|
251
|
-
resolve(this.body);
|
|
252
|
-
} catch (error) {
|
|
253
|
-
reject(error);
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
this.req.on('error', reject);
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
pipe(destination) {
|
|
262
|
-
return this.req.pipe(destination);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
unpipe(destination) {
|
|
266
|
-
return this.req.unpipe(destination);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
pause() {
|
|
270
|
-
return this.req.pause();
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
resume() {
|
|
274
|
-
return this.req.resume();
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
setTimeout(msecs, callback) {
|
|
278
|
-
return this.req.setTimeout(msecs, callback);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
destroy() {
|
|
282
|
-
return this.req.destroy();
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
toString() {
|
|
286
|
-
return `${this.method} ${this.url}`;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
toJSON() {
|
|
290
|
-
return {
|
|
291
|
-
method: this.method,
|
|
292
|
-
url: this.url,
|
|
293
|
-
headers: this.headers,
|
|
294
|
-
query: this.query,
|
|
295
|
-
params: this.params,
|
|
296
|
-
cookies: this.cookies,
|
|
297
|
-
body: this.body,
|
|
298
|
-
ip: this.ip,
|
|
299
|
-
ips: this.ips,
|
|
300
|
-
secure: this.secure,
|
|
301
|
-
xhr: this.xhr()
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
function parseRange(size, range) {
|
|
307
|
-
if (!range || !range.startsWith('bytes=')) {
|
|
308
|
-
return [];
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
const ranges = [];
|
|
312
|
-
const parts = range.slice(6).split(',');
|
|
313
|
-
|
|
314
|
-
for (const part of parts) {
|
|
315
|
-
const [start, end] = part.trim().split('-');
|
|
316
|
-
const startNum = parseInt(start);
|
|
317
|
-
const endNum = parseInt(end);
|
|
318
|
-
|
|
319
|
-
if (isNaN(startNum) && isNaN(endNum)) {
|
|
320
|
-
continue;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
if (isNaN(startNum)) {
|
|
324
|
-
ranges.push({ start: Math.max(0, size - endNum), end: size - 1 });
|
|
325
|
-
} else if (isNaN(endNum)) {
|
|
326
|
-
ranges.push({ start: startNum, end: size - 1 });
|
|
327
|
-
} else {
|
|
328
|
-
ranges.push({ start: startNum, end: Math.min(endNum, size - 1) });
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
return ranges;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
module.exports = Request;
|
package/src/core/response.js
DELETED
|
@@ -1,377 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { STATUS_CODES } = require('http');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const { promisify } = require('util');
|
|
7
|
-
const stat = promisify(fs.stat);
|
|
8
|
-
const readFile = promisify(fs.readFile);
|
|
9
|
-
|
|
10
|
-
class Response {
|
|
11
|
-
constructor(res) {
|
|
12
|
-
this.res = res;
|
|
13
|
-
this.statusCode = 200;
|
|
14
|
-
this.headers = {};
|
|
15
|
-
this.locals = {};
|
|
16
|
-
this.finished = false;
|
|
17
|
-
this.headersSent = false;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
status(code) {
|
|
21
|
-
this.statusCode = code;
|
|
22
|
-
return this;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
set(field, value) {
|
|
26
|
-
if (typeof field === 'object') {
|
|
27
|
-
Object.assign(this.headers, field);
|
|
28
|
-
} else {
|
|
29
|
-
this.headers[field.toLowerCase()] = value;
|
|
30
|
-
}
|
|
31
|
-
return this;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
get(field) {
|
|
35
|
-
return this.headers[field.toLowerCase()];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
header(field, value) {
|
|
39
|
-
return this.set(field, value);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
setHeader(name, value) {
|
|
43
|
-
return this.set(name, value);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
getHeader(name) {
|
|
47
|
-
return this.get(name);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
removeHeader(name) {
|
|
51
|
-
delete this.headers[name.toLowerCase()];
|
|
52
|
-
return this;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
append(field, value) {
|
|
56
|
-
const existing = this.get(field);
|
|
57
|
-
if (existing) {
|
|
58
|
-
this.set(field, Array.isArray(existing) ? existing.concat(value) : [existing, value]);
|
|
59
|
-
} else {
|
|
60
|
-
this.set(field, value);
|
|
61
|
-
}
|
|
62
|
-
return this;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
cookie(name, value, options = {}) {
|
|
66
|
-
let cookieString = `${name}=${encodeURIComponent(value)}`;
|
|
67
|
-
|
|
68
|
-
if (options.domain) {
|
|
69
|
-
cookieString += `; Domain=${options.domain}`;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (options.path) {
|
|
73
|
-
cookieString += `; Path=${options.path}`;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (options.expires) {
|
|
77
|
-
cookieString += `; Expires=${options.expires.toUTCString()}`;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (options.maxAge) {
|
|
81
|
-
cookieString += `; Max-Age=${options.maxAge}`;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (options.httpOnly) {
|
|
85
|
-
cookieString += '; HttpOnly';
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (options.secure) {
|
|
89
|
-
cookieString += '; Secure';
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (options.sameSite) {
|
|
93
|
-
cookieString += `; SameSite=${options.sameSite}`;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
this.append('Set-Cookie', cookieString);
|
|
97
|
-
return this;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
clearCookie(name, options = {}) {
|
|
101
|
-
const clearOptions = {
|
|
102
|
-
...options,
|
|
103
|
-
expires: new Date(0),
|
|
104
|
-
maxAge: 0
|
|
105
|
-
};
|
|
106
|
-
return this.cookie(name, '', clearOptions);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
redirect(url, status = 302) {
|
|
110
|
-
this.status(status);
|
|
111
|
-
this.set('Location', url);
|
|
112
|
-
this.end();
|
|
113
|
-
return this;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
location(url) {
|
|
117
|
-
this.set('Location', url);
|
|
118
|
-
return this;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
links(links) {
|
|
122
|
-
let linkHeader = this.get('Link') || '';
|
|
123
|
-
|
|
124
|
-
Object.keys(links).forEach(rel => {
|
|
125
|
-
if (linkHeader) linkHeader += ', ';
|
|
126
|
-
linkHeader += `<${links[rel]}>; rel="${rel}"`;
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
this.set('Link', linkHeader);
|
|
130
|
-
return this;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
type(type) {
|
|
134
|
-
const mimeTypes = {
|
|
135
|
-
html: 'text/html',
|
|
136
|
-
json: 'application/json',
|
|
137
|
-
xml: 'application/xml',
|
|
138
|
-
text: 'text/plain',
|
|
139
|
-
png: 'image/png',
|
|
140
|
-
jpg: 'image/jpeg',
|
|
141
|
-
jpeg: 'image/jpeg',
|
|
142
|
-
gif: 'image/gif',
|
|
143
|
-
css: 'text/css',
|
|
144
|
-
js: 'application/javascript',
|
|
145
|
-
pdf: 'application/pdf'
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
const contentType = mimeTypes[type] || type;
|
|
149
|
-
this.set('Content-Type', contentType);
|
|
150
|
-
return this;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
format(obj) {
|
|
154
|
-
const keys = Object.keys(obj);
|
|
155
|
-
const accepts = this.req.accepts(keys);
|
|
156
|
-
|
|
157
|
-
if (accepts) {
|
|
158
|
-
this.type(accepts);
|
|
159
|
-
obj[accepts](this.req, this);
|
|
160
|
-
} else {
|
|
161
|
-
this.status(406).send('Not Acceptable');
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
return this;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
attachment(filename) {
|
|
168
|
-
if (filename) {
|
|
169
|
-
this.type(path.extname(filename));
|
|
170
|
-
this.set('Content-Disposition', `attachment; filename="${filename}"`);
|
|
171
|
-
} else {
|
|
172
|
-
this.set('Content-Disposition', 'attachment');
|
|
173
|
-
}
|
|
174
|
-
return this;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
json(obj) {
|
|
178
|
-
this.type('json');
|
|
179
|
-
this.send(JSON.stringify(obj));
|
|
180
|
-
return this;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
jsonp(obj) {
|
|
184
|
-
const callback = this.req.query.callback || 'callback';
|
|
185
|
-
this.type('text/javascript');
|
|
186
|
-
this.send(`${callback}(${JSON.stringify(obj)})`);
|
|
187
|
-
return this;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
send(body) {
|
|
191
|
-
if (this.finished) {
|
|
192
|
-
return this;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
this.writeHead();
|
|
196
|
-
|
|
197
|
-
if (body === null || body === undefined) {
|
|
198
|
-
this.res.end();
|
|
199
|
-
} else if (typeof body === 'string') {
|
|
200
|
-
if (!this.get('Content-Type')) {
|
|
201
|
-
this.type('html');
|
|
202
|
-
}
|
|
203
|
-
this.res.end(body);
|
|
204
|
-
} else if (Buffer.isBuffer(body)) {
|
|
205
|
-
if (!this.get('Content-Type')) {
|
|
206
|
-
this.type('application/octet-stream');
|
|
207
|
-
}
|
|
208
|
-
this.res.end(body);
|
|
209
|
-
} else {
|
|
210
|
-
this.json(body);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
this.finished = true;
|
|
214
|
-
return this;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
async sendFile(filePath, options = {}) {
|
|
218
|
-
try {
|
|
219
|
-
const stats = await stat(filePath);
|
|
220
|
-
|
|
221
|
-
if (!stats.isFile()) {
|
|
222
|
-
return this.status(404).send('File not found');
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
const ext = path.extname(filePath);
|
|
226
|
-
this.type(ext);
|
|
227
|
-
|
|
228
|
-
if (options.maxAge) {
|
|
229
|
-
this.set('Cache-Control', `public, max-age=${options.maxAge}`);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
this.set('Content-Length', stats.size);
|
|
233
|
-
this.set('Last-Modified', stats.mtime.toUTCString());
|
|
234
|
-
|
|
235
|
-
const etag = `"${stats.size}-${stats.mtime.getTime()}"`;
|
|
236
|
-
this.set('ETag', etag);
|
|
237
|
-
|
|
238
|
-
if (this.req.headers['if-none-match'] === etag) {
|
|
239
|
-
return this.status(304).end();
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
if (options.headers) {
|
|
243
|
-
this.set(options.headers);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
this.writeHead();
|
|
247
|
-
|
|
248
|
-
const stream = fs.createReadStream(filePath);
|
|
249
|
-
stream.pipe(this.res);
|
|
250
|
-
|
|
251
|
-
stream.on('end', () => {
|
|
252
|
-
this.finished = true;
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
stream.on('error', (error) => {
|
|
256
|
-
this.status(500).send('Internal Server Error');
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
} catch (error) {
|
|
260
|
-
this.status(404).send('File not found');
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
return this;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
async sendStatus(statusCode) {
|
|
267
|
-
this.status(statusCode);
|
|
268
|
-
this.send(STATUS_CODES[statusCode] || 'Unknown Status');
|
|
269
|
-
return this;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
download(filePath, filename, options = {}) {
|
|
273
|
-
const name = filename || path.basename(filePath);
|
|
274
|
-
this.attachment(name);
|
|
275
|
-
return this.sendFile(filePath, options);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
render(view, locals = {}, callback) {
|
|
279
|
-
if (typeof locals === 'function') {
|
|
280
|
-
callback = locals;
|
|
281
|
-
locals = {};
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
const renderLocals = { ...this.locals, ...locals };
|
|
285
|
-
|
|
286
|
-
if (this.app.render) {
|
|
287
|
-
this.app.render(view, renderLocals, (err, html) => {
|
|
288
|
-
if (err) {
|
|
289
|
-
if (callback) return callback(err);
|
|
290
|
-
throw err;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
this.send(html);
|
|
294
|
-
if (callback) callback(null, html);
|
|
295
|
-
});
|
|
296
|
-
} else {
|
|
297
|
-
const error = new Error('No template engine configured');
|
|
298
|
-
if (callback) return callback(error);
|
|
299
|
-
throw error;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
return this;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
vary(field) {
|
|
306
|
-
this.append('Vary', field);
|
|
307
|
-
return this;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
writeHead() {
|
|
311
|
-
if (this.headersSent) {
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
this.res.statusCode = this.statusCode;
|
|
316
|
-
|
|
317
|
-
Object.keys(this.headers).forEach(name => {
|
|
318
|
-
this.res.setHeader(name, this.headers[name]);
|
|
319
|
-
});
|
|
320
|
-
|
|
321
|
-
this.headersSent = true;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
write(chunk, encoding) {
|
|
325
|
-
if (!this.headersSent) {
|
|
326
|
-
this.writeHead();
|
|
327
|
-
}
|
|
328
|
-
return this.res.write(chunk, encoding);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
end(data, encoding) {
|
|
332
|
-
if (!this.headersSent) {
|
|
333
|
-
this.writeHead();
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
this.finished = true;
|
|
337
|
-
this.res.end(data, encoding);
|
|
338
|
-
return this;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
destroy() {
|
|
342
|
-
this.res.destroy();
|
|
343
|
-
return this;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
setTimeout(msecs, callback) {
|
|
347
|
-
this.res.setTimeout(msecs, callback);
|
|
348
|
-
return this;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
headersSent() {
|
|
352
|
-
return this.headersSent;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
writableEnded() {
|
|
356
|
-
return this.res.writableEnded;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
writable() {
|
|
360
|
-
return this.res.writable;
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
toString() {
|
|
364
|
-
return `Response ${this.statusCode}`;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
toJSON() {
|
|
368
|
-
return {
|
|
369
|
-
statusCode: this.statusCode,
|
|
370
|
-
headers: this.headers,
|
|
371
|
-
finished: this.finished,
|
|
372
|
-
headersSent: this.headersSent
|
|
373
|
-
};
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
module.exports = Response;
|
package/src/middleware/index.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Middleware exports for @oxog/spark
|
|
5
|
-
* Aggregates all built-in middleware for convenient importing
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
module.exports = {
|
|
9
|
-
bodyParser: require('./body-parser'),
|
|
10
|
-
cache: require('./cache'),
|
|
11
|
-
compression: require('./compression'),
|
|
12
|
-
cors: require('./cors'),
|
|
13
|
-
health: require('./health'),
|
|
14
|
-
healthCheck: require('./health'), // alias
|
|
15
|
-
logger: require('./logger'),
|
|
16
|
-
metrics: require('./metrics'),
|
|
17
|
-
rateLimit: require('./rate-limit'),
|
|
18
|
-
security: require('./security'),
|
|
19
|
-
helmet: require('./security'), // alias for security
|
|
20
|
-
session: require('./session'),
|
|
21
|
-
static: require('./static'),
|
|
22
|
-
serveStatic: require('./static') // alias
|
|
23
|
-
};
|