xhs-mp-compiler-cli 1.4.0-beta.2 → 1.4.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/dist/compiler.d.ts +13 -15
- package/dist/compiler.js +40 -96
- package/dist/{compilerImpl.js → compilerCP.js} +42 -29
- package/dist/index.d.ts +4 -3
- package/dist/index.js +5 -5
- package/dist/packs/mp-pack/index.js +1 -0
- package/dist/packs/webpack/dev-server/index.js +0 -1
- package/dist/packs/webpack/index.d.ts +1 -1
- package/dist/packs/webpack/index.js +4 -3
- package/dist/packs/webpack/webpack.d.ts +1 -1
- package/dist/packs/webpack/webpack.js +2 -6
- package/dist/presets/configs/miniprogram/render/index.d.ts +3 -2
- package/dist/presets/configs/miniprogram/render/index.js +2 -2
- package/dist/presets/configs/miniprogram/service/index.js +0 -1
- package/dist/utils/apm/index.js +30 -13
- package/package.json +12 -26
- package/dist/compilerImpl.d.ts +0 -1
- package/dist/compilerImplChildProcess.js +0 -33
- package/dist/compilerImplThreadWorker.d.ts +0 -1
- package/dist/compilerImplThreadWorker.js +0 -30
- package/dist/compilerImplWebWorker.d.ts +0 -6
- package/dist/compilerImplWebWorker.js +0 -25
- package/dist/dev-server/compatibleAPI.d.ts +0 -25
- package/dist/dev-server/compatibleAPI.js +0 -56
- package/dist/dev-server/escapeHtml.d.ts +0 -5
- package/dist/dev-server/escapeHtml.js +0 -50
- package/dist/dev-server/etag.d.ts +0 -10
- package/dist/dev-server/etag.js +0 -69
- package/dist/dev-server/getFilenameFromUrl.d.ts +0 -9
- package/dist/dev-server/getFilenameFromUrl.js +0 -102
- package/dist/dev-server/getPaths.d.ts +0 -6
- package/dist/dev-server/getPaths.js +0 -20
- package/dist/dev-server/index.d.ts +0 -98
- package/dist/dev-server/index.js +0 -90
- package/dist/dev-server/memorize.d.ts +0 -7
- package/dist/dev-server/memorize.js +0 -23
- package/dist/dev-server/middleware.d.ts +0 -2
- package/dist/dev-server/middleware.js +0 -477
- package/dist/dev-server/ready.d.ts +0 -4
- package/dist/dev-server/ready.js +0 -11
- package/dist/sharedFs.d.ts +0 -2
- package/dist/sharedFs.js +0 -4
- package/dist/utils/workerPool.d.ts +0 -29
- package/dist/utils/workerPool.js +0 -115
- /package/dist/{compilerImplChildProcess.d.ts → compilerCP.d.ts} +0 -0
|
@@ -1,477 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const path = require('path');
|
|
13
|
-
const mime = require('mime-types');
|
|
14
|
-
const onFinishedStream = require('on-finished');
|
|
15
|
-
const getFilenameFromUrl = require('./getFilenameFromUrl');
|
|
16
|
-
const { setStatusCode, send, pipe, createReadStreamOrReadFileSync } = require('./compatibleAPI');
|
|
17
|
-
const ready = require('./ready');
|
|
18
|
-
const parseTokenList = require('./parseTokenList');
|
|
19
|
-
const memorize = require('./memorize');
|
|
20
|
-
const BYTES_RANGE_REGEXP = /^ *bytes/i;
|
|
21
|
-
function getValueContentRangeHeader(type, size, range) {
|
|
22
|
-
return `${type} ${range ? `${range.start}-${range.end}` : '*'}/${size}`;
|
|
23
|
-
}
|
|
24
|
-
function parseHttpDate(date) {
|
|
25
|
-
const timestamp = date && Date.parse(date);
|
|
26
|
-
// istanbul ignore next: guard against date.js Date.parse patching
|
|
27
|
-
return typeof timestamp === 'number' ? timestamp : NaN;
|
|
28
|
-
}
|
|
29
|
-
const CACHE_CONTROL_NO_CACHE_REGEXP = /(?:^|,)\s*?no-cache\s*?(?:,|$)/;
|
|
30
|
-
function destroyStream(stream, suppress) {
|
|
31
|
-
if (typeof stream.destroy === 'function') {
|
|
32
|
-
stream.destroy();
|
|
33
|
-
}
|
|
34
|
-
if (typeof stream.close === 'function') {
|
|
35
|
-
// Node.js core bug workaround
|
|
36
|
-
stream.on('open', function onOpenClose() {
|
|
37
|
-
// @ts-ignore
|
|
38
|
-
if (typeof this.fd === 'number') {
|
|
39
|
-
// actually close down the fd
|
|
40
|
-
this.close();
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
if (typeof stream.addListener === 'function' && suppress) {
|
|
45
|
-
stream.removeAllListeners('error');
|
|
46
|
-
stream.addListener('error', () => { });
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
const statuses = {
|
|
50
|
-
400: 'Bad Request',
|
|
51
|
-
403: 'Forbidden',
|
|
52
|
-
404: 'Not Found',
|
|
53
|
-
416: 'Range Not Satisfiable',
|
|
54
|
-
500: 'Internal Server Error'
|
|
55
|
-
};
|
|
56
|
-
const parseRangeHeaders = memorize((value) => {
|
|
57
|
-
const [len, rangeHeader] = value.split('|');
|
|
58
|
-
// eslint-disable-next-line global-require
|
|
59
|
-
return require('range-parser')(Number(len), rangeHeader, {
|
|
60
|
-
combine: true
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
function wrapper(context) {
|
|
64
|
-
return function middleware(req, res, next) {
|
|
65
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
-
const acceptedMethods = context.options.methods || ['GET', 'HEAD'];
|
|
67
|
-
// fixes #282. credit @cexoso. in certain edge situations res.locals is undefined.
|
|
68
|
-
// eslint-disable-next-line no-param-reassign
|
|
69
|
-
res.locals = res.locals || {};
|
|
70
|
-
function goNext() {
|
|
71
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
-
if (!context.options.serverSideRender) {
|
|
73
|
-
return next();
|
|
74
|
-
}
|
|
75
|
-
return new Promise(resolve => {
|
|
76
|
-
ready(context, () => {
|
|
77
|
-
res.locals.webpack = { devMiddleware: context };
|
|
78
|
-
resolve(next());
|
|
79
|
-
}, req);
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
if (req.method && !acceptedMethods.includes(req.method)) {
|
|
84
|
-
yield goNext();
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
function sendError(status, options) {
|
|
88
|
-
// eslint-disable-next-line global-require
|
|
89
|
-
const escapeHtml = require('./escapeHtml');
|
|
90
|
-
const content = statuses[status] || String(status);
|
|
91
|
-
let document = Buffer.from(`<!DOCTYPE html>
|
|
92
|
-
<html lang="en">
|
|
93
|
-
<head>
|
|
94
|
-
<meta charset="utf-8">
|
|
95
|
-
<title>Error</title>
|
|
96
|
-
</head>
|
|
97
|
-
<body>
|
|
98
|
-
<pre>${escapeHtml(content)}</pre>
|
|
99
|
-
</body>
|
|
100
|
-
</html>`, 'utf-8');
|
|
101
|
-
// Clear existing headers
|
|
102
|
-
const headers = res.getHeaderNames();
|
|
103
|
-
for (let i = 0; i < headers.length; i++) {
|
|
104
|
-
res.removeHeader(headers[i]);
|
|
105
|
-
}
|
|
106
|
-
if (options && options.headers) {
|
|
107
|
-
const keys = Object.keys(options.headers);
|
|
108
|
-
for (let i = 0; i < keys.length; i++) {
|
|
109
|
-
const key = keys[i];
|
|
110
|
-
const value = options.headers[key];
|
|
111
|
-
if (typeof value !== 'undefined') {
|
|
112
|
-
res.setHeader(key, value);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
// Send basic response
|
|
117
|
-
setStatusCode(res, status);
|
|
118
|
-
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
|
119
|
-
res.setHeader('Content-Security-Policy', "default-src 'none'");
|
|
120
|
-
res.setHeader('X-Content-Type-Options', 'nosniff');
|
|
121
|
-
let byteLength = Buffer.byteLength(document);
|
|
122
|
-
if (options && options.modifyResponseData) {
|
|
123
|
-
;
|
|
124
|
-
({ data: document, byteLength } = options.modifyResponseData(req, res, document, byteLength));
|
|
125
|
-
}
|
|
126
|
-
res.setHeader('Content-Length', byteLength);
|
|
127
|
-
res.end(document);
|
|
128
|
-
}
|
|
129
|
-
function isConditionalGET() {
|
|
130
|
-
return (req.headers['if-match'] ||
|
|
131
|
-
req.headers['if-unmodified-since'] ||
|
|
132
|
-
req.headers['if-none-match'] ||
|
|
133
|
-
req.headers['if-modified-since']);
|
|
134
|
-
}
|
|
135
|
-
function isPreconditionFailure() {
|
|
136
|
-
// if-match
|
|
137
|
-
const ifMatch = req.headers['if-match'];
|
|
138
|
-
// A recipient MUST ignore If-Unmodified-Since if the request contains
|
|
139
|
-
// an If-Match header field; the condition in If-Match is considered to
|
|
140
|
-
// be a more accurate replacement for the condition in
|
|
141
|
-
// If-Unmodified-Since, and the two are only combined for the sake of
|
|
142
|
-
// interoperating with older intermediaries that might not implement If-Match.
|
|
143
|
-
if (ifMatch) {
|
|
144
|
-
const etag = res.getHeader('ETag');
|
|
145
|
-
return (!etag ||
|
|
146
|
-
(ifMatch !== '*' &&
|
|
147
|
-
parseTokenList(ifMatch).every(match => match !== etag && match !== `W/${etag}` && `W/${match}` !== etag)));
|
|
148
|
-
}
|
|
149
|
-
// if-unmodified-since
|
|
150
|
-
const ifUnmodifiedSince = req.headers['if-unmodified-since'];
|
|
151
|
-
if (ifUnmodifiedSince) {
|
|
152
|
-
const unmodifiedSince = parseHttpDate(ifUnmodifiedSince);
|
|
153
|
-
// A recipient MUST ignore the If-Unmodified-Since header field if the
|
|
154
|
-
// received field-value is not a valid HTTP-date.
|
|
155
|
-
if (!isNaN(unmodifiedSince)) {
|
|
156
|
-
const lastModified = parseHttpDate(/** @type {string} */ res.getHeader('Last-Modified'));
|
|
157
|
-
return isNaN(lastModified) || lastModified > unmodifiedSince;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
return false;
|
|
161
|
-
}
|
|
162
|
-
function isCachable() {
|
|
163
|
-
return (res.statusCode >= 200 && res.statusCode < 300) || res.statusCode === 304;
|
|
164
|
-
}
|
|
165
|
-
function isFresh(resHeaders) {
|
|
166
|
-
// Always return stale when Cache-Control: no-cache to support end-to-end reload requests
|
|
167
|
-
// https://tools.ietf.org/html/rfc2616#section-14.9.4
|
|
168
|
-
const cacheControl = req.headers['cache-control'];
|
|
169
|
-
if (cacheControl && CACHE_CONTROL_NO_CACHE_REGEXP.test(cacheControl)) {
|
|
170
|
-
return false;
|
|
171
|
-
}
|
|
172
|
-
// fields
|
|
173
|
-
const noneMatch = req.headers['if-none-match'];
|
|
174
|
-
const modifiedSince = req.headers['if-modified-since'];
|
|
175
|
-
// unconditional request
|
|
176
|
-
if (!noneMatch && !modifiedSince) {
|
|
177
|
-
return false;
|
|
178
|
-
}
|
|
179
|
-
// if-none-match
|
|
180
|
-
if (noneMatch && noneMatch !== '*') {
|
|
181
|
-
if (!resHeaders.etag) {
|
|
182
|
-
return false;
|
|
183
|
-
}
|
|
184
|
-
const matches = parseTokenList(noneMatch);
|
|
185
|
-
let etagStale = true;
|
|
186
|
-
for (let i = 0; i < matches.length; i++) {
|
|
187
|
-
const match = matches[i];
|
|
188
|
-
if (match === resHeaders.etag ||
|
|
189
|
-
match === `W/${resHeaders.etag}` ||
|
|
190
|
-
`W/${match}` === resHeaders.etag) {
|
|
191
|
-
etagStale = false;
|
|
192
|
-
break;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
if (etagStale) {
|
|
196
|
-
return false;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
// A recipient MUST ignore If-Modified-Since if the request contains an If-None-Match header field;
|
|
200
|
-
// the condition in If-None-Match is considered to be a more accurate replacement for the condition in If-Modified-Since,
|
|
201
|
-
// and the two are only combined for the sake of interoperating with older intermediaries that might not implement If-None-Match.
|
|
202
|
-
if (noneMatch) {
|
|
203
|
-
return true;
|
|
204
|
-
}
|
|
205
|
-
// if-modified-since
|
|
206
|
-
if (modifiedSince) {
|
|
207
|
-
const lastModified = resHeaders['last-modified'];
|
|
208
|
-
// A recipient MUST ignore the If-Modified-Since header field if the
|
|
209
|
-
// received field-value is not a valid HTTP-date, or if the request
|
|
210
|
-
// method is neither GET nor HEAD.
|
|
211
|
-
const modifiedStale = !lastModified || !(parseHttpDate(lastModified) <= parseHttpDate(modifiedSince));
|
|
212
|
-
if (modifiedStale) {
|
|
213
|
-
return false;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
return true;
|
|
217
|
-
}
|
|
218
|
-
function isRangeFresh() {
|
|
219
|
-
const ifRange =
|
|
220
|
-
/** @type {string | undefined} */
|
|
221
|
-
req.headers['if-range'];
|
|
222
|
-
if (!ifRange) {
|
|
223
|
-
return true;
|
|
224
|
-
}
|
|
225
|
-
// if-range as etag
|
|
226
|
-
if (ifRange.indexOf('"') !== -1) {
|
|
227
|
-
const etag = res.getHeader('ETag');
|
|
228
|
-
if (!etag) {
|
|
229
|
-
return true;
|
|
230
|
-
}
|
|
231
|
-
return Boolean(etag && ifRange.indexOf(etag) !== -1);
|
|
232
|
-
}
|
|
233
|
-
// if-range as modified date
|
|
234
|
-
const lastModified = res.getHeader('Last-Modified');
|
|
235
|
-
if (!lastModified) {
|
|
236
|
-
return true;
|
|
237
|
-
}
|
|
238
|
-
return parseHttpDate(lastModified) <= parseHttpDate(ifRange);
|
|
239
|
-
}
|
|
240
|
-
function getRangeHeader() {
|
|
241
|
-
const rage = req.headers.range;
|
|
242
|
-
if (rage && BYTES_RANGE_REGEXP.test(rage)) {
|
|
243
|
-
return rage;
|
|
244
|
-
}
|
|
245
|
-
// eslint-disable-next-line no-undefined
|
|
246
|
-
return undefined;
|
|
247
|
-
}
|
|
248
|
-
function getOffsetAndLenFromRange(range) {
|
|
249
|
-
const offset = range.start;
|
|
250
|
-
const len = range.end - range.start + 1;
|
|
251
|
-
return [offset, len];
|
|
252
|
-
}
|
|
253
|
-
function calcStartAndEnd(offset, len) {
|
|
254
|
-
const start = offset;
|
|
255
|
-
const end = Math.max(offset, offset + len - 1);
|
|
256
|
-
return [start, end];
|
|
257
|
-
}
|
|
258
|
-
function processRequest() {
|
|
259
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
260
|
-
const extra = {};
|
|
261
|
-
const filename = getFilenameFromUrl(context, req.url, extra);
|
|
262
|
-
if (extra.errorCode) {
|
|
263
|
-
if (extra.errorCode === 403) {
|
|
264
|
-
context.logger.error(`Malicious path "${filename}".`);
|
|
265
|
-
}
|
|
266
|
-
sendError(extra.errorCode, {
|
|
267
|
-
modifyResponseData: context.options.modifyResponseData
|
|
268
|
-
});
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
if (!filename) {
|
|
272
|
-
yield goNext();
|
|
273
|
-
return;
|
|
274
|
-
}
|
|
275
|
-
const { size } = extra.stats;
|
|
276
|
-
let len = size;
|
|
277
|
-
let offset = 0;
|
|
278
|
-
// Send logic
|
|
279
|
-
let { headers } = context.options;
|
|
280
|
-
if (typeof headers === 'function') {
|
|
281
|
-
headers = headers(req, res, context);
|
|
282
|
-
}
|
|
283
|
-
const allHeaders = [];
|
|
284
|
-
if (typeof headers !== 'undefined') {
|
|
285
|
-
if (!Array.isArray(headers)) {
|
|
286
|
-
// eslint-disable-next-line guard-for-in
|
|
287
|
-
for (const name in headers) {
|
|
288
|
-
allHeaders.push({ key: name, value: headers[name] });
|
|
289
|
-
}
|
|
290
|
-
headers = allHeaders;
|
|
291
|
-
}
|
|
292
|
-
headers.forEach(header => {
|
|
293
|
-
res.setHeader(header.key, header.value);
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
if (!res.getHeader('Content-Type')) {
|
|
297
|
-
// content-type name(like application/javascript; charset=utf-8) or false
|
|
298
|
-
const contentType = mime.contentType(path.extname(filename));
|
|
299
|
-
// Only set content-type header if media type is known
|
|
300
|
-
// https://tools.ietf.org/html/rfc7231#section-3.1.1.5
|
|
301
|
-
if (contentType) {
|
|
302
|
-
res.setHeader('Content-Type', contentType);
|
|
303
|
-
}
|
|
304
|
-
else if (context.options.mimeTypeDefault) {
|
|
305
|
-
res.setHeader('Content-Type', context.options.mimeTypeDefault);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
if (!res.getHeader('Accept-Ranges')) {
|
|
309
|
-
res.setHeader('Accept-Ranges', 'bytes');
|
|
310
|
-
}
|
|
311
|
-
if (context.options.lastModified && !res.getHeader('Last-Modified')) {
|
|
312
|
-
const modified = extra.stats.mtime.toUTCString();
|
|
313
|
-
res.setHeader('Last-Modified', modified);
|
|
314
|
-
}
|
|
315
|
-
let start;
|
|
316
|
-
let end;
|
|
317
|
-
let bufferOrStream;
|
|
318
|
-
let byteLength;
|
|
319
|
-
const rangeHeader = getRangeHeader();
|
|
320
|
-
if (context.options.etag && !res.getHeader('ETag')) {
|
|
321
|
-
let value;
|
|
322
|
-
// TODO cache etag generation?
|
|
323
|
-
if (context.options.etag === 'weak') {
|
|
324
|
-
value = extra.stats;
|
|
325
|
-
}
|
|
326
|
-
else {
|
|
327
|
-
if (rangeHeader) {
|
|
328
|
-
const parsedRanges = parseRangeHeaders(`${size}|${rangeHeader}`);
|
|
329
|
-
if (parsedRanges !== -2 && parsedRanges !== -1 && parsedRanges.length === 1) {
|
|
330
|
-
;
|
|
331
|
-
[offset, len] = getOffsetAndLenFromRange(parsedRanges[0]);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
;
|
|
335
|
-
[start, end] = calcStartAndEnd(offset, len);
|
|
336
|
-
try {
|
|
337
|
-
const result = createReadStreamOrReadFileSync(filename, context.outputFileSystem, start, end);
|
|
338
|
-
value = result.bufferOrStream;
|
|
339
|
-
({ bufferOrStream, byteLength } = result);
|
|
340
|
-
}
|
|
341
|
-
catch (_err) {
|
|
342
|
-
// Ignore here
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
if (value) {
|
|
346
|
-
// eslint-disable-next-line global-require
|
|
347
|
-
const result = yield require('./etag')(value);
|
|
348
|
-
// Because we already read stream, we can cache buffer to avoid extra read from fs
|
|
349
|
-
if (result.buffer) {
|
|
350
|
-
bufferOrStream = result.buffer;
|
|
351
|
-
}
|
|
352
|
-
res.setHeader('ETag', result.hash);
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
// Conditional GET support
|
|
356
|
-
if (isConditionalGET()) {
|
|
357
|
-
if (isPreconditionFailure()) {
|
|
358
|
-
sendError(412, {
|
|
359
|
-
modifyResponseData: context.options.modifyResponseData
|
|
360
|
-
});
|
|
361
|
-
return;
|
|
362
|
-
}
|
|
363
|
-
// For Koa
|
|
364
|
-
if (res.statusCode === 404) {
|
|
365
|
-
setStatusCode(res, 200);
|
|
366
|
-
}
|
|
367
|
-
if (isCachable() &&
|
|
368
|
-
isFresh({
|
|
369
|
-
etag: res.getHeader('ETag'),
|
|
370
|
-
'last-modified': res.getHeader('Last-Modified')
|
|
371
|
-
})) {
|
|
372
|
-
setStatusCode(res, 304);
|
|
373
|
-
// Remove content header fields
|
|
374
|
-
res.removeHeader('Content-Encoding');
|
|
375
|
-
res.removeHeader('Content-Language');
|
|
376
|
-
res.removeHeader('Content-Length');
|
|
377
|
-
res.removeHeader('Content-Range');
|
|
378
|
-
res.removeHeader('Content-Type');
|
|
379
|
-
res.end();
|
|
380
|
-
return;
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
if (rangeHeader) {
|
|
384
|
-
let parsedRanges = parseRangeHeaders(`${size}|${rangeHeader}`);
|
|
385
|
-
// If-Range support
|
|
386
|
-
if (!isRangeFresh()) {
|
|
387
|
-
parsedRanges = [];
|
|
388
|
-
}
|
|
389
|
-
if (parsedRanges === -1) {
|
|
390
|
-
context.logger.error("Unsatisfiable range for 'Range' header.");
|
|
391
|
-
res.setHeader('Content-Range', getValueContentRangeHeader('bytes', size));
|
|
392
|
-
sendError(416, {
|
|
393
|
-
headers: {
|
|
394
|
-
'Content-Range': res.getHeader('Content-Range')
|
|
395
|
-
},
|
|
396
|
-
modifyResponseData: context.options.modifyResponseData
|
|
397
|
-
});
|
|
398
|
-
return;
|
|
399
|
-
}
|
|
400
|
-
else if (parsedRanges === -2) {
|
|
401
|
-
context.logger.error("A malformed 'Range' header was provided. A regular response will be sent for this request.");
|
|
402
|
-
}
|
|
403
|
-
else if (parsedRanges.length > 1) {
|
|
404
|
-
context.logger.error("A 'Range' header with multiple ranges was provided. Multiple ranges are not supported, so a regular response will be sent for this request.");
|
|
405
|
-
}
|
|
406
|
-
if (parsedRanges !== -2 && parsedRanges.length === 1) {
|
|
407
|
-
// Content-Range
|
|
408
|
-
setStatusCode(res, 206);
|
|
409
|
-
res.setHeader('Content-Range', getValueContentRangeHeader('bytes', size, parsedRanges[0]));
|
|
410
|
-
[offset, len] = getOffsetAndLenFromRange(parsedRanges[0]);
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
// When strong Etag generation is enabled we already read file, so we can skip extra fs call
|
|
414
|
-
if (!bufferOrStream) {
|
|
415
|
-
;
|
|
416
|
-
[start, end] = calcStartAndEnd(offset, len);
|
|
417
|
-
try {
|
|
418
|
-
;
|
|
419
|
-
({ bufferOrStream, byteLength } = createReadStreamOrReadFileSync(filename, context.outputFileSystem, start, end));
|
|
420
|
-
}
|
|
421
|
-
catch (_ignoreError) {
|
|
422
|
-
yield goNext();
|
|
423
|
-
return;
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
if (context.options.modifyResponseData) {
|
|
427
|
-
;
|
|
428
|
-
({ data: bufferOrStream, byteLength } = context.options.modifyResponseData(req, res, bufferOrStream, byteLength));
|
|
429
|
-
}
|
|
430
|
-
res.setHeader('Content-Length', byteLength);
|
|
431
|
-
if (req.method === 'HEAD') {
|
|
432
|
-
// For Koa
|
|
433
|
-
if (res.statusCode === 404) {
|
|
434
|
-
setStatusCode(res, 200);
|
|
435
|
-
}
|
|
436
|
-
res.end();
|
|
437
|
-
return;
|
|
438
|
-
}
|
|
439
|
-
const isPipeSupports = typeof bufferOrStream.pipe === 'function';
|
|
440
|
-
if (!isPipeSupports) {
|
|
441
|
-
send(res, bufferOrStream);
|
|
442
|
-
return;
|
|
443
|
-
}
|
|
444
|
-
// Cleanup
|
|
445
|
-
const cleanup = () => {
|
|
446
|
-
destroyStream(bufferOrStream, true);
|
|
447
|
-
};
|
|
448
|
-
// Error handling
|
|
449
|
-
bufferOrStream.on('error', error => {
|
|
450
|
-
// clean up stream early
|
|
451
|
-
cleanup();
|
|
452
|
-
// Handle Error
|
|
453
|
-
switch (error.code) {
|
|
454
|
-
case 'ENAMETOOLONG':
|
|
455
|
-
case 'ENOENT':
|
|
456
|
-
case 'ENOTDIR':
|
|
457
|
-
sendError(404, {
|
|
458
|
-
modifyResponseData: context.options.modifyResponseData
|
|
459
|
-
});
|
|
460
|
-
break;
|
|
461
|
-
default:
|
|
462
|
-
sendError(500, {
|
|
463
|
-
modifyResponseData: context.options.modifyResponseData
|
|
464
|
-
});
|
|
465
|
-
break;
|
|
466
|
-
}
|
|
467
|
-
});
|
|
468
|
-
pipe(res, bufferOrStream);
|
|
469
|
-
// Response finished, cleanup
|
|
470
|
-
onFinishedStream(res, cleanup);
|
|
471
|
-
});
|
|
472
|
-
}
|
|
473
|
-
ready(context, processRequest, req);
|
|
474
|
-
});
|
|
475
|
-
};
|
|
476
|
-
}
|
|
477
|
-
exports.default = wrapper;
|
package/dist/dev-server/ready.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
function ready(context, callback, req) {
|
|
3
|
-
if (context.state) {
|
|
4
|
-
callback(context.stats);
|
|
5
|
-
return;
|
|
6
|
-
}
|
|
7
|
-
const name = (req && req.url) || callback.name;
|
|
8
|
-
context.logger.info(`wait until bundle finished${name ? `: ${name}` : ""}`);
|
|
9
|
-
context.callbacks.push(callback);
|
|
10
|
-
}
|
|
11
|
-
module.exports = ready;
|
package/dist/sharedFs.d.ts
DELETED
package/dist/sharedFs.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
declare class WorkerPool {
|
|
2
|
-
workerPath: string;
|
|
3
|
-
numberOfThreads: number;
|
|
4
|
-
_queue: any[];
|
|
5
|
-
_workersById: {
|
|
6
|
-
[key: string]: any;
|
|
7
|
-
};
|
|
8
|
-
_activeWorkersById: {
|
|
9
|
-
[key: string]: any;
|
|
10
|
-
};
|
|
11
|
-
constructor(workerPath: any, options?: {}, numberOfThreads?: any);
|
|
12
|
-
/**
|
|
13
|
-
* 检查空闲的 Worker
|
|
14
|
-
*/
|
|
15
|
-
getInactiveWorkerId(): number;
|
|
16
|
-
/**
|
|
17
|
-
* 调用 Worker 执行,目的是在指定的 Worker 里执行指定的任务
|
|
18
|
-
*/
|
|
19
|
-
runWorker(workerId: any, taskObj: any): void;
|
|
20
|
-
/**
|
|
21
|
-
* 运行线程
|
|
22
|
-
*/
|
|
23
|
-
run(data: any): Promise<unknown>;
|
|
24
|
-
/**
|
|
25
|
-
* 销毁
|
|
26
|
-
*/
|
|
27
|
-
destroy(force?: boolean): void;
|
|
28
|
-
}
|
|
29
|
-
export default WorkerPool;
|
package/dist/utils/workerPool.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
// 获取当前设备的 CPU 线程数目,作为 numberOfThreads 的默认值。
|
|
4
|
-
const { length: cpusLength } = require('os').cpus();
|
|
5
|
-
const { Worker } = require('worker_threads');
|
|
6
|
-
class WorkerPool {
|
|
7
|
-
constructor(workerPath, options = {}, numberOfThreads = cpusLength) {
|
|
8
|
-
this._workersById = {};
|
|
9
|
-
this._activeWorkersById = {};
|
|
10
|
-
if (numberOfThreads < 1) {
|
|
11
|
-
throw new Error('Number of threads should be greater or equal than 1!');
|
|
12
|
-
}
|
|
13
|
-
this.workerPath = workerPath;
|
|
14
|
-
this.numberOfThreads = numberOfThreads;
|
|
15
|
-
// 任务队列
|
|
16
|
-
this._queue = [];
|
|
17
|
-
// Worker 索引
|
|
18
|
-
this._workersById = {};
|
|
19
|
-
// Worker 激活状态索引
|
|
20
|
-
this._activeWorkersById = {};
|
|
21
|
-
// 创建 Workers
|
|
22
|
-
for (let i = 0; i < this.numberOfThreads; i++) {
|
|
23
|
-
const worker = new Worker(workerPath, options);
|
|
24
|
-
this._workersById[i] = worker;
|
|
25
|
-
// 将这些 Worker 设置为未激活状态
|
|
26
|
-
this._activeWorkersById[i] = false;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* 检查空闲的 Worker
|
|
31
|
-
*/
|
|
32
|
-
getInactiveWorkerId() {
|
|
33
|
-
for (let i = 0; i < this.numberOfThreads; i++) {
|
|
34
|
-
if (!this._activeWorkersById[i])
|
|
35
|
-
return i;
|
|
36
|
-
}
|
|
37
|
-
return -1;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* 调用 Worker 执行,目的是在指定的 Worker 里执行指定的任务
|
|
41
|
-
*/
|
|
42
|
-
runWorker(workerId, taskObj) {
|
|
43
|
-
const worker = this._workersById[workerId];
|
|
44
|
-
// 当任务执行完毕后执行
|
|
45
|
-
const doAfterTaskIsFinished = () => {
|
|
46
|
-
// 去除所有的 Listener,不然一次次添加不同的 Listener 会内存溢出(OOM)
|
|
47
|
-
worker.removeAllListeners('message');
|
|
48
|
-
worker.removeAllListeners('error');
|
|
49
|
-
// 将这个 Worker 设为未激活状态
|
|
50
|
-
this._activeWorkersById[workerId] = false;
|
|
51
|
-
if (this._queue.length) {
|
|
52
|
-
// 任务队列非空,使用该 Worker 执行任务队列中第一个任务
|
|
53
|
-
this.runWorker(workerId, this._queue.shift());
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
// 将这个 Worker 设置为激活状态
|
|
57
|
-
this._activeWorkersById[workerId] = true;
|
|
58
|
-
// 设置两个回调,用于 Worker 的监听器
|
|
59
|
-
const messageCallback = result => {
|
|
60
|
-
taskObj.cb(null, result);
|
|
61
|
-
doAfterTaskIsFinished();
|
|
62
|
-
};
|
|
63
|
-
const errorCallback = error => {
|
|
64
|
-
taskObj.cb(error);
|
|
65
|
-
doAfterTaskIsFinished();
|
|
66
|
-
};
|
|
67
|
-
// 为 Worker 添加 'message' 和 'error' 两个 Listener
|
|
68
|
-
worker.once('message', messageCallback);
|
|
69
|
-
worker.once('error', errorCallback);
|
|
70
|
-
// 将数据传给 Worker 供其获取和执行
|
|
71
|
-
worker.postMessage(taskObj.data);
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* 运行线程
|
|
75
|
-
*/
|
|
76
|
-
run(data) {
|
|
77
|
-
// Promise 是个好东西
|
|
78
|
-
return new Promise((resolve, reject) => {
|
|
79
|
-
// 调用 getInactiveWorkerId() 获取一个空闲的 Worker
|
|
80
|
-
const availableWorkerId = this.getInactiveWorkerId();
|
|
81
|
-
const taskObj = {
|
|
82
|
-
data,
|
|
83
|
-
cb: (error, result) => {
|
|
84
|
-
// 虽然 Workers 需要使用 Listener 和 Callback,但这不能阻止我们使用 Promise,对吧?
|
|
85
|
-
// 不,你不能 util.promisify(taskObj) 。人不能,至少不应该。
|
|
86
|
-
if (error)
|
|
87
|
-
reject(error);
|
|
88
|
-
return resolve(result);
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
if (availableWorkerId === -1) {
|
|
92
|
-
// 当前没有空闲的 Workers 了,把任务丢进队列里,这样一旦有 Workers 空闲时就会开始执行。
|
|
93
|
-
this._queue.push(taskObj);
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
96
|
-
// 有一个空闲的 Worker,用它执行任务
|
|
97
|
-
this.runWorker(availableWorkerId, taskObj);
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* 销毁
|
|
102
|
-
*/
|
|
103
|
-
destroy(force = false) {
|
|
104
|
-
for (let i = 0; i < this.numberOfThreads; i++) {
|
|
105
|
-
if (this._activeWorkersById[i] && !force) {
|
|
106
|
-
// 通常情况下,不应该在还有 Worker 在执行的时候就销毁它,这一定是什么地方出了问题,所以还是抛个 Error 比较好
|
|
107
|
-
// 不过保留一个 force 参数,总有人用得到的
|
|
108
|
-
throw new Error(`The worker ${i} is still runing!`);
|
|
109
|
-
}
|
|
110
|
-
// 销毁这个 Worker
|
|
111
|
-
this._workersById[i].terminate();
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
exports.default = WorkerPool;
|
|
File without changes
|