<commit_before>/**
* @author: sihorton
* Implementation of a deskshell api.
*
var shellApp = deskShell.startApp({
	openSocket:true
	,openAppWin:true
	,htdocs:'/content'
	,width:500
	,height:500
		//optional parameters
		,port:8086
		,cport:8087
		,chromiumPath:'..path_to_chromium_binary'
});
*/
var Q = require("q");
var freeport = require("freeport");
var rDebug = require('chrome-rdebug').rDebug;
var request = require("request");
var fs = require("fs");
var path = require("path");
var mime = require("mime");

var shellApi = {
	v:"0.1"
	,loadEnv:function() {
		var loadingenv = Q.defer();
		try {
			deskShell.env = require(deskShell.envPath);
			deskShell.sysAppsDir = path.normalize(deskShell.installDir + deskShell.sysAppsDir)+"/";

			loadingenv.resolve();
		} catch(e) {
			//env setup was not found...
			//create it..
			var os = require('os');
			fs.readFile(deskShell.platformDir+"deskshell-env.js.sample."+os.platform(), 'utf8', function (err, data) {
				if (err) return loadingenv.reject(err);
				deskShell.env = JSON.parse(data);
				deskShell.sysAppsDir = path.normalize(deskShell.installDir + deskShell.sysAppsDir);

				fs.writeFile(deskShell.envPath,"module.exports="+ JSON.stringify(deskShell.env,null,4) , 'utf8', function (err, data) {
					if (err) return loadingenv.reject(err);
					loadingenv.resolve();
				});
			});
		}
		return loadingenv.promise;		
	}
	,getFreePort:function(app, port) {
		var getting = Q.defer();
		if (!port) {
			freeport(function(er, port) {
				if (er) getting.reject({error:er,app:app});
				getting.resolve({app:app,port:port});
			});
		} else {
			getting.resolve({app:app,port:port});
		}
		return getting.promise;		
	},getDebugSocket:function(app,appUrl,launching,timeout,tries) {
		if (tries == undefined) tries = 3;
		
		setTimeout(function() {
			request("http://localhost:"+app.cport+"/json", function(error, response, body) {
				var err = null;
				try {
					var chromeDebugOptions = JSON.parse(body);
				} catch(e) {
					launching.reject({error:e,app:app});
					err = e;
				}
				
				if (!err) {
					var chromeDebugUrl;
					for(var s=0;s<chromeDebugOptions.length;s++) {
						if ((chromeDebugOptions[s]['url'] == appUrl) && (chromeDebugOptions[s]['webSocketDebuggerUrl'])) {
							chromeDebugUrl = chromeDebugOptions[s].webSocketDebuggerUrl;
							console.log("found debug socket",chromeDebugUrl);
							break;
						}
					}
					if (!chromeDebugUrl) {
						if (tries > 40) {
							launching.reject({error:"Could not find debug websocket"});
						} else {
							shellApi.getDebugSocket(app,appUrl,launching,500,tries+1);
						}
					} else {
						//bugfix for wierd portable chrome on windows.
						if (chromeDebugUrl.indexOf('ws:///') > -1) {
							chromeDebugUrl = chromeDebugUrl.replace('ws:///','ws://localhost:'+app.cport+'/');
						}
						app.chromiumDebugUrl = chromeDebugUrl;
						app.rDebugApi = rDebug.openSocket(chromeDebugUrl);
						app.rDebugApi.on('*',function(event) {
							if (event.method == "Inspector.detached" && event.params && event.params['reason'] == "target_closed") {
								if (app.params['exitOnAppWinClose']) {
									console.log("auto close");
									process.exit(0);
								}
							}
							console.log("Event:",event);
						});
						launching.resolve(app);
					}
				}
				
			})
			
		},timeout);
		
	},startApp:function(params) {
		var starting = Q.defer();
		var myApp = {
			params:shellApi.appDef
		}
		for(var p in params) {
			myApp.params[p] = params[p];
		}
		if (typeof myApp.params['appSocket'] == 'undefined') myApp.params['appSocket'] = true;
		if (typeof myApp.params['openAppWin'] == 'undefined') myApp.params['openAppWin'] = true;
		if (typeof myApp.params['exitOnAppWinClose'] == 'undefined') myApp.params['exitOnAppWinClose'] = true;
		
			shellApi.getFreePort(myApp,myApp.params['port'])
			.then(function(dat) {
				var app = dat.app;
				app.port = dat.port;
				
				if (app.params['htdocs']) {
					//var fs = require('fs');
					if (!deskShell.appfs) deskShell.appfs = fs;
					app.server = require('http').createServer(myApp.params['serverHandler'] || function handler (req, res) {
						var handle = true;
						if (app.params['requestHandler']) {
							handle = app.params.requestHandler(req, res);<commit_msg>Make port in use available under environmental variables.<commit_after>/**
* @author: sihorton
* Implementation of a deskshell api.
*
var shellApp = deskShell.startApp({
	openSocket:true
	,openAppWin:true
	,htdocs:'/content'
	,width:500
	,height:500
		//optional parameters
		,port:8086
		,cport:8087
		,chromiumPath:'..path_to_chromium_binary'
});
*/
var Q = require("q");
var freeport = require("freeport");
var rDebug = require('chrome-rdebug').rDebug;
var request = require("request");
var fs = require("fs");
var path = require("path");
var mime = require("mime");

var shellApi = {
	v:"0.1"
	,loadEnv:function() {
		var loadingenv = Q.defer();
		try {
			deskShell.env = require(deskShell.envPath);
			deskShell.sysAppsDir = path.normalize(deskShell.installDir + deskShell.sysAppsDir)+"/";

			loadingenv.resolve();
		} catch(e) {
			//env setup was not found...
			//create it..
			var os = require('os');
			fs.readFile(deskShell.platformDir+"deskshell-env.js.sample."+os.platform(), 'utf8', function (err, data) {
				if (err) return loadingenv.reject(err);
				deskShell.env = JSON.parse(data);
				deskShell.sysAppsDir = path.normalize(deskShell.installDir + deskShell.sysAppsDir);

				fs.writeFile(deskShell.envPath,"module.exports="+ JSON.stringify(deskShell.env,null,4) , 'utf8', function (err, data) {
					if (err) return loadingenv.reject(err);
					loadingenv.resolve();
				});
			});
		}
		return loadingenv.promise;		
	}
	,getFreePort:function(app, port) {
		var getting = Q.defer();
		if (!port) {
			freeport(function(er, port) {
				if (er) getting.reject({error:er,app:app});
				getting.resolve({app:app,port:port});
			});
		} else {
			getting.resolve({app:app,port:port});
		}
		return getting.promise;		
	},getDebugSocket:function(app,appUrl,launching,timeout,tries) {
		if (tries == undefined) tries = 3;
		
		setTimeout(function() {
			request("http://localhost:"+app.cport+"/json", function(error, response, body) {
				var err = null;
				try {
					var chromeDebugOptions = JSON.parse(body);
				} catch(e) {
					launching.reject({error:e,app:app});
					err = e;
				}
				
				if (!err) {
					var chromeDebugUrl;
					for(var s=0;s<chromeDebugOptions.length;s++) {
						if ((chromeDebugOptions[s]['url'] == appUrl) && (chromeDebugOptions[s]['webSocketDebuggerUrl'])) {
							chromeDebugUrl = chromeDebugOptions[s].webSocketDebuggerUrl;
							console.log("found debug socket",chromeDebugUrl);
							break;
						}
					}
					if (!chromeDebugUrl) {
						if (tries > 40) {
							launching.reject({error:"Could not find debug websocket"});
						} else {
							shellApi.getDebugSocket(app,appUrl,launching,500,tries+1);
						}
					} else {
						//bugfix for wierd portable chrome on windows.
						if (chromeDebugUrl.indexOf('ws:///') > -1) {
							chromeDebugUrl = chromeDebugUrl.replace('ws:///','ws://localhost:'+app.cport+'/');
						}
						app.chromiumDebugUrl = chromeDebugUrl;
						app.rDebugApi = rDebug.openSocket(chromeDebugUrl);
						app.rDebugApi.on('*',function(event) {
							if (event.method == "Inspector.detached" && event.params && event.params['reason'] == "target_closed") {
								if (app.params['exitOnAppWinClose']) {
									console.log("auto close");
									process.exit(0);
								}
							}
							console.log("Event:",event);
						});
						launching.resolve(app);
					}
				}
				
			})
			
		},timeout);
		
	},startApp:function(params) {
		var starting = Q.defer();
		var myApp = {
			params:shellApi.appDef
		}
		for(var p in params) {
			myApp.params[p] = params[p];
		}
		if (typeof myApp.params['appSocket'] == 'undefined') myApp.params['appSocket'] = true;
		if (typeof myApp.params['openAppWin'] == 'undefined') myApp.params['openAppWin'] = true;
		if (typeof myApp.params['exitOnAppWinClose'] == 'undefined') myApp.params['exitOnAppWinClose'] = true;
		
			shellApi.getFreePort(myApp,myApp.params['port'])
			.then(function(dat) {
				var app = dat.app;
				app.port = dat.port;
				deskShell.env.port = dat.port;
				
				if (app.params['htdocs']) {
					//var fs = require('fs');
					if (!deskShell.appfs) deskShell.appfs = fs;
					app.server = require('http').createServer(myApp.params['serverHandler'] || function handler (req, res) {
						var handle = true;
						if (app.params['requestHandler']) {
							handle = app.params.requestHandler(req, res);
{'fraction_non_alphanumeric': 0.16888682960732074, 'fraction_numerical': 0.006718406116066257, 'mean_word_length': 3.420890937019969, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4210435', 'n_tokens_mistral': 3685, 'n_tokens_neox': 2917, 'n_words': 516}