How to import GUN SEA with Deno Fresh?

148 Views Asked by At

The first attempt to import GUN from Fresh was to add the gun library from esm to import_map.json, where it correctly works in simple examples of using GUN.

{
  "imports": {
    ...
    "gun": "https://esm.sh/[email protected]",
  }
}

But the problem occurred when I wanted to import additionally gun/sea, After importing import Sea from "gun/sea";.

I got this error:

error: Uncaught (in promise) Error: Dynamic require of "./lib/text-encoding" is not supported

On GitHub I read to import gun/lib/mobile before importing SEA when such a problem occurs. But this brings an additional problem:

error: Uncaught (in promise) TypeError: Assignment to constant variable.

I checked the gun/lib/mobile file and it literally contains a few lines of global variables:

import Buffer from "buffer";
import { TextEncoder, TextDecoder } from "text-encoding";
global.Buffer = global.Buffer || Buffer.Buffer;
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;

Is there any chance to make SEA work on Fresh?

1

There are 1 best solutions below

0
Mohsin Ali On

Add following line inside imports in import_map.json file:

"@gun": "https://esm.sh/[email protected]"

Then use this import statement to import GUN in any route/component:

import GUN from "@gun"

Usage (from GUN documentation):

const gun = GUN();

gun.get('mark').put({
  name: "Mark",
  email: "[email protected]",
});

gun.get('mark').on((data, key) => {
  console.log("realtime updates:", data);
});

setInterval(() => { gun.get('mark').get('live').put(Math.random()) }, 9);