Browse Source

jshinted all files

master
Christian Müller 9 years ago
parent
commit
91e5c745c1
  1. 1
      .jshintrc
  2. 10
      .tern-project
  3. 68
      api_spec.js
  4. 44
      resources/public/js/publishing.js
  5. 132
      server.js
  6. 8
      storage.js
  7. 48
      view.js

1
.jshintrc

@ -0,0 +1 @@ @@ -0,0 +1 @@
{ "esnext": true }

10
.tern-project

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
{
"plugins": {
"es_modules": {}
},
"libs": [
"ecma5",
"ecma6"
],
"ecmaVersion": 6
}

68
api_spec.js

@ -50,8 +50,8 @@ frisby.create('Invalid posting 2') @@ -50,8 +50,8 @@ frisby.create('Invalid posting 2')
frisby.create('Invalid posting 3')
.post('http://localhost:3000/note', {
action: 'POST',
session: md5("new"),
signature: "assdss",
session: md5('new'),
signature: 'assdss',
note: testNote
})
.expectStatus(400)
@ -62,8 +62,8 @@ frisby.create('Invalid posting 3') @@ -62,8 +62,8 @@ frisby.create('Invalid posting 3')
frisby.create('Valid posting')
.post('http://localhost:3000/note', {
action: 'POST',
session: md5("new"),
signature: md5(md5("new") + testNote),
session: md5('new'),
signature: md5(md5('new') + testNote),
password: '',
note: testNote
})
@ -81,39 +81,39 @@ frisby.create('Valid posting') @@ -81,39 +81,39 @@ frisby.create('Valid posting')
.post('http://localhost:3000/note', {
id: noteId,
action: 'UPDATE',
session: md5("new"),
signature: md5(md5("new") + testNote+'!!!'),
session: md5('new'),
signature: md5(md5('new') + testNote+'!!!'),
note: testNote + '!!!',
password: ''
})
.expectStatus(400)
.expectBodyContains('Password is wrong')
.toss()
.toss();
})
.after((err, res, body) => {
frisby.create('Illegal note editing attempt')
.post('http://localhost:3000/note', {
id: noteId,
action: 'UPDATE',
session: md5("new"),
signature: md5(md5("new") + testNote+'!!!'),
session: md5('new'),
signature: md5(md5('new') + testNote+'!!!'),
note: testNote + '!!!',
password: "aaabbb"
password: 'aaabbb'
})
.expectStatus(400)
.expectBodyContains('Password is wrong')
.toss()
.toss();
})
.toss()
.toss();
})
.toss();
frisby.create('Valid posting, editing and removal')
.post('http://localhost:3000/note', {
action: 'POST',
session: md5("new"),
signature: md5(md5("new") + testNote),
session: md5('new'),
signature: md5(md5('new') + testNote),
password: 'aabbcc',
note: testNote
})
@ -123,7 +123,7 @@ frisby.create('Valid posting, editing and removal') @@ -123,7 +123,7 @@ frisby.create('Valid posting, editing and removal')
.after(function(err, res, body) {
var noteId = body.replace('Found. Redirecting to /', '');
frisby.create('Export posted note')
.get('http://localhost:3000/' + noteId + "/export")
.get('http://localhost:3000/' + noteId + '/export')
.expectStatus(200)
.expectHeaderContains('content-type', 'text/plain; charset=utf-8')
.expectBodyContains(testNote)
@ -138,24 +138,24 @@ frisby.create('Valid posting, editing and removal') @@ -138,24 +138,24 @@ frisby.create('Valid posting, editing and removal')
.post('http://localhost:3000/note', {
id: noteId,
action: 'UPDATE',
session: md5("new"),
signature: md5(md5("new") + testNote+'!!!'),
session: md5('new'),
signature: md5(md5('new') + testNote+'!!!'),
note: testNote + '!!!',
password: "abbcc"
password: 'abbcc'
})
.expectStatus(400)
.expectBodyContains('Password is wrong')
.toss()
.toss();
})
.after((err, res, body) => {
frisby.create('Valid note editing attempt')
.post('http://localhost:3000/note', {
id: noteId,
action: 'UPDATE',
session: md5("new"),
signature: md5(md5("new") + 'Changed!'),
session: md5('new'),
signature: md5(md5('new') + 'Changed!'),
note: 'Changed!',
password: "aabbcc"
password: 'aabbcc'
})
.expectStatus(302)
.after((err, res, body) => {
@ -171,27 +171,27 @@ frisby.create('Valid posting, editing and removal') @@ -171,27 +171,27 @@ frisby.create('Valid posting, editing and removal')
id: noteId,
button: 'Delete',
action: 'UPDATE',
session: md5("new"),
signature: md5(md5("new") + 'Changed!'),
session: md5('new'),
signature: md5(md5('new') + 'Changed!'),
note: 'Changed!',
password: "aabbcc"
password: 'aabbcc'
})
.expectStatus(200)
.expectBodyContains('Note deleted')
.toss();
})
.toss()
.toss();
})
.toss()
.toss();
frisby.create('Read stats of posted note')
.expectStatus(200)
.get('http://localhost:3000/' + noteId).toss()
.get('http://localhost:3000/' + noteId).toss();
frisby.create('Read stats of posted note')
.expectStatus(200)
.get('http://localhost:3000/' + noteId).toss()
.get('http://localhost:3000/' + noteId).toss();
frisby.create('Read stats of posted note')
.expectStatus(200)
.get('http://localhost:3000/' + noteId).toss()
.get('http://localhost:3000/' + noteId).toss();
frisby.create('Read stats of posted note')
.get('http://localhost:3000/' + noteId + '/stats')
.expectHeaderContains('content-type', 'text/html; charset=utf-8')
@ -209,8 +209,8 @@ while (tooLongNote.length < 1024*200) tooLongNote += tooLongNote; @@ -209,8 +209,8 @@ while (tooLongNote.length < 1024*200) tooLongNote += tooLongNote;
frisby.create('Invalid posting of too long note')
.post('http://localhost:3000/note', {
action: 'POST',
session: md5("new"),
signature: md5(md5("new") + testNote),
session: md5('new'),
signature: md5(md5('new') + testNote),
password: 'aabbcc',
note: tooLongNote
})
@ -222,8 +222,8 @@ frisby.create('Invalid posting of too long note') @@ -222,8 +222,8 @@ frisby.create('Invalid posting of too long note')
frisby.create('Invalid update without id')
.post('http://localhost:3000/note', {
action: 'UPDATE',
session: md5("new"),
signature: md5(md5("new") + 'Any note'),
session: md5('new'),
signature: md5(md5('new') + 'Any note'),
password: 'aabbcc',
note: 'Any note'
})

