New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@softeq/utils

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@softeq/utils

Javascript Utils

latest
Source
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

Javascript Utils

Overview

Implementation of helper tools.
Development documentation

Install

npm install @softeq/utils

Usage

Package helper tools are responsible on helpers to simplify interaction with server commands. Node.js has child_process module that provides the ability to spawn child processes in a manner that is similar, but not identical, to popen(3)

Default child_process.exec callback has a bit non standard arguments order to have more stable common handlers callback approach. Default handler callback:

/**
 * @param {Error} error - Instance of Error object. Can be thrown when runtime errors occur.
 * @param {string | Buffer} stdout - `stdout` unix output string
 * @param {string | Buffer} stderr - `stderr` unix output string
 */
function callback(error, stdout, stderr) {
  // ...
}

exec('ps', (error, stdout, stderr) => {
  // ...
})

Helper function execAction provides more standard arguments order and provides internal errors handlers according to conditional configuration exitConditions.

/**
 * @param {string | Buffer} response - `stdout` unix output string
 * @param {string | Buffer} stderr - `stderr` unix output string
 * @param {Error} error - Instance of Error object. Can be thrown when runtime errors occur.
 */
function callback(response, stderr, error) {
  // ...
}

exec('ps', execAction((response, stderr, error) => {
  // ...
}, { stderr: true, error: false }));

Helper function stop stops any running process and displays message according to the Logger.stack method.

exec('ps', execAction(() => {
  stop([`${ANSI_FG_RED}%s${ANSI_FG_NC}`, `The machine will not be reboot according to the \`stop\` command.`]);
  exec('sudo shutdown -r now', execAction(() => {
    Logger.stack([
      [`${ANSI_FG_RED}%s${ANSI_FG_NC}`, `The machine has been reboot.`],
    ]);
  }))
}));

Pay attention to the argument of stop function:

  • Array of console.log arguments
  • Package contains default unix terminal colors according to ANSI escape code.

Colors:

  • red
  • yellow
  • green
  • no color

Note: try to consider why do we need no color ANSI escape code.

Logger

Node.js interaction with server requires terminal notifications often. Logger class provides the common way to compose notifications with next static methods.

static Logger.stack

Displays the stack of messages. Message is an array of console.log arguments.

Logger.stack([
  [`%s`, `${ANSI_FG_GREEN}Response:${ANSI_FG_NC}`],
  [response],
]);
static Logger.error

Displays an error according to unix output standard with Node.js improvement according to conditional configuration exitConditions.

Logger.error(error, stderr, { stderr: true, error: false });
Common
  • Some terminal messages can contain EMPTY_LINE as a new line or EMPTY_STRING.
  • Examples are focused on Unix environment.

Keywords

javascript

FAQs

Package last updated on 21 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts