@e-mc/file-manager 0.13.4 → 0.13.6

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.
Files changed (3) hide show
  1. package/README.md +11 -10
  2. package/index.js +95 -94
  3. package/package.json +10 -10
package/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- * [View Source](https://www.unpkg.com/@e-mc/types@0.13.4/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.13.6/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { ChecksumValue, DataSource, IncrementalMatch, TaskAction } from "./squared";
@@ -175,6 +175,7 @@ interface FileManagerConstructor extends HostConstructor {
175
175
  setTimeout(options: Record<string, number | string>): void;
176
176
  defineHttpCache(options: HttpMemorySettings, disk?: boolean): void;
177
177
  defineHttpConnect(options: HttpConnectSettings): void;
178
+ generateSessionId(): string;
178
179
  readonly prototype: IFileManager;
179
180
  new(baseDirectory: string, config: RequestData, postFinalize?: PostFinalizeCallback): IFileManager;
180
181
  new(baseDirectory: string, config: RequestData, permission?: IPermission | null, postFinalize?: PostFinalizeCallback): IFileManager;
@@ -296,15 +297,15 @@ NOTE: **FileManager** is a sub-class of [Host](https://www.npmjs.com/package/@e-
296
297
 
297
298
  ## References
298
299
 
299
- - https://www.unpkg.com/@e-mc/types@0.13.4/lib/squared.d.ts
300
- - https://www.unpkg.com/@e-mc/types@0.13.4/lib/asset.d.ts
301
- - https://www.unpkg.com/@e-mc/types@0.13.4/lib/core.d.ts
302
- - https://www.unpkg.com/@e-mc/types@0.13.4/lib/filemanager.d.ts
303
- - https://www.unpkg.com/@e-mc/types@0.13.4/lib/logger.d.ts
304
- - https://www.unpkg.com/@e-mc/types@0.13.4/lib/module.d.ts
305
- - https://www.unpkg.com/@e-mc/types@0.13.4/lib/node.d.ts
306
- - https://www.unpkg.com/@e-mc/types@0.13.4/lib/request.d.ts
307
- - https://www.unpkg.com/@e-mc/types@0.13.4/lib/settings.d.ts
300
+ - https://www.unpkg.com/@e-mc/types@0.13.6/lib/squared.d.ts
301
+ - https://www.unpkg.com/@e-mc/types@0.13.6/lib/asset.d.ts
302
+ - https://www.unpkg.com/@e-mc/types@0.13.6/lib/core.d.ts
303
+ - https://www.unpkg.com/@e-mc/types@0.13.6/lib/filemanager.d.ts
304
+ - https://www.unpkg.com/@e-mc/types@0.13.6/lib/logger.d.ts
305
+ - https://www.unpkg.com/@e-mc/types@0.13.6/lib/module.d.ts
306
+ - https://www.unpkg.com/@e-mc/types@0.13.6/lib/node.d.ts
307
+ - https://www.unpkg.com/@e-mc/types@0.13.6/lib/request.d.ts
308
+ - https://www.unpkg.com/@e-mc/types@0.13.6/lib/settings.d.ts
308
309
 
309
310
  * https://www.npmjs.com/package/@types/node
310
311
 
package/index.js CHANGED
@@ -70,7 +70,7 @@ let SESSION_ID = 0;
70
70
  let SESSION_LIMIT = 1000;
71
71
  let ASSET_ID = 0;
72
72
  let RETRY_LIMIT = 10;
73
- let PROCESS_SUB_LIMIT = Math.max(require('node:os').cpus().length, 1) * 2;
73
+ let PROCESS_SUB_LIMIT = core_1.Host.availableParallelism(2);
74
74
  let LOG_TIMEELAPSED = true;
75
75
  let LOG_TIMEPROCESS = true;
76
76
  const HTTP_CLIENT = {
@@ -109,14 +109,6 @@ function withinSizeRange(uri, value, pattern) {
109
109
  }
110
110
  return true;
111
111
  }
112
- function unsetContent(item) {
113
- if ('buffer' in item) {
114
- item.buffer = null;
115
- }
116
- if ('sourceUTF8' in item) {
117
- item.sourceUTF8 = undefined;
118
- }
119
- }
120
112
  function bundleTorrent(host, files, mimeType, encoding) {
121
113
  let output = '';
122
114
  for (const file of files) {
@@ -238,40 +230,6 @@ function formatMinutes(elapsed) {
238
230
  }
239
231
  return (0, types_1.formatTime)(elapsed, ':');
240
232
  }
241
- function setHttpCacheLimit(value, type, disk) {
242
- if (value === undefined) {
243
- return;
244
- }
245
- if (typeof value === 'string') {
246
- value = (0, types_1.formatSize)(value);
247
- }
248
- if (value >= 0) {
249
- switch (type) {
250
- case 1:
251
- (disk ? DISK : MEMORY).LIMIT = value;
252
- break;
253
- case 2:
254
- MEMORY.LIMIT_ALL = value;
255
- break;
256
- case 3:
257
- MEMORY.DISK_MIN = value;
258
- break;
259
- case 4:
260
- MEMORY.DISK_MAX = value;
261
- break;
262
- }
263
- }
264
- else if (value === -1) {
265
- switch (type) {
266
- case 3:
267
- MEMORY.DISK_MIN = Infinity;
268
- break;
269
- case 4:
270
- MEMORY.DISK_MAX = 0;
271
- break;
272
- }
273
- }
274
- }
275
233
  function clearQueue(data, attr) {
276
234
  if (attr && data[attr]) {
277
235
  for (const item of data[attr]) {
@@ -323,7 +281,7 @@ function processHeaders(item, headers, lastModified, mainEtag) {
323
281
  return item.lastModified = headers['last-modified'];
324
282
  }
325
283
  }
326
- function setBufferTarget(item, buffer) {
284
+ function setContent(item, buffer) {
327
285
  if (typeof buffer === 'string') {
328
286
  item.sourceUTF8 = buffer;
329
287
  }
@@ -331,6 +289,14 @@ function setBufferTarget(item, buffer) {
331
289
  item.buffer = buffer;
332
290
  }
333
291
  }
292
+ function unsetContent(item) {
293
+ if ('buffer' in item) {
294
+ item.buffer = null;
295
+ }
296
+ if ('sourceUTF8' in item) {
297
+ item.sourceUTF8 = undefined;
298
+ }
299
+ }
334
300
  async function doVerifyChecksum(root, from, options, nested) {
335
301
  const { digestEncoding = options.digest, sortBy = 0, recursive = false, ignore = [], ignoreRoot = [], verbose = true, joinRoot } = options;
336
302
  const parent = recursive === 1 && typeof verbose !== 'number';
@@ -472,6 +438,40 @@ function formatSegment(endTime, ...values) {
472
438
  }
473
439
  return values;
474
440
  }
441
+ function setHttpCache(value, type, disk) {
442
+ if (value === undefined) {
443
+ return;
444
+ }
445
+ if (typeof value === 'string') {
446
+ value = (0, types_1.formatSize)(value);
447
+ }
448
+ if (value >= 0) {
449
+ switch (type) {
450
+ case 1:
451
+ (disk ? DISK : MEMORY).LIMIT = value;
452
+ break;
453
+ case 2:
454
+ MEMORY.LIMIT_ALL = value;
455
+ break;
456
+ case 3:
457
+ MEMORY.DISK_MIN = value;
458
+ break;
459
+ case 4:
460
+ MEMORY.DISK_MAX = value;
461
+ break;
462
+ }
463
+ }
464
+ else if (value === -1) {
465
+ switch (type) {
466
+ case 3:
467
+ MEMORY.DISK_MIN = Infinity;
468
+ break;
469
+ case 4:
470
+ MEMORY.DISK_MAX = 0;
471
+ break;
472
+ }
473
+ }
474
+ }
475
475
  function setLogMinWidth() {
476
476
  LOGGER.MIN_WIDTH = LOGGER.TITLE_WIDTH + 2 + LOGGER.VALUE_WIDTH + 1;
477
477
  LOGGER.MESSAGE_WIDTH = LOGGER.MIN_WIDTH + LOGGER.MESSAGE_SEP_START;
@@ -488,7 +488,7 @@ const matchPathname = (value) => core_1.Host.PLATFORM_WIN32 ? value.toLowerCase(
488
488
  const mapPosix = (values) => core_1.Host.PLATFORM_WIN32 ? values.map(value => core_1.Permission.toPosix(value).toLowerCase()) : values;
489
489
  const formatPercent = (value) => Math.round(value).toString().padStart(3) + '%';
490
490
  const formatLength = (value, length) => `${length} ${value + (length === 1 ? '' : 's')}`;
491
- const checkEOF = (value) => /\n$/.test(value) ? value : value + '\n';
491
+ const checkEOF = (value) => value.endsWith('\n') ? value : value + '\n';
492
492
  const checksumFile = (algorithm) => "checksum" + '.' + ((0, types_1.isString)(algorithm) ? algorithm.toLowerCase() : "sha256");
493
493
  const ignoreAsset = (item, exists) => item.invalid || (0, types_1.hasBit)(item.flags, 1 | (!exists ? 128 : 0));
494
494
  const validateChecksum = (host, item, buffer, localUri = item.localUri) => !item.checksum || host.checkHash(item.checksum, buffer, localUri);
@@ -1403,7 +1403,7 @@ class FileManager extends core_1.Host {
1403
1403
  const [L, R] = message.braces;
1404
1404
  const Lch = L.trim();
1405
1405
  const Rch = R.trim();
1406
- LOGGER.MESSAGE_SEP = Lch.length + Rch.length === 1 ? ` ${Rch + Lch} ` : (R ? R[0] === ' ' ? R : ' ' + R.trimEnd() : '') + (L ? L[L.length - 1] === ' ' ? L : L.trimStart() + ' ' : '');
1406
+ LOGGER.MESSAGE_SEP = Lch.length + Rch.length === 1 ? ` ${Rch + Lch} ` : (R ? R[0] === ' ' ? R : ' ' + R.trimEnd() : '') + (L ? L.at(-1) === ' ' ? L : L.trimStart() + ' ' : '');
1407
1407
  LOGGER.MESSAGE_SEP_START = Lch.trimEnd().length;
1408
1408
  LOG_TIMEELAPSED = this.hasLogType(128);
1409
1409
  LOG_TIMEPROCESS = this.hasLogType(256);
@@ -1521,18 +1521,18 @@ class FileManager extends core_1.Host {
1521
1521
  cache.EXPIRES = expires ? (0, types_1.parseExpires)(expires) : 0;
1522
1522
  cache.INCLUDE = (0, types_1.isArray)(include) ? include.slice(0) : [];
1523
1523
  cache.EXCLUDE = (0, types_1.isArray)(exclude) ? exclude.slice(0) : [];
1524
- setHttpCacheLimit(limit, 1, disk);
1524
+ setHttpCache(limit, 1, disk);
1525
1525
  if (disk) {
1526
1526
  return;
1527
1527
  }
1528
- setHttpCacheLimit(limit_all, 2, disk);
1528
+ setHttpCache(limit_all, 2, disk);
1529
1529
  if (to_disk) {
1530
1530
  let limit_disk;
1531
1531
  if (Array.isArray(to_disk)) {
1532
1532
  [to_disk, limit_disk] = to_disk;
1533
1533
  }
1534
- setHttpCacheLimit(to_disk, 3, disk);
1535
- setHttpCacheLimit(limit_disk, 4, disk);
1534
+ setHttpCache(to_disk, 3, disk);
1535
+ setHttpCache(limit_disk, 4, disk);
1536
1536
  }
1537
1537
  if ((purge_amount = (0, util_2.asFloat)(purge_amount)) > 0) {
1538
1538
  MEMORY.PURGE = Math.min(purge_amount, 1);
@@ -1555,6 +1555,9 @@ class FileManager extends core_1.Host {
1555
1555
  HTTP_CLIENT.redirectLimit = redirect_limit;
1556
1556
  }
1557
1557
  }
1558
+ static generateSessionId() {
1559
+ return (++SESSION_ID === SESSION_LIMIT ? SESSION_ID = 1 : SESSION_ID).toString();
1560
+ }
1558
1561
  processTimeout = PROCESS_TIMEOUT.filemanager;
1559
1562
  Request;
1560
1563
  Document = [];
@@ -1604,15 +1607,15 @@ class FileManager extends core_1.Host {
1604
1607
  if (LOG_TIMEPROCESS) {
1605
1608
  core_1.Host.initCpuUsage(this);
1606
1609
  }
1607
- const index = baseDirectory.length - 1;
1608
- this.#baseDirectory = path.normalize(baseDirectory[index] === path.sep ? baseDirectory.substring(0, index) : baseDirectory);
1610
+ this.#baseDirectory = path.normalize(baseDirectory.at(-1) === path.sep ? baseDirectory.slice(0, -1) : baseDirectory);
1609
1611
  this.permission = permission && core_1.Host.isPermission(permission) ? permission : core_1.Host.getPermissionFromSettings();
1610
- this.sessionId = (++SESSION_ID === SESSION_LIMIT ? SESSION_ID = 1 : SESSION_ID).toString();
1612
+ this.sessionId = FileManager.generateSessionId();
1613
+ this.supports('permission', false, true);
1614
+ this.supports('sessionId', false, true);
1611
1615
  const assets = config.assets || [];
1612
1616
  const targeted = [];
1613
1617
  for (let i = 0, length = assets.length; i < length; ++i) {
1614
1618
  const item = assets[i];
1615
- let encoding = item.encoding;
1616
1619
  if (item.document) {
1617
1620
  this.documentAssets.push(item);
1618
1621
  }
@@ -1622,20 +1625,20 @@ class FileManager extends core_1.Host {
1622
1625
  if ((0, types_1.usingFlag)(item)) {
1623
1626
  targeted.push(item);
1624
1627
  }
1625
- item.id ||= ++ASSET_ID;
1626
- if (encoding) {
1627
- switch (encoding) {
1628
- case 'utf8':
1629
- break;
1630
- case 'utf-8':
1631
- item.encoding = 'utf8';
1632
- break;
1633
- default:
1634
- encoding = (0, types_1.getEncoding)(encoding);
1635
- item.encoding = encoding === 'utf8' ? undefined : encoding;
1636
- break;
1628
+ switch (item.encoding) {
1629
+ case undefined:
1630
+ case 'utf8':
1631
+ break;
1632
+ case 'utf-8':
1633
+ item.encoding = 'utf8';
1634
+ break;
1635
+ default: {
1636
+ const encoding = (0, types_1.getEncoding)(item.encoding);
1637
+ item.encoding = encoding === 'utf8' ? undefined : encoding;
1638
+ break;
1637
1639
  }
1638
1640
  }
1641
+ item.id ||= ++ASSET_ID;
1639
1642
  }
1640
1643
  this._assets = assets.slice(0);
1641
1644
  this.#incremental = config.incremental === "staging" ? "staging" : "none";
@@ -2714,11 +2717,11 @@ class FileManager extends core_1.Host {
2714
2717
  return mimeType;
2715
2718
  }
2716
2719
  async compressFile(file, overwrite = true) {
2717
- const { localUri, compress } = file;
2718
- const instance = this.Compress;
2719
- if (instance && compress && localUri && (this.has(localUri) || (0, types_1.existsFlag)(file))) {
2720
+ const compress = this.Compress;
2721
+ const localUri = file.localUri;
2722
+ if (compress && localUri && (this.has(localUri) || (0, types_1.existsFlag)(file)) && file.compress) {
2720
2723
  const tasks = [];
2721
- for (const options of compress) {
2724
+ for (const options of file.compress) {
2722
2725
  const { format, condition } = options;
2723
2726
  let output;
2724
2727
  switch (format) {
@@ -2734,7 +2737,7 @@ class FileManager extends core_1.Host {
2734
2737
  options.etag = file.etag;
2735
2738
  break;
2736
2739
  default:
2737
- if (format && typeof instance.compressors[format] === 'function') {
2740
+ if (format && typeof compress.compressors[format] === 'function') {
2738
2741
  output = (0, types_1.renameExt)(localUri, format);
2739
2742
  }
2740
2743
  break;
@@ -2743,7 +2746,7 @@ class FileManager extends core_1.Host {
2743
2746
  try {
2744
2747
  if (overwrite || !fs.existsSync(output) || !(0, types_1.existsFlag)(file) && fs.statSync(output).mtimeMs < this.startTime) {
2745
2748
  options.timeout = this.#getModuleTimeout('compress');
2746
- tasks.push(instance.tryFile(file.buffer || localUri, output, options).then(() => {
2749
+ tasks.push(compress.tryFile(file.buffer || localUri, output, options).then(() => {
2747
2750
  const outFile = options.outFile;
2748
2751
  if (outFile) {
2749
2752
  if (condition?.includes('%') && (0, util_2.getSize)(outFile) >= (0, util_2.getSize)(localUri)) {
@@ -3586,7 +3589,7 @@ class FileManager extends core_1.Host {
3586
3589
  }
3587
3590
  }
3588
3591
  if (item.willChange && buffer) {
3589
- setBufferTarget(item, buffer);
3592
+ setContent(item, buffer);
3590
3593
  }
3591
3594
  target.received(null, incremental, true, false, true);
3592
3595
  }
@@ -3597,7 +3600,7 @@ class FileManager extends core_1.Host {
3597
3600
  return;
3598
3601
  }
3599
3602
  if (buffer) {
3600
- setBufferTarget(item, buffer);
3603
+ setContent(item, buffer);
3601
3604
  fs.writeFileSync(localUri, buffer);
3602
3605
  this.addDownload(Buffer.byteLength(buffer, encoding), types_1.DOWNLOAD_TYPE.CACHE);
3603
3606
  if (checkEtag) {
@@ -4072,7 +4075,7 @@ class FileManager extends core_1.Host {
4072
4075
  }
4073
4076
  async finalizeCompress(assets) {
4074
4077
  const compress = this.Compress;
4075
- if (!(assets.length > 0 && compress)) {
4078
+ if (!compress || assets.length === 0) {
4076
4079
  return;
4077
4080
  }
4078
4081
  const tasks = [];
@@ -4343,28 +4346,26 @@ class FileManager extends core_1.Host {
4343
4346
  if (core_1.Host.isPath(pathname, true)) {
4344
4347
  const [content, encoding] = diffSource[pathname];
4345
4348
  const hunks = diff.structuredPatch('', '', checkEOF(content), checkEOF(fs.readFileSync(pathname, encoding)), '', '', { context: 0 }).hunks;
4346
- const length = hunks.length;
4347
- if (length === 0) {
4348
- continue;
4349
- }
4350
- const lastHunk = hunks[length - 1];
4351
- const oldMax = lastHunk.oldStart + lastHunk.oldLines;
4352
- const newMax = lastHunk.newStart + lastHunk.newLines;
4353
- const posLength = Math.max(Math.max(oldMax, newMax).toString().length, Math.min(oldMax, newMax).toString().length);
4354
- addHeader(pathname, formatLength('hunk', length));
4355
- for (const { lines, oldLines, oldStart, newStart, newLines } of hunks) {
4356
- for (let i = 0, j = oldStart, k = oldLines, l = 0, q = lines.length, added = false; i < q && k > 0; ++i) {
4357
- const line = lines[i].substring(1);
4358
- output.push(` ${chalk[!line && k === 1 ? 'grey' : added ? 'green' : 'red']((j + l++).toString().padStart(posLength))} ${chalk[added ? 'green' : 'red'](line)}`);
4359
- if (--k === 0 && !added) {
4360
- j = newStart;
4361
- k = newLines;
4362
- l = 0;
4363
- added = true;
4349
+ const lastHunk = hunks.at(-1);
4350
+ if (lastHunk) {
4351
+ const oldMax = lastHunk.oldStart + lastHunk.oldLines;
4352
+ const newMax = lastHunk.newStart + lastHunk.newLines;
4353
+ const posLength = Math.max(Math.max(oldMax, newMax).toString().length, Math.min(oldMax, newMax).toString().length);
4354
+ addHeader(pathname, formatLength('hunk', hunks.length));
4355
+ for (const { lines, oldLines, oldStart, newStart, newLines } of hunks) {
4356
+ for (let i = 0, j = oldStart, k = oldLines, l = 0, q = lines.length, added = false; i < q && k > 0; ++i) {
4357
+ const line = lines[i].substring(1);
4358
+ output.push(` ${chalk[!line && k === 1 ? 'grey' : added ? 'green' : 'red']((j + l++).toString().padStart(posLength))} ${chalk[added ? 'green' : 'red'](line)}`);
4359
+ if (--k === 0 && !added) {
4360
+ j = newStart;
4361
+ k = newLines;
4362
+ l = 0;
4363
+ added = true;
4364
+ }
4364
4365
  }
4365
4366
  }
4367
+ divider = true;
4366
4368
  }
4367
- divider = true;
4368
4369
  }
4369
4370
  else {
4370
4371
  addHeader(pathname, 'deleted');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/file-manager",
3
- "version": "0.13.4",
3
+ "version": "0.13.6",
4
4
  "description": "FileManager constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -19,15 +19,15 @@
19
19
  "license": "BSD-3-Clause",
20
20
  "homepage": "https://github.com/anpham6/e-mc#readme",
21
21
  "dependencies": {
22
- "@e-mc/cloud": "0.13.4",
23
- "@e-mc/compress": "0.13.4",
24
- "@e-mc/core": "0.13.4",
25
- "@e-mc/document": "0.13.4",
26
- "@e-mc/image": "0.13.4",
27
- "@e-mc/request": "0.13.4",
28
- "@e-mc/task": "0.13.4",
29
- "@e-mc/types": "0.13.4",
30
- "@e-mc/watch": "0.13.4",
22
+ "@e-mc/cloud": "0.13.6",
23
+ "@e-mc/compress": "0.13.6",
24
+ "@e-mc/core": "0.13.6",
25
+ "@e-mc/document": "0.13.6",
26
+ "@e-mc/image": "0.13.6",
27
+ "@e-mc/request": "0.13.6",
28
+ "@e-mc/task": "0.13.6",
29
+ "@e-mc/types": "0.13.6",
30
+ "@e-mc/watch": "0.13.6",
31
31
  "chalk": "4.1.2",
32
32
  "diff": "^8.0.2",
33
33
  "picomatch": "^4.0.3"