Skip to content

Commit 4147df0

Browse files
committed
fix: enhance fs path validation
1 parent a83714c commit 4147df0

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/devtools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { CLIENT_PATH, CLIENT_PORT, RPC_NAMESPACE } from './constants'
1212

1313
export function setupDevToolsUI(options: ModuleOptions, resolve: Resolver['resolve'], nuxt: Nuxt) {
1414
const clientPath = resolve('./client')
15-
const isProductionBuild = existsSync(clientPath)
15+
const isProductionBuild = typeof clientPath === 'string' && clientPath.length > 0 && existsSync(clientPath)
1616

1717
// serve production-built client (used when package is published)
1818
if (isProductionBuild) {

src/rpc/resource.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import mongoose from 'mongoose'
44
import type { CollectionDefinition, DevtoolsServerContext, Resource, ServerFunctions } from '../types'
55
import { capitalize, generateApiRoute, generateSchemaFile, pluralize, singularize } from '../utils'
66

7+
function safeExists(path: unknown): path is string {
8+
return typeof path === 'string' && path.length > 0 && fs.existsSync(path)
9+
}
10+
711
export function setupResourceRPC({ nuxt }: DevtoolsServerContext): any {
8-
const config = nuxt.options.runtimeConfig.mongoose
12+
const config = nuxt.options.runtimeConfig.mongoose as any
913

1014
return {
1115
async generateResource(collection: CollectionDefinition, resources: Resource[]) {
@@ -15,7 +19,7 @@ export function setupResourceRPC({ nuxt }: DevtoolsServerContext): any {
1519

1620
if (collection.fields) {
1721
const schemaPath = join(config.modelsDir, `${singular}.schema.ts`)
18-
if (!fs.existsSync(schemaPath)) {
22+
if (!safeExists(schemaPath)) {
1923
fs.ensureDirSync(config.modelsDir)
2024
fs.writeFileSync(schemaPath, generateSchemaFile(dbName, collection.fields))
2125
}
@@ -36,7 +40,7 @@ export function setupResourceRPC({ nuxt }: DevtoolsServerContext): any {
3640
: routeTypes[route.type]
3741

3842
const filePath = join(nuxt.options.serverDir, 'api', plural, fileName)
39-
if (!fs.existsSync(filePath)) {
43+
if (!safeExists(filePath)) {
4044
fs.ensureDirSync(join(nuxt.options.serverDir, `api/${plural}`))
4145
const content = generateApiRoute(route.type, { model, by: route.by })
4246
fs.writeFileSync(filePath, content)
@@ -52,7 +56,7 @@ export function setupResourceRPC({ nuxt }: DevtoolsServerContext): any {
5256
async resourceSchema(collection: string) {
5357
const singular = singularize(collection).toLowerCase()
5458
const schemaPath = join(config.modelsDir, `${singular}.schema.ts`)
55-
if (fs.existsSync(schemaPath)) {
59+
if (safeExists(schemaPath)) {
5660
const content = fs.readFileSync(schemaPath, 'utf-8').match(/schema: \{(.|\n)*\}/g)
5761
if (content) {
5862
const schemaString = content[0].replace('schema: ', '').slice(0, -3)

0 commit comments

Comments
 (0)