Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

switched to browserify

  • Loading branch information...
commit e3cc73b508e8b4102c15f76dfcad79fd4a5136ac 1 parent f03e9ff
@dimsmol authored
Showing with 4,116 additions and 634 deletions.
  1. +12 −5 Makefile
  2. +2 −2 Readme.md
  3. +909 −0 client/apis.amd.js
  4. +1 −0  client/apis.amd.min.js
  5. +923 −0 client/apis.global.js
  6. +1 −0  client/apis.global.min.js
  7. +914 −0 client/apis.js
  8. +1 −0  client/apis.min.js
  9. +0 −12 client/build.min.global.js
  10. +0 −7 client/build.min.js
  11. +3 −0  client/export/amd.js
  12. +8 −0 client/export/browserify.js
  13. +17 −0 client/export/global.js
  14. +0 −1  client/min/apis.min.global.js
  15. +0 −1  client/min/apis.min.js
  16. +0 −385 client/src/almond.js
  17. +0 −71 client/src/apis/error_base.js
  18. +0 −16 client/src/apis/inherits.js
  19. +0 −15 client/src/apis/main.js
  20. +0 −10 lib/client.js
  21. +4 −5 {client/src/apis → lib/client}/errors.js
  22. +4 −5 {client/src/apis → lib/client}/http.js
  23. +3 −5 {client/src/apis → lib/client}/http_request.js
  24. +15 −0 lib/client/index.js
  25. +5 −5 {client/src/apis → lib/client}/jsonp_request.js
  26. +3 −5 {client/src/apis → lib/client}/socket.js
  27. +3 −2 lib/contract.js
  28. +0 −25 lib/test_page.js
  29. +2 −4 {public/static/test_page/js → lib/test_page/client}/init.js
  30. +341 −0 lib/test_page/client/test_page.js
  31. +23 −0 lib/test_page/index.js
  32. +2 −1  package.json
  33. +1 −13 public/static/test_page/index.html
  34. +0 −35 public/static/test_page/js/require.min.js
  35. +919 −4 public/static/test_page/js/test_page.js
View
17 Makefile
@@ -1,7 +1,14 @@
-js_all: js_min js_min_global
+UGLIFY_OPTS = -c 'unused=false' -m
-js_min:
- r.js -o client/build.min.js
+client_all: client_js test_page
-js_min_global:
- r.js -o client/build.min.global.js
+test_page:
+ browserify -e lib/test_page/client/init.js > public/static/test_page/js/test_page.js
+
+client_js:
+ browserify ./client/export/browserify.js > client/apis.js
+ browserify ./client/export/amd.js > client/apis.amd.js
+ browserify ./client/export/global.js > client/apis.global.js
+ uglifyjs client/apis.js $(UGLIFY_OPTS) > client/apis.min.js
+ uglifyjs client/apis.amd.js $(UGLIFY_OPTS) > client/apis.amd.min.js
+ uglifyjs client/apis.global.js $(UGLIFY_OPTS) > client/apis.global.min.js
View
4 Readme.md
@@ -15,10 +15,10 @@ Handler interface:
## Test page
-To get test page on `/test_page/index.html` add to your contract:
+To get test page on `/test_page` add to your contract:
```js
-res.subpaths('/test_page', st(apis.tools.testPage.staticPath))
+cont('/test', [apis.testPage.contract])
```
## Apps known by Loader
View
909 client/apis.amd.js
@@ -0,0 +1,909 @@
+;(function(e,t,n,r){function i(r){if(!n[r]){if(!t[r]){if(e)return e(r);throw new Error("Cannot find module '"+r+"'")}var s=n[r]={exports:{}};t[r][0](function(e){var n=t[r][1][e];return i(n?n:e)},s,s.exports)}return n[r].exports}for(var s=0;s<r.length;s++)i(r[s]);return i})(typeof require!=="undefined"&&require,{1:[function(require,module,exports){
+define('apis', [], function () {
+ return require('../../lib/client');
+});
+
+},{"../../lib/client":2}],2:[function(require,module,exports){
+"use strict";
+var errors = require('./errors');
+var Http = require('./http');
+var HttpRequest = require('./http_request');
+var JsonpRequest = require('./jsonp_request');
+var Socket = require('./socket');
+
+
+module.exports = {
+ errors: errors,
+ Http: Http,
+ HttpRequest: HttpRequest,
+ JsonpRequest: JsonpRequest,
+ Socket: Socket
+};
+
+},{"./errors":3,"./http":4,"./http_request":5,"./jsonp_request":6,"./socket":7}],4:[function(require,module,exports){
+"use strict";
+var HttpRequest = require('./http_request');
+var JsonpRequest = require('./jsonp_request');
+
+
+var Http = function (opt_baseUri) {
+ this.baseUri = opt_baseUri;
+};
+
+Http.prototype.send = function (path, method, headers, data, options, cb) {
+ var result;
+ if (options == null || options.crossDomain == null) {
+ result = this.sendHttp(path, method, headers, data, options, cb);
+ }
+ else {
+ switch (options.crossDomain) {
+ case 'jsonp':
+ result = this.sendJsonp(path, method, headers, data, options, cb);
+ break;
+ default:
+ throw new Error('Unsupported crossDomain option ' + options.crossDomain);
+ }
+ }
+ return result;
+};
+
+Http.prototype.sendHttp = function (path, method, headers, data, options, cb) {
+ return new HttpRequest(this, path, method, headers, data, options, cb).send();
+};
+
+Http.prototype.sendJsonp = function (path, method, headers, data, options, cb) {
+ return new JsonpRequest(this, path, method, headers, data, options, cb).send();
+};
+
+
+module.exports = Http;
+
+},{"./http_request":5,"./jsonp_request":6}],5:[function(require,module,exports){
+"use strict";
+var errors = require('./errors');
+
+
+var HttpRequest = function (http, path, method, headers, data, options, cb) {
+ this.http = http;
+ this.options = options || {};
+ this.path = path;
+ this.method = method;
+ this.headers = headers || {};
+ this.data = data;
+ this.cb = cb;
+
+ this.url = null;
+ this.body = null;
+ this.transport = null;
+ this.isAborted = false;
+ this.timeout = null;
+};
+
+HttpRequest.prototype.methodHttpHeaderName = 'X-Method';
+HttpRequest.prototype.authHttpHeaderName = 'X-Auth';
+HttpRequest.prototype.authExpectedHttpHeaderName = 'X-AuthExpected';
+HttpRequest.prototype.authRenewalHttpHeaderName = 'X-AuthRenewal';
+HttpRequest.prototype.authRenewalIssuedHttpHeaderName = 'X-AuthRenewalIssued';
+HttpRequest.prototype.authRenewalMaxAgeHttpHeaderName = 'X-AuthRenewalMaxAge';
+
+HttpRequest.prototype.headersUrlKey = 'headers';
+HttpRequest.prototype.bodyUrlKey = 'body';
+HttpRequest.prototype.xdomainUrlKey = 'xdomain';
+
+HttpRequest.prototype.defaultContentType = 'application/json';
+
+HttpRequest.prototype.send = function () {
+ this.createTimeout();
+ this.url = this.createUrl();
+ this.body = this.createBody();
+ this.transport = this.createTransport();
+ this.createCallback();
+ this.sendInternal();
+ return this;
+};
+
+HttpRequest.prototype.abort = function () {
+ this.isAborted = true;
+ this.clearTimeout();
+ if (this.transport != null) {
+ this.transport.onreadystatechange = null;
+ this.transport.abort();
+ }
+};
+
+HttpRequest.prototype.createTimeout = function () {
+ var timeout = this.options.timeout;
+ if (timeout != null) {
+ var self = this;
+ this.timeout = setTimeout(function () {
+ self.handleTimeout();
+ }, timeout);
+ }
+};
+
+HttpRequest.prototype.handleTimeout = function () {
+ this.abort();
+ this.cb(new errors.TimeoutError());
+};
+
+HttpRequest.prototype.clearTimeout = function () {
+ if (this.timeout != null) {
+ clearTimeout(this.timeout);
+ }
+};
+
+HttpRequest.prototype.createUrl = function () {
+ var result = this.path;
+ if (this.http.baseUri) {
+ result = this.http.baseUri + this.path;
+ }
+ return result;
+};
+
+HttpRequest.prototype.createBody = function () {
+ var result;
+ if (this.data === undefined) {
+ result = '';
+ }
+ else {
+ result = JSON.stringify(this.data);
+ }
+ return result;
+};
+
+HttpRequest.prototype.createTransport = function () {
+ return new XMLHttpRequest();
+};
+
+HttpRequest.prototype.createCallback = function () {
+ var self = this;
+ // NOTE use onload/onerror pair instead of
+ // onreadystatechange in future (will break IE8 support)
+ this.transport.onreadystatechange = function() {
+ if(self.transport.readyState == 4) {
+ self.transport.onreadystatechange = null;
+ if (self.isNetworkError()) {
+ self.handleNetworkError();
+ }
+ else {
+ self.handleResponse();
+ }
+ }
+ };
+};
+
+HttpRequest.prototype.isNetworkError = function () {
+ // NOTE works incorrectly with file:// protocol
+ return !this.transport.status;
+};
+
+HttpRequest.prototype.handleNetworkError = function () {
+ this.clearTimeout();
+ this.cb(new errors.NetworkError());
+};
+
+HttpRequest.prototype.sendInternal = function () {
+ if (this.method == 'get') {
+ this.sendGet();
+ }
+ else {
+ this.sendPost();
+ }
+};
+
+HttpRequest.prototype.sendGet = function () {
+ this.transport.open('GET', this.createGetUrl());
+ this.setHttpHeaders();
+ this.transport.send();
+};
+
+HttpRequest.prototype.createGetUrl = function () {
+ var parts = this.createGetUrlParts();
+ var result = this.url;
+ if (parts.length > 0) {
+ result += '?' + parts.join('&');
+ }
+ return result;
+};
+
+HttpRequest.prototype.createGetUrlParts = function (opt_parts) {
+ var parts = opt_parts || [];
+ if (this.body) {
+ parts.push(this.bodyUrlKey + '=' + encodeURIComponent(this.body));
+ }
+ return parts;
+};
+
+HttpRequest.prototype.sendPost = function () {
+ var transport = this.transport;
+ transport.open('POST', this.url);
+ this.setContentTypeHttpHeader();
+ this.setMethodHttpHeader();
+ this.setHttpHeaders();
+ transport.send(this.body);
+};
+
+HttpRequest.prototype.setHttpHeaders = function () {
+ var headers = this.createHttpHeaders();
+ var transport = this.transport;
+ for (var k in headers) {
+ transport.setRequestHeader(k, headers[k]);
+ }
+};
+
+HttpRequest.prototype.createHttpHeaders = function () {
+ var headers = this.headers;
+ var result = headers.http || {};
+ delete headers.http;
+ if (headers.auth) {
+ result[this.authHttpHeaderName] = headers.auth;
+ }
+ if (headers.authExpected) {
+ result[this.authExpectedHttpHeaderName] = headers.authExpected;
+ }
+ return result;
+};
+
+HttpRequest.prototype.setContentTypeHttpHeader = function () {
+ var contentType = this.headers.http && this.headers.http['Content-type'] || this.headers.contentType || this.defaultContentType;
+ this.transport.setRequestHeader('Content-type', contentType);
+};
+
+HttpRequest.prototype.setMethodHttpHeader = function () {
+ if (this.method != 'create') {
+ this.transport.setRequestHeader(this.methodHttpHeaderName, this.method);
+ }
+};
+
+HttpRequest.prototype.handleResponse = function () {
+ this.clearTimeout();
+ var result = this.createResult();
+ var err = this.extractError(result);
+ if (err != null) {
+ this.cb(err);
+ }
+ else {
+ this.cb(null, result);
+ }
+};
+
+HttpRequest.prototype.createResult = function () {
+ var result = {
+ headers: this.getResultHeaders(),
+ data: this.getResultData()
+ };
+ var status = this.getResultStatus();
+ if (status != null) {
+ result.status = status;
+ }
+ var transport = this.getTransportForResult();
+ if (transport != null) {
+ result.transport = transport;
+ }
+ return result;
+};
+
+HttpRequest.prototype.getTransportForResult = function () {
+ return this.transport;
+};
+
+HttpRequest.prototype.getResultStatus = function () {
+ return this.transport.status;
+};
+
+HttpRequest.prototype.getResultHeaders = function () {
+ var result = {};
+ var authRenewalToken = this.transport.getResponseHeader(this.authRenewalHttpHeaderName);
+ if (authRenewalToken) {
+ var renewal = {
+ token: authRenewalToken
+ };
+ result.authRenewal = renewal;
+ var issued = this.transport.getResponseHeader(this.authRenewalIssuedHttpHeaderName);
+ if (issued) {
+ renewal.issued = new Date(issued);
+ }
+ var maxAge = this.transport.getResponseHeader(this.authRenewalMaxAgeHttpHeaderName);
+ if (maxAge) {
+ renewal.maxAge = parseInt(maxAge, 10);
+ }
+ }
+ return result;
+};
+
+HttpRequest.prototype.getResultData = function () {
+ var body = this.getResultBody();
+ return body ? JSON.parse(body) : undefined;
+};
+
+HttpRequest.prototype.getResultBody = function () {
+ return this.transport.responseText;
+};
+
+HttpRequest.prototype.extractError = function (result) {
+ var status = result.status;
+ var err = null;
+ if (status != 200 && status != 204) {
+ err = new errors.WebError(result);
+ }
+ return err;
+};
+
+
+module.exports = HttpRequest;
+
+},{"./errors":3}],7:[function(require,module,exports){
+"use strict";
+var errors = require('./errors');
+
+
+var Socket = function (opt_basePath, opt_socketUri, opt_protocols) {
+ this.basePath = opt_basePath;
+ this.socketUri = opt_socketUri || this.defaultSocketUri;
+ this.protocols = opt_protocols;
+
+ this.onSocketCreated = null;
+ this.onMessage = null;
+
+ // NOTE you can set this property to use sockjs or whatever
+ this.customWebSocketClass = null;
+
+ this.socket = null;
+ this.requests = {};
+
+ this.abortFunc = this.createAbortFunc();
+};
+
+Socket.prototype.defaultSocketUri = '/socket';
+Socket.prototype.headerSeparator = '\n\n';
+Socket.prototype.defaultTimeout = null;
+
+Socket.prototype.getWebSocketClass = function () {
+ return this.customWebSocketClass || WebSocket;
+};
+
+Socket.prototype.createAbortFunc = function () {
+ var self = this;
+ return function () {
+ self.handleAbort(this);
+ };
+};
+
+Socket.prototype.createSocket = function () {
+ var WebSocketClass = this.getWebSocketClass();
+ return new WebSocketClass(this.socketUri, this.protocols);
+};
+
+Socket.prototype.connect = function (opt_cb) {
+ if (this.socket != null) {
+ switch (this.socket.readyState) {
+ case 0: // connecting
+ if (opt_cb) {
+ this.socket.addEventListener('open', function () {
+ opt_cb();
+ });
+ }
+ break;
+ case 1: // open
+ if (opt_cb) {
+ opt_cb();
+ }
+ break;
+ default: // closing or closed
+ this.socket = null;
+ }
+ }
+ if (this.socket == null) {
+ this.socket = this.createSocket();
+
+ if (this.onSocketCreated) {
+ this.onSocketCreated(this.socket);
+ }
+
+ if (opt_cb) {
+ this.socket.addEventListener('open', function () {
+ opt_cb();
+ });
+ }
+
+ var self = this;
+ this.socket.addEventListener('message', function (ev) {
+ self.handleMessage(ev);
+ });
+
+ this.socket.addEventListener('close', function (ev) {
+ self.handleClose(ev);
+ });
+ }
+};
+
+Socket.prototype.handleMessage = function (ev) {
+ var result = this.parse(ev.data);
+ var requestId = (result.headers != null ? result.headers.requestId : null);
+ if (requestId != null) {
+ var request = this.requests[requestId];
+ if (request) {
+ var cb = request.cb;
+ this.clearRequest(request, requestId);
+ var err = this.extractError(result);
+ if (err != null) {
+ cb(err);
+ }
+ else {
+ cb(null, result);
+ }
+ }
+ }
+ else if (this.onMessage != null) {
+ this.onMessage(result);
+ }
+};
+
+Socket.prototype.createRequestId = function () {
+ var base = '' + (new Date().getTime());
+ var i = 0;
+ var result = base;
+ while (result in this.requests) {
+ result = base + '_' + (i++);
+ }
+ return result;
+};
+
+Socket.prototype.clearRequest = function (request) {
+ delete this.requests[request.id];
+ var timeout = request.timeout;
+ if (timeout != null) {
+ clearTimeout(timeout);
+ }
+};
+
+Socket.prototype.close = function () {
+ if (this.socket != null) {
+ this.socket.close();
+ }
+};
+
+Socket.prototype.send = function (path, method, headers, data, opt_options, opt_cb) {
+ var self = this;
+ this.connect(function () {
+ self.sendInternal(path, method, headers, data, opt_options, opt_cb);
+ });
+};
+
+Socket.prototype.sendInternal = function (path, method, headers, data, opt_options, opt_cb) {
+ headers = headers || {};
+
+ headers.method = method;
+ headers.path = this.getEffectivePath(path);
+
+ var request;
+ if (opt_cb) {
+ request = this.createRequest(opt_options, opt_cb);
+ headers.requestId = request.id;
+ }
+
+ var body;
+ if (data === undefined) {
+ body = '';
+ }
+ else {
+ body = JSON.stringify(data);
+ }
+
+ var msg = [
+ JSON.stringify(headers),
+ body
+ ].join(this.headerSeparator);
+
+ this.socket.send(msg);
+ return request;
+};
+
+Socket.prototype.getEffectivePath = function (path) {
+ var result = path;
+ if (this.basePath) {
+ result = this.basePath + path;
+ }
+ return result;
+};
+
+Socket.prototype.createRequest = function (options, cb) {
+ var requestId = this.createRequestId();
+ var request = {
+ id: requestId,
+ cb: cb,
+ options: options,
+ abort: this.abortFunc
+ };
+ request.timeout = this.createTimeout(request);
+ this.requests[requestId] = request;
+ return request;
+};
+
+Socket.prototype.createTimeout = function (request) {
+ var options = request.options;
+ var result = null;
+ var timeout = (options != null && options.timeout != null ? options.timeout : this.defaultTimeout);
+ if (timeout != null) {
+ var self = this;
+ result = setTimeout(function () {
+ self.handleTimeout(request);
+ }, timeout);
+ }
+ return result;
+};
+
+Socket.prototype.handleTimeout = function (request) {
+ var cb = request.cb;
+ this.clearRequest(request);
+ cb(new errors.TimeoutError());
+};
+
+Socket.prototype.handleAbort = function (request) {
+ this.clearRequest(request);
+};
+
+Socket.prototype.handleClose = function (ev) {
+ for (var k in this.requests) {
+ var request = this.requests[k];
+ var cb = request.cb;
+ this.clearRequest(request);
+ cb(new errors.ConnectionCloseError(ev));
+ }
+};
+
+Socket.prototype.parse = function (message) {
+ var sepPos = message.indexOf(this.headerSeparator);
+ var headersStr = message.substring(0, sepPos);
+ var body = message.substring(sepPos + this.headerSeparator.length);
+
+ var headers = JSON.parse(headersStr);
+
+ var result = {
+ headers: headers,
+ data: this.parseBody(body)
+ };
+
+ if (headers.status != null) {
+ result.status = headers.status;
+ }
+
+ return result;
+};
+
+Socket.prototype.parseBody = function (body) {
+ var result;
+ if (body) {
+ result = JSON.parse(body);
+ }
+ return result;
+};
+
+Socket.prototype.extractError = function (result) {
+ var status = result.status;
+ var err = null;
+ if (status != 200 && status != 204) {
+ err = new errors.WebError(result);
+ }
+ return err;
+};
+
+
+module.exports = Socket;
+
+},{"./errors":3}],3:[function(require,module,exports){
+"use strict";
+var inherits = require('inh');
+var ErrorBase = require('nerr/lib/error_base');
+
+
+var WebError = function (response) {
+ ErrorBase.call(this);
+
+ this.response = response;
+
+ this.status = response.status;
+ var data = response.data || {};
+ this._message = data.message;
+ this.status = response.status;
+ this.code = data.code;
+};
+inherits(WebError, ErrorBase);
+
+WebError.prototype.name = 'WebError';
+
+WebError.prototype.getMessage = function () {
+ return this._message;
+};
+
+
+var NetworkError = function () {
+ ErrorBase.call(this);
+};
+inherits(NetworkError, ErrorBase);
+
+NetworkError.prototype.name = 'NetworkError';
+
+
+var TimeoutError = function () {
+ ErrorBase.call(this);
+};
+inherits(TimeoutError, ErrorBase);
+
+TimeoutError.prototype.name = 'TimeoutError';
+
+
+var ConnectionCloseError = function (closeEvent) {
+ ErrorBase.call(this);
+ this.closeEvent = closeEvent;
+};
+inherits(ConnectionCloseError, ErrorBase);
+
+ConnectionCloseError.prototype.name = 'ConnectionCloseError';
+
+ConnectionCloseError.prototype.getMessage = function () {
+ return this.closeEvent.reason;
+};
+
+
+module.exports = {
+ WebError: WebError,
+ NetworkError: NetworkError,
+ TimeoutError: TimeoutError,
+ ConnectionCloseError: ConnectionCloseError
+};
+
+},{"nerr/lib/error_base":8,"inh":9}],6:[function(require,module,exports){
+"use strict";
+var inherits = require('inh');
+var HttpRequest = require('./http_request');
+var errors = require('./errors');
+
+
+var JsonpRequest = function (http, path, method, headers, data, options, cb) {
+ HttpRequest.call(this, http, path, method, headers, data, options, cb);
+ this.callbacks = this.getCallbacks();
+ this.scriptEl = null;
+ this.callbackId = null;
+ this.response = null;
+};
+inherits(JsonpRequest, HttpRequest);
+
+JsonpRequest.callbacks = null;
+
+JsonpRequest.prototype.callbacksGlobalPath = 'apis.jsonp.callbacks';
+JsonpRequest.prototype.xdomainValue = 'jsonp';
+JsonpRequest.prototype.jsonpCallbackUrlKey = 'callback';
+
+JsonpRequest.prototype.abort = function () {
+ this.isAborted = true;
+ this.clearTimeout();
+};
+
+JsonpRequest.prototype.getCallbacks = function () {
+ if (JsonpRequest.callbacks == null) {
+ var parts = this.callbacksGlobalPath.split('.');
+ var curr = window;
+ for (var i = 0; i < parts.length; i++) {
+ var part = parts[i];
+ var next = curr[part] || {};
+ curr[part] = next;
+ curr = next;
+ }
+ JsonpRequest.callbacks = curr;
+ }
+ return JsonpRequest.callbacks;
+};
+
+JsonpRequest.prototype.createTransport = function () {
+ var el = document.createElement('script');
+ el.async = true;
+ var self = this;
+ el.onerror = function (ev) {
+ self.handleScriptErrorEvent(ev);
+ };
+ this.scriptEl = el;
+ return el;
+};
+
+JsonpRequest.prototype.handleScriptErrorEvent = function (ev) {
+ this.cleanup();
+ if (!this.isAborted) {
+ this.cb(new errors.NetworkError());
+ }
+};
+
+JsonpRequest.prototype.createCallback = function () {
+ var self = this;
+ this.callbackId = this.createCallbackId();
+ this.callbacks[this.callbackId] = function (response) {
+ self.response = response;
+ self.handleResponse();
+ };
+};
+
+JsonpRequest.prototype.createCallbackId = function () {
+ var base = 'cb' + (new Date().getTime());
+ var i = 0;
+ var result = base;
+ while (result in this.callbacks) {
+ result = base + '_' + (i++);
+ }
+ return result;
+};
+
+JsonpRequest.prototype.sendInternal = function () {
+ this.scriptEl.src = this.createGetUrl();
+
+ var head = document.head || document.getElementsByTagName('head')[0] || document.documentElement;
+ var firstChild = head.firstChild;
+ if (firstChild) {
+ head.insertBefore(this.scriptEl, head.firstChild);
+ }
+ else {
+ head.appendChild(this.scriptEl);
+ }
+};
+
+JsonpRequest.prototype.createGetUrlParts = function (opt_parts) {
+ var parts = opt_parts || [];
+ var headers = this.headers;
+ if (this.method != 'get') {
+ headers = headers || {};
+ headers.method = this.method;
+ }
+ if (headers) {
+ parts.push(this.headersUrlKey + '=' + encodeURIComponent(JSON.stringify(headers)));
+ }
+ JsonpRequest.super_.prototype.createGetUrlParts.call(this, parts);
+ parts.push(this.xdomainUrlKey + '=' + this.xdomainValue);
+ parts.push(this.jsonpCallbackUrlKey + '=' + encodeURIComponent(this.createCallbackName()));
+ return parts;
+};
+
+JsonpRequest.prototype.createCallbackName = function () {
+ return this.callbacksGlobalPath + '.' + this.callbackId;
+};
+
+JsonpRequest.prototype.setHttpHeaders = function () {
+};
+
+JsonpRequest.prototype.handleResponse = function () {
+ this.cleanup();
+ if (!this.isAborted) {
+ JsonpRequest.super_.prototype.handleResponse.call(this);
+ }
+};
+
+JsonpRequest.prototype.cleanup = function () {
+ this.clearTimeout();
+ this.removeCallback();
+ this.removeScripEl();
+};
+
+JsonpRequest.prototype.removeCallback = function () {
+ delete this.callbacks[this.callbackId];
+};
+
+JsonpRequest.prototype.removeScripEl = function () {
+ var scriptEl = this.scriptEl;
+ if (scriptEl != null && scriptEl.parentNode) {
+ scriptEl.parentNode.removeChild(scriptEl);
+ this.scriptEl = null;
+ }
+};
+
+JsonpRequest.prototype.getTransportForResult = function () {
+ return null;
+};
+
+JsonpRequest.prototype.getResultStatus = function () {
+ var headers = this.getResultHeaders();
+ return headers.status;
+};
+
+JsonpRequest.prototype.getResultHeaders = function () {
+ return this.response.headers || {};
+};
+
+JsonpRequest.prototype.getResultData = function () {
+ return this.response.data;
+};
+
+
+module.exports = JsonpRequest;
+
+},{"./http_request":5,"./errors":3,"inh":9}],9:[function(require,module,exports){
+"use strict";
+var inherits = function(childCtor, parentCtor) {
+ var TempCtor = function () {};
+ TempCtor.prototype = parentCtor.prototype;
+ childCtor.super_ = parentCtor;
+ childCtor.prototype = new TempCtor();
+ childCtor.prototype.constructor = childCtor;
+};
+
+
+module.exports = inherits;
+
+},{}],8:[function(require,module,exports){
+"use strict";
+var inherits = require('inh');
+
+
+var ErrorBase = function () {
+ Error.call(this);
+ this.captureStackTrace();
+};
+inherits(ErrorBase, Error);
+
+ErrorBase.prototype.name = 'ErrorBase';
+
+ErrorBase.prototype.captureStackTrace = function () {
+ if (Error.captureStackTrace) {
+ Error.captureStackTrace(this, this.constructor);
+ }
+ else {
+ var stackKeeper = new Error();
+ var self = this;
+ stackKeeper.toString = function () { return self.toString(); };
+ var getStackTrace = function () {
+ return stackKeeper.stack;
+ };
+
+ if (Object.defineProperties) {
+ Object.defineProperties({
+ stack: getStackTrace
+ });
+ }
+ else {
+ this.getStackTrace = getStackTrace;
+ }
+ }
+};
+
+ErrorBase.prototype.toString = function () {
+ var result = this.name;
+ var message = this.getMessage();
+ if (message) {
+ result = [result, message].join(': ');
+ }
+ return result;
+};
+
+ErrorBase.prototype.getMessage = function () {
+ return null;
+};
+
+ErrorBase.prototype.getStackTrace = function () {
+ return this.stack;
+};
+
+if (Object.defineProperties) {
+ Object.defineProperties(ErrorBase.prototype, {
+ message: {
+ get: function () {
+ return this.getMessage();
+ }
+ }
+ });
+}
+
+
+module.exports = ErrorBase;
+
+},{"inh":10}],10:[function(require,module,exports){
+"use strict";
+var inherits = function(childCtor, parentCtor) {
+ var TempCtor = function () {};
+ TempCtor.prototype = parentCtor.prototype;
+ childCtor.super_ = parentCtor;
+ childCtor.prototype = new TempCtor();
+ childCtor.prototype.constructor = childCtor;
+};
+
+
+module.exports = inherits;
+
+},{}]},{},[1])
+;
View
1  client/apis.amd.min.js
@@ -0,0 +1 @@
+(function(t,e,r,o){function s(o){if(!r[o]){if(!e[o]){if(t)return t(o);throw Error("Cannot find module '"+o+"'")}var n=r[o]={exports:{}};e[o][0](function(t){var r=e[o][1][t];return s(r?r:t)},n,n.exports)}return r[o].exports}for(var n=0;o.length>n;n++)s(o[n]);return s})("undefined"!=typeof require&&require,{1:[function(t,e,r){define("apis",[],function(){return t("../../lib/client")})},{"../../lib/client":2}],2:[function(t,e,r){"use strict";var o=t("./errors"),s=t("./http"),n=t("./http_request"),a=t("./jsonp_request"),i=t("./socket");e.exports={errors:o,Http:s,HttpRequest:n,JsonpRequest:a,Socket:i}},{"./errors":3,"./http":4,"./http_request":5,"./jsonp_request":6,"./socket":7}],4:[function(t,e,r){"use strict";var o=t("./http_request"),s=t("./jsonp_request"),n=function(t){this.baseUri=t};n.prototype.send=function(t,e,r,o,s,n){var a;if(null==s||null==s.crossDomain)a=this.sendHttp(t,e,r,o,s,n);else switch(s.crossDomain){case"jsonp":a=this.sendJsonp(t,e,r,o,s,n);break;default:throw Error("Unsupported crossDomain option "+s.crossDomain)}return a},n.prototype.sendHttp=function(t,e,r,s,n,a){return new o(this,t,e,r,s,n,a).send()},n.prototype.sendJsonp=function(t,e,r,o,n,a){return new s(this,t,e,r,o,n,a).send()},e.exports=n},{"./http_request":5,"./jsonp_request":6}],5:[function(t,e,r){"use strict";var o=t("./errors"),s=function(t,e,r,o,s,n,a){this.http=t,this.options=n||{},this.path=e,this.method=r,this.headers=o||{},this.data=s,this.cb=a,this.url=null,this.body=null,this.transport=null,this.isAborted=!1,this.timeout=null};s.prototype.methodHttpHeaderName="X-Method",s.prototype.authHttpHeaderName="X-Auth",s.prototype.authExpectedHttpHeaderName="X-AuthExpected",s.prototype.authRenewalHttpHeaderName="X-AuthRenewal",s.prototype.authRenewalIssuedHttpHeaderName="X-AuthRenewalIssued",s.prototype.authRenewalMaxAgeHttpHeaderName="X-AuthRenewalMaxAge",s.prototype.headersUrlKey="headers",s.prototype.bodyUrlKey="body",s.prototype.xdomainUrlKey="xdomain",s.prototype.defaultContentType="application/json",s.prototype.send=function(){return this.createTimeout(),this.url=this.createUrl(),this.body=this.createBody(),this.transport=this.createTransport(),this.createCallback(),this.sendInternal(),this},s.prototype.abort=function(){this.isAborted=!0,this.clearTimeout(),null!=this.transport&&(this.transport.onreadystatechange=null,this.transport.abort())},s.prototype.createTimeout=function(){var t=this.options.timeout;if(null!=t){var e=this;this.timeout=setTimeout(function(){e.handleTimeout()},t)}},s.prototype.handleTimeout=function(){this.abort(),this.cb(new o.TimeoutError)},s.prototype.clearTimeout=function(){null!=this.timeout&&clearTimeout(this.timeout)},s.prototype.createUrl=function(){var t=this.path;return this.http.baseUri&&(t=this.http.baseUri+this.path),t},s.prototype.createBody=function(){var t;return t=void 0===this.data?"":JSON.stringify(this.data)},s.prototype.createTransport=function(){return new XMLHttpRequest},s.prototype.createCallback=function(){var t=this;this.transport.onreadystatechange=function(){4==t.transport.readyState&&(t.transport.onreadystatechange=null,t.isNetworkError()?t.handleNetworkError():t.handleResponse())}},s.prototype.isNetworkError=function(){return!this.transport.status},s.prototype.handleNetworkError=function(){this.clearTimeout(),this.cb(new o.NetworkError)},s.prototype.sendInternal=function(){"get"==this.method?this.sendGet():this.sendPost()},s.prototype.sendGet=function(){this.transport.open("GET",this.createGetUrl()),this.setHttpHeaders(),this.transport.send()},s.prototype.createGetUrl=function(){var t=this.createGetUrlParts(),e=this.url;return t.length>0&&(e+="?"+t.join("&")),e},s.prototype.createGetUrlParts=function(t){var e=t||[];return this.body&&e.push(this.bodyUrlKey+"="+encodeURIComponent(this.body)),e},s.prototype.sendPost=function(){var t=this.transport;t.open("POST",this.url),this.setContentTypeHttpHeader(),this.setMethodHttpHeader(),this.setHttpHeaders(),t.send(this.body)},s.prototype.setHttpHeaders=function(){var t=this.createHttpHeaders(),e=this.transport;for(var r in t)e.setRequestHeader(r,t[r])},s.prototype.createHttpHeaders=function(){var t=this.headers,e=t.http||{};return delete t.http,t.auth&&(e[this.authHttpHeaderName]=t.auth),t.authExpected&&(e[this.authExpectedHttpHeaderName]=t.authExpected),e},s.prototype.setContentTypeHttpHeader=function(){var t=this.headers.http&&this.headers.http["Content-type"]||this.headers.contentType||this.defaultContentType;this.transport.setRequestHeader("Content-type",t)},s.prototype.setMethodHttpHeader=function(){"create"!=this.method&&this.transport.setRequestHeader(this.methodHttpHeaderName,this.method)},s.prototype.handleResponse=function(){this.clearTimeout();var t=this.createResult(),e=this.extractError(t);null!=e?this.cb(e):this.cb(null,t)},s.prototype.createResult=function(){var t={headers:this.getResultHeaders(),data:this.getResultData()},e=this.getResultStatus();null!=e&&(t.status=e);var r=this.getTransportForResult();return null!=r&&(t.transport=r),t},s.prototype.getTransportForResult=function(){return this.transport},s.prototype.getResultStatus=function(){return this.transport.status},s.prototype.getResultHeaders=function(){var t={},e=this.transport.getResponseHeader(this.authRenewalHttpHeaderName);if(e){var r={token:e};t.authRenewal=r;var o=this.transport.getResponseHeader(this.authRenewalIssuedHttpHeaderName);o&&(r.issued=new Date(o));var s=this.transport.getResponseHeader(this.authRenewalMaxAgeHttpHeaderName);s&&(r.maxAge=parseInt(s,10))}return t},s.prototype.getResultData=function(){var t=this.getResultBody();return t?JSON.parse(t):void 0},s.prototype.getResultBody=function(){return this.transport.responseText},s.prototype.extractError=function(t){var e=t.status,r=null;return 200!=e&&204!=e&&(r=new o.WebError(t)),r},e.exports=s},{"./errors":3}],7:[function(t,e,r){"use strict";var o=t("./errors"),s=function(t,e,r){this.basePath=t,this.socketUri=e||this.defaultSocketUri,this.protocols=r,this.onSocketCreated=null,this.onMessage=null,this.customWebSocketClass=null,this.socket=null,this.requests={},this.abortFunc=this.createAbortFunc()};s.prototype.defaultSocketUri="/socket",s.prototype.headerSeparator="\n\n",s.prototype.defaultTimeout=null,s.prototype.getWebSocketClass=function(){return this.customWebSocketClass||WebSocket},s.prototype.createAbortFunc=function(){var t=this;return function(){t.handleAbort(this)}},s.prototype.createSocket=function(){var t=this.getWebSocketClass();return new t(this.socketUri,this.protocols)},s.prototype.connect=function(t){if(null!=this.socket)switch(this.socket.readyState){case 0:t&&this.socket.addEventListener("open",function(){t()});break;case 1:t&&t();break;default:this.socket=null}if(null==this.socket){this.socket=this.createSocket(),this.onSocketCreated&&this.onSocketCreated(this.socket),t&&this.socket.addEventListener("open",function(){t()});var e=this;this.socket.addEventListener("message",function(t){e.handleMessage(t)}),this.socket.addEventListener("close",function(t){e.handleClose(t)})}},s.prototype.handleMessage=function(t){var e=this.parse(t.data),r=null!=e.headers?e.headers.requestId:null;if(null!=r){var o=this.requests[r];if(o){var s=o.cb;this.clearRequest(o,r);var n=this.extractError(e);null!=n?s(n):s(null,e)}}else null!=this.onMessage&&this.onMessage(e)},s.prototype.createRequestId=function(){for(var t=""+(new Date).getTime(),e=0,r=t;r in this.requests;)r=t+"_"+e++;return r},s.prototype.clearRequest=function(t){delete this.requests[t.id];var e=t.timeout;null!=e&&clearTimeout(e)},s.prototype.close=function(){null!=this.socket&&this.socket.close()},s.prototype.send=function(t,e,r,o,s,n){var a=this;this.connect(function(){a.sendInternal(t,e,r,o,s,n)})},s.prototype.sendInternal=function(t,e,r,o,s,n){r=r||{},r.method=e,r.path=this.getEffectivePath(t);var a;n&&(a=this.createRequest(s,n),r.requestId=a.id);var i;i=void 0===o?"":JSON.stringify(o);var p=[JSON.stringify(r),i].join(this.headerSeparator);return this.socket.send(p),a},s.prototype.getEffectivePath=function(t){var e=t;return this.basePath&&(e=this.basePath+t),e},s.prototype.createRequest=function(t,e){var r=this.createRequestId(),o={id:r,cb:e,options:t,abort:this.abortFunc};return o.timeout=this.createTimeout(o),this.requests[r]=o,o},s.prototype.createTimeout=function(t){var e=t.options,r=null,o=null!=e&&null!=e.timeout?e.timeout:this.defaultTimeout;if(null!=o){var s=this;r=setTimeout(function(){s.handleTimeout(t)},o)}return r},s.prototype.handleTimeout=function(t){var e=t.cb;this.clearRequest(t),e(new o.TimeoutError)},s.prototype.handleAbort=function(t){this.clearRequest(t)},s.prototype.handleClose=function(t){for(var e in this.requests){var r=this.requests[e],s=r.cb;this.clearRequest(r),s(new o.ConnectionCloseError(t))}},s.prototype.parse=function(t){var e=t.indexOf(this.headerSeparator),r=t.substring(0,e),o=t.substring(e+this.headerSeparator.length),s=JSON.parse(r),n={headers:s,data:this.parseBody(o)};return null!=s.status&&(n.status=s.status),n},s.prototype.parseBody=function(t){var e;return t&&(e=JSON.parse(t)),e},s.prototype.extractError=function(t){var e=t.status,r=null;return 200!=e&&204!=e&&(r=new o.WebError(t)),r},e.exports=s},{"./errors":3}],3:[function(t,e,r){"use strict";var o=t("inh"),s=t("nerr/lib/error_base"),n=function(t){s.call(this),this.response=t,this.status=t.status;var e=t.data||{};this._message=e.message,this.status=t.status,this.code=e.code};o(n,s),n.prototype.name="WebError",n.prototype.getMessage=function(){return this._message};var a=function(){s.call(this)};o(a,s),a.prototype.name="NetworkError";var i=function(){s.call(this)};o(i,s),i.prototype.name="TimeoutError";var p=function(t){s.call(this),this.closeEvent=t};o(p,s),p.prototype.name="ConnectionCloseError",p.prototype.getMessage=function(){return this.closeEvent.reason},e.exports={WebError:n,NetworkError:a,TimeoutError:i,ConnectionCloseError:p}},{"nerr/lib/error_base":8,inh:9}],6:[function(t,e,r){"use strict";var o=t("inh"),s=t("./http_request"),n=t("./errors"),a=function(t,e,r,o,n,a,i){s.call(this,t,e,r,o,n,a,i),this.callbacks=this.getCallbacks(),this.scriptEl=null,this.callbackId=null,this.response=null};o(a,s),a.callbacks=null,a.prototype.callbacksGlobalPath="apis.jsonp.callbacks",a.prototype.xdomainValue="jsonp",a.prototype.jsonpCallbackUrlKey="callback",a.prototype.abort=function(){this.isAborted=!0,this.clearTimeout()},a.prototype.getCallbacks=function(){if(null==a.callbacks){for(var t=this.callbacksGlobalPath.split("."),e=window,r=0;t.length>r;r++){var o=t[r],s=e[o]||{};e[o]=s,e=s}a.callbacks=e}return a.callbacks},a.prototype.createTransport=function(){var t=document.createElement("script");t.async=!0;var e=this;return t.onerror=function(t){e.handleScriptErrorEvent(t)},this.scriptEl=t,t},a.prototype.handleScriptErrorEvent=function(t){this.cleanup(),this.isAborted||this.cb(new n.NetworkError)},a.prototype.createCallback=function(){var t=this;this.callbackId=this.createCallbackId(),this.callbacks[this.callbackId]=function(e){t.response=e,t.handleResponse()}},a.prototype.createCallbackId=function(){for(var t="cb"+(new Date).getTime(),e=0,r=t;r in this.callbacks;)r=t+"_"+e++;return r},a.prototype.sendInternal=function(){this.scriptEl.src=this.createGetUrl();var t=document.head||document.getElementsByTagName("head")[0]||document.documentElement,e=t.firstChild;e?t.insertBefore(this.scriptEl,t.firstChild):t.appendChild(this.scriptEl)},a.prototype.createGetUrlParts=function(t){var e=t||[],r=this.headers;return"get"!=this.method&&(r=r||{},r.method=this.method),r&&e.push(this.headersUrlKey+"="+encodeURIComponent(JSON.stringify(r))),a.super_.prototype.createGetUrlParts.call(this,e),e.push(this.xdomainUrlKey+"="+this.xdomainValue),e.push(this.jsonpCallbackUrlKey+"="+encodeURIComponent(this.createCallbackName())),e},a.prototype.createCallbackName=function(){return this.callbacksGlobalPath+"."+this.callbackId},a.prototype.setHttpHeaders=function(){},a.prototype.handleResponse=function(){this.cleanup(),this.isAborted||a.super_.prototype.handleResponse.call(this)},a.prototype.cleanup=function(){this.clearTimeout(),this.removeCallback(),this.removeScripEl()},a.prototype.removeCallback=function(){delete this.callbacks[this.callbackId]},a.prototype.removeScripEl=function(){var t=this.scriptEl;null!=t&&t.parentNode&&(t.parentNode.removeChild(t),this.scriptEl=null)},a.prototype.getTransportForResult=function(){return null},a.prototype.getResultStatus=function(){var t=this.getResultHeaders();return t.status},a.prototype.getResultHeaders=function(){return this.response.headers||{}},a.prototype.getResultData=function(){return this.response.data},e.exports=a},{"./http_request":5,"./errors":3,inh:9}],9:[function(t,e,r){"use strict";var o=function(t,e){var r=function(){};r.prototype=e.prototype,t.super_=e,t.prototype=new r,t.prototype.constructor=t};e.exports=o},{}],8:[function(t,e,r){"use strict";var o=t("inh"),s=function(){Error.call(this),this.captureStackTrace()};o(s,Error),s.prototype.name="ErrorBase",s.prototype.captureStackTrace=function(){if(Error.captureStackTrace)Error.captureStackTrace(this,this.constructor);else{var t=Error(),e=this;t.toString=function(){return""+e};var r=function(){return t.stack};Object.defineProperties?Object.defineProperties({stack:r}):this.getStackTrace=r}},s.prototype.toString=function(){var t=this.name,e=this.getMessage();return e&&(t=[t,e].join(": ")),t},s.prototype.getMessage=function(){return null},s.prototype.getStackTrace=function(){return this.stack},Object.defineProperties&&Object.defineProperties(s.prototype,{message:{get:function(){return this.getMessage()}}}),e.exports=s},{inh:10}],10:[function(t,e,r){"use strict";var o=function(t,e){var r=function(){};r.prototype=e.prototype,t.super_=e,t.prototype=new r,t.prototype.constructor=t};e.exports=o},{}]},{},[1]);
View
923 client/apis.global.js
@@ -0,0 +1,923 @@
+;(function(e,t,n,r){function i(r){if(!n[r]){if(!t[r]){if(e)return e(r);throw new Error("Cannot find module '"+r+"'")}var s=n[r]={exports:{}};t[r][0](function(e){var n=t[r][1][e];return i(n?n:e)},s,s.exports)}return n[r].exports}for(var s=0;s<r.length;s++)i(r[s]);return i})(typeof require!=="undefined"&&require,{1:[function(require,module,exports){
+"use strict"
+var apis = require('../../lib/client');
+
+
+var hasOld = 'apis' in window;
+var oldApis = window.apis;
+
+window.apis = apis;
+apis.noConflict = function () {
+ if (hasOld) {
+ window.apis = oldApis;
+ }
+ else {
+ delete window.apis;
+ }
+ return apis;
+};
+
+},{"../../lib/client":2}],2:[function(require,module,exports){
+"use strict";
+var errors = require('./errors');
+var Http = require('./http');
+var HttpRequest = require('./http_request');
+var JsonpRequest = require('./jsonp_request');
+var Socket = require('./socket');
+
+
+module.exports = {
+ errors: errors,
+ Http: Http,
+ HttpRequest: HttpRequest,
+ JsonpRequest: JsonpRequest,
+ Socket: Socket
+};
+
+},{"./errors":3,"./http":4,"./http_request":5,"./jsonp_request":6,"./socket":7}],4:[function(require,module,exports){
+"use strict";
+var HttpRequest = require('./http_request');
+var JsonpRequest = require('./jsonp_request');
+
+
+var Http = function (opt_baseUri) {
+ this.baseUri = opt_baseUri;
+};
+
+Http.prototype.send = function (path, method, headers, data, options, cb) {
+ var result;
+ if (options == null || options.crossDomain == null) {
+ result = this.sendHttp(path, method, headers, data, options, cb);
+ }
+ else {
+ switch (options.crossDomain) {
+ case 'jsonp':
+ result = this.sendJsonp(path, method, headers, data, options, cb);
+ break;
+ default:
+ throw new Error('Unsupported crossDomain option ' + options.crossDomain);
+ }
+ }
+ return result;
+};
+
+Http.prototype.sendHttp = function (path, method, headers, data, options, cb) {
+ return new HttpRequest(this, path, method, headers, data, options, cb).send();
+};
+
+Http.prototype.sendJsonp = function (path, method, headers, data, options, cb) {
+ return new JsonpRequest(this, path, method, headers, data, options, cb).send();
+};
+
+
+module.exports = Http;
+
+},{"./http_request":5,"./jsonp_request":6}],5:[function(require,module,exports){
+"use strict";
+var errors = require('./errors');
+
+
+var HttpRequest = function (http, path, method, headers, data, options, cb) {
+ this.http = http;
+ this.options = options || {};
+ this.path = path;
+ this.method = method;
+ this.headers = headers || {};
+ this.data = data;
+ this.cb = cb;
+
+ this.url = null;
+ this.body = null;
+ this.transport = null;
+ this.isAborted = false;
+ this.timeout = null;
+};
+
+HttpRequest.prototype.methodHttpHeaderName = 'X-Method';
+HttpRequest.prototype.authHttpHeaderName = 'X-Auth';
+HttpRequest.prototype.authExpectedHttpHeaderName = 'X-AuthExpected';
+HttpRequest.prototype.authRenewalHttpHeaderName = 'X-AuthRenewal';
+HttpRequest.prototype.authRenewalIssuedHttpHeaderName = 'X-AuthRenewalIssued';
+HttpRequest.prototype.authRenewalMaxAgeHttpHeaderName = 'X-AuthRenewalMaxAge';
+
+HttpRequest.prototype.headersUrlKey = 'headers';
+HttpRequest.prototype.bodyUrlKey = 'body';
+HttpRequest.prototype.xdomainUrlKey = 'xdomain';
+
+HttpRequest.prototype.defaultContentType = 'application/json';
+
+HttpRequest.prototype.send = function () {
+ this.createTimeout();
+ this.url = this.createUrl();
+ this.body = this.createBody();
+ this.transport = this.createTransport();
+ this.createCallback();
+ this.sendInternal();
+ return this;
+};
+
+HttpRequest.prototype.abort = function () {
+ this.isAborted = true;
+ this.clearTimeout();
+ if (this.transport != null) {
+ this.transport.onreadystatechange = null;
+ this.transport.abort();
+ }
+};
+
+HttpRequest.prototype.createTimeout = function () {
+ var timeout = this.options.timeout;
+ if (timeout != null) {
+ var self = this;
+ this.timeout = setTimeout(function () {
+ self.handleTimeout();
+ }, timeout);
+ }
+};
+
+HttpRequest.prototype.handleTimeout = function () {
+ this.abort();
+ this.cb(new errors.TimeoutError());
+};
+
+HttpRequest.prototype.clearTimeout = function () {
+ if (this.timeout != null) {
+ clearTimeout(this.timeout);
+ }
+};
+
+HttpRequest.prototype.createUrl = function () {
+ var result = this.path;
+ if (this.http.baseUri) {
+ result = this.http.baseUri + this.path;
+ }
+ return result;
+};
+
+HttpRequest.prototype.createBody = function () {
+ var result;
+ if (this.data === undefined) {
+ result = '';
+ }
+ else {
+ result = JSON.stringify(this.data);
+ }
+ return result;
+};
+
+HttpRequest.prototype.createTransport = function () {
+ return new XMLHttpRequest();
+};
+
+HttpRequest.prototype.createCallback = function () {
+ var self = this;
+ // NOTE use onload/onerror pair instead of
+ // onreadystatechange in future (will break IE8 support)
+ this.transport.onreadystatechange = function() {
+ if(self.transport.readyState == 4) {
+ self.transport.onreadystatechange = null;
+ if (self.isNetworkError()) {
+ self.handleNetworkError();
+ }
+ else {
+ self.handleResponse();
+ }
+ }
+ };
+};
+
+HttpRequest.prototype.isNetworkError = function () {
+ // NOTE works incorrectly with file:// protocol
+ return !this.transport.status;
+};
+
+HttpRequest.prototype.handleNetworkError = function () {
+ this.clearTimeout();
+ this.cb(new errors.NetworkError());
+};
+
+HttpRequest.prototype.sendInternal = function () {
+ if (this.method == 'get') {
+ this.sendGet();
+ }
+ else {
+ this.sendPost();
+ }
+};
+
+HttpRequest.prototype.sendGet = function () {
+ this.transport.open('GET', this.createGetUrl());
+ this.setHttpHeaders();
+ this.transport.send();
+};
+
+HttpRequest.prototype.createGetUrl = function () {
+ var parts = this.createGetUrlParts();
+ var result = this.url;
+ if (parts.length > 0) {
+ result += '?' + parts.join('&');
+ }
+ return result;
+};
+
+HttpRequest.prototype.createGetUrlParts = function (opt_parts) {
+ var parts = opt_parts || [];
+ if (this.body) {
+ parts.push(this.bodyUrlKey + '=' + encodeURIComponent(this.body));
+ }
+ return parts;
+};
+
+HttpRequest.prototype.sendPost = function () {
+ var transport = this.transport;
+ transport.open('POST', this.url);
+ this.setContentTypeHttpHeader();
+ this.setMethodHttpHeader();
+ this.setHttpHeaders();
+ transport.send(this.body);
+};
+
+HttpRequest.prototype.setHttpHeaders = function () {
+ var headers = this.createHttpHeaders();
+ var transport = this.transport;
+ for (var k in headers) {
+ transport.setRequestHeader(k, headers[k]);
+ }
+};
+
+HttpRequest.prototype.createHttpHeaders = function () {
+ var headers = this.headers;
+ var result = headers.http || {};
+ delete headers.http;
+ if (headers.auth) {
+ result[this.authHttpHeaderName] = headers.auth;
+ }
+ if (headers.authExpected) {
+ result[this.authExpectedHttpHeaderName] = headers.authExpected;
+ }
+ return result;
+};
+
+HttpRequest.prototype.setContentTypeHttpHeader = function () {
+ var contentType = this.headers.http && this.headers.http['Content-type'] || this.headers.contentType || this.defaultContentType;
+ this.transport.setRequestHeader('Content-type', contentType);
+};
+
+HttpRequest.prototype.setMethodHttpHeader = function () {
+ if (this.method != 'create') {
+ this.transport.setRequestHeader(this.methodHttpHeaderName, this.method);
+ }
+};
+
+HttpRequest.prototype.handleResponse = function () {
+ this.clearTimeout();
+ var result = this.createResult();
+ var err = this.extractError(result);
+ if (err != null) {
+ this.cb(err);
+ }
+ else {
+ this.cb(null, result);
+ }
+};
+
+HttpRequest.prototype.createResult = function () {
+ var result = {
+ headers: this.getResultHeaders(),
+ data: this.getResultData()
+ };
+ var status = this.getResultStatus();
+ if (status != null) {
+ result.status = status;
+ }
+ var transport = this.getTransportForResult();
+ if (transport != null) {
+ result.transport = transport;
+ }
+ return result;
+};
+
+HttpRequest.prototype.getTransportForResult = function () {
+ return this.transport;
+};
+
+HttpRequest.prototype.getResultStatus = function () {
+ return this.transport.status;
+};
+
+HttpRequest.prototype.getResultHeaders = function () {
+ var result = {};
+ var authRenewalToken = this.transport.getResponseHeader(this.authRenewalHttpHeaderName);
+ if (authRenewalToken) {
+ var renewal = {
+ token: authRenewalToken
+ };
+ result.authRenewal = renewal;
+ var issued = this.transport.getResponseHeader(this.authRenewalIssuedHttpHeaderName);
+ if (issued) {
+ renewal.issued = new Date(issued);
+ }
+ var maxAge = this.transport.getResponseHeader(this.authRenewalMaxAgeHttpHeaderName);
+ if (maxAge) {
+ renewal.maxAge = parseInt(maxAge, 10);
+ }
+ }
+ return result;
+};
+
+HttpRequest.prototype.getResultData = function () {
+ var body = this.getResultBody();
+ return body ? JSON.parse(body) : undefined;
+};
+
+HttpRequest.prototype.getResultBody = function () {
+ return this.transport.responseText;
+};
+
+HttpRequest.prototype.extractError = function (result) {
+ var status = result.status;
+ var err = null;
+ if (status != 200 && status != 204) {
+ err = new errors.WebError(result);
+ }
+ return err;
+};
+
+
+module.exports = HttpRequest;
+
+},{"./errors":3}],7:[function(require,module,exports){
+"use strict";
+var errors = require('./errors');
+
+
+var Socket = function (opt_basePath, opt_socketUri, opt_protocols) {
+ this.basePath = opt_basePath;
+ this.socketUri = opt_socketUri || this.defaultSocketUri;
+ this.protocols = opt_protocols;
+
+ this.onSocketCreated = null;
+ this.onMessage = null;
+
+ // NOTE you can set this property to use sockjs or whatever
+ this.customWebSocketClass = null;
+
+ this.socket = null;
+ this.requests = {};
+
+ this.abortFunc = this.createAbortFunc();
+};
+
+Socket.prototype.defaultSocketUri = '/socket';
+Socket.prototype.headerSeparator = '\n\n';
+Socket.prototype.defaultTimeout = null;
+
+Socket.prototype.getWebSocketClass = function () {
+ return this.customWebSocketClass || WebSocket;
+};
+
+Socket.prototype.createAbortFunc = function () {
+ var self = this;
+ return function () {
+ self.handleAbort(this);
+ };
+};
+
+Socket.prototype.createSocket = function () {
+ var WebSocketClass = this.getWebSocketClass();
+ return new WebSocketClass(this.socketUri, this.protocols);
+};
+
+Socket.prototype.connect = function (opt_cb) {
+ if (this.socket != null) {
+ switch (this.socket.readyState) {
+ case 0: // connecting
+ if (opt_cb) {
+ this.socket.addEventListener('open', function () {
+ opt_cb();
+ });
+ }
+ break;
+ case 1: // open
+ if (opt_cb) {
+ opt_cb();
+ }
+ break;
+ default: // closing or closed
+ this.socket = null;
+ }
+ }
+ if (this.socket == null) {
+ this.socket = this.createSocket();
+
+ if (this.onSocketCreated) {
+ this.onSocketCreated(this.socket);
+ }
+
+ if (opt_cb) {
+ this.socket.addEventListener('open', function () {
+ opt_cb();
+ });
+ }
+
+ var self = this;
+ this.socket.addEventListener('message', function (ev) {
+ self.handleMessage(ev);
+ });
+
+ this.socket.addEventListener('close', function (ev) {
+ self.handleClose(ev);
+ });
+ }
+};
+
+Socket.prototype.handleMessage = function (ev) {
+ var result = this.parse(ev.data);
+ var requestId = (result.headers != null ? result.headers.requestId : null);
+ if (requestId != null) {
+ var request = this.requests[requestId];
+ if (request) {
+ var cb = request.cb;
+ this.clearRequest(request, requestId);
+ var err = this.extractError(result);
+ if (err != null) {
+ cb(err);
+ }
+ else {
+ cb(null, result);
+ }
+ }
+ }
+ else if (this.onMessage != null) {
+ this.onMessage(result);
+ }
+};
+
+Socket.prototype.createRequestId = function () {
+ var base = '' + (new Date().getTime());
+ var i = 0;
+ var result = base;
+ while (result in this.requests) {
+ result = base + '_' + (i++);
+ }
+ return result;
+};
+
+Socket.prototype.clearRequest = function (request) {
+ delete this.requests[request.id];
+ var timeout = request.timeout;
+ if (timeout != null) {
+ clearTimeout(timeout);
+ }
+};
+
+Socket.prototype.close = function () {
+ if (this.socket != null) {
+ this.socket.close();
+ }
+};
+
+Socket.prototype.send = function (path, method, headers, data, opt_options, opt_cb) {
+ var self = this;
+ this.connect(function () {
+ self.sendInternal(path, method, headers, data, opt_options, opt_cb);
+ });
+};
+
+Socket.prototype.sendInternal = function (path, method, headers, data, opt_options, opt_cb) {
+ headers = headers || {};
+
+ headers.method = method;
+ headers.path = this.getEffectivePath(path);
+
+ var request;
+ if (opt_cb) {
+ request = this.createRequest(opt_options, opt_cb);
+ headers.requestId = request.id;
+ }
+
+ var body;
+ if (data === undefined) {
+ body = '';
+ }
+ else {
+ body = JSON.stringify(data);
+ }
+
+ var msg = [
+ JSON.stringify(headers),
+ body
+ ].join(this.headerSeparator);
+
+ this.socket.send(msg);
+ return request;
+};
+
+Socket.prototype.getEffectivePath = function (path) {
+ var result = path;
+ if (this.basePath) {
+ result = this.basePath + path;
+ }
+ return result;
+};
+
+Socket.prototype.createRequest = function (options, cb) {
+ var requestId = this.createRequestId();
+ var request = {
+ id: requestId,
+ cb: cb,
+ options: options,
+ abort: this.abortFunc
+ };
+ request.timeout = this.createTimeout(request);
+ this.requests[requestId] = request;
+ return request;
+};
+
+Socket.prototype.createTimeout = function (request) {
+ var options = request.options;
+ var result = null;
+ var timeout = (options != null && options.timeout != null ? options.timeout : this.defaultTimeout);
+ if (timeout != null) {
+ var self = this;
+ result = setTimeout(function () {
+ self.handleTimeout(request);
+ }, timeout);
+ }
+ return result;
+};
+
+Socket.prototype.handleTimeout = function (request) {
+ var cb = request.cb;
+ this.clearRequest(request);
+ cb(new errors.TimeoutError());
+};
+
+Socket.prototype.handleAbort = function (request) {
+ this.clearRequest(request);
+};
+
+Socket.prototype.handleClose = function (ev) {
+ for (var k in this.requests) {
+ var request = this.requests[k];
+ var cb = request.cb;
+ this.clearRequest(request);
+ cb(new errors.ConnectionCloseError(ev));
+ }
+};
+
+Socket.prototype.parse = function (message) {
+ var sepPos = message.indexOf(this.headerSeparator);
+ var headersStr = message.substring(0, sepPos);
+ var body = message.substring(sepPos + this.headerSeparator.length);
+
+ var headers = JSON.parse(headersStr);
+
+ var result = {
+ headers: headers,
+ data: this.parseBody(body)
+ };
+
+ if (headers.status != null) {
+ result.status = headers.status;
+ }
+
+ return result;
+};
+
+Socket.prototype.parseBody = function (body) {
+ var result;
+ if (body) {
+ result = JSON.parse(body);
+ }
+ return result;
+};
+
+Socket.prototype.extractError = function (result) {
+ var status = result.status;
+ var err = null;
+ if (status != 200 && status != 204) {
+ err = new errors.WebError(result);
+ }
+ return err;
+};
+
+
+module.exports = Socket;
+
+},{"./errors":3}],3:[function(require,module,exports){
+"use strict";
+var inherits = require('inh');
+var ErrorBase = require('nerr/lib/error_base');
+
+
+var WebError = function (response) {
+ ErrorBase.call(this);
+
+ this.response = response;
+
+ this.status = response.status;
+ var data = response.data || {};
+ this._message = data.message;
+ this.status = response.status;
+ this.code = data.code;
+};
+inherits(WebError, ErrorBase);
+
+WebError.prototype.name = 'WebError';
+
+WebError.prototype.getMessage = function () {
+ return this._message;
+};
+
+
+var NetworkError = function () {
+ ErrorBase.call(this);
+};
+inherits(NetworkError, ErrorBase);
+
+NetworkError.prototype.name = 'NetworkError';
+
+
+var TimeoutError = function () {
+ ErrorBase.call(this);
+};
+inherits(TimeoutError, ErrorBase);
+
+TimeoutError.prototype.name = 'TimeoutError';
+
+
+var ConnectionCloseError = function (closeEvent) {
+ ErrorBase.call(this);
+ this.closeEvent = closeEvent;
+};
+inherits(ConnectionCloseError, ErrorBase);
+
+ConnectionCloseError.prototype.name = 'ConnectionCloseError';
+
+ConnectionCloseError.prototype.getMessage = function () {
+ return this.closeEvent.reason;
+};
+
+
+module.exports = {
+ WebError: WebError,
+ NetworkError: NetworkError,
+ TimeoutError: TimeoutError,
+ ConnectionCloseError: ConnectionCloseError
+};
+
+},{"nerr/lib/error_base":8,"inh":9}],6:[function(require,module,exports){
+"use strict";
+var inherits = require('inh');
+var HttpRequest = require('./http_request');
+var errors = require('./errors');
+
+
+var JsonpRequest = function (http, path, method, headers, data, options, cb) {
+ HttpRequest.call(this, http, path, method, headers, data, options, cb);
+ this.callbacks = this.getCallbacks();
+ this.scriptEl = null;
+ this.callbackId = null;
+ this.response = null;
+};
+inherits(JsonpRequest, HttpRequest);
+
+JsonpRequest.callbacks = null;
+
+JsonpRequest.prototype.callbacksGlobalPath = 'apis.jsonp.callbacks';
+JsonpRequest.prototype.xdomainValue = 'jsonp';
+JsonpRequest.prototype.jsonpCallbackUrlKey = 'callback';
+
+JsonpRequest.prototype.abort = function () {
+ this.isAborted = true;
+ this.clearTimeout();
+};
+
+JsonpRequest.prototype.getCallbacks = function () {
+ if (JsonpRequest.callbacks == null) {
+ var parts = this.callbacksGlobalPath.split('.');
+ var curr = window;
+ for (var i = 0; i < parts.length; i++) {
+ var part = parts[i];
+ var next = curr[part] || {};
+ curr[part] = next;
+ curr = next;
+ }
+ JsonpRequest.callbacks = curr;
+ }
+ return JsonpRequest.callbacks;
+};
+
+JsonpRequest.prototype.createTransport = function () {
+ var el = document.createElement('script');
+ el.async = true;
+ var self = this;
+ el.onerror = function (ev) {
+ self.handleScriptErrorEvent(ev);
+ };
+ this.scriptEl = el;
+ return el;
+};
+
+JsonpRequest.prototype.handleScriptErrorEvent = function (ev) {
+ this.cleanup();
+ if (!this.isAborted) {
+ this.cb(new errors.NetworkError());
+ }
+};
+
+JsonpRequest.prototype.createCallback = function () {
+ var self = this;
+ this.callbackId = this.createCallbackId();
+ this.callbacks[this.callbackId] = function (response) {
+ self.response = response;
+ self.handleResponse();
+ };
+};
+
+JsonpRequest.prototype.createCallbackId = function () {
+ var base = 'cb' + (new Date().getTime());
+ var i = 0;
+ var result = base;
+ while (result in this.callbacks) {
+ result = base + '_' + (i++);
+ }
+ return result;
+};
+
+JsonpRequest.prototype.sendInternal = function () {
+ this.scriptEl.src = this.createGetUrl();
+
+ var head = document.head || document.getElementsByTagName('head')[0] || document.documentElement;
+ var firstChild = head.firstChild;
+ if (firstChild) {
+ head.insertBefore(this.scriptEl, head.firstChild);
+ }
+ else {
+ head.appendChild(this.scriptEl);
+ }
+};
+
+JsonpRequest.prototype.createGetUrlParts = function (opt_parts) {
+ var parts = opt_parts || [];
+ var headers = this.headers;
+ if (this.method != 'get') {
+ headers = headers || {};
+ headers.method = this.method;
+ }
+ if (headers) {
+ parts.push(this.headersUrlKey + '=' + encodeURIComponent(JSON.stringify(headers)));
+ }
+ JsonpRequest.super_.prototype.createGetUrlParts.call(this, parts);
+ parts.push(this.xdomainUrlKey + '=' + this.xdomainValue);
+ parts.push(this.jsonpCallbackUrlKey + '=' + encodeURIComponent(this.createCallbackName()));
+ return parts;
+};
+
+JsonpRequest.prototype.createCallbackName = function () {
+ return this.callbacksGlobalPath + '.' + this.callbackId;
+};
+
+JsonpRequest.prototype.setHttpHeaders = function () {
+};
+
+JsonpRequest.prototype.handleResponse = function () {
+ this.cleanup();
+ if (!this.isAborted) {
+ JsonpRequest.super_.prototype.handleResponse.call(this);
+ }
+};
+
+JsonpRequest.prototype.cleanup = function () {
+ this.clearTimeout();
+ this.removeCallback();
+ this.removeScripEl();
+};
+
+JsonpRequest.prototype.removeCallback = function () {
+ delete this.callbacks[this.callbackId];
+};
+
+JsonpRequest.prototype.removeScripEl = function () {
+ var scriptEl = this.scriptEl;
+ if (scriptEl != null && scriptEl.parentNode) {
+ scriptEl.parentNode.removeChild(scriptEl);
+ this.scriptEl = null;
+ }
+};
+
+JsonpRequest.prototype.getTransportForResult = function () {
+ return null;
+};
+
+JsonpRequest.prototype.getResultStatus = function () {
+ var headers = this.getResultHeaders();
+ return headers.status;
+};
+
+JsonpRequest.prototype.getResultHeaders = function () {
+ return this.response.headers || {};
+};
+
+JsonpRequest.prototype.getResultData = function () {
+ return this.response.data;
+};
+
+
+module.exports = JsonpRequest;
+
+},{"./http_request":5,"./errors":3,"inh":9}],9:[function(require,module,exports){
+"use strict";
+var inherits = function(childCtor, parentCtor) {
+ var TempCtor = function () {};
+ TempCtor.prototype = parentCtor.prototype;
+ childCtor.super_ = parentCtor;
+ childCtor.prototype = new TempCtor();
+ childCtor.prototype.constructor = childCtor;
+};
+
+
+module.exports = inherits;
+
+},{}],8:[function(require,module,exports){
+"use strict";
+var inherits = require('inh');
+
+
+var ErrorBase = function () {
+ Error.call(this);
+ this.captureStackTrace();
+};
+inherits(ErrorBase, Error);
+
+ErrorBase.prototype.name = 'ErrorBase';
+
+ErrorBase.prototype.captureStackTrace = function () {
+ if (Error.captureStackTrace) {
+ Error.captureStackTrace(this, this.constructor);
+ }
+ else {
+ var stackKeeper = new Error();
+ var self = this;
+ stackKeeper.toString = function () { return self.toString(); };
+ var getStackTrace = function () {
+ return stackKeeper.stack;
+ };
+
+ if (Object.defineProperties) {
+ Object.defineProperties({
+ stack: getStackTrace
+ });
+ }
+ else {
+ this.getStackTrace = getStackTrace;
+ }
+ }
+};
+
+ErrorBase.prototype.toString = function () {
+ var result = this.name;
+ var message = this.getMessage();
+ if (message) {
+ result = [result, message].join(': ');
+ }
+ return result;
+};
+
+ErrorBase.prototype.getMessage = function () {
+ return null;
+};
+
+ErrorBase.prototype.getStackTrace = function () {
+ return this.stack;
+};
+
+if (Object.defineProperties) {
+ Object.defineProperties(ErrorBase.prototype, {
+ message: {
+ get: function () {
+ return this.getMessage();
+ }
+ }
+ });
+}
+
+
+module.exports = ErrorBase;
+
+},{"inh":10}],10:[function(require,module,exports){
+"use strict";
+var inherits = function(childCtor, parentCtor) {
+ var TempCtor = function () {};
+ TempCtor.prototype = parentCtor.prototype;
+ childCtor.super_ = parentCtor;
+ childCtor.prototype = new TempCtor();
+ childCtor.prototype.constructor = childCtor;
+};
+
+
+module.exports = inherits;
+
+},{}]},{},[1])
+;
View
1  client/apis.global.min.js
@@ -0,0 +1 @@
+(function(t,e,r,o){function s(o){if(!r[o]){if(!e[o]){if(t)return t(o);throw Error("Cannot find module '"+o+"'")}var n=r[o]={exports:{}};e[o][0](function(t){var r=e[o][1][t];return s(r?r:t)},n,n.exports)}return r[o].exports}for(var n=0;o.length>n;n++)s(o[n]);return s})("undefined"!=typeof require&&require,{1:[function(t,e,r){"use strict";var o=t("../../lib/client"),s="apis"in window,n=window.apis;window.apis=o,o.noConflict=function(){return s?window.apis=n:delete window.apis,o}},{"../../lib/client":2}],2:[function(t,e,r){"use strict";var o=t("./errors"),s=t("./http"),n=t("./http_request"),a=t("./jsonp_request"),i=t("./socket");e.exports={errors:o,Http:s,HttpRequest:n,JsonpRequest:a,Socket:i}},{"./errors":3,"./http":4,"./http_request":5,"./jsonp_request":6,"./socket":7}],4:[function(t,e,r){"use strict";var o=t("./http_request"),s=t("./jsonp_request"),n=function(t){this.baseUri=t};n.prototype.send=function(t,e,r,o,s,n){var a;if(null==s||null==s.crossDomain)a=this.sendHttp(t,e,r,o,s,n);else switch(s.crossDomain){case"jsonp":a=this.sendJsonp(t,e,r,o,s,n);break;default:throw Error("Unsupported crossDomain option "+s.crossDomain)}return a},n.prototype.sendHttp=function(t,e,r,s,n,a){return new o(this,t,e,r,s,n,a).send()},n.prototype.sendJsonp=function(t,e,r,o,n,a){return new s(this,t,e,r,o,n,a).send()},e.exports=n},{"./http_request":5,"./jsonp_request":6}],5:[function(t,e,r){"use strict";var o=t("./errors"),s=function(t,e,r,o,s,n,a){this.http=t,this.options=n||{},this.path=e,this.method=r,this.headers=o||{},this.data=s,this.cb=a,this.url=null,this.body=null,this.transport=null,this.isAborted=!1,this.timeout=null};s.prototype.methodHttpHeaderName="X-Method",s.prototype.authHttpHeaderName="X-Auth",s.prototype.authExpectedHttpHeaderName="X-AuthExpected",s.prototype.authRenewalHttpHeaderName="X-AuthRenewal",s.prototype.authRenewalIssuedHttpHeaderName="X-AuthRenewalIssued",s.prototype.authRenewalMaxAgeHttpHeaderName="X-AuthRenewalMaxAge",s.prototype.headersUrlKey="headers",s.prototype.bodyUrlKey="body",s.prototype.xdomainUrlKey="xdomain",s.prototype.defaultContentType="application/json",s.prototype.send=function(){return this.createTimeout(),this.url=this.createUrl(),this.body=this.createBody(),this.transport=this.createTransport(),this.createCallback(),this.sendInternal(),this},s.prototype.abort=function(){this.isAborted=!0,this.clearTimeout(),null!=this.transport&&(this.transport.onreadystatechange=null,this.transport.abort())},s.prototype.createTimeout=function(){var t=this.options.timeout;if(null!=t){var e=this;this.timeout=setTimeout(function(){e.handleTimeout()},t)}},s.prototype.handleTimeout=function(){this.abort(),this.cb(new o.TimeoutError)},s.prototype.clearTimeout=function(){null!=this.timeout&&clearTimeout(this.timeout)},s.prototype.createUrl=function(){var t=this.path;return this.http.baseUri&&(t=this.http.baseUri+this.path),t},s.prototype.createBody=function(){var t;return t=void 0===this.data?"":JSON.stringify(this.data)},s.prototype.createTransport=function(){return new XMLHttpRequest},s.prototype.createCallback=function(){var t=this;this.transport.onreadystatechange=function(){4==t.transport.readyState&&(t.transport.onreadystatechange=null,t.isNetworkError()?t.handleNetworkError():t.handleResponse())}},s.prototype.isNetworkError=function(){return!this.transport.status},s.prototype.handleNetworkError=function(){this.clearTimeout(),this.cb(new o.NetworkError)},s.prototype.sendInternal=function(){"get"==this.method?this.sendGet():this.sendPost()},s.prototype.sendGet=function(){this.transport.open("GET",this.createGetUrl()),this.setHttpHeaders(),this.transport.send()},s.prototype.createGetUrl=function(){var t=this.createGetUrlParts(),e=this.url;return t.length>0&&(e+="?"+t.join("&")),e},s.prototype.createGetUrlParts=function(t){var e=t||[];return this.body&&e.push(this.bodyUrlKey+"="+encodeURIComponent(this.body)),e},s.prototype.sendPost=function(){var t=this.transport;t.open("POST",this.url),this.setContentTypeHttpHeader(),this.setMethodHttpHeader(),this.setHttpHeaders(),t.send(this.body)},s.prototype.setHttpHeaders=function(){var t=this.createHttpHeaders(),e=this.transport;for(var r in t)e.setRequestHeader(r,t[r])},s.prototype.createHttpHeaders=function(){var t=this.headers,e=t.http||{};return delete t.http,t.auth&&(e[this.authHttpHeaderName]=t.auth),t.authExpected&&(e[this.authExpectedHttpHeaderName]=t.authExpected),e},s.prototype.setContentTypeHttpHeader=function(){var t=this.headers.http&&this.headers.http["Content-type"]||this.headers.contentType||this.defaultContentType;this.transport.setRequestHeader("Content-type",t)},s.prototype.setMethodHttpHeader=function(){"create"!=this.method&&this.transport.setRequestHeader(this.methodHttpHeaderName,this.method)},s.prototype.handleResponse=function(){this.clearTimeout();var t=this.createResult(),e=this.extractError(t);null!=e?this.cb(e):this.cb(null,t)},s.prototype.createResult=function(){var t={headers:this.getResultHeaders(),data:this.getResultData()},e=this.getResultStatus();null!=e&&(t.status=e);var r=this.getTransportForResult();return null!=r&&(t.transport=r),t},s.prototype.getTransportForResult=function(){return this.transport},s.prototype.getResultStatus=function(){return this.transport.status},s.prototype.getResultHeaders=function(){var t={},e=this.transport.getResponseHeader(this.authRenewalHttpHeaderName);if(e){var r={token:e};t.authRenewal=r;var o=this.transport.getResponseHeader(this.authRenewalIssuedHttpHeaderName);o&&(r.issued=new Date(o));var s=this.transport.getResponseHeader(this.authRenewalMaxAgeHttpHeaderName);s&&(r.maxAge=parseInt(s,10))}return t},s.prototype.getResultData=function(){var t=this.getResultBody();return t?JSON.parse(t):void 0},s.prototype.getResultBody=function(){return this.transport.responseText},s.prototype.extractError=function(t){var e=t.status,r=null;return 200!=e&&204!=e&&(r=new o.WebError(t)),r},e.exports=s},{"./errors":3}],7:[function(t,e,r){"use strict";var o=t("./errors"),s=function(t,e,r){this.basePath=t,this.socketUri=e||this.defaultSocketUri,this.protocols=r,this.onSocketCreated=null,this.onMessage=null,this.customWebSocketClass=null,this.socket=null,this.requests={},this.abortFunc=this.createAbortFunc()};s.prototype.defaultSocketUri="/socket",s.prototype.headerSeparator="\n\n",s.prototype.defaultTimeout=null,s.prototype.getWebSocketClass=function(){return this.customWebSocketClass||WebSocket},s.prototype.createAbortFunc=function(){var t=this;return function(){t.handleAbort(this)}},s.prototype.createSocket=function(){var t=this.getWebSocketClass();return new t(this.socketUri,this.protocols)},s.prototype.connect=function(t){if(null!=this.socket)switch(this.socket.readyState){case 0:t&&this.socket.addEventListener("open",function(){t()});break;case 1:t&&t();break;default:this.socket=null}if(null==this.socket){this.socket=this.createSocket(),this.onSocketCreated&&this.onSocketCreated(this.socket),t&&this.socket.addEventListener("open",function(){t()});var e=this;this.socket.addEventListener("message",function(t){e.handleMessage(t)}),this.socket.addEventListener("close",function(t){e.handleClose(t)})}},s.prototype.handleMessage=function(t){var e=this.parse(t.data),r=null!=e.headers?e.headers.requestId:null;if(null!=r){var o=this.requests[r];if(o){var s=o.cb;this.clearRequest(o,r);var n=this.extractError(e);null!=n?s(n):s(null,e)}}else null!=this.onMessage&&this.onMessage(e)},s.prototype.createRequestId=function(){for(var t=""+(new Date).getTime(),e=0,r=t;r in this.requests;)r=t+"_"+e++;return r},s.prototype.clearRequest=function(t){delete this.requests[t.id];var e=t.timeout;null!=e&&clearTimeout(e)},s.prototype.close=function(){null!=this.socket&&this.socket.close()},s.prototype.send=function(t,e,r,o,s,n){var a=this;this.connect(function(){a.sendInternal(t,e,r,o,s,n)})},s.prototype.sendInternal=function(t,e,r,o,s,n){r=r||{},r.method=e,r.path=this.getEffectivePath(t);var a;n&&(a=this.createRequest(s,n),r.requestId=a.id);var i;i=void 0===o?"":JSON.stringify(o);var p=[JSON.stringify(r),i].join(this.headerSeparator);return this.socket.send(p),a},s.prototype.getEffectivePath=function(t){var e=t;return this.basePath&&(e=this.basePath+t),e},s.prototype.createRequest=function(t,e){var r=this.createRequestId(),o={id:r,cb:e,options:t,abort:this.abortFunc};return o.timeout=this.createTimeout(o),this.requests[r]=o,o},s.prototype.createTimeout=function(t){var e=t.options,r=null,o=null!=e&&null!=e.timeout?e.timeout:this.defaultTimeout;if(null!=o){var s=this;r=setTimeout(function(){s.handleTimeout(t)},o)}return r},s.prototype.handleTimeout=function(t){var e=t.cb;this.clearRequest(t),e(new o.TimeoutError)},s.prototype.handleAbort=function(t){this.clearRequest(t)},s.prototype.handleClose=function(t){for(var e in this.requests){var r=this.requests[e],s=r.cb;this.clearRequest(r),s(new o.ConnectionCloseError(t))}},s.prototype.parse=function(t){var e=t.indexOf(this.headerSeparator),r=t.substring(0,e),o=t.substring(e+this.headerSeparator.length),s=JSON.parse(r),n={headers:s,data:this.parseBody(o)};return null!=s.status&&(n.status=s.status),n},s.prototype.parseBody=function(t){var e;return t&&(e=JSON.parse(t)),e},s.prototype.extractError=function(t){var e=t.status,r=null;return 200!=e&&204!=e&&(r=new o.WebError(t)),r},e.exports=s},{"./errors":3}],3:[function(t,e,r){"use strict";var o=t("inh"),s=t("nerr/lib/error_base"),n=function(t){s.call(this),this.response=t,this.status=t.status;var e=t.data||{};this._message=e.message,this.status=t.status,this.code=e.code};o(n,s),n.prototype.name="WebError",n.prototype.getMessage=function(){return this._message};var a=function(){s.call(this)};o(a,s),a.prototype.name="NetworkError";var i=function(){s.call(this)};o(i,s),i.prototype.name="TimeoutError";var p=function(t){s.call(this),this.closeEvent=t};o(p,s),p.prototype.name="ConnectionCloseError",p.prototype.getMessage=function(){return this.closeEvent.reason},e.exports={WebError:n,NetworkError:a,TimeoutError:i,ConnectionCloseError:p}},{"nerr/lib/error_base":8,inh:9}],6:[function(t,e,r){"use strict";var o=t("inh"),s=t("./http_request"),n=t("./errors"),a=function(t,e,r,o,n,a,i){s.call(this,t,e,r,o,n,a,i),this.callbacks=this.getCallbacks(),this.scriptEl=null,this.callbackId=null,this.response=null};o(a,s),a.callbacks=null,a.prototype.callbacksGlobalPath="apis.jsonp.callbacks",a.prototype.xdomainValue="jsonp",a.prototype.jsonpCallbackUrlKey="callback",a.prototype.abort=function(){this.isAborted=!0,this.clearTimeout()},a.prototype.getCallbacks=function(){if(null==a.callbacks){for(var t=this.callbacksGlobalPath.split("."),e=window,r=0;t.length>r;r++){var o=t[r],s=e[o]||{};e[o]=s,e=s}a.callbacks=e}return a.callbacks},a.prototype.createTransport=function(){var t=document.createElement("script");t.async=!0;var e=this;return t.onerror=function(t){e.handleScriptErrorEvent(t)},this.scriptEl=t,t},a.prototype.handleScriptErrorEvent=function(t){this.cleanup(),this.isAborted||this.cb(new n.NetworkError)},a.prototype.createCallback=function(){var t=this;this.callbackId=this.createCallbackId(),this.callbacks[this.callbackId]=function(e){t.response=e,t.handleResponse()}},a.prototype.createCallbackId=function(){for(var t="cb"+(new Date).getTime(),e=0,r=t;r in this.callbacks;)r=t+"_"+e++;return r},a.prototype.sendInternal=function(){this.scriptEl.src=this.createGetUrl();var t=document.head||document.getElementsByTagName("head")[0]||document.documentElement,e=t.firstChild;e?t.insertBefore(this.scriptEl,t.firstChild):t.appendChild(this.scriptEl)},a.prototype.createGetUrlParts=function(t){var e=t||[],r=this.headers;return"get"!=this.method&&(r=r||{},r.method=this.method),r&&e.push(this.headersUrlKey+"="+encodeURIComponent(JSON.stringify(r))),a.super_.prototype.createGetUrlParts.call(this,e),e.push(this.xdomainUrlKey+"="+this.xdomainValue),e.push(this.jsonpCallbackUrlKey+"="+encodeURIComponent(this.createCallbackName())),e},a.prototype.createCallbackName=function(){return this.callbacksGlobalPath+"."+this.callbackId},a.prototype.setHttpHeaders=function(){},a.prototype.handleResponse=function(){this.cleanup(),this.isAborted||a.super_.prototype.handleResponse.call(this)},a.prototype.cleanup=function(){this.clearTimeout(),this.removeCallback(),this.removeScripEl()},a.prototype.removeCallback=function(){delete this.callbacks[this.callbackId]},a.prototype.removeScripEl=function(){var t=this.scriptEl;null!=t&&t.parentNode&&(t.parentNode.removeChild(t),this.scriptEl=null)},a.prototype.getTransportForResult=function(){return null},a.prototype.getResultStatus=function(){var t=this.getResultHeaders();return t.status},a.prototype.getResultHeaders=function(){return this.response.headers||{}},a.prototype.getResultData=function(){return this.response.data},e.exports=a},{"./http_request":5,"./errors":3,inh:9}],9:[function(t,e,r){"use strict";var o=function(t,e){var r=function(){};r.prototype=e.prototype,t.super_=e,t.prototype=new r,t.prototype.constructor=t};e.exports=o},{}],8:[function(t,e,r){"use strict";var o=t("inh"),s=function(){Error.call(this),this.captureStackTrace()};o(s,Error),s.prototype.name="ErrorBase",s.prototype.captureStackTrace=function(){if(Error.captureStackTrace)Error.captureStackTrace(this,this.constructor);else{var t=Error(),e=this;t.toString=function(){return""+e};var r=function(){return t.stack};Object.defineProperties?Object.defineProperties({stack:r}):this.getStackTrace=r}},s.prototype.toString=function(){var t=this.name,e=this.getMessage();return e&&(t=[t,e].join(": ")),t},s.prototype.getMessage=function(){return null},s.prototype.getStackTrace=function(){return this.stack},Object.defineProperties&&Object.defineProperties(s.prototype,{message:{get:function(){return this.getMessage()}}}),e.exports=s},{inh:10}],10:[function(t,e,r){"use strict";var o=function(t,e){var r=function(){};r.prototype=e.prototype,t.super_=e,t.prototype=new r,t.prototype.constructor=t};e.exports=o},{}]},{},[1]);
View
914 client/apis.js
@@ -0,0 +1,914 @@
+;(function(e,t,n,r){function i(r){if(!n[r]){if(!t[r]){if(e)return e(r);throw new Error("Cannot find module '"+r+"'")}var s=n[r]={exports:{}};t[r][0](function(e){var n=t[r][1][e];return i(n?n:e)},s,s.exports)}return n[r].exports}for(var s=0;s<r.length;s++)i(r[s]);return i})(typeof require!=="undefined"&&require,{1:[function(require,module,exports){
+"use strict"
+var apis = require('../../lib/client');
+
+
+var parentRequire = window.require;
+window.require = function (id) {
+ return id == 'apis' ? apis : parentRequire(id);
+};
+
+},{"../../lib/client":2}],2:[function(require,module,exports){
+"use strict";
+var errors = require('./errors');
+var Http = require('./http');
+var HttpRequest = require('./http_request');
+var JsonpRequest = require('./jsonp_request');
+var Socket = require('./socket');
+
+
+module.exports = {
+ errors: errors,
+ Http: Http,
+ HttpRequest: HttpRequest,
+ JsonpRequest: JsonpRequest,
+ Socket: Socket
+};
+
+},{"./errors":3,"./http":4,"./http_request":5,"./jsonp_request":6,"./socket":7}],4:[function(require,module,exports){
+"use strict";
+var HttpRequest = require('./http_request');
+var JsonpRequest = require('./jsonp_request');
+
+
+var Http = function (opt_baseUri) {
+ this.baseUri = opt_baseUri;
+};
+
+Http.prototype.send = function (path, method, headers, data, options, cb) {
+ var result;
+ if (options == null || options.crossDomain == null) {
+ result = this.sendHttp(path, method, headers, data, options, cb);
+ }
+ else {
+ switch (options.crossDomain) {
+ case 'jsonp':
+ result = this.sendJsonp(path, method, headers, data, options, cb);
+ break;
+ default:
+ throw new Error('Unsupported crossDomain option ' + options.crossDomain);
+ }
+ }
+ return result;
+};
+
+Http.prototype.sendHttp = function (path, method, headers, data, options, cb) {
+ return new HttpRequest(this, path, method, headers, data, options, cb).send();
+};
+
+Http.prototype.sendJsonp = function (path, method, headers, data, options, cb) {
+ return new JsonpRequest(this, path, method, headers, data, options, cb).send();
+};
+
+
+module.exports = Http;
+
+},{"./http_request":5,"./jsonp_request":6}],5:[function(require,module,exports){
+"use strict";
+var errors = require('./errors');
+
+
+var HttpRequest = function (http, path, method, headers, data, options, cb) {
+ this.http = http;
+ this.options = options || {};
+ this.path = path;
+ this.method = method;
+ this.headers = headers || {};
+ this.data = data;
+ this.cb = cb;
+
+ this.url = null;
+ this.body = null;
+ this.transport = null;
+ this.isAborted = false;
+ this.timeout = null;
+};
+
+HttpRequest.prototype.methodHttpHeaderName = 'X-Method';
+HttpRequest.prototype.authHttpHeaderName = 'X-Auth';
+HttpRequest.prototype.authExpectedHttpHeaderName = 'X-AuthExpected';
+HttpRequest.prototype.authRenewalHttpHeaderName = 'X-AuthRenewal';
+HttpRequest.prototype.authRenewalIssuedHttpHeaderName = 'X-AuthRenewalIssued';
+HttpRequest.prototype.authRenewalMaxAgeHttpHeaderName = 'X-AuthRenewalMaxAge';
+
+HttpRequest.prototype.headersUrlKey = 'headers';
+HttpRequest.prototype.bodyUrlKey = 'body';
+HttpRequest.prototype.xdomainUrlKey = 'xdomain';
+
+HttpRequest.prototype.defaultContentType = 'application/json';
+
+HttpRequest.prototype.send = function () {
+ this.createTimeout();
+ this.url = this.createUrl();
+ this.body = this.createBody();
+ this.transport = this.createTransport();
+ this.createCallback();
+ this.sendInternal();
+ return this;
+};
+
+HttpRequest.prototype.abort = function () {
+ this.isAborted = true;
+ this.clearTimeout();
+ if (this.transport != null) {
+ this.transport.onreadystatechange = null;
+ this.transport.abort();
+ }
+};
+
+HttpRequest.prototype.createTimeout = function () {
+ var timeout = this.options.timeout;
+ if (timeout != null) {
+ var self = this;
+ this.timeout = setTimeout(function () {
+ self.handleTimeout();
+ }, timeout);
+ }
+};
+
+HttpRequest.prototype.handleTimeout = function () {
+ this.abort();
+ this.cb(new errors.TimeoutError());
+};
+
+HttpRequest.prototype.clearTimeout = function () {
+ if (this.timeout != null) {
+ clearTimeout(this.timeout);
+ }
+};
+
+HttpRequest.prototype.createUrl = function () {
+ var result = this.path;
+ if (this.http.baseUri) {
+ result = this.http.baseUri + this.path;
+ }
+ return result;
+};
+
+HttpRequest.prototype.createBody = function () {
+ var result;
+ if (this.data === undefined) {
+ result = '';
+ }
+ else {
+ result = JSON.stringify(this.data);
+ }
+ return result;
+};
+
+HttpRequest.prototype.createTransport = function () {
+ return new XMLHttpRequest();
+};
+
+HttpRequest.prototype.createCallback = function () {
+ var self = this;
+ // NOTE use onload/onerror pair instead of
+ // onreadystatechange in future (will break IE8 support)
+ this.transport.onreadystatechange = function() {
+ if(self.transport.readyState == 4) {
+ self.transport.onreadystatechange = null;
+ if (self.isNetworkError()) {
+ self.handleNetworkError();
+ }
+ else {
+ self.handleResponse();
+ }
+ }
+ };
+};
+
+HttpRequest.prototype.isNetworkError = function () {
+ // NOTE works incorrectly with file:// protocol
+ return !this.transport.status;
+};
+
+HttpRequest.prototype.handleNetworkError = function () {
+ this.clearTimeout();
+ this.cb(new errors.NetworkError());
+};
+
+HttpRequest.prototype.sendInternal = function () {
+ if (this.method == 'get') {
+ this.sendGet();
+ }
+ else {
+ this.sendPost();
+ }
+};
+
+HttpRequest.prototype.sendGet = function () {
+ this.transport.open('GET', this.createGetUrl());
+ this.setHttpHeaders();
+ this.transport.send();
+};
+
+HttpRequest.prototype.createGetUrl = function () {
+ var parts = this.createGetUrlParts();
+ var result = this.url;
+ if (parts.length > 0) {
+ result += '?' + parts.join('&');
+ }
+ return result;
+};
+
+HttpRequest.prototype.createGetUrlParts = function (opt_parts) {
+ var parts = opt_parts || [];
+ if (this.body) {
+ parts.push(this.bodyUrlKey + '=' + encodeURIComponent(this.body));
+ }
+ return parts;
+};
+
+HttpRequest.prototype.sendPost = function () {
+ var transport = this.transport;
+ transport.open('POST', this.url);
+ this.setContentTypeHttpHeader();
+ this.setMethodHttpHeader();
+ this.setHttpHeaders();
+ transport.send(this.body);
+};
+
+HttpRequest.prototype.setHttpHeaders = function () {
+ var headers = this.createHttpHeaders();
+ var transport = this.transport;
+ for (var k in headers) {
+ transport.setRequestHeader(k, headers[k]);
+ }
+};
+
+HttpRequest.prototype.createHttpHeaders = function () {
+ var headers = this.headers;
+ var result = headers.http || {};
+ delete headers.http;
+ if (headers.auth) {
+ result[this.authHttpHeaderName] = headers.auth;
+ }
+ if (headers.authExpected) {
+ result[this.authExpectedHttpHeaderName] = headers.authExpected;
+ }
+ return result;
+};
+
+HttpRequest.prototype.setContentTypeHttpHeader = function () {
+ var contentType = this.headers.http && this.headers.http['Content-type'] || this.headers.contentType || this.defaultContentType;
+ this.transport.setRequestHeader('Content-type', contentType);
+};
+
+HttpRequest.prototype.setMethodHttpHeader = function () {
+ if (this.method != 'create') {
+ this.transport.setRequestHeader(this.methodHttpHeaderName, this.method);
+ }
+};
+
+HttpRequest.prototype.handleResponse = function () {
+ this.clearTimeout();
+ var result = this.createResult();
+ var err = this.extractError(result);
+ if (err != null) {
+ this.cb(err);
+ }
+ else {
+ this.cb(null, result);
+ }
+};
+
+HttpRequest.prototype.createResult = function () {
+ var result = {
+ headers: this.getResultHeaders(),
+ data: this.getResultData()
+ };
+ var status = this.getResultStatus();
+ if (status != null) {
+ result.status = status;
+ }
+ var transport = this.getTransportForResult();
+ if (transport != null) {
+ result.transport = transport;
+ }
+ return result;
+};
+
+HttpRequest.prototype.getTransportForResult = function () {
+ return this.transport;
+};
+
+HttpRequest.prototype.getResultStatus = function () {
+ return this.transport.status;
+};
+
+HttpRequest.prototype.getResultHeaders = function () {
+ var result = {};
+ var authRenewalToken = this.transport.getResponseHeader(this.authRenewalHttpHeaderName);
+ if (authRenewalToken) {
+ var renewal = {
+ token: authRenewalToken
+ };
+ result.authRenewal = renewal;
+ var issued = this.transport.getResponseHeader(this.authRenewalIssuedHttpHeaderName);
+ if (issued) {
+ renewal.issued = new Date(issued);
+ }
+ var maxAge = this.transport.getResponseHeader(this.authRenewalMaxAgeHttpHeaderName);
+ if (maxAge) {
+ renewal.maxAge = parseInt(maxAge, 10);
+ }
+ }
+ return result;
+};
+
+HttpRequest.prototype.getResultData = function () {
+ var body = this.getResultBody();
+ return body ? JSON.parse(body) : undefined;
+};
+
+HttpRequest.prototype.getResultBody = function () {
+ return this.transport.responseText;
+};
+
+HttpRequest.prototype.extractError = function (result) {
+ var status = result.status;
+ var err = null;
+ if (status != 200 && status != 204) {
+ err = new errors.WebError(result);
+ }
+ return err;
+};
+
+
+module.exports = HttpRequest;
+
+},{"./errors":3}],7:[function(require,module,exports){
+"use strict";
+var errors = require('./errors');
+
+
+var Socket = function (opt_basePath, opt_socketUri, opt_protocols) {
+ this.basePath = opt_basePath;
+ this.socketUri = opt_socketUri || this.defaultSocketUri;
+ this.protocols = opt_protocols;
+
+ this.onSocketCreated = null;
+ this.onMessage = null;
+
+ // NOTE you can set this property to use sockjs or whatever
+ this.customWebSocketClass = null;
+
+ this.socket = null;
+ this.requests = {};
+
+ this.abortFunc = this.createAbortFunc();
+};
+
+Socket.prototype.defaultSocketUri = '/socket';
+Socket.prototype.headerSeparator = '\n\n';
+Socket.prototype.defaultTimeout = null;
+
+Socket.prototype.getWebSocketClass = function () {
+ return this.customWebSocketClass || WebSocket;
+};
+
+Socket.prototype.createAbortFunc = function () {
+ var self = this;
+ return function () {
+ self.handleAbort(this);
+ };
+};
+
+Socket.prototype.createSocket = function () {
+ var WebSocketClass = this.getWebSocketClass();
+ return new WebSocketClass(this.socketUri, this.protocols);
+};
+
+Socket.prototype.connect = function (opt_cb) {
+ if (this.socket != null) {
+ switch (this.socket.readyState) {
+ case 0: // connecting
+ if (opt_cb) {