@oxog/spark 1.1.0 → 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 -293
- package/LICENSE +20 -20
- package/README.md +288 -241
- package/package.json +83 -89
- package/src/core/application.js +1070 -956
- package/src/core/context.js +1100 -830
- package/src/core/middleware.js +305 -217
- package/src/index.js +183 -278
- package/src/middleware/body-parser.js +484 -526
- package/src/middleware/cache.js +179 -170
- package/src/middleware/compression.js +179 -182
- package/src/middleware/cors.js +178 -95
- package/src/middleware/health.js +221 -218
- package/src/middleware/logger.js +197 -194
- package/src/middleware/metrics.js +341 -327
- package/src/middleware/rate-limit.js +369 -319
- package/src/middleware/security.js +444 -332
- package/src/middleware/session.js +649 -529
- package/src/middleware/static.js +406 -299
- package/src/router/layer.js +199 -117
- package/src/router/route.js +151 -123
- package/src/router/router.js +705 -755
- package/src/utils/async-handler.js +147 -144
- package/src/utils/context-pool.js +85 -82
- package/src/utils/regex-validator.js +189 -176
- 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 -957
- package/dist/core/context.js +0 -831
- package/dist/core/middleware.js +0 -218
- package/dist/core/request.js +0 -333
- package/dist/core/response.js +0 -375
- package/dist/index.js +0 -278
- package/dist/middleware/body-parser.js +0 -526
- package/dist/middleware/cache.js +0 -171
- package/dist/middleware/compression.js +0 -183
- package/dist/middleware/cors.js +0 -95
- package/dist/middleware/health.js +0 -219
- package/dist/middleware/index.js +0 -21
- package/dist/middleware/logger.js +0 -195
- package/dist/middleware/metrics.js +0 -328
- package/dist/middleware/rate-limit.js +0 -320
- package/dist/middleware/security.js +0 -333
- package/dist/middleware/session.js +0 -530
- package/dist/middleware/static.js +0 -299
- package/dist/package.json +0 -62
- package/dist/router/layer.js +0 -117
- package/dist/router/route.js +0 -123
- package/dist/router/router.js +0 -756
- package/dist/types/index.d.ts +0 -291
- package/dist/utils/async-handler.js +0 -145
- package/dist/utils/cache.js +0 -441
- package/dist/utils/context-pool.js +0 -83
- package/dist/utils/error-types.js +0 -132
- package/dist/utils/http.js +0 -413
- package/dist/utils/regex-validator.js +0 -177
- package/src/core/request.js +0 -333
- package/src/core/response.js +0 -375
- package/src/middleware/index.js +0 -21
- package/src/utils/cache.js +0 -441
- package/src/utils/error-types.js +0 -132
- package/src/utils/http.js +0 -413
package/dist/core/middleware.js
DELETED
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
const bodyParser = require('../middleware/body-parser');
|
|
2
|
-
const cors = require('../middleware/cors');
|
|
3
|
-
const compression = require('../middleware/compression');
|
|
4
|
-
const staticFiles = require('../middleware/static');
|
|
5
|
-
const session = require('../middleware/session');
|
|
6
|
-
const rateLimit = require('../middleware/rate-limit');
|
|
7
|
-
const security = require('../middleware/security');
|
|
8
|
-
const logger = require('../middleware/logger');
|
|
9
|
-
|
|
10
|
-
function createMiddleware(app) {
|
|
11
|
-
return {
|
|
12
|
-
bodyParser: (options) => bodyParser(options),
|
|
13
|
-
cors: (options) => cors(options),
|
|
14
|
-
compression: (options) => compression(options),
|
|
15
|
-
static: (root, options) => staticFiles(root, options),
|
|
16
|
-
session: (options) => session(options),
|
|
17
|
-
rateLimit: (options) => rateLimit(options),
|
|
18
|
-
security: (options) => security(options),
|
|
19
|
-
logger: (options) => logger(options),
|
|
20
|
-
|
|
21
|
-
helmet: (options) => security(options),
|
|
22
|
-
|
|
23
|
-
json: (options) => bodyParser({ ...options, type: 'json' }),
|
|
24
|
-
urlencoded: (options) => bodyParser({ ...options, type: 'urlencoded' }),
|
|
25
|
-
raw: (options) => bodyParser({ ...options, type: 'raw' }),
|
|
26
|
-
text: (options) => bodyParser({ ...options, type: 'text' }),
|
|
27
|
-
|
|
28
|
-
gzip: (options) => compression({ ...options, algorithm: 'gzip' }),
|
|
29
|
-
deflate: (options) => compression({ ...options, algorithm: 'deflate' }),
|
|
30
|
-
brotli: (options) => compression({ ...options, algorithm: 'br' }),
|
|
31
|
-
|
|
32
|
-
basicAuth: (options) => {
|
|
33
|
-
return async (ctx, next) => {
|
|
34
|
-
const auth = ctx.get('authorization');
|
|
35
|
-
if (!auth || !auth.startsWith('Basic ')) {
|
|
36
|
-
ctx.set('WWW-Authenticate', 'Basic realm="Secure Area"');
|
|
37
|
-
ctx.status(401).json({ error: 'Unauthorized' });
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const credentials = Buffer.from(auth.slice(6), 'base64').toString();
|
|
42
|
-
const [username, password] = credentials.split(':');
|
|
43
|
-
|
|
44
|
-
if (options.users && options.users[username] === password) {
|
|
45
|
-
ctx.user = { username };
|
|
46
|
-
await next();
|
|
47
|
-
} else if (options.verify && await options.verify(username, password)) {
|
|
48
|
-
ctx.user = { username };
|
|
49
|
-
await next();
|
|
50
|
-
} else {
|
|
51
|
-
ctx.set('WWW-Authenticate', 'Basic realm="Secure Area"');
|
|
52
|
-
ctx.status(401).json({ error: 'Unauthorized' });
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
},
|
|
56
|
-
|
|
57
|
-
timeout: (ms) => {
|
|
58
|
-
return async (ctx, next) => {
|
|
59
|
-
const timeout = setTimeout(() => {
|
|
60
|
-
if (!ctx.responded) {
|
|
61
|
-
ctx.status(408).json({ error: 'Request Timeout' });
|
|
62
|
-
}
|
|
63
|
-
}, ms);
|
|
64
|
-
|
|
65
|
-
try {
|
|
66
|
-
await next();
|
|
67
|
-
} finally {
|
|
68
|
-
clearTimeout(timeout);
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
errorHandler: (handler) => {
|
|
74
|
-
return async (ctx, next) => {
|
|
75
|
-
try {
|
|
76
|
-
await next();
|
|
77
|
-
} catch (error) {
|
|
78
|
-
if (handler) {
|
|
79
|
-
await handler(error, ctx);
|
|
80
|
-
} else {
|
|
81
|
-
ctx.status(500).json({
|
|
82
|
-
error: 'Internal Server Error',
|
|
83
|
-
message: process.env.NODE_ENV === 'development' ? error.message : undefined
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
},
|
|
89
|
-
|
|
90
|
-
conditional: (condition, middleware) => {
|
|
91
|
-
return async (ctx, next) => {
|
|
92
|
-
if (typeof condition === 'function' ? condition(ctx) : condition) {
|
|
93
|
-
await middleware(ctx, next);
|
|
94
|
-
} else {
|
|
95
|
-
await next();
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
},
|
|
99
|
-
|
|
100
|
-
compose: (...middlewares) => {
|
|
101
|
-
return async (ctx, next) => {
|
|
102
|
-
let index = 0;
|
|
103
|
-
|
|
104
|
-
const dispatch = async (i) => {
|
|
105
|
-
if (i >= middlewares.length) {
|
|
106
|
-
return next();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const middleware = middlewares[i];
|
|
110
|
-
return middleware(ctx, () => dispatch(i + 1));
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
return dispatch(0);
|
|
114
|
-
};
|
|
115
|
-
},
|
|
116
|
-
|
|
117
|
-
unless: (condition, middleware) => {
|
|
118
|
-
return async (ctx, next) => {
|
|
119
|
-
if (typeof condition === 'function' ? !condition(ctx) : !condition) {
|
|
120
|
-
await middleware(ctx, next);
|
|
121
|
-
} else {
|
|
122
|
-
await next();
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
},
|
|
126
|
-
|
|
127
|
-
skipIf: (condition) => {
|
|
128
|
-
return async (ctx, next) => {
|
|
129
|
-
if (typeof condition === 'function' ? condition(ctx) : condition) {
|
|
130
|
-
await next();
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
},
|
|
134
|
-
|
|
135
|
-
cache: (options = {}) => {
|
|
136
|
-
const { maxAge = 3600, private: isPrivate = false } = options;
|
|
137
|
-
|
|
138
|
-
return async (ctx, next) => {
|
|
139
|
-
await next();
|
|
140
|
-
|
|
141
|
-
if (ctx.method === 'GET' && ctx.statusCode === 200) {
|
|
142
|
-
const cacheControl = isPrivate ? 'private' : 'public';
|
|
143
|
-
ctx.set('Cache-Control', `${cacheControl}, max-age=${maxAge}`);
|
|
144
|
-
}
|
|
145
|
-
};
|
|
146
|
-
},
|
|
147
|
-
|
|
148
|
-
etag: (options = {}) => {
|
|
149
|
-
const crypto = require('crypto');
|
|
150
|
-
|
|
151
|
-
return async (ctx, next) => {
|
|
152
|
-
await next();
|
|
153
|
-
|
|
154
|
-
if (ctx.method === 'GET' && ctx.statusCode === 200) {
|
|
155
|
-
const body = ctx.res._getData ? ctx.res._getData() : '';
|
|
156
|
-
const etag = crypto.createHash('md5').update(body).digest('hex');
|
|
157
|
-
|
|
158
|
-
ctx.set('ETag', `"${etag}"`);
|
|
159
|
-
|
|
160
|
-
if (ctx.get('if-none-match') === `"${etag}"`) {
|
|
161
|
-
ctx.status(304).end();
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
};
|
|
165
|
-
},
|
|
166
|
-
|
|
167
|
-
favicon: (path) => {
|
|
168
|
-
const fs = require('fs');
|
|
169
|
-
const favicon = fs.readFileSync(path);
|
|
170
|
-
|
|
171
|
-
return async (ctx, next) => {
|
|
172
|
-
if (ctx.path === '/favicon.ico') {
|
|
173
|
-
ctx.set('Content-Type', 'image/x-icon');
|
|
174
|
-
ctx.set('Cache-Control', 'public, max-age=86400');
|
|
175
|
-
ctx.send(favicon);
|
|
176
|
-
} else {
|
|
177
|
-
await next();
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
},
|
|
181
|
-
|
|
182
|
-
responseTime: () => {
|
|
183
|
-
return async (ctx, next) => {
|
|
184
|
-
const start = Date.now();
|
|
185
|
-
await next();
|
|
186
|
-
const duration = Date.now() - start;
|
|
187
|
-
ctx.set('X-Response-Time', `${duration}ms`);
|
|
188
|
-
};
|
|
189
|
-
},
|
|
190
|
-
|
|
191
|
-
requestId: () => {
|
|
192
|
-
const crypto = require('crypto');
|
|
193
|
-
|
|
194
|
-
return async (ctx, next) => {
|
|
195
|
-
const id = crypto.randomBytes(16).toString('hex');
|
|
196
|
-
ctx.requestId = id;
|
|
197
|
-
ctx.set('X-Request-ID', id);
|
|
198
|
-
await next();
|
|
199
|
-
};
|
|
200
|
-
},
|
|
201
|
-
|
|
202
|
-
healthCheck: (path = '/health') => {
|
|
203
|
-
return async (ctx, next) => {
|
|
204
|
-
if (ctx.path === path) {
|
|
205
|
-
ctx.json({
|
|
206
|
-
status: 'healthy',
|
|
207
|
-
timestamp: new Date().toISOString(),
|
|
208
|
-
uptime: process.uptime()
|
|
209
|
-
});
|
|
210
|
-
} else {
|
|
211
|
-
await next();
|
|
212
|
-
}
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
module.exports = { createMiddleware };
|
package/dist/core/request.js
DELETED
|
@@ -1,333 +0,0 @@
|
|
|
1
|
-
const { URL } = require('url');
|
|
2
|
-
const querystring = require('querystring');
|
|
3
|
-
|
|
4
|
-
class Request {
|
|
5
|
-
constructor(req) {
|
|
6
|
-
this.req = req;
|
|
7
|
-
this.method = req.method;
|
|
8
|
-
this.url = req.url;
|
|
9
|
-
this.headers = req.headers;
|
|
10
|
-
this.httpVersion = req.httpVersion;
|
|
11
|
-
this.httpVersionMajor = req.httpVersionMajor;
|
|
12
|
-
this.httpVersionMinor = req.httpVersionMinor;
|
|
13
|
-
this.connection = req.connection;
|
|
14
|
-
this.socket = req.socket;
|
|
15
|
-
this.complete = req.complete;
|
|
16
|
-
this.trailers = req.trailers;
|
|
17
|
-
this.rawTrailers = req.rawTrailers;
|
|
18
|
-
this.aborted = req.aborted;
|
|
19
|
-
this.upgrade = req.upgrade;
|
|
20
|
-
|
|
21
|
-
this.parsedUrl = new URL(this.url, `http://${this.headers.host}`);
|
|
22
|
-
this.pathname = this.parsedUrl.pathname;
|
|
23
|
-
this.query = querystring.parse(this.parsedUrl.search.slice(1));
|
|
24
|
-
this.search = this.parsedUrl.search;
|
|
25
|
-
this.hash = this.parsedUrl.hash;
|
|
26
|
-
|
|
27
|
-
this.body = null;
|
|
28
|
-
this.rawBody = null;
|
|
29
|
-
this.files = {};
|
|
30
|
-
this.params = {};
|
|
31
|
-
|
|
32
|
-
this.parseHeaders();
|
|
33
|
-
this.parseCookies();
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
parseHeaders() {
|
|
37
|
-
this.host = this.headers.host;
|
|
38
|
-
this.hostname = this.host ? this.host.split(':')[0] : 'localhost';
|
|
39
|
-
this.protocol = this.connection.encrypted ? 'https' : 'http';
|
|
40
|
-
this.secure = this.connection.encrypted;
|
|
41
|
-
this.ip = this.connection.remoteAddress;
|
|
42
|
-
this.ips = this.getIPs();
|
|
43
|
-
this.userAgent = this.headers['user-agent'];
|
|
44
|
-
this.contentType = this.headers['content-type'];
|
|
45
|
-
this.contentLength = parseInt(this.headers['content-length']) || 0;
|
|
46
|
-
this.accept = this.headers.accept;
|
|
47
|
-
this.authorization = this.headers.authorization;
|
|
48
|
-
this.referer = this.headers.referer || this.headers.referrer;
|
|
49
|
-
this.origin = this.headers.origin;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
parseCookies() {
|
|
53
|
-
this.cookies = {};
|
|
54
|
-
const cookieHeader = this.headers.cookie;
|
|
55
|
-
|
|
56
|
-
if (cookieHeader) {
|
|
57
|
-
cookieHeader.split(';').forEach(cookie => {
|
|
58
|
-
const [name, value] = cookie.trim().split('=');
|
|
59
|
-
if (name && value) {
|
|
60
|
-
this.cookies[name] = decodeURIComponent(value);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
getIPs() {
|
|
67
|
-
const forwarded = this.headers['x-forwarded-for'];
|
|
68
|
-
if (forwarded) {
|
|
69
|
-
return forwarded.split(',').map(ip => ip.trim());
|
|
70
|
-
}
|
|
71
|
-
return [this.ip];
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
get(headerName) {
|
|
75
|
-
return this.headers[headerName.toLowerCase()];
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
header(headerName) {
|
|
79
|
-
return this.get(headerName);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
is(type) {
|
|
83
|
-
if (!this.contentType) return false;
|
|
84
|
-
|
|
85
|
-
const mimeTypes = {
|
|
86
|
-
json: 'application/json',
|
|
87
|
-
form: 'application/x-www-form-urlencoded',
|
|
88
|
-
multipart: 'multipart/form-data',
|
|
89
|
-
text: 'text/plain',
|
|
90
|
-
html: 'text/html',
|
|
91
|
-
xml: 'application/xml'
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
const targetType = mimeTypes[type] || type;
|
|
95
|
-
return this.contentType.includes(targetType);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
accepts(types) {
|
|
99
|
-
if (!this.accept) return false;
|
|
100
|
-
|
|
101
|
-
if (typeof types === 'string') {
|
|
102
|
-
types = [types];
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
for (const type of types) {
|
|
106
|
-
if (this.accept.includes(type) || this.accept.includes('*/*')) {
|
|
107
|
-
return type;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
acceptsCharsets(charsets) {
|
|
115
|
-
const acceptCharset = this.headers['accept-charset'];
|
|
116
|
-
if (!acceptCharset) return charsets[0];
|
|
117
|
-
|
|
118
|
-
if (typeof charsets === 'string') {
|
|
119
|
-
charsets = [charsets];
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
for (const charset of charsets) {
|
|
123
|
-
if (acceptCharset.includes(charset)) {
|
|
124
|
-
return charset;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return false;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
acceptsEncodings(encodings) {
|
|
132
|
-
const acceptEncoding = this.headers['accept-encoding'];
|
|
133
|
-
if (!acceptEncoding) return encodings[0];
|
|
134
|
-
|
|
135
|
-
if (typeof encodings === 'string') {
|
|
136
|
-
encodings = [encodings];
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
for (const encoding of encodings) {
|
|
140
|
-
if (acceptEncoding.includes(encoding)) {
|
|
141
|
-
return encoding;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
acceptsLanguages(languages) {
|
|
149
|
-
const acceptLanguage = this.headers['accept-language'];
|
|
150
|
-
if (!acceptLanguage) return languages[0];
|
|
151
|
-
|
|
152
|
-
if (typeof languages === 'string') {
|
|
153
|
-
languages = [languages];
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
for (const language of languages) {
|
|
157
|
-
if (acceptLanguage.includes(language)) {
|
|
158
|
-
return language;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return false;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
range(size) {
|
|
166
|
-
const range = this.headers.range;
|
|
167
|
-
if (!range) return null;
|
|
168
|
-
|
|
169
|
-
const ranges = parseRange(size, range);
|
|
170
|
-
return ranges.length > 0 ? ranges : null;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
param(name, defaultValue) {
|
|
174
|
-
if (this.params.hasOwnProperty(name)) {
|
|
175
|
-
return this.params[name];
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if (this.body && this.body.hasOwnProperty(name)) {
|
|
179
|
-
return this.body[name];
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (this.query.hasOwnProperty(name)) {
|
|
183
|
-
return this.query[name];
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
return defaultValue;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
fresh() {
|
|
190
|
-
const method = this.method;
|
|
191
|
-
const status = this.statusCode;
|
|
192
|
-
|
|
193
|
-
if (method !== 'GET' && method !== 'HEAD') return false;
|
|
194
|
-
if ((status >= 200 && status < 300) || status === 304) return true;
|
|
195
|
-
|
|
196
|
-
return false;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
stale() {
|
|
200
|
-
return !this.fresh();
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
xhr() {
|
|
204
|
-
return this.get('x-requested-with') === 'XMLHttpRequest';
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
subdomains() {
|
|
208
|
-
const parts = this.hostname.split('.');
|
|
209
|
-
return parts.length > 2 ? parts.slice(0, -2).reverse() : [];
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
route() {
|
|
213
|
-
return this.route;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
baseUrl() {
|
|
217
|
-
return `${this.protocol}://${this.host}`;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
originalUrl() {
|
|
221
|
-
return this.url;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
path() {
|
|
225
|
-
return this.pathname;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
async readBody() {
|
|
229
|
-
if (this.body !== null) {
|
|
230
|
-
return this.body;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
return new Promise((resolve, reject) => {
|
|
234
|
-
let body = '';
|
|
235
|
-
this.req.on('data', chunk => {
|
|
236
|
-
body += chunk;
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
this.req.on('end', () => {
|
|
240
|
-
this.rawBody = body;
|
|
241
|
-
try {
|
|
242
|
-
if (this.is('json')) {
|
|
243
|
-
this.body = JSON.parse(body);
|
|
244
|
-
} else if (this.is('form')) {
|
|
245
|
-
this.body = querystring.parse(body);
|
|
246
|
-
} else {
|
|
247
|
-
this.body = body;
|
|
248
|
-
}
|
|
249
|
-
resolve(this.body);
|
|
250
|
-
} catch (error) {
|
|
251
|
-
reject(error);
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
this.req.on('error', reject);
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
pipe(destination) {
|
|
260
|
-
return this.req.pipe(destination);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
unpipe(destination) {
|
|
264
|
-
return this.req.unpipe(destination);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
pause() {
|
|
268
|
-
return this.req.pause();
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
resume() {
|
|
272
|
-
return this.req.resume();
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
setTimeout(msecs, callback) {
|
|
276
|
-
return this.req.setTimeout(msecs, callback);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
destroy() {
|
|
280
|
-
return this.req.destroy();
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
toString() {
|
|
284
|
-
return `${this.method} ${this.url}`;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
toJSON() {
|
|
288
|
-
return {
|
|
289
|
-
method: this.method,
|
|
290
|
-
url: this.url,
|
|
291
|
-
headers: this.headers,
|
|
292
|
-
query: this.query,
|
|
293
|
-
params: this.params,
|
|
294
|
-
cookies: this.cookies,
|
|
295
|
-
body: this.body,
|
|
296
|
-
ip: this.ip,
|
|
297
|
-
ips: this.ips,
|
|
298
|
-
secure: this.secure,
|
|
299
|
-
xhr: this.xhr()
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
function parseRange(size, range) {
|
|
305
|
-
if (!range || !range.startsWith('bytes=')) {
|
|
306
|
-
return [];
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
const ranges = [];
|
|
310
|
-
const parts = range.slice(6).split(',');
|
|
311
|
-
|
|
312
|
-
for (const part of parts) {
|
|
313
|
-
const [start, end] = part.trim().split('-');
|
|
314
|
-
const startNum = parseInt(start);
|
|
315
|
-
const endNum = parseInt(end);
|
|
316
|
-
|
|
317
|
-
if (isNaN(startNum) && isNaN(endNum)) {
|
|
318
|
-
continue;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
if (isNaN(startNum)) {
|
|
322
|
-
ranges.push({ start: Math.max(0, size - endNum), end: size - 1 });
|
|
323
|
-
} else if (isNaN(endNum)) {
|
|
324
|
-
ranges.push({ start: startNum, end: size - 1 });
|
|
325
|
-
} else {
|
|
326
|
-
ranges.push({ start: startNum, end: Math.min(endNum, size - 1) });
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
return ranges;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
module.exports = Request;
|