@myrialabs/clopen 0.3.21 → 0.3.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/backend/engine/adapters/claude/environment.ts +0 -17
- package/backend/engine/adapters/claude/error-handler.ts +0 -4
- package/backend/engine/install-recipes.ts +26 -95
- package/backend/files/file-operations.test.ts +75 -0
- package/backend/files/file-operations.ts +10 -56
- package/backend/files/image-processing.test.ts +52 -0
- package/backend/files/image-processing.ts +11 -1
- package/backend/files/path-browsing.ts +141 -139
- package/backend/git/git-service.ts +13 -2
- package/backend/http/files-upload.test.ts +174 -0
- package/backend/http/files-upload.ts +27 -7
- package/backend/http/upload-temp-cleanup.ts +53 -0
- package/backend/index.ts +4 -0
- package/backend/mcp/remote-server.test.ts +150 -0
- package/backend/mcp/remote-server.ts +68 -0
- package/backend/terminal/pty-session-manager.ts +18 -11
- package/backend/ws/files/index.ts +5 -0
- package/backend/ws/files/reveal.ts +61 -0
- package/backend/ws/files/search.test.ts +115 -0
- package/backend/ws/files/search.ts +77 -26
- package/backend/ws/files/write.ts +3 -1
- package/backend/ws/git/branch-name.ts +170 -0
- package/backend/ws/git/branch.ts +15 -2
- package/backend/ws/git/commit-message.ts +8 -2
- package/backend/ws/git/index.ts +3 -1
- package/backend/ws/user/crud.ts +8 -4
- package/dist/assets/main-Ba19ADkk.js +377 -0
- package/dist/assets/main-CwCZzMVm.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/constants/git.ts +5 -0
- package/shared/types/git.ts +5 -0
- package/shared/types/stores/settings.ts +17 -0
- package/dist/assets/main-BgOiIS43.js +0 -360
- package/dist/assets/main-CJdJeen6.css +0 -1
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { join } from 'path';
|
|
11
|
-
import { isWindows, findGitBash } from '../../../terminal/shell-utils.js';
|
|
12
11
|
import { engineQueries } from '../../../database/queries';
|
|
13
12
|
import { debug } from '$shared/utils/logger';
|
|
14
13
|
import { getCleanSpawnEnv, getClopenDir } from '../../../utils/index.js';
|
|
@@ -104,22 +103,6 @@ async function _doSetup(): Promise<void> {
|
|
|
104
103
|
debug.warn('engine', '⚠️ Claude Code: Could not read active account (DB may not be ready)');
|
|
105
104
|
}
|
|
106
105
|
|
|
107
|
-
// Setup Git Bash on Windows
|
|
108
|
-
if (isWindows) {
|
|
109
|
-
const existingPath = process.env.CLAUDE_CODE_GIT_BASH_PATH;
|
|
110
|
-
if (existingPath) {
|
|
111
|
-
overrides['CLAUDE_CODE_GIT_BASH_PATH'] = existingPath;
|
|
112
|
-
} else {
|
|
113
|
-
const gitBashPath = await findGitBash();
|
|
114
|
-
if (gitBashPath) {
|
|
115
|
-
overrides['CLAUDE_CODE_GIT_BASH_PATH'] = gitBashPath;
|
|
116
|
-
debug.log('engine', '✅ Claude Code: Git Bash configured at:', gitBashPath);
|
|
117
|
-
} else {
|
|
118
|
-
debug.warn('engine', '⚠️ Claude Code: Git Bash not found - may have limited functionality on Windows');
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
106
|
// Terminal environment variables
|
|
124
107
|
overrides['FORCE_COLOR'] = '1';
|
|
125
108
|
overrides['COLORTERM'] = 'truecolor';
|
|
@@ -17,10 +17,6 @@ export function handleStreamError(error: unknown): void {
|
|
|
17
17
|
throw new Error('Unexpected conversation limit reached. This should not happen with unlimited turns enabled.');
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
if (error.message.includes('git-bash') || error.message.includes('requires git-bash')) {
|
|
21
|
-
throw new Error('Claude Code requires git-bash on Windows. Please ensure Git is installed and accessible.');
|
|
22
|
-
}
|
|
23
|
-
|
|
24
20
|
if (error.message.includes('ENOENT') || error.message.includes('spawn claude-code')) {
|
|
25
21
|
throw new Error('Claude Agent SDK not found. Please install it with: npm install @anthropic-ai/claude-agent-sdk');
|
|
26
22
|
}
|
|
@@ -343,72 +343,33 @@ function attachCurlRequirement(base: Recipe, toolLabel: string): boolean {
|
|
|
343
343
|
}
|
|
344
344
|
|
|
345
345
|
async function resolveClaudeRecipe(): Promise<Recipe> {
|
|
346
|
-
|
|
346
|
+
return {
|
|
347
347
|
tool: 'claude',
|
|
348
|
-
autoInstallable:
|
|
348
|
+
autoInstallable: true,
|
|
349
349
|
missingPrereqs: [],
|
|
350
|
-
manualInstructions: [
|
|
350
|
+
manualInstructions: [{
|
|
351
|
+
label: 'bun',
|
|
352
|
+
command: 'bun add -g @anthropic-ai/claude-code',
|
|
353
|
+
docs: 'https://code.claude.com/docs/en/setup'
|
|
354
|
+
}],
|
|
355
|
+
command: ['bun', 'add', '-g', '@anthropic-ai/claude-code'],
|
|
356
|
+
displayCommand: 'bun add -g @anthropic-ai/claude-code'
|
|
351
357
|
};
|
|
352
|
-
|
|
353
|
-
if (process.platform === 'win32') {
|
|
354
|
-
base.autoInstallable = true;
|
|
355
|
-
base.shell = { program: 'powershell.exe', args: ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command'] };
|
|
356
|
-
base.command = ['irm https://claude.ai/install.ps1 | iex'];
|
|
357
|
-
base.displayCommand = 'irm https://claude.ai/install.ps1 | iex';
|
|
358
|
-
base.manualInstructions.push({
|
|
359
|
-
label: 'PowerShell',
|
|
360
|
-
command: 'irm https://claude.ai/install.ps1 | iex',
|
|
361
|
-
docs: 'https://docs.claude.com/en/docs/claude-code/overview'
|
|
362
|
-
});
|
|
363
|
-
return base;
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
// macOS / Linux
|
|
367
|
-
base.manualInstructions.push({
|
|
368
|
-
label: 'curl + bash',
|
|
369
|
-
command: 'curl -fsSL https://claude.ai/install.sh | bash',
|
|
370
|
-
docs: 'https://docs.claude.com/en/docs/claude-code/overview'
|
|
371
|
-
});
|
|
372
|
-
if (!attachCurlRequirement(base, 'Claude Code')) return base;
|
|
373
|
-
|
|
374
|
-
base.autoInstallable = true;
|
|
375
|
-
base.shell = { program: 'bash', args: ['-c'] };
|
|
376
|
-
base.command = ['curl -fsSL https://claude.ai/install.sh | bash'];
|
|
377
|
-
base.displayCommand = 'curl -fsSL https://claude.ai/install.sh | bash';
|
|
378
|
-
return base;
|
|
379
358
|
}
|
|
380
359
|
|
|
381
360
|
async function resolveOpenCodeRecipe(): Promise<Recipe> {
|
|
382
361
|
const base: Recipe = {
|
|
383
362
|
tool: 'opencode',
|
|
384
|
-
autoInstallable:
|
|
363
|
+
autoInstallable: true,
|
|
385
364
|
missingPrereqs: [],
|
|
386
365
|
manualInstructions: [{
|
|
387
|
-
label: '
|
|
388
|
-
command: '
|
|
389
|
-
docs: 'https://opencode.ai'
|
|
390
|
-
}]
|
|
366
|
+
label: 'bun',
|
|
367
|
+
command: 'bun add -g opencode-ai',
|
|
368
|
+
docs: 'https://opencode.ai/docs'
|
|
369
|
+
}],
|
|
370
|
+
command: ['bun', 'add', '-g', 'opencode-ai'],
|
|
371
|
+
displayCommand: 'bun add -g opencode-ai'
|
|
391
372
|
};
|
|
392
|
-
|
|
393
|
-
if (process.platform === 'win32') {
|
|
394
|
-
base.autoInstallable = true;
|
|
395
|
-
base.shell = { program: 'powershell.exe', args: ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command'] };
|
|
396
|
-
base.command = ['irm https://opencode.ai/install.ps1 | iex'];
|
|
397
|
-
base.displayCommand = 'irm https://opencode.ai/install.ps1 | iex';
|
|
398
|
-
base.manualInstructions.push({
|
|
399
|
-
label: 'PowerShell',
|
|
400
|
-
command: 'irm https://opencode.ai/install.ps1 | iex',
|
|
401
|
-
docs: 'https://opencode.ai'
|
|
402
|
-
});
|
|
403
|
-
return base;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
if (!attachCurlRequirement(base, 'OpenCode')) return base;
|
|
407
|
-
|
|
408
|
-
base.autoInstallable = true;
|
|
409
|
-
base.shell = { program: 'bash', args: ['-c'] };
|
|
410
|
-
base.command = ['curl -fsSL https://opencode.ai/install | bash'];
|
|
411
|
-
base.displayCommand = 'curl -fsSL https://opencode.ai/install | bash';
|
|
412
373
|
return base;
|
|
413
374
|
}
|
|
414
375
|
|
|
@@ -429,60 +390,30 @@ async function resolveCopilotRecipe(): Promise<Recipe> {
|
|
|
429
390
|
}
|
|
430
391
|
|
|
431
392
|
async function resolveQwenRecipe(): Promise<Recipe> {
|
|
432
|
-
const manualInstructions: ManualInstruction[] = [{
|
|
433
|
-
label: 'bun',
|
|
434
|
-
command: 'bun add -g @qwen-code/qwen-code',
|
|
435
|
-
docs: 'https://github.com/QwenLM/qwen-code'
|
|
436
|
-
}];
|
|
437
|
-
|
|
438
|
-
// Qwen Code CLI ships bundled with `@qwen-code/sdk` from v0.1.1+ so the
|
|
439
|
-
// SDK path doesn't need a standalone install. We still expose this recipe
|
|
440
|
-
// for users who prefer the standalone CLI for `qwen-oauth` / shell use.
|
|
441
|
-
if (!resolveBinary('bun')) {
|
|
442
|
-
return {
|
|
443
|
-
tool: 'qwen',
|
|
444
|
-
autoInstallable: false,
|
|
445
|
-
unavailableReason: 'Bun is required to install the Qwen Code CLI. Install Bun first (https://bun.sh).',
|
|
446
|
-
missingPrereqs: [],
|
|
447
|
-
manualInstructions
|
|
448
|
-
};
|
|
449
|
-
}
|
|
450
|
-
|
|
451
393
|
return {
|
|
452
394
|
tool: 'qwen',
|
|
453
395
|
autoInstallable: true,
|
|
454
396
|
missingPrereqs: [],
|
|
455
|
-
manualInstructions
|
|
397
|
+
manualInstructions: [{
|
|
398
|
+
label: 'bun',
|
|
399
|
+
command: 'bun add -g @qwen-code/qwen-code',
|
|
400
|
+
docs: 'https://qwen.ai/qwencode'
|
|
401
|
+
}],
|
|
456
402
|
command: ['bun', 'add', '-g', '@qwen-code/qwen-code'],
|
|
457
403
|
displayCommand: 'bun add -g @qwen-code/qwen-code'
|
|
458
404
|
};
|
|
459
405
|
}
|
|
460
406
|
|
|
461
407
|
async function resolveCodexRecipe(): Promise<Recipe> {
|
|
462
|
-
const manualInstructions: ManualInstruction[] = [{
|
|
463
|
-
label: 'bun',
|
|
464
|
-
command: 'bun add -g @openai/codex',
|
|
465
|
-
docs: 'https://developers.openai.com/codex'
|
|
466
|
-
}];
|
|
467
|
-
|
|
468
|
-
// Codex CLI is installed via `bun add -g`. Bun must be on PATH for the
|
|
469
|
-
// auto-installer to work; otherwise we fall back to the manual instructions
|
|
470
|
-
// (the user installs Bun first via the existing Bun recipe / website).
|
|
471
|
-
if (!resolveBinary('bun')) {
|
|
472
|
-
return {
|
|
473
|
-
tool: 'codex',
|
|
474
|
-
autoInstallable: false,
|
|
475
|
-
unavailableReason: 'Bun is required to install the Codex CLI. Install Bun first (https://bun.sh).',
|
|
476
|
-
missingPrereqs: [],
|
|
477
|
-
manualInstructions
|
|
478
|
-
};
|
|
479
|
-
}
|
|
480
|
-
|
|
481
408
|
return {
|
|
482
409
|
tool: 'codex',
|
|
483
410
|
autoInstallable: true,
|
|
484
411
|
missingPrereqs: [],
|
|
485
|
-
manualInstructions
|
|
412
|
+
manualInstructions: [{
|
|
413
|
+
label: 'bun',
|
|
414
|
+
command: 'bun add -g @openai/codex',
|
|
415
|
+
docs: 'https://developers.openai.com/codex/quickstart'
|
|
416
|
+
}],
|
|
486
417
|
command: ['bun', 'add', '-g', '@openai/codex'],
|
|
487
418
|
displayCommand: 'bun add -g @openai/codex'
|
|
488
419
|
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test';
|
|
2
|
+
import { rm, mkdir, stat } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
|
|
6
|
+
// Stub settingsQueries so the module under test never touches a real database.
|
|
7
|
+
await mock.module('../database/queries', () => ({
|
|
8
|
+
settingsQueries: {
|
|
9
|
+
get: () => null,
|
|
10
|
+
getAll: () => [],
|
|
11
|
+
set: () => {},
|
|
12
|
+
delete: () => {}
|
|
13
|
+
}
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
const { deleteOperation, renameOperation, createDirectoryOperation } = await import('./file-operations');
|
|
17
|
+
|
|
18
|
+
// Shell-special name that would break cmd /c or rm -f via argument injection.
|
|
19
|
+
const SPECIAL_NAME = 'foo & bar.txt';
|
|
20
|
+
|
|
21
|
+
describe('file-operations — shell-special filenames', () => {
|
|
22
|
+
let testDir: string;
|
|
23
|
+
|
|
24
|
+
beforeEach(async () => {
|
|
25
|
+
testDir = join(tmpdir(), `clopen-file-ops-test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
|
|
26
|
+
await mkdir(testDir, { recursive: true });
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
afterEach(async () => {
|
|
30
|
+
await rm(testDir, { recursive: true, force: true });
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('deleteOperation removes a file whose name contains shell metacharacters', async () => {
|
|
34
|
+
const filePath = join(testDir, SPECIAL_NAME);
|
|
35
|
+
await Bun.write(filePath, 'contents');
|
|
36
|
+
|
|
37
|
+
await expect(deleteOperation(filePath)).resolves.toMatchObject({ message: 'File deleted successfully' });
|
|
38
|
+
await expect(stat(filePath)).rejects.toThrow();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('deleteOperation removes an empty directory', async () => {
|
|
42
|
+
const dirPath = join(testDir, 'empty-dir');
|
|
43
|
+
await mkdir(dirPath, { recursive: true });
|
|
44
|
+
|
|
45
|
+
await expect(deleteOperation(dirPath)).resolves.toMatchObject({ message: 'Directory deleted successfully' });
|
|
46
|
+
await expect(stat(dirPath)).rejects.toThrow();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('deleteOperation force-removes a non-empty directory', async () => {
|
|
50
|
+
const dirPath = join(testDir, 'non-empty-dir');
|
|
51
|
+
await mkdir(dirPath, { recursive: true });
|
|
52
|
+
await Bun.write(join(dirPath, 'child.txt'), 'contents');
|
|
53
|
+
|
|
54
|
+
await expect(deleteOperation(dirPath, true)).resolves.toMatchObject({ message: 'Directory deleted successfully' });
|
|
55
|
+
await expect(stat(dirPath)).rejects.toThrow();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('renameOperation renames a file whose name contains shell metacharacters', async () => {
|
|
59
|
+
const src = join(testDir, SPECIAL_NAME);
|
|
60
|
+
const dest = join(testDir, 'safe-name.txt');
|
|
61
|
+
await Bun.write(src, 'contents');
|
|
62
|
+
|
|
63
|
+
await expect(renameOperation(src, dest)).resolves.toMatchObject({ message: 'File/directory renamed successfully' });
|
|
64
|
+
await expect(stat(dest)).resolves.toBeTruthy();
|
|
65
|
+
await expect(stat(src)).rejects.toThrow();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('createDirectoryOperation creates a directory whose name contains shell metacharacters', async () => {
|
|
69
|
+
const dirPath = join(testDir, 'dir & name');
|
|
70
|
+
|
|
71
|
+
await expect(createDirectoryOperation(dirPath)).resolves.toMatchObject({ message: 'Directory created successfully' });
|
|
72
|
+
const s = await stat(dirPath);
|
|
73
|
+
expect(s.isDirectory()).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
@@ -1,46 +1,18 @@
|
|
|
1
1
|
import { dirname, extname, join } from 'path';
|
|
2
|
-
import { stat as fsStat } from 'node:fs/promises';
|
|
2
|
+
import { mkdir as fsMkdir, readdir as fsReaddir, rename as fsRename, rmdir as fsRmdir, stat as fsStat, unlink as fsUnlink, rm as fsRm } from 'node:fs/promises';
|
|
3
3
|
|
|
4
4
|
import { debug } from '$shared/utils/logger';
|
|
5
5
|
import { validateFileSize } from './file-size-limit';
|
|
6
6
|
|
|
7
|
-
//
|
|
8
|
-
const { spawn } = Bun;
|
|
9
|
-
|
|
10
|
-
// Bun-compatible readdir implementation (cross-platform)
|
|
7
|
+
// Cross-platform readdir using fs.promises
|
|
11
8
|
async function readdir(path: string): Promise<string[]> {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
proc = Bun.spawn(['cmd', '/c', 'dir', '/b', '/a', path], { stdout: 'pipe', stderr: 'ignore' });
|
|
15
|
-
} else {
|
|
16
|
-
proc = Bun.spawn(['ls', '-1A', path], { stdout: 'pipe', stderr: 'ignore' });
|
|
17
|
-
}
|
|
18
|
-
const result = await new Response(proc.stdout).text();
|
|
19
|
-
// Split and clean up, removing \r characters for Windows compatibility
|
|
20
|
-
return result.trim().split(/\r?\n/).map(line => line.trim()).filter(Boolean);
|
|
9
|
+
const entries = await fsReaddir(path, { withFileTypes: true });
|
|
10
|
+
return entries.map(e => e.name);
|
|
21
11
|
}
|
|
22
12
|
|
|
23
13
|
async function mkdir(path: string, options?: { recursive?: boolean }) {
|
|
24
|
-
// Pure Bun approach: create directory by writing a temp file
|
|
25
14
|
try {
|
|
26
|
-
|
|
27
|
-
await Bun.write(tempFile, '');
|
|
28
|
-
// Clean up the temp file
|
|
29
|
-
try {
|
|
30
|
-
if (process.platform === 'win32') {
|
|
31
|
-
await spawn(['cmd', '/c', 'del', '/f', '/q', tempFile.replace(/\//g, '\\')], {
|
|
32
|
-
stdout: 'ignore',
|
|
33
|
-
stderr: 'ignore'
|
|
34
|
-
}).exited;
|
|
35
|
-
} else {
|
|
36
|
-
await spawn(['rm', '-f', tempFile], {
|
|
37
|
-
stdout: 'ignore',
|
|
38
|
-
stderr: 'ignore'
|
|
39
|
-
}).exited;
|
|
40
|
-
}
|
|
41
|
-
} catch {
|
|
42
|
-
// Ignore cleanup errors
|
|
43
|
-
}
|
|
15
|
+
await fsMkdir(path, { recursive: options?.recursive ?? false });
|
|
44
16
|
} catch (error) {
|
|
45
17
|
throw new Error(`Failed to create directory ${path}: ${error}`);
|
|
46
18
|
}
|
|
@@ -79,37 +51,19 @@ async function copyDirectory(src: string, dest: string) {
|
|
|
79
51
|
}
|
|
80
52
|
|
|
81
53
|
async function rename(oldPath: string, newPath: string) {
|
|
82
|
-
|
|
83
|
-
const proc = spawn(['cmd', '/c', 'move', oldPath.replace(/\//g, '\\'), newPath.replace(/\//g, '\\')], { stdout: 'ignore', stderr: 'ignore' });
|
|
84
|
-
await proc.exited;
|
|
85
|
-
} else {
|
|
86
|
-
const proc = spawn(['mv', oldPath, newPath], { stdout: 'ignore', stderr: 'ignore' });
|
|
87
|
-
await proc.exited;
|
|
88
|
-
}
|
|
54
|
+
await fsRename(oldPath, newPath);
|
|
89
55
|
}
|
|
90
56
|
|
|
91
57
|
async function unlink(path: string) {
|
|
92
|
-
|
|
93
|
-
const proc = spawn(['cmd', '/c', 'del', '/f', '/q', path.replace(/\//g, '\\')], { stdout: 'ignore', stderr: 'ignore' });
|
|
94
|
-
await proc.exited;
|
|
95
|
-
} else {
|
|
96
|
-
const proc = spawn(['rm', '-f', path], { stdout: 'ignore', stderr: 'ignore' });
|
|
97
|
-
await proc.exited;
|
|
98
|
-
}
|
|
58
|
+
await fsUnlink(path);
|
|
99
59
|
}
|
|
100
60
|
|
|
101
61
|
async function rm(path: string, options?: { recursive?: boolean }) {
|
|
102
|
-
|
|
103
|
-
const proc = spawn(['cmd', '/c', 'rmdir', '/s', '/q', path.replace(/\//g, '\\')], { stdout: 'ignore', stderr: 'ignore' });
|
|
104
|
-
await proc.exited;
|
|
105
|
-
} else {
|
|
106
|
-
const proc = spawn(['rm', '-rf', path], { stdout: 'ignore', stderr: 'ignore' });
|
|
107
|
-
await proc.exited;
|
|
108
|
-
}
|
|
62
|
+
await fsRm(path, { recursive: options?.recursive ?? false, force: true });
|
|
109
63
|
}
|
|
110
64
|
|
|
111
|
-
async function rmdir(path: string
|
|
112
|
-
|
|
65
|
+
async function rmdir(path: string) {
|
|
66
|
+
await fsRmdir(path);
|
|
113
67
|
}
|
|
114
68
|
|
|
115
69
|
export async function writeFileOperation(filePath: string, content: string, baseModified?: string) {
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { describe, expect, mock, test } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
// Stub sharp before importing the module under test so no real image I/O
|
|
4
|
+
// happens during unit tests. mockPages is set per-test to control metadata.
|
|
5
|
+
let mockPages: number | undefined;
|
|
6
|
+
|
|
7
|
+
await mock.module('sharp', () => ({
|
|
8
|
+
default: (_input: unknown, _opts?: unknown) => ({
|
|
9
|
+
metadata: () =>
|
|
10
|
+
Promise.resolve({
|
|
11
|
+
width: 100,
|
|
12
|
+
height: 100,
|
|
13
|
+
format: 'gif',
|
|
14
|
+
pages: mockPages,
|
|
15
|
+
}),
|
|
16
|
+
}),
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
// Stub file-size-limit so the database is never touched.
|
|
20
|
+
await mock.module('./file-size-limit', () => ({
|
|
21
|
+
validateFileSize: () => {},
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
const { processImageEdit } = await import('./image-processing');
|
|
25
|
+
|
|
26
|
+
const RECIPE = { output: { format: 'gif' as const } };
|
|
27
|
+
|
|
28
|
+
describe('processImageEdit — animation frame limit', () => {
|
|
29
|
+
test('rejects when frame count exceeds the 100-frame limit', async () => {
|
|
30
|
+
mockPages = 101;
|
|
31
|
+
await expect(processImageEdit('/dev/null', RECIPE)).rejects.toThrow(
|
|
32
|
+
'Cannot process animated image: 101 frames exceeds the limit of 100'
|
|
33
|
+
);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('error message includes the actual and maximum frame counts', async () => {
|
|
37
|
+
mockPages = 500;
|
|
38
|
+
await expect(processImageEdit('/dev/null', RECIPE)).rejects.toThrow(
|
|
39
|
+
'Cannot process animated image: 500 frames exceeds the limit of 100'
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('allows animated images at exactly 100 frames', async () => {
|
|
44
|
+
mockPages = 100;
|
|
45
|
+
await expect(processImageEdit('/dev/null', RECIPE)).resolves.toBeDefined();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('allows static images with no pages field', async () => {
|
|
49
|
+
mockPages = undefined;
|
|
50
|
+
await expect(processImageEdit('/dev/null', RECIPE)).resolves.toBeDefined();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -27,6 +27,9 @@ export interface ProcessedImage {
|
|
|
27
27
|
size: number;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
/** Hard cap on animation frames — prevents resource exhaustion via GIF bomb. */
|
|
31
|
+
const MAX_ANIMATION_FRAMES = 100;
|
|
32
|
+
|
|
30
33
|
/** Clamp a value into [min, max], rounding to an integer. */
|
|
31
34
|
function clampInt(value: number, min: number, max: number): number {
|
|
32
35
|
return Math.max(min, Math.min(max, Math.round(value)));
|
|
@@ -193,6 +196,13 @@ export async function processImageEdit(
|
|
|
193
196
|
throw new Error('Source file is not a readable image');
|
|
194
197
|
}
|
|
195
198
|
|
|
199
|
+
const frameCount = srcMeta.pages ?? 1;
|
|
200
|
+
if (frameCount > MAX_ANIMATION_FRAMES) {
|
|
201
|
+
throw new Error(
|
|
202
|
+
`Cannot process animated image: ${frameCount} frames exceeds the limit of ${MAX_ANIMATION_FRAMES}`
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
196
206
|
// Nothing to do: not compressing, no edits, and the format is unchanged →
|
|
197
207
|
// return the original bytes verbatim so an untouched file stays identical.
|
|
198
208
|
const compress = recipe.output.compress ?? false;
|
|
@@ -208,7 +218,7 @@ export async function processImageEdit(
|
|
|
208
218
|
};
|
|
209
219
|
}
|
|
210
220
|
|
|
211
|
-
const animated =
|
|
221
|
+
const animated = frameCount > 1;
|
|
212
222
|
const rotate = recipe.rotate ?? 0;
|
|
213
223
|
|
|
214
224
|
let pipeline: Sharp;
|