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

pmcelhaney / counterfact / 11153055205

02 Oct 2024 11:19PM UTC coverage: 82.82% (+0.06%) from 82.759%
11153055205

push

github

renovate[bot]
reinstate check for JS extension

1046 of 1164 branches covered (89.86%)

Branch coverage included in aggregate %.

8 of 8 new or added lines in 1 file covered. (100.0%)

29 existing lines in 2 files now uncovered.

3259 of 4034 relevant lines covered (80.79%)

47.09 hits per line

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

85.21
/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 { Module, Registry } from "./registry.js";
2✔
15
import { uncachedImport } from "./uncached-import.js";
2✔
16

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

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

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

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

2✔
31
export class ModuleLoader extends EventTarget {
2✔
32
  private readonly basePath: string;
14✔
33

14✔
34
  public readonly registry: Registry;
14✔
35

14✔
36
  private watcher: FSWatcher | undefined;
14✔
37

14✔
38
  private readonly contextRegistry: ContextRegistry;
14✔
39

14✔
40
  private readonly dependencyGraph = new ModuleDependencyGraph();
14✔
41

14✔
42
  private readonly uncachedImport: (moduleName: string) => Promise<unknown> =
14✔
43
    async function (moduleName: string) {
14✔
44
      throw new Error(`uncachedImport not set up; importing ${moduleName}`);
×
45
    };
×
46

14✔
47
  public constructor(
14✔
48
    basePath: string,
14✔
49
    registry: Registry,
14✔
50
    contextRegistry = new ContextRegistry(),
14✔
51
  ) {
14✔
52
    super();
14✔
53
    this.basePath = basePath.replaceAll("\\", "/");
14✔
54
    this.registry = registry;
14✔
55
    this.contextRegistry = contextRegistry;
14✔
56
  }
14✔
57

14✔
58
  public async watch(): Promise<void> {
14✔
59
    this.watcher = watch(this.basePath, CHOKIDAR_OPTIONS).on(
4✔
60
      "all",
4✔
61

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

4✔
65
        if (
4✔
66
          !JS_EXTENSIONS.some((extension) =>
4✔
67
            pathNameOriginal.endsWith(`.${extension}`),
14✔
68
          )
4✔
69
        )
4✔
70
          return;
4✔
71

2✔
72
        const pathName = pathNameOriginal.replaceAll("\\", "/");
2✔
73

2✔
74
        if (pathName.includes("$.context") && eventName === "add") {
4!
UNCOV
75
          process.stdout.write(
×
UNCOV
76
            `\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`,
×
77
          );
×
78

×
UNCOV
79
          return;
×
UNCOV
80
        }
✔
81

2✔
82
        if (!["add", "change", "unlink"].includes(eventName)) {
4!
UNCOV
83
          return;
×
UNCOV
84
        }
✔
85

2✔
86
        const parts = nodePath.parse(pathName.replace(this.basePath, ""));
2✔
87
        const url = `/${parts.dir}/${parts.name}`
2✔
88
          .replaceAll("\\", "/")
2✔
89
          .replaceAll(/\/+/gu, "/");
2✔
90

2✔
91
        if (eventName === "unlink") {
2✔
92
          this.registry.remove(url);
2✔
93
          this.dispatchEvent(new Event("remove"));
2✔
94
        }
2✔
95

2✔
96
        const dependencies = this.dependencyGraph.dependentsOf(pathName);
2✔
97

2✔
98
        void this.loadEndpoint(pathName);
2✔
99

2✔
100
        for (const dependency of dependencies) {
4!
UNCOV
101
          void this.loadEndpoint(dependency);
×
UNCOV
102
        }
✔
103
      },
2✔
104
    );
4✔
105
    await once(this.watcher, "ready");
4✔
106
  }
4✔
107

14✔
108
  public async stopWatching(): Promise<void> {
14✔
109
    await this.watcher?.close();
4✔
110
  }
4✔
111

