2016-02-05 14:38:09 -05:00
|
|
|
// FunctionsRouter.js
|
2016-02-19 23:47:44 -05:00
|
|
|
|
2016-11-24 15:47:41 -05:00
|
|
|
var Parse = require('parse/node').Parse,
|
|
|
|
|
triggers = require('../triggers');
|
2016-02-19 23:47:44 -05:00
|
|
|
|
|
|
|
|
import PromiseRouter from '../PromiseRouter';
|
2016-08-30 07:19:21 -04:00
|
|
|
import { promiseEnforceMasterKeyAccess } from '../middlewares';
|
|
|
|
|
import { jobStatusHandler } from '../StatusHandler';
|
2016-06-10 03:37:05 +08:00
|
|
|
import _ from 'lodash';
|
2016-07-23 20:10:06 +02:00
|
|
|
import { logger } from '../logger';
|
2016-06-10 03:37:05 +08:00
|
|
|
|
2016-07-19 02:14:32 -04:00
|
|
|
function parseObject(obj) {
|
|
|
|
|
if (Array.isArray(obj)) {
|
2016-11-24 15:47:41 -05:00
|
|
|
return obj.map((item) => {
|
|
|
|
|
return parseObject(item);
|
|
|
|
|
});
|
2016-07-19 02:14:32 -04:00
|
|
|
} else if (obj && obj.__type == 'Date') {
|
|
|
|
|
return Object.assign(new Date(obj.iso), obj);
|
|
|
|
|
} else if (obj && obj.__type == 'File') {
|
|
|
|
|
return Parse.File.fromJSON(obj);
|
|
|
|
|
} else if (obj && typeof obj === 'object') {
|
|
|
|
|
return parseParams(obj);
|
|
|
|
|
} else {
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parseParams(params) {
|
|
|
|
|
return _.mapValues(params, parseObject);
|
2016-06-10 03:37:05 +08:00
|
|
|
}
|
2016-02-19 23:47:44 -05:00
|
|
|
|
|
|
|
|
export class FunctionsRouter extends PromiseRouter {
|
2016-04-22 08:20:14 +12:00
|
|
|
|
2016-02-19 23:47:44 -05:00
|
|
|
mountRoutes() {
|
|
|
|
|
this.route('POST', '/functions/:functionName', FunctionsRouter.handleCloudFunction);
|
2016-08-30 07:19:21 -04:00
|
|
|
this.route('POST', '/jobs/:jobName', promiseEnforceMasterKeyAccess, function(req) {
|
|
|
|
|
return FunctionsRouter.handleCloudJob(req);
|
|
|
|
|
});
|
|
|
|
|
this.route('POST', '/jobs', promiseEnforceMasterKeyAccess, function(req) {
|
|
|
|
|
return FunctionsRouter.handleCloudJob(req);
|
|
|
|
|
});
|
2016-02-19 23:47:44 -05:00
|
|
|
}
|
2016-04-22 08:20:14 +12:00
|
|
|
|
2016-08-30 07:19:21 -04:00
|
|
|
static handleCloudJob(req) {
|
|
|
|
|
const jobName = req.params.jobName || req.body.jobName;
|
|
|
|
|
const applicationId = req.config.applicationId;
|
|
|
|
|
const jobHandler = jobStatusHandler(req.config);
|
|
|
|
|
const jobFunction = triggers.getJob(jobName, applicationId);
|
|
|
|
|
if (!jobFunction) {
|
|
|
|
|
throw new Parse.Error(Parse.Error.SCRIPT_FAILED, 'Invalid job.');
|
|
|
|
|
}
|
|
|
|
|
let params = Object.assign({}, req.body, req.query);
|
|
|
|
|
params = parseParams(params);
|
|
|
|
|
const request = {
|
|
|
|
|
params: params,
|
|
|
|
|
log: req.config.loggerController,
|
2017-10-18 14:13:09 +02:00
|
|
|
headers: req.config.headers,
|
|
|
|
|
ip: req.config.ip,
|
2018-08-06 17:39:38 -04:00
|
|
|
jobName,
|
2016-08-30 07:19:21 -04:00
|
|
|
message: jobHandler.setMessage.bind(jobHandler)
|
2018-08-06 17:39:38 -04:00
|
|
|
};
|
|
|
|
|
|
2016-11-24 15:47:41 -05:00
|
|
|
return jobHandler.setRunning(jobName, params).then((jobStatus) => {
|
2016-08-30 07:19:21 -04:00
|
|
|
request.jobId = jobStatus.objectId
|
|
|
|
|
// run the function async
|
2016-11-24 15:47:41 -05:00
|
|
|
process.nextTick(() => {
|
2018-08-06 17:39:38 -04:00
|
|
|
Promise.resolve().then(() => {
|
|
|
|
|
return jobFunction(request);
|
|
|
|
|
}).then((result) => {
|
|
|
|
|
jobHandler.setSucceeded(result);
|
|
|
|
|
}, (error) => {
|
|
|
|
|
jobHandler.setFailed(error);
|
|
|
|
|
});
|
2016-08-30 07:19:21 -04:00
|
|
|
});
|
|
|
|
|
return {
|
|
|
|
|
headers: {
|
|
|
|
|
'X-Parse-Job-Status-Id': jobStatus.objectId
|
|
|
|
|
},
|
|
|
|
|
response: {}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static createResponseObject(resolve, reject, message) {
|
2016-02-19 23:47:44 -05:00
|
|
|
return {
|
|
|
|
|
success: function(result) {
|
|
|
|
|
resolve({
|
|
|
|
|
response: {
|
|
|
|
|
result: Parse._encode(result)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
2018-08-06 17:39:38 -04:00
|
|
|
error: function(message) {
|
|
|
|
|
// parse error, process away
|
|
|
|
|
if (message instanceof Parse.Error) {
|
|
|
|
|
return reject(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const code = Parse.Error.SCRIPT_FAILED;
|
|
|
|
|
// If it's an error, mark it as a script failed
|
|
|
|
|
if (typeof message === 'string') {
|
|
|
|
|
return reject(new Parse.Error(code, message));
|
|
|
|
|
}
|
|
|
|
|
if (message instanceof Error) {
|
|
|
|
|
message = message.message;
|
2016-06-01 10:28:06 -04:00
|
|
|
}
|
|
|
|
|
reject(new Parse.Error(code, message));
|
2016-08-30 07:19:21 -04:00
|
|
|
},
|
|
|
|
|
message: message
|
2016-02-19 23:47:44 -05:00
|
|
|
}
|
|
|
|
|
}
|
2016-04-22 08:20:14 +12:00
|
|
|
|
2016-02-19 23:47:44 -05:00
|
|
|
static handleCloudFunction(req) {
|
2016-08-30 07:19:21 -04:00
|
|
|
const functionName = req.params.functionName;
|
|
|
|
|
const applicationId = req.config.applicationId;
|
|
|
|
|
const theFunction = triggers.getFunction(functionName, applicationId);
|
|
|
|
|
const theValidator = triggers.getValidator(req.params.functionName, applicationId);
|
2018-08-06 17:39:38 -04:00
|
|
|
if (!theFunction) {
|
|
|
|
|
throw new Parse.Error(Parse.Error.SCRIPT_FAILED, `Invalid function: "${functionName}"`);
|
|
|
|
|
}
|
|
|
|
|
let params = Object.assign({}, req.body, req.query);
|
|
|
|
|
params = parseParams(params);
|
|
|
|
|
const request = {
|
|
|
|
|
params: params,
|
|
|
|
|
master: req.auth && req.auth.isMaster,
|
|
|
|
|
user: req.auth && req.auth.user,
|
|
|
|
|
installationId: req.info.installationId,
|
|
|
|
|
log: req.config.loggerController,
|
|
|
|
|
headers: req.config.headers,
|
|
|
|
|
ip: req.config.ip,
|
|
|
|
|
functionName
|
|
|
|
|
};
|
2016-02-19 23:47:44 -05:00
|
|
|
|
2018-08-06 17:39:38 -04:00
|
|
|
if (theValidator && typeof theValidator === "function") {
|
|
|
|
|
var result = theValidator(request);
|
|
|
|
|
if (!result) {
|
|
|
|
|
throw new Parse.Error(Parse.Error.VALIDATION_ERROR, 'Validation failed.');
|
2016-02-19 23:47:44 -05:00
|
|
|
}
|
2018-08-06 17:39:38 -04:00
|
|
|
}
|
2016-02-19 23:47:44 -05:00
|
|
|
|
2018-08-06 17:39:38 -04:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
|
const userString = (req.auth && req.auth.user) ? req.auth.user.id : undefined;
|
|
|
|
|
const cleanInput = logger.truncateLogMessage(JSON.stringify(params));
|
|
|
|
|
const { success, error, message } = FunctionsRouter.createResponseObject((result) => {
|
|
|
|
|
try {
|
|
|
|
|
const cleanResult = logger.truncateLogMessage(JSON.stringify(result.response.result));
|
|
|
|
|
logger.info(
|
|
|
|
|
`Ran cloud function ${functionName} for user ${userString} with:\n Input: ${cleanInput }\n Result: ${cleanResult }`,
|
|
|
|
|
{
|
|
|
|
|
functionName,
|
|
|
|
|
params,
|
|
|
|
|
user: userString,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
resolve(result);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
reject(e);
|
|
|
|
|
}
|
|
|
|
|
}, (error) => {
|
|
|
|
|
try {
|
|
|
|
|
logger.error(
|
|
|
|
|
`Failed running cloud function ${functionName} for user ${userString} with:\n Input: ${cleanInput}\n Error: ` + JSON.stringify(error),
|
|
|
|
|
{
|
|
|
|
|
functionName,
|
|
|
|
|
error,
|
|
|
|
|
params,
|
|
|
|
|
user: userString
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
reject(error);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
reject(e);
|
|
|
|
|
}
|
2016-02-19 23:47:44 -05:00
|
|
|
});
|
2018-08-06 17:39:38 -04:00
|
|
|
return Promise.resolve().then(() => {
|
|
|
|
|
return theFunction(request, { message });
|
|
|
|
|
}).then(success, error);
|
|
|
|
|
});
|
2016-02-19 23:47:44 -05:00
|
|
|
}
|
|
|
|
|
}
|