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

x2node-common

v1.5.2

Published

Common utilities.

Downloads

30

Readme

X2 Framework for Node.js | Common Utilities

This is a common utilities module for the X2 Framework for Node.js. The module is included by other modules that comprise the farmework but can also be used by the application as well.

See module's API Reference Documentation.

Table of Contents

Errors

The framework modules, when encounter an unrecoverable error, often throw an Error object. The framework uses the following three error classes that are exported by the common module:

  • X2UsageError - This is the error most commonly thrown by the framework components when they are not correctly invoked by the application. Examples include invalid function arguments, inappropriate function calls and all other kinds of incorrect use of the framework. Normally, this is a fatal error and it needs not to be catched, because getting this error means a bug in the application that needs to be fixed.

  • X2DataError - This error is used to indicate that invalid, unexpected data was provided to the framework by an external component. For example, this could indicate that unexpected data cames from the database in response to a otherwise correct query. Normally, to fix the error condition something needs to be done in the external data source rather than the application code. Therefore, the application may consider this type of error as non-fatal and continue operation, while having the error logged and brought up to the maintenance attention.

  • X2SyntaxError - This error is similar to the X2DataError, but has more specific "flavor". It indicates that something provided to the application from the outside has invalid syntax. Treatment of this error by the application depends on what exactly and under what circumstances throws it.

Normally, the application does create instances of these error, but it may catch and analyze one, for example like this:

const common = require('x2node-common');

...

try {

    // call some framework stuff
    ...

} catch (err) {
    if (err instanceof common.X2SyntaxError) {
        // do something
        ...
    } else {
        throw err;
    }
}

...

Logging

The framework modules use simple console-based logging provided by the common module. There are two type of logging provided: debug logging, which can be selectively turned on and off, and error logging. The application use the logging provided by the common module as well. The module exports two functions:

  • getDebugLogger(section) - Gets a debug log function for the specified by the string argument application section. Similarly to the Node.js internally logging, the function checks if NODE_DEBUG environment variable contains the specified section (the NODE_DEBUG variable is a space-separated list of section names) and if so, returns a function that takes a debug message and outputs it to the process standard error stream. If section is not listed in the NODE_DEBUG variable, the returned function does nothing. See specific module documentation for the section names used by the module.

  • error(msg, [err]) - Unconditionally outputs the error message to the process standard error stream. Besides the message, can be optionally provided with an Error object.

A custom application could use these logging facilities like the following:

const common = require('x2node-common');

const log = common.getDebugLogger('MYAPP');

try {
    log('starting dangerous operation');

    ...

} catch (err) {
    common.error('serious error', err);
}

The returned debug logger function also has a Boolean property enabled, which tells if the debug logger is enabled or is a noop. For example, it can be used like the following:

const log = common.getDebugLogger('MYAPP');

if (log.enabled)
    log(`a message ${that} is very ${expensive} to ${build}`);

By default, the log ouput includes the timestamp, the PID, the section (or "ERROR" for the error) and the message itself. An error message also includes the error stack, if provided.

The supplementary information included in the log output can be configured using X2_LOG environment variable, which is a comma-separated (no whitespace!) list of options. Each option can be:

  • nots - Do no include the message timestamp.
  • nopid - Do not include the process PID.
  • nosec - Do not include the debug log section. Does not affect error messages, which always include "ERROR" in the place of the section.
  • env:VARIABLE - Include value of the VARIABLE environment variable.

Actors

Throughout the framework's modules the notion of actor is used, which is the entity that performs an operation (makes a web-service API call, runs a database query, etc.). In the most cases the actor is what often call user or principal in many other systems. X2 Framework does not use the term user because the entity that performs operations is not necessarily a person (it can be a script, or the system itself).

For the modules, the actor is represented by an object, that exposes the following properties and methods:

  • stamp - Actor stamp, which is a string or a number that identifies the actor in various historical records, such as logs, record modification histories, etc. It can be, for example, a user login name or a user account id.

  • id - The shotest possible unique identification of the actor, such as the user id, for example. Can be either a number or a string.

  • hasRole(role) - Tells if the actor has the specified role.

Applications normally add more application-specific properties and methods to the actor objects, but the framework modules expect at least the above.