Skip to content

Commit ab63b50

Browse files
authored
Merge pull request #9 from xhoms/master
bump to version v0.1
2 parents 69489a5 + 84203c5 commit ab63b50

76 files changed

Lines changed: 6748 additions & 232 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,5 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
yarn-debug.log*
6-
yarn-error.log*
7-
8-
# Runtime data
9-
pids
10-
*.pid
11-
*.seed
12-
*.pid.lock
13-
14-
# Directory for instrumented libs generated by jscoverage/JSCover
15-
lib-cov
16-
17-
# Coverage directory used by tools like istanbul
18-
coverage
19-
20-
# nyc test coverage
21-
.nyc_output
22-
23-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24-
.grunt
25-
26-
# Bower dependency directory (https://bower.io/)
27-
bower_components
28-
29-
# node-waf configuration
30-
.lock-wscript
31-
32-
# Compiled binary addons (https://nodejs.org/api/addons.html)
33-
build/Release
34-
35-
# Dependency directories
36-
node_modules/
37-
jspm_packages/
38-
39-
# TypeScript v1 declaration files
40-
typings/
41-
42-
# Optional npm cache directory
43-
.npm
44-
45-
# Optional eslint cache
46-
.eslintcache
47-
48-
# Optional REPL history
49-
.node_repl_history
50-
51-
# Output of 'npm pack'
52-
*.tgz
53-
54-
# Yarn Integrity file
55-
.yarn-integrity
56-
57-
# dotenv environment variables file
58-
.env
59-
60-
# next.js build output
61-
.next
1+
package-lock.json
2+
secrets.*
3+
example/*js
4+
lib/*js
5+
node_modules/

dist/common.d.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Provides common resources for other modules in the pancloud SDK
3+
*/
4+
import { sdkErr } from './error';
5+
/**
6+
* A pancloud class must provide a className property that will be used to format its log messages
7+
*/
8+
export interface pancloudClass {
9+
className: string;
10+
}
11+
export declare enum logLevel {
12+
DEBUG = 0,
13+
INFO = 1,
14+
ALERT = 2,
15+
ERROR = 3
16+
}
17+
/**
18+
* User-provided logger classes are supported as long as they adhere to this interface
19+
*/
20+
export interface pancloudLogger {
21+
level: logLevel;
22+
error(e: sdkErr): void;
23+
alert(source: pancloudClass, message: string, name?: string): void;
24+
info(source: pancloudClass, message: string, name?: string): void;
25+
debug(source: pancloudClass, message: string, name?: string, payload?: any): void;
26+
}
27+
declare const LTYPES: {
28+
"panw.auth": string;
29+
"panw.config": string;
30+
"panw.dpi": string;
31+
"panw.dpi_hipreport": string;
32+
"panw.dpi_stats": string;
33+
"panw.gtp": string;
34+
"panw.gtpsum": string;
35+
"panw.hipmatch": string;
36+
"panw.sctp": string;
37+
"panw.sctpsum": string;
38+
"panw.system": string;
39+
"panw.threat": string;
40+
"panw.thsum": string;
41+
"panw.traffic": string;
42+
"panw.trsum": string;
43+
"panw.urlsum": string;
44+
"panw.userid": string;
45+
"tms.analytics": string;
46+
"tms.config": string;
47+
"tms.system": string;
48+
"tms.threat": string;
49+
"tms.traps": string;
50+
};
51+
/**
52+
* Convenience type to guide the developer using the right entry points
53+
*/
54+
export declare type ENTRYPOINT = 'https://api.eu.paloaltonetworks.com' | 'https://api.us.paloaltonetworks.com';
55+
/**
56+
* Convenience type to guide the developer using the right paths
57+
*/
58+
export declare type PATH = "event-service/v1/channels" | "logging-service/v1/queries" | "directory-sync-service/v1";
59+
/**
60+
* Convenience type to guide the developer using the common log types
61+
*/
62+
export declare type LOGTYPE = keyof typeof LTYPES;
63+
export declare function isKnownLogType(t: string): t is LOGTYPE;
64+
/**
65+
* Instantiate a module-provided logger at load time
66+
*/
67+
export declare let commonLogger: pancloudLogger;
68+
/**
69+
* Developer might decide to change the loglevel of the logger object at runtime
70+
* @param newLevel the new log level
71+
*/
72+
export declare function setLogLevel(newLevel: logLevel): void;
73+
/**
74+
* Changes the common logger variable to a user-provided object
75+
* @param logger user provided pancloudLogger compliant object to be used for SDK logging
76+
*/
77+
export declare function setLogger(logger: pancloudLogger): void;
78+
/**
79+
* Abstract function used to retry multiple times a user-provided operation
80+
* @param source class using the retrier. Its className property value will be used in logs generated by the retrier
81+
* @param n number of attempts
82+
* @param delay milliseconds to wait after a failed attempt
83+
* @param handler function that implements the operation
84+
* @param params additional arguments to be passed to the handler function
85+
*/
86+
export declare function retrier<T, O>(source: pancloudClass, n: number | undefined, delay: number | undefined, handler: (...args: T[]) => Promise<O>, ...params: T[]): Promise<O>;
87+
export {};

