From eaf6156d35f4201bbd2668252aba8fd718d81b88 Mon Sep 17 00:00:00 2001 From: Oleksandr Andriienko Date: Mon, 1 Jul 2019 11:23:23 +0300 Subject: [PATCH] Add self signed certificate path to the rest config (#18) * Support self signed sertificates. Signed-off-by: Oleksandr Andriienko --- src/index.ts | 25 ++++++++++++++++++++----- webpack.config.js | 3 +++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 77d833f..43a2666 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,7 +15,8 @@ import {Resources} from './rest/resources'; import {Backend, IBackend} from './rest/backend'; import {IWorkspaceMasterApi, WorkspaceMasterApi} from './json-rpc/workspace-master-api'; import {WebSocketClient} from './json-rpc/web-socket-client'; - +import * as fs from 'fs'; +import * as https from 'https'; export * from './rest/backend'; export * from './rest/remote-api'; @@ -24,12 +25,12 @@ export * from './json-rpc/workspace-master-api'; export interface IRestAPIConfig { baseUrl?: string; headers?: any; + // path to self signed certificate + ssCrtPath?: string; } export default class WorkspaceClient { - private static axiosInstance: AxiosInstance = axios; - public static getRestApi(config: IRestAPIConfig = {}): IRemoteAPI { let baseUrl = config.baseUrl; if (!baseUrl) { @@ -42,12 +43,12 @@ export default class WorkspaceClient { const headers = config.headers || {}; - const resources = new Resources(this.axiosInstance, baseUrl, headers); + const resources = new Resources(this.createAxiosInstance(config), baseUrl, headers); return new RemoteAPI(resources); } public static getRestBackend(): IBackend { - return new Backend(this.axiosInstance, moxios); + return new Backend(axios, moxios); } public static getJsonRpcApi(entryPoint: string): IWorkspaceMasterApi { @@ -55,4 +56,18 @@ export default class WorkspaceClient { return new WorkspaceMasterApi(transport, entryPoint); } + private static createAxiosInstance(config: IRestAPIConfig): AxiosInstance { + if (config.ssCrtPath && this.isItNode() && fs.existsSync(config.ssCrtPath)) { + const agent = new https.Agent({ + ca: fs.readFileSync(config.ssCrtPath) + }); + return axios.create({httpsAgent: agent}); + } + + return axios; + } + + private static isItNode() { + return (typeof process !== 'undefined') && (typeof process.versions.node !== 'undefined') + } } diff --git a/webpack.config.js b/webpack.config.js index db8d25d..fa751d3 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -49,6 +49,9 @@ var client = { extensions: ['.ts', '.js'] }, target: 'web', + node: { + fs: 'empty', + }, output: { filename: 'client.js', library: 'workspace-client',