// Load dependencies
var app = require('electron').app,
    ejs = require('ejs'),
    mime = require('mime')
    fs = require('fs'),
    url = require('url')

// Private variables
var protocol, // will be set to the protocol module, which is only available inside `app.on('ready', ...`
    userOpts = {}


// Helper functions
var compileEjs = function(contentBuffer) {
    var contentString = contentBuffer.toString(),
        compiledEjs = ejs.render(contentString, userOpts)
    
    return new Buffer(compiledEjs)
}

var protocolListener = function(request, callback) {
    try {
        var fileName = url.parse(request.url).pathname,
            fileContents = fs.readFileSync(fileName),
            extension = fileName.split('.').pop(),
            mimeType = mime.lookup(extension)
        
        if (extension === 'ejs') {
            userOpts.filename = fileName
            userOpts.ejse = this
            fileContents = compileEjs(fileContents)
            mimeType = 'text/html'
        }
        
        return callback({
            data: fileContents,
            mimeType: mimeType
        })
        
    } catch(exception) {
        return callback(-6) // NET_ERROR(FILE_NOT_FOUND, -6)
    }
}


// Module setup
var EJSE = function() {
    var self = this
    app.on('ready', function() {
        protocol = require('protocol')
        self.listen()
    })
}
EJSE.prototype = {
    
    // Start intercepting requests, looking for '.ejs' files.
    listen: function() {
        if (!protocol) return this
        protocol.interceptBufferProtocol('file', protocolListener.bind(this))
        return this
    },
    
    // Set the options to be passed in to `ejs.render()`.
    setOptions: function(optsIn) {
        userOpts = optsIn || {}
        return this
    },
    
    // Stop intercepting requests, restoring the original `file://` protocol handler.
    stopListening: function() {
        if (!protocol) return this
        
        protocol.uninterceptProtocol('file')
        return this
    }
}


// Expose stuff
module.exports = new EJSE()

@@ -49,16 +49,61 @@
 ').app,%0A
+    protocol = require('electron').protocol,%0A
     ejs 
@@ -116,24 +116,24 @@
 ire('ejs'),%0A
-
     mime = r
@@ -226,114 +226,8 @@
 var 
-protocol, // will be set to the protocol module, which is only available inside %60app.on('ready', ...%60%0A    
 user
@@ -237,17 +237,16 @@
 s = %7B%7D%0A%0A
-%0A
 // Helpe
@@ -408,21 +408,17 @@
 erOpts)%0A
-    
 %0A
+
     retu
@@ -717,25 +717,17 @@
 ension)%0A
-        
 %0A
+
         
@@ -924,25 +924,17 @@
       %7D%0A
-        
 %0A
+
         
@@ -1025,25 +1025,17 @@
      %7D)%0A
-        
 %0A
+
     %7D ca
@@ -1219,47 +1219,8 @@
 ) %7B%0A
-        protocol = require('protocol')%0A
     
@@ -1265,21 +1265,17 @@
 ype = %7B%0A
-    
 %0A
+
     // S
@@ -1485,37 +1485,33 @@
 urn this%0A    %7D,%0A
-    
 %0A
+
     // Set the o
@@ -1646,21 +1646,17 @@
 %0A    %7D,%0A
-    
 %0A
+
     // S
@@ -1800,24 +1800,16 @@
 rn this%0A
-        
 %0A       

Fix loading of protocol module for Electron 1.0