fix(host-api-wrapper): return undefined from readJSON for absent keys#226
Merged
Conversation
`readJSON` piped the read straight into `JSON.parse`, but a never-written
or cleared key reads back as `undefined` and an empty value decodes to
`''` — so `JSON.parse('')` threw `SyntaxError: Unexpected end of JSON
input` on the normal first-run path. Products had to wrap every read in a
try/catch just to handle "no value yet".
Guard the absent/empty case and return `undefined` instead. Genuinely
malformed (non-empty) data still throws, so real corruption stays visible.
Claude-Session: https://claude.ai/code/session_01SqQTbeaAxijCffoCcp6Hyt
kalininilya
approved these changes
Jul 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
hostLocalStorage.readJSON(key)piped the read straight intoJSON.parse:A never-written (or cleared) key reads back as
undefined, and an empty value decodes to''.JSON.parse('')throwsSyntaxError: Unexpected end of JSON input— so the normal first-run path (key not written yet) surfaced as an error. Every product usingreadJSONfor optional/first-run data had to wrap reads in atry/catchjust to get a default.This was observed in the wild via a product's forwarded console:
[coinflipgame03.dot] Error reading from localStorage: SyntaxError: Unexpected end of JSON inputon first launch, beforeflip_statshad ever been saved.Fix
Guard the absent/empty case in
readJSONand returnundefinedinstead of parsing:undefinedundefinedTests
Added two cases to
__tests__/hostApi/localStorage.spec.ts(missing key, empty value). Both reproduced the exactSyntaxErrorbefore the fix and pass after. Full suite green (673 passing), lint + typecheck clean.https://claude.ai/code/session_01SqQTbeaAxijCffoCcp6Hyt