Skip to content

Commit

Permalink
@tus/server: add upload id to onIncomingRequest (#516)
Browse files Browse the repository at this point in the history
fix: add upload-id to onIncomingRequest
  • Loading branch information
fenos authored Nov 27, 2023
1 parent 6fded1e commit 6cf2786
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/server/src/handlers/DeleteHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export class DeleteHandler extends BaseHandler {
throw ERRORS.FILE_NOT_FOUND
}

if (this.options.onIncomingRequest) {
await this.options.onIncomingRequest(req, res, id)
}

await this.store.remove(id)
const writtenRes = this.write(res, 204, {})
this.emit(EVENTS.POST_TERMINATE, req, writtenRes, id)
Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/handlers/GetHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export class GetHandler extends BaseHandler {
throw ERRORS.FILE_NOT_FOUND
}

if (this.options.onIncomingRequest) {
await this.options.onIncomingRequest(req, res, id)
}

const stats = await this.store.getUpload(id)
if (!stats || stats.offset !== stats.size) {
throw ERRORS.FILE_NOT_FOUND
Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/handlers/HeadHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export class HeadHandler extends BaseHandler {
throw ERRORS.FILE_NOT_FOUND
}

if (this.options.onIncomingRequest) {
await this.options.onIncomingRequest(req, res, id)
}

const file = await this.store.getUpload(id)

// If a Client does attempt to resume an upload which has since
Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/handlers/PatchHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class PatchHandler extends BaseHandler {
throw ERRORS.INVALID_CONTENT_TYPE
}

if (this.options.onIncomingRequest) {
await this.options.onIncomingRequest(req, res, id)
}

const upload = await this.store.getUpload(id)

// If a Client does attempt to resume an upload which has since
Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/handlers/PostHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export class PostHandler extends BaseHandler {
}
}

if (this.options.onIncomingRequest) {
await this.options.onIncomingRequest(req, res, id)
}

const upload = new Upload({
id,
size: upload_length ? Number.parseInt(upload_length, 10) : undefined,
Expand Down
6 changes: 0 additions & 6 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,6 @@ export class Server extends EventEmitter {
return this.write(res, status_code, body)
}

try {
await this.options.onIncomingRequest?.(req, res)
} catch (err) {
return onError(err)
}

if (req.method === 'GET') {
const handler = this.handlers.GET
return handler.send(req, res).catch(onError)
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export type ServerOptions = {
) => Promise<http.ServerResponse>
onIncomingRequest?: (
req: http.IncomingMessage,
res: http.ServerResponse
res: http.ServerResponse,
uploadId: string
) => Promise<void>
}

Expand Down

0 comments on commit 6cf2786

Please sign in to comment.