onsip/SIP.js — pull request #108 by joseph-onsip (2014-10-07 23:41:56+00:00)
Title: Allow Dependency Injection in Node

**TLDR**: this works:

```javascript
// get default environment
var environment = require('sip.js/src/environment');

// override with custom implementations
environment.WebSocket = require('ws');
environment.Promise = require('q');

// use custom environment with SIP.js
var SIP = require('sip.js/src/SIP')(environment);
var ua = new SIP.UA({traceSip: true});

// shows Q stuff
console.log(SIP.Utils.Promise);
```

**Details**:

`./src/environment.js` exports an object that looks roughly like this (note that browserify converts `global` to `window` for the browser):

```javascript
{
WebSocket: global.WebSocket,
open: global.open,
Promise: global.Promise || require('promiscuous'),
timers: require('timers'), // `global` in the browser
console: require('console'), // `global.console` in the browser

MediaStream: global.MediaStream
getUserMedia: global.navigator.getUserMedia,
RTCPeerConnection: global.RTCPeerConnection,
RTCSessionDescription: global.RTCSessionDescription,

attachMediaStream: global.attachMediaStream,
createObjectURL: global.URL.createObjectURL,
revokeObjectURL: global.URL.revokeObjectURL
};
```

`./src/SIP.js` uses this object instead of directly accessing globals, so the dependencies can be overridden to support (for example):

* media in Node using [wrtc](https://github.com/js-platform/node-webrtc)
* testing in Node without having to override globals like the current Jasmine/PhantomJS setup does

Note that the conventional ways of using SIP.js still work:

Browser:
```html

```

Node:
```javascript
global.WebSocket = require('ws');
var SIP = require('sip.js');
var ua = new SIP.UA({traceSip: true});
```

**Further details**: see the commit messages of 8f21803 and 8b800d9