Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serve sourcemap for socket.io-client #2482

Merged
merged 4 commits into from
Nov 24, 2016
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ module.exports = Server;
*/

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


/**
* Server constructor.
Expand Down Expand Up @@ -249,11 +256,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 @@ -289,6 +299,32 @@ 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){
var etag = req.headers['if-none-match'];
if (etag) {
if (clientVersion == etag) {
debug('serve client 304');
res.writeHead(304);
res.end();
return;
}
}

debug('serve client sourcemap');
res.setHeader('Content-Type', 'application/javascript');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, according to this, it seems the content-type should rather be application/json.

res.setHeader('ETag', clientVersion);
res.writeHead(200);
res.end(clientSourceMap);
};

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