@@ -147,12 +147,16 @@ export default function projectManagerShimMiddleware(
147
147
response : http . ServerResponse ,
148
148
next : ( ) => void ,
149
149
) {
150
- const requestUrl = request . url
151
- const requestPath = requestUrl ?. split ( '?' ) [ 0 ] ?. split ( '#' ) [ 0 ]
152
- if ( requestUrl != null && requestUrl . startsWith ( '/api/project-manager/' ) ) {
153
- const actualUrl = new URL (
154
- requestUrl . replace ( / ^ \/ a p i \/ p r o j e c t - m a n a g e r / , GLOBAL_CONFIG . projectManagerHttpEndpoint ) ,
150
+ const requestUrl = request . url ?? ''
151
+ if ( ! requestUrl . startsWith ( '/api/' ) ) return next ( )
152
+ const url = new URL ( requestUrl , 'https://apishim.local' )
153
+ const requestPath = url . pathname
154
+ if ( requestPath . startsWith ( '/api/project-manager/' ) ) {
155
+ const urlString = requestUrl . replace (
156
+ / ^ \/ a p i \/ p r o j e c t - m a n a g e r / ,
157
+ GLOBAL_CONFIG . projectManagerHttpEndpoint ,
155
158
)
159
+ const actualUrl = new URL ( urlString )
156
160
request . pipe (
157
161
http . request (
158
162
// `...actualUrl` does NOT work because `URL` properties are not enumerable.
@@ -181,7 +185,6 @@ export default function projectManagerShimMiddleware(
181
185
} else if ( requestUrl != null && requestUrl . startsWith ( '/api/cloud/' ) ) {
182
186
switch ( requestPath ) {
183
187
case '/api/cloud/download-project' : {
184
- const url = new URL ( `https://example.com/${ requestUrl } ` )
185
188
const downloadUrl = url . searchParams . get ( 'downloadUrl' )
186
189
const projectId = url . searchParams . get ( 'projectId' )
187
190
@@ -227,7 +230,6 @@ export default function projectManagerShimMiddleware(
227
230
break
228
231
}
229
232
case '/api/cloud/get-project-archive' : {
230
- const url = new URL ( `https://example.com/${ requestUrl } ` )
231
233
const parentDir = url . searchParams . get ( 'directory' )
232
234
233
235
if ( parentDir == null ) {
@@ -261,21 +263,19 @@ export default function projectManagerShimMiddleware(
261
263
}
262
264
}
263
265
} else if ( request . method === 'POST' ) {
264
- const params = new URL ( requestUrl ?? '' ) . searchParams
265
266
switch ( requestPath ) {
266
267
case `/api/${ EXPORT_ARCHIVE_PATH } ` : {
267
- httpDownloadArchive ( request , response , params )
268
+ httpDownloadArchive ( request , response , url . searchParams )
268
269
break
269
270
}
270
271
case '/api/upload-file' : {
271
- httpUploadFile ( request , response , params )
272
+ httpUploadFile ( request , response , url . searchParams )
272
273
break
273
274
}
274
275
// This endpoint should only be used when accessing the app from the browser.
275
276
// When accessing the app from Electron, the file input event will have the
276
277
// full system path.
277
278
case '/api/upload-project' : {
278
- const url = new URL ( `https://example.com/${ requestUrl } ` )
279
279
const directory = url . searchParams . get ( 'directory' )
280
280
const name = url . searchParams . get ( 'name' )
281
281
void projectManagement
@@ -295,9 +295,7 @@ export default function projectManagerShimMiddleware(
295
295
break
296
296
}
297
297
case '/api/run-project-manager-command' : {
298
- const cliArguments : unknown = JSON . parse (
299
- new URL ( `https://example.com/${ requestUrl } ` ) . searchParams . get ( 'cli-arguments' ) ?? '[]' ,
300
- )
298
+ const cliArguments : unknown = JSON . parse ( url . searchParams . get ( 'cli-arguments' ) ?? '[]' )
301
299
if (
302
300
! Array . isArray ( cliArguments ) ||
303
301
! cliArguments . every ( ( item ) : item is string => typeof item === 'string' )
0 commit comments