Skip to content

Commit 33f3ae6

Browse files
committed
fix: path is not resolved correctly if npm run is used
1 parent 9fb4d9f commit 33f3ae6

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

builder/source/utils/http-server.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as HTTP from 'node:http'
22
import * as Fs from 'node:fs'
3+
import * as Process from 'node:process'
34
import * as Path from 'node:path'
45

56
function IsLoopBack(IP: string) {
@@ -8,10 +9,13 @@ function IsLoopBack(IP: string) {
89

910
export function RunDebugServer(Port: number, FileName: string[], ShouldPreventHTTPResponse: boolean) {
1011
const HTTPServer = HTTP.createServer((Req, Res) => {
11-
const DistRoot = Path.resolve(process.cwd(), 'dist')
12+
let ProjectRoot = Process.cwd()
13+
if (Process.cwd().endsWith('/builder')) {
14+
ProjectRoot = Process.cwd() + '/..'
15+
}
1216
const RequestPath = Req.url?.substring(1) || ''
13-
const ResolvedPath = Path.resolve(DistRoot, RequestPath)
14-
const RelativePath = Path.relative(DistRoot, ResolvedPath)
17+
const ResolvedPath = Path.resolve(ProjectRoot + '/dist', RequestPath)
18+
const RelativePath = Path.relative(ProjectRoot + '/dist', ResolvedPath)
1519

1620
// Ensure the resolved path stays within the dist root to prevent directory traversal
1721
if (RelativePath.startsWith('..') || Path.isAbsolute(RelativePath)) {

0 commit comments

Comments
 (0)