Skip to content

Commit

Permalink
Use require() and browserify to organize dependencies and compilation.
Browse files Browse the repository at this point in the history
This is a squashed cherry-pick of the first three commits of
onsip#30, plus a small fix to the Timers module,
and slight updates to package.json and .npmignore.

The reason this isn't in separate commits is that the first three didn't work
individually. Squashing them together allows:

- the automated tests to pass
- <script src="dist/sip.js"></script> to work as usual
- a Node.js phone to be tested as follows:

    npm install ws
    node
    WebSocket = require('ws')
    var SIP = require('./.');
    var ua = new SIP.UA({traceSip: true});
    ua.message('[email protected]', 'Node says hi!');

For completeness, unsquashed commit messages are listed below:

1: Use require() and browserify to organize dependencies and compilation.

Conflicts:
	Gruntfile.js
	package.json
	src/tail.js

Cherry-picked from:
  josephfrazier@df33833

2: Clean up module dependencies.

- LoggerFactory does not depend on SIP
- Constants depends only on SIP.name and SIP.version
- Exceptions does not depend on SIP
- Timers does not depend on SIP
- Transport depends on window
- SIP.MediaHandler depends on EventEmitter, not SIP
- WebRTC depends on Utils, not SIP
- Hacks depends on window, not SIP
- DigestAuthentication depends on Utils, not SIP

Conflicts:
	src/Timers.js
	src/WebRTC.js

Cherry-picked from:
  josephfrazier@9af96e9

3: Grammar module depends on SIP

Cherry-picked from:
  josephfrazier@3e0c165

4: Timers module depends on window

See:
  josephfrazier@2510347#diff-7f75ad1c8b0a978a180c50fe4c649e8bL25

5: package.json: set "main": "src/SIP.js"

Now that our src/ files are modules, we don't have to compile the module before
require()ing it.

6: .npmignore dist/sip*

There's no need for `npm install` to bring in dist/ files. Leave that to bower.
  • Loading branch information
joseph-onsip committed Jul 15, 2014
1 parent 2dfcb49 commit ba42061
Show file tree
Hide file tree
Showing 36 changed files with 185 additions and 229 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
dist/sip*
.grunt
_SpecRunner.html
111 changes: 35 additions & 76 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,8 @@

