Skip to content

Commit 923f3c2

Browse files
committed
refactor: use node hash
1 parent 78437e1 commit 923f3c2

7 files changed

Lines changed: 34 additions & 26 deletions

File tree

‎package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
"hookable": "^6.1.1",
7878
"nf3": "^0.3.24",
7979
"ocache": "^0.3.0",
80-
"ohash": "^2.0.12",
8180
"rolldown": "^1.2.6",
8281
"rou3": "^0.9.2",
8382
"srvx": "^1.0.0",

‎pnpm-lock.yaml‎

Lines changed: 4 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/build/virtual/plugins.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Nitro } from "nitro/types";
2-
import { hash } from "ohash";
2+
import { hash } from "../../utils/hash.ts";
33

44
export default function plugins(nitro: Nitro) {
55
return {
@@ -8,12 +8,10 @@ export default function plugins(nitro: Nitro) {
88
const nitroPlugins = [...new Set(nitro.options.plugins)];
99

1010
return /* js */ `
11-
${nitroPlugins
12-
.map((plugin) => /* js */ `import _${hash(plugin).replace(/-/g, "")} from "${plugin}";`)
13-
.join("\n")}
11+
${nitroPlugins.map((plugin) => /* js */ `import _${hash(plugin)} from "${plugin}";`).join("\n")}
1412
1513
export const plugins = [
16-
${nitroPlugins.map((plugin) => `_${hash(plugin).replace(/-/g, "")}`).join(",\n")}
14+
${nitroPlugins.map((plugin) => `_${hash(plugin)}`).join(",\n")}
1715
]
1816
`;
1917
},

‎src/routing.ts‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { join } from "pathe";
66
import { runtimeDir } from "nitro/meta";
77
import { addRoute, createRouter, findRoute, findAllRoutes } from "rou3";
88
import { compileRouterToString } from "rou3/compiler";
9-
import { hash } from "ohash";
9+
import { hash } from "./utils/hash.ts";
1010

1111
const isGlobalMiddleware = (h: NitroEventHandler) => !h.method && (!h.route || h.route === "/**");
1212

@@ -122,7 +122,7 @@ export function initNitroRouting(nitro: Nitro) {
122122
}
123123

124124
function handlerWithImportHash(h: NitroEventHandler) {
125-
const id = (h.lazy ? "_lazy_" : "_") + hash(h.handler).replace(/-/g, "").slice(0, 6);
125+
const id = (h.lazy ? "_lazy_" : "_") + hash(h.handler);
126126
return { ...h, _importHash: id };
127127
}
128128

@@ -137,7 +137,15 @@ export interface Route<T = unknown> {
137137
export class Router<T> {
138138
_routes?: Route<T>[];
139139
_router?: RouterContext<T>;
140-
_compiled?: Record<string, string>;
140+
/**
141+
* Cached output of {@link compileToString}, invalidated by {@link _update}.
142+
*
143+
* Only one result is cached: each Router instance must always be compiled with
144+
* the same `opts`. Compiling one instance with differing `opts` would silently
145+
* return the first result, since `opts` holds a `serialize` closure and is
146+
* therefore not hashable into a cache key.
147+
*/
148+
_compiled?: string;
141149
_baseURL: string;
142150

143151
constructor(baseURL?: string) {
@@ -169,12 +177,10 @@ export class Router<T> {
169177
}
170178

171179
compileToString(opts?: RouterCompilerOptions<T>) {
172-
const key = opts ? hash(opts) : "";
173-
this._compiled ||= {};
174-
if (this._compiled[key]) {
175-
return this._compiled[key];
180+
if (this._compiled) {
181+
return this._compiled;
176182
}
177-
this._compiled[key] = compileRouterToString(this._router!, undefined, opts);
183+
this._compiled = compileRouterToString(this._router!, undefined, opts);
178184

179185
// TODO: Upstream to rou3 compiler
180186
const onlyWildcard =
@@ -190,11 +196,10 @@ export class Router<T> {
190196
const guardCode = base
191197
? `if(p!==${JSON.stringify(base)}&&!p.startsWith(${JSON.stringify(base + "/")})){return ${opts?.matchAll ? "[]" : "undefined"};}`
192198
: "";
193-
this._compiled[key] =
194-
/* js */ `/* @__PURE__ */ (() => {const data=${data};return ((_m, p)=>{${guardCode}return ${retCode};})})()`;
199+
this._compiled = /* js */ `/* @__PURE__ */ (() => {const data=${data};return ((_m, p)=>{${guardCode}return ${retCode};})})()`;
195200
}
196201

197-
return this._compiled[key];
202+
return this._compiled;
198203
}
199204

200205
match(method: string, path: string): undefined | T {

‎src/runtime/internal/route-rule-handlers.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { createCacheRuleHandler, type RuleHandler } from "h3/rules";
88
import { defineCachedHandler } from "./cache.ts";
99

1010
// `/* @__PURE__ */` so an app that uses only non-cache rules (e.g. `headers`)
11-
// tree-shakes this out along with `ocache`/`ohash`.
11+
// tree-shakes this out along with `ocache`.
1212
export const cache: RuleHandler<"cache"> = /* @__PURE__ */ createCacheRuleHandler({
1313
defineCachedHandler,
1414
// Preserve Nitro's cache storage key convention (`nitro/route-rules`) so

‎src/runtime/meta.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export const runtimeDependencies: string[] = [
2121
"rou3", // sub-dep of h3
2222
"hookable", // traced
2323
"ocache", // dep
24-
"ohash", // traced
2524
"rendu", // traced
2625
"scule", // traced
2726
"srvx", // dep

‎src/utils/hash.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { createHash } from "node:crypto";
2+
3+
/**
4+
* Stable hash used for generated identifiers and cache keys.
5+
*
6+
* Returns a hex digest (safe to embed in JS identifiers) truncated to `length`.
7+
*/
8+
export function hash(input: string, length = 16): string {
9+
return createHash("sha256").update(input).digest("hex").slice(0, length);
10+
}

0 commit comments

Comments
 (0)