@bloks/link-session-manager
Advanced tools
Comparing version 0.2.652 to 0.2.653
/** | ||
* proton-link-session-manager v0.2.652 | ||
* proton-link-session-manager v0.2.653 | ||
* https://github.com/greymass/proton-link-session-manager | ||
@@ -37,3 +37,2 @@ * | ||
import { Struct, PublicKey, UInt64, Bytes, UInt32, Name, TimePointSec, Checksum256, Checksum256Type, NameType, PublicKeyType, PrivateKeyType, PrivateKey } from '@greymass/eosio'; | ||
import WebSocket from 'isomorphic-ws'; | ||
import { SigningRequestEncodingOptions } from '@bloks/signing-request'; | ||
@@ -96,2 +95,3 @@ | ||
storage?: ProtonLinkSessionManagerStorage; | ||
WebSocket: any; | ||
} | ||
@@ -106,2 +106,3 @@ interface ProtonLinkSessionManagerEventHander { | ||
storage: ProtonLinkSessionManagerStorage; | ||
WebSocket: any; | ||
private socket; | ||
@@ -115,3 +116,3 @@ constructor(options: ProtonLinkSessionManagerOptions); | ||
updateLastUsed(publicKey: PublicKey): void; | ||
connect(): Promise<WebSocket>; | ||
connect(): Promise<any>; | ||
disconnect(): Promise<void>; | ||
@@ -118,0 +119,0 @@ handleRequest(encoded: Bytes): string; |
207
lib/index.js
/** | ||
* proton-link-session-manager v0.2.652 | ||
* proton-link-session-manager v0.2.653 | ||
* https://github.com/greymass/proton-link-session-manager | ||
@@ -106,204 +106,2 @@ * | ||
var RobustWebSocket = /** @class */ (function () { | ||
function RobustWebSocket(url, opts) { | ||
var e_1, _a; | ||
var _this = this; | ||
if (opts === void 0) { opts = {}; } | ||
this.attempts = 0; | ||
this.reconnects = -1; | ||
this.reconnectWhenOnlineAgain = false; | ||
this.explicitlyClosed = false; | ||
this.binaryType = 'arraybuffer'; | ||
this.pendingReconnect = undefined; | ||
this.connectTimeout = undefined; | ||
this.listeners = { | ||
open: [ | ||
function (event) { | ||
if (_this.connectTimeout) { | ||
clearTimeout(_this.connectTimeout); | ||
_this.connectTimeout = undefined; | ||
} | ||
event.reconnects = ++_this.reconnects; | ||
event.attempts = _this.attempts; | ||
_this.attempts = 0; | ||
_this.reconnectWhenOnlineAgain = false; | ||
}, | ||
], | ||
close: [function (event) { return _this.reconnect(event); }], | ||
}; | ||
this.opts = { | ||
// the time to wait before a successful connection | ||
// before the attempt is considered to have timed out | ||
timeout: 4000, | ||
// Given a CloseEvent or OnlineEvent and the RobustWebSocket state, | ||
// should a reconnect be attempted? Return the number of milliseconds to wait | ||
// to reconnect (or null or undefined to not), rather than true or false | ||
shouldReconnect: function (event, ws) { | ||
if ([1000, 4001].includes(event.code)) { | ||
return undefined; | ||
} | ||
else if (event.type === 'online') { | ||
return 0; | ||
} | ||
else { | ||
return Math.pow(1.5, ws.attempts) * 300; | ||
} | ||
}, | ||
// Create and connect the WebSocket when the instance is instantiated. | ||
// Defaults to true to match standard WebSocket behavior | ||
automaticOpen: true, | ||
handle1000: false, | ||
}; | ||
this.url = url; | ||
this.opts = Object.assign({}, this.opts, opts); | ||
if (typeof this.opts.timeout !== 'number') { | ||
throw new Error('timeout must be the number of milliseconds to timeout a connection attempt'); | ||
} | ||
if (typeof this.opts.shouldReconnect !== 'function') { | ||
throw new Error('shouldReconnect must be a function that returns the number of milliseconds to wait for a reconnect attempt, or null or undefined to not reconnect.'); | ||
} | ||
if (this.opts.automaticOpen) { | ||
this.newWebSocket(this.url); | ||
var _loop_1 = function (stdEvent) { | ||
var onEvent = function (event) { | ||
_this.dispatchEvent(event); | ||
var cb = _this['on' + stdEvent]; | ||
if (typeof cb === 'function') { | ||
return cb(event); | ||
} | ||
}; | ||
if (this_1.realWs) { | ||
this_1.realWs.addEventListener(stdEvent, onEvent); | ||
} | ||
}; | ||
var this_1 = this; | ||
try { | ||
// Only add once, e.g. onping for ping | ||
for (var _b = tslib.__values(['open', 'close', 'message', 'error', 'ping']), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var stdEvent = _c.value; | ||
_loop_1(stdEvent); | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
} | ||
} | ||
RobustWebSocket.prototype.newWebSocket = function (url) { | ||
var _this = this; | ||
console.log('Creating new websocket to', url); | ||
this.pendingReconnect = undefined; | ||
this.realWs = new WebSocket(url); | ||
this.realWs.binaryType = this.binaryType; | ||
this.attempts++; | ||
this.dispatchEvent({ | ||
type: 'connecting', | ||
attempts: this.attempts, | ||
reconnects: this.reconnects, | ||
}); | ||
this.connectTimeout = setTimeout(function () { | ||
_this.connectTimeout = undefined; | ||
_this.dispatchEvent({ | ||
type: 'timeout', | ||
attempts: _this.attempts, | ||
reconnects: _this.reconnects, | ||
}); | ||
}, this.opts.timeout); | ||
}; | ||
RobustWebSocket.prototype.clearPendingReconnectIfNeeded = function () { | ||
if (this.pendingReconnect) { | ||
clearTimeout(this.pendingReconnect); | ||
this.pendingReconnect = undefined; | ||
} | ||
}; | ||
RobustWebSocket.prototype.send = function (data) { | ||
if (this.realWs) { | ||
return this.realWs.send(data); | ||
} | ||
}; | ||
RobustWebSocket.prototype.close = function (code, reason) { | ||
console.log('Closing Websocket'); | ||
if (typeof code !== 'number') { | ||
reason = code; | ||
code = 1000; | ||
} | ||
this.clearPendingReconnectIfNeeded(); | ||
this.reconnectWhenOnlineAgain = false; | ||
this.explicitlyClosed = true; | ||
if (this.realWs) { | ||
return this.realWs.close(code, reason); | ||
} | ||
}; | ||
RobustWebSocket.prototype.open = function () { | ||
console.log('Opening Websocket'); | ||
if (this.realWs && | ||
this.realWs.readyState !== WebSocket.OPEN && | ||
this.realWs.readyState !== WebSocket.CONNECTING) { | ||
this.clearPendingReconnectIfNeeded(); | ||
this.reconnectWhenOnlineAgain = false; | ||
this.explicitlyClosed = false; | ||
this.newWebSocket(this.url); | ||
} | ||
}; | ||
RobustWebSocket.prototype.reconnect = function (event) { | ||
var _this = this; | ||
console.log('Reconnecting Websocket'); | ||
if ((!this.opts.handle1000 && event.code === 1000) || this.explicitlyClosed) { | ||
this.attempts = 0; | ||
return; | ||
} | ||
if (navigator.onLine === false) { | ||
this.reconnectWhenOnlineAgain = true; | ||
return; | ||
} | ||
var delay = this.opts.shouldReconnect(event, this); | ||
if (typeof delay === 'number') { | ||
this.pendingReconnect = setTimeout(function () { return _this.newWebSocket(_this.url); }, delay); | ||
} | ||
}; | ||
// Taken from MDN https://developer.mozilla.org/en-US/docs/Web/API/EventTarget | ||
RobustWebSocket.prototype.addEventListener = function (type, callback) { | ||
if (!this.listeners[type]) { | ||
this.listeners[type] = []; | ||
} | ||
this.listeners[type].push(callback); | ||
}; | ||
RobustWebSocket.prototype.removeEventListener = function (type, callback) { | ||
if (!this.listeners[type]) { | ||
return; | ||
} | ||
var stack = this.listeners[type]; | ||
for (var i = 0, l = stack.length; i < l; i++) { | ||
if (stack[i] === callback) { | ||
stack.splice(i, 1); | ||
return; | ||
} | ||
} | ||
}; | ||
RobustWebSocket.prototype.dispatchEvent = function (event) { | ||
var e_2, _a; | ||
if (!this.listeners[event.type]) { | ||
return; | ||
} | ||
try { | ||
for (var _b = tslib.__values(this.listeners[event.type]), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var listener = _c.value; | ||
listener(event); | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
}; | ||
return RobustWebSocket; | ||
}()); | ||
var ProtonLinkSessionManagerSession = /** @class */ (function () { | ||
@@ -410,2 +208,3 @@ function ProtonLinkSessionManagerSession(network, actor, permission, publicKey, name, created, lastUsed) { | ||
this.handler = options.handler; | ||
this.WebSocket = options.WebSocket; | ||
if (options && options.storage) { | ||
@@ -450,3 +249,3 @@ this.storage = options.storage; | ||
var linkUrl = "wss://" + _this.storage.linkUrl + "/" + _this.storage.linkId; | ||
var ws = new RobustWebSocket(linkUrl); | ||
var ws = new _this.WebSocket(linkUrl); | ||
var onSocketEvent = function (type, event) { | ||
@@ -453,0 +252,0 @@ try { |
/** | ||
* proton-link-session-manager v0.2.652 | ||
* proton-link-session-manager v0.2.653 | ||
* https://github.com/greymass/proton-link-session-manager | ||
@@ -83,173 +83,2 @@ * | ||
class RobustWebSocket { | ||
constructor(url, opts = {}) { | ||
this.attempts = 0; | ||
this.reconnects = -1; | ||
this.reconnectWhenOnlineAgain = false; | ||
this.explicitlyClosed = false; | ||
this.binaryType = 'arraybuffer'; | ||
this.pendingReconnect = undefined; | ||
this.connectTimeout = undefined; | ||
this.listeners = { | ||
open: [ | ||
(event) => { | ||
if (this.connectTimeout) { | ||
clearTimeout(this.connectTimeout); | ||
this.connectTimeout = undefined; | ||
} | ||
event.reconnects = ++this.reconnects; | ||
event.attempts = this.attempts; | ||
this.attempts = 0; | ||
this.reconnectWhenOnlineAgain = false; | ||
}, | ||
], | ||
close: [(event) => this.reconnect(event)], | ||
}; | ||
this.opts = { | ||
// the time to wait before a successful connection | ||
// before the attempt is considered to have timed out | ||
timeout: 4000, | ||
// Given a CloseEvent or OnlineEvent and the RobustWebSocket state, | ||
// should a reconnect be attempted? Return the number of milliseconds to wait | ||
// to reconnect (or null or undefined to not), rather than true or false | ||
shouldReconnect: function (event, ws) { | ||
if ([1000, 4001].includes(event.code)) { | ||
return undefined; | ||
} | ||
else if (event.type === 'online') { | ||
return 0; | ||
} | ||
else { | ||
return Math.pow(1.5, ws.attempts) * 300; | ||
} | ||
}, | ||
// Create and connect the WebSocket when the instance is instantiated. | ||
// Defaults to true to match standard WebSocket behavior | ||
automaticOpen: true, | ||
handle1000: false, | ||
}; | ||
this.url = url; | ||
this.opts = Object.assign({}, this.opts, opts); | ||
if (typeof this.opts.timeout !== 'number') { | ||
throw new Error('timeout must be the number of milliseconds to timeout a connection attempt'); | ||
} | ||
if (typeof this.opts.shouldReconnect !== 'function') { | ||
throw new Error('shouldReconnect must be a function that returns the number of milliseconds to wait for a reconnect attempt, or null or undefined to not reconnect.'); | ||
} | ||
if (this.opts.automaticOpen) { | ||
this.newWebSocket(this.url); | ||
// Only add once, e.g. onping for ping | ||
for (const stdEvent of ['open', 'close', 'message', 'error', 'ping']) { | ||
const onEvent = (event) => { | ||
this.dispatchEvent(event); | ||
const cb = this['on' + stdEvent]; | ||
if (typeof cb === 'function') { | ||
return cb(event); | ||
} | ||
}; | ||
if (this.realWs) { | ||
this.realWs.addEventListener(stdEvent, onEvent); | ||
} | ||
} | ||
} | ||
} | ||
newWebSocket(url) { | ||
console.log('Creating new websocket to', url); | ||
this.pendingReconnect = undefined; | ||
this.realWs = new WebSocket(url); | ||
this.realWs.binaryType = this.binaryType; | ||
this.attempts++; | ||
this.dispatchEvent({ | ||
type: 'connecting', | ||
attempts: this.attempts, | ||
reconnects: this.reconnects, | ||
}); | ||
this.connectTimeout = setTimeout(() => { | ||
this.connectTimeout = undefined; | ||
this.dispatchEvent({ | ||
type: 'timeout', | ||
attempts: this.attempts, | ||
reconnects: this.reconnects, | ||
}); | ||
}, this.opts.timeout); | ||
} | ||
clearPendingReconnectIfNeeded() { | ||
if (this.pendingReconnect) { | ||
clearTimeout(this.pendingReconnect); | ||
this.pendingReconnect = undefined; | ||
} | ||
} | ||
send(data) { | ||
if (this.realWs) { | ||
return this.realWs.send(data); | ||
} | ||
} | ||
close(code, reason) { | ||
console.log('Closing Websocket'); | ||
if (typeof code !== 'number') { | ||
reason = code; | ||
code = 1000; | ||
} | ||
this.clearPendingReconnectIfNeeded(); | ||
this.reconnectWhenOnlineAgain = false; | ||
this.explicitlyClosed = true; | ||
if (this.realWs) { | ||
return this.realWs.close(code, reason); | ||
} | ||
} | ||
open() { | ||
console.log('Opening Websocket'); | ||
if (this.realWs && | ||
this.realWs.readyState !== WebSocket.OPEN && | ||
this.realWs.readyState !== WebSocket.CONNECTING) { | ||
this.clearPendingReconnectIfNeeded(); | ||
this.reconnectWhenOnlineAgain = false; | ||
this.explicitlyClosed = false; | ||
this.newWebSocket(this.url); | ||
} | ||
} | ||
reconnect(event) { | ||
console.log('Reconnecting Websocket'); | ||
if ((!this.opts.handle1000 && event.code === 1000) || this.explicitlyClosed) { | ||
this.attempts = 0; | ||
return; | ||
} | ||
if (navigator.onLine === false) { | ||
this.reconnectWhenOnlineAgain = true; | ||
return; | ||
} | ||
const delay = this.opts.shouldReconnect(event, this); | ||
if (typeof delay === 'number') { | ||
this.pendingReconnect = setTimeout(() => this.newWebSocket(this.url), delay); | ||
} | ||
} | ||
// Taken from MDN https://developer.mozilla.org/en-US/docs/Web/API/EventTarget | ||
addEventListener(type, callback) { | ||
if (!this.listeners[type]) { | ||
this.listeners[type] = []; | ||
} | ||
this.listeners[type].push(callback); | ||
} | ||
removeEventListener(type, callback) { | ||
if (!this.listeners[type]) { | ||
return; | ||
} | ||
const stack = this.listeners[type]; | ||
for (let i = 0, l = stack.length; i < l; i++) { | ||
if (stack[i] === callback) { | ||
stack.splice(i, 1); | ||
return; | ||
} | ||
} | ||
} | ||
dispatchEvent(event) { | ||
if (!this.listeners[event.type]) { | ||
return; | ||
} | ||
for (const listener of this.listeners[event.type]) { | ||
listener(event); | ||
} | ||
} | ||
} | ||
class ProtonLinkSessionManagerSession { | ||
@@ -352,2 +181,3 @@ constructor(network, actor, permission, publicKey, name, created, lastUsed) { | ||
this.handler = options.handler; | ||
this.WebSocket = options.WebSocket; | ||
if (options && options.storage) { | ||
@@ -391,3 +221,3 @@ this.storage = options.storage; | ||
const linkUrl = `wss://${this.storage.linkUrl}/${this.storage.linkId}`; | ||
const ws = new RobustWebSocket(linkUrl); | ||
const ws = new this.WebSocket(linkUrl); | ||
const onSocketEvent = (type, event) => { | ||
@@ -394,0 +224,0 @@ try { |
{ | ||
"name": "@bloks/link-session-manager", | ||
"description": "Session management for signature providers when receiving requests using the Anchor Link protocol", | ||
"version": "0.2.652", | ||
"version": "0.2.653", | ||
"homepage": "https://github.com/greymass/proton-link-session-manager", | ||
@@ -6,0 +6,0 @@ "license": "BSD-3-Clause", |
@@ -1,3 +0,1 @@ | ||
import WebSocket from 'isomorphic-ws' | ||
import RobustWebSocket from './websocket' | ||
import { | ||
@@ -24,2 +22,3 @@ Bytes, | ||
storage?: ProtonLinkSessionManagerStorage | ||
WebSocket: any | ||
} | ||
@@ -36,7 +35,8 @@ | ||
public storage: ProtonLinkSessionManagerStorage | ||
public WebSocket: any | ||
private socket: any | ||
private socket: WebSocket | ||
constructor(options: ProtonLinkSessionManagerOptions) { | ||
this.handler = options.handler | ||
this.WebSocket = options.WebSocket | ||
if (options && options.storage) { | ||
@@ -91,6 +91,6 @@ this.storage = options.storage | ||
connect(): Promise<WebSocket> { | ||
connect(): Promise<any> { | ||
return new Promise((resolve, reject) => { | ||
const linkUrl = `wss://${this.storage.linkUrl}/${this.storage.linkId}` | ||
const ws = new RobustWebSocket(linkUrl) | ||
const ws = new this.WebSocket(linkUrl) | ||
@@ -97,0 +97,0 @@ const onSocketEvent = (type: string, event: any) => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
69592
1227