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

pmcelhaney / counterfact / 14072284100

25 Mar 2025 11:47PM UTC coverage: 82.574% (+0.03%) from 82.544%
14072284100

push

github

web-flow
Merge pull request #1239 from pmcelhaney/interceptors

Middleware

1123 of 1254 branches covered (89.55%)

Branch coverage included in aggregate %.

154 of 165 new or added lines in 6 files covered. (93.33%)

7 existing lines in 3 files now uncovered.

3336 of 4146 relevant lines covered (80.46%)

63.12 hits per line

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

86.01
/src/server/module-loader.ts
1
/* eslint-disable n/no-sync */
2✔
2
import { once } from "node:events";
2✔
3
import { existsSync } from "node:fs";
2✔
4
import fs from "node:fs/promises";
2✔
5
import nodePath, { basename, dirname } from "node:path";
2✔
6

2✔
7
import { type FSWatcher, watch } from "chokidar";
2✔
8
import createDebug from "debug";
2✔
9

2✔
10
import { CHOKIDAR_OPTIONS } from "./constants.js";
2✔
11
import { type Context, ContextRegistry } from "./context-registry.js";
2✔
12
import { determineModuleKind } from "./determine-module-kind.js";
2✔
13
import { ModuleDependencyGraph } from "./module-dependency-graph.js";
2✔
14
import type { MiddlewareFunction, Module, Registry } from "./registry.js";
2✔
15
import { uncachedImport } from "./uncached-import.js";
2✔
16
import path from "node:path";
2✔
17

2✔
18
const { uncachedRequire } = await import("./uncached-require.cjs");
2✔
19

2✔
20
const debug = createDebug("counterfact:server:module-loader");
2✔
21

2✔
22
interface ContextModule {
2✔
23
  Context?: Context;
2✔
24
}
2✔
25

2✔
26
function isContextModule(
12✔
27
  module: ContextModule | Module,
12✔
28
): module is ContextModule {
12✔
29
  return "Context" in module && typeof module.Context === "function";
12✔
30
}
12✔
31

2✔
32
function isMiddlewareModule(
4✔
33
  module: ContextModule | Module,
4✔
34
): module is ContextModule & { middleware: MiddlewareFunction } {
4✔
35
  return (
4✔
36
    "middleware" in module &&
4✔
37
    typeof Object.getOwnPropertyDescriptor(module, "middleware")?.value ===
4✔
38
      "function"
4✔
39
  );
4✔
40
}
4✔
41

