Webda

class
 new Webda() 

Option name Type Description

This is the main class of the framework, it handles the routing, the services initialization and resolution

class Webda {

constructor

constructor
 Webda.prototype.constructor() 

Option name Type Description
constructor (config) {

validate

method
 validate() 

Option name Type Description
object

to validate

schema

path to use

Validate the object with schema

validate(object, schema) {
	if (!this._ajvSchemas[schema]) {
		this._ajv.addSchema(require(schema), schema)
		this._ajvSchemas[schema]=true;
	}
	return this._ajv.validate(schema, object);
}

validationLastErrors

method
 validationLastErrors() 

Get last errors from AJV schema validator ( called through validate method )

validationLastErrors() {
	return this._ajv.errors;
}

require

method
 require() 

Option name Type Description
executor Object

The executor to give to the file

path String

The path of the file to executre

Execute a file in sandbox mode

require(executor, path) {
	var code = fs.readFileSync(require.resolve(path));
	this.sandbox(executore, code);
}

sandbox

method
 sandbox() 

Option name Type Description
executor Object

The executor to expose as executor

code String

to execute

sandbox(executor, code) {
	var sandbox = {
		// Should be custom console
		console : console,
		webda: executor._webda,
		executor: executor,
		module  : {},
		require : function(mod) {
			// We need to add more control here
	      	if (mod === 'net') {
	        	throw Error('not allowed');
	      	}
	      	// if the module is okay to load, load it:
	      	return require.apply(this, arguments);
	    }
	};
	vm.runInNewContext(code, sandbox);
	return sandbox.module.exports(executor);
}

getSession

method
 getSession() 

Option name Type Description

Get the current session object

getSession() {
	if (this._currentExecutor) {
		return this._currentExecutor.session;	
	}
}

getLocales

method
 getLocales() 

Option name Type Description
return

The configured locales or "en-GB" if none are defined

To define the locales just add a locales: ['en-GB', 'fr-FR'] in your host global configuration

getLocales() {
	if (!this._config || !this._config[this._vhost] || !this._config[this._vhost].global || !this._config[this._vhost].global.locales) {
		return ["en-GB"];
	}
	return this._config[this._vhost].global.locales;
}

getNewSession

method
 getNewSession() 

Option name Type Description

Get a new session object initiate with the data object
Can be used to create short term encrypted data, the keys of the session should be refresh frequently

getNewSession(data) {
	const SecureCookie = require("./utils/cookie.js");
	return new SecureCookie({secret: 'WebdaSecret'}, data);
}

getService

method
 getService() 

Option name Type Description
name String

The service name to retrieve

Check for a service name and return the wanted singleton or undefined if none found

getService(name) {
	if (!this._config || !name) {
		return;
	}
	name = name.toLowerCase();
	if (this._config[this._vhost] !== undefined) {
		if (this._config[this._vhost].global !== undefined && this._config[this._vhost].global._services !== undefined
				&& this._config[this._vhost].global._services[name]) {
			return this._config[this._vhost].global._services[name];
		}
	}
}

getModel

method
 getModel() 

Option name Type Description
name String

The model name to retrieve

Check for a model name and return the wanted class or throw exception if none found

getModel(name) {
	if (!this._config || !name) {
		return;
	}
	name = name.toLowerCase();
	if (this._config[this._vhost] !== undefined) {
		if (this._config[this._vhost].global !== undefined && this._config[this._vhost].global._models !== undefined
				&& this._config[this._vhost].global._models[name]) {
			return this._config[this._vhost].global._models[name];
		}
	}
	throw Error("Undefined model " + name);
}

getExecutor

method
 getExecutor() 

Option name Type Description
vhost String

The host for the request

method String

The http method

url String

The url path

protocol String

http or https

port String

Port can be usefull for auto redirection

headers Object

The headers of the request

Get the executor corresponding to a request
It can be usefull in unit test so you can test the all stack

getExecutor(ctx, vhost, method, url, protocol, port, headers) {
	// Check vhost
	var wildcard = false;
	var originalVhost = vhost;
    if (this._config[vhost] === undefined) {
       if (this._config['*'] === undefined) {
    	   return null;
       }
       wildcard = true;
       vhost = this._config['*'];
    }
    this.setHost(vhost);
    // Init vhost if needed
    this.initHosts(vhost, this._config[vhost]);
    // Check mapping
    var route = this.getRouteFromUrl(ctx, this._config[vhost], method, url);
    if (route === undefined) {
    	return;
    }
    route._http = {"host":vhost, "vhost": originalVhost, "method":method, "url":url, "protocol": protocol, "port": port, "headers": headers, "wildcard": wildcard, "root": protocol + "://" + vhost};
    return this.getServiceWithRoute(ctx, route);
}

getSecret

method
 getSecret() 

Option name Type Description

This should return a "turning" secret with cache and a service to modify it every x mins
WARNING The security is lower without this "turning" secret, you can still set the global.secret parameter

Dont rely on this method, it will probably disapear to avoid secret leak

getSecret() {
	// For now a static config file but should have a rolling service secret
	return this._config[this._vhost].global.secret;
}

getSalt

method
 getSalt() 

Option name Type Description

Return a salt to use when doing digest

getSalt() {
	// For now a static config file but should have a rolling service secret
	return this._config[this._vhost].global.salt;
}

flushHeaders

method
 flushHeaders() 

Option name Type Description

Flush the headers to the response, no more header modification is possible after that

flushHeaders(executor) {

}

flush

method
 flush() 

Flush the entire response to the client

flush(executor) {

}

isDebug

method
 isDebug() 

Return if Webda is in debug mode

isDebug() {
	return false;
}

getGlobalParams

method
 getGlobalParams() 

Option name Type Description
vhost String

The domain to retrieve or default if not specified

Return the global parameters of a domain

getGlobalParams(vhost) {
	if (this._config[vhost] === undefined) {
       if (this._config['*'] === undefined) {
    	   return {};
       }
       vhost = this._config['*'];
    }
    if (this._config[vhost] === undefined || 
    		this._config[vhost].global === undefined ||
    		this._config[vhost].global.params === undefined) {
    	return {};
    }
    return this._config[vhost].global.params;
}

newContext

method
 newContext() 

Option name Type Description
body Object
  • The request body
session Object
  • The request session
stream Object
  • The request output stream if any
files Object
  • The files input stream
headers Object
  • The request headers if any
return Object

A new context object to pass along

Create a new context for a request

newContext(body, session, stream, files, headers) {
	return new Context(this, body, session, stream, files, headers);
}

toPublicJSON

method
 toPublicJSON() 

Option name Type Description
object Object
  • The object to export
return String

The export of the strip object ( removed all attribute with _ )

Convert an object to JSON using the Webda json filter

toPublicJSON(object) {
	return JSON.stringify(object, this.jsonFilter);
}