Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(tools): extract Pi tarballs with GNU tar (#1712)

天魔aaa4ed5ad6af
diff --git a/scripts/__tests__/pi-update-layout.test.mjs b/scripts/__tests__/pi-update-layout.test.mjs
index 8ad99ea9fa..6565813308 100644
--- a/scripts/__tests__/pi-update-layout.test.mjs
+++ b/scripts/__tests__/pi-update-layout.test.mjs
@@ -1,4 +1,5 @@
import assert from 'node:assert/strict';
+import { spawnSync } from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
@@ -7,6 +8,7 @@ import test from 'node:test';
import {
assertPinnedRuntimeAsset,
assetDigestMatchesUpstream,
+ extractArchive,
flattenExtractedDir,
readCachedAssetDigest,
} from '../../tools/pi/update.mjs';
@@ -37,6 +39,50 @@ test('Pi updater still flattens the nested Unix release layout', (t) => {
assert.ok(fs.statSync(path.join(dir, 'theme')).isDirectory());
});
+test('Pi updater extracts tar.gz archives streamed to the system tar', async (t) => {
+ const root = tempDir();
+ t.after(() => fs.rmSync(root, { recursive: true, force: true }));
+ const inputDir = path.join(root, 'input');
+ const outputDir = path.join(root, 'output');
+ fs.mkdirSync(path.join(inputDir, 'pi'), { recursive: true });
+ fs.mkdirSync(outputDir);
+ fs.writeFileSync(path.join(inputDir, 'pi', 'pi'), 'pi-fixture');
+
+ const created = spawnSync('tar', ['-czf', 'fixture.tar.gz', '-C', 'input', 'pi'], {
+ cwd: root,
+ encoding: 'utf8',
+ });
+ assert.equal(created.status, 0, created.stderr || created.error?.message);
+
+ await extractArchive(path.join(root, 'fixture.tar.gz'), outputDir);
+
+ assert.equal(fs.readFileSync(path.join(outputDir, 'pi', 'pi'), 'utf8'), 'pi-fixture');
+});
+
+test('Pi updater rejects unreadable archives through the returned promise', async (t) => {
+ const root = tempDir();
+ t.after(() => fs.rmSync(root, { recursive: true, force: true }));
+ const outputDir = path.join(root, 'output');
+ fs.mkdirSync(outputDir);
+
+ await assert.rejects(
+ extractArchive(path.join(root, 'missing.tar.gz'), outputDir),
+ { code: 'ENOENT' },
+ );
+});
+
+test('Pi updater rejects corrupt streamed archives', async (t) => {
+ const root = tempDir();
+ t.after(() => fs.rmSync(root, { recursive: true, force: true }));
+ const archive = path.join(root, 'corrupt.tar.gz');
+ const outputDir = path.join(root, 'output');
+ // Keep the source multi-chunk so the pipeline also covers tar closing before all input is written.
+ fs.writeFileSync(archive, Buffer.alloc(4 * 1024 * 1024, 0x61));
+ fs.mkdirSync(outputDir);
+
+ await assert.rejects(extractArchive(archive, outputDir));
+});
+
test('Pi release pin covers every supported desktop architecture', () => {
const pin = JSON.parse(fs.readFileSync(new URL('../../tools/pi/latest.json', import.meta.url), 'utf8'));
assert.deepEqual(Object.keys(pin.runtimeAssets).sort(), [
diff --git a/tools/pi/update.mjs b/tools/pi/update.mjs
index e17745a783..e51913bb43 100644
--- a/tools/pi/update.mjs
+++ b/tools/pi/update.mjs
@@ -27,6 +27,7 @@ import { spawn } from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
+import { pipeline } from 'node:stream/promises';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { fetchJsonWithTimeout, downloadToFileWithTimeout, createDownloadProgressLogger } from '../shared/fetch-with-timeout.mjs';
@@ -202,14 +203,22 @@ function targetsExist(version, targets) {
});
}
-/** 用 bsdtar 解压归档(tar.gz / zip 皆可识别)到 destDir。 */
-async function extractArchive(archivePath, destDir) {
- return new Promise((resolve, reject) => {
- const child = spawn('tar', ['-xf', '-'], { cwd: destDir, stdio: ['pipe', 'inherit', 'inherit'] });
- child.on('error', reject);
- child.on('close', (code) => (code === 0 ? resolve() : reject(new Error(`tar exited with code ${code}`))));
- fs.createReadStream(archivePath).pipe(child.stdin);
+/** 用 tar 解压归档到 destDir;GNU tar 从 stdin 读取 gzip 时必须显式传 -z。 */
+export async function extractArchive(archivePath, destDir) {
+ const args = archivePath.endsWith('.tar.gz') ? ['-xzf', '-'] : ['-xf', '-'];
+ const child = spawn('tar', args, { cwd: destDir, stdio: ['pipe', 'inherit', 'inherit'] });
+ const exit = new Promise((resolve, reject) => {
+ child.once('error', reject);
+ child.once('close', (code) => (code === 0 ? resolve() : reject(new Error(`tar exited with code ${code}`))));
});
+ const input = pipeline(fs.createReadStream(archivePath), child.stdin);
+ try {
+ await Promise.all([input, exit]);
+ } catch (error) {
+ if (child.exitCode === null && child.signalCode === null) child.kill();
+ await Promise.allSettled([input, exit]);
+ throw error;
+ }
}
/**