Skip to content

Commit

Permalink
Enable web usage without webpack (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny McCormick authored Jun 6, 2019
1 parent 5eb9c90 commit db388ca
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
36 changes: 23 additions & 13 deletions lib/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import url = require("url");
import http = require("http");
import https = require("https");
import tunnel = require("tunnel");
import ifm = require('./Interfaces');
import fs = require('fs');
let fs: any;
let tunnel: any;

export enum HttpCodes {
OK = 200,
Expand Down Expand Up @@ -122,17 +122,22 @@ export class HttpClient implements ifm.IHttpClient {

this._certConfig = requestOptions.cert;

// cache the cert content into memory, so we don't have to read it from disk every time
if (this._certConfig && this._certConfig.caFile && fs.existsSync(this._certConfig.caFile)) {
this._ca = fs.readFileSync(this._certConfig.caFile, 'utf8');
}

if (this._certConfig && this._certConfig.certFile && fs.existsSync(this._certConfig.certFile)) {
this._cert = fs.readFileSync(this._certConfig.certFile, 'utf8');
}
if (this._certConfig) {
// If using cert, need fs
fs = require('fs');

if (this._certConfig && this._certConfig.keyFile && fs.existsSync(this._certConfig.keyFile)) {
this._key = fs.readFileSync(this._certConfig.keyFile, 'utf8');
// cache the cert content into memory, so we don't have to read it from disk every time
if (this._certConfig.caFile && fs.existsSync(this._certConfig.caFile)) {
this._ca = fs.readFileSync(this._certConfig.caFile, 'utf8');
}

if (this._certConfig.certFile && fs.existsSync(this._certConfig.certFile)) {
this._cert = fs.readFileSync(this._certConfig.certFile, 'utf8');
}

if (this._certConfig.keyFile && fs.existsSync(this._certConfig.keyFile)) {
this._key = fs.readFileSync(this._certConfig.keyFile, 'utf8');
}
}

if (requestOptions.allowRedirects != null) {
Expand Down Expand Up @@ -437,7 +442,12 @@ export class HttpClient implements ifm.IHttpClient {
}

if (useProxy) {
const agentOptions: tunnel.TunnelOptions = {
// If using proxy, need tunnel
if (!tunnel) {
tunnel = require('tunnel');
}

const agentOptions = {
maxSockets: maxSockets,
keepAlive: this._keepAlive,
proxy: {
Expand Down
30 changes: 22 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typed-rest-client",
"version": "1.4.0",
"version": "1.5.0",
"description": "Node Rest and Http Clients for use with TypeScript",
"main": "./RestClient.js",
"scripts": {
Expand Down

0 comments on commit db388ca

Please sign in to comment.