• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmcelhaney / counterfact / 5941942840

22 Aug 2023 05:13PM UTC coverage: 87.442% (+0.1%) from 87.318%
5941942840

Pull #377

github

pmcelhaney
is the promise really just not resolving?
Pull Request #377: run CI on Windows as well as Linux

432 of 460 branches covered (93.91%)

Branch coverage included in aggregate %.

214 of 214 new or added lines in 13 files covered. (100.0%)

2005 of 2327 relevant lines covered (86.16%)

21.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/server/start.js
1
/* eslint-disable max-statements */
×
2
import nodePath from "node:path";
×
3
import { pathToFileURL } from "node:url";
×
4

×
5
import yaml from "js-yaml";
×
6
import Koa from "koa";
×
7
import bodyParser from "koa-bodyparser";
×
8
import { koaSwagger } from "koa2-swagger-ui";
×
9
import Handlebars from "handlebars";
×
10

×
11
import { readFile } from "../util/read-file.js";
×
12

×
13
import { counterfact } from "./counterfact.js";
×
14

×
15
// eslint-disable-next-line no-underscore-dangle
×
16
const __dirname = nodePath.dirname(new URL(import.meta.url).pathname);
×
17

×
18
const DEFAULT_PORT = 3100;
×
19

×
20
Handlebars.registerHelper("escape_route", (route) =>
×
21
  route.replaceAll(/[^\w/]/gu, "-")
×
22
);
×
23

×
24
function openapi(openApiPath, url) {
×
25
  return async (ctx, next) => {
×
26
    if (ctx.URL.pathname === "/counterfact/openapi") {
×
27
      const openApiDocument = await yaml.load(await readFile(openApiPath));
×
28

×
29
      openApiDocument.servers ??= [];
×
30

×
31
      openApiDocument.servers.unshift({
×
32
        description: "Counterfact",
×
33
        url,
×
34
      });
×
35

×
36
      // OpenApi 2 support:
×
37
      openApiDocument.host = url;
×
38

×
39
      // eslint-disable-next-line require-atomic-updates
×
40
      ctx.body = yaml.dump(openApiDocument);
×
41

×
42
      return;
×
43
    }
×
44

×
45
    // eslint-disable-next-line  n/callback-return
×
46
    await next();
×
47
  };
×
48
}
×
49

×
50
function page(pathname, templateName, locals) {
×
51
  return async (ctx, next) => {
×
52
    const render = Handlebars.compile(
×
53
      await readFile(
×
54
        nodePath
×
55
          .join(__dirname, `../client/${templateName}.html.hbs`)
×
56
          .replaceAll("\\", "/")
×
57
      )
×
58
    );
×
59

×
60
    if (ctx.URL.pathname === pathname) {
×
61
      // eslint-disable-next-line require-atomic-updates
×
62
      ctx.body = await render(locals);
×
63

×
64
      return;
×
65
    }
×
66

×
67
    // eslint-disable-next-line  n/callback-return
×
68
    await next();
×
69
  };
×
70
}
×
71

×
72
export async function start(config) {
×
73
  const {
×
74
    basePath = process.cwd(),
×
75
    openApiPath = nodePath
×
76
      .join(basePath, "../openapi.yaml")
×
77
      .replaceAll("\\", "/"),
×
78
    port = DEFAULT_PORT,
×
79
  } = config;
×
80

×
81
  const app = new Koa();
×
82

×
83
  const { koaMiddleware, contextRegistry, registry } = await counterfact(
×
84
    basePath,
×
85
    openApiPath,
×
86
    config
×
87
  );
×
88

×
89
  app.use(openapi(openApiPath, `//localhost:${port}`));
×
90

×
91
  app.use(
×
92
    koaSwagger({
×
93
      routePrefix: "/counterfact/swagger",
×
94

×
95
      swaggerOptions: {
×
96
        url: "/counterfact/openapi",
×
97
      },
×
98
    })
×
99
  );
×
100

×
101
  app.use(
×
102
    page("/counterfact/", "index", {
×
103
      basePath,
×
104
      routes: registry.routes,
×
105
      methods: ["get", "post", "put", "delete", "patch"],
×
106
      openApiPath,
×
107

×
108
      openApiHref: openApiPath.includes("://")
×
109
        ? openApiPath
×
110
        : pathToFileURL(openApiPath).href,
×
111
    })
×
112
  );
×
113

×
114
  app.use(async (ctx, next) => {
×
115
    if (ctx.URL.pathname === "/counterfact") {
×
116
      ctx.redirect("/counterfact/");
×
117

×
118
      return;
×
119
    }
×
120

×
121
    // eslint-disable-next-line  n/callback-return
×
122
    await next();
×
123
  });
×
124

×
125
  app.use(
×
126
    page("/counterfact/rapidoc", "rapi-doc", {
×
127
      basePath,
×
128
      routes: registry.routes,
×
129
    })
×
130
  );
×
131

×
132
  app.use(bodyParser());
×
133

×
134
  app.use(koaMiddleware);
×
135

×
136
  app.listen(port);
×
137

×
138
  return { contextRegistry };
×
139
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc