2018-01-03 04:05:53 +00:00
|
|
|
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2017-08-15 01:34:46 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const util = require('util');
|
|
|
|
const extend = require('util')._extend;
|
|
|
|
const g = require('./globalize');
|
2017-08-15 01:34:46 +00:00
|
|
|
|
|
|
|
module.exports = function(modelCtor, remotingConfig, modelConfig) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const settings = {};
|
2017-08-15 01:34:46 +00:00
|
|
|
|
|
|
|
// apply config.json settings
|
2019-10-07 09:45:34 +00:00
|
|
|
const configHasSharedMethodsSettings = remotingConfig &&
|
2017-08-15 01:34:46 +00:00
|
|
|
remotingConfig.sharedMethods &&
|
|
|
|
typeof remotingConfig.sharedMethods === 'object';
|
|
|
|
if (configHasSharedMethodsSettings)
|
|
|
|
util._extend(settings, remotingConfig.sharedMethods);
|
|
|
|
|
|
|
|
// apply model-config.json settings
|
|
|
|
const options = modelConfig.options;
|
2019-10-07 09:45:34 +00:00
|
|
|
const modelConfigHasSharedMethodsSettings = options &&
|
2017-08-15 01:34:46 +00:00
|
|
|
options.remoting &&
|
|
|
|
options.remoting.sharedMethods &&
|
|
|
|
typeof options.remoting.sharedMethods === 'object';
|
|
|
|
if (modelConfigHasSharedMethodsSettings)
|
|
|
|
util._extend(settings, options.remoting.sharedMethods);
|
|
|
|
|
|
|
|
// validate setting values
|
|
|
|
Object.keys(settings).forEach(function(setting) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const settingValue = settings[setting];
|
|
|
|
const settingValueType = typeof settingValue;
|
2017-08-15 01:34:46 +00:00
|
|
|
if (settingValueType !== 'boolean')
|
|
|
|
throw new TypeError(g.f('Expected boolean, got %s', settingValueType));
|
|
|
|
});
|
|
|
|
|
|
|
|
// set sharedMethod.shared using the merged settings
|
2019-10-07 09:45:34 +00:00
|
|
|
const sharedMethods = modelCtor.sharedClass.methods({includeDisabled: true});
|
2017-08-15 01:34:46 +00:00
|
|
|
|
|
|
|
// re-map glob style values to regular expressions
|
2019-10-07 09:45:34 +00:00
|
|
|
const tests = Object
|
2017-08-15 01:34:46 +00:00
|
|
|
.keys(settings)
|
|
|
|
.filter(function(setting) {
|
|
|
|
return settings.hasOwnProperty(setting) && setting.indexOf('*') >= 0;
|
|
|
|
})
|
|
|
|
.map(function(setting) {
|
|
|
|
// Turn * into an testable regexp string
|
2019-10-07 09:45:34 +00:00
|
|
|
const glob = escapeRegExp(setting).replace(/\*/g, '(.)*');
|
2017-08-15 01:34:46 +00:00
|
|
|
return {regex: new RegExp(glob), setting: settings[setting]};
|
|
|
|
}) || [];
|
|
|
|
sharedMethods.forEach(function(sharedMethod) {
|
|
|
|
// use the specific setting if it exists
|
2019-10-07 09:45:34 +00:00
|
|
|
const methodName = sharedMethod.isStatic ? sharedMethod.name : 'prototype.' + sharedMethod.name;
|
|
|
|
const hasSpecificSetting = settings.hasOwnProperty(methodName);
|
2017-08-15 01:34:46 +00:00
|
|
|
if (hasSpecificSetting) {
|
|
|
|
if (settings[methodName] === false) {
|
|
|
|
sharedMethod.sharedClass.disableMethodByName(methodName);
|
|
|
|
} else {
|
|
|
|
sharedMethod.shared = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tests.forEach(function(glob) {
|
|
|
|
if (glob.regex.test(methodName)) {
|
|
|
|
if (glob.setting === false) {
|
|
|
|
sharedMethod.sharedClass.disableMethodByName(methodName);
|
|
|
|
} else {
|
|
|
|
sharedMethod.shared = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Sanitize all RegExp reserved characters except * for pattern gobbing
|
|
|
|
function escapeRegExp(str) {
|
|
|
|
return str.replace(/[\-\[\]\/\{\}\(\)\+\?\.\\\^\$\|]/g, '\\$&');
|
|
|
|
}
|