Files
kami-parse-server/src/Routers/FunctionsRouter.js

218 lines
6.0 KiB
JavaScript
Raw Normal View History

// FunctionsRouter.js
2016-02-19 23:47:44 -05:00
var Parse = require('parse/node').Parse,
triggers = require('../triggers');
2016-02-19 23:47:44 -05:00
import PromiseRouter from '../PromiseRouter';
import {
promiseEnforceMasterKeyAccess,
promiseEnsureIdempotency,
} from '../middlewares';
import { jobStatusHandler } from '../StatusHandler';
import _ from 'lodash';
import { logger } from '../logger';
function parseObject(obj) {
if (Array.isArray(obj)) {
2020-07-13 17:13:08 -05:00
return obj.map(item => {
return parseObject(item);
});
} 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-02-19 23:47:44 -05:00
export class FunctionsRouter extends PromiseRouter {
mountRoutes() {
this.route(
'POST',
'/functions/:functionName',
Add idempotency (#6748) * added idempotency router and middleware * added idempotency rules for routes classes, functions, jobs, installaions, users * fixed typo * ignore requests without header * removed unused var * enabled feature only for MongoDB * changed code comment * fixed inconsistend storage adapter specification * Trigger notification * Travis CI trigger * Travis CI trigger * Travis CI trigger * rebuilt option definitions * fixed incorrect import path * added new request ID header to allowed headers * fixed typescript typos * add new system class to spec helper * fixed typescript typos * re-added postgres conn parameter * removed postgres conn parameter * fixed incorrect schema for index creation * temporarily disabling index creation to fix postgres issue * temporarily disabling index creation to fix postgres issue * temporarily disabling index creation to fix postgres issue * temporarily disabling index creation to fix postgres issue * temporarily disabling index creation to fix postgres issue * temporarily disabling index creation to fix postgres issue * temporarily disabling index creation to fix postgres issue * trying to fix postgres issue * fixed incorrect auth when writing to _Idempotency * trying to fix postgres issue * Travis CI trigger * added test cases * removed number grouping * fixed test description * trying to fix postgres issue * added Github readme docs * added change log * refactored tests; fixed some typos * fixed test case * fixed default TTL value * Travis CI Trigger * Travis CI Trigger * Travis CI Trigger * added test case to increase coverage * Trigger Travis CI * changed configuration syntax to use regex; added test cases * removed unused vars * removed IdempotencyRouter * Trigger Travis CI * updated docs * updated docs * updated docs * updated docs * update docs * Trigger Travis CI * fixed coverage * removed code comments
2020-07-15 20:10:33 +02:00
promiseEnsureIdempotency,
FunctionsRouter.handleCloudFunction
);
this.route(
'POST',
'/jobs/:jobName',
Add idempotency (#6748) * added idempotency router and middleware * added idempotency rules for routes classes, functions, jobs, installaions, users * fixed typo * ignore requests without header * removed unused var * enabled feature only for MongoDB * changed code comment * fixed inconsistend storage adapter specification * Trigger notification * Travis CI trigger * Travis CI trigger * Travis CI trigger * rebuilt option definitions * fixed incorrect import path * added new request ID header to allowed headers * fixed typescript typos * add new system class to spec helper * fixed typescript typos * re-added postgres conn parameter * removed postgres conn parameter * fixed incorrect schema for index creation * temporarily disabling index creation to fix postgres issue * temporarily disabling index creation to fix postgres issue * temporarily disabling index creation to fix postgres issue * temporarily disabling index creation to fix postgres issue * temporarily disabling index creation to fix postgres issue * temporarily disabling index creation to fix postgres issue * temporarily disabling index creation to fix postgres issue * trying to fix postgres issue * fixed incorrect auth when writing to _Idempotency * trying to fix postgres issue * Travis CI trigger * added test cases * removed number grouping * fixed test description * trying to fix postgres issue * added Github readme docs * added change log * refactored tests; fixed some typos * fixed test case * fixed default TTL value * Travis CI Trigger * Travis CI Trigger * Travis CI Trigger * added test case to increase coverage * Trigger Travis CI * changed configuration syntax to use regex; added test cases * removed unused vars * removed IdempotencyRouter * Trigger Travis CI * updated docs * updated docs * updated docs * updated docs * update docs * Trigger Travis CI * fixed coverage * removed code comments
2020-07-15 20:10:33 +02:00
promiseEnsureIdempotency,
promiseEnforceMasterKeyAccess,
2020-07-13 13:06:52 -05:00
function (req) {
return FunctionsRouter.handleCloudJob(req);
}
);
2020-07-13 13:06:52 -05:00
this.route('POST', '/jobs', promiseEnforceMasterKeyAccess, function (req) {
return FunctionsRouter.handleCloudJob(req);
});
2016-02-19 23:47:44 -05: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,
headers: req.config.headers,
ip: req.config.ip,
jobName,
message: jobHandler.setMessage.bind(jobHandler),
};
2020-07-13 17:13:08 -05:00
return jobHandler.setRunning(jobName, params).then(jobStatus => {
request.jobId = jobStatus.objectId;
// run the function async
process.nextTick(() => {
Promise.resolve()
.then(() => {
return jobFunction(request);
})
.then(
2020-07-13 17:13:08 -05:00
result => {
jobHandler.setSucceeded(result);
},
2020-07-13 17:13:08 -05:00
error => {
jobHandler.setFailed(error);
}
);
});
return {
headers: {
'X-Parse-Job-Status-Id': jobStatus.objectId,
},
response: {},
};
});
}
static createResponseObject(resolve, reject, message) {
2016-02-19 23:47:44 -05:00
return {
2020-07-13 13:06:52 -05:00
success: function (result) {
2016-02-19 23:47:44 -05:00
resolve({
response: {
result: Parse._encode(result),
},
2016-02-19 23:47:44 -05:00
});
},
2020-07-13 13:06:52 -05: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;
}
reject(new Parse.Error(code, message));
},
message: message,
};
2016-02-19 23:47:44 -05:00
}
2016-02-19 23:47:44 -05:00
static handleCloudFunction(req) {
const functionName = req.params.functionName;
const applicationId = req.config.applicationId;
const theFunction = triggers.getFunction(functionName, applicationId);
const theValidator = triggers.getValidator(
req.params.functionName,
applicationId
);
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,
context: req.info.context,
};
2016-02-19 23:47:44 -05: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
}
}
2016-02-19 23:47:44 -05:00
2020-07-13 13:06:52 -05: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(
2020-07-13 17:13:08 -05:00
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);
}
},
2020-07-13 17:13:08 -05:00
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);
}
}
);
return Promise.resolve()
.then(() => {
return theFunction(request, { message });
})
.then(success, error);
});
2016-02-19 23:47:44 -05:00
}
}