@etherplay/connect 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +44 -0
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -333,6 +333,50 @@ The connection store is a Svelte store and works seamlessly with Svelte's reacti
333
333
  {/if}
334
334
  ```
335
335
 
336
+ ## Server-side rendering (SSR)
337
+
338
+ `createConnection(...)` is safe to call in any JavaScript environment, including a Node SSR / prerender pass with no DOM. This is a tested property of the package (`test/ssr-inert.test.ts`), not an accident: the regression test runs in vitest's `node` environment with no `window`, `document`, `localStorage` or `sessionStorage`, constructs both supported configurations, and asserts no throw, the exact initial store value, no storage access, and no pending timers/intervals.
339
+
340
+ ### What is guaranteed off-browser
341
+
342
+ - **Construction never throws and does no I/O.** No `window`, `document`, `localStorage`, `sessionStorage`, `navigator`, `crypto`, timers, intervals, or network requests are touched during `createConnection(...)`. The only thing it builds is in-memory state.
343
+ - **Nothing auto-connects without a `window`.** The entire auto-connect block (which reads saved accounts / last wallet from `localStorage` and polls installed wallets) is behind a `typeof window !== 'undefined'` guard, as is `fetchWallets()` in the Ethereum connector. Off-browser both are no-ops.
344
+ - **The store rests at `{step: 'Idle', loading: true, wallets: []}`.** This is the exact same value a browser renders on its very first paint (before the auto-connect promise has resolved), so a server-rendered app hydrates with no store mismatch.
345
+
346
+ ### Why `loading` stays `true` off-browser
347
+
348
+ Off-browser the auto-connect block is skipped entirely (no `window`), so the store keeps its initial `loading: true` rather than transitioning to `loading: false`. This is deliberate: it matches the browser's first render exactly. A browser also starts at `loading: true` and only flips to `false` once auto-connect has run its course. Keeping the server value identical means SSR output and the first client render agree, and hydration does not flash or remount.
349
+
350
+ > **Hydration-visible breaking change:** the value of `loading` (and the shape of the initial store object) at construction is part of the SSR contract. Changing it — for example resting at `loading: false` off-browser, or adding/removing a field — produces a mismatch between server-rendered and client-hydrated markup for any consumer that renders the store during SSR. Treat such a change as a breaking change for consumers and version it accordingly.
351
+
352
+ With `autoConnect: false` the behaviour differs: the explicit `else` branch sets `loading: false` immediately, since there is nothing to wait for. This is also deterministic and SSR-safe.
353
+
354
+ ### What is only reachable from user-initiated flows
355
+
356
+ The following DOM/browser access exists in the package but is only ever reached when a user acts (calling `connect()`, `requestSignature()`, `disconnect()`, etc.), never from `createConnection()` itself:
357
+
358
+ - `localStorage` / `sessionStorage` reads and writes (`getOriginAccount`, `saveOriginAccount`, `deleteOriginAccount`, `getLastWallet`, `saveLastWallet`, `deleteLastWallet`). These helpers reference the storage globals directly; they are only invoked from the window-guarded auto-connect path or from explicit connect/disconnect actions.
359
+ - `window.crypto?.subtle` (the Same-Origin Callback Bridge key setup in `connect()`), guarded by `typeof window !== 'undefined' && window.crypto?.subtle`.
360
+ - `window.open`, `window.addEventListener('message', ...)`, `BroadcastChannel`, and `window.origin` in the popup launcher (`popup.ts`), used only from `connect()`.
361
+ - `location.href` in `connectViaPopup`, used only from `connect()`.
362
+ - The Web-Crypto helpers in `@etherplay/connect-core` (`generateEcdhKeyPair`, `exportPublicKeyB64`, `importPublicKeyB64`, `deriveAesKey`), which call `window.crypto.subtle`; only invoked from the domain-redirect bridge path inside `connect()`.
363
+ - The Openfort provider (`@etherplay/openfort`) only touches `window` / `location` from its `connect()` / `confirmOAuth()` methods, and `createOpenfortProvider()` is not invoked by `createConnection()` at all.
364
+
365
+ No DOM shim is required or recommended. The goal is genuine environment independence, not a simulated browser.
366
+
367
+ ### `connection.provider.request(...)` off-browser (intended behaviour — read this)
368
+
369
+ The `provider` exposed on the store is the **always-on** provider. Its purpose is to serve read-only JSON-RPC (`eth_call`, `eth_blockNumber`, `eth_getBalance`, etc.) against the configured `chainInfo` RPC URL _even before a wallet is connected_ — that is its design. Non-signing methods are routed to the JSON-RPC endpoint via `fetch`.
370
+
371
+ Off-browser, Node provides a real global `fetch`, so `connection.provider.request(...)` for a non-signing method **performs a real network RPC request from the server**. Signing / wallet-only methods (`personal_sign`, `eth_sendTransaction`, `eth_requestAccounts`, `wallet_switchEthereumChain`, `wallet_addEthereumChain`, …) correctly reject with `wallet provider is not connected` when no wallet is set, so those are safe off-browser.
372
+
373
+ **This is intended and supported**, and the behaviour is deliberately left as-is:
374
+
375
+ - The always-on read path is a feature: a server render that needs chain state can read it without a wallet. Making `provider.request` reject off-browser would break legitimate server-side reads for consumers that want them.
376
+ - The package does not, and cannot, know whether a given consumer wants SSR-time reads or a fully IO-free render. It leaves that decision to the consumer.
377
+
378
+ **Footgun to be aware of:** if your SSR / prerender pass touches `connection.provider` (directly or via code that reads chain state), it will do live RPC IO at build/render time — hitting the configured RPC endpoint, subject to its rate limits, and requiring network at build time. If you want a fully IO-free SSR (as the `jolly-roger` template does — it constructs the runtime but never uses the provider during SSR), simply do not call `provider.request(...)` during the server render, or guard such calls with `typeof window !== 'undefined'` / a `connection.step` check. Constructing the store alone never touches the provider.
379
+
336
380
  ## Utility Exports
337
381
 
338
382
  ```typescript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etherplay/connect",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Main connection library for Etherplay Accounts - provides seamless wallet and social authentication with session account management",
5
5
  "keywords": [
6
6
  "web3",
@@ -45,8 +45,8 @@
45
45
  ],
46
46
  "dependencies": {
47
47
  "@etherplay/connect-core": "0.1.0",
48
- "@etherplay/wallet-connector-ethereum": "0.0.11",
49
- "@etherplay/wallet-connector": "0.0.5"
48
+ "@etherplay/wallet-connector": "0.0.5",
49
+ "@etherplay/wallet-connector-ethereum": "0.0.11"
50
50
  },
51
51
  "peerDependencies": {
52
52
  "svelte": "^5.0.0"