-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
109 lines (99 loc) · 2.26 KB
/
app.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
import { createServer } from './http/server.js'
import { createRouter } from './http/router.js'
import { runInCtx, mixCtx } from './als.js'
import {
ctx,
healthcheck,
errorBoundary,
notFound,
trace,
proxy,
timeout,
firewall,
advisory,
metrics,
} from './mwares/index.js'
import { loadConfig } from './config.js'
import { getCache, stopCache } from './cache.js'
export { getCache, stopCache }
export const _createApp = (cfg, {
cache,
logger
} = {}) => {
const config = loadConfig(cfg)
getCache(cache || config.cache) // init cache
mixCtx({
logger,
config,
})
const firewalls = createRoutes(config)
const router = createRouter([
ctx({config, logger}),
timeout,
trace,
['GET', config.server.healthcheck, healthcheck],
['GET', config.server.metrics, metrics],
...firewalls,
notFound,
errorBoundary,
], config.server.base)
const server = createServer({...config.server, router})
return {
server,
config,
start() { return this.server.start() },
stop() {
stopCache()
return this.server.stop()
},
}
}
export const createRoutes = (config) =>
config.firewall.map(({base, entrypoint, registry, token, rules}) => {
const f = firewall({registry, rules, entrypoint, token})
const a = advisory({registry, rules, entrypoint, token})
return createRouter([
// tarball
[
'GET',
[
/^\/((?:(@[a-z0-9\-._]+)(?:%2[fF]|\/))?[a-z0-9\-._]+)\/-\/[a-z0-9\-._]+-(\d+\.\d+\.\d+(?:-[+\-.a-z0-9_]+)?)\.tgz$/,
['name', 'org', 'version']
],
f
],
// packument
[
'*',
[
/^\/((?:(@[a-z0-9\-._]+)(?:%2[fF]|\/))?[a-z0-9\-._]+)\/?$/,
['name', 'org']
],
f
],
// advisory
[
'GET',
[
/^\/_check\/((?:(@[a-z0-9\-._]+)(?:%2[fF]|\/))?[a-z0-9\-._]+)\/(\d+\.\d+\.\d+(?:-[+\-.a-z0-9_]+)?)\/?$/,
['name', 'org', 'version']
],
a
],
[
'POST',
[
/^\/_check\/bulk\/?$/,
[]
],
a
],
proxy(registry),
errorBoundary,
], base)
})
export const createApp = (...args) => {
let app
runInCtx({}, () => app = _createApp(...args))
return app
}