-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (58 loc) · 1.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var path = require('path');
var net = require('net');
var spawn = require('child_process').spawn;
var dnode = require('dnode');
var PHP_ENTRY_SCRIPT = __dirname+'/php/require.php';
var parentModuleDir = path.dirname(module.parent.filename);
module.exports = requirePhp;
var loadedModules = {};
function requirePhp(modulePath, callback){
var fullpath = path.resolve(parentModuleDir, modulePath);
if(loadedModules[fullpath]){
var module = loadedModules[fullpath];
process.nextTick(function(){
callback(module);
});
}else{
createPhpModule(fullpath, function(err, module){
if(err){
return callback(err);
}
loadedModules[fullpath] = module;
callback(null, module);
});
}
}
function createPhpModule(modulePath, callback){
var d = dnode();
var server = net.createServer(function(connection){
d.pipe(connection).pipe(d);
connection.unref();
});
server.listen(0, '127.0.0.1', function(){
var port = server.address().port;
var childProcess = spawn('php', [PHP_ENTRY_SCRIPT, modulePath, port], {
stdio: ['ignore', null, null]
});
childProcess.stdout.pipe(process.stdout);
childProcess.stderr.pipe(process.stderr);
childProcess.stdout.unref();
childProcess.stderr.unref();
childProcess.on('error', function(err){
console.error(err);
});
childProcess.on('exit', function(code){
server.close();
});
childProcess.unref();
});
d.on('remote', function(remote){
remote.unref = function(){
server.unref();
};
remote.close = function(){
server.close();
};
callback(null, remote);
});
}