Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async onConnectionPayload (payload: JsonRpc) {
const { id } = payload
if (typeof id !== 'undefined') {
if (this.promises[id]) {
if (isJsonRpcResponseError(payload)) {
this.promises[id].reject(payload.error)
} else if (isJsonRpcResponseSuccess(payload)) {
this.promises[id].resolve(payload.result)
}
delete this.promises[id]
}
} else if (isJsonRpcSubscription(payload)) {
if (payload.method && payload.method.indexOf('_subscription') > -1) {
// Emit subscription result
this.emit(payload.params.subscription, payload.params.result)
this.emit(payload.method, payload.params) // Latest EIP-1193
this.emit('data', payload) // Backwards Compatibility
}
}
}
public async checkConnection () {
public async onConnectionPayload (payload: JsonRpc) {
const { id } = payload
if (typeof id !== 'undefined') {
if (this.promises[id]) {
if (isJsonRpcResponseError(payload)) {
this.promises[id].reject(payload.error)
} else if (isJsonRpcResponseSuccess(payload)) {
this.promises[id].resolve(payload.result)
}
delete this.promises[id]
}
} else if (isJsonRpcSubscription(payload)) {
if (payload.method && payload.method.indexOf('_subscription') > -1) {
// Emit subscription result
this.emit(payload.params.subscription, payload.params.result)
this.emit(payload.method, payload.params) // Latest EIP-1193
this.emit('data', payload) // Backwards Compatibility
}
}
}
public async checkConnection () {
public rejectRequest (response: Partial) {
if (isJsonRpcResponseError(response)) {
const formattedResponse = this._formatResponse(response)
this._sendResponse(formattedResponse)
} else {
throw new Error(ERROR_MISSING_ERROR)
}
}
public trigger (
payload:
| IJsonRpcRequest
| IJsonRpcResponseSuccess
| IJsonRpcResponseError
| IInternalEvent
): void {
let eventEmitters: IEventEmitter[] = []
let event: string
if (isJsonRpcRequest(payload)) {
event = payload.method
} else if (
isJsonRpcResponseSuccess(payload) ||
isJsonRpcResponseError(payload)
) {
event = `response:${payload.id}`
} else if (isInternalEvent(payload)) {
event = payload.event
} else {
event = ''
}
if (event) {
eventEmitters = this._eventEmitters.filter(
(eventEmitter: IEventEmitter) => eventEmitter.event === event
)
}
if (
(!eventEmitters || !eventEmitters.length) &&
protected _formatResponse (
response: Partial
): IJsonRpcResponseSuccess | IJsonRpcResponseError {
if (typeof response.id === 'undefined') {
throw new Error(ERROR_MISSING_ID)
}
if (isJsonRpcResponseError(response)) {
const error = formatRpcError(response.error)
const errorResponse: IJsonRpcResponseError = {
id: response.id,
jsonrpc: '2.0',
...response,
error
}
return errorResponse
} else if (isJsonRpcResponseSuccess(response)) {
const successResponse: IJsonRpcResponseSuccess = {
id: response.id,
jsonrpc: '2.0',
...response
}
eventEmitters.forEach((eventEmitter: IEventEmitter) => {
if (isJsonRpcResponseError(payload)) {
const error = new Error(payload.error.message)
eventEmitter.callback(error, null)
} else {
eventEmitter.callback(null, payload)
}
})
}