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
- Create a Pear desktop app that uses
ui.View to open child views
- Stage and release the app (
pear stage, pear release, pear seed)
- Run the staged app via
pear run pear://...
- 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
Bug Description
When creating a
ui.View(orui.Window) from within an app, thestateobject is passed through Electron IPC serialization. This convertsBufferobjects to plain objects like{ type: 'Buffer', data: [...] }.The session partition code in
gui/gui.jsuseshypercoreid.encode(this.state.key)which requires a Buffer, causing the error:Steps to Reproduce
ui.Viewto open child viewspear stage,pear release,pear seed)pear run pear://...ui.View, it fails with "Key must be a Buffer"Root Cause
In
gui/gui.js(Window.open and View.open):When
this.state.keyis 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.keyis a serialized Buffer object and convert it back to a real Buffer before encoding:Note: Using
hypercoreid.normalize()alone doesn't work becausenormalize()internally callsdecode(), which doesn't handle the{ type: 'Buffer', data: [...] }format.Environment