Skip to content

Commit

Permalink
Fix a maddeningly subtle URL parsing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnyby committed Apr 9, 2024
1 parent 6109748 commit dc1a473
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
6 changes: 6 additions & 0 deletions docs/assets/index-CzsmwQDA.js

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions docs/assets/index-Tcjc4s4K.js

This file was deleted.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<link rel="icon" type="image/svg+xml" href="/fivel/favicon.svg" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<script type="module" crossorigin src="/fivel/assets/index-Tcjc4s4K.js"></script>
<script type="module" crossorigin src="/fivel/assets/index-CzsmwQDA.js"></script>
<link rel="stylesheet" crossorigin href="/fivel/assets/index-BwGB0A6o.css">
</head>

Expand Down
10 changes: 8 additions & 2 deletions src/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,14 @@ const _createKeyColors = () => {
};
};

const _urlParams = new URLSearchParams(window.location.search);
const _serializedGameConfig = _urlParams.get("f");
// Using URLSearchParams might seem easier than manually parsing this URL. Unfortunately, URLSearchParams parses the
// URL, and that includes decoding values. If our Base64 includes a "+", this is bad news. That plus will be decoded as
// a space. That is *not* valid Base64 and will break when passed into GameConfig.deserialize().
let _serializedGameConfig: string | null = null;
if (location.search.length > 3) {
// Trim the "?f=" from our search
_serializedGameConfig = location.search.substring(3);
}

export const guesses = _createGuesses();
export const guessesAreExhausted = derived(guesses, $guesses => $guesses.every(guess => guess.isSubmitted));
Expand Down
3 changes: 2 additions & 1 deletion src/util/GameConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export default class GameConfig {

static deserialize(s: string) {
const binaryString = atob(s);
const bytes = Uint8Array.from(binaryString, (m) => m.codePointAt(0) ?? 0);
// 32 represents the space character in UTF-8
const bytes = Uint8Array.from(binaryString, (m) => m.codePointAt(0) ?? 32);
const jsonString = new TextDecoder().decode(bytes);
const json = JSON.parse(jsonString);
return new GameConfig(json.word, json.gameId, json.hint);
Expand Down

0 comments on commit dc1a473

Please sign in to comment.