mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-20 11:05:54 +00:00
JS: Split node utils into their own file.
This commit is contained in:
88
js/nodeutils.js
Normal file
88
js/nodeutils.js
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
var fs = require("fs");
|
||||||
|
var path = require("path");
|
||||||
|
|
||||||
|
var utils = require("./utils.js");
|
||||||
|
|
||||||
|
// Empty a directory.
|
||||||
|
var emptyPath = function(dirPath, done) {
|
||||||
|
fs.readdir(dirPath, function(err, files) {
|
||||||
|
if (err) {
|
||||||
|
done(err);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
utils.mapOr(rmPath, files.map(function(f) { return path.join(dirPath, f); }), done);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Make a directory and sub-directories.
|
||||||
|
var mkPath = function(dirPath, mode, done) {
|
||||||
|
fs.mkdir(dirPath, typeof mode === "string" ? parseInt(mode, 8) : mode, function(e) {
|
||||||
|
if (!e || e.code === "EEXIST") {
|
||||||
|
// Created or already exists, done.
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
else if (e.code === "ENOENT") {
|
||||||
|
// Missing sub dir.
|
||||||
|
|
||||||
|
mkPath(path.dirname(dirPath), mode, function(e) {
|
||||||
|
if (e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mkPath(dirPath, mode, done);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create directory if needed and empty if needed.
|
||||||
|
var resetPath = function(dirPath, mode, done) {
|
||||||
|
mkPath(dirPath, mode, function(e) {
|
||||||
|
if (e) {
|
||||||
|
done(e);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
emptyPath(dirPath, done);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Remove path recursively.
|
||||||
|
var rmPath = function(dirPath, done) {
|
||||||
|
// console.log("rmPath: %s", dirPath);
|
||||||
|
|
||||||
|
fs.lstat(dirPath, function(err, stats) {
|
||||||
|
if (err && err.code == "ENOENT") {
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
if (err) {
|
||||||
|
done(err);
|
||||||
|
}
|
||||||
|
else if (stats.isDirectory()) {
|
||||||
|
emptyPath(dirPath, function(e) {
|
||||||
|
if (e) {
|
||||||
|
done(e);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// console.log("rmdir: %s", dirPath); done();
|
||||||
|
fs.rmdir(dirPath, done);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// console.log("unlink: %s", dirPath); done();
|
||||||
|
fs.unlink(dirPath, done);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.mkPath = mkPath;
|
||||||
|
exports.resetPath = resetPath;
|
||||||
|
exports.rmPath = rmPath;
|
||||||
|
|
||||||
|
// vim:sw=2:sts=2:ts=8
|
||||||
87
js/utils.js
87
js/utils.js
@@ -1,8 +1,3 @@
|
|||||||
// YYY Should probably have two versions: node vs browser
|
|
||||||
|
|
||||||
var fs = require("fs");
|
|
||||||
var path = require("path");
|
|
||||||
|
|
||||||
Function.prototype.method = function(name,func) {
|
Function.prototype.method = function(name,func) {
|
||||||
this.prototype[name] = func;
|
this.prototype[name] = func;
|
||||||
|
|
||||||
@@ -42,84 +37,6 @@ var mapOr = function(func, array, done) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Make a directory and sub-directories.
|
|
||||||
var mkPath = function(dirPath, mode, done) {
|
|
||||||
fs.mkdir(dirPath, typeof mode === "string" ? parseInt(mode, 8) : mode, function(e) {
|
|
||||||
if (!e || e.code === "EEXIST") {
|
|
||||||
// Created or already exists, done.
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
else if (e.code === "ENOENT") {
|
|
||||||
// Missing sub dir.
|
|
||||||
|
|
||||||
mkPath(path.dirname(dirPath), mode, function(e) {
|
|
||||||
if (e) {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
mkPath(dirPath, mode, done);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// Empty a directory.
|
|
||||||
var emptyPath = function(dirPath, done) {
|
|
||||||
fs.readdir(dirPath, function(err, files) {
|
|
||||||
if (err) {
|
|
||||||
done(err);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
mapOr(rmPath, files.map(function(f) { return path.join(dirPath, f); }), done);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// Remove path recursively.
|
|
||||||
var rmPath = function(dirPath, done) {
|
|
||||||
// console.log("rmPath: %s", dirPath);
|
|
||||||
|
|
||||||
fs.lstat(dirPath, function(err, stats) {
|
|
||||||
if (err && err.code == "ENOENT") {
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
if (err) {
|
|
||||||
done(err);
|
|
||||||
}
|
|
||||||
else if (stats.isDirectory()) {
|
|
||||||
emptyPath(dirPath, function(e) {
|
|
||||||
if (e) {
|
|
||||||
done(e);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// console.log("rmdir: %s", dirPath); done();
|
|
||||||
fs.rmdir(dirPath, done);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// console.log("unlink: %s", dirPath); done();
|
|
||||||
fs.unlink(dirPath, done);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// Create directory if needed and empty if needed.
|
|
||||||
var resetPath = function(dirPath, mode, done) {
|
|
||||||
mkPath(dirPath, mode, function(e) {
|
|
||||||
if (e) {
|
|
||||||
done(e);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
emptyPath(dirPath, done);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var trace = function(comment, func) {
|
var trace = function(comment, func) {
|
||||||
return function() {
|
return function() {
|
||||||
console.log("%s: %s", trace, arguments.toString);
|
console.log("%s: %s", trace, arguments.toString);
|
||||||
@@ -151,11 +68,7 @@ var stringToHex = function (s) {
|
|||||||
}).join("");
|
}).join("");
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.emptyPath = emptyPath;
|
|
||||||
exports.mapOr = mapOr;
|
exports.mapOr = mapOr;
|
||||||
exports.mkPath = mkPath;
|
|
||||||
exports.resetPath = resetPath;
|
|
||||||
exports.rmPath = rmPath;
|
|
||||||
exports.trace = trace;
|
exports.trace = trace;
|
||||||
exports.hexToString = hexToString;
|
exports.hexToString = hexToString;
|
||||||
exports.stringToHex = stringToHex;
|
exports.stringToHex = stringToHex;
|
||||||
|
|||||||
196
test/server.js
196
test/server.js
@@ -8,148 +8,148 @@
|
|||||||
// Servers are created in tmp/server/$server
|
// Servers are created in tmp/server/$server
|
||||||
//
|
//
|
||||||
|
|
||||||
var config = require("./config.js");
|
var config = require("./config.js");
|
||||||
var utils = require("../js/utils.js");
|
var nodeutils = require("../js/nodeutils.js");
|
||||||
|
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var path = require("path");
|
var path = require("path");
|
||||||
var util = require("util");
|
var util = require("util");
|
||||||
var child = require("child_process");
|
var child = require("child_process");
|
||||||
|
|
||||||
var servers = {};
|
var servers = {};
|
||||||
|
|
||||||
// Create a server object
|
// Create a server object
|
||||||
var Server = function(name) {
|
var Server = function(name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Return a server's newcoind.cfg as string.
|
// Return a server's newcoind.cfg as string.
|
||||||
Server.method('configContent', function() {
|
Server.prototype.configContent = function() {
|
||||||
var cfg = config.servers[this.name];
|
var cfg = config.servers[this.name];
|
||||||
|
|
||||||
return Object.keys(cfg).map(function(o) {
|
return Object.keys(cfg).map(function(o) {
|
||||||
return util.format("[%s]\n%s\n", o, cfg[o]);
|
return util.format("[%s]\n%s\n", o, cfg[o]);
|
||||||
}).join("");
|
}).join("");
|
||||||
});
|
};
|
||||||
|
|
||||||
Server.method('serverPath', function() {
|
Server.prototype.serverPath = function() {
|
||||||
return "tmp/server/" + this.name;
|
return "tmp/server/" + this.name;
|
||||||
});
|
};
|
||||||
|
|
||||||
Server.method('configPath', function() {
|
Server.prototype.configPath = function() {
|
||||||
return path.join(this.serverPath(), "newcoind.cfg");
|
return path.join(this.serverPath(), "newcoind.cfg");
|
||||||
});
|
};
|
||||||
|
|
||||||
// Write a server's newcoind.cfg.
|
// Write a server's newcoind.cfg.
|
||||||
Server.method('writeConfig', function(done) {
|
Server.prototype.writeConfig = function(done) {
|
||||||
fs.writeFile(this.configPath(), this.configContent(), 'utf8', done);
|
fs.writeFile(this.configPath(), this.configContent(), 'utf8', done);
|
||||||
});
|
};
|
||||||
|
|
||||||
// Spawn the server.
|
// Spawn the server.
|
||||||
Server.method('serverSpawnSync', function() {
|
Server.prototype.serverSpawnSync = function() {
|
||||||
// Spawn in standalone mode for now.
|
// Spawn in standalone mode for now.
|
||||||
this.child = child.spawn(
|
this.child = child.spawn(
|
||||||
config.newcoind,
|
config.newcoind,
|
||||||
[
|
[
|
||||||
"-a",
|
"-a",
|
||||||
"-v",
|
"-v",
|
||||||
"--conf=newcoind.cfg"
|
"--conf=newcoind.cfg"
|
||||||
],
|
],
|
||||||
{
|
{
|
||||||
cwd: this.serverPath(),
|
cwd: this.serverPath(),
|
||||||
env: process.env,
|
env: process.env,
|
||||||
stdio: 'inherit'
|
stdio: 'inherit'
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("server: start %s: %s -a --conf=%s", this.child.pid, config.newcoind, this.configPath());
|
console.log("server: start %s: %s -a --conf=%s", this.child.pid, config.newcoind, this.configPath());
|
||||||
|
|
||||||
// By default, just log exits.
|
// By default, just log exits.
|
||||||
this.child.on('exit', function(code, signal) {
|
this.child.on('exit', function(code, signal) {
|
||||||
// If could not exec: code=127, signal=null
|
// If could not exec: code=127, signal=null
|
||||||
// If regular exit: code=0, signal=null
|
// If regular exit: code=0, signal=null
|
||||||
console.log("server: spawn: server exited code=%s: signal=%s", code, signal);
|
console.log("server: spawn: server exited code=%s: signal=%s", code, signal);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
};
|
||||||
|
|
||||||
// Prepare server's working directory.
|
// Prepare server's working directory.
|
||||||
Server.method('makeBase', function(done) {
|
Server.prototype.makeBase = function(done) {
|
||||||
var path = this.serverPath();
|
var path = this.serverPath();
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
// Reset the server directory, build it if needed.
|
// Reset the server directory, build it if needed.
|
||||||
utils.resetPath(path, '0777', function(e) {
|
nodeutils.resetPath(path, '0777', function(e) {
|
||||||
if (e) {
|
if (e) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
self.writeConfig(done);
|
self.writeConfig(done);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
};
|
||||||
|
|
||||||
// Create a standalone server.
|
// Create a standalone server.
|
||||||
// Prepare the working directory and spawn the server.
|
// Prepare the working directory and spawn the server.
|
||||||
Server.method('start', function(done) {
|
Server.prototype.start = function(done) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this.makeBase(function(e) {
|
this.makeBase(function(e) {
|
||||||
if (e) {
|
if (e) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
self.serverSpawnSync();
|
self.serverSpawnSync();
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
};
|
||||||
|
|
||||||
// Stop a standalone server.
|
// Stop a standalone server.
|
||||||
Server.method('stop', function(done) {
|
Server.prototype.stop = function(done) {
|
||||||
if (this.child) {
|
if (this.child) {
|
||||||
// Update the on exit to invoke done.
|
// Update the on exit to invoke done.
|
||||||
this.child.on('exit', function(code, signal) {
|
this.child.on('exit', function(code, signal) {
|
||||||
console.log("server: stop: server exited");
|
console.log("server: stop: server exited");
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
this.child.kill();
|
this.child.kill();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
console.log("server: stop: no such server");
|
console.log("server: stop: no such server");
|
||||||
done();
|
done('noSuchServer');
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
// Start the named server.
|
// Start the named server.
|
||||||
exports.start = function(name, done) {
|
exports.start = function(name, done) {
|
||||||
if (servers[name])
|
if (servers[name])
|
||||||
{
|
{
|
||||||
console.log("server: start: server already started.");
|
console.log("server: start: server already started.");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var server = new Server(name);
|
var server = new Server(name);
|
||||||
|
|
||||||
servers[name] = server;
|
servers[name] = server;
|
||||||
|
|
||||||
console.log("server: start: %s", JSON.stringify(server));
|
console.log("server: start: %s", JSON.stringify(server));
|
||||||
|
|
||||||
server.start(done);
|
server.start(done);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Delete the named server.
|
// Delete the named server.
|
||||||
exports.stop = function(name, done) {
|
exports.stop = function(name, done) {
|
||||||
console.log("server: stop: %s of %s", name, Object.keys(servers).toString());
|
console.log("server: stop: %s of %s", name, Object.keys(servers).toString());
|
||||||
|
|
||||||
var server = servers[name];
|
var server = servers[name];
|
||||||
if (server) {
|
if (server) {
|
||||||
server.stop(done);
|
server.stop(done);
|
||||||
delete servers[name];
|
delete servers[name];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.Server = Server;
|
exports.Server = Server;
|
||||||
|
|
||||||
// vim:ts=4
|
// vim:sw=2:sts=2:ts=8
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
var fs = require("fs");
|
|
||||||
var buster = require("buster");
|
var buster = require("buster");
|
||||||
|
|
||||||
|
var config = require("./config.js");
|
||||||
var server = require("./server.js");
|
var server = require("./server.js");
|
||||||
var remote = require("../js/remote.js");
|
var remote = require("../js/remote.js");
|
||||||
var config = require("./config.js");
|
|
||||||
|
|
||||||
// How long to wait for server to start.
|
// How long to wait for server to start.
|
||||||
var serverDelay = 1500;
|
var serverDelay = 1500;
|
||||||
|
|||||||
Reference in New Issue
Block a user