@e-mc/watch 0.0.1

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