|
| 1 | +var net = require('net'); |
| 2 | +var fs = require('fs'); |
| 3 | + |
| 4 | +var bundlePath = '../../app/assets/webpack/'; |
| 5 | +var bundleFileName = 'webpack-bundle.js'; |
| 6 | + |
| 7 | +var currentArg; |
| 8 | + |
| 9 | +function Handler() { |
| 10 | + this.queue = []; |
| 11 | + this.initialized = false; |
| 12 | +} |
| 13 | + |
| 14 | +Handler.prototype.handle = function (connection) { |
| 15 | + var callback = function () { |
| 16 | + connection.setEncoding('utf8'); |
| 17 | + connection.on('data', (data)=> { |
| 18 | + console.log('Processing request: ' + data); |
| 19 | + var result = eval(data); |
| 20 | + connection.write(result); |
| 21 | + }); |
| 22 | + }; |
| 23 | + |
| 24 | + if (this.initialized) { |
| 25 | + callback(); |
| 26 | + } else { |
| 27 | + this.queue.push(callback); |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +Handler.prototype.initialize = function () { |
| 32 | + console.log('Processing ' + this.queue.length + ' pending requests'); |
| 33 | + var callback; |
| 34 | + while (callback = this.queue.pop()) { |
| 35 | + callback(); |
| 36 | + } |
| 37 | + |
| 38 | + this.initialized = true; |
| 39 | +}; |
| 40 | + |
| 41 | +var handler = new Handler(); |
| 42 | + |
| 43 | +process.argv.forEach((val) => { |
| 44 | + if (val[0] == '-') { |
| 45 | + currentArg = val.slice(1); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (currentArg == 's') { |
| 50 | + bundleFileName = val; |
| 51 | + } |
| 52 | +}); |
| 53 | + |
| 54 | +try { |
| 55 | + fs.mkdirSync(bundlePath); |
| 56 | +} catch (e) { |
| 57 | + if (e.code != 'EEXIST') throw e; |
| 58 | +} |
| 59 | + |
| 60 | +fs.watchFile(bundlePath + bundleFileName, (curr) => { |
| 61 | + if (curr && curr.blocks && curr.blocks > 0) { |
| 62 | + if (handler.initialized) { |
| 63 | + console.log('Reloading server bundle must be implemented by restarting the node process!'); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + require(bundlePath + bundleFileName); |
| 68 | + console.log('Loaded server bundle: ' + bundlePath + bundleFileName); |
| 69 | + handler.initialize(); |
| 70 | + } |
| 71 | +}); |
| 72 | + |
| 73 | +var unixServer = net.createServer(function (connection) { |
| 74 | + handler.handle(connection); |
| 75 | +}); |
| 76 | + |
| 77 | +unixServer.listen('node.sock'); |
| 78 | + |
| 79 | +process.on('SIGINT', () => { |
| 80 | + unixServer.close(); |
| 81 | + process.exit(); |
| 82 | +}); |
0 commit comments