2026-10-20 11:20:55 +00:00
|
|
|
import { toProject } from '../../../../../app/lib/logic/project'
|
|
|
|
|
import type { CloudMeta } from '../../../../utils/accounts'
|
A settings for every part, overlap-free wiring with crossing styles, accounts with sync
- Clicking anything opens its settings: swap parts in place (wires stay on
matching pins), clock frequency per clock, keyboard keys for switches and
buttons, lamp/display colours, note sizes, component names on the canvas,
position, unlink all wires, unpack a component, make an independent copy.
- Wires of different signals never share a line: auto routes pick free
columns or detours, remaining clashes go on separate lanes. Crossings drawn
as bow, triangle, bridge, gap or plain.
- Accounts (email + scrypt password, httpOnly session) with two-way project
sync and conflict copies; per-account limits; cross-site writes refused.
- Compatibility suite: a project and both browser-storage layouts from
release a241adc must load and compute identically.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-20 11:09:37 +00:00
|
|
|
// Save a project to the account. `baseRev` is the revision the device last
|
|
|
|
|
// had; if another device saved in between, this answers 409 with the newer
|
|
|
|
|
// version's meta, unless `force` is set.
|
|
|
|
|
export default defineEventHandler(async event => {
|
|
|
|
|
rateLimit(event, 'save', 1200, 10 * 60_000)
|
|
|
|
|
const user = await requireUser(event)
|
|
|
|
|
const id = getRouterParam(event, 'id')
|
|
|
|
|
if (!isProjectId(id)) throw createError({ statusCode: 400, message: 'Bad project id' })
|
|
|
|
|
const raw = await readRawBody(event, 'utf8')
|
|
|
|
|
if (!raw || raw.length > 2_100_000) throw createError({ statusCode: 413, message: 'Project too large' })
|
2026-10-20 11:20:55 +00:00
|
|
|
let body: { project?: unknown; baseRev?: unknown; force?: unknown; link?: unknown; snapshot?: unknown }
|
A settings for every part, overlap-free wiring with crossing styles, accounts with sync
- Clicking anything opens its settings: swap parts in place (wires stay on
matching pins), clock frequency per clock, keyboard keys for switches and
buttons, lamp/display colours, note sizes, component names on the canvas,
position, unlink all wires, unpack a component, make an independent copy.
- Wires of different signals never share a line: auto routes pick free
columns or detours, remaining clashes go on separate lanes. Crossings drawn
as bow, triangle, bridge, gap or plain.
- Accounts (email + scrypt password, httpOnly session) with two-way project
sync and conflict copies; per-account limits; cross-site writes refused.
- Compatibility suite: a project and both browser-storage layouts from
release a241adc must load and compute identically.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-20 11:09:37 +00:00
|
|
|
try {
|
|
|
|
|
body = JSON.parse(raw)
|
|
|
|
|
} catch {
|
|
|
|
|
throw createError({ statusCode: 400, message: 'Not valid JSON' })
|
|
|
|
|
}
|
|
|
|
|
let project
|
|
|
|
|
try {
|
|
|
|
|
project = toProject(body.project)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw createError({ statusCode: 400, message: (e as Error).message })
|
|
|
|
|
}
|
|
|
|
|
const link = body.link as CloudMeta['link']
|
|
|
|
|
const cleanLink = link && isShareId(link.id) ? { id: link.id, ...(typeof link.key === 'string' ? { key: link.key } : {}) } : undefined
|
|
|
|
|
|
|
|
|
|
return withLock(user.id, async () => {
|
|
|
|
|
const index = await readIndex(user.id)
|
|
|
|
|
const existing = index.projects.find(p => p.id === id)
|
|
|
|
|
if (existing && existing.rev !== body.baseRev && body.force !== true) {
|
|
|
|
|
setResponseStatus(event, 409)
|
|
|
|
|
return { conflict: true, meta: existing }
|
|
|
|
|
}
|
|
|
|
|
if (!existing && index.projects.length >= MAX_PROJECTS) throw createError({ statusCode: 409, message: 'Too many projects in this account' })
|
|
|
|
|
const size = JSON.stringify(project).length
|
|
|
|
|
const others = index.projects.reduce((n, p) => n + (p.id === id ? 0 : (p.size ?? 0)), 0)
|
|
|
|
|
if (others + size > MAX_ACCOUNT_BYTES) throw createError({ statusCode: 413, message: 'Your account is full (100 MB). Delete some projects first.' })
|
|
|
|
|
const meta: CloudMeta = { id, name: project.name, updated: Date.now(), rev: (existing?.rev ?? 0) + 1, size }
|
|
|
|
|
if (cleanLink) meta.link = cleanLink
|
|
|
|
|
else if (existing?.link && body.link === undefined) meta.link = existing.link
|
2026-10-20 11:20:55 +00:00
|
|
|
// Savegame: keep the state this save replaces.
|
|
|
|
|
const previous = existing && (await readProject(user.id, id))
|
|
|
|
|
if (existing && previous) {
|
|
|
|
|
await keepVersion(user.id, id, { project: previous, rev: existing.rev, at: existing.updated, name: existing.name }, body.snapshot === true)
|
|
|
|
|
}
|
A settings for every part, overlap-free wiring with crossing styles, accounts with sync
- Clicking anything opens its settings: swap parts in place (wires stay on
matching pins), clock frequency per clock, keyboard keys for switches and
buttons, lamp/display colours, note sizes, component names on the canvas,
position, unlink all wires, unpack a component, make an independent copy.
- Wires of different signals never share a line: auto routes pick free
columns or detours, remaining clashes go on separate lanes. Crossings drawn
as bow, triangle, bridge, gap or plain.
- Accounts (email + scrypt password, httpOnly session) with two-way project
sync and conflict copies; per-account limits; cross-site writes refused.
- Compatibility suite: a project and both browser-storage layouts from
release a241adc must load and compute identically.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-20 11:09:37 +00:00
|
|
|
await accounts().setItem(projectKey(user.id, id), project)
|
|
|
|
|
index.projects = [meta, ...index.projects.filter(p => p.id !== id)]
|
|
|
|
|
index.deleted = index.deleted.filter(d => d.id !== id)
|
|
|
|
|
await writeIndex(user.id, index)
|
|
|
|
|
return { meta }
|
|
|
|
|
})
|
|
|
|
|
})
|