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

pmcelhaney / counterfact / 9200039388

23 May 2024 12:07AM UTC coverage: 87.981%. First build
9200039388

Pull #913

github

web-flow
Merge branch 'main' into per-endpoint-proxy
Pull Request #913: per endpoint proxy

991 of 1098 branches covered (90.26%)

Branch coverage included in aggregate %.

139 of 173 new or added lines in 7 files covered. (80.35%)

3218 of 3686 relevant lines covered (87.3%)

44.67 hits per line

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

0.0
/src/app.ts
1
import { rm } from "node:fs/promises";
×
2
import nodePath from "node:path";
×
3

×
4
import { createHttpTerminator, type HttpTerminator } from "http-terminator";
×
5
import yaml from "js-yaml";
×
6
import $RefParser from "json-schema-ref-parser";
×
7

×
NEW
8
import { startRepl } from "./repl/repl.js";
×
NEW
9
import type { Config } from "./server/config.js";
×
NEW
10
import { ContextRegistry } from "./server/context-registry.js";
×
NEW
11
import { createKoaApp } from "./server/create-koa-app.js";
×
NEW
12
import { Dispatcher, type OpenApiDocument } from "./server/dispatcher.js";
×
NEW
13
import { koaMiddleware } from "./server/koa-middleware.js";
×
NEW
14
import { ModuleLoader } from "./server/module-loader.js";
×
NEW
15
import { Registry } from "./server/registry.js";
×
NEW
16
import { Transpiler } from "./server/transpiler.js";
×
NEW
17
import { CodeGenerator } from "./typescript-generator/code-generator.js";
×
NEW
18
import { readFile } from "./util/read-file.js";
×
19

×
20
async function loadOpenApiDocument(source: string) {
×
21
  try {
×
22
    const text = await readFile(source);
×
23
    // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
×
24
    const openApiDocument = (await yaml.load(text)) as $RefParser.JSONSchema;
×
25

×
26
    // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
×
27
    return (await $RefParser.dereference(openApiDocument)) as OpenApiDocument;
×
28
  } catch {
×
29
    return undefined;
×
30
  }
×
31
}
×
32

×
33
// eslint-disable-next-line max-statements
×
34
export async function counterfact(config: Config) {
×
35
  const modulesPath = config.basePath;
×
36

×
37
  const compiledPathsDirectory = nodePath
×
38
    .join(modulesPath, ".cache")
×
39
    .replaceAll("\\", "/");
×
40

×
41
  await rm(compiledPathsDirectory, { force: true, recursive: true });
×
42

×
43
  const registry = new Registry();
×
44

×
45
  const contextRegistry = new ContextRegistry();
×
46

×
47
  const codeGenerator = new CodeGenerator(
×
48
    config.openApiPath,
×
49
    config.basePath,
×
50
    config.generate,
×
51
  );
×
52

×
53
  const dispatcher = new Dispatcher(
×
54
    registry,
×
55
    contextRegistry,
×
56
    await loadOpenApiDocument(config.openApiPath),
×
57
  );
×
58

×
59
  const transpiler = new Transpiler(
×
60
    nodePath.join(modulesPath, "paths").replaceAll("\\", "/"),
×
61
    compiledPathsDirectory,
×
62
    "commonjs",
×
63
  );
×
64

×
65
  const moduleLoader = new ModuleLoader(
×
66
    compiledPathsDirectory,
×
67
    registry,
×
68
    contextRegistry,
×
69
  );
×
70

×
71
  const middleware = koaMiddleware(dispatcher, config);
×
72

×
73
  const koaApp = createKoaApp(registry, middleware, config);
×
74

×
75
  // eslint-disable-next-line max-statements
×
76
  async function start(options: Config) {
×
77
    const {
×
78
      generate,
×
79
      startRepl: shouldStartRepl,
×
80
      startServer,
×
81
      watch,
×
82
    } = options;
×
83

×
84
    if (generate.routes || generate.types) {
×
85
      await codeGenerator.generate();
×
86
    }
×
87
    if (watch.routes || watch.types) {
×
88
      await codeGenerator.watch();
×
89
    }
×
90

×
91
    // eslint-disable-next-line @typescript-eslint/init-declarations
×
92
    let httpTerminator: HttpTerminator | undefined;
×
93

×
94
    if (startServer) {
×
95
      await transpiler.watch();
×
96
      await moduleLoader.load();
×
97
      await moduleLoader.watch();
×
98
      const server = koaApp.listen({
×
99
        port: config.port,
×
100
      });
×
101

×
102
      httpTerminator = createHttpTerminator({
×
103
        server,
×
104
      });
×
105
    }
×
106

×
107
    const replServer = shouldStartRepl && startRepl(contextRegistry, config);
×
108

×
109
    return {
×
110
      replServer,
×
111

×
112
      async stop() {
×
113
        await codeGenerator.stopWatching();
×
114
        await transpiler.stopWatching();
×
115
        await moduleLoader.stopWatching();
×
116
        await httpTerminator?.terminate();
×
117
      },
×
118
    };
×
119
  }
×
120

×
121
  return {
×
122
    contextRegistry,
×
123
    koaApp,
×
124
    koaMiddleware: middleware,
×
125
    registry,
×
126
    start,
×
127
  };
×
128
}
×
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