Files
socket.io/lib/url.js
Kevin Roark d5c35bb1f2 Added binary support to socket.io-client
Added changes to reflect socket.io-parser's async encoding, and use
of has-binarydata to check the event type of an event.

Next added browser tests for sending and receiving of binary data via
arraybuffers.

Then added blob tests and blob recognition.

To make blobs fully work (and Files as well), had to add packet buffering
to client so that slow-encoding blobs are still sent before other events.

I fixed a stupid bug I had added where I used the indexof module (for old
browsers) on a string somewhere instead of an array). This was causing
old IE to receive all events twice.

Old iphone tests were still failing so I updated tests to reflect that
some browsers can receive a blob but not construct them.

Finally, reduced build size by adding the "browser" field to package.json
and making browserify less confused.
2014-02-19 14:07:32 -05:00

69 lines
1.4 KiB
JavaScript

/**
* Module dependencies.
*/
var parseuri = require('parseuri');
var debug = require('debug')('socket.io-client:url');
/**
* Module exports.
*/
module.exports = url;
/**
* URL parser.
*
* @param {String} url
* @param {Object} An object meant to mimic window.location.
* Defaults to window.location.
* @api public
*/
function url(uri, loc){
var obj = uri;
// default to window.location
var loc = loc || global.location;
if (null == uri) uri = loc.protocol + '//' + loc.hostname;
// relative path support
if ('string' == typeof uri) {
if ('/' == uri.charAt(0)) {
if ('undefined' != typeof loc) {
uri = loc.hostname + uri;
}
}
if (!/^(https?|wss?):\/\//.test(uri)) {
debug('protocol-less url %s', uri);
if ('undefined' != typeof loc) {
uri = loc.protocol + '//' + uri;
} else {
uri = 'https://' + uri;
}
}
// parse
debug('parse %s', uri);
obj = parseuri(uri);
}
// make sure we treat `localhost:80` and `localhost` equally
if ((/(http|ws)/.test(obj.protocol) && 80 == obj.port) ||
(/(http|ws)s/.test(obj.protocol) && 443 == obj.port)) {
delete obj.port;
}
obj.path = obj.path || '/';
// define unique id
obj.id = obj.protocol + obj.host + (obj.port ? (':' + obj.port) : '');
// define href
obj.href = obj.protocol + '://' + obj.host + (obj.port ? (':' + obj.port) : '');
return obj;
}