Skip to content

Commit e31cf4d

Browse files
waleedlatif1claude
andcommitted
refactor(validation): remove Node path import, use plain string extraction
Replace `import path from 'path'` with a simple `extractExtension` helper that does `fileName.slice(fileName.lastIndexOf('.') + 1)`. This removes the only Node module dependency from validation.ts, making it safe to import from client components without pulling in a Node polyfill. Deletes the unnecessary validation-constants.ts that was introduced as a workaround — the constants now live back in validation.ts where they belong. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a7b732b commit e31cf4d

File tree

4 files changed

+57
-61
lines changed

4 files changed

+57
-61
lines changed

apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/file-viewer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Skeleton } from '@/components/emcn'
77
import { cn } from '@/lib/core/utils/cn'
88
import type { WorkspaceFileRecord } from '@/lib/uploads/contexts/workspace'
99
import { getFileExtension } from '@/lib/uploads/utils/file-utils'
10-
import { SUPPORTED_CODE_EXTENSIONS } from '@/lib/uploads/utils/validation-constants'
10+
import { SUPPORTED_CODE_EXTENSIONS } from '@/lib/uploads/utils/validation'
1111
import {
1212
useUpdateWorkspaceFileContent,
1313
useWorkspaceFileBinary,

apps/sim/app/workspace/[workspaceId]/files/files.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ import {
3636
isAudioFileType,
3737
isVideoFileType,
3838
} from '@/lib/uploads/utils/file-utils'
39-
import { SUPPORTED_CODE_EXTENSIONS } from '@/lib/uploads/utils/validation-constants'
4039
import {
4140
isSupportedExtension,
4241
SUPPORTED_AUDIO_EXTENSIONS,
42+
SUPPORTED_CODE_EXTENSIONS,
4343
SUPPORTED_DOCUMENT_EXTENSIONS,
4444
SUPPORTED_VIDEO_EXTENSIONS,
4545
} from '@/lib/uploads/utils/validation'

apps/sim/lib/uploads/utils/validation-constants.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

apps/sim/lib/uploads/utils/validation.ts

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
import path from 'path'
2-
3-
export { SUPPORTED_CODE_EXTENSIONS } from '@/lib/uploads/utils/validation-constants'
4-
export type { SupportedCodeExtension } from '@/lib/uploads/utils/validation-constants'
5-
61
/**
72
* Checks whether a string is a valid file extension (lowercase alphanumeric only).
83
* Rejects extensions containing spaces, punctuation, or other non-alphanumeric characters
@@ -12,6 +7,11 @@ export function isAlphanumericExtension(ext: string): boolean {
127
return /^[a-z0-9]+$/.test(ext)
138
}
149

10+
function extractExtension(fileName: string): string {
11+
const lastDot = fileName.lastIndexOf('.')
12+
return lastDot !== -1 ? fileName.slice(lastDot + 1).toLowerCase() : ''
13+
}
14+
1515
export const MAX_FILE_SIZE = 100 * 1024 * 1024 // 100MB
1616

1717
export const SUPPORTED_DOCUMENT_EXTENSIONS = [
@@ -32,6 +32,54 @@ export const SUPPORTED_DOCUMENT_EXTENSIONS = [
3232
'yml',
3333
] as const
3434

35+
export const SUPPORTED_CODE_EXTENSIONS = [
36+
'mdx',
37+
'xml',
38+
'css',
39+
'scss',
40+
'less',
41+
'js',
42+
'jsx',
43+
'ts',
44+
'tsx',
45+
'py',
46+
'rb',
47+
'go',
48+
'rs',
49+
'java',
50+
'kt',
51+
'swift',
52+
'c',
53+
'cpp',
54+
'h',
55+
'hpp',
56+
'cs',
57+
'php',
58+
'sh',
59+
'bash',
60+
'zsh',
61+
'fish',
62+
'sql',
63+
'graphql',
64+
'gql',
65+
'toml',
66+
'ini',
67+
'conf',
68+
'cfg',
69+
'env',
70+
'log',
71+
'diff',
72+
'patch',
73+
'dockerfile',
74+
'makefile',
75+
'gitignore',
76+
'editorconfig',
77+
'prettierrc',
78+
'eslintrc',
79+
] as const
80+
81+
export type SupportedCodeExtension = (typeof SUPPORTED_CODE_EXTENSIONS)[number]
82+
3583
export const SUPPORTED_AUDIO_EXTENSIONS = [
3684
'mp3',
3785
'm4a',
@@ -149,7 +197,7 @@ export interface FileValidationError {
149197
* Validate if a file type is supported for document processing
150198
*/
151199
export function validateFileType(fileName: string, mimeType: string): FileValidationError | null {
152-
const raw = path.extname(fileName).toLowerCase().substring(1)
200+
const raw = extractExtension(fileName)
153201
const extension = (isAlphanumericExtension(raw) ? raw : '') as SupportedDocumentExtension
154202

155203
if (!SUPPORTED_DOCUMENT_EXTENSIONS.includes(extension)) {
@@ -234,7 +282,7 @@ export function validateMediaFileType(
234282
fileName: string,
235283
mimeType: string
236284
): FileValidationError | null {
237-
const raw = path.extname(fileName).toLowerCase().substring(1)
285+
const raw = extractExtension(fileName)
238286
const extension = isAlphanumericExtension(raw) ? raw : ''
239287

240288
const isAudio = SUPPORTED_AUDIO_EXTENSIONS.includes(extension as SupportedAudioExtension)

0 commit comments

Comments
 (0)