Skip to content

Commit

Permalink
Merge pull request ElemeFE#21 from lujjjh/skip-requests-with-origin
Browse files Browse the repository at this point in the history
Skip requests with Origin header
  • Loading branch information
YanagiEiichi authored Jul 18, 2017
2 parents 88e69ce + 1f7222f commit 702673d
Show file tree
Hide file tree
Showing 14 changed files with 1,082 additions and 80 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
4 changes: 4 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
env:
node: true
extends: standard
22 changes: 11 additions & 11 deletions examples/restc-example-express/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const express = require('express');
const app = express();
const restc = require('../..');
const express = require('express')
const app = express()
const restc = require('../..')

app.use(restc.express());
app.use(restc.express())

app.get('/', (req, res) => {
res.send({ message: 'Hello world!' });
});
res.send({ message: 'Hello world!' })
})

app.get('/binary', (req, res) => {
res.set('Content-Type', 'application/octet-stream');
res.set('Content-Disposition', 'attachment; filename="hello.txt"');
res.send('Hello world!');
});
res.set('Content-Type', 'application/octet-stream')
res.set('Content-Disposition', 'attachment; filename="hello.txt"')
res.send('Hello world!')
})

app.listen(3000);
app.listen(3000)
14 changes: 7 additions & 7 deletions examples/restc-example-koa/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const app = require('koa')();
const restc = require('../..');
const app = require('koa')()
const restc = require('../..')

app.use(restc.koa());
app.use(restc.koa())

app.use(function *(next) {
this.body = { message: 'Hello world!' };
});
app.use(function * (next) {
this.body = { message: 'Hello world!' }
})

app.listen(3000);
app.listen(3000)
16 changes: 8 additions & 8 deletions examples/restc-example-koa2/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const Koa = require('koa');
const restc = require('../..');
const app = new Koa();
const Koa = require('koa')
const restc = require('../..')
const app = new Koa()

app.use(restc.koa2());
app.use(restc.koa2())

app.use((ctx, next) => {
ctx.body = { message: 'Hello world!' };
return next();
});
ctx.body = { message: 'Hello world!' }
return next()
})

app.listen(3000);
app.listen(3000)
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
'use strict'

const express = require('./lib/express');
const koa = require('./lib/koa');
const koa2 = require('./lib/koa2');
const express = require('./lib/express')
const koa = require('./lib/koa')
const koa2 = require('./lib/koa2')

module.exports = { express, koa, koa2 };
module.exports = { express, koa, koa2 }
17 changes: 5 additions & 12 deletions lib/express.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
'use strict';
'use strict'

const restc = require('./restc');
const serve = require('.')

module.exports = () => (req, res, next) => {
res.vary('Accept');
if (req.accepts([ 'json', 'html' ]) !== 'html') return next();
restc
.then(html => res.end(html))
.catch(error => {
console.error('[restc]', 'an error occurred when acquiring restc:', error.message);
next();
});
};
module.exports = options => (req, res, next) => {
serve(options)(req, res) || next()
}
27 changes: 27 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const fs = require('fs')
const path = require('path')

const content = fs.readFileSync(path.join(__dirname, '../faas/index.html'), 'utf-8')

module.exports = options => (req, res) => {
// set vary header for every response
res.setHeader('Vary', 'Accept, Origin')

// return if it is a cross-origin request
if ('origin' in req.headers) {
return false
}

// return if the client does not prefer text/html
let accept = req.headers.accept || ''
if (!/^text\/html(?:,|$)/.test(accept)) {
return false
}

// serve restc
res.send(content)

return true
}
18 changes: 7 additions & 11 deletions lib/koa.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
'use strict';
'use strict'

const restc = require('./restc');
const serve = require('.')
const response = require('./utils/response')

module.exports = () => function*(next) {
this.set('Vary', 'Accept');
if (this.accepts([ 'json', 'html' ]) !== 'html') return yield next;
try {
this.body = yield restc;
} catch (error) {
console.error('[restc]', 'an error occurred when acquiring restc:', error.message);
yield next;
module.exports = options => function * (next) {
if (!serve(options)(this.request, response.fromKoaContext(this))) {
yield next
}
};
}
20 changes: 8 additions & 12 deletions lib/koa2.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
'use strict';
'use strict'

const restc = require('./restc');
const serve = require('.')
const response = require('./utils/response')

module.exports = () => (ctx, next) => {
ctx.set('Vary', 'Accept');
if (ctx.accepts([ 'json', 'html' ]) !== 'html') return next();
return restc
.then(html => ctx.body = html)
.catch(error => {
console.error('[restc]', 'an error occurred when acquiring restc:', error.message);
return next();
});
};
module.exports = options => (ctx, next) => {
if (!serve(options)(ctx.request, response.fromKoaContext(ctx))) {
return next()
}
}
14 changes: 0 additions & 14 deletions lib/restc.js

This file was deleted.

16 changes: 16 additions & 0 deletions lib/utils/response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

// patch Koa responses
exports.fromKoaContext = ctx => Object.create(ctx.response, {
send: {
value: payload => {
ctx.body = payload
}
},

setHeader: {
value: (k, v) => {
ctx.set(k, v)
}
}
})
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"index.js",
"index.d.ts"
],
"scripts": {
"lint": "eslint --fix ."
},
"faas": {
"domain": "restc",
"public": "faas",
Expand All @@ -21,5 +24,13 @@
"notice": [
"[email protected]"
]
},
"devDependencies": {
"eslint": "^4.2.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-node": "^5.1.0",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^3.0.1"
}
}
Loading

0 comments on commit 702673d

Please sign in to comment.