-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathFunctionsRouter.js
195 lines (185 loc) · 5.92 KB
/
FunctionsRouter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// FunctionsRouter.js
var Parse = require('parse/node').Parse,
triggers = require('../triggers');
import PromiseRouter from '../PromiseRouter';
import { promiseEnforceMasterKeyAccess, promiseEnsureIdempotency } from '../middlewares';
import { jobStatusHandler } from '../StatusHandler';
import _ from 'lodash';
import { logger } from '../logger';
function parseObject(obj, config) {
if (Array.isArray(obj)) {
return obj.map(item => {
return parseObject(item, config);
});
} 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 && obj.__type == 'Pointer' && config.encodeParseObjectInCloudFunction) {
return Parse.Object.fromJSON({
__type: 'Pointer',
className: obj.className,
objectId: obj.objectId,
});
} else if (obj && typeof obj === 'object') {
return parseParams(obj, config);
} else {
return obj;
}
}
function parseParams(params, config) {
return _.mapValues(params, item => parseObject(item, config));
}
export class FunctionsRouter extends PromiseRouter {
mountRoutes() {
this.route(
'POST',
'/functions/:functionName',
promiseEnsureIdempotency,
FunctionsRouter.handleCloudFunction
);
this.route(
'POST',
'/jobs/:jobName',
promiseEnsureIdempotency,
promiseEnforceMasterKeyAccess,
function (req) {
return FunctionsRouter.handleCloudJob(req);
}
);
this.route('POST', '/jobs', promiseEnforceMasterKeyAccess, function (req) {
return FunctionsRouter.handleCloudJob(req);
});
}
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, req.config);
const request = {
params: params,
log: req.config.loggerController,
headers: req.config.headers,
ip: req.config.ip,
jobName,
message: jobHandler.setMessage.bind(jobHandler),
};
return jobHandler.setRunning(jobName, params).then(jobStatus => {
request.jobId = jobStatus.objectId;
// run the function async
process.nextTick(() => {
Promise.resolve()
.then(() => {
return jobFunction(request);
})
.then(
result => {
jobHandler.setSucceeded(result);
},
error => {
jobHandler.setFailed(error);
}
);
});
return {
headers: {
'X-Parse-Job-Status-Id': jobStatus.objectId,
},
response: {},
};
});
}
static createResponseObject(resolve, reject) {
return {
success: function (result) {
resolve({
response: {
result: Parse._encode(result),
},
});
},
error: function (message) {
const error = triggers.resolveError(message);
reject(error);
},
};
}
static handleCloudFunction(req) {
const functionName = req.params.functionName;
const applicationId = req.config.applicationId;
const theFunction = triggers.getFunction(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, req.config);
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,
};
return new Promise(function (resolve, reject) {
const userString = req.auth && req.auth.user ? req.auth.user.id : undefined;
const { success, error } = FunctionsRouter.createResponseObject(
result => {
try {
if (req.config.logLevels.cloudFunctionSuccess !== 'silent') {
const cleanInput = logger.truncateLogMessage(JSON.stringify(params));
const cleanResult = logger.truncateLogMessage(JSON.stringify(result.response.result));
logger[req.config.logLevels.cloudFunctionSuccess](
`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 {
if (req.config.logLevels.cloudFunctionError !== 'silent') {
const cleanInput = logger.truncateLogMessage(JSON.stringify(params));
logger[req.config.logLevels.cloudFunctionError](
`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 triggers.maybeRunValidator(request, functionName, req.auth);
})
.then(() => {
return theFunction(request);
})
.then(success, error);
});
}
}