Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

core-functions

Package Overview
Dependencies
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

core-functions - npm Package Compare versions

Comparing version 3.0.9 to 3.0.10

93

errors.js
'use strict';
const FATAL_PREFIX = 'FATAL - ';
/**

@@ -12,5 +10,5 @@ * Module containing common `Error` sub-classes.

/**
* Represents a "fatal" blocking error that will block processing indefinitely, which is typically caused by a
* misconfiguration or missing configuration or new, unplanned for scenario that prevents valid inputs from being
* processed until manual intervention can resolve the underlying issue.
* An Error subclass that indicates a "fatal" failure, which will block processing indefinitely and which is typically
* caused by a misconfiguration or missing configuration or new, unplanned for scenario that prevents a valid request
* from being processed until manual intervention can resolve the underlying issue.
*/

@@ -20,26 +18,69 @@ class FatalError extends Error {

* Constructs a new FatalError.
* @param {string} message
* @param {boolean|undefined} [usePrefix]
* @param {string} message - a message for this error.
*/
constructor(message, usePrefix) {
super(usePrefix ? prefixMessage(FATAL_PREFIX, message) : message);
constructor(message) {
super(message);
setTypeName(this.constructor);
}
toJSON() {
return toJSON(this);
}
}
Object.defineProperty(FatalError.prototype, 'blocking', {
value: true, enumerable: false, writable: true, configurable: true
});
/**
* An Error subclass that indicates a transient failure. The failed operation should be re-attempted again later.
*/
class TransientError extends Error {
/**
* Constructs a new TransientError.
* @param {string} message - a message for this error.
*/
constructor(message) {
super(message);
setTypeName(this.constructor);
}
toJSON() {
return toJSON(this);
}
}
/**
* An Error subclass that indicates that an operation timed out.
*/
class TimeoutError extends Error {
/**
* Constructs a new TimeoutError.
* @param {string} message - a message for this error.
*/
constructor(message) {
super(message);
setTypeName(this.constructor);
}
toJSON() {
return toJSON(this);
}
}
module.exports = {
FatalError: FatalError,
TransientError: TransientError,
TimeoutError: TimeoutError,
setTypeName: setTypeName,
prefixMessage: prefixMessage
prefixMessage: prefixMessage,
toJSON: toJSON
};
function prefixMessage(prefix, message) {
const msg = message ? message.toString().trim() : '';
return msg.toUpperCase().startsWith(prefix.toUpperCase()) ? msg : `${prefix}${msg}`;
const pfx = prefix ? prefix.toString() : '';
const msg = message ? message.toString() : '';
return msg.toUpperCase().startsWith(pfx.toUpperCase()) ? msg.trim() : `${pfx}${msg.trim()}`.trim();
}
/**
* Sets the `name` property of the prototype of the given type constructor to the given type constructor's name.
* @param {Function|Object} type - the type constructor
*/
function setTypeName(type) {

@@ -50,2 +91,24 @@ const prototype = type.prototype;

}
}
/**
* Converts the given error into a JSON object with `name` & `message` properties plus any and all own enumerable properties.
* @param {Error} error
* @returns {{name, message}}
*/
function toJSON(error) {
const json = {
name: error.name,
message: error.message
};
// Copy any and all enumerable own properties across too
const names = Object.keys(error); //.filter(n => !json.hasOwnProperty(n));
for (let i = 0; i < names.length; ++i) {
const name = names[i];
const value = error[name];
if (value !== undefined && typeof value !== 'function') {
json[name] = value;
}
}
return json;
}

2

