'use strict';


function jsonPlugin(rail, opt_options) {
  opt_options = opt_options || {};

  rail.use('buffer');

  var pluginOptions = {
    auto: opt_options.auto || false,
    max: opt_options.max || 1048576 // 1 MiB
  };


  function interceptResponse(call, options, response) {
    if (response.buffer) {
      if (response.buffer.length > pluginOptions.max) {
        call.emit('warn', 'json', 'blocked', 'max length exceeded');
      } else {
        try {
          response.json = JSON.parse(response.buffer);
        } catch (err) {
          call.emit('warn', 'json', 'failed', 'parse error');
        }
      }
    }
    call.__emit('response', response);
  }


  rail.on('plugin-response', function(call, options, response) {
    if (options.json || pluginOptions.auto &&
        response.headers['content-type'] === 'application/json') {
      rail.plugins.buffer.intercept(call);
      call.__intercept('response', interceptResponse);
    }
  });

  return pluginOptions;
}
module.exports = jsonPlugin;

@@ -9,16 +9,17 @@
 ict';%0A%0A%0A
+%0A
 function
@@ -19,20 +19,20 @@
 unction 
-json
+JSON
 Plugin(r
@@ -36,20 +36,16 @@
 n(rail, 
-opt_
 options)
@@ -53,103 +53,150 @@
 %7B%0A  
-opt_options = opt_options %7C%7C %7B%7D;%0A%0A  rail.use('buffer');%0A%0A  var pluginOptions = %7B%0A    auto: opt_
+if (!(this instanceof JSONPlugin)) %7B%0A    return new JSONPlugin(rail, options);%0A  %7D%0A  rail.use('buffer');%0A%0A  this._rail = rail;%0A%0A  this.auto = 
 opti
@@ -216,23 +216,23 @@
 alse
-,%0A    max: opt_
+;%0A  this.max = 
 opti
@@ -249,16 +249,17 @@
  1048576
+;
  // 1 Mi
@@ -264,73 +264,710 @@
 MiB%0A
+%0A
   
-%7D;%0A%0A%0A  function interceptResponse(call, options, response) %7B%0A  
+this._setup();%0A%7D%0Amodule.exports = JSONPlugin;%0A%0A%0AJSONPlugin.prototype._setup = function() %7B%0A  var self = this;%0A  var rail = this._rail;%0A%0A  this._rail.on('plugin-response', function(call, options, response) %7B%0A    if (options.json %7C%7C self.auto &&%0A        response.headers%5B'content-type'%5D === 'application/json') %7B%0A      self.intercept(call);%0A    %7D%0A  %7D);%0A%0A  this._intercept = function(call, options, response) %7B%0A    self._interceptResponse(call, options, response);%0A  %7D;%0A%7D;%0A%0A%0AJSONPlugin.prototype.intercept = function(call) %7B%0A  this._rail.plugins.buffer.intercept(call);%0A  call.__intercept('response', this._intercept);%0A%7D;%0A%0A%0AJSONPlugin.prototype._interceptResponse = function(call, options, response) %7B%0A
   if
@@ -983,26 +983,25 @@
 e.buffer) %7B%0A
-  
+%0A
     if (resp
@@ -1021,28 +1021,19 @@
 ength %3E 
-pluginOption
+thi
 s.max) %7B
@@ -1029,26 +1029,24 @@
 this.max) %7B%0A
-  
       call.e
@@ -1096,26 +1096,25 @@
 exceeded');%0A
-  
+%0A
     %7D else %7B
@@ -1124,18 +1124,14 @@
     
-  
 try %7B%0A
-  
     
@@ -1185,18 +1185,16 @@
 ;%0A      
-  
 %7D catch 
@@ -1197,26 +1197,24 @@
 tch (err) %7B%0A
-  
         call
@@ -1265,18 +1265,8 @@
 ');%0A
-        %7D%0A
     
@@ -1277,16 +1277,19 @@
     %7D%0A  
+%7D%0A%0A
   call._
@@ -1321,354 +1321,7 @@
 e);%0A
-  %7D%0A%0A%0A  rail.on('plugin-response', function(call, options, response) %7B%0A    if (options.json %7C%7C pluginOptions.auto &&%0A        response.headers%5B'content-type'%5D === 'application/json') %7B%0A      rail.plugins.buffer.intercept(call);%0A      call.__intercept('response', interceptResponse);%0A    %7D%0A  %7D);%0A%0A  return pluginOptions;%0A%7D%0Amodule.exports = jsonPlugin
+%7D
 ;%0A

rewrite as class