Skip to content

Instantly share code, notes, and snippets.

@zrrrzzt
Last active November 24, 2022 16:41
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zrrrzzt/6f88dc3cedee4ee18588236756d2cfce to your computer and use it in GitHub Desktop.
Save zrrrzzt/6f88dc3cedee4ee18588236756d2cfce to your computer and use it in GitHub Desktop.
Example code for restricting put on GUN
/* You'll need this on your client
Gun.on('opt', function (ctx) {
if (ctx.once) {
return
}
ctx.on('out', function (msg) {
var to = this.to
// Adds headers for put
msg.headers = {
token: 'thisIsTheTokenForReals'
}
to.next(msg) // pass to next middleware
})
})
*/
const port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8000
const Gun = require('gun')
function isValidPut (msg) {
return msg && msg && msg.headers && msg.headers.token && msg.headers.token === 'thisIsTheTokenForReals'
}
// Restrict put
Gun.on('opt', function (ctx) {
if (ctx.once) {
return
}
ctx.on('in', function (msg) {
var to = this.to
if (msg.put) {
if (isValidPut(msg)) {
to.next(msg)
}
} else {
to.next(msg)
}
})
})
const server = require('http').createServer(Gun.serve(__dirname));
Gun({
file: 'data.json',
web: server
})
server.listen(port)
console.log('Server started on port ' + port + ' with /gun')
@amark
Copy link

amark commented Jan 20, 2020

@zrrrzzt thanks for the responsiveness 👍 this was my fault. You're other repos are actually pretty popular reference points in the community (at least like once a month), thanks for merging, if you can still keep them public that'd be great. Have a good one!

@zrrrzzt
Copy link
Author

zrrrzzt commented Jan 20, 2020

okay

If it's helpful I'll leave them available :-) I will update this example as well.

And best of luck with your mission 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment