Skip to content

keiko: Storage writes are dropped, since the game's globalThis.localStorage is Node's (undefined), not the page's #130

Backlog

Description

@LoopyJack

What

Under keiko, Storage from @debuffjp/core never writes anything: a game's saves live only in memory, so a logic test can't check that a save was written, and nothing reaches the page's localStorage (game.window.localStorage).

Storage reads and writes globalThis.localStorage (storage.js, local() and write()). When keiko runs the game, that is Node's own global, not the page's: on Node 26 it is undefined unless Node was started with --localstorage-file, and reading it prints ExperimentalWarning: localStorage is not available because --localstorage-file was not provided. So write() returns false and every set is dropped. The page keiko builds has its own localStorage on game.window, which stays empty.

Reproduction

Any game that saves with new Storage(key, defaults) and save.set(...) when something happens, plus:

import { play, test } from "keiko";

test("a save reaches the page's localStorage", async () => {
  const game = await play();
  const before = {
    globalHasLocalStorage: (globalThis as { localStorage?: unknown }).localStorage !== undefined,
    windowIsGlobal: (game.window as unknown) === globalThis,
    page: game.window.localStorage.length,
  };
  // ...play until the game calls save.set(...)...
  throw new Error(JSON.stringify({ before, after: { page: game.window.localStorage.length } }));
});

Here, after a run that ended with save.set("best", 156):

{"before":{"globalHasLocalStorage":false,"windowIsGlobal":false,"page":0},"after":{"page":0}}

plus the Node warning above.

What would help

  • Point the game's globalThis.localStorage at the page's storage (as for window/document), or have Storage use window.localStorage when a window exists. Then a test can read back what a game saved.
  • An option on play() to start with given storage contents (or to keep storage from the previous play()), so "the best score survives a reload" can be a logic test. Today each play() starts empty, which is the right default.

Versions

debuff 0.35.0, keiko 0.9.0, Node 26.13.0, Linux.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions