owebjs 1.3.5 → 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.
- package/dist/utils/assignRoutes.js +23 -13
- package/dist/utils/generateFunctionFromTypescript.js +76 -28
- package/dist/uws_darwin_arm64_108.node +0 -0
- package/dist/uws_darwin_arm64_83.node +0 -0
- package/dist/uws_darwin_arm64_93.node +0 -0
- package/dist/uws_darwin_x64_108.node +0 -0
- package/dist/uws_darwin_x64_83.node +0 -0
- package/dist/uws_darwin_x64_93.node +0 -0
- package/dist/uws_linux_arm64_108.node +0 -0
- package/dist/uws_linux_arm64_83.node +0 -0
- package/dist/uws_linux_arm64_93.node +0 -0
- package/dist/uws_linux_x64_108.node +0 -0
- package/dist/uws_linux_x64_83.node +0 -0
- package/dist/uws_linux_x64_93.node +0 -0
- package/dist/uws_win32_x64_108.node +0 -0
- package/dist/uws_win32_x64_83.node +0 -0
- package/dist/uws_win32_x64_93.node +0 -0
- package/package.json +2 -2
|
@@ -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,16 +4,65 @@ 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
|
-
import { writeFile, unlink } from "node:fs/promises";
|
|
11
|
-
import {
|
|
10
|
+
import { writeFile, unlink, readFile } from "node:fs/promises";
|
|
11
|
+
import { existsSync } from "node:fs";
|
|
12
12
|
import { randomBytes } from "node:crypto";
|
|
13
|
-
import { createRequire } from "node:module";
|
|
14
13
|
import { error } from './logger.js';
|
|
15
|
-
|
|
14
|
+
async function getTsConfig(projectRoot) {
|
|
15
|
+
const tsConfigPath = path.join(projectRoot, "tsconfig.json");
|
|
16
|
+
if (!existsSync(tsConfigPath)) {
|
|
17
|
+
error("tsconfig.json not found in the project root.", "HMR");
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
const tsConfigFile = await readFile(tsConfigPath, "utf-8");
|
|
22
|
+
const json = JSON.parse(tsConfigFile.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, ""));
|
|
23
|
+
const compilerOptions = json.compilerOptions || {};
|
|
24
|
+
if (!compilerOptions.outDir) {
|
|
25
|
+
error("`compilerOptions.outDir` is not defined in tsconfig.json.", "HMR");
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
const baseUrl = path.resolve(path.dirname(tsConfigPath), compilerOptions.baseUrl || ".");
|
|
29
|
+
const outDir = path.resolve(path.dirname(tsConfigPath), compilerOptions.outDir);
|
|
30
|
+
const paths = compilerOptions.paths || {};
|
|
31
|
+
return {
|
|
32
|
+
paths,
|
|
33
|
+
baseUrl,
|
|
34
|
+
outDir
|
|
35
|
+
};
|
|
36
|
+
} catch (e) {
|
|
37
|
+
error(`Error reading or parsing tsconfig.json: ${e.message}`, "HMR");
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
__name(getTsConfig, "getTsConfig");
|
|
42
|
+
function resolveAliasToSourcePath(importPath, tsConfig) {
|
|
43
|
+
const { paths, baseUrl } = tsConfig;
|
|
44
|
+
for (const alias in paths) {
|
|
45
|
+
const aliasPattern = new RegExp(`^${alias.replace("*", "(.*)")}$`);
|
|
46
|
+
const match = importPath.match(aliasPattern);
|
|
47
|
+
if (match) {
|
|
48
|
+
const [_fullMatch, restOfPath] = match;
|
|
49
|
+
const targetPath = paths[alias][0];
|
|
50
|
+
if (!alias.endsWith("*")) {
|
|
51
|
+
return path.resolve(baseUrl, targetPath);
|
|
52
|
+
}
|
|
53
|
+
const resolvedPath = targetPath.replace("*", restOfPath);
|
|
54
|
+
return path.resolve(baseUrl, resolvedPath);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
__name(resolveAliasToSourcePath, "resolveAliasToSourcePath");
|
|
16
60
|
async function generateFunctionFromTypescript(tsCode, filePath) {
|
|
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
|
+
}
|
|
17
66
|
const result = babel.transformSync(tsCode, {
|
|
18
67
|
presets: [
|
|
19
68
|
"@babel/preset-typescript"
|
|
@@ -24,37 +73,33 @@ async function generateFunctionFromTypescript(tsCode, filePath) {
|
|
|
24
73
|
const ast = parse(jsCode, {
|
|
25
74
|
sourceType: "module"
|
|
26
75
|
});
|
|
76
|
+
const originalFileDir = path.dirname(filePath);
|
|
27
77
|
traverse.default(ast, {
|
|
28
78
|
ImportDeclaration(astPath) {
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const resolvedPath = require2.resolve(relativePath, {
|
|
43
|
-
paths: [
|
|
44
|
-
path.dirname(filePath)
|
|
45
|
-
]
|
|
46
|
-
});
|
|
47
|
-
const resolvedUrl = pathToFileURL(resolvedPath).href;
|
|
48
|
-
importSourceNode.value = resolvedUrl;
|
|
49
|
-
} catch (_) {
|
|
50
|
-
error(`Could not resolve module "${relativePath}". Please ensure it is installed.`, "HMR");
|
|
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);
|
|
88
|
+
}
|
|
89
|
+
if (resolvedPathForDist) {
|
|
90
|
+
if (resolvedPathForDist.startsWith("src" + path.sep)) {
|
|
91
|
+
resolvedPathForDist = resolvedPathForDist.substring("src".length + 1);
|
|
51
92
|
}
|
|
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;
|
|
52
97
|
}
|
|
53
98
|
}
|
|
54
99
|
});
|
|
55
100
|
const { code: modifiedCode } = generate.default(ast);
|
|
56
101
|
const tempFileName = `oweb-temp-${randomBytes(16).toString("hex")}.mjs`;
|
|
57
|
-
const tempFilePath = path.join(
|
|
102
|
+
const tempFilePath = path.join(process.cwd(), tempFileName);
|
|
58
103
|
let module;
|
|
59
104
|
try {
|
|
60
105
|
await writeFile(tempFilePath, modifiedCode, "utf-8");
|
|
@@ -64,6 +109,9 @@ async function generateFunctionFromTypescript(tsCode, filePath) {
|
|
|
64
109
|
await unlink(tempFilePath).catch(() => {
|
|
65
110
|
});
|
|
66
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
|
+
}
|
|
67
115
|
return module.default;
|
|
68
116
|
}
|
|
69
117
|
__name(generateFunctionFromTypescript, "generateFunctionFromTypescript");
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "owebjs",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.8",
|
|
4
4
|
"description": "A flexible and modern web framework built on top of Fastify",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
20
|
"start": "node .",
|
|
21
|
-
"build": "tsup",
|
|
21
|
+
"build": "tsup && node scripts/copyBinaries",
|
|
22
22
|
"dev": "tsup && node .",
|
|
23
23
|
"test": "tsup && node scripts/copyBinaries && node test/index.js",
|
|
24
24
|
"format": "prettier --write . --ignore-path .gitignore"
|