module.exports = function(grunt) {

var srcFiles = [
'src/SIP.js',
'src/Utils.js',
'src/LoggerFactory.js',
'src/EventEmitter.js',
'src/Constants.js',
'src/Exceptions.js',
'src/Timers.js',
'src/Transport.js',
'src/Parser.js',
'src/SIPMessage.js',
'src/URI.js',
'src/NameAddrHeader.js',
'src/Transactions.js',
'src/Dialogs.js',
'src/RequestSender.js',
'src/RegisterContext.js',
'src/MediaHandler.js',
'src/ClientContext.js',
'src/ServerContext.js',
'src/Session.js',
'src/Subscription.js',
'src/WebRTC.js',
'src/UA.js',
'src/Hacks.js',
'src/SanityCheck.js',
'src/DigestAuthentication.js',
'src/Grammar/dist/Grammar.js',
'src/tail.js'
];

var pkg = grunt.file.readJSON('package.json');

// Project configuration.
grunt.initConfig({
pkg: pkg,
name: pkg.name.replace(/\.js$/, ''),
meta: {
banner: '\
var banner = '\
/*\n\
* SIP version <%= pkg.version %>\n\
* Copyright (c) 2014-<%= grunt.template.today("yyyy") %> Junction Networks, Inc <http://www.onsip.com>\n\
Expand Down Expand Up @@ -72,45 +35,41 @@ module.exports = function(grunt) {
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\
*\n\
* ~~~ end JsSIP license ~~~\n\
*/\n\n\n'
*/\n\n\n';

// Project configuration.
grunt.initConfig({
pkg: pkg,
name: pkg.name.replace(/\.js$/, ''),
meta: {
banner: banner
},
concat: {
dist: {
src: srcFiles,
dest: 'dist/<%= name %>.js',
options: {
banner: '<%= meta.banner %>',
separator: '\n\n',
process: true
},
nonull: true
},
browserify: {
devel: {
src: srcFiles,
dest: 'dist/<%= name %>-<%= pkg.version %>.js',
options: {
banner: '<%= meta.banner %>',
separator: '\n\n',
process: true
src: 'src/SIP.js',
dest: 'dist/<%= name %>-<%= pkg.version %>.js'
},
options: {
bundleOptions: {
standalone: 'SIP'
},
nonull: true
postBundleCB: function (err, src, next) {
// prepend the banner and fill in placeholders
src = (banner + src).replace(/<%=(.*)%>/g, function (match, expr) {
return eval(expr)
});
next(err, src);
}
}
},
includereplace: {
copy: {
dist: {
files: {
'dist': 'dist/<%= name %>.js'
}
},
devel: {
files: {
'dist': 'dist/<%= name %>-<%= pkg.version %>.js'
}
src: 'dist/<%= name %>-<%= pkg.version %>.js',
dest: 'dist/<%= name %>.js'
}
},
jshint: {
dist: 'dist/<%= name %>.js',
devel: 'dist/<%= name %>-<%= pkg.version %>.js',
src: 'src/**/*.js',
options: {
browser: true,
curly: true,
Expand All @@ -128,7 +87,7 @@ module.exports = function(grunt) {
supernew: true,
globals: {
module: true,
define: true,
require: true,
global: true
}
}
Expand Down Expand Up @@ -165,7 +124,6 @@ module.exports = function(grunt) {
src: 'src/Grammar/src/Grammar.pegjs',
dest: 'src/Grammar/dist/Grammar.js',
options: {
exportVar: 'SIP.Grammar',
optimize: 'size',
allowedStartRules: [
'Contact',
Expand Down Expand Up @@ -200,7 +158,7 @@ module.exports = function(grunt) {
},
trimtrailingspaces: {
main: {
src: srcFiles,
src: "src/**/*.js",
options: {
filter: 'isFile',
encoding: 'utf8',
Expand All @@ -212,8 +170,8 @@ module.exports = function(grunt) {


// Load Grunt plugins.
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-include-replace');
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-jasmine');
Expand All @@ -230,6 +188,7 @@ module.exports = function(grunt) {
var modified_grammar = grammar.replace(/throw peg.*maxFailPos.*/, 'return -1;');
modified_grammar = modified_grammar.replace(/return peg.*result.*/, 'return data;');
modified_grammar = modified_grammar.replace(/parse:( *)parse/, 'parse:$1function (input, startRule) {return parse(input, {startRule: startRule});}');
modified_grammar = modified_grammar.replace(/\(function\(\)/, 'function(SIP)').replace(/\}\)\(\)/, '}');

// Don't jshint this big chunk of minified code
modified_grammar =
Expand All @@ -246,12 +205,12 @@ module.exports = function(grunt) {
// Task for building sip-devel.js (uncompressed), sip-X.Y.Z.js (uncompressed)
// and sip-X.Y.Z.min.js (minified).
// Both sip-devel.js and sip-X.Y.Z.js are the same file with different name.
grunt.registerTask('build', ['trimtrailingspaces:main', 'concat:devel', 'includereplace:devel', 'jshint:devel', 'concat:dist', 'includereplace:dist', 'jshint:dist', 'uglify:dist', 'uglify:devel']);
grunt.registerTask('build', ['trimtrailingspaces:main', 'devel', 'copy', 'uglify']);

// Task for building sip-devel.js (uncompressed).
grunt.registerTask('devel', ['concat:devel', 'includereplace:devel', 'jshint:devel']);
grunt.registerTask('devel', ['jshint', 'browserify']);

grunt.registerTask('quick', ['concat:dist', 'includereplace:dist']);
grunt.registerTask('quick', ['browserify']);

// Test tasks.
grunt.registerTask('test',['jasmine']);
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"title": "SIP.js",
"description": "A simple, intuitive, and powerful JavaScript signaling library",
"version": "0.6.0",
"main": "dist/sip.js",
"main": "src/SIP.js",
"homepage": "http://sipjs.com",
"author": "Will Mitchell <[email protected]>",
"contributors": [
Expand All @@ -23,19 +23,19 @@
"javascript"
],
"devDependencies": {
"browserify": "~2.36.0",
"grunt": "~0.4.0",
"grunt-cli": "~0.1.6",
"grunt-contrib-concat": "~0.1.3",
"grunt-contrib-jasmine": "~0.6.0",
"grunt-contrib-jshint": ">0.5.0",
"grunt-contrib-uglify": "~0.2.0",
"grunt-include-replace": "~0.1.0",
"grunt-peg": "~1.3.1",
"grunt-trimtrailingspaces": "^0.4.0",
"node-minify": "~0.7.2",
"pegjs": "0.8.0",
"sdp-transform": "~0.4.0"
"sdp-transform": "~0.4.0",
"grunt-contrib-copy": "^0.5.0",
"browserify": "^4.1.8",
"grunt-browserify": "^2.1.0"
},
"engines": {
"node": ">=0.8"
Expand Down
4 changes: 2 additions & 2 deletions src/ClientContext.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function (SIP) {
module.exports = function (SIP) {
var ClientContext;

ClientContext = function (ua, method, target, options) {
Expand Down Expand Up @@ -111,4 +111,4 @@ ClientContext.prototype.onTransportError = function () {
};

SIP.ClientContext = ClientContext;
}(SIP));
};
6 changes: 4 additions & 2 deletions src/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
* @augments SIP
*/

SIP.C= {
USER_AGENT: SIP.name +'/'+ SIP.version,
module.exports = function (name, version) {
return {
USER_AGENT: name +'/'+ version,

// SIP scheme
SIP: 'sip',
Expand Down Expand Up @@ -156,3 +157,4 @@ SIP.C= {
606: 'Not Acceptable'
}
};
};
4 changes: 2 additions & 2 deletions src/Dialog/RequestSender.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @fileoverview in-Dialog Request Sender
*/

(function(SIP) {
module.exports = function (SIP) {
var RequestSender;

RequestSender = function(dialog, applicant, request) {
Expand Down Expand Up @@ -90,4 +90,4 @@ RequestSender.prototype = {
};

return RequestSender;
}(SIP));
};
7 changes: 2 additions & 5 deletions src/Dialogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
* @param {Enum} type UAC / UAS
* @param {Enum} state SIP.Dialog.C.STATUS_EARLY / SIP.Dialog.C.STATUS_CONFIRMED
*/
(function(SIP) {

// Load dependencies
var RequestSender = @@include('../src/Dialog/RequestSender.js')
module.exports = function (SIP, RequestSender) {

var Dialog,
C = {
Expand Down Expand Up @@ -254,4 +251,4 @@ Dialog.prototype = {

Dialog.C = C;
SIP.Dialog = Dialog;
}(SIP));
};
22 changes: 11 additions & 11 deletions src/DigestAuthentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @function Digest Authentication
* @param {SIP.UA} ua
*/
(function(SIP) {
module.exports = function (Utils) {
var DigestAuthentication;

DigestAuthentication = function(ua) {
Expand Down Expand Up @@ -78,7 +78,7 @@ DigestAuthentication.prototype.authenticate = function(request, challenge) {

this.method = request.method;
this.uri = request.ruri;
this.cnonce = SIP.Utils.createRandomToken(12);
this.cnonce = Utils.createRandomToken(12);
this.nc += 1;
this.updateNcHex();

Expand All @@ -103,25 +103,25 @@ DigestAuthentication.prototype.calculateResponse = function() {
var ha1, ha2;

// HA1 = MD5(A1) = MD5(username:realm:password)
ha1 = SIP.Utils.calculateMD5(this.username + ":" + this.realm + ":" + this.password);
ha1 = Utils.calculateMD5(this.username + ":" + this.realm + ":" + this.password);

if (this.qop === 'auth') {
// HA2 = MD5(A2) = MD5(method:digestURI)
ha2 = SIP.Utils.calculateMD5(this.method + ":" + this.uri);
ha2 = Utils.calculateMD5(this.method + ":" + this.uri);
// response = MD5(HA1:nonce:nonceCount:credentialsNonce:qop:HA2)
this.response = SIP.Utils.calculateMD5(ha1 + ":" + this.nonce + ":" + this.ncHex + ":" + this.cnonce + ":auth:" + ha2);
this.response = Utils.calculateMD5(ha1 + ":" + this.nonce + ":" + this.ncHex + ":" + this.cnonce + ":auth:" + ha2);

} else if (this.qop === 'auth-int') {
// HA2 = MD5(A2) = MD5(method:digestURI:MD5(entityBody))
ha2 = SIP.Utils.calculateMD5(this.method + ":" + this.uri + ":" + SIP.Utils.calculateMD5(this.body ? this.body : ""));
ha2 = Utils.calculateMD5(this.method + ":" + this.uri + ":" + Utils.calculateMD5(this.body ? this.body : ""));
// response = MD5(HA1:nonce:nonceCount:credentialsNonce:qop:HA2)
this.response = SIP.Utils.calculateMD5(ha1 + ":" + this.nonce + ":" + this.ncHex + ":" + this.cnonce + ":auth-int:" + ha2);
this.response = Utils.calculateMD5(ha1 + ":" + this.nonce + ":" + this.ncHex + ":" + this.cnonce + ":auth-int:" + ha2);

} else if (this.qop === null) {
// HA2 = MD5(A2) = MD5(method:digestURI)
ha2 = SIP.Utils.calculateMD5(this.method + ":" + this.uri);
ha2 = Utils.calculateMD5(this.method + ":" + this.uri);
// response = MD5(HA1:nonce:HA2)
this.response = SIP.Utils.calculateMD5(ha1 + ":" + this.nonce + ":" + ha2);
this.response = Utils.calculateMD5(ha1 + ":" + this.nonce + ":" + ha2);
}
};

Expand Down Expand Up @@ -164,5 +164,5 @@ DigestAuthentication.prototype.updateNcHex = function() {
this.ncHex = '00000000'.substr(0, 8-hex.length) + hex;
};

SIP.DigestAuthentication = DigestAuthentication;
}(SIP));
return DigestAuthentication;
};
4 changes: 2 additions & 2 deletions src/EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @augments SIP
* @class Class creating an event emitter.
*/
(function(SIP) {
module.exports = function (SIP) {
var
EventEmitter,
Event,
Expand Down Expand Up @@ -209,4 +209,4 @@ EventEmitter.C = C;

SIP.EventEmitter = EventEmitter;
SIP.Event = Event;
}(SIP));
};
8 changes: 1 addition & 7 deletions src/Exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
* SIP Exceptions.
* @augments SIP
*/
(function(SIP) {
var Exceptions;

Exceptions= {
module.exports = {
ConfigurationError: (function(){
var exception = function(parameter, value) {
this.code = 1;
Expand Down Expand Up @@ -53,6 +50,3 @@ Exceptions= {
return exception;
}())
};

SIP.Exceptions = Exceptions;
}(SIP));
4 changes: 2 additions & 2 deletions src/Grammar/dist/Grammar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* jshint ignore:start */
SIP.Grammar = (function() {
module.exports = function(SIP) {
/*
* Generated by PEG.js 0.8.0.
*
Expand Down Expand Up @@ -1273,5 +1273,5 @@ SIP.Grammar = (function() {
SyntaxError: SyntaxError,
parse: function (input, startRule) {return parse(input, {startRule: startRule});}
};
})();
};
/* jshint ignore:end */
Loading

0 comments on commit ba42061

Please sign in to comment.