gun

how to add attributes to a PUT request in GUN?


I have the following code in my HTML page

    Gun.on('opt', function (ctx) {
        if (ctx.once) {
            return
        }
        this.to.next(ctx)
        window.auth = ctx.opt.auth
        ctx.on('get', function (msg) {
            msg.auth = window.auth
            this.to.next(msg)
        })
        ctx.on('put', function (msg) {
            msg.put.auth = window.auth
            this.to.next(msg)
        })
    })

    var gun = Gun({
        peers: ['http://localhost:8765/gun'],
        auth: {
            user: 'mroon',
            password: 'titi'
        }
    })

On the server, I simply watch the requests

Gun.on('create', function(db) {
    console.log('gun created')
    this.to.next(db);

    db.on('get', function(request) {
        // this request contains the auth attribute from the client
        this.to.next(request);
    });
    db.on('put', function(request) {
        // this request does not contain the auth attribute from the client
        this.to.next(request);
    });
});

every time I query the graph with gun.get('someAttribute') the request on the server contains the auth attribute.

but when a gun.get('someAttribute').put({attribute: 'my new value'}) is called, the request on the server does not contain the auth attribute.

How can I add the auth attribute to the put request in such a way that all the peers will get it too?


Solution

  • @micha-roon you jumped straight to GUN's core/internal wire details, which is not the easiest thing to start with, but here is something I do that I'm guessing is what you are looking for:

    (if not, please just comment & I'll update)

    What this does is it adds a DEBUG flag to all outbound messages in GUN, you can change this to add other metadata or info

    Gun.on('opt', function(root){
      if(!root.once){
        root.on('out', function(msg){
          msg.DBG = msg.DBG || +new Date;
          this.to.next(msg);
        });
      }
      this.to.next(root);
    })
    

    Also another good reference: https://github.com/zrrrzzt/bullet-catcher