@e-mc/file-manager 0.8.2 → 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.
- package/index.js +395 -362
- package/package.json +11 -11
package/index.js
CHANGED
|
@@ -155,26 +155,34 @@ function recurseDir(output, subDirs, options) {
|
|
|
155
155
|
}
|
|
156
156
|
return output;
|
|
157
157
|
}
|
|
158
|
-
function checkHash(
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
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
|
}
|
|
@@ -945,7 +953,7 @@ class FileManager extends core_1.Host {
|
|
|
945
953
|
item.id || (item.id = ++ASSET_ID);
|
|
946
954
|
}
|
|
947
955
|
this._assets = assets.slice(0);
|
|
948
|
-
this[kIncremental] = incremental ===
|
|
956
|
+
this[kIncremental] = incremental === "staging" /* INCREMENTAL.STAGING */ ? "staging" /* INCREMENTAL.STAGING */ : "none" /* INCREMENTAL.NONE */;
|
|
949
957
|
if (targeted) {
|
|
950
958
|
this.using(...targeted);
|
|
951
959
|
}
|
|
@@ -1265,7 +1273,7 @@ class FileManager extends core_1.Host {
|
|
|
1265
1273
|
}
|
|
1266
1274
|
}
|
|
1267
1275
|
}
|
|
1268
|
-
const manager = new FileManager(this.baseDirectory, { ...this.config, incremental:
|
|
1276
|
+
const manager = new FileManager(this.baseDirectory, { ...this.config, incremental: "none" /* INCREMENTAL.NONE */, assets }, postFinalize);
|
|
1269
1277
|
for (const { constructor, params } of this.Document) {
|
|
1270
1278
|
manager.install('document', constructor, ...params);
|
|
1271
1279
|
}
|
|
@@ -1617,21 +1625,19 @@ class FileManager extends core_1.Host {
|
|
|
1617
1625
|
return document === moduleName || Array.isArray(document) && document.includes(moduleName);
|
|
1618
1626
|
}
|
|
1619
1627
|
setLocalUri(file, replace) {
|
|
1620
|
-
let uri = file.uri,
|
|
1621
|
-
if (uri && !file.content && !file.base64 && !file.
|
|
1628
|
+
let uri = file.uri, type;
|
|
1629
|
+
if (uri && !file.url && !file.content && !file.base64 && !file.dataView) {
|
|
1622
1630
|
if (core_1.Host.isFile(uri, 'torrent')) {
|
|
1623
|
-
|
|
1631
|
+
type = 4 /* FETCH_TYPE.TORRENT */;
|
|
1624
1632
|
}
|
|
1625
1633
|
else if (core_1.Host.isURL(uri, 'file')) {
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
return { pathname: '', localUri: '' };
|
|
1634
|
-
}
|
|
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: '' };
|
|
1635
1641
|
}
|
|
1636
1642
|
}
|
|
1637
1643
|
else {
|
|
@@ -1659,7 +1665,6 @@ class FileManager extends core_1.Host {
|
|
|
1659
1665
|
}
|
|
1660
1666
|
}
|
|
1661
1667
|
const url = file.url;
|
|
1662
|
-
let type;
|
|
1663
1668
|
if (url) {
|
|
1664
1669
|
if (url.protocol.startsWith('http')) {
|
|
1665
1670
|
type = 1 /* FETCH_TYPE.HTTP */;
|
|
@@ -1667,12 +1672,7 @@ class FileManager extends core_1.Host {
|
|
|
1667
1672
|
else if (file.socketPath && url.protocol === 'file:') {
|
|
1668
1673
|
type = 2 /* FETCH_TYPE.UNIX_SOCKET */;
|
|
1669
1674
|
}
|
|
1670
|
-
|
|
1671
|
-
if (!type && uri) {
|
|
1672
|
-
if (core_1.Host.isFile(uri, 'torrent')) {
|
|
1673
|
-
type = 4 /* FETCH_TYPE.TORRENT */;
|
|
1674
|
-
}
|
|
1675
|
-
else if (core_1.Host.isFile(uri, 's/ftp')) {
|
|
1675
|
+
else if (core_1.Host.isFile(url, 's/ftp')) {
|
|
1676
1676
|
type = 3 /* FETCH_TYPE.FTP */;
|
|
1677
1677
|
}
|
|
1678
1678
|
}
|
|
@@ -1694,7 +1694,7 @@ class FileManager extends core_1.Host {
|
|
|
1694
1694
|
const pathname = segments.length ? path.join(this.baseDirectory, ...segments) : this.baseDirectory;
|
|
1695
1695
|
const localUri = file.filename ? path.join(pathname, file.filename) : pathname;
|
|
1696
1696
|
file.localUri = localUri;
|
|
1697
|
-
if (
|
|
1697
|
+
if (type !== 4 /* FETCH_TYPE.TORRENT */) {
|
|
1698
1698
|
file.mimeType || (file.mimeType = file.url && core_1.Host.lookupMime(path.basename(file.url.pathname)) || core_1.Host.lookupMime(file.filename));
|
|
1699
1699
|
}
|
|
1700
1700
|
return { pathname, localUri };
|
|
@@ -1804,42 +1804,36 @@ class FileManager extends core_1.Host {
|
|
|
1804
1804
|
}
|
|
1805
1805
|
getAssetContent({ localUri, document, mimeType }, content) {
|
|
1806
1806
|
const appending = this.contentToAppend.get(localUri);
|
|
1807
|
-
if (appending) {
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
if (
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
if (output) {
|
|
1824
|
-
value = output;
|
|
1825
|
-
break;
|
|
1826
|
-
}
|
|
1827
|
-
}
|
|
1828
|
-
}
|
|
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;
|
|
1829
1823
|
}
|
|
1830
|
-
content = content.substring(0, match.index) + value + getSeparator(content) + content.substring(match.index + match[0].length);
|
|
1831
|
-
continue;
|
|
1832
1824
|
}
|
|
1833
1825
|
}
|
|
1834
|
-
content += getSeparator(content) + value;
|
|
1835
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;
|
|
1836
1831
|
}
|
|
1837
|
-
return content;
|
|
1838
1832
|
}
|
|
1839
1833
|
}
|
|
1840
|
-
return
|
|
1834
|
+
return content;
|
|
1841
1835
|
}
|
|
1842
|
-
return content;
|
|
1836
|
+
return (content || '') + appending.reduce((a, b) => b ? a + getSeparator(b) + b : a, '');
|
|
1843
1837
|
}
|
|
1844
1838
|
writeBuffer(file, options) {
|
|
1845
1839
|
const buffer = file.sourceUTF8 ? Buffer.from(file.sourceUTF8, file.encoding || (file.encoding = 'utf-8')) : file.buffer;
|
|
@@ -2221,15 +2215,15 @@ class FileManager extends core_1.Host {
|
|
|
2221
2215
|
getDownload(type = 0) {
|
|
2222
2216
|
return this[kDownloadStats][type];
|
|
2223
2217
|
}
|
|
2224
|
-
async fetchObject(uri, options) {
|
|
2225
|
-
if (
|
|
2218
|
+
async fetchObject(uri, options = {}) {
|
|
2219
|
+
if (typeof options === 'string') {
|
|
2226
2220
|
options = { format: options };
|
|
2227
2221
|
}
|
|
2228
2222
|
options.format || (options.format = 'json');
|
|
2229
|
-
return this.
|
|
2223
|
+
return this.Request.get(uri, options).then(data => typeof data === 'object' ? data : null).catch(() => null);
|
|
2230
2224
|
}
|
|
2231
2225
|
async fetchBuffer(uri, options) {
|
|
2232
|
-
if (options) {
|
|
2226
|
+
if (options && 'format' in options) {
|
|
2233
2227
|
options.format = undefined;
|
|
2234
2228
|
}
|
|
2235
2229
|
return this.Request.get(uri, options).then(buffer => {
|
|
@@ -2246,8 +2240,8 @@ class FileManager extends core_1.Host {
|
|
|
2246
2240
|
return buffer;
|
|
2247
2241
|
});
|
|
2248
2242
|
}
|
|
2249
|
-
async fetchFiles(uri, options) {
|
|
2250
|
-
if (
|
|
2243
|
+
async fetchFiles(uri, options = {}) {
|
|
2244
|
+
if (typeof options === 'string') {
|
|
2251
2245
|
options = { pathname: options };
|
|
2252
2246
|
}
|
|
2253
2247
|
options.pathname || (options.pathname = this.baseDirectory);
|
|
@@ -2403,7 +2397,6 @@ class FileManager extends core_1.Host {
|
|
|
2403
2397
|
this.performFinalize();
|
|
2404
2398
|
}
|
|
2405
2399
|
}, types_1.THRESHOLD.FILEMANAGER_INTERVAL);
|
|
2406
|
-
const hasIncremental = (value) => value === 'etag' || value === 'exists';
|
|
2407
2400
|
const { assets, cacheToDisk, cacheToMemory, fetchedAssets, Watch: watch } = this;
|
|
2408
2401
|
const downloadable = new Map();
|
|
2409
2402
|
const completed = Object.create(null);
|
|
@@ -2413,8 +2406,10 @@ class FileManager extends core_1.Host {
|
|
|
2413
2406
|
const bundling = Object.create(null);
|
|
2414
2407
|
const originCount = Object.create(null);
|
|
2415
2408
|
const emptied = [this.baseDirectory];
|
|
2416
|
-
const staging = this[kIncremental] ===
|
|
2409
|
+
const staging = this[kIncremental] === "staging" /* INCREMENTAL.STAGING */;
|
|
2417
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 */;
|
|
2418
2413
|
let cacheable = false, cacheOpen = false, cacheEtag = false;
|
|
2419
2414
|
if (!staging) {
|
|
2420
2415
|
cacheable = this[kRecursionLimit] === RECURSION_LIMIT;
|
|
@@ -2422,25 +2417,24 @@ class FileManager extends core_1.Host {
|
|
|
2422
2417
|
if (cacheOpen = hasIncremental(incremental)) {
|
|
2423
2418
|
this[kIncremental] = incremental;
|
|
2424
2419
|
}
|
|
2425
|
-
cacheEtag = incremental ===
|
|
2420
|
+
cacheEtag = incremental === "etag" /* INCREMENTAL.ETAG */;
|
|
2426
2421
|
}
|
|
2427
2422
|
else {
|
|
2428
|
-
this[kIncremental] =
|
|
2423
|
+
this[kIncremental] = "none" /* INCREMENTAL.NONE */;
|
|
2429
2424
|
}
|
|
2430
2425
|
}
|
|
2431
2426
|
const targeting = this._usingObjects.size > 0;
|
|
2432
|
-
const isCacheable = (file) => file.initialValue?.cacheable !== false;
|
|
2433
2427
|
const hasEtag = (file) => {
|
|
2434
2428
|
if (!cacheable || !(0, types_1.existsFlag)(file.flags)) {
|
|
2435
2429
|
return false;
|
|
2436
2430
|
}
|
|
2437
2431
|
switch (file.incremental) {
|
|
2438
2432
|
case false:
|
|
2439
|
-
case
|
|
2440
|
-
case
|
|
2441
|
-
case
|
|
2433
|
+
case "none" /* INCREMENTAL.NONE */:
|
|
2434
|
+
case "exists" /* INCREMENTAL.EXISTS */:
|
|
2435
|
+
case "staging" /* INCREMENTAL.STAGING */:
|
|
2442
2436
|
return false;
|
|
2443
|
-
case
|
|
2437
|
+
case "etag" /* INCREMENTAL.ETAG */:
|
|
2444
2438
|
return true;
|
|
2445
2439
|
default:
|
|
2446
2440
|
return cacheEtag;
|
|
@@ -2551,7 +2545,7 @@ class FileManager extends core_1.Host {
|
|
|
2551
2545
|
};
|
|
2552
2546
|
const processQueue = async (file, localUri) => {
|
|
2553
2547
|
completed[localUri] = file;
|
|
2554
|
-
if (!(0, types_1.isEmpty)(file.bundleId)
|
|
2548
|
+
if (file.bundleIndex === 0 && !(0, types_1.isEmpty)(file.bundleId)) {
|
|
2555
2549
|
this.setAssetContent(file, this.getUTF8String(file, localUri));
|
|
2556
2550
|
const bundleQueue = file.bundleQueue;
|
|
2557
2551
|
let success = true, error, checkEtag = !file.trailingContent && hasEtag(file);
|
|
@@ -2574,13 +2568,18 @@ class FileManager extends core_1.Host {
|
|
|
2574
2568
|
if (checkEtag && queue.trailingContent) {
|
|
2575
2569
|
checkEtag = false;
|
|
2576
2570
|
}
|
|
2577
|
-
const
|
|
2571
|
+
const checksumValid = (buffer) => !queue.checksum || checkHash.call(this, queue.localUri || localUri, false, queue.checksum, buffer);
|
|
2572
|
+
const verifyBundle = (value, etag, checked) => {
|
|
2578
2573
|
if (!queue.invalid) {
|
|
2574
|
+
if (!checked && !checksumValid(value)) {
|
|
2575
|
+
queue.invalid = true;
|
|
2576
|
+
return;
|
|
2577
|
+
}
|
|
2579
2578
|
if (value instanceof Buffer) {
|
|
2580
2579
|
value = value.toString(encoding);
|
|
2581
2580
|
}
|
|
2582
2581
|
const url = queue.url;
|
|
2583
|
-
if (etag && cacheToMemory.has(url)) {
|
|
2582
|
+
if (etag && url && cacheToMemory.has(url)) {
|
|
2584
2583
|
cacheToMemory.add(url, encodeURIComponent(etag), value, {
|
|
2585
2584
|
encoding,
|
|
2586
2585
|
contentLength: queue.contentLength,
|
|
@@ -2607,7 +2606,7 @@ class FileManager extends core_1.Host {
|
|
|
2607
2606
|
options.socketPath = queue.socketPath;
|
|
2608
2607
|
options.httpVersion = 1;
|
|
2609
2608
|
}
|
|
2610
|
-
else {
|
|
2609
|
+
else if (url) {
|
|
2611
2610
|
if (etag = queue.etag) {
|
|
2612
2611
|
const valid = incremental !== false || file.incremental === true;
|
|
2613
2612
|
const cached = valid && MEMORY.CACHE[uri];
|
|
@@ -2615,11 +2614,15 @@ class FileManager extends core_1.Host {
|
|
|
2615
2614
|
if (cached) {
|
|
2616
2615
|
if (etagDir === cached[5]) {
|
|
2617
2616
|
const source = cached[1];
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
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);
|
|
2621
2625
|
}
|
|
2622
|
-
cacheToMemory.clear(uri);
|
|
2623
2626
|
}
|
|
2624
2627
|
if (cacheToDisk.has(url) && isCacheable(queue)) {
|
|
2625
2628
|
const baseDir = path.join(this.getCacheDir(url), etagDir);
|
|
@@ -2627,9 +2630,11 @@ class FileManager extends core_1.Host {
|
|
|
2627
2630
|
try {
|
|
2628
2631
|
if (valid && (0, lib_v4_1.hasSize)(pipeTo)) {
|
|
2629
2632
|
const buffer = fs.readFileSync(pipeTo, { encoding });
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
+
if (checksumValid(buffer)) {
|
|
2634
|
+
verifyBundle(buffer, etag, true);
|
|
2635
|
+
this.addDownload(Buffer.byteLength(buffer, encoding), types_1.DOWNLOAD_TYPE.CACHE);
|
|
2636
|
+
continue;
|
|
2637
|
+
}
|
|
2633
2638
|
}
|
|
2634
2639
|
if (!fs.existsSync(baseDir)) {
|
|
2635
2640
|
fs.mkdirSync(baseDir);
|
|
@@ -2650,7 +2655,7 @@ class FileManager extends core_1.Host {
|
|
|
2650
2655
|
return true;
|
|
2651
2656
|
};
|
|
2652
2657
|
}
|
|
2653
|
-
tasks.push(this.fetchBuffer(url, options)
|
|
2658
|
+
tasks.push(this.fetchBuffer(url || uri, options)
|
|
2654
2659
|
.then(data => {
|
|
2655
2660
|
if (data) {
|
|
2656
2661
|
verifyBundle(data, etag);
|
|
@@ -2660,7 +2665,7 @@ class FileManager extends core_1.Host {
|
|
|
2660
2665
|
}
|
|
2661
2666
|
const downloaded = bundling[uri];
|
|
2662
2667
|
if ((0, types_1.isArray)(downloaded)) {
|
|
2663
|
-
if (data) {
|
|
2668
|
+
if (data && !queue.invalid) {
|
|
2664
2669
|
if (typeof data === 'string') {
|
|
2665
2670
|
queue.sourceUTF8 = data;
|
|
2666
2671
|
}
|
|
@@ -2815,16 +2820,19 @@ class FileManager extends core_1.Host {
|
|
|
2815
2820
|
}
|
|
2816
2821
|
return true;
|
|
2817
2822
|
};
|
|
2818
|
-
const fileReceived = (item, localUri, err, fetched, binary) => {
|
|
2819
|
-
if (item.checksum && !err && !checkHash(item.buffer || localUri, !!item.buffer, item.checksum)) {
|
|
2820
|
-
err = (0, types_1.errorValue)("Checksum did not match" /* ERR_MESSAGE.FAILED_CHECKSUM */, item.uri);
|
|
2821
|
-
this.filesToRemove.add(localUri);
|
|
2822
|
-
}
|
|
2823
|
+
const fileReceived = (item, localUri, err, fetched, binary, checked) => {
|
|
2823
2824
|
if (err) {
|
|
2824
2825
|
item.invalid = true;
|
|
2825
2826
|
this.completeAsyncTask(err, localUri);
|
|
2826
2827
|
return;
|
|
2827
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
|
+
}
|
|
2828
2836
|
if (fetched) {
|
|
2829
2837
|
fetchedAssets.push(item);
|
|
2830
2838
|
}
|
|
@@ -2865,7 +2873,7 @@ class FileManager extends core_1.Host {
|
|
|
2865
2873
|
const type = item.incremental;
|
|
2866
2874
|
let valid = cacheOpen;
|
|
2867
2875
|
if (valid) {
|
|
2868
|
-
if (type === false || type ===
|
|
2876
|
+
if (type === false || type === "none" /* INCREMENTAL.NONE */) {
|
|
2869
2877
|
valid = false;
|
|
2870
2878
|
}
|
|
2871
2879
|
}
|
|
@@ -2878,9 +2886,9 @@ class FileManager extends core_1.Host {
|
|
|
2878
2886
|
}
|
|
2879
2887
|
}
|
|
2880
2888
|
if (valid && filename) {
|
|
2881
|
-
const etag = (cacheEtag && type !==
|
|
2889
|
+
const etag = (cacheEtag && type !== "exists" /* INCREMENTAL.EXISTS */ || type === "etag" /* INCREMENTAL.ETAG */) && !!uri;
|
|
2882
2890
|
if (etag && imported.some(file => file.imported.includes(uri))) {
|
|
2883
|
-
item.incremental =
|
|
2891
|
+
item.incremental = "none" /* INCREMENTAL.NONE */;
|
|
2884
2892
|
}
|
|
2885
2893
|
else if (!(0, types_1.mainFlag)(item.flags)) {
|
|
2886
2894
|
let cached;
|
|
@@ -2896,7 +2904,7 @@ class FileManager extends core_1.Host {
|
|
|
2896
2904
|
if ((!etag || item.fetchType === 1 /* FETCH_TYPE.HTTP */ && ((0, types_1.isEmpty)(bundleId) || bundleIndex <= 0)) && (!watch || !item.watch || setBuffer(item))) {
|
|
2897
2905
|
let childBundle, childDownload;
|
|
2898
2906
|
if (!(0, types_1.isEmpty)(bundleId) && bundleIndex > 0) {
|
|
2899
|
-
const target = assets.find(parent => parent.
|
|
2907
|
+
const target = assets.find(parent => parent.bundleIndex === 0 && parent.bundleId === bundleId);
|
|
2900
2908
|
if (target) {
|
|
2901
2909
|
if ((0, types_1.existsFlag)(target.flags)) {
|
|
2902
2910
|
setBuffer(item);
|
|
@@ -2929,10 +2937,10 @@ class FileManager extends core_1.Host {
|
|
|
2929
2937
|
setBuffer(item);
|
|
2930
2938
|
}
|
|
2931
2939
|
const checksumOutput = item.checksumOutput;
|
|
2932
|
-
if (core_1.Host.isPath(localUri) && (!checksumOutput || checkHash(localUri,
|
|
2940
|
+
if (core_1.Host.isPath(localUri) && (!checksumOutput || checkHash.call(this, localUri, true, checksumOutput))) {
|
|
2933
2941
|
item.flags |= 128 /* ASSET_FLAG.EXISTS */;
|
|
2934
2942
|
if (!etag || item.fetchType !== 1 /* FETCH_TYPE.HTTP */ || checksumOutput) {
|
|
2935
|
-
if (!(0, types_1.isEmpty)(bundleId)
|
|
2943
|
+
if (bundleIndex === 0 && !(0, types_1.isEmpty)(bundleId)) {
|
|
2936
2944
|
assets.filter(child => child.bundleId === bundleId && child.bundleIndex > 0).forEach(child => {
|
|
2937
2945
|
setBuffer(child);
|
|
2938
2946
|
child.invalid = true;
|
|
@@ -2947,14 +2955,14 @@ class FileManager extends core_1.Host {
|
|
|
2947
2955
|
else if (cached) {
|
|
2948
2956
|
try {
|
|
2949
2957
|
const buffer = cached[1];
|
|
2950
|
-
if (!item.checksum || checkHash(
|
|
2958
|
+
if (!item.checksum || checkHash.call(this, localUri, false, item.checksum, buffer)) {
|
|
2951
2959
|
fs.writeFileSync(localUri, buffer);
|
|
2952
2960
|
if (item.willChange && Buffer.isBuffer(buffer)) {
|
|
2953
2961
|
item.buffer = buffer;
|
|
2954
2962
|
}
|
|
2955
|
-
if (!(checksumOutput && (0, types_1.isEmpty)(bundleId) && checkHash(
|
|
2963
|
+
if (!(checksumOutput && (0, types_1.isEmpty)(bundleId) && checkHash.call(this, localUri, true, checksumOutput, buffer))) {
|
|
2956
2964
|
this.performAsyncTask();
|
|
2957
|
-
fileReceived(item, localUri, null, true);
|
|
2965
|
+
fileReceived(item, localUri, null, true, false, true);
|
|
2958
2966
|
}
|
|
2959
2967
|
continue;
|
|
2960
2968
|
}
|
|
@@ -2985,7 +2993,8 @@ class FileManager extends core_1.Host {
|
|
|
2985
2993
|
if (downloadable.size > 1 && this.Request.httpVersion !== 1) {
|
|
2986
2994
|
for (const { url } of downloadable.keys()) {
|
|
2987
2995
|
if (url?.protocol === 'https:') {
|
|
2988
|
-
|
|
2996
|
+
const count = originCount[url.origin];
|
|
2997
|
+
originCount[url.origin] = count ? count + 1 : 1;
|
|
2989
2998
|
}
|
|
2990
2999
|
}
|
|
2991
3000
|
}
|
|
@@ -2994,6 +3003,7 @@ class FileManager extends core_1.Host {
|
|
|
2994
3003
|
break;
|
|
2995
3004
|
}
|
|
2996
3005
|
const { pathname, localUri } = download;
|
|
3006
|
+
const uri = item.uri;
|
|
2997
3007
|
if (item.content) {
|
|
2998
3008
|
if (!checkQueue(item, localUri, pathname, true)) {
|
|
2999
3009
|
const content = item.content;
|
|
@@ -3001,8 +3011,9 @@ class FileManager extends core_1.Host {
|
|
|
3001
3011
|
this.performAsyncTask();
|
|
3002
3012
|
fs.writeFile(localUri, content, item.encoding || (item.encoding = 'utf-8'), err => fileReceived(item, localUri, err));
|
|
3003
3013
|
}
|
|
3014
|
+
continue;
|
|
3004
3015
|
}
|
|
3005
|
-
|
|
3016
|
+
if (item.dataView) {
|
|
3006
3017
|
if (createFolder(item, pathname)) {
|
|
3007
3018
|
this.performAsyncTask();
|
|
3008
3019
|
const dataView = item.dataView;
|
|
@@ -3013,117 +3024,124 @@ class FileManager extends core_1.Host {
|
|
|
3013
3024
|
fileReceived(item, localUri, err, false, true);
|
|
3014
3025
|
});
|
|
3015
3026
|
}
|
|
3027
|
+
continue;
|
|
3016
3028
|
}
|
|
3017
|
-
|
|
3029
|
+
if (item.base64) {
|
|
3018
3030
|
if (createFolder(item, pathname)) {
|
|
3019
3031
|
this.performAsyncTask();
|
|
3020
3032
|
fs.writeFile(localUri, item.base64, 'base64', err => fileReceived(item, localUri, err, false, true));
|
|
3021
3033
|
}
|
|
3034
|
+
continue;
|
|
3022
3035
|
}
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
|
|
3028
|
-
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
if (
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
|
|
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] = [];
|
|
3044
3062
|
}
|
|
3045
|
-
|
|
3046
|
-
|
|
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 */);
|
|
3047
3069
|
}
|
|
3048
|
-
|
|
3049
|
-
|
|
3050
|
-
|
|
3070
|
+
const closeResponse = () => client?.destroy();
|
|
3071
|
+
const downloadUri = (request, etagDir) => {
|
|
3072
|
+
if (checkEtag) {
|
|
3073
|
+
item.flags &= ~128 /* ASSET_FLAG.EXISTS */;
|
|
3051
3074
|
}
|
|
3052
|
-
|
|
3053
|
-
|
|
3075
|
+
closeResponse();
|
|
3076
|
+
const location = request.url.toString();
|
|
3077
|
+
request.encoding = encoding;
|
|
3078
|
+
request.pipeTo = localUri;
|
|
3079
|
+
request.statusMessage = location + (bundleMain ? ' (0)' : '');
|
|
3054
3080
|
if (isHttp) {
|
|
3055
|
-
|
|
3056
|
-
|
|
3057
|
-
|
|
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;
|
|
3058
3094
|
}
|
|
3059
|
-
|
|
3060
|
-
|
|
3061
|
-
if (
|
|
3062
|
-
item.
|
|
3095
|
+
this.fetchBuffer(request.url, request)
|
|
3096
|
+
.then(data => {
|
|
3097
|
+
if (typeof data === 'string') {
|
|
3098
|
+
item.sourceUTF8 = data;
|
|
3063
3099
|
}
|
|
3064
|
-
|
|
3065
|
-
|
|
3066
|
-
request.encoding = encoding;
|
|
3067
|
-
request.pipeTo = localUri;
|
|
3068
|
-
request.statusMessage = location + (bundleMain ? ' (0)' : '');
|
|
3069
|
-
if (isHttp) {
|
|
3070
|
-
request.method = 'GET';
|
|
3071
|
-
request.httpVersion = (originCount[request.url.origin] || 0) <= 1 ? 1 : undefined;
|
|
3072
|
-
request.connected = headers => {
|
|
3073
|
-
applyHeaders(item, headers, true, mainEtag);
|
|
3074
|
-
return item.willChange || !!etagDir && (cacheDir || !item.contentLength || cacheToMemory.within(item.contentLength));
|
|
3075
|
-
};
|
|
3100
|
+
else if (data) {
|
|
3101
|
+
item.buffer = data;
|
|
3076
3102
|
}
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
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
|
+
});
|
|
3080
3113
|
}
|
|
3081
|
-
|
|
3082
|
-
|
|
3083
|
-
|
|
3084
|
-
|
|
3085
|
-
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
|
|
3112
|
-
const statusCode = res.statusCode;
|
|
3113
|
-
if (statusCode < 300 /* HTTP_STATUS.MULTIPLE_CHOICES */) {
|
|
3114
|
-
const etag = applyHeaders(item, res.headers, false, mainEtag);
|
|
3115
|
-
let tempDir, etagDir;
|
|
3116
|
-
if (etag) {
|
|
3117
|
-
if (cacheDir) {
|
|
3118
|
-
tempDir = this.getCacheDir(href);
|
|
3119
|
-
}
|
|
3120
|
-
etagDir = encodeURIComponent(etag);
|
|
3121
|
-
if (incremental !== false || item.incremental === true) {
|
|
3122
|
-
const location = href.toString();
|
|
3123
|
-
const cached = MEMORY.CACHE[location];
|
|
3124
|
-
let buffer;
|
|
3125
|
-
if (cached) {
|
|
3126
|
-
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 {
|
|
3127
3145
|
if (checkEtag) {
|
|
3128
3146
|
this.completeAsyncTask(null, localUri);
|
|
3129
3147
|
closeResponse();
|
|
@@ -3134,194 +3152,209 @@ class FileManager extends core_1.Host {
|
|
|
3134
3152
|
item.encoding = encoding;
|
|
3135
3153
|
cached[0] = Date.now();
|
|
3136
3154
|
}
|
|
3137
|
-
else {
|
|
3138
|
-
cacheToMemory.clear(location);
|
|
3139
|
-
}
|
|
3140
3155
|
}
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
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
|
+
}
|
|
3148
3181
|
}
|
|
3149
|
-
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
cacheToMemory.add(location, etagDir, buffer, { encoding, contentLength: item.contentLength, tempFile: pipeAs });
|
|
3157
|
-
}
|
|
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();
|
|
3158
3189
|
}
|
|
3159
|
-
if (
|
|
3160
|
-
|
|
3161
|
-
|
|
3162
|
-
|
|
3163
|
-
|
|
3164
|
-
else {
|
|
3165
|
-
fs.copyFileSync(pipeAs, localUri);
|
|
3166
|
-
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;
|
|
3167
3195
|
}
|
|
3196
|
+
writeBuffer();
|
|
3168
3197
|
}
|
|
3169
|
-
|
|
3170
|
-
|
|
3198
|
+
else {
|
|
3199
|
+
fs.copyFileSync(pipeAs, localUri);
|
|
3200
|
+
this.addDownload(pipeAs, types_1.DOWNLOAD_TYPE.CACHE);
|
|
3171
3201
|
}
|
|
3172
|
-
fileReceived(item, localUri, null, true);
|
|
3173
3202
|
}
|
|
3174
|
-
|
|
3175
|
-
|
|
3203
|
+
if (item.willChange) {
|
|
3204
|
+
setBuffer();
|
|
3176
3205
|
}
|
|
3177
|
-
|
|
3178
|
-
return;
|
|
3206
|
+
fileReceived(item, localUri, null, true, false, true);
|
|
3179
3207
|
}
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
fs.writeFileSync(localUri, buffer);
|
|
3183
|
-
this.addDownload(Buffer.byteLength(buffer, encoding), types_1.DOWNLOAD_TYPE.CACHE);
|
|
3184
|
-
if (checkEtag) {
|
|
3185
|
-
item.flags &= ~128 /* ASSET_FLAG.EXISTS */;
|
|
3186
|
-
}
|
|
3187
|
-
fileReceived(item, localUri, null, true);
|
|
3188
|
-
closeResponse();
|
|
3189
|
-
if (pipeAs) {
|
|
3190
|
-
fs.writeFile(pipeAs, buffer, () => { });
|
|
3191
|
-
}
|
|
3192
|
-
return;
|
|
3208
|
+
else {
|
|
3209
|
+
this.completeAsyncTask(null, localUri);
|
|
3193
3210
|
}
|
|
3211
|
+
closeResponse();
|
|
3212
|
+
return;
|
|
3194
3213
|
}
|
|
3195
|
-
|
|
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;
|
|
3196
3227
|
}
|
|
3197
3228
|
}
|
|
3198
|
-
}
|
|
3199
|
-
downloadUri(target, etagDir);
|
|
3200
|
-
}
|
|
3201
|
-
else if (statusCode < 400 /* HTTP_STATUS.BAD_REQUEST */) {
|
|
3202
|
-
closeResponse();
|
|
3203
|
-
const location = res.headers.location;
|
|
3204
|
-
if (location && ++redirects <= HTTP_CLIENT.redirectLimit) {
|
|
3205
|
-
try {
|
|
3206
|
-
checkHeaders.call(this, new URL(request_1.default.fromURL(href, location)));
|
|
3207
|
-
return;
|
|
3208
|
-
}
|
|
3209
3229
|
catch {
|
|
3210
3230
|
}
|
|
3211
3231
|
}
|
|
3212
|
-
errorRequest(item, (0, types_1.errorMessage)(statusCode, 'Exceeded redirect limit', url.toString()));
|
|
3213
|
-
}
|
|
3214
|
-
else {
|
|
3215
|
-
downloadUri(target);
|
|
3216
3232
|
}
|
|
3217
|
-
|
|
3218
|
-
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
-
|
|
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;
|
|
3222
3242
|
}
|
|
3223
|
-
|
|
3224
|
-
errorRequest(item, err);
|
|
3243
|
+
catch {
|
|
3225
3244
|
}
|
|
3226
3245
|
}
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
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)) {
|
|
3230
3255
|
downloadUri(target);
|
|
3231
3256
|
}
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
|
|
3235
|
-
|
|
3236
|
-
}
|
|
3237
|
-
|
|
3238
|
-
|
|
3239
|
-
|
|
3240
|
-
|
|
3241
|
-
|
|
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));
|
|
3242
3275
|
}
|
|
3243
3276
|
}
|
|
3244
3277
|
}
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
|
|
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;
|
|
3248
3287
|
}
|
|
3249
|
-
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
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));
|
|
3253
3294
|
}
|
|
3254
|
-
|
|
3255
|
-
|
|
3256
|
-
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
|
|
3260
|
-
|
|
3261
|
-
|
|
3262
|
-
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
|
|
3273
|
-
|
|
3274
|
-
const ext = item.filename && path.extname(item.filename);
|
|
3275
|
-
let found;
|
|
3276
|
-
for (const file of result) {
|
|
3277
|
-
if (!found && ext === path.extname(file).toLowerCase()) {
|
|
3278
|
-
try {
|
|
3279
|
-
if (file !== localUri && !core_1.Host.renameFile(file, localUri)) {
|
|
3280
|
-
this.filesToRemove.add(file);
|
|
3281
|
-
}
|
|
3282
|
-
found = true;
|
|
3283
|
-
}
|
|
3284
|
-
catch (err) {
|
|
3285
|
-
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);
|
|
3286
3315
|
}
|
|
3316
|
+
found = true;
|
|
3287
3317
|
}
|
|
3288
|
-
|
|
3289
|
-
this.
|
|
3318
|
+
catch (err) {
|
|
3319
|
+
this.writeFail(["Unable to rename file" /* ERR_MESSAGE.RENAME_FILE */, item.filename], err, 32 /* LOG_TYPE.FILE */);
|
|
3290
3320
|
}
|
|
3291
3321
|
}
|
|
3292
|
-
if (found) {
|
|
3293
|
-
fetchedAssets.push(item);
|
|
3294
|
-
processQueue(item, localUri);
|
|
3295
|
-
}
|
|
3296
3322
|
else {
|
|
3297
|
-
|
|
3298
|
-
item.localUri = '';
|
|
3299
|
-
item.mimeType = '';
|
|
3300
|
-
clearQueue(processing, localUri);
|
|
3301
|
-
clearQueue(downloading, uri);
|
|
3302
|
-
this.completeAsyncTask();
|
|
3323
|
+
this.add(file, item, types_1.FILE_TYPE.TORRENT);
|
|
3303
3324
|
}
|
|
3304
3325
|
}
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
|
|
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));
|
|
3308
3341
|
}
|
|
3309
|
-
|
|
3310
|
-
|
|
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
|
+
});
|
|
3311
3355
|
}
|
|
3312
|
-
else
|
|
3313
|
-
|
|
3314
|
-
if (checkDest(uri)) {
|
|
3315
|
-
fs.copyFile(uri, localUri, err => {
|
|
3316
|
-
if (!err) {
|
|
3317
|
-
this.addDownload(localUri, types_1.DOWNLOAD_TYPE.DISK);
|
|
3318
|
-
}
|
|
3319
|
-
fileReceived(item, localUri, err);
|
|
3320
|
-
});
|
|
3321
|
-
}
|
|
3322
|
-
else {
|
|
3323
|
-
fileReceived(item, localUri);
|
|
3324
|
-
}
|
|
3356
|
+
else {
|
|
3357
|
+
fileReceived(item, localUri);
|
|
3325
3358
|
}
|
|
3326
3359
|
}
|
|
3327
3360
|
}
|
|
@@ -3478,10 +3511,10 @@ class FileManager extends core_1.Host {
|
|
|
3478
3511
|
for (const item of this.assets) {
|
|
3479
3512
|
if (item.checksumOutput && !ignoreAsset(item)) {
|
|
3480
3513
|
const localUri = item.localUri;
|
|
3481
|
-
if (!checkHash(localUri,
|
|
3514
|
+
if (!filesToRemove.has(localUri) && !checkHash.call(this, localUri, true, item.checksumOutput)) {
|
|
3482
3515
|
item.invalid = true;
|
|
3483
3516
|
filesToRemove.add(localUri);
|
|
3484
|
-
this.writeFail(["
|
|
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 });
|
|
3485
3518
|
}
|
|
3486
3519
|
}
|
|
3487
3520
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/file-manager",
|
|
3
|
-
"version": "0.8.
|
|
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.
|
|
24
|
-
"@e-mc/compress": "0.8.
|
|
25
|
-
"@e-mc/core": "0.8.
|
|
26
|
-
"@e-mc/document": "0.8.
|
|
27
|
-
"@e-mc/image": "0.8.
|
|
28
|
-
"@e-mc/request": "0.8.
|
|
29
|
-
"@e-mc/task": "0.8.
|
|
30
|
-
"@e-mc/types": "0.8.
|
|
31
|
-
"@e-mc/watch": "0.8.
|
|
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
|
}
|