-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmodule-loader.ts
More file actions
296 lines (233 loc) · 8.46 KB
/
module-loader.ts
File metadata and controls
296 lines (233 loc) · 8.46 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { once } from "node:events";
import fs from "node:fs/promises";
import nodePath, { basename, dirname } from "node:path";
import { type FSWatcher, watch } from "chokidar";
import createDebug from "debug";
import { CHOKIDAR_OPTIONS } from "./constants.js";
import { ContextRegistry } from "./context-registry.js";
import { determineModuleKind } from "./determine-module-kind.js";
import type { OpenApiDocument } from "./dispatcher.js";
import { FileDiscovery } from "./file-discovery.js";
import {
type ContextModule,
isContextModule,
isMiddlewareModule,
} from "./middleware-detector.js";
import { ModuleDependencyGraph } from "./module-dependency-graph.js";
import type { Module, Registry } from "./registry.js";
import { uncachedImport } from "./uncached-import.js";
import { unescapePathForWindows } from "../util/windows-escape.js";
const { uncachedRequire } = await import("./uncached-require.cjs");
const debug = createDebug("counterfact:server:module-loader");
export class ModuleLoader extends EventTarget {
private readonly basePath: string;
public readonly registry: Registry;
private watcher: FSWatcher | undefined;
private readonly contextRegistry: ContextRegistry;
private openApiDocumentRef: OpenApiDocument;
private readonly openApiDocumentProxy: OpenApiDocument;
private readonly dependencyGraph = new ModuleDependencyGraph();
private readonly fileDiscovery: FileDiscovery;
private readonly uncachedImport: (moduleName: string) => Promise<unknown> =
async function (moduleName: string) {
throw new Error(`uncachedImport not set up; importing ${moduleName}`);
};
public constructor(
basePath: string,
registry: Registry,
contextRegistry = new ContextRegistry(),
openApiDocument: OpenApiDocument = {},
) {
super();
this.basePath = basePath.replaceAll("\\", "/");
this.registry = registry;
this.contextRegistry = contextRegistry;
this.openApiDocumentRef = openApiDocument;
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
this.openApiDocumentProxy = new Proxy({} as OpenApiDocument, {
deleteProperty() {
return false;
},
get(_target, prop) {
return Reflect.get(self.openApiDocumentRef as object, prop);
},
getOwnPropertyDescriptor(_target, prop) {
return Reflect.getOwnPropertyDescriptor(self.openApiDocumentRef, prop);
},
has(_target, prop) {
return Reflect.has(self.openApiDocumentRef, prop);
},
ownKeys() {
return Reflect.ownKeys(self.openApiDocumentRef);
},
set() {
return false;
},
});
this.fileDiscovery = new FileDiscovery(this.basePath);
}
public setOpenApiDocument(newDoc: OpenApiDocument): void {
this.openApiDocumentRef = newDoc;
}
public async watch(): Promise<void> {
this.watcher = watch(this.basePath, CHOKIDAR_OPTIONS).on(
"all",
(eventName: string, pathNameOriginal: string) => {
const JS_EXTENSIONS = ["js", "mjs", "cjs", "ts", "mts", "cts"];
if (
!JS_EXTENSIONS.some((extension) =>
pathNameOriginal.endsWith(`.${extension}`),
)
)
return;
const pathName = pathNameOriginal.replaceAll("\\", "/");
if (pathName.includes("$.context") && eventName === "add") {
process.stdout.write(
`\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`,
);
return;
}
if (!["add", "change", "unlink"].includes(eventName)) {
return;
}
const parts = nodePath.parse(pathName.replace(this.basePath, ""));
const url = unescapePathForWindows(
`/${parts.dir}/${parts.name}`
.replaceAll("\\", "/")
.replaceAll(/\/+/gu, "/"),
);
if (eventName === "unlink") {
this.registry.remove(url);
this.dispatchEvent(new Event("remove"));
return;
}
const dependencies = this.dependencyGraph.dependentsOf(pathName);
void this.loadEndpoint(pathName);
for (const dependency of dependencies) {
void this.loadEndpoint(dependency);
}
},
);
await once(this.watcher, "ready");
}
public async stopWatching(): Promise<void> {
await this.watcher?.close();
}
public async load(directory = ""): Promise<void> {
const files = await this.fileDiscovery.findFiles(directory);
await Promise.all(files.map((file) => this.loadEndpoint(file)));
}
private async loadEndpoint(pathName: string) {
debug("importing module: %s", pathName);
const directory = dirname(pathName.slice(this.basePath.length)).replaceAll(
"\\",
"/",
);
const url = unescapePathForWindows(
`/${nodePath.join(directory, nodePath.parse(basename(pathName)).name)}`
.replaceAll("\\", "/")
.replaceAll(/\/+/gu, "/"),
);
debug(`loading pathName from dependencyGraph: ${pathName}`);
this.dependencyGraph.load(pathName);
try {
const doImport =
(await determineModuleKind(pathName)) === "commonjs"
? uncachedRequire
: uncachedImport;
let importError: unknown;
const endpoint = (await doImport(pathName).catch((error: unknown) => {
importError = error;
})) as ContextModule | Module;
if (importError !== undefined) {
const isSyntaxError =
importError instanceof SyntaxError ||
String(importError).startsWith("SyntaxError:");
const displayPath = nodePath
.relative(process.cwd(), unescapePathForWindows(pathName))
.replaceAll("\\", "/");
const message = isSyntaxError
? `There is a syntax error in the route file: ${displayPath}`
: `There was an error loading the route file: ${displayPath}`;
const errorResponse = () => ({
body: message,
status: 500,
});
this.registry.add(url, {
DELETE: errorResponse,
GET: errorResponse,
HEAD: errorResponse,
OPTIONS: errorResponse,
PATCH: errorResponse,
POST: errorResponse,
PUT: errorResponse,
TRACE: errorResponse,
});
this.dispatchEvent(new Event("add"));
return;
}
if (!endpoint) {
return;
}
this.dispatchEvent(new Event("add"));
if (
basename(pathName).startsWith("_.context.") &&
isContextModule(endpoint)
) {
const loadContext = (path: string) => this.contextRegistry.find(path);
const contextDir = nodePath.dirname(unescapePathForWindows(pathName));
const readJson = async (relativePath: string): Promise<unknown> => {
const absolutePath = nodePath.resolve(contextDir, relativePath);
let content: string;
try {
content = await fs.readFile(absolutePath, "utf8");
} catch {
throw new Error(
`readJson: could not read file at "${absolutePath}" (resolved from "${relativePath}" relative to "${contextDir}")`,
);
}
try {
return JSON.parse(content) as unknown;
} catch {
throw new Error(
`readJson: file at "${absolutePath}" does not contain valid JSON`,
);
}
};
this.contextRegistry.update(
directory,
// @ts-expect-error TS says Context has no constructable signatures but that's not true?
new endpoint.Context({
loadContext,
openApiDocument: this.openApiDocumentProxy,
readJson,
}),
);
return;
}
if (
basename(pathName).startsWith("_.middleware.") &&
isMiddlewareModule(endpoint)
) {
this.registry.addMiddleware(
url.slice(0, url.lastIndexOf("/")) || "/",
endpoint.middleware,
);
}
if (url === "/index") this.registry.add("/", endpoint as Module);
debug(`adding "${url}" to registry`);
this.registry.add(url, endpoint as Module);
} catch (error: unknown) {
if (
String(error) ===
"SyntaxError: Identifier 'Context' has already been declared"
) {
// Not sure why Node throws this error. It doesn't seem to matter.
return;
}
process.stdout.write(`\nError loading ${pathName}:\n${String(error)}\n`);
throw error;
}
}
}