-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
@tus/server: support
Tus-Max-Size
(#517)
- Loading branch information
Showing
11 changed files
with
489 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {Transform, TransformCallback} from 'stream' | ||
import {ERRORS} from '../constants' | ||
|
||
// TODO: create HttpError and use it everywhere instead of throwing objects | ||
export class MaxFileExceededError extends Error { | ||
status_code: number | ||
body: string | ||
|
||
constructor() { | ||
super(ERRORS.ERR_MAX_SIZE_EXCEEDED.body) | ||
this.status_code = ERRORS.ERR_MAX_SIZE_EXCEEDED.status_code | ||
this.body = ERRORS.ERR_MAX_SIZE_EXCEEDED.body | ||
Object.setPrototypeOf(this, MaxFileExceededError.prototype) | ||
} | ||
} | ||
|
||
export class StreamLimiter extends Transform { | ||
private maxSize: number | ||
private currentSize = 0 | ||
|
||
constructor(maxSize: number) { | ||
super() | ||
this.maxSize = maxSize | ||
} | ||
|
||
_transform(chunk: Buffer, encoding: BufferEncoding, callback: TransformCallback): void { | ||
this.currentSize += chunk.length | ||
if (this.currentSize > this.maxSize) { | ||
callback(new MaxFileExceededError()) | ||
} else { | ||
callback(null, chunk) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,17 +5,27 @@ export function addPipableStreamBody<T extends httpMocks.MockRequest<unknown>>( | |
mockRequest: T | ||
) { | ||
// Create a Readable stream that simulates the request body | ||
const bodyStream = new stream.Readable({ | ||
const bodyStream = new stream.Duplex({ | ||
read() { | ||
this.push(JSON.stringify(mockRequest.body)) | ||
this.push( | ||
mockRequest.body instanceof Buffer | ||
? mockRequest.body | ||
: JSON.stringify(mockRequest.body) | ||
) | ||
this.push(null) | ||
}, | ||
}) | ||
|
||
// Add the pipe method to the mockRequest | ||
// @ts-expect-error pipe exists | ||
// @ts-ignore | ||
Check warning on line 20 in packages/server/test/utils.ts GitHub Actions / lts/hydrogen
Check warning on line 20 in packages/server/test/utils.ts GitHub Actions / node
|
||
mockRequest.pipe = function (dest: stream.Writable) { | ||
bodyStream.pipe(dest) | ||
return bodyStream.pipe(dest) | ||
} | ||
|
||
// Add the unpipe method to the mockRequest | ||
// @ts-ignore | ||
Check warning on line 26 in packages/server/test/utils.ts GitHub Actions / lts/hydrogen
Check warning on line 26 in packages/server/test/utils.ts GitHub Actions / node
|
||
mockRequest.unpipe = function (dest: stream.Writable) { | ||
return bodyStream.unpipe(dest) | ||
} | ||
return mockRequest | ||
} |
Oops, something went wrong.