@ndarilek/janus
Advanced tools
Comparing version 0.1.7 to 0.2.0
@@ -0,1 +1,6 @@ | ||
# V0.2.0 (2016-04-14) | ||
* Rewrite in TypeScript. | ||
* Remove need to set `Session.fetch`. | ||
# V0.1.7 (2016-04-11) | ||
@@ -2,0 +7,0 @@ |
412
lib/index.js
"use strict"; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _lodash = require("lodash"); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _eventemitter = require("eventemitter3"); | ||
var _eventemitter2 = _interopRequireDefault(_eventemitter); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var getTransactionId = function getTransactionId() { | ||
return (Math.random() * 10000000).toFixed().toString(); | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var janusFetch = function janusFetch(endpoint, args) { | ||
return Session.fetch(endpoint, _extends({ | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); | ||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | ||
return c > 3 && r && Object.defineProperty(target, key, r), r; | ||
}; | ||
var __metadata = (this && this.__metadata) || function (k, v) { | ||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
}; | ||
var es6_promise_1 = require("es6-promise"); | ||
var EventEmitter = require("eventemitter3"); | ||
var _ = require("lodash"); | ||
var objectAssign = require("object-assign"); | ||
var runtime_type_checks_1 = require('runtime-type-checks'); | ||
var getTransactionId = function () { return (Math.random() * 10000000).toFixed().toString(); }; | ||
var janusFetch = function (endpoint, args) { return fetch(endpoint, objectAssign({ | ||
headers: { | ||
"Accept": "application/json", | ||
"Content-Type": "application/json" | ||
"Accept": "application/json", | ||
"Content-Type": "application/json", | ||
}, | ||
}, args)).then(function (r) { return r.json(); }) | ||
.then(function (r) { | ||
if (r.janus == "error") | ||
throw new Error(r.error.reason); | ||
else | ||
return r; | ||
}); }; | ||
var Session = (function (_super) { | ||
__extends(Session, _super); | ||
function Session(endpoint) { | ||
var _this = this; | ||
_super.call(this); | ||
this.endpoint = endpoint; | ||
this.handles = {}; | ||
this.destroyed = false; | ||
if (!endpoint || endpoint.length == 0) | ||
throw new Error("Endpoint not specified"); | ||
janusFetch(endpoint, { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
janus: "create", | ||
transaction: Session.getTransactionId() | ||
}) | ||
}).then(function (r) { return r.data.id; }) | ||
.then(function (id) { | ||
_this.sessionId = id; | ||
_this.emit("connected"); | ||
_this._poll(); | ||
}).catch(console.error); | ||
} | ||
}, args)).then(function (r) { | ||
return r.json(); | ||
}).then(function (r) { | ||
if (r.janus == "error") throw new Error(r.error.reason);else return r; | ||
}).catch(console.error); | ||
}; | ||
var Session = function (_EventEmitter) { | ||
_inherits(Session, _EventEmitter); | ||
function Session(endpoint) { | ||
_classCallCheck(this, Session); | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Session).call(this)); | ||
if (!endpoint) throw new Error("Must specify an endpoint"); | ||
if (typeof endpoint != "string") throw new Error("`endpoint` is not a string"); | ||
if (typeof Session.fetch == "undefined") { | ||
if (typeof window != "undefined" && window.fetch) { | ||
Session.fetch = window.fetch.bind(window); | ||
console.warn("Setting .fetch property to the value of window.fetch. Set it explicitly if things behave oddly."); | ||
} else if (typeof fetch != "undefined") { | ||
Session.fetch = fetch; | ||
console.warn("Setting .fetch property to the global value. Set it explicitly if things behave oddly."); | ||
} else throw new Error("No fetch implementation configured. Please set the .fetch static property on this class to an implementor of the fetch specification."); | ||
Session.prototype.fullEndpoint = function () { | ||
return this.endpoint + "/" + this.sessionId; | ||
}; | ||
Session.prototype._poll = function () { | ||
var _this = this; | ||
janusFetch(this.fullEndpoint()) | ||
.then(function (r) { | ||
var handle = null; | ||
if (r.sender && _this.handles[r.sender]) | ||
handle = _this.handles[r.sender]; | ||
if (r.janus == "event" && handle) { | ||
var payload = {}; | ||
if (r.plugindata && r.plugindata.data) | ||
payload.data = r.plugindata.data; | ||
if (r.jsep) | ||
payload.jsep = r.jsep; | ||
handle.emit("event", payload); | ||
} | ||
else if (r.janus == "webrtcup") { | ||
_this.emit("webrtcup", r); | ||
if (handle) | ||
handle.emit("webrtcup"); | ||
} | ||
else if (r.janus == "media") { | ||
_this.emit("media", r); | ||
if (handle) | ||
handle.emit("media", r); | ||
} | ||
else if (r.janus == "hangup") { | ||
_this.emit("hangup", r); | ||
if (handle) | ||
handle.emit("hangup"); | ||
} | ||
if (!_this.destroyed) | ||
_this._poll(); | ||
}); | ||
}; | ||
Session.prototype.attach = function (pluginId) { | ||
var _this = this; | ||
return janusFetch(this.fullEndpoint(), { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
janus: "attach", | ||
plugin: pluginId, | ||
transaction: Session.getTransactionId() | ||
}) | ||
}).then(function (r) { | ||
var id = r.data.id; | ||
var h = new Handle(_this, id); | ||
_this.handles[id] = h; | ||
return h; | ||
}); | ||
}; | ||
Session.prototype.destroy = function () { | ||
var _this = this; | ||
this.emit("destroying"); | ||
this.destroyed = true; | ||
es6_promise_1.Promise.all(_.values(this.handles).map(function (h) { return h.destroy(); })) | ||
.then(function () { | ||
janusFetch(_this.fullEndpoint(), { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
janus: "destroy", | ||
transaction: Session.getTransactionId() | ||
}) | ||
}); | ||
}).then(function () { return _this.emit("destroyed"); }); | ||
}; | ||
Session.getTransactionId = getTransactionId; | ||
Session = __decorate([ | ||
runtime_type_checks_1.CheckParams(), | ||
__metadata('design:paramtypes', [String]) | ||
], Session); | ||
return Session; | ||
}(EventEmitter)); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = Session; | ||
var Handle = (function (_super) { | ||
__extends(Handle, _super); | ||
function Handle(session, id) { | ||
_super.call(this); | ||
this.session = session; | ||
this.id = id; | ||
} | ||
_this.endpoint = endpoint; | ||
_this.handles = {}; | ||
_this.destroyed = false; | ||
if (!Session.getTransactionId) Session.getTransactionId = getTransactionId; | ||
janusFetch(endpoint, { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
janus: "create", | ||
transaction: Session.getTransactionId() | ||
}) | ||
}).then(function (r) { | ||
return r.data.id; | ||
}).then(function (id) { | ||
_this.sessionId = id; | ||
_this.emit("connected"); | ||
_this._poll(); | ||
}).catch(console.error); | ||
return _this; | ||
} | ||
_createClass(Session, [{ | ||
key: "fullEndpoint", | ||
value: function fullEndpoint() { | ||
return this.endpoint + "/" + this.sessionId; | ||
} | ||
}, { | ||
key: "_poll", | ||
value: function _poll() { | ||
var _this2 = this; | ||
janusFetch(this.fullEndpoint()).then(function (r) { | ||
var handle = null; | ||
if (r.sender && _this2.handles[r.sender]) handle = _this2.handles[r.sender]; | ||
if (r.janus == "event" && handle) { | ||
var payload = {}; | ||
if (r.plugindata && r.plugindata.data) payload.data = r.plugindata.data; | ||
if (r.jsep) payload.jsep = r.jsep; | ||
handle.emit("event", payload); | ||
} else if (r.janus == "webrtcup") { | ||
_this2.emit("webrtcup", r); | ||
if (handle) handle.emit("webrtcup"); | ||
} else if (r.janus == "media") { | ||
_this2.emit("media", r); | ||
if (handle) handle.emit("media", r); | ||
} else if (r.janus == "hangup") { | ||
_this2.emit("hangup", r); | ||
if (handle) handle.emit("hangup"); | ||
} | ||
if (!_this2.destroyed) _this2._poll(); | ||
}); | ||
} | ||
}, { | ||
key: "attach", | ||
value: function attach(pluginId) { | ||
var _this3 = this; | ||
return janusFetch(this.fullEndpoint(), { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
janus: "attach", | ||
plugin: pluginId, | ||
transaction: Session.getTransactionId() | ||
}) | ||
}).then(function (r) { | ||
var id = r.data.id; | ||
var h = new Handle(_this3, id); | ||
_this3.handles[id] = h; | ||
return h; | ||
}); | ||
} | ||
}, { | ||
key: "destroy", | ||
value: function destroy() { | ||
var _this4 = this; | ||
this.emit("destroying"); | ||
this.destroyed = true; | ||
Promise.all(_lodash2.default.values(this.handles).map(function (h) { | ||
return h.destroy(); | ||
})).then(function () { | ||
janusFetch(_this4.fullEndpoint(), { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
janus: "destroy", | ||
transaction: Session.getTransactionId() | ||
}) | ||
Handle.prototype.fullEndpoint = function () { | ||
return this.session.fullEndpoint() + "/" + this.id; | ||
}; | ||
Handle.prototype.message = function (body, jsep) { | ||
var payload = { janus: "message", transaction: Session.getTransactionId() }; | ||
if (body) | ||
payload.body = body; | ||
else | ||
payload.body = {}; | ||
if (jsep) | ||
payload.jsep = jsep; | ||
return janusFetch(this.fullEndpoint(), { | ||
method: "POST", | ||
body: JSON.stringify(payload) | ||
}); | ||
}).then(function () { | ||
return _this4.emit("destroyed"); | ||
}); | ||
} | ||
}]); | ||
return Session; | ||
}(_eventemitter2.default); | ||
var Handle = function (_EventEmitter2) { | ||
_inherits(Handle, _EventEmitter2); | ||
function Handle(session, id) { | ||
_classCallCheck(this, Handle); | ||
var _this5 = _possibleConstructorReturn(this, Object.getPrototypeOf(Handle).call(this)); | ||
_this5.session = session; | ||
_this5.id = id; | ||
return _this5; | ||
} | ||
_createClass(Handle, [{ | ||
key: "fullEndpoint", | ||
value: function fullEndpoint() { | ||
return this.session.fullEndpoint() + "/" + this.id; | ||
} | ||
}, { | ||
key: "message", | ||
value: function message(body, jsep) { | ||
var payload = { janus: "message", transaction: Session.getTransactionId() }; | ||
if (body) payload.body = body;else payload.body = {}; | ||
if (jsep) payload.jsep = jsep; | ||
return janusFetch(this.fullEndpoint(), { | ||
method: "POST", | ||
body: JSON.stringify(payload) | ||
}); | ||
} | ||
}, { | ||
key: "trickle", | ||
value: function trickle(candidates) { | ||
var body = { janus: "trickle", transaction: Session.getTransactionId() }; | ||
if (!candidates) body.candidate = { completed: true };else if (candidates.constructor == Array) body.candidates = candidates;else if ((typeof candidates === "undefined" ? "undefined" : _typeof(candidates)) == "object") body.candidate = candidates; | ||
return janusFetch(this.fullEndpoint(), { | ||
method: "POST", | ||
body: JSON.stringify(body) | ||
}); | ||
} | ||
}, { | ||
key: "hangup", | ||
value: function hangup() { | ||
return janusFetch(this.fullEndpoint(), { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
janus: "hangup", | ||
transaction: Session.getTransactionId() | ||
}) | ||
}); | ||
} | ||
}, { | ||
key: "destroy", | ||
value: function destroy() { | ||
var _this6 = this; | ||
this.emit("destroying"); | ||
janusFetch(this.fullEndpoint(), { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
janus: "detach", | ||
transaction: Session.getTransactionId() | ||
}) | ||
}).then(function (r) { | ||
return _this6.emit("destroyed"); | ||
}); | ||
} | ||
}]); | ||
return Handle; | ||
}(_eventemitter2.default); | ||
module.exports = Session; | ||
}; | ||
Handle.prototype.trickle = function (candidates) { | ||
var body = { janus: "trickle", transaction: Session.getTransactionId() }; | ||
if (!candidates) | ||
body.candidate = { completed: true }; | ||
else if (candidates.constructor == Array) | ||
body.candidates = candidates; | ||
else if (typeof (candidates) == "object") | ||
body.candidate = candidates; | ||
return janusFetch(this.fullEndpoint(), { | ||
method: "POST", | ||
body: JSON.stringify(body) | ||
}); | ||
}; | ||
Handle.prototype.hangup = function () { | ||
return janusFetch(this.fullEndpoint(), { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
janus: "hangup", | ||
transaction: Session.getTransactionId() | ||
}) | ||
}); | ||
}; | ||
Handle.prototype.destroy = function () { | ||
var _this = this; | ||
this.emit("destroying"); | ||
janusFetch(this.fullEndpoint(), { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
janus: "detach", | ||
transaction: Session.getTransactionId() | ||
}) | ||
}).then(function (r) { return _this.emit("destroyed"); }); | ||
}; | ||
Handle = __decorate([ | ||
runtime_type_checks_1.CheckParams(), | ||
__metadata('design:paramtypes', [Session, Number]) | ||
], Handle); | ||
return Handle; | ||
}(EventEmitter)); | ||
exports.Handle = Handle; |
{ | ||
"name": "@ndarilek/janus", | ||
"version": "0.1.7", | ||
"version": "0.2.0", | ||
"description": "Simple API for the Janus media server", | ||
"main": "lib/index.js", | ||
"typings": "lib/index", | ||
"scripts": { | ||
"build": "babel src --out-dir lib", | ||
"build": "tsc", | ||
"postinstall": "typings install", | ||
"prepublish": "npm run build", | ||
"pretest": "tsc", | ||
"test": "mocha --compilers js:babel-register" | ||
@@ -23,12 +26,16 @@ }, | ||
"babel-cli": "^6.6.5", | ||
"babel-plugin-transform-object-rest-spread": "^6.6.5", | ||
"babel-preset-es2015": "^6.6.0", | ||
"babel-register": "^6.7.2", | ||
"mocha": "^2.4.5", | ||
"simple-mock": "^0.7.0" | ||
"simple-mock": "^0.7.0", | ||
"typescript": "^1.9.0-dev.20160414", | ||
"typings": "^0.7.12" | ||
}, | ||
"dependencies": { | ||
"es6-promise": "^3.1.2", | ||
"eventemitter3": "^1.2.0", | ||
"lodash": "^4.8.2" | ||
"isomorphic-fetch": "^2.2.1", | ||
"lodash": "^4.8.2", | ||
"runtime-type-checks": "0.0.4" | ||
} | ||
} |
@@ -61,16 +61,2 @@ # A Leaner, Cleaner JavaScript API for the [Janus WebRTC Gateway](https://janus.conf.meetecho.com/) | ||
## A Note About Fetch | ||
This API uses the [Fetch](https://fetch.spec.whatwg.org/) API extensively. Before using it, you can configure which implementation to use like so: | ||
``` | ||
import Session fro "@nolan/janus" | ||
import fetch from "node-fetch" | ||
Session.fetch = fetch | ||
const s = new Session(...) | ||
``` | ||
If there is a `fetch` variable in the global namespace and the `.fetch` property is unset, it will be set to the value of the `fetch` global when an instance is first created. If this isn't what you want, then set the property explicitly. | ||
## API | ||
@@ -77,0 +63,0 @@ |
import assert from "assert" | ||
import simple from "simple-mock" | ||
import Session from "../src" | ||
import Session from "../lib" | ||
@@ -13,3 +13,3 @@ describe("Session", function() { | ||
simple.mock(Session, "getTransactionId").returnWith(this.transactionId) | ||
simple.mock(Session, "fetch").resolveWith(this.result) | ||
simple.mock(global, "fetch").resolveWith(this.result) | ||
simple.mock(Session.prototype, "emit") | ||
@@ -25,7 +25,2 @@ simple.mock(Session.prototype, "_poll").resolveWith(true) | ||
it("throws if no `fetch` implementation is specified", function() { | ||
Session.fetch = null | ||
assert.throws(() => new Session()) | ||
}) | ||
it("throws if no endpoint is specified", function() { | ||
@@ -41,4 +36,4 @@ assert.throws(() => new Session()) | ||
const s = new Session("endpoint") | ||
assert(Session.fetch.called) | ||
const call = Session.fetch.calls[0] | ||
assert(fetch.called) | ||
const call = fetch.calls[0] | ||
assert.equal(call.args[0], "endpoint") | ||
@@ -45,0 +40,0 @@ assert.deepEqual(call.args[1], { |
Sorry, the diff of this file is not supported yet
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
7
276
14239
5
7
79
1
4
+ Addedes6-promise@^3.1.2
+ Addedisomorphic-fetch@^2.2.1
+ Addedruntime-type-checks@0.0.4
+ Addedencoding@0.1.13(transitive)
+ Addedes6-promise@3.3.1(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisomorphic-fetch@2.2.1(transitive)
+ Addednode-fetch@1.7.3(transitive)
+ Addedreflect-metadata@0.1.14(transitive)
+ Addedruntime-type-checks@0.0.4(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedwhatwg-fetch@3.6.20(transitive)