2✔
42
export class ModuleLoader extends EventTarget {
2✔
43
  private readonly basePath: string;
16✔
44

16✔
45
  public readonly registry: Registry;
16✔
46

16✔
47
  private watcher: FSWatcher | undefined;
16✔
48

16✔
49
  private readonly contextRegistry: ContextRegistry;
16✔
50

16✔
51
  private readonly dependencyGraph = new ModuleDependencyGraph();
16✔
52

16✔
53
  private readonly uncachedImport: (moduleName: string) => Promise<unknown> =
16✔
54
    async function (moduleName: string) {
16✔
55
      throw new Error(`uncachedImport not set up; importing ${moduleName}`);
×
56
    };
×
57

16✔
58
  public constructor(
16✔
59
    basePath: string,
16✔
60
    registry: Registry,
16✔
61
    contextRegistry = new ContextRegistry(),
16✔
62
  ) {
16✔
63
    super();
16✔
64
    this.basePath = basePath.replaceAll("\\", "/");
16✔
65
    this.registry = registry;
16✔
66
    this.contextRegistry = contextRegistry;
16✔
67
  }
16✔
68

16✔
69
  public async watch(): Promise<void> {
16✔
70
    this.watcher = watch(this.basePath, CHOKIDAR_OPTIONS).on(
4✔
71
      "all",
4✔
72

4✔
73
      (eventName: string, pathNameOriginal: string) => {
4✔
74
        const JS_EXTENSIONS = ["js", "mjs", "cjs", "ts", "mts", "cts"];
4✔
75

4✔
76
        if (
4✔
77
          !JS_EXTENSIONS.some((extension) =>
4✔
78
            pathNameOriginal.endsWith(`.${extension}`),
14✔
79
          )
4✔
80
        )
4✔
81
          return;
4✔
82

2✔
83
        const pathName = pathNameOriginal.replaceAll("\\", "/");
2✔
84

2✔
85
        if (pathName.includes("$.context") && eventName === "add") {
4!
86
          process.stdout.write(
×
87
            `\n\n!!! The file at ${pathName} needs a minor update.\n    See https://github.com/pmcelhaney/counterfact/blob/main/docs/context-change.md\n\n\n`,
×
88
          );
×
89

×
90
          return;
×
91
        }
✔
92

2✔
93
        if (!["add", "change", "unlink"].includes(eventName)) {
4!
94
          return;
×
95
        }
✔
96

2✔
97
        const parts = nodePath.parse(pathName.replace(this.basePath, ""));
2✔
98
        const url = `/${parts.dir}/${parts.name}`
2✔
99
          .replaceAll("\\", "/")
2✔
100
          .replaceAll(/\/+/gu, "/");
2✔
101

2✔
102
        if (eventName === "unlink") {
2✔
103
          this.registry.remove(url);
2✔
104
          this.dispatchEvent(new Event("remove"));
2✔
105
        }
2✔
106

2✔
107
        const dependencies = this.dependencyGraph.dependentsOf(pathName);
2✔
108

2✔
109
        void this.loadEndpoint(pathName);
2✔
110

2✔
111
        for (const dependency of dependencies) {
4!
112
          void this.loadEndpoint(dependency);
×
113
        }
✔
114
      },
2✔
115
    );
4✔
116
    await once(this.watcher, "ready");
4✔
117
  }
4✔
118

16✔
119
  public async stopWatching(): Promise<void> {
16✔
120
    await this.watcher?.close();
4✔
121
  }
4✔
122

16✔
123
  public async load(directory = ""): Promise<void> {
16✔
124
    if (
28✔
125
      !existsSync(nodePath.join(this.basePath, directory).replaceAll("\\", "/"))
28✔
126
    ) {
28!
127
      throw new Error(`Directory does not exist ${this.basePath}`);
×
128
    }
×
129

28✔
130
    const files = await fs.readdir(
28✔
131
      nodePath.join(this.basePath, directory).replaceAll("\\", "/"),
28✔
132
      {
28✔
133
        withFileTypes: true,
28✔
134
      },
28✔
135
    );
28✔
136

28✔
137
    const imports = files.flatMap(async (file): Promise<void> => {
28✔
138
      const extension = file.name.split(".").at(-1);
56✔
139

56✔
140
      if (file.isDirectory()) {
56✔
141
        await this.load(
12✔
142
          nodePath.join(directory, file.name).replaceAll("\\", "/"),
12✔
143
        );
12✔
144

12✔
145
        return;
12✔
146
      }
12✔
147

44✔
148
      if (!["cjs", "cts", "js", "mjs", "mts", "ts"].includes(extension ?? "")) {
56✔
149
        return;
18✔
150
      }
18✔
151

26✔
152
      const fullPath = nodePath
26✔
153
        .join(this.basePath, directory, file.name)
26✔
154
        .replaceAll("\\", "/");
26✔
155

26✔
156
      await this.loadEndpoint(fullPath);
26✔
157
    });
26✔
158

28✔
159
    await Promise.all(imports);
28✔
160
  }
28✔
161

16✔
162
  private async loadEndpoint(pathName: string) {
16✔
163
    debug("importing module: %s", pathName);
28✔
164

28✔
165
    const directory = dirname(pathName.slice(this.basePath.length)).replaceAll(
28✔
166
      "\\",
28✔
167
      "/",
28✔
168
    );
28✔
169

28✔
170
    const url = `/${nodePath.join(
28✔
171
      directory,
28✔
172
      nodePath.parse(basename(pathName)).name,
28✔
173
    )}`
28✔
174
      .replaceAll("\\", "/")
28✔
175
      .replaceAll(/\/+/gu, "/");
28✔
176

28✔
177
    this.dependencyGraph.load(pathName);
28✔
178

28✔
179
    try {
28✔
180
      const doImport =
28✔
181
        (await determineModuleKind(pathName)) === "commonjs"
28!
182
          ? uncachedRequire
×
183
          : uncachedImport;
28✔
184

28✔
185
      const endpoint = (await doImport(pathName).catch((err) => {
28✔
NEW
186
        console.log("ERROR");
×
NEW
187
      })) as ContextModule | Module;
×
188

28✔
189
      this.dispatchEvent(new Event("add"));
28✔
190

28✔
191
      if (
28✔
192
        basename(pathName).startsWith("_.context.") &&
28✔
193
        isContextModule(endpoint)
12✔
194
      ) {
28✔
195
        const loadContext = (path: string) => this.contextRegistry.find(path);
12✔
196

12✔
197
        this.contextRegistry.update(
12✔
198
          directory,
12✔
199

12✔
200
          // @ts-expect-error TS says Context has no constructable signatures but that's not true?
12✔
201

12✔
202
          new endpoint.Context({
12✔
203
            loadContext,
12✔
204
          }),
12✔
205
        );
12✔
206
        return;
12✔
207
      }
12✔
208

16✔
209
      if (
16✔
210
        basename(pathName).startsWith("_.middleware.") &&
16✔
211
        isMiddlewareModule(endpoint)
4✔
212
      ) {
28✔
213
        this.registry.addMiddleware(
4✔
214
          url.slice(0, url.lastIndexOf("/")) || "/",
4✔
215
          endpoint.middleware,
4✔
216
        );
4✔
217
      }
4✔
218

16✔
219
      if (url === "/index") this.registry.add("/", endpoint as Module);
28✔
220
      this.registry.add(url, endpoint as Module);
16✔
221
    } catch (error: unknown) {
28!
222
      if (
×
223
        String(error) ===
×
224
        "SyntaxError: Identifier 'Context' has already been declared"
×
225
      ) {
×
226
        // Not sure why Node throws this error. It doesn't seem to matter.
×
227
        return;
×
228
      }
×
229

×
230
      process.stdout.write(`\nError loading ${pathName}:\n${String(error)}\n`);
×
NEW
231

×
NEW
232
      throw error;
×
UNCOV
233
    }
×
234
  }
28✔
235
}
16✔
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

© 2026 Coveralls, Inc