Skip to content

Commit a49bc4b

Browse files
authored
@tus/server: Metadata on namingFunction + propagate error (#558)
* fix: propagate error * feat: add metadata to namingFunction
1 parent 367fdec commit a49bc4b

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

packages/server/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ By default, it expects everything in the path after the last `/` to be the uploa
104104
105105
#### `options.namingFunction`
106106

107-
Control how you want to name files (`(req) => string`)
107+
Control how you want to name files (`(req, metadata) => string`)
108108

109109
In `@tus/server`, the upload ID in the URL is the same as the file name.
110110
This means using a custom `namingFunction` will return a different `Location` header for uploading

packages/server/src/handlers/PostHandler.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,21 @@ export class PostHandler extends BaseHandler {
5454
throw ERRORS.INVALID_LENGTH
5555
}
5656

57+
let metadata
58+
if ('upload-metadata' in req.headers) {
59+
try {
60+
metadata = Metadata.parse(upload_metadata)
61+
} catch {
62+
throw ERRORS.INVALID_METADATA
63+
}
64+
}
65+
5766
let id
5867
try {
59-
id = this.options.namingFunction(req)
68+
id = this.options.namingFunction(req, metadata)
6069
} catch (error) {
6170
log('create: check your `namingFunction`. Error', error)
62-
throw ERRORS.FILE_WRITE_ERROR
71+
throw error
6372
}
6473

6574
const maxFileSize = await this.getConfiguredMaxSize(req, id)
@@ -72,15 +81,6 @@ export class PostHandler extends BaseHandler {
7281
throw ERRORS.ERR_MAX_SIZE_EXCEEDED
7382
}
7483

75-
let metadata
76-
if ('upload-metadata' in req.headers) {
77-
try {
78-
metadata = Metadata.parse(upload_metadata)
79-
} catch {
80-
throw ERRORS.INVALID_METADATA
81-
}
82-
}
83-
8484
if (this.options.onIncomingRequest) {
8585
await this.options.onIncomingRequest(req, res, id)
8686
}

packages/server/src/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ export type ServerOptions = {
5757
* Default uses `crypto.randomBytes(16).toString('hex')`.
5858
* @param req - The incoming HTTP request.
5959
*/
60-
namingFunction?: (req: http.IncomingMessage) => string
60+
namingFunction?: (
61+
req: http.IncomingMessage,
62+
metadata?: Record<string, string | null>
63+
) => string
6164

6265
/**
6366
* The Lock interface defines methods for implementing a locking mechanism.

packages/server/test/PostHandler.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,13 @@ describe('PostHandler', () => {
7676
const handler = new PostHandler(fake_store, {
7777
path: '/test',
7878
locker: new MemoryLocker(),
79-
namingFunction: sinon.stub().throws(),
79+
namingFunction: () => {
80+
throw {status_code: 400}
81+
},
8082
})
8183

8284
req.headers = {'upload-length': '1000'}
83-
return assert.rejects(() => handler.send(req, res, context), {status_code: 500})
85+
return assert.rejects(() => handler.send(req, res, context), {status_code: 400})
8486
})
8587

8688
it('should call custom namingFunction', async () => {

0 commit comments

Comments
 (0)