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": {
"node": true,
"es6": true
"node": true
},
"parserOptions": {
"ecmaVersion": 8
},
"extends": ["eslint:recommended"],
"parser": "babel-eslint",
"rules": {
"indent": ["error", "tab"],
"quotes": ["error", "single", "avoid-escape"],

@ -8,12 +8,12 @@
Try it out at [paste.fyi](http://paste.fyi/)
## 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/)
* <kbd>CTRL</kbd>+<kbd>Enter</kbd> hotkey for quick paste submission
* Short URLs via [shortid](https://github.com/dylang/shortid), e.g. `NyQO9puMe`
* 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
* 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/)
@ -53,7 +53,7 @@ $ git diff README.md | curl -F 'paste=<-' -F 'highlight=diff' http://paste.fyi
5. Start MongoDB:
```sh
docker-compose up db -d
docker-compose up -d db
```
5. Start app and watch for changes:

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

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

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

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

@ -1,7 +1,7 @@
var mongoose = require('mongoose');
var shortid = require('shortid');
const mongoose = require('mongoose');
const shortid = require('shortid');
var paste = new mongoose.Schema({
const paste = new mongoose.Schema({
_id: { type: String, unique: true, default: shortid.generate },
paste: { type: String },
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",
"version": "1.2.0",
"version": "1.3.0",
"description": "Simple Node.js pastebin",
"license": "MIT",
"repository": "JoeBiellik/paste",
@ -23,24 +23,20 @@
"lint": "eslint . && pug-lint ./views"
},
"dependencies": {
"babel-core": "^6.1.2",
"babel-polyfill": "^6.0.16",
"babel-preset-es2015": "^6.1.2",
"config": "^1.16.0",
"koa": "^1.1.0",
"koa-body": "^1.3.0",
"koa-compress": "^1.0.8",
"koa-logger": "^1.3.0",
"koa-router": "^5.2.3",
"koa-static-cache": "^3.1.2",
"koa-views": "^4.1.0",
"mongoose": "^4.1.12",
"pug": "^2.0.0-beta4",
"koa": "^2.3.0",
"koa-body": "^2.3.0",
"koa-compress": "^2.0.0",
"koa-logger": "^3.0.1",
"koa-router": "^7.2.1",
"koa-static-cache": "^5.1.1",
"koa-views": "^6.0.2",
"mongoose": "^4.11.4",
"pug": "^2.0.0-rc.2",
"shortid": "^2.2.4"
},
"devDependencies": {
"babel-eslint": "^7.1.0",
"eslint": "^3.2.2",
"eslint": "^4.3.0",
"nodemon": "^1.7.2",
"pug-lint": "^2.3.0"
}

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

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

Loading…
Cancel
Save