-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
250 additions
and
7 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
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' | ||
|
||
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 { | ||
this.push(chunk) | ||
callback() | ||
} | ||
} | ||
} |
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,45 @@ | ||
import {Readable} from 'node:stream' | ||
import {ReadableOptions} from 'stream' | ||
|
||
interface MockIncomingMessageOptions extends ReadableOptions { | ||
headers?: Record<string, string> | ||
httpVersion?: string | ||
method?: string | ||
url?: string | ||
chunks?: Buffer[] // Array of data chunks to emit | ||
} | ||
|
||
export class MockIncomingMessage extends Readable { | ||
public headers: Record<string, string> | ||
public httpVersion: string | ||
public method: string | ||
public url: string | ||
private chunks: Buffer[] | ||
private currentIndex: number | ||
|
||
constructor(options: MockIncomingMessageOptions = {}) { | ||
super(options) | ||
this.headers = options.headers || {} | ||
this.httpVersion = options.httpVersion || '1.1' | ||
this.method = options.method || 'GET' | ||
this.url = options.url || '/' | ||
this.chunks = options.chunks || [] | ||
this.currentIndex = 0 | ||
} | ||
|
||
addBodyChunk(buffer: Buffer) { | ||
this.chunks.push(buffer) | ||
} | ||
|
||
_read(): void { | ||
if (this.currentIndex < this.chunks.length) { | ||
const chunk = this.chunks[this.currentIndex] | ||
this.push(chunk) | ||
this.currentIndex++ | ||
} else if (this.currentIndex === this.chunks.length) { | ||
// No more chunks, end the stream | ||
this.push(null) | ||
this.currentIndex++ | ||
} | ||
} | ||
} |