MCPcopy Index your code
hub / github.com/restify/node-restify / serveFileFromStats

Function serveFileFromStats

lib/plugins/static.js:95–143  ·  view source on GitHub ↗
(file, err, stats, isGzip, req, res, next)

Source from the content-addressed store, hash-verified

93 var re = new RegExp('^' + escapeRE(p) + '/?.*');
94
95 function serveFileFromStats(file, err, stats, isGzip, req, res, next) {
96 if (
97 typeof req.connectionState === 'function' &&
98 req.connectionState() === 'close'
99 ) {
100 next(false);
101 return;
102 }
103
104 if (err) {
105 next(new ResourceNotFoundError(err, '%s', req.path()));
106 return;
107 } else if (!stats.isFile()) {
108 next(new ResourceNotFoundError('%s does not exist', req.path()));
109 return;
110 }
111
112 if (res.handledGzip && isGzip) {
113 res.handledGzip();
114 }
115
116 var fstream = fs.createReadStream(file + (isGzip ? '.gz' : ''));
117 var maxAge = opts.maxAge === undefined ? 3600 : opts.maxAge;
118 fstream.once('open', function onceOpen(fd) {
119 res.cache({ maxAge: maxAge });
120 res.set('Content-Length', stats.size);
121 res.set('Content-Type', mime.getType(file));
122 res.set('Last-Modified', stats.mtime);
123
124 if (opts.charSet) {
125 var type =
126 res.getHeader('Content-Type') + '; charset=' + opts.charSet;
127 res.setHeader('Content-Type', type);
128 }
129
130 if (opts.etag) {
131 res.set('ETag', opts.etag(stats, opts));
132 }
133 res.writeHead(200);
134 fstream.pipe(res);
135 fstream.once('close', function onceClose() {
136 next(false);
137 });
138 });
139
140 res.once('close', function onceClose() {
141 fstream.close();
142 });
143 }
144
145 function serveNormal(file, req, res, next) {
146 fs.stat(file, function fileStat(err, stats) {

Callers 2

serveNormalFunction · 0.85
serveFunction · 0.85

Calls 1

nextFunction · 0.50

Tested by

no test coverage detected