Comparing version 1.4.0 to 1.4.1
{ | ||
"name": "depsync", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"author": "Dom Chen", | ||
@@ -27,2 +27,3 @@ "homepage": "https://github.com/domchen/depsync", | ||
"adm-zip": "latest", | ||
"compressjs": "latest", | ||
"follow-redirects": "latest", | ||
@@ -29,0 +30,0 @@ "progress": "latest" |
@@ -72,2 +72,3 @@ ////////////////////////////////////////////////////////////////////////////////////// | ||
} | ||
item.timeout = item.timeout ? parseInt(item.timeout) : 15000; // Default to 15000 ms if not specified | ||
list.push(item); | ||
@@ -177,2 +178,2 @@ } | ||
exports.findConfigFile = findConfigFile; | ||
exports.parse = parse; | ||
exports.parse = parse; |
@@ -33,3 +33,4 @@ ////////////////////////////////////////////////////////////////////////////////////// | ||
const ProgressBar = require("progress"); | ||
const Utils = require('../Utils') | ||
const Utils = require('../Utils'); | ||
const Compress = require('compressjs'); | ||
@@ -45,4 +46,102 @@ | ||
function parseTar(buffer, outputDir) { | ||
let offset = 0; | ||
const BLOCK_SIZE = 512; | ||
let topLevelDir = null; | ||
let isFirstFile = true; | ||
while (offset < buffer.length) { | ||
const header = buffer.slice(offset, offset + BLOCK_SIZE); | ||
offset += BLOCK_SIZE; | ||
// Check if it's an empty block, two consecutive empty blocks indicate the end | ||
const isEnd = header.every(byte => byte === 0); | ||
if (isEnd) break; | ||
// Parse file name | ||
let fileName = ''; | ||
for (let i = 0; i < 100; i++) { | ||
if (header[i] === 0) break; | ||
fileName += String.fromCharCode(header[i]); | ||
} | ||
// Parse file size (Octal) | ||
let sizeOctal = ''; | ||
for (let i = 124; i < 124 + 12; i++) { | ||
if (header[i] === 0) break; | ||
sizeOctal += String.fromCharCode(header[i]); | ||
} | ||
const fileSize = parseInt(sizeOctal.trim(), 8); | ||
// Parse file type | ||
const typeFlag = String.fromCharCode(header[156]); | ||
// Ignore non-regular files | ||
if (typeFlag !== '0' && typeFlag !== '\0') { | ||
// Skip the data part of the file | ||
offset += Math.ceil(fileSize / BLOCK_SIZE) * BLOCK_SIZE; | ||
continue; | ||
} | ||
// Extract file data | ||
const fileData = buffer.slice(offset, offset + fileSize); | ||
offset += Math.ceil(fileSize / BLOCK_SIZE) * BLOCK_SIZE; | ||
// Handle file path, remove top-level directory | ||
if (isFirstFile) { | ||
const parts = fileName.split('/'); | ||
if (parts.length > 1) { | ||
topLevelDir = parts[0]; | ||
} | ||
isFirstFile = false; | ||
} | ||
let relativePath = fileName; | ||
if (topLevelDir) { | ||
const prefix = `${topLevelDir}/`; | ||
if (fileName.startsWith(prefix)) { | ||
relativePath = fileName.slice(prefix.length); | ||
} | ||
} | ||
// If the relative path is empty (i.e., the top-level directory itself), skip it | ||
if (!relativePath) continue; | ||
// Create directory structure | ||
const fullPath = path.join(outputDir, relativePath); | ||
const dirName = path.dirname(fullPath); | ||
if (!fs.existsSync(dirName)) { | ||
fs.mkdirSync(dirName, { recursive: true }); | ||
} | ||
// Write file | ||
fs.writeFileSync(fullPath, fileData); | ||
console.log(`Extracted file: ${fullPath}`); | ||
} | ||
} | ||
// Main decompression function | ||
function decompressTarBz2Sync(inputPath, outputDir) { | ||
// Read .tar.bz2 file | ||
const compressedData = fs.readFileSync(inputPath); | ||
// Decompress Bzip2 | ||
const decompressedData = Compress.Bzip2.decompressFile(compressedData); | ||
// Convert decompressed data to Buffer | ||
const tarBuffer = Buffer.from(decompressedData); | ||
// Parse and extract TAR | ||
parseTar(tarBuffer, outputDir); | ||
// Delete original file | ||
Utils.deletePath(inputPath); | ||
} | ||
function unzipFile(filePath, dir) { | ||
Utils.log("Unzipping: " + filePath); | ||
// Handle bz2 files separately | ||
if (filePath.endsWith('.tar.bz2')) { | ||
decompressTarBz2Sync(filePath, dir); | ||
return; | ||
} | ||
let zip = new AdmZip(filePath); | ||
@@ -84,3 +183,3 @@ let entries = zip.getEntries(); | ||
function loadMultiParts(urls, filePath, callback) { | ||
function loadMultiParts(urls, filePath, timeout, callback) { | ||
if (urls.length === 0) { | ||
@@ -91,3 +190,3 @@ callback && callback(); | ||
let url = urls.shift(); | ||
loadSingleFile(url, filePath, function (error) { | ||
loadSingleFile(url, filePath, timeout, function (error) { | ||
if (error) { | ||
@@ -97,10 +196,10 @@ callback && callback(error); | ||
} | ||
loadMultiParts(urls, filePath, callback); | ||
loadMultiParts(urls, filePath, timeout, callback); | ||
}, {flags: 'a'}); | ||
} | ||
function loadSingleFile(url, filePath, callback, options) { | ||
function loadSingleFile(url, filePath, timeout, callback, options) { | ||
let retryTimes = 0; | ||
Utils.log("Downloading: " + url); | ||
loadSingleFileWithTimeOut(url, filePath, onFinish, options); | ||
loadSingleFileWithTimeOut(url, filePath, timeout, onFinish, options); | ||
@@ -111,3 +210,3 @@ function onFinish(error) { | ||
Utils.log("Downloading retry " + retryTimes + ": " + url); | ||
loadSingleFileWithTimeOut(url, filePath, onFinish, options); | ||
loadSingleFileWithTimeOut(url, filePath, timeout, onFinish, options); | ||
} else { | ||
@@ -119,3 +218,3 @@ callback(error); | ||
function loadSingleFileWithTimeOut(url, filePath, callback, options) { | ||
function loadSingleFileWithTimeOut(url, filePath, timeout, callback, options) { | ||
let httpClient = url.slice(0, 5) === 'https' ? https : http; | ||
@@ -162,3 +261,3 @@ try { | ||
}); | ||
request.setTimeout(15000, function () { | ||
request.setTimeout(timeout, function () { | ||
request.abort(); | ||
@@ -194,5 +293,5 @@ file.close(); | ||
} | ||
loadMultiParts(urls, filePath, onFinish); | ||
loadMultiParts(urls, filePath, item.timeout, onFinish); | ||
} else { | ||
loadSingleFile(item.url, filePath, onFinish); | ||
loadSingleFile(item.url, filePath, item.timeout, onFinish); | ||
} | ||
@@ -210,3 +309,4 @@ | ||
} catch (e) { | ||
Utils.error("Cannot unzip file: " + filePath); | ||
Utils.error("Cannot unzip file: " + filePath); | ||
console.error(e); | ||
process.exit(1); | ||
@@ -220,2 +320,2 @@ } | ||
module.exports = FileTask; | ||
module.exports = FileTask; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57318
1327
4
+ Addedcompressjs@latest
+ Addedamdefine@1.0.1(transitive)
+ Addedcommander@2.8.1(transitive)
+ Addedcompressjs@1.0.3(transitive)
+ Addedgraceful-readlink@1.0.1(transitive)