Refactor for Node v8, native async, remove Babel

pull/10/head 1.3.0
Joe Biellik 8 years ago
parent fd4be2e53b
commit c3e8534157

@ -1,10 +1,11 @@
{ {
"env": { "env": {
"node": true, "node": true
"es6": true },
"parserOptions": {
"ecmaVersion": 8
}, },
"extends": ["eslint:recommended"], "extends": ["eslint:recommended"],
"parser": "babel-eslint",
"rules": { "rules": {
"indent": ["error", "tab"], "indent": ["error", "tab"],
"quotes": ["error", "single", "avoid-escape"], "quotes": ["error", "single", "avoid-escape"],

@ -8,12 +8,12 @@
Try it out at [paste.fyi](http://paste.fyi/) Try it out at [paste.fyi](http://paste.fyi/)
## Features ## Features
* Clean code thanks to ES7 async/await, [Koa](http://koajs.com/) and [Babel](https://babeljs.io/) * Clean code thanks to ES7 async/await and [Koa v2](http://koajs.com/)
* Full syntax highlighting via [Prism.js](http://prismjs.com/) * Full syntax highlighting via [Prism.js](http://prismjs.com/)
* <kbd>CTRL</kbd>+<kbd>Enter</kbd> hotkey for quick paste submission * <kbd>CTRL</kbd>+<kbd>Enter</kbd> hotkey for quick paste submission
* Short URLs via [shortid](https://github.com/dylang/shortid), e.g. `NyQO9puMe` * Short URLs via [shortid](https://github.com/dylang/shortid), e.g. `NyQO9puMe`
* Full support for CLI requests with [curl](http://curl.haxx.se/) etc * Full support for CLI requests with [curl](http://curl.haxx.se/) etc
* Textarea grows to fit content via [autosize](https://github.com/jackmoore/autosize) * Textarea grows to fit content via [autosize.js](https://github.com/jackmoore/autosize)
* Automatic and configurable paste expiry * Automatic and configurable paste expiry
* Runs fully containerized with [Docker](https://www.docker.com/) and [Vagrant](https://www.vagrantup.com/) * Runs fully containerized with [Docker](https://www.docker.com/) and [Vagrant](https://www.vagrantup.com/)
* Simple and responsive UI built with [Bootstrap 4](http://v4-alpha.getbootstrap.com/) * Simple and responsive UI built with [Bootstrap 4](http://v4-alpha.getbootstrap.com/)
@ -53,7 +53,7 @@ $ git diff README.md | curl -F 'paste=<-' -F 'highlight=diff' http://paste.fyi
5. Start MongoDB: 5. Start MongoDB:
```sh ```sh
docker-compose up db -d docker-compose up -d db
``` ```
5. Start app and watch for changes: 5. Start app and watch for changes:

@ -1,11 +1,7 @@
require('babel-polyfill'); const koa = require('koa');
require('babel-core/register')({ const app = new koa();
presets: ['es2015'] const config = require('config');
}); const router = require('./router');
var config = require('config');
var app = require('koa')();
var router = require('./router');
require('./db')(); require('./db')();
app.keys = config.keys; app.keys = config.keys;

@ -1,11 +1,13 @@
{ {
"name": "Paste",
"port": 3000, "port": 3000,
"db": "localhost/paste", "db": "mongodb://localhost/paste",
"cacheAge": 86400, "cacheAge": 86400,
"sizeLimit": "2mb", "sizeLimit": "2mb",
"prettyHtml": true, "prettyHtml": true,
"expiresDefault": 604800, "expiresDefault": 604800,
"expires": { "expires": {
"600": "10 minutes",
"3600": "1 hour", "3600": "1 hour",
"86400": "1 day", "86400": "1 day",
"604800": "1 week", "604800": "1 week",
@ -15,6 +17,7 @@
"highlights": { "highlights": {
"abap": "ABAP", "abap": "ABAP",
"actionscript": "ActionScript", "actionscript": "ActionScript",
"ada": "Ada",
"apacheconf": "Apache Configuration", "apacheconf": "Apache Configuration",
"apl": "APL", "apl": "APL",
"applescript": "AppleScript", "applescript": "AppleScript",
@ -27,6 +30,7 @@
"batch": "Batch", "batch": "Batch",
"bison": "Bison", "bison": "Bison",
"brainfuck": "Brainfuck", "brainfuck": "Brainfuck",
"bro": "Bro",
"c": "C", "c": "C",
"clike": "C-like", "clike": "C-like",
"csharp": "C#", "csharp": "C#",
@ -37,6 +41,7 @@
"css-extras": "CSS Extras", "css-extras": "CSS Extras",
"d": "D", "d": "D",
"dart": "Dart", "dart": "Dart",
"django": "Django/Jinja2",
"diff": "Diff", "diff": "Diff",
"docker": "Docker", "docker": "Docker",
"eiffel": "Eiffel", "eiffel": "Eiffel",
@ -48,6 +53,7 @@
"git": "Git", "git": "Git",
"glsl": "GLSL", "glsl": "GLSL",
"go": "Go", "go": "Go",
"graphql": "GraphQL",
"groovy": "Groovy", "groovy": "Groovy",
"haml": "Haml", "haml": "Haml",
"handlebars": "Handlebars", "handlebars": "Handlebars",
@ -61,11 +67,14 @@
"jade": "Jade", "jade": "Jade",
"java": "Java", "java": "Java",
"javascript": "JavaScript", "javascript": "JavaScript",
"jolie": "Jolie",
"json": "JSON",
"julia": "Julia", "julia": "Julia",
"keyman": "Keyman", "keyman": "Keyman",
"kotlin": "Kotlin", "kotlin": "Kotlin",
"latex": "LaTeX", "latex": "LaTeX",
"less": "Less", "less": "Less",
"livescript": "LiveScript",
"lolcode": "LOLCODE", "lolcode": "LOLCODE",
"lua": "Lua", "lua": "Lua",
"makefile": "Makefile", "makefile": "Makefile",
@ -93,6 +102,8 @@
"powershell": "PowerShell", "powershell": "PowerShell",
"processing": "Processing", "processing": "Processing",
"prolog": "Prolog", "prolog": "Prolog",
"properties": ".properties",
"Protocol Buffers": "protobuf",
"puppet": "Puppet", "puppet": "Puppet",
"pure": "Pure", "pure": "Pure",
"python": "Python", "python": "Python",
@ -100,6 +111,7 @@
"qore": "Qore", "qore": "Qore",
"r": "R", "r": "R",
"jsx": "React JSX", "jsx": "React JSX",
"reason": "Reason",
"rest": "reST (reStructuredText)", "rest": "reST (reStructuredText)",
"rip": "Rip", "rip": "Rip",
"roboconf": "Roboconf", "roboconf": "Roboconf",
@ -120,10 +132,12 @@
"textile": "Textile", "textile": "Textile",
"twig": "Twig", "twig": "Twig",
"typescript": "TypeScript", "typescript": "TypeScript",
"VB.Net": "vbnet",
"verilog": "Verilog", "verilog": "Verilog",
"vhdl": "VHDL", "vhdl": "VHDL",
"vim": "vim", "vim": "vim",
"wiki": "Wiki markup", "wiki": "Wiki markup",
"xojo": "Xojo (REALbasic)",
"yaml": "YAML" "yaml": "YAML"
} }
} }

@ -1,4 +1,4 @@
{ {
"port": 80, "port": 80,
"db": "db/paste" "db": "mongodb://db/paste"
} }

@ -1,62 +1,62 @@
var config = require('config'); const config = require('config');
var Paste = require('../models/paste'); const Paste = require('../models/paste');
module.exports = { module.exports = {
*view() { async view(ctx) {
try { try {
let paste = yield Paste.findById(this.params.id).exec(); let paste = await Paste.findById(ctx.params.id).exec();
let lang = Object.keys(this.query)[0]; let lang = Object.keys(ctx.query)[0];
if (lang) { if (lang) {
yield this.render('highlight', { await ctx.render('highlight', {
pretty: config.prettyHtml, pretty: config.prettyHtml,
title: 'Paste ' + paste.id, title: config.name + ' ' + paste.id,
paste: paste.paste, paste: paste.paste,
lang: lang lang: lang
}); });
} else { } else {
this.type = 'text/plain'; ctx.type = 'text/plain';
this.body = paste.paste; ctx.body = paste.paste;
} }
} catch (ex) { } catch (ex) {
this.throw('Paste Not Found', 404); ctx.throw('Paste Not Found', 404);
} }
}, },
*create() { async create(ctx) {
if (this.request.body.fields) { if (ctx.request.body.fields) {
if (this.request.body.fields.paste) { if (ctx.request.body.fields.paste) {
this.request.body.paste = this.request.body.fields.paste; ctx.request.body.paste = ctx.request.body.fields.paste;
} }
if (this.request.body.fields.highlight) { if (ctx.request.body.fields.highlight) {
this.request.body.highlight = this.request.body.fields.highlight; ctx.request.body.highlight = ctx.request.body.fields.highlight;
} }
if (this.request.body.fields.expire) { if (ctx.request.body.fields.expire) {
this.request.body.expire = this.request.body.fields.expire; ctx.request.body.expire = ctx.request.body.fields.expire;
} }
} }
if (!this.request.body.expire) { if (!ctx.request.body.expire) {
this.request.body.expire = config.expiresDefault; ctx.request.body.expire = config.expiresDefault;
} }
let paste = new Paste({ let paste = new Paste({
paste: this.request.body.paste, paste: ctx.request.body.paste,
expiresAt: new Date(Date.now() + this.request.body.expire * 1000) expiresAt: new Date(Date.now() + ctx.request.body.expire * 1000)
}); });
yield paste.save(); await paste.save();
let link = paste.id; let link = paste.id;
if (this.request.body.highlight) { if (ctx.request.body.highlight) {
link += '?' + this.request.body.highlight; link += '?' + ctx.request.body.highlight;
} }
if (Object.keys(this.query).includes('redirect')) { if (Object.keys(ctx.query).includes('redirect')) {
this.redirect(link); ctx.redirect(link);
} else { } else {
this.body = this.request.origin + '/' + link + '\n'; ctx.body = ctx.request.origin + '/' + link + '\n';
} }
} }
}; };

18
db.js

@ -1,16 +1,16 @@
var config = require('config'); const config = require('config');
var util = require('util'); const util = require('util');
var mongoose = require('mongoose'); const mongoose = require('mongoose');
module.exports = function() { module.exports = () => {
mongoose.Promise = global.Promise; mongoose.Promise = global.Promise;
mongoose.connect(config.db);
mongoose.connection.once('open', function() {
util.log('MongoDB connection open');
});
mongoose.connection.once('open', util.log.bind(util, 'MongoDB connection open'));
mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection error:')); mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection error:'));
mongoose.connect(config.db, {
useMongoClient: true
});
return mongoose.connection; return mongoose.connection;
}; };

@ -1,7 +1,7 @@
var mongoose = require('mongoose'); const mongoose = require('mongoose');
var shortid = require('shortid'); const shortid = require('shortid');
var paste = new mongoose.Schema({ const paste = new mongoose.Schema({
_id: { type: String, unique: true, default: shortid.generate }, _id: { type: String, unique: true, default: shortid.generate },
paste: { type: String }, paste: { type: String },
expiresAt: { type: Date, expires: 0, default: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7) } expiresAt: { type: Date, expires: 0, default: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7) }

3168
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -1,6 +1,6 @@
{ {
"name": "paste", "name": "paste",
"version": "1.2.0", "version": "1.3.0",
"description": "Simple Node.js pastebin", "description": "Simple Node.js pastebin",
"license": "MIT", "license": "MIT",
"repository": "JoeBiellik/paste", "repository": "JoeBiellik/paste",
@ -23,24 +23,20 @@
"lint": "eslint . && pug-lint ./views" "lint": "eslint . && pug-lint ./views"
}, },
"dependencies": { "dependencies": {
"babel-core": "^6.1.2",
"babel-polyfill": "^6.0.16",
"babel-preset-es2015": "^6.1.2",
"config": "^1.16.0", "config": "^1.16.0",
"koa": "^1.1.0", "koa": "^2.3.0",
"koa-body": "^1.3.0", "koa-body": "^2.3.0",
"koa-compress": "^1.0.8", "koa-compress": "^2.0.0",
"koa-logger": "^1.3.0", "koa-logger": "^3.0.1",
"koa-router": "^5.2.3", "koa-router": "^7.2.1",
"koa-static-cache": "^3.1.2", "koa-static-cache": "^5.1.1",
"koa-views": "^4.1.0", "koa-views": "^6.0.2",
"mongoose": "^4.1.12", "mongoose": "^4.11.4",
"pug": "^2.0.0-beta4", "pug": "^2.0.0-rc.2",
"shortid": "^2.2.4" "shortid": "^2.2.4"
}, },
"devDependencies": { "devDependencies": {
"babel-eslint": "^7.1.0", "eslint": "^4.3.0",
"eslint": "^3.2.2",
"nodemon": "^1.7.2", "nodemon": "^1.7.2",
"pug-lint": "^2.3.0" "pug-lint": "^2.3.0"
} }

@ -1,18 +1,18 @@
var router = require('koa-router')(); const router = require('koa-router')();
var config = require('config'); const config = require('config');
var pastes = require('./controllers/pastes'); const pastes = require('./controllers/pastes');
router.get('/', function *() { router
yield this.render('index', { .get('/', async (ctx) => {
pretty: config.prettyHtml, await ctx.render('index', {
title: 'Paste', pretty: config.prettyHtml,
url: this.request.origin, title: config.name,
expires: config.expires, url: ctx.request.origin,
highlights: config.highlights expires: config.expires,
}); highlights: config.highlights
}); });
})
router.post('/', pastes.create); .post('/', pastes.create)
router.get('/:id', pastes.view); .get('/:id', pastes.view);
module.exports = router; module.exports = router;

@ -1,6 +1,6 @@
var app = require('./app'); const app = require('./app');
var config = require('config'); const config = require('config');
var util = require('util'); const util = require('util');
module.exports = app.listen(process.env.PORT || config.port || 3000, function() { module.exports = app.listen(process.env.PORT || config.port || 3000, function() {
util.log('Server started: http://localhost:%s/', this.address().port); util.log('Server started: http://localhost:%s/', this.address().port);

Loading…
Cancel
Save