Skip to content

Commit eb6d6a9

Browse files
committed
support env vars
1 parent b1b4089 commit eb6d6a9

File tree

7 files changed

+50
-39
lines changed

7 files changed

+50
-39
lines changed

examples/install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ git clone https://github.com/isaacs/nave.git
3333
# If you just want nodejs and npm but not gun, stop here.
3434
#npm install gun@latest
3535
#cd ./node_modules/gun
36+
> relay.env
3637
mkdir node_modules
3738
git clone https://github.com/amark/gun.git
3839
cd gun

examples/relay.service

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ WantedBy=multi-user.target
88

99
[Service]
1010
Environment=PATH=/usr/bin:/usr/local/bin
11+
EnvironmentFile=%h/relay.env
1112
LimitNOFILE=infinity
1213
LimitNPROC=infinity
13-
LimitCORE=infinity
14+
LimitCORE=0
1415
StartLimitBurst=999999
1516
StartLimitIntervalSec=999999
1617
Restart=always

lib/axe.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -240,35 +240,6 @@ function start(root){
240240
});
241241

242242
}());
243-
244-
;(function(){ // THIS IS THE UNIVERSAL NOTIFICATION MODULE
245-
var to = {}, key = {}, email = require('./email');
246-
if(email.err){ return }
247-
mesh.hear['tag'] = function(msg, peer, who){
248-
if(who = key[msg.key]){ who.rate = Math.max(msg.rate||1000*60*15, 1000*60); return }
249-
if(!msg.src || !msg.email){ return }
250-
if(+new Date < peer.emailed + 1000*60*2){ mesh.say({dam:'tag',err:'too fast'},peer); return } // peer can only send notifications > 2min
251-
var src; try{ src = new URL(msg.src = msg.src.split(/\s/)[0]); } catch(e){ return } // throws if invalid URL.
252-
(who = (to[msg.email] = to[msg.email] || {go:{}})).go[''+src] = 1; // we're keeping in-memory for now, maybe will "stay" to disk in future.
253-
peer.emailed = +new Date;
254-
if(who.batch){ return }
255-
key[who.key = Math.random().toString(36).slice(2)] = who;
256-
who.batch = setTimeout(function(){
257-
email.send({
258-
from: process.env.EMAIL,
259-
to: msg.email,
260-
subject: "Notification:",
261-
text: 'Someone or a bot tagged you at: (⚠️ only click link if you recognize & trust it ⚠️)\n'+
262-
'[use #'+who.key+' to unsubscribe please mute this thread by tapping the top most "⋮" button and clicking mute]\n\n' +
263-
Object.keys(who.go).join('\n'), // TODO: NEEDS TO BE CPU SCHEDULED
264-
headers: {'message-id': '<[email protected]>'} // hardcode id so all batches also group into the same email thread to reduce clutter.
265-
}, function(err, r){
266-
who.batch = null; who.go = {};
267-
err && console.log("email TAG:", err);
268-
});
269-
}, who.rate || (1000*60*60*24)); // default to 1 day
270-
};
271-
}());
272243
};
273244

