Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/use-load-balancer-apis #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions dist/broadcastChannel.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,10 @@ function averageResponseTime() {
}
;// CONCATENATED MODULE: external "oblivious-set"
const external_oblivious_set_namespaceObject = require("oblivious-set");
;// CONCATENATED MODULE: external "@toruslabs/constants"
const constants_namespaceObject = require("@toruslabs/constants");
;// CONCATENATED MODULE: ./src/options.js

function fillOptionsWithDefaults(originalOptions = {}) {
const options = JSON.parse(JSON.stringify(originalOptions));

Expand All @@ -303,7 +306,8 @@ function fillOptionsWithDefaults(originalOptions = {}) {

// server
if (!options.server) options.server = {};
if (!options.server.url) options.server.url = 'https://session.web3auth.io';
if (!options.server.api_url) options.server.api_url = constants_namespaceObject.SESSION_SERVER_API_URL;
if (!options.server.socket_url) options.server.socket_url = constants_namespaceObject.SESSION_SERVER_SOCKET_URL;
if (!options.server.removeTimeout) options.server.removeTimeout = 1000 * 60 * 5; // 5 minutes

// custom methods
Expand Down Expand Up @@ -819,7 +823,7 @@ function server_postMessage(channelState, messageJson) {
signature: (await (0,eccrypto_namespaceObject.sign)(channelEncPrivKey, (0,metadata_helpers_namespaceObject.keccak256)(Buffer.from(encData, 'utf8')))).toString('hex')
};
if (channelState.timeout) body.timeout = channelState.timeout;
return fetch(channelState.serverUrl + '/channel/set', {
return fetch(channelState.server.api_url + '/channel/set', {
method: 'POST',
body: JSON.stringify(body),
headers: {
Expand All @@ -829,11 +833,11 @@ function server_postMessage(channelState, messageJson) {
});
});
}
function getSocketInstance(serverUrl) {
function getSocketInstance(socketUrl) {
if (SOCKET_CONN_INSTANCE) {
return SOCKET_CONN_INSTANCE;
}
const SOCKET_CONN = (0,external_socket_io_client_namespaceObject.io)(serverUrl, {
const SOCKET_CONN = (0,external_socket_io_client_namespaceObject.io)(socketUrl, {
transports: ['websocket', 'polling'],
// use WebSocket first, if available
withCredentials: true,
Expand Down Expand Up @@ -866,8 +870,8 @@ function getSocketInstance(serverUrl) {
SOCKET_CONN_INSTANCE = SOCKET_CONN;
return SOCKET_CONN;
}
function setupSocketConnection(serverUrl, channelState, fn) {
const socketConn = getSocketInstance(serverUrl);
function setupSocketConnection(socketUrl, channelState, fn) {
const socketConn = getSocketInstance(socketUrl);
const key = server_storageKey(channelState.channelName);
const channelEncPrivKey = (0,metadata_helpers_namespaceObject.keccak256)(Buffer.from(key, 'utf8'));
const channelPubKey = (0,eccrypto_namespaceObject.getPublic)(channelEncPrivKey).toString('hex');
Expand Down Expand Up @@ -949,11 +953,14 @@ function server_create(channelName, options) {
uuid,
eMIs,
// emittedMessagesIds
serverUrl: options.server.url,
server: {
api_url: options.server.api_url,
socket_url: options.server.socket_url
},
time: microSeconds()
};
if (options.server.timeout) state.timeout = options.server.timeout;
setupSocketConnection(options.server.url, state, msgObj => {
setupSocketConnection(options.server.socket_url, state, msgObj => {
if (!state.messagesCallback) return; // no listener
if (msgObj.uuid === state.uuid) return; // own message
if (!msgObj.token || state.eMIs.has(msgObj.token)) return; // already emitted
Expand Down
21 changes: 13 additions & 8 deletions dist/broadcastChannel.esm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import loglevel from 'loglevel';
import { ObliviousSet } from 'oblivious-set';
import { SESSION_SERVER_API_URL, SESSION_SERVER_SOCKET_URL } from '@toruslabs/constants';
import { io } from 'socket.io-client';
import { getPublic, sign } from '@toruslabs/eccrypto';
import { keccak256, encryptData, decryptData } from '@toruslabs/metadata-helpers';
Expand Down Expand Up @@ -158,7 +159,8 @@ function fillOptionsWithDefaults(originalOptions = {}) {

// server
if (!options.server) options.server = {};
if (!options.server.url) options.server.url = 'https://session.web3auth.io';
if (!options.server.api_url) options.server.api_url = SESSION_SERVER_API_URL;
if (!options.server.socket_url) options.server.socket_url = SESSION_SERVER_SOCKET_URL;
if (!options.server.removeTimeout) options.server.removeTimeout = 1000 * 60 * 5; // 5 minutes

// custom methods
Expand Down Expand Up @@ -693,7 +695,7 @@ function postMessage$1(channelState, messageJson) {
signature: (await sign(channelEncPrivKey, keccak256(Buffer.from(encData, 'utf8')))).toString('hex')
};
if (channelState.timeout) body.timeout = channelState.timeout;
return fetch(channelState.serverUrl + '/channel/set', {
return fetch(channelState.server.api_url + '/channel/set', {
method: 'POST',
body: JSON.stringify(body),
headers: {
Expand All @@ -703,11 +705,11 @@ function postMessage$1(channelState, messageJson) {
});
});
}
function getSocketInstance(serverUrl) {
function getSocketInstance(socketUrl) {
if (SOCKET_CONN_INSTANCE) {
return SOCKET_CONN_INSTANCE;
}
const SOCKET_CONN = io(serverUrl, {
const SOCKET_CONN = io(socketUrl, {
transports: ['websocket', 'polling'],
// use WebSocket first, if available
withCredentials: true,
Expand Down Expand Up @@ -740,8 +742,8 @@ function getSocketInstance(serverUrl) {
SOCKET_CONN_INSTANCE = SOCKET_CONN;
return SOCKET_CONN;
}
function setupSocketConnection(serverUrl, channelState, fn) {
const socketConn = getSocketInstance(serverUrl);
function setupSocketConnection(socketUrl, channelState, fn) {
const socketConn = getSocketInstance(socketUrl);
const key = storageKey(channelState.channelName);
const channelEncPrivKey = keccak256(Buffer.from(key, 'utf8'));
const channelPubKey = getPublic(channelEncPrivKey).toString('hex');
Expand Down Expand Up @@ -820,11 +822,14 @@ function create$1(channelName, options) {
uuid,
eMIs,
// emittedMessagesIds
serverUrl: options.server.url,
server: {
api_url: options.server.api_url,
socket_url: options.server.socket_url
},
time: microSeconds$5()
};
if (options.server.timeout) state.timeout = options.server.timeout;
setupSocketConnection(options.server.url, state, msgObj => {
setupSocketConnection(options.server.socket_url, state, msgObj => {
if (!state.messagesCallback) return; // no listener
if (msgObj.uuid === state.uuid) return; // own message
if (!msgObj.token || state.eMIs.has(msgObj.token)) return; // already emitted
Expand Down
2 changes: 1 addition & 1 deletion dist/broadcastChannel.umd.min.js

Large diffs are not rendered by default.

17 changes: 10 additions & 7 deletions dist/esbrowser/methods/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function postMessage(channelState, messageJson) {
signature: _context.t2
};
if (channelState.timeout) body.timeout = channelState.timeout;
return _context.abrupt("return", fetch(channelState.serverUrl + '/channel/set', {
return _context.abrupt("return", fetch(channelState.server.api_url + '/channel/set', {
method: 'POST',
body: JSON.stringify(body),
headers: {
Expand All @@ -76,11 +76,11 @@ export function postMessage(channelState, messageJson) {
})));
});
}
export function getSocketInstance(serverUrl) {
export function getSocketInstance(socketUrl) {
if (SOCKET_CONN_INSTANCE) {
return SOCKET_CONN_INSTANCE;
}
var SOCKET_CONN = io(serverUrl, {
var SOCKET_CONN = io(socketUrl, {
transports: ['websocket', 'polling'],
// use WebSocket first, if available
withCredentials: true,
Expand Down Expand Up @@ -120,8 +120,8 @@ export function getSocketInstance(serverUrl) {
SOCKET_CONN_INSTANCE = SOCKET_CONN;
return SOCKET_CONN;
}
export function setupSocketConnection(serverUrl, channelState, fn) {
var socketConn = getSocketInstance(serverUrl);
export function setupSocketConnection(socketUrl, channelState, fn) {
var socketConn = getSocketInstance(socketUrl);
var key = storageKey(channelState.channelName);
var channelEncPrivKey = keccak256(Buffer.from(key, 'utf8'));
var channelPubKey = getPublic(channelEncPrivKey).toString('hex');
Expand Down Expand Up @@ -231,11 +231,14 @@ export function create(channelName, options) {
uuid: uuid,
eMIs: eMIs,
// emittedMessagesIds
serverUrl: options.server.url,
server: {
api_url: options.server.api_url,
socket_url: options.server.socket_url
},
time: micro()
};
if (options.server.timeout) state.timeout = options.server.timeout;
setupSocketConnection(options.server.url, state, function (msgObj) {
setupSocketConnection(options.server.socket_url, state, function (msgObj) {
if (!state.messagesCallback) return; // no listener
if (msgObj.uuid === state.uuid) return; // own message
if (!msgObj.token || state.eMIs.has(msgObj.token)) return; // already emitted
Expand Down
4 changes: 3 additions & 1 deletion dist/esbrowser/options.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SESSION_SERVER_API_URL, SESSION_SERVER_SOCKET_URL } from '@toruslabs/constants';
export function fillOptionsWithDefaults() {
var originalOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = JSON.parse(JSON.stringify(originalOptions));
Expand All @@ -19,7 +20,8 @@ export function fillOptionsWithDefaults() {

// server
if (!options.server) options.server = {};
if (!options.server.url) options.server.url = 'https://session.web3auth.io';
if (!options.server.api_url) options.server.api_url = SESSION_SERVER_API_URL;
if (!options.server.socket_url) options.server.socket_url = SESSION_SERVER_SOCKET_URL;
if (!options.server.removeTimeout) options.server.removeTimeout = 1000 * 60 * 5; // 5 minutes

// custom methods
Expand Down
17 changes: 10 additions & 7 deletions dist/esnode/methods/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function postMessage(channelState, messageJson) {
signature: _context.t2
};
if (channelState.timeout) body.timeout = channelState.timeout;
return _context.abrupt("return", fetch(channelState.serverUrl + '/channel/set', {
return _context.abrupt("return", fetch(channelState.server.api_url + '/channel/set', {
method: 'POST',
body: JSON.stringify(body),
headers: {
Expand All @@ -76,11 +76,11 @@ export function postMessage(channelState, messageJson) {
})));
});
}
export function getSocketInstance(serverUrl) {
export function getSocketInstance(socketUrl) {
if (SOCKET_CONN_INSTANCE) {
return SOCKET_CONN_INSTANCE;
}
var SOCKET_CONN = io(serverUrl, {
var SOCKET_CONN = io(socketUrl, {
transports: ['websocket', 'polling'],
// use WebSocket first, if available
withCredentials: true,
Expand Down Expand Up @@ -120,8 +120,8 @@ export function getSocketInstance(serverUrl) {
SOCKET_CONN_INSTANCE = SOCKET_CONN;
return SOCKET_CONN;
}
export function setupSocketConnection(serverUrl, channelState, fn) {
var socketConn = getSocketInstance(serverUrl);
export function setupSocketConnection(socketUrl, channelState, fn) {
var socketConn = getSocketInstance(socketUrl);
var key = storageKey(channelState.channelName);
var channelEncPrivKey = keccak256(Buffer.from(key, 'utf8'));
var channelPubKey = getPublic(channelEncPrivKey).toString('hex');
Expand Down Expand Up @@ -231,11 +231,14 @@ export function create(channelName, options) {
uuid: uuid,
eMIs: eMIs,
// emittedMessagesIds
serverUrl: options.server.url,
server: {
api_url: options.server.api_url,
socket_url: options.server.socket_url
},
time: micro()
};
if (options.server.timeout) state.timeout = options.server.timeout;
setupSocketConnection(options.server.url, state, function (msgObj) {
setupSocketConnection(options.server.socket_url, state, function (msgObj) {
if (!state.messagesCallback) return; // no listener
if (msgObj.uuid === state.uuid) return; // own message
if (!msgObj.token || state.eMIs.has(msgObj.token)) return; // already emitted
Expand Down
4 changes: 3 additions & 1 deletion dist/esnode/options.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SESSION_SERVER_API_URL, SESSION_SERVER_SOCKET_URL } from '@toruslabs/constants';
export function fillOptionsWithDefaults() {
var originalOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = JSON.parse(JSON.stringify(originalOptions));
Expand All @@ -19,7 +20,8 @@ export function fillOptionsWithDefaults() {

// server
if (!options.server) options.server = {};
if (!options.server.url) options.server.url = 'https://session.web3auth.io';
if (!options.server.api_url) options.server.api_url = SESSION_SERVER_API_URL;
if (!options.server.socket_url) options.server.socket_url = SESSION_SERVER_SOCKET_URL;
if (!options.server.removeTimeout) options.server.removeTimeout = 1000 * 60 * 5; // 5 minutes

// custom methods
Expand Down
17 changes: 10 additions & 7 deletions dist/lib.cjs/methods/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function postMessage(channelState, messageJson) {
signature: (await eccrypto.sign(channelEncPrivKey, metadataHelpers.keccak256(Buffer.from(encData, 'utf8')))).toString('hex')
};
if (channelState.timeout) body.timeout = channelState.timeout;
return fetch(channelState.serverUrl + '/channel/set', {
return fetch(channelState.server.api_url + '/channel/set', {
method: 'POST',
body: JSON.stringify(body),
headers: {
Expand All @@ -58,11 +58,11 @@ function postMessage(channelState, messageJson) {
});
});
}
function getSocketInstance(serverUrl) {
function getSocketInstance(socketUrl) {
if (SOCKET_CONN_INSTANCE) {
return SOCKET_CONN_INSTANCE;
}
const SOCKET_CONN = socket_ioClient.io(serverUrl, {
const SOCKET_CONN = socket_ioClient.io(socketUrl, {
transports: ['websocket', 'polling'],
// use WebSocket first, if available
withCredentials: true,
Expand Down Expand Up @@ -95,8 +95,8 @@ function getSocketInstance(serverUrl) {
SOCKET_CONN_INSTANCE = SOCKET_CONN;
return SOCKET_CONN;
}
function setupSocketConnection(serverUrl, channelState, fn) {
const socketConn = getSocketInstance(serverUrl);
function setupSocketConnection(socketUrl, channelState, fn) {
const socketConn = getSocketInstance(socketUrl);
const key = storageKey(channelState.channelName);
const channelEncPrivKey = metadataHelpers.keccak256(Buffer.from(key, 'utf8'));
const channelPubKey = eccrypto.getPublic(channelEncPrivKey).toString('hex');
Expand Down Expand Up @@ -175,11 +175,14 @@ function create(channelName, options$1) {
uuid,
eMIs,
// emittedMessagesIds
serverUrl: options$1.server.url,
server: {
api_url: options$1.server.api_url,
socket_url: options$1.server.socket_url
},
time: util.microSeconds()
};
if (options$1.server.timeout) state.timeout = options$1.server.timeout;
setupSocketConnection(options$1.server.url, state, msgObj => {
setupSocketConnection(options$1.server.socket_url, state, msgObj => {
if (!state.messagesCallback) return; // no listener
if (msgObj.uuid === state.uuid) return; // own message
if (!msgObj.token || state.eMIs.has(msgObj.token)) return; // already emitted
Expand Down
5 changes: 4 additions & 1 deletion dist/lib.cjs/options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var constants = require('@toruslabs/constants');

function fillOptionsWithDefaults(originalOptions = {}) {
const options = JSON.parse(JSON.stringify(originalOptions));

Expand All @@ -20,7 +22,8 @@ function fillOptionsWithDefaults(originalOptions = {}) {

// server
if (!options.server) options.server = {};
if (!options.server.url) options.server.url = 'https://session.web3auth.io';
if (!options.server.api_url) options.server.api_url = constants.SESSION_SERVER_API_URL;
if (!options.server.socket_url) options.server.socket_url = constants.SESSION_SERVER_SOCKET_URL;
if (!options.server.removeTimeout) options.server.removeTimeout = 1000 * 60 * 5; // 5 minutes

// custom methods
Expand Down
17 changes: 10 additions & 7 deletions dist/lib.esm/methods/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function postMessage(channelState, messageJson) {
signature: (await sign(channelEncPrivKey, keccak256(Buffer.from(encData, 'utf8')))).toString('hex')
};
if (channelState.timeout) body.timeout = channelState.timeout;
return fetch(channelState.serverUrl + '/channel/set', {
return fetch(channelState.server.api_url + '/channel/set', {
method: 'POST',
body: JSON.stringify(body),
headers: {
Expand All @@ -56,11 +56,11 @@ function postMessage(channelState, messageJson) {
});
});
}
function getSocketInstance(serverUrl) {
function getSocketInstance(socketUrl) {
if (SOCKET_CONN_INSTANCE) {
return SOCKET_CONN_INSTANCE;
}
const SOCKET_CONN = io(serverUrl, {
const SOCKET_CONN = io(socketUrl, {
transports: ['websocket', 'polling'],
// use WebSocket first, if available
withCredentials: true,
Expand Down Expand Up @@ -93,8 +93,8 @@ function getSocketInstance(serverUrl) {
SOCKET_CONN_INSTANCE = SOCKET_CONN;
return SOCKET_CONN;
}
function setupSocketConnection(serverUrl, channelState, fn) {
const socketConn = getSocketInstance(serverUrl);
function setupSocketConnection(socketUrl, channelState, fn) {
const socketConn = getSocketInstance(socketUrl);
const key = storageKey(channelState.channelName);
const channelEncPrivKey = keccak256(Buffer.from(key, 'utf8'));
const channelPubKey = getPublic(channelEncPrivKey).toString('hex');
Expand Down Expand Up @@ -173,11 +173,14 @@ function create(channelName, options) {
uuid,
eMIs,
// emittedMessagesIds
serverUrl: options.server.url,
server: {
api_url: options.server.api_url,
socket_url: options.server.socket_url
},
time: microSeconds$1()
};
if (options.server.timeout) state.timeout = options.server.timeout;
setupSocketConnection(options.server.url, state, msgObj => {
setupSocketConnection(options.server.socket_url, state, msgObj => {
if (!state.messagesCallback) return; // no listener
if (msgObj.uuid === state.uuid) return; // own message
if (!msgObj.token || state.eMIs.has(msgObj.token)) return; // already emitted
Expand Down
Loading
Loading