gun icon indicating copy to clipboard operation
gun copied to clipboard

Buffer is not defined in ison.js when using Vite build tool

Open davay42 opened this issue 2 years ago • 6 comments

Снимок экрана 2022-02-15 в 12 17 12

Store adapter isn't working in recent GitHub version. Throws the error on app initiation. @amark

Here's the repro: https://github.com/davay42/gun-vite-repro/blob/master/src/App.vue

davay42 avatar Feb 15 '22 09:02 davay42

Here's the hack that makes the new Gun v. 0.2020.1236 work with Vite. Had to use https://github.com/feross/buffer and https://github.com/YuzuJS/setImmediate for it. Seems ok as they're quite small in size.

davay42 avatar Mar 22 '22 21:03 davay42

That's pretty strange @amark: why introduce such dependencies node dependencies? It used to work without it before and adding these introduces polyfill so gunjs can work on a browser... (at least, as far as I can understand)

philippedasilva-orizone avatar Mar 22 '22 21:03 philippedasilva-orizone

As I understand the problem appears only for Vite build system. When people use the compiled Gun.js in the browser everything works fine.

davay42 avatar Mar 23 '22 08:03 davay42

As I understand the problem appears only for Vite build system.

I faced the same in rollup

atordvairn avatar Mar 23 '22 08:03 atordvairn

For those TypeScript users out there, just found out that instead of:

document.setImmediate = setImmediate 

which will throw an error, we can use:

window.setImmediate = setTimeout

Btw, it also avoids us to add a dependency to https://github.com/YuzuJS/setImmediate ;)

philippedasilva-orizone avatar Mar 23 '22 09:03 philippedasilva-orizone

@philippedasilva-orizone Thank you for the update! That works just fine! �

// polyfiils for Gun 0.2020.1236
import { Buffer } from 'buffer'
window.Buffer = Buffer
window.setImmediate = setTimeout

davay42 avatar Mar 23 '22 16:03 davay42