-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from radarlabs/latency-ws
latency calcs with web sockets
- Loading branch information
Showing
7 changed files
with
137 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const base64Encode = (str: string): string => | ||
btoa(str).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); | ||
|
||
export const signJWT = async (payload: object, key: string): Promise<string> => { | ||
const encoder = new TextEncoder(); | ||
|
||
const encodedHeader = base64Encode(JSON.stringify({ | ||
alg: 'HS256', | ||
typ: 'JWT', | ||
})); | ||
const encodedPayload = base64Encode(JSON.stringify(payload)); | ||
|
||
const keyData = encoder.encode(key); | ||
const messageData = encoder.encode(`${encodedHeader}.${encodedPayload}`); | ||
|
||
const cryptoKey = await crypto.subtle.importKey( | ||
'raw', | ||
keyData, | ||
{ name: 'HMAC', hash: { name: 'SHA-256' } }, | ||
false, | ||
['sign'] | ||
); | ||
|
||
const signatureArrayBuffer = await crypto.subtle.sign('HMAC', cryptoKey, messageData); | ||
const signature = base64Encode(String.fromCharCode(...Array.from(new Uint8Array(signatureArrayBuffer)))); | ||
|
||
return `${encodedHeader}.${encodedPayload}.${signature}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import Logger from '../logger'; | ||
|
||
export const ping = (host: string): Promise<any> => { | ||
return new Promise((resolve) => { | ||
const socket = new WebSocket(host); | ||
|
||
let pings = 0; | ||
const latencies: number[] = []; | ||
let pingInterval: any; | ||
let timeoutInterval: any; | ||
|
||
const ping = () => { | ||
pings++; | ||
|
||
const start = Date.now(); | ||
socket.send('ping'); | ||
|
||
socket.onmessage = (event) => { | ||
if (event.data === 'pong') { | ||
const latency = Date.now() - start; | ||
latencies.push(latency); | ||
|
||
if (pings >= 3) { | ||
clearInterval(pingInterval); | ||
clearInterval(timeoutInterval); | ||
const median = latencies.sort((a, b) => a - b)[1]; | ||
socket.close(); | ||
resolve(median); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
const timeout = () => { | ||
Logger.warn('Socket timeout'); | ||
clearInterval(pingInterval); | ||
clearInterval(timeoutInterval); | ||
socket.close(); | ||
resolve(-1); | ||
} | ||
|
||
socket.onerror = (err) => { | ||
Logger.warn('Error opening socket'); | ||
socket.close(); | ||
resolve(-1); | ||
}; | ||
|
||
socket.onopen = () => { | ||
ping(); | ||
pingInterval = setInterval(ping, 1000); | ||
timeoutInterval = setInterval(timeout, 10000); | ||
}; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export default '4.1.16'; | ||
export default '4.1.17'; |