1.2.1 • Published 1 year ago

@lite-monitor/base v1.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@lite-monitor/base

A basic event tracking library that provides maximum flexibility and complete event definition

Table of Contents

Background

LiteMonitor started with a database online examination real-time monitoring system. During the development of the system, I found that I needed an event tracking library that could meet the following characteristics:

  • Lightweight
  • Low invasiveness
  • Well defined
  • Good compatibility

As a result, I built a JavaScript library and published it to npm. In February 2021, I started developing version 1.0 of the project.

Installation

This library uses Node.js and its package manager. Please make sure they are installed locally.

$ npm install @lite-monitor/base

or

$ yarn add @lite-monitor/base

or

$ pnpm add @lite-monitor/base

Usage

This library can be used in CommonJS project and ESM project. Please refer to the Examples.

Examples

For Node.js

import {
  Monitor,
  type ErrorEvent,
  type MonitorFetcher,
  type PublicAttrArch,
  type PublicAttrOrientation,
  type PublicAttrOs,
  type PublicAttrPlatform,
  type PublicAttrType,
} from '@lite-monitor/base';
import http from 'http';
import https from 'https';
import os from 'os';

// Initialize
const fetcher: MonitorFetcher = (method, url, type, body) => {
  return new Promise((resolve, reject) => {
    if (url.protocol !== 'http:' && url.protocol !== 'https:') {
      reject(new Error('bad url'));
    } else {
      const nodeModule = url.protocol === 'http:' ? http : https;
      const options = { method, headers: type ? { 'Content-Type': type } : {} };
      const request = nodeModule.request(url, options, (res) =>
        resolve(res.statusMessage || ''),
      );
      request.on('error', (err) => reject(err));
      request.write(body);
      request.end();
    }
  });
};
const monitor = new Monitor(fetcher);

// Report error event
const { name, message, stack } = new Error();
const event: ErrorEvent = {
  type: PublicAttrType.ERROR,
  core: os.cpus().length,
  memory: os.totalmem() / (1 << 30),
  platform: PublicAttrPlatform.NODE,
  platformVersion: process.version.substr(1),
  os: PublicAttrOs.UNKNOWN,
  osVersion: os.release(),
  arch: PublicAttrArch.UNKNOWN,
  orientation: PublicAttrOrientation.UNKNOWN,
  screenResolution: [0, 0],
  windowResolution: [0, 0],
  name,
  message,
  stack: stack?.split('\n    at ').slice(1) || [],
};
monitor.report([event]);

For web browsers

import {
  Monitor,
  type ErrorEvent,
  type MonitorFetcher,
  type PublicAttrArch,
  type PublicAttrOrientation,
  type PublicAttrOs,
  type PublicAttrPlatform,
  type PublicAttrType,
} from '@lite-monitor/base';

// Initialize
const fetcher: MonitorFetcher = (method, url, type, body) => {
  return new Promise((resolve, reject) => {
    if (url.protocol !== 'http:' && url.protocol !== 'https:') {
      reject(new Error('bad url'));
    } else {
      const options: RequestInit = {
        method,
        headers: type ? { 'Content-Type': type } : {},
        body,
        mode: 'cors',
      };
      globalThis
        .fetch(url.href, options)
        .then((res) => resolve(res.statusText))
        .catch((err) => reject(err));
    }
  });
};
const monitor = new Monitor(fetcher);

// Report error event
const { name, message, stack } = new Error();
const event: ErrorEvent = {
  type: PublicAttrType.ERROR,
  core: globalThis.navigator.hardwareConcurrency || 0,
  memory:
    (globalThis.navigator as Navigator & { deviceMemory: number })
      .deviceMemory || 0,
  platform: PublicAttrPlatform.UNKNOWN,
  platformVersion: '',
  os: PublicAttrOs.UNKNOWN,
  osVersion: '',
  arch: PublicAttrArch.UNKNOWN,
  orientation: PublicAttrOrientation.UNKNOWN,
  screenResolution: [
    globalThis.screen?.width || 0,
    globalThis.screen?.height || 0,
  ],
  windowResolution: [globalThis?.innerWidth || 0, globalThis?.innerHeight || 0],
  name,
  message,
  stack: stack?.split('\n    at ').slice(1) || [],
};
monitor.report([event]);

Related Efforts

Maintainers

@peidongxie

Contributing

Feel free to open an issue or PR.

License

MIT © 谢沛东

1.2.1

1 year ago

1.2.0

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.10

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0-beta.4

2 years ago

1.0.0-beta.5

2 years ago

1.0.0-beta.10

2 years ago

1.0.0-beta.6

2 years ago

1.0.0-beta.7

2 years ago

1.0.0-beta.8

2 years ago

1.0.0-beta.9

2 years ago

1.0.0-beta.2

3 years ago

1.0.0-beta.1

3 years ago

1.0.0-beta.0

3 years ago