Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Fix incomplete macOS native preparation (#5362)
Nick Gomez13b832b8460b
diff --git a/packages/desktop/scripts/prepare-platform-natives.mjs b/packages/desktop/scripts/prepare-platform-natives.mjsindex 763d91497..abd42e7ae 100644--- a/packages/desktop/scripts/prepare-platform-natives.mjs+++ b/packages/desktop/scripts/prepare-platform-natives.mjs@@ -346,11 +346,8 @@ async function prepareOne({export async function prepareNatives({repoRoot,platform = process.platform,- registry = process.env.npm_config_registry || DEFAULT_REGISTRY,- fetchImpl = fetch,- extract = extractWithTar(defaultTarBin(platform)),- checkOnly = false,log = console.log,+ ...options}) {const suffixes = PLATFORM_PACKAGES[platform];if (!suffixes) {@@ -367,6 +364,21 @@ export async function prepareNatives({);}const version = JSON.parse(readFileSync(wrapperPkgJson, 'utf8')).version;+ return prepareKeyringNatives({ ...options, repoRoot, platform, log, version, suffixes });+}++export async function prepareKeyringNatives({+ repoRoot,+ version,+ suffixes,+ platform = process.platform,+ registry = process.env.npm_config_registry || DEFAULT_REGISTRY,+ fetchImpl = fetch,+ extract = extractWithTar(defaultTarBin(platform)),+ checkOnly = false,+ log = console.log,+}) {+ const napiDir = join(repoRoot, 'node_modules', '@napi-rs');const lockfile = readFileSync(join(repoRoot, 'pnpm-lock.yaml'), 'utf8');log(`${LOG_PREFIX} target version: @napi-rs/keyring-* v${version}`);log(`${LOG_PREFIX} @napi-rs root: ${napiDir}`);diff --git a/packages/desktop/scripts/prepare-universal.mjs b/packages/desktop/scripts/prepare-universal.mjsindex c30deffde..8f79b5f9a 100644--- a/packages/desktop/scripts/prepare-universal.mjs+++ b/packages/desktop/scripts/prepare-universal.mjs@@ -1,11 +1,8 @@#!/usr/bin/env node-import { execFileSync } from 'node:child_process';-import { createWriteStream, existsSync, mkdirSync, readFileSync, rmSync } from 'node:fs';-import { tmpdir } from 'node:os';+import { existsSync, readFileSync } from 'node:fs';import { dirname, join, resolve } from 'node:path';-import { Readable } from 'node:stream';-import { pipeline } from 'node:stream/promises';import { fileURLToPath } from 'node:url';+import { NativesError, prepareKeyringNatives } from './prepare-platform-natives.mjs';if (process.platform !== 'darwin') {console.log(`[prepare-universal] platform=${process.platform} — no-op (darwin-only).`);@@ -29,45 +26,17 @@ if (!existsSync(hostPkgJson)) {}const version = JSON.parse(readFileSync(hostPkgJson, 'utf8')).version;-console.log(`[prepare-universal] target version: @napi-rs/keyring-darwin-* v${version}`);-console.log(`[prepare-universal] @napi-rs root: ${NAPI_DIR}`);--for (const arch of ARCHES) {- const pkgName = `@napi-rs/keyring-${arch}`;- const targetDir = join(NAPI_DIR, `keyring-${arch}`);- const targetPkgJson = join(targetDir, 'package.json');-- if (existsSync(targetPkgJson)) {- const installed = JSON.parse(readFileSync(targetPkgJson, 'utf8'));- if (installed.version === version) {- console.log(`[prepare-universal] ${pkgName}@${version} present — skip`);- continue;- }- console.log(- `[prepare-universal] ${pkgName} version mismatch (have=${installed.version}, want=${version}) — re-extracting`,- );- rmSync(targetDir, { recursive: true, force: true });- } else {- console.log(`[prepare-universal] ${pkgName}@${version} missing — fetching`);- }-- const tarballUrl = `https://registry.npmjs.org/${pkgName}/-/keyring-${arch}-${version}.tgz`;- const tmpTarball = join(tmpdir(), `keyring-${arch}-${version}-${process.pid}.tgz`);-- const res = await fetch(tarballUrl);- if (!res.ok) {- console.error(`[prepare-universal] fetch ${tarballUrl} → ${res.status} ${res.statusText}`);- process.exit(1);- }- await pipeline(Readable.fromWeb(res.body), createWriteStream(tmpTarball));-- mkdirSync(targetDir, { recursive: true });- execFileSync('tar', ['-xzf', tmpTarball, '-C', targetDir, '--strip-components=1'], {- stdio: 'inherit',+try {+ await prepareKeyringNatives({+ repoRoot: REPO_ROOT,+ version,+ suffixes: ARCHES,+ registry: 'https://registry.npmjs.org/',});- rmSync(tmpTarball, { force: true });-- console.log(`[prepare-universal] extracted ${pkgName}@${version} → ${targetDir}`);+ console.log('[prepare-universal] both darwin arches present; universal merge unblocked.');+} catch (error) {+ console.error(+ `[prepare-universal] ${error instanceof NativesError ? error.message : error.stack}`,+ );+ process.exitCode = 1;}--console.log('[prepare-universal] both darwin arches present; universal merge unblocked.');diff --git a/packages/desktop/scripts/prepare-universal.test.mjs b/packages/desktop/scripts/prepare-universal.test.mjsnew file mode 100644index 000000000..b29e4bbb4--- /dev/null+++ b/packages/desktop/scripts/prepare-universal.test.mjs@@ -0,0 +1,178 @@+import { execFileSync, spawn } from 'node:child_process';+import { createHash } from 'node:crypto';+import {+ copyFileSync,+ existsSync,+ mkdirSync,+ mkdtempSync,+ readFileSync,+ rmSync,+ statSync,+ symlinkSync,+ writeFileSync,+} from 'node:fs';+import { createServer } from 'node:http';+import { tmpdir } from 'node:os';+import { dirname, join } from 'node:path';+import { fileURLToPath } from 'node:url';+import { afterEach, expect, test } from 'vitest';++const scripts = dirname(fileURLToPath(import.meta.url));+const version = '1.3.0';+const payload = Buffer.from('complete native payload');+const resources = [];++afterEach(async () => {+ for (const { root, server } of resources.splice(0)) {+ await new Promise((resolve) => server.close(resolve));+ rmSync(root, { recursive: true, force: true });+ }+});++async function fixture({+ unavailable = false,+ missing = false,+ hostArch = 'arm64',+ targetVersion = version,+ tampered = false,+} = {}) {+ const targetArch = hostArch === 'arm64' ? 'x64' : 'arm64';+ const tarballPath = `/@napi-rs/keyring-darwin-${targetArch}/-/keyring-darwin-${targetArch}-${version}.tgz`;+ const root = mkdtempSync(join(tmpdir(), 'prepare-universal-'));+ const server = createServer((request, response) => {+ if (unavailable) response.writeHead(503, 'Service Unavailable').end();+ else if (request.url !== tarballPath) response.writeHead(404, 'Not Found').end();+ else response.writeHead(200).end(tampered ? Buffer.from('different archive') : archive);+ });+ resources.push({ root, server });+ const targetScripts = join(root, 'packages', 'desktop', 'scripts');+ mkdirSync(targetScripts, { recursive: true });+ for (const name of ['prepare-universal.mjs', 'prepare-platform-natives.mjs']) {+ copyFileSync(join(scripts, name), join(targetScripts, name));+ }+ const napi = join(root, 'node_modules', '@napi-rs');+ mkdirSync(join(napi, 'keyring'), { recursive: true });+ writeFileSync(join(napi, 'keyring', 'package.json'), JSON.stringify({ version }));+ const host = join(+ root,+ 'node_modules',+ '.pnpm',+ `@napi-rs+keyring-darwin-${hostArch}@${version}`,+ 'node_modules',+ '@napi-rs',+ `keyring-darwin-${hostArch}`,+ );+ mkdirSync(host, { recursive: true });+ writeFileSync(join(host, 'package.json'), JSON.stringify({ version }));+ writeFileSync(join(host, `keyring.darwin-${hostArch}.node`), payload);+ symlinkSync(host, join(napi, `keyring-darwin-${hostArch}`), 'junction');+ const target = join(napi, `keyring-darwin-${targetArch}`);+ const binary = join(target, `keyring.darwin-${targetArch}.node`);+ mkdirSync(target);+ writeFileSync(join(target, 'package.json'), JSON.stringify({ version: targetVersion }));+ if (!missing) writeFileSync(binary, payload.subarray(0, 3));+ const archiveRoot = join(root, 'archive');+ mkdirSync(join(archiveRoot, 'package'), { recursive: true });+ writeFileSync(join(archiveRoot, 'package', 'package.json'), JSON.stringify({ version }));+ writeFileSync(join(archiveRoot, 'package', `keyring.darwin-${targetArch}.node`), payload);+ const archivePath = join(root, 'native.tgz');+ execFileSync('tar', ['--format=ustar', '-czf', archivePath, '-C', archiveRoot, 'package'], {+ env: { ...process.env, COPYFILE_DISABLE: '1' },+ });+ const archive = readFileSync(archivePath);+ const integrity = `sha512-${createHash('sha512').update(archive).digest('base64')}`;+ writeFileSync(+ join(root, 'pnpm-lock.yaml'),+ `packages:\n '@napi-rs/keyring-darwin-${targetArch}@${version}':\n resolution: {integrity: ${integrity}}\n`,+ );+ await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));+ const fixtureRegistry = `http://127.0.0.1:${server.address().port}/`;+ const preload = join(root, 'platform.mjs');+ writeFileSync(+ preload,+ `Object.defineProperty(process, 'platform', { value: 'darwin' });+Object.defineProperty(process, 'arch', { value: ${JSON.stringify(hostArch)} });+const fetchFromNetwork = globalThis.fetch;+globalThis.fetch = (url, options) => {+ const requested = new URL(url);+ if (requested.origin !== 'https://registry.npmjs.org') {+ return Promise.reject(new Error('unexpected registry ' + requested.origin));+ }+ return fetchFromNetwork(new URL(requested.pathname, ${JSON.stringify(fixtureRegistry)}), options);+};+`,+ );+ return { root, target, binary, preload };+}++async function run({ root, preload }) {+ const child = spawn(+ process.execPath,+ ['--import', preload, join(root, 'packages', 'desktop', 'scripts', 'prepare-universal.mjs')],+ {+ cwd: root,+ env: { ...process.env, npm_config_registry: 'https://registry.invalid/' },+ stdio: ['ignore', 'pipe', 'pipe'],+ },+ );+ let stdout = '';+ let stderr = '';+ child.stdout.on('data', (data) => {+ stdout += data;+ });+ child.stderr.on('data', (data) => {+ stderr += data;+ });+ const code = await new Promise((resolve, reject) => {+ child.once('error', reject);+ child.once('close', resolve);+ });+ return { code, stdout, stderr };+}++test('fails when a partial native cannot be restored', async () => {+ const input = await fixture({ unavailable: true });+ const result = await run(input);+ expect(result.code, result.stdout + result.stderr).not.toBe(0);+ expect(result.stderr).toContain('HTTP 503');+ expect(readFileSync(input.binary)).toEqual(payload.subarray(0, 3));+});++test.each([+ { hostArch: 'arm64', missing: false },+ { hostArch: 'arm64', missing: true },+ { hostArch: 'x64', missing: false },+ { hostArch: 'x64', missing: true },+])(+ 'restores an incomplete native and leaves it untouched on rerun (host=$hostArch, missing=$missing)',+ async (options) => {+ const input = await fixture(options);+ const { binary } = input;+ const result = await run(input);+ expect(result.code, result.stdout + result.stderr).toBe(0);+ expect(existsSync(binary)).toBe(true);+ expect(readFileSync(binary)).toEqual(payload);+ const before = statSync(binary);+ const repeated = await run(input);+ expect(repeated.code, repeated.stdout + repeated.stderr).toBe(0);+ expect(statSync(binary)).toMatchObject({ ino: before.ino, mtimeMs: before.mtimeMs });+ },+);++test('updates an older native package to the host version', async () => {+ const input = await fixture({ targetVersion: '1.2.0' });+ const result = await run(input);+ expect(result.code, result.stdout + result.stderr).toBe(0);+ expect(JSON.parse(readFileSync(join(input.target, 'package.json'), 'utf8')).version).toBe(+ version,+ );+ expect(readFileSync(input.binary)).toEqual(payload);+});++test('refuses an archive that differs from the lockfile before replacing the partial target', async () => {+ const input = await fixture({ tampered: true });+ const result = await run(input);+ expect(result.code, result.stdout + result.stderr).not.toBe(0);+ expect(result.stderr).toContain('sha512 hash mismatch');+ expect(readFileSync(input.binary)).toEqual(payload.subarray(0, 3));+});