14✔
112
  public async load(directory = ""): Promise<void> {
14✔
113
    if (
24✔
114
      !existsSync(nodePath.join(this.basePath, directory).replaceAll("\\", "/"))
24✔
115
    ) {
24!
UNCOV
116
      throw new Error(`Directory does not exist ${this.basePath}`);
×
UNCOV
117
    }
×
118

24✔
119
    const files = await fs.readdir(
24✔
120
      nodePath.join(this.basePath, directory).replaceAll("\\", "/"),
24✔
121
      {
24✔
122
        withFileTypes: true,
24✔
123
      },
24✔
124
    );
24✔
125

24✔
126
    const imports = files.flatMap(async (file): Promise<void> => {
24✔
127
      const extension = file.name.split(".").at(-1);
48✔
128

48✔
129
      if (file.isDirectory()) {
48✔
130
        await this.load(
10✔
131
          nodePath.join(directory, file.name).replaceAll("\\", "/"),
10✔
132
        );
10✔
133

10✔
134
        return;
10✔
135
      }
10✔
136

38✔
137
      if (!["cjs", "cts", "js", "mjs", "mts", "ts"].includes(extension ?? "")) {
48✔
138
        return;
16✔
139
      }
16✔
140

22✔
141
      const fullPath = nodePath
22✔
142
        .join(this.basePath, directory, file.name)
22✔
143
        .replaceAll("\\", "/");
22✔
144

22✔
145
      await this.loadEndpoint(fullPath);
22✔
146
    });
22✔
147

24✔
148
    await Promise.all(imports);
24✔
149
  }
24✔
150

14✔
151
  private async loadEndpoint(pathName: string) {
14✔
152
    debug("importing module: %s", pathName);
24✔
153

24✔
154
    const directory = dirname(pathName.slice(this.basePath.length)).replaceAll(
24✔
155
      "\\",
24✔
156
      "/",
24✔
157
    );
24✔
158

24✔
159
    const url = `/${nodePath.join(
24✔
160
      directory,
24✔
161
      nodePath.parse(basename(pathName)).name,
24✔
162
    )}`
24✔
163
      .replaceAll("\\", "/")
24✔
164
      .replaceAll(/\/+/gu, "/");
24✔
165

24✔
166
    this.dependencyGraph.load(pathName);
24✔
167

24✔
168
    try {
24✔
169
      const doImport =
24✔
170
        (await determineModuleKind(pathName)) === "commonjs"
24!
UNCOV
171
          ? uncachedRequire
×
172
          : uncachedImport;
24✔
173

24✔
174
      const endpoint = (await doImport(pathName)) as ContextModule | Module;
24✔
175

24✔
176
      this.dispatchEvent(new Event("add"));
24✔
177

24✔
178
      if (basename(pathName).startsWith("_.context")) {
24✔
179
        if (isContextModule(endpoint)) {
12✔
180
          const loadContext = (path: string) => this.contextRegistry.find(path);
12✔
181

12✔
182
          this.contextRegistry.update(
12✔
183
            directory,
12✔
184

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

12✔
187
            new endpoint.Context({
12✔
188
              loadContext,
12✔
189
            }),
12✔
190
          );
12✔
191
        }
12✔
192
      } else {
12✔
193
        if (url === "/index") this.registry.add("/", endpoint as Module);
12✔
194
        this.registry.add(url, endpoint as Module);
12✔
195
      }
12✔
196
    } catch (error: unknown) {
24!
197
      if (
×
198
        String(error) ===
×
199
        "SyntaxError: Identifier 'Context' has already been declared"
×
200
      ) {
×
201
        // Not sure why Node throws this error. It doesn't seem to matter.
×
202
        return;
×
UNCOV
203
      }
×
UNCOV
204

×
UNCOV
205
      throw error;
×
UNCOV
206

×
UNCOV
207
      process.stdout.write(`\nError loading ${pathName}:\n${String(error)}\n`);
×
UNCOV
208
    }
×
209
  }
24✔
210
}
14✔
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