From 36059731d6695cfed3a145031a1eb52fd34c456b Mon Sep 17 00:00:00 2001 From: williamsbdev Date: Fri, 21 Nov 2014 20:46:31 -0600 Subject: [PATCH 01/10] Refactor: consistent spacing and removing trailing spaces --- Cakefile | 56 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/Cakefile b/Cakefile index e74b799..13c584a 100644 --- a/Cakefile +++ b/Cakefile @@ -5,26 +5,26 @@ util = require 'util' binDir = "./node_modules/.bin/" task 'watch', 'Watch for changes in coffee files to build and test', -> - util.log "Watching for changes in src and test" - lastTest = 0 - watchDir 'src', -> - invoke 'build:src' - invoke 'build:min' - invoke 'build:doc' - invoke 'build:test' - watchDir 'test', -> - invoke 'build:test' - watchDir 'dist/test', (file)-> - # 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 - lastTest = time - invoke 'test' + util.log "Watching for changes in src and test" + lastTest = 0 + watchDir 'src', -> + invoke 'build:src' + invoke 'build:min' + invoke 'build:doc' + invoke 'build:test' + watchDir 'test', -> + invoke 'build:test' + watchDir 'dist/test', (file)-> + # 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 + lastTest = time + invoke '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,7 +38,7 @@ 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) -> + exec binDir + "coffee -o lib/ -c src/", (err, stdout, stderr) -> handleError(err) if err task 'build:min', 'Build the minified files into lib', -> @@ -48,21 +48,21 @@ 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) -> fs.readdir dir, (err, files) -> - handleError(err) if err - for file in files then do (file) -> - fs.watchFile "#{dir}/#{file}", (curr, prev) -> - if +curr.mtime isnt +prev.mtime - callback "#{dir}/#{file}" + handleError(err) if err + for file in files then do (file) -> + fs.watchFile "#{dir}/#{file}", (curr, prev) -> + if +curr.mtime isnt +prev.mtime + callback "#{dir}/#{file}" parseTestResults = (data) -> lines = (line for line in data.split('\n') when line.length > 5) @@ -73,14 +73,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 From 19a3e9332ca13af732a0395e5aa0da934b8b2128 Mon Sep 17 00:00:00 2001 From: williamsbdev Date: Fri, 21 Nov 2014 20:48:46 -0600 Subject: [PATCH 02/10] Feature: Adding ability to create AMD module --- Cakefile | 9 + lib/stomp-websocket-amd.js | 508 +++++++++++++++++++++++++++++++++++++ src/amd/amd_head.js | 3 + src/amd/amd_tail.js | 1 + 4 files changed, 521 insertions(+) create mode 100644 lib/stomp-websocket-amd.js create mode 100644 src/amd/amd_head.js create mode 100644 src/amd/amd_tail.js diff --git a/Cakefile b/Cakefile index 13c584a..e4b95cc 100644 --- a/Cakefile +++ b/Cakefile @@ -34,6 +34,7 @@ task 'test', 'Run the tests', -> task 'build', 'Build source and tests', -> invoke 'build:src' invoke 'build:min' + invoke 'build:amd' invoke 'build:test' task 'build:src', 'Build the src files into lib', -> @@ -41,6 +42,14 @@ task 'build:src', 'Build the src files into lib', -> exec binDir + "coffee -o lib/ -c src/", (err, stdout, stderr) -> handleError(err) if err +task 'build:amd', 'Build the src files into lib as an AMD module', -> + sources = ["src/amd/amd_head.js", "lib/stomp.js", "src/amd/amd_tail.js"] + util.log "Compiling amd..." + output = fs.openSync("lib/stomp-websocket-amd.js", "w") + for source in sources + fs.writeSync(output, fs.readFileSync(source) + "\n") + fs.closeSync(output) + 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) -> diff --git a/lib/stomp-websocket-amd.js b/lib/stomp-websocket-amd.js new file mode 100644 index 0000000..22ac0f0 --- /dev/null +++ b/lib/stomp-websocket-amd.js @@ -0,0 +1,508 @@ +define("js/stomp-websocket", + function() { + "use strict"; + +// Generated by CoffeeScript 1.8.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 Byte, Client, Frame, Stomp, + __hasProp = {}.hasOwnProperty, + __slice = [].slice; + + Byte = { + LF: '\x0A', + NULL: '\x00' + }; + + Frame = (function() { + var unmarshallSingle; + + function Frame(command, headers, body) { + this.command = command; + this.headers = headers != null ? headers : {}; + this.body = body != null ? body : ''; + } + + 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); + }; + + 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; + }; + + 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.prototype.debug = function(message) { + var _ref; + return typeof window !== "undefined" && window !== null ? (_ref = window.console) != null ? _ref.log(message) : void 0 : void 0; + }; + + now = function() { + if (Date.now) { + return Date.now(); + } else { + return new Date().valueOf; + } + }; + + 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); + } + } + }; + + 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); + 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("did not receive server activity for the last " + delta + "ms"); + } + return _this.ws.close(); + } + }; + })(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 = 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) { + if (typeof _this.debug === "function") { + _this.debug("<<< PONG"); + } + 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); + } + } + 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._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.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); + } + }; + }; + + 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.commit = function(transaction) { + return this._transmit("COMMIT", { + 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.nack = function(messageID, subscription, headers) { + if (headers == null) { + headers = {}; + } + headers["message-id"] = messageID; + headers.subscription = subscription; + return this._transmit("NACK", headers); + }; + + 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 + }; + + if (typeof exports !== "undefined" && exports !== null) { + exports.Stomp = 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/src/amd/amd_head.js b/src/amd/amd_head.js new file mode 100644 index 0000000..9d6c41a --- /dev/null +++ b/src/amd/amd_head.js @@ -0,0 +1,3 @@ +define("js/stomp-websocket", + function() { + "use strict"; diff --git a/src/amd/amd_tail.js b/src/amd/amd_tail.js new file mode 100644 index 0000000..27b8217 --- /dev/null +++ b/src/amd/amd_tail.js @@ -0,0 +1 @@ +}); From 264adfba66d222b4ba6cdd811aff7a2607c366cd Mon Sep 17 00:00:00 2001 From: williamsbdev Date: Mon, 24 Nov 2014 20:35:29 -0600 Subject: [PATCH 03/10] Tech Debt: moving the file into an AMD directory in lib --- Cakefile | 2 +- lib/{stomp-websocket-amd.js => amd/stomp-websocket.js} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename lib/{stomp-websocket-amd.js => amd/stomp-websocket.js} (100%) diff --git a/Cakefile b/Cakefile index e4b95cc..f3e2dd1 100644 --- a/Cakefile +++ b/Cakefile @@ -45,7 +45,7 @@ task 'build:src', 'Build the src files into lib', -> task 'build:amd', 'Build the src files into lib as an AMD module', -> sources = ["src/amd/amd_head.js", "lib/stomp.js", "src/amd/amd_tail.js"] util.log "Compiling amd..." - output = fs.openSync("lib/stomp-websocket-amd.js", "w") + output = fs.openSync("lib/amd/stomp-websocket.js", "w") for source in sources fs.writeSync(output, fs.readFileSync(source) + "\n") fs.closeSync(output) diff --git a/lib/stomp-websocket-amd.js b/lib/amd/stomp-websocket.js similarity index 100% rename from lib/stomp-websocket-amd.js rename to lib/amd/stomp-websocket.js From ea414e6f07137271a15454f10915368fc2328b95 Mon Sep 17 00:00:00 2001 From: williamsbdev Date: Mon, 24 Nov 2014 20:36:57 -0600 Subject: [PATCH 04/10] Tech Debt: cleaning up white space and changing fat arrows to normal rockets --- src/stomp.coffee | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/stomp.coffee b/src/stomp.coffee index 69788a9..6a50b09 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) @@ -290,9 +290,9 @@ class Client messageID = frame.headers["message-id"] # add `ack()` and `nack()` methods directly to the returned frame # so that a simple call to `message.ack()` can acknowledge the message. - frame.ack = (headers = {}) => + frame.ack = (headers = {}) -> client .ack messageID , subscription, headers - frame.nack = (headers = {}) => + frame.nack = (headers = {}) -> client .nack messageID, subscription, headers onreceive frame else @@ -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. From a419e6840d5aeb9bf19faa741ba9ea8e673e1b12 Mon Sep 17 00:00:00 2001 From: williamsbdev Date: Mon, 24 Nov 2014 21:04:11 -0600 Subject: [PATCH 05/10] Feature: creating separate directory for each export type --- Cakefile | 38 ++- lib/amd/stomp-websocket.js | 14 +- lib/commonjs/commonjs_exports.js | 5 + lib/commonjs/stomp-websocket.js | 492 +++++++++++++++++++++++++++ lib/global/global_exports.js | 13 + lib/stomp-node.js | 2 +- lib/stomp.js | 18 +- lib/stomp.min.js | 4 +- src/commonjs/commonjs_exports.coffee | 1 + src/global/global_exports.coffee | 7 + src/stomp.coffee | 18 +- 11 files changed, 561 insertions(+), 51 deletions(-) create mode 100644 lib/commonjs/commonjs_exports.js create mode 100644 lib/commonjs/stomp-websocket.js create mode 100644 lib/global/global_exports.js create mode 100644 src/commonjs/commonjs_exports.coffee create mode 100644 src/global/global_exports.coffee diff --git a/Cakefile b/Cakefile index f3e2dd1..91c61d9 100644 --- a/Cakefile +++ b/Cakefile @@ -34,7 +34,9 @@ task 'test', 'Run the tests', -> task 'build', 'Build source and tests', -> invoke 'build:src' invoke 'build:min' - invoke 'build:amd' + invoke 'build:amd:module' + invoke 'build:commonjs:module' + invoke 'build:global:window' invoke 'build:test' task 'build:src', 'Build the src files into lib', -> @@ -42,10 +44,38 @@ task 'build:src', 'Build the src files into lib', -> exec binDir + "coffee -o lib/ -c src/", (err, stdout, stderr) -> handleError(err) if err +task 'build:global', 'Build the src files into lib as global window function', -> + invoke 'build:src' + invoke 'build:global:window' + +task 'build:global:window', 'Take src files to create a global window function', -> + sources = ['lib/stomp.js', 'lib/global/global_exports.js'] + util.log "Compiling global window function..." + output = fs.openSync('lib/amd/stomp-websocket.js', 'w') + for source in sources + fs.writeSync(output, fs.readFileSync(source) + '\n') + fs.closeSync(output) + task 'build:amd', 'Build the src files into lib as an AMD module', -> - sources = ["src/amd/amd_head.js", "lib/stomp.js", "src/amd/amd_tail.js"] - util.log "Compiling amd..." - output = fs.openSync("lib/amd/stomp-websocket.js", "w") + invoke 'build:src' + invoke 'build:amd:module' + +task 'build:amd:module', 'Take src files to create an AMD module', -> + sources = ['src/amd/amd_head.js', 'lib/stomp.js', 'src/amd/amd_tail.js'] + util.log 'Compiling amd...' + output = fs.openSync('lib/amd/stomp-websocket.js', 'w') + for source in sources + fs.writeSync(output, fs.readFileSync(source) + '\n') + fs.closeSync(output) + +task 'build:commonjs', 'Build the src files into lib as a commonjs module', -> + invoke 'build:src' + invoke 'build:commonjs:module' + +task 'build:commonjs:module', 'Take src files to create a commonjs module', -> + sources = ['lib/stomp.js', 'lib/commonjs/commonjs_exports.js'] + util.log "Compiling commonjs..." + output = fs.openSync("lib/commonjs/stomp-websocket.js", "w") for source in sources fs.writeSync(output, fs.readFileSync(source) + "\n") fs.closeSync(output) diff --git a/lib/amd/stomp-websocket.js b/lib/amd/stomp-websocket.js index 22ac0f0..edb2db2 100644 --- a/lib/amd/stomp-websocket.js +++ b/lib/amd/stomp-websocket.js @@ -1,7 +1,3 @@ -define("js/stomp-websocket", - function() { - "use strict"; - // Generated by CoffeeScript 1.8.0 /* @@ -486,10 +482,10 @@ define("js/stomp-websocket", Frame: Frame }; - if (typeof exports !== "undefined" && exports !== null) { - exports.Stomp = Stomp; - } +}).call(this); +// Generated by CoffeeScript 1.8.0 +(function() { if (typeof window !== "undefined" && window !== null) { Stomp.setInterval = function(interval, f) { return window.setInterval(f, interval); @@ -498,11 +494,7 @@ define("js/stomp-websocket", return window.clearInterval(id); }; window.Stomp = Stomp; - } else if (!exports) { - self.Stomp = Stomp; } }).call(this); -}); - diff --git a/lib/commonjs/commonjs_exports.js b/lib/commonjs/commonjs_exports.js new file mode 100644 index 0000000..5e98154 --- /dev/null +++ b/lib/commonjs/commonjs_exports.js @@ -0,0 +1,5 @@ +// Generated by CoffeeScript 1.8.0 +(function() { + exports.Stomp = Stomp; + +}).call(this); diff --git a/lib/commonjs/stomp-websocket.js b/lib/commonjs/stomp-websocket.js new file mode 100644 index 0000000..322862b --- /dev/null +++ b/lib/commonjs/stomp-websocket.js @@ -0,0 +1,492 @@ +// Generated by CoffeeScript 1.8.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 Byte, Client, Frame, Stomp, + __hasProp = {}.hasOwnProperty, + __slice = [].slice; + + Byte = { + LF: '\x0A', + NULL: '\x00' + }; + + Frame = (function() { + var unmarshallSingle; + + function Frame(command, headers, body) { + this.command = command; + this.headers = headers != null ? headers : {}; + this.body = body != null ? body : ''; + } + + 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); + }; + + 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; + }; + + 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.prototype.debug = function(message) { + var _ref; + return typeof window !== "undefined" && window !== null ? (_ref = window.console) != null ? _ref.log(message) : void 0 : void 0; + }; + + now = function() { + if (Date.now) { + return Date.now(); + } else { + return new Date().valueOf; + } + }; + + 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); + } + } + }; + + 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); + 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("did not receive server activity for the last " + delta + "ms"); + } + return _this.ws.close(); + } + }; + })(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 = 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) { + if (typeof _this.debug === "function") { + _this.debug("<<< PONG"); + } + 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); + } + } + 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._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.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); + } + }; + }; + + 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.commit = function(transaction) { + return this._transmit("COMMIT", { + 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.nack = function(messageID, subscription, headers) { + if (headers == null) { + headers = {}; + } + headers["message-id"] = messageID; + headers.subscription = subscription; + return this._transmit("NACK", headers); + }; + + 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 + }; + +}).call(this); + +// Generated by CoffeeScript 1.8.0 +(function() { + exports.Stomp = Stomp; + +}).call(this); + diff --git a/lib/global/global_exports.js b/lib/global/global_exports.js new file mode 100644 index 0000000..75e30eb --- /dev/null +++ b/lib/global/global_exports.js @@ -0,0 +1,13 @@ +// Generated by CoffeeScript 1.8.0 +(function() { + 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; + } + +}).call(this); diff --git a/lib/stomp-node.js b/lib/stomp-node.js index 68660ee..a268274 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.8.0 /* Stomp Over WebSocket http://www.jmesnil.net/stomp-websocket/doc/ | Apache License V2.0 diff --git a/lib/stomp.js b/lib/stomp.js index 9c30356..02894b8 100644 --- a/lib/stomp.js +++ b/lib/stomp.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.7.1 +// Generated by CoffeeScript 1.8.0 /* Stomp Over WebSocket http://www.jmesnil.net/stomp-websocket/doc/ | Apache License V2.0 @@ -482,20 +482,6 @@ Frame: Frame }; - if (typeof exports !== "undefined" && exports !== null) { - exports.Stomp = 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; - } + this.Stomp = Stomp; }).call(this); diff --git a/lib/stomp.min.js b/lib/stomp.min.js index dd0b1fa..c435965 100644 --- a/lib/stomp.min.js +++ b/lib/stomp.min.js @@ -1,8 +1,8 @@ -// Generated by CoffeeScript 1.7.1 +// Generated by CoffeeScript 1.8.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(s,a,i)};n.unmarshall=function(n){var i,r,s,o;r=n.split(RegExp(""+t.NULL+t.LF+"*"));o={frames:[],partial:""};o.frames=function(){var t,n,s,o;s=r.slice(0,-1);o=[];for(t=0,n=s.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,s,o,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||s===0)){o=Math.max(this.heartbeat.incoming,s);if(typeof this.debug==="function"){this.debug("check PONG every "+o+"ms")}return this.ponger=i.setInterval(o,function(t){return function(){var n;n=e()-t.serverActivity;if(n>o*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?s.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,o,u,a;r=1<=arguments.length?s.call(arguments,0):[];a=this._parseConnect.apply(this,r);u=a[0],this.connectCallback=a[1],o=a[2];if(typeof this.debug==="function"){this.debug("Opening Web Socket...")}this.ws.onmessage=function(i){return function(r){var s,u,a,c,f,h,l,p,d,g,b,m,v;c=typeof ArrayBuffer!=="undefined"&&r.data instanceof ArrayBuffer?(s=new Uint8Array(r.data),typeof i.debug==="function"?i.debug("--- got data length: "+s.length):void 0,function(){var t,e,n;n=[];for(t=0,e=s.length;t + window.setInterval f, interval + Stomp.clearInterval= (id) -> + window.clearInterval id + window.Stomp = Stomp diff --git a/src/stomp.coffee b/src/stomp.coffee index 6a50b09..9ffe1ee 100644 --- a/src/stomp.coffee +++ b/src/stomp.coffee @@ -508,20 +508,4 @@ Stomp = # marshall/unmarshall frames Frame: Frame -# # `Stomp` object exportation - -# export as CommonJS module -if exports? - exports.Stomp = Stomp - -# export in the Web Browser -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 +this.Stomp = Stomp From 88b3c29ab172d73fd6a545c09fb15385e51c24f6 Mon Sep 17 00:00:00 2001 From: williamsbdev Date: Mon, 24 Nov 2014 21:16:17 -0600 Subject: [PATCH 06/10] Refactor: reworking cake file to be more modular Added a task for compiling each version of the desired export --- Cakefile | 51 +- lib/amd/stomp-websocket.js | 20 +- lib/commonjs/stomp-websocket.js | 2 + lib/stomp.min.js | 2 +- lib/window/stomp-websocket.js | 502 ++++++++++++++++++ .../window_export.js} | 0 src/amd/amd_head.js | 2 +- .../window_export.coffee} | 0 8 files changed, 536 insertions(+), 43 deletions(-) create mode 100644 lib/window/stomp-websocket.js rename lib/{global/global_exports.js => window/window_export.js} (100%) rename src/{global/global_exports.coffee => window/window_export.coffee} (100%) diff --git a/Cakefile b/Cakefile index 91c61d9..cbc1410 100644 --- a/Cakefile +++ b/Cakefile @@ -36,25 +36,21 @@ task 'build', 'Build source and tests', -> invoke 'build:min' invoke 'build:amd:module' invoke 'build:commonjs:module' - invoke 'build:global:window' + invoke 'build:window:function' invoke 'build:test' task 'build:src', 'Build the src files into lib', -> - util.log "Compiling src..." - exec binDir + "coffee -o lib/ -c src/", (err, stdout, stderr) -> + util.log 'Compiling src...' + exec binDir + 'coffee -o lib/ -c src/', (err, stdout, stderr) -> handleError(err) if err -task 'build:global', 'Build the src files into lib as global window function', -> +task 'build:window', 'Build the src files into lib as global window function', -> invoke 'build:src' - invoke 'build:global:window' + invoke 'build:window:function' -task 'build:global:window', 'Take src files to create a global window function', -> - sources = ['lib/stomp.js', 'lib/global/global_exports.js'] - util.log "Compiling global window function..." - output = fs.openSync('lib/amd/stomp-websocket.js', 'w') - for source in sources - fs.writeSync(output, fs.readFileSync(source) + '\n') - fs.closeSync(output) +task 'build:window:function', 'Take src files to create a window function', -> + sources = ['lib/stomp.js', 'lib/window/window_export.js'] + buildModule('window', sources) task 'build:amd', 'Build the src files into lib as an AMD module', -> invoke 'build:src' @@ -62,11 +58,7 @@ task 'build:amd', 'Build the src files into lib as an AMD module', -> task 'build:amd:module', 'Take src files to create an AMD module', -> sources = ['src/amd/amd_head.js', 'lib/stomp.js', 'src/amd/amd_tail.js'] - util.log 'Compiling amd...' - output = fs.openSync('lib/amd/stomp-websocket.js', 'w') - for source in sources - fs.writeSync(output, fs.readFileSync(source) + '\n') - fs.closeSync(output) + buildModule('amd', sources) task 'build:commonjs', 'Build the src files into lib as a commonjs module', -> invoke 'build:src' @@ -74,27 +66,30 @@ task 'build:commonjs', 'Build the src files into lib as a commonjs module', -> task 'build:commonjs:module', 'Take src files to create a commonjs module', -> sources = ['lib/stomp.js', 'lib/commonjs/commonjs_exports.js'] - util.log "Compiling commonjs..." - output = fs.openSync("lib/commonjs/stomp-websocket.js", "w") - for source in sources - fs.writeSync(output, fs.readFileSync(source) + "\n") - fs.closeSync(output) + buildModule('commonjs', sources) 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) -> + util.log 'Minify src...' + exec binDir + 'uglifyjs -m --comments all -o lib/stomp.min.js lib/stomp.js', (err, stdout, stderr) -> handleError(err) if err task 'build:doc', 'Build docco documentation', -> - util.log "Building doc..." - exec binDir + "docco -o doc/ src/*.coffee", (err, stdout, stderr) -> + util.log 'Building doc...' + 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) -> + util.log 'Compiling test...' + exec binDir + 'coffee -o dist/test/ -c test/', (err, stdout, stderr) -> handleError(err) if err +buildModule = (module, sources) -> + util.log "Compiling #{module}..." + output = fs.openSync("lib/#{module}/stomp-websocket.js", 'w') + for source in sources + fs.writeSync(output, fs.readFileSync(source) + '\n') + fs.closeSync(output) + watchDir = (dir, callback) -> fs.readdir dir, (err, files) -> handleError(err) if err diff --git a/lib/amd/stomp-websocket.js b/lib/amd/stomp-websocket.js index edb2db2..39b367b 100644 --- a/lib/amd/stomp-websocket.js +++ b/lib/amd/stomp-websocket.js @@ -1,3 +1,7 @@ +define("jmesnil/stomp-websocket", + function() { + "use strict"; + // Generated by CoffeeScript 1.8.0 /* @@ -482,19 +486,9 @@ Frame: Frame }; -}).call(this); - -// Generated by CoffeeScript 1.8.0 -(function() { - 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; - } + this.Stomp = Stomp; }).call(this); +}); + diff --git a/lib/commonjs/stomp-websocket.js b/lib/commonjs/stomp-websocket.js index 322862b..4b5ee32 100644 --- a/lib/commonjs/stomp-websocket.js +++ b/lib/commonjs/stomp-websocket.js @@ -482,6 +482,8 @@ Frame: Frame }; + this.Stomp = Stomp; + }).call(this); // Generated by CoffeeScript 1.8.0 diff --git a/lib/stomp.min.js b/lib/stomp.min.js index c435965..e014564 100644 --- a/lib/stomp.min.js +++ b/lib/stomp.min.js @@ -5,4 +5,4 @@ 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,s=[].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,s,o,u;e=[this.command];s=this.headers["content-length"]===false?true:false;if(s){delete this.headers["content-length"]}u=this.headers;for(i in u){if(!r.call(u,i))continue;o=u[i];e.push(""+i+":"+o)}if(this.body&&!s){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,s,o,u,a,c,f,h,l,p,d,g,b,m,v,y;o=e.search(RegExp(""+t.LF+t.LF));u=e.substring(0,o).split(t.LF);s=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(s,a,i)};n.unmarshall=function(n){var i,r,s,o;r=n.split(RegExp(""+t.NULL+t.LF+"*"));o={frames:[],partial:""};o.frames=function(){var t,n,s,o;s=r.slice(0,-1);o=[];for(t=0,n=s.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,s,o,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||s===0)){o=Math.max(this.heartbeat.incoming,s);if(typeof this.debug==="function"){this.debug("check PONG every "+o+"ms")}return this.ponger=i.setInterval(o,function(t){return function(){var n;n=e()-t.serverActivity;if(n>o*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?s.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,o,u,a;r=1<=arguments.length?s.call(arguments,0):[];a=this._parseConnect.apply(this,r);u=a[0],this.connectCallback=a[1],o=a[2];if(typeof this.debug==="function"){this.debug("Opening Web Socket...")}this.ws.onmessage=function(i){return function(r){var s,u,a,c,f,h,l,p,d,g,b,m,v;c=typeof ArrayBuffer!=="undefined"&&r.data instanceof ArrayBuffer?(s=new Uint8Array(r.data),typeof i.debug==="function"?i.debug("--- got data length: "+s.length):void 0,function(){var t,e,n;n=[];for(t=0,e=s.length;ty;c=p<=y?++b:--b){r=e.charAt(c);if(r===t.NULL){break}i+=r}}return new n(s,a,i)};n.unmarshall=function(n){var i,r,s,o;r=n.split(RegExp(""+t.NULL+t.LF+"*"));o={frames:[],partial:""};o.frames=function(){var t,n,s,o;s=r.slice(0,-1);o=[];for(t=0,n=s.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,s,o,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||s===0)){o=Math.max(this.heartbeat.incoming,s);if(typeof this.debug==="function"){this.debug("check PONG every "+o+"ms")}return this.ponger=i.setInterval(o,function(t){return function(){var n;n=e()-t.serverActivity;if(n>o*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?s.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,o,u,a;r=1<=arguments.length?s.call(arguments,0):[];a=this._parseConnect.apply(this,r);u=a[0],this.connectCallback=a[1],o=a[2];if(typeof this.debug==="function"){this.debug("Opening Web Socket...")}this.ws.onmessage=function(i){return function(r){var s,u,a,c,f,h,l,p,d,g,b,m,v;c=typeof ArrayBuffer!=="undefined"&&r.data instanceof ArrayBuffer?(s=new Uint8Array(r.data),typeof i.debug==="function"?i.debug("--- got data length: "+s.length):void 0,function(){var t,e,n;n=[];for(t=0,e=s.length;t _ref1; i = start <= _ref1 ? ++_j : --_j) { + chr = data.charAt(i); + if (chr === Byte.NULL) { + break; + } + body += chr; + } + } + return new Frame(command, headers, body); + }; + + 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; + }; + + 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.prototype.debug = function(message) { + var _ref; + return typeof window !== "undefined" && window !== null ? (_ref = window.console) != null ? _ref.log(message) : void 0 : void 0; + }; + + now = function() { + if (Date.now) { + return Date.now(); + } else { + return new Date().valueOf; + } + }; + + 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); + } + } + }; + + 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); + 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("did not receive server activity for the last " + delta + "ms"); + } + return _this.ws.close(); + } + }; + })(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 = 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) { + if (typeof _this.debug === "function") { + _this.debug("<<< PONG"); + } + 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); + } + } + 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._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.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); + } + }; + }; + + 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.commit = function(transaction) { + return this._transmit("COMMIT", { + 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.nack = function(messageID, subscription, headers) { + if (headers == null) { + headers = {}; + } + headers["message-id"] = messageID; + headers.subscription = subscription; + return this._transmit("NACK", headers); + }; + + 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 + }; + + this.Stomp = Stomp; + +}).call(this); + +// Generated by CoffeeScript 1.8.0 +(function() { + 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; + } + +}).call(this); + diff --git a/lib/global/global_exports.js b/lib/window/window_export.js similarity index 100% rename from lib/global/global_exports.js rename to lib/window/window_export.js diff --git a/src/amd/amd_head.js b/src/amd/amd_head.js index 9d6c41a..d077354 100644 --- a/src/amd/amd_head.js +++ b/src/amd/amd_head.js @@ -1,3 +1,3 @@ -define("js/stomp-websocket", +define("jmesnil/stomp-websocket", function() { "use strict"; diff --git a/src/global/global_exports.coffee b/src/window/window_export.coffee similarity index 100% rename from src/global/global_exports.coffee rename to src/window/window_export.coffee From 4edf89b9cb49a9491e8897d521cb756abc740826 Mon Sep 17 00:00:00 2001 From: williamsbdev Date: Mon, 24 Nov 2014 21:39:14 -0600 Subject: [PATCH 07/10] Tech Debt: minify all versions of exports and changed file name to stomp.js in each export --- Cakefile | 16 +- lib/amd/stomp.js | 494 ++++++++++++++++++++++++++ lib/amd/stomp.min.js | 8 + lib/commonjs/commonjs_exports.js | 5 - lib/commonjs/stomp-websocket.js | 6 +- lib/commonjs/stomp.js | 490 ++++++++++++++++++++++++++ lib/commonjs/stomp.min.js | 8 + lib/window/stomp-websocket.js | 8 +- lib/window/stomp.js | 498 +++++++++++++++++++++++++++ lib/window/stomp.min.js | 8 + src/commonjs/commonjs_exports.coffee | 1 - src/commonjs/commonjs_exports.js | 1 + src/window/window_export.coffee | 7 - {lib => src}/window/window_export.js | 8 +- 14 files changed, 1523 insertions(+), 35 deletions(-) create mode 100644 lib/amd/stomp.js create mode 100644 lib/amd/stomp.min.js delete mode 100644 lib/commonjs/commonjs_exports.js create mode 100644 lib/commonjs/stomp.js create mode 100644 lib/commonjs/stomp.min.js create mode 100644 lib/window/stomp.js create mode 100644 lib/window/stomp.min.js delete mode 100644 src/commonjs/commonjs_exports.coffee create mode 100644 src/commonjs/commonjs_exports.js delete mode 100644 src/window/window_export.coffee rename {lib => src}/window/window_export.js (62%) diff --git a/Cakefile b/Cakefile index cbc1410..2c7aed0 100644 --- a/Cakefile +++ b/Cakefile @@ -49,7 +49,7 @@ task 'build:window', 'Build the src files into lib as global window function', - invoke 'build:window:function' task 'build:window:function', 'Take src files to create a window function', -> - sources = ['lib/stomp.js', 'lib/window/window_export.js'] + sources = ['lib/stomp.js', 'src/window/window_export.js'] buildModule('window', sources) task 'build:amd', 'Build the src files into lib as an AMD module', -> @@ -65,13 +65,15 @@ task 'build:commonjs', 'Build the src files into lib as a commonjs module', -> invoke 'build:commonjs:module' task 'build:commonjs:module', 'Take src files to create a commonjs module', -> - sources = ['lib/stomp.js', 'lib/commonjs/commonjs_exports.js'] + sources = ['lib/stomp.js', 'src/commonjs/commonjs_exports.js'] buildModule('commonjs', sources) 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) -> - handleError(err) if err + minify('lib/stomp.js', 'lib/stomp.min.js') + minify('lib/amd/stomp.js', 'lib/amd/stomp.min.js') + minify('lib/commonjs/stomp.js', 'lib/commonjs/stomp.min.js') + minify('lib/window/stomp.js', 'lib/window/stomp.min.js') task 'build:doc', 'Build docco documentation', -> util.log 'Building doc...' @@ -85,11 +87,15 @@ task 'build:test', 'Build the test files into lib/test', -> buildModule = (module, sources) -> util.log "Compiling #{module}..." - output = fs.openSync("lib/#{module}/stomp-websocket.js", 'w') + output = fs.openSync("lib/#{module}/stomp.js", 'w') for source in sources fs.writeSync(output, fs.readFileSync(source) + '\n') fs.closeSync(output) +minify = (input, output) -> + exec binDir + "uglifyjs -m --comments all -o #{output} #{input}", (err, stdout, stderr) -> + handleError(err) if err + watchDir = (dir, callback) -> fs.readdir dir, (err, files) -> handleError(err) if err diff --git a/lib/amd/stomp.js b/lib/amd/stomp.js new file mode 100644 index 0000000..39b367b --- /dev/null +++ b/lib/amd/stomp.js @@ -0,0 +1,494 @@ +define("jmesnil/stomp-websocket", + function() { + "use strict"; + +// Generated by CoffeeScript 1.8.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 Byte, Client, Frame, Stomp, + __hasProp = {}.hasOwnProperty, + __slice = [].slice; + + Byte = { + LF: '\x0A', + NULL: '\x00' + }; + + Frame = (function() { + var unmarshallSingle; + + function Frame(command, headers, body) { + this.command = command; + this.headers = headers != null ? headers : {}; + this.body = body != null ? body : ''; + } + + 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); + }; + + 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; + }; + + 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.prototype.debug = function(message) { + var _ref; + return typeof window !== "undefined" && window !== null ? (_ref = window.console) != null ? _ref.log(message) : void 0 : void 0; + }; + + now = function() { + if (Date.now) { + return Date.now(); + } else { + return new Date().valueOf; + } + }; + + 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); + } + } + }; + + 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); + 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("did not receive server activity for the last " + delta + "ms"); + } + return _this.ws.close(); + } + }; + })(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 = 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) { + if (typeof _this.debug === "function") { + _this.debug("<<< PONG"); + } + 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); + } + } + 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._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.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); + } + }; + }; + + 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.commit = function(transaction) { + return this._transmit("COMMIT", { + 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.nack = function(messageID, subscription, headers) { + if (headers == null) { + headers = {}; + } + headers["message-id"] = messageID; + headers.subscription = subscription; + return this._transmit("NACK", headers); + }; + + 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 + }; + + this.Stomp = Stomp; + +}).call(this); + +}); + diff --git a/lib/amd/stomp.min.js b/lib/amd/stomp.min.js new file mode 100644 index 0000000..f47b62b --- /dev/null +++ b/lib/amd/stomp.min.js @@ -0,0 +1,8 @@ +define("jmesnil/stomp-websocket",function(){"use strict";// Generated by CoffeeScript 1.8.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,s=[].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,s,o,u;e=[this.command];s=this.headers["content-length"]===false?true:false;if(s){delete this.headers["content-length"]}u=this.headers;for(i in u){if(!r.call(u,i))continue;o=u[i];e.push(""+i+":"+o)}if(this.body&&!s){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,s,o,u,a,c,f,h,l,p,d,g,b,m,v,y;o=e.search(RegExp(""+t.LF+t.LF));u=e.substring(0,o).split(t.LF);s=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(s,a,i)};n.unmarshall=function(n){var i,r,s,o;r=n.split(RegExp(""+t.NULL+t.LF+"*"));o={frames:[],partial:""};o.frames=function(){var t,n,s,o;s=r.slice(0,-1);o=[];for(t=0,n=s.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,s,o,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||s===0)){o=Math.max(this.heartbeat.incoming,s);if(typeof this.debug==="function"){this.debug("check PONG every "+o+"ms")}return this.ponger=i.setInterval(o,function(t){return function(){var n;n=e()-t.serverActivity;if(n>o*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?s.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,o,u,a;r=1<=arguments.length?s.call(arguments,0):[];a=this._parseConnect.apply(this,r);u=a[0],this.connectCallback=a[1],o=a[2];if(typeof this.debug==="function"){this.debug("Opening Web Socket...")}this.ws.onmessage=function(i){return function(r){var s,u,a,c,f,h,l,p,d,g,b,m,v;c=typeof ArrayBuffer!=="undefined"&&r.data instanceof ArrayBuffer?(s=new Uint8Array(r.data),typeof i.debug==="function"?i.debug("--- got data length: "+s.length):void 0,function(){var t,e,n;n=[];for(t=0,e=s.length;t _ref1; i = start <= _ref1 ? ++_j : --_j) { + chr = data.charAt(i); + if (chr === Byte.NULL) { + break; + } + body += chr; + } + } + return new Frame(command, headers, body); + }; + + 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; + }; + + 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.prototype.debug = function(message) { + var _ref; + return typeof window !== "undefined" && window !== null ? (_ref = window.console) != null ? _ref.log(message) : void 0 : void 0; + }; + + now = function() { + if (Date.now) { + return Date.now(); + } else { + return new Date().valueOf; + } + }; + + 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); + } + } + }; + + 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); + 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("did not receive server activity for the last " + delta + "ms"); + } + return _this.ws.close(); + } + }; + })(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 = 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) { + if (typeof _this.debug === "function") { + _this.debug("<<< PONG"); + } + 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); + } + } + 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._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.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); + } + }; + }; + + 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.commit = function(transaction) { + return this._transmit("COMMIT", { + 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.nack = function(messageID, subscription, headers) { + if (headers == null) { + headers = {}; + } + headers["message-id"] = messageID; + headers.subscription = subscription; + return this._transmit("NACK", headers); + }; + + 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 + }; + + this.Stomp = Stomp; + +}).call(this); + +exports.Stomp = Stomp; + diff --git a/lib/commonjs/stomp.min.js b/lib/commonjs/stomp.min.js new file mode 100644 index 0000000..7f0fb18 --- /dev/null +++ b/lib/commonjs/stomp.min.js @@ -0,0 +1,8 @@ +// Generated by CoffeeScript 1.8.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;t _ref1; i = start <= _ref1 ? ++_j : --_j) { + chr = data.charAt(i); + if (chr === Byte.NULL) { + break; + } + body += chr; + } + } + return new Frame(command, headers, body); + }; + + 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; + }; + + 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.prototype.debug = function(message) { + var _ref; + return typeof window !== "undefined" && window !== null ? (_ref = window.console) != null ? _ref.log(message) : void 0 : void 0; + }; + + now = function() { + if (Date.now) { + return Date.now(); + } else { + return new Date().valueOf; + } + }; + + 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); + } + } + }; + + 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); + 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("did not receive server activity for the last " + delta + "ms"); + } + return _this.ws.close(); + } + }; + })(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 = 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) { + if (typeof _this.debug === "function") { + _this.debug("<<< PONG"); + } + 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); + } + } + 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._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.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); + } + }; + }; + + 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.commit = function(transaction) { + return this._transmit("COMMIT", { + 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.nack = function(messageID, subscription, headers) { + if (headers == null) { + headers = {}; + } + headers["message-id"] = messageID; + headers.subscription = subscription; + return this._transmit("NACK", headers); + }; + + 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 + }; + + this.Stomp = Stomp; + +}).call(this); + +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; +} + diff --git a/lib/window/stomp.min.js b/lib/window/stomp.min.js new file mode 100644 index 0000000..42b4634 --- /dev/null +++ b/lib/window/stomp.min.js @@ -0,0 +1,8 @@ +// Generated by CoffeeScript 1.8.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;t - window.setInterval f, interval - Stomp.clearInterval= (id) -> - window.clearInterval id - window.Stomp = Stomp diff --git a/lib/window/window_export.js b/src/window/window_export.js similarity index 62% rename from lib/window/window_export.js rename to src/window/window_export.js index 75e30eb..b85225d 100644 --- a/lib/window/window_export.js +++ b/src/window/window_export.js @@ -1,6 +1,4 @@ -// Generated by CoffeeScript 1.8.0 -(function() { - if (typeof window !== "undefined" && window !== null) { +if (typeof window !== "undefined" && window !== null) { Stomp.setInterval = function(interval, f) { return window.setInterval(f, interval); }; @@ -8,6 +6,4 @@ return window.clearInterval(id); }; window.Stomp = Stomp; - } - -}).call(this); +} From a9ec33d3bed1dfde18eba05342e1ad0d6fac4a74 Mon Sep 17 00:00:00 2001 From: williamsbdev Date: Mon, 24 Nov 2014 21:46:23 -0600 Subject: [PATCH 08/10] Refactor: renamed AMD module name --- src/amd/amd_head.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/amd/amd_head.js b/src/amd/amd_head.js index d077354..698460f 100644 --- a/src/amd/amd_head.js +++ b/src/amd/amd_head.js @@ -1,3 +1,3 @@ -define("jmesnil/stomp-websocket", +define("jmesnil/stomp", function() { "use strict"; From b0643cbe12ef48865428c805ed4a4e30796ef7d4 Mon Sep 17 00:00:00 2001 From: williamsbdev Date: Mon, 24 Nov 2014 21:47:31 -0600 Subject: [PATCH 09/10] Tech Debt: removed stomp-websocket.js file in amd directory --- lib/amd/stomp-websocket.js | 494 ------------------------------------- 1 file changed, 494 deletions(-) delete mode 100644 lib/amd/stomp-websocket.js diff --git a/lib/amd/stomp-websocket.js b/lib/amd/stomp-websocket.js deleted file mode 100644 index 39b367b..0000000 --- a/lib/amd/stomp-websocket.js +++ /dev/null @@ -1,494 +0,0 @@ -define("jmesnil/stomp-websocket", - function() { - "use strict"; - -// Generated by CoffeeScript 1.8.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 Byte, Client, Frame, Stomp, - __hasProp = {}.hasOwnProperty, - __slice = [].slice; - - Byte = { - LF: '\x0A', - NULL: '\x00' - }; - - Frame = (function() { - var unmarshallSingle; - - function Frame(command, headers, body) { - this.command = command; - this.headers = headers != null ? headers : {}; - this.body = body != null ? body : ''; - } - - 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); - }; - - 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; - }; - - 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.prototype.debug = function(message) { - var _ref; - return typeof window !== "undefined" && window !== null ? (_ref = window.console) != null ? _ref.log(message) : void 0 : void 0; - }; - - now = function() { - if (Date.now) { - return Date.now(); - } else { - return new Date().valueOf; - } - }; - - 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); - } - } - }; - - 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); - 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("did not receive server activity for the last " + delta + "ms"); - } - return _this.ws.close(); - } - }; - })(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 = 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) { - if (typeof _this.debug === "function") { - _this.debug("<<< PONG"); - } - 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); - } - } - 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._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.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); - } - }; - }; - - 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.commit = function(transaction) { - return this._transmit("COMMIT", { - 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.nack = function(messageID, subscription, headers) { - if (headers == null) { - headers = {}; - } - headers["message-id"] = messageID; - headers.subscription = subscription; - return this._transmit("NACK", headers); - }; - - 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 - }; - - this.Stomp = Stomp; - -}).call(this); - -}); - From 45b2e52fbe096ca60ec18ea5d83ed49dc441389f Mon Sep 17 00:00:00 2001 From: williamsbdev Date: Mon, 24 Nov 2014 21:48:55 -0600 Subject: [PATCH 10/10] Tech Debt: remove the stomp-websocket.js files from export directories --- lib/commonjs/stomp-websocket.js | 490 ------------------------------- lib/window/stomp-websocket.js | 498 -------------------------------- 2 files changed, 988 deletions(-) delete mode 100644 lib/commonjs/stomp-websocket.js delete mode 100644 lib/window/stomp-websocket.js diff --git a/lib/commonjs/stomp-websocket.js b/lib/commonjs/stomp-websocket.js deleted file mode 100644 index de45179..0000000 --- a/lib/commonjs/stomp-websocket.js +++ /dev/null @@ -1,490 +0,0 @@ -// Generated by CoffeeScript 1.8.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 Byte, Client, Frame, Stomp, - __hasProp = {}.hasOwnProperty, - __slice = [].slice; - - Byte = { - LF: '\x0A', - NULL: '\x00' - }; - - Frame = (function() { - var unmarshallSingle; - - function Frame(command, headers, body) { - this.command = command; - this.headers = headers != null ? headers : {}; - this.body = body != null ? body : ''; - } - - 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); - }; - - 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; - }; - - 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.prototype.debug = function(message) { - var _ref; - return typeof window !== "undefined" && window !== null ? (_ref = window.console) != null ? _ref.log(message) : void 0 : void 0; - }; - - now = function() { - if (Date.now) { - return Date.now(); - } else { - return new Date().valueOf; - } - }; - - 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); - } - } - }; - - 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); - 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("did not receive server activity for the last " + delta + "ms"); - } - return _this.ws.close(); - } - }; - })(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 = 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) { - if (typeof _this.debug === "function") { - _this.debug("<<< PONG"); - } - 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); - } - } - 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._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.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); - } - }; - }; - - 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.commit = function(transaction) { - return this._transmit("COMMIT", { - 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.nack = function(messageID, subscription, headers) { - if (headers == null) { - headers = {}; - } - headers["message-id"] = messageID; - headers.subscription = subscription; - return this._transmit("NACK", headers); - }; - - 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 - }; - - this.Stomp = Stomp; - -}).call(this); - -exports.Stomp = Stomp; - diff --git a/lib/window/stomp-websocket.js b/lib/window/stomp-websocket.js deleted file mode 100644 index ae0dd62..0000000 --- a/lib/window/stomp-websocket.js +++ /dev/null @@ -1,498 +0,0 @@ -// Generated by CoffeeScript 1.8.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 Byte, Client, Frame, Stomp, - __hasProp = {}.hasOwnProperty, - __slice = [].slice; - - Byte = { - LF: '\x0A', - NULL: '\x00' - }; - - Frame = (function() { - var unmarshallSingle; - - function Frame(command, headers, body) { - this.command = command; - this.headers = headers != null ? headers : {}; - this.body = body != null ? body : ''; - } - - 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); - }; - - 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; - }; - - 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.prototype.debug = function(message) { - var _ref; - return typeof window !== "undefined" && window !== null ? (_ref = window.console) != null ? _ref.log(message) : void 0 : void 0; - }; - - now = function() { - if (Date.now) { - return Date.now(); - } else { - return new Date().valueOf; - } - }; - - 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); - } - } - }; - - 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); - 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("did not receive server activity for the last " + delta + "ms"); - } - return _this.ws.close(); - } - }; - })(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 = 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) { - if (typeof _this.debug === "function") { - _this.debug("<<< PONG"); - } - 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); - } - } - 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._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.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); - } - }; - }; - - 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.commit = function(transaction) { - return this._transmit("COMMIT", { - 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.nack = function(messageID, subscription, headers) { - if (headers == null) { - headers = {}; - } - headers["message-id"] = messageID; - headers.subscription = subscription; - return this._transmit("NACK", headers); - }; - - 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 - }; - - this.Stomp = Stomp; - -}).call(this); - -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; -} -