A component faces, project library with share links, fine-grained wire editing
- Lamps and hex displays inside a component show on its box with their
labels, lit by that instance's state; Counter 4 example shows its count.
- Several projects per browser; share links at /p/<id> that the creator's
edits save to automatically, others get their own copy. Export, open a
file as project, or add a file's components to the open project.
- Wires: drag any segment or corner, Alt+drag for a new bend, Ctrl+drag to
branch, double-click selects the whole signal, multi-select, arrow keys,
colour tags.
- Share-link API (POST/GET/PUT /api/p) on Nitro fs storage in data/, mounted
as a volume on edgerealm.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 11:21:49 +00:00
|
|
|
// Reading project data from files, links and storage. Shared by the browser
|
|
|
|
|
// and the share-link server, so it must stay free of browser APIs.
|
|
|
|
|
|
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
|
|
|
import { CROSSING_STYLES, LAMP_COLORS, WIRE_COLORS, type CircuitNode, type Project } from './types'
|
|
|
|
|
|
|
|
|
|
/** Drops optional node settings that are out of range, so a hand-edited file can't break the editor. */
|
|
|
|
|
function cleanNode(n: CircuitNode) {
|
|
|
|
|
if (n.hz !== undefined && !(typeof n.hz === 'number' && n.hz > 0 && n.hz <= 1000)) delete n.hz
|
|
|
|
|
if (n.color !== undefined && !LAMP_COLORS.includes(n.color)) delete n.color
|
|
|
|
|
if (n.key !== undefined && !(typeof n.key === 'string' && n.key.length === 1)) delete n.key
|
|
|
|
|
if (n.size !== undefined && !['sm', 'md', 'lg'].includes(n.size)) delete n.size
|
|
|
|
|
}
|
A component faces, project library with share links, fine-grained wire editing
- Lamps and hex displays inside a component show on its box with their
labels, lit by that instance's state; Counter 4 example shows its count.
- Several projects per browser; share links at /p/<id> that the creator's
edits save to automatically, others get their own copy. Export, open a
file as project, or add a file's components to the open project.
- Wires: drag any segment or corner, Alt+drag for a new bend, Ctrl+drag to
branch, double-click selects the whole signal, multi-select, arrow keys,
colour tags.
- Share-link API (POST/GET/PUT /api/p) on Nitro fs storage in data/, mounted
as a volume on edgerealm.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 11:21:49 +00:00
|
|
|
|
|
|
|
|
/** Upper bounds for a project that comes from outside. */
|
|
|
|
|
export const LIMITS = { bytes: 2_000_000, components: 2_000, nodes: 100_000 }
|
|
|
|
|
|
|
|
|
|
/** Checks and normalises project data. Throws with a readable message. */
|
|
|
|
|
export function toProject(data: unknown): Project {
|
|
|
|
|
const p = data as Partial<Project>
|
|
|
|
|
if (!p || typeof p !== 'object' || !Array.isArray(p.components) || p.components.length === 0) {
|
|
|
|
|
throw new Error('Not a Logigate project (no components)')
|
|
|
|
|
}
|
|
|
|
|
if (p.components.length > LIMITS.components) throw new Error('Too many components')
|
|
|
|
|
let nodes = 0
|
|
|
|
|
for (const d of p.components) {
|
|
|
|
|
if (typeof d?.id !== 'string' || typeof d.name !== 'string' || !Array.isArray(d.nodes) || !Array.isArray(d.wires)) {
|
|
|
|
|
throw new Error('Not a Logigate project (broken component)')
|
|
|
|
|
}
|
|
|
|
|
nodes += d.nodes.length
|
|
|
|
|
d.symbol ??= 'box'
|
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
|
|
|
for (const n of d.nodes) cleanNode(n)
|
A component faces, project library with share links, fine-grained wire editing
- Lamps and hex displays inside a component show on its box with their
labels, lit by that instance's state; Counter 4 example shows its count.
- Several projects per browser; share links at /p/<id> that the creator's
edits save to automatically, others get their own copy. Export, open a
file as project, or add a file's components to the open project.
- Wires: drag any segment or corner, Alt+drag for a new bend, Ctrl+drag to
branch, double-click selects the whole signal, multi-select, arrow keys,
colour tags.
- Share-link API (POST/GET/PUT /api/p) on Nitro fs storage in data/, mounted
as a volume on edgerealm.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 11:21:49 +00:00
|
|
|
for (const w of d.wires) if (w.color && !WIRE_COLORS.includes(w.color)) delete w.color
|
|
|
|
|
}
|
|
|
|
|
if (nodes > LIMITS.nodes) throw new Error('Project too large')
|
|
|
|
|
return {
|
|
|
|
|
version: 1,
|
|
|
|
|
name: typeof p.name === 'string' && p.name.trim() ? p.name.trim().slice(0, 80) : 'Untitled',
|
|
|
|
|
components: p.components,
|
|
|
|
|
openId: p.components.some(d => d.id === p.openId) ? p.openId! : p.components[0]!.id,
|
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
|
|
|
settings: {
|
|
|
|
|
symbols: p.settings?.symbols === 'ansi' ? 'ansi' : 'iec',
|
|
|
|
|
flow: p.settings?.flow ?? true,
|
|
|
|
|
...(p.settings?.crossing && CROSSING_STYLES.includes(p.settings.crossing) ? { crossing: p.settings.crossing } : {}),
|
|
|
|
|
},
|
A component faces, project library with share links, fine-grained wire editing
- Lamps and hex displays inside a component show on its box with their
labels, lit by that instance's state; Counter 4 example shows its count.
- Several projects per browser; share links at /p/<id> that the creator's
edits save to automatically, others get their own copy. Export, open a
file as project, or add a file's components to the open project.
- Wires: drag any segment or corner, Alt+drag for a new bend, Ctrl+drag to
branch, double-click selects the whole signal, multi-select, arrow keys,
colour tags.
- Share-link API (POST/GET/PUT /api/p) on Nitro fs storage in data/, mounted
as a volume on edgerealm.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 11:21:49 +00:00
|
|
|
solved: Array.isArray(p.solved) ? p.solved.filter(s => typeof s === 'string') : [],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function parseProject(json: string): Project {
|
|
|
|
|
if (json.length > LIMITS.bytes) throw new Error('File too large')
|
|
|
|
|
let data: unknown
|
|
|
|
|
try {
|
|
|
|
|
data = JSON.parse(json)
|
|
|
|
|
} catch {
|
|
|
|
|
throw new Error('Not a valid JSON file')
|
|
|
|
|
}
|
|
|
|
|
return toProject(data)
|
|
|
|
|
}
|