@@ -346,6 +346,11 @@ function patch(Response) {
346
346
} ;
347
347
}
348
348
349
+ assert . ok (
350
+ typeof sendArgs . body === 'string' || Buffer . isBuffer ( sendArgs . body ) ,
351
+ 'res.sendRaw() accepts only strings or buffers'
352
+ ) ;
353
+
349
354
sendArgs . format = false ;
350
355
return self . __send ( sendArgs ) ;
351
356
} ;
@@ -417,71 +422,66 @@ function patch(Response) {
417
422
return flush ( self ) ;
418
423
}
419
424
420
- // if no formatting, assert that the value to be written is a string
421
- // or a buffer, then send it.
422
- if ( opts . format === false ) {
423
- assert . ok (
424
- typeof body === 'string' || Buffer . isBuffer ( body ) ,
425
- 'res.sendRaw() accepts only strings or buffers'
426
- ) ;
427
- return flush ( self , body ) ;
428
- }
429
-
430
- // if no body, then no need to format. if this was an error caught by a
431
- // domain, don't send the domain error either.
432
- if ( body === undefined || ( body instanceof Error && body . domain ) ) {
433
- return flush ( self ) ;
434
- }
425
+ if ( opts . format === true ) {
426
+ // if no body, then no need to format. if this was an error caught
427
+ // by a domain, don't send the domain error either.
428
+ if ( body === undefined || ( body instanceof Error && body . domain ) ) {
429
+ return flush ( self ) ;
430
+ }
435
431
436
- // At this point we know we have a body that needs to be formatted, so
437
- // lets derive the formatter based on the response object's properties
432
+ // At this point we know we have a body that needs to be formatted,
433
+ // so lets derive the formatter based on the response object's
434
+ // properties
438
435
439
- var formatter ;
440
- var type = self . contentType || self . getHeader ( 'Content-Type' ) ;
436
+ var formatter ;
437
+ var type = self . contentType || self . getHeader ( 'Content-Type' ) ;
441
438
442
- // Set Content-Type to application/json when
443
- // res.send is called with an Object instead of calling res.json
444
- if ( ! type && typeof body === 'object' && ! Buffer . isBuffer ( body ) ) {
445
- type = 'application/json' ;
446
- }
439
+ // Set Content-Type to application/json when
440
+ // res.send is called with an Object instead of calling res.json
441
+ if ( ! type && typeof body === 'object' && ! Buffer . isBuffer ( body ) ) {
442
+ type = 'application/json' ;
443
+ }
447
444
448
- // Derive type if not provided by the user
449
- type = type || self . req . accepts ( self . acceptable ) ;
450
-
451
- // Check to see if we could find a content type to use for the response.
452
- if ( ! type ) {
453
- return formatterError (
454
- self ,
455
- new errors . NotAcceptableError ( {
456
- message :
457
- 'could not find suitable content-type to use ' +
458
- 'for the response'
459
- } )
460
- ) ;
461
- }
445
+ // Derive type if not provided by the user
446
+ type = type || self . req . accepts ( self . acceptable ) ;
447
+
448
+ // Check to see if we could find a content type to use for the
449
+ // response.
450
+ if ( ! type ) {
451
+ return formatterError (
452
+ self ,
453
+ new errors . NotAcceptableError ( {
454
+ message :
455
+ 'could not find suitable content-type to use ' +
456
+ 'for the response'
457
+ } )
458
+ ) ;
459
+ }
462
460
463
- type = type . split ( ';' ) [ 0 ] ;
461
+ type = type . split ( ';' ) [ 0 ] ;
464
462
465
- if ( ! self . formatters [ type ] && type . indexOf ( '/' ) === - 1 ) {
466
- type = mime . lookup ( type ) ;
467
- }
463
+ if ( ! self . formatters [ type ] && type . indexOf ( '/' ) === - 1 ) {
464
+ type = mime . lookup ( type ) ;
465
+ }
468
466
469
- formatter = self . formatters [ type ] || self . formatters [ '*/*' ] ;
467
+ formatter = self . formatters [ type ] || self . formatters [ '*/*' ] ;
470
468
471
- if ( self . _charSet ) {
472
- type = type + '; charset=' + self . _charSet ;
473
- }
469
+ if ( self . _charSet ) {
470
+ type = type + '; charset=' + self . _charSet ;
471
+ }
474
472
475
- // Update Content-Type header to the one originally set or to the type
476
- // inferred from the most relevant formatter found.
477
- self . setHeader ( 'Content-Type' , type ) ;
473
+ // Update Content-Type header to the one originally set or to the
474
+ // type inferred from the most relevant formatter found.
475
+ self . setHeader ( 'Content-Type' , type ) ;
478
476
479
- if ( ! formatter ) {
480
- return flush ( self , body ) ;
477
+ if ( formatter ) {
478
+ // Finally, invoke the formatter and flush the request with it's
479
+ // results
480
+ return flush ( self , formatter ( self . req , self , body ) ) ;
481
+ }
481
482
}
482
483
483
- // Finally, invoke the formatter and flush the request with it's results
484
- return flush ( self , formatter ( self . req , self , body ) ) ;
484
+ return flush ( self , body ) ;
485
485
} ;
486
486
487
487
/**
@@ -802,11 +802,19 @@ function patch(Response) {
802
802
* @private
803
803
* @function flush
804
804
* @param {Response } res - response
805
- * @param {String|Buffer } formattedBody - formatted body
805
+ * @param {String|Buffer } body - response body
806
806
* @returns {Response } response
807
807
*/
808
- function flush ( res , formattedBody ) {
809
- res . _data = formattedBody ;
808
+ function flush ( res , body ) {
809
+ assert . ok (
810
+ body === null ||
811
+ body === undefined ||
812
+ typeof body === 'string' ||
813
+ Buffer . isBuffer ( body ) ,
814
+ 'body must be a string or a Buffer instance'
815
+ ) ;
816
+
817
+ res . _data = body ;
810
818
811
819
// Flush headers
812
820
res . writeHead ( res . statusCode ) ;
0 commit comments