|
5 | 5 | import type { Hono } from "hono"; |
6 | 6 | import { bodyLimit } from "hono/body-limit"; |
7 | 7 | import { |
| 8 | + closeSync, |
8 | 9 | existsSync, |
| 10 | + ftruncateSync, |
| 11 | + openSync, |
9 | 12 | readFileSync, |
10 | 13 | writeFileSync, |
| 14 | + writeSync, |
11 | 15 | mkdirSync, |
12 | 16 | unlinkSync, |
13 | 17 | rmSync, |
@@ -1959,36 +1963,95 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void { |
1959 | 1963 | if ("error" in res) return res.error; |
1960 | 1964 |
|
1961 | 1965 | const body = await c.req.text(); |
1962 | | - const existed = existsSync(res.absPath); |
1963 | | - const currentContent = existed ? readFileSync(res.absPath, "utf-8") : null; |
1964 | | - const currentVersion = currentContent === null ? null : fileContentVersion(currentContent); |
1965 | 1966 | const expectedVersion = c.req.header("If-Match")?.trim() ?? null; |
1966 | 1967 | const createOnly = c.req.header("If-None-Match")?.trim() === "*"; |
1967 | 1968 | if (expectedVersion === null && !createOnly) { |
1968 | | - return c.json( |
1969 | | - { error: "precondition required", path: res.filePath, currentVersion, currentContent }, |
1970 | | - 428, |
1971 | | - ); |
1972 | | - } |
1973 | | - if ( |
1974 | | - (createOnly && existed) || |
1975 | | - (expectedVersion !== null && expectedVersion !== currentVersion) |
1976 | | - ) { |
| 1969 | + let currentContent: string | null = null; |
| 1970 | + try { |
| 1971 | + currentContent = readFileSync(res.absPath, "utf-8"); |
| 1972 | + } catch (error) { |
| 1973 | + if (!error || typeof error !== "object" || !("code" in error) || error.code !== "ENOENT") { |
| 1974 | + throw error; |
| 1975 | + } |
| 1976 | + } |
1977 | 1977 | return c.json( |
1978 | 1978 | { |
1979 | | - error: "file conflict", |
| 1979 | + error: "precondition required", |
1980 | 1980 | path: res.filePath, |
1981 | | - currentVersion, |
| 1981 | + currentVersion: currentContent === null ? null : fileContentVersion(currentContent), |
1982 | 1982 | currentContent, |
1983 | 1983 | }, |
1984 | | - 409, |
| 1984 | + 428, |
1985 | 1985 | ); |
1986 | 1986 | } |
1987 | 1987 |
|
1988 | | - ensureDir(res.absPath); |
1989 | | - const backup = snapshotBeforeWrite(res.project.dir, res.absPath); |
1990 | | - if (backup.error) console.warn(`Failed to create backup for ${res.filePath}: ${backup.error}`); |
1991 | | - writeFileSync(res.absPath, body, "utf-8"); |
| 1988 | + let backup: ReturnType<typeof snapshotBeforeWrite> = { backupPath: null }; |
| 1989 | + if (createOnly) { |
| 1990 | + ensureDir(res.absPath); |
| 1991 | + let fd: number; |
| 1992 | + try { |
| 1993 | + fd = openSync(res.absPath, "wx"); |
| 1994 | + } catch (error) { |
| 1995 | + if (!error || typeof error !== "object" || !("code" in error) || error.code !== "EEXIST") { |
| 1996 | + throw error; |
| 1997 | + } |
| 1998 | + const currentContent = readFileSync(res.absPath, "utf-8"); |
| 1999 | + return c.json( |
| 2000 | + { |
| 2001 | + error: "file conflict", |
| 2002 | + path: res.filePath, |
| 2003 | + currentVersion: fileContentVersion(currentContent), |
| 2004 | + currentContent, |
| 2005 | + }, |
| 2006 | + 409, |
| 2007 | + ); |
| 2008 | + } |
| 2009 | + try { |
| 2010 | + writeSync(fd, body, 0, "utf-8"); |
| 2011 | + } finally { |
| 2012 | + closeSync(fd); |
| 2013 | + } |
| 2014 | + } else { |
| 2015 | + let fd: number; |
| 2016 | + try { |
| 2017 | + fd = openSync(res.absPath, "r+"); |
| 2018 | + } catch (error) { |
| 2019 | + if (!error || typeof error !== "object" || !("code" in error) || error.code !== "ENOENT") { |
| 2020 | + throw error; |
| 2021 | + } |
| 2022 | + return c.json( |
| 2023 | + { |
| 2024 | + error: "file conflict", |
| 2025 | + path: res.filePath, |
| 2026 | + currentVersion: null, |
| 2027 | + currentContent: null, |
| 2028 | + }, |
| 2029 | + 409, |
| 2030 | + ); |
| 2031 | + } |
| 2032 | + try { |
| 2033 | + const currentContent = readFileSync(fd, "utf-8"); |
| 2034 | + const currentVersion = fileContentVersion(currentContent); |
| 2035 | + if (expectedVersion !== currentVersion) { |
| 2036 | + return c.json( |
| 2037 | + { |
| 2038 | + error: "file conflict", |
| 2039 | + path: res.filePath, |
| 2040 | + currentVersion, |
| 2041 | + currentContent, |
| 2042 | + }, |
| 2043 | + 409, |
| 2044 | + ); |
| 2045 | + } |
| 2046 | + backup = snapshotBeforeWrite(res.project.dir, res.absPath); |
| 2047 | + if (backup.error) |
| 2048 | + console.warn(`Failed to create backup for ${res.filePath}: ${backup.error}`); |
| 2049 | + ftruncateSync(fd, 0); |
| 2050 | + writeSync(fd, body, 0, "utf-8"); |
| 2051 | + } finally { |
| 2052 | + closeSync(fd); |
| 2053 | + } |
| 2054 | + } |
1992 | 2055 | const version = fileContentVersion(body); |
1993 | 2056 | const writeToken = createWriteToken(c.req.header("X-Hyperframes-Write-Token")); |
1994 | 2057 | recordFileWriteReceipt(res.absPath, { path: res.filePath, version, writeToken }); |
|
0 commit comments