-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (55 loc) · 1.89 KB
/
index.js
File metadata and controls
69 lines (55 loc) · 1.89 KB
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
export default fixtureServerMiddleware;
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 = Object.assign({}, DEFAULTS, options);
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
Object.values(state.fixtures)
.flat()
.map((fixture) => fixture.scope.replace(/:(80|443)$/, ""))
.filter((url, i, arr) => arr.indexOf(url) === i)
.forEach((target) => {
middleware.use(proxy(state, { target }));
});
return middleware;
}