initial commit

This commit is contained in:
Jesse D. McDonald 2020-04-04 04:18:36 -05:00
commit b43eee044c
4 changed files with 6424 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
node_modules
journal.txt
*.bak
*.bak2
radata/
stats.radata
.*.swp
.*.swo
*~

135
index.js Normal file
View File

@ -0,0 +1,135 @@
;(function(){
var cluster = require('cluster');
if(cluster.isMaster){
return cluster.fork() && cluster.on('exit', function(){ cluster.fork(); require('../lib/crashed'); });
}
var config = { port: process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765 };
var fs = require('fs');
var Gun = require('gun');
var SEA = require('gun/sea');
var NTS = require('gun/nts');
var rtc = require('gun/lib/webrtc');
if(process.env.HTTPS_KEY){
config.key = fs.readFileSync(process.env.HTTPS_KEY);
config.cert = fs.readFileSync(process.env.HTTPS_CERT);
config.server = require('https').createServer(config, Gun.serve(__dirname));
} else {
config.server = require('http').createServer(Gun.serve(__dirname));
}
Gun.chain.onWithCancel = (function() {
function cancelCallback(data,key,msg,ev) {
if (ev && typeof ev.off === 'function') {
ev.off();
}
}
return function(tag, arg, eas, as) {
if (typeof tag === 'function') {
let callback = tag;
const cancelEv = function() {
callback = cancelCallback;
};
const wrapper = function() {
return callback.apply(this, arguments);
};
this.on(wrapper, arg, eas, as);
return cancelEv;
} else {
this.on(tag, arg, eas, as);
return null;
}
};
})();
let lastJournalText = null;
function appendJournal(text) {
if (text !== lastJournalText) {
fs.appendFileSync('journal.txt', text + '\n');
lastJournalText = text;
}
}
var gun = Gun({
web: config.server.listen(config.port),
peers: ['https://jessemcdonald.info/gun'],
});
console.log('Relay peer started on port ' + config.port + ' with /gun');
function logIn(msg){
console.log(`in msg:${JSON.stringify(msg)}.........`);
}
function logOut(msg){
console.log(`out msg:${JSON.stringify(msg)}.........`);
}
function logPeers() {
console.log(`Peers: ${Object.keys(gun._.opt.peers).join(', ')}`);
}
function logData() {
console.log(`In Memory: ${JSON.stringify(gun._.graph)}`);
}
//gun._.on('in', logIn);
//gun._.on('out', logOut);
//setInterval(logPeers, 5000); //Log peer list every 5 secs
//setInterval(logData, 20000); //Log gun graph every 20 secs
const PacoSakoUUID = 'b425b812-6bdb-11ea-9414-6f946662bac3';
let cancellers = {};
gun.get(PacoSakoUUID + '/meta').on(function(meta) {
for (const gameId in meta) { /* use of 'in' here is deliberate */
/* 'gameId' may include extra GUN fields like '_' */
if (gameId.match(/^[0-9a-f]{16}$/)) {
if (!Gun.obj.is(meta[gameId])) {
appendJournal(JSON.stringify({ meta: { [gameId]: null } }));
if (gameId in cancellers) {
cancellers[gameId]();
delete cancellers[gameId];
}
} else if (!(gameId in cancellers)) {
let cancelMeta = gun.get(meta[gameId]).onWithCancel(function(data) {
let text;
try {
let clean = null;
if (data !== null) {
clean = {};
for (const k of Object.keys(data).sort()) {
if (k !== '_') {
clean[k] = data[k];
}
}
}
text = JSON.stringify({ meta: { [gameId]: clean } });
} catch(err) {}
if (text) {
appendJournal(text);
}
});
let cancelGame = gun.get(PacoSakoUUID + '/game/' + gameId).onWithCancel(function(data) {
if (data && typeof data.board === 'string') {
let text;
try {
text = JSON.stringify({ game: { [gameId]: { board: JSON.parse(data.board) } } });
} catch(err) {}
if (text) {
appendJournal(text);
}
}
});
cancellers[gameId] = function() { cancelMeta(); cancelGame(); };
}
}
}
}, { change: true });
}());

6261
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

19
package.json Normal file
View File

@ -0,0 +1,19 @@
{
"name": "code",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"core-js": "^3.6.4",
"gun": "^0.2020.301",
"gun-db": "^1.0.571",
"react": "^16.9.0",
"react-native": "^0.61.5"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}