var regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (!regex.test(address)) {
reject(address + " is not valid e-mail address.");
if (declaration) this._process_declaration(declaration);
}
AccessStrategy.prototype = new function(){
/*
Process declaration of access strategy. Gives new access strategy checker_function and item_sensitive from declaration.
@param {object} delcaration - is an object with attributes:
name - string, required. The name of the access strategy - it has to be a string unique amongst any other access strategies in your application.
checker_function - function, required. It’s a function that takes a context instance as an argument and implements the logic of the access strategy. Its return values can be:
* boolean - true for granting the access and false for denying.
* a Promise - that resolves when the access is granted and rejects otherwise. Use promises only when the decision depends on a result of an asynchronous function.
item_sensitive - boolean, defaults to false. If set to true, the checker_function is provided with a second argument, which contains an object representing the resource being requested.
var ConfigurationManager = require("../config/config-manager.js");
var chip_type_functions = {
channel: require("./channel.js"),
resource_type: require("./resource-type.js"),
field_type: require("./field-type.js"),
datastore: require("./datastore.js")
}
chips_by_module = {};
var started_chips_longids = new Set();
var chips = {};
var chip_type_start_order = ["datastore", "access_strategy", "field_type", "resource_type", "channel"];
var registred_chips_longids = new Set();
var ChipManager = new function(){
/**
* Starts chips in proper order.
* @param {array} chip_types_to_start - array of names of chips we want to start
* @returns {Promise} - Promise, which will resolve with starting chips or reject with error
*/
this.start_chips = function(){
Sealious.Logger.info("Starting all chips:");
var promises = [];
for (var i in chip_type_start_order){
var type = chip_type_start_order[i];
Sealious.Logger.info(" " + type + ":");
for (var name in chips[type]){
var chip = chips[type][name];
Sealious.Logger.info("\t \u2713 " + name);
try {
if (chip.start){
var promise = chip.start();
promises.push(promise);
}
} catch (error){
Sealious.Logger.error("\t " + "couldn't start `" + name + "`");
return Promise.reject(error);
}
}
}
return Promise.all(promises);
}
/**
* Adds chip to chips array and chips_by_module array
* @param {string} type - chip type ex. access-strategy, channel
* @param {string} name - chip name
* @param {object} chip - chip itselfs
* @returns void
*/
this.add_chip = function(type, name, chip){
if (chips[type]==undefined){
chips[type]=[];
}
chips[type][name]=chip;
}
/**
* Gets all resource types
* @returns {array} Array of names of resource types.
*/
this.get_all_resource_types = function(){
var names = [];
for (resource_type in chips.resource_type) {
names.push(resource_type);
}
return names;
}
/**
* Checks if chip exists
* @param {string} type - chip type
* @param {string} name - chip name
* @returns {boolean} true if exists, otherwise false
*/
this.chip_exists = function(type, name){
if (chips[type] && chips[type][name]){
return true;
} else {
return false;
}
}
/**
* Gets chip
* @param {string} type - chip type
* @param {string} name - chip name
* @returns {object} requested chip or throws error
*/
this.get_chip = function(type, name){
try {
var ret = chips[type][name];
if (ret == undefined){
throw new Error("Chip of type " + type + " and name " + name + " has not yet been registered.");
}
return ret;
} catch (e){
throw new Sealious.Errors.ValidationError("ChipManager was asked to return a chip of type `" + type + "` and name `" + name + "`, but it was not found", {}, {short_message: "chip_not_found"});
}
}
/*
* Gets chip by long id
* @param {string} longid - longid is chip_type.chip_name ex. chanell.cli
* @returns {object} requested chip or throws error
*/
this.get_chip_by_longid = function(longid){
var type = longid.split(".")[0];
var name = longid.split(".")[1];
return this.get_chip(type, name);
}
/*
* Checks if chip is registered
* @param {string} longid - longid is chip_type.chip_name ex. chanell.cli
* @returns {boolean} true if chip is registered, otherwise false
var datastore_chip_name = ConfigurationManager.get_config().datastore_chip_name;
if (datastore_chip_name===undefined){
throw new Sealious.Errors.Error("Chip manager was requested to return a datastore chip. Multiple chips of type `datastore` have been registered, and no default provided in configuration.")
assert.deepEqual(to_insert, response, ".insert method should respond with the created document");
return Promise.resolve();
});
}).then(function(){
//check if find resolves with an array
return self.find(test_collection_name, {}, {})
.then(function(documents){
assert(documents instanceof Array, "datastore." + self.name + ".find should resolve with an array");
return Promise.resolve();
});
}).then(function(){
//check if amount of created documents checks out
var creates = [
self.insert(test_collection_name, {
value: 2,
random: rand
}),
self.insert(test_collection_name, {
value: 3,
random: rand
}),
self.insert(test_collection_name, {
value: 4,
random: -rand
}),
]
var created_so_far = 4;
return Promise.all(creates)
.then(function(){
return self.find(test_collection_name, {}, {});
}).then(function(documents){
assert(documents.length == created_so_far, "Inserted " + created_so_far + " documents so far, but " + documents.length + " were returned on .find()");
return Promise.resolve(created_so_far);
});
}).then(function(created_so_far){
//check if there is a proper amount of documents with random value set to rand
var documents_with_rand = created_so_far - 1;
return self.find(test_collection_name, {
random: rand
}, {})
.then(function(documents){
assert(documents.length == documents_with_rand, "Inserted " + documents_with_rand + " documents with `random` set to `" + rand + "` so far, but " + documents.length + " were returned on .find({random: " + rand + "})");
if (!Sealious.ChipManager.chip_exists("field_type", this.type_name)) {
throw new Sealious.Errors.DeveloperError("In declaration of resource type '" + resource_type.name +"': unknown field type '"+this.type_name+"' in field '"+this.name+"'.");
throw new Sealious.Errors.DeveloperError("In declaration of resource type '" + resource_type.name +"': unknown field structure '"+structure_name+"' in field '"+this.name+"'.");
}
this.type.init && this.type.init();
this.required = declaration.required || false;
this.derived = declaration.derived || false;
};
ResourceTypeField.prototype = new function(){
/**
* Shorthand for ResourceTypeField.type.isProperValue
//with_validator:boolean - whether to include validator function in field description. Warning! If set to true, the output is not serializable in JSON.
//with_validators:boolean - whether to include validator functions in field descriptions. Warning! If set to true, the output is not serializable in JSON.
var resource_type_specification = {};
for (var field_name in this.fields){
var field_specification = this.fields[field_name].get_specification(with_validators);