diff --git a/Cakefile b/Cakefile index e74b799..57520d8 100644 --- a/Cakefile +++ b/Cakefile @@ -1,6 +1,7 @@ fs = require 'fs' {exec} = require 'child_process' util = require 'util' +umdify = require 'libumd' binDir = "./node_modules/.bin/" @@ -15,7 +16,7 @@ task 'watch', 'Watch for changes in coffee files to build and test', -> watchDir 'test', -> invoke 'build:test' watchDir 'dist/test', (file)-> - # We only want to run tests once (a second), + # We only want to run tests once (a second), # even if a bunch of test files change time = new Date().getTime() if (time-lastTest) > 1000 @@ -24,7 +25,7 @@ task 'watch', 'Watch for changes in coffee files to build and test', -> task 'test', 'Run the tests', -> util.log "Running tests..." - exec binDir + "jasmine-node --nocolor dist/test", (err, stdout, stderr) -> + exec binDir + "jasmine-node --nocolor dist/test", (err, stdout, stderr) -> if err handleError(parseTestResults(stdout), stderr) else @@ -38,9 +39,24 @@ task 'build', 'Build source and tests', -> task 'build:src', 'Build the src files into lib', -> util.log "Compiling src..." - exec binDir + "coffee -o lib/ -c src/", (err, stdout, stderr) -> + invoke 'build:node' + invoke 'build:umd' + +task 'build:node', 'Build node version of the lib', -> + util.log "Compiling node version..." + exec binDir + "coffee -j lib/stomp-node.js -c src/stomp-node.coffee", (err, stdout, stderr) -> handleError(err) if err +task 'build:umd', 'Build UMD version of the lib', -> + util.log "Compiling UMD version..." + exec binDir + "coffee -b -j lib/stomp.js -c src/stomp.coffee", (err, stdout, stderr) -> + handleError(err) if err + fs.readFile "lib/stomp.js", (err, data) -> + handleError(err) if err + result = umdify String(data), { globalAlias: 'Stomp' } + fs.writeFile "lib/stomp.js", result, (err) -> + handleError(err) if err + task 'build:min', 'Build the minified files into lib', -> util.log "Minify src..." exec binDir + "uglifyjs -m --comments all -o lib/stomp.min.js lib/stomp.js", (err, stdout, stderr) -> @@ -48,12 +64,12 @@ task 'build:min', 'Build the minified files into lib', -> task 'build:doc', 'Build docco documentation', -> util.log "Building doc..." - exec binDir + "docco -o doc/ src/*.coffee", (err, stdout, stderr) -> + exec binDir + "docco -o doc/ src/*.coffee", (err, stdout, stderr) -> handleError(err) if err task 'build:test', 'Build the test files into lib/test', -> util.log "Compiling test..." - exec binDir + "coffee -o dist/test/ -c test/", (err, stdout, stderr) -> + exec binDir + "coffee -o dist/test/ -c test/", (err, stdout, stderr) -> handleError(err) if err watchDir = (dir, callback) -> @@ -73,14 +89,14 @@ parseTestResults = (data) -> lastLine = (data) -> (line for line in data.split('\n') when line.length > 5).pop() -handleError = (error, stderr) -> +handleError = (error, stderr) -> if stderr? and !error util.log stderr displayNotification stderr.match(/\n(Error:[^\n]+)/)?[1] else util.log error displayNotification error - -displayNotification = (message = '') -> + +displayNotification = (message = '') -> options = { title: 'CoffeeScript' } try require('growl').notify message, options diff --git a/lib/stomp-node.js b/lib/stomp-node.js index 68660ee..6bbb106 100644 --- a/lib/stomp-node.js +++ b/lib/stomp-node.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.7.1 +// Generated by CoffeeScript 1.9.0 /* Stomp Over WebSocket http://www.jmesnil.net/stomp-websocket/doc/ | Apache License V2.0 @@ -13,11 +13,11 @@ net = require('net'); - Stomp.Stomp.setInterval = function(interval, f) { + Stomp.setInterval = function(interval, f) { return setInterval(f, interval); }; - Stomp.Stomp.clearInterval = function(id) { + Stomp.clearInterval = function(id) { return clearInterval(id); }; @@ -92,13 +92,13 @@ overTCP = function(host, port) { var socket; socket = wrapTCP(port, host); - return Stomp.Stomp.over(socket); + return Stomp.over(socket); }; overWS = function(url) { var socket; socket = wrapWS(url); - return Stomp.Stomp.over(socket); + return Stomp.over(socket); }; exports.overTCP = overTCP; diff --git a/lib/stomp.js b/lib/stomp.js index 9c30356..ed24e12 100644 --- a/lib/stomp.js +++ b/lib/stomp.js @@ -1,4 +1,20 @@ -// Generated by CoffeeScript 1.7.1 +(function (root, factory) { + if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module unless amdModuleId is set + define([], function () { + return (factory()); + }); + } else if (typeof exports === 'object') { + // Node. Does not work with strict CommonJS, but + // only CommonJS-like environments that support module.exports, + // like Node. + module.exports = factory(); + } else { + root['Stomp'] = factory(); + } +}(this, function () { + +// Generated by CoffeeScript 1.9.0 /* Stomp Over WebSocket http://www.jmesnil.net/stomp-websocket/doc/ | Apache License V2.0 @@ -6,496 +22,490 @@ Copyright (C) 2010-2013 [Jeff Mesnil](http://jmesnil.net/) Copyright (C) 2012 [FuseSource, Inc.](http://fusesource.com) */ +var Byte, Client, Frame, Stomp, + __hasProp = {}.hasOwnProperty, + __slice = [].slice; + +Byte = { + LF: '\x0A', + NULL: '\x00' +}; + +Frame = (function() { + var unmarshallSingle; + + function Frame(_at_command, _at_headers, _at_body) { + this.command = _at_command; + this.headers = _at_headers != null ? _at_headers : {}; + this.body = _at_body != null ? _at_body : ''; + } -(function() { - var Byte, Client, Frame, Stomp, - __hasProp = {}.hasOwnProperty, - __slice = [].slice; - - Byte = { - LF: '\x0A', - NULL: '\x00' + Frame.prototype.toString = function() { + var lines, name, skipContentLength, value, _ref; + lines = [this.command]; + skipContentLength = this.headers['content-length'] === false ? true : false; + if (skipContentLength) { + delete this.headers['content-length']; + } + _ref = this.headers; + for (name in _ref) { + if (!__hasProp.call(_ref, name)) continue; + value = _ref[name]; + lines.push(name + ":" + value); + } + if (this.body && !skipContentLength) { + lines.push("content-length:" + (Frame.sizeOfUTF8(this.body))); + } + lines.push(Byte.LF + this.body); + return lines.join(Byte.LF); }; - Frame = (function() { - var unmarshallSingle; - - function Frame(command, headers, body) { - this.command = command; - this.headers = headers != null ? headers : {}; - this.body = body != null ? body : ''; + Frame.sizeOfUTF8 = function(s) { + if (s) { + return encodeURI(s).match(/%..|./g).length; + } else { + return 0; } + }; - Frame.prototype.toString = function() { - var lines, name, skipContentLength, value, _ref; - lines = [this.command]; - skipContentLength = this.headers['content-length'] === false ? true : false; - if (skipContentLength) { - delete this.headers['content-length']; - } - _ref = this.headers; - for (name in _ref) { - if (!__hasProp.call(_ref, name)) continue; - value = _ref[name]; - lines.push("" + name + ":" + value); - } - if (this.body && !skipContentLength) { - lines.push("content-length:" + (Frame.sizeOfUTF8(this.body))); - } - lines.push(Byte.LF + this.body); - return lines.join(Byte.LF); - }; - - Frame.sizeOfUTF8 = function(s) { - if (s) { - return encodeURI(s).match(/%..|./g).length; - } else { - return 0; - } - }; - - unmarshallSingle = function(data) { - var body, chr, command, divider, headerLines, headers, i, idx, len, line, start, trim, _i, _j, _len, _ref, _ref1; - divider = data.search(RegExp("" + Byte.LF + Byte.LF)); - headerLines = data.substring(0, divider).split(Byte.LF); - command = headerLines.shift(); - headers = {}; - trim = function(str) { - return str.replace(/^\s+|\s+$/g, ''); - }; - _ref = headerLines.reverse(); - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - line = _ref[_i]; - idx = line.indexOf(':'); - headers[trim(line.substring(0, idx))] = trim(line.substring(idx + 1)); - } - body = ''; - start = divider + 2; - if (headers['content-length']) { - len = parseInt(headers['content-length']); - body = ('' + data).substring(start, start + len); - } else { - chr = null; - for (i = _j = start, _ref1 = data.length; start <= _ref1 ? _j < _ref1 : _j > _ref1; i = start <= _ref1 ? ++_j : --_j) { - chr = data.charAt(i); - if (chr === Byte.NULL) { - break; - } - body += chr; - } - } - return new Frame(command, headers, body); + unmarshallSingle = function(data) { + var body, chr, command, divider, headerLines, headers, i, idx, len, line, start, trim, _i, _j, _len, _ref, _ref1; + divider = data.search(RegExp("" + Byte.LF + Byte.LF)); + headerLines = data.substring(0, divider).split(Byte.LF); + command = headerLines.shift(); + headers = {}; + trim = function(str) { + return str.replace(/^\s+|\s+$/g, ''); }; - - Frame.unmarshall = function(datas) { - var frame, frames, last_frame, r; - frames = datas.split(RegExp("" + Byte.NULL + Byte.LF + "*")); - r = { - frames: [], - partial: '' - }; - r.frames = (function() { - var _i, _len, _ref, _results; - _ref = frames.slice(0, -1); - _results = []; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - frame = _ref[_i]; - _results.push(unmarshallSingle(frame)); + _ref = headerLines.reverse(); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + line = _ref[_i]; + idx = line.indexOf(':'); + headers[trim(line.substring(0, idx))] = trim(line.substring(idx + 1)); + } + body = ''; + start = divider + 2; + if (headers['content-length']) { + len = parseInt(headers['content-length']); + body = ('' + data).substring(start, start + len); + } else { + chr = null; + for (i = _j = start, _ref1 = data.length; start <= _ref1 ? _j < _ref1 : _j > _ref1; i = start <= _ref1 ? ++_j : --_j) { + chr = data.charAt(i); + if (chr === Byte.NULL) { + break; } - return _results; - })(); - last_frame = frames.slice(-1)[0]; - if (last_frame === Byte.LF || (last_frame.search(RegExp("" + Byte.NULL + Byte.LF + "*$"))) !== -1) { - r.frames.push(unmarshallSingle(last_frame)); - } else { - r.partial = last_frame; + body += chr; } - return r; - }; + } + return new Frame(command, headers, body); + }; - Frame.marshall = function(command, headers, body) { - var frame; - frame = new Frame(command, headers, body); - return frame.toString() + Byte.NULL; + Frame.unmarshall = function(datas) { + var frame, frames, last_frame, r; + frames = datas.split(RegExp("" + Byte.NULL + Byte.LF + "*")); + r = { + frames: [], + partial: '' }; + r.frames = (function() { + var _i, _len, _ref, _results; + _ref = frames.slice(0, -1); + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + frame = _ref[_i]; + _results.push(unmarshallSingle(frame)); + } + return _results; + })(); + last_frame = frames.slice(-1)[0]; + if (last_frame === Byte.LF || (last_frame.search(RegExp("" + Byte.NULL + Byte.LF + "*$"))) !== -1) { + r.frames.push(unmarshallSingle(last_frame)); + } else { + r.partial = last_frame; + } + return r; + }; - return Frame; + Frame.marshall = function(command, headers, body) { + var frame; + frame = new Frame(command, headers, body); + return frame.toString() + Byte.NULL; + }; - })(); + return Frame; - Client = (function() { - var now; +})(); - function Client(ws) { - this.ws = ws; - this.ws.binaryType = "arraybuffer"; - this.counter = 0; - this.connected = false; - this.heartbeat = { - outgoing: 10000, - incoming: 10000 - }; - this.maxWebSocketFrameSize = 16 * 1024; - this.subscriptions = {}; - this.partialData = ''; - } +Client = (function() { + var now; - Client.prototype.debug = function(message) { - var _ref; - return typeof window !== "undefined" && window !== null ? (_ref = window.console) != null ? _ref.log(message) : void 0 : void 0; + function Client(_at_ws) { + this.ws = _at_ws; + this.ws.binaryType = "arraybuffer"; + this.counter = 0; + this.connected = false; + this.heartbeat = { + outgoing: 10000, + incoming: 10000 }; + this.maxWebSocketFrameSize = 16 * 1024; + this.subscriptions = {}; + this.partialData = ''; + } - now = function() { - if (Date.now) { - return Date.now(); - } else { - return new Date().valueOf; - } - }; + Client.prototype.debug = function(message) { + var _ref; + return typeof window !== "undefined" && window !== null ? (_ref = window.console) != null ? _ref.log(message) : void 0 : void 0; + }; - Client.prototype._transmit = function(command, headers, body) { - var out; - out = Frame.marshall(command, headers, body); - if (typeof this.debug === "function") { - this.debug(">>> " + out); - } - while (true) { - if (out.length > this.maxWebSocketFrameSize) { - this.ws.send(out.substring(0, this.maxWebSocketFrameSize)); - out = out.substring(this.maxWebSocketFrameSize); - if (typeof this.debug === "function") { - this.debug("remaining = " + out.length); - } - } else { - return this.ws.send(out); - } - } - }; + now = function() { + if (Date.now) { + return Date.now(); + } else { + return new Date().valueOf; + } + }; - Client.prototype._setupHeartbeat = function(headers) { - var serverIncoming, serverOutgoing, ttl, v, _ref, _ref1; - if ((_ref = headers.version) !== Stomp.VERSIONS.V1_1 && _ref !== Stomp.VERSIONS.V1_2) { - return; - } - _ref1 = (function() { - var _i, _len, _ref1, _results; - _ref1 = headers['heart-beat'].split(","); - _results = []; - for (_i = 0, _len = _ref1.length; _i < _len; _i++) { - v = _ref1[_i]; - _results.push(parseInt(v)); - } - return _results; - })(), serverOutgoing = _ref1[0], serverIncoming = _ref1[1]; - if (!(this.heartbeat.outgoing === 0 || serverIncoming === 0)) { - ttl = Math.max(this.heartbeat.outgoing, serverIncoming); - if (typeof this.debug === "function") { - this.debug("send PING every " + ttl + "ms"); - } - this.pinger = Stomp.setInterval(ttl, (function(_this) { - return function() { - _this.ws.send(Byte.LF); - return typeof _this.debug === "function" ? _this.debug(">>> PING") : void 0; - }; - })(this)); - } - if (!(this.heartbeat.incoming === 0 || serverOutgoing === 0)) { - ttl = Math.max(this.heartbeat.incoming, serverOutgoing); + Client.prototype._transmit = function(command, headers, body) { + var out; + out = Frame.marshall(command, headers, body); + if (typeof this.debug === "function") { + this.debug(">>> " + out); + } + while (true) { + if (out.length > this.maxWebSocketFrameSize) { + this.ws.send(out.substring(0, this.maxWebSocketFrameSize)); + out = out.substring(this.maxWebSocketFrameSize); if (typeof this.debug === "function") { - this.debug("check PONG every " + ttl + "ms"); + this.debug("remaining = " + out.length); } - return this.ponger = Stomp.setInterval(ttl, (function(_this) { - return function() { - var delta; - delta = now() - _this.serverActivity; - if (delta > ttl * 2) { - if (typeof _this.debug === "function") { - _this.debug("did not receive server activity for the last " + delta + "ms"); - } - return _this.ws.close(); - } - }; - })(this)); + } else { + return this.ws.send(out); } - }; + } + }; - Client.prototype._parseConnect = function() { - var args, connectCallback, errorCallback, headers; - args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; - headers = {}; - switch (args.length) { - case 2: - headers = args[0], connectCallback = args[1]; - break; - case 3: - if (args[1] instanceof Function) { - headers = args[0], connectCallback = args[1], errorCallback = args[2]; - } else { - headers.login = args[0], headers.passcode = args[1], connectCallback = args[2]; - } - break; - case 4: - headers.login = args[0], headers.passcode = args[1], connectCallback = args[2], errorCallback = args[3]; - break; - default: - headers.login = args[0], headers.passcode = args[1], connectCallback = args[2], errorCallback = args[3], headers.host = args[4]; + Client.prototype._setupHeartbeat = function(headers) { + var serverIncoming, serverOutgoing, ttl, v, _ref, _ref1; + if ((_ref = headers.version) !== Stomp.VERSIONS.V1_1 && _ref !== Stomp.VERSIONS.V1_2) { + return; + } + _ref1 = (function() { + var _i, _len, _ref1, _results; + _ref1 = headers['heart-beat'].split(","); + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + v = _ref1[_i]; + _results.push(parseInt(v)); } - return [headers, connectCallback, errorCallback]; - }; - - Client.prototype.connect = function() { - var args, errorCallback, headers, out; - args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; - out = this._parseConnect.apply(this, args); - headers = out[0], this.connectCallback = out[1], errorCallback = out[2]; + return _results; + })(), serverOutgoing = _ref1[0], serverIncoming = _ref1[1]; + if (!(this.heartbeat.outgoing === 0 || serverIncoming === 0)) { + ttl = Math.max(this.heartbeat.outgoing, serverIncoming); if (typeof this.debug === "function") { - this.debug("Opening Web Socket..."); + this.debug("send PING every " + ttl + "ms"); } - this.ws.onmessage = (function(_this) { - return function(evt) { - var arr, c, client, data, frame, messageID, onreceive, subscription, unmarshalledData, _i, _len, _ref, _results; - data = typeof ArrayBuffer !== 'undefined' && evt.data instanceof ArrayBuffer ? (arr = new Uint8Array(evt.data), typeof _this.debug === "function" ? _this.debug("--- got data length: " + arr.length) : void 0, ((function() { - var _i, _len, _results; - _results = []; - for (_i = 0, _len = arr.length; _i < _len; _i++) { - c = arr[_i]; - _results.push(String.fromCharCode(c)); - } - return _results; - })()).join('')) : evt.data; - _this.serverActivity = now(); - if (data === Byte.LF) { + this.pinger = Stomp.setInterval(ttl, (function(_this) { + return function() { + _this.ws.send(Byte.LF); + return typeof _this.debug === "function" ? _this.debug(">>> PING") : void 0; + }; + })(this)); + } + if (!(this.heartbeat.incoming === 0 || serverOutgoing === 0)) { + ttl = Math.max(this.heartbeat.incoming, serverOutgoing); + if (typeof this.debug === "function") { + this.debug("check PONG every " + ttl + "ms"); + } + return this.ponger = Stomp.setInterval(ttl, (function(_this) { + return function() { + var delta; + delta = now() - _this.serverActivity; + if (delta > ttl * 2) { if (typeof _this.debug === "function") { - _this.debug("<<< PONG"); + _this.debug("did not receive server activity for the last " + delta + "ms"); } - return; + return _this.ws.close(); } - if (typeof _this.debug === "function") { - _this.debug("<<< " + data); - } - unmarshalledData = Frame.unmarshall(_this.partialData + data); - _this.partialData = unmarshalledData.partial; - _ref = unmarshalledData.frames; + }; + })(this)); + } + }; + + Client.prototype._parseConnect = function() { + var args, connectCallback, errorCallback, headers; + args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; + headers = {}; + switch (args.length) { + case 2: + headers = args[0], connectCallback = args[1]; + break; + case 3: + if (args[1] instanceof Function) { + headers = args[0], connectCallback = args[1], errorCallback = args[2]; + } else { + headers.login = args[0], headers.passcode = args[1], connectCallback = args[2]; + } + break; + case 4: + headers.login = args[0], headers.passcode = args[1], connectCallback = args[2], errorCallback = args[3]; + break; + default: + headers.login = args[0], headers.passcode = args[1], connectCallback = args[2], errorCallback = args[3], headers.host = args[4]; + } + return [headers, connectCallback, errorCallback]; + }; + + Client.prototype.connect = function() { + var args, errorCallback, headers, out; + args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; + out = this._parseConnect.apply(this, args); + headers = out[0], this.connectCallback = out[1], errorCallback = out[2]; + if (typeof this.debug === "function") { + this.debug("Opening Web Socket..."); + } + this.ws.onmessage = (function(_this) { + return function(evt) { + var arr, c, client, data, frame, messageID, onreceive, subscription, unmarshalledData, _i, _len, _ref, _results; + data = typeof ArrayBuffer !== 'undefined' && evt.data instanceof ArrayBuffer ? (arr = new Uint8Array(evt.data), typeof _this.debug === "function" ? _this.debug("--- got data length: " + arr.length) : void 0, ((function() { + var _i, _len, _results; _results = []; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - frame = _ref[_i]; - switch (frame.command) { - case "CONNECTED": - if (typeof _this.debug === "function") { - _this.debug("connected to server " + frame.headers.server); - } - _this.connected = true; - _this._setupHeartbeat(frame.headers); - _results.push(typeof _this.connectCallback === "function" ? _this.connectCallback(frame) : void 0); - break; - case "MESSAGE": - subscription = frame.headers.subscription; - onreceive = _this.subscriptions[subscription] || _this.onreceive; - if (onreceive) { - client = _this; - messageID = frame.headers["message-id"]; - frame.ack = function(headers) { - if (headers == null) { - headers = {}; - } - return client.ack(messageID, subscription, headers); - }; - frame.nack = function(headers) { - if (headers == null) { - headers = {}; - } - return client.nack(messageID, subscription, headers); - }; - _results.push(onreceive(frame)); - } else { - _results.push(typeof _this.debug === "function" ? _this.debug("Unhandled received MESSAGE: " + frame) : void 0); - } - break; - case "RECEIPT": - _results.push(typeof _this.onreceipt === "function" ? _this.onreceipt(frame) : void 0); - break; - case "ERROR": - _results.push(typeof errorCallback === "function" ? errorCallback(frame) : void 0); - break; - default: - _results.push(typeof _this.debug === "function" ? _this.debug("Unhandled frame: " + frame) : void 0); - } + for (_i = 0, _len = arr.length; _i < _len; _i++) { + c = arr[_i]; + _results.push(String.fromCharCode(c)); } return _results; - }; - })(this); - this.ws.onclose = (function(_this) { - return function() { - var msg; - msg = "Whoops! Lost connection to " + _this.ws.url; + })()).join('')) : evt.data; + _this.serverActivity = now(); + if (data === Byte.LF) { if (typeof _this.debug === "function") { - _this.debug(msg); + _this.debug("<<< PONG"); } - _this._cleanUp(); - return typeof errorCallback === "function" ? errorCallback(msg) : void 0; - }; - })(this); - return this.ws.onopen = (function(_this) { - return function() { - if (typeof _this.debug === "function") { - _this.debug('Web Socket Opened...'); + return; + } + if (typeof _this.debug === "function") { + _this.debug("<<< " + data); + } + unmarshalledData = Frame.unmarshall(_this.partialData + data); + _this.partialData = unmarshalledData.partial; + _ref = unmarshalledData.frames; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + frame = _ref[_i]; + switch (frame.command) { + case "CONNECTED": + if (typeof _this.debug === "function") { + _this.debug("connected to server " + frame.headers.server); + } + _this.connected = true; + _this._setupHeartbeat(frame.headers); + _results.push(typeof _this.connectCallback === "function" ? _this.connectCallback(frame) : void 0); + break; + case "MESSAGE": + subscription = frame.headers.subscription; + onreceive = _this.subscriptions[subscription] || _this.onreceive; + if (onreceive) { + client = _this; + messageID = frame.headers["message-id"]; + frame.ack = function(headers) { + if (headers == null) { + headers = {}; + } + return client.ack(messageID, subscription, headers); + }; + frame.nack = function(headers) { + if (headers == null) { + headers = {}; + } + return client.nack(messageID, subscription, headers); + }; + _results.push(onreceive(frame)); + } else { + _results.push(typeof _this.debug === "function" ? _this.debug("Unhandled received MESSAGE: " + frame) : void 0); + } + break; + case "RECEIPT": + _results.push(typeof _this.onreceipt === "function" ? _this.onreceipt(frame) : void 0); + break; + case "ERROR": + _results.push(typeof errorCallback === "function" ? errorCallback(frame) : void 0); + break; + default: + _results.push(typeof _this.debug === "function" ? _this.debug("Unhandled frame: " + frame) : void 0); } - headers["accept-version"] = Stomp.VERSIONS.supportedVersions(); - headers["heart-beat"] = [_this.heartbeat.outgoing, _this.heartbeat.incoming].join(','); - return _this._transmit("CONNECT", headers); - }; - })(this); - }; + } + return _results; + }; + })(this); + this.ws.onclose = (function(_this) { + return function() { + var msg; + msg = "Whoops! Lost connection to " + _this.ws.url; + if (typeof _this.debug === "function") { + _this.debug(msg); + } + _this._cleanUp(); + return typeof errorCallback === "function" ? errorCallback(msg) : void 0; + }; + })(this); + return this.ws.onopen = (function(_this) { + return function() { + if (typeof _this.debug === "function") { + _this.debug('Web Socket Opened...'); + } + headers["accept-version"] = Stomp.VERSIONS.supportedVersions(); + headers["heart-beat"] = [_this.heartbeat.outgoing, _this.heartbeat.incoming].join(','); + return _this._transmit("CONNECT", headers); + }; + })(this); + }; - Client.prototype.disconnect = function(disconnectCallback, headers) { - if (headers == null) { - headers = {}; - } - this._transmit("DISCONNECT", headers); - this.ws.onclose = null; - this.ws.close(); - this._cleanUp(); - return typeof disconnectCallback === "function" ? disconnectCallback() : void 0; - }; + Client.prototype.disconnect = function(disconnectCallback, headers) { + if (headers == null) { + headers = {}; + } + this._transmit("DISCONNECT", headers); + this.ws.onclose = null; + this.ws.close(); + this._cleanUp(); + return typeof disconnectCallback === "function" ? disconnectCallback() : void 0; + }; - Client.prototype._cleanUp = function() { - this.connected = false; - if (this.pinger) { - Stomp.clearInterval(this.pinger); - } - if (this.ponger) { - return Stomp.clearInterval(this.ponger); - } - }; + Client.prototype._cleanUp = function() { + this.connected = false; + if (this.pinger) { + Stomp.clearInterval(this.pinger); + } + if (this.ponger) { + return Stomp.clearInterval(this.ponger); + } + }; - Client.prototype.send = function(destination, headers, body) { - if (headers == null) { - headers = {}; - } - if (body == null) { - body = ''; - } - headers.destination = destination; - return this._transmit("SEND", headers, body); - }; + Client.prototype.send = function(destination, headers, body) { + if (headers == null) { + headers = {}; + } + if (body == null) { + body = ''; + } + headers.destination = destination; + return this._transmit("SEND", headers, body); + }; - Client.prototype.subscribe = function(destination, callback, headers) { - var client; - if (headers == null) { - headers = {}; - } - if (!headers.id) { - headers.id = "sub-" + this.counter++; + Client.prototype.subscribe = function(destination, callback, headers) { + var client; + if (headers == null) { + headers = {}; + } + if (!headers.id) { + headers.id = "sub-" + this.counter++; + } + headers.destination = destination; + this.subscriptions[headers.id] = callback; + this._transmit("SUBSCRIBE", headers); + client = this; + return { + id: headers.id, + unsubscribe: function() { + return client.unsubscribe(headers.id); } - headers.destination = destination; - this.subscriptions[headers.id] = callback; - this._transmit("SUBSCRIBE", headers); - client = this; - return { - id: headers.id, - unsubscribe: function() { - return client.unsubscribe(headers.id); - } - }; }; + }; - Client.prototype.unsubscribe = function(id) { - delete this.subscriptions[id]; - return this._transmit("UNSUBSCRIBE", { - id: id - }); - }; + Client.prototype.unsubscribe = function(id) { + delete this.subscriptions[id]; + return this._transmit("UNSUBSCRIBE", { + id: id + }); + }; - Client.prototype.begin = function(transaction) { - var client, txid; - txid = transaction || "tx-" + this.counter++; - this._transmit("BEGIN", { - transaction: txid - }); - client = this; - return { - id: txid, - commit: function() { - return client.commit(txid); - }, - abort: function() { - return client.abort(txid); - } - }; + Client.prototype.begin = function(transaction) { + var client, txid; + txid = transaction || "tx-" + this.counter++; + this._transmit("BEGIN", { + transaction: txid + }); + client = this; + return { + id: txid, + commit: function() { + return client.commit(txid); + }, + abort: function() { + return client.abort(txid); + } }; + }; - Client.prototype.commit = function(transaction) { - return this._transmit("COMMIT", { - transaction: transaction - }); - }; + Client.prototype.commit = function(transaction) { + return this._transmit("COMMIT", { + transaction: transaction + }); + }; - Client.prototype.abort = function(transaction) { - return this._transmit("ABORT", { - transaction: transaction - }); - }; + Client.prototype.abort = function(transaction) { + return this._transmit("ABORT", { + transaction: transaction + }); + }; - Client.prototype.ack = function(messageID, subscription, headers) { - if (headers == null) { - headers = {}; - } - headers["message-id"] = messageID; - headers.subscription = subscription; - return this._transmit("ACK", headers); - }; + Client.prototype.ack = function(messageID, subscription, headers) { + if (headers == null) { + headers = {}; + } + headers["message-id"] = messageID; + headers.subscription = subscription; + return this._transmit("ACK", headers); + }; - Client.prototype.nack = function(messageID, subscription, headers) { - if (headers == null) { - headers = {}; - } - headers["message-id"] = messageID; - headers.subscription = subscription; - return this._transmit("NACK", headers); - }; + Client.prototype.nack = function(messageID, subscription, headers) { + if (headers == null) { + headers = {}; + } + headers["message-id"] = messageID; + headers.subscription = subscription; + return this._transmit("NACK", headers); + }; - return Client; + return Client; - })(); +})(); - Stomp = { - VERSIONS: { - V1_0: '1.0', - V1_1: '1.1', - V1_2: '1.2', - supportedVersions: function() { - return '1.1,1.0'; - } - }, - client: function(url, protocols) { - var klass, ws; - if (protocols == null) { - protocols = ['v10.stomp', 'v11.stomp']; - } - klass = Stomp.WebSocketClass || WebSocket; - ws = new klass(url, protocols); - return new Client(ws); - }, - over: function(ws) { - return new Client(ws); - }, - Frame: Frame +Stomp = { + VERSIONS: { + V1_0: '1.0', + V1_1: '1.1', + V1_2: '1.2', + supportedVersions: function() { + return '1.1,1.0'; + } + }, + client: function(url, protocols) { + var klass, ws; + if (protocols == null) { + protocols = ['v10.stomp', 'v11.stomp']; + } + klass = Stomp.WebSocketClass || WebSocket; + ws = new klass(url, protocols); + return new Client(ws); + }, + over: function(ws) { + return new Client(ws); + }, + Frame: Frame +}; + +if (typeof window !== "undefined" && window !== null) { + Stomp.setInterval = function(interval, f) { + return window.setInterval(f, interval); }; + Stomp.clearInterval = function(id) { + return window.clearInterval(id); + }; +} - if (typeof exports !== "undefined" && exports !== null) { - exports.Stomp = Stomp; - } +return Stomp; - if (typeof window !== "undefined" && window !== null) { - Stomp.setInterval = function(interval, f) { - return window.setInterval(f, interval); - }; - Stomp.clearInterval = function(id) { - return window.clearInterval(id); - }; - window.Stomp = Stomp; - } else if (!exports) { - self.Stomp = Stomp; - } -}).call(this); +})); diff --git a/lib/stomp.min.js b/lib/stomp.min.js index dd0b1fa..fa65b5f 100644 --- a/lib/stomp.min.js +++ b/lib/stomp.min.js @@ -1,8 +1,12 @@ -// Generated by CoffeeScript 1.7.1 +(function(t,e){if(typeof define==="function"&&define.amd){// AMD. Register as an anonymous module unless amdModuleId is set +define([],function(){return e()})}else if(typeof exports==="object"){// Node. Does not work with strict CommonJS, but +// only CommonJS-like environments that support module.exports, +// like Node. +module.exports=e()}else{t["Stomp"]=e()}})(this,function(){// Generated by CoffeeScript 1.9.0 /* Stomp Over WebSocket http://www.jmesnil.net/stomp-websocket/doc/ | Apache License V2.0 Copyright (C) 2010-2013 [Jeff Mesnil](http://jmesnil.net/) Copyright (C) 2012 [FuseSource, Inc.](http://fusesource.com) */ -(function(){var t,e,n,i,r={}.hasOwnProperty,o=[].slice;t={LF:"\n",NULL:"\x00"};n=function(){var e;function n(t,e,n){this.command=t;this.headers=e!=null?e:{};this.body=n!=null?n:""}n.prototype.toString=function(){var e,i,o,s,u;e=[this.command];o=this.headers["content-length"]===false?true:false;if(o){delete this.headers["content-length"]}u=this.headers;for(i in u){if(!r.call(u,i))continue;s=u[i];e.push(""+i+":"+s)}if(this.body&&!o){e.push("content-length:"+n.sizeOfUTF8(this.body))}e.push(t.LF+this.body);return e.join(t.LF)};n.sizeOfUTF8=function(t){if(t){return encodeURI(t).match(/%..|./g).length}else{return 0}};e=function(e){var i,r,o,s,u,a,c,f,h,l,p,d,g,b,m,v,y;s=e.search(RegExp(""+t.LF+t.LF));u=e.substring(0,s).split(t.LF);o=u.shift();a={};d=function(t){return t.replace(/^\s+|\s+$/g,"")};v=u.reverse();for(g=0,m=v.length;gy;c=p<=y?++b:--b){r=e.charAt(c);if(r===t.NULL){break}i+=r}}return new n(o,a,i)};n.unmarshall=function(n){var i,r,o,s;r=n.split(RegExp(""+t.NULL+t.LF+"*"));s={frames:[],partial:""};s.frames=function(){var t,n,o,s;o=r.slice(0,-1);s=[];for(t=0,n=o.length;t>> "+r)}while(true){if(r.length>this.maxWebSocketFrameSize){this.ws.send(r.substring(0,this.maxWebSocketFrameSize));r=r.substring(this.maxWebSocketFrameSize);if(typeof this.debug==="function"){this.debug("remaining = "+r.length)}}else{return this.ws.send(r)}}};r.prototype._setupHeartbeat=function(n){var r,o,s,u,a,c;if((a=n.version)!==i.VERSIONS.V1_1&&a!==i.VERSIONS.V1_2){return}c=function(){var t,e,i,r;i=n["heart-beat"].split(",");r=[];for(t=0,e=i.length;t>> PING"):void 0}}(this))}if(!(this.heartbeat.incoming===0||o===0)){s=Math.max(this.heartbeat.incoming,o);if(typeof this.debug==="function"){this.debug("check PONG every "+s+"ms")}return this.ponger=i.setInterval(s,function(t){return function(){var n;n=e()-t.serverActivity;if(n>s*2){if(typeof t.debug==="function"){t.debug("did not receive server activity for the last "+n+"ms")}return t.ws.close()}}}(this))}};r.prototype._parseConnect=function(){var t,e,n,i;t=1<=arguments.length?o.call(arguments,0):[];i={};switch(t.length){case 2:i=t[0],e=t[1];break;case 3:if(t[1]instanceof Function){i=t[0],e=t[1],n=t[2]}else{i.login=t[0],i.passcode=t[1],e=t[2]}break;case 4:i.login=t[0],i.passcode=t[1],e=t[2],n=t[3];break;default:i.login=t[0],i.passcode=t[1],e=t[2],n=t[3],i.host=t[4]}return[i,e,n]};r.prototype.connect=function(){var r,s,u,a;r=1<=arguments.length?o.call(arguments,0):[];a=this._parseConnect.apply(this,r);u=a[0],this.connectCallback=a[1],s=a[2];if(typeof this.debug==="function"){this.debug("Opening Web Socket...")}this.ws.onmessage=function(i){return function(r){var o,u,a,c,f,h,l,p,d,g,b,m,v;c=typeof ArrayBuffer!=="undefined"&&r.data instanceof ArrayBuffer?(o=new Uint8Array(r.data),typeof i.debug==="function"?i.debug("--- got data length: "+o.length):void 0,function(){var t,e,n;n=[];for(t=0,e=o.length;ty;c=p<=y?++b:--b){r=e.charAt(c);if(r===t.NULL){break}i+=r}}return new n(o,a,i)};n.unmarshall=function(n){var i,r,o,s;r=n.split(RegExp(""+t.NULL+t.LF+"*"));s={frames:[],partial:""};s.frames=function(){var t,n,o,s;o=r.slice(0,-1);s=[];for(t=0,n=o.length;t>> "+r)}while(true){if(r.length>this.maxWebSocketFrameSize){this.ws.send(r.substring(0,this.maxWebSocketFrameSize));r=r.substring(this.maxWebSocketFrameSize);if(typeof this.debug==="function"){this.debug("remaining = "+r.length)}}else{return this.ws.send(r)}}};r.prototype._setupHeartbeat=function(n){var r,o,s,u,a,c;if((a=n.version)!==i.VERSIONS.V1_1&&a!==i.VERSIONS.V1_2){return}c=function(){var t,e,i,r;i=n["heart-beat"].split(",");r=[];for(t=0,e=i.length;t>> PING"):void 0}}(this))}if(!(this.heartbeat.incoming===0||o===0)){s=Math.max(this.heartbeat.incoming,o);if(typeof this.debug==="function"){this.debug("check PONG every "+s+"ms")}return this.ponger=i.setInterval(s,function(t){return function(){var n;n=e()-t.serverActivity;if(n>s*2){if(typeof t.debug==="function"){t.debug("did not receive server activity for the last "+n+"ms")}return t.ws.close()}}}(this))}};r.prototype._parseConnect=function(){var t,e,n,i;t=1<=arguments.length?o.call(arguments,0):[];i={};switch(t.length){case 2:i=t[0],e=t[1];break;case 3:if(t[1]instanceof Function){i=t[0],e=t[1],n=t[2]}else{i.login=t[0],i.passcode=t[1],e=t[2]}break;case 4:i.login=t[0],i.passcode=t[1],e=t[2],n=t[3];break;default:i.login=t[0],i.passcode=t[1],e=t[2],n=t[3],i.host=t[4]}return[i,e,n]};r.prototype.connect=function(){var r,s,u,a;r=1<=arguments.length?o.call(arguments,0):[];a=this._parseConnect.apply(this,r);u=a[0],this.connectCallback=a[1],s=a[2];if(typeof this.debug==="function"){this.debug("Opening Web Socket...")}this.ws.onmessage=function(i){return function(r){var o,u,a,c,f,h,l,p,d,g,b,m,v;c=typeof ArrayBuffer!=="undefined"&&r.data instanceof ArrayBuffer?(o=new Uint8Array(r.data),typeof i.debug==="function"?i.debug("--- got data length: "+o.length):void 0,function(){var t,e,n;n=[];for(t=0,e=o.length;t +Stomp.setInterval = (interval, f) -> setInterval f, interval -Stomp.Stomp.clearInterval = (id) -> +Stomp.clearInterval = (id) -> clearInterval id # wrap a TCP socket (provided by node.js's net module) in a "Web Socket"-like @@ -80,13 +80,13 @@ wrapWS = (url) -> # TCP socket overTCP = (host, port) -> socket = wrapTCP port, host - Stomp.Stomp.over socket + Stomp.over socket # This method can be used by node.js app to connect to a STOMP broker over a # Web socket overWS = (url) -> socket = wrapWS url - Stomp.Stomp.over socket + Stomp.over socket exports.overTCP = overTCP exports.overWS = overWS diff --git a/src/stomp.coffee b/src/stomp.coffee index 69788a9..cc84832 100644 --- a/src/stomp.coffee +++ b/src/stomp.coffee @@ -163,11 +163,11 @@ class Client # }; debug: (message) -> window?.console?.log message - + # Utility method to get the current timestamp (Date.now is not defined in IE8) now= -> if Date.now then Date.now() else new Date().valueOf - + # Base method to transmit any stomp frame _transmit: (command, headers, body) -> out = Frame.marshall(command, headers, body) @@ -398,7 +398,7 @@ class Client abort: -> client.abort txid } - + # [COMMIT Frame](http://stomp.github.com/stomp-specification-1.1.html#COMMIT) # # * `transaction` is MANDATORY. @@ -413,7 +413,7 @@ class Client @_transmit "COMMIT", { transaction: transaction } - + # [ABORT Frame](http://stomp.github.com/stomp-specification-1.1.html#ABORT) # # * `transaction` is MANDATORY. @@ -428,7 +428,7 @@ class Client @_transmit "ABORT", { transaction: transaction } - + # [ACK Frame](http://stomp.github.com/stomp-specification-1.1.html#ACK) # # * `messageID` & `subscription` are MANDATORY. @@ -510,18 +510,12 @@ Stomp = # # `Stomp` object exportation -# export as CommonJS module -if exports? - exports.Stomp = Stomp - -# export in the Web Browser +# in the Web browser, rely on `window.setInterval` to handle heart-beats if window? - # in the Web browser, rely on `window.setInterval` to handle heart-beats Stomp.setInterval= (interval, f) -> window.setInterval f, interval Stomp.clearInterval= (id) -> window.clearInterval id - window.Stomp = Stomp -# or in the current object (e.g. a WebWorker) -else if !exports - self.Stomp = Stomp + +# return the Stomp object +return Stomp diff --git a/test/server.mock.coffee b/test/server.mock.coffee index 73cc14c..8aa9e69 100644 --- a/test/server.mock.coffee +++ b/test/server.mock.coffee @@ -1,40 +1,40 @@ WebSocketMock = require('./websocket.mock.js').WebSocketMock -Stomp = require('../../lib/stomp.js').Stomp +Stomp = require('../../lib/stomp.js') console = require 'console' class StompServerMock extends WebSocketMock # WebSocketMock handlers - + handle_send: (msg) => @stomp_dispatch(Stomp.Frame.unmarshall(msg).frames[0]) - + handle_close: => @_shutdown() - + handle_open: => @stomp_init() @_accept() - + # Stomp server implementation - + stomp_init: -> @transactions = {} @subscriptions = {} @messages = [] - + stomp_send: (command, headers, body=null) -> @_respond(Stomp.Frame.marshall(command, headers, body)) - + stomp_send_receipt: (frame) -> if frame.headers.message? @stomp_send("ERROR", {'receipt-id': frame.headers['receipt-id'], 'message': frame.headers.message}) else @stomp_send("RECEIPT", {'receipt-id': frame.headers['receipt-id']}) - + stomp_send_message: (destination, subscription, message_id, body) -> @stomp_send("MESSAGE", { - 'destination': destination, + 'destination': destination, 'message-id': message_id, 'subscription': subscription}, body) @@ -50,10 +50,10 @@ class StompServerMock extends WebSocketMock stomp_handle_connect: (frame) -> @session_id = Math.random() @stomp_send("CONNECTED", {'session': @session_id}) - + stomp_handle_begin: (frame) -> @transactions[frame.headers.transaction] = [] - + stomp_handle_commit: (frame) -> transaction = @transactions[frame.headers.transaction] for frame in transaction @@ -79,15 +79,15 @@ class StompServerMock extends WebSocketMock delete @subscriptions[frame.headers.id] else frame.headers.message = "Subscription does not exist" - + stomp_handle_disconnect: (frame) -> @_shutdown() - + # Test helpers - + test_send: (sub_id, message) -> msgid = 'msg-' + Math.random() @subscriptions[sub_id][1](msgid, message) - -exports.StompServerMock = StompServerMock \ No newline at end of file + +exports.StompServerMock = StompServerMock diff --git a/test/stomp.spec.coffee b/test/stomp.spec.coffee index b826ca7..4de74a3 100644 --- a/test/stomp.spec.coffee +++ b/test/stomp.spec.coffee @@ -1,4 +1,4 @@ -Stomp = require('../../lib/stomp.js').Stomp +Stomp = require('../../lib/stomp.js') StompServerMock = require('./server.mock.js').StompServerMock Stomp.WebSocketClass = StompServerMock @@ -13,7 +13,7 @@ describe "Stomp", -> ) waitsFor -> connected runs -> expect(client.connected).toBe(true) - + it "lets you connect to a server and get a callback", -> client = Stomp.client("ws://mocked/stomp/server") connected = false @@ -22,7 +22,7 @@ describe "Stomp", -> ) waitsFor -> connected runs -> expect(client.connected).toBe(true) - + it "lets you subscribe to a destination", -> client = Stomp.client("ws://mocked/stomp/server") subscription = null @@ -31,7 +31,7 @@ describe "Stomp", -> ) waitsFor -> subscription runs -> expect(Object.keys(client.ws.subscriptions)).toContain(subscription.id) - + it "lets you publish a message to a destination", -> client = Stomp.client("ws://mocked/stomp/server") message = null @@ -42,7 +42,7 @@ describe "Stomp", -> waitsFor -> message runs -> expect(client.ws.messages.pop().toString()).toContain(message) - + it "lets you unsubscribe from a destination", -> client = Stomp.client("ws://mocked/stomp/server") unsubscribed = false @@ -54,7 +54,7 @@ describe "Stomp", -> ) waitsFor -> unsubscribed runs -> expect(Object.keys(client.ws.subscriptions)).not.toContain(subscription.id) - + it "lets you receive messages only while subscribed", -> client = Stomp.client("ws://mocked/stomp/server") subscription = null @@ -71,11 +71,11 @@ describe "Stomp", -> expect(messages.length).toEqual(2) subscription.unsubscribe() try - client.ws.test_send(id, Math.random()) + client.ws.test_send(id, Math.random()) catch err null expect(messages.length).toEqual(2) - + it "lets you send messages in a transaction", -> client = Stomp.client("ws://mocked/stomp/server") connected = false @@ -92,7 +92,7 @@ describe "Stomp", -> client.send("/queue/test", {transaction: txid}, "messages 3") client.commit(txid) expect(client.ws.messages.length).toEqual(3) - + it "lets you abort a transaction", -> client = Stomp.client("ws://mocked/stomp/server") connected = false