-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
112 lines (104 loc) · 3.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict'
const path = require('path')
const stampit = require('stampit')
const Koa = require('koa')
const favicon = require('koa-favicon')
const router = require('koa-router')()
const bunyan = require('bunyan')
const uuid = require('uuid')
const session = require('koa-session')
const InvalidArgumentError = require(path.resolve(__dirname, 'invalid-arg-error'))
module.exports = stampit()
.props({
uriFactoryPlugins: {},
sessionOpts: {
key: 'janus:sess',
signed: false
},
redirectLog: { name: 'redirect' },
errorLog: { name: 'error' },
uriPathPrefix: '/',
favicon: path.resolve(__dirname, 'assets/favicon.ico')
})
.methods({
sessionId (ctx) {
return new Promise(function (resolve, reject) {
resolve(uuid.v1())
})
},
errorLogger (errorLog) {
return bunyan.createLogger(errorLog || this.errorLog)
},
redirectLogger (redirectLog) {
return bunyan.createLogger(redirectLog || this.redirectLog)
},
redirectLogEvent (ctx, defaultEvent) {
return defaultEvent
},
defaultRedirectLogEvent (ctx, supportedQueryParams) {
return {
request: {
method: ctx.request.method,
url: ctx.request.url,
referer: ctx.request.header.referer,
userAgent: ctx.request.header['user-agent']
},
response: {
location: ctx.response.header.location
},
user: {
sessionId: ctx.session.id
},
query: supportedQueryParams
}
}
})
.init(function (params) {
const factory = require(path.resolve(__dirname, 'uri-factory/'))(params.uriFactoryPlugins)
const sessionId = this.sessionId.bind(this)
const errorLogger = this.errorLogger(params.errorLog)
const redirectLogger = this.redirectLogger(params.redirectLog)
const defaultRedirectLogEvent = this.defaultRedirectLogEvent.bind(this)
const redirectLogEvent = this.redirectLogEvent.bind(this)
router.get(params.uriPathPrefix, async function redirect (ctx, next) {
await next()
const queryParams = factory.normalizeQueryParams(ctx.request.query)
const supportedOnly = true
const supportedQueryParams = factory.normalizeQueryParams(queryParams, supportedOnly)
const [warning, uri] = await factory.uriFor(queryParams)
ctx.status = 302
ctx.redirect(uri.href())
if (warning) {
redirectLogger.warn(
{ event: redirectLogEvent(ctx, defaultRedirectLogEvent(ctx, supportedQueryParams)) },
warning
)
} else {
redirectLogger.info(
{ event: redirectLogEvent(ctx, defaultRedirectLogEvent(ctx, supportedQueryParams)) },
'ok'
)
}
})
const app = new Koa()
app
.use(favicon(this.favicon))
.use(session(this.sessionOpts, app))
.use(async function session (ctx, next) {
if (!ctx.session.id) {
ctx.session.id = await sessionId(ctx)
}
await next()
})
.use(router.routes())
.use(router.allowedMethods())
.on('error', (err, ctx) => {
if (err instanceof InvalidArgumentError) {
err.status = ctx.status = 400
errorLogger.warn({ event: redirectLogEvent(ctx, defaultRedirectLogEvent(ctx)) }, err.message)
} else {
errorLogger.error({ error: err }, err.message)
}
})
return app
})