11import { ServerResponse } from "node:http" ;
22import Jrror , { JoorError } from "@/core/error" ;
3- import { HeaderSent } from "@/core/error/response" ;
43import logger from '@/helpers/joorLogger' ;
5- import Response , { RESPONSE_LOCATION_STATUS } from "@/types/response" ;
4+ import Response , { RESPONSE_LOCATION_STATUS } from "@/types/response" ;
5+ import assert from "node:assert" ;
6+ import jssert from "./error/jssert" ;
67
78const response = ServerResponse . prototype as Response ;
89
@@ -20,26 +21,9 @@ const response = ServerResponse.prototype as Response;
2021*/
2122response . status = function ( this : ServerResponse , status : number ) : Response {
2223 try {
23- if ( this . headersSent ) {
24- throw HeaderSent ; // Headers have already been sent
25- }
26- if ( ! Number . isInteger ( status ) ) {
27- throw new Jrror ( {
28- code : "response-status-invalid" ,
29- message : `Status must be a integer number, but ${ status } is provided.` ,
30- type : "error" ,
31- docsPath : "/response" ,
32- } ) ;
33- }
34-
35- if ( status < 100 || status > 999 ) {
36- throw new Jrror ( {
37- code : "response-status-invalid" ,
38- message : `Status must be between 100 and 999, but ${ status } is provided.` ,
39- type : "error" ,
40- docsPath : "/response" ,
41- } ) ;
42- }
24+ jssert ( ! this . headersSent , "Headers have already been sent" , "/response" ) ;
25+ jssert ( ! Number . isInteger ( status ) , `Status must be a integer number, but ${ status } is provided.` , "/response" ) ;
26+ jssert ( status < 100 || status > 999 , `Status must be between 100 and 999, but ${ status } is provided.` , "/response" ) ;
4327 this . statusCode = status ;
4428 }
4529 catch ( error : unknown ) {
@@ -70,9 +54,13 @@ For more information, see the [HTTP/1.1 RFC](https://datatracker.ietf.org/doc/ht
7054*/
7155response . location = function ( this : ServerResponse , location : string , status : RESPONSE_LOCATION_STATUS = 301 ) : void {
7256 try {
73- if ( this . headersSent ) {
74- throw HeaderSent ; // Headers have already been sent
75- }
57+ assert ( typeof location === "string" , "Location must be a string" ) ;
58+ assert ( location . length > 0 , "Location must not be empty" ) ;
59+ assert (
60+ status >= 300 && status <= 308 ,
61+ "Status must be between 300 and 308"
62+ ) ;
63+ assert ( ! this . headersSent , "Headers have already been sent" ) ;
7664 this . setHeader ( "Location" , location ) ;
7765 this . status ( status ) ;
7866 }
0 commit comments