@carno.js/core 0.2.9 → 0.2.10

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.
@@ -1,6 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Memoirist = void 0;
4
+ /**
5
+ * * Empty object shared for static routes without parameters.
6
+ * * Frozen for V8 optimization and immutability.
7
+ * * Avoid allocating an empty object on every request in static routes.
8
+ * */
9
+ const EMPTY_PARAMS = Object.freeze({});
4
10
  const createNode = (part, inert) => ({
5
11
  part,
6
12
  store: null,
@@ -54,6 +60,7 @@ class Memoirist {
54
60
  constructor() {
55
61
  this.root = {};
56
62
  this.history = [];
63
+ this.routeCache = new Map();
57
64
  }
58
65
  add(method, path, store) {
59
66
  if (typeof path !== 'string')
@@ -62,6 +69,7 @@ class Memoirist {
62
69
  path = '/';
63
70
  else if (path[0] !== '/')
64
71
  path = `/${path}`;
72
+ this.invalidateCache();
65
73
  this.history.push([method, path, store]);
66
74
  const isWildcard = path[path.length - 1] === '*';
67
75
  if (isWildcard) {
@@ -162,10 +170,16 @@ class Memoirist {
162
170
  return node.store;
163
171
  }
164
172
  find(method, url) {
173
+ const cacheKey = this.buildCacheKey(method, url);
174
+ if (this.routeCache.has(cacheKey)) {
175
+ return this.routeCache.get(cacheKey);
176
+ }
165
177
  const root = this.root[method];
166
178
  if (!root)
167
179
  return null;
168
- return matchRoute(url, url.length, root, 0);
180
+ const result = matchRoute(url, url.length, root, 0);
181
+ this.routeCache.set(cacheKey, result);
182
+ return result;
169
183
  }
170
184
  updateStore(method, path, oldStore, newStore) {
171
185
  const node = this.findNode(method, path);
@@ -175,6 +189,7 @@ class Memoirist {
175
189
  if (node.store === oldStore) {
176
190
  node.store = newStore;
177
191
  this.updateHistoryStore(method, path, newStore);
192
+ this.invalidateCache();
178
193
  return true;
179
194
  }
180
195
  if (node.params?.store === oldStore) {
@@ -184,15 +199,24 @@ class Memoirist {
184
199
  node.params.names.set(newStore, paramName);
185
200
  }
186
201
  this.updateHistoryStore(method, path, newStore);
202
+ this.invalidateCache();
187
203
  return true;
188
204
  }
189
205
  if (node.wildcardStore === oldStore) {
190
206
  node.wildcardStore = newStore;
191
207
  this.updateHistoryStore(method, path, newStore);
208
+ this.invalidateCache();
192
209
  return true;
193
210
  }
194
211
  return false;
195
212
  }
213
+ buildCacheKey(method, url) {
214
+ const normalizedMethod = method.toLowerCase();
215
+ return `${normalizedMethod}:${url}`;
216
+ }
217
+ invalidateCache() {
218
+ this.routeCache.clear();
219
+ }
196
220
  updateHistoryStore(method, path, newStore) {
197
221
  const normalizedPath = this.normalizePath(path);
198
222
  for (let i = 0; i < this.history.length; i++) {
@@ -288,7 +312,7 @@ const matchRoute = (url, urlLength, node, startIndex) => {
288
312
  if (node.store !== null)
289
313
  return {
290
314
  store: node.store,
291
- params: {}
315
+ params: EMPTY_PARAMS
292
316
  };
293
317
  if (node.wildcardStore !== null)
294
318
  return {
@@ -325,7 +349,13 @@ const matchRoute = (url, urlLength, node, startIndex) => {
325
349
  const route = matchRoute(url, urlLength, param.inert, slashIndex);
326
350
  if (route !== null) {
327
351
  const paramName = resolveParamName(param, route.store);
328
- route.params[paramName] = url.substring(endIndex, slashIndex);
352
+ const paramValue = url.substring(endIndex, slashIndex);
353
+ if (route.params === EMPTY_PARAMS) {
354
+ route.params = { [paramName]: paramValue };
355
+ }
356
+ else {
357
+ route.params[paramName] = paramValue;
358
+ }
329
359
  return route;
330
360
  }
331
361
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carno.js/core",
3
- "version": "0.2.9",
3
+ "version": "0.2.10",
4
4
  "description": "Carno.js is a framework for building web applications object oriented with TypeScript and Bun.sh",
5
5
  "keywords": [
6
6
  "bun",
@@ -62,5 +62,5 @@
62
62
  "publishConfig": {
63
63
  "access": "public"
64
64
  },
65
- "gitHead": "cc116bcd2f96e3cf6e87bbb38d5e4dfe1245150a"
65
+ "gitHead": "5c765c2c9172bec7f509ca2513f6c10472ec89eb"
66
66
  }