app-tracker 2.1.2 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,7 +16,12 @@ export declare class AppTrackerService {
16
16
  private os;
17
17
  private isRunningLocal;
18
18
  private ident;
19
+ private eventListenerAdded;
19
20
  init(apiKey: string, config?: AppTrackerConfig): void;
21
+ private addEventListener;
22
+ private removeEventListener;
23
+ private beforeunloadHandler;
24
+ private setRunningSystem;
20
25
  getIdent(): string;
21
26
  debug(message?: string, ...optionalParams: any[]): void;
22
27
  info(message?: string, ...optionalParams: any[]): void;
@@ -1 +1 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(e,s,o,t){return new(o||(o=Promise))((function(i,n){function r(e){try{a(t.next(e))}catch(e){n(e)}}function l(e){try{a(t.throw(e))}catch(e){n(e)}}function a(e){var s;e.done?i(e.value):(s=e.value,s instanceof o?s:new o((function(e){e(s)}))).then(r,l)}a((t=t.apply(e,s||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0});const log_1=require("./log"),cross_fetch_1=require("cross-fetch");class AppTrackerService{constructor(){this.sendLogsInDevelopment=!1,this.logInConsole=!1,this.endpointUrl="https://api-app-tracker.azurewebsites.net/",this.apiKey="",this.logs=[],this.sendTries=0,this.maxSendTries=5,this.isSending=!1,this.maxLogsCount=10,this.userAgent="",this.logLevel=[log_1.LogType.error],this.sendLogsByTypesImmediately=[log_1.LogType.error,log_1.LogType.critical],this.isNode=!1,this.isRunningLocal=!0,this.ident=""}init(e,s){var o,t,i;this.apiKey=e,s&&(s.endpointUrl&&(this.endpointUrl=s.endpointUrl),s.maxLogsCount&&(this.maxLogsCount=s.maxLogsCount),s.maxSendTries&&(this.maxSendTries=s.maxSendTries),s.sendLogsInDevelopment&&(this.sendLogsInDevelopment=s.sendLogsInDevelopment),s.logInConsole&&(this.logInConsole=s.logInConsole),s.logLevel&&(this.logLevel=s.logLevel),s.sendLogsByTypesImmediately&&(this.sendLogsByTypesImmediately=s.sendLogsByTypesImmediately)),this.isNode="[object process]"===Object.prototype.toString.call("undefined"!=typeof process?process:0),this.isNode?(this.os=null===(t=s)||void 0===t?void 0:t.userAgentOrOs,this.userAgent=this.setUserAgentFromOs()):(this.userAgent=null===(o=s)||void 0===o?void 0:o.userAgentOrOs,this.ident=this.getIdent()),this.isRunningLocal=this.isNode?(i=process.env.NODE_ENV,null!=i?i:"").indexOf("development")>-1:window.location.href.indexOf("localhost")>-1,""==e||this.isNode||this.isRunningLocal||window.addEventListener("beforeunload",(e=>{if(e.preventDefault(),e.returnValue="",!this.isSending){const e=this.logs;this.isSending=!0,this.logs=[],this.sendLogsToServerCore(e)}}))}getIdent(){var e;let s=null===(e=localStorage)||void 0===e?void 0:e.getItem("App-Tracker.device-ident");return s||(s=this.createUUID(),localStorage.setItem("App-Tracker.device-ident",s)),s}debug(e,...s){this.addLog(log_1.LogType.debug,e,...s)}info(e,...s){this.addLog(log_1.LogType.info,e,...s)}log(e,...s){this.addLog(log_1.LogType.log,e,...s)}warn(e,...s){this.addLog(log_1.LogType.warn,e,...s)}error(e,...s){this.addLog(log_1.LogType.error,e,...s)}critical(e,...s){this.addLog(log_1.LogType.critical,e,...s)}addLog(e,s,...o){this.checkLogType(e)?this.isRunningLocal&&!this.sendLogsInDevelopment?this.logToConsole(e,s,...o):this.sendLogsToServer(e,s,...o):this.logToConsole(e,s,...o)}logToConsole(e,s,...o){if(this.logInConsole)switch(e){case log_1.LogType.debug:console.debug(s,...o);break;case log_1.LogType.critical:case log_1.LogType.error:console.error(s,...o);break;case log_1.LogType.info:console.info(s,...o);break;case log_1.LogType.log:console.log(s,...o);break;case log_1.LogType.warn:console.warn(s,...o);break;default:console.warn("Unkown LogType: ",e)}}sendLogsToServer(e,s,...o){var t;const i={logType:e,message:s,optionalParams:[],createdAt:new Date,ident:this.ident};if(o.forEach((e=>{Array.isArray(e)?e.forEach((e=>i.optionalParams.push(e))):i.optionalParams.push(e)})),this.logs.push(i),this.logs.length>=this.maxLogsCount&&!this.isSending||null!=((null===(t=this.sendLogsByTypesImmediately)||void 0===t?void 0:t.find((s=>s==e)))||e==log_1.LogType.critical)&&!this.isSending){const e=this.logs;this.logs=[],this.isSending=!0,this.sendLogsToServerCore(e)}}checkLogType(e){return null!=this.logLevel.find((s=>s===e))||e===log_1.LogType.critical}sendLogsToServerCore(e){return __awaiter(this,void 0,void 0,(function*(){if(this.sendTries<this.maxSendTries)try{const s=this.endpointUrl.charAt(this.endpointUrl.length-1);yield cross_fetch_1.default(`${this.endpointUrl}${"/"===s?"logs/createLog":"/logs/createLog"}`,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","X-Auth-Key":this.apiKey},body:JSON.stringify({logs:e,userAgent:this.userAgent},((e,s)=>{if(s instanceof Error){let e={};return Object.getOwnPropertyNames(s).forEach((function(o){e[o]=s[o]})),e}return s}))});e=[],this.isSending=!1}catch(s){console.error(`AppLogger: Could not send Logs to Server try ${this.sendTries}`,s),this.sendTries++,setTimeout((()=>{this.sendLogsToServerCore(e)}),1e4)}else this.sendTries=0,this.isSending=!1,console.error("AppLogger: Could not send Logs to Server finally")}))}setUserAgentFromOs(){const e={arch:this.os.arch(),cpus:this.os.cpus(),endianness:this.os.endianness(),freemem:this.os.freemem(),homedir:this.os.homedir(),hostname:this.os.hostname(),loadavg:this.os.loadavg(),networkInterfaces:this.os.networkInterfaces(),platform:this.os.platform(),release:this.os.release(),tmpdir:this.os.tmpdir(),totalmem:this.os.totalmem(),type:this.os.type(),uptime:this.os.uptime(),userInfo:this.os.userInfo(),version:this.os.version()};return JSON.stringify(e)}createUUID(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){var s=16*Math.random()|0;return("x"==e?s:3&s|8).toString(16)}))}}exports.AppTrackerService=AppTrackerService;
1
+ "use strict";var __awaiter=this&&this.__awaiter||function(e,s,o,t){return new(o||(o=Promise))((function(i,n){function r(e){try{a(t.next(e))}catch(e){n(e)}}function l(e){try{a(t.throw(e))}catch(e){n(e)}}function a(e){var s;e.done?i(e.value):(s=e.value,s instanceof o?s:new o((function(e){e(s)}))).then(r,l)}a((t=t.apply(e,s||[])).next())}))};Object.defineProperty(exports,"__esModule",{value:!0});const log_1=require("./log"),cross_fetch_1=require("cross-fetch");class AppTrackerService{constructor(){this.sendLogsInDevelopment=!1,this.logInConsole=!1,this.endpointUrl="https://api-app-tracker.azurewebsites.net/",this.apiKey="",this.logs=[],this.sendTries=0,this.maxSendTries=5,this.isSending=!1,this.maxLogsCount=10,this.userAgent="",this.logLevel=[log_1.LogType.error],this.sendLogsByTypesImmediately=[log_1.LogType.error,log_1.LogType.critical],this.isNode=!1,this.isRunningLocal=!0,this.ident="",this.eventListenerAdded=!1}init(e,s){this.apiKey=e,s&&(s.endpointUrl&&(this.endpointUrl=s.endpointUrl),s.maxLogsCount&&(this.maxLogsCount=s.maxLogsCount),s.maxSendTries&&(this.maxSendTries=s.maxSendTries),s.sendLogsInDevelopment&&(this.sendLogsInDevelopment=s.sendLogsInDevelopment),s.logInConsole&&(this.logInConsole=s.logInConsole),s.logLevel&&(this.logLevel=s.logLevel),s.sendLogsByTypesImmediately&&(this.sendLogsByTypesImmediately=s.sendLogsByTypesImmediately)),this.setRunningSystem(s)}addEventListener(e){""==e||this.isNode||this.isRunningLocal||(window.addEventListener("beforeunload",(e=>this.beforeunloadHandler(e)),!0),this.eventListenerAdded=!0)}removeEventListener(e){""==e||this.isNode||this.isRunningLocal||(window.removeEventListener("beforeunload",(e=>this.beforeunloadHandler(e)),!0),this.eventListenerAdded=!1)}beforeunloadHandler(e){if(e.preventDefault(),e.returnValue="",!this.isSending){const e=this.logs;this.isSending=!0,this.logs=[],this.sendLogsToServerCore(e)}}setRunningSystem(e){var s,o,t;this.isNode="[object process]"===Object.prototype.toString.call("undefined"!=typeof process?process:0),this.isNode?(this.os=null===(o=e)||void 0===o?void 0:o.userAgentOrOs,this.userAgent=this.setUserAgentFromOs()):(this.userAgent=null===(s=e)||void 0===s?void 0:s.userAgentOrOs,this.ident=this.getIdent()),this.isRunningLocal=this.isNode?(t=process.env.NODE_ENV,null!=t?t:"").indexOf("development")>-1:window.location.href.indexOf("localhost")>-1}getIdent(){var e;let s=null===(e=localStorage)||void 0===e?void 0:e.getItem("App-Tracker.device-ident");return s||(s=this.createUUID(),localStorage.setItem("App-Tracker.device-ident",s)),s}debug(e,...s){this.addLog(log_1.LogType.debug,e,...s)}info(e,...s){this.addLog(log_1.LogType.info,e,...s)}log(e,...s){this.addLog(log_1.LogType.log,e,...s)}warn(e,...s){this.addLog(log_1.LogType.warn,e,...s)}error(e,...s){this.addLog(log_1.LogType.error,e,...s)}critical(e,...s){this.addLog(log_1.LogType.critical,e,...s)}addLog(e,s,...o){this.checkLogType(e)?this.isRunningLocal&&!this.sendLogsInDevelopment?this.logToConsole(e,s,...o):this.sendLogsToServer(e,s,...o):this.logToConsole(e,s,...o)}logToConsole(e,s,...o){if(this.logInConsole)switch(e){case log_1.LogType.debug:console.debug(s,...o);break;case log_1.LogType.critical:case log_1.LogType.error:console.error(s,...o);break;case log_1.LogType.info:console.info(s,...o);break;case log_1.LogType.log:console.log(s,...o);break;case log_1.LogType.warn:console.warn(s,...o);break;default:console.warn("Unkown LogType: ",e)}}sendLogsToServer(e,s,...o){var t,i;const n={logType:e,message:s,optionalParams:[],createdAt:new Date,ident:this.ident};if(o.forEach((e=>{Array.isArray(e)?e.forEach((e=>n.optionalParams.push(e))):n.optionalParams.push(e)})),this.logs.push(n),this.logs.length>=this.maxLogsCount&&!this.isSending||null!=((null===(t=this.sendLogsByTypesImmediately)||void 0===t?void 0:t.find((s=>s==e)))||e==log_1.LogType.critical)&&!this.isSending){const e=this.logs;this.logs=[],this.isSending=!0,this.sendLogsToServerCore(e),this.removeEventListener(this.apiKey)}(null===(i=this.logs)||void 0===i?void 0:i.length)>0&&!this.eventListenerAdded&&this.addEventListener(this.apiKey)}checkLogType(e){return null!=this.logLevel.find((s=>s===e))||e===log_1.LogType.critical}sendLogsToServerCore(e){return __awaiter(this,void 0,void 0,(function*(){if(this.sendTries<this.maxSendTries)try{const s=this.endpointUrl.charAt(this.endpointUrl.length-1);yield cross_fetch_1.default(`${this.endpointUrl}${"/"===s?"logs/createLog":"/logs/createLog"}`,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","X-Auth-Key":this.apiKey},body:JSON.stringify({logs:e,userAgent:this.userAgent},((e,s)=>{if(s instanceof Error){let e={};return Object.getOwnPropertyNames(s).forEach((function(o){e[o]=s[o]})),e}return s}))});e=[],this.isSending=!1}catch(s){console.error(`AppLogger: Could not send Logs to Server try ${this.sendTries}`,s),this.sendTries++,setTimeout((()=>{this.sendLogsToServerCore(e)}),1e4)}else this.sendTries=0,this.isSending=!1,console.error("AppLogger: Could not send Logs to Server finally")}))}setUserAgentFromOs(){const e={arch:this.os.arch(),cpus:this.os.cpus(),endianness:this.os.endianness(),freemem:this.os.freemem(),homedir:this.os.homedir(),hostname:this.os.hostname(),loadavg:this.os.loadavg(),networkInterfaces:this.os.networkInterfaces(),platform:this.os.platform(),release:this.os.release(),tmpdir:this.os.tmpdir(),totalmem:this.os.totalmem(),type:this.os.type(),uptime:this.os.uptime(),userInfo:this.os.userInfo(),version:this.os.version()};return JSON.stringify(e)}createUUID(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){const s=16*Math.random()|0;return("x"==e?s:3&s|8).toString(16)}))}}exports.AppTrackerService=AppTrackerService;
package/log.d.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  export declare class AppTrackerConfig {
2
2
  /** Should logs in the development environment be sent to the server. For Node enviroment process.env.NODE_ENV must be set to 'development' */
3
- sendLogsInDevelopment?: boolean | undefined;
4
- logInConsole?: boolean | undefined;
5
- endpointUrl?: string | undefined;
6
- maxSendTries?: number | undefined;
7
- maxLogsCount?: number | undefined;
3
+ sendLogsInDevelopment?: boolean;
4
+ logInConsole?: boolean;
5
+ endpointUrl?: string;
6
+ maxSendTries?: number;
7
+ maxLogsCount?: number;
8
8
  /** Defines which log types should be sent to the server */
9
- logLevel?: LogType[] | undefined;
10
- sendLogsByTypesImmediately?: LogType[] | undefined;
9
+ logLevel?: LogType[];
10
+ sendLogsByTypesImmediately?: LogType[];
11
11
  /**
12
12
  * Please pass:
13
13
  * Web -> navigator.userAgent,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "app-tracker",
3
- "version": "2.1.2",
3
+ "version": "2.2.0",
4
4
  "description": "App Tracker for Web-Applications",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -10,7 +10,26 @@
10
10
  "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
11
11
  "lint": "tslint -p tsconfig.json"
12
12
  },
13
- "keywords": [],
13
+ "keywords": [
14
+ "logging",
15
+ "protocol",
16
+ "event logging",
17
+ "monitoring",
18
+ "tracking",
19
+ "recording",
20
+ "debugging",
21
+ "iroubleshooting",
22
+ "user activity",
23
+ "data analysis",
24
+ "system events",
25
+ "performance monitoring",
26
+ "processing",
27
+ "archiving",
28
+ "alerting",
29
+ "notification",
30
+ "integration",
31
+ "interfaces"
32
+ ],
14
33
  "author": "Wigtertainment Ltd",
15
34
  "license": "ISC",
16
35
  "devDependencies": {