dist/common.js

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
"use strict";
2+
/**
3+
* Provides common resources for other modules in the pancloud SDK
4+
*/
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
var logLevel;
7+
(function (logLevel) {
8+
logLevel[logLevel["DEBUG"] = 0] = "DEBUG";
9+
logLevel[logLevel["INFO"] = 1] = "INFO";
10+
logLevel[logLevel["ALERT"] = 2] = "ALERT";
11+
logLevel[logLevel["ERROR"] = 3] = "ERROR";
12+
})(logLevel = exports.logLevel || (exports.logLevel = {}));
13+
const LTYPES = {
14+
"panw.auth": "",
15+
"panw.config": "",
16+
"panw.dpi": "",
17+
"panw.dpi_hipreport": "",
18+
"panw.dpi_stats": "",
19+
"panw.gtp": "",
20+
"panw.gtpsum": "",
21+
"panw.hipmatch": "",
22+
"panw.sctp": "",
23+
"panw.sctpsum": "",
24+
"panw.system": "",
25+
"panw.threat": "",
26+
"panw.thsum": "",
27+
"panw.traffic": "",
28+
"panw.trsum": "",
29+
"panw.urlsum": "",
30+
"panw.userid": "",
31+
"tms.analytics": "",
32+
"tms.config": "",
33+
"tms.system": "",
34+
"tms.threat": "",
35+
"tms.traps": ""
36+
};
37+
function isKnownLogType(t) {
38+
return LTYPES.hasOwnProperty(t);
39+
}
40+
exports.isKnownLogType = isKnownLogType;
41+
/**
42+
* Centralized logging capability for the whole pancloud SDK
43+
*/
44+
class sdkLogger {
45+
/**
46+
*
47+
* @param level only messages with a level equal or avobe this provided value will be loogged
48+
* @param stackTrace boolean value to toggle stacktrace logging
49+
*/
50+
constructor(level, stackTrace = true) {
51+
this.level = level;
52+
this.stackTrace = stackTrace;
53+
}
54+
error(e) {
55+
this.format(e.getSourceClass(), e.getErrorMessage(), logLevel.ERROR, e.name, e.getErrorCode(), undefined, e.stack);
56+
}
57+
alert(source, message, name) {
58+
this.format(source.className, message, logLevel.ALERT, name);
59+
}
60+
info(source, message, name) {
61+
this.format(source.className, message, logLevel.INFO, name);
62+
}
63+
debug(source, message, name, payload) {
64+
this.format(source.className, message, logLevel.DEBUG, name, undefined, payload);
65+
}
66+
format(source, message, level, name, code, payload, stack) {
67+
if (level >= this.level) {
68+
let output = {
69+
source,
70+
message
71+
};
72+
let payloadOut = '';
73+
if (name) {
74+
output['name'] = name;
75+
}
76+
if (code) {
77+
output['code'] = code;
78+
}
79+
if (stack) {
80+
output['stack'] = stack;
81+
}
82+
if (payload) {
83+
if (typeof payload == 'string') {
84+
payloadOut = payload;
85+
}
86+
else {
87+
let jsonText = JSON.stringify(payload);
88+
if (jsonText.length > 300) {
89+
payloadOut = jsonText.substr(0, 300) + ' ...';
90+
}
91+
else {
92+
payloadOut = jsonText;
93+
}
94+
}
95+
}
96+
let finalOutput = `PANCLOUD: ${JSON.stringify(output)}`;
97+
if (payloadOut != '') {
98+
finalOutput += ` payload=${payloadOut}`;
99+
}
100+
switch (level) {
101+
case logLevel.ERROR: {
102+
console.error(finalOutput);
103+
break;
104+
}
105+
case logLevel.ALERT:
106+
case logLevel.INFO: {
107+
console.info(finalOutput);
108+
break;
109+
}
110+
default: {
111+
console.info(finalOutput);
112+
}
113+
}
114+
if (this.stackTrace && stack) {
115+
console.error(stack);
116+
}
117+
}
118+
}
119+
}
120+
/**
121+
* Instantiate a module-provided logger at load time
122+
*/
123+
exports.commonLogger = new sdkLogger(logLevel.INFO, false);
124+
/**
125+
* Developer might decide to change the loglevel of the logger object at runtime
126+
* @param newLevel the new log level
127+
*/
128+
function setLogLevel(newLevel) {
129+
exports.commonLogger.level = newLevel;
130+
}
131+
exports.setLogLevel = setLogLevel;
132+
/**
133+
* Changes the common logger variable to a user-provided object
134+
* @param logger user provided pancloudLogger compliant object to be used for SDK logging
135+
*/
136+
function setLogger(logger) {
137+
exports.commonLogger = logger;
138+
}
139+
exports.setLogger = setLogger;
140+
/**
141+
* Abstract function used to retry multiple times a user-provided operation
142+
* @param source class using the retrier. Its className property value will be used in logs generated by the retrier
143+
* @param n number of attempts
144+
* @param delay milliseconds to wait after a failed attempt
145+
* @param handler function that implements the operation
146+
* @param params additional arguments to be passed to the handler function
147+
*/
148+
async function retrier(source, n = 3, delay = 100, handler, ...params) {
149+
let a = n;
150+
let lastError = undefined;
151+
while (a > 0) {
152+
try {
153+
return await handler(...params);
154+
}
155+
catch (e) {
156+
exports.commonLogger.info(source, `Failed attempt ${a}`, 'RETRIER');
157+
lastError = e;
158+
}
159+
await new Promise((resolve) => {
160+
setTimeout(resolve, delay);
161+
});
162+
a--;
163+
}
164+
throw (lastError) ? lastError : new Error('reties exhausted');
165+
}
166+
exports.retrier = retrier;

0 commit comments

Comments
 (0)