Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bloks/link-session-manager

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bloks/link-session-manager - npm Package Compare versions

Comparing version 0.2.653 to 0.2.654

8

lib/index.d.ts
/**
* proton-link-session-manager v0.2.653
* proton-link-session-manager v0.2.654
* https://github.com/greymass/proton-link-session-manager

@@ -94,3 +94,2 @@ *

storage?: ProtonLinkSessionManagerStorage;
WebSocket: any;
}

@@ -105,4 +104,3 @@ interface ProtonLinkSessionManagerEventHander {

storage: ProtonLinkSessionManagerStorage;
WebSocket: any;
private socket;
private socket?;
constructor(options: ProtonLinkSessionManagerOptions);

@@ -115,3 +113,3 @@ addSession(session: ProtonLinkSessionManagerSession): void;

updateLastUsed(publicKey: PublicKey): void;
connect(): Promise<any>;
connect(): Promise<unknown>;
disconnect(): Promise<void>;

@@ -118,0 +116,0 @@ handleRequest(encoded: Bytes): string;

/**
* proton-link-session-manager v0.2.653
* proton-link-session-manager v0.2.654
* https://github.com/greymass/proton-link-session-manager

@@ -106,2 +106,209 @@ *

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.enableAck = 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 += 1;
event.attempts = _this.attempts;
_this.attempts = 0;
_this.reconnectWhenOnlineAgain = false;
// Send ACK
if (_this.enableAck) {
var header = Buffer.from([0x42, 0x42, 0x00]);
_this.send(header);
}
},
],
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;
}
if (event.type === 'online') {
return 0;
}
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 = tslib.__assign(tslib.__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 per event e.g. onping for ping
var onEvent = function (stdEvent) { return function (event) {
// ACK stripping
if (_this.enableAck && event.data && event.data.byteLength) {
var array = new Uint8Array(event.data);
if (array[0] === 0x42 && array[1] === 0x42 && array[2] === 0x01) {
_this.send(Buffer.from([0x42, 0x42, 0x02, array[3]]));
event.data = event.data.slice(4);
}
}
_this.dispatchEvent(event);
var cb = _this["on" + stdEvent];
if (typeof cb === 'function') {
return cb(event);
}
}; };
try {
for (var _b = tslib.__values(['open', 'close', 'message', 'error', 'ping']), _c = _b.next(); !_c.done; _c = _b.next()) {
var stdEvent = _c.value;
if (this.realWs) {
this.realWs.addEventListener(stdEvent, onEvent(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;
this.pendingReconnect = undefined;
this.realWs = new WebSocket(url);
this.realWs.binaryType = this.binaryType;
this.attempts += 1;
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) {
if (typeof code !== 'number') {
reason = code;
code = 1000;
}
this.clearPendingReconnectIfNeeded();
this.reconnectWhenOnlineAgain = false;
this.explicitlyClosed = true;
if (this.realWs) {
this.realWs.close(code, reason);
return;
}
};
RobustWebSocket.prototype.open = function () {
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;
if ((!this.opts.handle1000 && event.code === 1000) || this.explicitlyClosed) {
this.attempts = 0;
return;
}
if (navigator && 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 += 1) {
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 () {

@@ -208,3 +415,2 @@ function ProtonLinkSessionManagerSession(network, actor, permission, publicKey, name, created, lastUsed) {

this.handler = options.handler;
this.WebSocket = options.WebSocket;
if (options && options.storage) {

@@ -249,3 +455,3 @@ this.storage = options.storage;

var linkUrl = "wss://" + _this.storage.linkUrl + "/" + _this.storage.linkId;
var ws = new _this.WebSocket(linkUrl);
var ws = new RobustWebSocket(linkUrl);
var onSocketEvent = function (type, event) {

@@ -264,3 +470,2 @@ try {

onSocketEvent('onopen', event);
resolve(_this.socket);
});

@@ -278,4 +483,5 @@ ws.addEventListener('message', function (event) {

ws.addEventListener('ping', function (event) {
var _a;
onSocketEvent('onping', event);
_this.socket.send('pong');
(_a = _this.socket) === null || _a === void 0 ? void 0 : _a.send('pong');
});

@@ -288,8 +494,9 @@ _this.socket = ws;

ProtonLinkSessionManager.prototype.disconnect = function () {
var _a;
return tslib.__awaiter(this, void 0, void 0, function () {
return tslib.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.socket.close(1000)];
return tslib.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.close(1000))];
case 1:
_a.sent();
_b.sent();
return [2 /*return*/];

@@ -296,0 +503,0 @@ }

