Permalink
Browse files

remove cookies, update engine.io

  • Loading branch information...
1 parent fed33ef commit 3b1a5f6a1daa32724c7db1884edfbee3283f502c @contra contra committed Aug 27, 2012
Showing with 49 additions and 252 deletions.
  1. +3 −18 README.md
  2. +20 −80 examples/add/vein.js
  3. +2 −39 lib/Client.coffee
  4. +0 −14 lib/Server.coffee
  5. +2 −2 package.json
  6. +1 −18 test/server.coffee
  7. +20 −80 vein.js
  8. +1 −1 vein.min.js
View
@@ -17,6 +17,8 @@
</tr>
</table>
+Vein uses ES5 features so be sure to include es5shim on your page.
+
## Example
### Server
@@ -67,23 +69,6 @@ vein.add('getNumber', function (res, name, num) {
});
```
-### Cookies
-
-The server can read and write cookies to the client via res.cookie()
-
-```javascript
-vein.add('login', function (res, username, password) {
- if (res.cookie('login')) {
- res.reply(false, 'You already logged in!');
- } else if (username == 'username' && password == 'pass123') {
- res.cookie('login', 'success');
- res.reply(true);
- } else {
- res.reply(false, 'Invalid username or password');
- }
-});
-```
-
### Middleware
You can use middleware to add layers in front of your services. Any arguments passed into next will be thrown as an error on the client and end the middleware chain.
@@ -104,7 +89,7 @@ vein.use(function(req, res, next){
### Testing
-Vein supports calling the res object as a function. If you write your code like this you will be able to test your services without writing anything specific to vein. The only difference is that you still have to put the callback first (this is to prevent headaches with variable arguments).
+Vein supports calling the res object as a function. This makes it easier to integrate vanilla-JS services into vein. The only difference is that you still have to put the callback first (this is to prevent headaches with variable arguments from the client).
```javascript
vein.add('echoUser', function (res, username, password) {
View
@@ -6,7 +6,7 @@
* @api public.
*/
-exports.version = '0.2.1';
+exports.version = '0.2.2';
/**
* Protocol version.
@@ -421,7 +421,7 @@ exports.decodePayload = function (data) {
*/
var Polling = require('./polling')
- , util = require('../util')
+ , util = require('../util');
/**
* Module exports.
@@ -433,13 +433,13 @@ module.exports = JSONPPolling;
* Cached regular expressions.
*/
-var rNewline = /\n/g
+var rNewline = /\n/g;
/**
* Global JSONP callbacks.
*/
-var callbacks = global.___eio = [];
+var callbacks;
/**
* Callbacks count.
@@ -463,8 +463,16 @@ function empty () { }
function JSONPPolling (opts) {
Polling.call(this, opts);
+ // define global callbacks array if not present
+ // we do this here (lazily) to avoid unneeded global pollution
+ if (!callbacks) {
+ // we need to consider multiple engines in the same page
+ if (!global.___eio) global.___eio = [];
+ callbacks = global.___eio;
+ }
+
// callback identifier
- this.index = index++;
+ this.index = callbacks.length;
// add callback to jsonp global
var self = this;
@@ -473,7 +481,7 @@ function JSONPPolling (opts) {
});
// append to query string
- this.query.j = callbacks.length - 1;
+ this.query.j = this.index;
};
/**
@@ -532,7 +540,7 @@ JSONPPolling.prototype.doPoll = function () {
script.async = true;
script.src = this.uri();
- var insertAt = document.getElementsByTagName('script')[0]
+ var insertAt = document.getElementsByTagName('script')[0];
insertAt.parentNode.insertBefore(script, insertAt);
this.script = script;
@@ -554,7 +562,7 @@ JSONPPolling.prototype.doPoll = function () {
*/
JSONPPolling.prototype.doWrite = function (data, fn) {
- var self = this
+ var self = this;
if (!this.form) {
var form = document.createElement('form')
@@ -613,7 +621,7 @@ JSONPPolling.prototype.doWrite = function (data, fn) {
} catch(e) {}
if (this.iframe.attachEvent) {
- iframe.onreadystatechange = function () {
+ this.iframe.onreadystatechange = function(){
if (self.iframe.readyState == 'complete') {
complete();
}
@@ -2692,10 +2700,6 @@ Transport.prototype.onClose = function () {
if (!Array.isArray(msg.args)) {
return done(false);
}
- } else if (msg.type === 'cookie') {
- if (typeof msg.key !== 'string') {
- return done(false);
- }
} else if (msg.type === 'services') {
if (!Array.isArray(msg.args)) {
return done(false);
@@ -2711,9 +2715,8 @@ Transport.prototype.onClose = function () {
message: function(socket, msg) {
var k, _i, _len, _ref, _ref1;
if (msg.type === 'response') {
- return (_ref = this.callbacks)[msg.id].apply(_ref, msg.args);
- } else if (msg.type === 'cookie') {
- return this.cookie(msg.key, msg.val);
+ (_ref = this.callbacks)[msg.id].apply(_ref, msg.args);
+ return delete this.callbacks[msg.id];
} else if (msg.type === 'services') {
this.services = msg.args;
_ref1 = this.services;
@@ -2736,72 +2739,9 @@ Transport.prototype.onClose = function () {
type: 'request',
id: id,
service: service,
- args: args,
- cookies: _this.cookie()
+ args: args
});
};
- },
- cookie: function(key, val, expires) {
- var all, remove, set, _ref,
- _this = this;
- if (isBrowser) {
- all = function() {
- var cookie, pair, _i, _len, _ref;
- out = {};
- _ref = document.cookie.split(";");
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- cookie = _ref[_i];
- pair = cookie.split("=");
- if (!(pair[0] && pair[1])) {
- continue;
- }
- out[pair[0].trim()] = pair[1].trim();
- }
- return out;
- };
- set = function(key, val, expires) {
- var sExpires;
- sExpires = "";
- if (typeof expires === 'number') {
- sExpires = "; max-age=" + expires;
- }
- if (typeof expires === 'string') {
- sExpires = "; expires=" + expires;
- }
- if (typeof expires === 'object' ? expires.toGMTString : void 0) {
- sExpires = "; expires=" + (expires.toGMTString());
- }
- document.cookie = "" + (escape(key)) + "=" + (escape(val)) + sExpires;
- };
- remove = function(key) {
- document.cookie = "" + (escape(key)) + "=; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/";
- };
- } else {
- if ((_ref = this.cookies) == null) {
- this.cookies = {};
- }
- all = function() {
- return _this.cookies;
- };
- set = function(key, val, expires) {
- _this.cookies[key] = val;
- };
- remove = function(key) {
- delete _this.cookies[key];
- };
- }
- if (!key) {
- return all();
- }
- if (key && val === null) {
- return remove(key);
- }
- if (key && !val) {
- return all()[key];
- }
- if (key && val) {
- return set(key, val, expires);
- }
}
};
for (k in opt) {
View
@@ -26,8 +26,6 @@ client = (opt) ->
return done false unless typeof @callbacks[msg.id] is 'function'
return done false unless typeof msg.service is 'string'
return done false unless Array.isArray msg.args
- else if msg.type is 'cookie'
- return done false unless typeof msg.key is 'string'
else if msg.type is 'services'
return done false unless Array.isArray msg.args
else
@@ -39,11 +37,10 @@ client = (opt) ->
message: (socket, msg) ->
if msg.type is 'response'
@callbacks[msg.id] msg.args...
- else if msg.type is 'cookie'
- @cookie msg.key, msg.val
+ delete @callbacks[msg.id]
else if msg.type is 'services'
@services = msg.args
- @[k]=@getSender(socket,k) for k in @services
+ @[k] = @getSender(socket,k) for k in @services
@synced = true
@emit 'ready', @services
@@ -56,40 +53,6 @@ client = (opt) ->
id: id
service: service
args: args
- cookies: @cookie()
-
- cookie: (key, val, expires) ->
- if isBrowser
- all = ->
- out = {}
- for cookie in document.cookie.split ";"
- pair = cookie.split "="
- continue unless pair[0] and pair[1]
- out[pair[0].trim()] = pair[1].trim()
- return out
- set = (key, val, expires) ->
- sExpires = ""
- sExpires = "; max-age=#{expires}" if typeof expires is 'number'
- sExpires = "; expires=#{expires}" if typeof expires is 'string'
- sExpires = "; expires=#{expires.toGMTString()}" if expires.toGMTString if typeof expires is 'object'
- document.cookie = "#{escape(key)}=#{escape(val)}#{sExpires}"
- return
- remove = (key) ->
- document.cookie = "#{escape(key)}=; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/"
- return
- else
- @cookies ?= {}
- all = => @cookies
- set = (key, val, expires) =>
- @cookies[key] = val
- return
- remove = (key) =>
- delete @cookies[key]
- return
- return all() unless key
- return remove key if key and val is null
- return all()[key] if key and not val
- return set key, val, expires if key and val
out.options[k]=v for k,v of opt
return out
View
@@ -38,7 +38,6 @@ module.exports = (opt) ->
return done false unless typeof msg.service is 'string'
return done false unless typeof @services[msg.service] is 'function'
return done false unless Array.isArray msg.args
- return done false if msg.cookies? and typeof msg.cookies isnt 'object'
else
return done false
return done true
@@ -67,19 +66,6 @@ module.exports = (opt) ->
args: args
return @
- responder.cookie = (key, val) ->
- # TODO: implement expires
- return msg.cookies unless key or val
- if key and not val
- return msg.cookies[key]
- else
- msg.cookies[key] = val
- socket.write
- type: 'cookie'
- key: key
- val: val
- return @
-
responder.reply = responder
return responder
View
@@ -1,7 +1,7 @@
{
"name":"vein",
"description":"WebSocket RPC",
- "version":"0.4.1",
+ "version":"0.4.2",
"homepage":"http://github.com/wearefractal/vein",
"repository":"git://github.com/wearefractal/vein.git",
"author":"Fractal <contact@wearefractal.com> (http://wearefractal.com/)",
@@ -10,7 +10,7 @@
"dependencies":{
"coffee-script":"*",
"async":"*",
- "protosock":"0.0.5"
+ "protosock":"0.0.6"
},
"devDependencies":{
"mocha":"*",
View
@@ -53,7 +53,6 @@ describe 'Vein', ->
services.should.eql ['test']
client.test 5, 6, (num) ->
num.should.equal 30
- serv.destroy()
done()
it 'should call as fn', (done) ->
@@ -69,27 +68,12 @@ describe 'Vein', ->
services.should.eql ['test']
client.test 5, 6, (num) ->
num.should.equal 30
- serv.destroy()
- done()
-
- it 'should transmit cookies', (done) ->
- serv = getServer()
- serv.add 'test', (res) ->
- res.cookie 'result', 'oi'
- res.reply 'goyta'
-
- client = getClient serv
- client.ready (services) ->
- client.test ->
- client.cookie('result').should.equal 'oi'
- serv.destroy()
done()
describe 'middleware', ->
it 'should add', (done) ->
serv = getServer()
serv.use (req, res, next) -> next()
- serv.destroy()
done()
it 'should call', (done) ->
@@ -105,7 +89,6 @@ describe 'Vein', ->
client.ready (services) ->
client.test ->
called.should.equal true
- serv.destroy()
done()
describe 'client', ->
@@ -116,7 +99,7 @@ describe 'client', ->
client2 = getClient serv
client2.ready (services) -> done()
- it 'should disconnect before ready', (done) ->
+ it 'should disconnect after ready', (done) ->
serv = getServer()
client = getClient serv
client.ready ->
Oops, something went wrong.

0 comments on commit 3b1a5f6

Please sign in to comment.