feat(plugins): add JavaScript plugin system with overlay panel (#303)

This commit is contained in:
sudacode authored and GitHub committed 2026-10-31 19:09:54 -07:00
1 parent 90fc7cf940
commit a9a4c4f083
110 files changed
+5897 -344

No files matched your search

+1 -17
View File
@@ -1,22 +1,6 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import {
buildNotifySendEnv,
createNotifySendReplacer,
resolveDefaultNotificationIconPath,
} from './notification';
test('notify-send child environment drops the AppImage library-path override', () => {
const env = buildNotifySendEnv({
LD_LIBRARY_PATH: '/tmp/.mount_SubMinXXXXXX/usr/lib',
DBUS_SESSION_BUS_ADDRESS: 'unix:path=/run/user/1000/bus',
HOME: '/home/user',
});
assert.equal(env.LD_LIBRARY_PATH, undefined);
assert.equal(env.DBUS_SESSION_BUS_ADDRESS, 'unix:path=/run/user/1000/bus');
assert.equal(env.HOME, '/home/user');
});
import { createNotifySendReplacer, resolveDefaultNotificationIconPath } from './notification';
test('default notification icon resolves packaged SubMiner asset when no per-notification icon is provided', () => {
const path = resolveDefaultNotificationIconPath({
+2 -12
View File
@@ -3,6 +3,7 @@ import electron from 'electron';
import * as fs from 'fs';
import * as path from 'path';
import { createLogger } from '../../logger';
import { buildSystemBinaryEnv } from './system-binary-env';
const { Notification, nativeImage } = electron;
const logger = createLogger('core:notification');
@@ -203,19 +204,8 @@ export function createNotifySendReplacer(
};
}
/**
* Electron AppImages export `LD_LIBRARY_PATH=<mount>/usr/lib`, whose bundled libnotify predates the
* symbols the system notify-send links against, so an inherited environment kills the child with a
* symbol lookup error before it can send anything. A system binary resolves its own libraries fine,
* so the override is dropped entirely rather than filtered.
*/
export function buildNotifySendEnv(env: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv {
const { LD_LIBRARY_PATH: _dropped, ...rest } = env;
return rest;
}
const showLinuxReplaceableNotification = createNotifySendReplacer((args, callback) =>
execFile('notify-send', args, { timeout: 5_000, env: buildNotifySendEnv() }, (error, stdout) =>
execFile('notify-send', args, { timeout: 5_000, env: buildSystemBinaryEnv() }, (error, stdout) =>
callback(error, stdout ?? ''),
),
);
+16
View File
@@ -0,0 +1,16 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { buildSystemBinaryEnv } from './system-binary-env';
test('system binary environment drops the AppImage library-path override', () => {
const env = buildSystemBinaryEnv({
LD_LIBRARY_PATH: '/tmp/.mount_SubMinXXXXXX/usr/lib',
DBUS_SESSION_BUS_ADDRESS: 'unix:path=/run/user/1000/bus',
HOME: '/home/user',
});
assert.equal(env.LD_LIBRARY_PATH, undefined);
assert.equal(env.DBUS_SESSION_BUS_ADDRESS, 'unix:path=/run/user/1000/bus');
assert.equal(env.HOME, '/home/user');
});
+10
View File
@@ -0,0 +1,10 @@
/**
* Electron AppImages export `LD_LIBRARY_PATH=<mount>/usr/lib`, whose bundled libraries (libnotify and
* friends) predate the symbols system binaries link against, so an inherited environment can kill a
* spawned child with a symbol lookup error. A system binary resolves its own libraries fine, so the
* override is dropped entirely rather than filtered.
*/
export function buildSystemBinaryEnv(env: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv {
const { LD_LIBRARY_PATH: _dropped, ...rest } = env;
return rest;
}
@@ -0,0 +1,63 @@
import assert from 'node:assert/strict';
import path from 'node:path';
import test from 'node:test';
import {
buildWindowsShimInvocation,
isWindowsCommandShim,
resolveWindowsCommand,
} from './windows-command-shim';
test('resolves a bare name through PATH and PATHEXT in order', () => {
const npmDir = path.join('/fake', 'npm');
const binDir = path.join('/fake', 'bin');
const existing = new Set([path.join(npmDir, 'codex.CMD'), path.join(binDir, 'codex.EXE')]);
const env = { PATH: [npmDir, binDir].join(path.delimiter), PATHEXT: '.EXE;.CMD' };
assert.equal(
resolveWindowsCommand('codex', env, (candidate) => existing.has(candidate)),
path.join(npmDir, 'codex.CMD'),
);
assert.equal(
resolveWindowsCommand('codex.exe', env, () => true),
'codex.exe',
'commands with an extension are left alone',
);
assert.equal(
resolveWindowsCommand('missing', env, () => false),
'missing',
);
});
test('resolves an extensionless path in its own directory', () => {
const shim = path.join('/fake', 'npm', 'codex');
assert.equal(
resolveWindowsCommand(shim, { PATHEXT: '.CMD' }, (candidate) => candidate === `${shim}.CMD`),
`${shim}.CMD`,
);
});
test('recognizes batch shims only', () => {
assert.equal(isWindowsCommandShim('C:\\npm\\codex.cmd'), true);
assert.equal(isWindowsCommandShim('run.BAT'), true);
assert.equal(isWindowsCommandShim('codex.exe'), false);
});
test('quotes the shim call for cmd.exe', () => {
const invocation = buildWindowsShimInvocation(
'C:\\npm\\codex.cmd',
['-C', 'C:\\Temp dir\\a&b', '-c', 'effort="low"'],
{ ComSpec: 'C:\\Windows\\system32\\cmd.exe' },
);
assert.deepEqual(invocation, {
file: 'C:\\Windows\\system32\\cmd.exe',
args: [
'/d',
'/s',
'/v:off',
'/c',
'"^"C:\\npm\\codex.cmd^" ^"-C^" ^"C:\\Temp^ dir\\a^&b^" ^"-c^" ^"effort=\\^"low\\^"^""',
],
});
});
+83
View File
@@ -0,0 +1,83 @@
import fs from 'node:fs';
import path from 'node:path';
/** True for batch shims (`.cmd`/`.bat`), which Windows only runs through cmd.exe. */
export function isWindowsCommandShim(command: string): boolean {
return /\.(cmd|bat)$/i.test(command);
}
/**
* Resolves an extensionless Windows command the way cmd.exe does: each `PATHEXT` extension in
* the command's own directory, or in each `PATH` entry for a bare name. Returns `command`
* unchanged when it already has an extension or nothing matches.
*/
export function resolveWindowsCommand(
command: string,
env: NodeJS.ProcessEnv,
existsSync: (candidate: string) => boolean = fs.existsSync,
): string {
if (path.extname(command)) return command;
const directory = path.dirname(command);
const directories =
directory === '.' ? (env.PATH ?? env.Path ?? '').split(path.delimiter).filter(Boolean) : [''];
const extensions = (env.PATHEXT ?? '.COM;.EXE;.BAT;.CMD').split(';').filter(Boolean);
for (const entry of directories) {
for (const extension of extensions) {
const candidate = entry ? path.join(entry, command + extension) : command + extension;
if (existsSync(candidate)) return candidate;
}
}
return command;
}
/*!
* Windows command escaping adapted from cross-spawn 7.0.6.
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Made With MOXY Lda <hello@moxy.studio>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
const WINDOWS_SHELL_META_CHARACTERS = /([()\][%!^"`<>&|;, *?])/g;
// Quote for both cmd.exe and the Windows argv parser. The outer caret escapes
// are consumed by cmd, leaving the quoted argument unchanged for the command.
function escapeWindowsShellArgument(value: string): string {
const quotesEscaped = value
.replace(/(?=(\\+?)?)\1"/g, '$1$1\\"')
.replace(/(?=(\\+?)?)\1$/, '$1$1');
return `"${quotesEscaped}"`.replace(WINDOWS_SHELL_META_CHARACTERS, '^$1');
}
/**
* Builds the cmd.exe invocation that runs a batch shim with `args`. Spawn the result with
* `windowsVerbatimArguments: true` so Node does not re-quote the prepared command line.
*/
export function buildWindowsShimInvocation(
command: string,
args: readonly string[],
env: NodeJS.ProcessEnv,
): { file: string; args: string[] } {
const commandLine = [command, ...args].map(escapeWindowsShellArgument).join(' ');
return {
file: env.ComSpec ?? env.COMSPEC ?? process.env.ComSpec ?? 'cmd.exe',
args: ['/d', '/s', '/v:off', '/c', `"${commandLine}"`],
};
}