-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
72 lines (58 loc) · 1.96 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
export default fixtureServerMiddleware;
import _ from "lodash";
import bodyParser from "body-parser";
import cachimo from "cachimo";
import { Router } from "express";
import fixtures from "@octokit/fixtures";
import Log from "console-log-level";
import additions from "./lib/additions.js";
import proxy from "./lib/proxy.js";
import DEFAULTS from "./lib/defaults.js";
function fixtureServerMiddleware(options) {
const middleware = Router();
const state = _.defaults(_.clone(options), DEFAULTS);
if (!state.fixturesUrl) {
state.fixturesUrl = `http://localhost:${state.port}`;
}
state.cachimo = cachimo;
state.log = Log({
level: state.logLevel === "silent" ? "fatal" : state.logLevel,
});
middleware.post("/fixtures", bodyParser.json(), (request, response) => {
const id = Math.random().toString(36).substr(2);
const requestedFixture = state.fixtures[request.body.scenario];
if (!requestedFixture) {
return response.status(400).json({
error: `Scenario "${request.body.scenario}" not found`,
});
}
const mock = fixtures.mock(requestedFixture, (fixture) =>
additions(state, { id, fixture }),
);
cachimo
.put(id, mock, state.ttl)
.then(() => {
state.log.debug(
`Deleted fixtures "${id}" (${mock.pending().length} pending)`,
);
})
// throws error if key was deleted before timeout, safe to ignore
.catch(() => {});
const path = new URL(requestedFixture[0].scope).hostname + "/" + id;
response.status(201).json({
id,
url: new URL(path, state.fixturesUrl).href,
});
});
// load proxies for all unique scope URLs in fixtures
_.chain(state.fixtures)
.values()
.flatten()
.map("scope")
.uniq()
// remove default ports for http / https, they cause problems for the proxy
.map((url) => url.replace(/:(80|443)$/, ""))
.forEach((target) => middleware.use(proxy(state, { target })))
.value();
return middleware;
}