Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/many-ads-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@tanstack/start-plugin-core': patch
'@tanstack/router-generator': patch
'@tanstack/router-utils': patch
---

Use node crypto.hash instead of crypto.createHash for single string hashes
5 changes: 1 addition & 4 deletions docs/start/framework/react/guide/isr.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,7 @@ const etagMiddleware = createMiddleware().server(async ({ next }) => {
const result = await next()

// Generate ETag from response content
const etag = crypto
.createHash('md5')
.update(JSON.stringify(result.data))
.digest('hex')
const etag = crypto.hash('md5', JSON.stringify(result.data), 'hex')

result.response.headers.set('ETag', `"${etag}"`)

Expand Down
5 changes: 1 addition & 4 deletions docs/start/framework/react/guide/server-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,7 @@ export default defineConfig({
serverFns: {
generateFunctionId: ({ filename, functionName }) => {
// Return a custom ID string
return crypto
.createHash('sha1')
.update(`${filename}--${functionName}`)
.digest('hex')
return crypto.hash('sha1', `${filename}--${functionName}`, 'hex')
Comment thread
Sheraff marked this conversation as resolved.

// If you return undefined, the default is used
// return undefined
Expand Down
3 changes: 2 additions & 1 deletion packages/router-generator/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fsp from 'node:fs/promises'
import { existsSync, mkdirSync } from 'node:fs'
import crypto from 'node:crypto'
import { rootRouteId } from '@tanstack/router-core'
import { hash as hashString } from '@tanstack/router-utils'
import { logging } from './logger'
import {
isVirtualConfigFile,
Expand Down Expand Up @@ -1243,7 +1244,7 @@ ${acc.routeTree.map((child) => `${child.variableName}Route: typeof ${getResolved

private getTempFileName(filePath: string) {
const absPath = path.resolve(filePath)
const hash = crypto.createHash('md5').update(absPath).digest('hex')
const hash = hashString('md5', absPath, 'hex')
// lazy initialize sessionId to only create tmpDir when it is first needed
if (!this.sessionId) {
// ensure the directory exists
Expand Down
16 changes: 16 additions & 0 deletions packages/router-utils/src/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import crypto from 'node:crypto'
import type { BinaryLike, BinaryToTextEncoding } from 'node:crypto'

const nativeHashAvailable = typeof crypto.hash === 'function'

export function hash(
algorithm: string,
data: BinaryLike,
outputEncoding: BinaryToTextEncoding = 'hex',
): string {
if (nativeHashAvailable) {
return crypto.hash(algorithm, data, outputEncoding)
}

return crypto.createHash(algorithm).update(data).digest(outputEncoding)
}
1 change: 1 addition & 0 deletions packages/router-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export {
} from './ast'
export type { ParseAstOptions, ParseAstResult, GeneratorResult } from './ast'
export { logDiff } from './logger'
export { hash } from './hash'

export { copyFilesPlugin } from './copy-files-plugin'
5 changes: 2 additions & 3 deletions packages/start-plugin-core/src/start-compiler/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable import/no-commonjs */
import crypto from 'node:crypto'
import * as t from '@babel/types'
import {
deadCodeElimination,
findReferencedIdentifiers,
generateFromAst,
hash,
parseAst,
} from '@tanstack/router-utils'
import babel from '@babel/core'
Expand Down Expand Up @@ -600,7 +599,7 @@ export class StartCompiler {
})
}
if (!functionId) {
functionId = crypto.createHash('sha256').update(entryId).digest('hex')
functionId = hash('sha256', entryId, 'hex')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're on a roll with modernizing things, what do you think about functionId ??= hash('sha256', entryId, 'hex')? I have a personal vendetta against the ! operator it looks like a tiny person shouting at my code 😂

}
// Deduplicate in case the generated id conflicts with an existing id
if (this.functionIds.has(functionId)) {
Expand Down
Loading