Files
kami-parse-server/src/ParseServer.js

563 lines
19 KiB
JavaScript
Raw Normal View History

2016-03-23 19:05:56 -04:00
// ParseServer - open-source compatible API Server for Parse apps
var batch = require('./batch'),
bodyParser = require('body-parser'),
express = require('express'),
middlewares = require('./middlewares'),
Parse = require('parse/node').Parse,
{ parse } = require('graphql'),
path = require('path'),
fs = require('fs');
import { ParseServerOptions, LiveQueryServerOptions } from './Options';
import defaults from './defaults';
import * as logging from './logger';
import Config from './Config';
import PromiseRouter from './PromiseRouter';
import requiredParameter from './requiredParameter';
import { AnalyticsRouter } from './Routers/AnalyticsRouter';
import { ClassesRouter } from './Routers/ClassesRouter';
import { FeaturesRouter } from './Routers/FeaturesRouter';
import { FilesRouter } from './Routers/FilesRouter';
import { FunctionsRouter } from './Routers/FunctionsRouter';
import { GlobalConfigRouter } from './Routers/GlobalConfigRouter';
GraphQL Configuration Options (#5782) * add parse-graph-ql configuration for class schema customisation Not yet tested - essentially an RFC * refactor and add graphql router, controller and config cache * fix(GraphQLController): add missing check isEnabled * chore(GraphQLController): remove awaits from cache put * chore(GraphQLController): remove check for if its enabled * refactor(GraphQLController): only use cache if mounted * chore(GraphQLController): group all validation errors and throw at once * chore(GraphQLSchema): move transformations into controller validation * refactor(GraphQL): improve ctrl validation and fix schema usage of config * refactor(GraphQLSchema): remove code related to additional schema This code has been moved into a separate feature branch. * fix(GraphQLSchema): fix incorrect default return type for class configs * refactor(GraphQLSchema): update staleness check code to account for config * fix(GraphQLServer): fix regressed tests due to internal schema changes This will be followed up with a backwards compatability fix for the `ClassFields` issue to avoid breakages for our users * refactor: rename to ParseGraphQLController for consistency * fix(ParseGraphQLCtrl): numerous fixes for validity checking Also includes some minor code refactoring * chore(GraphQL): minor syntax cleanup * fix(SchemaController): add _GraphQLConfig to volatile classes * refactor(ParseGraphQLServer): return update config value in setGraphQLConfig * testing(ParseGraphQL): add test cases for new graphQLConfig * fix(GraphQLController): fix issue where config with multiple items was not being mapped to the db * fix(postgres): add _GraphQLConfig default schema on load fixes failing postgres tests * GraphQL @mock directive (#5836) * Add mock directive * Include tests for @mock directive * Fix existing tests due to the change from ClassFields to ClassCreateFields * fix(parseClassMutations): safer type transformation based on input type * fix(parseClassMutations): only define necessary input fields * fix(GraphQL): fix incorrect import paths
2019-07-25 20:46:25 +01:00
import { GraphQLRouter } from './Routers/GraphQLRouter';
import { HooksRouter } from './Routers/HooksRouter';
import { IAPValidationRouter } from './Routers/IAPValidationRouter';
import { InstallationsRouter } from './Routers/InstallationsRouter';
import { LogsRouter } from './Routers/LogsRouter';
2016-03-22 22:09:22 -07:00
import { ParseLiveQueryServer } from './LiveQuery/ParseLiveQueryServer';
Add page localization (#7128) * added localized pages; added refactored page templates; adapted test cases; introduced localization test cases * added changelog entry * fixed test description typo * fixed bug in PromiseRouter where headers are not added for text reponse * added page parameters in page headers for programmatic use * refactored tests for PublicAPIRouter * added mustache lib for template rendering * fixed fs.promises module reference * fixed template placeholder typo * changed redirect response to provide headers instead of query parameters * fix lint * fixed syntax errors and typos in html templates * removed obsolete URI encoding * added locale inferring from request body and header * added end-to-end localizaton test * added server option validation; refactored pages server option * fixed invalid redirect URL for no locale matching file * added end-to-end localizaton tests * adapted tests to new response content * re-added PublicAPIRouter; added PagesRouter as experimental feature * refactored PagesRouter test structure * added configuration option for custom path to pages * added configuration option for custom endpoint to pages * fixed lint * added tests * added a distinct page for invalid password reset link * renamed generic page invalidLink to expiredVerificationLink * improved HTML files documentation * improved HTML files documentation * changed changelog entry for experimental feature * improved file naming to make it more descriptive * fixed file naming and env parameter naming * added readme entry * fixed readme TOC - hasn't been updated in a while * added localization with JSON resource * added JSON localization to feature pages (password reset, email verification) * updated readme * updated readme * optimized JSON localization for feature pages; added e2e test case * fixed readme typo * minor refactoring of existing tests * fixed bug where Object type was not recognized as config key type * added feature config placeholders * prettier * added passing locale to page config placeholder callback * refactored passing locale to placeholder to pass test * added config placeholder feature to README * fixed typo in README
2021-02-09 14:03:57 +01:00
import { PagesRouter } from './Routers/PagesRouter';
import { PublicAPIRouter } from './Routers/PublicAPIRouter';
import { PushRouter } from './Routers/PushRouter';
import { CloudCodeRouter } from './Routers/CloudCodeRouter';
import { RolesRouter } from './Routers/RolesRouter';
import { SchemasRouter } from './Routers/SchemasRouter';
import { SessionsRouter } from './Routers/SessionsRouter';
import { UsersRouter } from './Routers/UsersRouter';
import { PurgeRouter } from './Routers/PurgeRouter';
import { AudiencesRouter } from './Routers/AudiencesRouter';
import { AggregateRouter } from './Routers/AggregateRouter';
import { ParseServerRESTController } from './ParseServerRESTController';
import * as controllers from './Controllers';
import { ParseGraphQLServer } from './GraphQL/ParseGraphQLServer';
import { SecurityRouter } from './Routers/SecurityRouter';
import CheckRunner from './Security/CheckRunner';
import Deprecator from './Deprecator/Deprecator';
import { DefinedSchemas } from './SchemaMigrations/DefinedSchemas';
2016-03-23 19:05:56 -04:00
// Mutate the Parse object to add the Cloud Code handlers
addParseCloud();
// ParseServer works like a constructor of an express app.
// https://parseplatform.org/parse-server/api/master/ParseServerOptions.html
2016-03-23 19:05:56 -04:00
class ParseServer {
/**
* @constructor
* @param {ParseServerOptions} options the parse server initialization options
*/
constructor(options: ParseServerOptions) {
// Scan for deprecated Parse Server options
Deprecator.scanParseServerOptions(options);
// Set option defaults
injectDefaults(options);
const {
appId = requiredParameter('You must provide an appId!'),
masterKey = requiredParameter('You must provide a masterKey!'),
javascriptKey,
serverURL = requiredParameter('You must provide a serverURL!'),
} = options;
2016-03-23 19:05:56 -04:00
// Initialize the node client SDK automatically
Parse.initialize(appId, javascriptKey || 'unused', masterKey);
Parse.serverURL = serverURL;
Config.validateOptions(options);
const allControllers = controllers.getControllers(options);
options.state = 'initialized';
this.config = Config.put(Object.assign({}, options, allControllers));
logging.setLogger(allControllers.loggerController);
}
2016-03-23 19:05:56 -04:00
/**
* Starts Parse Server as an express app; this promise resolves when Parse Server is ready to accept requests.
*/
async start() {
try {
if (this.config.state === 'ok') {
return this;
}
this.config.state = 'starting';
Config.put(this.config);
const {
databaseController,
hooksController,
cloud,
security,
schema,
cacheAdapter,
liveQueryController,
} = this.config;
try {
await databaseController.performInitialization();
} catch (e) {
if (e.code !== Parse.Error.DUPLICATE_VALUE) {
throw e;
}
}
await hooksController.load();
const startupPromises = [];
if (schema) {
startupPromises.push(new DefinedSchemas(schema, this.config).execute());
}
if (cacheAdapter?.connect && typeof cacheAdapter.connect === 'function') {
startupPromises.push(cacheAdapter.connect());
}
startupPromises.push(liveQueryController.connect());
await Promise.all(startupPromises);
if (cloud) {
addParseCloud();
if (typeof cloud === 'function') {
await Promise.resolve(cloud(Parse));
} else if (typeof cloud === 'string') {
let json;
if (process.env.npm_package_json) {
json = require(process.env.npm_package_json);
}
if (process.env.npm_package_type === 'module' || json?.type === 'module') {
await import(path.resolve(process.cwd(), cloud));
} else {
require(path.resolve(process.cwd(), cloud));
}
} else {
throw "argument 'cloud' must either be a string or a function";
}
await new Promise(resolve => setTimeout(resolve, 10));
}
if (security && security.enableCheck && security.enableCheckLog) {
new CheckRunner(security).run();
}
this.config.state = 'ok';
Config.put(this.config);
return this;
} catch (error) {
console.error(error);
this.config.state = 'error';
throw error;
}
2016-03-23 19:05:56 -04:00
}
get app() {
if (!this._app) {
this._app = ParseServer.app(this.config);
}
return this._app;
2016-03-23 19:05:56 -04:00
}
handleShutdown() {
const promises = [];
const { adapter: databaseAdapter } = this.config.databaseController;
if (databaseAdapter && typeof databaseAdapter.handleShutdown === 'function') {
promises.push(databaseAdapter.handleShutdown());
}
const { adapter: fileAdapter } = this.config.filesController;
if (fileAdapter && typeof fileAdapter.handleShutdown === 'function') {
promises.push(fileAdapter.handleShutdown());
}
const { adapter: cacheAdapter } = this.config.cacheController;
if (cacheAdapter && typeof cacheAdapter.handleShutdown === 'function') {
promises.push(cacheAdapter.handleShutdown());
}
if (this.liveQueryServer?.server?.close) {
promises.push(new Promise(resolve => this.liveQueryServer.server.close(resolve)));
}
if (this.liveQueryServer) {
promises.push(this.liveQueryServer.shutdown());
}
return (promises.length > 0 ? Promise.all(promises) : Promise.resolve()).then(() => {
if (this.config.serverCloseComplete) {
this.config.serverCloseComplete();
}
});
}
/**
* @static
* Create an express app for the parse server
* @param {Object} options let you specify the maxUploadSize when creating the express app */
Add page localization (#7128) * added localized pages; added refactored page templates; adapted test cases; introduced localization test cases * added changelog entry * fixed test description typo * fixed bug in PromiseRouter where headers are not added for text reponse * added page parameters in page headers for programmatic use * refactored tests for PublicAPIRouter * added mustache lib for template rendering * fixed fs.promises module reference * fixed template placeholder typo * changed redirect response to provide headers instead of query parameters * fix lint * fixed syntax errors and typos in html templates * removed obsolete URI encoding * added locale inferring from request body and header * added end-to-end localizaton test * added server option validation; refactored pages server option * fixed invalid redirect URL for no locale matching file * added end-to-end localizaton tests * adapted tests to new response content * re-added PublicAPIRouter; added PagesRouter as experimental feature * refactored PagesRouter test structure * added configuration option for custom path to pages * added configuration option for custom endpoint to pages * fixed lint * added tests * added a distinct page for invalid password reset link * renamed generic page invalidLink to expiredVerificationLink * improved HTML files documentation * improved HTML files documentation * changed changelog entry for experimental feature * improved file naming to make it more descriptive * fixed file naming and env parameter naming * added readme entry * fixed readme TOC - hasn't been updated in a while * added localization with JSON resource * added JSON localization to feature pages (password reset, email verification) * updated readme * updated readme * optimized JSON localization for feature pages; added e2e test case * fixed readme typo * minor refactoring of existing tests * fixed bug where Object type was not recognized as config key type * added feature config placeholders * prettier * added passing locale to page config placeholder callback * refactored passing locale to placeholder to pass test * added config placeholder feature to README * fixed typo in README
2021-02-09 14:03:57 +01:00
static app(options) {
const { maxUploadSize = '20mb', appId, directAccess, pages, rateLimit = [] } = options;
2016-03-23 19:05:56 -04:00
// This app serves the Parse API directly.
// It's the equivalent of https://api.parse.com/1 in the hosted Parse API.
var api = express();
//api.use("/apps", express.static(__dirname + "/public"));
api.use(middlewares.allowCrossDomain(appId));
2016-03-23 19:05:56 -04:00
// File handling needs to be before default middlewares are applied
api.use(
'/',
new FilesRouter().expressRouter({
maxUploadSize: maxUploadSize,
})
);
api.use('/health', function (req, res) {
res.status(options.state === 'ok' ? 200 : 503);
if (options.state === 'starting') {
res.set('Retry-After', 1);
}
res.json({
status: options.state,
});
});
Add page localization (#7128) * added localized pages; added refactored page templates; adapted test cases; introduced localization test cases * added changelog entry * fixed test description typo * fixed bug in PromiseRouter where headers are not added for text reponse * added page parameters in page headers for programmatic use * refactored tests for PublicAPIRouter * added mustache lib for template rendering * fixed fs.promises module reference * fixed template placeholder typo * changed redirect response to provide headers instead of query parameters * fix lint * fixed syntax errors and typos in html templates * removed obsolete URI encoding * added locale inferring from request body and header * added end-to-end localizaton test * added server option validation; refactored pages server option * fixed invalid redirect URL for no locale matching file * added end-to-end localizaton tests * adapted tests to new response content * re-added PublicAPIRouter; added PagesRouter as experimental feature * refactored PagesRouter test structure * added configuration option for custom path to pages * added configuration option for custom endpoint to pages * fixed lint * added tests * added a distinct page for invalid password reset link * renamed generic page invalidLink to expiredVerificationLink * improved HTML files documentation * improved HTML files documentation * changed changelog entry for experimental feature * improved file naming to make it more descriptive * fixed file naming and env parameter naming * added readme entry * fixed readme TOC - hasn't been updated in a while * added localization with JSON resource * added JSON localization to feature pages (password reset, email verification) * updated readme * updated readme * optimized JSON localization for feature pages; added e2e test case * fixed readme typo * minor refactoring of existing tests * fixed bug where Object type was not recognized as config key type * added feature config placeholders * prettier * added passing locale to page config placeholder callback * refactored passing locale to placeholder to pass test * added config placeholder feature to README * fixed typo in README
2021-02-09 14:03:57 +01:00
api.use(
'/',
bodyParser.urlencoded({ extended: false }),
pages.enableRouter
? new PagesRouter(pages).expressRouter()
: new PublicAPIRouter().expressRouter()
);
2016-03-23 19:05:56 -04:00
api.use(bodyParser.json({ type: '*/*', limit: maxUploadSize }));
2016-03-23 19:05:56 -04:00
api.use(middlewares.allowMethodOverride);
api.use(middlewares.handleParseHeaders);
const routes = Array.isArray(rateLimit) ? rateLimit : [rateLimit];
for (const route of routes) {
middlewares.addRateLimit(route, options);
}
api.use(middlewares.handleParseSession);
2016-03-23 19:05:56 -04:00
2016-12-07 15:17:05 -08:00
const appRouter = ParseServer.promiseRouter({ appId });
api.use(appRouter.expressRouter());
api.use(middlewares.handleParseErrors);
// run the following when not testing
if (!process.env.TESTING) {
//This causes tests to spew some useless warnings, so disable in test
/* istanbul ignore next */
process.on('uncaughtException', err => {
if (err.code === 'EADDRINUSE') {
// user-friendly message for this common error
process.stderr.write(`Unable to listen on port ${err.port}. The port is already in use.`);
process.exit(0);
} else {
throw err;
}
});
// verify the server url after a 'mount' event is received
/* istanbul ignore next */
api.on('mount', async function () {
await new Promise(resolve => setTimeout(resolve, 1000));
ParseServer.verifyServerUrl();
});
}
if (process.env.PARSE_SERVER_ENABLE_EXPERIMENTAL_DIRECT_ACCESS === '1' || directAccess) {
Parse.CoreManager.setRESTController(ParseServerRESTController(appId, appRouter));
}
return api;
}
static promiseRouter({ appId }) {
2016-12-07 15:17:05 -08:00
const routers = [
2016-03-23 19:05:56 -04:00
new ClassesRouter(),
new UsersRouter(),
new SessionsRouter(),
new RolesRouter(),
new AnalyticsRouter(),
new InstallationsRouter(),
new FunctionsRouter(),
new SchemasRouter(),
new PushRouter(),
new LogsRouter(),
new IAPValidationRouter(),
new FeaturesRouter(),
new GlobalConfigRouter(),
GraphQL Configuration Options (#5782) * add parse-graph-ql configuration for class schema customisation Not yet tested - essentially an RFC * refactor and add graphql router, controller and config cache * fix(GraphQLController): add missing check isEnabled * chore(GraphQLController): remove awaits from cache put * chore(GraphQLController): remove check for if its enabled * refactor(GraphQLController): only use cache if mounted * chore(GraphQLController): group all validation errors and throw at once * chore(GraphQLSchema): move transformations into controller validation * refactor(GraphQL): improve ctrl validation and fix schema usage of config * refactor(GraphQLSchema): remove code related to additional schema This code has been moved into a separate feature branch. * fix(GraphQLSchema): fix incorrect default return type for class configs * refactor(GraphQLSchema): update staleness check code to account for config * fix(GraphQLServer): fix regressed tests due to internal schema changes This will be followed up with a backwards compatability fix for the `ClassFields` issue to avoid breakages for our users * refactor: rename to ParseGraphQLController for consistency * fix(ParseGraphQLCtrl): numerous fixes for validity checking Also includes some minor code refactoring * chore(GraphQL): minor syntax cleanup * fix(SchemaController): add _GraphQLConfig to volatile classes * refactor(ParseGraphQLServer): return update config value in setGraphQLConfig * testing(ParseGraphQL): add test cases for new graphQLConfig * fix(GraphQLController): fix issue where config with multiple items was not being mapped to the db * fix(postgres): add _GraphQLConfig default schema on load fixes failing postgres tests * GraphQL @mock directive (#5836) * Add mock directive * Include tests for @mock directive * Fix existing tests due to the change from ClassFields to ClassCreateFields * fix(parseClassMutations): safer type transformation based on input type * fix(parseClassMutations): only define necessary input fields * fix(GraphQL): fix incorrect import paths
2019-07-25 20:46:25 +01:00
new GraphQLRouter(),
new PurgeRouter(),
new HooksRouter(),
2017-06-21 02:54:13 -03:00
new CloudCodeRouter(),
new AudiencesRouter(),
new AggregateRouter(),
new SecurityRouter(),
2016-03-23 19:05:56 -04:00
];
2016-12-07 15:17:05 -08:00
const routes = routers.reduce((memo, router) => {
2016-03-23 19:05:56 -04:00
return memo.concat(router.routes);
}, []);
2016-12-07 15:17:05 -08:00
const appRouter = new PromiseRouter(routes, appId);
2016-03-23 19:05:56 -04:00
batch.mountOnto(appRouter);
return appRouter;
2016-03-23 19:05:56 -04:00
}
/**
* starts the parse server's express app
* @param {ParseServerOptions} options to use to start the server
* @returns {ParseServer} the parse server instance
*/
async startApp(options: ParseServerOptions) {
try {
await this.start();
} catch (e) {
console.error('Error on ParseServer.startApp: ', e);
throw e;
}
const app = express();
if (options.middleware) {
let middleware;
if (typeof options.middleware == 'string') {
middleware = require(path.resolve(process.cwd(), options.middleware));
} else {
middleware = options.middleware; // use as-is let express fail
}
app.use(middleware);
}
app.use(options.mountPath, this.app);
if (options.mountGraphQL === true || options.mountPlayground === true) {
let graphQLCustomTypeDefs = undefined;
if (typeof options.graphQLSchema === 'string') {
graphQLCustomTypeDefs = parse(fs.readFileSync(options.graphQLSchema, 'utf8'));
} else if (
typeof options.graphQLSchema === 'object' ||
typeof options.graphQLSchema === 'function'
) {
graphQLCustomTypeDefs = options.graphQLSchema;
}
const parseGraphQLServer = new ParseGraphQLServer(this, {
graphQLPath: options.graphQLPath,
playgroundPath: options.playgroundPath,
graphQLCustomTypeDefs,
});
if (options.mountGraphQL) {
parseGraphQLServer.applyGraphQL(app);
}
if (options.mountPlayground) {
parseGraphQLServer.applyPlayground(app);
}
}
const server = await new Promise(resolve => {
app.listen(options.port, options.host, function () {
resolve(this);
});
});
this.server = server;
if (options.startLiveQueryServer || options.liveQueryServerOptions) {
this.liveQueryServer = await ParseServer.createLiveQueryServer(
server,
options.liveQueryServerOptions,
options
);
}
if (options.trustProxy) {
app.set('trust proxy', options.trustProxy);
}
/* istanbul ignore next */
if (!process.env.TESTING) {
configureListeners(this);
}
this.expressApp = app;
return this;
}
/**
* Creates a new ParseServer and starts it.
* @param {ParseServerOptions} options used to start the server
* @returns {ParseServer} the parse server instance
*/
static async startApp(options: ParseServerOptions) {
const parseServer = new ParseServer(options);
return parseServer.startApp(options);
}
/**
* Helper method to create a liveQuery server
* @static
* @param {Server} httpServer an optional http server to pass
* @param {LiveQueryServerOptions} config options for the liveQueryServer
* @param {ParseServerOptions} options options for the ParseServer
* @returns {Promise<ParseLiveQueryServer>} the live query server instance
*/
static async createLiveQueryServer(
httpServer,
config: LiveQueryServerOptions,
options: ParseServerOptions
) {
if (!httpServer || (config && config.port)) {
var app = express();
httpServer = require('http').createServer(app);
httpServer.listen(config.port);
}
const server = new ParseLiveQueryServer(httpServer, config, options);
await server.connect();
return server;
2016-03-23 19:05:56 -04:00
}
static async verifyServerUrl() {
// perform a health check on the serverURL value
if (Parse.serverURL) {
const isValidHttpUrl = string => {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === 'http:' || url.protocol === 'https:';
};
const url = `${Parse.serverURL.replace(/\/$/, '')}/health`;
if (!isValidHttpUrl(url)) {
console.warn(
`\nWARNING, Unable to connect to '${Parse.serverURL}' as the URL is invalid.` +
` Cloud code and push notifications may be unavailable!\n`
);
return;
}
const request = require('./request');
const response = await request({ url }).catch(response => response);
const json = response.data || null;
const retry = response.headers?.['retry-after'];
if (retry) {
await new Promise(resolve => setTimeout(resolve, retry * 1000));
return this.verifyServerUrl();
}
if (response.status !== 200 || json?.status !== 'ok') {
/* eslint-disable no-console */
console.warn(
`\nWARNING, Unable to connect to '${Parse.serverURL}'.` +
` Cloud code and push notifications may be unavailable!\n`
);
/* eslint-enable no-console */
return;
}
return true;
}
}
2016-03-23 19:05:56 -04:00
}
function addParseCloud() {
const ParseCloud = require('./cloud-code/Parse.Cloud');
const ParseServer = require('./cloud-code/Parse.Server');
Object.defineProperty(Parse, 'Server', {
get() {
const conf = Config.get(Parse.applicationId);
return { ...conf, ...ParseServer };
},
set(newVal) {
newVal.appId = Parse.applicationId;
Config.put(newVal);
},
configurable: true,
});
2016-03-23 19:05:56 -04:00
Object.assign(Parse.Cloud, ParseCloud);
global.Parse = Parse;
}
function injectDefaults(options: ParseServerOptions) {
Object.keys(defaults).forEach(key => {
if (!Object.prototype.hasOwnProperty.call(options, key)) {
options[key] = defaults[key];
}
});
if (!Object.prototype.hasOwnProperty.call(options, 'serverURL')) {
options.serverURL = `http://localhost:${options.port}${options.mountPath}`;
}
// Reserved Characters
if (options.appId) {
const regex = /[!#$%'()*+&/:;=?@[\]{}^,|<>]/g;
if (options.appId.match(regex)) {
console.warn(
`\nWARNING, appId that contains special characters can cause issues while using with urls.\n`
);
}
}
// Backwards compatibility
if (options.userSensitiveFields) {
/* eslint-disable no-console */
!process.env.TESTING &&
console.warn(
`\nDEPRECATED: userSensitiveFields has been replaced by protectedFields allowing the ability to protect fields in all classes with CLP. \n`
);
2019-02-22 09:45:22 +00:00
/* eslint-enable no-console */
const userSensitiveFields = Array.from(
new Set([...(defaults.userSensitiveFields || []), ...(options.userSensitiveFields || [])])
);
// If the options.protectedFields is unset,
// it'll be assigned the default above.
// Here, protect against the case where protectedFields
// is set, but doesn't have _User.
if (!('_User' in options.protectedFields)) {
options.protectedFields = Object.assign({ _User: [] }, options.protectedFields);
}
options.protectedFields['_User']['*'] = Array.from(
new Set([...(options.protectedFields['_User']['*'] || []), ...userSensitiveFields])
);
}
// Merge protectedFields options with defaults.
Object.keys(defaults.protectedFields).forEach(c => {
const cur = options.protectedFields[c];
if (!cur) {
options.protectedFields[c] = defaults.protectedFields[c];
} else {
Object.keys(defaults.protectedFields[c]).forEach(r => {
const unq = new Set([
...(options.protectedFields[c][r] || []),
...defaults.protectedFields[c][r],
]);
options.protectedFields[c][r] = Array.from(unq);
});
}
});
}
// Those can't be tested as it requires a subprocess
/* istanbul ignore next */
function configureListeners(parseServer) {
const server = parseServer.server;
const sockets = {};
/* Currently, express doesn't shut down immediately after receiving SIGINT/SIGTERM if it has client connections that haven't timed out. (This is a known issue with node - https://github.com/nodejs/node/issues/2642)
This function, along with `destroyAliveConnections()`, intend to fix this behavior such that parse server will close all open connections and initiate the shutdown process as soon as it receives a SIGINT/SIGTERM signal. */
server.on('connection', socket => {
const socketId = socket.remoteAddress + ':' + socket.remotePort;
sockets[socketId] = socket;
socket.on('close', () => {
delete sockets[socketId];
});
});
const destroyAliveConnections = function () {
for (const socketId in sockets) {
try {
sockets[socketId].destroy();
} catch (e) {
/* */
}
}
};
const handleShutdown = function () {
process.stdout.write('Termination signal received. Shutting down.');
destroyAliveConnections();
server.close();
parseServer.handleShutdown();
};
process.on('SIGTERM', handleShutdown);
process.on('SIGINT', handleShutdown);
}
2016-03-23 19:05:56 -04:00
export default ParseServer;