Synchronous, per-app, file-backed string storage. morphStorage is a native global: it is available without an import and works in module initializers, component bodies, and event handlers.
morphStorage.setItem("theme", "dark")
const theme = morphStorage.getItem("theme") // "dark" | nulllocalStorage?localStorage is still rejected with mx-js-global. Its contract includes property syntax (localStorage.theme), bracket access (localStorage["theme"]), a cross-window storage event, origin-scoped storage, and quota exceptions. Morph cannot reproduce those promises, so the API gets a distinct name under Name Follows Behavior.
localStorage |
morphStorage |
|
|---|---|---|
| Access | Property and method | Methods only |
| Scope | Browser origin | One file per app |
| Values | Strings | Strings |
length |
Property | length() method |
| Write result | Throws on quota | setItem returns false |
| Cross-window notification | storage event |
None |
| Capacity | Browser quota | 1 MiB of key + value bytes |
morphStorage is a namespace, not a value. Only these direct calls lower:
morphStorage.getItem(key): string | null
morphStorage.setItem(key, value): boolean
morphStorage.removeItem(key): undefined
morphStorage.clear(): undefined
morphStorage.key(index): string | null
morphStorage.length(): number| Method | Parameters | Returns | Behavior |
|---|---|---|---|
getItem |
key |
string | null |
Stored string, or null when absent |
setItem |
key, value |
boolean |
true on write, false on cap/write failure |
removeItem |
key |
undefined |
Removes the key; missing keys are a no-op |
clear |
— | undefined |
Removes every key |
key |
index |
string | null |
Key at an insertion-order index, or null out of range |
length |
— | number |
Number of stored keys |
Keys and values are converted to strings with Morph's runtime conversion. Strings, numbers, booleans, null, and undefined match JavaScript String() exactly. Arrays become "[object Array]", objects "[object Object]", and functions "function". setItem(7, 42) stores the key "7" with the value "42". Stored values keep whitespace, newlines, and embedded NUL bytes; reads return exactly what was written.
The store is loaded lazily on first use and written after every mutation. One file per app:
| Platform | Path |
|---|---|
MORPH_STORAGE_DIR set |
$MORPH_STORAGE_DIR/<app>/storage.dat |
| Linux | $XDG_DATA_HOME/morph/<app>/storage.dat, or ~/.local/share/morph/<app>/storage.dat |
| macOS | ~/Library/Application Support/morph/<app>/storage.dat |
| Windows | %APPDATA%\morph\<app>\storage.dat |
<app> is the cleaned name from morph.config.json (spaces and unsupported characters become _). MORPH_STORAGE_DIR exists for tests and explicit overrides; normal apps do not set it.
Writes are atomic: the new file is written beside the target and renamed over it. A failed write leaves the previous file and the in-memory store unchanged. A missing file is an empty store. A corrupt or unreadable file is treated as empty rather than aborting the app; the next successful write replaces it.
The on-disk format is private. It is not JSON, not localStorage's format, and not a compatibility surface.
The store is capped at 1 MiB of key plus value bytes across all entries. setItem returns false when the new entry would exceed the cap or when the file cannot be written; it does not throw, and the previous value (if any) stays intact. There is no quota exception to catch.
import { morphState } from 'morph'
export const windowConfig = { title: "Settings", width: 640, height: 480 }
// Module scope: runs once at startup.
const savedTheme = morphStorage.getItem("theme")
console.log("stored theme:", savedTheme)
export default function Settings() {
const [theme, setTheme] = morphState(savedTheme ?? "light")
const save = () => {
const ok = morphStorage.setItem("theme", theme)
console.log("saved:", ok)
}
const reset = () => {
morphStorage.removeItem("theme")
setTheme("light")
}
return (
<body>
<text>Theme: {theme}</text>
<text>Keys: {morphStorage.length()}</text>
<button onClick={save}>Save</button>
<button onClick={reset}>Reset</button>
</body>
)
}// Iterate keys in insertion order.
for (let i = 0; i < morphStorage.length(); i++) {
const name = morphStorage.key(i)
if (name !== null) {
console.log(name, morphStorage.getItem(name))
}
}morph check rejects anything outside the method-only surface, so unsupported code never compiles to a different meaning:
| Form | Error |
|---|---|
morphStorage.getItemX(...) |
mx-js-member |
morphStorage.length (property) |
mx-js-member |
morphStorage["getItem"](...) |
mx-js-member |
const store = morphStorage |
mx-js-member |
morphStorage.getItem() (wrong arity) |
mx-js-syntax |
morphStorage.setItem(...args) (spread) |
mx-js-syntax |
localStorage, sessionStorage, and the storage event remain browser globals and are rejected with mx-js-global.