Add sanatizeString() method

This commit is contained in:
Bryan Ashby 2018-01-26 21:34:32 -07:00
parent cc74616a93
commit ec1876084c
1 changed files with 20 additions and 0 deletions

View File

@ -19,6 +19,7 @@ const dbs = {};
exports.getTransactionDatabase = getTransactionDatabase; exports.getTransactionDatabase = getTransactionDatabase;
exports.getModDatabasePath = getModDatabasePath; exports.getModDatabasePath = getModDatabasePath;
exports.getISOTimestampString = getISOTimestampString; exports.getISOTimestampString = getISOTimestampString;
exports.sanatizeString = sanatizeString;
exports.initializeDatabases = initializeDatabases; exports.initializeDatabases = initializeDatabases;
exports.dbs = dbs; exports.dbs = dbs;
@ -59,6 +60,25 @@ function getISOTimestampString(ts) {
return ts.format('YYYY-MM-DDTHH:mm:ss.SSSZ'); return ts.format('YYYY-MM-DDTHH:mm:ss.SSSZ');
} }
function sanatizeString(s) {
return s.replace(/[\0\x08\x09\x1a\n\r"'\\%]/g, c => { // eslint-disable-line no-control-regex
switch (c) {
case '\0' : return '\\0';
case '\x08' : return '\\b';
case '\x09' : return '\\t';
case '\x1a' : return '\\z';
case '\n' : return '\\n';
case '\r' : return '\\r';
case '"' :
case '\'' :
case '\\' :
case '%' :
return `\\${c}`;
}
});
}
function initializeDatabases(cb) { function initializeDatabases(cb) {
async.eachSeries( [ 'system', 'user', 'message', 'file' ], (dbName, next) => { async.eachSeries( [ 'system', 'user', 'message', 'file' ], (dbName, next) => {
dbs[dbName] = sqlite3Trans.wrap(new sqlite3.Database(getDatabasePath(dbName), err => { dbs[dbName] = sqlite3Trans.wrap(new sqlite3.Database(getDatabasePath(dbName), err => {