-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
150 lines (143 loc) · 3.5 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import crypto from 'node:crypto'
import { testFactory, objectContaining } from '../test-utils.js'
import { createApp } from '../../main/js/index.js'
import { request } from '../../main/js/http/client.js'
const test = testFactory('app', import.meta)
const cfg = {
server: {
host: 'localhost',
port: 3001
},
cache: {
ttl: 1
},
firewall: {
'/registry': {
registry: 'https://registry.npmjs.org',
rules: [
{
"policy": "deny",
"name": "colors",
"version": ">= v1.3.0"
},
]
},
'/block-all': {
registry: ['https://registry.yarnpkg.com', 'https://registry.npmjs.org'],
rules: { policy: 'deny', name: '*' }
},
'/npm-proxy': {
registry: 'https://registry.npmjs.org'
},
'/yarn-proxy': {
registry: 'https://registry.yarnpkg.com',
},
'*': {
registry: 'https://registry.yarnpkg.com',
}
}
}
const app = createApp(cfg)
test('is runnable', async () => {
await app.start()
})
;[
[
'returns healthcheck',
{ url: 'http://localhost:3001/healthcheck/', method: 'GET'},
{ statusCode: 200, body: '{"status":"OK"}' }
],
[
'returns metrics',
{ url: 'http://localhost:3001/metrics/', method: 'GET'},
{ statusCode: 200}
],
[
'404 if not found',
{ url: 'http://localhost:3001/registry/not-found/path/on/remote', method: 'GET'},
{ statusCode: 404 }
],
[
'gets tarball if allowed',
{ url: 'http://localhost:3001/registry/@antongolub/git-root/-/git-root-1.5.6.tgz', method: 'GET'},
{ statusCode: 302 }
],
[
'reads packument via GET',
{ url: 'http://localhost:3001/registry/d', method: 'GET'},
{ statusCode: 200, json: {
name: 'd',
description: 'Property descriptor factory',
versions: {
'0.1.0': {
name: 'd',
version: '0.1.0',
dist: {
tarball: 'http://localhost:3001/registry/d/-/d-0.1.0.tgz'
}
}
}
}
}
],
[
'reads packument via HEAD',
{ url: 'http://localhost:3001/registry/d', method: 'HEAD'},
{ statusCode: 200 }
],
[
'405 if not allowed',
{ url: 'http://localhost:3001/registry/d', method: 'PUT'},
{ statusCode: 405 }
],
[
'304 if etag matches if-none-match',
{
url: 'http://localhost:3001/registry/d',
method: 'GET',
headers() {
return request({url: 'http://localhost:3001/registry/d'})
.then(({headers: {etag}}) => ({
'if-none-match': etag
}))
}
},
{ statusCode: 304 }
],
[
'works as proxy',
{ url: 'http://localhost:3001/npm-proxy/d', method: 'GET'},
{ statusCode: 200 }
],
[
'uses default proxy if specified',
{ url: 'http://localhost:3001/unknown/d', method: 'GET'},
{ statusCode: 200 }
],
].forEach(([name, {url, method, headers: _headers = {}}, expected]) => {
test(name, async () => {
let result
const headers = typeof _headers === 'function' ? await _headers() : _headers
try {
const res = await request({url, method, headers})
const hash = crypto
.createHash('sha512')
.update(res.buffer)
.digest('base64')
result = {
statusCode: res.statusCode,
body: res.body,
json: JSON.parse(res.body || null),
hash,
}
} catch ({statusCode}) {
result = {
statusCode
}
}
objectContaining(result, expected)
})
})
test('is stoppable', async () => {
await app.stop()
})