[Groonga-commit] droonga/express-droonga at a2881e2 [master] Define RequestResponseHTTPCommand type commands.

Back to archive index

YUKI Hiroshi null+****@clear*****
Mon Dec 16 14:09:57 JST 2013


YUKI Hiroshi	2013-12-16 14:09:57 +0900 (Mon, 16 Dec 2013)

  New Revision: a2881e2efb05833bc518c5b0334015d3fdc95a6f
  https://github.com/droonga/express-droonga/commit/a2881e2efb05833bc518c5b0334015d3fdc95a6f

  Message:
    Define RequestResponseHTTPCommand type commands.
    
    This change breaks backward compatibility which use "HTTPCommand". They should be updted to use the RequestResponseHTTPCommand type.

  Modified files:
    lib/adapter/api/droonga.js
    lib/adapter/api/groonga.js
    lib/adapter/api/rest.js
    lib/adapter/command.js
    lib/adapter/http.js

  Modified: lib/adapter/api/droonga.js (+1 -1)
===================================================================
--- lib/adapter/api/droonga.js    2013-12-16 13:39:00 +0900 (5af2e04)
+++ lib/adapter/api/droonga.js    2013-12-16 14:09:57 +0900 (28c5936)
@@ -2,7 +2,7 @@ var command = require('../command');
 
 module.exports = {
   // XXX dangerous! this must be disabled on public services.
-  'droonga': new command.HTTPCommand({
+  'droonga': new command.RequestResponseHTTPCommand({
     method: 'POST',
     path: '/droonga/:messageType',
     onRequest: function(request, connection) {

  Modified: lib/adapter/api/groonga.js (+1 -1)
===================================================================
--- lib/adapter/api/groonga.js    2013-12-16 13:39:00 +0900 (0a24dff)
+++ lib/adapter/api/groonga.js    2013-12-16 14:09:57 +0900 (2578168)
@@ -1,7 +1,7 @@
 var command = require('../command');
 
 module.exports = {
-  'groonga': new command.HTTPCommand({
+  'groonga': new command.RequestResponseHTTPCommand({
     path: '/d/:commandName',
     onRequest: function(request, connection) {
       connection.emit(request.params.commandName, request.query);

  Modified: lib/adapter/api/rest.js (+8 -8)
===================================================================
--- lib/adapter/api/rest.js    2013-12-16 13:39:00 +0900 (a1985fe)
+++ lib/adapter/api/rest.js    2013-12-16 14:09:57 +0900 (404bc89)
@@ -2,54 +2,54 @@ var command = require('../command');
 var requestBuilders = require('./rest-request-builder');
 
 module.exports = {
-//  'status': new command.HTTPCommand({
+//  'status': new command.RequestResponseHTTPCommand({
 //    path: '/status/:target',
 //    onRequest: function(request, connection) {
 //      connection.emit('status', requestBuilders.status(request));
 //    }
 //  }),
-  'search': new command.HTTPCommand({
+  'search': new command.RequestResponseHTTPCommand({
     path: '/tables/:tableName',
     onRequest: function(request, connection) {
       connection.emit('search', requestBuilders.search(request));
     }
   }) //,
-//  'createtable': new command.HTTPCommand({
+//  'createtable': new command.RequestResponseHTTPCommand({
 //    method: 'PUT',
 //    path: '/tables/:tableName',
 //    onRequest: function(request, connection) {
 //      connection.emit('createtable', requestBuilders.createTable(request));
 //    }
 //  }),
-//  'removetable': new command.HTTPCommand({
+//  'removetable': new command.RequestResponseHTTPCommand({
 //    method: 'DELETE',
 //    path: '/tables/:tableName',
 //    onRequest: function(request, connection) {
 //      connection.emit('removetable', requestBuilders.removeTable(request));
 //    }
 //  }),
-//  'createcolumn': new command.HTTPCommand({
+//  'createcolumn': new command.RequestResponseHTTPCommand({
 //    method: 'PUT',
 //    path: '/tables/:tableName/columns/:columnName',
 //    onRequest: function(request, connection) {
 //      connection.emit('createcolumn', requestBuilders.createColumn(request));
 //    }
 //  }),
-//  'removecolumn': new command.HTTPCommand({
+//  'removecolumn': new command.RequestResponseHTTPCommand({
 //    method: 'DELETE',
 //    path: '/tables/:tableName/columns/:columnName',
 //    onRequest: function(request, connection) {
 //      connection.emit('removecolumn', requestBuilders.removeColumn(request));
 //    }
 //  }),
-//  'loadrecord': new command.HTTPCommand({
+//  'loadrecord': new command.RequestResponseHTTPCommand({
 //    method: 'PUT',
 //    path: '/tables/:tableName/records/:key',
 //    onRequest: function(request, connection) {
 //      connection.emit('loadrecord', requestBuilders.loadRecord(request));
 //    }
 //  }),
-//  'loadrecords': new command.HTTPCommand({
+//  'loadrecords': new command.RequestResponseHTTPCommand({
 //    method: 'PUT',
 //    path: '/tables/:tableName/records',
 //    onRequest: function(request, connection) {

  Modified: lib/adapter/command.js (+18 -3)
===================================================================
--- lib/adapter/command.js    2013-12-16 13:39:00 +0900 (61c53cd)
+++ lib/adapter/command.js    2013-12-16 14:09:57 +0900 (7fbf1e3)
@@ -69,21 +69,36 @@ exports.PublishSubscribe = PublishSubscribe;
 
 
 function HTTPCommand(options) {
-  RequestResponse.apply(this, arguments);
+  Command.apply(this, arguments);
   this._commandTypes.push(HTTPCommand);
 }
-util.inherits(HTTPCommand, RequestResponse);
-HTTPCommand.isInstance = RequestResponse.isInstance;
+util.inherits(HTTPCommand, Command);
+HTTPCommand.isInstance = Command.isInstance;
 Object.defineProperty(HTTPCommand.prototype, 'path', {
   get: function() { return this._options.path; }
 });
 Object.defineProperty(HTTPCommand.prototype, 'method', {
   get: function() { return this._options.method || 'GET'; }
 });
+Object.defineProperty(HTTPCommand.prototype, 'onHandle', {
+  get: function() { return this._options.onHandle; }
+});
 exports.HTTPCommand = HTTPCommand;
 
 
 
+function RequestResponseHTTPCommand(options) {
+  HTTPCommand.apply(this, arguments);
+  this._commandTypes.push(RequestResponseHTTPCommand);
+  this._commandTypes.push(RequestResponse);
+}
+util.inherits(RequestResponseHTTPCommand, HTTPCommand);
+util.inherits(RequestResponseHTTPCommand, RequestResponse);
+RequestResponseHTTPCommand.isInstance = HTTPCommand.isInstance;
+exports.RequestResponseHTTPCommand = RequestResponseHTTPCommand;
+
+
+
 function SocketCommand() {
 }
 SocketCommand.isInstance = Command.isInstance;

  Modified: lib/adapter/http.js (+36 -5)
===================================================================
--- lib/adapter/http.js    2013-12-16 13:39:00 +0900 (76ffbfd)
+++ lib/adapter/http.js    2013-12-16 14:09:57 +0900 (8b5edba)
@@ -50,6 +50,26 @@ function createRequestResponseHandler(params) {
 }
 exports.createRequestResponseHandler = createRequestResponseHandler;
 
+function createGenericHandler(params) {
+  params = params || {};
+  var connection = params.connection;
+  var commandName = params.name;
+  var definition = params.definition;
+
+  return (function(request, response) {
+    debug('adapter.http.createGenericHandler.handle');
+
+    var wrappedConnection = new wrapper.DroongaProtocolConnectionWrapper(connection, options);
+    try {
+      definition.onHandle(request, response, wrappedConnection);
+    } catch(error) {
+      wrappedConnection.destroy();
+      response.jsonp(error, 500);
+    }
+  });
+}
+exports.createGenericHandler = createGenericHandler;
+
 function getRegisterationMethod(method) {
   method = method || 'GET';
   switch (method.toUpperCase()) {
@@ -87,12 +107,23 @@ exports.register = function(application, params) {
     var definition = unifiedCommandSet[commandName];
     if (!command.HTTPCommand.isInstance(definition))
       return;
+
     var method = getRegisterationMethod(definition.method);
-    var handler = createRequestResponseHandler({
-      connection:  connection,
-      name:        definition.command || commandName,
-      definition:  definition
-    });
+
+    var creator;
+    if (command.RequestResponse.isInstance(definition)) {
+      creator = createRequestResponseHandler;
+    } else {
+      if (typeof defintion.onHandle != 'function')
+        throw new Error('onHandle() is missing');
+      creator = createGenericHandler;
+    }
+
+    var handler = creator({
+        connection:  connection,
+        name:        definition.command || commandName,
+        definition:  definition
+      });
     application[method](prefix + definition.path, handler);
     registeredCommands.push({ name:       commandName,
                               definition: definition,
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index