reduce browser bundle size

reduces browser bundle size by ~612k or ~92%.
This commit is contained in:
Stefan Aebischer
2021-05-15 19:34:32 +02:00
parent c5fb3f2b02
commit 082633a5ca
8 changed files with 38 additions and 464 deletions

View 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
View 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');
};

View File

@@ -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
);
}
}

View File

@@ -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;

View File

@@ -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) {