Skip to content

Commit

Permalink
[feature] serve sourcemap for socket.io-client (#2482)
Browse files Browse the repository at this point in the history
  • Loading branch information
paradite authored and darrachequesne committed Nov 24, 2016
1 parent 4d8e2d3 commit 0ef55b2
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = Server;
*/

var clientSource = undefined;
var clientSourceMap = undefined;

/**
* Server constructor.
Expand Down Expand Up @@ -98,6 +99,11 @@ Server.prototype.serveClient = function(v){

if (v && !clientSource) {
clientSource = read(require.resolve('socket.io-client/socket.io.js'), 'utf-8');
try {
clientSourceMap = read(require.resolve('socket.io-client/socket.io.js.map'), 'utf-8');
} catch(err) {
debug('could not load sourcemap file');
}
}

return this;
Expand Down Expand Up @@ -255,11 +261,14 @@ Server.prototype.attach = function(srv, opts){
Server.prototype.attachServe = function(srv){
debug('attaching client serving req handler');
var url = this._path + '/socket.io.js';
var urlMap = this._path + '/socket.io.js.map';
var evs = srv.listeners('request').slice(0);
var self = this;
srv.removeAllListeners('request');
srv.on('request', function(req, res) {
if (0 === req.url.indexOf(url)) {
if (0 === req.url.indexOf(urlMap)) {
self.serveMap(req, res);
} else if (0 === req.url.indexOf(url)) {
self.serve(req, res);
} else {
for (var i = 0; i < evs.length; i++) {
Expand Down Expand Up @@ -299,6 +308,36 @@ Server.prototype.serve = function(req, res){
res.end(clientSource);
};

/**
* Handles a request serving `/socket.io.js.map`
*
* @param {http.Request} req
* @param {http.Response} res
* @api private
*/

Server.prototype.serveMap = function(req, res){
// Per the standard, ETags must be quoted:
// https://tools.ietf.org/html/rfc7232#section-2.3
var expectedEtag = '"' + clientVersion + '"';

var etag = req.headers['if-none-match'];
if (etag) {
if (expectedEtag == etag) {
debug('serve client 304');
res.writeHead(304);
res.end();
return;
}
}

debug('serve client sourcemap');
res.setHeader('Content-Type', 'application/json');
res.setHeader('ETag', expectedEtag);
res.writeHead(200);
res.end(clientSourceMap);
};

/**
* Binds socket.io to an engine.io instance.
*
Expand Down

0 comments on commit 0ef55b2

Please sign in to comment.