diff --git a/lib/socket.js b/lib/socket.js index 7238e866..16f37709 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -8,6 +8,8 @@ var Emitter = require('./emitter'); var debug = require('debug')('engine.io-client:socket'); var index = require('indexof'); var parser = require('engine.io-parser'); +var parseuri = require('parseuri'); +var parsejson = require('parsejson'); /** * Module exports. @@ -42,7 +44,7 @@ function Socket(uri, opts){ } if (uri) { - uri = util.parseUri(uri); + uri = parseuri(uri); opts.host = uri.host; opts.secure = uri.protocol == 'https' || uri.protocol == 'wss'; opts.port = uri.port; @@ -349,7 +351,7 @@ Socket.prototype.onPacket = function (packet) { switch (packet.type) { case 'open': - this.onHandshake(util.parseJSON(packet.data)); + this.onHandshake(parsejson(packet.data)); break; case 'pong': diff --git a/lib/util.js b/lib/util.js index 5fe8e3cc..92ac62be 100644 --- a/lib/util.js +++ b/lib/util.js @@ -74,39 +74,6 @@ if ('undefined' != typeof window) { }); } -/** - * JSON parse. - * - * @see Based on jQuery#parseJSON (MIT) and JSON2 - * @api private - */ - -var rvalidchars = /^[\],:{}\s]*$/; -var rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g; -var rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g; -var rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g; -var rtrimLeft = /^\s+/; -var rtrimRight = /\s+$/; - -exports.parseJSON = function (data) { - if ('string' != typeof data || !data) { - return null; - } - - data = data.replace(rtrimLeft, '').replace(rtrimRight, ''); - - // Attempt to parse using the native JSON parser first - if (global.JSON && JSON.parse) { - return JSON.parse(data); - } - - if (rvalidchars.test(data.replace(rvalidescape, '@') - .replace(rvalidtokens, ']') - .replace(rvalidbraces, ''))) { - return (new Function('return ' + data))(); - } -}; - /** * UA / engines detection namespace. * @@ -154,32 +121,6 @@ exports.ua.ios6 = exports.ua.ios && /OS 6_/.test(navigator.userAgent); exports.ua.chromeframe = Boolean(global.externalHost); -/** - * Parses an URI - * - * @author Steven Levithan (MIT license) - * @api private - */ - -var re = /^(?:(?![^:@]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/; - -var parts = [ - 'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host' - , 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor' -]; - -exports.parseUri = function (str) { - var m = re.exec(str || '') - , uri = {} - , i = 14; - - while (i--) { - uri[parts[i]] = m[i] || ''; - } - - return uri; -}; - /** * Compiles a querystring * diff --git a/package.json b/package.json index 0f1f5349..34a2ba0d 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,10 @@ "xmlhttprequest": "https://github.com/LearnBoost/node-XMLHttpRequest/archive/0f36d0b5ebc03d85f860d42a64ae9791e1daa433.tar.gz", "emitter": "http://github.com/component/emitter/archive/1.0.1.tar.gz", "indexof": "0.0.1", - "engine.io-parser": "1.0.1", - "debug": "0.7.4" + "engine.io-parser": "0.3.0", + "debug": "0.7.4", + "parseuri": "0.0.2", + "parsejson": "0.0.1" }, "devDependencies": { "zuul": "1.5.4", diff --git a/test/util.js b/test/util.js index ca475dc6..88dfda8e 100644 --- a/test/util.js +++ b/test/util.js @@ -14,43 +14,6 @@ var util = eio.util describe('util', function () { - it('should parse an uri', function () { - var http = util.parseUri('http://google.com') - , https = util.parseUri('https://www.google.com:80') - , query = util.parseUri('google.com:8080/foo/bar?foo=bar') - , localhost = util.parseUri('localhost:8080') - , ipv6 = util.parseUri('2001:0db8:85a3:0042:1000:8a2e:0370:7334') - , ipv6short = util.parseUri('2001:db8:85a3:42:1000:8a2e:370:7334') - , ipv6port = util.parseUri('2001:db8:85a3:42:1000:8a2e:370:7334:80') - , ipv6abbrev = util.parseUri('2001::7334:a:80') - - expect(http.protocol).to.be('http'); - expect(http.port).to.be(''); - expect(http.host).to.be('google.com'); - expect(https.protocol).to.be('https'); - expect(https.port).to.be('80'); - expect(https.host).to.be('www.google.com'); - expect(query.port).to.be('8080'); - expect(query.query).to.be('foo=bar'); - expect(query.path).to.be('/foo/bar'); - expect(query.relative).to.be('/foo/bar?foo=bar'); - expect(localhost.protocol).to.be(''); - expect(localhost.host).to.be('localhost'); - expect(localhost.port).to.be('8080'); - expect(ipv6.protocol).to.be(''); - expect(ipv6.host).to.be('2001:0db8:85a3:0042:1000:8a2e:0370:7334'); - expect(ipv6.port).to.be(''); - expect(ipv6short.protocol).to.be(''); - expect(ipv6short.host).to.be('2001:db8:85a3:42:1000:8a2e:370:7334'); - expect(ipv6short.port).to.be(''); - expect(ipv6port.protocol).to.be(''); - expect(ipv6port.host).to.be('2001:db8:85a3:42:1000:8a2e:370:7334'); - expect(ipv6port.port).to.be('80'); - expect(ipv6abbrev.protocol).to.be(''); - expect(ipv6abbrev.host).to.be('2001::7334:a:80'); - expect(ipv6abbrev.port).to.be(''); - }); - it('should construct a query string from an object', function () { expect(util.qs({ a: 'b' })).to.be('a=b'); expect(util.qs({ a: 'b', c: 'd' })).to.be('a=b&c=d');