mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 09:45:36 +08:00
fix: preserve save-as backups and register history CLI routes
This commit is contained in:
1 parent
ea9461c085
commit
91e6005152
6 files changed
+152
-3
No files matched your search
@@ -295,6 +295,15 @@ export default (app: Router) => {
|
|||||||
fileExists &&
|
fileExists &&
|
||||||
resolveFileAccess(config.scriptPath, [filePath], config.blackFileList)
|
resolveFileAccess(config.scriptPath, [filePath], config.blackFileList)
|
||||||
) {
|
) {
|
||||||
|
// Save-as removes the source after committing the destination. Keep
|
||||||
|
// its original content too: destination history only protects the
|
||||||
|
// file being overwritten, not the source being deleted.
|
||||||
|
if (filename !== originFilename) {
|
||||||
|
await fs.copyFile(
|
||||||
|
originFilePath,
|
||||||
|
join(config.bakPath, originFilename.replace(/\//g, '')),
|
||||||
|
);
|
||||||
|
}
|
||||||
const data = await historyService().save(path, filename, content, {
|
const data = await historyService().save(path, filename, content, {
|
||||||
skipHistory: req.body.skipHistory,
|
skipHistory: req.body.skipHistory,
|
||||||
expectedHash: req.body.expectedHash,
|
expectedHash: req.body.expectedHash,
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
# OpenAPI 全量参考 / Complete OpenAPI reference
|
# OpenAPI 全量参考 / Complete OpenAPI reference
|
||||||
|
|
||||||
CLI 对应当前 develop 的 143 条有效路由。通过 `ql api routes --json` 查看方法、路径、对应命令、位置参数、请求体与上传字段;CLI 测试与 back/api 路由逐项核对。GET configs/:file、scripts/:file、logs/:file 已返回 410,改用 detail 命令,不再暴露旧入口。旧版面板可能没有新路由,以服务端响应为准。
|
CLI 对应本版本后端的 146 条有效路由。通过 `ql api routes --json` 查看方法、路径、对应命令、位置参数、请求体与上传字段;CLI 测试与 back/api 路由逐项核对。GET configs/:file、scripts/:file、logs/:file 已返回 410,改用 detail 命令,不再暴露旧入口。旧版面板可能没有新路由,以服务端响应为准。
|
||||||
|
|
||||||
The catalogue covers 143 active routes in the current develop backend. `ql api routes --json` lists methods, paths, commands, positional parameters, bodies and upload fields. A route-coverage test detects drift. Three retired GET :file routes return 410; use detail-based commands. Older panel versions may not implement newer routes.
|
The catalogue covers 146 active routes in the current backend. `ql api routes --json` lists methods, paths, commands, positional parameters, bodies and upload fields. A route-coverage test detects drift. Three retired GET :file routes return 410; use detail-based commands. Older panel versions may not implement newer routes.
|
||||||
|
|
||||||
## 认证与权限 / Authentication and permissions
|
## 认证与权限 / Authentication and permissions
|
||||||
|
|
||||||
@@ -46,6 +46,9 @@ ql env create --data @envs.json --json
|
|||||||
ql env update 8 --data '{"name":"EXAMPLE","value":"value"}' --json
|
ql env update 8 --data '{"name":"EXAMPLE","value":"value"}' --json
|
||||||
ql script create --file ./demo.js --data '{"filename":"demo.js","path":""}' --json
|
ql script create --file ./demo.js --data '{"filename":"demo.js","path":""}' --json
|
||||||
ql script get --query '{"file":"demo.js","path":""}' --json
|
ql script get --query '{"file":"demo.js","path":""}' --json
|
||||||
|
ql script history-list --query '{"filename":"demo.js","path":""}' --json
|
||||||
|
ql script history-detail --query '{"filename":"demo.js","path":"","id":"VERSION_UUID"}' --json
|
||||||
|
ql script history-restore --data '{"filename":"demo.js","path":"","id":"VERSION_UUID","expectedHash":"CURRENT_SHA256"}' --json
|
||||||
ql config save --data '{"name":"example.sh","content":"# example"}' --json
|
ql config save --data '{"name":"example.sh","content":"# example"}' --json
|
||||||
ql config get --query '{"path":"example.sh"}' --json
|
ql config get --query '{"path":"example.sh"}' --json
|
||||||
ql log download --data '{"filename":"example.log","path":"example"}' --output ./example.log --json
|
ql log download --data '{"filename":"example.log","path":"example"}' --output ./example.log --json
|
||||||
@@ -133,6 +136,9 @@ System/user operations below execute remotely through the panel API. They do not
|
|||||||
| `ql config save ` | POST | `configs/save` | --data, --query |
|
| `ql config save ` | POST | `configs/save` | --data, --query |
|
||||||
| `ql script list ` | GET | `scripts` | --query |
|
| `ql script list ` | GET | `scripts` | --query |
|
||||||
| `ql script get ` | GET | `scripts/detail` | --query |
|
| `ql script get ` | GET | `scripts/detail` | --query |
|
||||||
|
| `ql script history-list ` | GET | `scripts/history` | --query |
|
||||||
|
| `ql script history-detail ` | GET | `scripts/history/detail` | --query |
|
||||||
|
| `ql script history-restore ` | PUT | `scripts/history/restore` | --data, --query |
|
||||||
| `ql script create ` | POST | `scripts` | --data, --file (file), --query |
|
| `ql script create ` | POST | `scripts` | --data, --file (file), --query |
|
||||||
| `ql script update ` | PUT | `scripts` | --data, --file (file), --query |
|
| `ql script update ` | PUT | `scripts` | --data, --file (file), --query |
|
||||||
| `ql script delete ` | DELETE | `scripts` | --data, --query |
|
| `ql script delete ` | DELETE | `scripts` | --data, --query |
|
||||||
|
|||||||
@@ -275,6 +275,18 @@ export const openOperations: readonly OpenOperation[] = [
|
|||||||
{ name: 'config save', method: 'POST', path: 'configs/save', body: 'json' },
|
{ name: 'config save', method: 'POST', path: 'configs/save', body: 'json' },
|
||||||
{ name: 'script list', method: 'GET', path: 'scripts' },
|
{ name: 'script list', method: 'GET', path: 'scripts' },
|
||||||
{ name: 'script get', method: 'GET', path: 'scripts/detail' },
|
{ name: 'script get', method: 'GET', path: 'scripts/detail' },
|
||||||
|
{ name: 'script history-list', method: 'GET', path: 'scripts/history' },
|
||||||
|
{
|
||||||
|
name: 'script history-detail',
|
||||||
|
method: 'GET',
|
||||||
|
path: 'scripts/history/detail',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'script history-restore',
|
||||||
|
method: 'PUT',
|
||||||
|
path: 'scripts/history/restore',
|
||||||
|
body: 'json',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'script create',
|
name: 'script create',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|||||||
@@ -58,6 +58,31 @@ test('dashboard record sends execution statistics and rejects a missing body bef
|
|||||||
assert.equal(f.requests.length, 1);
|
assert.equal(f.requests.length, 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('script history commands send exact query parameters and restore payloads', async t => {
|
||||||
|
const f = await fixture(t);
|
||||||
|
const identity = { filename: '中文 script.js', path: 'nested folder' };
|
||||||
|
const version = { ...identity, id: '32ec8b22-2c48-4bcf-85d4-b394198417ba' };
|
||||||
|
for (const [command, endpoint, query] of [
|
||||||
|
['history-list', 'scripts/history', identity],
|
||||||
|
['history-detail', 'scripts/history/detail', version],
|
||||||
|
]) {
|
||||||
|
success(await f.run(['script', command, '--query', JSON.stringify(query)]));
|
||||||
|
const req = f.requests.at(-1), url = new URL(req.url, 'http://localhost');
|
||||||
|
assert.equal(req.method, 'GET');
|
||||||
|
assert.equal(url.pathname, '/panel/open/' + endpoint);
|
||||||
|
for (const [key, value] of Object.entries(query)) assert.equal(url.searchParams.get(key), value);
|
||||||
|
assert.equal(req.body, '');
|
||||||
|
}
|
||||||
|
const payload = { ...version, expectedHash: 'a'.repeat(64) };
|
||||||
|
success(await f.run(['script', 'history-restore', '--data', '-'], JSON.stringify(payload)));
|
||||||
|
assert.equal(f.requests.at(-1).method, 'PUT');
|
||||||
|
assert.equal(f.requests.at(-1).url, '/panel/open/scripts/history/restore');
|
||||||
|
assert.deepEqual(JSON.parse(f.requests.at(-1).body), payload);
|
||||||
|
const count = f.requests.length;
|
||||||
|
assert.equal((await f.run(['script', 'history-restore'])).code, 2);
|
||||||
|
assert.equal(f.requests.length, count, 'missing restore payload must not issue a request');
|
||||||
|
});
|
||||||
|
|
||||||
test('OpenAPI catalogue covers every active registered backend route; retired 410 routes are explicit', async () => {
|
test('OpenAPI catalogue covers every active registered backend route; retired 410 routes are explicit', async () => {
|
||||||
const root = path.resolve(__dirname, '../../../back/api');
|
const root = path.resolve(__dirname, '../../../back/api');
|
||||||
const expected = [];
|
const expected = [];
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
- 编辑器 `PUT /api/scripts` 和调试窗口 `POST /api/scripts` 覆盖已有脚本时,在内容变化时留档,首次修改前的内容也保留;相同内容不写历史。
|
- 编辑器 `PUT /api/scripts` 和调试窗口 `POST /api/scripts` 覆盖已有脚本时,在内容变化时留档,首次修改前的内容也保留;相同内容不写历史。
|
||||||
- 历史只包含保存前真实存在的内容;不把失败写入当成一个内容版本。
|
- 历史只包含保存前真实存在的内容;不把失败写入当成一个内容版本。
|
||||||
- 上传、新增、重命名、删除、订阅更新和外部编辑不主动留档。若下次面板保存时检测到外部变更,会保留那一刻的内容,标记为“面板外修改后”。这不代表外部修改之前的内容受到保护。
|
- 上传、新增、重命名、删除、订阅更新和外部编辑不主动留档。若下次面板保存时检测到外部变更,会保留那一刻的内容,标记为“面板外修改后”。这不代表外部修改之前的内容受到保护。
|
||||||
- 按脚本规范化后的相对路径隔离历史;不同目录同名文件不会混淆,指向同一脚本的安全符号链接共用历史。重命名后不会自动迁移历史。历史不能用于回收已删除的文件。
|
- 按脚本规范化后的相对路径隔离历史;不同目录同名文件不会混淆,指向同一脚本的安全符号链接共用历史。重命名后不会自动迁移历史。调试器另存为覆盖已有脚本时,目标旧内容进入历史,删除源文件前将其旧内容保留到现有 bak 目录;备份失败则不覆盖目标、不删除源文件。历史不能用于回收已删除的文件。
|
||||||
- 历史记录支持至多 1 MiB 的 UTF-8 文本文件(含空文件)。当前文件或新内容超出范围时,界面要求明确确认“不记录历史直接保存”;取消则保持文件和草稿不变。确认后以 `skipHistory: true` 和服务端返回的当前哈希重试,锁内检查哈希以防确认期间覆盖他人修改。成功后明确提示本次未留档。大文件可以通过此流程缩小,后续恢复普通留档。未确认的 API 请求返回 413,并包含 `historyUnavailable` 和 `currentHash`;历史读取损坏等其他错误不允许用此选项绕过。
|
- 历史记录支持至多 1 MiB 的 UTF-8 文本文件(含空文件)。当前文件或新内容超出范围时,界面要求明确确认“不记录历史直接保存”;取消则保持文件和草稿不变。确认后以 `skipHistory: true` 和服务端返回的当前哈希重试,锁内检查哈希以防确认期间覆盖他人修改。成功后明确提示本次未留档。大文件可以通过此流程缩小,后续恢复普通留档。未确认的 API 请求返回 413,并包含 `historyUnavailable` 和 `currentHash`;历史读取损坏等其他错误不允许用此选项绕过。
|
||||||
|
|
||||||
## 存储与资源边界
|
## 存储与资源边界
|
||||||
|
|||||||
@@ -18,9 +18,12 @@ test('script routes save, list, preview and restore, with validation and conflic
|
|||||||
const config = {
|
const config = {
|
||||||
scriptPath: scripts,
|
scriptPath: scripts,
|
||||||
dataPath: root,
|
dataPath: root,
|
||||||
|
bakPath: path.join(root, 'bak'),
|
||||||
blackFileList: ['auth.json'],
|
blackFileList: ['auth.json'],
|
||||||
writePathList: [scripts],
|
writePathList: [scripts],
|
||||||
};
|
};
|
||||||
|
await fs.mkdir(config.bakPath);
|
||||||
|
let failure = '';
|
||||||
class ScriptService {
|
class ScriptService {
|
||||||
checkFilePath(dir, name) {
|
checkFilePath(dir, name) {
|
||||||
return resolveFileAccess(scripts, [dir || '', name]);
|
return resolveFileAccess(scripts, [dir || '', name]);
|
||||||
@@ -28,7 +31,21 @@ test('script routes save, list, preview and restore, with validation and conflic
|
|||||||
}
|
}
|
||||||
const api = load(path.join(__dirname, '../../back/api/script.ts'), {
|
const api = load(path.join(__dirname, '../../back/api/script.ts'), {
|
||||||
'../config': { default: config, __esModule: true },
|
'../config': { default: config, __esModule: true },
|
||||||
|
'fs/promises': {
|
||||||
|
...fs,
|
||||||
|
copyFile: async (from, to) => {
|
||||||
|
if (failure === 'backup' && path.dirname(to) === config.bakPath)
|
||||||
|
throw Object.assign(new Error('backup denied'), { code: 'EACCES' });
|
||||||
|
return fs.copyFile(from, to);
|
||||||
|
},
|
||||||
|
writeFile: async (file, ...args) => {
|
||||||
|
if (failure === 'history' && String(file).endsWith('.pending.tmp'))
|
||||||
|
throw Object.assign(new Error('history full'), { code: 'ENOSPC' });
|
||||||
|
return fs.writeFile(file, ...args);
|
||||||
|
},
|
||||||
|
},
|
||||||
'../config/util': {
|
'../config/util': {
|
||||||
|
rmPath: (file) => fs.rm(file, { force: true, recursive: true }),
|
||||||
fileExist: async (file) =>
|
fileExist: async (file) =>
|
||||||
fs.access(file).then(
|
fs.access(file).then(
|
||||||
() => true,
|
() => true,
|
||||||
@@ -159,4 +176,84 @@ test('script routes save, list, preview and restore, with validation and conflic
|
|||||||
'small',
|
'small',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
await t.test(
|
||||||
|
'save-as backs up the source and retains destination history before removing the source',
|
||||||
|
async () => {
|
||||||
|
await fs.writeFile(path.join(scripts, 'source.js'), 'source-original');
|
||||||
|
await fs.writeFile(
|
||||||
|
path.join(scripts, 'destination.js'),
|
||||||
|
'destination-original',
|
||||||
|
);
|
||||||
|
const saved = await call(
|
||||||
|
'/scripts',
|
||||||
|
{
|
||||||
|
filename: 'destination.js',
|
||||||
|
originFilename: 'source.js',
|
||||||
|
path: '',
|
||||||
|
content: 'source-edited',
|
||||||
|
},
|
||||||
|
'POST',
|
||||||
|
);
|
||||||
|
assert.equal(saved.status, 200);
|
||||||
|
await assert.rejects(fs.access(path.join(scripts, 'source.js')), {
|
||||||
|
code: 'ENOENT',
|
||||||
|
});
|
||||||
|
assert.equal(
|
||||||
|
await fs.readFile(path.join(config.bakPath, 'source.js'), 'utf8'),
|
||||||
|
'source-original',
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
await fs.readFile(path.join(scripts, 'destination.js'), 'utf8'),
|
||||||
|
'source-edited',
|
||||||
|
);
|
||||||
|
const list = await call('/scripts/history?filename=destination.js');
|
||||||
|
const preview = await call(
|
||||||
|
`/scripts/history/detail?filename=destination.js&id=${list.body.data.versions[0].id}`,
|
||||||
|
);
|
||||||
|
assert.equal(preview.body.data.version.content, 'destination-original');
|
||||||
|
},
|
||||||
|
);
|
||||||
|
for (const step of ['backup', 'history']) {
|
||||||
|
await t.test(
|
||||||
|
`save-as ${step} failure preserves both original scripts`,
|
||||||
|
async () => {
|
||||||
|
const sourceName = `${step}-source.js`,
|
||||||
|
destinationName = `${step}-destination.js`;
|
||||||
|
await fs.writeFile(path.join(scripts, sourceName), 'source-original');
|
||||||
|
await fs.writeFile(
|
||||||
|
path.join(scripts, destinationName),
|
||||||
|
'destination-original',
|
||||||
|
);
|
||||||
|
failure = step;
|
||||||
|
try {
|
||||||
|
const saved = await call(
|
||||||
|
'/scripts',
|
||||||
|
{
|
||||||
|
filename: destinationName,
|
||||||
|
originFilename: sourceName,
|
||||||
|
path: '',
|
||||||
|
content: 'source-edited',
|
||||||
|
},
|
||||||
|
'POST',
|
||||||
|
);
|
||||||
|
assert.equal(saved.status, 500);
|
||||||
|
assert.equal(
|
||||||
|
await fs.readFile(path.join(scripts, sourceName), 'utf8'),
|
||||||
|
'source-original',
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
await fs.readFile(path.join(scripts, destinationName), 'utf8'),
|
||||||
|
'destination-original',
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
(await call(`/scripts/history?filename=${destinationName}`)).body
|
||||||
|
.data.versions.length,
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
failure = '';
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user