/**
* proton-link-session-manager v0.2.653
* proton-link-session-manager v0.2.654
* https://github.com/greymass/proton-link-session-manager

@@ -83,2 +83,182 @@ *

class RobustWebSocket {
constructor(url, opts = {}) {
this.attempts = 0;
this.reconnects = -1;
this.reconnectWhenOnlineAgain = false;
this.enableAck = 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 += 1;
event.attempts = this.attempts;
this.attempts = 0;
this.reconnectWhenOnlineAgain = false;
// Send ACK
if (this.enableAck) {
const header = Buffer.from([0x42, 0x42, 0x00]);
this.send(header);
}
},
],
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(event, ws) {
if ([1000, 4001].includes(event.code)) {
return undefined;
}
if (event.type === 'online') {
return 0;
}
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 = { ...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 per event e.g. onping for ping
const onEvent = (stdEvent) => (event) => {
// ACK stripping
if (this.enableAck && event.data && event.data.byteLength) {
const array = new Uint8Array(event.data);
if (array[0] === 0x42 && array[1] === 0x42 && array[2] === 0x01) {
this.send(Buffer.from([0x42, 0x42, 0x02, array[3]]));
event.data = event.data.slice(4);
}
}
this.dispatchEvent(event);
const cb = this[`on${stdEvent}`];
if (typeof cb === 'function') {
return cb(event);
}
};
for (const stdEvent of ['open', 'close', 'message', 'error', 'ping']) {
if (this.realWs) {
this.realWs.addEventListener(stdEvent, onEvent(stdEvent));
}
}
}
}
newWebSocket(url) {
this.pendingReconnect = undefined;
this.realWs = new WebSocket(url);
this.realWs.binaryType = this.binaryType;
this.attempts += 1;
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) {
if (typeof code !== 'number') {
reason = code;
code = 1000;
}
this.clearPendingReconnectIfNeeded();
this.reconnectWhenOnlineAgain = false;
this.explicitlyClosed = true;
if (this.realWs) {
this.realWs.close(code, reason);
return;
}
}
open() {
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) {
if ((!this.opts.handle1000 && event.code === 1000) || this.explicitlyClosed) {
this.attempts = 0;
return;
}
if (navigator && 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 += 1) {
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 {

@@ -181,3 +361,2 @@ constructor(network, actor, permission, publicKey, name, created, lastUsed) {

this.handler = options.handler;
this.WebSocket = options.WebSocket;
if (options && options.storage) {

@@ -221,3 +400,3 @@ this.storage = options.storage;

const linkUrl = `wss://${this.storage.linkUrl}/${this.storage.linkId}`;
const ws = new this.WebSocket(linkUrl);
const ws = new RobustWebSocket(linkUrl);
const onSocketEvent = (type, event) => {

@@ -236,3 +415,2 @@ try {

onSocketEvent('onopen', event);
resolve(this.socket);
});

@@ -251,3 +429,3 @@ ws.addEventListener('message', (event) => {

onSocketEvent('onping', event);
this.socket.send('pong');
this.socket?.send('pong');
});

@@ -260,3 +438,3 @@ this.socket = ws;

async disconnect() {
await this.socket.close(1000);
await this.socket?.close(1000);
}

@@ -263,0 +441,0 @@ handleRequest(encoded) {

{
"name": "@bloks/link-session-manager",
"description": "Session management for signature providers when receiving requests using the Anchor Link protocol",
"version": "0.2.653",
"version": "0.2.654",
"homepage": "https://github.com/greymass/proton-link-session-manager",

@@ -6,0 +6,0 @@ "license": "BSD-3-Clause",

@@ -0,1 +1,2 @@

import RobustWebSocket from './websocket'
import {

@@ -22,3 +23,2 @@ Bytes,

storage?: ProtonLinkSessionManagerStorage
WebSocket: any
}

@@ -35,8 +35,7 @@

public storage: ProtonLinkSessionManagerStorage
public WebSocket: any
private socket: any
private socket?: RobustWebSocket
constructor(options: ProtonLinkSessionManagerOptions) {
this.handler = options.handler
this.WebSocket = options.WebSocket
if (options && options.storage) {

@@ -91,6 +90,6 @@ this.storage = options.storage

connect(): Promise<any> {
connect() {
return new Promise((resolve, reject) => {
const linkUrl = `wss://${this.storage.linkUrl}/${this.storage.linkId}`
const ws = new this.WebSocket(linkUrl)
const ws = new RobustWebSocket(linkUrl)

@@ -110,3 +109,2 @@ const onSocketEvent = (type: string, event: any) => {

onSocketEvent('onopen', event)
resolve(this.socket)
})

@@ -129,3 +127,3 @@

onSocketEvent('onping', event)
this.socket.send('pong')
this.socket?.send('pong')
})

@@ -144,3 +142,3 @@

async disconnect() {
await this.socket.close(1000)
await this.socket?.close(1000)
}

@@ -147,0 +145,0 @@

interface Options {
timeout: number
shouldReconnect: (event: any, ws: RobustWebSocket) => number | undefined
shouldReconnect: (event, ws: RobustWebSocket) => number | undefined
automaticOpen: boolean

@@ -9,14 +9,15 @@ handle1000: boolean

export default class RobustWebSocket {
realWs?: WebSocket
url: string
attempts = 0
reconnects = -1
reconnectWhenOnlineAgain = false
explicitlyClosed = false
binaryType: BinaryType = 'arraybuffer'
public realWs?: WebSocket
public url: string
public attempts = 0
public reconnects = -1
public reconnectWhenOnlineAgain = false
public enableAck = false
public explicitlyClosed = false
public binaryType: BinaryType = 'arraybuffer'
pendingReconnect?: number = undefined
connectTimeout?: number = undefined
public pendingReconnect?: number = undefined
public connectTimeout?: number = undefined
listeners = {
public listeners = {
open: [

@@ -28,6 +29,12 @@ (event) => {

}
event.reconnects = ++this.reconnects
event.reconnects += 1
event.attempts = this.attempts
this.attempts = 0
this.reconnectWhenOnlineAgain = false
// Send ACK
if (this.enableAck) {
const header = Buffer.from([0x42, 0x42, 0x00])
this.send(header)
}
},

@@ -38,3 +45,3 @@ ],

opts: Options = {
public opts: Options = {
// the time to wait before a successful connection

@@ -46,10 +53,11 @@ // before the attempt is considered to have timed out

// to reconnect (or null or undefined to not), rather than true or false
shouldReconnect: function (event, ws) {
shouldReconnect(event, ws) {
if ([1000, 4001].includes(event.code)) {
return undefined
} else if (event.type === 'online') {
}
if (event.type === 'online') {
return 0
} else {
return Math.pow(1.5, ws.attempts) * 300
}
return Math.pow(1.5, ws.attempts) * 300
},

@@ -64,6 +72,6 @@

constructor(url, opts: Partial<Options> = {}) {
public constructor(url: string, opts: Partial<Options> = {}) {
this.url = url
this.opts = Object.assign({}, this.opts, opts)
this.opts = {...this.opts, ...opts}

@@ -85,15 +93,24 @@ if (typeof this.opts.timeout !== 'number') {

// Only add once, e.g. onping for ping
for (const stdEvent of ['open', 'close', 'message', 'error', 'ping']) {
const onEvent = (event: Event) => {
this.dispatchEvent(event)
const cb = this['on' + stdEvent]
if (typeof cb === 'function') {
return cb(event)
// Only add once per event e.g. onping for ping
const onEvent = (stdEvent: string) => (event: {data: ArrayBuffer}) => {
// ACK stripping
if (this.enableAck && event.data && event.data.byteLength) {
const array = new Uint8Array(event.data)
if (array[0] === 0x42 && array[1] === 0x42 && array[2] === 0x01) {
this.send(Buffer.from([0x42, 0x42, 0x02, array[3]]))
event.data = event.data.slice(4)
}
}
this.dispatchEvent(event)
const cb = this[`on${stdEvent}`]
if (typeof cb === 'function') {
return cb(event)
}
}
for (const stdEvent of ['open', 'close', 'message', 'error', 'ping']) {
if (this.realWs) {
this.realWs.addEventListener(stdEvent, onEvent)
this.realWs.addEventListener(stdEvent, onEvent(stdEvent) as any)
}

@@ -104,5 +121,3 @@ }

newWebSocket(url: string) {
console.log('Creating new websocket to', url)
public newWebSocket(url: string) {
this.pendingReconnect = undefined

@@ -113,3 +128,3 @@

this.attempts++
this.attempts += 1
this.dispatchEvent({

@@ -132,3 +147,3 @@ type: 'connecting',

clearPendingReconnectIfNeeded() {
public clearPendingReconnectIfNeeded() {
if (this.pendingReconnect) {

@@ -140,3 +155,3 @@ clearTimeout(this.pendingReconnect)

send(data) {
public send(data) {
if (this.realWs) {

@@ -147,5 +162,3 @@ return this.realWs.send(data)

close(code, reason) {
console.log('Closing Websocket')
public close(code?: number, reason?: string) {
if (typeof code !== 'number') {

@@ -161,9 +174,9 @@ reason = code

if (this.realWs) {
return this.realWs.close(code, reason)
this.realWs.close(code, reason)
return
}
}
open() {
console.log('Opening Websocket')
public open() {
if (

@@ -182,11 +195,11 @@ this.realWs &&

reconnect(event) {
console.log('Reconnecting Websocket')
public reconnect(event) {
if ((!this.opts.handle1000 && event.code === 1000) || this.explicitlyClosed) {
this.attempts = 0
return
}
if (navigator.onLine === false) {
if (navigator && navigator.onLine === false) {
this.reconnectWhenOnlineAgain = true
return

@@ -205,3 +218,3 @@ }

// Taken from MDN https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
addEventListener(type, callback) {
public addEventListener(type: string, callback) {
if (!this.listeners[type]) {

@@ -214,3 +227,3 @@ this.listeners[type] = []

removeEventListener(type, callback) {
public removeEventListener(type: string, callback) {
if (!this.listeners[type]) {

@@ -220,5 +233,6 @@ return

const stack = this.listeners[type]
for (let i = 0, l = stack.length; i < l; i++) {
for (let i = 0, l = stack.length; i < l; i += 1) {
if (stack[i] === callback) {
stack.splice(i, 1)
return

@@ -229,3 +243,3 @@ }

dispatchEvent(event) {
public dispatchEvent(event) {
if (!this.listeners[event.type]) {

@@ -232,0 +246,0 @@ return

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc