UNPKG

kyoto-client

Version:

Client for Kyoto Tycoon

164 lines 5.55 kB
var RestClient, http; var __slice = Array.prototype.slice; http = require('http'); RestClient = (function() { function RestClient(port, host) { this.port = port; this.host = host; this; } RestClient.prototype._extractArgs = function(args) { switch (args.length) { case 1: return [{}, args[0]]; case 2: return args; default: throw new Error("Invalid number of arguments (" + args.length + ")"); } }; RestClient.prototype._expirationTime = function(date_or_number) { if (typeof date_or_number === 'number') { return Math.round((Date.now() + (1000 * date_or_number)) / 1000); } else if (date_or_number instanceof Date) { return date_or_number.toUTCString(); } else { throw new Error("Invalid expires value, must be Date or Number"); } }; RestClient.prototype._buildPath = function(params, key) { var path; path = "/" + (encodeURIComponent(key)); if (params.DB) { path = ("/" + (encodeURIComponent(params.DB))) + path; } return path; }; RestClient.prototype.get = function() { var args, callback, key, params, request, _ref; key = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; _ref = this._extractArgs(args), params = _ref[0], callback = _ref[1]; request = { host: this.host, port: this.port, path: this._buildPath(params, key), headers: { 'Content-Length': 0, 'Connection': 'keep-alive' } }; return http.get(request, function(response) { var content_length, expires, offset, value; content_length = parseInt(response.headers['content-length'], 10); expires = response.headers.hasOwnProperty('x-kt-xt') ? new Date(response.headers['x-kt-xt']) : null; value = new Buffer(content_length); offset = 0; response.on('data', function(chunk) { chunk.copy(value, offset, 0); return offset += chunk.length; }); return response.on('end', function() { switch (response.statusCode) { case 200: return callback(void 0, value, expires); case 404: return callback(void 0, null, expires); default: return callback(new Error("Unexpected response from server: " + response.statusCode)); } }); }).on('error', function(error) { return callback(error); }); }; RestClient.prototype.head = function() { var args, callback, key, options, params, _ref; key = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; _ref = this._extractArgs(args), params = _ref[0], callback = _ref[1]; options = { host: this.host, port: this.port, method: 'HEAD', path: this._buildPath(params, key), headers: { 'Content-Length': 0, 'Connection': 'keep-alive' } }; return http.request(options, function(response) { return response.on('end', function() { switch (response.statusCode) { case 200: return callback(void 0, response.headers); case 404: return callback(void 0, null); default: return callback(new Error("Unexpected response from server: " + response.statusCode)); } }); }).on('error', function(error) { return callback(error); }).end(); }; RestClient.prototype.put = function() { var args, callback, key, options, params, value, _ref; key = arguments[0], value = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : []; _ref = this._extractArgs(args), params = _ref[0], callback = _ref[1]; options = { host: this.host, port: this.port, method: 'PUT', path: this._buildPath(params, key), headers: { 'Content-Length': typeof value === 'string' ? Buffer.byteLength(value) : value.length, 'Connection': 'keep-alive' } }; if (params.xt != null) { options.headers['X-Kt-Xt'] = this._expirationTime(params.xt); } return http.request(options, function(response) { return response.on('end', function() { switch (response.statusCode) { case 201: return callback(); default: return callback(new Error("Unexpected response from server: " + response.statusCode)); } }); }).on('error', function(error) { return callback(error); }).end(value); }; RestClient.prototype["delete"] = function() { var args, callback, key, options, params, _ref; key = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; _ref = this._extractArgs(args), params = _ref[0], callback = _ref[1]; options = { host: this.host, port: this.port, method: 'DELETE', path: this._buildPath(params, key), headers: { 'Content-Length': 0, 'Connection': 'keep-alive' } }; return http.request(options, function(response) { return response.on('end', function() { switch (response.statusCode) { case 204: return callback(); case 404: return callback(new Error("Record not found")); default: return callback(new Error("Unexpected response from server: " + response.statusCode)); } }); }).on('error', function(error) { return callback(error); }).end(); }; return RestClient; })(); module.exports = RestClient;