@veducio/keja 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
1
3
|
import path from "node:path";
|
|
2
4
|
import { describe, expect, it } from "vitest";
|
|
3
5
|
import { freshImportUrl, resolveProjectImport } from "./reload-graph.ts";
|
|
@@ -26,6 +28,32 @@ describe("project reload imports", () => {
|
|
|
26
28
|
});
|
|
27
29
|
});
|
|
28
30
|
|
|
31
|
+
it("adds a TypeScript extension to an extensionless relative import", () => {
|
|
32
|
+
const project = mkdtempSync(path.join(tmpdir(), "keja-reload-"));
|
|
33
|
+
const repositories = path.join(project, "src", "db", "repositories.ts");
|
|
34
|
+
const importer = path.join(
|
|
35
|
+
project,
|
|
36
|
+
"src",
|
|
37
|
+
"public",
|
|
38
|
+
"config",
|
|
39
|
+
"tenants.ts",
|
|
40
|
+
);
|
|
41
|
+
mkdirSync(path.dirname(repositories), { recursive: true });
|
|
42
|
+
mkdirSync(path.dirname(importer), { recursive: true });
|
|
43
|
+
writeFileSync(repositories, "export {}\n");
|
|
44
|
+
try {
|
|
45
|
+
const resolved = resolveProjectImport({
|
|
46
|
+
importer: `keja:${importer}`,
|
|
47
|
+
specifier: "../../db/repositories",
|
|
48
|
+
projectRoot: project,
|
|
49
|
+
generation: "4",
|
|
50
|
+
});
|
|
51
|
+
expect(resolved?.file).toBe(repositories);
|
|
52
|
+
} finally {
|
|
53
|
+
rmSync(project, { recursive: true, force: true });
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
29
57
|
it("follows the next file imported by that dependency", () => {
|
|
30
58
|
const data = path.join(root, "src", "data.ts");
|
|
31
59
|
const resolved = resolveProjectImport({
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
1
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
|
|
@@ -35,7 +35,9 @@ export function resolveProjectImport(input: {
|
|
|
35
35
|
if (!inProject && !inGraph) {
|
|
36
36
|
return null;
|
|
37
37
|
}
|
|
38
|
-
const resolved =
|
|
38
|
+
const resolved = resolveRelativeFile(
|
|
39
|
+
path.resolve(path.dirname(importerFile), specifier),
|
|
40
|
+
);
|
|
39
41
|
if (isNodeModules(resolved)) {
|
|
40
42
|
return null;
|
|
41
43
|
}
|
|
@@ -148,6 +150,46 @@ function isRelativeSpecifier(specifier: string) {
|
|
|
148
150
|
return specifier.startsWith("./") || specifier.startsWith("../");
|
|
149
151
|
}
|
|
150
152
|
|
|
153
|
+
const sourceExtensions = [
|
|
154
|
+
".ts",
|
|
155
|
+
".tsx",
|
|
156
|
+
".js",
|
|
157
|
+
".jsx",
|
|
158
|
+
".mjs",
|
|
159
|
+
".cjs",
|
|
160
|
+
".json",
|
|
161
|
+
] as const;
|
|
162
|
+
|
|
163
|
+
function resolveRelativeFile(resolved: string) {
|
|
164
|
+
if (fileExists(resolved)) {
|
|
165
|
+
return resolved;
|
|
166
|
+
}
|
|
167
|
+
if (path.extname(resolved) !== "") {
|
|
168
|
+
return resolved;
|
|
169
|
+
}
|
|
170
|
+
for (const extension of sourceExtensions) {
|
|
171
|
+
const candidate = `${resolved}${extension}`;
|
|
172
|
+
if (fileExists(candidate)) {
|
|
173
|
+
return candidate;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
for (const extension of sourceExtensions) {
|
|
177
|
+
const candidate = path.join(resolved, `index${extension}`);
|
|
178
|
+
if (fileExists(candidate)) {
|
|
179
|
+
return candidate;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return resolved;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function fileExists(file: string) {
|
|
186
|
+
try {
|
|
187
|
+
return existsSync(file) && statSync(file).isFile();
|
|
188
|
+
} catch {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
151
193
|
function filePath(importer: string) {
|
|
152
194
|
const withoutQuery = importer.split("?")[0] ?? importer;
|
|
153
195
|
const clean = withoutQuery.startsWith(`${namespace}:`)
|
|
@@ -60,4 +60,18 @@ describe("runMiddleware", () => {
|
|
|
60
60
|
).rejects.toThrow(`Middleware cannot pass reserved field ${key}`);
|
|
61
61
|
}
|
|
62
62
|
});
|
|
63
|
+
|
|
64
|
+
it("rejects next() when the rest of the chain does not return a Response", async () => {
|
|
65
|
+
const step = defineMiddleware(async ({ next }) => {
|
|
66
|
+
const response = await next();
|
|
67
|
+
return response;
|
|
68
|
+
});
|
|
69
|
+
await expect(
|
|
70
|
+
runMiddleware(
|
|
71
|
+
[{ definition: step, allowFields: true }],
|
|
72
|
+
context,
|
|
73
|
+
() => undefined as never,
|
|
74
|
+
),
|
|
75
|
+
).rejects.toThrow("next() must resolve to a Response");
|
|
76
|
+
});
|
|
63
77
|
});
|
package/src/route/middleware.ts
CHANGED
|
@@ -128,7 +128,11 @@ export async function runMiddleware(
|
|
|
128
128
|
}
|
|
129
129
|
Object.assign(fields, passed);
|
|
130
130
|
}
|
|
131
|
-
|
|
131
|
+
const nextResponse = await dispatch();
|
|
132
|
+
if (!(nextResponse instanceof Response)) {
|
|
133
|
+
throw new Error("next() must resolve to a Response");
|
|
134
|
+
}
|
|
135
|
+
return nextResponse;
|
|
132
136
|
},
|
|
133
137
|
});
|
|
134
138
|
if (!(response instanceof Response)) {
|