From f85f0e0b85c9d1bcbaef0bedf7c5271eee7a4b5c Mon Sep 17 00:00:00 2001 From: whyour Date: Tue, 6 Oct 2026 03:03:52 +0800 Subject: [PATCH] fix: preserve script aliases when saving as the same file --- back/api/script.ts | 14 ++++++- docs/script-history.md | 2 +- test/back/script-history-api.test.cjs | 57 +++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/back/api/script.ts b/back/api/script.ts index 3d6f00cd..5cc9650d 100644 --- a/back/api/script.ts +++ b/back/api/script.ts @@ -287,10 +287,20 @@ export default (app: Router) => { fileExists && resolveFileAccess(config.scriptPath, [filePath], config.blackFileList) ) { + let removeSource = filename !== originFilename; + if (removeSource) { + const [originRealPath, targetRealPath] = await Promise.all([ + fs.realpath(originFilePath), + fs.realpath(filePath), + ]); + // Aliases of one script are an in-place save. Removing the source + // would also remove the destination behind a target symlink. + removeSource = originRealPath !== targetRealPath; + } // 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) { + if (removeSource) { await fs.copyFile( originFilePath, join(config.bakPath, originFilename.replace(/\//g, '')), @@ -301,7 +311,7 @@ export default (app: Router) => { skipHistory: req.body.skipHistory, expectedHash: req.body.expectedHash, }); - if (filename !== originFilename) await rmPath(originFilePath); + if (removeSource) await rmPath(originFilePath); return res.send({ code: 200, data }); } if (fileExists) { diff --git a/docs/script-history.md b/docs/script-history.md index 854bd121..e3d0f03a 100644 --- a/docs/script-history.md +++ b/docs/script-history.md @@ -7,7 +7,7 @@ - 编辑器 `PUT /api/scripts` 和调试窗口 `POST /api/scripts` 覆盖已有脚本时,在内容变化时留档,首次修改前的内容也保留;相同内容不写历史。 - 历史只包含保存前真实存在的内容;不把失败写入当成一个内容版本。 - 上传、新增、重命名、删除、订阅更新和外部编辑不主动留档。若下次面板保存时检测到外部变更,会保留那一刻的内容,标记为“面板外修改后”。这不代表外部修改之前的内容受到保护。 -- 按脚本规范化后的相对路径隔离历史;不同目录同名文件不会混淆,指向同一脚本的安全符号链接共用历史。重命名后不会自动迁移历史。调试器另存为覆盖已有脚本时,目标旧内容进入历史,删除源文件前将其旧内容保留到现有 bak 目录;备份失败则不覆盖目标、不删除源文件。历史不能用于回收已删除的文件。 +- 按脚本规范化后的相对路径隔离历史;不同目录同名文件不会混淆,指向同一脚本的安全符号链接共用历史。重命名后不会自动迁移历史。调试器另存为覆盖已有脚本时,目标旧内容进入历史,删除源文件前将其旧内容保留到现有 bak 目录;备份失败则不覆盖目标、不删除源文件。源和目标实际指向同一脚本时(如安全符号链接或不同路径写法),按原地保存处理,保留两个路径,不删除源文件。历史不能用于回收已删除的文件。 - 历史记录支持至多 1 MiB 的 UTF-8 文本文件(含空文件)。当前文件或新内容超出范围时,界面要求明确确认“不记录历史直接保存”;取消则保持文件和草稿不变。确认后以 `skipHistory: true` 和服务端返回的当前哈希重试,锁内检查哈希以防确认期间覆盖他人修改。成功后明确提示本次未留档。大文件可以通过此流程缩小,后续恢复普通留档。未确认的 API 请求返回 413,并包含 `historyUnavailable` 和 `currentHash`;历史读取损坏等其他错误不允许用此选项绕过。 ## 存储与资源边界 diff --git a/test/back/script-history-api.test.cjs b/test/back/script-history-api.test.cjs index 0f6d4bfb..299ac289 100644 --- a/test/back/script-history-api.test.cjs +++ b/test/back/script-history-api.test.cjs @@ -201,6 +201,63 @@ test('script routes save, list, preview and restore, with validation and conflic 'small', ); } + for (const scenario of ['target-link', 'source-link', 'normalized-path']) { + await t.test( + `save-as preserves aliases of the same file: ${scenario}`, + async () => { + const sourceName = `${scenario}-source.js`; + const aliasName = `${scenario}-alias.js`; + const sourcePath = path.join(scripts, sourceName); + await fs.writeFile(sourcePath, 'source-original'); + if (scenario !== 'normalized-path') + await fs.symlink(sourceName, path.join(scripts, aliasName)); + const filename = + scenario === 'target-link' + ? aliasName + : scenario === 'normalized-path' + ? `./${sourceName}` + : sourceName; + const originFilename = + scenario === 'source-link' ? aliasName : sourceName; + const saved = await call( + '/scripts', + { filename, originFilename, path: '', content: 'source-edited' }, + 'POST', + ); + assert.equal(saved.status, 200); + assert.equal(saved.body.code, 200); + assert.equal(await fs.readFile(sourcePath, 'utf8'), 'source-edited'); + assert.equal( + await fs.readFile(path.join(scripts, filename), 'utf8'), + 'source-edited', + ); + if (scenario !== 'normalized-path') { + assert.equal( + await fs.readlink(path.join(scripts, aliasName)), + sourceName, + ); + const sourceHistory = await call( + `/scripts/history?filename=${sourceName}`, + ); + const aliasHistory = await call( + `/scripts/history?filename=${aliasName}`, + ); + assert.deepEqual(aliasHistory.body.data, sourceHistory.body.data); + } + const list = await call( + `/scripts/history?filename=${encodeURIComponent(filename)}`, + ); + assert.equal(list.body.data.versions.length, 1); + const preview = await call( + `/scripts/history/detail?filename=${encodeURIComponent( + filename, + )}&id=${list.body.data.versions[0].id}`, + ); + assert.equal(preview.body.data.version.content, 'source-original'); + assert.equal(preview.body.data.current, 'source-edited'); + }, + ); + } await t.test( 'save-as backs up the source and retains destination history before removing the source', async () => {