Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Baumgart committed Jul 7, 2012
0 parents commit 3d3edaf
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
8 changes: 8 additions & 0 deletions LICENSE
@@ -0,0 +1,8 @@
Copyright (C) 2012 ProxV, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

27 changes: 27 additions & 0 deletions README.md
@@ -0,0 +1,27 @@
node-xvfb: easily start and stop an X Virtual Frame Buffer from your node apps.
-----

### Usage

```javascript
var Xvfb = require('xvfb');
var xvfb = new Xvfb();
xvfb.start();

// code that uses the virtual frame buffer here

xvfb.stop();
```

The Xvfb constructor takes two options:

* <code>displayNum</code> - the X display to use, defaults to the lowest unused display number >= 99 if <code>reuse</code> is false or 99 if <code>reuse</code> is true.
* <code>reuse</code> - whether to reuse an existing Xvfb instance if it already exists on the X display referenced by displayNum

### Thanks to

* @kelsa for https://github.com/kesla/node-headless
* @leonid-shevtsov for https://github.com/leonid-shevtsov/headless

Both of which served as inspiration for this package.

72 changes: 72 additions & 0 deletions index.js
@@ -0,0 +1,72 @@
var path = require('path');
var spawn = require('child_process').spawn;
var usleep = require('sleep').usleep;

function _lockFileForDisplay (display) {
displayNum = display.toString().replace(/^:/, '');
return '/tmp/.X' + displayNum + '-lock';
};

function Xvfb(options) {
options = options || {};
this._display = (options.displayNum ? ':' + options.displayNum : null);
this._reuse = options.reuse;
}

Xvfb.prototype = {
start: function() {
if (this._process) {
return;
}

var display = this.display();
var lockFile = _lockFileForDisplay(display);
this._oldDisplay = process.env.DISPLAY;
process.env.DISPLAY = display;

if (path.existsSync(lockFile)) {
if (this._reuse) {
return;
} else {
throw new Error('Display ' + display + ' is already in use and the "reuse" option is false.');
}
}

this._process = spawn('Xvfb', [ display ]);

var sleepMs = 10;
var timeoutMs = 500;
while (!path.existsSync(lockFile)) {
if (timeoutMs <= 0) {
throw new Error('could not start Xvfb');
}
usleep(sleepMs * 1000);
timeoutMs -= sleepMs;
}
},

stop: function() {
if (this._process) {
this._process.kill();
this._process = null;
}
process.env.DISPLAY = this._oldDisplay;
},


display: function() {
if (!this._display) {
var displayNum = 98;
var lockFile;
do {
displayNum++;
lockFile = _lockFileForDisplay(displayNum);
} while (!this._reuse && path.existsSync(lockFile));
this._display = ':' + displayNum;
}
return this._display;
}
}

module.exports = Xvfb;

17 changes: 17 additions & 0 deletions package.json
@@ -0,0 +1,17 @@
{
"author": "ProxV, Inc. <support@proxv.com> (http://proxv.com)",
"name": "xvfb",
"description": "Easily start and stop an X Virtual Frame Buffer from your node apps.",
"version": "0.0.1",
"repository": {
"url": "https://github.com/proxv/node-xvfb.git"
},
"dependencies": {
"sleep": "1.1.x"
},
"devDependencies": {},
"optionalDependencies": {},
"engines": {
"node": "*"
}
}

0 comments on commit 3d3edaf

Please sign in to comment.