npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gibraltarsoftware/loupe-typescript

v2.0.4

Published

Loupe Agent for Web clients

Downloads

44

Readme

loupe-typescript

@gibraltarsoftware/loupe-typescript is a Loupe Agent for web browser applications.

The agent can optionally hook into the window.onerror event to automatically log any uncaught JavaScript errors.

Use of the agent needs to be combined with a server component to correlate the actions your user performs client side with the corresponding server side processing, giving you a better insight into end-to-end functionality. See the project readme for more details.

Installation

You can install the module via npm:

Installation in Angular

While we receommend using loupe-angular for Angular applications as it wraps up additional functionality, such as auto-handling of uncaught exceptions and the addition of correlation IDs to your HTTP requests. You can of course use the basic agent. You'll want to use the agent throughout your application, so it needs to be globally available, and the simplest way to do this is to wrap it in a service.

  1. Install the agent:
  1. Create a wrapper service:
  1. Configure the agent as early as possible in the application lifecycle. We recommend app.component.ts:

Installation in React

  1. Install the agent:
  1. Create a wrapper service (LoupeService.js):
  1. Import the service into your component:
  1. Log some details:

Installation in NextJS

  1. Install the agent:
  1. Create a service to create the agent:
  1. Log some details:

Installation in Javascript

Using a Package Manager

  1. Install the agent:
  1. Import the agent package:
  1. Create a new instance of the agent:
  1. Log some details:

Without a Package Manager

If you are creating applications without a package manager, you will need to download/clone this project (loupe-typescript), build it, and use the built Javascript agent code directly.

  1. Build the project

The build generates a javascript source (see dist/loupe.typescript.js), which you can copy to your project folder.

  1. Import the Javascript agent into your module code:
  1. Create a new instance of the agent:
  1. Log some details:

Creating the Agent

Version 2 introduced a breaking change whereby the constructor arguments have changed. These are now:

  • window. Optional. The global Window object. If supplied, the agent hooks into the "onError" method to log uncaught exceptions.
  • documen. Optional. The global document object. If supplied, used to obtain document size for client platform details per log message, but only if platform details are also supplied.
  • platform. Optional. The client platform details (see interface ILocalPlatform). If supplied they will be attached to the logged client details.

Prior versions included platformjs, which automatically retrieved details of the client (user agent, engine, os, etc). To allow the Loupe Agent to be used in browserless scenarios, this dependency has been removed, but you can still pass in these platform details, either constructing them yourself, or by using platform-js and passing in the appropriate structure. This is shown in the loupe-angular project.

If you wish to supply platform details, then you can install platform and associated types from NPM:

You can then import platform and use the clientPatform as the third parameter on the constructor. Eg:

If you don't wish to use platform-js, then you can just construct a platform object manually.

You Must Set The Server Location

If you are not passing in the window object, you must set the origin of the log server, so that the agent knows where to send log messages. You can do this via the setLogServer method, which should be done as soon as the agent is created. If the server location is not specified, either directly from setLogServer or inferred from window then an error is thrown when attempting to log messages.

CORS and Credentials

When sending log messages to your API, you may need to include credentials. The setLogServer has an optional parameter that allows you to set the request credentials. For example:

This would limit credentials to be from the same origina as the web application. Values are taken from the RequestCredentials, and can be "omit", "same-origin", or "include". The default for the Loupe Agent is "include". See Request: credentials property for more details.

API

  • constructor(window?: Window, document?: Document, clientPlatform?: ILocalPlatform) - creates a new instance of the agent.

  • critical(category: string, caption: string, description: string, parameters?: any[] | null, exception?: any | null, details?: any | null, methodSourceInfo?: * MethodSourceInfo | null) - write a categorized Crticial message to Loupe.

  • error(category: string, caption: string, description: string, parameters?: any[] | null, exception?: any | null, details?: any | null, methodSourceInfo?: * MethodSourceInfo | null) - write a categorized Error message to Loupe.

  • information(category: string, caption: string, description: string, parameters?: any[] | null, exception?: any | null, details?: any | null, methodSourceInfo?: MethodSourceInfo | null) - write a categorized Information message to Loupe.

  • warning(category: string, caption: string, description: string, parameters?: any[] | null, exception?: any | null, details?: any | null, methodSourceInfo?: * MethodSourceInfo | null) - write a categorized Warning message to Loupe.

  • verbose(category: string, caption: string, description: string, parameters?: any[] | null, exception?: any | null, details?: any | null, methodSourceInfo?:MethodSourceInfo | null) - write a categorized Verbose message to Loupe.

  • write(severity: LogMessageSeverity, category: string, caption: string, description: string, parameters?: any[] | null, exception?: any | null, details?: any |null, methodSourceInfo?: MethodSourceInfo | null) - write a categorized message to Loupe.

  • recordException(exception: any, details?: any, category?: string) - write an exception to Loupe. Parses out a stack trace from the exception.

  • setLogServer(value: string | null) - set the target endpoint for log message. If not set, the current host is used with the default log point of /Loupe/Log.

  • addHeader(header: Header) - add the header to the request when logging to Loupe.

  • clientSessionHeader() - gets the Header used as the agent session id.

  • resetMessageInterval(interval: number) - resets the interval used to batch up and send messages. This interval starts at 10 milliseconds and increases if there are failures to send; messages are stored in the browser local storage and resent in order when communication is restored.

  • flushToServer() - immediately send any queued messages to the server.

The critical, error, information, warning and verbose methods are all convenience wrappers over the write method. For these, the parameters are:

  • category - The application subsystem or logging category that the log message is associated with, which supports a dot-delimited hierarchy (eg the logger name).
  • caption - A simple single-line message caption. (Will not be processed for formatting).
  • description - Additional multi-line descriptive message (or may be null) which can be a format string followed by corresponding args.
  • parameters - Optional. A variable number of arguments referenced by the formatted description string (or no arguments to skip formatting).
  • exception - Optional. The error details. This can be a string detailing the exception, an Exception object, or a JavaScript Error object.
  • details - Optional. A JSON object, or string version of, with additional details to be logged and shown in the details in Loupe Desktop and Loupe Server. This is useful for passing contextual or state information that could be useful for diagnostics.
  • methodSourceInfo - Optional. Details of the file, method, line and column number. This allows source information to be passed without the performance overhead of working out the current file and line (eg by examining the stack, which may well be different with compressed source, especially if source maps are not being used).

Examples

The first step to using Loupe is to create an instance of the agent, optionally passing in the window and document objects. If present, window will be used to hook into unhandled exceptions, and document will be used to get the page resolution. You can also optionally provide platform details (see the LocalPlatform interface) that will be logged along with requests; this can be manually created, or extracted from platform-js.

If your server project is hosted on a different domain or port, then you'll need to set the target:

Next, start logging:

The description supports C# style string formatting parameters, which should be passed as an array in parameters. For example:

For errors and additional details you can use:

The additional details parameter can be either a string or a JSON object.

The agent will attempt to parse stack traces and source details from the error, but to add source information manually you can use the MethodSourceInfo:

For me examples, see the sample project.

Dependencies

  • stacktrace-js for stack trace handling for uncaught window errors.

License

This module is licensed under ISC