Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var childProcess = require('child_process');
var path = require('path');
var http = require('http');
var async = require('async');
var vows = require('vows');
var assert = require('assert');
var clone = require('clone');
var express = require('express');
var redis = require('redis');
var Client = require('work-already').Client;
var MemorySocketStore = require('socket.io/lib/stores/memory');
var RedisSocketStore = require('socket.io/lib/stores/redis');
var RedisSessionStore = require('connect-redis')(express);
var MemorySessionStore = require('connect/lib/middleware/session/memory');
var Thywill = require('thywill');
var Message = Thywill.getBaseClass('Message');
// ------------------------------------------------------------
// Relating to setting up headless Thywill instances without
// applications running in this process.
// ------------------------------------------------------------
exports.headless = {};
/**
* Utility function to create a client for a local Redis server.
* @return {object}
* A Redis client.
*/
function createRedisClient () {
var options = {};
/**
* @fileOverview
* Batches for testing the Echo example application.
*/
var assert = require("assert");
var Thywill = require("thywill");
var Message = Thywill.getBaseClass("Message");
var tools = require("./tools");
/**
* Add general tests for the Echo application to the suite.
*/
exports.general = function (suite) {
var instanceIndex = 0;
var applicationIndex = 0;
// The initial batches load the application page and then connect via
// Socket.IO. The matches are checked against the page contents. Here
// we're looking at the templates that should be included.
var pageMatches = [
"<button>{{buttonText}}</button>",
'<div class="echoed-message">{{data}}</div>'
];
tools.workAlready.addInitialBatches(suite, instanceIndex, pageMatches);
/**
* @fileOverview
* Batches for testing the Calculations example application.
*/
var assert = require("assert");
var Thywill = require("thywill");
var Message = Thywill.getBaseClass("Message");
var RpcCapableApplication = Thywill.getBaseClass("RpcCapableApplication");
var tools = require("./tools");
/**
* Add general tests for the Calculations application to the suite.
*/
exports.general = function (suite) {
var instanceIndex = 0;
var applicationIndex = 0;
// The initial batches load the application page and then connect via
// Socket.IO. The matches are checked against the page contents. Here
// we're looking at the templates that should be included.
var pageMatches = [
'<div id="calculations-wrapper">'
];
tools.workAlready.addInitialBatches(suite, instanceIndex, pageMatches);</div>
* });
*
* thywill.cluster.on(thywill.cluster.eventNames.CLUSTER_MEMBER_UP, function (data) {
* console.log('Cluster process up: ' + data.clusterMemberId);
* });
*
*/
function Cluster() {
Cluster.super_.call(this);
this.componentType = 'cluster';
// Useful shortcuts.
this.clusterMemberStatus = Cluster.CLUSTER_MEMBER_STATUS;
this.eventNames = Cluster.EVENT_NAMES;
}
util.inherits(Cluster, Thywill.getBaseClass('Component'));
var p = Cluster.prototype;
//-----------------------------------------------------------
// 'Static' parameters
//-----------------------------------------------------------
Cluster.EVENT_NAMES = {
CLUSTER_MEMBER_DOWN: 'thywill.cluster.down',
CLUSTER_MEMBER_UP: 'thywill.cluster.up'
};
Cluster.CLUSTER_MEMBER_STATUS = {
DOWN: false,
UNKNOWN: undefined,
UP: true
};
/**
* @fileOverview
* InMemoryClientTracker class definition.
*/
var util = require('util');
var Thywill = require('thywill');
var Client = Thywill.getBaseClass('Client');
//-----------------------------------------------------------
// Class Definition
//-----------------------------------------------------------
/**
* @class
* A ClientTracker implementation that stores all data in memory. Every cluster
* member process keeps an up to date record of client connections to all
* cluster processes.
*
* Obviously this doesn't scale as well as other implementations to large
* numbers of cluster member processes - there is a lot of cross-talk needed
* between cluster members to keep the data updated in all of them. It is best
* used when your applications must make very frequent requests to ClientTracker
* methods.
* Emit when a client disconnects from this or any other cluster member.
* clientTracker.on(clientTracker.events.CONNECTION_TO, function (clusterMemberId, client {});
*
* Emit on disconnection of a client to this cluster member.
* clientTracker.on(clientTracker.events.DISCONNECTION, function (client) {});
*
* Emit when a client disconnects from this or any other cluster member.
* clientTracker.on(clientTracker.events.DISCONNECTION_FROM, function (clusterMemberId, client) {});
*/
function ClientTracker() {
ClientTracker.super_.call(this);
this.componentType = 'clientTracker';
// Convenience reference.
this.events = ClientTracker.EVENTS;
}
util.inherits(ClientTracker, Thywill.getBaseClass('Component'));
var p = ClientTracker.prototype;
//-----------------------------------------------------------
// 'Static'
//-----------------------------------------------------------
ClientTracker.EVENTS = {
CONNECTION: 'connection',
CONNECTION_TO: 'connectionTo',
DISCONNECTION: 'disconnection',
DISCONNECTION_FROM: 'disconnectionFrom',
CLUSTER_MEMBER_DOWN: 'clusterMemberDown'
};
//-----------------------------------------------------------
// Methods
* A channelManager implementation must emit the following events, which
* require integration with the cluster communication mechanisms.
*
* Sessions are added to a channel in any process in the cluster.
* channelManager.on(channelManager.events.SESSIONS_ADDED, function (channelId, sessionIds) {});
*
* Sessions are removed from a channel in any process in the cluster.
* channelManager.on(channelManager.events.SESSIONS_REMOVED, function (channelId, sessionIds) {});
*/
function ChannelManager() {
ChannelManager.super_.call(this);
this.componentType = 'channelManager';
// Convenience reference.
this.events = ChannelManager.EVENTS;
}
util.inherits(ChannelManager, Thywill.getBaseClass('Component'));
var p = ChannelManager.prototype;
//-----------------------------------------------------------
// 'Static'
//-----------------------------------------------------------
ChannelManager.EVENTS = {
SESSIONS_ADDED: 'sessionsAdded',
SESSIONS_REMOVED: 'sessionsRemoved'
};
//-----------------------------------------------------------
// Methods
//-----------------------------------------------------------
/**
var util = require('util');
var Thywill = require('thywill');
//-----------------------------------------------------------
// Class Definition
//-----------------------------------------------------------
/**
* @class
* The superclass for interfaces to templating systems.
*/
function TemplateEngine() {
TemplateEngine.super_.call(this);
this.componentType = 'templateEngine';
}
util.inherits(TemplateEngine, Thywill.getBaseClass('Component'));
var p = TemplateEngine.prototype;
//-----------------------------------------------------------
// Methods.
//-----------------------------------------------------------
/**
* @see Component#_getDependencies
*/
p._getDependencies = function () {
return {
components: [
'log'
]
};
};
* A channel manager that stores channel information in Redis.
*
* It maintains lookup sets for each channel and each session that belongs to
* at least one channel.
*
* This implementation requires that a clientTracker implementation is used.
*
* TODO: mechanisms for clearing out old sessions?
*
* @see ChannelManager
*/
function RedisChannelManager() {
RedisChannelManager.super_.call(this);
this.channels = {};
}
util.inherits(RedisChannelManager, Thywill.getBaseClass('ChannelManager'));
var p = RedisChannelManager.prototype;
//-----------------------------------------------------------
// 'Static' parameters
//-----------------------------------------------------------
RedisChannelManager.CONFIG_TEMPLATE = {
redisPrefix: {
_configInfo: {
description: 'A prefix applied to Redis keys.',
types: 'string',
required: true
}
},
redisClient: {
_configInfo: {
var Thywill = require("thywill");
var Message = require("./message");
//-----------------------------------------------------------
// Class Definition
//-----------------------------------------------------------
/**
* @class
* A trivial synchronous in-memory resource manager.
*/
function SimpleMessageManager () {
SimpleMessageManager.super_.call(this);
this.data = {};
}
util.inherits(SimpleMessageManager, Thywill.getBaseClass("MessageManager"));
var p = SimpleMessageManager.prototype;
//-----------------------------------------------------------
// "Static" parameters
//-----------------------------------------------------------
SimpleMessageManager.CONFIG_TEMPLATE = null;
//-----------------------------------------------------------
// Methods
//-----------------------------------------------------------
/**
* @see MessageManager#createMessage
*/
p.createMessage = function (data, metadata) {