reduce browser bundle size
reduces browser bundle size by ~612k or ~92%.
This commit is contained in:
18
src/util/browser-support.ts
Normal file
18
src/util/browser-support.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
export async function signMessage(message: string, secret: string): Promise<string> {
|
||||
const encoder = new TextEncoder();
|
||||
const key = await window.crypto.subtle.importKey(
|
||||
'raw',
|
||||
encoder.encode(secret),
|
||||
{name: 'HMAC', hash: {name: 'SHA-256'}},
|
||||
false,
|
||||
['sign']
|
||||
);
|
||||
|
||||
const signature = await window.crypto.subtle.sign('HMAC', key, encoder.encode(message));
|
||||
|
||||
return Array.prototype.map.call(
|
||||
new Uint8Array(signature),
|
||||
(x: any) => ('00'+x.toString(16)).slice(-2)
|
||||
).join('');
|
||||
};
|
||||
7
src/util/node-support.ts
Normal file
7
src/util/node-support.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { createHmac } from 'crypto';
|
||||
|
||||
export async function signMessage(message: string, secret: string): Promise<string> {
|
||||
return createHmac('sha256', secret)
|
||||
.update(message)
|
||||
.digest('hex');
|
||||
};
|
||||
@@ -1,5 +1,3 @@
|
||||
import { createHmac } from 'crypto';
|
||||
|
||||
export interface RestClientOptions {
|
||||
// override the max size of the request window (in ms)
|
||||
recv_window?: number;
|
||||
@@ -23,12 +21,6 @@ export interface RestClientOptions {
|
||||
|
||||
export type GenericAPIResponse = Promise<any>;
|
||||
|
||||
export function signMessage(message: string, secret: string): string {
|
||||
return createHmac('sha256', secret)
|
||||
.update(message)
|
||||
.digest('hex');
|
||||
};
|
||||
|
||||
export function serializeParams(params: object = {}, strict_validation = false): string {
|
||||
return Object.keys(params)
|
||||
.sort()
|
||||
@@ -75,4 +67,4 @@ export function isWsPong(response: any) {
|
||||
response.ret_msg === 'pong' &&
|
||||
response.success === true
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios';
|
||||
|
||||
import { signMessage, serializeParams, RestClientOptions, GenericAPIResponse, isPublicEndpoint } from './requestUtils';
|
||||
import { signMessage } from './node-support';
|
||||
import { serializeParams, RestClientOptions, GenericAPIResponse, isPublicEndpoint } from './requestUtils';
|
||||
|
||||
export default class RequestUtil {
|
||||
private timeOffset: number | null;
|
||||
@@ -76,7 +77,7 @@ export default class RequestUtil {
|
||||
await this.syncTime();
|
||||
}
|
||||
|
||||
params = this.signRequest(params);
|
||||
params = await this.signRequest(params);
|
||||
}
|
||||
|
||||
const options = {
|
||||
@@ -134,7 +135,7 @@ export default class RequestUtil {
|
||||
/**
|
||||
* @private sign request and set recv window
|
||||
*/
|
||||
signRequest(data: any): any {
|
||||
async signRequest(data: any): Promise<any> {
|
||||
const params = {
|
||||
...data,
|
||||
api_key: this.key,
|
||||
@@ -148,7 +149,7 @@ export default class RequestUtil {
|
||||
|
||||
if (this.key && this.secret) {
|
||||
const serializedParams = serializeParams(params, this.options.strict_param_validation);
|
||||
params.sign = signMessage(serializedParams, this.secret);
|
||||
params.sign = await signMessage(serializedParams, this.secret);
|
||||
}
|
||||
|
||||
return params;
|
||||
|
||||
@@ -2,7 +2,8 @@ import { EventEmitter } from 'events';
|
||||
import { InverseClient } from './inverse-client';
|
||||
import { LinearClient } from './linear-client';
|
||||
import { DefaultLogger } from './logger';
|
||||
import { signMessage, serializeParams, isWsPong } from './util/requestUtils';
|
||||
import { signMessage } from './util/node-support';
|
||||
import { serializeParams, isWsPong } from './util/requestUtils';
|
||||
|
||||
import WebSocket from 'isomorphic-ws';
|
||||
import WsStore from './util/WsStore';
|
||||
@@ -250,7 +251,7 @@ export class WebsocketClient extends EventEmitter {
|
||||
expires: (Date.now() + timeOffset + 5000)
|
||||
};
|
||||
|
||||
params.signature = signMessage('GET/realtime' + params.expires, secret);
|
||||
params.signature = await signMessage('GET/realtime' + params.expires, secret);
|
||||
return '?' + serializeParams(params);
|
||||
|
||||
} else if (!key || !secret) {
|
||||
|
||||
Reference in New Issue
Block a user