This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

0.0.3

* improved readme
* some cleanup
  • Loading branch information...
1 parent d1cbbdd commit 6ddd646b51883f431f8aa1a5b3437d12c809091e @ramitos committed Dec 9, 2012
Showing with 109 additions and 849 deletions.
  1. +1 −2 .gitignore
  2. +2 −1 .npmignore
  3. +13 −2 component.json
  4. +0 −268 dist/restio.js
  5. +14 −7 package.json
  6. +22 −3 readme.md
  7. +9 −274 src/client/restio.js
  8. +40 −0 src/client/setup.js
  9. +2 −285 test/index.html
  10. +6 −7 test/server.js
View
@@ -1,5 +1,4 @@
node_modules
.DS_Store
build
-components
-*.gz
+components
View
@@ -2,4 +2,5 @@ node_modules
.DS_Store
components
build
-component.json
+component.json
+src/client
View
@@ -1,10 +1,21 @@
{
"name": "restio",
- "version": "0.0.1",
+ "license": "MIT",
+ "version": "0.0.3",
+ "twitter": "@ramitos",
+ "description": "REST-like syntax for websockets",
"main": "src/client/restio.js",
- "scripts": ["src/client/restio.js"],
+ "scripts": [
+ "src/client/restio.js",
+ "src/client/setup.js"
+ ],
"dependencies": {
+ "LearnBoost/engine.io-client": "*",
"ramitos/match-route": "*",
"ramitos/sgen": "*"
+ },
+ "development": {
+ "visionmedia/mocha": "*",
+ "component/assert": "*"
}
}
View
@@ -1,268 +0,0 @@
-;(function (window) {
-/******************************** PATHTOREGEXP ********************************/
- /**
- * Normalize the given path string,
- * returning a regular expression.
- *
- * An empty array should be passed,
- * which will contain the placeholder
- * key names. For example "/user/:id" will
- * then contain ["id"].
- *
- * @param {String|RegExp|Array} path
- * @param {Array} keys
- * @param {Object} options
- * @return {RegExp}
- * @api private
- */
- var pathtoRegexp = function (path, keys, options) {
- options = options || {};
- var sensitive = options.sensitive;
- var strict = options.strict;
- keys = keys || [];
-
- if (path instanceof RegExp) return path;
- if (path instanceof Array) path = '(' + path.join('|') + ')';
-
- path = path
- .concat(strict ? '' : '/?')
- .replace(/\/\(/g, '(?:/')
- .replace(/\+/g, '__plus__')
- .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){
- keys.push({ name: key, optional: !! optional });
- slash = slash || '';
- return ''
- + (optional ? '' : slash)
- + '(?:'
- + (optional ? slash : '')
- + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'
- + (optional || '');
- })
- .replace(/([\/.])/g, '\\$1')
- .replace(/__plus__/g, '(.+)')
- .replace(/\*/g, '(.*)');
-
- return new RegExp('^' + path + '$', sensitive ? '' : 'i');
- };
-
-/********************************* QUERYSTRING ********************************/
- /**
- * Parse the given query `str`.
- *
- * @param {String} str
- * @return {Object}
- * @api public
- */
- var qs = {};
- qs.parse = function(str){
- if ('string' != typeof str) return {};
- str = str.trim();
- if ('' == str) return {};
- return str
- .split('&')
- .reduce(function(obj, pair){
- var parts = pair.split('=');
- obj[parts[0]] = null == parts[1]
- ? ''
- : decodeURIComponent(parts[1]);
- return obj;
- }, {});
- };
-
- /**
- * Stringify the given `obj`.
- *
- * @param {Object} obj
- * @return {String}
- * @api public
- */
-
- qs.stringify = function(obj){
- if (!obj) return '';
- var pairs = [];
- for (var key in obj) {
- pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]));
- }
- return pairs.join('&');
- };
-
-/************************************ URL *************************************/
- /**
- * Parse the given `url`.
- *
- * @param {String} str
- * @return {Object}
- * @api public
- */
- var url = {};
-
- url.parse = function (url){
- var a = document.createElement('a');
- a.href = url;
- return {
- href: a.href,
- host: a.host || location.host,
- port: a.port || location.port,
- hash: a.hash,
- hostname: a.hostname || location.hostname,
- pathname: a.pathname,
- protocol: !a.protocol || ':' == a.protocol ? location.protocol : a.protocol,
- search: a.search,
- query: a.search.slice(1)
- };
- };
-
- /**
- * Check if `url` is absolute.
- *
- * @param {String} url
- * @return {Boolean}
- * @api public
- */
- url.isAbsolute = function (url){
- return 0 == url.indexOf('//') || !!~url.indexOf('://');
- };
-
- /**
- * Check if `url` is relative.
- *
- * @param {String} url
- * @return {Boolean}
- * @api public
- */
- url.isRelative = function (url){
- return !exports.isAbsolute(url);
- };
-
- /**
- * Check if `url` is cross domain.
- *
- * @param {String} url
- * @return {Boolean}
- * @api public
- */
- url.isCrossDomain = function (url){
- url = exports.parse(url);
- return url.hostname !== location.hostname
- || url.port !== location.port
- || url.protocol !== location.protocol;
- };
-
-/********************************* MATCHROUTE *********************************/
- var matchRoute = function (req, routes, callback) {
- var method = req.method.toLowerCase();
- var parsedurl = url.parse(req.url);
- var querystring = parsedurl.query;
- var query = qs.parse(querystring);
- var pathname = parsedurl.pathname;
- var params = new Object();
- var matchroute = null;
-
- Object.keys(routes[method]).forEach(function (route) {
- var keys = new Array();
- var rexp = pathtoRegexp(route, keys, false, false);
- var match = pathname.match(rexp);
- if(!match) return;
-
- match.shift();
- matchroute = routes[method][route];
-
- match.forEach(function (param, index) {
- params[keys[index].name] = param;
- });
- });
-
- callback(matchroute, params, query);
- };
-
-/************************************ SGEN ************************************/
- var sgen = function (length) {
- var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.split('');
- var hash = [];
-
- for(var i = 0; i < length; i += 1) {
- hash.push(chars[Math.floor(Math.random()*62)]);
- };
-
- return hash.join('');
- };
-
-/******************************************************************************/
-/********************************** RESTIO ************************************/
-/******************************************************************************/
-
- var restio = window.restio = {};
-
- var setup = function (io, socket) {
- io.callbacks = {};
- io.routes = {};
- io.on = {};
-
- ['get', 'post', 'put', 'delete'].forEach(function (method) {
- io.callbacks[method] = {};
- io.routes[method] = {};
-
- io.on[method] = function (path, callback) {
- io.routes[method][path] = callback;
- };
-
- io[method] = function (url) {
- var callback = function () {};
- var id = sgen(10);
- var idset = false;
- var data = {};
-
- if(typeof arguments[1] === 'function') {
- callback = arguments[1];
- } else {
- data = arguments[1];
- callback = arguments[2];
- }
-
- while(!idset) {
- if(!io.callbacks[method][id]) idset = true;
- else id = sgen(10);
- }
-
- callback.tm = setTimeout(function () {
- delete io.callbacks[method][id];
- }, 3600000);
-
- socket.send(JSON.stringify({id: id, url: url, method: method, data: data}));
- io.callbacks[method][id] = callback;
- };
- });
- };
-
- restio.listen = function (addr, callback) {
- var socket = new eio.Socket(addr);
- socket.on('open', function () {
- var io = {};
- var on = {};
- setup(io, socket);
-
- on.response = function (response) {
- clearTimeout(io.callbacks[response.method][response.id].tm);
- io.callbacks[response.method][response.id](response.data);
- };
-
- on.message = function (message) {
- message = JSON.parse(message);
- if(io.callbacks[message.method][message.id]) on.response(message);
- else on.request(message);
- };
-
- on.request = function (request) {
- matchRoute(request, io.routes, function (fn, params, query) {
- fn(params, query, request.data, function (data) {
- request.data = data;
- socket.send(JSON.stringify(request));
- });
- });
- };
-
- socket.on('message', on.message);
- callback(io);
- });
- };
-})(window);
View
@@ -1,22 +1,29 @@
{
"name": "restio",
- "version": "0.0.2",
- "description": "engine.io REST helper",
+ "license": "MIT",
+ "version": "0.0.3",
+ "author": "Sérgio Ramos",
+ "description": "REST-like syntax for websockets",
"main": "src/server/restio.js",
"repository": {
"type": "git",
"url": "https://github.com/ramitos/restio"
},
- "keywords": ["socket.io", "rest", "crud"],
+ "keywords": [
+ "socket.io",
+ "websockets",
+ "engine.io",
+ "rest",
+ "crud"
+ ],
"dependencies": {
"match-route": "0.0.x",
"engine.io": "0.3.x",
"sgen": "0.0.x"
},
- "author": "Sérgio Ramos",
- "license": "MIT",
"devDependencies": {
- "kip": "0.0.8beta",
- "blage": "0.0.7"
+ "better-assert": "0.1.x"
+ "blage": "0.0.x",
+ "kip": "*"
}
}
View
@@ -1,5 +1,17 @@
# restio
+REST-like syntax for websockets built on top of [engine.io](https://github.com/LearnBoost/engine.io)
+
+## install
+
+```bash
+$ component install [--dev] ramitos/restio
+```
+
+```bash
+$ npm install [--save/--save-dev] restio
+```
+
## usage
### server
@@ -20,11 +32,18 @@ Object.keys(io.clients).forEach(function (id) {
### client
-*only component/component supported at the moment*
-
```js
require('restio').listen('http://localhost:7589', function (io) {
io.get('/user/4', function (user) {})
io.on.post('/user', function (params, query, data, respond) {})
})
-```
+```
+
+## todo
+
+ * api documentation
+ * test suite
+
+## license
+
+MIT
Oops, something went wrong.

0 comments on commit 6ddd646

Please sign in to comment.