Socket
Socket
Sign inDemoInstall

@walletconnect/http-connection

Package Overview
Dependencies
Maintainers
1
Versions
154
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@walletconnect/http-connection - npm Package Compare versions

Comparing version 1.0.0-beta.52 to 1.0.0-beta.53

26

lib/index.d.ts

@@ -10,21 +10,17 @@ /// <reference types="node" />

}
declare class HttpConnection extends EventEmitter {
closed: boolean;
connected: boolean;
subscriptions: boolean;
status: string;
declare class HTTPConnection extends EventEmitter {
url: string;
pollId: string;
post: IXHRPost;
subscriptionTimeout: any;
constructor(url: string);
create(): void;
init(): void;
pollSubscriptions(): void;
close(): void;
filterStatus(res: any): any;
onError(payload: any, message: string, code?: number): void;
send(payload: any, internal?: any): void;
formatError(payload: any, message: string, code?: number): {
error: {
message: string;
code: number;
};
id: any;
jsonrpc: any;
};
send(payload: any, internal?: any): Promise<unknown>;
}
export default HttpConnection;
export default HTTPConnection;
//# sourceMappingURL=index.d.ts.map
{
"name": "@walletconnect/http-connection",
"version": "1.0.0-beta.52",
"version": "1.0.0-beta.53",
"description": "Http Connection for WalletConnect Providers",

@@ -58,4 +58,4 @@ "scripts": {

"dependencies": {
"@walletconnect/types": "^1.0.0-beta.52",
"@walletconnect/utils": "^1.0.0-beta.52",
"@walletconnect/types": "^1.0.0-beta.53",
"@walletconnect/utils": "^1.0.0-beta.53",
"xhr2-cookies": "1.1.0"

@@ -62,0 +62,0 @@ },

import EventEmitter from 'events'
import { XMLHttpRequest } from 'xhr2-cookies'
import { uuid } from '@walletconnect/utils'
import { IError } from '@walletconnect/types'

@@ -26,178 +25,78 @@

class HttpConnection extends EventEmitter {
public closed: boolean
public connected: boolean
public subscriptions: boolean
public status: string
class HTTPConnection extends EventEmitter {
public url: string
public pollId: string
public post: IXHRPost
public subscriptionTimeout: any
constructor (url: string) {
super()
this.closed = false
this.connected = false
this.subscriptions = false
this.status = 'loading'
this.url = url
this.pollId = uuid()
this.post = {
method: 'POST',
body: null,
headers: { 'Content-Type': 'application/json' },
body: null
method: 'POST'
}
setTimeout(() => this.create(), 0)
}
public create (): void {
if (!XHR) {
this.emit('error', new Error('No HTTP transport available'))
return
formatError (payload: any, message: string, code = -1) {
return {
error: { message, code },
id: payload.id,
jsonrpc: payload.jsonrpc
}
this.on('error', () => {
if (this.connected) {
this.close()
}
})
this.init()
}
public init () {
this.send(
{ jsonrpc: '2.0', method: 'eth_syncing', params: [], id: 1 },
(err: IError, response: any) => {
if (err) {
this.emit('error', err)
return
}
this.send(
{
jsonrpc: '2.0',
id: 1,
method: 'eth_pollSubscriptions',
params: [this.pollId, 'immediate']
},
(err: IError, response: any) => {
if (!err) {
this.subscriptions = true
this.pollSubscriptions()
}
this.connected = true
this.emit('connect')
}
)
}
)
}
public pollSubscriptions () {
this.send(
{
jsonrpc: '2.0',
id: 1,
method: 'eth_pollSubscriptions',
params: [this.pollId]
},
(err: IError, result: any) => {
if (err) {
this.subscriptionTimeout = setTimeout(
() => this.pollSubscriptions(),
10000
)
this.emit('error', err)
} else {
if (!this.closed) {
this.subscriptionTimeout = this.pollSubscriptions()
}
if (result) {
result
.map((p: any) => {
let parse
try {
parse = JSON.parse(p)
} catch (e) {
parse = false
}
return parse
})
.filter((n: any) => n)
.forEach((p: any) => this.emit('payload', p))
}
}
}
)
}
public close () {
this.closed = true
this.emit('close')
clearTimeout(this.subscriptionTimeout)
this.removeAllListeners()
}
public filterStatus (res: any) {
if (res.status >= 200 && res.status < 300) {
return res
}
const error: IError = new Error(res.statusText)
error.res = res
throw error.message
}
public onError (payload: any, message: string, code = -1) {
this.emit('payload', {
id: payload.id,
jsonrpc: payload.jsonrpc,
error: { message, code }
})
}
public send (payload: any, internal?: any) {
if (this.closed) {
return this.onError(payload, 'Not connected')
}
if (payload.method === 'eth_subscribe') {
if (this.subscriptions) {
payload.pollId = this.pollId
} else {
return this.onError(
return new Promise(resolve => {
if (payload.method === 'eth_subscribe') {
const error = this.formatError(
payload,
'Subscriptions are not supported by this HTTP endpoint'
)
return resolve(error)
}
}
const xhr = new XHR()
let responded = false
const res = (err: IError, result?: any) => {
if (!responded) {
xhr.abort()
responded = true
if (internal) {
internal(err, result)
} else {
const { id, jsonrpc } = payload
const load = err
? { id, jsonrpc, error: { message: err.message, code: err.code } }
: { id, jsonrpc, result }
this.emit('payload', load)
const xhr = new XHR()
let responded = false
const res = (err: IError, result?: any) => {
if (!responded) {
xhr.abort()
responded = true
if (internal) {
internal(err, result)
} else {
const { id, jsonrpc } = payload
const response = err
? { id, jsonrpc, error: { message: err.message, code: err.code } }
: { id, jsonrpc, result }
resolve(response)
}
}
}
}
try {
this.post.body = JSON.stringify(payload)
} catch (e) {
return res(e)
}
xhr.open('POST', this.url, true)
xhr.timeout = 60 * 1000
xhr.onerror = res
xhr.ontimeout = res
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
try {
const response = JSON.parse(xhr.responseText)
res(response.error, response.result)
} catch (e) {
res(e)
try {
this.post.body = JSON.stringify(payload)
} catch (e) {
return res(e)
}
xhr.open('POST', this.url, true)
xhr.timeout = 60 * 1000
xhr.onerror = res as any
xhr.ontimeout = res as any
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
try {
const response = JSON.parse(xhr.responseText)
res(response.error, response.result)
} catch (e) {
res(e)
}
}
}
}
xhr.send(JSON.stringify(payload))
xhr.send(JSON.stringify(payload))
})
}
}
export default HttpConnection
export default HTTPConnection

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

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