CLI for parse-live-query-server (#2765)

* adds CLI for parse-live-query-server, adds ability to start parse-server with live-query server

* Don't crash when the message is badly formatted
This commit is contained in:
Florent Vilmart
2016-09-24 13:34:05 -04:00
committed by GitHub
parent a41cbcbc7f
commit 2183b84565
9 changed files with 281 additions and 115 deletions

3
bin/parse-live-query-server Executable file
View File

@@ -0,0 +1,3 @@
#!/usr/bin/env node
require("../lib/cli/parse-live-query-server");

View File

@@ -62,7 +62,13 @@ class ParseLiveQueryServer {
// to the subscribers and the handler will be called. // to the subscribers and the handler will be called.
this.subscriber.on('message', (channel, messageStr) => { this.subscriber.on('message', (channel, messageStr) => {
logger.verbose('Subscribe messsage %j', messageStr); logger.verbose('Subscribe messsage %j', messageStr);
let message = JSON.parse(messageStr); let message;
try {
message = JSON.parse(messageStr);
} catch(e) {
logger.error('unable to parse message', messageStr, e);
return;
}
this._inflateParseObject(message); this._inflateParseObject(message);
if (channel === 'afterSave') { if (channel === 'afterSave') {
this._onAfterSave(message); this._onAfterSave(message);
@@ -229,7 +235,12 @@ class ParseLiveQueryServer {
_onConnect(parseWebsocket: any): void { _onConnect(parseWebsocket: any): void {
parseWebsocket.on('message', (request) => { parseWebsocket.on('message', (request) => {
if (typeof request === 'string') { if (typeof request === 'string') {
try {
request = JSON.parse(request); request = JSON.parse(request);
} catch(e) {
logger.error('unable to parse request', request, e);
return;
}
} }
logger.verbose('Request: %j', request); logger.verbose('Request: %j', request);

View File

@@ -0,0 +1,48 @@
import {
numberParser,
numberOrBoolParser,
objectParser,
arrayParser,
moduleOrObjectParser,
booleanParser,
nullParser
} from '../utils/parsers';
export default {
"appId": {
required: true,
help: "Required. This string should match the appId in use by your Parse Server. If you deploy the LiveQuery server alongside Parse Server, the LiveQuery server will try to use the same appId."
},
"masterKey": {
required: true,
help: "Required. This string should match the masterKey in use by your Parse Server. If you deploy the LiveQuery server alongside Parse Server, the LiveQuery server will try to use the same masterKey."
},
"serverURL": {
required: true,
help: "Required. This string should match the serverURL in use by your Parse Server. If you deploy the LiveQuery server alongside Parse Server, the LiveQuery server will try to use the same serverURL."
},
"redisURL": {
help: "Optional. This string should match the masterKey in use by your Parse Server. If you deploy the LiveQuery server alongside Parse Server, the LiveQuery server will try to use the same masterKey."
},
"keyPairs": {
help: "Optional. A JSON object that serves as a whitelist of keys. It is used for validating clients when they try to connect to the LiveQuery server. Check the following Security section and our protocol specification for details."
},
"websocketTimeout": {
help: "Optional. Number of milliseconds between ping/pong frames. The WebSocket server sends ping/pong frames to the clients to keep the WebSocket alive. This value defines the interval of the ping/pong frame from the server to clients. Defaults to 10 * 1000 ms (10 s).",
action: numberParser("websocketTimeout")
},
"cacheTimeout": {
help: "Optional. Number in milliseconds. When clients provide the sessionToken to the LiveQuery server, the LiveQuery server will try to fetch its ParseUser's objectId from parse server and store it in the cache. The value defines the duration of the cache. Check the following Security section and our protocol specification for details. Defaults to 30 * 24 * 60 * 60 * 1000 ms (~30 days).",
action: numberParser("cacheTimeout")
},
"logLevel": {
help: "Optional. This string defines the log level of the LiveQuery server. We support VERBOSE, INFO, ERROR, NONE. Defaults to INFO.",
},
"port": {
env: "PORT",
help: "The port to run the ParseServer. defaults to 1337.",
default: 1337,
action: numberParser("port")
},
};

View File

@@ -1,52 +1,13 @@
function numberParser(key) { import {
return function(opt) { numberParser,
opt = parseInt(opt); numberOrBoolParser,
if (!Number.isInteger(opt)) { objectParser,
throw new Error(`The ${key} is invalid`); arrayParser,
} moduleOrObjectParser,
return opt; booleanParser,
} nullParser
} } from '../utils/parsers';
function numberOrBoolParser(key) {
return function(opt) {
if (typeof opt === 'boolean') {
return opt;
}
return numberParser(key)(opt);
}
}
function objectParser(opt) {
if (typeof opt == 'object') {
return opt;
}
return JSON.parse(opt)
}
function moduleOrObjectParser(opt) {
if (typeof opt == 'object') {
return opt;
}
try {
return JSON.parse(opt);
} catch(e) {}
return opt;
}
function booleanParser(opt) {
if (opt == true || opt == "true" || opt == "1") {
return true;
}
return false;
}
function nullParser(opt) {
if (opt == 'null') {
return null;
}
return opt;
}
export default { export default {
"appId": { "appId": {
@@ -128,9 +89,7 @@ export default {
env: "PARSE_SERVER_FACEBOOK_APP_IDS", env: "PARSE_SERVER_FACEBOOK_APP_IDS",
help: "Comma separated list for your facebook app Ids", help: "Comma separated list for your facebook app Ids",
type: "list", type: "list",
action: function(opt) { action: arrayParser
return opt.split(",")
}
}, },
"enableAnonymousUsers": { "enableAnonymousUsers": {
env: "PARSE_SERVER_ENABLE_ANON_USERS", env: "PARSE_SERVER_ENABLE_ANON_USERS",
@@ -239,5 +198,24 @@ export default {
"cluster": { "cluster": {
help: "Run with cluster, optionally set the number of processes default to os.cpus().length", help: "Run with cluster, optionally set the number of processes default to os.cpus().length",
action: numberOrBoolParser("cluster") action: numberOrBoolParser("cluster")
} },
"liveQuery.classNames": {
help: "parse-server's LiveQuery configuration object",
action: objectParser
},
"liveQuery.classNames": {
help: "parse-server's LiveQuery classNames",
action: arrayParser
},
"liveQuery.redisURL": {
help: "parse-server's LiveQuery redisURL",
},
"startLiveQueryServer": {
help: "Starts the liveQuery server",
action: booleanParser
},
"liveQueryServerOptions": {
help: "Live query server configuration options (will start the liveQuery server)",
action: objectParser
},
}; };

View File

@@ -0,0 +1,15 @@
import definitions from './definitions/parse-live-query-server';
import runner from './utils/runner';
import { ParseServer } from '../index';
import express from 'express';
runner({
definitions,
start: function(program, options, logOptions) {
logOptions();
var app = express();
var httpServer = require('http').createServer(app);
httpServer.listen(options.port);
ParseServer.createLiveQueryServer(httpServer, options);
}
})

View File

@@ -1,18 +1,12 @@
import path from 'path'; import path from 'path';
import express from 'express'; import express from 'express';
import { ParseServer } from '../index'; import { ParseServer } from '../index';
import definitions from './cli-definitions'; import definitions from './definitions/parse-server';
import program from './utils/commander';
import { mergeWithOptions } from './utils/commander';
import cluster from 'cluster'; import cluster from 'cluster';
import os from 'os'; import os from 'os';
import runner from './utils/runner';
program.loadDefinitions(definitions); const help = function(){
program
.usage('[options] <path/to/configuration.json>');
program.on('--help', function(){
console.log(' Get Started guide:'); console.log(' Get Started guide:');
console.log(''); console.log('');
console.log(' Please have a look at the get started guide!') console.log(' Please have a look at the get started guide!')
@@ -32,12 +26,33 @@ program.on('--help', function(){
console.log(' $ parse-server -- --appId APP_ID --masterKey MASTER_KEY --serverURL serverURL'); console.log(' $ parse-server -- --appId APP_ID --masterKey MASTER_KEY --serverURL serverURL');
console.log(' $ parse-server -- --appId APP_ID --masterKey MASTER_KEY --serverURL serverURL'); console.log(' $ parse-server -- --appId APP_ID --masterKey MASTER_KEY --serverURL serverURL');
console.log(''); console.log('');
};
function startServer(options, callback) {
const app = express();
const api = new ParseServer(options);
app.use(options.mountPath, api);
var server = app.listen(options.port, callback);
if (options.startLiveQueryServer || options.liveQueryServerOptions) {
ParseServer.createLiveQueryServer(server, options.liveQueryServerOptions);
}
var handleShutdown = function() {
console.log('Termination signal received. Shutting down.');
server.close(function () {
process.exit(0);
}); });
};
process.on('SIGTERM', handleShutdown);
process.on('SIGINT', handleShutdown);
}
program.parse(process.argv, process.env);
let options = program.getOptions();
runner({
definitions,
help,
usage: '[options] <path/to/configuration.json>',
start: function(program, options, logOptions) {
if (!options.serverURL) { if (!options.serverURL) {
options.serverURL = `http://localhost:${options.port}${options.mountPath}`; options.serverURL = `http://localhost:${options.port}${options.mountPath}`;
} }
@@ -50,37 +65,20 @@ if (!options.appId || !options.masterKey || !options.serverURL) {
process.exit(1); process.exit(1);
} }
function logStartupOptions(options) { if (options["liveQuery.classNames"]) {
for (let key in options) { options.liveQuery = options.liveQuery || {};
let value = options[key]; options.liveQuery.classNames = options["liveQuery.classNames"];
if (key == "masterKey") { delete options["liveQuery.classNames"];
value = "***REDACTED***";
} }
console.log(`${key}: ${value}`); if (options["liveQuery.redisURL"]) {
} options.liveQuery = options.liveQuery || {};
} options.liveQuery.redisURL = options["liveQuery.redisURL"];
delete options["liveQuery.redisURL"];
function startServer(options, callback) {
const app = express();
const api = new ParseServer(options);
app.use(options.mountPath, api);
var server = app.listen(options.port, callback);
var handleShutdown = function() {
console.log('Termination signal received. Shutting down.');
server.close(function () {
process.exit(0);
});
};
process.on('SIGTERM', handleShutdown);
process.on('SIGINT', handleShutdown);
} }
if (options.cluster) { if (options.cluster) {
const numCPUs = typeof options.cluster === 'number' ? options.cluster : os.cpus().length; const numCPUs = typeof options.cluster === 'number' ? options.cluster : os.cpus().length;
if (cluster.isMaster) { if (cluster.isMaster) {
logStartupOptions(options);
for(var i = 0; i < numCPUs; i++) { for(var i = 0; i < numCPUs; i++) {
cluster.fork(); cluster.fork();
} }
@@ -95,9 +93,13 @@ if (options.cluster) {
} }
} else { } else {
startServer(options, () => { startServer(options, () => {
logStartupOptions(options); logOptions();
console.log(''); console.log('');
console.log('['+process.pid+'] parse-server running on '+options.serverURL); console.log('['+process.pid+'] parse-server running on '+options.serverURL);
}); });
} }
}
})

59
src/cli/utils/parsers.js Normal file
View File

@@ -0,0 +1,59 @@
export function numberParser(key) {
return function(opt) {
opt = parseInt(opt);
if (!Number.isInteger(opt)) {
throw new Error(`The ${key} is invalid`);
}
return opt;
}
}
export function numberOrBoolParser(key) {
return function(opt) {
if (typeof opt === 'boolean') {
return opt;
}
return numberParser(key)(opt);
}
}
export function objectParser(opt) {
if (typeof opt == 'object') {
return opt;
}
return JSON.parse(opt)
}
export function arrayParser(opt) {
if (Array.isArray(opt)) {
return opt;
} else if (typeof opt === 'string') {
return opt.split(',');
} else {
throw new Error(`${opt} should be a comma separated string or an array`);
}
}
export function moduleOrObjectParser(opt) {
if (typeof opt == 'object') {
return opt;
}
try {
return JSON.parse(opt);
} catch(e) {}
return opt;
}
export function booleanParser(opt) {
if (opt == true || opt == "true" || opt == "1") {
return true;
}
return false;
}
export function nullParser(opt) {
if (opt == 'null') {
return null;
}
return opt;
}

37
src/cli/utils/runner.js Normal file
View File

@@ -0,0 +1,37 @@
import program from './commander';
import { mergeWithOptions } from './commander';
function logStartupOptions(options) {
for (let key in options) {
let value = options[key];
if (key == "masterKey") {
value = "***REDACTED***";
}
if (typeof value === 'object') {
value = JSON.stringify(value);
}
console.log(`${key}: ${value}`);
}
}
export default function({
definitions,
help,
usage,
start
}) {
program.loadDefinitions(definitions);
if (usage) {
program.usage(usage);
}
if (help) {
program.on('--help', help);
}
program.parse(process.argv, process.env);
let options = program.getOptions();
start(program, options, function() {
logStartupOptions(options);
});
}

View File

@@ -1,5 +1,18 @@
'use strict'; 'use strict';
let logger; import defaults from './defaults';
import { WinstonLoggerAdapter } from './Adapters/Logger/WinstonLoggerAdapter';
import { LoggerController } from './Controllers/LoggerController';
function defaultLogger() {
let adapter = new WinstonLoggerAdapter({
logsFolder: defaults.logsFolder,
jsonLogs: defaults.jsonLogs,
verbose: defaults.verbose,
silent: defaults.silent });
return new LoggerController(adapter);
}
let logger = defaultLogger();
export function setLogger(aLogger) { export function setLogger(aLogger) {
logger = aLogger; logger = aLogger;