owebjs 1.3.7 → 1.3.8
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.
|
@@ -12,7 +12,14 @@ import generateFunctionFromTypescript from './generateFunctionFromTypescript.js'
|
|
|
12
12
|
import { readdirSync } from "node:fs";
|
|
13
13
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
14
14
|
let matcherOverrides = {};
|
|
15
|
-
let routeFunctions = {
|
|
15
|
+
let routeFunctions = {
|
|
16
|
+
get: {},
|
|
17
|
+
post: {},
|
|
18
|
+
put: {},
|
|
19
|
+
delete: {},
|
|
20
|
+
patch: {},
|
|
21
|
+
options: {}
|
|
22
|
+
};
|
|
16
23
|
const temporaryRequests = {
|
|
17
24
|
get: {},
|
|
18
25
|
post: {},
|
|
@@ -71,7 +78,7 @@ const applyRouteHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, fallba
|
|
|
71
78
|
if (op === "new-file") {
|
|
72
79
|
const start = Date.now();
|
|
73
80
|
const files = await walk(workingDir, [], fallbackDir);
|
|
74
|
-
const routes = await generateRoutes(files);
|
|
81
|
+
const routes = await generateRoutes(files, path2);
|
|
75
82
|
routesCache = routes;
|
|
76
83
|
const f = routes.find((x) => x.fileInfo.filePath == path2);
|
|
77
84
|
temporaryRequests[f.method.toLowerCase()][f.url] = inner(oweb, f);
|
|
@@ -80,13 +87,13 @@ const applyRouteHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, fallba
|
|
|
80
87
|
} else if (op === "modify-file") {
|
|
81
88
|
const start = Date.now();
|
|
82
89
|
const files = await walk(workingDir, [], fallbackDir);
|
|
83
|
-
const routes = await generateRoutes(files);
|
|
90
|
+
const routes = await generateRoutes(files, path2);
|
|
84
91
|
routesCache = routes;
|
|
85
92
|
const f = routes.find((x) => x.fileInfo.filePath == path2);
|
|
86
93
|
if (f.url in temporaryRequests[f.method.toLowerCase()]) {
|
|
87
94
|
temporaryRequests[f.method.toLowerCase()][f.url] = inner(oweb, f);
|
|
88
95
|
} else {
|
|
89
|
-
routeFunctions[f.
|
|
96
|
+
routeFunctions[f.method.toLowerCase()][f.url] = inner(oweb, f);
|
|
90
97
|
}
|
|
91
98
|
const end = Date.now() - start;
|
|
92
99
|
success(`Route ${f.method.toUpperCase()}:${f.url} reloaded in ${end}ms`, "HMR");
|
|
@@ -96,13 +103,13 @@ const applyRouteHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, fallba
|
|
|
96
103
|
if (f.url in temporaryRequests[f.method.toLowerCase()]) {
|
|
97
104
|
delete temporaryRequests[f.method.toLowerCase()][f.url];
|
|
98
105
|
} else {
|
|
99
|
-
delete routeFunctions[f.
|
|
106
|
+
delete routeFunctions[f.method.toLowerCase()][f.url];
|
|
100
107
|
}
|
|
101
108
|
const end = Date.now() - start;
|
|
102
109
|
success(`Route ${f.method.toUpperCase()}:${f.url} removed in ${end}ms`, "HMR");
|
|
103
110
|
}
|
|
104
111
|
}, "applyRouteHMR");
|
|
105
|
-
const generateRoutes = /* @__PURE__ */ __name(async (files) => {
|
|
112
|
+
const generateRoutes = /* @__PURE__ */ __name(async (files, onlyGenerateFn) => {
|
|
106
113
|
const routes = [];
|
|
107
114
|
for (const file of files) {
|
|
108
115
|
const parsedFile = path.parse(file.rel);
|
|
@@ -120,9 +127,12 @@ const generateRoutes = /* @__PURE__ */ __name(async (files) => {
|
|
|
120
127
|
});
|
|
121
128
|
continue;
|
|
122
129
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
130
|
+
let routeFuncs;
|
|
131
|
+
if (!(onlyGenerateFn && file.filePath !== onlyGenerateFn)) {
|
|
132
|
+
const cacheBuster = `?t=${Date.now()}`;
|
|
133
|
+
const def = await import(packageURL + cacheBuster);
|
|
134
|
+
routeFuncs = def.default;
|
|
135
|
+
}
|
|
126
136
|
routes.push({
|
|
127
137
|
url: route.url,
|
|
128
138
|
method: route.method,
|
|
@@ -205,12 +215,12 @@ __name(send404, "send404");
|
|
|
205
215
|
function assignSpecificRoute(oweb, route) {
|
|
206
216
|
if (!route.fn) return;
|
|
207
217
|
const routeFunc = new route.fn();
|
|
208
|
-
routeFunctions[route.
|
|
218
|
+
routeFunctions[route.method][route.url] = inner(oweb, route);
|
|
209
219
|
oweb[route.method](route.url, routeFunc._options || {}, function(req, res) {
|
|
210
|
-
if (routeFunctions[route.
|
|
211
|
-
return routeFunctions[route.
|
|
220
|
+
if (routeFunctions[route.method][route.url]) {
|
|
221
|
+
return routeFunctions[route.method][route.url](req, res);
|
|
212
222
|
} else {
|
|
213
|
-
const vals = temporaryRequests[
|
|
223
|
+
const vals = temporaryRequests[route.method];
|
|
214
224
|
const keys = Object.keys(vals);
|
|
215
225
|
if (!vals || !keys.length) {
|
|
216
226
|
return send404(req, res);
|
|
@@ -4,44 +4,49 @@ import {
|
|
|
4
4
|
import { parse } from "@babel/parser";
|
|
5
5
|
import traverse from "@babel/traverse";
|
|
6
6
|
import generate from "@babel/generator";
|
|
7
|
+
import * as babel from "@babel/core";
|
|
7
8
|
import path from "node:path";
|
|
8
|
-
import babel from "@babel/core";
|
|
9
9
|
import { pathToFileURL } from "node:url";
|
|
10
10
|
import { writeFile, unlink, readFile } from "node:fs/promises";
|
|
11
11
|
import { existsSync } from "node:fs";
|
|
12
|
-
import { tmpdir } from "node:os";
|
|
13
12
|
import { randomBytes } from "node:crypto";
|
|
14
|
-
import { createRequire } from "node:module";
|
|
15
13
|
import { error } from './logger.js';
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
async function getTsConfig(projectRoot) {
|
|
15
|
+
const tsConfigPath = path.join(projectRoot, "tsconfig.json");
|
|
18
16
|
if (!existsSync(tsConfigPath)) {
|
|
17
|
+
error("tsconfig.json not found in the project root.", "HMR");
|
|
19
18
|
return null;
|
|
20
19
|
}
|
|
21
20
|
try {
|
|
22
21
|
const tsConfigFile = await readFile(tsConfigPath, "utf-8");
|
|
23
22
|
const json = JSON.parse(tsConfigFile.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, ""));
|
|
24
23
|
const compilerOptions = json.compilerOptions || {};
|
|
24
|
+
if (!compilerOptions.outDir) {
|
|
25
|
+
error("`compilerOptions.outDir` is not defined in tsconfig.json.", "HMR");
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
25
28
|
const baseUrl = path.resolve(path.dirname(tsConfigPath), compilerOptions.baseUrl || ".");
|
|
29
|
+
const outDir = path.resolve(path.dirname(tsConfigPath), compilerOptions.outDir);
|
|
26
30
|
const paths = compilerOptions.paths || {};
|
|
27
31
|
return {
|
|
28
32
|
paths,
|
|
29
|
-
baseUrl
|
|
33
|
+
baseUrl,
|
|
34
|
+
outDir
|
|
30
35
|
};
|
|
31
|
-
} catch (
|
|
32
|
-
|
|
36
|
+
} catch (e) {
|
|
37
|
+
error(`Error reading or parsing tsconfig.json: ${e.message}`, "HMR");
|
|
33
38
|
return null;
|
|
34
39
|
}
|
|
35
40
|
}
|
|
36
|
-
__name(
|
|
37
|
-
function
|
|
38
|
-
|
|
41
|
+
__name(getTsConfig, "getTsConfig");
|
|
42
|
+
function resolveAliasToSourcePath(importPath, tsConfig) {
|
|
43
|
+
const { paths, baseUrl } = tsConfig;
|
|
44
|
+
for (const alias in paths) {
|
|
39
45
|
const aliasPattern = new RegExp(`^${alias.replace("*", "(.*)")}$`);
|
|
40
46
|
const match = importPath.match(aliasPattern);
|
|
41
47
|
if (match) {
|
|
42
48
|
const [_fullMatch, restOfPath] = match;
|
|
43
|
-
const
|
|
44
|
-
const targetPath = targetPaths[0];
|
|
49
|
+
const targetPath = paths[alias][0];
|
|
45
50
|
if (!alias.endsWith("*")) {
|
|
46
51
|
return path.resolve(baseUrl, targetPath);
|
|
47
52
|
}
|
|
@@ -51,10 +56,13 @@ function resolveAlias(importPath, tsConfigPaths, baseUrl) {
|
|
|
51
56
|
}
|
|
52
57
|
return null;
|
|
53
58
|
}
|
|
54
|
-
__name(
|
|
59
|
+
__name(resolveAliasToSourcePath, "resolveAliasToSourcePath");
|
|
55
60
|
async function generateFunctionFromTypescript(tsCode, filePath) {
|
|
56
|
-
const
|
|
57
|
-
const tsConfig = await
|
|
61
|
+
const projectRoot = process.cwd();
|
|
62
|
+
const tsConfig = await getTsConfig(projectRoot);
|
|
63
|
+
if (!tsConfig) {
|
|
64
|
+
throw new Error("Failed to load or validate tsconfig.json configuration.");
|
|
65
|
+
}
|
|
58
66
|
const result = babel.transformSync(tsCode, {
|
|
59
67
|
presets: [
|
|
60
68
|
"@babel/preset-typescript"
|
|
@@ -65,44 +73,33 @@ async function generateFunctionFromTypescript(tsCode, filePath) {
|
|
|
65
73
|
const ast = parse(jsCode, {
|
|
66
74
|
sourceType: "module"
|
|
67
75
|
});
|
|
68
|
-
const
|
|
76
|
+
const originalFileDir = path.dirname(filePath);
|
|
69
77
|
traverse.default(ast, {
|
|
70
78
|
ImportDeclaration(astPath) {
|
|
71
|
-
const
|
|
72
|
-
const importPath =
|
|
73
|
-
let
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
79
|
+
const importNode = astPath.node.source;
|
|
80
|
+
const importPath = importNode.value;
|
|
81
|
+
let resolvedPathForDist = null;
|
|
82
|
+
const aliasedSourcePath = resolveAliasToSourcePath(importPath, tsConfig);
|
|
83
|
+
if (aliasedSourcePath) {
|
|
84
|
+
resolvedPathForDist = path.relative(tsConfig.baseUrl, aliasedSourcePath);
|
|
85
|
+
} else if (importPath.startsWith(".")) {
|
|
86
|
+
const absoluteSourcePath = path.resolve(originalFileDir, importPath);
|
|
87
|
+
resolvedPathForDist = path.relative(tsConfig.baseUrl, absoluteSourcePath);
|
|
80
88
|
}
|
|
81
|
-
if (
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
resolvedUrl = pathToFileURL(absoluteDepPath).href;
|
|
85
|
-
}
|
|
86
|
-
if (!resolvedUrl) {
|
|
87
|
-
try {
|
|
88
|
-
const resolvedNodeModulePath = require2.resolve(importPath, {
|
|
89
|
-
paths: [
|
|
90
|
-
fileDir
|
|
91
|
-
]
|
|
92
|
-
});
|
|
93
|
-
resolvedUrl = pathToFileURL(resolvedNodeModulePath).href;
|
|
94
|
-
} catch (_) {
|
|
95
|
-
error(`Could not resolve import path for: ${importPath} in ${filePath}.`, "HMR");
|
|
89
|
+
if (resolvedPathForDist) {
|
|
90
|
+
if (resolvedPathForDist.startsWith("src" + path.sep)) {
|
|
91
|
+
resolvedPathForDist = resolvedPathForDist.substring("src".length + 1);
|
|
96
92
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
93
|
+
let jsPath = resolvedPathForDist.replace(/\.(ts|mts|cts)$/, "");
|
|
94
|
+
jsPath += ".js";
|
|
95
|
+
const targetDistPath = path.join(tsConfig.outDir, jsPath);
|
|
96
|
+
importNode.value = pathToFileURL(targetDistPath).href;
|
|
100
97
|
}
|
|
101
98
|
}
|
|
102
99
|
});
|
|
103
100
|
const { code: modifiedCode } = generate.default(ast);
|
|
104
101
|
const tempFileName = `oweb-temp-${randomBytes(16).toString("hex")}.mjs`;
|
|
105
|
-
const tempFilePath = path.join(
|
|
102
|
+
const tempFilePath = path.join(process.cwd(), tempFileName);
|
|
106
103
|
let module;
|
|
107
104
|
try {
|
|
108
105
|
await writeFile(tempFilePath, modifiedCode, "utf-8");
|
|
@@ -112,6 +109,9 @@ async function generateFunctionFromTypescript(tsCode, filePath) {
|
|
|
112
109
|
await unlink(tempFilePath).catch(() => {
|
|
113
110
|
});
|
|
114
111
|
}
|
|
112
|
+
if (!module || typeof module.default === "undefined") {
|
|
113
|
+
throw new Error(`The file ${filePath} was processed, but it did not have a default export.`);
|
|
114
|
+
}
|
|
115
115
|
return module.default;
|
|
116
116
|
}
|
|
117
117
|
__name(generateFunctionFromTypescript, "generateFunctionFromTypescript");
|