Skip to content

Commit c3e8534

Browse files
committed
Refactor for Node v8, native async, remove Babel
1 parent fd4be2e commit c3e8534

12 files changed

Lines changed: 3264 additions & 89 deletions

File tree

.eslintrc.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"env": {
3-
"node": true,
4-
"es6": true
3+
"node": true
4+
},
5+
"parserOptions": {
6+
"ecmaVersion": 8
57
},
68
"extends": ["eslint:recommended"],
7-
"parser": "babel-eslint",
89
"rules": {
910
"indent": ["error", "tab"],
1011
"quotes": ["error", "single", "avoid-escape"],

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
Try it out at [paste.fyi](http://paste.fyi/)
99

1010
## Features
11-
* Clean code thanks to ES7 async/await, [Koa](http://koajs.com/) and [Babel](https://babeljs.io/)
11+
* Clean code thanks to ES7 async/await and [Koa v2](http://koajs.com/)
1212
* Full syntax highlighting via [Prism.js](http://prismjs.com/)
1313
* <kbd>CTRL</kbd>+<kbd>Enter</kbd> hotkey for quick paste submission
1414
* Short URLs via [shortid](https://github.com/dylang/shortid), e.g. `NyQO9puMe`
1515
* Full support for CLI requests with [curl](http://curl.haxx.se/) etc
16-
* Textarea grows to fit content via [autosize](https://github.com/jackmoore/autosize)
16+
* Textarea grows to fit content via [autosize.js](https://github.com/jackmoore/autosize)
1717
* Automatic and configurable paste expiry
1818
* Runs fully containerized with [Docker](https://www.docker.com/) and [Vagrant](https://www.vagrantup.com/)
1919
* 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
5353

5454
5. Start MongoDB:
5555
```sh
56-
docker-compose up db -d
56+
docker-compose up -d db
5757
```
5858

5959
5. Start app and watch for changes:

app.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
require('babel-polyfill');
2-
require('babel-core/register')({
3-
presets: ['es2015']
4-
});
5-
6-
var config = require('config');
7-
var app = require('koa')();
8-
var router = require('./router');
1+
const koa = require('koa');
2+
const app = new koa();
3+
const config = require('config');
4+
const router = require('./router');
95
require('./db')();
106

117
app.keys = config.keys;

config/default.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
2+
"name": "Paste",
23
"port": 3000,
3-
"db": "localhost/paste",
4+
"db": "mongodb://localhost/paste",
45
"cacheAge": 86400,
56
"sizeLimit": "2mb",
67
"prettyHtml": true,
78
"expiresDefault": 604800,
89
"expires": {
10+
"600": "10 minutes",
911
"3600": "1 hour",
1012
"86400": "1 day",
1113
"604800": "1 week",
@@ -15,6 +17,7 @@
1517
"highlights": {
1618
"abap": "ABAP",
1719
"actionscript": "ActionScript",
20+
"ada": "Ada",
1821
"apacheconf": "Apache Configuration",
1922
"apl": "APL",
2023
"applescript": "AppleScript",
@@ -27,6 +30,7 @@
2730
"batch": "Batch",
2831
"bison": "Bison",
2932
"brainfuck": "Brainfuck",
33+
"bro": "Bro",
3034
"c": "C",
3135
"clike": "C-like",
3236
"csharp": "C#",
@@ -37,6 +41,7 @@
3741
"css-extras": "CSS Extras",
3842
"d": "D",
3943
"dart": "Dart",
44+
"django": "Django/Jinja2",
4045
"diff": "Diff",
4146
"docker": "Docker",
4247
"eiffel": "Eiffel",
@@ -48,6 +53,7 @@
4853
"git": "Git",
4954
"glsl": "GLSL",
5055
"go": "Go",
56+
"graphql": "GraphQL",
5157
"groovy": "Groovy",
5258
"haml": "Haml",
5359
"handlebars": "Handlebars",
@@ -61,11 +67,14 @@
6167
"jade": "Jade",
6268
"java": "Java",
6369
"javascript": "JavaScript",
70+
"jolie": "Jolie",
71+
"json": "JSON",
6472
"julia": "Julia",
6573
"keyman": "Keyman",
6674
"kotlin": "Kotlin",
6775
"latex": "LaTeX",
6876
"less": "Less",
77+
"livescript": "LiveScript",
6978
"lolcode": "LOLCODE",
7079
"lua": "Lua",
7180
"makefile": "Makefile",
@@ -93,13 +102,16 @@
93102
"powershell": "PowerShell",
94103
"processing": "Processing",
95104
"prolog": "Prolog",
105+
"properties": ".properties",
106+
"Protocol Buffers": "protobuf",
96107
"puppet": "Puppet",
97108
"pure": "Pure",
98109
"python": "Python",
99110
"q": "Q",
100111
"qore": "Qore",
101112
"r": "R",
102113
"jsx": "React JSX",
114+
"reason": "Reason",
103115
"rest": "reST (reStructuredText)",
104116
"rip": "Rip",
105117
"roboconf": "Roboconf",
@@ -120,10 +132,12 @@
120132
"textile": "Textile",
121133
"twig": "Twig",
122134
"typescript": "TypeScript",
135+
"VB.Net": "vbnet",
123136
"verilog": "Verilog",
124137
"vhdl": "VHDL",
125138
"vim": "vim",
126139
"wiki": "Wiki markup",
140+
"xojo": "Xojo (REALbasic)",
127141
"yaml": "YAML"
128142
}
129143
}

config/docker.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"port": 80,
3-
"db": "db/paste"
3+
"db": "mongodb://db/paste"
44
}

controllers/pastes.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
1-
var config = require('config');
2-
var Paste = require('../models/paste');
1+
const config = require('config');
2+
const Paste = require('../models/paste');
33

44
module.exports = {
5-
*view() {
5+
async view(ctx) {
66
try {
7-
let paste = yield Paste.findById(this.params.id).exec();
8-
let lang = Object.keys(this.query)[0];
7+
let paste = await Paste.findById(ctx.params.id).exec();
8+
let lang = Object.keys(ctx.query)[0];
99

1010
if (lang) {
11-
yield this.render('highlight', {
11+
await ctx.render('highlight', {
1212
pretty: config.prettyHtml,
13-
title: 'Paste ' + paste.id,
13+
title: config.name + ' ' + paste.id,
1414
paste: paste.paste,
1515
lang: lang
1616
});
1717
} else {
18-
this.type = 'text/plain';
19-
this.body = paste.paste;
18+
ctx.type = 'text/plain';
19+
ctx.body = paste.paste;
2020
}
2121
} catch (ex) {
22-
this.throw('Paste Not Found', 404);
22+
ctx.throw('Paste Not Found', 404);
2323
}
2424
},
2525

26-
*create() {
27-
if (this.request.body.fields) {
28-
if (this.request.body.fields.paste) {
29-
this.request.body.paste = this.request.body.fields.paste;
26+
async create(ctx) {
27+
if (ctx.request.body.fields) {
28+
if (ctx.request.body.fields.paste) {
29+
ctx.request.body.paste = ctx.request.body.fields.paste;
3030
}
31-
if (this.request.body.fields.highlight) {
32-
this.request.body.highlight = this.request.body.fields.highlight;
31+
if (ctx.request.body.fields.highlight) {
32+
ctx.request.body.highlight = ctx.request.body.fields.highlight;
3333
}
34-
if (this.request.body.fields.expire) {
35-
this.request.body.expire = this.request.body.fields.expire;
34+
if (ctx.request.body.fields.expire) {
35+
ctx.request.body.expire = ctx.request.body.fields.expire;
3636
}
3737
}
3838

39-
if (!this.request.body.expire) {
40-
this.request.body.expire = config.expiresDefault;
39+
if (!ctx.request.body.expire) {
40+
ctx.request.body.expire = config.expiresDefault;
4141
}
4242

4343
let paste = new Paste({
44-
paste: this.request.body.paste,
45-
expiresAt: new Date(Date.now() + this.request.body.expire * 1000)
44+
paste: ctx.request.body.paste,
45+
expiresAt: new Date(Date.now() + ctx.request.body.expire * 1000)
4646
});
4747

48-
yield paste.save();
48+
await paste.save();
4949

5050
let link = paste.id;
5151

52-
if (this.request.body.highlight) {
53-
link += '?' + this.request.body.highlight;
52+
if (ctx.request.body.highlight) {
53+
link += '?' + ctx.request.body.highlight;
5454
}
5555

56-
if (Object.keys(this.query).includes('redirect')) {
57-
this.redirect(link);
56+
if (Object.keys(ctx.query).includes('redirect')) {
57+
ctx.redirect(link);
5858
} else {
59-
this.body = this.request.origin + '/' + link + '\n';
59+
ctx.body = ctx.request.origin + '/' + link + '\n';
6060
}
6161
}
6262
};

db.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
var config = require('config');
2-
var util = require('util');
3-
var mongoose = require('mongoose');
1+
const config = require('config');
2+
const util = require('util');
3+
const mongoose = require('mongoose');
44

5-
module.exports = function() {
5+
module.exports = () => {
66
mongoose.Promise = global.Promise;
7-
mongoose.connect(config.db);
8-
9-
mongoose.connection.once('open', function() {
10-
util.log('MongoDB connection open');
11-
});
127

8+
mongoose.connection.once('open', util.log.bind(util, 'MongoDB connection open'));
139
mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection error:'));
1410

11+
mongoose.connect(config.db, {
12+
useMongoClient: true
13+
});
14+
1515
return mongoose.connection;
1616
};

models/paste.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
var mongoose = require('mongoose');
2-
var shortid = require('shortid');
1+
const mongoose = require('mongoose');
2+
const shortid = require('shortid');
33

4-
var paste = new mongoose.Schema({
4+
const paste = new mongoose.Schema({
55
_id: { type: String, unique: true, default: shortid.generate },
66
paste: { type: String },
77
expiresAt: { type: Date, expires: 0, default: new Date(Date.now() + 1000 * 60 * 60 * 24 * 7) }

0 commit comments

Comments
 (0)