Skip to content

Commit

Permalink
Merge e96788c into 0debedf
Browse files Browse the repository at this point in the history
  • Loading branch information
debadutta98 committed Mar 5, 2023
2 parents 0debedf + e96788c commit 2175f10
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
43 changes: 38 additions & 5 deletions lib/response.js
Expand Up @@ -19,6 +19,7 @@ var deprecate = require('depd')('express');
var encodeUrl = require('encodeurl');
var escapeHtml = require('escape-html');
var http = require('http');
var Blob = require('buffer').Blob;
var isAbsolute = require('./utils').isAbsolute;
var onFinished = require('on-finished');
var path = require('path');
Expand All @@ -34,12 +35,12 @@ var extname = path.extname;
var mime = send.mime;
var resolve = path.resolve;
var vary = require('vary');

/**
* Response prototype.
* @public
*/


var res = Object.create(http.ServerResponse.prototype)

/**
Expand Down Expand Up @@ -104,7 +105,7 @@ res.links = function(links){
* res.send({ some: 'json' });
* res.send('<p>some html</p>');
*
* @param {string|number|boolean|object|Buffer} body
* @param {string|number|boolean|object|Buffer|Blob} body
* @public
*/

Expand All @@ -117,6 +118,15 @@ res.send = function send(body) {
// settings
var app = this.app;

// blob need to be a instance of Node blob
if (chunk && chunk.constructor && chunk.constructor.name === 'Blob') {
if (!(Blob)) {
throw new ReferenceError('Blob is not defined');
} else if(!(chunk instanceof Blob)) {
throw new TypeError('body is not a instance of node Blob');
}
}

// allow status / body
if (arguments.length === 2) {
// res.send(body, status) backwards compat
Expand Down Expand Up @@ -158,6 +168,9 @@ res.send = function send(body) {
if (!this.get('Content-Type')) {
this.type('bin');
}
} else if (chunk && chunk.constructor && chunk.constructor.name === 'Blob') {
this.set('Content-Type', chunk.type || this.get('Content-Type') || 'text/plain')
this.set('Content-Length', chunk.size);
} else {
return this.json(chunk);
}
Expand All @@ -181,15 +194,14 @@ res.send = function send(body) {

// populate Content-Length
var len
if (chunk !== undefined) {
if (chunk !== undefined && chunk.constructor && chunk.constructor.name !== 'Blob') {
if (Buffer.isBuffer(chunk)) {
// get length of Buffer
len = chunk.length
} else if (!generateETag && chunk.length < 1000) {
// just calculate length when no ETag + small chunk
len = Buffer.byteLength(chunk, encoding)
} else {
// convert chunk to Buffer and calculate
chunk = Buffer.from(chunk, encoding)
encoding = undefined;
len = chunk.length
Expand Down Expand Up @@ -229,7 +241,28 @@ res.send = function send(body) {
this.end();
} else {
// respond
this.end(chunk, encoding);
if (chunk && chunk.constructor && chunk.constructor.name === 'Blob') {
var response = this;
try {
var WritableStream = require('stream/web').WritableStream;
chunk.stream().pipeTo(new WritableStream({
write: function(chunk) {
response.write(chunk,encoding);
},
close: function() {
response.end();
}
}))
} catch(error) {
chunk.arrayBuffer().then(function (result) {
response.end(Buffer.from(result), encoding);
}).catch(function (err) {
response.end();
});
}
} else {
this.end(chunk, encoding);
}
}

return this;
Expand Down
40 changes: 40 additions & 0 deletions test/res.send.js
Expand Up @@ -250,6 +250,46 @@ describe('res', function(){
})
})

describe('.send(Blob)', function(){
it('should send as blob type', function (done) {
var Blob = require('buffer').Blob;
if(Blob) {
var str = '<h1>express app</h1>';
var blob = new Blob([str], { type: 'text/html' });
var app = express();
app.use(function (req, res) {
res.send(blob);
});

request(app)
.get('/')
.expect('Content-Type','text/html; charset=utf-8')
.expect(200,'<h1>express app</h1>',done)
} else {
this.skip();
}
})

it('should take default content type of text/plain', function(done){
var Blob = require('buffer').Blob;
if(Blob) {
var str = '<h1>express app</h1>';
var blob = new Blob([str]);
var app = express();
app.use(function (req, res) {
res.send(blob);
});

request(app)
.get('/')
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(200, '<h1>express app</h1>', done)
} else {
this.skip();
}
})
})

describe('when the request method is HEAD', function(){
it('should ignore the body', function(done){
var app = express();
Expand Down

0 comments on commit 2175f10

Please sign in to comment.