amark / gun

An open source cybersecurity protocol for syncing decentralized graph data.
https://gun.eco/docs
Other
18.05k stars 1.16k forks source link

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

Open davay42 opened 2 years ago

davay42 commented 2 years ago
Снимок экрана 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 commented 2 years ago

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.

philippedasilva-orizone commented 2 years ago

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)

davay42 commented 2 years ago

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

atordvairn commented 2 years ago

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

I faced the same in rollup

philippedasilva-orizone commented 2 years ago

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 ;)

davay42 commented 2 years ago

@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