@e-mc/file-manager 0.8.1 → 0.8.3

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 +2 -0
  2. package/index.js +397 -375
  3. package/package.json +11 -11
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  ### @e-mc/file-manager
2
2
 
3
+ https://e-mc.readthedocs.io
4
+
3
5
  ### LICENSE
4
6
 
5
7
  BSD 3-Clause
package/index.js CHANGED
@@ -155,26 +155,34 @@ function recurseDir(output, subDirs, options) {
155
155
  }
156
156
  return output;
157
157
  }
158
- function checkHash(data, fromBuffer, options) {
159
- if (options) {
160
- let algorithm, digest, value;
161
- if ((0, types_1.isObject)(options)) {
162
- ({ algorithm, digest, value } = options);
163
- }
164
- else {
165
- value = options;
166
- }
167
- if ((0, types_1.isString)(value)) {
168
- if (!fromBuffer) {
169
- try {
170
- data = fs.readFileSync(data);
171
- }
172
- catch {
173
- return false;
158
+ function checkHash(localUri, output, options, data) {
159
+ let algorithm, digest, value;
160
+ if ((0, types_1.isObject)(options)) {
161
+ ({ algorithm, digest, value } = options);
162
+ }
163
+ else {
164
+ value = options;
165
+ }
166
+ if ((0, types_1.isString)(value)) {
167
+ algorithm || (algorithm = "sha256" /* HASH_OUTPUT.ALGORITHM */);
168
+ if (!data) {
169
+ try {
170
+ data = fs.readFileSync(localUri);
171
+ }
172
+ catch (err) {
173
+ if (output && err instanceof Error && err.code === 'ENOENT') {
174
+ this.addLog(this.statusType.WARN, algorithm + ' -> ENOENT -> No checksum performed', path.basename(localUri), value);
175
+ return true;
174
176
  }
177
+ this.addLog(this.statusType.WARN, err, path.basename(localUri), value);
178
+ return false;
175
179
  }
176
- return value.toLowerCase() === core_1.Host.asHash(data, { algorithm, digest });
177
180
  }
181
+ if ((value = value.toLowerCase()) === core_1.Host.asHash(data, { algorithm, digest })) {
182
+ this.formatMessage(32 /* LOG_TYPE.FILE */, algorithm, ["Checksum matched" /* VAL_MESSAGE.CHECKSUM */ + (output ? ' (output)' : ''), path.basename(localUri)], value, { ...core_1.Host.LOG_STYLE_INFO, queue: true });
183
+ return true;
184
+ }
185
+ return false;
178
186
  }
179
187
  return true;
180
188
  }
@@ -936,19 +944,8 @@ class FileManager extends core_1.Host {
936
944
  if (item.tasks) {
937
945
  this.taskAssets.push(item);
938
946
  }
939
- if (encoding) {
940
- switch (encoding) {
941
- case 'utf-8':
942
- break;
943
- case 'utf-16':
944
- case 'utf-16be':
945
- case 'utf-16le':
946
- item.encoding = 'utf16le';
947
- break;
948
- default:
949
- item.encoding = (0, types_1.getEncoding)(encoding);
950
- break;
951
- }
947
+ if (encoding && encoding !== 'utf8' && encoding !== 'utf-8') {
948
+ item.encoding = encoding === 'utf-16be' ? 'utf16le' : (0, types_1.getEncoding)(encoding);
952
949
  }
953
950
  if ((0, types_1.usingFlag)(item.flags)) {
954
951
  (targeted || (targeted = [])).push(item);
@@ -956,7 +953,7 @@ class FileManager extends core_1.Host {
956
953
  item.id || (item.id = ++ASSET_ID);
957
954
  }
958
955
  this._assets = assets.slice(0);
959
- this[kIncremental] = incremental === 'staging' ? 'staging' : 'none';
956
+ this[kIncremental] = incremental === "staging" /* INCREMENTAL.STAGING */ ? "staging" /* INCREMENTAL.STAGING */ : "none" /* INCREMENTAL.NONE */;
960
957
  if (targeted) {
961
958
  this.using(...targeted);
962
959
  }
@@ -1276,7 +1273,7 @@ class FileManager extends core_1.Host {
1276
1273
  }
1277
1274
  }
1278
1275
  }
1279
- const manager = new FileManager(this.baseDirectory, { ...this.config, incremental: 'none', assets }, postFinalize);
1276
+ const manager = new FileManager(this.baseDirectory, { ...this.config, incremental: "none" /* INCREMENTAL.NONE */, assets }, postFinalize);
1280
1277
  for (const { constructor, params } of this.Document) {
1281
1278
  manager.install('document', constructor, ...params);
1282
1279
  }
@@ -1628,21 +1625,19 @@ class FileManager extends core_1.Host {
1628
1625
  return document === moduleName || Array.isArray(document) && document.includes(moduleName);
1629
1626
  }
1630
1627
  setLocalUri(file, replace) {
1631
- let uri = file.uri, torrent;
1632
- if (uri && !file.content && !file.base64 && !file.url) {
1628
+ let uri = file.uri, type;
1629
+ if (uri && !file.url && !file.content && !file.base64 && !file.dataView) {
1633
1630
  if (core_1.Host.isFile(uri, 'torrent')) {
1634
- torrent = true;
1631
+ type = 4 /* FETCH_TYPE.TORRENT */;
1635
1632
  }
1636
1633
  else if (core_1.Host.isURL(uri, 'file')) {
1637
- if (!core_1.Host.isFile(uri, 's/ftp')) {
1638
- try {
1639
- file.url = new URL(uri);
1640
- file.uri = file.url.toString();
1641
- }
1642
- catch (err) {
1643
- this.writeFail(['Unable to parse URL', uri], err, 1024 /* LOG_TYPE.HTTP */);
1644
- return { pathname: '', localUri: '' };
1645
- }
1634
+ try {
1635
+ file.url = new URL(uri);
1636
+ file.uri = file.url.toString();
1637
+ }
1638
+ catch (err) {
1639
+ this.writeFail(['Unable to parse URL', uri], err, 1024 /* LOG_TYPE.HTTP */);
1640
+ return { pathname: '', localUri: '' };
1646
1641
  }
1647
1642
  }
1648
1643
  else {
@@ -1670,7 +1665,6 @@ class FileManager extends core_1.Host {
1670
1665
  }
1671
1666
  }
1672
1667
  const url = file.url;
1673
- let type;
1674
1668
  if (url) {
1675
1669
  if (url.protocol.startsWith('http')) {
1676
1670
  type = 1 /* FETCH_TYPE.HTTP */;
@@ -1678,12 +1672,7 @@ class FileManager extends core_1.Host {
1678
1672
  else if (file.socketPath && url.protocol === 'file:') {
1679
1673
  type = 2 /* FETCH_TYPE.UNIX_SOCKET */;
1680
1674
  }
1681
- }
1682
- if (!type && uri) {
1683
- if (core_1.Host.isFile(uri, 'torrent')) {
1684
- type = 4 /* FETCH_TYPE.TORRENT */;
1685
- }
1686
- else if (core_1.Host.isFile(uri, 's/ftp')) {
1675
+ else if (core_1.Host.isFile(url, 's/ftp')) {
1687
1676
  type = 3 /* FETCH_TYPE.FTP */;
1688
1677
  }
1689
1678
  }
@@ -1705,7 +1694,7 @@ class FileManager extends core_1.Host {
1705
1694
  const pathname = segments.length ? path.join(this.baseDirectory, ...segments) : this.baseDirectory;
1706
1695
  const localUri = file.filename ? path.join(pathname, file.filename) : pathname;
1707
1696
  file.localUri = localUri;
1708
- if (!torrent) {
1697
+ if (type !== 4 /* FETCH_TYPE.TORRENT */) {
1709
1698
  file.mimeType || (file.mimeType = file.url && core_1.Host.lookupMime(path.basename(file.url.pathname)) || core_1.Host.lookupMime(file.filename));
1710
1699
  }
1711
1700
  return { pathname, localUri };
@@ -1815,42 +1804,36 @@ class FileManager extends core_1.Host {
1815
1804
  }
1816
1805
  getAssetContent({ localUri, document, mimeType }, content) {
1817
1806
  const appending = this.contentToAppend.get(localUri);
1818
- if (appending) {
1819
- let newline;
1820
- const getSeparator = (value) => newline || (newline = (0, util_2.getNewline)(value));
1821
- if (content) {
1822
- const replacing = this.contentToReplace.get(localUri);
1823
- if (replacing?.length) {
1824
- for (let i = 0; i < replacing.length; ++i) {
1825
- let value = appending[i];
1826
- if ((0, types_1.isString)(value)) {
1827
- if (replacing[i]) {
1828
- const match = new RegExp(replacing[i], 'i').exec(content);
1829
- if (match) {
1830
- if (document) {
1831
- for (const { instance } of this.Document) {
1832
- if (instance.replaceContent && this.hasDocument(instance, document)) {
1833
- const output = instance.replaceContent(value, match, mimeType);
1834
- if (output) {
1835
- value = output;
1836
- break;
1837
- }
1838
- }
1839
- }
1807
+ if (!appending) {
1808
+ return content;
1809
+ }
1810
+ let replacing, newline;
1811
+ const getSeparator = (value) => newline || (newline = (0, util_2.getNewline)(value));
1812
+ if (content && (replacing = this.contentToReplace.get(localUri)) && replacing.length) {
1813
+ for (let i = 0, value, match; i < replacing.length; ++i) {
1814
+ if ((0, types_1.isString)(value = appending[i])) {
1815
+ if (replacing[i] && (match = new RegExp(replacing[i], 'i').exec(content))) {
1816
+ if (document) {
1817
+ for (const { instance } of this.Document) {
1818
+ if (instance.replaceContent && this.hasDocument(instance, document)) {
1819
+ const output = instance.replaceContent(value, match, mimeType);
1820
+ if (output) {
1821
+ value = output;
1822
+ break;
1840
1823
  }
1841
- content = content.substring(0, match.index) + value + getSeparator(content) + content.substring(match.index + match[0].length);
1842
- continue;
1843
1824
  }
1844
1825
  }
1845
- content += getSeparator(content) + value;
1846
1826
  }
1827
+ content = content.substring(0, match.index) + value + getSeparator(content) + content.substring(match.index + match[0].length);
1828
+ }
1829
+ else {
1830
+ content += getSeparator(content) + value;
1847
1831
  }
1848
- return content;
1849
1832
  }
1850
1833
  }
1851
- return (content || '') + appending.reduce((a, b) => b ? a + getSeparator(b) + b : a, '');
1834
+ return content;
1852
1835
  }
1853
- return content;
1836
+ return (content || '') + appending.reduce((a, b) => b ? a + getSeparator(b) + b : a, '');
1854
1837
  }
1855
1838
  writeBuffer(file, options) {
1856
1839
  const buffer = file.sourceUTF8 ? Buffer.from(file.sourceUTF8, file.encoding || (file.encoding = 'utf-8')) : file.buffer;
@@ -2232,15 +2215,15 @@ class FileManager extends core_1.Host {
2232
2215
  getDownload(type = 0) {
2233
2216
  return this[kDownloadStats][type];
2234
2217
  }
2235
- async fetchObject(uri, options) {
2236
- if (!(0, types_1.isObject)(options)) {
2218
+ async fetchObject(uri, options = {}) {
2219
+ if (typeof options === 'string') {
2237
2220
  options = { format: options };
2238
2221
  }
2239
2222
  options.format || (options.format = 'json');
2240
- return this.fetchBuffer(uri, options).then(data => typeof data === 'object' ? data : null).catch(() => null);
2223
+ return this.Request.get(uri, options).then(data => typeof data === 'object' ? data : null).catch(() => null);
2241
2224
  }
2242
2225
  async fetchBuffer(uri, options) {
2243
- if (options) {
2226
+ if (options && 'format' in options) {
2244
2227
  options.format = undefined;
2245
2228
  }
2246
2229
  return this.Request.get(uri, options).then(buffer => {
@@ -2257,8 +2240,8 @@ class FileManager extends core_1.Host {
2257
2240
  return buffer;
2258
2241
  });
2259
2242
  }
2260
- async fetchFiles(uri, options) {
2261
- if (!(0, types_1.isObject)(options)) {
2243
+ async fetchFiles(uri, options = {}) {
2244
+ if (typeof options === 'string') {
2262
2245
  options = { pathname: options };
2263
2246
  }
2264
2247
  options.pathname || (options.pathname = this.baseDirectory);
@@ -2414,7 +2397,6 @@ class FileManager extends core_1.Host {
2414
2397
  this.performFinalize();
2415
2398
  }
2416
2399
  }, types_1.THRESHOLD.FILEMANAGER_INTERVAL);
2417
- const hasIncremental = (value) => value === 'etag' || value === 'exists';
2418
2400
  const { assets, cacheToDisk, cacheToMemory, fetchedAssets, Watch: watch } = this;
2419
2401
  const downloadable = new Map();
2420
2402
  const completed = Object.create(null);
@@ -2424,8 +2406,10 @@ class FileManager extends core_1.Host {
2424
2406
  const bundling = Object.create(null);
2425
2407
  const originCount = Object.create(null);
2426
2408
  const emptied = [this.baseDirectory];
2427
- const staging = this[kIncremental] === 'staging';
2409
+ const staging = this[kIncremental] === "staging" /* INCREMENTAL.STAGING */;
2428
2410
  const incremental = this.config.incremental;
2411
+ const isCacheable = (file) => file.initialValue?.cacheable !== false;
2412
+ const hasIncremental = (value) => value === "etag" /* INCREMENTAL.ETAG */ || value === "exists" /* INCREMENTAL.EXISTS */;
2429
2413
  let cacheable = false, cacheOpen = false, cacheEtag = false;
2430
2414
  if (!staging) {
2431
2415
  cacheable = this[kRecursionLimit] === RECURSION_LIMIT;
@@ -2433,25 +2417,24 @@ class FileManager extends core_1.Host {
2433
2417
  if (cacheOpen = hasIncremental(incremental)) {
2434
2418
  this[kIncremental] = incremental;
2435
2419
  }
2436
- cacheEtag = incremental === 'etag';
2420
+ cacheEtag = incremental === "etag" /* INCREMENTAL.ETAG */;
2437
2421
  }
2438
2422
  else {
2439
- this[kIncremental] = 'none';
2423
+ this[kIncremental] = "none" /* INCREMENTAL.NONE */;
2440
2424
  }
2441
2425
  }
2442
2426
  const targeting = this._usingObjects.size > 0;
2443
- const isCacheable = (file) => file.initialValue?.cacheable !== false;
2444
2427
  const hasEtag = (file) => {
2445
2428
  if (!cacheable || !(0, types_1.existsFlag)(file.flags)) {
2446
2429
  return false;
2447
2430
  }
2448
2431
  switch (file.incremental) {
2449
2432
  case false:
2450
- case 'none':
2451
- case 'exists':
2452
- case 'staging':
2433
+ case "none" /* INCREMENTAL.NONE */:
2434
+ case "exists" /* INCREMENTAL.EXISTS */:
2435
+ case "staging" /* INCREMENTAL.STAGING */:
2453
2436
  return false;
2454
- case 'etag':
2437
+ case "etag" /* INCREMENTAL.ETAG */:
2455
2438
  return true;
2456
2439
  default:
2457
2440
  return cacheEtag;
@@ -2562,7 +2545,7 @@ class FileManager extends core_1.Host {
2562
2545
  };
2563
2546
  const processQueue = async (file, localUri) => {
2564
2547
  completed[localUri] = file;
2565
- if (!(0, types_1.isEmpty)(file.bundleId) && file.bundleIndex === 0) {
2548
+ if (file.bundleIndex === 0 && !(0, types_1.isEmpty)(file.bundleId)) {
2566
2549
  this.setAssetContent(file, this.getUTF8String(file, localUri));
2567
2550
  const bundleQueue = file.bundleQueue;
2568
2551
  let success = true, error, checkEtag = !file.trailingContent && hasEtag(file);
@@ -2585,13 +2568,18 @@ class FileManager extends core_1.Host {
2585
2568
  if (checkEtag && queue.trailingContent) {
2586
2569
  checkEtag = false;
2587
2570
  }
2588
- const verifyBundle = (value, etag) => {
2571
+ const checksumValid = (buffer) => !queue.checksum || checkHash.call(this, queue.localUri || localUri, false, queue.checksum, buffer);
2572
+ const verifyBundle = (value, etag, checked) => {
2589
2573
  if (!queue.invalid) {
2574
+ if (!checked && !checksumValid(value)) {
2575
+ queue.invalid = true;
2576
+ return;
2577
+ }
2590
2578
  if (value instanceof Buffer) {
2591
2579
  value = value.toString(encoding);
2592
2580
  }
2593
2581
  const url = queue.url;
2594
- if (etag && cacheToMemory.has(url)) {
2582
+ if (etag && url && cacheToMemory.has(url)) {
2595
2583
  cacheToMemory.add(url, encodeURIComponent(etag), value, {
2596
2584
  encoding,
2597
2585
  contentLength: queue.contentLength,
@@ -2618,7 +2606,7 @@ class FileManager extends core_1.Host {
2618
2606
  options.socketPath = queue.socketPath;
2619
2607
  options.httpVersion = 1;
2620
2608
  }
2621
- else {
2609
+ else if (url) {
2622
2610
  if (etag = queue.etag) {
2623
2611
  const valid = incremental !== false || file.incremental === true;
2624
2612
  const cached = valid && MEMORY.CACHE[uri];
@@ -2626,11 +2614,15 @@ class FileManager extends core_1.Host {
2626
2614
  if (cached) {
2627
2615
  if (etagDir === cached[5]) {
2628
2616
  const source = cached[1];
2629
- verifyBundle(typeof source === 'string' ? source : source.toString(encoding));
2630
- this.addDownload(Buffer.byteLength(source, encoding), types_1.DOWNLOAD_TYPE.CACHE);
2631
- continue;
2617
+ if (checksumValid(source)) {
2618
+ verifyBundle(typeof source === 'string' ? source : source.toString(encoding), '', true);
2619
+ this.addDownload(Buffer.byteLength(source, encoding), types_1.DOWNLOAD_TYPE.CACHE);
2620
+ continue;
2621
+ }
2622
+ }
2623
+ else {
2624
+ cacheToMemory.clear(uri);
2632
2625
  }
2633
- cacheToMemory.clear(uri);
2634
2626
  }
2635
2627
  if (cacheToDisk.has(url) && isCacheable(queue)) {
2636
2628
  const baseDir = path.join(this.getCacheDir(url), etagDir);
@@ -2638,9 +2630,11 @@ class FileManager extends core_1.Host {
2638
2630
  try {
2639
2631
  if (valid && (0, lib_v4_1.hasSize)(pipeTo)) {
2640
2632
  const buffer = fs.readFileSync(pipeTo, { encoding });
2641
- verifyBundle(buffer, etag);
2642
- this.addDownload(Buffer.byteLength(buffer, encoding), types_1.DOWNLOAD_TYPE.CACHE);
2643
- continue;
2633
+ if (checksumValid(buffer)) {
2634
+ verifyBundle(buffer, etag, true);
2635
+ this.addDownload(Buffer.byteLength(buffer, encoding), types_1.DOWNLOAD_TYPE.CACHE);
2636
+ continue;
2637
+ }
2644
2638
  }
2645
2639
  if (!fs.existsSync(baseDir)) {
2646
2640
  fs.mkdirSync(baseDir);
@@ -2661,7 +2655,7 @@ class FileManager extends core_1.Host {
2661
2655
  return true;
2662
2656
  };
2663
2657
  }
2664
- tasks.push(this.fetchBuffer(url, options)
2658
+ tasks.push(this.fetchBuffer(url || uri, options)
2665
2659
  .then(data => {
2666
2660
  if (data) {
2667
2661
  verifyBundle(data, etag);
@@ -2671,7 +2665,7 @@ class FileManager extends core_1.Host {
2671
2665
  }
2672
2666
  const downloaded = bundling[uri];
2673
2667
  if ((0, types_1.isArray)(downloaded)) {
2674
- if (data) {
2668
+ if (data && !queue.invalid) {
2675
2669
  if (typeof data === 'string') {
2676
2670
  queue.sourceUTF8 = data;
2677
2671
  }
@@ -2826,16 +2820,19 @@ class FileManager extends core_1.Host {
2826
2820
  }
2827
2821
  return true;
2828
2822
  };
2829
- const fileReceived = (item, localUri, err, fetched, binary) => {
2830
- if (item.checksum && !err && !checkHash(item.buffer || localUri, !!item.buffer, item.checksum)) {
2831
- err = (0, types_1.errorValue)("Checksum did not match" /* ERR_MESSAGE.FAILED_CHECKSUM */, item.uri);
2832
- this.filesToRemove.add(localUri);
2833
- }
2823
+ const fileReceived = (item, localUri, err, fetched, binary, checked) => {
2834
2824
  if (err) {
2835
2825
  item.invalid = true;
2836
2826
  this.completeAsyncTask(err, localUri);
2837
2827
  return;
2838
2828
  }
2829
+ if (item.checksum && !checked && !checkHash.call(this, localUri, false, item.checksum, item.buffer)) {
2830
+ item.invalid = true;
2831
+ this.filesToRemove.add(localUri);
2832
+ this.completeAsyncTask();
2833
+ this.writeFail(["Checksum did not match" /* ERR_MESSAGE.FAILED_CHECKSUM */, path.basename(localUri)], (0, types_1.errorValue)(item.uri || localUri, "Invalid checksum" /* ERR_MESSAGE.CHECKSUM */), { type: 32 /* LOG_TYPE.FILE */, queue: true });
2834
+ return;
2835
+ }
2839
2836
  if (fetched) {
2840
2837
  fetchedAssets.push(item);
2841
2838
  }
@@ -2876,7 +2873,7 @@ class FileManager extends core_1.Host {
2876
2873
  const type = item.incremental;
2877
2874
  let valid = cacheOpen;
2878
2875
  if (valid) {
2879
- if (type === false || type === 'none') {
2876
+ if (type === false || type === "none" /* INCREMENTAL.NONE */) {
2880
2877
  valid = false;
2881
2878
  }
2882
2879
  }
@@ -2889,9 +2886,9 @@ class FileManager extends core_1.Host {
2889
2886
  }
2890
2887
  }
2891
2888
  if (valid && filename) {
2892
- const etag = (cacheEtag && type !== 'exists' || type === 'etag') && !!uri;
2889
+ const etag = (cacheEtag && type !== "exists" /* INCREMENTAL.EXISTS */ || type === "etag" /* INCREMENTAL.ETAG */) && !!uri;
2893
2890
  if (etag && imported.some(file => file.imported.includes(uri))) {
2894
- item.incremental = 'none';
2891
+ item.incremental = "none" /* INCREMENTAL.NONE */;
2895
2892
  }
2896
2893
  else if (!(0, types_1.mainFlag)(item.flags)) {
2897
2894
  let cached;
@@ -2907,7 +2904,7 @@ class FileManager extends core_1.Host {
2907
2904
  if ((!etag || item.fetchType === 1 /* FETCH_TYPE.HTTP */ && ((0, types_1.isEmpty)(bundleId) || bundleIndex <= 0)) && (!watch || !item.watch || setBuffer(item))) {
2908
2905
  let childBundle, childDownload;
2909
2906
  if (!(0, types_1.isEmpty)(bundleId) && bundleIndex > 0) {
2910
- const target = assets.find(parent => parent.bundleId === bundleId && parent.bundleIndex === 0);
2907
+ const target = assets.find(parent => parent.bundleIndex === 0 && parent.bundleId === bundleId);
2911
2908
  if (target) {
2912
2909
  if ((0, types_1.existsFlag)(target.flags)) {
2913
2910
  setBuffer(item);
@@ -2940,10 +2937,10 @@ class FileManager extends core_1.Host {
2940
2937
  setBuffer(item);
2941
2938
  }
2942
2939
  const checksumOutput = item.checksumOutput;
2943
- if (core_1.Host.isPath(localUri) && (!checksumOutput || checkHash(localUri, false, checksumOutput))) {
2940
+ if (core_1.Host.isPath(localUri) && (!checksumOutput || checkHash.call(this, localUri, true, checksumOutput))) {
2944
2941
  item.flags |= 128 /* ASSET_FLAG.EXISTS */;
2945
2942
  if (!etag || item.fetchType !== 1 /* FETCH_TYPE.HTTP */ || checksumOutput) {
2946
- if (!(0, types_1.isEmpty)(bundleId) && bundleIndex === 0) {
2943
+ if (bundleIndex === 0 && !(0, types_1.isEmpty)(bundleId)) {
2947
2944
  assets.filter(child => child.bundleId === bundleId && child.bundleIndex > 0).forEach(child => {
2948
2945
  setBuffer(child);
2949
2946
  child.invalid = true;
@@ -2958,14 +2955,14 @@ class FileManager extends core_1.Host {
2958
2955
  else if (cached) {
2959
2956
  try {
2960
2957
  const buffer = cached[1];
2961
- if (!item.checksum || checkHash(buffer, true, item.checksum)) {
2958
+ if (!item.checksum || checkHash.call(this, localUri, false, item.checksum, buffer)) {
2962
2959
  fs.writeFileSync(localUri, buffer);
2963
2960
  if (item.willChange && Buffer.isBuffer(buffer)) {
2964
2961
  item.buffer = buffer;
2965
2962
  }
2966
- if (!(checksumOutput && (0, types_1.isEmpty)(bundleId) && checkHash(buffer, true, checksumOutput))) {
2963
+ if (!(checksumOutput && (0, types_1.isEmpty)(bundleId) && checkHash.call(this, localUri, true, checksumOutput, buffer))) {
2967
2964
  this.performAsyncTask();
2968
- fileReceived(item, localUri, null, true);
2965
+ fileReceived(item, localUri, null, true, false, true);
2969
2966
  }
2970
2967
  continue;
2971
2968
  }
@@ -2996,7 +2993,8 @@ class FileManager extends core_1.Host {
2996
2993
  if (downloadable.size > 1 && this.Request.httpVersion !== 1) {
2997
2994
  for (const { url } of downloadable.keys()) {
2998
2995
  if (url?.protocol === 'https:') {
2999
- originCount[url.origin] = (originCount[url.origin] || 0) + 1;
2996
+ const count = originCount[url.origin];
2997
+ originCount[url.origin] = count ? count + 1 : 1;
3000
2998
  }
3001
2999
  }
3002
3000
  }
@@ -3005,6 +3003,7 @@ class FileManager extends core_1.Host {
3005
3003
  break;
3006
3004
  }
3007
3005
  const { pathname, localUri } = download;
3006
+ const uri = item.uri;
3008
3007
  if (item.content) {
3009
3008
  if (!checkQueue(item, localUri, pathname, true)) {
3010
3009
  const content = item.content;
@@ -3012,8 +3011,9 @@ class FileManager extends core_1.Host {
3012
3011
  this.performAsyncTask();
3013
3012
  fs.writeFile(localUri, content, item.encoding || (item.encoding = 'utf-8'), err => fileReceived(item, localUri, err));
3014
3013
  }
3014
+ continue;
3015
3015
  }
3016
- else if (item.dataView) {
3016
+ if (item.dataView) {
3017
3017
  if (createFolder(item, pathname)) {
3018
3018
  this.performAsyncTask();
3019
3019
  const dataView = item.dataView;
@@ -3024,117 +3024,124 @@ class FileManager extends core_1.Host {
3024
3024
  fileReceived(item, localUri, err, false, true);
3025
3025
  });
3026
3026
  }
3027
+ continue;
3027
3028
  }
3028
- else if (item.base64) {
3029
+ if (item.base64) {
3029
3030
  if (createFolder(item, pathname)) {
3030
3031
  this.performAsyncTask();
3031
3032
  fs.writeFile(localUri, item.base64, 'base64', err => fileReceived(item, localUri, err, false, true));
3032
3033
  }
3034
+ continue;
3033
3035
  }
3034
- else {
3035
- const uri = item.uri;
3036
- if (!uri) {
3037
- item.invalid = true;
3038
- continue;
3039
- }
3040
- const checkDest = (src) => staging || !(0, lib_v4_1.hasSameStat)(src, localUri);
3041
- const type = item.fetchType || 0 /* FETCH_TYPE.UNKNOWN */;
3042
- const bundleMain = !(0, types_1.isEmpty)(item.bundleId) && item.bundleIndex === 0;
3043
- const isHttp = type === 1 /* FETCH_TYPE.HTTP */;
3044
- if (isHttp || type === 2 /* FETCH_TYPE.UNIX_SOCKET */) {
3045
- let checkEtag;
3046
- if ((0, types_1.isEmpty)(item.bundleId) && bundling[uri]) {
3047
- bundling[uri].push(item);
3048
- }
3049
- else if ((checkEtag = hasEtag(item)) || !checkQueue(item, localUri, pathname)) {
3050
- if (!isHttp && !checkDest(uri)) {
3051
- fileReceived(item, localUri);
3052
- }
3053
- else if (!checkEtag && downloading[uri]) {
3054
- downloading[uri].push(item);
3036
+ if (!uri) {
3037
+ item.invalid = true;
3038
+ continue;
3039
+ }
3040
+ const checkDest = (src) => staging || !(0, lib_v4_1.hasSameStat)(src, localUri);
3041
+ const type = item.fetchType || 0 /* FETCH_TYPE.UNKNOWN */;
3042
+ const bundleMain = item.bundleIndex === 0 && !(0, types_1.isEmpty)(item.bundleId);
3043
+ const isHttp = type === 1 /* FETCH_TYPE.HTTP */;
3044
+ if (isHttp || type === 2 /* FETCH_TYPE.UNIX_SOCKET */) {
3045
+ let checkEtag;
3046
+ if (bundling[uri] && (0, types_1.isEmpty)(item.bundleId)) {
3047
+ bundling[uri].push(item);
3048
+ }
3049
+ else if ((checkEtag = hasEtag(item)) || !checkQueue(item, localUri, pathname)) {
3050
+ if (!isHttp && !checkDest(uri)) {
3051
+ fileReceived(item, localUri);
3052
+ }
3053
+ else if (!checkEtag && downloading[uri]) {
3054
+ downloading[uri].push(item);
3055
+ }
3056
+ else if (!isHttp && !this.canRead(uri)) {
3057
+ errorPermission(item);
3058
+ }
3059
+ else {
3060
+ if (!checkEtag) {
3061
+ downloading[uri] = [];
3055
3062
  }
3056
- else if (!isHttp && !this.canRead(uri)) {
3057
- errorPermission(item);
3063
+ const url = item.url;
3064
+ let cacheDir, cacheBuffer, mainEtag, encoding = item.encoding, client = null;
3065
+ if (isHttp) {
3066
+ cacheDir = cacheToDisk.has(url) && isCacheable(item);
3067
+ cacheBuffer = cacheToMemory.has(url);
3068
+ mainEtag = (0, types_1.mainFlag)(item.flags) && (cacheEtag || item.incremental === "etag" /* INCREMENTAL.ETAG */);
3058
3069
  }
3059
- else {
3060
- if (!checkEtag) {
3061
- downloading[uri] = [];
3070
+ const closeResponse = () => client?.destroy();
3071
+ const downloadUri = (request, etagDir) => {
3072
+ if (checkEtag) {
3073
+ item.flags &= ~128 /* ASSET_FLAG.EXISTS */;
3062
3074
  }
3063
- const url = item.url;
3064
- let cacheDir, cacheBuffer, mainEtag, encoding = item.encoding, client = null;
3075
+ closeResponse();
3076
+ const location = request.url.toString();
3077
+ request.encoding = encoding;
3078
+ request.pipeTo = localUri;
3079
+ request.statusMessage = location + (bundleMain ? ' (0)' : '');
3065
3080
  if (isHttp) {
3066
- cacheDir = cacheToDisk.has(url) && isCacheable(item);
3067
- cacheBuffer = cacheToMemory.has(url);
3068
- mainEtag = (0, types_1.mainFlag)(item.flags) && (cacheEtag || item.incremental === 'etag');
3081
+ request.method = 'GET';
3082
+ const count = originCount[request.url.origin] || 0;
3083
+ if (count <= 1) {
3084
+ request.httpVersion = 1;
3085
+ }
3086
+ request.connected = headers => {
3087
+ applyHeaders(item, headers, true, mainEtag);
3088
+ return item.willChange || !!etagDir && (cacheDir || !item.contentLength || cacheToMemory.within(item.contentLength));
3089
+ };
3090
+ }
3091
+ else {
3092
+ request.socketPath = item.socketPath;
3093
+ request.httpVersion = 1;
3069
3094
  }
3070
- const closeResponse = () => client?.destroy();
3071
- const downloadUri = (request, etagDir) => {
3072
- if (checkEtag) {
3073
- item.flags &= ~128 /* ASSET_FLAG.EXISTS */;
3095
+ this.fetchBuffer(request.url, request)
3096
+ .then(data => {
3097
+ if (typeof data === 'string') {
3098
+ item.sourceUTF8 = data;
3074
3099
  }
3075
- closeResponse();
3076
- const location = request.url.toString();
3077
- request.encoding = encoding;
3078
- request.pipeTo = localUri;
3079
- request.statusMessage = location + (bundleMain ? ' (0)' : '');
3080
- if (isHttp) {
3081
- request.method = 'GET';
3082
- request.httpVersion = (originCount[request.url.origin] || 0) <= 1 ? 1 : undefined;
3083
- request.connected = headers => {
3084
- applyHeaders(item, headers, true, mainEtag);
3085
- return item.willChange || !!etagDir && (cacheDir || !item.contentLength || cacheToMemory.within(item.contentLength));
3086
- };
3100
+ else if (data) {
3101
+ item.buffer = data;
3087
3102
  }
3088
- else {
3089
- request.socketPath = item.socketPath;
3090
- request.httpVersion = 1;
3103
+ fileReceived(item, localUri, null, isHttp);
3104
+ if (etagDir) {
3105
+ queueMicrotask(() => {
3106
+ if (cacheBuffer && data) {
3107
+ cacheToMemory.add(location, etagDir, data, { encoding, contentLength: item.contentLength, toDisk: !cacheDir && isCacheable(item) ? path.basename(localUri) : '' });
3108
+ }
3109
+ if (cacheDir) {
3110
+ cacheToDisk.add(location, etagDir, localUri, { buffer: data, contentLength: item.contentLength });
3111
+ }
3112
+ });
3091
3113
  }
3092
- this.fetchBuffer(request.url, request)
3093
- .then(data => {
3094
- if (typeof data === 'string') {
3095
- item.sourceUTF8 = data;
3096
- }
3097
- else if (data) {
3098
- item.buffer = data;
3099
- }
3100
- fileReceived(item, localUri, null, isHttp);
3101
- if (etagDir) {
3102
- queueMicrotask(() => {
3103
- if (cacheBuffer && data) {
3104
- cacheToMemory.add(location, etagDir, data, { encoding, contentLength: item.contentLength, toDisk: !cacheDir && isCacheable(item) ? path.basename(localUri) : '' });
3105
- }
3106
- if (cacheDir) {
3107
- cacheToDisk.add(location, etagDir, localUri, { buffer: data, contentLength: item.contentLength });
3108
- }
3109
- });
3110
- }
3111
- })
3112
- .catch(err => errorRequest(item, err));
3113
- };
3114
- this.performAsyncTask();
3115
- const request = this.Request;
3116
- if (cacheDir || cacheBuffer || mainEtag) {
3117
- let redirects = 0;
3118
- (function checkHeaders(href) {
3119
- try {
3120
- const target = request.opts(href, { method: 'HEAD', httpVersion: 1 });
3121
- (client = request.open(href, target))
3122
- .on('response', res => {
3123
- const statusCode = res.statusCode;
3124
- if (statusCode < 300 /* HTTP_STATUS.MULTIPLE_CHOICES */) {
3125
- const etag = applyHeaders(item, res.headers, false, mainEtag);
3126
- let tempDir, etagDir;
3127
- if (etag) {
3128
- if (cacheDir) {
3129
- tempDir = this.getCacheDir(href);
3130
- }
3131
- etagDir = encodeURIComponent(etag);
3132
- if (incremental !== false || item.incremental === true) {
3133
- const location = href.toString();
3134
- const cached = MEMORY.CACHE[location];
3135
- let buffer;
3136
- if (cached) {
3137
- if (etagDir === cached[5] && (!encoding || encoding === cached[2])) {
3114
+ })
3115
+ .catch(err => errorRequest(item, err));
3116
+ };
3117
+ this.performAsyncTask();
3118
+ if (cacheDir || cacheBuffer || mainEtag) {
3119
+ let redirects = 0;
3120
+ (function checkHeaders(href) {
3121
+ const request = this.Request;
3122
+ try {
3123
+ const target = request.opts(href, { method: 'HEAD', httpVersion: 1 });
3124
+ (client = request.open(href, target))
3125
+ .on('response', res => {
3126
+ const statusCode = res.statusCode;
3127
+ if (statusCode < 300 /* HTTP_STATUS.MULTIPLE_CHOICES */) {
3128
+ const etag = applyHeaders(item, res.headers, false, mainEtag);
3129
+ let tempDir, etagDir;
3130
+ if (etag) {
3131
+ if (cacheDir) {
3132
+ tempDir = this.getCacheDir(href);
3133
+ }
3134
+ etagDir = encodeURIComponent(etag);
3135
+ if (incremental !== false || item.incremental === true) {
3136
+ const location = href.toString();
3137
+ const cached = MEMORY.CACHE[location];
3138
+ let buffer;
3139
+ if (cached) {
3140
+ if (etagDir === cached[5] && (!encoding || encoding === cached[2])) {
3141
+ if (item.checksum && !checkHash.call(this, localUri, false, item.checksum, cached[1])) {
3142
+ tempDir = undefined;
3143
+ }
3144
+ else {
3138
3145
  if (checkEtag) {
3139
3146
  this.completeAsyncTask(null, localUri);
3140
3147
  closeResponse();
@@ -3145,194 +3152,209 @@ class FileManager extends core_1.Host {
3145
3152
  item.encoding = encoding;
3146
3153
  cached[0] = Date.now();
3147
3154
  }
3148
- else {
3149
- cacheToMemory.clear(location);
3150
- }
3151
3155
  }
3152
- try {
3153
- const setBuffer = () => {
3154
- if (typeof buffer === 'string') {
3155
- item.sourceUTF8 = buffer;
3156
- }
3157
- else if (buffer) {
3158
- item.buffer = buffer;
3156
+ else {
3157
+ cacheToMemory.clear(location);
3158
+ }
3159
+ }
3160
+ try {
3161
+ const setBuffer = () => {
3162
+ if (typeof buffer === 'string') {
3163
+ item.sourceUTF8 = buffer;
3164
+ }
3165
+ else if (buffer) {
3166
+ item.buffer = buffer;
3167
+ }
3168
+ };
3169
+ let pipeAs;
3170
+ if (tempDir && (0, lib_v4_1.hasSize)(pipeAs = path.join(tempDir, etagDir, path.basename(localUri)))) {
3171
+ if (!checkEtag) {
3172
+ if (cacheBuffer && !buffer) {
3173
+ buffer = fs.readFileSync(pipeAs, encoding);
3174
+ if (item.checksum && !checkHash.call(this, localUri, false, item.checksum, buffer)) {
3175
+ downloadUri(target, etagDir);
3176
+ return;
3177
+ }
3178
+ if (cacheToMemory.within(item.contentLength = Buffer.byteLength(buffer, encoding))) {
3179
+ cacheToMemory.add(location, etagDir, buffer, { encoding, contentLength: item.contentLength, tempFile: pipeAs });
3180
+ }
3159
3181
  }
3160
- };
3161
- let pipeAs;
3162
- if (tempDir && (0, lib_v4_1.hasSize)(pipeAs = path.join(tempDir, etagDir, path.basename(localUri)))) {
3163
- if (!checkEtag) {
3164
- if (cacheBuffer && !buffer) {
3165
- buffer = fs.readFileSync(pipeAs, encoding);
3166
- if (cacheToMemory.within(item.contentLength = Buffer.byteLength(buffer, encoding))) {
3167
- cacheToMemory.add(location, etagDir, buffer, { encoding, contentLength: item.contentLength, tempFile: pipeAs });
3168
- }
3182
+ if (checkDest(pipeAs)) {
3183
+ const writeBuffer = () => {
3184
+ fs.writeFileSync(localUri, buffer);
3185
+ this.addDownload(Buffer.byteLength(buffer, encoding), types_1.DOWNLOAD_TYPE.CACHE);
3186
+ };
3187
+ if (buffer) {
3188
+ writeBuffer();
3169
3189
  }
3170
- if (checkDest(pipeAs)) {
3171
- if (buffer) {
3172
- fs.writeFileSync(localUri, buffer);
3173
- this.addDownload(Buffer.byteLength(buffer, encoding), types_1.DOWNLOAD_TYPE.CACHE);
3174
- }
3175
- else {
3176
- fs.copyFileSync(pipeAs, localUri);
3177
- this.addDownload(pipeAs, types_1.DOWNLOAD_TYPE.CACHE);
3190
+ else if (item.checksum) {
3191
+ buffer = fs.readFileSync(pipeAs, encoding);
3192
+ if (!checkHash.call(this, localUri, false, item.checksum, buffer)) {
3193
+ downloadUri(target, etagDir);
3194
+ return;
3178
3195
  }
3196
+ writeBuffer();
3179
3197
  }
3180
- if (item.willChange) {
3181
- setBuffer();
3198
+ else {
3199
+ fs.copyFileSync(pipeAs, localUri);
3200
+ this.addDownload(pipeAs, types_1.DOWNLOAD_TYPE.CACHE);
3182
3201
  }
3183
- fileReceived(item, localUri, null, true);
3184
3202
  }
3185
- else {
3186
- this.completeAsyncTask(null, localUri);
3203
+ if (item.willChange) {
3204
+ setBuffer();
3187
3205
  }
3188
- closeResponse();
3189
- return;
3206
+ fileReceived(item, localUri, null, true, false, true);
3190
3207
  }
3191
- if (buffer) {
3192
- setBuffer();
3193
- fs.writeFileSync(localUri, buffer);
3194
- this.addDownload(Buffer.byteLength(buffer, encoding), types_1.DOWNLOAD_TYPE.CACHE);
3195
- if (checkEtag) {
3196
- item.flags &= ~128 /* ASSET_FLAG.EXISTS */;
3197
- }
3198
- fileReceived(item, localUri, null, true);
3199
- closeResponse();
3200
- if (pipeAs) {
3201
- fs.writeFile(pipeAs, buffer, () => { });
3202
- }
3203
- return;
3208
+ else {
3209
+ this.completeAsyncTask(null, localUri);
3204
3210
  }
3211
+ closeResponse();
3212
+ return;
3205
3213
  }
3206
- catch {
3214
+ if (buffer) {
3215
+ setBuffer();
3216
+ fs.writeFileSync(localUri, buffer);
3217
+ this.addDownload(Buffer.byteLength(buffer, encoding), types_1.DOWNLOAD_TYPE.CACHE);
3218
+ if (checkEtag) {
3219
+ item.flags &= ~128 /* ASSET_FLAG.EXISTS */;
3220
+ }
3221
+ fileReceived(item, localUri, null, true, false, true);
3222
+ closeResponse();
3223
+ if (pipeAs) {
3224
+ fs.writeFile(pipeAs, buffer, () => { });
3225
+ }
3226
+ return;
3207
3227
  }
3208
3228
  }
3209
- }
3210
- downloadUri(target, etagDir);
3211
- }
3212
- else if (statusCode < 400 /* HTTP_STATUS.BAD_REQUEST */) {
3213
- closeResponse();
3214
- const location = res.headers.location;
3215
- if (location && ++redirects <= HTTP_CLIENT.redirectLimit) {
3216
- try {
3217
- checkHeaders.call(this, new URL(request_1.default.fromURL(href, location)));
3218
- return;
3219
- }
3220
3229
  catch {
3221
3230
  }
3222
3231
  }
3223
- errorRequest(item, (0, types_1.errorMessage)(statusCode, 'Exceeded redirect limit', url.toString()));
3224
3232
  }
3225
- else {
3226
- downloadUri(target);
3227
- }
3228
- })
3229
- .on('error', err => {
3230
- if (!client.destroyed) {
3231
- if ((0, util_1.checkRetryable)(err)) {
3232
- downloadUri(target);
3233
+ downloadUri(target, etagDir);
3234
+ }
3235
+ else if (statusCode < 400 /* HTTP_STATUS.BAD_REQUEST */) {
3236
+ closeResponse();
3237
+ const location = res.headers.location;
3238
+ if (location && ++redirects <= HTTP_CLIENT.redirectLimit) {
3239
+ try {
3240
+ checkHeaders.call(this, new URL(request_1.default.fromURL(href, location)));
3241
+ return;
3233
3242
  }
3234
- else {
3235
- errorRequest(item, err);
3243
+ catch {
3236
3244
  }
3237
3245
  }
3238
- })
3239
- .on('timeout', () => {
3240
- if (!client.destroyed) {
3246
+ errorRequest(item, (0, types_1.errorMessage)(statusCode, 'Exceeded redirect limit', url.toString()));
3247
+ }
3248
+ else {
3249
+ downloadUri(target);
3250
+ }
3251
+ })
3252
+ .on('error', err => {
3253
+ if (!client.destroyed) {
3254
+ if ((0, util_1.checkRetryable)(err)) {
3241
3255
  downloadUri(target);
3242
3256
  }
3243
- });
3244
- }
3245
- catch {
3246
- downloadUri(request.opts(href));
3247
- }
3248
- }).bind(this)(url);
3249
- }
3250
- else {
3251
- downloadUri(request.opts(url));
3252
- }
3257
+ else {
3258
+ errorRequest(item, err);
3259
+ }
3260
+ }
3261
+ })
3262
+ .on('timeout', () => {
3263
+ if (!client.destroyed) {
3264
+ downloadUri(target);
3265
+ }
3266
+ });
3267
+ }
3268
+ catch {
3269
+ downloadUri(request.opts(href));
3270
+ }
3271
+ }).bind(this)(url);
3272
+ }
3273
+ else {
3274
+ downloadUri(this.Request.opts(url));
3253
3275
  }
3254
3276
  }
3255
3277
  }
3256
- else if (type) {
3257
- if (bundleMain && !item.mimeType) {
3258
- errorRequest(item, (0, types_1.errorValue)("MIME not found" /* ERR_MESSAGE.NOTFOUND_MIME */, uri), true);
3278
+ }
3279
+ else if (type) {
3280
+ if (bundleMain && !item.mimeType) {
3281
+ errorRequest(item, (0, types_1.errorValue)("MIME not found" /* ERR_MESSAGE.NOTFOUND_MIME */, uri), true);
3282
+ }
3283
+ else if (!checkQueue(item, localUri, pathname, false)) {
3284
+ if (downloading[uri]) {
3285
+ downloading[uri].push(item);
3286
+ continue;
3259
3287
  }
3260
- else if (!checkQueue(item, localUri, pathname, false)) {
3261
- if (downloading[uri]) {
3262
- downloading[uri].push(item);
3263
- continue;
3288
+ downloading[uri] = [];
3289
+ this.performAsyncTask();
3290
+ this.fetchFiles(item.url || uri, { pathname, binOpts: item.binOpts })
3291
+ .then(result => {
3292
+ if (result.length === 0) {
3293
+ errorRequest(item, (0, types_1.errorValue)('No files were successfully downloaded', uri));
3264
3294
  }
3265
- downloading[uri] = [];
3266
- this.performAsyncTask();
3267
- this.fetchFiles(item.url || uri, { pathname, binOpts: item.binOpts })
3268
- .then(result => {
3269
- if (result.length === 0) {
3270
- errorRequest(item, (0, types_1.errorValue)('No files were successfully downloaded', uri));
3271
- }
3272
- else if (bundleMain) {
3273
- const encoding = item.encoding || (item.encoding = 'utf-8');
3274
- fs.writeFile(localUri, bundleTorrent.call(this, result, item.mimeType, encoding), encoding, err => {
3275
- if (!err) {
3276
- fetchedAssets.push(item);
3277
- processQueue(item, localUri);
3278
- }
3279
- else {
3280
- errorRequest(item, err);
3281
- }
3282
- });
3283
- }
3284
- else {
3285
- const ext = item.filename && path.extname(item.filename);
3286
- let found;
3287
- for (const file of result) {
3288
- if (!found && ext === path.extname(file).toLowerCase()) {
3289
- try {
3290
- if (file !== localUri && !core_1.Host.renameFile(file, localUri)) {
3291
- this.filesToRemove.add(file);
3292
- }
3293
- found = true;
3294
- }
3295
- catch (err) {
3296
- this.writeFail(["Unable to rename file" /* ERR_MESSAGE.RENAME_FILE */, item.filename], err, 32 /* LOG_TYPE.FILE */);
3295
+ else if (bundleMain) {
3296
+ const encoding = item.encoding || (item.encoding = 'utf-8');
3297
+ fs.writeFile(localUri, bundleTorrent.call(this, result, item.mimeType, encoding), encoding, err => {
3298
+ if (!err) {
3299
+ fetchedAssets.push(item);
3300
+ processQueue(item, localUri);
3301
+ }
3302
+ else {
3303
+ errorRequest(item, err);
3304
+ }
3305
+ });
3306
+ }
3307
+ else {
3308
+ const ext = item.filename && path.extname(item.filename);
3309
+ let found;
3310
+ for (const file of result) {
3311
+ if (!found && ext === path.extname(file).toLowerCase()) {
3312
+ try {
3313
+ if (file !== localUri && !core_1.Host.renameFile(file, localUri)) {
3314
+ this.filesToRemove.add(file);
3297
3315
  }
3316
+ found = true;
3298
3317
  }
3299
- else {
3300
- this.add(file, item, types_1.FILE_TYPE.TORRENT);
3318
+ catch (err) {
3319
+ this.writeFail(["Unable to rename file" /* ERR_MESSAGE.RENAME_FILE */, item.filename], err, 32 /* LOG_TYPE.FILE */);
3301
3320
  }
3302
3321
  }
3303
- if (found) {
3304
- fetchedAssets.push(item);
3305
- processQueue(item, localUri);
3306
- }
3307
3322
  else {
3308
- item.filename = '';
3309
- item.localUri = '';
3310
- item.mimeType = '';
3311
- clearQueue(processing, localUri);
3312
- clearQueue(downloading, uri);
3313
- this.completeAsyncTask();
3323
+ this.add(file, item, types_1.FILE_TYPE.TORRENT);
3314
3324
  }
3315
3325
  }
3316
- })
3317
- .catch(err => errorRequest(item, err));
3318
- }
3326
+ if (found) {
3327
+ fetchedAssets.push(item);
3328
+ processQueue(item, localUri);
3329
+ }
3330
+ else {
3331
+ item.filename = '';
3332
+ item.localUri = '';
3333
+ item.mimeType = '';
3334
+ clearQueue(processing, localUri);
3335
+ clearQueue(downloading, uri);
3336
+ this.completeAsyncTask();
3337
+ }
3338
+ }
3339
+ })
3340
+ .catch(err => errorRequest(item, err));
3319
3341
  }
3320
- else if (!this.canRead(uri)) {
3321
- errorPermission(item);
3342
+ }
3343
+ else if (!this.canRead(uri)) {
3344
+ errorPermission(item);
3345
+ }
3346
+ else if (!checkQueue(item, localUri, pathname)) {
3347
+ this.performAsyncTask();
3348
+ if (checkDest(uri)) {
3349
+ fs.copyFile(uri, localUri, err => {
3350
+ if (!err) {
3351
+ this.addDownload(localUri, types_1.DOWNLOAD_TYPE.DISK);
3352
+ }
3353
+ fileReceived(item, localUri, err);
3354
+ });
3322
3355
  }
3323
- else if (!checkQueue(item, localUri, pathname)) {
3324
- this.performAsyncTask();
3325
- if (checkDest(uri)) {
3326
- fs.copyFile(uri, localUri, err => {
3327
- if (!err) {
3328
- this.addDownload(localUri, types_1.DOWNLOAD_TYPE.DISK);
3329
- }
3330
- fileReceived(item, localUri, err);
3331
- });
3332
- }
3333
- else {
3334
- fileReceived(item, localUri);
3335
- }
3356
+ else {
3357
+ fileReceived(item, localUri);
3336
3358
  }
3337
3359
  }
3338
3360
  }
@@ -3489,10 +3511,10 @@ class FileManager extends core_1.Host {
3489
3511
  for (const item of this.assets) {
3490
3512
  if (item.checksumOutput && !ignoreAsset(item)) {
3491
3513
  const localUri = item.localUri;
3492
- if (!checkHash(localUri, false, item.checksumOutput)) {
3514
+ if (!filesToRemove.has(localUri) && !checkHash.call(this, localUri, true, item.checksumOutput)) {
3493
3515
  item.invalid = true;
3494
3516
  filesToRemove.add(localUri);
3495
- this.writeFail(["Invalid checksum" /* ERR_MESSAGE.CHECKSUM */, path.basename(localUri)], (0, types_1.errorValue)("Checksum did not match" /* ERR_MESSAGE.FAILED_CHECKSUM */, localUri), { type: 32 /* LOG_TYPE.FILE */, startTime });
3517
+ this.writeFail(["Checksum did not match" /* ERR_MESSAGE.FAILED_CHECKSUM */, path.basename(localUri)], (0, types_1.errorValue)(localUri, "Invalid checksum" /* ERR_MESSAGE.CHECKSUM */), { type: 32 /* LOG_TYPE.FILE */, startTime, queue: true });
3496
3518
  }
3497
3519
  }
3498
3520
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/file-manager",
3
- "version": "0.8.1",
3
+ "version": "0.8.3",
4
4
  "description": "FileManager constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -9,7 +9,7 @@
9
9
  },
10
10
  "repository": {
11
11
  "type": "git",
12
- "url": "https://github.com/anpham6/e-mc.git",
12
+ "url": "git+https://github.com/anpham6/e-mc.git",
13
13
  "directory": "src/file-manager"
14
14
  },
15
15
  "keywords": [
@@ -20,15 +20,15 @@
20
20
  "license": "BSD 3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/cloud": "0.8.1",
24
- "@e-mc/compress": "0.8.1",
25
- "@e-mc/core": "0.8.1",
26
- "@e-mc/document": "0.8.1",
27
- "@e-mc/image": "0.8.1",
28
- "@e-mc/request": "0.8.1",
29
- "@e-mc/task": "0.8.1",
30
- "@e-mc/types": "0.8.1",
31
- "@e-mc/watch": "0.8.1",
23
+ "@e-mc/cloud": "0.8.3",
24
+ "@e-mc/compress": "0.8.3",
25
+ "@e-mc/core": "0.8.3",
26
+ "@e-mc/document": "0.8.3",
27
+ "@e-mc/image": "0.8.3",
28
+ "@e-mc/request": "0.8.3",
29
+ "@e-mc/task": "0.8.3",
30
+ "@e-mc/types": "0.8.3",
31
+ "@e-mc/watch": "0.8.3",
32
32
  "picomatch": "^3.0.1"
33
33
  }
34
34
  }