package.json
{
"name": "core-functions",
"version": "3.0.9",
"version": "3.0.10",
"description": "Core functions, utilities and classes for working with Node/JavaScript primitives and built-in objects, including strings, booleans, Promises, base 64, Arrays, Objects, standard AppErrors, etc.",

@@ -5,0 +5,0 @@ "author": "Byron du Preez",

@@ -68,2 +68,4 @@ 'use strict';

wrapMethod: wrapMethod,
/** Returns a function that will wrap and convert a named node-style method into a Promise-returning function */
wrapNamedMethod: wrapNamedMethod,
/** Triggers execution of the given (typically synchronous) no-arg function, which may throw an error, within a new promise and returns the new promise */

@@ -143,7 +145,7 @@ try: attempt,

/**
* Wraps and converts the given node-style function into a Promise-returning function, which when invoked must be passed
* all of the wrapped function's arguments other than its last callback argument.
* Wraps and converts the given callback-last Node-style function into a Promise-returning function, which when invoked
* must be passed all of the wrapped function's arguments other than its last callback argument.
*
* NB: This function must be passed a node-style function that accepts as its last parameter a callback that in turn
* accepts 2 parameters: error; and data.
* NB: This function must be passed a Node-style function that accepts as its last parameter a Node-style callback that
* in turn accepts 2 parameters: error; and data.
*

@@ -182,4 +184,5 @@ * Borrowed and slightly tweaked from "You don't know JavaScript - Async & Performance" (thanks Kyle Simpson)

* @param {Function} fn - a Node-callback style function to promisify
* @returns {Function} a function, which when invoked will return a new Promise that will resolve or reject based on the
* outcome of the callback
* @returns {function(...args: *): Promise.<R>} a function, which when invoked will return a new Promise that will
* resolve or reject based on the outcome of the callback
* @template R
*/

@@ -207,7 +210,7 @@ function wrap(fn) {

/**
* Wraps and converts the given node-style method call into a promise-returning function, which will later apply the
* method to the given object and which accepts all of the wrapped method's arguments other than its last callback
* argument.
* NB: This function must be passed an object and a node-style method, which is defined on the object and which accepts
* as its last parameter a callback that in turn accepts 2 parameters: error; and data.
* Wraps and converts the given callback-last Node-style method into a promise-returning function, which will later
* apply the method to the given object and which accepts all of the wrapped method's arguments other than its last
* callback argument.
* NB: This function must be passed an object and a Node-style method, which is defined on the object and which accepts
* as its last parameter a Node-style callback that in turn accepts 2 parameters: error; and data.
* Example:

@@ -236,4 +239,5 @@ * const Promises = require('core-functions/promises');

* @param {Function} method - a Node-callback style method (of the given object) to promisify
* @returns {Function} a function, which when invoked will return a new Promise that will resolve or reject based on the
* outcome of the callback
* @returns {function(...args: *): Promise.<R>} a function, which when invoked will return a new Promise that will
* resolve or reject based on the outcome of the callback
* @template R
*/

@@ -261,2 +265,14 @@ function wrapMethod(obj, method) {

/**
* Returns a function that will wrap and convert a named callback-last Node-style method into a Promise-returning
* function.
* @param {Object} obj - the object on which to execute the given method
* @param {string} methodName - the name of a Node callback-last style method (of the given object) to promisify
* @returns {function(...args: *): Promise.<R>}
* @template R
*/
function wrapNamedMethod(obj, methodName) {
return wrapMethod(obj, obj[methodName]);
}
/**
* Triggers execution of the given (typically synchronous) no-arg function, which may throw an error, and returns a

@@ -263,0 +279,0 @@ * promise of its result or failure.

@@ -1,2 +0,2 @@

# core-functions v3.0.9
# core-functions v3.0.10

@@ -3,0 +3,0 @@ Core functions, utilities and classes for working with Node/JavaScript primitives and built-in objects, including

## Changes
### 3.0.10
- Changes to `errors` module:
- Removed `usePrefix` argument from `FatalError` constructor
- Removed `blocking` property from `FatalError` prototype
- Added new `TransientError` & `TimeoutError` Error subclasses
- Added new `toJSON` function
- Added new `toJSON` methods to all 3 Error subclasses that delegate to the new `toJSON` function
- Changes to `promises` module:
- Added convenience `wrapNamedMethod` version of `wrapMethod` function
### 3.0.9

@@ -4,0 +14,0 @@ - Fixed `toJSON` method in `AppError` class to also include any enumerable own properties

@@ -12,2 +12,5 @@ 'use strict';

const FatalError = errors.FatalError;
const TransientError = errors.TransientError;
const TimeoutError = errors.TimeoutError;
const prefixMessage = errors.prefixMessage;

@@ -20,17 +23,17 @@ test('FatalError must be initialized ', t => {

t.equal(err.blocking, true, `${err} blocking must be ${true}`);
t.notOk(err.hasOwnProperty('blocking'), `${err} must NOT have own property 'blocking'`);
// t.equal(err.fatal, true, `${err} fatal must be ${true}`);
// t.equal(err.hasOwnProperty('fatal'), true, `${err} must have own property 'fatal'`);
const errPrototype = Object.getPrototypeOf(err);
t.ok(errPrototype.hasOwnProperty('name'), `prototype ${errPrototype} must have own property 'name'`);
t.ok(errPrototype.hasOwnProperty('blocking'), `prototype ${errPrototype} must have own property 'blocking'`);
// t.notOk(errPrototype.hasOwnProperty('fatal'), `prototype ${errPrototype} must NOT have own property 'fatal'`);
err.blocking = false;
t.equal(err.blocking, false, `${err} blocking must NOW be ${false}`);
t.equal(errPrototype.blocking, true, `prototype ${errPrototype} blocking must STILL be ${true}`);
// err.fatal = false;
// t.equal(err.fatal, false, `${err} fatal must NOW be ${false}`);
// t.equal(errPrototype.fatal, undefined, `prototype ${errPrototype} fatal must be ${undefined}`);
}
const fatalError1 = new FatalError('FE msg 1');
const fatalError2 = new FatalError('FE msg 2', false);
const fatalError3 = new FatalError('FE msg 3', true);
const fatalError2 = new FatalError('FE msg 2');
const fatalError3 = new FatalError('FATAL - FE msg 3');

@@ -59,3 +62,189 @@ check(fatalError1, 'FE msg 1', 'FatalError');

fatalError1.a = 1;
fatalError2.b = 2;
// fatalError3.c = 3;
let json1 = {name: fatalError1.name, message: fatalError1.message, a: 1};
let json2 = {name: fatalError2.name, message: fatalError2.message, b: 2};
let json3 = {name: fatalError3.name, message: fatalError3.message};
t.deepEqual(JSON.parse(JSON.stringify(errors.toJSON(fatalError1))), json1, `JSON.parse(errors.toJSON(fatalError1)) must be ${JSON.stringify(json1)}`);
t.deepEqual(JSON.parse(JSON.stringify(fatalError1.toJSON())), json1, `JSON.parse(fatalError1.toJSON()) must be ${JSON.stringify(json1)}`);
t.deepEqual(JSON.parse(JSON.stringify(fatalError1)), json1, `JSON.parse(JSON.stringify(fatalError1)) must be ${JSON.stringify(json1)}`);
t.deepEqual(JSON.parse(JSON.stringify(errors.toJSON(fatalError2))), json2, `JSON.parse(errors.toJSON(fatalError2)) must be ${JSON.stringify(json2)}`);
t.deepEqual(JSON.parse(JSON.stringify(fatalError2.toJSON())), json2, `JSON.parse(fatalError2.toJSON()) must be ${JSON.stringify(json2)}`);
t.deepEqual(JSON.parse(JSON.stringify(fatalError2)), json2, `JSON.parse(JSON.stringify(fatalError2)) must be ${JSON.stringify(json2)}`);
t.deepEqual(JSON.parse(JSON.stringify(errors.toJSON(fatalError3))), json3, `JSON.parse(errors.toJSON(fatalError3)) must be ${JSON.stringify(json3)}`);
t.deepEqual(JSON.parse(JSON.stringify(fatalError3.toJSON())), json3, `JSON.parse(fatalError3.toJSON()) must be ${JSON.stringify(json3)}`);
t.deepEqual(JSON.parse(JSON.stringify(fatalError3)), json3, `JSON.parse(JSON.stringify(fatalError3)) must be ${JSON.stringify(json3)}`);
t.end();
});
test('TransientError must be initialized ', t => {
function check(err, message, name) {
t.equal(err.message, message, `${err} message must be ${message}`);
t.equal(err.name, name, `${err} name must be ${name}`);
t.notOk(err.hasOwnProperty('name'), `${err} must NOT have own property 'name'`);
// t.equal(err.transient, true, `${err} transient must be ${true}`);
// t.equal(err.hasOwnProperty('transient'), true, `${err} must have own property 'transient'`);
const errPrototype = Object.getPrototypeOf(err);
t.ok(errPrototype.hasOwnProperty('name'), `prototype ${errPrototype} must have own property 'name'`);
// t.notOk(errPrototype.hasOwnProperty('transient'), `prototype ${errPrototype} must NOT have own property 'transient'`);
// err.transient = false;
// t.equal(err.transient, false, `${err} transient must NOW be ${false}`);
// t.equal(errPrototype.transient, undefined, `prototype ${errPrototype} transient must be ${undefined}`);
}
const transientError1 = new TransientError('FE msg 1');
const transientError2 = new TransientError('FE msg 2');
const transientError3 = new TransientError('TRANSIENT - FE msg 3');
check(transientError1, 'FE msg 1', 'TransientError');
check(transientError2, 'FE msg 2', 'TransientError');
check(transientError3, 'TRANSIENT - FE msg 3', 'TransientError');
const transientError1Prototype = Object.getPrototypeOf(transientError1);
const transientError2Prototype = Object.getPrototypeOf(transientError2);
const transientError3Prototype = Object.getPrototypeOf(transientError3);
t.equal(transientError1Prototype, TransientError.prototype, `transientError1 prototype must be TransientError.prototype`);
t.equal(transientError2Prototype, TransientError.prototype, `transientError2 prototype must be TransientError.prototype`);
t.equal(transientError3Prototype, TransientError.prototype, `transientError3 prototype must be TransientError.prototype`);
t.notEqual(transientError1Prototype, Error.prototype, `transientError1 prototype must NOT be Error.prototype`);
t.equal(transientError1Prototype.__proto__, Error.prototype, `transientError1 prototype prototype must be Error.prototype`);
t.equal(transientError2Prototype.__proto__, Error.prototype, `transientError2 prototype prototype must be Error.prototype`);
t.equal(transientError3Prototype.__proto__, Error.prototype, `transientError3 prototype prototype must be Error.prototype`);
t.equal(transientError1Prototype, transientError2Prototype, `transientError1 prototype must be transientError2 prototype`);
t.equal(transientError1Prototype, transientError3Prototype, `transientError1 prototype must be transientError3 prototype`);
t.equal(transientError2Prototype, transientError3Prototype, `transientError2 prototype must be transientError3 prototype`);
// transientError1.a = 1;
transientError2.b = 2;
transientError3.c = 3;
let json1 = {name: transientError1.name, message: transientError1.message};
let json2 = {name: transientError2.name, message: transientError2.message, b: 2};
let json3 = {name: transientError3.name, message: transientError3.message, c: 3};
t.deepEqual(JSON.parse(JSON.stringify(errors.toJSON(transientError1))), json1, `JSON.parse(errors.toJSON(transientError1)) must be ${JSON.stringify(json1)}`);
t.deepEqual(JSON.parse(JSON.stringify(transientError1.toJSON())), json1, `JSON.parse(transientError1.toJSON()) must be ${JSON.stringify(json1)}`);
t.deepEqual(JSON.parse(JSON.stringify(transientError1)), json1, `JSON.parse(JSON.stringify(transientError1)) must be ${JSON.stringify(json1)}`);
t.deepEqual(JSON.parse(JSON.stringify(errors.toJSON(transientError2))), json2, `JSON.parse(errors.toJSON(transientError2)) must be ${JSON.stringify(json2)}`);
t.deepEqual(JSON.parse(JSON.stringify(transientError2.toJSON())), json2, `JSON.parse(transientError2.toJSON()) must be ${JSON.stringify(json2)}`);
t.deepEqual(JSON.parse(JSON.stringify(transientError2)), json2, `JSON.parse(JSON.stringify(transientError2)) must be ${JSON.stringify(json2)}`);
t.deepEqual(JSON.parse(JSON.stringify(errors.toJSON(transientError3))), json3, `JSON.parse(errors.toJSON(transientError3)) must be ${JSON.stringify(json3)}`);
t.deepEqual(JSON.parse(JSON.stringify(transientError3.toJSON())), json3, `JSON.parse(transientError3.toJSON()) must be ${JSON.stringify(json3)}`);
t.deepEqual(JSON.parse(JSON.stringify(transientError3)), json3, `JSON.parse(JSON.stringify(transientError3)) must be ${JSON.stringify(json3)}`);
t.end();
});
test('TimeoutError must be initialized ', t => {
function check(err, message, name) {
t.equal(err.message, message, `${err} message must be ${message}`);
t.equal(err.name, name, `${err} name must be ${name}`);
t.notOk(err.hasOwnProperty('name'), `${err} must NOT have own property 'name'`);
// t.equal(err.timeout, true, `${err} timeout must be ${true}`);
// t.equal(err.hasOwnProperty('timeout'), true, `${err} must have own property 'timeout'`);
const errPrototype = Object.getPrototypeOf(err);
t.ok(errPrototype.hasOwnProperty('name'), `prototype ${errPrototype} must have own property 'name'`);
// t.notOk(errPrototype.hasOwnProperty('timeout'), `prototype ${errPrototype} must NOT have own property 'timeout'`);
// err.timeout = false;
// t.equal(err.timeout, false, `${err} timeout must NOW be ${false}`);
// t.equal(errPrototype.timeout, undefined, `prototype ${errPrototype} timeout must be ${undefined}`);
}
const timeoutError1 = new TimeoutError('Timeout msg 1');
const timeoutError2 = new TimeoutError('Timeout msg 2');
const timeoutError3 = new TimeoutError('TIMEOUT - Timeout msg 3');
check(timeoutError1, 'Timeout msg 1', 'TimeoutError');
check(timeoutError2, 'Timeout msg 2', 'TimeoutError');
check(timeoutError3, 'TIMEOUT - Timeout msg 3', 'TimeoutError');
const timeoutError1Prototype = Object.getPrototypeOf(timeoutError1);
const timeoutError2Prototype = Object.getPrototypeOf(timeoutError2);
const timeoutError3Prototype = Object.getPrototypeOf(timeoutError3);
t.equal(timeoutError1Prototype, TimeoutError.prototype, `timeoutError1 prototype must be TimeoutError.prototype`);
t.equal(timeoutError2Prototype, TimeoutError.prototype, `timeoutError2 prototype must be TimeoutError.prototype`);
t.equal(timeoutError3Prototype, TimeoutError.prototype, `timeoutError3 prototype must be TimeoutError.prototype`);
t.notEqual(timeoutError1Prototype, Error.prototype, `timeoutError1 prototype must NOT be Error.prototype`);
t.equal(timeoutError1Prototype.__proto__, Error.prototype, `timeoutError1 prototype prototype must be Error.prototype`);
t.equal(timeoutError2Prototype.__proto__, Error.prototype, `timeoutError2 prototype prototype must be Error.prototype`);
t.equal(timeoutError3Prototype.__proto__, Error.prototype, `timeoutError3 prototype prototype must be Error.prototype`);
t.equal(timeoutError1Prototype, timeoutError2Prototype, `timeoutError1 prototype must be timeoutError2 prototype`);
t.equal(timeoutError1Prototype, timeoutError3Prototype, `timeoutError1 prototype must be timeoutError3 prototype`);
t.equal(timeoutError2Prototype, timeoutError3Prototype, `timeoutError2 prototype must be timeoutError3 prototype`);
timeoutError1.a = 1;
// timeoutError2.b = 2;
timeoutError3.c = 3;
let json1 = {name: timeoutError1.name, message: timeoutError1.message, a: 1};
let json2 = {name: timeoutError2.name, message: timeoutError2.message};
let json3 = {name: timeoutError3.name, message: timeoutError3.message, c: 3};
t.deepEqual(JSON.parse(JSON.stringify(errors.toJSON(timeoutError1))), json1, `JSON.parse(errors.toJSON(timeoutError1)) must be ${JSON.stringify(json1)}`);
t.deepEqual(JSON.parse(JSON.stringify(timeoutError1.toJSON())), json1, `JSON.parse(timeoutError1.toJSON()) must be ${JSON.stringify(json1)}`);
t.deepEqual(JSON.parse(JSON.stringify(timeoutError1)), json1, `JSON.parse(JSON.stringify(timeoutError1)) must be ${JSON.stringify(json1)}`);
t.deepEqual(JSON.parse(JSON.stringify(errors.toJSON(timeoutError2))), json2, `JSON.parse(errors.toJSON(timeoutError2)) must be ${JSON.stringify(json2)}`);
t.deepEqual(JSON.parse(JSON.stringify(timeoutError2.toJSON())), json2, `JSON.parse(timeoutError2.toJSON()) must be ${JSON.stringify(json2)}`);
t.deepEqual(JSON.parse(JSON.stringify(timeoutError2)), json2, `JSON.parse(JSON.stringify(timeoutError2)) must be ${JSON.stringify(json2)}`);
t.deepEqual(JSON.parse(JSON.stringify(errors.toJSON(timeoutError3))), json3, `JSON.parse(errors.toJSON(timeoutError3)) must be ${JSON.stringify(json3)}`);
t.deepEqual(JSON.parse(JSON.stringify(timeoutError3.toJSON())), json3, `JSON.parse(timeoutError3.toJSON()) must be ${JSON.stringify(json3)}`);
t.deepEqual(JSON.parse(JSON.stringify(timeoutError3)), json3, `JSON.parse(JSON.stringify(timeoutError3)) must be ${JSON.stringify(json3)}`);
t.end();
});
test('prefixMessage', t => {
let msg = ' ';
let prefix = 'FATAL ';
let both = 'FATAL ';
let expected = 'FATAL';
t.equal(prefixMessage(prefix, msg), expected, `prefixMessage('${prefix}', '${msg}') must be '${expected}'`);
t.equal(prefixMessage(prefix, both), expected, `prefixMessage('${prefix}', '${both}') must be '${expected}'`);
msg = ' MSG ';
prefix = ' ';
both = ' MSG ';
expected = 'MSG';
t.equal(prefixMessage(prefix, msg), expected, `prefixMessage('${prefix}', '${msg}') must be '${expected}'`);
t.equal(prefixMessage(prefix, both), expected, `prefixMessage('${prefix}', '${both}') must be '${expected}'`);
msg = ' Abc xyz ';
prefix = ' FATAL - ';
both = ' FATAL - Abc xyz ';
expected = 'FATAL - Abc xyz';
t.equal(prefixMessage(prefix, msg), expected, `prefixMessage('${prefix}', '${msg}') must be '${expected}'`);
t.equal(prefixMessage(prefix, both), expected, `prefixMessage('${prefix}', '${both}') must be '${expected}'`);
msg = ' [Abc xyz] ';
prefix = ' TRANSIENT';
both = ' TRANSIENT[Abc xyz] ';
expected = 'TRANSIENT[Abc xyz]';
t.equal(prefixMessage(prefix, msg), expected, `prefixMessage('${prefix}, '${msg}') must be '${expected}'`);
t.equal(prefixMessage(prefix, both), expected, `prefixMessage('${prefix}', '${both} ') must be '${expected}'`);
t.end();
});

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc