Skip to content

Commit

Permalink
- Added NodeJS TLS Enable/Disable mode
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidArayan committed Nov 30, 2023
1 parent 0e46d13 commit 6256977
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
18 changes: 14 additions & 4 deletions sdk-core/src/core/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export interface ServiceOptions {
// NOTE: When SDK is used in browser, gzip is automatically enabled
readonly gzip?: boolean | null;

// (optuonal) for non-secure NodeJS environments to enable/disable tls
// this defaults to 'false'
// (optional) for non-secure NodeJS environments to enable/disable tls
// this defaults to 'true'
readonly tls?: boolean | null;

// (optional) for error handling when api throws errors like 404, 401 etc
Expand Down Expand Up @@ -89,8 +89,8 @@ export abstract class Service {
url: config.url,
options: {
version: (config.options && config.options.version) ? config.options.version : 'v3',
tls: (config.options && config.options.tls) ? true : false,
gzip: (config.options && config.options.gzip) ? true : false,
tls: (config.options && config.options.tls) ? Util.parseBool(config.options.tls) : false,
gzip: (config.options && config.options.gzip) ? Util.parseBool(config.options.gzip) : false,
errorHandler: (config.options && config.options.errorHandler) ? config.options.errorHandler : 'console.error',
errorListener: (config.options && config.options.errorListener && Util.isFunction(config.options.errorListener)) ? config.options.errorListener : (_: Error) => { /* silent handler */ }
},
Expand All @@ -99,6 +99,16 @@ export abstract class Service {
token: (config.auth && config.auth.token) ? config.auth.token : null
}
});

// set TLS options for NodeJS
if (Util.isNode()) {
if (this._config.options.tls) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "1";
}
else {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
}
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions sdk-core/src/generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class Generator {
private static generateIndexFile(files: Array<{ dir: string, schemas: Array<GeneratedSchema> }>): string {
let output: string = '/*\n * Warning: Do Not Edit - Auto Generated via @plattar/sdk-core\n */\n\n';

output += `export { Service, ServiceConfig, ServiceAuth } from '@plattar/sdk-core';\n`;
output += `export { Service, ServiceConfig, ServiceAuth, ServiceOptions, ServiceAuthType, ServiceErrorHandler, ServiceErrorListener } from '@plattar/sdk-core';\n`;

files.forEach((schemas) => {
schemas.schemas.forEach((schema) => {
Expand Down Expand Up @@ -96,8 +96,8 @@ export class Generator {
output += '}\n';

return {
name: `connection-service`,
fname: `connection-service.ts`,
name: `connection`,
fname: `connection.ts`,
data: output
}
}
Expand Down
19 changes: 19 additions & 0 deletions sdk-core/src/generator/generators/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,23 @@ export class Util {
public static isFunction(obj: any): boolean {
return !!(obj && obj.constructor && obj.call && obj.apply);
}

/**
* Checks if running inside NodeJS
*/
public static isNode(): boolean {
return (typeof process !== 'undefined') && (process.release.name === 'node');
}

public static parseBool(value: any): boolean {
if (!value) {
return false;
}

if ((typeof value === 'string') || (value instanceof String)) {
return value.toLowerCase() === 'true' ? true : false;
}

return value === true ? true : false;
}
}

0 comments on commit 6256977

Please sign in to comment.