Comparing version 0.5.0 to 0.6.0
@@ -90,7 +90,28 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Minijanus = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
/** Destroys this session. **/ | ||
/** | ||
* Destroys this session. Note that upon destruction, Janus will also close the signalling transport (if applicable) and | ||
* any open WebRTC connections. | ||
**/ | ||
JanusSession.prototype.destroy = function() { | ||
// Note that this message, unlike others, does *not* receive a response from janus, so there is no promise returned. | ||
this.send("destroy"); | ||
return this.send("destroy").then((resp) => { | ||
this.dispose(); | ||
return resp; | ||
}); | ||
}; | ||
/** | ||
* Disposes of this session in a way such that no further incoming signalling messages will be processed. | ||
* Outstanding transactions will be rejected. | ||
**/ | ||
JanusSession.prototype.dispose = function() { | ||
this._killKeepalive(); | ||
this.eventHandlers = {}; | ||
for (var txId in this.txns) { | ||
if (this.txns.hasOwnProperty(txId)) { | ||
var txn = this.txns[txId]; | ||
clearTimeout(txn.timeout); | ||
txn.reject(new Error("Janus session was disposed.")); | ||
delete this.txns[txId]; | ||
} | ||
} | ||
}; | ||
@@ -145,3 +166,3 @@ | ||
// this is a response to a transaction that wasn't caused via JanusSession.send, or a plugin replied twice to a | ||
// single request, or something else that isn't under our purview; that's fine | ||
// single request, or the session was disposed, or something else that isn't under our purview; that's fine | ||
return; | ||
@@ -156,5 +177,3 @@ } | ||
if (txn.timeout != null) { | ||
clearTimeout(txn.timeout); | ||
} | ||
clearTimeout(txn.timeout); | ||
@@ -167,15 +186,8 @@ delete this.txns[signal.transaction]; | ||
/** | ||
* Sends a signal associated with this session. Signals should be JSON-serializable objects. Returns a promise that will | ||
* be resolved or rejected when a response to this signal is received, or when no response is received within the | ||
* session timeout. | ||
* Sends a signal associated with this session, beginning a new transaction. Returns a promise that will be resolved or | ||
* rejected when a response is received in the same transaction, or when no response is received within the session | ||
* timeout. | ||
**/ | ||
JanusSession.prototype.send = function(type, signal) { | ||
var txid = (this.nextTxId++).toString(); | ||
signal = Object.assign({ janus: type, transaction: txid }, signal); | ||
if (this.id != null) { // this.id is undefined in the special case when we're sending the session create message | ||
signal = Object.assign({ session_id: this.id }, signal); | ||
} | ||
if (this.options.verbose) { | ||
console.debug("Outgoing Janus signal: ", signal); | ||
} | ||
signal = Object.assign({ transaction: (this.nextTxId++).toString() }, signal); | ||
return new Promise((resolve, reject) => { | ||
@@ -186,11 +198,25 @@ var timeout = null; | ||
delete this.txns[signal.transaction]; | ||
reject(new Error("Signalling message with txid " + txid + " timed out.")); | ||
reject(new Error("Signalling message with txid " + signal.transaction + " timed out.")); | ||
}, this.options.timeoutMs); | ||
} | ||
this.txns[signal.transaction] = { resolve: resolve, reject: reject, timeout: timeout, type: type }; | ||
this.output(JSON.stringify(signal)); | ||
this._resetKeepalive(); | ||
this._transmit(type, signal); | ||
}); | ||
}; | ||
JanusSession.prototype._transmit = function(type, signal) { | ||
signal = Object.assign({ janus: type }, signal); | ||
if (this.id != null) { // this.id is undefined in the special case when we're sending the session create message | ||
signal = Object.assign({ session_id: this.id }, signal); | ||
} | ||
if (this.options.verbose) { | ||
console.debug("Outgoing Janus signal: ", signal); | ||
} | ||
this.output(JSON.stringify(signal)); | ||
this._resetKeepalive(); | ||
}; | ||
JanusSession.prototype._sendKeepalive = function() { | ||
@@ -201,5 +227,3 @@ return this.send("keepalive"); | ||
JanusSession.prototype._killKeepalive = function() { | ||
if (this.keepaliveTimeout) { | ||
clearTimeout(this.keepaliveTimeout); | ||
} | ||
clearTimeout(this.keepaliveTimeout); | ||
}; | ||
@@ -210,3 +234,5 @@ | ||
if (this.options.keepaliveMs) { | ||
this.keepaliveTimeout = setTimeout(() => this._sendKeepalive(), this.options.keepaliveMs); | ||
this.keepaliveTimeout = setTimeout(() => { | ||
this._sendKeepalive().catch(e => console.error("Error received from keepalive: ", e)); | ||
}, this.options.keepaliveMs); | ||
} | ||
@@ -213,0 +239,0 @@ }; |
@@ -89,7 +89,28 @@ /** | ||
/** Destroys this session. **/ | ||
/** | ||
* Destroys this session. Note that upon destruction, Janus will also close the signalling transport (if applicable) and | ||
* any open WebRTC connections. | ||
**/ | ||
JanusSession.prototype.destroy = function() { | ||
// Note that this message, unlike others, does *not* receive a response from janus, so there is no promise returned. | ||
this.send("destroy"); | ||
return this.send("destroy").then((resp) => { | ||
this.dispose(); | ||
return resp; | ||
}); | ||
}; | ||
/** | ||
* Disposes of this session in a way such that no further incoming signalling messages will be processed. | ||
* Outstanding transactions will be rejected. | ||
**/ | ||
JanusSession.prototype.dispose = function() { | ||
this._killKeepalive(); | ||
this.eventHandlers = {}; | ||
for (var txId in this.txns) { | ||
if (this.txns.hasOwnProperty(txId)) { | ||
var txn = this.txns[txId]; | ||
clearTimeout(txn.timeout); | ||
txn.reject(new Error("Janus session was disposed.")); | ||
delete this.txns[txId]; | ||
} | ||
} | ||
}; | ||
@@ -144,3 +165,3 @@ | ||
// this is a response to a transaction that wasn't caused via JanusSession.send, or a plugin replied twice to a | ||
// single request, or something else that isn't under our purview; that's fine | ||
// single request, or the session was disposed, or something else that isn't under our purview; that's fine | ||
return; | ||
@@ -155,5 +176,3 @@ } | ||
if (txn.timeout != null) { | ||
clearTimeout(txn.timeout); | ||
} | ||
clearTimeout(txn.timeout); | ||
@@ -166,15 +185,8 @@ delete this.txns[signal.transaction]; | ||
/** | ||
* Sends a signal associated with this session. Signals should be JSON-serializable objects. Returns a promise that will | ||
* be resolved or rejected when a response to this signal is received, or when no response is received within the | ||
* session timeout. | ||
* Sends a signal associated with this session, beginning a new transaction. Returns a promise that will be resolved or | ||
* rejected when a response is received in the same transaction, or when no response is received within the session | ||
* timeout. | ||
**/ | ||
JanusSession.prototype.send = function(type, signal) { | ||
var txid = (this.nextTxId++).toString(); | ||
signal = Object.assign({ janus: type, transaction: txid }, signal); | ||
if (this.id != null) { // this.id is undefined in the special case when we're sending the session create message | ||
signal = Object.assign({ session_id: this.id }, signal); | ||
} | ||
if (this.options.verbose) { | ||
console.debug("Outgoing Janus signal: ", signal); | ||
} | ||
signal = Object.assign({ transaction: (this.nextTxId++).toString() }, signal); | ||
return new Promise((resolve, reject) => { | ||
@@ -185,11 +197,25 @@ var timeout = null; | ||
delete this.txns[signal.transaction]; | ||
reject(new Error("Signalling message with txid " + txid + " timed out.")); | ||
reject(new Error("Signalling message with txid " + signal.transaction + " timed out.")); | ||
}, this.options.timeoutMs); | ||
} | ||
this.txns[signal.transaction] = { resolve: resolve, reject: reject, timeout: timeout, type: type }; | ||
this.output(JSON.stringify(signal)); | ||
this._resetKeepalive(); | ||
this._transmit(type, signal); | ||
}); | ||
}; | ||
JanusSession.prototype._transmit = function(type, signal) { | ||
signal = Object.assign({ janus: type }, signal); | ||
if (this.id != null) { // this.id is undefined in the special case when we're sending the session create message | ||
signal = Object.assign({ session_id: this.id }, signal); | ||
} | ||
if (this.options.verbose) { | ||
console.debug("Outgoing Janus signal: ", signal); | ||
} | ||
this.output(JSON.stringify(signal)); | ||
this._resetKeepalive(); | ||
}; | ||
JanusSession.prototype._sendKeepalive = function() { | ||
@@ -200,5 +226,3 @@ return this.send("keepalive"); | ||
JanusSession.prototype._killKeepalive = function() { | ||
if (this.keepaliveTimeout) { | ||
clearTimeout(this.keepaliveTimeout); | ||
} | ||
clearTimeout(this.keepaliveTimeout); | ||
}; | ||
@@ -209,3 +233,5 @@ | ||
if (this.options.keepaliveMs) { | ||
this.keepaliveTimeout = setTimeout(() => this._sendKeepalive(), this.options.keepaliveMs); | ||
this.keepaliveTimeout = setTimeout(() => { | ||
this._sendKeepalive().catch(e => console.error("Error received from keepalive: ", e)); | ||
}, this.options.keepaliveMs); | ||
} | ||
@@ -212,0 +238,0 @@ }; |
{ | ||
"name": "minijanus", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Really simple and minimal wrapper for talking to the Janus signalling API.", | ||
@@ -5,0 +5,0 @@ "main": "minijanus.js", |
27
tests.js
@@ -87,1 +87,28 @@ var mj = require('./minijanus.js'); | ||
}); | ||
test('session transactions are properly disposed of', function(t) { | ||
var session = new mj.JanusSession(signal => {}, { timeoutMs: 5, keepaliveMs: null }); | ||
var message1 = session.send("message", { transaction: "message1" }).then( | ||
resp => { t.pass("Message 1 was received."); return resp; }, | ||
err => { t.fail("Message should have been received."); return err; } | ||
); | ||
var message2 = session.send("message", { transaction: "message2" }).then( | ||
resp => { t.fail("Message 2 shouldn't have been received."); return resp; }, | ||
err => { t.pass("Message 2 was correctly rejected."); return err; } | ||
); | ||
session.receive({ transaction: "message1", value: "test" }); | ||
session.dispose(); | ||
Promise.all([message1, message2]).then(results => { | ||
t.deepEqual(results[0], { transaction: "message1", value: "test" }); | ||
t.deepEqual(session.txns, {}); | ||
t.end(); | ||
}).catch(err => { | ||
t.fail(err); | ||
t.end(); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76708
515