owebjs 1.5.9-dev → 1.6.0-dev
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 +73 -54
- package/package.json +1 -1
|
@@ -12,8 +12,8 @@ import { readdirSync } from "node:fs";
|
|
|
12
12
|
import { WebSocketRoute } from '../structures/WebSocketRoute.js';
|
|
13
13
|
import { FastifyWebSocketAdapter } from '../structures/FastifyWebSocketAdapter.js';
|
|
14
14
|
import { formatSSE } from './utils.js';
|
|
15
|
-
const websocketRoutes = {};
|
|
16
15
|
const WS_REGISTRY_KEY = "ws:registered-routes";
|
|
16
|
+
const runtimeStates = /* @__PURE__ */ new WeakMap();
|
|
17
17
|
const createMethodMap = /* @__PURE__ */ __name(() => ({
|
|
18
18
|
get: {},
|
|
19
19
|
post: {},
|
|
@@ -22,11 +22,26 @@ const createMethodMap = /* @__PURE__ */ __name(() => ({
|
|
|
22
22
|
patch: {},
|
|
23
23
|
options: {}
|
|
24
24
|
}), "createMethodMap");
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
function createRuntimeState() {
|
|
26
|
+
return {
|
|
27
|
+
matcherOverrides: {},
|
|
28
|
+
routeFunctions: createMethodMap(),
|
|
29
|
+
temporaryRequests: createMethodMap(),
|
|
30
|
+
routesCache: [],
|
|
31
|
+
compiledRoutes: {},
|
|
32
|
+
websocketRoutes: {}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
__name(createRuntimeState, "createRuntimeState");
|
|
36
|
+
function getRuntimeState(oweb) {
|
|
37
|
+
let state = runtimeStates.get(oweb);
|
|
38
|
+
if (!state) {
|
|
39
|
+
state = createRuntimeState();
|
|
40
|
+
runtimeStates.set(oweb, state);
|
|
41
|
+
}
|
|
42
|
+
return state;
|
|
43
|
+
}
|
|
44
|
+
__name(getRuntimeState, "getRuntimeState");
|
|
30
45
|
function normalizeFsPath(filePath) {
|
|
31
46
|
return path.resolve(filePath).replaceAll("\\", "/").toLowerCase();
|
|
32
47
|
}
|
|
@@ -47,14 +62,13 @@ async function importFreshModule(filePath, source) {
|
|
|
47
62
|
}
|
|
48
63
|
__name(importFreshModule, "importFreshModule");
|
|
49
64
|
function resetRuntimeCaches(oweb) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
65
|
+
const state = getRuntimeState(oweb);
|
|
66
|
+
state.matcherOverrides = {};
|
|
67
|
+
state.routeFunctions = createMethodMap();
|
|
68
|
+
state.temporaryRequests = createMethodMap();
|
|
69
|
+
state.routesCache = [];
|
|
70
|
+
state.compiledRoutes = {};
|
|
71
|
+
state.websocketRoutes = {};
|
|
58
72
|
oweb._internalKV.delete(WS_REGISTRY_KEY);
|
|
59
73
|
}
|
|
60
74
|
__name(resetRuntimeCaches, "resetRuntimeCaches");
|
|
@@ -66,8 +80,8 @@ function removeExtension(filePath) {
|
|
|
66
80
|
return filePath;
|
|
67
81
|
}
|
|
68
82
|
__name(removeExtension, "removeExtension");
|
|
69
|
-
function createWebSocketProxy(url) {
|
|
70
|
-
const getHandler = /* @__PURE__ */ __name(() => websocketRoutes[url], "getHandler");
|
|
83
|
+
function createWebSocketProxy(oweb, url) {
|
|
84
|
+
const getHandler = /* @__PURE__ */ __name(() => getRuntimeState(oweb).websocketRoutes[url], "getHandler");
|
|
71
85
|
return {
|
|
72
86
|
compression: getHandler()?._options.compression,
|
|
73
87
|
maxPayloadLength: getHandler()?._options.maxPayloadLength,
|
|
@@ -101,10 +115,11 @@ function createWebSocketProxy(url) {
|
|
|
101
115
|
}
|
|
102
116
|
__name(createWebSocketProxy, "createWebSocketProxy");
|
|
103
117
|
const applyMatcherHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, fallbackDir, filePath, content) => {
|
|
118
|
+
const state = getRuntimeState(oweb);
|
|
104
119
|
let def;
|
|
105
120
|
const fileName = path.basename(filePath);
|
|
106
121
|
if (op === "delete-file") {
|
|
107
|
-
delete matcherOverrides[removeExtension(fileName)];
|
|
122
|
+
delete state.matcherOverrides[removeExtension(fileName)];
|
|
108
123
|
success(`Matcher ${filePath} removed in 0ms`, "HMR");
|
|
109
124
|
return;
|
|
110
125
|
}
|
|
@@ -122,10 +137,11 @@ const applyMatcherHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, fall
|
|
|
122
137
|
success(`Matcher ${filePath} reloaded in ${end}ms`, "HMR");
|
|
123
138
|
}
|
|
124
139
|
if (def) {
|
|
125
|
-
matcherOverrides[removeExtension(fileName)] = def;
|
|
140
|
+
state.matcherOverrides[removeExtension(fileName)] = def;
|
|
126
141
|
}
|
|
127
142
|
}, "applyMatcherHMR");
|
|
128
143
|
const applyRouteHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, fallbackDir, path2, content) => {
|
|
144
|
+
const state = getRuntimeState(oweb);
|
|
129
145
|
const normalizedChangedPath = normalizeFsPath(path2);
|
|
130
146
|
if (path2.endsWith("hooks.js") || path2.endsWith("hooks.ts")) {
|
|
131
147
|
warn(`Hot Module Replacement is not supported for hooks. Restart the server for changes to take effect.`, "HMR");
|
|
@@ -133,15 +149,15 @@ const applyRouteHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, fallba
|
|
|
133
149
|
}
|
|
134
150
|
if (path2.endsWith(".ts")) {
|
|
135
151
|
const start = Date.now();
|
|
136
|
-
compiledRoutes[path2] = content.length ? await generateFunctionFromTypescript(content, path2) : void 0;
|
|
152
|
+
state.compiledRoutes[path2] = content.length ? await generateFunctionFromTypescript(content, path2) : void 0;
|
|
137
153
|
const end = Date.now() - start;
|
|
138
154
|
success(`File ${path2} compiled in ${end}ms`, "HMR");
|
|
139
155
|
}
|
|
140
156
|
if (op === "new-file") {
|
|
141
157
|
const start = Date.now();
|
|
142
158
|
const files = await walk(workingDir, [], fallbackDir);
|
|
143
|
-
const routes = await generateRoutes(files, path2);
|
|
144
|
-
routesCache = routes;
|
|
159
|
+
const routes = await generateRoutes(files, path2, state.compiledRoutes);
|
|
160
|
+
state.routesCache = routes;
|
|
145
161
|
const f = routes.find((x) => normalizeFsPath(x.fileInfo.filePath) === normalizedChangedPath);
|
|
146
162
|
if (!f) {
|
|
147
163
|
warn(`HMR could not resolve route metadata for file ${path2}`, "HMR");
|
|
@@ -161,18 +177,18 @@ const applyRouteHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, fallba
|
|
|
161
177
|
}
|
|
162
178
|
const method = f.method.toLowerCase();
|
|
163
179
|
const nextHandler = inner(oweb, f);
|
|
164
|
-
if (routeFunctions[method][f.url]) {
|
|
165
|
-
routeFunctions[method][f.url] = nextHandler;
|
|
180
|
+
if (state.routeFunctions[method][f.url]) {
|
|
181
|
+
state.routeFunctions[method][f.url] = nextHandler;
|
|
166
182
|
} else {
|
|
167
|
-
temporaryRequests[method][f.url] = nextHandler;
|
|
183
|
+
state.temporaryRequests[method][f.url] = nextHandler;
|
|
168
184
|
}
|
|
169
185
|
const end = Date.now() - start;
|
|
170
186
|
success(`Route ${f.method.toUpperCase()}:${f.url} created in ${end}ms`, "HMR");
|
|
171
187
|
} else if (op === "modify-file") {
|
|
172
188
|
const start = Date.now();
|
|
173
189
|
const files = await walk(workingDir, [], fallbackDir);
|
|
174
|
-
const routes = await generateRoutes(files, path2);
|
|
175
|
-
routesCache = routes;
|
|
190
|
+
const routes = await generateRoutes(files, path2, state.compiledRoutes);
|
|
191
|
+
state.routesCache = routes;
|
|
176
192
|
const f = routes.find((x) => normalizeFsPath(x.fileInfo.filePath) === normalizedChangedPath);
|
|
177
193
|
if (!f) {
|
|
178
194
|
warn(`HMR could not resolve route metadata for file ${path2}`, "HMR");
|
|
@@ -185,19 +201,19 @@ const applyRouteHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, fallba
|
|
|
185
201
|
}
|
|
186
202
|
}
|
|
187
203
|
if (f.fn?.prototype instanceof WebSocketRoute) {
|
|
188
|
-
websocketRoutes[f.url] = new f.fn();
|
|
204
|
+
state.websocketRoutes[f.url] = new f.fn();
|
|
189
205
|
const end2 = Date.now() - start;
|
|
190
206
|
success(`WebSocket Route ${f.url} reloaded in ${end2}ms`, "HMR");
|
|
191
207
|
return;
|
|
192
208
|
}
|
|
193
209
|
const method = f.method.toLowerCase();
|
|
194
210
|
const nextHandler = inner(oweb, f);
|
|
195
|
-
if (routeFunctions[method][f.url]) {
|
|
196
|
-
routeFunctions[method][f.url] = nextHandler;
|
|
197
|
-
} else if (f.url in temporaryRequests[method]) {
|
|
198
|
-
temporaryRequests[method][f.url] = nextHandler;
|
|
211
|
+
if (state.routeFunctions[method][f.url]) {
|
|
212
|
+
state.routeFunctions[method][f.url] = nextHandler;
|
|
213
|
+
} else if (f.url in state.temporaryRequests[method]) {
|
|
214
|
+
state.temporaryRequests[method][f.url] = nextHandler;
|
|
199
215
|
} else {
|
|
200
|
-
routeFunctions[method][f.url] = nextHandler;
|
|
216
|
+
state.routeFunctions[method][f.url] = nextHandler;
|
|
201
217
|
}
|
|
202
218
|
const end = Date.now() - start;
|
|
203
219
|
success(`Route ${f.method.toUpperCase()}:${f.url} reloaded in ${end}ms`, "HMR");
|
|
@@ -208,25 +224,25 @@ const applyRouteHMR = /* @__PURE__ */ __name(async (oweb, op, workingDir, fallba
|
|
|
208
224
|
if (builded.url.endsWith("/index")) {
|
|
209
225
|
builded.url = builded.url.slice(0, -"/index".length);
|
|
210
226
|
}
|
|
211
|
-
if (websocketRoutes[builded.url]) {
|
|
212
|
-
delete websocketRoutes[builded.url];
|
|
227
|
+
if (state.websocketRoutes[builded.url]) {
|
|
228
|
+
delete state.websocketRoutes[builded.url];
|
|
213
229
|
const end = Date.now() - start;
|
|
214
230
|
success(`WebSocket Route ${builded.url} removed (shimmed) in ${end}ms`, "HMR");
|
|
215
231
|
return;
|
|
216
232
|
}
|
|
217
|
-
const f = routesCache.find((x) => x.method == builded.method && x.url == builded.url);
|
|
233
|
+
const f = state.routesCache.find((x) => x.method == builded.method && x.url == builded.url);
|
|
218
234
|
if (f) {
|
|
219
|
-
if (f.url in temporaryRequests[f.method.toLowerCase()]) {
|
|
220
|
-
delete temporaryRequests[f.method.toLowerCase()][f.url];
|
|
235
|
+
if (f.url in state.temporaryRequests[f.method.toLowerCase()]) {
|
|
236
|
+
delete state.temporaryRequests[f.method.toLowerCase()][f.url];
|
|
221
237
|
} else {
|
|
222
|
-
delete routeFunctions[f.method.toLowerCase()][f.url];
|
|
238
|
+
delete state.routeFunctions[f.method.toLowerCase()][f.url];
|
|
223
239
|
}
|
|
224
240
|
const end = Date.now() - start;
|
|
225
241
|
success(`Route ${f.method.toUpperCase()}:${f.url} removed in ${end}ms`, "HMR");
|
|
226
242
|
}
|
|
227
243
|
}
|
|
228
244
|
}, "applyRouteHMR");
|
|
229
|
-
const generateRoutes = /* @__PURE__ */ __name(async (files, onlyGenerateFn) => {
|
|
245
|
+
const generateRoutes = /* @__PURE__ */ __name(async (files, onlyGenerateFn, compiledRoutes = {}) => {
|
|
230
246
|
const routes = [];
|
|
231
247
|
for (const file of files) {
|
|
232
248
|
const parsedFile = path.parse(file.rel);
|
|
@@ -272,11 +288,12 @@ function inner(oweb, route) {
|
|
|
272
288
|
const handleIsAsync = routeFunc.handle.constructor.name === "AsyncFunction";
|
|
273
289
|
const hasRouteErrorHandler = typeof routeFunc?.handleError === "function";
|
|
274
290
|
const hmrEnabled = !!oweb._internalKV.get("hmr");
|
|
291
|
+
const state = getRuntimeState(oweb);
|
|
275
292
|
const checkMatchers = /* @__PURE__ */ __name((req) => {
|
|
276
293
|
if (!hasMatchers) return true;
|
|
277
294
|
for (const matcher of matchers) {
|
|
278
295
|
const param = req.params[matcher.paramName];
|
|
279
|
-
const fun = matcherOverrides[matcher.matcherName];
|
|
296
|
+
const fun = state.matcherOverrides[matcher.matcherName];
|
|
280
297
|
if (fun) {
|
|
281
298
|
return fun(param);
|
|
282
299
|
}
|
|
@@ -432,7 +449,7 @@ function inner(oweb, route) {
|
|
|
432
449
|
if (hmrEnabled && isParametric) {
|
|
433
450
|
const currentPath = req.raw.url.split("?")[0];
|
|
434
451
|
const method = req.method.toLowerCase();
|
|
435
|
-
const specificHandler = temporaryRequests[method]?.[currentPath];
|
|
452
|
+
const specificHandler = state.temporaryRequests[method]?.[currentPath];
|
|
436
453
|
if (specificHandler) {
|
|
437
454
|
return specificHandler(req, res);
|
|
438
455
|
}
|
|
@@ -467,14 +484,15 @@ function getRegisteredWebSocketsForApp(oweb) {
|
|
|
467
484
|
__name(getRegisteredWebSocketsForApp, "getRegisteredWebSocketsForApp");
|
|
468
485
|
function assignSpecificRoute(oweb, route) {
|
|
469
486
|
if (!route?.fn) return;
|
|
487
|
+
const state = getRuntimeState(oweb);
|
|
470
488
|
if (route?.fn?.prototype instanceof WebSocketRoute) {
|
|
471
489
|
const wsInstance = new route.fn();
|
|
472
|
-
websocketRoutes[route.url] = wsInstance;
|
|
490
|
+
state.websocketRoutes[route.url] = wsInstance;
|
|
473
491
|
const registeredWebSockets = getRegisteredWebSocketsForApp(oweb);
|
|
474
492
|
if (!registeredWebSockets.has(route.url)) {
|
|
475
493
|
registeredWebSockets.add(route.url);
|
|
476
494
|
if (oweb._options.uWebSocketsEnabled && oweb.uServer) {
|
|
477
|
-
const proxy = createWebSocketProxy(route.url);
|
|
495
|
+
const proxy = createWebSocketProxy(oweb, route.url);
|
|
478
496
|
oweb.ws(route.url, proxy, route.fileInfo.hooks);
|
|
479
497
|
} else {
|
|
480
498
|
oweb.get(route.url, {
|
|
@@ -532,7 +550,7 @@ function assignSpecificRoute(oweb, route) {
|
|
|
532
550
|
}
|
|
533
551
|
return;
|
|
534
552
|
}
|
|
535
|
-
const getHandler = /* @__PURE__ */ __name(() => websocketRoutes[route.url], "getHandler");
|
|
553
|
+
const getHandler = /* @__PURE__ */ __name(() => state.websocketRoutes[route.url], "getHandler");
|
|
536
554
|
socket.on("message", (message, isBinary) => {
|
|
537
555
|
const h = getHandler();
|
|
538
556
|
if (h?.message) h.message(adapter, message, isBinary);
|
|
@@ -573,12 +591,12 @@ function assignSpecificRoute(oweb, route) {
|
|
|
573
591
|
if (!routeHandler) return;
|
|
574
592
|
const hmrEnabled = !!oweb._internalKV.get("hmr");
|
|
575
593
|
if (hmrEnabled) {
|
|
576
|
-
routeFunctions[route.method][route.url] = routeHandler;
|
|
594
|
+
state.routeFunctions[route.method][route.url] = routeHandler;
|
|
577
595
|
oweb[route.method](route.url, routeFunc._options || {}, function(req, res) {
|
|
578
|
-
if (routeFunctions[route.method][route.url]) {
|
|
579
|
-
return routeFunctions[route.method][route.url](req, res);
|
|
596
|
+
if (state.routeFunctions[route.method][route.url]) {
|
|
597
|
+
return state.routeFunctions[route.method][route.url](req, res);
|
|
580
598
|
} else {
|
|
581
|
-
const vals = temporaryRequests[route.method];
|
|
599
|
+
const vals = state.temporaryRequests[route.method];
|
|
582
600
|
const keys = Object.keys(vals);
|
|
583
601
|
if (!vals || !keys.length) {
|
|
584
602
|
return send404(req, res);
|
|
@@ -599,7 +617,7 @@ function assignSpecificRoute(oweb, route) {
|
|
|
599
617
|
oweb[route.method](route.url, routeFunc._options || {}, routeHandler);
|
|
600
618
|
}
|
|
601
619
|
__name(assignSpecificRoute, "assignSpecificRoute");
|
|
602
|
-
async function loadMatchers(directoryPath) {
|
|
620
|
+
async function loadMatchers(directoryPath, state) {
|
|
603
621
|
const files = readdirSync(directoryPath).filter((f) => [
|
|
604
622
|
".js",
|
|
605
623
|
".ts"
|
|
@@ -609,20 +627,21 @@ async function loadMatchers(directoryPath) {
|
|
|
609
627
|
const fileName = path.basename(filePath);
|
|
610
628
|
const packageURL = pathToFileURL(path.resolve(filePath)).href;
|
|
611
629
|
const def = await import(packageURL);
|
|
612
|
-
matcherOverrides[removeExtension(fileName)] = def.default;
|
|
630
|
+
state.matcherOverrides[removeExtension(fileName)] = def.default;
|
|
613
631
|
}
|
|
614
632
|
}
|
|
615
633
|
__name(loadMatchers, "loadMatchers");
|
|
616
634
|
const assignRoutes = /* @__PURE__ */ __name(async (oweb, directory, matchersDirectory) => {
|
|
617
635
|
resetRuntimeCaches(oweb);
|
|
636
|
+
const state = getRuntimeState(oweb);
|
|
618
637
|
if (matchersDirectory) {
|
|
619
|
-
await loadMatchers(matchersDirectory);
|
|
638
|
+
await loadMatchers(matchersDirectory, state);
|
|
620
639
|
}
|
|
621
640
|
const files = await walk(directory);
|
|
622
|
-
const routes = await generateRoutes(files);
|
|
623
|
-
routesCache = routes;
|
|
641
|
+
const routes = await generateRoutes(files, void 0, state.compiledRoutes);
|
|
642
|
+
state.routesCache = routes;
|
|
624
643
|
function fallbackHandle(req, res) {
|
|
625
|
-
const vals = temporaryRequests[req.method.toLowerCase()];
|
|
644
|
+
const vals = state.temporaryRequests[req.method.toLowerCase()];
|
|
626
645
|
const keys = Object.keys(vals);
|
|
627
646
|
if (!vals || !keys.length) {
|
|
628
647
|
return send404(req, res);
|