Skip to content

Commit 376b985

Browse files
committed
🚨 Throw exception if function already exists in Cloud.define
1 parent 8692269 commit 376b985

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

lib/cloud.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var debug = require('debug')('AV:LeanEngine');
33
var utils = require('./utils');
44

55
var Cloud = module.exports = {
6-
__code: {}
6+
functions: {}
77
};
88

99
Cloud.define = function(name, options, func) {
@@ -18,7 +18,11 @@ Cloud.define = function(name, options, func) {
1818
func.fetchUser = false;
1919
}
2020

21-
Cloud.__code[name] = func;
21+
if (Cloud.functions[name]) {
22+
throw new Error(`LeanEngine: ${name} already defined`);
23+
} else {
24+
Cloud.functions[name] = func;
25+
}
2226
};
2327

2428
var originalCloudRun = AV.Cloud.run;
@@ -55,7 +59,7 @@ Cloud.run = function(name, data, options) {
5559
}
5660
});
5761

58-
Cloud.__code[name](request, response);
62+
Cloud.functions[name](request, response);
5963
});
6064
}).catch(function(err) {
6165
console.log('Run function \'' + name + '\' failed with error:', err);
@@ -136,7 +140,12 @@ Cloud.logInByIdAndSessionToken = function(uid, sessionToken, fetchUser, cb) {
136140

137141
function defineClassHook(className, hook, func) {
138142
debug('define class hook: %s %s', hook, className);
139-
Cloud.__code[hook + className] = func;
143+
144+
if (Cloud.functions[hook + className]) {
145+
throw new Error(`LeanEngine: ${hook} of ${className} already defined`);
146+
} else {
147+
Cloud.functions[hook + className] = func;
148+
}
140149
}
141150

142151
function className(clazz) {

lib/leanengine.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function createCloudFunctionRouter(options) {
9191

9292
cloudFunctions.use('/_ops/metadatas', function(req, res) {
9393
if (req.AV.authMasterKey) {
94-
return resp(res, Object.keys(Cloud.__code));
94+
return resp(res, Object.keys(Cloud.functions));
9595
} else {
9696
return utils.unauthResp(res);
9797
}
@@ -145,7 +145,7 @@ function createCloudFunctionRouter(options) {
145145
});
146146
};
147147

148-
if (Cloud.__code[splited[1]] && Cloud.__code[splited[1]].fetchUser === false) {
148+
if (Cloud.functions[splited[1]] && Cloud.functions[splited[1]].fetchUser === false) {
149149
handleCloudfunction();
150150
} else {
151151
fetchUser(handleCloudfunction);
@@ -239,7 +239,7 @@ var call = function(funcName, params, user, req, options, cb) {
239239
options = {};
240240
}
241241

242-
if (!Cloud.__code[funcName]) {
242+
if (!Cloud.functions[funcName]) {
243243
var err = new Error("LeanEngine not found function named '" + funcName + "' for app '" + AV.applicationId + "' on " + NODE_ENV + ".");
244244
err.statusCode = 404;
245245
return cb(err);
@@ -262,7 +262,7 @@ var call = function(funcName, params, user, req, options, cb) {
262262
cb(err, result);
263263
});
264264

265-
Cloud.__code[funcName](request, response);
265+
Cloud.functions[funcName](request, response);
266266
} catch (err) {
267267
console.warn('Execute \'' + funcName + '\' failed with error: ' + (err.stack || err));
268268
err.statusCode = 500;
@@ -271,7 +271,7 @@ var call = function(funcName, params, user, req, options, cb) {
271271
};
272272

273273
var classHook = function(className, hook, object, user, req, cb) {
274-
if (!Cloud.__code[hook + className]) {
274+
if (!Cloud.functions[hook + className]) {
275275
var err = new Error("LeanEngine could not find hook '" + hook + className + "' for app '" + AV.applicationId + "' on " + NODE_ENV + ".");
276276
err.statusCode = 404;
277277
return cb(err);
@@ -297,11 +297,11 @@ var classHook = function(className, hook, object, user, req, cb) {
297297
if (hook.indexOf('__after_') === 0) {
298298
setHookMark('__after', obj);
299299
// after 的 hook 不需要 response 参数,并且请求默认返回 ok
300-
Cloud.__code[hook + className](request);
300+
Cloud.functions[hook + className](request);
301301
return cb(null, 'ok');
302302
} else {
303303
setHookMark('__before', obj);
304-
Cloud.__code[hook + className](request, utils.prepareResponseObject(req.res, function(err) {
304+
Cloud.functions[hook + className](request, utils.prepareResponseObject(req.res, function(err) {
305305
if (err) {
306306
cb(new Error(err));
307307
} else if ('__before_delete_for_' === hook) {
@@ -342,7 +342,7 @@ var onVerified = function(req, type, user) {
342342
req: req
343343
});
344344

345-
Cloud.__code['__on_verified_' + type](request);
345+
Cloud.functions['__on_verified_' + type](request);
346346
} catch (err) {
347347
console.warn('Execute onVerified ' + type + ' failed with error: ' + (err.stack || err));
348348
}
@@ -364,15 +364,15 @@ var onLogin = function(req, user, cb) {
364364
}
365365
});
366366

367-
Cloud.__code.__on_login__User(request, response);
367+
Cloud.functions['__on_login__User'](request, response);
368368
} catch (err) {
369369
console.warn('Execute onLogin failed with error: ' + (err.stack || err));
370370
}
371371
};
372372

373373
var onCompleteBigQueryJob = function(data) {
374374
try {
375-
Cloud.__code.__on_complete_bigquery_job(null, data);
375+
Cloud.functions['__on_complete_bigquery_job'](null, data);
376376
} catch (err) {
377377
console.warn('Execute onCompleteBigQueryJob failed with error: ' + (err.stack || err));
378378
}

0 commit comments

Comments
 (0)