Fixed for complicated filesystems
Publish Library / Build NPM Project (push) Successful in 2m56s
Publish Library / Tag Version (push) Successful in 35s
Code review / review (pull_request) Successful in 1m12s

This commit is contained in:
2026-09-19 18:23:23 -04:00
parent 08ecefbe05
commit 11f90c74cb
+14 -3
View File
@@ -46,22 +46,24 @@ function download(url, dest, redirectsLeft = MAX_REDIRECTS) {
const req = https.get(url, {agent}, res => { const req = https.get(url, {agent}, res => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
res.resume(); // discard body res.resume();
file.close(); file.close();
if (redirectsLeft <= 0) { if (redirectsLeft <= 0) {
reject(new Error(`Too many redirects while downloading ${url}`)); reject(new Error(`Too many redirects while downloading ${url}`));
return; return;
} }
// Location can be relative, so resolve it against the current URL.
const nextUrl = new URL(res.headers.location, url).toString(); const nextUrl = new URL(res.headers.location, url).toString();
resolve(download(nextUrl, dest, redirectsLeft - 1)); resolve(download(nextUrl, dest, redirectsLeft - 1));
return; return;
} }
if (!res.statusCode || res.statusCode >= 400) { if (!res.statusCode || res.statusCode >= 400) {
res.resume(); res.resume();
cleanupAndReject(new Error(`HTTP ${res.statusCode} - ${res.statusMessage}`)); cleanupAndReject(new Error(`HTTP ${res.statusCode} - ${res.statusMessage}`));
return; return;
} }
res.pipe(file); res.pipe(file);
file.on('finish', () => { file.on('finish', () => {
file.close(resolve); file.close(resolve);
@@ -85,12 +87,19 @@ async function flattenExtractedDir(srcDir, destDir) {
for (const entry of entries) { for (const entry of entries) {
const src = path.join(sourceDir, entry.name); const src = path.join(sourceDir, entry.name);
const dest = path.join(destDir, entry.name); const dest = path.join(destDir, entry.name);
await fs.promises.rename(src, dest);
await fs.promises.cp(src, dest, {
recursive: true,
force: true
});
await fs.promises.rm(src, {recursive: true, force: true});
} }
} }
async function extractTarGz(archivePath, destDir) { async function extractTarGz(archivePath, destDir) {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kiwix-extract-')); const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kiwix-extract-'));
await tar.extract({ await tar.extract({
file: archivePath, file: archivePath,
cwd: tmpDir cwd: tmpDir
@@ -103,6 +112,7 @@ async function extractTarGz(archivePath, destDir) {
async function extractZip(archivePath, destDir) { async function extractZip(archivePath, destDir) {
const zip = new AdmZip(archivePath); const zip = new AdmZip(archivePath);
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kiwix-extract-')); const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kiwix-extract-'));
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
zip.extractAllToAsync(tmpDir, true, false, err => { zip.extractAllToAsync(tmpDir, true, false, err => {
if (err) reject(err); if (err) reject(err);
@@ -159,6 +169,7 @@ async function install() {
if (entry.startsWith('kiwix-')) { if (entry.startsWith('kiwix-')) {
const fullPath = path.join(BIN_DIR, entry); const fullPath = path.join(BIN_DIR, entry);
const stat = await fs.promises.stat(fullPath); const stat = await fs.promises.stat(fullPath);
if (stat.isFile()) { if (stat.isFile()) {
await fs.promises.chmod(fullPath, 0o755); await fs.promises.chmod(fullPath, 0o755);
} }