Skip to content

ui.View fails with 'Key must be a Buffer' when state.key is serialized through IPC #99

@blahah

Description

@blahah

Bug Description

When creating a ui.View (or ui.Window) from within an app, the state object is passed through Electron IPC serialization. This converts Buffer objects to plain objects like { type: 'Buffer', data: [...] }.

The session partition code in gui/gui.js uses hypercoreid.encode(this.state.key) which requires a Buffer, causing the error:

Error: Key must be a Buffer
    at Object.encode (hypercore-id-encoding/index.js)
    at View.open (gui/gui.js)

Steps to Reproduce

  1. Create a Pear desktop app that uses ui.View to open child views
  2. Stage and release the app (pear stage, pear release, pear seed)
  3. Run the staged app via pear run pear://...
  4. When the app tries to open a ui.View, it fails with "Key must be a Buffer"

Root Cause

In gui/gui.js (Window.open and View.open):

const session = electron.session.fromPartition(`persist:${this.sessname || (this.state.key ? hypercoreid.encode(this.state.key) : this.state.dir)}`)

When this.state.key is truthy (because it's a serialized Buffer object { type: 'Buffer', data: [...] }), hypercoreid.encode() is called, but it fails because the key is no longer a real Buffer after IPC serialization.

Fix

Check if state.key is a serialized Buffer object and convert it back to a real Buffer before encoding:

// Handle serialized Buffer from IPC (becomes { type: 'Buffer', data: [...] })
let stateKey = this.state.key
if (stateKey && stateKey.type === 'Buffer' && Array.isArray(stateKey.data)) {
  stateKey = Buffer.from(stateKey.data)
}
const session = electron.session.fromPartition(`persist:${this.sessname || (stateKey ? hypercoreid.encode(stateKey) : this.state.dir)}`)

Note: Using hypercoreid.normalize() alone doesn't work because normalize() internally calls decode(), which doesn't handle the { type: 'Buffer', data: [...] } format.

Environment

  • pear-electron version: 1.7.25
  • Platform: macOS (darwin-arm64)
  • Pear Runtime: latest

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