// Copyright 2017-2018 @polkadot/api-provider authors & contributors // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. import { Logger } from '@polkadot/util/types'; import { RpcCoder } from '../coder/json/types'; import { ProviderInterface, ProviderInterface$Callback, ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types'; import './polyfill'; import assert from '@polkadot/util/assert'; import logger from '@polkadot/util/logger'; import coder from '../coder/json'; const ERROR_SUBSCRIBE = 'HTTP Provider does not have subscriptions, use WebSockets instead'; /** * The HTTP Provider allows sending requests using HTTP. it does not support subscriptions * so you won´t be able to listen to events such as new blocks or balance changes. * It is usually preferrable using the [[WsProvider]]. * * @example * import createApi from '@polkadot/api'; * import WsProvider from '@polkadot/api-provider/ws'; * * const provider = new WsProvider('http://127.0.0.1:9933'); * const api = createApi(provider); * * @see [[WsProvider]] */ export default class HttpProvider implements ProviderInterface { private coder: RpcCoder; private endpoint: string; private l: Logger; constructor (endpoint: string) { assert(/^(https|http):\/\//.test(endpoint), `Endpoint should start with 'http://', received '${endpoint}'`); this.coder = coder(); this.endpoint = endpoint; this.l = logger('api-http'); } /** * Whether the node is connected or not. * @return {boolean} true if connected */ isConnected (): boolean { return true; } /** * Events are not supported with the HttpProvider, see [[WsProvider]]. */ on (type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void { this.l.error(`HTTP Provider does not have 'on' emitters, use WebSockets instead`); } async send (method: string, params: Array): Promise { const body = this.coder.encodeJson(method, params); const response = await fetch(this.endpoint, { body, headers: { 'Accept': 'application/json', 'Content-Length': `${body.length}`, 'Content-Type': 'application/json' }, method: 'POST' }); assert(response.ok, `[${response.status}]: ${response.statusText}`); const result = await response.json(); return this.coder.decodeResponse(result); } /** * Subscriptions are not supported with the HttpProvider, see [[WsProvider]]. */ async subscribe (types: string, method: string, params: Array, cb: ProviderInterface$Callback): Promise { this.l.error(ERROR_SUBSCRIBE); throw new Error(ERROR_SUBSCRIBE); } /** * Subscriptions are not supported with the HttpProvider, see [[WsProvider]]. */ async unsubscribe (type: string, method: string, id: number): Promise { this.l.error(ERROR_SUBSCRIBE); throw new Error(ERROR_SUBSCRIBE); } }