feat: add support for browser es modules

This commit is contained in:
Justin Starry
2021-02-02 10:53:24 +08:00
committed by Justin Starry
parent bbae23358c
commit 08ff2d12f2
20 changed files with 4235 additions and 8432 deletions

View File

@@ -26,9 +26,6 @@ export class AgentManager {
}
requestStart(): http.Agent | https.Agent {
// $FlowExpectedError - Don't manage agents in the browser
if (process.browser) return;
this._activeRequests++;
clearTimeout(this._destroyTimeout);
this._destroyTimeout = null;
@@ -36,9 +33,6 @@ export class AgentManager {
}
requestEnd() {
// $FlowExpectedError - Don't manage agents in the browser
if (process.browser) return;
this._activeRequests--;
if (this._activeRequests === 0 && this._destroyTimeout === null) {
this._destroyTimeout = setTimeout(() => {

View File

@@ -2,12 +2,14 @@
import assert from 'assert';
import bs58 from 'bs58';
import {Buffer} from 'buffer';
import {parse as urlParse, format as urlFormat} from 'url';
import fetch from 'node-fetch';
import jayson from 'jayson/lib/client/browser';
import {struct} from 'superstruct';
import {Client as RpcWebSocketClient} from 'rpc-websockets';
import {AgentManager} from './agent-manager';
import {NonceAccount} from './nonce-account';
import {PublicKey} from './publickey';
import {MS_PER_SLOT} from './timing';
@@ -21,7 +23,6 @@ import type {FeeCalculator} from './fee-calculator';
import type {Account} from './account';
import type {TransactionSignature} from './transaction';
import type {CompiledInstruction} from './message';
import {AgentManager} from './agent-manager';
export const BLOCKHASH_CACHE_TIMEOUT_MS = 30 * 1000;
@@ -578,10 +579,13 @@ type PerfSample = {
};
function createRpcRequest(url: string, useHttps: boolean): RpcRequest {
const agentManager = new AgentManager(useHttps);
let agentManager;
if (!process.env.BROWSER) {
agentManager = new AgentManager(useHttps);
}
const server = jayson(async (request, callback) => {
const agent = agentManager.requestStart();
const agent = agentManager ? agentManager.requestStart() : undefined;
const options = {
method: 'POST',
body: request,
@@ -620,7 +624,7 @@ function createRpcRequest(url: string, useHttps: boolean): RpcRequest {
} catch (err) {
callback(err);
} finally {
agentManager.requestEnd();
agentManager && agentManager.requestEnd();
}
});

View File

@@ -1,5 +1,6 @@
// @flow
import {Buffer} from 'buffer';
import * as BufferLayout from 'buffer-layout';
import * as Layout from './layout';

View File

@@ -1,5 +1,6 @@
// @flow
import {Buffer} from 'buffer';
import * as BufferLayout from 'buffer-layout';
/**

View File

@@ -1,5 +1,6 @@
// @flow
import {Buffer} from 'buffer';
import * as BufferLayout from 'buffer-layout';
import {Account} from './account';

View File

@@ -1,6 +1,7 @@
// @flow
import bs58 from 'bs58';
import {Buffer} from 'buffer';
import * as BufferLayout from 'buffer-layout';
import {PublicKey} from './publickey';

View File

@@ -4,6 +4,7 @@ import BN from 'bn.js';
import bs58 from 'bs58';
import nacl from 'tweetnacl';
import {sha256} from 'crypto-hash';
import {Buffer} from 'buffer';
//$FlowFixMe
let naclLowLevel = nacl.lowlevel;

View File

@@ -1,9 +1,10 @@
// @flow
import {Buffer} from 'buffer';
import * as BufferLayout from 'buffer-layout';
import secp256k1 from 'secp256k1';
import createKeccakHash from 'keccak';
import assert from 'assert';
import {keccak_256} from 'js-sha3';
import {PublicKey} from './publickey';
import {TransactionInstruction} from './transaction';
@@ -135,9 +136,9 @@ export class Secp256k1Program {
try {
const publicKey = publicKeyCreate(privateKey, false);
const messageHash = createKeccakHash('keccak256')
.update(toBuffer(message))
.digest();
const messageHash = Buffer.from(
keccak_256.update(toBuffer(message)).digest(),
);
const {signature, recid: recoveryId} = ecdsaSign(messageHash, privateKey);
return this.createInstructionWithPublicKey({
@@ -152,11 +153,12 @@ export class Secp256k1Program {
}
}
export function constructEthPubkey(
function constructEthPubkey(
publicKey: Buffer | Uint8Array | Array<number>,
): Buffer {
return createKeccakHash('keccak256')
.update(toBuffer(publicKey.slice(1))) // throw away leading byte
.digest()
.slice(-HASHED_PUBKEY_SERIALIZED_SIZE);
return Buffer.from(
keccak_256
.update(toBuffer(publicKey.slice(1))) // throw away leading byte
.digest(),
).slice(-HASHED_PUBKEY_SERIALIZED_SIZE);
}

View File

@@ -3,6 +3,7 @@
import invariant from 'assert';
import nacl from 'tweetnacl';
import bs58 from 'bs58';
import {Buffer} from 'buffer';
import type {CompiledInstruction} from './message';
import {Message} from './message';

View File

@@ -1,5 +1,7 @@
// @flow
import {Buffer} from 'buffer';
export const toBuffer = (arr: Buffer | Uint8Array | Array<number>): Buffer => {
if (arr instanceof Buffer) {
return arr;

View File

@@ -1,5 +1,6 @@
// @flow
import {Buffer} from 'buffer';
import {struct} from 'superstruct';
import * as Layout from './layout';