diff options
25 files changed, 10764 insertions, 49 deletions
diff --git a/CONTRIBUTING_lsp.md b/CONTRIBUTING_lsp.md new file mode 100644 index 0000000..a4bd6a9 --- /dev/null +++ b/CONTRIBUTING_lsp.md @@ -0,0 +1,37 @@ +#Contributing to Orion/LSP Support + +Thanks for your interest in this project. + +#Steps to reproduce: + +1) Clone the git repository : [https://git.eclipse.org/r/orion/org.eclipse.orion.client] and checkout the branch java-lsp. + +2) Get the lsp server from git repo: [https://github.com/gorkem/java-language-server] and follow the README.md to build it. + +3) Install the lsp server in org.eclipse.orion.client/modules/orionode/server folder. + +4) Inside a console where node.js is installed, do: + - npm install + - start it using: node --debug server.js -p 8083 –w <workspace_path> + This runs the Node.js Orion server on localhost:8083. + +5) Open the browser on http://local.orion.org:8083. +The Orion client with the Java™ plugin requires a Node.js Orion server that is enabled for the language server connections. +This is why for now I use a local Node.js server instead of the instance running on orion.eclipse.org. + +#Contributor License Agreement + +Before your contribution can be accepted by the project, you need to create and electronically sign the +Eclipse Foundation [Contributor License Agreement](https://www.eclipse.org/legal/CLA.php) (CLA): + +1. Log in to the [Eclipse projects forge](https://projects.eclipse.org/user/login/sso). You will need to + create an account with the Eclipse Foundation if you have not already done so. +2. Click on "Contributor License Agreement", and complete the form. + +Be sure to use the same email address in your Eclipse account that you intend to use when you commit to Git. + +#Contact + +Contact the project developers via the project's "dev" list. + +- [https://dev.eclipse.org/mailman/listinfo/orion-dev] (https://dev.eclipse.org/mailman/listinfo/orion-dev) diff --git a/bundles/org.eclipse.orion.client.core/web/socketio/socket.io.js b/bundles/org.eclipse.orion.client.core/web/socketio/socket.io.js new file mode 100644 index 0000000..084c403 --- /dev/null +++ b/bundles/org.eclipse.orion.client.core/web/socketio/socket.io.js @@ -0,0 +1,7248 @@ +(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.io = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){ + +/** + * Module dependencies. + */ + +var url = _dereq_('./url'); +var parser = _dereq_('socket.io-parser'); +var Manager = _dereq_('./manager'); +var debug = _dereq_('debug')('socket.io-client'); + +/** + * Module exports. + */ + +module.exports = exports = lookup; + +/** + * Managers cache. + */ + +var cache = exports.managers = {}; + +/** + * Looks up an existing `Manager` for multiplexing. + * If the user summons: + * + * `io('http://localhost/a');` + * `io('http://localhost/b');` + * + * We reuse the existing instance based on same scheme/port/host, + * and we initialize sockets for each namespace. + * + * @api public + */ + +function lookup(uri, opts) { + if (typeof uri == 'object') { + opts = uri; + uri = undefined; + } + + opts = opts || {}; + + var parsed = url(uri); + var source = parsed.source; + var id = parsed.id; + var path = parsed.path; + var sameNamespace = cache[id] && path in cache[id].nsps; + var newConnection = opts.forceNew || opts['force new connection'] || + false === opts.multiplex || sameNamespace; + + var io; + + if (newConnection) { + debug('ignoring socket cache for %s', source); + io = Manager(source, opts); + } else { + if (!cache[id]) { + debug('new io instance for %s', source); + cache[id] = Manager(source, opts); + } + io = cache[id]; + } + + return io.socket(parsed.path); +} + +/** + * Protocol version. + * + * @api public + */ + +exports.protocol = parser.protocol; + +/** + * `connect`. + * + * @param {String} uri + * @api public + */ + +exports.connect = lookup; + +/** + * Expose constructors for standalone build. + * + * @api public + */ + +exports.Manager = _dereq_('./manager'); +exports.Socket = _dereq_('./socket'); + +},{"./manager":2,"./socket":4,"./url":5,"debug":14,"socket.io-parser":40}],2:[function(_dereq_,module,exports){ + +/** + * Module dependencies. + */ + +var eio = _dereq_('engine.io-client'); +var Socket = _dereq_('./socket'); +var Emitter = _dereq_('component-emitter'); +var parser = _dereq_('socket.io-parser'); +var on = _dereq_('./on'); +var bind = _dereq_('component-bind'); +var debug = _dereq_('debug')('socket.io-client:manager'); +var indexOf = _dereq_('indexof'); +var Backoff = _dereq_('backo2'); + +/** + * IE6+ hasOwnProperty + */ + +var has = Object.prototype.hasOwnProperty; + +/** + * Module exports + */ + +module.exports = Manager; + +/** + * `Manager` constructor. + * + * @param {String} engine instance or engine uri/opts + * @param {Object} options + * @api public + */ + +function Manager(uri, opts){ + if (!(this instanceof Manager)) return new Manager(uri, opts); + if (uri && ('object' == typeof uri)) { + opts = uri; + uri = undefined; + } + opts = opts || {}; + + opts.path = opts.path || '/socket.io'; + this.nsps = {}; + this.subs = []; + this.opts = opts; + this.reconnection(opts.reconnection !== false); + this.reconnectionAttempts(opts.reconnectionAttempts || Infinity); + this.reconnectionDelay(opts.reconnectionDelay || 1000); + this.reconnectionDelayMax(opts.reconnectionDelayMax || 5000); + this.randomizationFactor(opts.randomizationFactor || 0.5); + this.backoff = new Backoff({ + min: this.reconnectionDelay(), + max: this.reconnectionDelayMax(), + jitter: this.randomizationFactor() + }); + this.timeout(null == opts.timeout ? 20000 : opts.timeout); + this.readyState = 'closed'; + this.uri = uri; + this.connecting = []; + this.lastPing = null; + this.encoding = false; + this.packetBuffer = []; + this.encoder = new parser.Encoder(); + this.decoder = new parser.Decoder(); + this.autoConnect = opts.autoConnect !== false; + if (this.autoConnect) this.open(); +} + +/** + * Propagate given event to sockets and emit on `this` + * + * @api private + */ + +Manager.prototype.emitAll = function() { + this.emit.apply(this, arguments); + for (var nsp in this.nsps) { + if (has.call(this.nsps, nsp)) { + this.nsps[nsp].emit.apply(this.nsps[nsp], arguments); + } + } +}; + +/** + * Update `socket.id` of all sockets + * + * @api private + */ + +Manager.prototype.updateSocketIds = function(){ + for (var nsp in this.nsps) { + if (has.call(this.nsps, nsp)) { + this.nsps[nsp].id = this.engine.id; + } + } +}; + +/** + * Mix in `Emitter`. + */ + +Emitter(Manager.prototype); + +/** + * Sets the `reconnection` config. + * + * @param {Boolean} true/false if it should automatically reconnect + * @return {Manager} self or value + * @api public + */ + +Manager.prototype.reconnection = function(v){ + if (!arguments.length) return this._reconnection; + this._reconnection = !!v; + return this; +}; + +/** + * Sets the reconnection attempts config. + * + * @param {Number} max reconnection attempts before giving up + * @return {Manager} self or value + * @api public + */ + +Manager.prototype.reconnectionAttempts = function(v){ + if (!arguments.length) return this._reconnectionAttempts; + this._reconnectionAttempts = v; + return this; +}; + +/** + * Sets the delay between reconnections. + * + * @param {Number} delay + * @return {Manager} self or value + * @api public + */ + +Manager.prototype.reconnectionDelay = function(v){ + if (!arguments.length) return this._reconnectionDelay; + this._reconnectionDelay = v; + this.backoff && this.backoff.setMin(v); + return this; +}; + +Manager.prototype.randomizationFactor = function(v){ + if (!arguments.length) return this._randomizationFactor; + this._randomizationFactor = v; + this.backoff && this.backoff.setJitter(v); + return this; +}; + +/** + * Sets the maximum delay between reconnections. + * + * @param {Number} delay + * @return {Manager} self or value + * @api public + */ + +Manager.prototype.reconnectionDelayMax = function(v){ + if (!arguments.length) return this._reconnectionDelayMax; + this._reconnectionDelayMax = v; + this.backoff && this.backoff.setMax(v); + return this; +}; + +/** + * Sets the connection timeout. `false` to disable + * + * @return {Manager} self or value + * @api public + */ + +Manager.prototype.timeout = function(v){ + if (!arguments.length) return this._timeout; + this._timeout = v; + return this; +}; + +/** + * Starts trying to reconnect if reconnection is enabled and we have not + * started reconnecting yet + * + * @api private + */ + +Manager.prototype.maybeReconnectOnOpen = function() { + // Only try to reconnect if it's the first time we're connecting + if (!this.reconnecting && this._reconnection && this.backoff.attempts === 0) { + // keeps reconnection from firing twice for the same reconnection loop + this.reconnect(); + } +}; + + +/** + * Sets the current transport `socket`. + * + * @param {Function} optional, callback + * @return {Manager} self + * @api public + */ + +Manager.prototype.open = +Manager.prototype.connect = function(fn){ + debug('readyState %s', this.readyState); + if (~this.readyState.indexOf('open')) return this; + + debug('opening %s', this.uri); + this.engine = eio(this.uri, this.opts); + var socket = this.engine; + var self = this; + this.readyState = 'opening'; + this.skipReconnect = false; + + // emit `open` + var openSub = on(socket, 'open', function() { + self.onopen(); + fn && fn(); + }); + + // emit `connect_error` + var errorSub = on(socket, 'error', function(data){ + debug('connect_error'); + self.cleanup(); + self.readyState = 'closed'; + self.emitAll('connect_error', data); + if (fn) { + var err = new Error('Connection error'); + err.data = data; + fn(err); + } else { + // Only do this if there is no fn to handle the error + self.maybeReconnectOnOpen(); + } + }); + + // emit `connect_timeout` + if (false !== this._timeout) { + var timeout = this._timeout; + debug('connect attempt will timeout after %d', timeout); + + // set timer + var timer = setTimeout(function(){ + debug('connect attempt timed out after %d', timeout); + openSub.destroy(); + socket.close(); + socket.emit('error', 'timeout'); + self.emitAll('connect_timeout', timeout); + }, timeout); + + this.subs.push({ + destroy: function(){ + clearTimeout(timer); + } + }); + } + + this.subs.push(openSub); + this.subs.push(errorSub); + + return this; +}; + +/** + * Called upon transport open. + * + * @api private + */ + +Manager.prototype.onopen = function(){ + debug('open'); + + // clear old subs + this.cleanup(); + + // mark as open + this.readyState = 'open'; + this.emit('open'); + + // add new subs + var socket = this.engine; + this.subs.push(on(socket, 'data', bind(this, 'ondata'))); + this.subs.push(on(socket, 'ping', bind(this, 'onping'))); + this.subs.push(on(socket, 'pong', bind(this, 'onpong'))); + this.subs.push(on(socket, 'error', bind(this, 'onerror'))); + this.subs.push(on(socket, 'close', bind(this, 'onclose'))); + this.subs.push(on(this.decoder, 'decoded', bind(this, 'ondecoded'))); +}; + +/** + * Called upon a ping. + * + * @api private + */ + +Manager.prototype.onping = function(){ + this.lastPing = new Date; + this.emitAll('ping'); +}; + +/** + * Called upon a packet. + * + * @api private + */ + +Manager.prototype.onpong = function(){ + this.emitAll('pong', new Date - this.lastPing); +}; + +/** + * Called with data. + * + * @api private + */ + +Manager.prototype.ondata = function(data){ + this.decoder.add(data); +}; + +/** + * Called when parser fully decodes a packet. + * + * @api private + */ + +Manager.prototype.ondecoded = function(packet) { + this.emit('packet', packet); +}; + +/** + * Called upon socket error. + * + * @api private + */ + +Manager.prototype.onerror = function(err){ + debug('error', err); + this.emitAll('error', err); +}; + +/** + * Creates a new socket for the given `nsp`. + * + * @return {Socket} + * @api public + */ + +Manager.prototype.socket = function(nsp){ + var socket = this.nsps[nsp]; + if (!socket) { + socket = new Socket(this, nsp); + this.nsps[nsp] = socket; + var self = this; + socket.on('connecting', onConnecting); + socket.on('connect', function(){ + socket.id = self.engine.id; + }); + + if (this.autoConnect) { + // manually call here since connecting evnet is fired before listening + onConnecting(); + } + } + + function onConnecting() { + if (!~indexOf(self.connecting, socket)) { + self.connecting.push(socket); + } + } + + return socket; +}; + +/** + * Called upon a socket close. + * + * @param {Socket} socket + */ + +Manager.prototype.destroy = function(socket){ + var index = indexOf(this.connecting, socket); + if (~index) this.connecting.splice(index, 1); + if (this.connecting.length) return; + + this.close(); +}; + +/** + * Writes a packet. + * + * @param {Object} packet + * @api private + */ + +Manager.prototype.packet = function(packet){ + debug('writing packet %j', packet); + var self = this; + + if (!self.encoding) { + // encode, then write to engine with result + self.encoding = true; + this.encoder.encode(packet, function(encodedPackets) { + for (var i = 0; i < encodedPackets.length; i++) { + self.engine.write(encodedPackets[i], packet.options); + } + self.encoding = false; + self.processPacketQueue(); + }); + } else { // add packet to the queue + self.packetBuffer.push(packet); + } +}; + +/** + * If packet buffer is non-empty, begins encoding the + * next packet in line. + * + * @api private + */ + +Manager.prototype.processPacketQueue = function() { + if (this.packetBuffer.length > 0 && !this.encoding) { + var pack = this.packetBuffer.shift(); + this.packet(pack); + } +}; + +/** + * Clean up transport subscriptions and packet buffer. + * + * @api private + */ + +Manager.prototype.cleanup = function(){ + debug('cleanup'); + + var sub; + while (sub = this.subs.shift()) sub.destroy(); + + this.packetBuffer = []; + this.encoding = false; + this.lastPing = null; + + this.decoder.destroy(); +}; + +/** + * Close the current socket. + * + * @api private + */ + +Manager.prototype.close = +Manager.prototype.disconnect = function(){ + debug('disconnect'); + this.skipReconnect = true; + this.reconnecting = false; + if ('opening' == this.readyState) { + // `onclose` will not fire because + // an open event never happened + this.cleanup(); + } + this.backoff.reset(); + this.readyState = 'closed'; + if (this.engine) this.engine.close(); +}; + +/** + * Called upon engine close. + * + * @api private + */ + +Manager.prototype.onclose = function(reason){ + debug('onclose'); + + this.cleanup(); + this.backoff.reset(); + this.readyState = 'closed'; + this.emit('close', reason); + + if (this._reconnection && !this.skipReconnect) { + this.reconnect(); + } +}; + +/** + * Attempt a reconnection. + * + * @api private + */ + +Manager.prototype.reconnect = function(){ + if (this.reconnecting || this.skipReconnect) return this; + + var self = this; + + if (this.backoff.attempts >= this._reconnectionAttempts) { + debug('reconnect failed'); + this.backoff.reset(); + this.emitAll('reconnect_failed'); + this.reconnecting = false; + } else { + var delay = this.backoff.duration(); + debug('will wait %dms before reconnect attempt', delay); + + this.reconnecting = true; + var timer = setTimeout(function(){ + if (self.skipReconnect) return; + + debug('attempting reconnect'); + self.emitAll('reconnect_attempt', self.backoff.attempts); + self.emitAll('reconnecting', self.backoff.attempts); + + // check again for the case socket closed in above events + if (self.skipReconnect) return; + + self.open(function(err){ + if (err) { + debug('reconnect attempt error'); + self.reconnecting = false; + self.reconnect(); + self.emitAll('reconnect_error', err.data); + } else { + debug('reconnect success'); + self.onreconnect(); + } + }); + }, delay); + + this.subs.push({ + destroy: function(){ + clearTimeout(timer); + } + }); + } +}; + +/** + * Called upon successful reconnect. + * + * @api private + */ + +Manager.prototype.onreconnect = function(){ + var attempt = this.backoff.attempts; + this.reconnecting = false; + this.backoff.reset(); + this.updateSocketIds(); + this.emitAll('reconnect', attempt); +}; + +},{"./on":3,"./socket":4,"backo2":8,"component-bind":11,"component-emitter":12,"debug":14,"engine.io-client":16,"indexof":32,"socket.io-parser":40}],3:[function(_dereq_,module,exports){ + +/** + * Module exports. + */ + +module.exports = on; + +/** + * Helper for subscriptions. + * + * @param {Object|EventEmitter} obj with `Emitter` mixin or `EventEmitter` + * @param {String} event name + * @param {Function} callback + * @api public + */ + +function on(obj, ev, fn) { + obj.on(ev, fn); + return { + destroy: function(){ + obj.removeListener(ev, fn); + } + }; +} + +},{}],4:[function(_dereq_,module,exports){ + +/** + * Module dependencies. + */ + +var parser = _dereq_('socket.io-parser'); +var Emitter = _dereq_('component-emitter'); +var toArray = _dereq_('to-array'); +var on = _dereq_('./on'); +var bind = _dereq_('component-bind'); +var debug = _dereq_('debug')('socket.io-client:socket'); +var hasBin = _dereq_('has-binary'); + +/** + * Module exports. + */ + +module.exports = exports = Socket; + +/** + * Internal events (blacklisted). + * These events can't be emitted by the user. + * + * @api private + */ + +var events = { + connect: 1, + connect_error: 1, + connect_timeout: 1, + connecting: 1, + disconnect: 1, + error: 1, + reconnect: 1, + reconnect_attempt: 1, + reconnect_failed: 1, + reconnect_error: 1, + reconnecting: 1, + ping: 1, + pong: 1 +}; + +/** + * Shortcut to `Emitter#emit`. + */ + +var emit = Emitter.prototype.emit; + +/** + * `Socket` constructor. + * + * @api public + */ + +function Socket(io, nsp){ + this.io = io; + this.nsp = nsp; + this.json = this; // compat + this.ids = 0; + this.acks = {}; + this.receiveBuffer = []; + this.sendBuffer = []; + this.connected = false; + this.disconnected = true; + if (this.io.autoConnect) this.open(); +} + +/** + * Mix in `Emitter`. + */ + +Emitter(Socket.prototype); + +/** + * Subscribe to open, close and packet events + * + * @api private + */ + +Socket.prototype.subEvents = function() { + if (this.subs) return; + + var io = this.io; + this.subs = [ + on(io, 'open', bind(this, 'onopen')), + on(io, 'packet', bind(this, 'onpacket')), + on(io, 'close', bind(this, 'onclose')) + ]; +}; + +/** + * "Opens" the socket. + * + * @api public + */ + +Socket.prototype.open = +Socket.prototype.connect = function(){ + if (this.connected) return this; + + this.subEvents(); + this.io.open(); // ensure open + if ('open' == this.io.readyState) this.onopen(); + this.emit('connecting'); + return this; +}; + +/** + * Sends a `message` event. + * + * @return {Socket} self + * @api public + */ + +Socket.prototype.send = function(){ + var args = toArray(arguments); + args.unshift('message'); + this.emit.apply(this, args); + return this; +}; + +/** + * Override `emit`. + * If the event is in `events`, it's emitted normally. + * + * @param {String} event name + * @return {Socket} self + * @api public + */ + +Socket.prototype.emit = function(ev){ + if (events.hasOwnProperty(ev)) { + emit.apply(this, arguments); + return this; + } + + var args = toArray(arguments); + var parserType = parser.EVENT; // default + if (hasBin(args)) { parserType = parser.BINARY_EVENT; } // binary + var packet = { type: parserType, data: args }; + + packet.options = {}; + packet.options.compress = !this.flags || false !== this.flags.compress; + + // event ack callback + if ('function' == typeof args[args.length - 1]) { + debug('emitting packet with ack id %d', this.ids); + this.acks[this.ids] = args.pop(); + packet.id = this.ids++; + } + + if (this.connected) { + this.packet(packet); + } else { + this.sendBuffer.push(packet); + } + + delete this.flags; + + return this; +}; + +/** + * Sends a packet. + * + * @param {Object} packet + * @api private + */ + +Socket.prototype.packet = function(packet){ + packet.nsp = this.nsp; + this.io.packet(packet); +}; + +/** + * Called upon engine `open`. + * + * @api private + */ + +Socket.prototype.onopen = function(){ + debug('transport is open - connecting'); + + // write connect packet if necessary + if ('/' != this.nsp) { + this.packet({ type: parser.CONNECT }); + } +}; + +/** + * Called upon engine `close`. + * + * @param {String} reason + * @api private + */ + +Socket.prototype.onclose = function(reason){ + debug('close (%s)', reason); + this.connected = false; + this.disconnected = true; + delete this.id; + this.emit('disconnect', reason); +}; + +/** + * Called with socket packet. + * + * @param {Object} packet + * @api private + */ + +Socket.prototype.onpacket = function(packet){ + if (packet.nsp != this.nsp) return; + + switch (packet.type) { + case parser.CONNECT: + this.onconnect(); + break; + + case parser.EVENT: + this.onevent(packet); + break; + + case parser.BINARY_EVENT: + this.onevent(packet); + break; + + case parser.ACK: + this.onack(packet); + break; + + case parser.BINARY_ACK: + this.onack(packet); + break; + + case parser.DISCONNECT: + this.ondisconnect(); + break; + + case parser.ERROR: + this.emit('error', packet.data); + break; + } +}; + +/** + * Called upon a server event. + * + * @param {Object} packet + * @api private + */ + +Socket.prototype.onevent = function(packet){ + var args = packet.data || []; + debug('emitting event %j', args); + + if (null != packet.id) { + debug('attaching ack callback to event'); + args.push(this.ack(packet.id)); + } + + if (this.connected) { + emit.apply(this, args); + } else { + this.receiveBuffer.push(args); + } +}; + +/** + * Produces an ack callback to emit with an event. + * + * @api private + */ + +Socket.prototype.ack = function(id){ + var self = this; + var sent = false; + return function(){ + // prevent double callbacks + if (sent) return; + sent = true; + var args = toArray(arguments); + debug('sending ack %j', args); + + var type = hasBin(args) ? parser.BINARY_ACK : parser.ACK; + self.packet({ + type: type, + id: id, + data: args + }); + }; +}; + +/** + * Called upon a server acknowlegement. + * + * @param {Object} packet + * @api private + */ + +Socket.prototype.onack = function(packet){ + var ack = this.acks[packet.id]; + if ('function' == typeof ack) { + debug('calling ack %s with %j', packet.id, packet.data); + ack.apply(this, packet.data); + delete this.acks[packet.id]; + } else { + debug('bad ack %s', packet.id); + } +}; + +/** + * Called upon server connect. + * + * @api private + */ + +Socket.prototype.onconnect = function(){ + this.connected = true; + this.disconnected = false; + this.emit('connect'); + this.emitBuffered(); +}; + +/** + * Emit buffered events (received and emitted). + * + * @api private + */ + +Socket.prototype.emitBuffered = function(){ + var i; + for (i = 0; i < this.receiveBuffer.length; i++) { + emit.apply(this, this.receiveBuffer[i]); + } + this.receiveBuffer = []; + + for (i = 0; i < this.sendBuffer.length; i++) { + this.packet(this.sendBuffer[i]); + } + this.sendBuffer = []; +}; + +/** + * Called upon server disconnect. + * + * @api private + */ + +Socket.prototype.ondisconnect = function(){ + debug('server disconnect (%s)', this.nsp); + this.destroy(); + this.onclose('io server disconnect'); +}; + +/** + * Called upon forced client/server side disconnections, + * this method ensures the manager stops tracking us and + * that reconnections don't get triggered for this. + * + * @api private. + */ + +Socket.prototype.destroy = function(){ + if (this.subs) { + // clean subscriptions to avoid reconnections + for (var i = 0; i < this.subs.length; i++) { + this.subs[i].destroy(); + } + this.subs = null; + } + + this.io.destroy(this); +}; + +/** + * Disconnects the socket manually. + * + * @return {Socket} self + * @api public + */ + +Socket.prototype.close = +Socket.prototype.disconnect = function(){ + if (this.connected) { + debug('performing disconnect (%s)', this.nsp); + this.packet({ type: parser.DISCONNECT }); + } + + // remove socket from pool + this.destroy(); + + if (this.connected) { + // fire events + this.onclose('io client disconnect'); + } + return this; +}; + +/** + * Sets the compress flag. + * + * @param {Boolean} if `true`, compresses the sending data + * @return {Socket} self + * @api public + */ + +Socket.prototype.compress = function(compress){ + this.flags = this.flags || {}; + this.flags.compress = compress; + return this; +}; + +},{"./on":3,"component-bind":11,"component-emitter":12,"debug":14,"has-binary":30,"socket.io-parser":40,"to-array":43}],5:[function(_dereq_,module,exports){ +(function (global){ + +/** + * Module dependencies. + */ + +var parseuri = _dereq_('parseuri'); +var debug = _dereq_('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.host; + + // relative path support + if ('string' == typeof uri) { + if ('/' == uri.charAt(0)) { + if ('/' == uri.charAt(1)) { + uri = loc.protocol + uri; + } else { + uri = loc.host + 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 (!obj.port) { + if (/^(http|ws)$/.test(obj.protocol)) { + obj.port = '80'; + } + else if (/^(http|ws)s$/.test(obj.protocol)) { + obj.port = '443'; + } + } + + obj.path = obj.path || '/'; + + var ipv6 = obj.host.indexOf(':') !== -1; + var host = ipv6 ? '[' + obj.host + ']' : obj.host; + + // define unique id + obj.id = obj.protocol + '://' + host + ':' + obj.port; + // define href + obj.href = obj.protocol + '://' + host + (loc && loc.port == obj.port ? '' : (':' + obj.port)); + + return obj; +} + +}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {}) +},{"debug":14,"parseuri":38}],6:[function(_dereq_,module,exports){ +module.exports = after + +function after(count, callback, err_cb) { + var bail = false + err_cb = err_cb || noop + proxy.count = count + + return (count === 0) ? callback() : proxy + + function proxy(err, result) { + if (proxy.count <= 0) { + throw new Error('after called too many times') + } + --proxy.count + + // after first error, rest are passed to err_cb + if (err) { + bail = true + callback(err) + // future error callbacks will go to error handler + callback = err_cb + } else if (proxy.count === 0 && !bail) { + callback(null, result) + } + } +} + +function noop() {} + +},{}],7:[function(_dereq_,module,exports){ +/** + * An abstraction for slicing an arraybuffer even when + * ArrayBuffer.prototype.slice is not supported + * + * @api public + */ + +module.exports = function(arraybuffer, start, end) { + var bytes = arraybuffer.byteLength; + start = start || 0; + end = end || bytes; + + if (arraybuffer.slice) { return arraybuffer.slice(start, end); } + + if (start < 0) { start += bytes; } + if (end < 0) { end += bytes; } + if (end > bytes) { end = bytes; } + + if (start >= bytes || start >= end || bytes === 0) { + return new ArrayBuffer(0); + } + + var abv = new Uint8Array(arraybuffer); + var result = new Uint8Array(end - start); + for (var i = start, ii = 0; i < end; i++, ii++) { + result[ii] = abv[i]; + } + return result.buffer; +}; + +},{}],8:[function(_dereq_,module,exports){ + +/** + * Expose `Backoff`. + */ + +module.exports = Backoff; + +/** + * Initialize backoff timer with `opts`. + * + * - `min` initial timeout in milliseconds [100] + * - `max` max timeout [10000] + * - `jitter` [0] + * - `factor` [2] + * + * @param {Object} opts + * @api public + */ + +function Backoff(opts) { + opts = opts || {}; + this.ms = opts.min || 100; + this.max = opts.max || 10000; + this.factor = opts.factor || 2; + this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0; + this.attempts = 0; +} + +/** + * Return the backoff duration. + * + * @return {Number} + * @api public + */ + +Backoff.prototype.duration = function(){ + var ms = this.ms * Math.pow(this.factor, this.attempts++); + if (this.jitter) { + var rand = Math.random(); + var deviation = Math.floor(rand * this.jitter * ms); + ms = (Math.floor(rand * 10) & 1) == 0 ? ms - deviation : ms + deviation; + } + return Math.min(ms, this.max) | 0; +}; + +/** + * Reset the number of attempts. + * + * @api public + */ + +Backoff.prototype.reset = function(){ + this.attempts = 0; +}; + +/** + * Set the minimum duration + * + * @api public + */ + +Backoff.prototype.setMin = function(min){ + this.ms = min; +}; + +/** + * Set the maximum duration + * + * @api public + */ + +Backoff.prototype.setMax = function(max){ + this.max = max; +}; + +/** + * Set the jitter + * + * @api public + */ + +Backoff.prototype.setJitter = function(jitter){ + this.jitter = jitter; +}; + + +},{}],9:[function(_dereq_,module,exports){ +/* + * base64-arraybuffer + * https://github.com/niklasvh/base64-arraybuffer + * + * Copyright (c) 2012 Niklas von Hertzen + * Licensed under the MIT license. + */ +(function(){ + "use strict"; + + var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + // Use a lookup table to find the index. + var lookup = new Uint8Array(256); + for (var i = 0; i < chars.length; i++) { + lookup[chars.charCodeAt(i)] = i; + } + + exports.encode = function(arraybuffer) { + var bytes = new Uint8Array(arraybuffer), + i, len = bytes.length, base64 = ""; + + for (i = 0; i < len; i+=3) { + base64 += chars[bytes[i] >> 2]; + base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)]; + base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)]; + base64 += chars[bytes[i + 2] & 63]; + } + + if ((len % 3) === 2) { + base64 = base64.substring(0, base64.length - 1) + "="; + } else if (len % 3 === 1) { + base64 = base64.substring(0, base64.length - 2) + "=="; + } + + return base64; + }; + + exports.decode = function(base64) { + var bufferLength = base64.length * 0.75, + len = base64.length, i, p = 0, + encoded1, encoded2, encoded3, encoded4; + + if (base64[base64.length - 1] === "=") { + bufferLength--; + if (base64[base64.length - 2] === "=") { + bufferLength--; + } + } + + var arraybuffer = new ArrayBuffer(bufferLength), + bytes = new Uint8Array(arraybuffer); + + for (i = 0; i < len; i+=4) { + encoded1 = lookup[base64.charCodeAt(i)]; + encoded2 = lookup[base64.charCodeAt(i+1)]; + encoded3 = lookup[base64.charCodeAt(i+2)]; + encoded4 = lookup[base64.charCodeAt(i+3)]; + + bytes[p++] = (encoded1 << 2) | (encoded2 >> 4); + bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2); + bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63); + } + + return arraybuffer; + }; +})(); + +},{}],10:[function(_dereq_,module,exports){ +(function (global){ +/** + * Create a blob builder even when vendor prefixes exist + */ + +var BlobBuilder = global.BlobBuilder + || global.WebKitBlobBuilder + || global.MSBlobBuilder + || global.MozBlobBuilder; + +/** + * Check if Blob constructor is supported + */ + +var blobSupported = (function() { + try { + var a = new Blob(['hi']); + return a.size === 2; + } catch(e) { + return false; + } +})(); + +/** + * Check if Blob constructor supports ArrayBufferViews + * Fails in Safari 6, so we need to map to ArrayBuffers there. + */ + +var blobSupportsArrayBufferView = blobSupported && (function() { + try { + var b = new Blob([new Uint8Array([1,2])]); + return b.size === 2; + } catch(e) { + return false; + } +})(); + +/** + * Check if BlobBuilder is supported + */ + +var blobBuilderSupported = BlobBuilder + && BlobBuilder.prototype.append + && BlobBuilder.prototype.getBlob; + +/** + * Helper function that maps ArrayBufferViews to ArrayBuffers + * Used by BlobBuilder constructor and old browsers that didn't + * support it in the Blob constructor. + */ + +function mapArrayBufferViews(ary) { + for (var i = 0; i < ary.length; i++) { + var chunk = ary[i]; + if (chunk.buffer instanceof ArrayBuffer) { + var buf = chunk.buffer; + + // if this is a subarray, make a copy so we only + // include the subarray region from the underlying buffer + if (chunk.byteLength !== buf.byteLength) { + var copy = new Uint8Array(chunk.byteLength); + copy.set(new Uint8Array(buf, chunk.byteOffset, chunk.byteLength)); + buf = copy.buffer; + } + + ary[i] = buf; + } + } +} + +function BlobBuilderConstructor(ary, options) { + options = options || {}; + + var bb = new BlobBuilder(); + mapArrayBufferViews(ary); + + for (var i = 0; i < ary.length; i++) { + bb.append(ary[i]); + } + + return (options.type) ? bb.getBlob(options.type) : bb.getBlob(); +}; + +function BlobConstructor(ary, options) { + mapArrayBufferViews(ary); + return new Blob(ary, options || {}); +}; + +module.exports = (function() { + if (blobSupported) { + return blobSupportsArrayBufferView ? global.Blob : BlobConstructor; + } else if (blobBuilderSupported) { + return BlobBuilderConstructor; + } else { + return undefined; + } +})(); + +}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {}) +},{}],11:[function(_dereq_,module,exports){ +/** + * Slice reference. + */ + +var slice = [].slice; + +/** + * Bind `obj` to `fn`. + * + * @param {Object} obj + * @param {Function|String} fn or string + * @return {Function} + * @api public + */ + +module.exports = function(obj, fn){ + if ('string' == typeof fn) fn = obj[fn]; + if ('function' != typeof fn) throw new Error('bind() requires a function'); + var args = slice.call(arguments, 2); + return function(){ + return fn.apply(obj, args.concat(slice.call(arguments))); + } +}; + +},{}],12:[function(_dereq_,module,exports){ + +/** + * Expose `Emitter`. + */ + +module.exports = Emitter; + +/** + * Initialize a new `Emitter`. + * + * @api public + */ + +function Emitter(obj) { + if (obj) return mixin(obj); +}; + +/** + * Mixin the emitter properties. + * + * @param {Object} obj + * @return {Object} + * @api private + */ + +function mixin(obj) { + for (var key in Emitter.prototype) { + obj[key] = Emitter.prototype[key]; + } + return obj; +} + +/** + * Listen on the given `event` with `fn`. + * + * @param {String} event + * @param {Function} fn + * @return {Emitter} + * @api public + */ + +Emitter.prototype.on = +Emitter.prototype.addEventListener = function(event, fn){ + this._callbacks = this._callbacks || {}; + (this._callbacks['$' + event] = this._callbacks['$' + event] || []) + .push(fn); + return this; +}; + +/** + * Adds an `event` listener that will be invoked a single + * time then automatically removed. + * + * @param {String} event + * @param {Function} fn + * @return {Emitter} + * @api public + */ + +Emitter.prototype.once = function(event, fn){ + function on() { + this.off(event, on); + fn.apply(this, arguments); + } + + on.fn = fn; + this.on(event, on); + return this; +}; + +/** + * Remove the given callback for `event` or all + * registered callbacks. + * + * @param {String} event + * @param {Function} fn + * @return {Emitter} + * @api public + */ + +Emitter.prototype.off = +Emitter.prototype.removeListener = +Emitter.prototype.removeAllListeners = +Emitter.prototype.removeEventListener = function(event, fn){ + this._callbacks = this._callbacks || {}; + + // all + if (0 == arguments.length) { + this._callbacks = {}; + return this; + } + + // specific event + var callbacks = this._callbacks['$' + event]; + if (!callbacks) return this; + + // remove all handlers + if (1 == arguments.length) { + delete this._callbacks['$' + event]; + return this; + } + + // remove specific handler + var cb; + for (var i = 0; i < callbacks.length; i++) { + cb = callbacks[i]; + if (cb === fn || cb.fn === fn) { + callbacks.splice(i, 1); + break; + } + } + return this; +}; + +/** + * Emit `event` with the given args. + * + * @param {String} event + * @param {Mixed} ... + * @return {Emitter} + */ + +Emitter.prototype.emit = function(event){ + this._callbacks = this._callbacks || {}; + var args = [].slice.call(arguments, 1) + , callbacks = this._callbacks['$' + event]; + + if (callbacks) { + callbacks = callbacks.slice(0); + for (var i = 0, len = callbacks.length; i < len; ++i) { + callbacks[i].apply(this, args); + } + } + + return this; +}; + +/** + * Return array of callbacks for `event`. + * + * @param {String} event + * @return {Array} + * @api public + */ + +Emitter.prototype.listeners = function(event){ + this._callbacks = this._callbacks || {}; + return this._callbacks['$' + event] || []; +}; + +/** + * Check if this emitter has `event` handlers. + * + * @param {String} event + * @return {Boolean} + * @api public + */ + +Emitter.prototype.hasListeners = function(event){ + return !! this.listeners(event).length; +}; + +},{}],13:[function(_dereq_,module,exports){ + +module.exports = function(a, b){ + var fn = function(){}; + fn.prototype = b.prototype; + a.prototype = new fn; + a.prototype.constructor = a; +}; +},{}],14:[function(_dereq_,module,exports){ + +/** + * This is the web browser implementation of `debug()`. + * + * Expose `debug()` as the module. + */ + +exports = module.exports = _dereq_('./debug'); +exports.log = log; +exports.formatArgs = formatArgs; +exports.save = save; +exports.load = load; +exports.useColors = useColors; +exports.storage = 'undefined' != typeof chrome + && 'undefined' != typeof chrome.storage + ? chrome.storage.local + : localstorage(); + +/** + * Colors. + */ + +exports.colors = [ + 'lightseagreen', + 'forestgreen', + 'goldenrod', + 'dodgerblue', + 'darkorchid', + 'crimson' +]; + +/** + * Currently only WebKit-based Web Inspectors, Firefox >= v31, + * and the Firebug extension (any Firefox version) are known + * to support "%c" CSS customizations. + * + * TODO: add a `localStorage` variable to explicitly enable/disable colors + */ + +function useColors() { + // is webkit? http://stackoverflow.com/a/16459606/376773 + return ('WebkitAppearance' in document.documentElement.style) || + // is firebug? http://stackoverflow.com/a/398120/376773 + (window.console && (console.firebug || (console.exception && console.table))) || + // is firefox >= v31? + // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages + (navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31); +} + +/** + * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default. + */ + +exports.formatters.j = function(v) { + return JSON.stringify(v); +}; + + +/** + * Colorize log arguments if enabled. + * + * @api public + */ + +function formatArgs() { + var args = arguments; + var useColors = this.useColors; + + args[0] = (useColors ? '%c' : '') + + this.namespace + + (useColors ? ' %c' : ' ') + + args[0] + + (useColors ? '%c ' : ' ') + + '+' + exports.humanize(this.diff); + + if (!useColors) return args; + + var c = 'color: ' + this.color; + args = [args[0], c, 'color: inherit'].concat(Array.prototype.slice.call(args, 1)); + + // the final "%c" is somewhat tricky, because there could be other + // arguments passed either before or after the %c, so we need to + // figure out the correct index to insert the CSS into + var index = 0; + var lastC = 0; + args[0].replace(/%[a-z%]/g, function(match) { + if ('%%' === match) return; + index++; + if ('%c' === match) { + // we only are interested in the *last* %c + // (the user may have provided their own) + lastC = index; + } + }); + + args.splice(lastC, 0, c); + return args; +} + +/** + * Invokes `console.log()` when available. + * No-op when `console.log` is not a "function". + * + * @api public + */ + +function log() { + // this hackery is required for IE8/9, where + // the `console.log` function doesn't have 'apply' + return 'object' === typeof console + && console.log + && Function.prototype.apply.call(console.log, console, arguments); +} + +/** + * Save `namespaces`. + * + * @param {String} namespaces + * @api private + */ + +function save(namespaces) { + try { + if (null == namespaces) { + exports.storage.removeItem('debug'); + } else { + exports.storage.debug = namespaces; + } + } catch(e) {} +} + +/** + * Load `namespaces`. + * + * @return {String} returns the previously persisted debug modes + * @api private + */ + +function load() { + var r; + try { + r = exports.storage.debug; + } catch(e) {} + return r; +} + +/** + * Enable namespaces listed in `localStorage.debug` initially. + */ + +exports.enable(load()); + +/** + * Localstorage attempts to return the localstorage. + * + * This is necessary because safari throws + * when a user disables cookies/localstorage + * and you attempt to access it. + * + * @return {LocalStorage} + * @api private + */ + +function localstorage(){ + try { + return window.localStorage; + } catch (e) {} +} + +},{"./debug":15}],15:[function(_dereq_,module,exports){ + +/** + * This is the common logic for both the Node.js and web browser + * implementations of `debug()`. + * + * Expose `debug()` as the module. + */ + +exports = module.exports = debug; +exports.coerce = coerce; +exports.disable = disable; +exports.enable = enable; +exports.enabled = enabled; +exports.humanize = _dereq_('ms'); + +/** + * The currently active debug mode names, and names to skip. + */ + +exports.names = []; +exports.skips = []; + +/** + * Map of special "%n" handling functions, for the debug "format" argument. + * + * Valid key names are a single, lowercased letter, i.e. "n". + */ + +exports.formatters = {}; + +/** + * Previously assigned color. + */ + +var prevColor = 0; + +/** + * Previous log timestamp. + */ + +var prevTime; + +/** + * Select a color. + * + * @return {Number} + * @api private + */ + +function selectColor() { + return exports.colors[prevColor++ % exports.colors.length]; +} + +/** + * Create a debugger with the given `namespace`. + * + * @param {String} namespace + * @return {Function} + * @api public + */ + +function debug(namespace) { + + // define the `disabled` version + function disabled() { + } + disabled.enabled = false; + + // define the `enabled` version + function enabled() { + + var self = enabled; + + // set `diff` timestamp + var curr = +new Date(); + var ms = curr - (prevTime || curr); + self.diff = ms; + self.prev = prevTime; + self.curr = curr; + prevTime = curr; + + // add the `color` if not set + if (null == self.useColors) self.useColors = exports.useColors(); + if (null == self.color && self.useColors) self.color = selectColor(); + + var args = Array.prototype.slice.call(arguments); + + args[0] = exports.coerce(args[0]); + + if ('string' !== typeof args[0]) { + // anything else let's inspect with %o + args = ['%o'].concat(args); + } + + // apply any `formatters` transformations + var index = 0; + args[0] = args[0].replace(/%([a-z%])/g, function(match, format) { + // if we encounter an escaped % then don't increase the array index + if (match === '%%') return match; + index++; + var formatter = exports.formatters[format]; + if ('function' === typeof formatter) { + var val = args[index]; + match = formatter.call(self, val); + + // now we need to remove `args[index]` since it's inlined in the `format` + args.splice(index, 1); + index--; + } + return match; + }); + + if ('function' === typeof exports.formatArgs) { + args = exports.formatArgs.apply(self, args); + } + var logFn = enabled.log || exports.log || console.log.bind(console); + logFn.apply(self, args); + } + enabled.enabled = true; + + var fn = exports.enabled(namespace) ? enabled : disabled; + + fn.namespace = namespace; + + return fn; +} + +/** + * Enables a debug mode by namespaces. This can include modes + * separated by a colon and wildcards. + * + * @param {String} namespaces + * @api public + */ + +function enable(namespaces) { + exports.save(namespaces); + + var split = (namespaces || '').split(/[\s,]+/); + var len = split.length; + + for (var i = 0; i < len; i++) { + if (!split[i]) continue; // ignore empty strings + namespaces = split[i].replace(/\*/g, '.*?'); + if (namespaces[0] === '-') { + exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$')); + } else { + exports.names.push(new RegExp('^' + namespaces + '$')); + } + } +} + +/** + * Disable debug output. + * + * @api public + */ + +function disable() { + exports.enable(''); +} + +/** + * Returns true if the given mode name is enabled, false otherwise. + * + * @param {String} name + * @return {Boolean} + * @api public + */ + +function enabled(name) { + var i, len; + for (i = 0, len = exports.skips.length; i < len; i++) { + if (exports.skips[i].test(name)) { + return false; + } + } + for (i = 0, len = exports.names.length; i < len; i++) { + if (exports.names[i].test(name)) { + return true; + } + } + return false; +} + +/** + * Coerce `val`. + * + * @param {Mixed} val + * @return {Mixed} + * @api private + */ + +function coerce(val) { + if (val instanceof Error) return val.stack || val.message; + return val; +} + +},{"ms":35}],16:[function(_dereq_,module,exports){ + +module.exports = _dereq_('./lib/'); + +},{"./lib/":17}],17:[function(_dereq_,module,exports){ + +module.exports = _dereq_('./socket'); + +/** + * Exports parser + * + * @api public + * + */ +module.exports.parser = _dereq_('engine.io-parser'); + +},{"./socket":18,"engine.io-parser":27}],18:[function(_dereq_,module,exports){ +(function (global){ +/** + * Module dependencies. + */ + +var transports = _dereq_('./transports'); +var Emitter = _dereq_('component-emitter'); +var debug = _dereq_('debug')('engine.io-client:socket'); +var index = _dereq_('indexof'); +var parser = _dereq_('engine.io-parser'); +var parseuri = _dereq_('parseuri'); +var parsejson = _dereq_('parsejson'); +var parseqs = _dereq_('parseqs'); + +/** + * Module exports. + */ + +module.exports = Socket; + +/** + * Noop function. + * + * @api private + */ + +function noop(){} + +/** + * Socket constructor. + * + * @param {String|Object} uri or options + * @param {Object} options + * @api public + */ + +function Socket(uri, opts){ + if (!(this instanceof Socket)) return new Socket(uri, opts); + + opts = opts || {}; + + if (uri && 'object' == typeof uri) { + opts = uri; + uri = null; + } + + if (uri) { + uri = parseuri(uri); + opts.hostname = uri.host; + opts.secure = uri.protocol == 'https' || uri.protocol == 'wss'; + opts.port = uri.port; + if (uri.query) opts.query = uri.query; + } else if (opts.host) { + opts.hostname = parseuri(opts.host).host; + } + + this.secure = null != opts.secure ? opts.secure : + (global.location && 'https:' == location.protocol); + + if (opts.hostname && !opts.port) { + // if no port is specified manually, use the protocol default + opts.port = this.secure ? '443' : '80'; + } + + this.agent = opts.agent || false; + this.hostname = opts.hostname || + (global.location ? location.hostname : 'localhost'); + this.port = opts.port || (global.location && location.port ? + location.port : + (this.secure ? 443 : 80)); + this.query = opts.query || {}; + if ('string' == typeof this.query) this.query = parseqs.decode(this.query); + this.upgrade = false !== opts.upgrade; + this.path = (opts.path || '/engine.io').replace(/\/$/, '') + '/'; + this.forceJSONP = !!opts.forceJSONP; + this.jsonp = false !== opts.jsonp; + this.forceBase64 = !!opts.forceBase64; + this.enablesXDR = !!opts.enablesXDR; + this.timestampParam = opts.timestampParam || 't'; + this.timestampRequests = opts.timestampRequests; + this.transports = opts.transports || ['polling', 'websocket']; + this.readyState = ''; + this.writeBuffer = []; + this.policyPort = opts.policyPort || 843; + this.rememberUpgrade = opts.rememberUpgrade || false; + this.binaryType = null; + this.onlyBinaryUpgrades = opts.onlyBinaryUpgrades; + this.perMessageDeflate = false !== opts.perMessageDeflate ? (opts.perMessageDeflate || {}) : false; + + if (true === this.perMessageDeflate) this.perMessageDeflate = {}; + if (this.perMessageDeflate && null == this.perMessageDeflate.threshold) { + this.perMessageDeflate.threshold = 1024; + } + + // SSL options for Node.js client + this.pfx = opts.pfx || null; + this.key = opts.key || null; + this.passphrase = opts.passphrase || null; + this.cert = opts.cert || null; + this.ca = opts.ca || null; + this.ciphers = opts.ciphers || null; + this.rejectUnauthorized = opts.rejectUnauthorized === undefined ? true : opts.rejectUnauthorized; + + // other options for Node.js client + var freeGlobal = typeof global == 'object' && global; + if (freeGlobal.global === freeGlobal) { + if (opts.extraHeaders && Object.keys(opts.extraHeaders).length > 0) { + this.extraHeaders = opts.extraHeaders; + } + } + + this.open(); +} + +Socket.priorWebsocketSuccess = false; + +/** + * Mix in `Emitter`. + */ + +Emitter(Socket.prototype); + +/** + * Protocol version. + * + * @api public + */ + +Socket.protocol = parser.protocol; // this is an int + +/** + * Expose deps for legacy compatibility + * and standalone browser access. + */ + +Socket.Socket = Socket; +Socket.Transport = _dereq_('./transport'); +Socket.transports = _dereq_('./transports'); +Socket.parser = _dereq_('engine.io-parser'); + +/** + * Creates transport of the given type. + * + * @param {String} transport name + * @return {Transport} + * @api private + */ + +Socket.prototype.createTransport = function (name) { + debug('creating transport "%s"', name); + var query = clone(this.query); + + // append engine.io protocol identifier + query.EIO = parser.protocol; + + // transport name + query.transport = name; + + // session id if we already have one + if (this.id) query.sid = this.id; + + var transport = new transports[name]({ + agent: this.agent, + hostname: this.hostname, + port: this.port, + secure: this.secure, + path: this.path, + query: query, + forceJSONP: this.forceJSONP, + jsonp: this.jsonp, + forceBase64: this.forceBase64, + enablesXDR: this.enablesXDR, + timestampRequests: this.timestampRequests, + timestampParam: this.timestampParam, + policyPort: this.policyPort, + socket: this, + pfx: this.pfx, + key: this.key, + passphrase: this.passphrase, + cert: this.cert, + ca: this.ca, + ciphers: this.ciphers, + rejectUnauthorized: this.rejectUnauthorized, + perMessageDeflate: this.perMessageDeflate, + extraHeaders: this.extraHeaders + }); + + return transport; +}; + +function clone (obj) { + var o = {}; + for (var i in obj) { + if (obj.hasOwnProperty(i)) { + o[i] = obj[i]; + } + } + return o; +} + +/** + * Initializes transport to use and starts probe. + * + * @api private + */ +Socket.prototype.open = function () { + var transport; + if (this.rememberUpgrade && Socket.priorWebsocketSuccess && this.transports.indexOf('websocket') != -1) { + transport = 'websocket'; + } else if (0 === this.transports.length) { + // Emit error on next tick so it can be listened to + var self = this; + setTimeout(function() { + self.emit('error', 'No transports available'); + }, 0); + return; + } else { + transport = this.transports[0]; + } + this.readyState = 'opening'; + + // Retry with the next transport if the transport is disabled (jsonp: false) + try { + transport = this.createTransport(transport); + } catch (e) { + this.transports.shift(); + this.open(); + return; + } + + transport.open(); + this.setTransport(transport); +}; + +/** + * Sets the current transport. Disables the existing one (if any). + * + * @api private + */ + +Socket.prototype.setTransport = function(transport){ + debug('setting transport %s', transport.name); + var self = this; + + if (this.transport) { + debug('clearing existing transport %s', this.transport.name); + this.transport.removeAllListeners(); + } + + // set up transport + this.transport = transport; + + // set up transport listeners + transport + .on('drain', function(){ + self.onDrain(); + }) + .on('packet', function(packet){ + self.onPacket(packet); + }) + .on('error', function(e){ + self.onError(e); + }) + .on('close', function(){ + self.onClose('transport close'); + }); +}; + +/** + * Probes a transport. + * + * @param {String} transport name + * @api private + */ + +Socket.prototype.probe = function (name) { + debug('probing transport "%s"', name); + var transport = this.createTransport(name, { probe: 1 }) + , failed = false + , self = this; + + Socket.priorWebsocketSuccess = false; + + function onTransportOpen(){ + if (self.onlyBinaryUpgrades) { + var upgradeLosesBinary = !this.supportsBinary && self.transport.supportsBinary; + failed = failed || upgradeLosesBinary; + } + if (failed) return; + + debug('probe transport "%s" opened', name); + transport.send([{ type: 'ping', data: 'probe' }]); + transport.once('packet', function (msg) { + if (failed) return; + if ('pong' == msg.type && 'probe' == msg.data) { + debug('probe transport "%s" pong', name); + self.upgrading = true; + self.emit('upgrading', transport); + if (!transport) return; + Socket.priorWebsocketSuccess = 'websocket' == transport.name; + + debug('pausing current transport "%s"', self.transport.name); + self.transport.pause(function () { + if (failed) return; + if ('closed' == self.readyState) return; + debug('changing transport and sending upgrade packet'); + + cleanup(); + + self.setTransport(transport); + transport.send([{ type: 'upgrade' }]); + self.emit('upgrade', transport); + transport = null; + self.upgrading = false; + self.flush(); + }); + } else { + debug('probe transport "%s" failed', name); + var err = new Error('probe error'); + err.transport = transport.name; + self.emit('upgradeError', err); + } + }); + } + + function freezeTransport() { + if (failed) return; + + // Any callback called by transport should be ignored since now + failed = true; + + cleanup(); + + transport.close(); + transport = null; + } + + //Handle any error that happens while probing + function onerror(err) { + var error = new Error('probe error: ' + err); + error.transport = transport.name; + + freezeTransport(); + + debug('probe transport "%s" failed because of error: %s', name, err); + + self.emit('upgradeError', error); + } + + function onTransportClose(){ + onerror("transport closed"); + } + + //When the socket is closed while we're probing + function onclose(){ + onerror("socket closed"); + } + + //When the socket is upgraded while we're probing + function onupgrade(to){ + if (transport && to.name != transport.name) { + debug('"%s" works - aborting "%s"', to.name, transport.name); + freezeTransport(); + } + } + + //Remove all listeners on the transport and on self + function cleanup(){ + transport.removeListener('open', onTransportOpen); + transport.removeListener('error', onerror); + transport.removeListener('close', onTransportClose); + self.removeListener('close', onclose); + self.removeListener('upgrading', onupgrade); + } + + transport.once('open', onTransportOpen); + transport.once('error', onerror); + transport.once('close', onTransportClose); + + this.once('close', onclose); + this.once('upgrading', onupgrade); + + transport.open(); + +}; + +/** + * Called when connection is deemed open. + * + * @api public + */ + +Socket.prototype.onOpen = function () { + debug('socket open'); + this.readyState = 'open'; + Socket.priorWebsocketSuccess = 'websocket' == this.transport.name; + this.emit('open'); + this.flush(); + + // we check for `readyState` in case an `open` + // listener already closed the socket + if ('open' == this.readyState && this.upgrade && this.transport.pause) { + debug('starting upgrade probes'); + for (var i = 0, l = this.upgrades.length; i < l; i++) { + this.probe(this.upgrades[i]); + } + } +}; + +/** + * Handles a packet. + * + * @api private + */ + +Socket.prototype.onPacket = function (packet) { + if ('opening' == this.readyState || 'open' == this.readyState) { + debug('socket receive: type "%s", data "%s"', packet.type, packet.data); + + this.emit('packet', packet); + + // Socket is live - any packet counts + this.emit('heartbeat'); + + switch (packet.type) { + case 'open': + this.onHandshake(parsejson(packet.data)); + break; + + case 'pong': + this.setPing(); + this.emit('pong'); + break; + + case 'error': + var err = new Error('server error'); + err.code = packet.data; + this.onError(err); + break; + + case 'message': + this.emit('data', packet.data); + this.emit('message', packet.data); + break; + } + } else { + debug('packet received with socket readyState "%s"', this.readyState); + } +}; + +/** + * Called upon handshake completion. + * + * @param {Object} handshake obj + * @api private + */ + +Socket.prototype.onHandshake = function (data) { + this.emit('handshake', data); + this.id = data.sid; + this.transport.query.sid = data.sid; + this.upgrades = this.filterUpgrades(data.upgrades); + this.pingInterval = data.pingInterval; + this.pingTimeout = data.pingTimeout; + this.onOpen(); + // In case open handler closes socket + if ('closed' == this.readyState) return; + this.setPing(); + + // Prolong liveness of socket on heartbeat + this.removeListener('heartbeat', this.onHeartbeat); + this.on('heartbeat', this.onHeartbeat); +}; + +/** + * Resets ping timeout. + * + * @api private + */ + +Socket.prototype.onHeartbeat = function (timeout) { + clearTimeout(this.pingTimeoutTimer); + var self = this; + self.pingTimeoutTimer = setTimeout(function () { + if ('closed' == self.readyState) return; + self.onClose('ping timeout'); + }, timeout || (self.pingInterval + self.pingTimeout)); +}; + +/** + * Pings server every `this.pingInterval` and expects response + * within `this.pingTimeout` or closes connection. + * + * @api private + */ + +Socket.prototype.setPing = function () { + var self = this; + clearTimeout(self.pingIntervalTimer); + self.pingIntervalTimer = setTimeout(function () { + debug('writing ping packet - expecting pong within %sms', self.pingTimeout); + self.ping(); + self.onHeartbeat(self.pingTimeout); + }, self.pingInterval); +}; + +/** +* Sends a ping packet. +* +* @api private +*/ + +Socket.prototype.ping = function () { + var self = this; + this.sendPacket('ping', function(){ + self.emit('ping'); + }); +}; + +/** + * Called on `drain` event + * + * @api private + */ + +Socket.prototype.onDrain = function() { + this.writeBuffer.splice(0, this.prevBufferLen); + + // setting prevBufferLen = 0 is very important + // for example, when upgrading, upgrade packet is sent over, + // and a nonzero prevBufferLen could cause problems on `drain` + this.prevBufferLen = 0; + + if (0 === this.writeBuffer.length) { + this.emit('drain'); + } else { + this.flush(); + } +}; + +/** + * Flush write buffers. + * + * @api private + */ + +Socket.prototype.flush = function () { + if ('closed' != this.readyState && this.transport.writable && + !this.upgrading && this.writeBuffer.length) { + debug('flushing %d packets in socket', this.writeBuffer.length); + this.transport.send(this.writeBuffer); + // keep track of current length of writeBuffer + // splice writeBuffer and callbackBuffer on `drain` + this.prevBufferLen = this.writeBuffer.length; + this.emit('flush'); + } +}; + +/** + * Sends a message. + * + * @param {String} message. + * @param {Function} callback function. + * @param {Object} options. + * @return {Socket} for chaining. + * @api public + */ + +Socket.prototype.write = +Socket.prototype.send = function (msg, options, fn) { + this.sendPacket('message', msg, options, fn); + return this; +}; + +/** + * Sends a packet. + * + * @param {String} packet type. + * @param {String} data. + * @param {Object} options. + * @param {Function} callback function. + * @api private + */ + +Socket.prototype.sendPacket = function (type, data, options, fn) { + if('function' == typeof data) { + fn = data; + data = undefined; + } + + if ('function' == typeof options) { + fn = options; + options = null; + } + + if ('closing' == this.readyState || 'closed' == this.readyState) { + return; + } + + options = options || {}; + options.compress = false !== options.compress; + + var packet = { + type: type, + data: data, + options: options + }; + this.emit('packetCreate', packet); + this.writeBuffer.push(packet); + if (fn) this.once('flush', fn); + this.flush(); +}; + +/** + * Closes the connection. + * + * @api private + */ + +Socket.prototype.close = function () { + if ('opening' == this.readyState || 'open' == this.readyState) { + this.readyState = 'closing'; + + var self = this; + + if (this.writeBuffer.length) { + this.once('drain', function() { + if (this.upgrading) { + waitForUpgrade(); + } else { + close(); + } + }); + } else if (this.upgrading) { + waitForUpgrade(); + } else { + close(); + } + } + + function close() { + self.onClose('forced close'); + debug('socket closing - telling transport to close'); + self.transport.close(); + } + + function cleanupAndClose() { + self.removeListener('upgrade', cleanupAndClose); + self.removeListener('upgradeError', cleanupAndClose); + close(); + } + + function waitForUpgrade() { + // wait for upgrade to finish since we can't send packets while pausing a transport + self.once('upgrade', cleanupAndClose); + self.once('upgradeError', cleanupAndClose); + } + + return this; +}; + +/** + * Called upon transport error + * + * @api private + */ + +Socket.prototype.onError = function (err) { + debug('socket error %j', err); + Socket.priorWebsocketSuccess = false; + this.emit('error', err); + this.onClose('transport error', err); +}; + +/** + * Called upon transport close. + * + * @api private + */ + +Socket.prototype.onClose = function (reason, desc) { + if ('opening' == this.readyState || 'open' == this.readyState || 'closing' == this.readyState) { + debug('socket close with reason: "%s"', reason); + var self = this; + + // clear timers + clearTimeout(this.pingIntervalTimer); + clearTimeout(this.pingTimeoutTimer); + + // stop event from firing again for transport + this.transport.removeAllListeners('close'); + + // ensure transport won't stay open + this.transport.close(); + + // ignore further transport communication + this.transport.removeAllListeners(); + + // set ready state + this.readyState = 'closed'; + + // clear session id + this.id = null; + + // emit close event + this.emit('close', reason, desc); + + // clean buffers after, so users can still + // grab the buffers on `close` event + self.writeBuffer = []; + self.prevBufferLen = 0; + } +}; + +/** + * Filters upgrades, returning only those matching client transports. + * + * @param {Array} server upgrades + * @api private + * + */ + +Socket.prototype.filterUpgrades = function (upgrades) { + var filteredUpgrades = []; + for (var i = 0, j = upgrades.length; i<j; i++) { + if (~index(this.transports, upgrades[i])) filteredUpgrades.push(upgrades[i]); + } + return filteredUpgrades; +}; + +}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {}) +},{"./transport":19,"./transports":20,"component-emitter":26,"debug":14,"engine.io-parser":27,"indexof":32,"parsejson":36,"parseqs":37,"parseuri":38}],19:[function(_dereq_,module,exports){ +/** + * Module dependencies. + */ + +var parser = _dereq_('engine.io-parser'); +var Emitter = _dereq_('component-emitter'); + +/** + * Module exports. + */ + +module.exports = Transport; + +/** + * Transport abstract constructor. + * + * @param {Object} options. + * @api private + */ + +function Transport (opts) { + this.path = opts.path; + this.hostname = opts.hostname; + this.port = opts.port; + this.secure = opts.secure; + this.query = opts.query; + this.timestampParam = opts.timestampParam; + this.timestampRequests = opts.timestampRequests; + this.readyState = ''; + this.agent = opts.agent || false; + this.socket = opts.socket; + this.enablesXDR = opts.enablesXDR; + + // SSL options for Node.js client + this.pfx = opts.pfx; + this.key = opts.key; + this.passphrase = opts.passphrase; + this.cert = opts.cert; + this.ca = opts.ca; + this.ciphers = opts.ciphers; + this.rejectUnauthorized = opts.rejectUnauthorized; + + // other options for Node.js client + this.extraHeaders = opts.extraHeaders; +} + +/** + * Mix in `Emitter`. + */ + +Emitter(Transport.prototype); + +/** + * Emits an error. + * + * @param {String} str + * @return {Transport} for chaining + * @api public + */ + +Transport.prototype.onError = function (msg, desc) { + var err = new Error(msg); + err.type = 'TransportError'; + err.description = desc; + this.emit('error', err); + return this; +}; + +/** + * Opens the transport. + * + * @api public + */ + +Transport.prototype.open = function () { + if ('closed' == this.readyState || '' == this.readyState) { + this.readyState = 'opening'; + this.doOpen(); + } + + return this; +}; + +/** + * Closes the transport. + * + * @api private + */ + +Transport.prototype.close = function () { + if ('opening' == this.readyState || 'open' == this.readyState) { + this.doClose(); + this.onClose(); + } + + return this; +}; + +/** + * Sends multiple packets. + * + * @param {Array} packets + * @api private + */ + +Transport.prototype.send = function(packets){ + if ('open' == this.readyState) { + this.write(packets); + } else { + throw new Error('Transport not open'); + } +}; + +/** + * Called upon open + * + * @api private + */ + +Transport.prototype.onOpen = function () { + this.readyState = 'open'; + this.writable = true; + this.emit('open'); +}; + +/** + * Called with data. + * + * @param {String} data + * @api private + */ + +Transport.prototype.onData = function(data){ + var packet = parser.decodePacket(data, this.socket.binaryType); + this.onPacket(packet); +}; + +/** + * Called with a decoded packet. + */ + +Transport.prototype.onPacket = function (packet) { + this.emit('packet', packet); +}; + +/** + * Called upon close. + * + * @api private + */ + +Transport.prototype.onClose = function () { + this.readyState = 'closed'; + this.emit('close'); +}; + +},{"component-emitter":26,"engine.io-parser":27}],20:[function(_dereq_,module,exports){ +(function (global){ +/** + * Module dependencies + */ + +var XMLHttpRequest = _dereq_('xmlhttprequest-ssl'); +var XHR = _dereq_('./polling-xhr'); +var JSONP = _dereq_('./polling-jsonp'); +var websocket = _dereq_('./websocket'); + +/** + * Export transports. + */ + +exports.polling = polling; +exports.websocket = websocket; + +/** + * Polling transport polymorphic constructor. + * Decides on xhr vs jsonp based on feature detection. + * + * @api private + */ + +function polling(opts){ + var xhr; + var xd = false; + var xs = false; + var jsonp = false !== opts.jsonp; + + if (global.location) { + var isSSL = 'https:' == location.protocol; + var port = location.port; + + // some user agents have empty `location.port` + if (!port) { + port = isSSL ? 443 : 80; + } + + xd = opts.hostname != location.hostname || port != opts.port; + xs = opts.secure != isSSL; + } + + opts.xdomain = xd; + opts.xscheme = xs; + xhr = new XMLHttpRequest(opts); + + if ('open' in xhr && !opts.forceJSONP) { + return new XHR(opts); + } else { + if (!jsonp) throw new Error('JSONP disabled'); + return new JSONP(opts); + } +} + +}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {}) +},{"./polling-jsonp":21,"./polling-xhr":22,"./websocket":24,"xmlhttprequest-ssl":25}],21:[function(_dereq_,module,exports){ +(function (global){ + +/** + * Module requirements. + */ + +var Polling = _dereq_('./polling'); +var inherit = _dereq_('component-inherit'); + +/** + * Module exports. + */ + +module.exports = JSONPPolling; + +/** + * Cached regular expressions. + */ + +var rNewline = /\n/g; +var rEscapedNewline = /\\n/g; + +/** + * Global JSONP callbacks. + */ + +var callbacks; + +/** + * Callbacks count. + */ + +var index = 0; + +/** + * Noop. + */ + +function empty () { } + +/** + * JSONP Polling constructor. + * + * @param {Object} opts. + * @api public + */ + +function JSONPPolling (opts) { + Polling.call(this, opts); + + this.query = this.query || {}; + + // define global callbacks array if not present + // we do this here (lazily) to avoid unneeded global pollution + if (!callbacks) { + // we need to consider multiple engines in the same page + if (!global.___eio) global.___eio = []; + callbacks = global.___eio; + } + + // callback identifier + this.index = callbacks.length; + + // add callback to jsonp global + var self = this; + callbacks.push(function (msg) { + self.onData(msg); + }); + + // append to query string + this.query.j = this.index; + + // prevent spurious errors from being emitted when the window is unloaded + if (global.document && global.addEventListener) { + global.addEventListener('beforeunload', function () { + if (self.script) self.script.onerror = empty; + }, false); + } +} + +/** + * Inherits from Polling. + */ + +inherit(JSONPPolling, Polling); + +/* + * JSONP only supports binary as base64 encoded strings + */ + +JSONPPolling.prototype.supportsBinary = false; + +/** + * Closes the socket. + * + * @api private + */ + +JSONPPolling.prototype.doClose = function () { + if (this.script) { + this.script.parentNode.removeChild(this.script); + this.script = null; + } + + if (this.form) { + this.form.parentNode.removeChild(this.form); + this.form = null; + this.iframe = null; + } + + Polling.prototype.doClose.call(this); +}; + +/** + * Starts a poll cycle. + * + * @api private + */ + +JSONPPolling.prototype.doPoll = function () { + var self = this; + var script = document.createElement('script'); + + if (this.script) { + this.script.parentNode.removeChild(this.script); + this.script = null; + } + + script.async = true; + script.src = this.uri(); + script.onerror = function(e){ + self.onError('jsonp poll error',e); + }; + + var insertAt = document.getElementsByTagName('script')[0]; + if (insertAt) { + insertAt.parentNode.insertBefore(script, insertAt); + } + else { + (document.head || document.body).appendChild(script); + } + this.script = script; + + var isUAgecko = 'undefined' != typeof navigator && /gecko/i.test(navigator.userAgent); + + if (isUAgecko) { + setTimeout(function () { + var iframe = document.createElement('iframe'); + document.body.appendChild(iframe); + document.body.removeChild(iframe); + }, 100); + } +}; + +/** + * Writes with a hidden iframe. + * + * @param {String} data to send + * @param {Function} called upon flush. + * @api private + */ + +JSONPPolling.prototype.doWrite = function (data, fn) { + var self = this; + + if (!this.form) { + var form = document.createElement('form'); + var area = document.createElement('textarea'); + var id = this.iframeId = 'eio_iframe_' + this.index; + var iframe; + + form.className = 'socketio'; + form.style.position = 'absolute'; + form.style.top = '-1000px'; + form.style.left = '-1000px'; + form.target = id; + form.method = 'POST'; + form.setAttribute('accept-charset', 'utf-8'); + area.name = 'd'; + form.appendChild(area); + document.body.appendChild(form); + + this.form = form; + this.area = area; + } + + this.form.action = this.uri(); + + function complete () { + initIframe(); + fn(); + } + + function initIframe () { + if (self.iframe) { + try { + self.form.removeChild(self.iframe); + } catch (e) { + self.onError('jsonp polling iframe removal error', e); + } + } + + try { + // ie6 dynamic iframes with target="" support (thanks Chris Lambacher) + var html = '<iframe src="javascript:0" name="'+ self.iframeId +'">'; + iframe = document.createElement(html); + } catch (e) { + iframe = document.createElement('iframe'); + iframe.name = self.iframeId; + iframe.src = 'javascript:0'; + } + + iframe.id = self.iframeId; + + self.form.appendChild(iframe); + self.iframe = iframe; + } + + initIframe(); + + // escape \n to prevent it from being converted into \r\n by some UAs + // double escaping is required for escaped new lines because unescaping of new lines can be done safely on server-side + data = data.replace(rEscapedNewline, '\\\n'); + this.area.value = data.replace(rNewline, '\\n'); + + try { + this.form.submit(); + } catch(e) {} + + if (this.iframe.attachEvent) { + this.iframe.onreadystatechange = function(){ + if (self.iframe.readyState == 'complete') { + complete(); + } + }; + } else { + this.iframe.onload = complete; + } +}; + +}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {}) +},{"./polling":23,"component-inherit":13}],22:[function(_dereq_,module,exports){ +(function (global){ +/** + * Module requirements. + */ + +var XMLHttpRequest = _dereq_('xmlhttprequest-ssl'); +var Polling = _dereq_('./polling'); +var Emitter = _dereq_('component-emitter'); +var inherit = _dereq_('component-inherit'); +var debug = _dereq_('debug')('engine.io-client:polling-xhr'); + +/** + * Module exports. + */ + +module.exports = XHR; +module.exports.Request = Request; + +/** + * Empty function + */ + +function empty(){} + +/** + * XHR Polling constructor. + * + * @param {Object} opts + * @api public + */ + +function XHR(opts){ + Polling.call(this, opts); + + if (global.location) { + var isSSL = 'https:' == location.protocol; + var port = location.port; + + // some user agents have empty `location.port` + if (!port) { + port = isSSL ? 443 : 80; + } + + this.xd = opts.hostname != global.location.hostname || + port != opts.port; + this.xs = opts.secure != isSSL; + } else { + this.extraHeaders = opts.extraHeaders; + } +} + +/** + * Inherits from Polling. + */ + +inherit(XHR, Polling); + +/** + * XHR supports binary + */ + +XHR.prototype.supportsBinary = true; + +/** + * Creates a request. + * + * @param {String} method + * @api private + */ + +XHR.prototype.request = function(opts){ + opts = opts || {}; + opts.uri = this.uri(); + opts.xd = this.xd; + opts.xs = this.xs; + opts.agent = this.agent || false; + opts.supportsBinary = this.supportsBinary; + opts.enablesXDR = this.enablesXDR; + + // SSL options for Node.js client + opts.pfx = this.pfx; + opts.key = this.key; + opts.passphrase = this.passphrase; + opts.cert = this.cert; + opts.ca = this.ca; + opts.ciphers = this.ciphers; + opts.rejectUnauthorized = this.rejectUnauthorized; + + // other options for Node.js client + opts.extraHeaders = this.extraHeaders; + + return new Request(opts); +}; + +/** + * Sends data. + * + * @param {String} data to send. + * @param {Function} called upon flush. + * @api private + */ + +XHR.prototype.doWrite = function(data, fn){ + var isBinary = typeof data !== 'string' && data !== undefined; + var req = this.request({ method: 'POST', data: data, isBinary: isBinary }); + var self = this; + req.on('success', fn); + req.on('error', function(err){ + self.onError('xhr post error', err); + }); + this.sendXhr = req; +}; + +/** + * Starts a poll cycle. + * + * @api private + */ + +XHR.prototype.doPoll = function(){ + debug('xhr poll'); + var req = this.request(); + var self = this; + req.on('data', function(data){ + self.onData(data); + }); + req.on('error', function(err){ + self.onError('xhr poll error', err); + }); + this.pollXhr = req; +}; + +/** + * Request constructor + * + * @param {Object} options + * @api public + */ + +function Request(opts){ + this.method = opts.method || 'GET'; + this.uri = opts.uri; + this.xd = !!opts.xd; + this.xs = !!opts.xs; + this.async = false !== opts.async; + this.data = undefined != opts.data ? opts.data : null; + this.agent = opts.agent; + this.isBinary = opts.isBinary; + this.supportsBinary = opts.supportsBinary; + this.enablesXDR = opts.enablesXDR; + + // SSL options for Node.js client + this.pfx = opts.pfx; + this.key = opts.key; + this.passphrase = opts.passphrase; + this.cert = opts.cert; + this.ca = opts.ca; + this.ciphers = opts.ciphers; + this.rejectUnauthorized = opts.rejectUnauthorized; + + // other options for Node.js client + this.extraHeaders = opts.extraHeaders; + + this.create(); +} + +/** + * Mix in `Emitter`. + */ + +Emitter(Request.prototype); + +/** + * Creates the XHR object and sends the request. + * + * @api private + */ + +Request.prototype.create = function(){ + var opts = { agent: this.agent, xdomain: this.xd, xscheme: this.xs, enablesXDR: this.enablesXDR }; + + // SSL options for Node.js client + opts.pfx = this.pfx; + opts.key = this.key; + opts.passphrase = this.passphrase; + opts.cert = this.cert; + opts.ca = this.ca; + opts.ciphers = this.ciphers; + opts.rejectUnauthorized = this.rejectUnauthorized; + + var xhr = this.xhr = new XMLHttpRequest(opts); + var self = this; + + try { + debug('xhr open %s: %s', this.method, this.uri); + xhr.open(this.method, this.uri, this.async); + try { + if (this.extraHeaders) { + xhr.setDisableHeaderCheck(true); + for (var i in this.extraHeaders) { + if (this.extraHeaders.hasOwnProperty(i)) { + xhr.setRequestHeader(i, this.extraHeaders[i]); + } + } + } + } catch (e) {} + if (this.supportsBinary) { + // This has to be done after open because Firefox is stupid + // http://stackoverflow.com/questions/13216903/get-binary-data-with-xmlhttprequest-in-a-firefox-extension + xhr.responseType = 'arraybuffer'; + } + + if ('POST' == this.method) { + try { + if (this.isBinary) { + xhr.setRequestHeader('Content-type', 'application/octet-stream'); + } else { + xhr.setRequestHeader('Content-type', 'text/plain;charset=UTF-8'); + } + } catch (e) {} + } + + // ie6 check + if ('withCredentials' in xhr) { + xhr.withCredentials = true; + } + + if (this.hasXDR()) { + xhr.onload = function(){ + self.onLoad(); + }; + xhr.onerror = function(){ + self.onError(xhr.responseText); + }; + } else { + xhr.onreadystatechange = function(){ + if (4 != xhr.readyState) return; + if (200 == xhr.status || 1223 == xhr.status) { + self.onLoad(); + } else { + // make sure the `error` event handler that's user-set + // does not throw in the same tick and gets caught here + setTimeout(function(){ + self.onError(xhr.status); + }, 0); + } + }; + } + + debug('xhr data %s', this.data); + xhr.send(this.data); + } catch (e) { + // Need to defer since .create() is called directly fhrom the constructor + // and thus the 'error' event can only be only bound *after* this exception + // occurs. Therefore, also, we cannot throw here at all. + setTimeout(function() { + self.onError(e); + }, 0); + return; + } + + if (global.document) { + this.index = Request.requestsCount++; + Request.requests[this.index] = this; + } +}; + +/** + * Called upon successful response. + * + * @api private + */ + +Request.prototype.onSuccess = function(){ + this.emit('success'); + this.cleanup(); +}; + +/** + * Called if we have data. + * + * @api private + */ + +Request.prototype.onData = function(data){ + this.emit('data', data); + this.onSuccess(); +}; + +/** + * Called upon error. + * + * @api private + */ + +Request.prototype.onError = function(err){ + this.emit('error', err); + this.cleanup(true); +}; + +/** + * Cleans up house. + * + * @api private + */ + +Request.prototype.cleanup = function(fromError){ + if ('undefined' == typeof this.xhr || null === this.xhr) { + return; + } + // xmlhttprequest + if (this.hasXDR()) { + this.xhr.onload = this.xhr.onerror = empty; + } else { + this.xhr.onreadystatechange = empty; + } + + if (fromError) { + try { + this.xhr.abort(); + } catch(e) {} + } + + if (global.document) { + delete Request.requests[this.index]; + } + + this.xhr = null; +}; + +/** + * Called upon load. + * + * @api private + */ + +Request.prototype.onLoad = function(){ + var data; + try { + var contentType; + try { + contentType = this.xhr.getResponseHeader('Content-Type').split(';')[0]; + } catch (e) {} + if (contentType === 'application/octet-stream') { + data = this.xhr.response; + } else { + if (!this.supportsBinary) { + data = this.xhr.responseText; + } else { + try { + data = String.fromCharCode.apply(null, new Uint8Array(this.xhr.response)); + } catch (e) { + var ui8Arr = new Uint8Array(this.xhr.response); + var dataArray = []; + for (var idx = 0, length = ui8Arr.length; idx < length; idx++) { + dataArray.push(ui8Arr[idx]); + } + + data = String.fromCharCode.apply(null, dataArray); + } + } + } + } catch (e) { + this.onError(e); + } + if (null != data) { + this.onData(data); + } +}; + +/** + * Check if it has XDomainRequest. + * + * @api private + */ + +Request.prototype.hasXDR = function(){ + return 'undefined' !== typeof global.XDomainRequest && !this.xs && this.enablesXDR; +}; + +/** + * Aborts the request. + * + * @api public + */ + +Request.prototype.abort = function(){ + this.cleanup(); +}; + +/** + * Aborts pending requests when unloading the window. This is needed to prevent + * memory leaks (e.g. when using IE) and to ensure that no spurious error is + * emitted. + */ + +if (global.document) { + Request.requestsCount = 0; + Request.requests = {}; + if (global.attachEvent) { + global.attachEvent('onunload', unloadHandler); + } else if (global.addEventListener) { + global.addEventListener('beforeunload', unloadHandler, false); + } +} + +function unloadHandler() { + for (var i in Request.requests) { + if (Request.requests.hasOwnProperty(i)) { + Request.requests[i].abort(); + } + } +} + +}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {}) +},{"./polling":23,"component-emitter":26,"component-inherit":13,"debug":14,"xmlhttprequest-ssl":25}],23:[function(_dereq_,module,exports){ +/** + * Module dependencies. + */ + +var Transport = _dereq_('../transport'); +var parseqs = _dereq_('parseqs'); +var parser = _dereq_('engine.io-parser'); +var inherit = _dereq_('component-inherit'); +var yeast = _dereq_('yeast'); +var debug = _dereq_('debug')('engine.io-client:polling'); + +/** + * Module exports. + */ + +module.exports = Polling; + +/** + * Is XHR2 supported? + */ + +var hasXHR2 = (function() { + var XMLHttpRequest = _dereq_('xmlhttprequest-ssl'); + var xhr = new XMLHttpRequest({ xdomain: false }); + return null != xhr.responseType; +})(); + +/** + * Polling interface. + * + * @param {Object} opts + * @api private + */ + +function Polling(opts){ + var forceBase64 = (opts && opts.forceBase64); + if (!hasXHR2 || forceBase64) { + this.supportsBinary = false; + } + Transport.call(this, opts); +} + +/** + * Inherits from Transport. + */ + +inherit(Polling, Transport); + +/** + * Transport name. + */ + +Polling.prototype.name = 'polling'; + +/** + * Opens the socket (triggers polling). We write a PING message to determine + * when the transport is open. + * + * @api private + */ + +Polling.prototype.doOpen = function(){ + this.poll(); +}; + +/** + * Pauses polling. + * + * @param {Function} callback upon buffers are flushed and transport is paused + * @api private + */ + +Polling.prototype.pause = function(onPause){ + var pending = 0; + var self = this; + + this.readyState = 'pausing'; + + function pause(){ + debug('paused'); + self.readyState = 'paused'; + onPause(); + } + + if (this.polling || !this.writable) { + var total = 0; + + if (this.polling) { + debug('we are currently polling - waiting to pause'); + total++; + this.once('pollComplete', function(){ + debug('pre-pause polling complete'); + --total || pause(); + }); + } + + if (!this.writable) { + debug('we are currently writing - waiting to pause'); + total++; + this.once('drain', function(){ + debug('pre-pause writing complete'); + --total || pause(); + }); + } + } else { + pause(); + } +}; + +/** + * Starts polling cycle. + * + * @api public + */ + +Polling.prototype.poll = function(){ + debug('polling'); + this.polling = true; + this.doPoll(); + this.emit('poll'); +}; + +/** + * Overloads onData to detect payloads. + * + * @api private + */ + +Polling.prototype.onData = function(data){ + var self = this; + debug('polling got data %s', data); + var callback = function(packet, index, total) { + // if its the first message we consider the transport open + if ('opening' == self.readyState) { + self.onOpen(); + } + + // if its a close packet, we close the ongoing requests + if ('close' == packet.type) { + self.onClose(); + return false; + } + + // otherwise bypass onData and handle the message + self.onPacket(packet); + }; + + // decode payload + parser.decodePayload(data, this.socket.binaryType, callback); + + // if an event did not trigger closing + if ('closed' != this.readyState) { + // if we got data we're not polling + this.polling = false; + this.emit('pollComplete'); + + if ('open' == this.readyState) { + this.poll(); + } else { + debug('ignoring poll - transport state "%s"', this.readyState); + } + } +}; + +/** + * For polling, send a close packet. + * + * @api private + */ + +Polling.prototype.doClose = function(){ + var self = this; + + function close(){ + debug('writing close packet'); + self.write([{ type: 'close' }]); + } + + if ('open' == this.readyState) { + debug('transport open - closing'); + close(); + } else { + // in case we're trying to close while + // handshaking is in progress (GH-164) + debug('transport not open - deferring close'); + this.once('open', close); + } +}; + +/** + * Writes a packets payload. + * + * @param {Array} data packets + * @param {Function} drain callback + * @api private + */ + +Polling.prototype.write = function(packets){ + var self = this; + this.writable = false; + var callbackfn = function() { + self.writable = true; + self.emit('drain'); + }; + + var self = this; + parser.encodePayload(packets, this.supportsBinary, function(data) { + self.doWrite(data, callbackfn); + }); +}; + +/** + * Generates uri for connection. + * + * @api private + */ + +Polling.prototype.uri = function(){ + var query = this.query || {}; + var schema = this.secure ? 'https' : 'http'; + var port = ''; + + // cache busting is forced + if (false !== this.timestampRequests) { + query[this.timestampParam] = yeast(); + } + + if (!this.supportsBinary && !query.sid) { + query.b64 = 1; + } + + query = parseqs.encode(query); + + // avoid port if default for schema + if (this.port && (('https' == schema && this.port != 443) || + ('http' == schema && this.port != 80))) { + port = ':' + this.port; + } + + // prepend ? to query + if (query.length) { + query = '?' + query; + } + + var ipv6 = this.hostname.indexOf(':') !== -1; + return schema + '://' + (ipv6 ? '[' + this.hostname + ']' : this.hostname) + port + this.path + query; +}; + +},{"../transport":19,"component-inherit":13,"debug":14,"engine.io-parser":27,"parseqs":37,"xmlhttprequest-ssl":25,"yeast":45}],24:[function(_dereq_,module,exports){ +(function (global){ +/** + * Module dependencies. + */ + +var Transport = _dereq_('../transport'); +var parser = _dereq_('engine.io-parser'); +var parseqs = _dereq_('parseqs'); +var inherit = _dereq_('component-inherit'); +var yeast = _dereq_('yeast'); +var debug = _dereq_('debug')('engine.io-client:websocket'); +var BrowserWebSocket = global.WebSocket || global.MozWebSocket; + +/** + * Get either the `WebSocket` or `MozWebSocket` globals + * in the browser or try to resolve WebSocket-compatible + * interface exposed by `ws` for Node-like environment. + */ + +var WebSocket = BrowserWebSocket; +if (!WebSocket && typeof window === 'undefined') { + try { + WebSocket = _dereq_('ws'); + } catch (e) { } +} + +/** + * Module exports. + */ + +module.exports = WS; + +/** + * WebSocket transport constructor. + * + * @api {Object} connection options + * @api public + */ + +function WS(opts){ + var forceBase64 = (opts && opts.forceBase64); + if (forceBase64) { + this.supportsBinary = false; + } + this.perMessageDeflate = opts.perMessageDeflate; + Transport.call(this, opts); +} + +/** + * Inherits from Transport. + */ + +inherit(WS, Transport); + +/** + * Transport name. + * + * @api public + */ + +WS.prototype.name = 'websocket'; + +/* + * WebSockets support binary + */ + +WS.prototype.supportsBinary = true; + +/** + * Opens socket. + * + * @api private + */ + +WS.prototype.doOpen = function(){ + if (!this.check()) { + // let probe timeout + return; + } + + var self = this; + var uri = this.uri(); + var protocols = void(0); + var opts = { + agent: this.agent, + perMessageDeflate: this.perMessageDeflate + }; + + // SSL options for Node.js client + opts.pfx = this.pfx; + opts.key = this.key; + opts.passphrase = this.passphrase; + opts.cert = this.cert; + opts.ca = this.ca; + opts.ciphers = this.ciphers; + opts.rejectUnauthorized = this.rejectUnauthorized; + if (this.extraHeaders) { + opts.headers = this.extraHeaders; + } + + this.ws = BrowserWebSocket ? new WebSocket(uri) : new WebSocket(uri, protocols, opts); + + if (this.ws.binaryType === undefined) { + this.supportsBinary = false; + } + + if (this.ws.supports && this.ws.supports.binary) { + this.supportsBinary = true; + this.ws.binaryType = 'buffer'; + } else { + this.ws.binaryType = 'arraybuffer'; + } + + this.addEventListeners(); +}; + +/** + * Adds event listeners to the socket + * + * @api private + */ + +WS.prototype.addEventListeners = function(){ + var self = this; + + this.ws.onopen = function(){ + self.onOpen(); + }; + this.ws.onclose = function(){ + self.onClose(); + }; + this.ws.onmessage = function(ev){ + self.onData(ev.data); + }; + this.ws.onerror = function(e){ + self.onError('websocket error', e); + }; +}; + +/** + * Override `onData` to use a timer on iOS. + * See: https://gist.github.com/mloughran/2052006 + * + * @api private + */ + +if ('undefined' != typeof navigator + && /iPad|iPhone|iPod/i.test(navigator.userAgent)) { + WS.prototype.onData = function(data){ + var self = this; + setTimeout(function(){ + Transport.prototype.onData.call(self, data); + }, 0); + }; +} + +/** + * Writes data to socket. + * + * @param {Array} array of packets. + * @api private + */ + +WS.prototype.write = function(packets){ + var self = this; + this.writable = false; + + // encodePacket efficient as it uses WS framing + // no need for encodePayload + var total = packets.length; + for (var i = 0, l = total; i < l; i++) { + (function(packet) { + parser.encodePacket(packet, self.supportsBinary, function(data) { + if (!BrowserWebSocket) { + // always create a new object (GH-437) + var opts = {}; + if (packet.options) { + opts.compress = packet.options.compress; + } + + if (self.perMessageDeflate) { + var len = 'string' == typeof data ? global.Buffer.byteLength(data) : data.length; + if (len < self.perMessageDeflate.threshold) { + opts.compress = false; + } + } + } + + //Sometimes the websocket has already been closed but the browser didn't + //have a chance of informing us about it yet, in that case send will + //throw an error + try { + if (BrowserWebSocket) { + // TypeError is thrown when passing the second argument on Safari + self.ws.send(data); + } else { + self.ws.send(data, opts); + } + } catch (e){ + debug('websocket closed before onclose event'); + } + + --total || done(); + }); + })(packets[i]); + } + + function done(){ + self.emit('flush'); + + // fake drain + // defer to next tick to allow Socket to clear writeBuffer + setTimeout(function(){ + self.writable = true; + self.emit('drain'); + }, 0); + } +}; + +/** + * Called upon close + * + * @api private + */ + +WS.prototype.onClose = function(){ + Transport.prototype.onClose.call(this); +}; + +/** + * Closes socket. + * + * @api private + */ + +WS.prototype.doClose = function(){ + if (typeof this.ws !== 'undefined') { + this.ws.close(); + } +}; + +/** + * Generates uri for connection. + * + * @api private + */ + +WS.prototype.uri = function(){ + var query = this.query || {}; + var schema = this.secure ? 'wss' : 'ws'; + var port = ''; + + // avoid port if default for schema + if (this.port && (('wss' == schema && this.port != 443) + || ('ws' == schema && this.port != 80))) { + port = ':' + this.port; + } + + // append timestamp to URI + if (this.timestampRequests) { + query[this.timestampParam] = yeast(); + } + + // communicate binary support capabilities + if (!this.supportsBinary) { + query.b64 = 1; + } + + query = parseqs.encode(query); + + // prepend ? to query + if (query.length) { + query = '?' + query; + } + + var ipv6 = this.hostname.indexOf(':') !== -1; + return schema + '://' + (ipv6 ? '[' + this.hostname + ']' : this.hostname) + port + this.path + query; +}; + +/** + * Feature detection for WebSocket. + * + * @return {Boolean} whether this transport is available. + * @api public + */ + +WS.prototype.check = function(){ + return !!WebSocket && !('__initialize' in WebSocket && this.name === WS.prototype.name); +}; + +}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {}) +},{"../transport":19,"component-inherit":13,"debug":14,"engine.io-parser":27,"parseqs":37,"ws":undefined,"yeast":45}],25:[function(_dereq_,module,exports){ +// browser shim for xmlhttprequest module +var hasCORS = _dereq_('has-cors'); + +module.exports = function(opts) { + var xdomain = opts.xdomain; + + // scheme must be same when usign XDomainRequest + // http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx + var xscheme = opts.xscheme; + + // XDomainRequest has a flow of not sending cookie, therefore it should be disabled as a default. + // https://github.com/Automattic/engine.io-client/pull/217 + var enablesXDR = opts.enablesXDR; + + // XMLHttpRequest can be disabled on IE + try { + if ('undefined' != typeof XMLHttpRequest && (!xdomain || hasCORS)) { + return new XMLHttpRequest(); + } + } catch (e) { } + + // Use XDomainRequest for IE8 if enablesXDR is true + // because loading bar keeps flashing when using jsonp-polling + // https://github.com/yujiosaka/socke.io-ie8-loading-example + try { + if ('undefined' != typeof XDomainRequest && !xscheme && enablesXDR) { + return new XDomainRequest(); + } + } catch (e) { } + + if (!xdomain) { + try { + return new ActiveXObject('Microsoft.XMLHTTP'); + } catch(e) { } + } +} + +},{"has-cors":31}],26:[function(_dereq_,module,exports){ + +/** + * Expose `Emitter`. + */ + +module.exports = Emitter; + +/** + * Initialize a new `Emitter`. + * + * @api public + */ + +function Emitter(obj) { + if (obj) return mixin(obj); +}; + +/** + * Mixin the emitter properties. + * + * @param {Object} obj + * @return {Object} + * @api private + */ + +function mixin(obj) { + for (var key in Emitter.prototype) { + obj[key] = Emitter.prototype[key]; + } + return obj; +} + +/** + * Listen on the given `event` with `fn`. + * + * @param {String} event + * @param {Function} fn + * @return {Emitter} + * @api public + */ + +Emitter.prototype.on = +Emitter.prototype.addEventListener = function(event, fn){ + this._callbacks = this._callbacks || {}; + (this._callbacks[event] = this._callbacks[event] || []) + .push(fn); + return this; +}; + +/** + * Adds an `event` listener that will be invoked a single + * time then automatically removed. + * + * @param {String} event + * @param {Function} fn + * @return {Emitter} + * @api public + */ + +Emitter.prototype.once = function(event, fn){ + var self = this; + this._callbacks = this._callbacks || {}; + + function on() { + self.off(event, on); + fn.apply(this, arguments); + } + + on.fn = fn; + this.on(event, on); + return this; +}; + +/** + * Remove the given callback for `event` or all + * registered callbacks. + * + * @param {String} event + * @param {Function} fn + * @return {Emitter} + * @api public + */ + +Emitter.prototype.off = +Emitter.prototype.removeListener = +Emitter.prototype.removeAllListeners = +Emitter.prototype.removeEventListener = function(event, fn){ + this._callbacks = this._callbacks || {}; + + // all + if (0 == arguments.length) { + this._callbacks = {}; + return this; + } + + // specific event + var callbacks = this._callbacks[event]; + if (!callbacks) return this; + + // remove all handlers + if (1 == arguments.length) { + delete this._callbacks[event]; + return this; + } + + // remove specific handler + var cb; + for (var i = 0; i < callbacks.length; i++) { + cb = callbacks[i]; + if (cb === fn || cb.fn === fn) { + callbacks.splice(i, 1); + break; + } + } + return this; +}; + +/** + * Emit `event` with the given args. + * + * @param {String} event + * @param {Mixed} ... + * @return {Emitter} + */ + +Emitter.prototype.emit = function(event){ + this._callbacks = this._callbacks || {}; + var args = [].slice.call(arguments, 1) + , callbacks = this._callbacks[event]; + + if (callbacks) { + callbacks = callbacks.slice(0); + for (var i = 0, len = callbacks.length; i < len; ++i) { + callbacks[i].apply(this, args); + } + } + + return this; +}; + +/** + * Return array of callbacks for `event`. + * + * @param {String} event + * @return {Array} + * @api public + */ + +Emitter.prototype.listeners = function(event){ + this._callbacks = this._callbacks || {}; + return this._callbacks[event] || []; +}; + +/** + * Check if this emitter has `event` handlers. + * + * @param {String} event + * @return {Boolean} + * @api public + */ + +Emitter.prototype.hasListeners = function(event){ + return !! this.listeners(event).length; +}; + +},{}],27:[function(_dereq_,module,exports){ +(function (global){ +/** + * Module dependencies. + */ + +var keys = _dereq_('./keys'); +var hasBinary = _dereq_('has-binary'); +var sliceBuffer = _dereq_('arraybuffer.slice'); +var base64encoder = _dereq_('base64-arraybuffer'); +var after = _dereq_('after'); +var utf8 = _dereq_('utf8'); + +/** + * Check if we are running an android browser. That requires us to use + * ArrayBuffer with polling transports... + * + * http://ghinda.net/jpeg-blob-ajax-android/ + */ + +var isAndroid = navigator.userAgent.match(/Android/i); + +/** + * Check if we are running in PhantomJS. + * Uploading a Blob with PhantomJS does not work correctly, as reported here: + * https://github.com/ariya/phantomjs/issues/11395 + * @type boolean + */ +var isPhantomJS = /PhantomJS/i.test(navigator.userAgent); + +/** + * When true, avoids using Blobs to encode payloads. + * @type boolean + */ +var dontSendBlobs = isAndroid || isPhantomJS; + +/** + * Current protocol version. + */ + +exports.protocol = 3; + +/** + * Packet types. + */ + +var packets = exports.packets = { + open: 0 // non-ws + , close: 1 // non-ws + , ping: 2 + , pong: 3 + , message: 4 + , upgrade: 5 + , noop: 6 +}; + +var packetslist = keys(packets); + +/** + * Premade error packet. + */ + +var err = { type: 'error', data: 'parser error' }; + +/** + * Create a blob api even for blob builder when vendor prefixes exist + */ + +var Blob = _dereq_('blob'); + +/** + * Encodes a packet. + * + * <packet type id> [ <data> ] + * + * Example: + * + * 5hello world + * 3 + * 4 + * + * Binary is encoded in an identical principle + * + * @api private + */ + +exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) { + if ('function' == typeof supportsBinary) { + callback = supportsBinary; + supportsBinary = false; + } + + if ('function' == typeof utf8encode) { + callback = utf8encode; + utf8encode = null; + } + + var data = (packet.data === undefined) + ? undefined + : packet.data.buffer || packet.data; + + if (global.ArrayBuffer && data instanceof ArrayBuffer) { + return encodeArrayBuffer(packet, supportsBinary, callback); + } else if (Blob && data instanceof global.Blob) { + return encodeBlob(packet, supportsBinary, callback); + } + + // might be an object with { base64: true, data: dataAsBase64String } + if (data && data.base64) { + return encodeBase64Object(packet, callback); + } + + // Sending data as a utf-8 string + var encoded = packets[packet.type]; + + // data fragment is optional + if (undefined !== packet.data) { + encoded += utf8encode ? utf8.encode(String(packet.data)) : String(packet.data); + } + + return callback('' + encoded); + +}; + +function encodeBase64Object(packet, callback) { + // packet data is an object { base64: true, data: dataAsBase64String } + var message = 'b' + exports.packets[packet.type] + packet.data.data; + return callback(message); +} + +/** + * Encode packet helpers for binary types + */ + +function encodeArrayBuffer(packet, supportsBinary, callback) { + if (!supportsBinary) { + return exports.encodeBase64Packet(packet, callback); + } + + var data = packet.data; + var contentArray = new Uint8Array(data); + var resultBuffer = new Uint8Array(1 + data.byteLength); + + resultBuffer[0] = packets[packet.type]; + for (var i = 0; i < contentArray.length; i++) { + resultBuffer[i+1] = contentArray[i]; + } + + return callback(resultBuffer.buffer); +} + +function encodeBlobAsArrayBuffer(packet, supportsBinary, callback) { + if (!supportsBinary) { + return exports.encodeBase64Packet(packet, callback); + } + + var fr = new FileReader(); + fr.onload = function() { + packet.data = fr.result; + exports.encodePacket(packet, supportsBinary, true, callback); + }; + return fr.readAsArrayBuffer(packet.data); +} + +function encodeBlob(packet, supportsBinary, callback) { + if (!supportsBinary) { + return exports.encodeBase64Packet(packet, callback); + } + + if (dontSendBlobs) { + return encodeBlobAsArrayBuffer(packet, supportsBinary, callback); + } + + var length = new Uint8Array(1); + length[0] = packets[packet.type]; + var blob = new Blob([length.buffer, packet.data]); + + return callback(blob); +} + +/** + * Encodes a packet with binary data in a base64 string + * + * @param {Object} packet, has `type` and `data` + * @return {String} base64 encoded message + */ + +exports.encodeBase64Packet = function(packet, callback) { + var message = 'b' + exports.packets[packet.type]; + if (Blob && packet.data instanceof global.Blob) { + var fr = new FileReader(); + fr.onload = function() { + var b64 = fr.result.split(',')[1]; + callback(message + b64); + }; + return fr.readAsDataURL(packet.data); + } + + var b64data; + try { + b64data = String.fromCharCode.apply(null, new Uint8Array(packet.data)); + } catch (e) { + // iPhone Safari doesn't let you apply with typed arrays + var typed = new Uint8Array(packet.data); + var basic = new Array(typed.length); + for (var i = 0; i < typed.length; i++) { + basic[i] = typed[i]; + } + b64data = String.fromCharCode.apply(null, basic); + } + message += global.btoa(b64data); + return callback(message); +}; + +/** + * Decodes a packet. Changes format to Blob if requested. + * + * @return {Object} with `type` and `data` (if any) + * @api private + */ + +exports.decodePacket = function (data, binaryType, utf8decode) { + // String data + if (typeof data == 'string' || data === undefined) { + if (data.charAt(0) == 'b') { + return exports.decodeBase64Packet(data.substr(1), binaryType); + } + + if (utf8decode) { + try { + data = utf8.decode(data); + } catch (e) { + return err; + } + } + var type = data.charAt(0); + + if (Number(type) != type || !packetslist[type]) { + return err; + } + + if (data.length > 1) { + return { type: packetslist[type], data: data.substring(1) }; + } else { + return { type: packetslist[type] }; + } + } + + var asArray = new Uint8Array(data); + var type = asArray[0]; + var rest = sliceBuffer(data, 1); + if (Blob && binaryType === 'blob') { + rest = new Blob([rest]); + } + return { type: packetslist[type], data: rest }; +}; + +/** + * Decodes a packet encoded in a base64 string + * + * @param {String} base64 encoded message + * @return {Object} with `type` and `data` (if any) + */ + +exports.decodeBase64Packet = function(msg, binaryType) { + var type = packetslist[msg.charAt(0)]; + if (!global.ArrayBuffer) { + return { type: type, data: { base64: true, data: msg.substr(1) } }; + } + + var data = base64encoder.decode(msg.substr(1)); + + if (binaryType === 'blob' && Blob) { + data = new Blob([data]); + } + + return { type: type, data: data }; +}; + +/** + * Encodes multiple messages (payload). + * + * <length>:data + * + * Example: + * + * 11:hello world2:hi + * + * If any contents are binary, they will be encoded as base64 strings. Base64 + * encoded strings are marked with a b before the length specifier + * + * @param {Array} packets + * @api private + */ + +exports.encodePayload = function (packets, supportsBinary, callback) { + if (typeof supportsBinary == 'function') { + callback = supportsBinary; + supportsBinary = null; + } + + var isBinary = hasBinary(packets); + + if (supportsBinary && isBinary) { + if (Blob && !dontSendBlobs) { + return exports.encodePayloadAsBlob(packets, callback); + } + + return exports.encodePayloadAsArrayBuffer(packets, callback); + } + + if (!packets.length) { + return callback('0:'); + } + + function setLengthHeader(message) { + return message.length + ':' + message; + } + + function encodeOne(packet, doneCallback) { + exports.encodePacket(packet, !isBinary ? false : supportsBinary, true, function(message) { + doneCallback(null, setLengthHeader(message)); + }); + } + + map(packets, encodeOne, function(err, results) { + return callback(results.join('')); + }); +}; + +/** + * Async array map using after + */ + +function map(ary, each, done) { + var result = new Array(ary.length); + var next = after(ary.length, done); + + var eachWithIndex = function(i, el, cb) { + each(el, function(error, msg) { + result[i] = msg; + cb(error, result); + }); + }; + + for (var i = 0; i < ary.length; i++) { + eachWithIndex(i, ary[i], next); + } +} + +/* + * Decodes data when a payload is maybe expected. Possible binary contents are + * decoded from their base64 representation + * + * @param {String} data, callback method + * @api public + */ + +exports.decodePayload = function (data, binaryType, callback) { + if (typeof data != 'string') { + return exports.decodePayloadAsBinary(data, binaryType, callback); + } + + if (typeof binaryType === 'function') { + callback = binaryType; + binaryType = null; + } + + var packet; + if (data == '') { + // parser error - ignoring payload + return callback(err, 0, 1); + } + + var length = '' + , n, msg; + + for (var i = 0, l = data.length; i < l; i++) { + var chr = data.charAt(i); + + if (':' != chr) { + length += chr; + } else { + if ('' == length || (length != (n = Number(length)))) { + // parser error - ignoring payload + return callback(err, 0, 1); + } + + msg = data.substr(i + 1, n); + + if (length != msg.length) { + // parser error - ignoring payload + return callback(err, 0, 1); + } + + if (msg.length) { + packet = exports.decodePacket(msg, binaryType, true); + + if (err.type == packet.type && err.data == packet.data) { + // parser error in individual packet - ignoring payload + return callback(err, 0, 1); + } + + var ret = callback(packet, i + n, l); + if (false === ret) return; + } + + // advance cursor + i += n; + length = ''; + } + } + + if (length != '') { + // parser error - ignoring payload + return callback(err, 0, 1); + } + +}; + +/** + * Encodes multiple messages (payload) as binary. + * + * <1 = binary, 0 = string><number from 0-9><number from 0-9>[...]<number + * 255><data> + * + * Example: + * 1 3 255 1 2 3, if the binary contents are interpreted as 8 bit integers + * + * @param {Array} packets + * @return {ArrayBuffer} encoded payload + * @api private + */ + +exports.encodePayloadAsArrayBuffer = function(packets, callback) { + if (!packets.length) { + return callback(new ArrayBuffer(0)); + } + + function encodeOne(packet, doneCallback) { + exports.encodePacket(packet, true, true, function(data) { + return doneCallback(null, data); + }); + } + + map(packets, encodeOne, function(err, encodedPackets) { + var totalLength = encodedPackets.reduce(function(acc, p) { + var len; + if (typeof p === 'string'){ + len = p.length; + } else { + len = p.byteLength; + } + return acc + len.toString().length + len + 2; // string/binary identifier + separator = 2 + }, 0); + + var resultArray = new Uint8Array(totalLength); + + var bufferIndex = 0; + encodedPackets.forEach(function(p) { + var isString = typeof p === 'string'; + var ab = p; + if (isString) { + var view = new Uint8Array(p.length); + for (var i = 0; i < p.length; i++) { + view[i] = p.charCodeAt(i); + } + ab = view.buffer; + } + + if (isString) { // not true binary + resultArray[bufferIndex++] = 0; + } else { // true binary + resultArray[bufferIndex++] = 1; + } + + var lenStr = ab.byteLength.toString(); + for (var i = 0; i < lenStr.length; i++) { + resultArray[bufferIndex++] = parseInt(lenStr[i]); + } + resultArray[bufferIndex++] = 255; + + var view = new Uint8Array(ab); + for (var i = 0; i < view.length; i++) { + resultArray[bufferIndex++] = view[i]; + } + }); + + return callback(resultArray.buffer); + }); +}; + +/** + * Encode as Blob + */ + +exports.encodePayloadAsBlob = function(packets, callback) { + function encodeOne(packet, doneCallback) { + exports.encodePacket(packet, true, true, function(encoded) { + var binaryIdentifier = new Uint8Array(1); + binaryIdentifier[0] = 1; + if (typeof encoded === 'string') { + var view = new Uint8Array(encoded.length); + for (var i = 0; i < encoded.length; i++) { + view[i] = encoded.charCodeAt(i); + } + encoded = view.buffer; + binaryIdentifier[0] = 0; + } + + var len = (encoded instanceof ArrayBuffer) + ? encoded.byteLength + : encoded.size; + + var lenStr = len.toString(); + var lengthAry = new Uint8Array(lenStr.length + 1); + for (var i = 0; i < lenStr.length; i++) { + lengthAry[i] = parseInt(lenStr[i]); + } + lengthAry[lenStr.length] = 255; + + if (Blob) { + var blob = new Blob([binaryIdentifier.buffer, lengthAry.buffer, encoded]); + doneCallback(null, blob); + } + }); + } + + map(packets, encodeOne, function(err, results) { + return callback(new Blob(results)); + }); +}; + +/* + * Decodes data when a payload is maybe expected. Strings are decoded by + * interpreting each byte as a key code for entries marked to start with 0. See + * description of encodePayloadAsBinary + * + * @param {ArrayBuffer} data, callback method + * @api public + */ + +exports.decodePayloadAsBinary = function (data, binaryType, callback) { + if (typeof binaryType === 'function') { + callback = binaryType; + binaryType = null; + } + + var bufferTail = data; + var buffers = []; + + var numberTooLong = false; + while (bufferTail.byteLength > 0) { + var tailArray = new Uint8Array(bufferTail); + var isString = tailArray[0] === 0; + var msgLength = ''; + + for (var i = 1; ; i++) { + if (tailArray[i] == 255) break; + + if (msgLength.length > 310) { + numberTooLong = true; + break; + } + + msgLength += tailArray[i]; + } + + if(numberTooLong) return callback(err, 0, 1); + + bufferTail = sliceBuffer(bufferTail, 2 + msgLength.length); + msgLength = parseInt(msgLength); + + var msg = sliceBuffer(bufferTail, 0, msgLength); + if (isString) { + try { + msg = String.fromCharCode.apply(null, new Uint8Array(msg)); + } catch (e) { + // iPhone Safari doesn't let you apply to typed arrays + var typed = new Uint8Array(msg); + msg = ''; + for (var i = 0; i < typed.length; i++) { + msg += String.fromCharCode(typed[i]); + } + } + } + + buffers.push(msg); + bufferTail = sliceBuffer(bufferTail, msgLength); + } + + var total = buffers.length; + buffers.forEach(function(buffer, i) { + callback(exports.decodePacket(buffer, binaryType, true), i, total); + }); +}; + +}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {}) +},{"./keys":28,"after":6,"arraybuffer.slice":7,"base64-arraybuffer":9,"blob":10,"has-binary":29,"utf8":44}],28:[function(_dereq_,module,exports){ + +/** + * Gets the keys for an object. + * + * @return {Array} keys + * @api private + */ + +module.exports = Object.keys || function keys (obj){ + var arr = []; + var has = Object.prototype.hasOwnProperty; + + for (var i in obj) { + if (has.call(obj, i)) { + arr.push(i); + } + } + return arr; +}; + +},{}],29:[function(_dereq_,module,exports){ +(function (global){ + +/* + * Module requirements. + */ + +var isArray = _dereq_('isarray'); + +/** + * Module exports. + */ + +module.exports = hasBinary; + +/** + * Checks for binary data. + * + * Right now only Buffer and ArrayBuffer are supported.. + * + * @param {Object} anything + * @api public + */ + +function hasBinary(data) { + + function _hasBinary(obj) { + if (!obj) return false; + + if ( (global.Buffer && global.Buffer.isBuffer(obj)) || + (global.ArrayBuffer && obj instanceof ArrayBuffer) || + (global.Blob && obj instanceof Blob) || + (global.File && obj instanceof File) + ) { + return true; + } + + if (isArray(obj)) { + for (var i = 0; i < obj.length; i++) { + if (_hasBinary(obj[i])) { + return true; + } + } + } else if (obj && 'object' == typeof obj) { + if (obj.toJSON) { + obj = obj.toJSON(); + } + + for (var key in obj) { + if (Object.prototype.hasOwnProperty.call(obj, key) && _hasBinary(obj[key])) { + return true; + } + } + } + + return false; + } + + return _hasBinary(data); +} + +}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {}) +},{"isarray":33}],30:[function(_dereq_,module,exports){ +(function (global){ + +/* + * Module requirements. + */ + +var isArray = _dereq_('isarray'); + +/** + * Module exports. + */ + +module.exports = hasBinary; + +/** + * Checks for binary data. + * + * Right now only Buffer and ArrayBuffer are supported.. + * + * @param {Object} anything + * @api public + */ + +function hasBinary(data) { + + function _hasBinary(obj) { + if (!obj) return false; + + if ( (global.Buffer && global.Buffer.isBuffer && global.Buffer.isBuffer(obj)) || + (global.ArrayBuffer && obj instanceof ArrayBuffer) || + (global.Blob && obj instanceof Blob) || + (global.File && obj instanceof File) + ) { + return true; + } + + if (isArray(obj)) { + for (var i = 0; i < obj.length; i++) { + if (_hasBinary(obj[i])) { + return true; + } + } + } else if (obj && 'object' == typeof obj) { + // see: https://github.com/Automattic/has-binary/pull/4 + if (obj.toJSON && 'function' == typeof obj.toJSON) { + obj = obj.toJSON(); + } + + for (var key in obj) { + if (Object.prototype.hasOwnProperty.call(obj, key) && _hasBinary(obj[key])) { + return true; + } + } + } + + return false; + } + + return _hasBinary(data); +} + +}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {}) +},{"isarray":33}],31:[function(_dereq_,module,exports){ + +/** + * Module exports. + * + * Logic borrowed from Modernizr: + * + * - https://github.com/Modernizr/Modernizr/blob/master/feature-detects/cors.js + */ + +try { + module.exports = typeof XMLHttpRequest !== 'undefined' && + 'withCredentials' in new XMLHttpRequest(); +} catch (err) { + // if XMLHttp support is disabled in IE then it will throw + // when trying to create + module.exports = false; +} + +},{}],32:[function(_dereq_,module,exports){ + +var indexOf = [].indexOf; + +module.exports = function(arr, obj){ + if (indexOf) return arr.indexOf(obj); + for (var i = 0; i < arr.length; ++i) { + if (arr[i] === obj) return i; + } + return -1; +}; +},{}],33:[function(_dereq_,module,exports){ +module.exports = Array.isArray || function (arr) { + return Object.prototype.toString.call(arr) == '[object Array]'; +}; + +},{}],34:[function(_dereq_,module,exports){ +(function (global){ +/*! JSON v3.3.2 | http://bestiejs.github.io/json3 | Copyright 2012-2014, Kit Cambridge | http://kit.mit-license.org */ +;(function () { + // Detect the `define` function exposed by asynchronous module loaders. The + // strict `define` check is necessary for compatibility with `r.js`. + var isLoader = typeof define === "function" && define.amd; + + // A set of types used to distinguish objects from primitives. + var objectTypes = { + "function": true, + "object": true + }; + + // Detect the `exports` object exposed by CommonJS implementations. + var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports; + + // Use the `global` object exposed by Node (including Browserify via + // `insert-module-globals`), Narwhal, and Ringo as the default context, + // and the `window` object in browsers. Rhino exports a `global` function + // instead. + var root = objectTypes[typeof window] && window || this, + freeGlobal = freeExports && objectTypes[typeof module] && module && !module.nodeType && typeof global == "object" && global; + + if (freeGlobal && (freeGlobal["global"] === freeGlobal || freeGlobal["window"] === freeGlobal || freeGlobal["self"] === freeGlobal)) { + root = freeGlobal; + } + + // Public: Initializes JSON 3 using the given `context` object, attaching the + // `stringify` and `parse` functions to the specified `exports` object. + function runInContext(context, exports) { + context || (context = root["Object"]()); + exports || (exports = root["Object"]()); + + // Native constructor aliases. + var Number = context["Number"] || root["Number"], + String = context["String"] || root["String"], + Object = context["Object"] || root["Object"], + Date = context["Date"] || root["Date"], + SyntaxError = context["SyntaxError"] || root["SyntaxError"], + TypeError = context["TypeError"] || root["TypeError"], + Math = context["Math"] || root["Math"], + nativeJSON = context["JSON"] || root["JSON"]; + + // Delegate to the native `stringify` and `parse` implementations. + if (typeof nativeJSON == "object" && nativeJSON) { + exports.stringify = nativeJSON.stringify; + exports.parse = nativeJSON.parse; + } + + // Convenience aliases. + var objectProto = Object.prototype, + getClass = objectProto.toString, + isProperty, forEach, undef; + + // Test the `Date#getUTC*` methods. Based on work by @Yaffle. + var isExtended = new Date(-3509827334573292); + try { + // The `getUTCFullYear`, `Month`, and `Date` methods return nonsensical + // results for certain dates in Opera >= 10.53. + isExtended = isExtended.getUTCFullYear() == -109252 && isExtended.getUTCMonth() === 0 && isExtended.getUTCDate() === 1 && + // Safari < 2.0.2 stores the internal millisecond time value correctly, + // but clips the values returned by the date methods to the range of + // signed 32-bit integers ([-2 ** 31, 2 ** 31 - 1]). + isExtended.getUTCHours() == 10 && isExtended.getUTCMinutes() == 37 && isExtended.getUTCSeconds() == 6 && isExtended.getUTCMilliseconds() == 708; + } catch (exception) {} + + // Internal: Determines whether the native `JSON.stringify` and `parse` + // implementations are spec-compliant. Based on work by Ken Snyder. + function has(name) { + if (has[name] !== undef) { + // Return cached feature test result. + return has[name]; + } + var isSupported; + if (name == "bug-string-char-index") { + // IE <= 7 doesn't support accessing string characters using square + // bracket notation. IE 8 only supports this for primitives. + isSupported = "a"[0] != "a"; + } else if (name == "json") { + // Indicates whether both `JSON.stringify` and `JSON.parse` are + // supported. + isSupported = has("json-stringify") && has("json-parse"); + } else { + var value, serialized = '{"a":[1,true,false,null,"\\u0000\\b\\n\\f\\r\\t"]}'; + // Test `JSON.stringify`. + if (name == "json-stringify") { + var stringify = exports.stringify, stringifySupported = typeof stringify == "function" && isExtended; + if (stringifySupported) { + // A test function object with a custom `toJSON` method. + (value = function () { + return 1; + }).toJSON = value; + try { + stringifySupported = + // Firefox 3.1b1 and b2 serialize string, number, and boolean + // primitives as object literals. + stringify(0) === "0" && + // FF 3.1b1, b2, and JSON 2 serialize wrapped primitives as object + // literals. + stringify(new Number()) === "0" && + stringify(new String()) == '""' && + // FF 3.1b1, 2 throw an error if the value is `null`, `undefined`, or + // does not define a canonical JSON representation (this applies to + // objects with `toJSON` properties as well, *unless* they are nested + // within an object or array). + stringify(getClass) === undef && + // IE 8 serializes `undefined` as `"undefined"`. Safari <= 5.1.7 and + // FF 3.1b3 pass this test. + stringify(undef) === undef && + // Safari <= 5.1.7 and FF 3.1b3 throw `Error`s and `TypeError`s, + // respectively, if the value is omitted entirely. + stringify() === undef && + // FF 3.1b1, 2 throw an error if the given value is not a number, + // string, array, object, Boolean, or `null` literal. This applies to + // objects with custom `toJSON` methods as well, unless they are nested + // inside object or array literals. YUI 3.0.0b1 ignores custom `toJSON` + // methods entirely. + stringify(value) === "1" && + stringify([value]) == "[1]" && + // Prototype <= 1.6.1 serializes `[undefined]` as `"[]"` instead of + // `"[null]"`. + stringify([undef]) == "[null]" && + // YUI 3.0.0b1 fails to serialize `null` literals. + stringify(null) == "null" && + // FF 3.1b1, 2 halts serialization if an array contains a function: + // `[1, true, getClass, 1]` serializes as "[1,true,],". FF 3.1b3 + // elides non-JSON values from objects and arrays, unless they + // define custom `toJSON` methods. + stringify([undef, getClass, null]) == "[null,null,null]" && + // Simple serialization test. FF 3.1b1 uses Unicode escape sequences + // where character escape codes are expected (e.g., `\b` => `\u0008`). + stringify({ "a": [value, true, false, null, "\x00\b\n\f\r\t"] }) == serialized && + // FF 3.1b1 and b2 ignore the `filter` and `width` arguments. + stringify(null, value) === "1" && + stringify([1, 2], null, 1) == "[\n 1,\n 2\n]" && + // JSON 2, Prototype <= 1.7, and older WebKit builds incorrectly + // serialize extended years. + stringify(new Date(-8.64e15)) == '"-271821-04-20T00:00:00.000Z"' && + // The milliseconds are optional in ES 5, but required in 5.1. + stringify(new Date(8.64e15)) == '"+275760-09-13T00:00:00.000Z"' && + // Firefox <= 11.0 incorrectly serializes years prior to 0 as negative + // four-digit years instead of six-digit years. Credits: @Yaffle. + stringify(new Date(-621987552e5)) == '"-000001-01-01T00:00:00.000Z"' && + // Safari <= 5.1.5 and Opera >= 10.53 incorrectly serialize millisecond + // values less than 1000. Credits: @Yaffle. + stringify(new Date(-1)) == '"1969-12-31T23:59:59.999Z"'; + } catch (exception) { + stringifySupported = false; + } + } + isSupported = stringifySupported; + } + // Test `JSON.parse`. + if (name == "json-parse") { + var parse = exports.parse; + if (typeof parse == "function") { + try { + // FF 3.1b1, b2 will throw an exception if a bare literal is provided. + // Conforming implementations should also coerce the initial argument to + // a string prior to parsing. + if (parse("0") === 0 && !parse(false)) { + // Simple parsing test. + value = parse(serialized); + var parseSupported = value["a"].length == 5 && value["a"][0] === 1; + if (parseSupported) { + try { + // Safari <= 5.1.2 and FF 3.1b1 allow unescaped tabs in strings. + parseSupported = !parse('"\t"'); + } catch (exception) {} + if (parseSupported) { + try { + // FF 4.0 and 4.0.1 allow leading `+` signs and leading + // decimal points. FF 4.0, 4.0.1, and IE 9-10 also allow + // certain octal literals. + parseSupported = parse("01") !== 1; + } catch (exception) {} + } + if (parseSupported) { + try { + // FF 4.0, 4.0.1, and Rhino 1.7R3-R4 allow trailing decimal + // points. These environments, along with FF 3.1b1 and 2, + // also allow trailing commas in JSON objects and arrays. + parseSupported = parse("1.") !== 1; + } catch (exception) {} + } + } + } + } catch (exception) { + parseSupported = false; + } + } + isSupported = parseSupported; + } + } + return has[name] = !!isSupported; + } + + if (!has("json")) { + // Common `[[Class]]` name aliases. + var functionClass = "[object Function]", + dateClass = "[object Date]", + numberClass = "[object Number]", + stringClass = "[object String]", + arrayClass = "[object Array]", + booleanClass = "[object Boolean]"; + + // Detect incomplete support for accessing string characters by index. + var charIndexBuggy = has("bug-string-char-index"); + + // Define additional utility methods if the `Date` methods are buggy. + if (!isExtended) { + var floor = Math.floor; + // A mapping between the months of the year and the number of days between + // January 1st and the first of the respective month. + var Months = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]; + // Internal: Calculates the number of days between the Unix epoch and the + // first day of the given month. + var getDay = function (year, month) { + return Months[month] + 365 * (year - 1970) + floor((year - 1969 + (month = +(month > 1))) / 4) - floor((year - 1901 + month) / 100) + floor((year - 1601 + month) / 400); + }; + } + + // Internal: Determines if a property is a direct property of the given + // object. Delegates to the native `Object#hasOwnProperty` method. + if (!(isProperty = objectProto.hasOwnProperty)) { + isProperty = function (property) { + var members = {}, constructor; + if ((members.__proto__ = null, members.__proto__ = { + // The *proto* property cannot be set multiple times in recent + // versions of Firefox and SeaMonkey. + "toString": 1 + }, members).toString != getClass) { + // Safari <= 2.0.3 doesn't implement `Object#hasOwnProperty`, but + // supports the mutable *proto* property. + isProperty = function (property) { + // Capture and break the object's prototype chain (see section 8.6.2 + // of the ES 5.1 spec). The parenthesized expression prevents an + // unsafe transformation by the Closure Compiler. + var original = this.__proto__, result = property in (this.__proto__ = null, this); + // Restore the original prototype chain. + this.__proto__ = original; + return result; + }; + } else { + // Capture a reference to the top-level `Object` constructor. + constructor = members.constructor; + // Use the `constructor` property to simulate `Object#hasOwnProperty` in + // other environments. + isProperty = function (property) { + var parent = (this.constructor || constructor).prototype; + return property in this && !(property in parent && this[property] === parent[property]); + }; + } + members = null; + return isProperty.call(this, property); + }; + } + + // Internal: Normalizes the `for...in` iteration algorithm across + // environments. Each enumerated key is yielded to a `callback` function. + forEach = function (object, callback) { + var size = 0, Properties, members, property; + + // Tests for bugs in the current environment's `for...in` algorithm. The + // `valueOf` property inherits the non-enumerable flag from + // `Object.prototype` in older versions of IE, Netscape, and Mozilla. + (Properties = function () { + this.valueOf = 0; + }).prototype.valueOf = 0; + + // Iterate over a new instance of the `Properties` class. + members = new Properties(); + for (property in members) { + // Ignore all properties inherited from `Object.prototype`. + if (isProperty.call(members, property)) { + size++; + } + } + Properties = members = null; + + // Normalize the iteration algorithm. + if (!size) { + // A list of non-enumerable properties inherited from `Object.prototype`. + members = ["valueOf", "toString", "toLocaleString", "propertyIsEnumerable", "isPrototypeOf", "hasOwnProperty", "constructor"]; + // IE <= 8, Mozilla 1.0, and Netscape 6.2 ignore shadowed non-enumerable + // properties. + forEach = function (object, callback) { + var isFunction = getClass.call(object) == functionClass, property, length; + var hasProperty = !isFunction && typeof object.constructor != "function" && objectTypes[typeof object.hasOwnProperty] && object.hasOwnProperty || isProperty; + for (property in object) { + // Gecko <= 1.0 enumerates the `prototype` property of functions under + // certain conditions; IE does not. + if (!(isFunction && property == "prototype") && hasProperty.call(object, property)) { + callback(property); + } + } + // Manually invoke the callback for each non-enumerable property. + for (length = members.length; property = members[--length]; hasProperty.call(object, property) && callback(property)); + }; + } else if (size == 2) { + // Safari <= 2.0.4 enumerates shadowed properties twice. + forEach = function (object, callback) { + // Create a set of iterated properties. + var members = {}, isFunction = getClass.call(object) == functionClass, property; + for (property in object) { + // Store each property name to prevent double enumeration. The + // `prototype` property of functions is not enumerated due to cross- + // environment inconsistencies. + if (!(isFunction && property == "prototype") && !isProperty.call(members, property) && (members[property] = 1) && isProperty.call(object, property)) { + callback(property); + } + } + }; + } else { + // No bugs detected; use the standard `for...in` algorithm. + forEach = function (object, callback) { + var isFunction = getClass.call(object) == functionClass, property, isConstructor; + for (property in object) { + if (!(isFunction && property == "prototype") && isProperty.call(object, property) && !(isConstructor = property === "constructor")) { + callback(property); + } + } + // Manually invoke the callback for the `constructor` property due to + // cross-environment inconsistencies. + if (isConstructor || isProperty.call(object, (property = "constructor"))) { + callback(property); + } + }; + } + return forEach(object, callback); + }; + + // Public: Serializes a JavaScript `value` as a JSON string. The optional + // `filter` argument may specify either a function that alters how object and + // array members are serialized, or an array of strings and numbers that + // indicates which properties should be serialized. The optional `width` + // argument may be either a string or number that specifies the indentation + // level of the output. + if (!has("json-stringify")) { + // Internal: A map of control characters and their escaped equivalents. + var Escapes = { + 92: "\\\\", + 34: '\\"', + 8: "\\b", + 12: "\\f", + 10: "\\n", + 13: "\\r", + 9: "\\t" + }; + + // Internal: Converts `value` into a zero-padded string such that its + // length is at least equal to `width`. The `width` must be <= 6. + var leadingZeroes = "000000"; + var toPaddedString = function (width, value) { + // The `|| 0` expression is necessary to work around a bug in + // Opera <= 7.54u2 where `0 == -0`, but `String(-0) !== "0"`. + return (leadingZeroes + (value || 0)).slice(-width); + }; + + // Internal: Double-quotes a string `value`, replacing all ASCII control + // characters (characters with code unit values between 0 and 31) with + // their escaped equivalents. This is an implementation of the + // `Quote(value)` operation defined in ES 5.1 section 15.12.3. + var unicodePrefix = "\\u00"; + var quote = function (value) { + var result = '"', index = 0, length = value.length, useCharIndex = !charIndexBuggy || length > 10; + var symbols = useCharIndex && (charIndexBuggy ? value.split("") : value); + for (; index < length; index++) { + var charCode = value.charCodeAt(index); + // If the character is a control character, append its Unicode or + // shorthand escape sequence; otherwise, append the character as-is. + switch (charCode) { + case 8: case 9: case 10: case 12: case 13: case 34: case 92: + result += Escapes[charCode]; + break; + default: + if (charCode < 32) { + result += unicodePrefix + toPaddedString(2, charCode.toString(16)); + break; + } + result += useCharIndex ? symbols[index] : value.charAt(index); + } + } + return result + '"'; + }; + + // Internal: Recursively serializes an object. Implements the + // `Str(key, holder)`, `JO(value)`, and `JA(value)` operations. + var serialize = function (property, object, callback, properties, whitespace, indentation, stack) { + var value, className, year, month, date, time, hours, minutes, seconds, milliseconds, results, element, index, length, prefix, result; + try { + // Necessary for host object support. + value = object[property]; + } catch (exception) {} + if (typeof value == "object" && value) { + className = getClass.call(value); + if (className == dateClass && !isProperty.call(value, "toJSON")) { + if (value > -1 / 0 && value < 1 / 0) { + // Dates are serialized according to the `Date#toJSON` method + // specified in ES 5.1 section 15.9.5.44. See section 15.9.1.15 + // for the ISO 8601 date time string format. + if (getDay) { + // Manually compute the year, month, date, hours, minutes, + // seconds, and milliseconds if the `getUTC*` methods are + // buggy. Adapted from @Yaffle's `date-shim` project. + date = floor(value / 864e5); + for (year = floor(date / 365.2425) + 1970 - 1; getDay(year + 1, 0) <= date; year++); + for (month = floor((date - getDay(year, 0)) / 30.42); getDay(year, month + 1) <= date; month++); + date = 1 + date - getDay(year, month); + // The `time` value specifies the time within the day (see ES + // 5.1 section 15.9.1.2). The formula `(A % B + B) % B` is used + // to compute `A modulo B`, as the `%` operator does not + // correspond to the `modulo` operation for negative numbers. + time = (value % 864e5 + 864e5) % 864e5; + // The hours, minutes, seconds, and milliseconds are obtained by + // decomposing the time within the day. See section 15.9.1.10. + hours = floor(time / 36e5) % 24; + minutes = floor(time / 6e4) % 60; + seconds = floor(time / 1e3) % 60; + milliseconds = time % 1e3; + } else { + year = value.getUTCFullYear(); + month = value.getUTCMonth(); + date = value.getUTCDate(); + hours = value.getUTCHours(); + minutes = value.getUTCMinutes(); + seconds = value.getUTCSeconds(); + milliseconds = value.getUTCMilliseconds(); + } + // Serialize extended years correctly. + value = (year <= 0 || year >= 1e4 ? (year < 0 ? "-" : "+") + toPaddedString(6, year < 0 ? -year : year) : toPaddedString(4, year)) + + "-" + toPaddedString(2, month + 1) + "-" + toPaddedString(2, date) + + // Months, dates, hours, minutes, and seconds should have two + // digits; milliseconds should have three. + "T" + toPaddedString(2, hours) + ":" + toPaddedString(2, minutes) + ":" + toPaddedString(2, seconds) + + // Milliseconds are optional in ES 5.0, but required in 5.1. + "." + toPaddedString(3, milliseconds) + "Z"; + } else { + value = null; + } + } else if (typeof value.toJSON == "function" && ((className != numberClass && className != stringClass && className != arrayClass) || isProperty.call(value, "toJSON"))) { + // Prototype <= 1.6.1 adds non-standard `toJSON` methods to the + // `Number`, `String`, `Date`, and `Array` prototypes. JSON 3 + // ignores all `toJSON` methods on these objects unless they are + // defined directly on an instance. + value = value.toJSON(property); + } + } + if (callback) { + // If a replacement function was provided, call it to obtain the value + // for serialization. + value = callback.call(object, property, value); + } + if (value === null) { + return "null"; + } + className = getClass.call(value); + if (className == booleanClass) { + // Booleans are represented literally. + return "" + value; + } else if (className == numberClass) { + // JSON numbers must be finite. `Infinity` and `NaN` are serialized as + // `"null"`. + return value > -1 / 0 && value < 1 / 0 ? "" + value : "null"; + } else if (className == stringClass) { + // Strings are double-quoted and escaped. + return quote("" + value); + } + // Recursively serialize objects and arrays. + if (typeof value == "object") { + // Check for cyclic structures. This is a linear search; performance + // is inversely proportional to the number of unique nested objects. + for (length = stack.length; length--;) { + if (stack[length] === value) { + // Cyclic structures cannot be serialized by `JSON.stringify`. + throw TypeError(); + } + } + // Add the object to the stack of traversed objects. + stack.push(value); + results = []; + // Save the current indentation level and indent one additional level. + prefix = indentation; + indentation += whitespace; + if (className == arrayClass) { + // Recursively serialize array elements. + for (index = 0, length = value.length; index < length; index++) { + element = serialize(index, value, callback, properties, whitespace, indentation, stack); + results.push(element === undef ? "null" : element); + } + result = results.length ? (whitespace ? "[\n" + indentation + results.join(",\n" + indentation) + "\n" + prefix + "]" : ("[" + results.join(",") + "]")) : "[]"; + } else { + // Recursively serialize object members. Members are selected from + // either a user-specified list of property names, or the object + // itself. + forEach(properties || value, function (property) { + var element = serialize(property, value, callback, properties, whitespace, indentation, stack); + if (element !== undef) { + // According to ES 5.1 section 15.12.3: "If `gap` {whitespace} + // is not the empty string, let `member` {quote(property) + ":"} + // be the concatenation of `member` and the `space` character." + // The "`space` character" refers to the literal space + // character, not the `space` {width} argument provided to + // `JSON.stringify`. + results.push(quote(property) + ":" + (whitespace ? " " : "") + element); + } + }); + result = results.length ? (whitespace ? "{\n" + indentation + results.join(",\n" + indentation) + "\n" + prefix + "}" : ("{" + results.join(",") + "}")) : "{}"; + } + // Remove the object from the traversed object stack. + stack.pop(); + return result; + } + }; + + // Public: `JSON.stringify`. See ES 5.1 section 15.12.3. + exports.stringify = function (source, filter, width) { + var whitespace, callback, properties, className; + if (objectTypes[typeof filter] && filter) { + if ((className = getClass.call(filter)) == functionClass) { + callback = filter; + } else if (className == arrayClass) { + // Convert the property names array into a makeshift set. + properties = {}; + for (var index = 0, length = filter.length, value; index < length; value = filter[index++], ((className = getClass.call(value)), className == stringClass || className == numberClass) && (properties[value] = 1)); + } + } + if (width) { + if ((className = getClass.call(width)) == numberClass) { + // Convert the `width` to an integer and create a string containing + // `width` number of space characters. + if ((width -= width % 1) > 0) { + for (whitespace = "", width > 10 && (width = 10); whitespace.length < width; whitespace += " "); + } + } else if (className == stringClass) { + whitespace = width.length <= 10 ? width : width.slice(0, 10); + } + } + // Opera <= 7.54u2 discards the values associated with empty string keys + // (`""`) only if they are used directly within an object member list + // (e.g., `!("" in { "": 1})`). + return serialize("", (value = {}, value[""] = source, value), callback, properties, whitespace, "", []); + }; + } + + // Public: Parses a JSON source string. + if (!has("json-parse")) { + var fromCharCode = String.fromCharCode; + + // Internal: A map of escaped control characters and their unescaped + // equivalents. + var Unescapes = { + 92: "\\", + 34: '"', + 47: "/", + 98: "\b", + 116: "\t", + 110: "\n", + 102: "\f", + 114: "\r" + }; + + // Internal: Stores the parser state. + var Index, Source; + + // Internal: Resets the parser state and throws a `SyntaxError`. + var abort = function () { + Index = Source = null; + throw SyntaxError(); + }; + + // Internal: Returns the next token, or `"$"` if the parser has reached + // the end of the source string. A token may be a string, number, `null` + // literal, or Boolean literal. + var lex = function () { + var source = Source, length = source.length, value, begin, position, isSigned, charCode; + while (Index < length) { + charCode = source.charCodeAt(Index); + switch (charCode) { + case 9: case 10: case 13: case 32: + // Skip whitespace tokens, including tabs, carriage returns, line + // feeds, and space characters. + Index++; + break; + case 123: case 125: case 91: case 93: case 58: case 44: + // Parse a punctuator token (`{`, `}`, `[`, `]`, `:`, or `,`) at + // the current position. + value = charIndexBuggy ? source.charAt(Index) : source[Index]; + Index++; + return value; + case 34: + // `"` delimits a JSON string; advance to the next character and + // begin parsing the string. String tokens are prefixed with the + // sentinel `@` character to distinguish them from punctuators and + // end-of-string tokens. + for (value = "@", Index++; Index < length;) { + charCode = source.charCodeAt(Index); + if (charCode < 32) { + // Unescaped ASCII control characters (those with a code unit + // less than the space character) are not permitted. + abort(); + } else if (charCode == 92) { + // A reverse solidus (`\`) marks the beginning of an escaped + // control character (including `"`, `\`, and `/`) or Unicode + // escape sequence. + charCode = source.charCodeAt(++Index); + switch (charCode) { + case 92: case 34: case 47: case 98: case 116: case 110: case 102: case 114: + // Revive escaped control characters. + value += Unescapes[charCode]; + Index++; + break; + case 117: + // `\u` marks the beginning of a Unicode escape sequence. + // Advance to the first character and validate the + // four-digit code point. + begin = ++Index; + for (position = Index + 4; Index < position; Index++) { + charCode = source.charCodeAt(Index); + // A valid sequence comprises four hexdigits (case- + // insensitive) that form a single hexadecimal value. + if (!(charCode >= 48 && charCode <= 57 || charCode >= 97 && charCode <= 102 || charCode >= 65 && charCode <= 70)) { + // Invalid Unicode escape sequence. + abort(); + } + } + // Revive the escaped character. + value += fromCharCode("0x" + source.slice(begin, Index)); + break; + default: + // Invalid escape sequence. + abort(); + } + } else { + if (charCode == 34) { + // An unescaped double-quote character marks the end of the + // string. + break; + } + charCode = source.charCodeAt(Index); + begin = Index; + // Optimize for the common case where a string is valid. + while (charCode >= 32 && charCode != 92 && charCode != 34) { + charCode = source.charCodeAt(++Index); + } + // Append the string as-is. + value += source.slice(begin, Index); + } + } + if (source.charCodeAt(Index) == 34) { + // Advance to the next character and return the revived string. + Index++; + return value; + } + // Unterminated string. + abort(); + default: + // Parse numbers and literals. + begin = Index; + // Advance past the negative sign, if one is specified. + if (charCode == 45) { + isSigned = true; + charCode = source.charCodeAt(++Index); + } + // Parse an integer or floating-point value. + if (charCode >= 48 && charCode <= 57) { + // Leading zeroes are interpreted as octal literals. + if (charCode == 48 && ((charCode = source.charCodeAt(Index + 1)), charCode >= 48 && charCode <= 57)) { + // Illegal octal literal. + abort(); + } + isSigned = false; + // Parse the integer component. + for (; Index < length && ((charCode = source.charCodeAt(Index)), charCode >= 48 && charCode <= 57); Index++); + // Floats cannot contain a leading decimal point; however, this + // case is already accounted for by the parser. + if (source.charCodeAt(Index) == 46) { + position = ++Index; + // Parse the decimal component. + for (; position < length && ((charCode = source.charCodeAt(position)), charCode >= 48 && charCode <= 57); position++); + if (position == Index) { + // Illegal trailing decimal. + abort(); + } + Index = position; + } + // Parse exponents. The `e` denoting the exponent is + // case-insensitive. + charCode = source.charCodeAt(Index); + if (charCode == 101 || charCode == 69) { + charCode = source.charCodeAt(++Index); + // Skip past the sign following the exponent, if one is + // specified. + if (charCode == 43 || charCode == 45) { + Index++; + } + // Parse the exponential component. + for (position = Index; position < length && ((charCode = source.charCodeAt(position)), charCode >= 48 && charCode <= 57); position++); + if (position == Index) { + // Illegal empty exponent. + abort(); + } + Index = position; + } + // Coerce the parsed value to a JavaScript number. + return +source.slice(begin, Index); + } + // A negative sign may only precede numbers. + if (isSigned) { + abort(); + } + // `true`, `false`, and `null` literals. + if (source.slice(Index, Index + 4) == "true") { + Index += 4; + return true; + } else if (source.slice(Index, Index + 5) == "false") { + Index += 5; + return false; + } else if (source.slice(Index, Index + 4) == "null") { + Index += 4; + return null; + } + // Unrecognized token. + abort(); + } + } + // Return the sentinel `$` character if the parser has reached the end + // of the source string. + return "$"; + }; + + // Internal: Parses a JSON `value` token. + var get = function (value) { + var results, hasMembers; + if (value == "$") { + // Unexpected end of input. + abort(); + } + if (typeof value == "string") { + if ((charIndexBuggy ? value.charAt(0) : value[0]) == "@") { + // Remove the sentinel `@` character. + return value.slice(1); + } + // Parse object and array literals. + if (value == "[") { + // Parses a JSON array, returning a new JavaScript array. + results = []; + for (;; hasMembers || (hasMembers = true)) { + value = lex(); + // A closing square bracket marks the end of the array literal. + if (value == "]") { + break; + } + // If the array literal contains elements, the current token + // should be a comma separating the previous element from the + // next. + if (hasMembers) { + if (value == ",") { + value = lex(); + if (value == "]") { + // Unexpected trailing `,` in array literal. + abort(); + } + } else { + // A `,` must separate each array element. + abort(); + } + } + // Elisions and leading commas are not permitted. + if (value == ",") { + abort(); + } + results.push(get(value)); + } + return results; + } else if (value == "{") { + // Parses a JSON object, returning a new JavaScript object. + results = {}; + for (;; hasMembers || (hasMembers = true)) { + value = lex(); + // A closing curly brace marks the end of the object literal. + if (value == "}") { + break; + } + // If the object literal contains members, the current token + // should be a comma separator. + if (hasMembers) { + if (value == ",") { + value = lex(); + if (value == "}") { + // Unexpected trailing `,` in object literal. + abort(); + } + } else { + // A `,` must separate each object member. + abort(); + } + } + // Leading commas are not permitted, object property names must be + // double-quoted strings, and a `:` must separate each property + // name and value. + if (value == "," || typeof value != "string" || (charIndexBuggy ? value.charAt(0) : value[0]) != "@" || lex() != ":") { + abort(); + } + results[value.slice(1)] = get(lex()); + } + return results; + } + // Unexpected token encountered. + abort(); + } + return value; + }; + + // Internal: Updates a traversed object member. + var update = function (source, property, callback) { + var element = walk(source, property, callback); + if (element === undef) { + delete source[property]; + } else { + source[property] = element; + } + }; + + // Internal: Recursively traverses a parsed JSON object, invoking the + // `callback` function for each value. This is an implementation of the + // `Walk(holder, name)` operation defined in ES 5.1 section 15.12.2. + var walk = function (source, property, callback) { + var value = source[property], length; + if (typeof value == "object" && value) { + // `forEach` can't be used to traverse an array in Opera <= 8.54 + // because its `Object#hasOwnProperty` implementation returns `false` + // for array indices (e.g., `![1, 2, 3].hasOwnProperty("0")`). + if (getClass.call(value) == arrayClass) { + for (length = value.length; length--;) { + update(value, length, callback); + } + } else { + forEach(value, function (property) { + update(value, property, callback); + }); + } + } + return callback.call(source, property, value); + }; + + // Public: `JSON.parse`. See ES 5.1 section 15.12.2. + exports.parse = function (source, callback) { + var result, value; + Index = 0; + Source = "" + source; + result = get(lex()); + // If a JSON string contains multiple tokens, it is invalid. + if (lex() != "$") { + abort(); + } + // Reset the parser state. + Index = Source = null; + return callback && getClass.call(callback) == functionClass ? walk((value = {}, value[""] = result, value), "", callback) : result; + }; + } + } + + exports["runInContext"] = runInContext; + return exports; + } + + if (freeExports && !isLoader) { + // Export for CommonJS environments. + runInContext(root, freeExports); + } else { + // Export for web browsers and JavaScript engines. + var nativeJSON = root.JSON, + previousJSON = root["JSON3"], + isRestored = false; + + var JSON3 = runInContext(root, (root["JSON3"] = { + // Public: Restores the original value of the global `JSON` object and + // returns a reference to the `JSON3` object. + "noConflict": function () { + if (!isRestored) { + isRestored = true; + root.JSON = nativeJSON; + root["JSON3"] = previousJSON; + nativeJSON = previousJSON = null; + } + return JSON3; + } + })); + + root.JSON = { + "parse": JSON3.parse, + "stringify": JSON3.stringify + }; + } + + // Export for asynchronous module loaders. + if (isLoader) { + define(function () { + return JSON3; + }); + } +}).call(this); + +}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {}) +},{}],35:[function(_dereq_,module,exports){ +/** + * Helpers. + */ + +var s = 1000; +var m = s * 60; +var h = m * 60; +var d = h * 24; +var y = d * 365.25; + +/** + * Parse or format the given `val`. + * + * Options: + * + * - `long` verbose formatting [false] + * + * @param {String|Number} val + * @param {Object} options + * @return {String|Number} + * @api public + */ + +module.exports = function(val, options){ + options = options || {}; + if ('string' == typeof val) return parse(val); + return options.long + ? long(val) + : short(val); +}; + +/** + * Parse the given `str` and return milliseconds. + * + * @param {String} str + * @return {Number} + * @api private + */ + +function parse(str) { + str = '' + str; + if (str.length > 10000) return; + var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(str); + if (!match) return; + var n = parseFloat(match[1]); + var type = (match[2] || 'ms').toLowerCase(); + switch (type) { + case 'years': + case 'year': + case 'yrs': + case 'yr': + case 'y': + return n * y; + case 'days': + case 'day': + case 'd': + return n * d; + case 'hours': + case 'hour': + case 'hrs': + case 'hr': + case 'h': + return n * h; + case 'minutes': + case 'minute': + case 'mins': + case 'min': + case 'm': + return n * m; + case 'seconds': + case 'second': + case 'secs': + case 'sec': + case 's': + return n * s; + case 'milliseconds': + case 'millisecond': + case 'msecs': + case 'msec': + case 'ms': + return n; + } +} + +/** + * Short format for `ms`. + * + * @param {Number} ms + * @return {String} + * @api private + */ + +function short(ms) { + if (ms >= d) return Math.round(ms / d) + 'd'; + if (ms >= h) return Math.round(ms / h) + 'h'; + if (ms >= m) return Math.round(ms / m) + 'm'; + if (ms >= s) return Math.round(ms / s) + 's'; + return ms + 'ms'; +} + +/** + * Long format for `ms`. + * + * @param {Number} ms + * @return {String} + * @api private + */ + +function long(ms) { + return plural(ms, d, 'day') + || plural(ms, h, 'hour') + || plural(ms, m, 'minute') + || plural(ms, s, 'second') + || ms + ' ms'; +} + +/** + * Pluralization helper. + */ + +function plural(ms, n, name) { + if (ms < n) return; + if (ms < n * 1.5) return Math.floor(ms / n) + ' ' + name; + return Math.ceil(ms / n) + ' ' + name + 's'; +} + +},{}],36:[function(_dereq_,module,exports){ +(function (global){ +/** + * 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+$/; + +module.exports = function parsejson(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))(); + } +}; +}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {}) +},{}],37:[function(_dereq_,module,exports){ +/** + * Compiles a querystring + * Returns string representation of the object + * + * @param {Object} + * @api private + */ + +exports.encode = function (obj) { + var str = ''; + + for (var i in obj) { + if (obj.hasOwnProperty(i)) { + if (str.length) str += '&'; + str += encodeURIComponent(i) + '=' + encodeURIComponent(obj[i]); + } + } + + return str; +}; + +/** + * Parses a simple querystring into an object + * + * @param {String} qs + * @api private + */ + +exports.decode = function(qs){ + var qry = {}; + var pairs = qs.split('&'); + for (var i = 0, l = pairs.length; i < l; i++) { + var pair = pairs[i].split('='); + qry[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]); + } + return qry; +}; + +},{}],38:[function(_dereq_,module,exports){ +/** + * Parses an URI + * + * @author Steven Levithan <stevenlevithan.com> (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' +]; + +module.exports = function parseuri(str) { + var src = str, + b = str.indexOf('['), + e = str.indexOf(']'); + + if (b != -1 && e != -1) { + str = str.substring(0, b) + str.substring(b, e).replace(/:/g, ';') + str.substring(e, str.length); + } + + var m = re.exec(str || ''), + uri = {}, + i = 14; + + while (i--) { + uri[parts[i]] = m[i] || ''; + } + + if (b != -1 && e != -1) { + uri.source = src; + uri.host = uri.host.substring(1, uri.host.length - 1).replace(/;/g, ':'); + uri.authority = uri.authority.replace('[', '').replace(']', '').replace(/;/g, ':'); + uri.ipv6uri = true; + } + + return uri; +}; + +},{}],39:[function(_dereq_,module,exports){ +(function (global){ +/*global Blob,File*/ + +/** + * Module requirements + */ + +var isArray = _dereq_('isarray'); +var isBuf = _dereq_('./is-buffer'); + +/** + * Replaces every Buffer | ArrayBuffer in packet with a numbered placeholder. + * Anything with blobs or files should be fed through removeBlobs before coming + * here. + * + * @param {Object} packet - socket.io event packet + * @return {Object} with deconstructed packet and list of buffers + * @api public + */ + +exports.deconstructPacket = function(packet){ + var buffers = []; + var packetData = packet.data; + + function _deconstructPacket(data) { + if (!data) return data; + + if (isBuf(data)) { + var placeholder = { _placeholder: true, num: buffers.length }; + buffers.push(data); + return placeholder; + } else if (isArray(data)) { + var newData = new Array(data.length); + for (var i = 0; i < data.length; i++) { + newData[i] = _deconstructPacket(data[i]); + } + return newData; + } else if ('object' == typeof data && !(data instanceof Date)) { + var newData = {}; + for (var key in data) { + newData[key] = _deconstructPacket(data[key]); + } + return newData; + } + return data; + } + + var pack = packet; + pack.data = _deconstructPacket(packetData); + pack.attachments = buffers.length; // number of binary 'attachments' + return {packet: pack, buffers: buffers}; +}; + +/** + * Reconstructs a binary packet from its placeholder packet and buffers + * + * @param {Object} packet - event packet with placeholders + * @param {Array} buffers - binary buffers to put in placeholder positions + * @return {Object} reconstructed packet + * @api public + */ + +exports.reconstructPacket = function(packet, buffers) { + var curPlaceHolder = 0; + + function _reconstructPacket(data) { + if (data && data._placeholder) { + var buf = buffers[data.num]; // appropriate buffer (should be natural order anyway) + return buf; + } else if (isArray(data)) { + for (var i = 0; i < data.length; i++) { + data[i] = _reconstructPacket(data[i]); + } + return data; + } else if (data && 'object' == typeof data) { + for (var key in data) { + data[key] = _reconstructPacket(data[key]); + } + return data; + } + return data; + } + + packet.data = _reconstructPacket(packet.data); + packet.attachments = undefined; // no longer useful + return packet; +}; + +/** + * Asynchronously removes Blobs or Files from data via + * FileReader's readAsArrayBuffer method. Used before encoding + * data as msgpack. Calls callback with the blobless data. + * + * @param {Object} data + * @param {Function} callback + * @api private + */ + +exports.removeBlobs = function(data, callback) { + function _removeBlobs(obj, curKey, containingObject) { + if (!obj) return obj; + + // convert any blob + if ((global.Blob && obj instanceof Blob) || + (global.File && obj instanceof File)) { + pendingBlobs++; + + // async filereader + var fileReader = new FileReader(); + fileReader.onload = function() { // this.result == arraybuffer + if (containingObject) { + containingObject[curKey] = this.result; + } + else { + bloblessData = this.result; + } + + // if nothing pending its callback time + if(! --pendingBlobs) { + callback(bloblessData); + } + }; + + fileReader.readAsArrayBuffer(obj); // blob -> arraybuffer + } else if (isArray(obj)) { // handle array + for (var i = 0; i < obj.length; i++) { + _removeBlobs(obj[i], i, obj); + } + } else if (obj && 'object' == typeof obj && !isBuf(obj)) { // and object + for (var key in obj) { + _removeBlobs(obj[key], key, obj); + } + } + } + + var pendingBlobs = 0; + var bloblessData = data; + _removeBlobs(bloblessData); + if (!pendingBlobs) { + callback(bloblessData); + } +}; + +}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {}) +},{"./is-buffer":41,"isarray":33}],40:[function(_dereq_,module,exports){ + +/** + * Module dependencies. + */ + +var debug = _dereq_('debug')('socket.io-parser'); +var json = _dereq_('json3'); +var isArray = _dereq_('isarray'); +var Emitter = _dereq_('component-emitter'); +var binary = _dereq_('./binary'); +var isBuf = _dereq_('./is-buffer'); + +/** + * Protocol version. + * + * @api public + */ + +exports.protocol = 4; + +/** + * Packet types. + * + * @api public + */ + +exports.types = [ + 'CONNECT', + 'DISCONNECT', + 'EVENT', + 'ACK', + 'ERROR', + 'BINARY_EVENT', + 'BINARY_ACK' +]; + +/** + * Packet type `connect`. + * + * @api public + */ + +exports.CONNECT = 0; + +/** + * Packet type `disconnect`. + * + * @api public + */ + +exports.DISCONNECT = 1; + +/** + * Packet type `event`. + * + * @api public + */ + +exports.EVENT = 2; + +/** + * Packet type `ack`. + * + * @api public + */ + +exports.ACK = 3; + +/** + * Packet type `error`. + * + * @api public + */ + +exports.ERROR = 4; + +/** + * Packet type 'binary event' + * + * @api public + */ + +exports.BINARY_EVENT = 5; + +/** + * Packet type `binary ack`. For acks with binary arguments. + * + * @api public + */ + +exports.BINARY_ACK = 6; + +/** + * Encoder constructor. + * + * @api public + */ + +exports.Encoder = Encoder; + +/** + * Decoder constructor. + * + * @api public + */ + +exports.Decoder = Decoder; + +/** + * A socket.io Encoder instance + * + * @api public + */ + +function Encoder() {} + +/** + * Encode a packet as a single string if non-binary, or as a + * buffer sequence, depending on packet type. + * + * @param {Object} obj - packet object + * @param {Function} callback - function to handle encodings (likely engine.write) + * @return Calls callback with Array of encodings + * @api public + */ + +Encoder.prototype.encode = function(obj, callback){ + debug('encoding packet %j', obj); + + if (exports.BINARY_EVENT == obj.type || exports.BINARY_ACK == obj.type) { + encodeAsBinary(obj, callback); + } + else { + var encoding = encodeAsString(obj); + callback([encoding]); + } +}; + +/** + * Encode packet as string. + * + * @param {Object} packet + * @return {String} encoded + * @api private + */ + +function encodeAsString(obj) { + var str = ''; + var nsp = false; + + // first is type + str += obj.type; + + // attachments if we have them + if (exports.BINARY_EVENT == obj.type || exports.BINARY_ACK == obj.type) { + str += obj.attachments; + str += '-'; + } + + // if we have a namespace other than `/` + // we append it followed by a comma `,` + if (obj.nsp && '/' != obj.nsp) { + nsp = true; + str += obj.nsp; + } + + // immediately followed by the id + if (null != obj.id) { + if (nsp) { + str += ','; + nsp = false; + } + str += obj.id; + } + + // json data + if (null != obj.data) { + if (nsp) str += ','; + str += json.stringify(obj.data); + } + + debug('encoded %j as %s', obj, str); + return str; +} + +/** + * Encode packet as 'buffer sequence' by removing blobs, and + * deconstructing packet into object with placeholders and + * a list of buffers. + * + * @param {Object} packet + * @return {Buffer} encoded + * @api private + */ + +function encodeAsBinary(obj, callback) { + + function writeEncoding(bloblessData) { + var deconstruction = binary.deconstructPacket(bloblessData); + var pack = encodeAsString(deconstruction.packet); + var buffers = deconstruction.buffers; + + buffers.unshift(pack); // add packet info to beginning of data list + callback(buffers); // write all the buffers + } + + binary.removeBlobs(obj, writeEncoding); +} + +/** + * A socket.io Decoder instance + * + * @return {Object} decoder + * @api public + */ + +function Decoder() { + this.reconstructor = null; +} + +/** + * Mix in `Emitter` with Decoder. + */ + +Emitter(Decoder.prototype); + +/** + * Decodes an ecoded packet string into packet JSON. + * + * @param {String} obj - encoded packet + * @return {Object} packet + * @api public + */ + +Decoder.prototype.add = function(obj) { + var packet; + if ('string' == typeof obj) { + packet = decodeString(obj); + if (exports.BINARY_EVENT == packet.type || exports.BINARY_ACK == packet.type) { // binary packet's json + this.reconstructor = new BinaryReconstructor(packet); + + // no attachments, labeled binary but no binary data to follow + if (this.reconstructor.reconPack.attachments === 0) { + this.emit('decoded', packet); + } + } else { // non-binary full packet + this.emit('decoded', packet); + } + } + else if (isBuf(obj) || obj.base64) { // raw binary data + if (!this.reconstructor) { + throw new Error('got binary data when not reconstructing a packet'); + } else { + packet = this.reconstructor.takeBinaryData(obj); + if (packet) { // received final buffer + this.reconstructor = null; + this.emit('decoded', packet); + } + } + } + else { + throw new Error('Unknown type: ' + obj); + } +}; + +/** + * Decode a packet String (JSON data) + * + * @param {String} str + * @return {Object} packet + * @api private + */ + +function decodeString(str) { + var p = {}; + var i = 0; + + // look up type + p.type = Number(str.charAt(0)); + if (null == exports.types[p.type]) return error(); + + // look up attachments if type binary + if (exports.BINARY_EVENT == p.type || exports.BINARY_ACK == p.type) { + var buf = ''; + while (str.charAt(++i) != '-') { + buf += str.charAt(i); + if (i == str.length) break; + } + if (buf != Number(buf) || str.charAt(i) != '-') { + throw new Error('Illegal attachments'); + } + p.attachments = Number(buf); + } + + // look up namespace (if any) + if ('/' == str.charAt(i + 1)) { + p.nsp = ''; + while (++i) { + var c = str.charAt(i); + if (',' == c) break; + p.nsp += c; + if (i == str.length) break; + } + } else { + p.nsp = '/'; + } + + // look up id + var next = str.charAt(i + 1); + if ('' !== next && Number(next) == next) { + p.id = ''; + while (++i) { + var c = str.charAt(i); + if (null == c || Number(c) != c) { + --i; + break; + } + p.id += str.charAt(i); + if (i == str.length) break; + } + p.id = Number(p.id); + } + + // look up json data + if (str.charAt(++i)) { + try { + p.data = json.parse(str.substr(i)); + } catch(e){ + return error(); + } + } + + debug('decoded %s as %j', str, p); + return p; +} + +/** + * Deallocates a parser's resources + * + * @api public + */ + +Decoder.prototype.destroy = function() { + if (this.reconstructor) { + this.reconstructor.finishedReconstruction(); + } +}; + +/** + * A manager of a binary event's 'buffer sequence'. Should + * be constructed whenever a packet of type BINARY_EVENT is + * decoded. + * + * @param {Object} packet + * @return {BinaryReconstructor} initialized reconstructor + * @api private + */ + +function BinaryReconstructor(packet) { + this.reconPack = packet; + this.buffers = []; +} + +/** + * Method to be called when binary data received from connection + * after a BINARY_EVENT packet. + * + * @param {Buffer | ArrayBuffer} binData - the raw binary data received + * @return {null | Object} returns null if more binary data is expected or + * a reconstructed packet object if all buffers have been received. + * @api private + */ + +BinaryReconstructor.prototype.takeBinaryData = function(binData) { + this.buffers.push(binData); + if (this.buffers.length == this.reconPack.attachments) { // done with buffer list + var packet = binary.reconstructPacket(this.reconPack, this.buffers); + this.finishedReconstruction(); + return packet; + } + return null; +}; + +/** + * Cleans up binary packet reconstruction variables. + * + * @api private + */ + +BinaryReconstructor.prototype.finishedReconstruction = function() { + this.reconPack = null; + this.buffers = []; +}; + +function error(data){ + return { + type: exports.ERROR, + data: 'parser error' + }; +} + +},{"./binary":39,"./is-buffer":41,"component-emitter":42,"debug":14,"isarray":33,"json3":34}],41:[function(_dereq_,module,exports){ +(function (global){ + +module.exports = isBuf; + +/** + * Returns true if obj is a buffer or an arraybuffer. + * + * @api private + */ + +function isBuf(obj) { + return (global.Buffer && global.Buffer.isBuffer(obj)) || + (global.ArrayBuffer && obj instanceof ArrayBuffer); +} + +}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {}) +},{}],42:[function(_dereq_,module,exports){ +arguments[4][26][0].apply(exports,arguments) +},{"dup":26}],43:[function(_dereq_,module,exports){ +module.exports = toArray + +function toArray(list, index) { + var array = [] + + index = index || 0 + + for (var i = index || 0; i < list.length; i++) { + array[i - index] = list[i] + } + + return array +} + +},{}],44:[function(_dereq_,module,exports){ +(function (global){ +/*! https://mths.be/utf8js v2.0.0 by @mathias */ +;(function(root) { + + // Detect free variables `exports` + var freeExports = typeof exports == 'object' && exports; + + // Detect free variable `module` + var freeModule = typeof module == 'object' && module && + module.exports == freeExports && module; + + // Detect free variable `global`, from Node.js or Browserified code, + // and use it as `root` + var freeGlobal = typeof global == 'object' && global; + if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { + root = freeGlobal; + } + + /*--------------------------------------------------------------------------*/ + + var stringFromCharCode = String.fromCharCode; + + // Taken from https://mths.be/punycode + function ucs2decode(string) { + var output = []; + var counter = 0; + var length = string.length; + var value; + var extra; + while (counter < length) { + value = string.charCodeAt(counter++); + if (value >= 0xD800 && value <= 0xDBFF && counter < length) { + // high surrogate, and there is a next character + extra = string.charCodeAt(counter++); + if ((extra & 0xFC00) == 0xDC00) { // low surrogate + output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); + } else { + // unmatched surrogate; only append this code unit, in case the next + // code unit is the high surrogate of a surrogate pair + output.push(value); + counter--; + } + } else { + output.push(value); + } + } + return output; + } + + // Taken from https://mths.be/punycode + function ucs2encode(array) { + var length = array.length; + var index = -1; + var value; + var output = ''; + while (++index < length) { + value = array[index]; + if (value > 0xFFFF) { + value -= 0x10000; + output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800); + value = 0xDC00 | value & 0x3FF; + } + output += stringFromCharCode(value); + } + return output; + } + + function checkScalarValue(codePoint) { + if (codePoint >= 0xD800 && codePoint <= 0xDFFF) { + throw Error( + 'Lone surrogate U+' + codePoint.toString(16).toUpperCase() + + ' is not a scalar value' + ); + } + } + /*--------------------------------------------------------------------------*/ + + function createByte(codePoint, shift) { + return stringFromCharCode(((codePoint >> shift) & 0x3F) | 0x80); + } + + function encodeCodePoint(codePoint) { + if ((codePoint & 0xFFFFFF80) == 0) { // 1-byte sequence + return stringFromCharCode(codePoint); + } + var symbol = ''; + if ((codePoint & 0xFFFFF800) == 0) { // 2-byte sequence + symbol = stringFromCharCode(((codePoint >> 6) & 0x1F) | 0xC0); + } + else if ((codePoint & 0xFFFF0000) == 0) { // 3-byte sequence + checkScalarValue(codePoint); + symbol = stringFromCharCode(((codePoint >> 12) & 0x0F) | 0xE0); + symbol += createByte(codePoint, 6); + } + else if ((codePoint & 0xFFE00000) == 0) { // 4-byte sequence + symbol = stringFromCharCode(((codePoint >> 18) & 0x07) | 0xF0); + symbol += createByte(codePoint, 12); + symbol += createByte(codePoint, 6); + } + symbol += stringFromCharCode((codePoint & 0x3F) | 0x80); + return symbol; + } + + function utf8encode(string) { + var codePoints = ucs2decode(string); + var length = codePoints.length; + var index = -1; + var codePoint; + var byteString = ''; + while (++index < length) { + codePoint = codePoints[index]; + byteString += encodeCodePoint(codePoint); + } + return byteString; + } + + /*--------------------------------------------------------------------------*/ + + function readContinuationByte() { + if (byteIndex >= byteCount) { + throw Error('Invalid byte index'); + } + + var continuationByte = byteArray[byteIndex] & 0xFF; + byteIndex++; + + if ((continuationByte & 0xC0) == 0x80) { + return continuationByte & 0x3F; + } + + // If we end up here, it’s not a continuation byte + throw Error('Invalid continuation byte'); + } + + function decodeSymbol() { + var byte1; + var byte2; + var byte3; + var byte4; + var codePoint; + + if (byteIndex > byteCount) { + throw Error('Invalid byte index'); + } + + if (byteIndex == byteCount) { + return false; + } + + // Read first byte + byte1 = byteArray[byteIndex] & 0xFF; + byteIndex++; + + // 1-byte sequence (no continuation bytes) + if ((byte1 & 0x80) == 0) { + return byte1; + } + + // 2-byte sequence + if ((byte1 & 0xE0) == 0xC0) { + var byte2 = readContinuationByte(); + codePoint = ((byte1 & 0x1F) << 6) | byte2; + if (codePoint >= 0x80) { + return codePoint; + } else { + throw Error('Invalid continuation byte'); + } + } + + // 3-byte sequence (may include unpaired surrogates) + if ((byte1 & 0xF0) == 0xE0) { + byte2 = readContinuationByte(); + byte3 = readContinuationByte(); + codePoint = ((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3; + if (codePoint >= 0x0800) { + checkScalarValue(codePoint); + return codePoint; + } else { + throw Error('Invalid continuation byte'); + } + } + + // 4-byte sequence + if ((byte1 & 0xF8) == 0xF0) { + byte2 = readContinuationByte(); + byte3 = readContinuationByte(); + byte4 = readContinuationByte(); + codePoint = ((byte1 & 0x0F) << 0x12) | (byte2 << 0x0C) | + (byte3 << 0x06) | byte4; + if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) { + return codePoint; + } + } + + throw Error('Invalid UTF-8 detected'); + } + + var byteArray; + var byteCount; + var byteIndex; + function utf8decode(byteString) { + byteArray = ucs2decode(byteString); + byteCount = byteArray.length; + byteIndex = 0; + var codePoints = []; + var tmp; + while ((tmp = decodeSymbol()) !== false) { + codePoints.push(tmp); + } + return ucs2encode(codePoints); + } + + /*--------------------------------------------------------------------------*/ + + var utf8 = { + 'version': '2.0.0', + 'encode': utf8encode, + 'decode': utf8decode + }; + + // Some AMD build optimizers, like r.js, check for specific condition patterns + // like the following: + if ( + typeof define == 'function' && + typeof define.amd == 'object' && + define.amd + ) { + define(function() { + return utf8; + }); + } else if (freeExports && !freeExports.nodeType) { + if (freeModule) { // in Node.js or RingoJS v0.8.0+ + freeModule.exports = utf8; + } else { // in Narwhal or RingoJS v0.7.0- + var object = {}; + var hasOwnProperty = object.hasOwnProperty; + for (var key in utf8) { + hasOwnProperty.call(utf8, key) && (freeExports[key] = utf8[key]); + } + } + } else { // in Rhino or a web browser + root.utf8 = utf8; + } + +}(this)); + +}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {}) +},{}],45:[function(_dereq_,module,exports){ +'use strict'; + +var alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_'.split('') + , length = 64 + , map = {} + , seed = 0 + , i = 0 + , prev; + +/** + * Return a string representing the specified number. + * + * @param {Number} num The number to convert. + * @returns {String} The string representation of the number. + * @api public + */ +function encode(num) { + var encoded = ''; + + do { + encoded = alphabet[num % length] + encoded; + num = Math.floor(num / length); + } while (num > 0); + + return encoded; +} + +/** + * Return the integer value specified by the given string. + * + * @param {String} str The string to convert. + * @returns {Number} The integer value represented by the string. + * @api public + */ +function decode(str) { + var decoded = 0; + + for (i = 0; i < str.length; i++) { + decoded = decoded * length + map[str.charAt(i)]; + } + + return decoded; +} + +/** + * Yeast: A tiny growing id generator. + * + * @returns {String} A unique id. + * @api public + */ +function yeast() { + var now = encode(+new Date()); + + if (now !== prev) return seed = 0, prev = now; + return now +'.'+ encode(seed++); +} + +// +// Map each character to its index. +// +for (; i < length; i++) map[alphabet[i]] = i; + +// +// Expose the `yeast`, `encode` and `decode` functions. +// +yeast.encode = encode; +yeast.decode = decode; +module.exports = yeast; + +},{}]},{},[1])(1) +});
\ No newline at end of file diff --git a/bundles/org.eclipse.orion.client.editor/web/orion/editor/contentAssist.js b/bundles/org.eclipse.orion.client.editor/web/orion/editor/contentAssist.js index ffa14c0..77c32a4 100644 --- a/bundles/org.eclipse.orion.client.editor/web/orion/editor/contentAssist.js +++ b/bundles/org.eclipse.orion.client.editor/web/orion/editor/contentAssist.js @@ -191,14 +191,15 @@ define("orion/editor/contentAssist", [ //$NON-NLS-0$ };
this.setState(State.INACTIVE);
var proposalText = typeof proposal === "string" ? proposal : proposal.proposal; //$NON-NLS-0$
- view.setText(proposalText, start, end);
+ var offset = 0;
if (proposal.additionalEdits) {
- var edit;
- for (var i = 0; i < proposal.additionalEdits.length; i++) {
- edit = proposal.additionalEdits[i];
+ for (var i = proposal.additionalEdits.length - 1; i >= 0 ; i--) {
+ var edit = proposal.additionalEdits[i];
view.setText(edit.text, edit.offset, edit.offset + edit.length);
+ offset += edit.text.length;
}
}
+ view.setText(proposalText, start + offset, end + offset);
this.dispatchEvent({type: "ProposalApplied", data: data}); //$NON-NLS-0$
mMetrics.logEvent("contentAssist", "apply"); //$NON-NLS-1$ //$NON-NLS-0$
return true;
diff --git a/bundles/org.eclipse.orion.client.editor/web/orion/editor/editor.js b/bundles/org.eclipse.orion.client.editor/web/orion/editor/editor.js index 0949af5..31e543c 100644 --- a/bundles/org.eclipse.orion.client.editor/web/orion/editor/editor.js +++ b/bundles/org.eclipse.orion.client.editor/web/orion/editor/editor.js @@ -1165,7 +1165,7 @@ define("orion/editor/editor", [ //$NON-NLS-0$ if (createAnnotation) { annotation = createAnnotation(annotation); } else { - var start, end; + var start, end, lineIndex, lineStart; if (annotation.lineStart && annotation.lineEnd){ start = model.getLineStart(annotation.lineStart); // If the closing line number of the modified range is on the last line, @@ -1176,10 +1176,17 @@ define("orion/editor/editor", [ //$NON-NLS-0$ } else if (typeof annotation.line === "number") { //$NON-NLS-0$ // line/column - var lineIndex = annotation.line - 1; - var lineStart = model.getLineStart(lineIndex); + lineIndex = annotation.line - 1; + lineStart = model.getLineStart(lineIndex); start = lineStart + annotation.start - 1; end = lineStart + annotation.end - 1; + } else if (annotation.range) { + lineIndex = annotation.range.start.line; + lineStart = model.getLineStart(lineIndex); + start = lineStart + annotation.range.start.character; + lineIndex = annotation.range.end.line; + lineStart = model.getLineStart(lineIndex); + end = lineStart + annotation.range.end.character; } else { // document offsets start = annotation.start; diff --git a/bundles/org.eclipse.orion.client.editor/web/orion/editor/textView.js b/bundles/org.eclipse.orion.client.editor/web/orion/editor/textView.js index 97b5306..52908d3 100644 --- a/bundles/org.eclipse.orion.client.editor/web/orion/editor/textView.js +++ b/bundles/org.eclipse.orion.client.editor/web/orion/editor/textView.js @@ -506,6 +506,9 @@ define("orion/editor/textView", [ //$NON-NLS-1$ //W3C var sel = win.getSelection(); range = doc.createRange(); + if(start.offset > end.offset) { + start.offset = end.offset; + } range.setStart(start.node, start.offset); range.setEnd(end.node, end.offset); if (view._hasFocus && ( @@ -6608,11 +6611,14 @@ define("orion/editor/textView", [ //$NON-NLS-1$ if (e.text === null || e.text === undefined) { return false; } + var previousSelection; + if (e.preserveSelection) previousSelection = this._getSelections(); + if (e.selection.length > 1) this.setRedraw(false); var undo = this._compoundChange; if (undo) { - if (!Selection.compare(this._getSelections(), undo.owner.selection)) { + if (!Selection.compare(previousSelection || this._getSelections(), undo.owner.selection)) { this._endUndo(); if (e.selection.length > 1) this._startUndo(); } @@ -6623,23 +6629,29 @@ define("orion/editor/textView", [ //$NON-NLS-1$ var model = this._model; try { if (e._ignoreDOMSelection) { this._ignoreDOMSelection = true; } - var offset = 0, i = 0; - e.selection.forEach(function(selection) { + var offset = 0; + e.selection.forEach(function(selection, i) { selection.start += offset; selection.end += offset; var text = Array.isArray(e.text) ? e.text[i] : e.text; model.setText(text, selection.start, selection.end); - offset += (selection.start - selection.end) + text.length; + var delta = selection.start - selection.end + text.length; + offset += delta; + if (previousSelection) { + previousSelection.forEach(function(ps) { + if (ps.start > selection.start) ps.start += delta; + if (ps.end > selection.start) ps.end += delta; + }); + } selection.setCaret(caretAtEnd ? selection.start + text.length : selection.start); - i++; }); } finally { if (e._ignoreDOMSelection) { this._ignoreDOMSelection = false; } } - this._setSelection(e.selection, show, true, callback); + this._setSelection(previousSelection || e.selection, show, true, callback); undo = this._compoundChange; - if (undo) undo.owner.selection = e.selection; + if (undo) undo.owner.selection = previousSelection || e.selection; if (e.selection.length > 1) this.setRedraw(true); diff --git a/bundles/org.eclipse.orion.client.javascript/web/js-tests/javascript/validatorTests.js b/bundles/org.eclipse.orion.client.javascript/web/js-tests/javascript/validatorTests.js index 08be33b..f37c16e 100644 --- a/bundles/org.eclipse.orion.client.javascript/web/js-tests/javascript/validatorTests.js +++ b/bundles/org.eclipse.orion.client.javascript/web/js-tests/javascript/validatorTests.js @@ -319,7 +319,7 @@ define([ assertProblems(problems, [
{start: 38,
end: 39,
- severity: 'error', + severity: 'error',
description: "Identifier directly after number"
}
]);
diff --git a/bundles/org.eclipse.orion.client.ui/web/edit/setup.js b/bundles/org.eclipse.orion.client.ui/web/edit/setup.js index 9f221d5..f81bee0 100644 --- a/bundles/org.eclipse.orion.client.ui/web/edit/setup.js +++ b/bundles/org.eclipse.orion.client.ui/web/edit/setup.js @@ -743,7 +743,7 @@ objects.mixin(EditorSetup.prototype, { * @since 9.0 */ openEditor: function(loc, options) { - var href = this.computeNavigationHref({Location: loc}, {start: options.start, end: options.end}); + var href = this.computeNavigationHref({Location: loc}, {start: options.start, end: options.end, line: options.line, offset: options.offset, length: options.length}); if (!href) return; diff --git a/bundles/org.eclipse.orion.client.ui/web/orion/edit/dispatcher.js b/bundles/org.eclipse.orion.client.ui/web/orion/edit/dispatcher.js index 9e55a64..43fff2f 100644 --- a/bundles/org.eclipse.orion.client.ui/web/orion/edit/dispatcher.js +++ b/bundles/org.eclipse.orion.client.ui/web/orion/edit/dispatcher.js @@ -85,6 +85,21 @@ define([], function() { var listener = function(evnt) { // Inject metadata about the file being edited into the event. evnt.file = _self.getServiceFileObject(); + //TODO - total hack for the language server + if (evnt.type === "ModelChanging") { + var model = textView.getModel(); + var sl = model.getLineAtOffset(evnt.start), el = this.getLineAtOffset(evnt.start + evnt.removedCharCount); + evnt.range = { + start: { + line: sl, + character: evnt.start - model.getLineStart(sl) + }, + end: { + line: el, + character: evnt.start + evnt.removedCharCount - model.getLineStart(el) + } + }; + } serviceMethod(evnt).then(/*No return value*/); }; var serviceId = serviceReference.getProperty('service.id'); //$NON-NLS-0$ diff --git a/bundles/org.eclipse.orion.client.ui/web/orion/lsp/ipc.js b/bundles/org.eclipse.orion.client.ui/web/orion/lsp/ipc.js new file mode 100644 index 0000000..9ade03c --- /dev/null +++ b/bundles/org.eclipse.orion.client.ui/web/orion/lsp/ipc.js @@ -0,0 +1,767 @@ +/******************************************************************************* + * @license + * Copyright (c) 2016 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials are made + * available under the terms of the Eclipse Public License v1.0 + * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution + * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +/*eslint-env amd, browser*/ +define([ + "orion/Deferred", + "socketio/socket.io" +], function(Deferred, io) { + + /** + * The object of error codes + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#response-message + */ + IPC.prototype.ERROR_CODES = Object.freeze({ + ParseError: -32700, + InvalidRequest: -32600, + MethodNotFound: -32601, + InvalidParams: -32602, + InternalError: -32603, + ServerErrorStart: -32099, + ServerErrorEnd: -32000, + }); + + /** + * The map of error types + */ + IPC.prototype.ERROR_TYPES = Object.freeze({ + 1: 'error', + 2: 'warn', + 3: 'info' + }); + + /** + * The collection of message types corresponding to the launguage server protocol + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md + */ + IPC.prototype.MESSAGE_TYPES = Object.freeze({ + /** + * @description The initialize request is sent as the first request from the client to the server. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#initialize-request + */ + initialize: 'initialize', + /** + * @description The shutdown request is sent from the client to the server. + * It asks the server to shut down, but to not exit (otherwise the response might not be delivered correctly to the client). + * There is a separate exit notification that asks the server to exit. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#shutdown-request + */ + shutdown: 'shutdown', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#status-notification + */ + status: 'language/status', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#exit-notification + */ + exit: 'exit', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#showmessage-notification + */ + showMessage: 'window/showMessage', + /** + * @description The show message request is sent from a server to a client to ask the client to display a particular message in the user interface. + * In addition to the show message notification the request allows to pass actions and to wait for an answer from the client. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#showmessage-request + */ + showMessageRequest: 'winow/showMessageRequest', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#logmessage-notification + */ + logMessage: 'window/logMessage', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#telemetry-notification + */ + telemetryEvent: 'telemetry/event', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#didchangeconfiguration-notification + */ + didChangeConfiguration: 'workspace/didChangeConfiguration', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#didchangewatchedfiles-notification + */ + didChangeWatchedFiles: 'workspace/didChangeWatchedFiles', + /** + * @description The workspace symbol request is sent from the client to the server to list project-wide symbols matching the query string. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#workspace-symbols + */ + workspaceSymbol: 'workspace/symbol', + /** + * @description The code action request is sent from the client to the server to compute commands for a given text document and range. + * The request is triggered when the user moves the cursor into a problem marker in the editor or presses the lightbulb associated with a marker. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#code-action + */ + codeAction: 'textDocument/codeAction', + /** + * @description The code lens request is sent from the client to the server to compute code lenses for a given text document. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#code-lens + */ + codeLens: 'textDocument/codeLens', + /** + * @description The Completion request is sent from the client to the server to compute completion items at a given cursor position. + * Completion items are presented in the IntelliSense user interface. If computing full completion items is expensive, + * servers can additionally provide a handler for the completion item resolve request. This request is sent when a completion item is selected in the user interface. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#completion-request + */ + completion: 'textDocument/completion', + /** + * @description The goto definition request is sent from the client to the server to resolve the definition location of a symbol at a given text document position. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#goto-definition + */ + definition: 'textDocument/definition', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#didopentextdocument-notification + */ + didOpen: 'textDocument/didOpen', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#didchangetextdocument-notification + */ + didChange: 'textDocument/didChange', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#didclosetextdocument-notification + */ + didClose: 'textDocument/didClose', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#didsavetextdocument-notification + */ + didSave: 'textDocument/didSave', + /** + * @description The document highlight request is sent from the client to the server to resolve a document highlights for a given text document position. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#document-highlights + */ + documentHighlight: 'textDocument/documentHighlight', + /** + * @description The document symbol request is sent from the client to the server to list all symbols found in a given text document. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#document-symbols + */ + documentSymbol: 'textDocument/documentSymbol', + /** + * @description The document formatting request is sent from the server to the client to format a whole document. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#document-formatting + */ + formatting: 'textDocument/formatting', + /** + * @description The hover request is sent from the client to the server to request hover information at a given text document position. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#hover + */ + hover: 'textDocument/hover', + /** + * @description The document on type formatting request is sent from the client to the server to format parts of the document during typing. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#document-on-type-formatting + */ + onTypeFormatting: 'textDocument/onTypeFormatting', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#publishdiagnostics-notification + */ + publishDiagnostics: 'textDocument/publishDiagnostics', + /** + * @description The document range formatting request is sent from the client to the server to format a given range in a document. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#document-range-formatting + */ + rangeFormatting: 'textDocument/rangeFormatting', + /** + * @description The references request is sent from the client to the server to resolve project-wide references for the symbol denoted by the given text document position. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#find-references + */ + references: 'textDocument/references', + /** + * @description The rename request is sent from the client to the server to perform a workspace-wide rename of a symbol. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#rename + */ + rename: 'textDocument/rename', + /** + * @description The signature help request is sent from the client to the server to request signature information at a given cursor position. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#signature-help + */ + signatureHelp: 'textDocument/signatureHelp', + /** + * @description The request is sent from the client to the server to resolve additional information for a given completion item. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#completion-item-resolve-request + */ + completionItemResolve: 'completionItem/resolve', + /** + * @description The code lens resolve request is sent from the client to the server to resolve the command for a given code lens item. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#code-lens-resolve + */ + codeLensResolve: 'codeLens/resolve' + }); + + /** + * @name IPC + * @description Creates a new IPC class + * @param {String} channel The channel + * @param {String} language The id of the language + * @returns {IPC} A new class + * @since 13.0 + */ + function IPC(channel, language) { + this.socket = null; + this.channel = channel; + this.languageId = language; + this.id = 1; + this.requests = {}; + this.initialized = false; + this.queue = []; + this.listeners = Object.create(null); + } + + /** + * @name _notifyListeners + * @description Notify the given list of listeners with the given data. If no type is given, 'data.method' will be queried. If there is no + * 'data.method' property, no work is done + * @private + * @param {Array.<{}>} listeners The list of listeners to notify + * @param {String} type The type of listener to notify + * @param {?} data The data to tell the listeneres about + */ + function _notifyListeners(listeners, type, data) { + var t = type ? type : data.method; + if(t) { + var l = listeners[t]; + if(Array.isArray(l) && l.length > 0) { + l.forEach(function(listener) { + listener.handleNotification(data); + }); + } + } + } + /** + * @name IPC.prototype.sendMessage + * @description Send a message over the socket + * @function + * @param {number} id The id of the message + * @param {String} message The name of the message to send + * @param {?} params The object of parameters for the message + */ + IPC.prototype.sendMessage = function sendMessage(id, message, params) { + var json = { + "jsonrpc": "2.0", + "method": message, + "params": params + }; + if (id) { + json.id = id; + } + if(!this.initialized && message !== this.MESSAGE_TYPES.initialize) { + this.queue.push(json); + } else { + this.socket.emit('data', json); + } + if (id) { + return this.requests[id] = new Deferred(); + } + }; + + /** + * @name IPC.prototype.addListener + * @description Adds a listener for a given type. A type can be 'log' for logging, 'error' for only errors or the name of one of the message kinds + * @function + * @param {String} type The type of the listener + * @param {?} listener The listener object + */ + IPC.prototype.addListener = function addListener(type, listener) { + if(!Array.isArray(this.listeners[type])) { + this.listeners[type] = []; + } + this.listeners[type].push(listener); + }; + + /** + * @name IPC.prototype.connect + * @description Connects to the class channel name + * @function + */ + IPC.prototype.connect = function connect() { + this.socket = io.connect(this.channel); + this.socket.on('connect', function(sock) { + this.socket.emit('start'); + this.socket.emit('junk'); + }.bind(this)); + this.socket.on('fail', function(error) { + console.log(error); + }.bind(this)); + this.socket.on('error', function(error) { + console.log(error); + }.bind(this)); + this.socket.on('data', function(data) { + try { + if(!data) { + _notifyListeners(this.listeners, this.MESSAGE_TYPES.logMessage, "Dropped response with null data."); + return; + } + if (data && data.id) { + var deferred = this.requests[data.id]; + if(deferred) { + if(data.error) { + deferred.reject(data.error); + } else { + deferred.resolve(data.result); + } + delete this.requests[data.id]; + } + } + _notifyListeners(this.listeners, data.method, data); + } catch(err) { + _notifyListeners(this.listeners, this.MESSAGE_TYPES.logMessage, err.toString()); + } + }.bind(this)); + this.socket.on('ready', function(data) { + var pid; + try { + var json = JSON.parse(data); + this.workspaceDir = json.workspaceDir; + pid = json.processId; + } catch(err) { + _notifyListeners(this.listeners, this.MESSAGE_TYPES.logMessage, err.toString()); + } + this.initialize(pid, this.workspaceDir).then(/* @callback */ function initializeCallback(result) { + this.initialized = true; + this.capabilities = result.capabilities; + this.queue.forEach(function queueFlushCallback(item) { + this.socket.emit('data', item); + _notifyListeners(this.listeners, this.MESSAGE_TYPES.logMessage, JSON.stringify(item)); + }.bind(this)); + this.queue = []; + }.bind(this)); + }.bind(this)); + }; + + /** + * @name IPC.prototype.initialize + * @description The initialize request is sent as the first request from the client to the server. + * @param {String} processId The id of the process + * @param {String} workspaceDir The root of the current workspace + * @function + * @returns {Deferred} The deferred that resolves to the result of the request + */ + IPC.prototype.initialize = function initialize(processId, workspaceDir) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.initialize, { + rootPath: workspaceDir, + processId: processId + }); + }; + + /** + * @name IPC.prototype.didOpen + * @description The document open notification is sent from the client to the server to signal newly opened text documents. + * The document's truth is now managed by the client and the server must not try to read the document's truth using the document's uri. + * @function + * @param {String} uri The URI of the file + * @param {String} text The optional current source code + */ + IPC.prototype.didOpen = function didOpen(uri, version, text) { + return this.sendMessage(0, this.MESSAGE_TYPES.didOpen, { + textDocument: { + uri: uri, + languageId: this.languageId, + version: version, + text: text + } + }); + }; + + /** + * @name IPC.prototype.didClose + * @description The document close notification is sent from the client to the server when the document got closed in the client. + * The document's truth now exists where the document's uri points to (e.g. if the document's uri is a file uri the truth now exists on disk). + * @function + * @param {String} uri The URI of the file + */ + IPC.prototype.didClose = function didClose(uri) { + return this.sendMessage(0, this.MESSAGE_TYPES.didClose, { + textDocument: { + uri: uri + } + }); + }; + + /** + * @name IPC.prototype.didSave + * @description The document save notification is sent from the client to the server when the document was saved in the client. + * @function + * @param {String} uri The URI of the file + */ + IPC.prototype.didSave = function didSave(uri) { + return this.sendMessage(0, this.MESSAGE_TYPES.didSave, { + textDocument: { + uri: uri + } + }); + }; + + /** + * @name IPC.prototype.didChange + * @description The document change notification is sent from the client to the server to signal changes to a text document. + * In 2.0 the shape of the params has changed to include proper version numbers and language ids. + * @function + * @param {String} uri The URI of the file + * @param {Number} version the version of the document + * @param Array<TextDocumentContentChangeEvent> changes the changes in the document + */ + IPC.prototype.didChange = function didChange(uri, version, changes) { + return this.sendMessage(0, this.MESSAGE_TYPES.didChange, { + textDocument: { + uri: uri, + version: version + }, + contentChanges: changes + }); + }; + + /** + * @name IPC.prototype.documentHighlight + * @description The document highlight request is sent from the client to the server to resolve a document highlights for a given text document position. + * @function + * @param {String} uri The URI of the file + * @param {{line: number, character: number}} offset The offset into the file to compute the highlight for + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.documentHighlight = function documentHighlight(uri, position) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.documentHighlight, { + position: position, + textDocument: { + uri: uri + } + }); + }; + + /** + * @name IPC.prototype.completion + * @description The Completion request is sent from the client to the server to compute completion items at a given cursor position. + * Completion items are presented in the IntelliSense user interface. If computing full completion items is expensive, servers can additionally provide a handler for the completion item resolve request. + * This request is sent when a completion item is selected in the user interface. + * @function + * @param {String} uri The URI of the file + * @param {{line: number, character: number}} offset The offset into the file to compute completions at + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.completion = function completion(uri, position) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.completion, { + position: position, + textDocument: { + uri: uri + } + }); + }; + + /** + * @name IPC.prototype.hover + * @description The hover request is sent from the client to the server to request hover information at a given text document position. + * @function + * @param {String} uri The URI of the file + * @param {{line: number, character: number}} offset The offset into the file to hover at + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.hover = function hover(uri, position) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.hover, { + position: position, + textDocument: { + uri: uri + } + }); + }; + + /** + * @name IPC.prototype.documentSymbol + * @description The document symbol request is sent from the client to the server to list all symbols found in a given text document. + * @function + * @param {String} uri The URI for the file to find symbols in + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.documentSymbol = function documentSymbol(uri) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.documentSymbol, { + textDocument: { + uri: uri + } + }); + }; + + /** + * @name IPC.prototype.formatDocument + * @description The document formatting request is sent from the server to the client to format a whole document. + * @function + * @param {String} uri The URI for the file to find symbols in + * @param {FormattingOptions} options the formatting options + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.formatDocument = function formatDocument(uri, options) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.formatting, { + textDocument: { + uri: uri + }, + options: options + }); + }; + + /** + * @name IPC.prototype.codeLens + * @description The code lens request is sent from the client to the server to compute code lenses for a given text document. + * @function + * @param {String} uri The URI to request the lens within + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.codeLens = function codeLens(uri) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.codeLens, { + textDocument: { + uri: uri + } + }); + }; + + /** + * @name IPC.prototype.references + * @description The references request is sent from the client to the server to resolve project-wide references for the symbol denoted by the given text document position. + * @function + * @param {String} uri The URI to request the references from + * @param {{line: number, character: number}} offset The offset into the file to compute references for + * @param context extra context info (i.e for the java server it is possible to specify whether to include declarations) + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.references = function references(uri, position, context) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.references, { + position: position, + context: context, + textDocument: { + uri: uri + } + }); + }; + + /** + * @name IPC.prototype.references + * @description The goto definition request is sent from the client to the server to resolve the definition location of a symbol at a given text document position. + * @function + * @param {String} uri The URI to request the definition from + * @param {{line: number, character: number}} offset The offset into the file to compute the definition for + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.definition = function definition(uri, position) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.definition, { + position: position, + textDocument: { + uri: uri + } + }); + }; + + /** + * @name IPC.prototype.shutdown + * @description The shutdown request is sent from the client to the server. It asks the server to shut down, but to not exit (otherwise the response might not be delivered correctly to the client). + * There is a separate exit notification that asks the server to exit. + * @function + * @returns {Deferred} The deferred to return the results of the request. In this case the result is always undefined. + */ + IPC.prototype.shutdown = function shutdown() { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.shutdown, { + }); + }; + + /** + * @name IPC.prototype.showMessageRequest + * @description The show message request is sent from a server to a client to ask the client to display a particular message in the user interface. + * In addition to the show message notification the request allows to pass actions and to wait for an answer from the client. + * @function + * @param {number} type The type of the message {@see #this.MESSAGE_TYPES} + * @param {String} message The message to send + * @param {Array.<String>} actions Ant command actions to be processed + * @returns {Deferred} The deferred to return the results of the request. In this case the result is always undefined. + */ + IPC.prototype.showMessageRequest = function showMessageRequest(type, message, actions) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.showMessageRequest, { + type: type, + message: message, + actions: actions + }); + }; + + /** + * @name IPC.prototype.workspaceSymbol + * @description The workspace symbol request is sent from the client to the server to list project-wide symbols matching the query string. + * @function + * @param {String} query The string query + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.workspaceSymbol = function workspaceSymbol(query) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.workspaceSymbol, { + query: query + }); + }; + + /** + * @name IPC.prototype.codeAction + * @description The code action request is sent from the client to the server to compute commands for a given text document and range. + * The request is triggered when the user moves the cursor into a problem marker in the editor or presses the lightbulb associated with a marker. + * @function + * @param {String} uri The URI of the file + * @param {{line:number, character:number}} start The start position + * @param {{line:number, character:number}} end The end position + * @param {Array.<{}>} diagnostics The array of diagnostic objects + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.codeAction = function codeAction(uri, start, end, diagnostics) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.workspaceSymbol, { + textDocument: { + uri: uri + }, + range: { + start: start, + end: end + }, + context: { + diagnostics: diagnostics + } + }); + }; + + /** + * @name IPC.prototype.onTypeFormatting + * @description The document on type formatting request is sent from the client to the server to format parts of the document during typing. + * @function + * @param {String} uri The URI of the file + * @param {{line:number, character:number}} position The position of the edit + * @param {string} char The character typed + * @param {?} options The formatting options + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.onTypeFormatting = function onTypeFormatting(uri, position, char, options) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.onTypeFormatting, { + textDocument: { + uri: uri + }, + position: position, + char: char, + options: options + }); + }; + + /** + * @name IPC.prototype.rangeFormatting + * @description The document range formatting request is sent from the client to the server to format a given range in a document. + * @function + * @param {String} uri The URI of the file + * @param {{line:number, character:number}} start The start position + * @param {{line:number, character:number}} end The end position + * @param {?} options The formatting options + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.rangeFormatting = function rangeFormatting(uri, start, end, options) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.rangeFormatting, { + textDocument: { + uri: uri + }, + range: { + start: start, + end: end + }, + options: options + }); + }; + + /** + * @name IPC.prototype.codeLensResolve + * @description he code lens resolve request is sent from the client to the server to resolve the command for a given code lens item. + * @function + * @param {?} codeLens The result froma codeLens request + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.codeLensResolve = function codeLensResolve(codeLens) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.codeLensResolve, { + codeLens: codeLens + }); + }; + + /** + * @name IPC.prototype.rename + * @description The rename request is sent from the client to the server to perform a workspace-wide rename of a symbol. + * @function + * @param {String} uri The URI of the file + * @param {{line:number, character:number}} position The position in the editor to invoke the rename from + * @param {String} newName The new name to set + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.rename = function rename(uri, position, newName) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.rename, { + textDocument: { + uri: uri + }, + position: position, + newName: newName + }); + }; + + /** + * @name IPC.prototype.signatureHelp + * @description The signature help request is sent from the client to the server to request signature information at a given cursor position. + * @function + * @param {String} uri The URI of the file + * @param {{line:number, character:number}} position The position in the editor + * @param {Array.<String>} signatures + * @param {String} activeSignature + * @param {String} activeParameter + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.signatureHelp = function signatureHelp(uri, position, signatures, activeSignature, activeParameter) { + return this.sendMessage(this.id++, this.MESSAGE_TYPES.signatureHelp, { + textDocument: { + uri: uri, + position: position + }, + signatures: signatures, + activeSignature: activeSignature, + activeParameter: activeParameter + }); + }; + + /** + * @name IPC.prototype.completionItemResolve + * @description The request is sent from the client to the server to resolve additional information for a given completion item. + * @function + * @param {?} completionItem A completion item response from a completion request + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.completionItemResolve = function completionItemResolve(completionItem) { + this.sendMessage(this.id++, this.MESSAGE_TYPES.completionItemResolve, { + completionItem: completionItem + }); + }; + return IPC; +});
\ No newline at end of file diff --git a/bundles/org.eclipse.orion.client.ui/web/orion/lsp/languageServer.js b/bundles/org.eclipse.orion.client.ui/web/orion/lsp/languageServer.js new file mode 100644 index 0000000..bd22699 --- /dev/null +++ b/bundles/org.eclipse.orion.client.ui/web/orion/lsp/languageServer.js @@ -0,0 +1,72 @@ +/******************************************************************************* + * @license + * Copyright (c) 2016 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials are made + * available under the terms of the Eclipse Public License v1.0 + * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution + * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +/* eslint-env amd */ +define([ + "orion/Deferred", + "orion/lsp/ipc" +], function(Deferred, IPC) { + /** + * @name LanguageServer + * @description Creates a new instance of the language server placeholder. This object wraps the server impl, + * options and identifying information in one place. The functions from the underlying IPC instance are wrapped in the promise-like + * function and set directly on this instance + * @class + * @param {LanguageService} service The backing language service that instantiated this server + * @param {String} id The optional identifier for this language server + * @param {String} url The URL for the location for this language server + * @param {?} ls The language server client side hooks + * @param {?} options The optional collection of options + * @returns {LanguageServer} Returns a new instance of LanguageServer + * @since 13.0 + */ + function LanguageServer(service, id, url, options) { + this._service = service; + this._id = id; + this._url = url; + this._options = options || Object.create(null); + this._ipc = new IPC(url, id); + Object.keys(this._ipc).forEach(function(key) { //this mitigates having to check for own property + if(typeof this._ipc[key] === 'function') { + this[key] = _ipcCall(key, this._ipc); + } + }, this); + + } + + /** + * @name _ipcCall + * @description Create a deferred-wrappped function call + * @private + * @param {String} method The name of the method to wrap + * @param {IPC} ls The protocol impl to call back to + * @returns {function} Returns a function wrapper for the named IPC function + */ + function _ipcCall(method, ls) { + return function() { + var d; + try { + var result = ls[method].apply(ls, Array.prototype.slice.call(arguments)); + if (result && typeof result.then === "function") { + return result; + } + d = new Deferred(); + d.resolve(result); + } catch (e) { + d = new Deferred(); + d.reject(e); + } + return d.promise; + }; + } + + return LanguageServer; +});
\ No newline at end of file diff --git a/bundles/org.eclipse.orion.client.ui/web/orion/lsp/languageService.js b/bundles/org.eclipse.orion.client.ui/web/orion/lsp/languageService.js new file mode 100644 index 0000000..8aed8f8 --- /dev/null +++ b/bundles/org.eclipse.orion.client.ui/web/orion/lsp/languageService.js @@ -0,0 +1,66 @@ +/******************************************************************************* + * @license + * Copyright (c) 2016 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials are made + * available under the terms of the Eclipse Public License v1.0 + * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution + * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +/*eslint-env amd, es6*/ +define([ + "orion/lsp/languageServer" +], function(LanguageServer) { + + var _registry, + _srvcreg; + + /** + * @name LanguageServerRegistry + * @description The registry of all available language servers, cached by language ID + * @param {?} serviceRegistry The backing Orion service registry + * @returns {LanguageServerRegistry} A new instance of the registry + * @since 13.0 + */ + function LanguageServerRegistry(serviceRegistry) { + _srvcreg = serviceRegistry; + } + + /** + * @name LanguageServerRegistry.prototype.init + * @description Initialize the language server registry if not already initialized + * @function + */ + LanguageServerRegistry.prototype.init = function init() { + if(!_registry && _srvcreg) { + var srvcs = _srvcreg.getServiceReferences("orion.languages.server"); + if(srvcs) { + _registry = new Map(); //map by languageId + srvcs.forEach(function(ref) { + var channel = ref.getProperty("channel"), + id = ref.getProperty("languageId"), + url = ref.getProperty("url"); + if(channel && id) { + _registry.set(id, new LanguageServer(this, id, url, {channel: channel})); + } + }.bind(this)); + } + } + }; + + /** + * @name LanguageServerRegistry.prototype.getServer + * @description description + * @function + * @param {String} languageId The id of the language to look for a server for + * @returns {LanguageServer} A language server impl - if one is mapped + */ + LanguageServerRegistry.prototype.getServer = function getServer(languageId) { + this.init(); + return _registry.get(languageId); + }; + + return LanguageServerRegistry; +});
\ No newline at end of file diff --git a/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/ipc.js b/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/ipc.js new file mode 100644 index 0000000..020be15 --- /dev/null +++ b/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/ipc.js @@ -0,0 +1,759 @@ +/*eslint-env amd, browser*/ +define([ + "orion/Deferred", + "/socketio/socket.io.js", +], function(Deferred, io) { + + /** + * The object of error codes + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#response-message + */ + var errorCodes = { + ParseError: -32700, + InvalidRequest: -32600, + MethodNotFound: -32601, + InvalidParams: -32602, + InternalError: -32603, + ServerErrorStart: -32099, + ServerErrorEnd: -32000, + }; + + var errorTypes = { + 1: 'error', + 2: 'warn', + 3: 'info' + }; + /** + * The map of error types + */ + IPC.prototype.ERROR_TYPES = errorTypes; //TODO should be a clone not a live copy + + var messageTypes = { + /** + * @description The initialize request is sent as the first request from the client to the server. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#initialize-request + */ + initialize: 'initialize', + /** + * @description The shutdown request is sent from the client to the server. + * It asks the server to shut down, but to not exit (otherwise the response might not be delivered correctly to the client). + * There is a separate exit notification that asks the server to exit. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#shutdown-request + */ + shutdown: 'shutdown', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#status-notification + */ + status: 'language/status', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#exit-notification + */ + exit: 'exit', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#showmessage-notification + */ + showMessage: 'window/showMessage', + /** + * @description The show message request is sent from a server to a client to ask the client to display a particular message in the user interface. + * In addition to the show message notification the request allows to pass actions and to wait for an answer from the client. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#showmessage-request + */ + showMessageRequest: 'winow/showMessageRequest', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#logmessage-notification + */ + logMessage: 'window/logMessage', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#telemetry-notification + */ + telemetryEvent: 'telemetry/event', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#didchangeconfiguration-notification + */ + didChangeConfiguration: 'workspace/didChangeConfiguration', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#didchangewatchedfiles-notification + */ + didChangeWatchedFiles: 'workspace/didChangeWatchedFiles', + /** + * @description The workspace symbol request is sent from the client to the server to list project-wide symbols matching the query string. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#workspace-symbols + */ + workspaceSymbol: 'workspace/symbol', + /** + * @description The code action request is sent from the client to the server to compute commands for a given text document and range. + * The request is triggered when the user moves the cursor into a problem marker in the editor or presses the lightbulb associated with a marker. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#code-action + */ + codeAction: 'textDocument/codeAction', + /** + * @description The code lens request is sent from the client to the server to compute code lenses for a given text document. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#code-lens + */ + codeLens: 'textDocument/codeLens', + /** + * @description The Completion request is sent from the client to the server to compute completion items at a given cursor position. + * Completion items are presented in the IntelliSense user interface. If computing full completion items is expensive, + * servers can additionally provide a handler for the completion item resolve request. This request is sent when a completion item is selected in the user interface. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#completion-request + */ + completion: 'textDocument/completion', + /** + * @description The goto definition request is sent from the client to the server to resolve the definition location of a symbol at a given text document position. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#goto-definition + */ + definition: 'textDocument/definition', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#didopentextdocument-notification + */ + didOpen: 'textDocument/didOpen', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#didchangetextdocument-notification + */ + didChange: 'textDocument/didChange', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#didclosetextdocument-notification + */ + didClose: 'textDocument/didClose', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#didsavetextdocument-notification + */ + didSave: 'textDocument/didSave', + /** + * @description The document highlight request is sent from the client to the server to resolve a document highlights for a given text document position. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#document-highlights + */ + documentHighlight: 'textDocument/documentHighlight', + /** + * @description The document symbol request is sent from the client to the server to list all symbols found in a given text document. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#document-symbols + */ + documentSymbol: 'textDocument/documentSymbol', + /** + * @description The document formatting request is sent from the server to the client to format a whole document. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#document-formatting + */ + formatting: 'textDocument/formatting', + /** + * @description The hover request is sent from the client to the server to request hover information at a given text document position. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#hover + */ + hover: 'textDocument/hover', + /** + * @description The document on type formatting request is sent from the client to the server to format parts of the document during typing. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#document-on-type-formatting + */ + onTypeFormatting: 'textDocument/onTypeFormatting', + /** + * @kind notification + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#publishdiagnostics-notification + */ + publishDiagnostics: 'textDocument/publishDiagnostics', + /** + * @description The document range formatting request is sent from the client to the server to format a given range in a document. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#document-range-formatting + */ + rangeFormatting: 'textDocument/rangeFormatting', + /** + * @description The references request is sent from the client to the server to resolve project-wide references for the symbol denoted by the given text document position. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#find-references + */ + references: 'textDocument/references', + /** + * @description The rename request is sent from the client to the server to perform a workspace-wide rename of a symbol. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#rename + */ + rename: 'textDocument/rename', + /** + * @description The signature help request is sent from the client to the server to request signature information at a given cursor position. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#signature-help + */ + signatureHelp: 'textDocument/signatureHelp', + /** + * @description The request is sent from the client to the server to resolve additional information for a given completion item. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#completion-item-resolve-request + */ + completionItemResolve: 'completionItem/resolve', + /** + * @description The code lens resolve request is sent from the client to the server to resolve the command for a given code lens item. + * @kind request + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#code-lens-resolve + */ + codeLensResolve: 'codeLens/resolve' + }; + + /** + * @name IPC + * @description Creates a new IPC class + * @param {String} channel + * @returns {IPC} A new class + * @ since 13.0 + */ + function IPC(channel) { + this.socket = null; + this.channel = channel; + this.id = 1; + this.requests = {}; + this.initialized = false; + this.queue = []; + this.listeners = Object.create(null); + } + + /** + * The collection of message types corresponding to the launguage server protocol + * @see https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md + */ + IPC.prototype.MESSAGE_TYPES = messageTypes; //TODO should be a clone, not a live copy + + /** + * @name _notifyListeners + * @description Notify the given list of listeners with the given data. If no type is given, 'data.method' will be queried. If there is no + * 'data.method' property, no work is done + * @private + * @param {Array.<{}>} listeners The list of listeners to notify + * @param {String} type The type of listener to notify + * @param {?} data The data to tell the listeneres about + */ + function _notifyListeners(listeners, type, data) { + var t = type ? type : data.method; + if (t) { + var l = listeners[t]; + if (Array.isArray(l) && l.length > 0) { + l.forEach(function(listener) { + listener.handleNotification(data); + }); + } + } + } + /** + * @name IPC.prototype.sendMessage + * @description Send a message over the socket + * @function + * @param {number} id The id of the message + * @param {String} message The name of the message to send + * @param {?} params The object of parameters for the message + */ + IPC.prototype.sendMessage = function sendMessage(id, message, params) { + var json = { + "jsonrpc": "2.0", + "method": message, + "params": params + }; + if (id) { + json.id = id; + } + if (!this.initialized && message !== messageTypes.initialize) { + this.queue.push(json); + } else { + console.log(json); + this.socket.emit('data', json); + } + if (id) { + return this.requests[id] = new Deferred(); + } + }; + + /** + * @name IPC.prototype.addListener + * @description Adds a listener for a given type. A type can be 'log' for logging, 'error' for only errors or the name of one of the message kinds + * @function + * @param {String} type The type of the listener + * @param {?} listener The listener object + */ + IPC.prototype.addListener = function addListener(type, listener) { + if (!Array.isArray(this.listeners[type])) { + this.listeners[type] = []; + } + this.listeners[type].push(listener); + }; + + /** + * @name IPC.prototype.connect + * @description Connects to the class channel name + * @function + */ + IPC.prototype.connect = function connect() { + this.socket = io.connect(this.channel); + this.socket.on('connect', function() { + this.socket.emit('start'); + }.bind(this)); + this.socket.on('fail', function(error) { + console.log(error); + }.bind(this)); + this.socket.on('error', function(error) { + console.log(error); + }.bind(this)); + this.socket.on('data', function(data) { + try { + if (!data) { + _notifyListeners(this.listeners, messageTypes.logMessage, "Dropped response with null data."); + return; + } + if (data && data.id) { + var deferred = this.requests[data.id]; + if (deferred) { + if (data.error) { + deferred.reject(data.error); + } else { + deferred.resolve(data.result); + } + delete this.requests[data.id]; + } + } + _notifyListeners(this.listeners, data.method, data); + } catch (err) { + _notifyListeners(this.listeners, messageTypes.logMessage, err.toString()); + } + }.bind(this)); + this.socket.on('ready', function(data) { + var pid; + try { + var json = JSON.parse(data); + this.workspaceDir = json.workspaceDir; + pid = json.processId; + } catch (err) { + _notifyListeners(this.listeners, messageTypes.logMessage, err.toString()); + } + this.initialize(pid, this.workspaceDir).then( /* @callback */ function initializeCallback(result) { + this.initialized = true; + this.capabilities = result.capabilities; + this.queue.forEach(function queueFlushCallback(item) { + this.socket.emit('data', item); + _notifyListeners(this.listeners, messageTypes.logMessage, JSON.stringify(item)); + }.bind(this)); + this.queue = []; + }.bind(this)); + }.bind(this)); + }; + + /** + * @name IPC.prototype.initialize + * @description The initialize request is sent as the first request from the client to the server. + * @param {String} processId The id of the process + * @param {String} workspaceDir The root of the current workspace + * @function + * @returns {Deferred} The deferred that resolves to the result of the request + */ + IPC.prototype.initialize = function initialize(processId, workspaceDir) { + return this.sendMessage(this.id++, messageTypes.initialize, { + rootPath: workspaceDir, + processId: processId + }); + }; + + /** + * @name IPC.prototype.didOpen + * @description The document open notification is sent from the client to the server to signal newly opened text documents. + * The document's truth is now managed by the client and the server must not try to read the document's truth using the document's uri. + * @function + * @param {String} uri The URI of the file + * @param {String} languageId The content type of the source + * @param {String} text The optional current source code + */ + IPC.prototype.didOpen = function didOpen(uri, languageId, version, text) { + return this.sendMessage(0, messageTypes.didOpen, { + textDocument: { + uri: uri, + languageId: languageId, + version: version, + text: text + } + }); + }; + + /** + * @name IPC.prototype.didClose + * @description The document close notification is sent from the client to the server when the document got closed in the client. + * The document's truth now exists where the document's uri points to (e.g. if the document's uri is a file uri the truth now exists on disk). + * @function + * @param {String} uri The URI of the file + */ + IPC.prototype.didClose = function didClose(uri) { + return this.sendMessage(0, messageTypes.didClose, { + textDocument: { + uri: uri, + } + }); + }; + + /** + * @name IPC.prototype.didSave + * @description The document save notification is sent from the client to the server when the document was saved in the client. + * @function + * @param {String} uri The URI of the file + */ + IPC.prototype.didSave = function didSave(uri) { + return this.sendMessage(0, messageTypes.didSave, { + textDocument: { + uri: uri, + } + }); + }; + + /** + * @name IPC.prototype.didChange + * @description The document change notification is sent from the client to the server to signal changes to a text document. + * In 2.0 the shape of the params has changed to include proper version numbers and language ids. + * @function + * @param {String} uri The URI of the file + * @param {Number} version the version of the document + * @param Array<TextDocumentContentChangeEvent> changes the changes in the document + */ + IPC.prototype.didChange = function didChange(uri, version, changes) { + return this.sendMessage(0, messageTypes.didChange, { + textDocument: { + uri: uri, + version: version + }, + contentChanges: changes + }); + }; + + /** + * @name IPC.prototype.documentHighlight + * @description The document highlight request is sent from the client to the server to resolve a document highlights for a given text document position. + * @function + * @param {String} uri The URI of the file + * @param {{line: number, character: number}} offset The offset into the file to compute the highlight for + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.documentHighlight = function documentHighlight(uri, position) { + return this.sendMessage(this.id++, messageTypes.documentHighlight, { + position: position, + textDocument: { + uri: uri, + } + }); + }; + + /** + * @name IPC.prototype.completion + * @description The Completion request is sent from the client to the server to compute completion items at a given cursor position. + * Completion items are presented in the IntelliSense user interface. If computing full completion items is expensive, servers can additionally provide a handler for the completion item resolve request. + * This request is sent when a completion item is selected in the user interface. + * @function + * @param {String} uri The URI of the file + * @param {{line: number, character: number}} offset The offset into the file to compute completions at + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.completion = function completion(uri, position) { + return this.sendMessage(this.id++, messageTypes.completion, { + position: position, + textDocument: { + uri: uri, + } + }); + }; + + /** + * @name IPC.prototype.hover + * @description The hover request is sent from the client to the server to request hover information at a given text document position. + * @function + * @param {String} uri The URI of the file + * @param {{line: number, character: number}} offset The offset into the file to hover at + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.hover = function hover(uri, position) { + return this.sendMessage(this.id++, messageTypes.hover, { + position: position, + textDocument: { + uri: uri, + } + }); + }; + + /** + * @name IPC.prototype.documentSymbol + * @description The document symbol request is sent from the client to the server to list all symbols found in a given text document. + * @function + * @param {String} uri The URI for the file to find symbols in + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.documentSymbol = function documentSymbol(uri) { + return this.sendMessage(this.id++, messageTypes.documentSymbol, { + textDocument: { + uri: uri, + } + }); + }; + + /** + * @name IPC.prototype.formatDocument + * @description The document formatting request is sent from the server to the client to format a whole document. + * @function + * @param {String} uri The URI for the file to find symbols in + * @param {FormattingOptions} options the formatting options + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.formatDocument = function formatDocument(uri, options) { + return this.sendMessage(this.id++, messageTypes.formatting, { + textDocument: { + uri: uri, + }, + options: options + }); + }; + + /** + * @name IPC.prototype.codeLens + * @description The code lens request is sent from the client to the server to compute code lenses for a given text document. + * @function + * @param {String} uri The URI to request the lens within + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.codeLens = function codeLens(uri) { + return this.sendMessage(this.id++, messageTypes.codeLens, { + textDocument: { + uri: uri + } + }); + }; + + /** + * @name IPC.prototype.references + * @description The references request is sent from the client to the server to resolve project-wide references for the symbol denoted by the given text document position. + * @function + * @param {String} uri The URI to request the references from + * @param {{line: number, character: number}} offset The offset into the file to compute references for + * @param context extra context info (i.e for the java server it is possible to specify whether to include declarations) + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.references = function references(uri, position, context) { + return this.sendMessage(this.id++, messageTypes.references, { + position: position, + context: context, + textDocument: { + uri: uri + } + }); + }; + + /** + * @name IPC.prototype.references + * @description The goto definition request is sent from the client to the server to resolve the definition location of a symbol at a given text document position. + * @function + * @param {String} uri The URI to request the definition from + * @param {{line: number, character: number}} offset The offset into the file to compute the definition for + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.definition = function definition(uri, position) { + return this.sendMessage(this.id++, messageTypes.definition, { + position: position, + textDocument: { + uri: uri + } + }); + }; + + /** + * @name IPC.prototype.shutdown + * @description The shutdown request is sent from the client to the server. It asks the server to shut down, but to not exit (otherwise the response might not be delivered correctly to the client). + * There is a separate exit notification that asks the server to exit. + * @function + * @returns {Deferred} The deferred to return the results of the request. In this case the result is always +. + */ + IPC.prototype.shutdown = function shutdown() { + return this.sendMessage(this.id++, messageTypes.shutdown, {}); + }; + + /** + * @name IPC.prototype.showMessageRequest + * @description The show message request is sent from a server to a client to ask the client to display a particular message in the user interface. + * In addition to the show message notification the request allows to pass actions and to wait for an answer from the client. + * @function + * @param {number} type The type of the message {@see #messageTypes} + * @param {String} message The message to send + * @param {Array.<String>} actions Ant command actions to be processed + * @returns {Deferred} The deferred to return the results of the request. In this case the result is always +. + */ + IPC.prototype.showMessageRequest = function showMessageRequest(type, message, actions) { + return this.sendMessage(this.id++, messageTypes.showMessageRequest, { + type: type, + message: message, + actions: actions + }); + }; + + /** + * @name IPC.prototype.workspaceSymbol + * @description The workspace symbol request is sent from the client to the server to list project-wide symbols matching the query string. + * @function + * @param {String} query The string query + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.workspaceSymbol = function workspaceSymbol(query) { + return this.sendMessage(this.id++, messageTypes.workspaceSymbol, { + query: query + }); + }; + + /** + * @name IPC.prototype.codeAction + * @description The code action request is sent from the client to the server to compute commands for a given text document and range. + * The request is triggered when the user moves the cursor into a problem marker in the editor or presses the lightbulb associated with a marker. + * @function + * @param {String} uri The URI of the file + * @param {{line:number, character:number}} start The start position + * @param {{line:number, character:number}} end The end position + * @param {Array.<{}>} diagnostics The array of diagnostic objects + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.codeAction = function codeAction(uri, start, end, diagnostics) { + return this.sendMessage(this.id++, messageTypes.workspaceSymbol, { + textDocument: { + uri: uri + }, + range: { + start: start, + end: end + }, + context: { + diagnostics: diagnostics + } + }); + }; + + /** + * @name IPC.prototype.onTypeFormatting + * @description The document on type formatting request is sent from the client to the server to format parts of the document during typing. + * @function + * @param {String} uri The URI of the file + * @param {{line:number, character:number}} position The position of the edit + * @param {string} char The character typed + * @param {?} options The formatting options + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.onTypeFormatting = function onTypeFormatting(uri, position, char, options) { + return this.sendMessage(this.id++, messageTypes.onTypeFormatting, { + textDocument: { + uri: uri + }, + position: position, + char: char, + options: options + }); + }; + + /** + * @name IPC.prototype.rangeFormatting + * @description The document range formatting request is sent from the client to the server to format a given range in a document. + * @function + * @param {String} uri The URI of the file + * @param {{line:number, character:number}} start The start position + * @param {{line:number, character:number}} end The end position + * @param {?} options The formatting options + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.rangeFormatting = function rangeFormatting(uri, start, end, options) { + return this.sendMessage(this.id++, messageTypes.rangeFormatting, { + textDocument: { + uri: uri + }, + range: { + start: start, + end: end + }, + options: options + }); + }; + + /** + * @name IPC.prototype.codLensResolve + * @description he code lens resolve request is sent from the client to the server to resolve the command for a given code lens item. + * @function + * @param {?} codeLens The result froma codeLens request + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.codLensResolve = function codeLensResolve(codeLens) { + return this.sendMessage(this.id++, messageTypes.codeLensResolve, { + codeLens: codeLens + }); + }; + + /** + * @name IPC.prototype.rename + * @description The rename request is sent from the client to the server to perform a workspace-wide rename of a symbol. + * @function + * @param {String} uri The URI of the file + * @param {{line:number, character:number}} position The position in the editor to invoke the rename from + * @param {String} newName The new name to set + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.rename = function rename(uri, position, newName) { + return this.sendMessage(this.id++, messageTypes.rename, { + textDocument: { + uri: uri + }, + position: position, + newName: newName + }); + }; + + /** + * @name IPC.prototype.signatureHelp + * @description The signature help request is sent from the client to the server to request signature information at a given cursor position. + * @function + * @param {String} uri The URI of the file + * @param {{line:number, character:number}} position The position in the editor + * @param {Array.<String>} signatures + * @param {String} activeSignature + * @param {String} activeParameter + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.signatureHelp = function signatureHelp(uri, position, signatures, activeSignature, activeParameter) { + return this.sendMessage(this.id++, messageTypes.signatureHelp, { + textDocument: { + uri: uri, + position: position + }, + signatures: signatures, + activeSignature: activeSignature, + activeParameter: activeParameter + }); + }; + + /** + * @name IPC.prototype.completionItemResolve + * @description The request is sent from the client to the server to resolve additional information for a given completion item. + * @function + * @param {?} completionItem A completion item response from a completion request + * @returns {Deferred} The deferred to return the results of the request + */ + IPC.prototype.completionItemResolve = function completionItemResolve(completionItem) { + this.sendMessage(this.id++, messageTypes.completionItemResolve, { + completionItem: completionItem + }); + }; + return IPC; +});
\ No newline at end of file diff --git a/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/javaPlugin.js b/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/javaPlugin.js index e9590ed..e8926b2 100644 --- a/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/javaPlugin.js +++ b/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/javaPlugin.js @@ -1,6 +1,6 @@ /******************************************************************************* * @license - * Copyright (c) 2014 IBM Corporation and others. + * Copyright (c) 2014, 2016 IBM Corporation and others. * All rights reserved. This program and the accompanying materials are made * available under the terms of the Eclipse Public License v1.0 * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution @@ -10,43 +10,771 @@ * IBM Corporation - initial API and implementation *******************************************************************************/ /*eslint-env browser, amd*/ -define(['orion/plugin', 'orion/editor/stylers/text_x-java-source/syntax', 'orion/editor/stylers/application_x-jsp/syntax'], function(PluginProvider, mJava, mJSP) { - - function connect() { - var headers = { - name: "Orion Java Tool Support", - version: "1.0", - description: "This plugin provides Java tools support for Orion." - }; - var pluginProvider = new PluginProvider(headers); - registerServiceProviders(pluginProvider); - pluginProvider.connect(); +define([ +'orion/plugin', +"orion/Deferred", +'orion/editor/stylers/text_x-java-source/syntax', +'orion/editor/stylers/application_x-jsp/syntax', +"plugins/languages/java/javaProject", +'orion/serviceregistry', +"plugins/languages/java/ipc", +"plugins/languages/java/javaValidator", +"javascript/finder", +"orion/editor/textModel" +], function(PluginProvider, Deferred, mJava, mJSP, JavaProject, mServiceRegistry, IPC, JavaValidator, Finder, TextModel) { + + var ipc = new IPC('/languageServer'), + project, + validator, + LOG_ERRORS = localStorage.getItem('java.langserver.logmessage.error') === 'true', + LOG_WARNINGS = localStorage.getItem('java.langserver.logmessage.warn') === 'true', + LOG_INFO = localStorage.getItem('java.langserver.logmessage.error.info') === 'true'; + + /** + * @name initializeIPC + * @param serviceRegistry the service registry + * @description Connects the IPC instance to the websocket and sets the deafult listeners + */ + function initializeIPC(serviceRegistry) { + ipc.connect(); + /** + * Default logging listener + */ + ipc.addListener(ipc.MESSAGE_TYPES.logMessage, + { + handleNotification: function handleNotification(data) { + if(data !== null && typeof data === 'object') { + if(data.params && (LOG_ERRORS && data.params.type === 1) || (LOG_WARNINGS && data.params.type === 2) || (LOG_INFO && data.params.type === 3)) { + if(typeof data === 'object' && data !== null) { + console.log(JSON.stringify(data)); + } else if(typeof data === 'string') { + console.log(data); + } + } + } + } + } + ); + /** + * Listener to handle diagnostics notifications + */ + ipc.addListener(ipc.MESSAGE_TYPES.publishDiagnostics, + { + handleNotification: function handleNotification(data) { + validator.updateDiagnostics(data.params.uri, data.params.diagnostics); + } + } + ); + /** + * Listener to handle diagnostics notifications + */ + ipc.addListener(ipc.MESSAGE_TYPES.status, + { + handleNotification: function handleNotification(data) { + var statusService = serviceRegistry.getService("orion.page.message"); //$NON-NLS-1$ + if (statusService) { + if (data.params.type === "Started") { + statusService.setProgressResult("Java " + data.params.type + " " + data.params.message) ; + } else { + statusService.setProgressMessage("Java " + data.params.type + " " + data.params.message) ; + } + } + } + } + ); } + /** + * Single place to add in all of the provide hooks for the various services + */ + function hookAPIs(provider) { + /** + * Content Assist + */ + provider.registerService("orion.edit.contentassist", + { + /** + * @callback + */ + computeContentAssist: function(editorContext, args) { + return editorContext.getFileMetadata().then(function(meta) { + return getPosition(editorContext, args.selection.start).then(function(position) { + return ipc.completion(meta.location, position).then(function(results) { + var items = results.items; + if (Array.isArray(items) && items.length > 0) { + return Deferred.all(convertToCompletionProposals(editorContext, items)).then(function(proposals) { + return new Deferred().resolve(proposals); + }); + } + return new Deferred().resolve([]); + }, + /* @callback */ + function(err) { + return new Deferred().resolve([]); + }); + }); + }); + } + }, + { + contentType: ["text/x-java-source", "application/x-jsp"], //$NON-NLS-1$ //$NON-NLS-2$ + name: 'Java Content Assist', //$NON-NLS-1$ + id: "orion.edit.contentassist.java", //$NON-NLS-1$ + charTriggers: "[.]", //$NON-NLS-1$ + excludedStyles: "(string.*)" //$NON-NLS-1$ + } + ); + + function convertToCompletionProposals(editorContext, items) { + var tempArray = items.map(function(item) { + var temp = { + name: item.label, + proposal: item.insertText, + description: ' (' + resolveCompletionKind(item.kind) + ')', + relevance: 100, + style: 'emphasis', //$NON-NLS-1$ + overwrite: true, + kind: 'java' //$NON-NLS-1$ + }; + if (item.documentation) { + temp.hover = { + content: item.documentation, + type: 'markdown' + }; + } + if (Array.isArray(item.additionalTextEdits) && item.additionalTextEdits.length !== 0) { + var tempEdits = []; + item.additionalTextEdits.forEach(function(additionalEdit) { + var newEdit = Object.create(null); + newEdit.text = additionalEdit.newText; + newEdit.range = additionalEdit.range; + tempEdits.push(newEdit); + }); + temp.additionalEdits = tempEdits; + } + return temp; + }); + return tempArray.map(function(proposal) { + return convertEachProposal(editorContext, proposal); + }); + } + + function convertEachProposal(editorContext, proposal) { + var deferred = new Deferred(); + if (proposal.additionalEdits) { + var additionalEditsLength = proposal.additionalEdits.length; + proposal.additionalEdits.forEach(function(edit, index) { + return convertEdit(editorContext, edit).then(function(newAdditionalEdit) { + edit.offset = newAdditionalEdit.start; + edit.length = newAdditionalEdit.end - edit.offset; + delete edit.range; + if (index === additionalEditsLength - 1) { + deferred.resolve(proposal); + } + }); + }); + } else { + deferred.resolve(proposal); + } + return deferred; + } - function registerServiceProviders(pluginProvider) { - pluginProvider.registerServiceProvider("orion.core.contenttype", {}, { - contentTypes: [ - { id: "text/x-java-source", - "extends": "text/plain", - name: "Java", - extension: ["java"] - }, {id: "application/x-jsp", - "extends": "text/plain", - name: "Java Server Page", - extension: ["jsp"] - } - ] - }); - mJava.grammars.forEach(function(current) { - pluginProvider.registerServiceProvider("orion.edit.highlighter", {}, current); - }); - mJSP.grammars.forEach(function(current) { - pluginProvider.registerServiceProvider("orion.edit.highlighter", {}, current); + /** + * Symbol outline + */ + provider.registerService("orion.edit.outliner", + { + /** + * @callback + */ + computeOutline: function computeOutline(editorContext, options) { + return editorContext.getFileMetadata().then(function(meta) { + return ipc.documentSymbol(meta.location).then(function(results) { + if(Array.isArray(results) && results.length > 0) { + var outline = []; + results.forEach(function(result) { + if(!result.containerName) { + outline.push({label: result.name, children: []}); + } else { + var idx = _findContainerIndex(outline, result.containerName), + _p; + if(idx < 0) { + _p = {label: result.containerName, children: []}; + outline.push(_p); + } else { + _p = outline[idx]; + } + var offset = result.location.range.start.character; + _p.children.push({ + label: result.name, + labelPost: ' ('+resolveSymbolKind(result.kind)+')', + line: result.location.range.start.line+1, + offset: offset, + length: result.location.range.end.character-offset + }); + } + }); + return outline; + } + return new Deferred().resolve([]); + }); + }); + } + }, + { + contentType: ["text/x-java-source", "application/x-jsp"], //$NON-NLS-1$ + name: "Java Symbol Outline", + title: "Java Symbols", + id: "orion.java.symbols.outliner.source" //$NON-NLS-1$ + } + ); + + function convertRange(editorContext, range) { + return editorContext.getLineStart(range.start.line).then(function(startLineOffset) { + return editorContext.getLineStart(range.end.line).then(function(endLineOffset) { + return { + start: range.start.character+startLineOffset, + end: range.end.character+endLineOffset, + }; + }); + }); + } + + function convertEdit(editorContext, edit) { + var range = edit.range; + return editorContext.getLineStart(range.start.line).then(function(startLineOffset) { + return editorContext.getLineStart(range.end.line).then(function(endLineOffset) { + return { + text: edit.newText, + start: range.start.character+startLineOffset, + end: range.end.character+endLineOffset, + }; + }); + }); + } + + //TODO only show command when the provider is available in capabilities + //TODO send the options to the language server + //TODO integrate with the new orion formatting service. We may have to change the orion service because + // the changes are done in the server side. + provider.registerServiceProvider("orion.edit.command", //$NON-NLS-1$ + { + execute: /** @callback */ function(editorContext, options) { + return editorContext.getFileMetadata().then(function(metadata) { + return ipc.formatDocument(metadata.location, {}).then(function(edits) { + if (Array.isArray(edits) && edits.length !== 0) { + return Deferred.all(edits.map(function(edit) { + return convertRange(editorContext, edit.range); + })).then(function(selections) { + var text = edits.map(function(e) { + return e.newText; + }); + editorContext.setText({text: text, selection: selections, preserveSelection: true}); + }); + } + return new Deferred().resolve([]); + }); + }); + } + }, + { + name: "Format", + id : "orion.java.format", //$NON-NLS-1$ + // key : [ 114, false, false, false, false], + contentType: ["text/x-java-source", "application/x-jsp"] //$NON-NLS-1$ //$NON-NLS-2$ + } + ); + provider.registerServiceProvider("orion.edit.command", //$NON-NLS-1$ + { + execute: /** @callback */ function(editorContext, options) { + return editorContext.getFileMetadata().then(function(metadata) { + return editorContext.getSelection().then(function(selection) { + return getPosition(editorContext, selection.start).then(function(start) { + return getPosition(editorContext, selection.end).then(function(end) { + return ipc.rangeFormatting(metadata.location, start, end, {}).then(function(edits) { + // if there is no edits (formatting the code doesn't provide any edit - already formatted), simply return + if (Array.isArray(edits) && edits.length !== 0) { + return Deferred.all(edits.map(function(edit) { + return convertRange(editorContext, edit.range); + })).then(function(selections) { + var text = edits.map(function(e) { + return e.newText; + }); + editorContext.setText({text: text, selection: selections, preserveSelection: true}); + }); + } + return new Deferred().resolve([]); + }); + }); + }); + }); + }); + } + }, + { + name: "Format Selection", + id : "orion.java.format.range", //$NON-NLS-1$ + // key : [ 114, false, false, false, false], + contentType: ["text/x-java-source", "application/x-jsp"] //$NON-NLS-1$ //$NON-NLS-2$ + } + ); + provider.registerServiceProvider("orion.edit.command", //$NON-NLS-1$ + { + execute: /** @callback */ function(editorContext, options) { + return editorContext.getFileMetadata().then(function(metadata) { + return editorContext.getSelection().then(function(selection) { + return getPosition(editorContext, selection.start).then(function(start) { + return ipc.references(metadata.location, start, {includeDeclaration: true}).then(function(locations) { + if (Array.isArray(locations) && locations.length !== 0) { + return editorContext.getText().then(function(text) { + var word = Finder.findWord(text, selection.start); + if(word) { + return Deferred.all(convertToRefResults(locations, metadata.location, text)).then(function(refResults) { + var result = { + searchParams: { + keyword: word, + fileNamePatterns: ["*.java"], //$NON-NLS-1$ + caseSensitive: true, + incremental:false, + shape: 'file' //$NON-NLS-1$ + }, + categories: { + uncategorized: { + category: "uncategorized", + name: "Uncategorized", + sort: 13 + } + }, + refResult: refResults + }; + return new Deferred().resolve(result); + }); + } + return new Deferred().resolve([]); + }); + } + return new Deferred().resolve([]); + }); + }); + }); + }); + } + }, + { + name: "References", + id : "orion.java.references", //$NON-NLS-1$ + // key : [ 114, false, false, false, false], + contentType: ["text/x-java-source", "application/x-jsp"] //$NON-NLS-1$ //$NON-NLS-2$ + } + ); + + + function convertToRefResults(locations, editorLocation, text) { + // edit has a uri for the file location and a range + // we need to answer a ref result that contains: + /* + * refResult: array + [0] + children <array> + [] + lineNumber <line number within the file> + matches: + category: "" + confidence: 100 + end: <position within the file> + length: end - start + start: <position within the file> + startIndex: position within the name + name <string> + contents: array of lines for the file + location: workspace relative path + metadata: file metadata object + name: <file name> + path: project relative path + totalMatches: number of matches in that file + */ + // need to collect the number of matches per file + var results = new Map(); + var projectPath = project.getProjectPath(); + for (var i = 0, max = locations.length;i < max; i++) { + var loc = locations[i]; + var uri = loc.uri; + var match = Object.create(null); + match.startIndex = loc.range.start.character; + match.confidence = 100; + match.category = ""; + match.range = loc.range; + var result = results.get(uri); + var lineNumber = loc.range.start.line + 1; + var children; + if (!result) { + result = Object.create(null); + result.location = uri; + result.path = uri.substring(projectPath.length); + result.totalMatches = 0; + var index = uri.lastIndexOf('/'); + result.name = uri.substring(index + 1); // extract file name from path + children = new Map(); + result.children = children; + results.set(uri, result); + } else { + children = result.children; + } + // already found a file with a match in it + result.totalMatches = result.totalMatches + 1; + + // handle the current match + var child = children.get(lineNumber); + if (!child) { + child = Object.create(null); + child.lineNumber = lineNumber; + child.matches = []; + children.set(lineNumber, child); + } + child.matches.push(match); + } + // iterate over the matches's keys + var returnedValue = []; + results.forEach(function(value) { + // get just the children's values + var temp = []; + value.children.forEach(function(element) { + temp.push(element); + }); + value.children = temp; + returnedValue.push(value); + }); + return returnedValue.map(function(element) { + // convert each match + return convertEachMatch(element, editorLocation, text); + }); + } + + function split_linebreaks(text) { + return text.split(/\r\n|\r|\n|\u2028|\u2029/g); + } + + function convertEachMatch(element, editorLocation, text) { + if (editorLocation === element.location) { + var deferred = new Deferred(); + var allLines = split_linebreaks(text); + element.contents = allLines; + var textModel = new TextModel.TextModel(text, "auto"); + element.children.map(function(child) { + child.name = allLines[child.lineNumber - 1]; // line number is 0-based + return Deferred.all(convertMatchRange(textModel, child.matches)).then(function(selections) { + deferred.resolve(element); + }); + }); + return deferred; + } + return project.getFile(element.path).then(function(file) { + var newDeferred = new Deferred(); + if (file) { + var contents = file.contents; + var allLines = split_linebreaks(contents); + element.contents = allLines; + var textModel = new TextModel.TextModel(contents, "auto"); + element.children.map(function(child) { + child.name = allLines[child.lineNumber - 1]; // line number is 0-based + return Deferred.all(convertMatchRange(textModel, child.matches)).then(function(selections) { + newDeferred.resolve(element); + }); + }); + } else { + newDeferred.resolve(); + } + return newDeferred; + }); + } + + function convertRangeFromTextModel(textModel, range) { + var startLineOffset = textModel.getLineStart(range.start.line); + var endLineOffset = textModel.getLineStart(range.end.line); + return { + start: range.start.character+startLineOffset, + end: range.end.character+endLineOffset, + }; + } + + function convertMatchRange(textModel, matches) { + return matches.map(function(match) { + var deferred = new Deferred(); + var selection = convertRangeFromTextModel(textModel, match.range); + var start = selection.start; + var end = selection.end; + match.start = start; + match.end = end; + match.length = end - start; + delete match.range; + return deferred.resolve(match); + }); + } + + provider.registerServiceProvider("orion.edit.command", //$NON-NLS-1$ + { + execute: /** @callback */ function(editorContext, options) { + return editorContext.getFileMetadata().then(function(metadata) { + return editorContext.getSelection().then(function(selection) { + return getPosition(editorContext, selection.start).then(function(position) { + return ipc.definition(metadata.location, position).then(function(loc) { + if (!loc) { + return; + } + if (Array.isArray(loc)) { + loc = loc[0]; + } + if (loc.range.start.line === loc.range.end.line) { + var sel = { + line: loc.range.start.line + 1, + offset: loc.range.start.character, + length: loc.range.end.character - loc.range.start.character + }; + return editorContext.openEditor(loc.uri, sel); + } + return convertRange(editorContext, loc.range).then(function(selection) { + editorContext.openEditor(loc.uri, selection); + }); + }); + }); + }); + }); + } + }, + { + name: "Open Declaration", + id : "orion.java.openDeclaration", //$NON-NLS-1$ + key : [ 114, false, false, false, false], + contentType: ["text/x-java-source", "application/x-jsp"] //$NON-NLS-1$ //$NON-NLS-2$ + } + ); + + /** + * Validator + */ + provider.registerService( + "orion.edit.validator", + validator, + { + contentType: ["text/x-java-source", "application/x-jsp"] + } + ); + /** + * Hover + */ + provider.registerService("orion.edit.hover", + { + computeHoverInfo: function computeHoverInfo(editorContext, args) { + if(args.proposal && args.proposal.kind === 'java') { + return args.proposal.hover; + } + if (args.offset === undefined) { + return ""; + } + return editorContext.getFileMetadata().then(function(meta) { + return getPosition(editorContext, args.offset).then(function(position) { + return ipc.hover(meta.location, position).then(function(result) { + var hover = {type: 'markdown'}; + if(typeof result.contents === 'string') { + if (result.contents.length === 0) { + return new Deferred().resolve(''); + } + hover.content = result.contents; + } else if(result.contents !== null && typeof result.contents === 'object') { + hover.content = result.contents.value; + } + return new Deferred().resolve(hover); + }, + /* @callback */ function(err) { + return new Deferred().resolve(''); + }); + }); + }); + } + }, + { + name: "Java Hover", + contentType: ["text/x-java-source", "application/x-jsp"] //$NON-NLS-1$ //$NON-NLS-2$ + } + ); + /** + * Occurrences + */ + provider.registerService("orion.edit.occurrences", + { + computeOccurrences: function computeOccurrences(editorContext, args) { + return editorContext.getSelectionText().then(function(text) { + if (text.trim().length === 0) { + return new Deferred().resolve([]); + } + return editorContext.getFileMetadata().then(function(meta) { + return getPosition(editorContext, args.selection.start).then(function(position) { + return ipc.documentHighlight(meta.location, position).then(function(results) { + if(Array.isArray(results) && results.length > 0) { + return Deferred.all(results.map(function(result) { + return editorContext.getLineStart(result.range.start.line).then(function(offset) { + return { + start: result.range.start.character+offset, + end: result.range.end.character+offset + }; + }); + })); + } + return new Deferred().resolve([]); + }, + /* @callback */ function(err) { + return new Deferred().resolve([]); + }); + }); + }); + }); + } + }, + { + contentType: ["text/x-java-source", "application/x-jsp"] //$NON-NLS-1$ //$NON-NLS-2$ + } + ); + } + + function _findContainerIndex(list, container) { + var item = list[list.length-1]; + if(item && item.label === container) { + return list.length-1; + } + for (var i = 0; i < list.length; i++) { + if(list[i].label === container) { + return i; + } + } + return -1; + } + + /** + * @name resolveSymbolKind + * @description Converts the given symbol kind into its name + * @param {number} num The symbl kind + * @returns {String} The name of the symbol kind + */ + function resolveSymbolKind(num) { + switch(num) { + case 1: return 'File'; + case 2: return 'Module'; + case 3: return 'Namespace'; + case 4: return 'Package'; + case 5: return 'Class'; + case 6: return 'Method'; + case 7: return 'Property'; + case 8: return 'Field'; + case 9: return 'Constructor'; + case 10: return 'Enum'; + case 11: return 'Interface'; + case 12: return 'Function'; + case 13: return 'Variable'; + case 14: return 'Constant'; + case 15: return 'String'; + case 16: return 'Number'; + case 17: return 'Boolean'; + case 18: return 'Array'; + default: return 'Unknown'; + } + } + + /** + * @name getPosition + * @description Return a document position object for use with the protocol + * @param {?} editorContext The backing editor context + * @param {number} offset The Orion editor offset + * @returns {Deferred} Return a deferred that will resolve to a position object for the protocol textDocument requests + */ + function getPosition(editorContext, offset) { + return editorContext.getLineAtOffset(offset).then(function(line) { + return editorContext.getLineStart(line).then(function(lineOffset) { + return {line: line, character: offset-lineOffset}; + }); }); } + /** + * Converts the completion result kind into a human-readable string + */ + function resolveCompletionKind(num) { + switch(num) { + case 1: return 'Text'; + case 2: return 'Method'; + case 3: return 'Function'; + case 4: return 'Constructor'; + case 5: return 'Field'; + case 6: return 'Variable'; + case 7: return 'Class'; + case 8: return 'Interface'; + case 9: return 'Module'; + case 10: return 'Property'; + case 11: return 'Unit'; + case 12: return 'Value'; + case 13: return 'Enum'; + case 14: return 'Keyword'; + case 15: return 'Snippet'; + case 16: return 'Color'; + case 17: return 'File'; + case 18: return 'Reference'; + default: return 'Unknown'; + } + } + /** + * Register all of the service providers + */ + function registerServiceProviders(pluginProvider) { + } + return { - connect: connect, + connect: function connect() { + var headers = { + name: "Orion Java Tool Support", + version: "1.0", + description: "This plugin provides Java tools support for Orion." + }; + var serviceRegistry = new mServiceRegistry.ServiceRegistry(); + project = new JavaProject(serviceRegistry, ipc); + validator = new JavaValidator(project, serviceRegistry); + var pluginProvider = new PluginProvider(headers, serviceRegistry); + pluginProvider.registerServiceProvider("orion.core.contenttype", {}, { + contentTypes: [ + { + id: "text/x-java-source", + "extends": "text/plain", + name: "Java", + extension: ["java"] + }, + { + id: "application/x-jsp", + "extends": "text/plain", + name: "Java Server Page", + extension: ["jsp"] + } + ] + }); + mJava.grammars.forEach(function(current) { + pluginProvider.registerServiceProvider("orion.edit.highlighter", {}, current); + }); + mJSP.grammars.forEach(function(current) { + pluginProvider.registerServiceProvider("orion.edit.highlighter", {}, current); + }); + /** + * editor model changes + */ + pluginProvider.registerService("orion.edit.model", //$NON-NLS-1$ + { + onSaving: project.onSaving.bind(project), + onModelChanging: project.onModelChanging.bind(project), + onInputChanged: project.onInputChanged.bind(project) + }, + { + contentType: ["text/x-java-source", "application/x-jsp"] //$NON-NLS-1$ //$NON-NLS-2$ + } + ); + hookAPIs(pluginProvider); + pluginProvider.connect(); + initializeIPC(serviceRegistry); + }, registerServiceProviders: registerServiceProviders }; }); diff --git a/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/javaProject.js b/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/javaProject.js new file mode 100644 index 0000000..05cddd3 --- /dev/null +++ b/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/javaProject.js @@ -0,0 +1,325 @@ +/*******************************************************************************
+ * @license
+ * Copyright (c) 2016 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License v1.0
+ * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
+ * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+ /*eslint-env amd, browser*/
+define([
+ "orion/Deferred"
+], function(Deferred) {
+
+ var openedDocument;
+ function openDocument(project, evnt) {
+ //TODO handle split editor
+ if (openedDocument) {
+ project.ipc.didClose(openedDocument.location);
+ }
+ openedDocument = evnt.file;
+ evnt.file.version = 1;
+ project.ipc.didOpen(evnt.file.location, evnt.file.contentType.id, evnt.file.version, evnt.text);
+ }
+
+ var initialized = false,
+ fileHandler = {};
+ fileHandler.onInputChanged = fileHandler.onProjectChanged = function(project) { project.inputChanged = true; };
+
+ /**
+ * @description Creates a new JavaScript project
+ * @constructor
+ * @public
+ * @param {ServiceRegistry} serviceRegistry The service registry
+ * @param {IPC} ipc The backing IPC to send requests to
+ * @since 13.0
+ */
+ function JavaProject(serviceRegistry, ipc) {
+ this.projectMeta = null;
+ this.map = Object.create(null);
+ this.registry = serviceRegistry;
+ this.fileClient = null;
+ this.handlers = Object.create(null);
+ this.ipc = ipc;
+ this.handlers = [fileHandler];
+ }
+
+ /**
+ * The .tern-project file name
+ */
+ JavaProject.prototype.CLASSPATH = '.classpath';
+
+ /**
+ * @description Adds a handler for the given file name to the mapping of handlers
+ * @function
+ * @param {Object} functions The object map of functions
+ */
+ JavaProject.prototype.addHandler = function addHandler(functions) {
+ this.handlers.push(functions);
+ };
+
+ /**
+ * @description Returns the current project path
+ * @function
+ * @returns {String} The current project path or null if there is no project context
+ */
+ JavaProject.prototype.getProjectPath = function getProjectPath() {
+ if(this.projectMeta) {
+ return this.projectMeta.Location;
+ }
+ return null;
+ };
+
+ /** + * @name JavaProject.prototype.getJavaOptions + * @description Reads and returns the Java options for the project context + * @function + * @returns {?} The collection of java options + */ + JavaProject.prototype.getJavaOptions = function getJavaOptions() {
+ var d = new Deferred();
+ if(this.map.javaoptions) {
+ return d.resolve(this.map.javaoptions);
+ }
+ //TODO read the eclipse pref files
+ return d.resolve(null);
+ }
+
+ /**
+ * @description Fetch the named child of the current project context
+ * @function
+ * @param {String} childName The short name of the project child to get
+ * @returns {Deferred} A deferred that will resolve to the requested child metadata or null
+ */
+ JavaProject.prototype.getFile = function getFile(childName) {
+ if(!this.projectMeta) {
+ return new Deferred().resolve(null);
+ }
+ var filePath = this.projectMeta.Location+childName;
+ if(this.map[filePath]) {
+ return new Deferred().resolve(this.map[filePath]);
+ }
+ return this.getFileClient().read(filePath, false, false, {readIfExists: true}).then(function(child) {
+ this.map[filePath] = {name: filePath, contents: child, project: this.projectMeta.Location};
+ return this.map[filePath];
+ }.bind(this),
+ function() {
+ return null;
+ });
+ };
+
+ JavaProject.prototype.initFrom = function initFrom(path) {
+ if(!initialized) {
+ initialized = true;
+ return this.getFileClient().read(path, true, false, {readIfExists: true}).then(function(child) {
+ if(child) {
+ this.onInputChanged({file: child});
+ }
+ }.bind(this));
+ }
+ return new Deferred().resolve();
+ };
+
+ /**
+ * @description Update the contents of the given file name, and optionally create the file if it does not exist.
+ * NOTE: this function does not check for existig values or duplicate entries, those checks must be done prior to calling
+ * this function with the JSON values to merge
+ * @function
+ * @param {String} childName The short name of the project child to get
+ * @param {Boolean} create If the file should be created if it does not exist
+ * @param {Object} values The object of values to mix-in to the current values for a file.
+ */
+ JavaProject.prototype.updateFile = function updateFile(childName, create, values) {
+ if(this.projectMeta) {
+ return this.getFile(childName).then(function(child) {
+ if(child) {
+ var contents = child.contents;
+ if(typeof contents === 'string') {
+ var json;
+ if (contents.length) {
+ json = JSON.parse(contents);
+ _merge(values, json);
+ } else {
+ json = values;
+ }
+ return this.getFileClient().write(this.projectMeta.Location+childName, JSON.stringify(json, null, '\t'));
+ } else if(create) {
+ return this.getFileClient().createFile(this.projectMeta.Location, childName).then(function(file) {
+ return this.getFileClient().write(file.Location, JSON.stringify(values, null, '\t'));
+ }.bind(this));
+ }
+ }
+ }.bind(this));
+ }
+ };
+
+ function _merge(source, dest) {
+ Object.keys(source).forEach(function(key) {
+ if(Array.isArray(dest[key]) && Array.isArray(source[key])) {
+ dest[key] = [].concat(dest[key], source[key]);
+ } else if(typeof dest[key] === 'object' && dest[key] !== null) {
+ source[key] = source[key] || Object.create(null);
+ _merge(source[key], dest[key]);
+ } else {
+ dest[key] = source[key];
+ }
+ });
+ }
+
+ /**
+ * @name JavaScriptProject.prototype.getFileClient
+ * @description Returns the file client to use
+ * @function
+ * @returns {orion.FileClient} The file client
+ */
+ JavaProject.prototype.getFileClient = function getFileClient() {
+ if(!this.fileClient) {
+ this.fileClient = this.registry.getService("orion.core.file.client"); //$NON-NLS-1$
+ }
+ return this.fileClient;
+ };
+ /**
+ * Callback from the orion.edit.model service
+ * @param {Object} evnt An <tt>orion.edit.model</tt> event.
+ * @see https://wiki.eclipse.org/Orion/Documentation/Developer_Guide/Plugging_into_the_editor#orion.edit.model
+ */
+ JavaProject.prototype.onSaving = function onSaving(evnt) {
+ this.ipc.didSave(evnt.file.location);
+ };
+ /**
+ * Callback from the orion.edit.model service
+ * @param {Object} evnt An <tt>orion.edit.model</tt> event.
+ * @see https://wiki.eclipse.org/Orion/Documentation/Developer_Guide/Plugging_into_the_editor#orion.edit.model
+ */
+ JavaProject.prototype.onModelChanging = function onModelChanging(evnt) {
+ if (this.inputChanged) {
+ delete this.inputChanged;
+ openDocument(this, evnt);
+ return;
+ }
+ if (!evnt.range) return;
+ var change = {
+ range: evnt.range,
+ rangeLength: evnt.removedCharCount,
+ text: evnt.text
+ };
+ //TODO better way to get the version of the openDocument
+ this.ipc.didChange(evnt.file.location, openedDocument.version++, [change]);
+ };
+ /**
+ * Callback from the orion.edit.model service
+ * @param {Object} evnt An <tt>orion.edit.model</tt> event.
+ * @see https://wiki.eclipse.org/Orion/Documentation/Developer_Guide/Plugging_into_the_editor#orion.edit.model
+ */
+ JavaProject.prototype.onInputChanged = function onInputChanged(evnt) {
+ initialized = true;
+ var file = evnt.file,
+ project;
+ if(file) {
+ var parents = file.parents ? file.parents : file.Parents;
+ if (Array.isArray(parents)) {
+ if(parents.length > 0) {
+ project = parents[parents.length-1];
+ } else {
+ project = file;
+ }
+ }
+ }
+ if (project) {
+ if(!this.projectMeta || project.Location !== this.projectMeta.Location) {
+ this.projectMeta = project;
+ delete this.ecma;
+ delete this.map[this.TERN_PROJECT];
+ _handle.call(this, "onProjectChanged", this, evnt, project.Location);
+ return;
+ }
+ _handle.call(this, "onInputChanged", this, evnt, project.Location);
+ } else {
+ delete this.ecma;
+ _handle.call(this, "onProjectChanged", this, evnt, null);
+ }
+ };
+ /**
+ * Callback from the fileClient event listener
+ * @param {Object} evnt A file client Changed event.
+ */
+ JavaProject.prototype.onFileChanged = function onFileChanged(evnt) {
+ if(evnt && evnt.type === 'Changed') {
+ _updateMap.call(this, evnt.modified, "onModified");
+ _updateMap.call(this, evnt.deleted, "onDeleted");
+ _updateMap.call(this, evnt.created, "onCreated");
+ _updateMap.call(this, evnt.moved, "onMoved");
+ }
+ };
+ /**
+ * Update the backing map
+ * @param {Array.<String>} arr The array to walk
+ * @param {String} state The state, one of: onModified, onDeleted, onCreated
+ */
+ function _updateMap(arr, state) {
+ if(Array.isArray(arr)) {
+ arr.forEach(function(file) {
+ var f, toQ, toN, n;
+ switch(state) {
+ case 'onCreated': {
+ n = file.result ? file.result.Name : undefined;
+ f = file.result ? file.result.Location : undefined;
+ break;
+ }
+ case 'onDeleted': {
+ f = file.deleteLocation;
+ n = _shortName(file.deleteLocation);
+ break;
+ }
+ case 'onModified': {
+ n = _shortName(file);
+ f = file;
+ break;
+ }
+ case 'onMoved': {
+ toQ = file.result ? file.result.Location : undefined;
+ toN = file.result ? file.result.Name : undefined;
+ n = _shortName(file.source);
+ f = file.source;
+ break;
+ }
+ }
+ delete this.map[f];
+ _handle.call(this, state, this, f, n, toQ, toN);
+ }.bind(this));
+ }
+ }
+ /**
+ * @description Returns the shortname of the file
+ * @param {String} fileName The fully qualified path of the file
+ * @returns {String} The last segment of the path (short name)
+ */
+ function _shortName(fileName) {
+ var i = fileName.lastIndexOf('/');
+ if(i > -1) {
+ return fileName.substr(i+1);
+ }
+ return fileName;
+ }
+
+ /**
+ * @description Delegates to a handler for the given handler name (file type), with the given function name
+ * @param {String} funcName The name of the function to call on the handler iff it exists
+ */
+ function _handle(funcName) {
+ if(Array.isArray(this.handlers)) {
+ var args = Array.prototype.slice.call(arguments);
+ this.handlers.forEach(function(handler) {
+ var f = handler[funcName];
+ if(typeof f === 'function') {
+ f.apply(handler, args.slice(1));
+ }
+ });
+ }
+ }
+
+ return JavaProject;
+});
\ No newline at end of file diff --git a/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/javaValidator.js b/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/javaValidator.js new file mode 100644 index 0000000..b96f222 --- /dev/null +++ b/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/javaValidator.js @@ -0,0 +1,155 @@ +/*******************************************************************************
+ * @license
+ * Copyright (c) 2016 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License v1.0
+ * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
+ * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+ /*eslint-env amd, browser*/
+define([
+ "orion/Deferred",
+ "plugins/languages/java/ruleConfig"
+], function(Deferred, RuleConfig) {
+
+ var registry,
+ severities = {
+ 1: 'error',
+ 2: 'warning',
+ 3: 'info'
+ };
+
+ function JavaValidator(javaProject, serviceRegistry) {
+ this.project = javaProject;
+ registry = serviceRegistry;
+ this.config = new RuleConfig();
+ this.map = Object.create(null);
+ }
+
+ /**
+ * @description Callback to create problems from orion.edit.validator
+ * @function
+ * @public
+ * @param {orion.edit.EditorContext} editorContext The editor context
+ * @param {Object} context The in-editor context (selection, offset, etc)
+ * @returns {orion.Promise} A promise to compute some problems
+ * @callback
+ */
+ JavaValidator.prototype.computeProblems = function computeProblems(editorContext , context, config) {
+ var deferred = new Deferred();
+ var start = Date.now();
+ editorContext.getFileMetadata().then(function(meta) {
+ if(this.map[meta.location]) {
+ deferred.resolve(toProblems(this.map[meta.location].info));
+ delete this.map[meta.location];
+ logTiming(Date.now()-start, meta.contentType.id);
+ } else {
+ this.map[meta.location] = {deferred: deferred};
+ }
+ }.bind(this));
+ return deferred;
+ };
+
+ /** + * @name JavaValidator.prototype.updateDiagnostics + * @description Callback from the Java plugin when diagnostic notifications are received + * @function
+ * @callback + * @param {String} file The full URI of the file + * @param {[{?}]} info The array of diagnostic items + */ + JavaValidator.prototype.updateDiagnostics = function updateDiagnostics(file, info) {
+ if(this.map[file]) {
+ var c = this.map[file];
+ if(c.deferred) {
+ if(this.project) {
+ this.project.getJavaOptions().then(function(cfg) {
+ c.deferred.resolve(toProblems(info, cfg || this.config));
+ delete this.map[file];
+ }.bind(this));
+ } else {
+ c.deferred.resolve(toProblems(info));
+ delete this.map[file];
+ }
+ } else {
+ // we are getting back some diagnostics before computeProblems is run
+ // clear the cached one and replace it with the new one
+ this.map[file] = {info: info};
+ }
+ } else {
+ //got the notification before computeProblems was called, cache it
+ this.map[file] = {info: info};
+ }
+ if (!this.markerService) {
+ this.markerService = registry.getService("orion.core.marker");
+ }
+ if (this.markerService && this.map[file].info) {
+ this.markerService._setProblems(toProblems(this.map[file].info));
+ // problems have been reported, we can remove them. No need to report them again on save during computeProblems
+ delete this.map[file];
+ }
+ };
+
+ /** + * @name toProblems + * @description Convert the diagnostics data to Orion problems + * @param {?} info The diagnostics info
+ * @param {?} config The configuration to set severites with + * @returns {[?]} The array of Orion problem objects + */ + function toProblems(info, config) {
+ var problems = [],
+ c = config || {};
+ info.forEach(function(item) {
+ var severity = getSeverity(item.code, item.severity, c);
+ if(!severity) {
+ return;
+ }
+ problems.push({
+ description: item.message,
+ id: item.code,
+ severity: severity,
+ range: item.range
+ });
+ });
+ return problems;
+ }
+
+ /** + * @name getSeverity + * @description Returns the severity for the given problem id or null if there is no configuration entry for it + * @param {String} id The id of the problem + * @param {?} config The collection of configuration entries + * @returns {String | null} The name of the severity or null if its 'ignore' + */ + function getSeverity(id, severity, config) {
+ if(config[id]) {
+ if(config[id] === 3) {
+ return null;
+ }
+ return severities[config[id]];
+ } else if(severities[severity] === 3) {
+ return null;
+ }
+ return severities[severity];
+ }
+
+ /**
+ * @description Log the given timing in the metrics service
+ * @param {Number} end The total time to log
+ * @param {String} contentTypeId The id of the content type - this validator handles multiple languages
+ */
+ function logTiming(end, contentTypeId) {
+ if(registry) {
+ var metrics = registry.getService("orion.core.metrics.client"); //$NON-NLS-1$
+ if(metrics) {
+ metrics.logTiming('language tools', 'validation', end, contentTypeId); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ }
+ }
+ }
+
+ return JavaValidator;
+});
\ No newline at end of file diff --git a/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/ruleConfig.js b/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/ruleConfig.js new file mode 100644 index 0000000..f7214ad --- /dev/null +++ b/bundles/org.eclipse.orion.client.ui/web/plugins/languages/java/ruleConfig.js @@ -0,0 +1,93 @@ +/*******************************************************************************
+ * @license
+ * Copyright (c) 2016 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License v1.0
+ * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
+ * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+ /*eslint-env amd, browser*/
+define([], function() {
+
+ /** + * @name RuleConfig
+ * @constructs + * @description Creates a new RuleConfig instance + * @param {?} rules An optional pre-existing collection of rules to initialize this config with + * @returns {RuleConfig} A new RuleConfig instance
+ * @since 13.0 + */ + function RuleConfig(rules) {
+ if(rules && typeof rules === 'object') {
+ Object.keys(rules).forEach(function(key) {
+ this.setOption(key, rules[key] /*, key?, index*/);
+ }.bind(this));
+ }
+ }
+
+ RuleConfig.prototype = {
+ // 0:off, 1:warning, 2:error
+ defaults: Object.freeze({
+ //TODO put rule defaults in here
+ }),
+ /**
+ * @description Sets the given rule to the given enabled value
+ * @function
+ * @private
+ * @param {String} ruleId The id of the rule to change
+ * @param {Number} value The value to set the rule to
+ * @param {Object} [key] Optional key to use for complex rule configuration.
+ */
+ setOption: function(ruleId, value, key, index) {
+ if(Array.isArray(this.rules[ruleId])) {
+ var ruleConfig = this.rules[ruleId];
+ var lastIndex = ruleConfig.length - 1;
+ if (index) {
+ ruleConfig[index] = value;
+ } else if (key) {
+ ruleConfig[lastIndex] = ruleConfig[lastIndex] || {};
+ ruleConfig[lastIndex][key] = value;
+ } else {
+ ruleConfig[0] = value;
+ }
+ } else {
+ this.rules[ruleId] = value;
+ }
+ },
+
+ /**
+ * @description Resets the rules to their default values
+ * @function
+ */
+ setDefaults: function setDefaults() {
+ this.rules = Object.create(null);
+ var keys = Object.keys(this.defaults);
+ for (var i = 0; i < keys.length; i++) {
+ var key = keys[i];
+ var defaultValue = this.defaults[key];
+ if (Array.isArray(defaultValue)) {
+ var value = [];
+ defaultValue.forEach(function(element) {
+ if (typeof element === 'object') {
+ var newElement= Object.create(null);
+ Object.keys(element).forEach(function (key) {
+ newElement[key] = element[key];
+ });
+ value.push(newElement);
+ } else {
+ value.push(element);
+ }
+ });
+ this.rules[key] = value;
+ } else {
+ this.rules[key] = this.defaults[key];
+ }
+ }
+ }
+ };
+
+ return RuleConfig;
+});
\ No newline at end of file diff --git a/modules/orionode/.gitignore b/modules/orionode/.gitignore index 6c07a75..971d77f 100644 --- a/modules/orionode/.gitignore +++ b/modules/orionode/.gitignore @@ -16,3 +16,4 @@ lib/orion.client/ # Ignore per-user Orionode data .workspace*/ test/.test_workspace/ +/server/ diff --git a/modules/orionode/Dockerfile b/modules/orionode/Dockerfile new file mode 100644 index 0000000..5112f22 --- /dev/null +++ b/modules/orionode/Dockerfile @@ -0,0 +1,61 @@ +FROM java:8 + +ARG JAVA_LANGUAGE_SERVER +ARG NODE_VERSION +ARG LSP_ORION_VERSION + +# install java lsp server based on the latest build +RUN mkdir -p /home/java-language-server +WORKDIR /home/java-language-server +RUN curl -L https://dl.bintray.com/gorkem/java-language-server/java-server-1.0.0-$JAVA_LANGUAGE_SERVER.tar.gz -o /home/java-language-server/java-server.tar.gz +RUN tar -xzvf /home/java-language-server/java-server.tar.gz +RUN rm /home/java-language-server/java-server.tar.gz + +# install node 4.4.1 which is the version that matches the one used on hudson for nodejs server builds +RUN groupadd --gid 1000 node \ + && useradd --uid 1000 --gid node --shell /bin/bash --create-home node + +# gpg keys listed at https://github.com/nodejs/node +RUN set -ex \ + && for key in \ + 9554F04D7259F04124DE6B476D5A82AC7E37093B \ + 94AE36675C464D64BAFA68DD7434390BDBE9B9C5 \ + 0034A06D9D9B0064CE8ADF6BF1747F4AD2306D93 \ + FD3A5288F042B6850C66B31F09FE44734EB7990E \ + 71DCFD284A79C3B38668286BC97EC7A07EDE3FC1 \ + DD8F2338BAE7501E3DD5AC78C273792F7D83545D \ + B9AE9905FFD7803F25714661B63B535A4C206CA9 \ + C4F0DFFF4E8C1A8236409D08E73BC641CC11F4C8 \ + ; do \ + gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \ + done + +RUN curl -SLO "https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.xz" \ + && curl -SLO "https://nodejs.org/dist/v$NODE_VERSION/SHASUMS256.txt.asc" \ + && gpg --batch --decrypt --output SHASUMS256.txt SHASUMS256.txt.asc \ + && grep " node-v$NODE_VERSION-linux-x64.tar.xz\$" SHASUMS256.txt | sha256sum -c - \ + && tar -xJf "node-v$NODE_VERSION-linux-x64.tar.xz" -C /usr/local --strip-components=1 \ + && rm "node-v$NODE_VERSION-linux-x64.tar.xz" SHASUMS256.txt.asc SHASUMS256.txt \ + && ln -s /usr/local/bin/node /usr/local/bin/nodejs + +# Create app directory +RUN mkdir -p /home/orion +WORKDIR /home/orion + +# Retrieve nodejs Orion server and install it +RUN curl -s download.eclipse.org/orion/lsporionode/lsporionode_$LSP_ORION_VERSION.tar.gz > /home/orion/orionode.tar.gz +RUN tar -xzvf /home/orion/orionode.tar.gz +RUN rm /home/orion/orionode.tar.gz + +# Copy the lsp server into the server folder +RUN cp -R /home/java-language-server/ /home/orion/orionode/server + +WORKDIR /home/orion/orionode + +# install sample project for demoing +RUN mkdir -p /home/workspace + +EXPOSE 8081 + +WORKDIR /home/orion/orionode +CMD [ "node", "server.js","-w","/home/workspace" ]
\ No newline at end of file diff --git a/modules/orionode/build/lsporionode_build.sh b/modules/orionode/build/lsporionode_build.sh new file mode 100644 index 0000000..4e4f6c4 --- /dev/null +++ b/modules/orionode/build/lsporionode_build.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +function die () { + echo "Error:" "$1" >&2 + exit 1 +} + +DOWNLOADS=/home/data/httpd/download.eclipse.org + +NODEGIT_VERSION=v0.16.0 +(npm install --no-optional) || die "Failed to install dependencies, consult the npm log to find out why." +cp ${DOWNLOADS}/orion/orionode/nodegit/${NODEGIT_VERSION}/linux/nodegit.node ../node_modules/nodegit/build/Release +(../node_modules/.bin/grunt ${GRUNT_TASK}) || die "Failed to minify client code." +rm -rf ../node_modules +(npm install --production --no-optional) || die "Failed to install dependencies, consult the npm log to find out why." +cp ${DOWNLOADS}/orion/orionode/nodegit/${NODEGIT_VERSION}/linux/nodegit.node ../node_modules/nodegit/build/Release +rm -rf ../node_modules/pty.js +rm -rf ../node_modules/nodegit/vendor +rm -rf ../node_modules/nodegit/build/Release/obj.target +rm -rf ../target +cd ../.. + +# Update build ID +sed -i "s/orion\.buildId\=/orion\.buildId\=${1}/" orionode/orion.conf +sed -i "s/var BUILD_ID \= \"unknown\"\;/var BUILD_ID \= \"${1}\"\;/" orionode/lib/version.js + +tar -czf "lsporionode_$1.tar.gz" orionode/ +cp "lsporionode_$1.tar.gz" ${DOWNLOADS}/orion/lsporionode/ diff --git a/modules/orionode/docker_build.sh b/modules/orionode/docker_build.sh new file mode 100755 index 0000000..f167568 --- /dev/null +++ b/modules/orionode/docker_build.sh @@ -0,0 +1,5 @@ +#!/bin/bash +LSP_ORION_VERSION=`curl -s https://hudson.eclipse.org/orion/job/lsp-orion-node/lastSuccessfulBuild/api/json | jq -r '.id'` +JAVA_LANGUAGE_SERVER=latest +NODE_VERSION=4.4.1 +docker build --build-arg JAVA_LANGUAGE_SERVER=$JAVA_LANGUAGE_SERVER --build-arg LSP_ORION_VERSION=$LSP_ORION_VERSION --build-arg NODE_VERSION=$NODE_VERSION -t orionlsp . diff --git a/modules/orionode/lib/languageServer.js b/modules/orionode/lib/languageServer.js new file mode 100644 index 0000000..6966ce6 --- /dev/null +++ b/modules/orionode/lib/languageServer.js @@ -0,0 +1,327 @@ +/******************************************************************************* + * Copyright (c) 2016 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials are made + * available under the terms of the Eclipse Public License v1.0 + * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution + * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +/*eslint-env node, rimraf*/ +/*eslint-disable no-sync*/ +var path = require('path'); +var cp = require('child_process'); +var rimraf = require('rimraf'); +var api = require('./api'); +var net = require('net'); +var fs = require('fs'); + +var IN_PORT = 8123; +var OUT_PORT = 8124; + +function generatePatchedEnv(env, inPort, outPort) { + // Set the two unique pipe names and the electron flag as process env + var newEnv = {}; + for (var key in env) { + newEnv[key] = env[key]; + } + newEnv['STDIN_PORT'] = inPort; + newEnv['STDOUT_PORT'] = outPort; + return newEnv; +} + +function fork(modulePath, args, options, callback) { + var callbackCalled = false; + var resolve = function(result) { + if (callbackCalled) { + return; + } + callbackCalled = true; + callback(null, result); + }; + var reject = function(err) { + if (callbackCalled) { + return; + } + callbackCalled = true; + callback(err, null); + }; + + var newEnv = generatePatchedEnv(options.env || process.env, IN_PORT, OUT_PORT); + var childProcess = cp.spawn(modulePath, args, { + silent: true, + cwd: options.cwd, + env: newEnv, + execArgv: options.execArgv + }); + childProcess.once('error', function(err) { + console.log(err); + reject(err); + }); + childProcess.once('exit', function(err) { + console.log(err); + reject(err); + }); + resolve(childProcess); +} + +var DEBUG = true; + +function runJavaServer(javaHome) { + return new Promise(function(resolve, reject) { + var child = javaHome + '/jre/bin/java'; // 'C:/devops/git/java-language-server/org.jboss.tools.vscode.product/target/products/languageServer.product/win32/win32/x86_64/eclipse'; + var params = []; + var workspacePath = path.resolve(__dirname, "../server/tmp_ws"); + rimraf(workspacePath, function(error) { + //TODO handle error + if (DEBUG) { + params.push('-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044'); + } + params.push("-Dlog.level=ALL"); + params.push('-Declipse.application=org.jboss.tools.vscode.java.id1'); + params.push('-Dosgi.bundles.defaultStartLevel=4'); + params.push('-Declipse.product=org.jboss.tools.vscode.java.product'); + if (DEBUG) { + params.push('-Dlog.protocol=true'); + } + + var pluginsFolder = path.resolve(__dirname, '../server/plugins'); + return fs.readdirAsync(pluginsFolder).then(function(files) { + if (Array.isArray(files) && files.length !== 0) { + for (var i = 0, length = files.length; i < length; i++) { + var file = files[i]; + var indexOf = file.indexOf('org.eclipse.equinox.launcher_'); + if (indexOf !== -1) { + params.push('-jar'); + params.push(path.resolve(__dirname, '../server/plugins/' + file)); + + //select configuration directory according to OS + var configDir = 'config_win'; + if (process.platform === 'darwin') { + configDir = 'config_mac'; + } else if (process.platform === 'linux') { + configDir = 'config_linux'; + } + params.push('-configuration'); + params.push(path.resolve(__dirname, '../server', configDir)); + params.push('-data'); + params.push(workspacePath); + + return fork(child, params, {}, function(err, result) { + if (err) reject(err); + if (result) resolve(result); + }); + } + } + } + }); + }); + }); +} +var CONTENT_LENGTH = 'Content-Length: '; +var CONTENT_LENGTH_SIZE = CONTENT_LENGTH.length; + +function fixURI(p, workspaceUrl) { + if (Array.isArray(p)) { + p.forEach(function(element) { + fixURI(element, workspaceUrl); + }); + } + if (p.uri) { + var s = p.uri.slice(workspaceUrl.length); + p.uri = api.join('/file', s.charAt(0) === '/' ? s.slice(1) : s); + } +} + +var remainingData; +var ready = false; + +function parseMessage(data, workspaceUrl, sock) { + try { + var dataContents = data.toString('utf-8'); + if (remainingData) { + dataContents = remainingData + dataContents; + } + var offset = -1; + var headerIndex = -1; + loop: while ((headerIndex = dataContents.indexOf(CONTENT_LENGTH, offset)) !== -1) { + // this is an known header + var headerSizeIndex = dataContents.indexOf('\r\n\r\n', headerIndex + CONTENT_LENGTH_SIZE, 'utf-8'); + if (headerSizeIndex !== -1) { + var messageSize = Number(dataContents.slice(headerIndex + CONTENT_LENGTH_SIZE, headerSizeIndex)); + if (messageSize + headerSizeIndex >= dataContents.length) { + // not enough data + offset = headerIndex; + break loop; + } + offset = headerSizeIndex + 4 + messageSize; + var message = Object.create(null); + message.headers = { + 'Content-Length': messageSize + }; + // enough data to get the message contents + var contents = dataContents.slice(headerSizeIndex + 4, headerSizeIndex + 4 + messageSize); + var json = null; + try { + json = JSON.parse(contents); + } catch(e) { + console.log(e); + } + message.content = json; + if (json) { + if (json.params) { + fixURI(json.params, workspaceUrl); + } + if (json.result) { + fixURI(json.result, workspaceUrl); + } + } + if (sock) { + // detect that the server is ready +// {"method":"language/status","params":{"type":"Started","message":"Ready"},"jsonrpc":"2.0"} + if (!ready + && json.method === "language/status" + && json.params + && json.params.type === "Started" + && json.params.message === "Ready") { + ready = true; + } + console.log(contents); + sock.emit('data', json); + } + } else { + offset = headerIndex; + break loop; + } + } + if (offset === -1) { + remainingData = dataContents; + } else if (offset < dataContents.length) { + remainingData = dataContents.slice(offset, dataContents.length); + } else { + remainingData = null; + } + //console.log('remaining data: >>>' + remainingData + '<<<'); + } catch (err) { + console.log(err); + } +} + +exports.install = function(options) { + var io = options.io; + if (!io) { + throw new Error('missing options.io'); + } + var workspaceUrl = "file:///" + options.workspaceDir.replace(/\\/g, "/"); + + var javaHome = process.env["JAVA_HOME"]; + if (!javaHome) { + throw new Error('JAVA_HOME needs to be set'); + } + + io.of('/languageServer').on('connection', function(sock) { + sock.on('start', /* @callback */ function(cwd) { + var receiveFromServer = net.createServer({}, function(stream) { + console.log('receiveFromServer socket connected'); + stream.on('data', function(data) { + parseMessage(data, workspaceUrl, sock); + }); + stream.on('error', function(err) { + console.log('receiveFromServer stream error: ' + err.toString()); + }); + stream.on('end', function() { + console.log('receiveFromServer disconnected'); + }); + }); + receiveFromServer.listen(IN_PORT, null, null, function() { + console.log("Listening to lsp server replies"); + }); + receiveFromServer.on('error', function(err) { + console.log('receiveFromServer error: ' + err.toString()); + }); + receiveFromServer.on('end', function() { + console.log('Disconnected receiveFromServer'); + }); + + var startup = true; + var sendToServer = net.createServer({}, function(stream) { + console.log('sendToServer socket connected'); + + sock.on('data', function(data) { + var textDocument = data.params && data.params.textDocument && data.params.textDocument; + if (textDocument && textDocument.uri) { + textDocument.uri = workspaceUrl + textDocument.uri.replace(/^\/file/, ''); + } + var s = JSON.stringify(data); + if (!ready) { + // check if this is the initialization message if not skip it + if (data.method !== "initialize" && data.method !== "textDocument/didOpen") { + return; + } + } + console.log('data sent : ' + s); + stream.write("Content-Length: " + s.length + "\r\n\r\n" + s); + }); + stream.on('error', function(err) { + console.log('sendToServer stream error: ' + err.toString()); + }); + stream.on('end', function() { + console.log('sendToServer stream disconnected'); + }); + if (sock && startup) { + startup = false; + sock.emit('ready', + JSON.stringify({ + workspaceDir: options.workspaceDir, + processId: process.pid + })); + } + + }); + sendToServer.listen(OUT_PORT, null, null, function() { + console.log("sendToServer socket is listening"); + }); + sendToServer.on('error', function(err) { + console.log('sendToServer socket error: ' + err.toString()); + }); + sendToServer.on('end', function() { + console.log('Disconnected sendToServer'); + }); + var serverClosed = false; + var closeServer = function () { + if (serverClosed) { + return; + } + serverClosed = true; + receiveFromServer.close(); + sendToServer.close(); + }; + runJavaServer(javaHome).then(function(child) { + child.on('error', function(err) { + closeServer(); + console.log('java server process error: ' + err.toString()); + }); + child.once('error', function(err) { + closeServer(); + console.log(err); + }); + child.once('exit', function() { + closeServer(); + }); + sock.on('disconnect', function() { + if (child.connected) { + child.disconnect(); + } else { + child.kill(); + } + sock = null; + remainingData = null; + ready = false; + }); + }); + }); + }); +}; + +exports.uninstall = function() {};
\ No newline at end of file diff --git a/modules/orionode/lib/orionode.client/defaults.pref b/modules/orionode/lib/orionode.client/defaults.pref index d81b1d3..f0bd34c 100644 --- a/modules/orionode/lib/orionode.client/defaults.pref +++ b/modules/orionode/lib/orionode.client/defaults.pref @@ -1,9 +1,11 @@ { "/plugins":{ "plugins/orionSharedWorker.js": true, + "plugins/languages/java/javaPlugin.html": true, "git/plugins/gitPlugin.html":true, "webtools/plugins/webToolsPlugin.html":true, "javascript/plugins/javascriptPlugin.html":true, + "plugins/consolePlugin.html":true, "edit/content/imageViewerPlugin.html":true, "edit/content/jsonEditorPlugin.html":true, "cfui/plugins/cFPlugin.html":true, diff --git a/modules/orionode/lib/searchWorker.js b/modules/orionode/lib/searchWorker.js index 95354c0..b53a22f 100644 --- a/modules/orionode/lib/searchWorker.js +++ b/modules/orionode/lib/searchWorker.js @@ -121,7 +121,7 @@ try { } } else if(term.indexOf(":") > -1) { //unknown search term - continue; + continue; } else { this.searchTerm = decodeURIComponent(term); this.fileContentSearch = true; diff --git a/modules/orionode/package.json b/modules/orionode/package.json index 02c6475..3743aaf 100644 --- a/modules/orionode/package.json +++ b/modules/orionode/package.json @@ -28,6 +28,7 @@ "mongodb": "^2.1.7", "mongoose": "^4.4.6", "multiparty": "^4.1.2", + "net" : "^1.0.2", "nodegit": "0.16.0", "nodemailer": "^2.3.0", "passport": "^0.3.2", diff --git a/modules/orionode/server.js b/modules/orionode/server.js index c2f4d9f..e715599 100644 --- a/modules/orionode/server.js +++ b/modules/orionode/server.js @@ -22,6 +22,7 @@ var auth = require('./lib/middleware/auth'), util = require('util'), argslib = require('./lib/args'), ttyShell = require('./lib/tty_shell'), + languageServer = require('./lib/languageServer'), orion = require('./index.js'), prefs = require('./lib/controllers/prefs'); @@ -91,9 +92,13 @@ function startServer(cb) { configParams: configParams, maxAge: dev ? 0 : undefined, })); + var io = socketio.listen(server, { 'log level': 1 }); + ttyShell.install({ io: io, fileRoot: '/file', workspaceDir: workspaceDir }); + languageServer.install({ io: io, workspaceDir: workspaceDir }); //TODO no good for multiuser + server.on('listening', function() { console.log(util.format('Listening on port %d...', port)); if (cb) { |