@e-mc/watch 0.3.2 → 0.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/filegroup/index.js +176 -176
- package/index.js +822 -822
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,825 +1,825 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
const path = require("path");
|
|
5
|
-
const fs = require("fs");
|
|
6
|
-
const https = require("https");
|
|
7
|
-
const ws = require("ws");
|
|
8
|
-
const pm = require("picomatch");
|
|
9
|
-
const util_1 = require("../request/util");
|
|
10
|
-
const types_1 = require("../types");
|
|
11
|
-
const core_1 = require("../core");
|
|
12
|
-
const request_1 = require("../request");
|
|
13
|
-
const filegroup_1 = require("./filegroup");
|
|
14
|
-
const kInterval = Symbol('interval');
|
|
15
|
-
const kPort = Symbol('port');
|
|
16
|
-
const kSecurePort = Symbol('securePort');
|
|
17
|
-
const kCa = Symbol('ca');
|
|
18
|
-
const kTlsKey = Symbol('tlsKey');
|
|
19
|
-
const kTlsCert = Symbol('tlsCert');
|
|
20
|
-
const kTlsVersion = Symbol('tlsVersion');
|
|
21
|
-
const kTlsPassphrase = Symbol('tlsPassphrase');
|
|
22
|
-
const kTlsConfig = Symbol('tlsConfig');
|
|
23
|
-
const PLATFORM_WIN32 = process.platform === 'win32';
|
|
24
|
-
let STATE_MAP = new WeakSet();
|
|
25
|
-
let HTTP_MAP = {};
|
|
26
|
-
let DISK_MAP = {};
|
|
27
|
-
let PORT_MAP = {};
|
|
28
|
-
let SECURE_MAP = {};
|
|
29
|
-
function isConnectionTimeout(err) {
|
|
30
|
-
switch (err instanceof Error && err.code) {
|
|
31
|
-
case 'ETIMEDOUT':
|
|
32
|
-
case 'ECONNRESET':
|
|
33
|
-
return true;
|
|
34
|
-
default:
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
function abortTimeout(group) {
|
|
39
|
-
group.timeout.aborted = true;
|
|
40
|
-
if (group.watcher) {
|
|
41
|
-
group.watcher.close();
|
|
42
|
-
group.watcher = null;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
const getInterval = ({ watch }) => Math.max((0, types_1.isObject)(watch) && watch.interval || 0, 0);
|
|
46
|
-
const formatDate = (value) => new Date(value).toLocaleString().replace(/\/20\d+, /, '@').replace(/:\d+ (AM|PM)$/, (...match) => match[1]);
|
|
47
|
-
class Watch extends core_1.Client {
|
|
48
|
-
static createServer(port, secure, active) {
|
|
49
|
-
if (typeof secure === 'boolean') {
|
|
50
|
-
active = secure;
|
|
51
|
-
secure = null;
|
|
52
|
-
}
|
|
53
|
-
let wss;
|
|
54
|
-
try {
|
|
55
|
-
if ((0, types_1.isPlainObject)(secure)) {
|
|
56
|
-
if (!(wss = SECURE_MAP[port])) {
|
|
57
|
-
const { ca, passphrase, version, config } = secure;
|
|
58
|
-
let { key, cert } = secure;
|
|
59
|
-
if (key && cert && (key = request_1.default.readTLSKey(key, true)) && (cert = request_1.default.readTLSKey(cert, true))) {
|
|
60
|
-
const server = https.createServer({
|
|
61
|
-
...config,
|
|
62
|
-
ca: ca && request_1.default.readCACert(ca, true) || undefined,
|
|
63
|
-
key: passphrase ? [{ pem: key, passphrase }] : key,
|
|
64
|
-
cert,
|
|
65
|
-
minVersion: version || config?.minVersion
|
|
66
|
-
});
|
|
67
|
-
server.listen(port);
|
|
68
|
-
wss = new ws.Server({ server });
|
|
69
|
-
SECURE_MAP[port] = wss;
|
|
70
|
-
if (active) {
|
|
71
|
-
STATE_MAP.add(wss);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
this.writeFail('TLS/SSL key and cert not found', (0, types_1.errorMessage)('ws', 'Missing TLS/SSL credentials'));
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
return wss;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
else if (!(wss = PORT_MAP[port])) {
|
|
83
|
-
wss = new ws.Server({ port });
|
|
84
|
-
PORT_MAP[port] = wss;
|
|
85
|
-
if (active) {
|
|
86
|
-
STATE_MAP.add(wss);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
return wss;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
catch (err) {
|
|
94
|
-
this.writeFail("Unknown" /* ERR_MESSAGE.UNKNOWN */, err);
|
|
95
|
-
}
|
|
96
|
-
if (wss) {
|
|
97
|
-
wss.on('connection', function (socket) {
|
|
98
|
-
socket.on('message', function () {
|
|
99
|
-
filegroup_1.default.CLIENT_SESSION.delete(this);
|
|
100
|
-
});
|
|
101
|
-
socket.on('close', function () {
|
|
102
|
-
filegroup_1.default.CLIENT_SESSION.delete(this);
|
|
103
|
-
});
|
|
104
|
-
this.clients.forEach(client => filegroup_1.default.checkTimeout(client));
|
|
105
|
-
});
|
|
106
|
-
wss.on('error', function (err) {
|
|
107
|
-
const data = JSON.stringify({ event: types_1.WATCH_EVENT.ERROR, errors: [err.message] });
|
|
108
|
-
this.clients.forEach(client => filegroup_1.default.checkTimeout(client) && client.send(data));
|
|
109
|
-
});
|
|
110
|
-
wss.on('close', function () {
|
|
111
|
-
this.clients.forEach(client => client.terminate());
|
|
112
|
-
});
|
|
113
|
-
return wss;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
static shutdown() {
|
|
117
|
-
for (const item of [HTTP_MAP, DISK_MAP]) {
|
|
118
|
-
for (const uri in item) {
|
|
119
|
-
for (const group of item[uri].values()) {
|
|
120
|
-
abortTimeout(group);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
for (const item of [PORT_MAP, SECURE_MAP]) {
|
|
125
|
-
for (const port in item) {
|
|
126
|
-
item[port].close(err => err && this.writeFail([`Unable to shutdown ${item === PORT_MAP ? 'WS' : 'WSS'} server`, 'port: ' + port], err));
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
filegroup_1.default.CLIENT_SESSION.clear();
|
|
130
|
-
HTTP_MAP = {};
|
|
131
|
-
DISK_MAP = {};
|
|
132
|
-
PORT_MAP = {};
|
|
133
|
-
SECURE_MAP = {};
|
|
134
|
-
STATE_MAP = new WeakSet();
|
|
135
|
-
}
|
|
136
|
-
static checkTimeout(client) {
|
|
137
|
-
return filegroup_1.default.checkTimeout(client);
|
|
138
|
-
}
|
|
139
|
-
static setTimeout(value) {
|
|
140
|
-
if ((value = (0, types_1.parseTime)(value)) > 0) {
|
|
141
|
-
filegroup_1.default.CONNECTION_TIMEOUT = Math.min(value, core_1.Client.MAX_TIMEOUT);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
constructor(data, port, securePort, extensions) {
|
|
145
|
-
super(data);
|
|
146
|
-
this.connectTimeout = Watch.PROCESS_TIMEOUT;
|
|
147
|
-
this._moduleName = 'watch';
|
|
148
|
-
this._assets = [];
|
|
149
|
-
this._extensions = [];
|
|
150
|
-
this._hostEvents = [];
|
|
151
|
-
this[_a] = types_1.THRESHOLD.WATCH_INTERVAL;
|
|
152
|
-
this[_b] = 80 /* HTTP.PORT */;
|
|
153
|
-
this[_c] = 443 /* HTTP.PORT_SECURE */;
|
|
154
|
-
this[_d] = '';
|
|
155
|
-
this[_e] = '';
|
|
156
|
-
this[_f] = '';
|
|
157
|
-
this[_g] = '';
|
|
158
|
-
this[_h] = undefined;
|
|
159
|
-
this[_j] = undefined;
|
|
160
|
-
let interval;
|
|
161
|
-
if ((0, types_1.isPlainObject)(data)) {
|
|
162
|
-
let secure;
|
|
163
|
-
({ interval, port, secure } = data);
|
|
164
|
-
if (secure) {
|
|
165
|
-
securePort = secure.port;
|
|
166
|
-
if (secure.cert && secure.key) {
|
|
167
|
-
this.configureServer(secure);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
if ((interval = (0, util_1.asInt)(interval)) > 0) {
|
|
172
|
-
this.interval = interval;
|
|
173
|
-
}
|
|
174
|
-
if ((port = (0, util_1.asInt)(port)) > 0) {
|
|
175
|
-
this.port = port;
|
|
176
|
-
}
|
|
177
|
-
if ((securePort = (0, util_1.asInt)(securePort)) > 0) {
|
|
178
|
-
this.securePort = securePort;
|
|
179
|
-
}
|
|
180
|
-
if ((0, types_1.isArray)(extensions)) {
|
|
181
|
-
this.module.extensions = extensions;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
start(assets, permission) {
|
|
185
|
-
var _k;
|
|
186
|
-
if (this.aborted) {
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
if (permission) {
|
|
190
|
-
this.permission = permission;
|
|
191
|
-
}
|
|
192
|
-
const startTime = Date.now();
|
|
193
|
-
const username = this.host?.username;
|
|
194
|
-
const destMap = Object.create(null);
|
|
195
|
-
for (const item of assets) {
|
|
196
|
-
if ((0, types_1.ignoreFlag)(item.flags)) {
|
|
197
|
-
continue;
|
|
198
|
-
}
|
|
199
|
-
const { bundleId, uri, localUri } = item;
|
|
200
|
-
if (!(0, types_1.isEmpty)(bundleId)) {
|
|
201
|
-
(destMap[_k = ':' + bundleId] || (destMap[_k] = [])).push(item);
|
|
202
|
-
}
|
|
203
|
-
else if (uri && localUri) {
|
|
204
|
-
(destMap[localUri] || (destMap[localUri] = [])).push(item);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
for (let dest in destMap) {
|
|
208
|
-
const items = destMap[dest];
|
|
209
|
-
if (!items.some(item => item.watch)) {
|
|
210
|
-
continue;
|
|
211
|
-
}
|
|
212
|
-
let watchInterval, bundleMain;
|
|
213
|
-
if (dest[0] === ':') {
|
|
214
|
-
items.sort((a, b) => a.bundleIndex - b.bundleIndex);
|
|
215
|
-
bundleMain = items[0];
|
|
216
|
-
dest = bundleMain.localUri;
|
|
217
|
-
const leading = items.find(item => getInterval(item) > 0);
|
|
218
|
-
if (leading) {
|
|
219
|
-
watchInterval = getInterval(leading);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
const related = [];
|
|
223
|
-
for (let i = 0, length = items.length; i < length; ++i) {
|
|
224
|
-
const watch = items[i].watch;
|
|
225
|
-
if ((0, types_1.isObject)(watch) && watch.assets) {
|
|
226
|
-
watch.assets.forEach(other => {
|
|
227
|
-
if (items.includes(other)) {
|
|
228
|
-
return;
|
|
229
|
-
}
|
|
230
|
-
if ((0, types_1.watchFlag)(other.flags)) {
|
|
231
|
-
if (!other.watch) {
|
|
232
|
-
other = filegroup_1.default.cloneAsset(other);
|
|
233
|
-
other.watch = { ...watch };
|
|
234
|
-
}
|
|
235
|
-
other.flags &= ~1 /* ASSET_FLAG.IGNORE */;
|
|
236
|
-
other.invalid = undefined;
|
|
237
|
-
items.push(other);
|
|
238
|
-
}
|
|
239
|
-
else if (!related.includes(other)) {
|
|
240
|
-
related.push(other);
|
|
241
|
-
}
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
for (const item of items) {
|
|
246
|
-
let { watch, sourceFiles } = item;
|
|
247
|
-
if (watch) {
|
|
248
|
-
const fatalError = (map, target, err) => {
|
|
249
|
-
const uri = target.value.uri;
|
|
250
|
-
delete map[uri];
|
|
251
|
-
abortTimeout(target);
|
|
252
|
-
this.writeFail(["Unable to watch file" /* ERR_MESSAGE.WATCH_FILE */, uri], err, 16 /* LOG_TYPE.WATCH */);
|
|
253
|
-
};
|
|
254
|
-
const watchExpired = (map, target, message = 'Expired') => {
|
|
255
|
-
this.formatMessage(16 /* LOG_TYPE.WATCH */, 'WATCH', [message, target.startTime ? 'since ' + formatDate(target.startTime) : ''], target.uri, { titleColor: 'grey' });
|
|
256
|
-
const data = map[target.uri];
|
|
257
|
-
if (!data || data.size === 0) {
|
|
258
|
-
delete map[target.uri];
|
|
259
|
-
return true;
|
|
260
|
-
}
|
|
261
|
-
return false;
|
|
262
|
-
};
|
|
263
|
-
let expires = 0, status = 0, wss, main, id, socketId, port, secure, hot, always, watched, message, settings;
|
|
264
|
-
if (watch === true && username && (settings = this.settings.users?.[username])) {
|
|
265
|
-
const { uri, localUri, mimeType } = item;
|
|
266
|
-
watch = false;
|
|
267
|
-
if (uri && !(watch = settings[uri])) {
|
|
268
|
-
for (const pattern in settings) {
|
|
269
|
-
if ((0, types_1.hasGlob)(pattern) && pm.isMatch(uri, pattern, { matchBase: true }) && (watch = settings[pattern])) {
|
|
270
|
-
break;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
if (!watch && localUri && !(watch = settings[localUri])) {
|
|
275
|
-
for (const pattern in settings) {
|
|
276
|
-
if ((0, types_1.hasGlob)(pattern) && pm.isMatch(localUri, pattern, { nocase: PLATFORM_WIN32 }) && (watch = settings[pattern])) {
|
|
277
|
-
break;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
if (!watch && mimeType && !(watch = settings[mimeType])) {
|
|
282
|
-
for (const pattern in settings) {
|
|
283
|
-
if (/^[^*/]+\/\*+$/.test(pattern) && pm.isMatch(mimeType, pattern) && (watch = settings[pattern])) {
|
|
284
|
-
break;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
if ((0, types_1.isPlainObject)(watch)) {
|
|
290
|
-
const reload = watch.reload;
|
|
291
|
-
id = watch.id;
|
|
292
|
-
if (watch.main) {
|
|
293
|
-
main = item;
|
|
294
|
-
}
|
|
295
|
-
if (watch.expires) {
|
|
296
|
-
expires = (0, types_1.parseExpires)(watch.expires, startTime);
|
|
297
|
-
}
|
|
298
|
-
if (reload && (socketId = reload.socketId)) {
|
|
299
|
-
({ port, module: hot, always } = reload);
|
|
300
|
-
if (reload.secure) {
|
|
301
|
-
const key = this[kTlsKey];
|
|
302
|
-
const cert = this[kTlsCert];
|
|
303
|
-
if (key && cert) {
|
|
304
|
-
wss = Watch.createServer(port || (port = this.securePort), { ca: this[kCa], key, cert, passphrase: this[kTlsPassphrase], version: this[kTlsVersion], config: this[kTlsConfig] });
|
|
305
|
-
}
|
|
306
|
-
secure = true;
|
|
307
|
-
}
|
|
308
|
-
else {
|
|
309
|
-
wss = Watch.createServer(port || (port = this.port));
|
|
310
|
-
}
|
|
311
|
-
if (!wss) {
|
|
312
|
-
this.writeFail('Unable to create WebSocket server', (0, types_1.errorMessage)(secure ? 'wss' : 'ws', "Invalid parameters" /* ERR_MESSAGE.PARAMETERS */, 'port: ' + (port || "Unknown" /* ERR_MESSAGE.UNKNOWN */)), 16 /* LOG_TYPE.WATCH */);
|
|
313
|
-
socketId = undefined;
|
|
314
|
-
port = undefined;
|
|
315
|
-
secure = undefined;
|
|
316
|
-
hot = undefined;
|
|
317
|
-
always = undefined;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
item.watch = watch;
|
|
321
|
-
}
|
|
322
|
-
const interval = getInterval(item) || watchInterval || this.interval;
|
|
323
|
-
const watching = (uri, esm) => {
|
|
324
|
-
const group = new filegroup_1.default(uri, main ? assets : items, startTime, this.willAbort("(watch)" /* ABORT_NAME.WATCH */));
|
|
325
|
-
group.add(expires, socketId, id);
|
|
326
|
-
if (main) {
|
|
327
|
-
group.main = main;
|
|
328
|
-
}
|
|
329
|
-
else if (related.length) {
|
|
330
|
-
group.related = related;
|
|
331
|
-
}
|
|
332
|
-
if (bundleMain) {
|
|
333
|
-
group.bundleMain = bundleMain;
|
|
334
|
-
}
|
|
335
|
-
if (item.document) {
|
|
336
|
-
group.document = item.document;
|
|
337
|
-
}
|
|
338
|
-
if (hot) {
|
|
339
|
-
group.hot = true;
|
|
340
|
-
}
|
|
341
|
-
if (always) {
|
|
342
|
-
group.always = true;
|
|
343
|
-
}
|
|
344
|
-
if (sourceFiles) {
|
|
345
|
-
group.sourceFiles = sourceFiles.filter(file => file !== uri);
|
|
346
|
-
}
|
|
347
|
-
if (wss) {
|
|
348
|
-
group.connection(wss, port, secure);
|
|
349
|
-
}
|
|
350
|
-
const timeout = { interval, retries: 0, aborted: false };
|
|
351
|
-
const target = { value: group, timeout, watcher: null };
|
|
352
|
-
const isMap = (value) => value ? value.size > 0 : false;
|
|
353
|
-
const checkAborted = (result, current) => result?.aborted && current.abortable && current.abort();
|
|
354
|
-
const checkPreceding = (map) => {
|
|
355
|
-
const data = map[uri];
|
|
356
|
-
if (!data) {
|
|
357
|
-
return;
|
|
358
|
-
}
|
|
359
|
-
const current = data.get(dest)?.value;
|
|
360
|
-
if (current && !current.expired) {
|
|
361
|
-
let reset;
|
|
362
|
-
if (id) {
|
|
363
|
-
const sockets = current.sockets;
|
|
364
|
-
for (let i = 0, active; i < sockets.length; ++i) {
|
|
365
|
-
const socket = sockets[i];
|
|
366
|
-
if (socket.id === id) {
|
|
367
|
-
if (active) {
|
|
368
|
-
sockets.splice(i, 1);
|
|
369
|
-
}
|
|
370
|
-
else {
|
|
371
|
-
reset = true;
|
|
372
|
-
}
|
|
373
|
-
break;
|
|
374
|
-
}
|
|
375
|
-
if (!socket.expired) {
|
|
376
|
-
active = true;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
if (!reset) {
|
|
381
|
-
if (wss) {
|
|
382
|
-
const server = current.server;
|
|
383
|
-
if (!server) {
|
|
384
|
-
current.connection(wss, port, secure);
|
|
385
|
-
}
|
|
386
|
-
else if (server !== wss) {
|
|
387
|
-
if (wss.clients.size === 0 && !STATE_MAP.has(wss)) {
|
|
388
|
-
if (secure) {
|
|
389
|
-
delete SECURE_MAP[port];
|
|
390
|
-
}
|
|
391
|
-
else {
|
|
392
|
-
delete PORT_MAP[port];
|
|
393
|
-
}
|
|
394
|
-
wss.close();
|
|
395
|
-
}
|
|
396
|
-
message = 'Destination already watched @ ' + (current.secure ? 'wss' : 'ws') + '://hostname:' + current.port;
|
|
397
|
-
return 3 /* ERR.SERVER */;
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
const socket = socketId && current.find(socketId);
|
|
401
|
-
if (socket) {
|
|
402
|
-
socket.expires = expires;
|
|
403
|
-
}
|
|
404
|
-
else {
|
|
405
|
-
current.add(expires, socketId, id);
|
|
406
|
-
}
|
|
407
|
-
return 0 /* ERR.NONE */;
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
data.set(dest, target);
|
|
411
|
-
return 0 /* ERR.NONE */;
|
|
412
|
-
};
|
|
413
|
-
if (core_1.Client.isFile(uri, 'http/s')) {
|
|
414
|
-
group.url = item.url;
|
|
415
|
-
group.etag = item.etag;
|
|
416
|
-
group.lastModified = item.lastModified;
|
|
417
|
-
if (!group.etag && !group.lastModified) {
|
|
418
|
-
return 1 /* ERR.ETAG */;
|
|
419
|
-
}
|
|
420
|
-
if ((status = checkPreceding(HTTP_MAP)) !== undefined) {
|
|
421
|
-
return status;
|
|
422
|
-
}
|
|
423
|
-
const url = group.url || (group.url = new URL(uri));
|
|
424
|
-
const request = this.host?.Request || new request_1.default();
|
|
425
|
-
const agentTimeout = Math.max(timeout.interval * 10, this.connectTimeout);
|
|
426
|
-
const opts = request.opts(url, { method: 'HEAD', httpVersion: 1, timeout: agentTimeout, agentTimeout });
|
|
427
|
-
(function recurse() {
|
|
428
|
-
let client = null;
|
|
429
|
-
new Promise((resolve, reject) => {
|
|
430
|
-
client = request.open(uri, opts)
|
|
431
|
-
.on('response', res => {
|
|
432
|
-
if (group.aborted) {
|
|
433
|
-
return;
|
|
434
|
-
}
|
|
435
|
-
if (this.aborted) {
|
|
436
|
-
reject((0, types_1.createAbortError)());
|
|
437
|
-
return;
|
|
438
|
-
}
|
|
439
|
-
const statusCode = res.statusCode;
|
|
440
|
-
const valid = statusCode >= 200 /* HTTP_STATUS.OK */ && statusCode < 300 /* HTTP_STATUS.MULTIPLE_CHOICES */;
|
|
441
|
-
const map = HTTP_MAP[uri];
|
|
442
|
-
const etag = res.headers.etag;
|
|
443
|
-
const lastModified = res.headers['last-modified'];
|
|
444
|
-
if (valid && map?.size && (etag || lastModified)) {
|
|
445
|
-
for (const [destUrl, input] of map) {
|
|
446
|
-
const value = input.value;
|
|
447
|
-
if (value.paused) {
|
|
448
|
-
continue;
|
|
449
|
-
}
|
|
450
|
-
let aborted = value.aborted;
|
|
451
|
-
if (!aborted) {
|
|
452
|
-
if (value.expired) {
|
|
453
|
-
aborted = true;
|
|
454
|
-
}
|
|
455
|
-
else if (value.etag) {
|
|
456
|
-
if (etag) {
|
|
457
|
-
if (etag !== value.etag) {
|
|
458
|
-
value.etag = etag;
|
|
459
|
-
this.modified(value).then(result => checkAborted(result, value));
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
else if (value.lastModified && lastModified) {
|
|
464
|
-
if (lastModified !== value.lastModified) {
|
|
465
|
-
value.lastModified = lastModified;
|
|
466
|
-
this.modified(value).then(result => checkAborted(result, value));
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
if (aborted) {
|
|
471
|
-
map.delete(destUrl);
|
|
472
|
-
if (watchExpired(HTTP_MAP, value)) {
|
|
473
|
-
abortTimeout(target);
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
else {
|
|
479
|
-
if (isMap(map)) {
|
|
480
|
-
reject(valid ? (0, types_1.errorValue)('ETag not supported', uri) : (0, types_1.errorMessage)(statusCode, 'Invalid HTTP request', uri));
|
|
481
|
-
return;
|
|
482
|
-
}
|
|
483
|
-
watchExpired(HTTP_MAP, group);
|
|
484
|
-
abortTimeout(target);
|
|
485
|
-
}
|
|
486
|
-
if (client) {
|
|
487
|
-
client.destroy();
|
|
488
|
-
}
|
|
489
|
-
resolve();
|
|
490
|
-
})
|
|
491
|
-
.on('error', err => {
|
|
492
|
-
if (!timeout.aborted && !(isConnectionTimeout(err) && ++timeout.retries <= 10 /* HTTP.TIMEOUT_LIMIT */)) {
|
|
493
|
-
reject(err);
|
|
494
|
-
return;
|
|
495
|
-
}
|
|
496
|
-
resolve();
|
|
497
|
-
})
|
|
498
|
-
.on('timeout', () => {
|
|
499
|
-
if (!timeout.aborted && ++timeout.retries > 10 /* HTTP.TIMEOUT_LIMIT */) {
|
|
500
|
-
reject((0, types_1.errorMessage)(408 /* HTTP_STATUS.REQUEST_TIMEOUT */, 'HTTP request timeout'));
|
|
501
|
-
return;
|
|
502
|
-
}
|
|
503
|
-
resolve();
|
|
504
|
-
});
|
|
505
|
-
})
|
|
506
|
-
.then(() => {
|
|
507
|
-
if (!timeout.aborted) {
|
|
508
|
-
const map = HTTP_MAP[uri];
|
|
509
|
-
if (isMap(map)) {
|
|
510
|
-
let ms = Watch.MAX_TIMEOUT;
|
|
511
|
-
for (const session of map.values()) {
|
|
512
|
-
ms = Math.min(session.timeout.interval, ms);
|
|
513
|
-
}
|
|
514
|
-
setTimeout(recurse.bind(this), timeout.interval = ms);
|
|
515
|
-
}
|
|
516
|
-
else {
|
|
517
|
-
delete HTTP_MAP[uri];
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
})
|
|
521
|
-
.catch(err => {
|
|
522
|
-
fatalError(HTTP_MAP, target, err);
|
|
523
|
-
if (client) {
|
|
524
|
-
client.destroy();
|
|
525
|
-
}
|
|
526
|
-
});
|
|
527
|
-
}).call(this);
|
|
528
|
-
HTTP_MAP[uri] = new Map([[dest, target]]);
|
|
529
|
-
}
|
|
530
|
-
else if (esm || path.isAbsolute(uri = core_1.Client.resolveFile(uri)) && this.canRead(uri, { hostPermissionOnly: !!this.host })) {
|
|
531
|
-
if ((status = checkPreceding(DISK_MAP)) !== undefined) {
|
|
532
|
-
return status;
|
|
533
|
-
}
|
|
534
|
-
let ptime = 0;
|
|
535
|
-
target.watcher = fs.watch(uri, (event, filename) => {
|
|
536
|
-
if (this.aborted || group.aborted) {
|
|
537
|
-
fatalError(DISK_MAP, target, (0, types_1.createAbortError)());
|
|
538
|
-
return;
|
|
539
|
-
}
|
|
540
|
-
const map = DISK_MAP[uri];
|
|
541
|
-
switch (event) {
|
|
542
|
-
case 'change': {
|
|
543
|
-
if (isMap(map)) {
|
|
544
|
-
try {
|
|
545
|
-
const mtime = Math.floor(fs.statSync(uri).mtimeMs);
|
|
546
|
-
if (mtime > ptime) {
|
|
547
|
-
for (const [key, input] of map) {
|
|
548
|
-
const value = input.value;
|
|
549
|
-
if (value.expired) {
|
|
550
|
-
map.delete(key);
|
|
551
|
-
if (watchExpired(DISK_MAP, value)) {
|
|
552
|
-
abortTimeout(target);
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
else if (value.aborted) {
|
|
556
|
-
map.delete(key);
|
|
557
|
-
if (map.size === 0) {
|
|
558
|
-
abortTimeout(target);
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
else {
|
|
562
|
-
this.modified(value).then(result => checkAborted(result, value));
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
ptime = mtime + types_1.THRESHOLD.WATCH_CHANGE;
|
|
566
|
-
}
|
|
567
|
-
break;
|
|
568
|
-
}
|
|
569
|
-
catch (err) {
|
|
570
|
-
this.writeFail(["Unable to read file" /* ERR_MESSAGE.READ_FILE */, path.basename(uri)], err, { type: 32 /* LOG_TYPE.FILE */, fatal: false });
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
case 'rename':
|
|
575
|
-
if (map) {
|
|
576
|
-
map.clear();
|
|
577
|
-
}
|
|
578
|
-
watchExpired(DISK_MAP, group, event === 'rename' ? 'File renamed: ' + filename : undefined);
|
|
579
|
-
abortTimeout(target);
|
|
580
|
-
break;
|
|
581
|
-
}
|
|
582
|
-
});
|
|
583
|
-
DISK_MAP[uri] = new Map([[dest, target]]);
|
|
584
|
-
}
|
|
585
|
-
else {
|
|
586
|
-
return 2 /* ERR.LOCAL_ACCESS */;
|
|
587
|
-
}
|
|
588
|
-
return 0 /* ERR.NONE */;
|
|
589
|
-
};
|
|
590
|
-
if ((0, types_1.isArray)(sourceFiles) && sourceFiles.some(file => this.canRead(file, { hostPermissionOnly: !!this.host }))) {
|
|
591
|
-
let index = 0;
|
|
592
|
-
for (const file of new Set(sourceFiles)) {
|
|
593
|
-
if (index++ > 0) {
|
|
594
|
-
watching(file, true);
|
|
595
|
-
}
|
|
596
|
-
else if ((status = watching(watched = file, true)) > 0) {
|
|
597
|
-
break;
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
else if (watched = item.uri) {
|
|
602
|
-
status = watching(watched, false);
|
|
603
|
-
}
|
|
604
|
-
else {
|
|
605
|
-
continue;
|
|
606
|
-
}
|
|
607
|
-
if (status > 0) {
|
|
608
|
-
if (!message) {
|
|
609
|
-
switch (status) {
|
|
610
|
-
case 1 /* ERR.ETAG */:
|
|
611
|
-
if ((0, types_1.existsFlag)(item.flags)) {
|
|
612
|
-
continue;
|
|
613
|
-
}
|
|
614
|
-
message = 'ETag unavailable';
|
|
615
|
-
break;
|
|
616
|
-
case 2 /* ERR.LOCAL_ACCESS */:
|
|
617
|
-
message = 'No read permission';
|
|
618
|
-
break;
|
|
619
|
-
case 3 /* ERR.SERVER */:
|
|
620
|
-
message = 'Server already in use';
|
|
621
|
-
break;
|
|
622
|
-
default:
|
|
623
|
-
message = "Unknown" /* ERR_MESSAGE.UNKNOWN */;
|
|
624
|
-
break;
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
this.formatFail((16 /* LOG_TYPE.WATCH */ | (status === 2 /* ERR.LOCAL_ACCESS */ ? 8192 /* LOG_TYPE.PERMISSION */ : 0)), 'WATCH', ["Unable to watch file" /* ERR_MESSAGE.WATCH_FILE */, watched && path.basename(watched)], (0, types_1.errorValue)(message, watched));
|
|
628
|
-
}
|
|
629
|
-
else {
|
|
630
|
-
this.formatMessage(16 /* LOG_TYPE.WATCH */, 'WATCH', ['Start', interval + 'ms ' + (expires ? formatDate(expires) : 'never')], watched, { titleColor: 'blue' });
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
async modified(watch) {
|
|
637
|
-
this.formatMessage(16 /* LOG_TYPE.WATCH */, 'WATCH', 'File modified', watch.uri, { ...core_1.Client.LOG_STYLE_WARN });
|
|
638
|
-
const { host, assets } = this;
|
|
639
|
-
let items, sanitize = false;
|
|
640
|
-
if (watch.main) {
|
|
641
|
-
sanitize = watch.assets.length > assets.length;
|
|
642
|
-
items = sanitize ? watch.assets.map(item => filegroup_1.default.cloneAsset(item)) : assets.map(item => (0, types_1.cloneObject)(item, true));
|
|
643
|
-
}
|
|
644
|
-
else {
|
|
645
|
-
items = [];
|
|
646
|
-
if (assets.length) {
|
|
647
|
-
for (const item of watch.assets) {
|
|
648
|
-
const asset = assets.find(other => item.id && item.id === other.id);
|
|
649
|
-
if (!asset) {
|
|
650
|
-
sanitize = true;
|
|
651
|
-
break;
|
|
652
|
-
}
|
|
653
|
-
items.push((0, types_1.cloneObject)(item, true));
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
else {
|
|
657
|
-
sanitize = true;
|
|
658
|
-
}
|
|
659
|
-
if (sanitize) {
|
|
660
|
-
items = watch.assets.map(item => filegroup_1.default.cloneAsset(item));
|
|
661
|
-
}
|
|
662
|
-
if (watch.related.length) {
|
|
663
|
-
items.push(...watch.related);
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
if (!sanitize) {
|
|
667
|
-
items.forEach(item => item.flags |= 2 /* ASSET_FLAG.CLONE */);
|
|
668
|
-
}
|
|
669
|
-
watch.captured = false;
|
|
670
|
-
try {
|
|
671
|
-
const manager = this.whenModified?.(items, sanitize);
|
|
672
|
-
for (const callback of this.extensions) {
|
|
673
|
-
try {
|
|
674
|
-
const listener = callback(watch, items);
|
|
675
|
-
if (typeof listener === 'function') {
|
|
676
|
-
if (manager) {
|
|
677
|
-
manager.on('end', listener);
|
|
678
|
-
}
|
|
679
|
-
else if (!watch.aborted) {
|
|
680
|
-
listener([], []);
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
catch {
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
if (manager) {
|
|
688
|
-
if (host && watch.document) {
|
|
689
|
-
for (const { instance } of host.Document) {
|
|
690
|
-
if (host.hasDocument(instance, watch.document)) {
|
|
691
|
-
const result = instance.watchInit?.(watch, items, sanitize);
|
|
692
|
-
if (result) {
|
|
693
|
-
if ((0, types_1.isArray)(result.using)) {
|
|
694
|
-
manager.using(...result.using);
|
|
695
|
-
}
|
|
696
|
-
if (Array.isArray(result.dataSource)) {
|
|
697
|
-
const document = manager.find(instance.moduleName);
|
|
698
|
-
if (document) {
|
|
699
|
-
document.dataSource = result.dataSource;
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
const listener = instance.watchModified?.(watch, items);
|
|
704
|
-
if (typeof listener === 'function') {
|
|
705
|
-
manager.on('end', listener);
|
|
706
|
-
}
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
return await manager.start();
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
|
-
catch (err) {
|
|
714
|
-
this.writeFail(["Unknown" /* ERR_MESSAGE.UNKNOWN */, watch.url?.pathname || path.basename(watch.uri)], err);
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
configureServer({ ca, key, cert, passphrase, version, config }) {
|
|
718
|
-
if ((0, types_1.isString)(key) && (0, types_1.isString)(cert)) {
|
|
719
|
-
if (ca) {
|
|
720
|
-
this.setCA(ca);
|
|
721
|
-
}
|
|
722
|
-
if (request_1.default.isCert(key) || core_1.Client.isPath(key = path.resolve(key = key.trim())) && this.canRead(key, { ownPermissionOnly: true })) {
|
|
723
|
-
this[kTlsKey] = key.trim();
|
|
724
|
-
}
|
|
725
|
-
if (request_1.default.isCert(cert) || core_1.Client.isPath(cert = path.resolve(cert = cert.trim())) && this.canRead(cert, { ownPermissionOnly: true })) {
|
|
726
|
-
this[kTlsCert] = cert.trim();
|
|
727
|
-
}
|
|
728
|
-
if ((0, types_1.isString)(passphrase)) {
|
|
729
|
-
this[kTlsPassphrase] = passphrase;
|
|
730
|
-
}
|
|
731
|
-
if (!(0, types_1.isPlainObject)(config)) {
|
|
732
|
-
config = undefined;
|
|
733
|
-
}
|
|
734
|
-
else if (version || (version = config.minVersion)) {
|
|
735
|
-
delete config.minVersion;
|
|
736
|
-
delete config.secureProtocol;
|
|
737
|
-
}
|
|
738
|
-
switch (version) {
|
|
739
|
-
case 'TLSv1':
|
|
740
|
-
case 'TLSv1.1':
|
|
741
|
-
case 'TLSv1.2':
|
|
742
|
-
case 'TLSv1.3':
|
|
743
|
-
break;
|
|
744
|
-
default:
|
|
745
|
-
version = undefined;
|
|
746
|
-
break;
|
|
747
|
-
}
|
|
748
|
-
this[kTlsConfig] = config;
|
|
749
|
-
this[kTlsVersion] = version;
|
|
750
|
-
return this.hasSecureProtocol();
|
|
751
|
-
}
|
|
752
|
-
return false;
|
|
753
|
-
}
|
|
754
|
-
setCA(value) {
|
|
755
|
-
if ((0, types_1.isString)(value) && core_1.Client.isPath(value = path.resolve(value = value.trim())) && this.canRead(value, { ownPermissionOnly: true })) {
|
|
756
|
-
this[kCa] = value;
|
|
757
|
-
return true;
|
|
758
|
-
}
|
|
759
|
-
return false;
|
|
760
|
-
}
|
|
761
|
-
setSSLKey(value) {
|
|
762
|
-
this[kTlsConfig] = { secureProtocol: 'SSLv23_method' };
|
|
763
|
-
this[kTlsVersion] = undefined;
|
|
764
|
-
return !!(this[kTlsKey] = request_1.default.readTLSKey(value, true));
|
|
765
|
-
}
|
|
766
|
-
setSSLCert(value) {
|
|
767
|
-
return !!(this[kTlsCert] = request_1.default.readTLSCert(value, true));
|
|
768
|
-
}
|
|
769
|
-
hasSecureProtocol() {
|
|
770
|
-
return !!this[kTlsKey] && !!this[kTlsCert];
|
|
771
|
-
}
|
|
772
|
-
willAbort(value) {
|
|
773
|
-
return this.host ? this.host.willAbort(value) : value === "(watch)" /* ABORT_NAME.WATCH */;
|
|
774
|
-
}
|
|
775
|
-
set host(value) {
|
|
776
|
-
super.host = value;
|
|
777
|
-
if (value && (0, types_1.isArray)(value.assets)) {
|
|
778
|
-
this.assets = value.assets;
|
|
779
|
-
}
|
|
780
|
-
}
|
|
781
|
-
get host() {
|
|
782
|
-
return this._host;
|
|
783
|
-
}
|
|
784
|
-
set assets(value) {
|
|
785
|
-
const length = value.length;
|
|
786
|
-
const items = new Array(length);
|
|
787
|
-
for (let i = 0; i < length; ++i) {
|
|
788
|
-
items[i] = Object.freeze((0, types_1.cloneObject)(value[i], true));
|
|
789
|
-
}
|
|
790
|
-
this._assets = items;
|
|
791
|
-
}
|
|
792
|
-
get assets() {
|
|
793
|
-
return this._assets;
|
|
794
|
-
}
|
|
795
|
-
set interval(value) {
|
|
796
|
-
if (value > 0) {
|
|
797
|
-
this[kInterval] = value;
|
|
798
|
-
}
|
|
799
|
-
}
|
|
800
|
-
get interval() {
|
|
801
|
-
return this[kInterval];
|
|
802
|
-
}
|
|
803
|
-
set port(value) {
|
|
804
|
-
if (value > 0) {
|
|
805
|
-
this[kPort] = value;
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
get port() {
|
|
809
|
-
return this[kPort];
|
|
810
|
-
}
|
|
811
|
-
set securePort(value) {
|
|
812
|
-
if (value > 0) {
|
|
813
|
-
this[kSecurePort] = value;
|
|
814
|
-
}
|
|
815
|
-
}
|
|
816
|
-
get securePort() {
|
|
817
|
-
return this[kSecurePort];
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
_a = kInterval, _b = kPort, _c = kSecurePort, _d = kCa, _e = kTlsKey, _f = kTlsCert, _g = kTlsPassphrase, _h = kTlsVersion, _j = kTlsConfig;
|
|
821
|
-
Watch.PROCESS_TIMEOUT = 5 * 1000 /* TIME.S */;
|
|
822
|
-
exports.default = Watch;
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const fs = require("fs");
|
|
6
|
+
const https = require("https");
|
|
7
|
+
const ws = require("ws");
|
|
8
|
+
const pm = require("picomatch");
|
|
9
|
+
const util_1 = require("../request/util");
|
|
10
|
+
const types_1 = require("../types");
|
|
11
|
+
const core_1 = require("../core");
|
|
12
|
+
const request_1 = require("../request");
|
|
13
|
+
const filegroup_1 = require("./filegroup");
|
|
14
|
+
const kInterval = Symbol('interval');
|
|
15
|
+
const kPort = Symbol('port');
|
|
16
|
+
const kSecurePort = Symbol('securePort');
|
|
17
|
+
const kCa = Symbol('ca');
|
|
18
|
+
const kTlsKey = Symbol('tlsKey');
|
|
19
|
+
const kTlsCert = Symbol('tlsCert');
|
|
20
|
+
const kTlsVersion = Symbol('tlsVersion');
|
|
21
|
+
const kTlsPassphrase = Symbol('tlsPassphrase');
|
|
22
|
+
const kTlsConfig = Symbol('tlsConfig');
|
|
23
|
+
const PLATFORM_WIN32 = process.platform === 'win32';
|
|
24
|
+
let STATE_MAP = new WeakSet();
|
|
25
|
+
let HTTP_MAP = {};
|
|
26
|
+
let DISK_MAP = {};
|
|
27
|
+
let PORT_MAP = {};
|
|
28
|
+
let SECURE_MAP = {};
|
|
29
|
+
function isConnectionTimeout(err) {
|
|
30
|
+
switch (err instanceof Error && err.code) {
|
|
31
|
+
case 'ETIMEDOUT':
|
|
32
|
+
case 'ECONNRESET':
|
|
33
|
+
return true;
|
|
34
|
+
default:
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function abortTimeout(group) {
|
|
39
|
+
group.timeout.aborted = true;
|
|
40
|
+
if (group.watcher) {
|
|
41
|
+
group.watcher.close();
|
|
42
|
+
group.watcher = null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const getInterval = ({ watch }) => Math.max((0, types_1.isObject)(watch) && watch.interval || 0, 0);
|
|
46
|
+
const formatDate = (value) => new Date(value).toLocaleString().replace(/\/20\d+, /, '@').replace(/:\d+ (AM|PM)$/, (...match) => match[1]);
|
|
47
|
+
class Watch extends core_1.Client {
|
|
48
|
+
static createServer(port, secure, active) {
|
|
49
|
+
if (typeof secure === 'boolean') {
|
|
50
|
+
active = secure;
|
|
51
|
+
secure = null;
|
|
52
|
+
}
|
|
53
|
+
let wss;
|
|
54
|
+
try {
|
|
55
|
+
if ((0, types_1.isPlainObject)(secure)) {
|
|
56
|
+
if (!(wss = SECURE_MAP[port])) {
|
|
57
|
+
const { ca, passphrase, version, config } = secure;
|
|
58
|
+
let { key, cert } = secure;
|
|
59
|
+
if (key && cert && (key = request_1.default.readTLSKey(key, true)) && (cert = request_1.default.readTLSKey(cert, true))) {
|
|
60
|
+
const server = https.createServer({
|
|
61
|
+
...config,
|
|
62
|
+
ca: ca && request_1.default.readCACert(ca, true) || undefined,
|
|
63
|
+
key: passphrase ? [{ pem: key, passphrase }] : key,
|
|
64
|
+
cert,
|
|
65
|
+
minVersion: version || config?.minVersion
|
|
66
|
+
});
|
|
67
|
+
server.listen(port);
|
|
68
|
+
wss = new ws.Server({ server });
|
|
69
|
+
SECURE_MAP[port] = wss;
|
|
70
|
+
if (active) {
|
|
71
|
+
STATE_MAP.add(wss);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
this.writeFail('TLS/SSL key and cert not found', (0, types_1.errorMessage)('ws', 'Missing TLS/SSL credentials'));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
return wss;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else if (!(wss = PORT_MAP[port])) {
|
|
83
|
+
wss = new ws.Server({ port });
|
|
84
|
+
PORT_MAP[port] = wss;
|
|
85
|
+
if (active) {
|
|
86
|
+
STATE_MAP.add(wss);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
return wss;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
this.writeFail("Unknown" /* ERR_MESSAGE.UNKNOWN */, err);
|
|
95
|
+
}
|
|
96
|
+
if (wss) {
|
|
97
|
+
wss.on('connection', function (socket) {
|
|
98
|
+
socket.on('message', function () {
|
|
99
|
+
filegroup_1.default.CLIENT_SESSION.delete(this);
|
|
100
|
+
});
|
|
101
|
+
socket.on('close', function () {
|
|
102
|
+
filegroup_1.default.CLIENT_SESSION.delete(this);
|
|
103
|
+
});
|
|
104
|
+
this.clients.forEach(client => filegroup_1.default.checkTimeout(client));
|
|
105
|
+
});
|
|
106
|
+
wss.on('error', function (err) {
|
|
107
|
+
const data = JSON.stringify({ event: types_1.WATCH_EVENT.ERROR, errors: [err.message] });
|
|
108
|
+
this.clients.forEach(client => filegroup_1.default.checkTimeout(client) && client.send(data));
|
|
109
|
+
});
|
|
110
|
+
wss.on('close', function () {
|
|
111
|
+
this.clients.forEach(client => client.terminate());
|
|
112
|
+
});
|
|
113
|
+
return wss;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
static shutdown() {
|
|
117
|
+
for (const item of [HTTP_MAP, DISK_MAP]) {
|
|
118
|
+
for (const uri in item) {
|
|
119
|
+
for (const group of item[uri].values()) {
|
|
120
|
+
abortTimeout(group);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
for (const item of [PORT_MAP, SECURE_MAP]) {
|
|
125
|
+
for (const port in item) {
|
|
126
|
+
item[port].close(err => err && this.writeFail([`Unable to shutdown ${item === PORT_MAP ? 'WS' : 'WSS'} server`, 'port: ' + port], err));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
filegroup_1.default.CLIENT_SESSION.clear();
|
|
130
|
+
HTTP_MAP = {};
|
|
131
|
+
DISK_MAP = {};
|
|
132
|
+
PORT_MAP = {};
|
|
133
|
+
SECURE_MAP = {};
|
|
134
|
+
STATE_MAP = new WeakSet();
|
|
135
|
+
}
|
|
136
|
+
static checkTimeout(client) {
|
|
137
|
+
return filegroup_1.default.checkTimeout(client);
|
|
138
|
+
}
|
|
139
|
+
static setTimeout(value) {
|
|
140
|
+
if ((value = (0, types_1.parseTime)(value)) > 0) {
|
|
141
|
+
filegroup_1.default.CONNECTION_TIMEOUT = Math.min(value, core_1.Client.MAX_TIMEOUT);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
constructor(data, port, securePort, extensions) {
|
|
145
|
+
super(data);
|
|
146
|
+
this.connectTimeout = Watch.PROCESS_TIMEOUT;
|
|
147
|
+
this._moduleName = 'watch';
|
|
148
|
+
this._assets = [];
|
|
149
|
+
this._extensions = [];
|
|
150
|
+
this._hostEvents = [];
|
|
151
|
+
this[_a] = types_1.THRESHOLD.WATCH_INTERVAL;
|
|
152
|
+
this[_b] = 80 /* HTTP.PORT */;
|
|
153
|
+
this[_c] = 443 /* HTTP.PORT_SECURE */;
|
|
154
|
+
this[_d] = '';
|
|
155
|
+
this[_e] = '';
|
|
156
|
+
this[_f] = '';
|
|
157
|
+
this[_g] = '';
|
|
158
|
+
this[_h] = undefined;
|
|
159
|
+
this[_j] = undefined;
|
|
160
|
+
let interval;
|
|
161
|
+
if ((0, types_1.isPlainObject)(data)) {
|
|
162
|
+
let secure;
|
|
163
|
+
({ interval, port, secure } = data);
|
|
164
|
+
if (secure) {
|
|
165
|
+
securePort = secure.port;
|
|
166
|
+
if (secure.cert && secure.key) {
|
|
167
|
+
this.configureServer(secure);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if ((interval = (0, util_1.asInt)(interval)) > 0) {
|
|
172
|
+
this.interval = interval;
|
|
173
|
+
}
|
|
174
|
+
if ((port = (0, util_1.asInt)(port)) > 0) {
|
|
175
|
+
this.port = port;
|
|
176
|
+
}
|
|
177
|
+
if ((securePort = (0, util_1.asInt)(securePort)) > 0) {
|
|
178
|
+
this.securePort = securePort;
|
|
179
|
+
}
|
|
180
|
+
if ((0, types_1.isArray)(extensions)) {
|
|
181
|
+
this.module.extensions = extensions;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
start(assets, permission) {
|
|
185
|
+
var _k;
|
|
186
|
+
if (this.aborted) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (permission) {
|
|
190
|
+
this.permission = permission;
|
|
191
|
+
}
|
|
192
|
+
const startTime = Date.now();
|
|
193
|
+
const username = this.host?.username;
|
|
194
|
+
const destMap = Object.create(null);
|
|
195
|
+
for (const item of assets) {
|
|
196
|
+
if ((0, types_1.ignoreFlag)(item.flags)) {
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
const { bundleId, uri, localUri } = item;
|
|
200
|
+
if (!(0, types_1.isEmpty)(bundleId)) {
|
|
201
|
+
(destMap[_k = ':' + bundleId] || (destMap[_k] = [])).push(item);
|
|
202
|
+
}
|
|
203
|
+
else if (uri && localUri) {
|
|
204
|
+
(destMap[localUri] || (destMap[localUri] = [])).push(item);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
for (let dest in destMap) {
|
|
208
|
+
const items = destMap[dest];
|
|
209
|
+
if (!items.some(item => item.watch)) {
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
let watchInterval, bundleMain;
|
|
213
|
+
if (dest[0] === ':') {
|
|
214
|
+
items.sort((a, b) => a.bundleIndex - b.bundleIndex);
|
|
215
|
+
bundleMain = items[0];
|
|
216
|
+
dest = bundleMain.localUri;
|
|
217
|
+
const leading = items.find(item => getInterval(item) > 0);
|
|
218
|
+
if (leading) {
|
|
219
|
+
watchInterval = getInterval(leading);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const related = [];
|
|
223
|
+
for (let i = 0, length = items.length; i < length; ++i) {
|
|
224
|
+
const watch = items[i].watch;
|
|
225
|
+
if ((0, types_1.isObject)(watch) && watch.assets) {
|
|
226
|
+
watch.assets.forEach(other => {
|
|
227
|
+
if (items.includes(other)) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if ((0, types_1.watchFlag)(other.flags)) {
|
|
231
|
+
if (!other.watch) {
|
|
232
|
+
other = filegroup_1.default.cloneAsset(other);
|
|
233
|
+
other.watch = { ...watch };
|
|
234
|
+
}
|
|
235
|
+
other.flags &= ~1 /* ASSET_FLAG.IGNORE */;
|
|
236
|
+
other.invalid = undefined;
|
|
237
|
+
items.push(other);
|
|
238
|
+
}
|
|
239
|
+
else if (!related.includes(other)) {
|
|
240
|
+
related.push(other);
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
for (const item of items) {
|
|
246
|
+
let { watch, sourceFiles } = item;
|
|
247
|
+
if (watch) {
|
|
248
|
+
const fatalError = (map, target, err) => {
|
|
249
|
+
const uri = target.value.uri;
|
|
250
|
+
delete map[uri];
|
|
251
|
+
abortTimeout(target);
|
|
252
|
+
this.writeFail(["Unable to watch file" /* ERR_MESSAGE.WATCH_FILE */, uri], err, 16 /* LOG_TYPE.WATCH */);
|
|
253
|
+
};
|
|
254
|
+
const watchExpired = (map, target, message = 'Expired') => {
|
|
255
|
+
this.formatMessage(16 /* LOG_TYPE.WATCH */, 'WATCH', [message, target.startTime ? 'since ' + formatDate(target.startTime) : ''], target.uri, { titleColor: 'grey' });
|
|
256
|
+
const data = map[target.uri];
|
|
257
|
+
if (!data || data.size === 0) {
|
|
258
|
+
delete map[target.uri];
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
return false;
|
|
262
|
+
};
|
|
263
|
+
let expires = 0, status = 0, wss, main, id, socketId, port, secure, hot, always, watched, message, settings;
|
|
264
|
+
if (watch === true && username && (settings = this.settings.users?.[username])) {
|
|
265
|
+
const { uri, localUri, mimeType } = item;
|
|
266
|
+
watch = false;
|
|
267
|
+
if (uri && !(watch = settings[uri])) {
|
|
268
|
+
for (const pattern in settings) {
|
|
269
|
+
if ((0, types_1.hasGlob)(pattern) && pm.isMatch(uri, pattern, { matchBase: true }) && (watch = settings[pattern])) {
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
if (!watch && localUri && !(watch = settings[localUri])) {
|
|
275
|
+
for (const pattern in settings) {
|
|
276
|
+
if ((0, types_1.hasGlob)(pattern) && pm.isMatch(localUri, pattern, { nocase: PLATFORM_WIN32 }) && (watch = settings[pattern])) {
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (!watch && mimeType && !(watch = settings[mimeType])) {
|
|
282
|
+
for (const pattern in settings) {
|
|
283
|
+
if (/^[^*/]+\/\*+$/.test(pattern) && pm.isMatch(mimeType, pattern) && (watch = settings[pattern])) {
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if ((0, types_1.isPlainObject)(watch)) {
|
|
290
|
+
const reload = watch.reload;
|
|
291
|
+
id = watch.id;
|
|
292
|
+
if (watch.main) {
|
|
293
|
+
main = item;
|
|
294
|
+
}
|
|
295
|
+
if (watch.expires) {
|
|
296
|
+
expires = (0, types_1.parseExpires)(watch.expires, startTime);
|
|
297
|
+
}
|
|
298
|
+
if (reload && (socketId = reload.socketId)) {
|
|
299
|
+
({ port, module: hot, always } = reload);
|
|
300
|
+
if (reload.secure) {
|
|
301
|
+
const key = this[kTlsKey];
|
|
302
|
+
const cert = this[kTlsCert];
|
|
303
|
+
if (key && cert) {
|
|
304
|
+
wss = Watch.createServer(port || (port = this.securePort), { ca: this[kCa], key, cert, passphrase: this[kTlsPassphrase], version: this[kTlsVersion], config: this[kTlsConfig] });
|
|
305
|
+
}
|
|
306
|
+
secure = true;
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
wss = Watch.createServer(port || (port = this.port));
|
|
310
|
+
}
|
|
311
|
+
if (!wss) {
|
|
312
|
+
this.writeFail('Unable to create WebSocket server', (0, types_1.errorMessage)(secure ? 'wss' : 'ws', "Invalid parameters" /* ERR_MESSAGE.PARAMETERS */, 'port: ' + (port || "Unknown" /* ERR_MESSAGE.UNKNOWN */)), 16 /* LOG_TYPE.WATCH */);
|
|
313
|
+
socketId = undefined;
|
|
314
|
+
port = undefined;
|
|
315
|
+
secure = undefined;
|
|
316
|
+
hot = undefined;
|
|
317
|
+
always = undefined;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
item.watch = watch;
|
|
321
|
+
}
|
|
322
|
+
const interval = getInterval(item) || watchInterval || this.interval;
|
|
323
|
+
const watching = (uri, esm) => {
|
|
324
|
+
const group = new filegroup_1.default(uri, main ? assets : items, startTime, this.willAbort("(watch)" /* ABORT_NAME.WATCH */));
|
|
325
|
+
group.add(expires, socketId, id);
|
|
326
|
+
if (main) {
|
|
327
|
+
group.main = main;
|
|
328
|
+
}
|
|
329
|
+
else if (related.length) {
|
|
330
|
+
group.related = related;
|
|
331
|
+
}
|
|
332
|
+
if (bundleMain) {
|
|
333
|
+
group.bundleMain = bundleMain;
|
|
334
|
+
}
|
|
335
|
+
if (item.document) {
|
|
336
|
+
group.document = item.document;
|
|
337
|
+
}
|
|
338
|
+
if (hot) {
|
|
339
|
+
group.hot = true;
|
|
340
|
+
}
|
|
341
|
+
if (always) {
|
|
342
|
+
group.always = true;
|
|
343
|
+
}
|
|
344
|
+
if (sourceFiles) {
|
|
345
|
+
group.sourceFiles = sourceFiles.filter(file => file !== uri);
|
|
346
|
+
}
|
|
347
|
+
if (wss) {
|
|
348
|
+
group.connection(wss, port, secure);
|
|
349
|
+
}
|
|
350
|
+
const timeout = { interval, retries: 0, aborted: false };
|
|
351
|
+
const target = { value: group, timeout, watcher: null };
|
|
352
|
+
const isMap = (value) => value ? value.size > 0 : false;
|
|
353
|
+
const checkAborted = (result, current) => result?.aborted && current.abortable && current.abort();
|
|
354
|
+
const checkPreceding = (map) => {
|
|
355
|
+
const data = map[uri];
|
|
356
|
+
if (!data) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
const current = data.get(dest)?.value;
|
|
360
|
+
if (current && !current.expired) {
|
|
361
|
+
let reset;
|
|
362
|
+
if (id) {
|
|
363
|
+
const sockets = current.sockets;
|
|
364
|
+
for (let i = 0, active; i < sockets.length; ++i) {
|
|
365
|
+
const socket = sockets[i];
|
|
366
|
+
if (socket.id === id) {
|
|
367
|
+
if (active) {
|
|
368
|
+
sockets.splice(i, 1);
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
reset = true;
|
|
372
|
+
}
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
if (!socket.expired) {
|
|
376
|
+
active = true;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
if (!reset) {
|
|
381
|
+
if (wss) {
|
|
382
|
+
const server = current.server;
|
|
383
|
+
if (!server) {
|
|
384
|
+
current.connection(wss, port, secure);
|
|
385
|
+
}
|
|
386
|
+
else if (server !== wss) {
|
|
387
|
+
if (wss.clients.size === 0 && !STATE_MAP.has(wss)) {
|
|
388
|
+
if (secure) {
|
|
389
|
+
delete SECURE_MAP[port];
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
delete PORT_MAP[port];
|
|
393
|
+
}
|
|
394
|
+
wss.close();
|
|
395
|
+
}
|
|
396
|
+
message = 'Destination already watched @ ' + (current.secure ? 'wss' : 'ws') + '://hostname:' + current.port;
|
|
397
|
+
return 3 /* ERR.SERVER */;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
const socket = socketId && current.find(socketId);
|
|
401
|
+
if (socket) {
|
|
402
|
+
socket.expires = expires;
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
current.add(expires, socketId, id);
|
|
406
|
+
}
|
|
407
|
+
return 0 /* ERR.NONE */;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
data.set(dest, target);
|
|
411
|
+
return 0 /* ERR.NONE */;
|
|
412
|
+
};
|
|
413
|
+
if (core_1.Client.isFile(uri, 'http/s')) {
|
|
414
|
+
group.url = item.url;
|
|
415
|
+
group.etag = item.etag;
|
|
416
|
+
group.lastModified = item.lastModified;
|
|
417
|
+
if (!group.etag && !group.lastModified) {
|
|
418
|
+
return 1 /* ERR.ETAG */;
|
|
419
|
+
}
|
|
420
|
+
if ((status = checkPreceding(HTTP_MAP)) !== undefined) {
|
|
421
|
+
return status;
|
|
422
|
+
}
|
|
423
|
+
const url = group.url || (group.url = new URL(uri));
|
|
424
|
+
const request = this.host?.Request || new request_1.default();
|
|
425
|
+
const agentTimeout = Math.max(timeout.interval * 10, this.connectTimeout);
|
|
426
|
+
const opts = request.opts(url, { method: 'HEAD', httpVersion: 1, timeout: agentTimeout, agentTimeout });
|
|
427
|
+
(function recurse() {
|
|
428
|
+
let client = null;
|
|
429
|
+
new Promise((resolve, reject) => {
|
|
430
|
+
client = request.open(uri, opts)
|
|
431
|
+
.on('response', res => {
|
|
432
|
+
if (group.aborted) {
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
if (this.aborted) {
|
|
436
|
+
reject((0, types_1.createAbortError)());
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
const statusCode = res.statusCode;
|
|
440
|
+
const valid = statusCode >= 200 /* HTTP_STATUS.OK */ && statusCode < 300 /* HTTP_STATUS.MULTIPLE_CHOICES */;
|
|
441
|
+
const map = HTTP_MAP[uri];
|
|
442
|
+
const etag = res.headers.etag;
|
|
443
|
+
const lastModified = res.headers['last-modified'];
|
|
444
|
+
if (valid && map?.size && (etag || lastModified)) {
|
|
445
|
+
for (const [destUrl, input] of map) {
|
|
446
|
+
const value = input.value;
|
|
447
|
+
if (value.paused) {
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
let aborted = value.aborted;
|
|
451
|
+
if (!aborted) {
|
|
452
|
+
if (value.expired) {
|
|
453
|
+
aborted = true;
|
|
454
|
+
}
|
|
455
|
+
else if (value.etag) {
|
|
456
|
+
if (etag) {
|
|
457
|
+
if (etag !== value.etag) {
|
|
458
|
+
value.etag = etag;
|
|
459
|
+
this.modified(value).then(result => checkAborted(result, value));
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
else if (value.lastModified && lastModified) {
|
|
464
|
+
if (lastModified !== value.lastModified) {
|
|
465
|
+
value.lastModified = lastModified;
|
|
466
|
+
this.modified(value).then(result => checkAborted(result, value));
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
if (aborted) {
|
|
471
|
+
map.delete(destUrl);
|
|
472
|
+
if (watchExpired(HTTP_MAP, value)) {
|
|
473
|
+
abortTimeout(target);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
else {
|
|
479
|
+
if (isMap(map)) {
|
|
480
|
+
reject(valid ? (0, types_1.errorValue)('ETag not supported', uri) : (0, types_1.errorMessage)(statusCode, 'Invalid HTTP request', uri));
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
watchExpired(HTTP_MAP, group);
|
|
484
|
+
abortTimeout(target);
|
|
485
|
+
}
|
|
486
|
+
if (client) {
|
|
487
|
+
client.destroy();
|
|
488
|
+
}
|
|
489
|
+
resolve();
|
|
490
|
+
})
|
|
491
|
+
.on('error', err => {
|
|
492
|
+
if (!timeout.aborted && !(isConnectionTimeout(err) && ++timeout.retries <= 10 /* HTTP.TIMEOUT_LIMIT */)) {
|
|
493
|
+
reject(err);
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
resolve();
|
|
497
|
+
})
|
|
498
|
+
.on('timeout', () => {
|
|
499
|
+
if (!timeout.aborted && ++timeout.retries > 10 /* HTTP.TIMEOUT_LIMIT */) {
|
|
500
|
+
reject((0, types_1.errorMessage)(408 /* HTTP_STATUS.REQUEST_TIMEOUT */, 'HTTP request timeout'));
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
resolve();
|
|
504
|
+
});
|
|
505
|
+
})
|
|
506
|
+
.then(() => {
|
|
507
|
+
if (!timeout.aborted) {
|
|
508
|
+
const map = HTTP_MAP[uri];
|
|
509
|
+
if (isMap(map)) {
|
|
510
|
+
let ms = Watch.MAX_TIMEOUT;
|
|
511
|
+
for (const session of map.values()) {
|
|
512
|
+
ms = Math.min(session.timeout.interval, ms);
|
|
513
|
+
}
|
|
514
|
+
setTimeout(recurse.bind(this), timeout.interval = ms);
|
|
515
|
+
}
|
|
516
|
+
else {
|
|
517
|
+
delete HTTP_MAP[uri];
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
})
|
|
521
|
+
.catch(err => {
|
|
522
|
+
fatalError(HTTP_MAP, target, err);
|
|
523
|
+
if (client) {
|
|
524
|
+
client.destroy();
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
}).call(this);
|
|
528
|
+
HTTP_MAP[uri] = new Map([[dest, target]]);
|
|
529
|
+
}
|
|
530
|
+
else if (esm || path.isAbsolute(uri = core_1.Client.resolveFile(uri)) && this.canRead(uri, { hostPermissionOnly: !!this.host })) {
|
|
531
|
+
if ((status = checkPreceding(DISK_MAP)) !== undefined) {
|
|
532
|
+
return status;
|
|
533
|
+
}
|
|
534
|
+
let ptime = 0;
|
|
535
|
+
target.watcher = fs.watch(uri, (event, filename) => {
|
|
536
|
+
if (this.aborted || group.aborted) {
|
|
537
|
+
fatalError(DISK_MAP, target, (0, types_1.createAbortError)());
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
const map = DISK_MAP[uri];
|
|
541
|
+
switch (event) {
|
|
542
|
+
case 'change': {
|
|
543
|
+
if (isMap(map)) {
|
|
544
|
+
try {
|
|
545
|
+
const mtime = Math.floor(fs.statSync(uri).mtimeMs);
|
|
546
|
+
if (mtime > ptime) {
|
|
547
|
+
for (const [key, input] of map) {
|
|
548
|
+
const value = input.value;
|
|
549
|
+
if (value.expired) {
|
|
550
|
+
map.delete(key);
|
|
551
|
+
if (watchExpired(DISK_MAP, value)) {
|
|
552
|
+
abortTimeout(target);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
else if (value.aborted) {
|
|
556
|
+
map.delete(key);
|
|
557
|
+
if (map.size === 0) {
|
|
558
|
+
abortTimeout(target);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
else {
|
|
562
|
+
this.modified(value).then(result => checkAborted(result, value));
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
ptime = mtime + types_1.THRESHOLD.WATCH_CHANGE;
|
|
566
|
+
}
|
|
567
|
+
break;
|
|
568
|
+
}
|
|
569
|
+
catch (err) {
|
|
570
|
+
this.writeFail(["Unable to read file" /* ERR_MESSAGE.READ_FILE */, path.basename(uri)], err, { type: 32 /* LOG_TYPE.FILE */, fatal: false });
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
case 'rename':
|
|
575
|
+
if (map) {
|
|
576
|
+
map.clear();
|
|
577
|
+
}
|
|
578
|
+
watchExpired(DISK_MAP, group, event === 'rename' ? 'File renamed: ' + filename : undefined);
|
|
579
|
+
abortTimeout(target);
|
|
580
|
+
break;
|
|
581
|
+
}
|
|
582
|
+
});
|
|
583
|
+
DISK_MAP[uri] = new Map([[dest, target]]);
|
|
584
|
+
}
|
|
585
|
+
else {
|
|
586
|
+
return 2 /* ERR.LOCAL_ACCESS */;
|
|
587
|
+
}
|
|
588
|
+
return 0 /* ERR.NONE */;
|
|
589
|
+
};
|
|
590
|
+
if ((0, types_1.isArray)(sourceFiles) && sourceFiles.some(file => this.canRead(file, { hostPermissionOnly: !!this.host }))) {
|
|
591
|
+
let index = 0;
|
|
592
|
+
for (const file of new Set(sourceFiles)) {
|
|
593
|
+
if (index++ > 0) {
|
|
594
|
+
watching(file, true);
|
|
595
|
+
}
|
|
596
|
+
else if ((status = watching(watched = file, true)) > 0) {
|
|
597
|
+
break;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
else if (watched = item.uri) {
|
|
602
|
+
status = watching(watched, false);
|
|
603
|
+
}
|
|
604
|
+
else {
|
|
605
|
+
continue;
|
|
606
|
+
}
|
|
607
|
+
if (status > 0) {
|
|
608
|
+
if (!message) {
|
|
609
|
+
switch (status) {
|
|
610
|
+
case 1 /* ERR.ETAG */:
|
|
611
|
+
if ((0, types_1.existsFlag)(item.flags)) {
|
|
612
|
+
continue;
|
|
613
|
+
}
|
|
614
|
+
message = 'ETag unavailable';
|
|
615
|
+
break;
|
|
616
|
+
case 2 /* ERR.LOCAL_ACCESS */:
|
|
617
|
+
message = 'No read permission';
|
|
618
|
+
break;
|
|
619
|
+
case 3 /* ERR.SERVER */:
|
|
620
|
+
message = 'Server already in use';
|
|
621
|
+
break;
|
|
622
|
+
default:
|
|
623
|
+
message = "Unknown" /* ERR_MESSAGE.UNKNOWN */;
|
|
624
|
+
break;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
this.formatFail((16 /* LOG_TYPE.WATCH */ | (status === 2 /* ERR.LOCAL_ACCESS */ ? 8192 /* LOG_TYPE.PERMISSION */ : 0)), 'WATCH', ["Unable to watch file" /* ERR_MESSAGE.WATCH_FILE */, watched && path.basename(watched)], (0, types_1.errorValue)(message, watched));
|
|
628
|
+
}
|
|
629
|
+
else {
|
|
630
|
+
this.formatMessage(16 /* LOG_TYPE.WATCH */, 'WATCH', ['Start', interval + 'ms ' + (expires ? formatDate(expires) : 'never')], watched, { titleColor: 'blue' });
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
async modified(watch) {
|
|
637
|
+
this.formatMessage(16 /* LOG_TYPE.WATCH */, 'WATCH', 'File modified', watch.uri, { ...core_1.Client.LOG_STYLE_WARN });
|
|
638
|
+
const { host, assets } = this;
|
|
639
|
+
let items, sanitize = false;
|
|
640
|
+
if (watch.main) {
|
|
641
|
+
sanitize = watch.assets.length > assets.length;
|
|
642
|
+
items = sanitize ? watch.assets.map(item => filegroup_1.default.cloneAsset(item)) : assets.map(item => (0, types_1.cloneObject)(item, true));
|
|
643
|
+
}
|
|
644
|
+
else {
|
|
645
|
+
items = [];
|
|
646
|
+
if (assets.length) {
|
|
647
|
+
for (const item of watch.assets) {
|
|
648
|
+
const asset = assets.find(other => item.id && item.id === other.id);
|
|
649
|
+
if (!asset) {
|
|
650
|
+
sanitize = true;
|
|
651
|
+
break;
|
|
652
|
+
}
|
|
653
|
+
items.push((0, types_1.cloneObject)(item, true));
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
else {
|
|
657
|
+
sanitize = true;
|
|
658
|
+
}
|
|
659
|
+
if (sanitize) {
|
|
660
|
+
items = watch.assets.map(item => filegroup_1.default.cloneAsset(item));
|
|
661
|
+
}
|
|
662
|
+
if (watch.related.length) {
|
|
663
|
+
items.push(...watch.related);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
if (!sanitize) {
|
|
667
|
+
items.forEach(item => item.flags |= 2 /* ASSET_FLAG.CLONE */);
|
|
668
|
+
}
|
|
669
|
+
watch.captured = false;
|
|
670
|
+
try {
|
|
671
|
+
const manager = this.whenModified?.(items, sanitize);
|
|
672
|
+
for (const callback of this.extensions) {
|
|
673
|
+
try {
|
|
674
|
+
const listener = callback(watch, items);
|
|
675
|
+
if (typeof listener === 'function') {
|
|
676
|
+
if (manager) {
|
|
677
|
+
manager.on('end', listener);
|
|
678
|
+
}
|
|
679
|
+
else if (!watch.aborted) {
|
|
680
|
+
listener([], []);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
catch {
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
if (manager) {
|
|
688
|
+
if (host && watch.document) {
|
|
689
|
+
for (const { instance } of host.Document) {
|
|
690
|
+
if (host.hasDocument(instance, watch.document)) {
|
|
691
|
+
const result = instance.watchInit?.(watch, items, sanitize);
|
|
692
|
+
if (result) {
|
|
693
|
+
if ((0, types_1.isArray)(result.using)) {
|
|
694
|
+
manager.using(...result.using);
|
|
695
|
+
}
|
|
696
|
+
if (Array.isArray(result.dataSource)) {
|
|
697
|
+
const document = manager.find(instance.moduleName);
|
|
698
|
+
if (document) {
|
|
699
|
+
document.dataSource = result.dataSource;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
const listener = instance.watchModified?.(watch, items);
|
|
704
|
+
if (typeof listener === 'function') {
|
|
705
|
+
manager.on('end', listener);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
return await manager.start();
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
catch (err) {
|
|
714
|
+
this.writeFail(["Unknown" /* ERR_MESSAGE.UNKNOWN */, watch.url?.pathname || path.basename(watch.uri)], err);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
configureServer({ ca, key, cert, passphrase, version, config }) {
|
|
718
|
+
if ((0, types_1.isString)(key) && (0, types_1.isString)(cert)) {
|
|
719
|
+
if (ca) {
|
|
720
|
+
this.setCA(ca);
|
|
721
|
+
}
|
|
722
|
+
if (request_1.default.isCert(key) || core_1.Client.isPath(key = path.resolve(key = key.trim())) && this.canRead(key, { ownPermissionOnly: true })) {
|
|
723
|
+
this[kTlsKey] = key.trim();
|
|
724
|
+
}
|
|
725
|
+
if (request_1.default.isCert(cert) || core_1.Client.isPath(cert = path.resolve(cert = cert.trim())) && this.canRead(cert, { ownPermissionOnly: true })) {
|
|
726
|
+
this[kTlsCert] = cert.trim();
|
|
727
|
+
}
|
|
728
|
+
if ((0, types_1.isString)(passphrase)) {
|
|
729
|
+
this[kTlsPassphrase] = passphrase;
|
|
730
|
+
}
|
|
731
|
+
if (!(0, types_1.isPlainObject)(config)) {
|
|
732
|
+
config = undefined;
|
|
733
|
+
}
|
|
734
|
+
else if (version || (version = config.minVersion)) {
|
|
735
|
+
delete config.minVersion;
|
|
736
|
+
delete config.secureProtocol;
|
|
737
|
+
}
|
|
738
|
+
switch (version) {
|
|
739
|
+
case 'TLSv1':
|
|
740
|
+
case 'TLSv1.1':
|
|
741
|
+
case 'TLSv1.2':
|
|
742
|
+
case 'TLSv1.3':
|
|
743
|
+
break;
|
|
744
|
+
default:
|
|
745
|
+
version = undefined;
|
|
746
|
+
break;
|
|
747
|
+
}
|
|
748
|
+
this[kTlsConfig] = config;
|
|
749
|
+
this[kTlsVersion] = version;
|
|
750
|
+
return this.hasSecureProtocol();
|
|
751
|
+
}
|
|
752
|
+
return false;
|
|
753
|
+
}
|
|
754
|
+
setCA(value) {
|
|
755
|
+
if ((0, types_1.isString)(value) && core_1.Client.isPath(value = path.resolve(value = value.trim())) && this.canRead(value, { ownPermissionOnly: true })) {
|
|
756
|
+
this[kCa] = value;
|
|
757
|
+
return true;
|
|
758
|
+
}
|
|
759
|
+
return false;
|
|
760
|
+
}
|
|
761
|
+
setSSLKey(value) {
|
|
762
|
+
this[kTlsConfig] = { secureProtocol: 'SSLv23_method' };
|
|
763
|
+
this[kTlsVersion] = undefined;
|
|
764
|
+
return !!(this[kTlsKey] = request_1.default.readTLSKey(value, true));
|
|
765
|
+
}
|
|
766
|
+
setSSLCert(value) {
|
|
767
|
+
return !!(this[kTlsCert] = request_1.default.readTLSCert(value, true));
|
|
768
|
+
}
|
|
769
|
+
hasSecureProtocol() {
|
|
770
|
+
return !!this[kTlsKey] && !!this[kTlsCert];
|
|
771
|
+
}
|
|
772
|
+
willAbort(value) {
|
|
773
|
+
return this.host ? this.host.willAbort(value) : value === "(watch)" /* ABORT_NAME.WATCH */;
|
|
774
|
+
}
|
|
775
|
+
set host(value) {
|
|
776
|
+
super.host = value;
|
|
777
|
+
if (value && (0, types_1.isArray)(value.assets)) {
|
|
778
|
+
this.assets = value.assets;
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
get host() {
|
|
782
|
+
return this._host;
|
|
783
|
+
}
|
|
784
|
+
set assets(value) {
|
|
785
|
+
const length = value.length;
|
|
786
|
+
const items = new Array(length);
|
|
787
|
+
for (let i = 0; i < length; ++i) {
|
|
788
|
+
items[i] = Object.freeze((0, types_1.cloneObject)(value[i], true));
|
|
789
|
+
}
|
|
790
|
+
this._assets = items;
|
|
791
|
+
}
|
|
792
|
+
get assets() {
|
|
793
|
+
return this._assets;
|
|
794
|
+
}
|
|
795
|
+
set interval(value) {
|
|
796
|
+
if (value > 0) {
|
|
797
|
+
this[kInterval] = value;
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
get interval() {
|
|
801
|
+
return this[kInterval];
|
|
802
|
+
}
|
|
803
|
+
set port(value) {
|
|
804
|
+
if (value > 0) {
|
|
805
|
+
this[kPort] = value;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
get port() {
|
|
809
|
+
return this[kPort];
|
|
810
|
+
}
|
|
811
|
+
set securePort(value) {
|
|
812
|
+
if (value > 0) {
|
|
813
|
+
this[kSecurePort] = value;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
get securePort() {
|
|
817
|
+
return this[kSecurePort];
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
_a = kInterval, _b = kPort, _c = kSecurePort, _d = kCa, _e = kTlsKey, _f = kTlsCert, _g = kTlsPassphrase, _h = kTlsVersion, _j = kTlsConfig;
|
|
821
|
+
Watch.PROCESS_TIMEOUT = 5 * 1000 /* TIME.S */;
|
|
822
|
+
exports.default = Watch;
|
|
823
823
|
|
|
824
824
|
if (exports.default) {
|
|
825
825
|
module.exports = exports.default;
|