44
resources/public/js/publishing.js

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
var $ = function(id) {
return document.getElementById(id);
}
var iosDetected = navigator.userAgent.match("(iPad|iPod|iPhone)");
};
var iosDetected = navigator.userAgent.match('(iPad|iPod|iPhone)');
var timer = null;
var timerDelay = iosDetected ? 800 : 400;
var $note, $action, $preview, $plain_password, $tableau;
@ -21,10 +21,10 @@ function md2html(input) { @@ -21,10 +21,10 @@ function md2html(input) {
}
function saveDraft() {
if ($action == "UPDATE") return;
console.log("draft autosave...");
$tableau.innerHTML = "Draft autosaved."
localStorage.setItem("draft", $note.value);
if ($action == 'UPDATE') return;
console.log('draft autosave...');
$tableau.innerHTML = 'Draft autosaved.';
localStorage.setItem('draft', $note.value);
}
function enableButton() {
@ -34,40 +34,40 @@ function enableButton() { @@ -34,40 +34,40 @@ function enableButton() {
}
function onLoad() {
$note = $("note");
$action = $("action").value;
$preview = $("draft");
$tableau = $("tableau");
$plain_password = $("plain-password");
$note = $('note');
$action = $('action').value;
$preview = $('draft');
$tableau = $('tableau');
$plain_password = $('plain-password');
var updatePreview = function() {
clearTimeout(timer);
var content = $note.value;
var delay = Math.min(timerDelay, timerDelay * (content.length / 400));
timer = setTimeout(function() {
$preview.innerHTML = md2html(content);
$tableau.innerHTML = content.split(/\s+/).length + " words";
$tableau.innerHTML = content.split(/\s+/).length + ' words';
}, delay);
};
if ($action == "UPDATE") updatePreview();
if ($action == 'UPDATE') updatePreview();
else {
$("delete-button").style.display = "none";
$note.value = "";
var draft = localStorage.getItem("draft");
$('delete-button').style.display = 'none';
$note.value = '';
var draft = localStorage.getItem('draft');
if (draft) {
$note.value = draft;
updatePreview();
}
}
$note.onkeyup = updatePreview;
$("delete-button").onclick = $("publish-button").onclick = function(e) {
localStorage.removeItem("draft");
$('delete-button').onclick = $('publish-button').onclick = function(e) {
localStorage.removeItem('draft');
self.onbeforeunload = null;
if ($plain_password.value != "") $("password").value = md5($plain_password.value);
if ($plain_password.value !== '') $('password').value = md5($plain_password.value);
$plain_password.value = null;
$("signature").value = md5($("session").value + $note.value.replace(/[\n\r]/g, ""));
$('signature').value = md5($('session').value + $note.value.replace(/[\n\r]/g, ''));
};
if (iosDetected) $note.className += " ui-border";
if (iosDetected) $note.className += ' ui-border';
else $note.focus();
self.onbeforeunload = saveDraft;
setInterval(saveDraft, 60 * 1000)
setInterval(saveDraft, 60 * 1000);
}

132
server.js

@ -2,7 +2,7 @@ var express = require('express'); @@ -2,7 +2,7 @@ var express = require('express');
var view = require('./view');
var storage = require('./storage');
var md5 = require('md5');
var LRU = require("lru-cache")
var LRU = require('lru-cache');
var bodyParser = require('body-parser');
var fs = require('fs');
var blackList;
@ -13,8 +13,8 @@ app.use(bodyParser.urlencoded({ extended: true, limit: '200kb' })); @@ -13,8 +13,8 @@ app.use(bodyParser.urlencoded({ extended: true, limit: '200kb' }));
app.use(express.static(__dirname + '/resources/public'));
app.use(function (error, req, res, next) {
if (error) {
sendResponse(res, 400, "Bad request", error.message);
log("REQUEST ERROR:", error);
sendResponse(res, 400, 'Bad request', error.message);
log('REQUEST ERROR:', error);
} else {
next();
}
@ -24,28 +24,28 @@ var MODELS = {}; @@ -24,28 +24,28 @@ var MODELS = {};
var CACHE = new LRU({
max: 50,
dispose: key => {
log("disposing", key, "from cache");
log('disposing', key, 'from cache');
var model = MODELS[key];
model && model.save();
if (model) model.save();
delete MODELS[key];
}
});
var log = function() {
var date = new Date();
var timestamp = date.getDate() + "/" + date.getMonth() + " " + date.getHours() + ":" +
date.getMinutes() + ":" + date.getSeconds() + "." + date.getMilliseconds();
var timestamp = date.getDate() + '/' + date.getMonth() + ' ' + date.getHours() + ':' +
date.getMinutes() + ':' + date.getSeconds() + '.' + date.getMilliseconds();
var message = Array.prototype.slice.call(arguments);
message.unshift("--");
message.unshift('--');
message.unshift(timestamp);
console.log.apply(console, message);
}
};
app.get('/TOS', (req, res) => res.send(view.renderTOS()));
app.get('/new', (req, res) => {
log(req.ip, "opens /new");
res.send(view.newNotePage(md5("new")));
log(req.ip, 'opens /new');
res.send(view.newNotePage(md5('new')));
});
app.post('/note', (req, res) => {
@ -55,43 +55,43 @@ app.post('/note', (req, res) => { @@ -55,43 +55,43 @@ app.post('/note', (req, res) => {
password = body.password,
action = body.action,
id = body.id;
log(req.ip, "calls /note to", action, id);
var goToNote = note => res.redirect("/" + note.id);
if (!note || !session || session.indexOf(md5('edit/' + id)) != 0 && session.indexOf(md5('new')) != 0)
log(req.ip, 'calls /note to', action, id);
var goToNote = note => res.redirect('/' + note.id);
if (!note || !session || session.indexOf(md5('edit/' + id)) !== 0 && session.indexOf(md5('new')) !== 0)
return sendResponse(res, 400, 'Bad request');
if (body.signature != md5(session + note.replace(/[\n\r]/g, "")))
return sendResponse(res, 400, "Signature mismatch");
if (action == "POST")
if (body.signature != md5(session + note.replace(/[\n\r]/g, '')))
return sendResponse(res, 400, 'Signature mismatch');
if (action == 'POST')
storage.addNote(note, password).then(goToNote);
else {
if (!id) return sendResponse(res, 400, "Wrong note ID");
if (!id) return sendResponse(res, 400, 'Wrong note ID');
CACHE.del(id);
if (body.button == "Delete") {
log("deleting note", id);
if (body.button == 'Delete') {
log('deleting note', id);
storage.deleteNote(id, password).then(
() => sendResponse(res, 200, "Note deleted"),
error => sendResponse(res, 400, "Bad request", error.message));
() => sendResponse(res, 200, 'Note deleted'),
error => sendResponse(res, 400, 'Bad request', error.message));
} else {
log("updating note", id);
log('updating note', id);
storage.updateNote(id, password, note).then(goToNote,
error => sendResponse(res, 400, "Bad request", error.message));
error => sendResponse(res, 400, 'Bad request', error.message));
}
}
});
app.get("/:year/:month/:day/:title", (req, res) => {
var P = req.params, url = P.year + "/" + P.month + "/" + P.day + "/" + P.title;
log(req.ip, "resolves deprecated id", url);
app.get('/:year/:month/:day/:title', (req, res) => {
var P = req.params, url = P.year + '/' + P.month + '/' + P.day + '/' + P.title;
log(req.ip, 'resolves deprecated id', url);
if (CACHE.has(url)) {
log(url, "is cached!");
log(url, 'is cached!');
var id = CACHE.get(url);
if (id) res.redirect("/" + id);
if (id) res.redirect('/' + id);
else notFound(res);
} else storage.getNoteId(url).then(note => {
log(url, "is not cached, resolving...");
log(url, 'is not cached, resolving...');
if (note) {
CACHE.set(url, note.id);
res.redirect("/" + note.id)
res.redirect('/' + note.id);
} else {
CACHE.set(url, null);
notFound(res);
@ -100,44 +100,44 @@ app.get("/:year/:month/:day/:title", (req, res) => { @@ -100,44 +100,44 @@ app.get("/:year/:month/:day/:title", (req, res) => {
});
app.get(/\/([a-z0-9]+)\/edit/, (req, res) => {
var id = req.params["0"];
log(req.ip, "calls /edit on", id);
storage.getNote(id).then(note => res.send(note
? view.editNotePage(md5('edit/' + id), note)
: notFound(res)));
var id = req.params['0'];
log(req.ip, 'calls /edit on', id);
storage.getNote(id).then(note => res.send(note ?
view.editNotePage(md5('edit/' + id), note) :
notFound(res)));
});
app.get(/\/([a-z0-9]+)\/export/, (req, res) => {
var id = req.params["0"];
log(req.ip, "calls /export on", id);
var id = req.params['0'];
log(req.ip, 'calls /export on', id);
res.set({ 'Content-Type': 'text/plain', 'Charset': 'utf-8' });
storage.getNote(id).then(note => note
? res.send(note.text)
: notFound(res));
storage.getNote(id).then(note => note ?
res.send(note.text) :
notFound(res));
});
app.get(/\/([a-z0-9]+)\/stats/, (req, res) => {
var id = req.params["0"];
log(req.ip, "calls /stats on", id);
var promise = id in MODELS
? new Promise(resolve => resolve(MODELS[id]))
: storage.getNote(id);
promise.then(note => note
? res.send(view.renderStats(note))
: notFound(res));
var id = req.params['0'];
log(req.ip, 'calls /stats on', id);
var promise = id in MODELS ?
new Promise(resolve => resolve(MODELS[id])) :
storage.getNote(id);
promise.then(note => note ?
res.send(view.renderStats(note)) :
notFound(res));
});
app.get(/\/([a-z0-9]+)/, (req, res) => {
var id = req.params["0"];
log(req.ip, "open note", id, "from", req.get("Referer"));
var id = req.params['0'];
log(req.ip, 'open note', id, 'from', req.get('Referer'));
if (CACHE.has(id)) {
log(id, "is cached!");
var note = MODELS[id];
if (!note) return notFound(res);
note.views++;
log(id, 'is cached!');
var obj = MODELS[id];
if (!obj) return notFound(res);
obj.views++;
res.send(CACHE.get(id));
} else storage.getNote(id).then(note => {
log(id, "is not cached, resolving...");
log(id, 'is not cached, resolving...');
if (!note) {
CACHE.set(id, null);
return notFound(res);
@ -151,31 +151,31 @@ app.get(/\/([a-z0-9]+)/, (req, res) => { @@ -151,31 +151,31 @@ app.get(/\/([a-z0-9]+)/, (req, res) => {
});
var sendResponse = (res, code, message, details) => {
log("sending response", code, message);
log('sending response', code, message);
res.status(code).send(view.renderPage(null, message,
`<h1>${message}</h1><br/>` +
`<center>${details || "¯\\_(ツ)_/¯"}</center>`, ""));
}
`<center>${details || '¯\\_(ツ)_/¯'}</center>`, ''));
};
var notFound = res => sendResponse(res, 404, "Not found");
var notFound = res => sendResponse(res, 404, 'Not found');
var server = app.listen(process.env.PORT || 3000,
() => log('NoteHub server listening on port', server.address().port));
setInterval(() => {
var keys = Object.keys(MODELS);
log("saving stats for", keys.length, "models...");
keys.forEach(id => MODELS[id].save())
log('saving stats for', keys.length, 'models...');
keys.forEach(id => MODELS[id].save());
}, 5 * 60 * 1000);
var updateBlackList = () => {
var ids = fs.readFileSync(process.env.BLACK_LIST || "/dev/null", "utf-8").split(/\n+/).filter(Boolean);
ids.forEach(id => CACHE.del(id))
var ids = fs.readFileSync(process.env.BLACK_LIST || '/dev/null', 'utf-8').split(/\n+/).filter(Boolean);
ids.forEach(id => CACHE.del(id));
blackList = new Set(ids);
log("black list updated, entries:", blackList.size);
log('black list updated, entries:', blackList.size);
};
setInterval(updateBlackList, 60 * 60 * 1000)
setInterval(updateBlackList, 60 * 60 * 1000);
updateBlackList();

8
storage.js

@ -32,7 +32,7 @@ var generateId = () => [1, 1, 1, 1, 1] @@ -32,7 +32,7 @@ var generateId = () => [1, 1, 1, 1, 1]
var code = Math.floor(Math.random() * 36);
return String.fromCharCode(code + (code < 10 ? 48 : 87));
})
.join("");
.join('');
var getFreeId = () => {
var id = generateId();
@ -46,9 +46,9 @@ module.exports.addNote = (note, password) => getFreeId().then(id => Note.create( @@ -46,9 +46,9 @@ module.exports.addNote = (note, password) => getFreeId().then(id => Note.create(
}));
var passwordCheck = (note, password, callback) =>
(!note || note.password.length == 0 || note.password !== password)
? new Promise((resolve, reject) => reject({ message: "Password is wrong" }))
: callback();
(!note || note.password.length === 0 || note.password !== password) ?
new Promise((resolve, reject) => reject({ message: 'Password is wrong' })) :
callback();
module.exports.updateNote = (id, password, text) =>
Note.findById(id).then(note =>

48
view.js

@ -1,25 +1,25 @@ @@ -1,25 +1,25 @@
var marked = require("marked");
var fs = require("fs");
var hljs = require("highlight.js");
var marked = require('marked');
var fs = require('fs');
var hljs = require('highlight.js');
var TOS = fs.readFileSync("resources/TOS.md", "utf-8");
var pageTemplate = fs.readFileSync("resources/template.html", "utf-8");
var footerTemplate = fs.readFileSync("resources/footer.html", "utf-8");
var editTemplate = fs.readFileSync("resources/edit.html", "utf-8");
var header = fs.readFileSync(process.env.HEADER || "/dev/null", "utf-8");
var TOS = fs.readFileSync('resources/TOS.md', 'utf-8');
var pageTemplate = fs.readFileSync('resources/template.html', 'utf-8');
var footerTemplate = fs.readFileSync('resources/footer.html', 'utf-8');
var editTemplate = fs.readFileSync('resources/edit.html', 'utf-8');
var header = fs.readFileSync(process.env.HEADER || '/dev/null', 'utf-8');
var deriveTitle = text => text
.split(/[\n\r]/)[0].slice(0,25)
.replace(/[`~!@#\$%^&\*_|\+=\?;:'",.<>\{\}\\\/]/g, "");
.replace(/[`~!@#\$%^&\*_|\+=\?;:'",.<>\{\}\\\/]/g, '');
var renderPage = (id, title, content, footer, blackList) => pageTemplate
.replace("%HEADER%", blackList && blackList.has(id) ? header : "")
.replace("%TITLE%", title)
.replace("%CONTENT%", content.replace(/<meta.*?>/gi, "").replace(/<script[\s\S.]*?\/script>/gi, ""))
.replace("%FOOTER%", footer || "");
.replace('%HEADER%', blackList && blackList.has(id) ? header : '')
.replace('%TITLE%', title)
.replace('%CONTENT%', content.replace(/<meta.*?>/gi, '').replace(/<script[\s\S.]*?\/script>/gi, ''))
.replace('%FOOTER%', footer || '');
marked.setOptions({
langPrefix: "hljs lang-",
langPrefix: 'hljs lang-',
highlight: code => hljs.highlightAuto(code).value,
});
@ -29,11 +29,11 @@ module.exports.renderStats = note => renderPage(note.id, deriveTitle(note.text), @@ -29,11 +29,11 @@ module.exports.renderStats = note => renderPage(note.id, deriveTitle(note.text),
`<h2>Statistics</h2>
<table>
<tr><td>Published</td><td>${note.published}</td></tr>
<tr><td>Edited</td><td>${note.edited || "N/A"}</td></tr>
<tr><td>Edited</td><td>${note.edited || 'N/A'}</td></tr>
<tr><td>Views</td><td>${note.views}</td></tr>
</table>`);
module.exports.renderTOS = () => renderPage("tos", "Terms of Service", marked(TOS));
module.exports.renderTOS = () => renderPage('tos', 'Terms of Service', marked(TOS));
module.exports.renderNote = (note, blackList) => renderPage(note.id,
deriveTitle(note.text),
@ -42,14 +42,14 @@ module.exports.renderNote = (note, blackList) => renderPage(note.id, @@ -42,14 +42,14 @@ module.exports.renderNote = (note, blackList) => renderPage(note.id,
blackList);
module.exports.newNotePage = session => editTemplate
.replace("%ACTION%", "POST")
.replace("%SESSION%", session)
.replace("%CONTENT%", "Loading...");
.replace('%ACTION%', 'POST')
.replace('%SESSION%', session)
.replace('%CONTENT%', 'Loading...');
module.exports.editNotePage = (session, note) => editTemplate
.replace("%ACTION%", "UPDATE")
.replace("%SESSION%", session)
.replace("%ID%", note.id)
.replace("%CONTENT%", escape$(note.text));
.replace('%ACTION%', 'UPDATE')
.replace('%SESSION%', session)
.replace('%ID%', note.id)
.replace('%CONTENT%', escape$(note.text));
var escape$ = s => s.split("").map(chr => chr == "$" ? "$$" : chr).join("");
var escape$ = s => s.split('').map(chr => chr == '$' ? '$$' : chr).join('');

Loading…
Cancel
Save