Skip to content

Commit 98de664

Browse files
committed
refactor(core): simplify getId to use last URL segment
1 parent 2a437e9 commit 98de664

2 files changed

Lines changed: 29 additions & 13 deletions

File tree

packages/core/src/handlers/base-handler.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,7 @@ export abstract class BaseHandler<TFile extends UploadxFile>
231231
*/
232232
getId(req: IncomingMessage): string {
233233
const { pathname } = new URL(req.url || '', 'http://localhost');
234-
const path = req.originalUrl
235-
? `/${pathname}`.replace('//', '')
236-
: `/${pathname}`.replace(`/${this.storage.basePath}/`, '');
237-
return path.startsWith('/') ? '' : path;
234+
return pathname.split('/').filter(Boolean).pop() ?? '';
238235
}
239236

240237
async getAndVerifyId(req: IncomingMessage, res: ServerResponse): Promise<string> {

test/base-handler.spec.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('BaseHandler', () => {
7373
});
7474

7575
it.each([
76-
['/1/2', '1/2'],
76+
['/1/2', '2'],
7777
['/3', '3'],
7878
['/files', 'files'],
7979
['/', '']
@@ -82,33 +82,36 @@ describe('BaseHandler', () => {
8282
});
8383

8484
it.each([
85-
['/files/1/2', '1/2'],
85+
['/files/1/2', '2'],
8686
['/files/3', '3'],
8787
['/files/files', 'files'],
8888
['/', ''],
89-
['/1/2', ''],
90-
['/3/files/4', '']
89+
['/1/2', '2'],
90+
['/3/files/4', '4']
9191
])('nodejs: getId(%p) === %p', (url, id) => {
92-
expect(uploader.getId({ url } as http.IncomingMessage)).toBe(id);
92+
const nodeUploader = new TestUploader({ storage: new TestStorage({ basePath: '/files' }) });
93+
expect(nodeUploader.getId({ url } as http.IncomingMessage)).toBe(id);
9394
});
9495

9596
it('should reject path outside basePath (raw Node.js)', () => {
97+
const uploader2 = new TestUploader({ storage: new TestStorage({ basePath: '/files' }) });
9698
const res = createResponse();
9799
const req = createRequest({ method: 'GET', url: '/wrong' });
98100
delete (req as any).originalUrl;
99-
uploader.handle(req, res);
101+
uploader2.handle(req, res);
100102
expect(res.statusCode).toBe(404);
101103
});
102104

103105
it('should accept path matching basePath (raw Node.js)', () => {
106+
const uploader2 = new TestUploader({ storage: new TestStorage({ basePath: '/files' }) });
104107
const res = createResponse();
105108
const req = createRequest({ method: 'PATCH', url: '/files' });
106109
delete (req as any).originalUrl;
107-
uploader.handle(req, res);
110+
uploader2.handle(req, res);
108111
expect(res.statusCode).toBe(405);
109112
});
110113

111-
it('should accept all paths when basePath is "/"', () => {
114+
it('should accept all paths when basePath is "/" (raw Node.js)', () => {
112115
const uploader2 = new TestUploader({ storage: new TestStorage({ basePath: '/' }) });
113116
const res = createResponse();
114117
const req = createRequest({ method: 'PATCH', url: '/anything' });
@@ -117,13 +120,29 @@ describe('BaseHandler', () => {
117120
expect(res.statusCode).toBe(405);
118121
});
119122

120-
it('should skip basePath check when originalUrl is set', () => {
123+
it('should skip basePath check when basePath is not set in options', () => {
121124
const res = createResponse();
122125
const req = createRequest({ method: 'PATCH', url: '/api/other' });
123126
uploader.handle(req, res);
124127
expect(res.statusCode).toBe(405);
125128
});
126129

130+
it('should reject path outside basePath when basePath is set (Express)', () => {
131+
const uploader2 = new TestUploader({ storage: new TestStorage({ basePath: '/files' }) });
132+
const res = createResponse();
133+
const req = createRequest({ method: 'GET', url: '/wrong' });
134+
uploader2.handle(req, res);
135+
expect(res.statusCode).toBe(404);
136+
});
137+
138+
it('should accept path matching basePath when basePath is set (Express)', () => {
139+
const uploader2 = new TestUploader({ storage: new TestStorage({ basePath: '/files' }) });
140+
const res = createResponse();
141+
const req = createRequest({ method: 'PATCH', url: '/files' });
142+
uploader2.handle(req, res);
143+
expect(res.statusCode).toBe(405);
144+
});
145+
127146
interface MockReq extends http.IncomingMessage {
128147
user?: { _id: string };
129148
}

0 commit comments

Comments
 (0)