@conectar/conectar-ws
Advanced tools
Comparing version 0.0.24 to 0.0.25
355
lib/wire.js
@@ -12,4 +12,4 @@ export default class Wire { | ||
{ | ||
urls: 'stun:stun.l.google.com:19302', | ||
}, | ||
urls: "stun:stun.l.google.com:19302" | ||
} | ||
]; | ||
@@ -23,21 +23,23 @@ this.listeners = {}; | ||
if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') { | ||
this.host = 'localhost:8081'; | ||
} else if(window.location.hostname === 'staging.conectar.ru') { | ||
if ( | ||
window.location.hostname === "localhost" || | ||
window.location.hostname === "127.0.0.1" | ||
) { | ||
this.host = "localhost:8081"; | ||
} else if (window.location.hostname === "staging.conectar.ru") { | ||
// if the url is the staging url | ||
this.host = 'classroom-staging-api.conectar.ru'; | ||
this.host = "classroom-staging-api.conectar.ru"; | ||
} else { | ||
this.host = "classroom-api.conectar.ru"; | ||
} | ||
else { | ||
this.host = 'classroom-api.conectar.ru'; | ||
} | ||
if (window.location.protocol === 'http:') { | ||
if (window.location.protocol === "http:") { | ||
this.protocols = { | ||
ws: 'ws:', | ||
http: 'http:', | ||
ws: "ws:", | ||
http: "http:" | ||
}; | ||
} else { | ||
this.protocols = { | ||
ws: 'wss:', | ||
http: 'https:', | ||
ws: "wss:", | ||
http: "https:" | ||
}; | ||
@@ -49,3 +51,3 @@ } | ||
if (this.listeners[event]) { | ||
this.listeners[event].forEach((listener) => { | ||
this.listeners[event].forEach(listener => { | ||
listener(...params); | ||
@@ -76,6 +78,8 @@ }); | ||
this.connection = new WebSocket(`${this.protocols.ws}//${this.host}${url}`); | ||
this.connection = new WebSocket( | ||
`${this.protocols.ws}//${this.host}${url}` | ||
); | ||
this.connection.onopen = () => { | ||
this.wait_to_reconnect = 1; | ||
this.run_listeners('connected'); | ||
this.run_listeners("connected"); | ||
resolve(); | ||
@@ -85,3 +89,3 @@ }; | ||
// eslint-disable-next-line | ||
console.error('WIRE: websocket connection closed, reconnecting...'); | ||
console.error("WIRE: websocket connection closed, reconnecting..."); | ||
setTimeout(() => { | ||
@@ -91,6 +95,6 @@ this.connect(url); | ||
}; | ||
this.connection.onmessage = (event) => { | ||
this.connection.onmessage = event => { | ||
const data = JSON.parse(event.data); | ||
switch (data.action) { | ||
case 'join': | ||
case "join": | ||
this.peers[data.user_id] = data; | ||
@@ -101,28 +105,30 @@ this.peer_connections[data.user_id] = {}; | ||
}); | ||
this.run_listeners('joined', this.prepare_time(data)); | ||
this.run_listeners("joined", this.prepare_time(data)); | ||
break; | ||
case 'leave': | ||
case "leave": | ||
delete this.peers[data.user_id]; | ||
Object.entries(this.peer_connections[data.user_id]).forEach(([tag, pc]) => { | ||
pc.close(); | ||
}); | ||
Object.entries(this.peer_connections[data.user_id]).forEach( | ||
([tag, pc]) => { | ||
pc.close(); | ||
} | ||
); | ||
delete this.peer_connections[data.user_id]; | ||
this.run_listeners('left', this.prepare_time(data)); | ||
this.run_listeners("left", this.prepare_time(data)); | ||
break; | ||
case 'message': | ||
case 'private_message': | ||
case "message": | ||
case "private_message": | ||
this.run_listeners(data.action, this.prepare_time(data)); | ||
break; | ||
case 'private_event': | ||
case "private_event": | ||
this.run_listeners(data.payload, data.sender); | ||
break; | ||
case 'webrtc': | ||
case "webrtc": | ||
switch (data.type) { | ||
case 'offer': | ||
case "offer": | ||
this.handle_remote_offer(data); | ||
break; | ||
case 'answer': | ||
case "answer": | ||
this.handle_remote_answer(data); | ||
break; | ||
case 'candidate': | ||
case "candidate": | ||
this.handle_remote_candidate(data); | ||
@@ -132,3 +138,3 @@ break; | ||
// eslint-disable-next-line | ||
console.error(`unknown wrtc type ${data.type}`); | ||
console.error(`unknown wrtc type ${data.type}`); | ||
} | ||
@@ -147,4 +153,4 @@ break; | ||
action, | ||
...data, | ||
}), | ||
...data | ||
}) | ||
); | ||
@@ -154,12 +160,18 @@ } | ||
send_private_message(recipient, text) { | ||
this.send('private_message', { | ||
this.send("private_message", { | ||
recipient, | ||
text, | ||
}); | ||
text | ||
} | ||
send_room_message(text) { | ||
this.send("message", { | ||
text | ||
}); | ||
} | ||
send_voice_call_request(recipient) { | ||
this.send('private_event', { | ||
payload: 'voice_call_request', | ||
recipient, | ||
this.send("private_event", { | ||
payload: "voice_call_request", | ||
recipient | ||
}); | ||
@@ -169,5 +181,5 @@ } | ||
send_voice_call_accept(recipient) { | ||
this.send('private_event', { | ||
payload: 'voice_call_accept', | ||
recipient, | ||
this.send("private_event", { | ||
payload: "voice_call_accept", | ||
recipient | ||
}); | ||
@@ -177,5 +189,5 @@ } | ||
send_video_call_request(recipient) { | ||
this.send('private_event', { | ||
payload: 'video_call_request', | ||
recipient, | ||
this.send("private_event", { | ||
payload: "video_call_request", | ||
recipient | ||
}); | ||
@@ -185,5 +197,5 @@ } | ||
send_video_call_accept(recipient) { | ||
this.send('private_event', { | ||
payload: 'video_call_accept', | ||
recipient, | ||
this.send("private_event", { | ||
payload: "video_call_accept", | ||
recipient | ||
}); | ||
@@ -193,6 +205,6 @@ } | ||
createWhiteboard(whiteboard_id, whiteboard_name) { | ||
this.send('new_whiteboard', { | ||
action: 'new_whiteboard', | ||
this.send("new_whiteboard", { | ||
action: "new_whiteboard", | ||
whiteboard_id, | ||
whiteboard_name, | ||
whiteboard_name | ||
}); | ||
@@ -202,6 +214,6 @@ } | ||
renameWhiteboard(whiteboard_id, whiteboard_name) { | ||
this.send('rename_whiteboard', { | ||
action: 'rename_whiteboard', | ||
this.send("rename_whiteboard", { | ||
action: "rename_whiteboard", | ||
whiteboard_id, | ||
whiteboard_name, | ||
whiteboard_name | ||
}); | ||
@@ -211,6 +223,6 @@ } | ||
add_whiteboard_metadata(whiteboard_id, whiteboard_meta) { | ||
this.send('update_whiteboard', { | ||
action: 'update_whiteboard', | ||
this.send("update_whiteboard", { | ||
action: "update_whiteboard", | ||
whiteboard_id, | ||
whiteboard_meta, | ||
whiteboard_meta | ||
}); | ||
@@ -220,5 +232,5 @@ } | ||
add_room_metadata(meta) { | ||
this.send('update_room_meta', { | ||
action: 'update_room_meta', | ||
meta, | ||
this.send("update_room_meta", { | ||
action: "update_room_meta", | ||
meta | ||
}); | ||
@@ -228,5 +240,5 @@ } | ||
removeWhiteboard(whiteboard_id) { | ||
this.send('remove_whiteboard', { | ||
action: 'remove_whiteboard', | ||
whiteboard_id, | ||
this.send("remove_whiteboard", { | ||
action: "remove_whiteboard", | ||
whiteboard_id | ||
}); | ||
@@ -236,7 +248,7 @@ } | ||
addObject(whiteboard_id, object_id, object) { | ||
this.send('add_whiteboard_object', { | ||
action: 'add_whiteboard_object', | ||
this.send("add_whiteboard_object", { | ||
action: "add_whiteboard_object", | ||
whiteboard_id, | ||
object_id, | ||
object, | ||
object | ||
}); | ||
@@ -246,6 +258,6 @@ } | ||
removeObject(whiteboard_id, object_id) { | ||
this.send('remove_whiteboard_object', { | ||
action: 'remove_whiteboard_object', | ||
this.send("remove_whiteboard_object", { | ||
action: "remove_whiteboard_object", | ||
whiteboard_id, | ||
object_id, | ||
object_id | ||
}); | ||
@@ -256,5 +268,5 @@ } | ||
const payload = { | ||
action: 'private_event', | ||
payload: 'call_decline', | ||
recipient, | ||
action: "private_event", | ||
payload: "call_decline", | ||
recipient | ||
}; | ||
@@ -267,3 +279,3 @@ this.connection.send(JSON.stringify(payload)); | ||
.fetch(`${this.protocols.http}//${this.host}/${roomId}/whiteboards/`, { | ||
headers: this.options.httpHeaders, | ||
headers: this.options.httpHeaders | ||
}) | ||
@@ -277,3 +289,3 @@ .then(res => res.json()); | ||
headers: this.options.httpHeaders, | ||
credentials: 'include', | ||
credentials: "include" | ||
}) | ||
@@ -291,4 +303,4 @@ .then(res => res.json()); | ||
headers: this.options.httpHeaders, | ||
credentials: 'include', | ||
}, | ||
credentials: "include" | ||
} | ||
) | ||
@@ -302,3 +314,3 @@ .then(res => res.json()); | ||
headers: this.options.httpHeaders, | ||
credentials: 'include', | ||
credentials: "include" | ||
}) | ||
@@ -310,6 +322,11 @@ .then(res => res.json()); | ||
return window | ||
.fetch(`${this.protocols.http}//${this.host}/conversations/${user_id}/messages/`, { | ||
credentials: 'include', | ||
headers: this.options.httpHeaders, | ||
}) | ||
.fetch( | ||
`${this.protocols.http}//${ | ||
this.host | ||
}/conversations/${user_id}/messages/`, | ||
{ | ||
credentials: "include", | ||
headers: this.options.httpHeaders | ||
} | ||
) | ||
.then(response => response.json() || response); | ||
@@ -321,4 +338,4 @@ } | ||
.fetch(`${this.protocols.http}//${this.host}/conversations/`, { | ||
credentials: 'include', | ||
headers: this.options.httpHeaders, | ||
credentials: "include", | ||
headers: this.options.httpHeaders | ||
}) | ||
@@ -332,4 +349,4 @@ .then(response => response.json() || response); | ||
...options, | ||
credentials: 'include', | ||
headers: this.options.httpHeaders, | ||
credentials: "include", | ||
headers: this.options.httpHeaders | ||
}) | ||
@@ -339,10 +356,8 @@ .then(response => response.json() || response); | ||
handle_remote_offer({ | ||
sender, media, direction, payload, | ||
}) { | ||
handle_remote_offer({ sender, media, direction, payload }) { | ||
const pc = this.get_peerconnection(sender, media, direction); | ||
const desc = new RTCSessionDescription({ | ||
type: 'offer', | ||
sdp: payload, | ||
type: "offer", | ||
sdp: payload | ||
}); | ||
@@ -354,8 +369,8 @@ | ||
.then(() => { | ||
this.send('webrtc', { | ||
this.send("webrtc", { | ||
recipient: sender, | ||
type: 'answer', | ||
type: "answer", | ||
media, | ||
direction, | ||
payload: pc.localDescription.sdp, | ||
payload: pc.localDescription.sdp | ||
}); | ||
@@ -367,8 +382,8 @@ }); | ||
if (event.candidate) { | ||
this.send('webrtc', { | ||
this.send("webrtc", { | ||
recipient: user_id, | ||
type: 'candidate', | ||
type: "candidate", | ||
media, | ||
direction, | ||
payload: JSON.stringify(event.candidate.toJSON()), | ||
payload: JSON.stringify(event.candidate.toJSON()) | ||
}); | ||
@@ -378,9 +393,7 @@ } | ||
handle_remote_candidate({ | ||
sender, media, direction, payload, | ||
}) { | ||
handle_remote_candidate({ sender, media, direction, payload }) { | ||
const candidate = new RTCIceCandidate(JSON.parse(payload)); | ||
const pc = this.get_peerconnection(sender, media, direction); | ||
// eslint-disable-next-line | ||
pc.addIceCandidate(candidate).catch(console.error); | ||
pc.addIceCandidate(candidate).catch(console.error); | ||
} | ||
@@ -390,12 +403,12 @@ | ||
const pc = this.get_peerconnection(user_id, media, direction); | ||
if (pc.signalingState !== 'stable') return; | ||
if (pc.signalingState !== "stable") return; | ||
pc.createOffer() | ||
.then(offer => pc.setLocalDescription(offer)) | ||
.then(() => { | ||
this.send('webrtc', { | ||
this.send("webrtc", { | ||
recipient: user_id, | ||
type: 'offer', | ||
type: "offer", | ||
media, | ||
direction, | ||
payload: pc.localDescription.sdp, | ||
payload: pc.localDescription.sdp | ||
}); | ||
@@ -405,9 +418,7 @@ }); | ||
handle_remote_answer({ | ||
sender, media, direction, payload, | ||
}) { | ||
handle_remote_answer({ sender, media, direction, payload }) { | ||
const pc = this.get_peerconnection(sender, media, direction); | ||
const desc = new RTCSessionDescription({ | ||
type: 'answer', | ||
sdp: payload, | ||
type: "answer", | ||
sdp: payload | ||
}); | ||
@@ -418,3 +429,3 @@ pc.setRemoteDescription(desc); | ||
handle_remote_track(user_id, media, event) { | ||
this.run_listeners('new_stream', user_id, media, event.streams[0]); | ||
this.run_listeners("new_stream", user_id, media, event.streams[0]); | ||
} | ||
@@ -425,14 +436,16 @@ | ||
switch (pc.iceConnectionState) { | ||
case 'connected': | ||
case "connected": | ||
break; | ||
case 'failed': | ||
case "failed": | ||
// eslint-disable-next-line | ||
console.error(`Peer connection ${user_id} ${media}_${direction} failed`); | ||
console.error( | ||
`Peer connection ${user_id} ${media}_${direction} failed` | ||
); | ||
break; | ||
case 'disconnected': | ||
case 'closed': | ||
this.run_listeners('closed', { | ||
case "disconnected": | ||
case "closed": | ||
this.run_listeners("closed", { | ||
user_id, | ||
media, | ||
direction, | ||
direction | ||
}); | ||
@@ -445,7 +458,10 @@ break; | ||
hande_remove_track(user_id, media, event) { | ||
this.run_listeners('stream_removed', user_id, media); | ||
this.run_listeners("stream_removed", user_id, media); | ||
} | ||
get_peerconnection(user_id, media, direction) { | ||
if (this.peer_connections[user_id] && this.peer_connections[user_id][`${media}_${direction}`]) { | ||
if ( | ||
this.peer_connections[user_id] && | ||
this.peer_connections[user_id][`${media}_${direction}`] | ||
) { | ||
return this.peer_connections[user_id][`${media}_${direction}`]; | ||
@@ -457,18 +473,18 @@ } | ||
const pc = new RTCPeerConnection({ | ||
iceServers: this.iceServers, | ||
iceServers: this.iceServers | ||
}); | ||
this.peer_connections[user_id][`${media}_${direction}`] = pc; | ||
pc.addEventListener('icecandidate', (event) => { | ||
pc.addEventListener("icecandidate", event => { | ||
this.handle_local_candidate(user_id, media, direction, event); | ||
}); | ||
pc.addEventListener('track', (event) => { | ||
pc.addEventListener("track", event => { | ||
this.handle_remote_track(user_id, media, event); | ||
}); | ||
pc.addEventListener('negotiationneeded', () => { | ||
pc.addEventListener("negotiationneeded", () => { | ||
this.handle_negotiation_needed(user_id, media, direction); | ||
}); | ||
pc.addEventListener('removetrack', (event) => { | ||
pc.addEventListener("removetrack", event => { | ||
this.handle_remove_track(user_id, media, event); | ||
}); | ||
pc.addEventListener('iceconnectionstatechange', (event) => { | ||
pc.addEventListener("iceconnectionstatechange", event => { | ||
this.handle_ice_connection_state_change(user_id, media, direction, event); | ||
@@ -482,3 +498,3 @@ }); | ||
send_stream(user_id, media, stream) { | ||
const pc = this.get_peerconnection(user_id, media, 'out'); | ||
const pc = this.get_peerconnection(user_id, media, "out"); | ||
stream.getTracks().forEach(track => pc.addTrack(track, stream)); | ||
@@ -518,7 +534,7 @@ } | ||
if (!this.local_streams.mic) { | ||
this.start_mic().then((stream) => { | ||
this.add_stream(stream, 'mic', peer_id); | ||
this.start_mic().then(stream => { | ||
this.add_stream(stream, "mic", peer_id); | ||
}); | ||
} else { | ||
this.add_stream(this.local_streams.mic, 'mic', peer_id); | ||
this.add_stream(this.local_streams.mic, "mic", peer_id); | ||
} | ||
@@ -529,14 +545,14 @@ } | ||
if (!this.local_streams.mic) { | ||
this.start_mic().then((stream) => { | ||
this.add_stream(stream, 'mic', peer_id); | ||
this.start_mic().then(stream => { | ||
this.add_stream(stream, "mic", peer_id); | ||
}); | ||
} else { | ||
this.add_stream(this.local_streams.mic, 'mic', peer_id); | ||
this.add_stream(this.local_streams.mic, "mic", peer_id); | ||
} | ||
if (!this.local_streams.camera) { | ||
this.start_camera().then((stream) => { | ||
this.add_stream(stream, 'camera', peer_id); | ||
this.start_camera().then(stream => { | ||
this.add_stream(stream, "camera", peer_id); | ||
}); | ||
} | ||
this.add_stream(this.local_streams.camera, 'camera', peer_id); | ||
this.add_stream(this.local_streams.camera, "camera", peer_id); | ||
} | ||
@@ -548,5 +564,5 @@ | ||
.getUserMedia({ | ||
video: true, | ||
video: true | ||
}) | ||
.then((stream) => { | ||
.then(stream => { | ||
this.local_streams.camera = stream; | ||
@@ -563,5 +579,5 @@ return stream; | ||
.getUserMedia({ | ||
audio: true, | ||
audio: true | ||
}) | ||
.then((stream) => { | ||
.then(stream => { | ||
this.local_streams.mic = stream; | ||
@@ -578,4 +594,4 @@ return stream; | ||
this.start_screensharing() | ||
.then((stream) => { | ||
this.add_stream(stream, 'screen', peer_id); | ||
.then(stream => { | ||
this.add_stream(stream, "screen", peer_id); | ||
}) | ||
@@ -586,4 +602,4 @@ .catch(reject) | ||
this.start_mic() | ||
.then((stream) => { | ||
this.add_stream(stream, 'mic', peer_id); | ||
.then(stream => { | ||
this.add_stream(stream, "mic", peer_id); | ||
resolve(); | ||
@@ -620,5 +636,5 @@ }) | ||
hang_up(recipient) { | ||
this.send('private_event', { | ||
payload: 'hang_up', | ||
recipient, | ||
this.send("private_event", { | ||
payload: "hang_up", | ||
recipient | ||
}); | ||
@@ -633,3 +649,3 @@ this.end_mic(); | ||
if (window.chrome) { | ||
const extension_tag = 'cnctrchrxt'; | ||
const extension_tag = "cnctrchrxt"; | ||
let extension_found = false; | ||
@@ -641,16 +657,19 @@ | ||
extension_tag, | ||
direction: 'p2e', | ||
...message, | ||
direction: "p2e", | ||
...message | ||
}, | ||
window.origin, | ||
window.origin | ||
); | ||
}; | ||
window.addEventListener('message', (event) => { | ||
if (event.data.extension_tag === extension_tag && event.data.direction === 'e2p') { | ||
window.addEventListener("message", event => { | ||
if ( | ||
event.data.extension_tag === extension_tag && | ||
event.data.direction === "e2p" | ||
) { | ||
switch (event.data.type) { | ||
case 'helo': | ||
case "helo": | ||
extension_found = true; | ||
break; | ||
case 'streamId': | ||
case "streamId": | ||
navigator.mediaDevices | ||
@@ -661,10 +680,10 @@ .getUserMedia({ | ||
mandatory: { | ||
chromeMediaSource: 'desktop', | ||
chromeMediaSource: "desktop", | ||
maxWidth: 1920, | ||
maxHeight: 1080, | ||
chromeMediaSourceId: event.data.streamId, | ||
}, | ||
}, | ||
chromeMediaSourceId: event.data.streamId | ||
} | ||
} | ||
}) | ||
.then((stream) => { | ||
.then(stream => { | ||
resolve(stream); | ||
@@ -679,10 +698,10 @@ }); | ||
send_ext({ | ||
type: 'ehlo', | ||
type: "ehlo" | ||
}); | ||
setTimeout(() => { | ||
if (!extension_found) { | ||
reject(new Error('please install extension')); | ||
reject(new Error("please install extension")); | ||
} else { | ||
send_ext({ | ||
type: 'chooseDesktopMedia', | ||
type: "chooseDesktopMedia" | ||
}); | ||
@@ -695,6 +714,6 @@ } | ||
video: { | ||
mediaSource: 'screen', | ||
}, | ||
mediaSource: "screen" | ||
} | ||
}) | ||
.then((stream) => { | ||
.then(stream => { | ||
resolve(stream); | ||
@@ -709,5 +728,5 @@ }); | ||
timestamp: new Date(timestamp * 1000), | ||
...rest, | ||
...rest | ||
}; | ||
} | ||
} |
{ | ||
"name": "@conectar/conectar-ws", | ||
"version": "0.0.24", | ||
"version": "0.0.25", | ||
"description": "Conectar WS connections", | ||
@@ -5,0 +5,0 @@ "main": "dist/wire.js", |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
763
35184
8
1