Added conditional-GET support to res.send()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 430699c · Browse files · 6 files·+94 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+94 added−4 removed
- other:+94 added−4 removed
Provenance
- Commit
a58e3de- Tree
59046e5- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
430699c
6 files changed+94 added−4 removed
added:examples/big-view/index.js+24 added
123456789101112131415161718192021222324
var express = require('../..') , app = express();
app.set('views', __dirname);app.set('view engine', 'jade');
var pets = [];
var n = 1000;while (n--) { pets.push({ name: 'Tobi', age: 2, species: 'ferret' }); pets.push({ name: 'Loki', age: 1, species: 'ferret' }); pets.push({ name: 'Jane', age: 6, species: 'ferret' });}
app.use(express.logger('dev'));
app.get('/', function(req, res){ res.render('pets', { pets: pets });});
app.listen(3000);console.log('Express listening on port 3000');No newline at end of fileadded:examples/big-view/pets.jade+12 added
1+style2+ body {3+ padding: 50px;4+ font: 16px "Helvetica Neue", Helvetica;5+ }6+7+table8+ for pet in pets9+ tr10+ td= pet.name11+ td= pet.age12+ td= pet.speciesmodified:lib/response.js+21 added-4 removed
16 unmodified lines17 , normalizeType = require('./utils').normalizeType18 , normalizeTypes = require('./utils').normalizeTypes19 , statusCodes = http.STATUS_CODES20 , send = connect.static.send21+ , crc32 = require('crc').crc3222 , mime = require('mime')23 , basename = path.basename24 , extname = path.extname25 , join = path.join;37 unmodified lines63 */64 65 res.send = function(body){66 var req = this.req66- , head = 'HEAD' == req.method;67+ , head = 'HEAD' == req.method68+ , len;69 70 // allow status / body71 if (2 == arguments.length) {72 this.statusCode = body;5 unmodified lines78 79 switch (typeof body) {80 // response status81 case 'number':82+<<<<<<< HEAD83 this.get('Content-Type') || this.type('txt');84+=======85+ this.get('Content-Type') || this.type('.txt');86+>>>>>>> feature/send-etag87 this.statusCode = body;88 body = http.STATUS_CODES[body];89 break;90 // string defaulting to html7 unmodified lines98 case 'object':99 if (null == body) {100 body = '';101 } else if (Buffer.isBuffer(body)) {102+<<<<<<< HEAD103 this.get('Content-Type') || this.type('bin');104+=======105+ this.get('Content-Type') || this.type('.bin');106+>>>>>>> feature/send-etag107 } else {108 return this.json(body);109 }110 break;111 }112 113 // populate Content-Length114 if (undefined !== body && !this.get('Content-Length')) {105- this.set('Content-Length', Buffer.isBuffer(body)106- ? body.length107- : Buffer.byteLength(body));115+ len = Buffer.isBuffer(body) ? body.length : Buffer.byteLength(body);116+ this.set('Content-Length', len);117 }118 119+ // ETag support120+ // TODO: W/ support121+ // TODO: Buffer support for crc32122+ if (len > 1024 && 'string' == typeof body) {123+ this.set('ETag', crc32(body));124+ if (req.fresh) this.statusCode = 304;125+ }126+127 // strip irrelevant headers128 if (204 == this.statusCode || 304 == this.statusCode) {129 this.removeHeader('Content-Type');130 this.removeHeader('Content-Length');528 unmodified linesmodified:support/app.js+18 added
7 unmodified lines89101112131415161718192021222324252627282930313233343536373839404142434445464736 unmodified lines7 unmodified linesvar app = express() , blog = express() , admin = express();
<<<<<<< HEADblog.use('/admin', admin);=======// app.use(express.logger('dev'))>>>>>>> feature/send-etagapp.use('/blog', blog);app.set('views', __dirname + '/views');app.set('view engine', 'jade');app.locals.self = true;
var repo = require('../package.json');
app.get('/render', function(req, res){ res.render('hello');});
<<<<<<< HEADadmin.get('/', function(req, res){ res.send('Hello World\n');=======app.get('/json/small', function(req, res){ res.send(repo);});
app.get('/json/large', function(req, res){ var repos = []; var n = 10; while (n--) repos.push(repo); res.send(repos);>>>>>>> feature/send-etag});
blog.get('/', function(req, res){ res.send('Hello World\n');});
36 unmodified linesmodified:support/bench+18 added
8 unmodified lines9 | cut -d ' ' -f 7 \10 | xargs echo "$2:"11 }12 13+bench_conditional() {14+ ab -n 5000 -c 50 -H "If-None-Match: $3" -k -q http://127.0.0.1:8000$1 \15+ | grep "Requests per" \16+ | cut -d ' ' -f 7 \17+ | xargs echo "$2:"18+}19+20 sleep .521 bench / "Hello World"22 bench /blog "Mounted Hello World"23+<<<<<<< HEAD24 bench /blog/admin "Mounted 2 Hello World"25+=======26+>>>>>>> feature/send-etag27 bench /middleware "Middleware"28 bench /match "Router"29 bench /render "Render"30 bench /json "JSON tiny"31+<<<<<<< HEAD32 bench /json/15 "JSON small"33 bench /json/50 "JSON medium"34 bench /json/150 "JSON large"35+=======36+bench /json/small "JSON small"37+bench /json/large "JSON large"38+bench_conditional /json/large "JSON large etag" -135543568039 40+# 1.25 mb/s41+>>>>>>> feature/send-etag42+43 kill -9 $pidmodified:package.json+1 added
12 unmodified lines13141516171819202119 unmodified lines12 unmodified lines "connect": "2.2.0", "commander": "0.5.2", "mime": "1.2.5", "mkdirp": "0.3.1", "crc": "0.1.0", "debug": "*" }, "devDependencies": { "ejs": "*",19 unmodified lines