274245
;(function(){

lib/crashed.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
;(function(){ try {
2-
var fs = require('fs'), logs = [], up = __dirname+'/../';
2+
var fs = require('fs'), logs = [], up = require('os').homedir()+'/';
3+
;(function(){ try{ if(!process.env.EMAIL){ return } // ONLY EMAIL IF DEVELOPER OPTS IN!!!
4+
require('./email').send({ text: "",
5+
from: process.env.EMAIL,
6+
7+
subject: "RELAY CRASH"
8+
}, noop);}catch(e){}; }());
9+
return;
310
fs.readdir(up, function(err, list){ try{
411
var i = 0, f; while(f = list[i++]){
512
if(0 === f.indexOf('isolate-') && '.log' === f.slice(-4)){ logs.push(f) }

lib/service.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ module.exports = function(root){
22
var mesh = root.opt.mesh, cmd = {}, run = require('child_process').exec, fs = require('fs'), home = require('os').homedir(), examp = require('path').resolve(__dirname, '../examples');
33
mesh.hear['service'] = function(msg, peer){
44
if(!fs.existsSync('/lib/systemd/system/relay.service')){
5-
mesh.say({dam: '!', err: "Not serviced."});
5+
mesh.say({dam: '!', err: "Not serviced."}, peer);
66
return;
77
}
8-
try{ (cmd[msg.try]||cmd.any)(msg, peer); }catch(err){ mesh.say({dam: '!', err: "service error: "+err}) }
8+
try{ (cmd[msg.try]||cmd.any)(msg, peer); }catch(err){ mesh.say({dam: '!', err: "service error: "+err}, peer) }
99
}
1010
cmd.https = function(msg, peer){ var log;
1111
if(!msg.email || !msg.domain){
12-
mesh.say({dam: '!', err: 'Domain/email missing, use `location.hostname`!'});
12+
mesh.say({dam: '!', err: 'Domain/email missing, use `location.hostname`!'}, peer);
1313
return;
1414
}
1515
if(fs.existsSync(home+'/cert.pem')){
16-
mesh.say({dam: '!', err: 'Cert already exists.'});
16+
mesh.say({dam: '!', err: 'Cert already exists.'}, peer);
1717
return;
1818
}
1919
fs.writeFile(examp+'/../email', msg.email, function(){});
@@ -33,15 +33,17 @@ module.exports = function(root){
3333
setTimeout(function(){ process.exit() },999);
3434
});
3535
}
36-
;(function update(){ var last;
36+
setTimeout(function update(){ var last;
3737
if(!fs.existsSync(home+'/cert.pem')){ return }
3838
setTimeout(update, 1000*60*60*24);
3939
last = root.stats.stay.updated || 0;
40-
if(+new Date - last < 1000*60*60*24*15){ return }
40+
if(+new Date - last < 1000*60*60*24*15){ return } // try to update every half month
4141
root.stats.stay.updated = +new Date;
4242
run("bash "+examp+"/install.sh", {}, function(){});
43-
}());
43+
},999);
4444

4545
cmd.any = function(){};
4646

47+
// list other modules here:
48+
require('./tag')(root);
4749
};

lib/tag.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module.exports = function(root){ // THIS IS THE UNIVERSAL NOTIFICATION MODULE
2+
var to = {}, key = {}, mesh = root.opt.mesh, email = require('./email');
3+
if(email.err){ return }
4+
mesh.hear['tag'] = function(msg, peer, who){
5+
if(who = key[msg.key]){ who.rate = Math.max(msg.rate||1000*60*15, 1000*60); return }
6+
if(!msg.src || !msg.email){ return }
7+
if(+new Date < peer.emailed + 1000*60*2){ mesh.say({dam:'tag',err:'too fast'},peer); return } // peer can only send notifications > 2min
8+
var src; try{ src = new URL(msg.src = msg.src.split(/\s/)[0]); } catch(e){ return } // throws if invalid URL.
9+
(who = (to[msg.email] = to[msg.email] || {go:{}})).go[''+src] = 1; // we're keeping in-memory for now, maybe will "stay" to disk in future.
10+
peer.emailed = +new Date;
11+
mesh.say({dam:'tag',ok:'queued in-memory'},peer);
12+
if(who.batch){ return }
13+
key[who.key = Math.random().toString(36).slice(2)] = who;
14+
who.batch = setTimeout(function(){
15+
email.send({
16+
from: process.env.EMAIL,
17+
to: msg.email,
18+
subject: "Notification:",
19+
text: 'Someone or a bot tagged you at: (⚠️ only click link if you recognize & trust it ⚠️)\n'+
20+
'[use #'+who.key+' to unsubscribe please mute this thread by tapping the top most "⋮" button and clicking mute]\n\n' +
21+
Object.keys(who.go).join('\n'), // TODO: NEEDS TO BE CPU SCHEDULED
22+
headers: {'message-id': '<[email protected]>'} // hardcode id so all batches also group into the same email thread to reduce clutter.
23+
}, function(err, r){
24+
who.batch = null; who.go = {};
25+
err && console.log("email TAG:", err);
26+
});
27+
}, who.rate || (1000*60*60*24)); // default to 1 day
28+
};
29+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"ios": "browser.ios.js",
99
"android": "browser.android.js",
1010
"scripts": {
11-
"start": "node --prof examples/http.js",
11+
"start": "node examples/http.js",
1212
"debug": "node --prof-process --preprocess -j isolate*.log > v8data.json && rm isolate*.log && echo 'drag & drop ./v8data.json into https://mapbox.github.io/flamebearer/'",
1313
"https": "HTTPS_KEY=test/https/server.key HTTPS_CERT=test/https/server.crt npm start",
1414
"prepublishOnly": "npm run unbuild",

0 commit comments

Comments
 (0)