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

@litert/core

Package Overview
Dependencies
Maintainers
3
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@litert/core - npm Package Compare versions

Comparing version 1.0.7 to 1.1.0

examples/01-errors.d.ts

7

CHANGES.md
# Changes Logs
## v1.0.7
## v1.1.0
- Added `setCodeBase` method for `IErrorHub`.
- Now the counter of auto-increase code number will be overwritten by a bigger specific code number
while defining a new error.
- Added wanring handling mechanism.
- Improved the `toString` of `IError` objects.

@@ -9,0 +8,0 @@ ## v1.0.6

/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -29,2 +29,4 @@ * Licensed under the Apache License, Version 2.0 (the "License");

* exposes the Promise object.
*
* @deprecated Use `@litert/observable` instead. And this class will be removed in v2.0.0 version.
*/

@@ -31,0 +33,0 @@ export declare class RawPromise<T = void, E = Error> {

"use strict";
/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -23,2 +23,4 @@ * Licensed under the Apache License, Version 2.0 (the "License");

* exposes the Promise object.
*
* @deprecated Use `@litert/observable` instead. And this class will be removed in v2.0.0 version.
*/

@@ -25,0 +27,0 @@ class RawPromise {

/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -25,2 +25,7 @@ * Licensed under the Apache License, Version 2.0 (the "License");

export declare type IPromiseTimeoutResultHandler<T, E> = (result: ITimeoutResult<T, E>) => void;
/**
* A promise controllation with timeout.
*
* @deprecated Use `@litert/observable` instead. And this class will be removed in v2.0.0 version.
*/
export declare class TimeoutPromise<T = any, E = Error> extends RawPromise<T, E> {

@@ -40,2 +45,4 @@ private _timer;

* @param handleTimeout Inject a handler to receiver the result after timeout.
*
* @deprecated Use `@litert/observable` instead. And this class will be removed in v2.0.0 version.
*/

@@ -42,0 +49,0 @@ constructor(msTimeout: number, timeoutError: E, autoStart?: boolean, handleTimeout?: IPromiseTimeoutResultHandler<T, E>);

"use strict";
/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -19,2 +19,7 @@ * Licensed under the Apache License, Version 2.0 (the "License");

const class_RawPromise_1 = require("./class.RawPromise");
/**
* A promise controllation with timeout.
*
* @deprecated Use `@litert/observable` instead. And this class will be removed in v2.0.0 version.
*/
class TimeoutPromise extends class_RawPromise_1.RawPromise {

@@ -28,2 +33,4 @@ /**

* @param handleTimeout Inject a handler to receiver the result after timeout.
*
* @deprecated Use `@litert/observable` instead. And this class will be removed in v2.0.0 version.
*/

@@ -30,0 +37,0 @@ constructor(msTimeout, timeoutError, autoStart = true, handleTimeout) {

/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -114,2 +114,6 @@ * Licensed under the Apache License, Version 2.0 (the "License");

toJSON(withStack: true): IErrorFullData<M>;
/**
* Emit as a warning.
*/
warn(): this;
}

@@ -179,8 +183,2 @@ /**

/**
* Set the base of error code number while defining an error with an omitted code number.
*
* @param codeBase The new base of error code number.
*/
setCodeBase(codeBase: number): this;
/**
* Get the error constructor by its name or code.

@@ -205,2 +203,27 @@ *

is(e: any, id?: string | number): e is IError<M>;
/**
* Emit a warning.
*
* @param e The details of warning.
*/
warn(e: IError): this;
/**
* Force throwing warnings as errors.
*
* @param enabled Set to true to force throwing warnings as exceptions. [Default: true]
*/
forceWarningAsError(enabled?: boolean): this;
/**
* Add a new listener callback for warnings.
*
* @param key The key of listener.
* @param listener The callback of listener.
*/
addWarningListener(key: string, listener: (e: IError) => void): this;
/**
* Remove an existing listener by key.
*
* @param key The key of listener.
*/
removeWarningListener(key: string): this;
}

@@ -211,2 +234,4 @@ /**

export declare const DEFAULT_ERROR_HUB_MODULE = "unknown";
export declare const DEFAULT_WARNING_LISTENER_KEY = "litert:errors:hub:default-warning-listener";
export declare const DEFAULT_WARNING_LISTENER: (e: IError<Record<string, any>>) => void;
/**

@@ -213,0 +238,0 @@ * Create a new error hub that has a standalone namespace of error types.

"use strict";
/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -99,3 +99,3 @@ * Licensed under the Apache License, Version 2.0 (the "License");

ret.prototype.toString = function () {
return `Error ${this.code} (${this.name}): ${this.message}
return `Error #${this.code} (${this.name} from ${this.module}): ${this.message}
Call Stack:

@@ -107,38 +107,70 @@ ${this.getStackAsArray().join("\n ")}`;

let DEFAULT_HUB;
const HUB_SECRETS = new WeakMap();
class ErrorHub {
constructor(moduleName) {
this._counter = 0;
this._errors = {};
this._module = moduleName;
this._baseError = (Function("BaseError", `class __ extends BaseError {
constructor(code, name, message, metadata, moduleName, aliasCodes, aliases) {
super(
code,
name,
message,
metadata,
moduleName,
aliasCodes,
aliases
);
};
}
return __;`))(BaseError, this._module);
HUB_SECRETS.set(this, {
warningListeners: {},
warningAsError: false,
counter: 0,
errors: {},
module: moduleName,
baseError: (Function("BaseError", `class __ extends BaseError {
constructor(code, name, message, metadata, moduleName, aliasCodes, aliases) {
super(
code,
name,
message,
metadata,
moduleName,
aliasCodes,
aliases
);
};
}
setCodeBase(codeBase) {
this._counter = codeBase;
return __;`))(BaseError, moduleName)
});
}
removeWarningListener(key) {
delete HUB_SECRETS.get(this).warningListeners[key];
return this;
}
addWarningListener(key, callback) {
const _this = HUB_SECRETS.get(this);
if (_this.warningListeners[key]) {
const TheError = DEFAULT_HUB.get("DUPLICATED_WARNING_LISTENER");
throw new TheError({
message: `Duplicated key of warning listener: ${JSON.stringify(name)}.`
});
}
_this.warningListeners[key] = callback;
return this;
}
forceWarningAsError(enabled = true) {
HUB_SECRETS.get(this).warningAsError = enabled;
return this;
}
warn(e) {
const _this = HUB_SECRETS.get(this);
if (_this.warningAsError) {
throw e;
}
for (const k in _this.warningListeners) {
_this.warningListeners[k](e);
}
return this;
}
get module() {
return this._module;
return HUB_SECRETS.get(this).module;
}
is(e, id) {
const _this = HUB_SECRETS.get(this);
if (typeof id !== "undefined") {
return (!!this._errors[id]) && (e instanceof this._errors[id]);
return (!!_this.errors[id]) && (e instanceof _this.errors[id]);
}
else {
return e instanceof this._baseError;
return e instanceof _this.baseError;
}
}
define(code, name, message, metadata, aliasCodes = [], aliases = []) {
const _this = HUB_SECRETS.get(this);
if (!/^[a-z]\w+$/i.test(name)) {

@@ -151,3 +183,3 @@ const TheError = DEFAULT_HUB.get("INVALID_ERROR_NAME");

if (code === null) {
code = this._counter++;
code = _this.counter++;
}

@@ -160,12 +192,6 @@ else if (!Number.isSafeInteger(code)) {

}
if (this._errors[code]) {
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_CODE");
throw new TheError({
message: `The code ${JSON.stringify(code)} of new error already exists.`
});
else {
_this.counter = code;
}
if (code > this._counter) {
this._counter = code + 1;
}
if (this._errors[name]) {
if (_this.errors[name]) {
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_NAME");

@@ -178,3 +204,3 @@ throw new TheError({

for (const alias of aliases) {
if (this._errors[alias]) {
if (_this.errors[alias]) {
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_NAME");

@@ -187,5 +213,11 @@ throw new TheError({

}
if (_this.errors[code]) {
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_CODE");
throw new TheError({
message: `The code ${JSON.stringify(code)} of new error already exists.`
});
}
if (aliasCodes.length) {
for (const alias of aliasCodes) {
if (this._errors[alias]) {
if (_this.errors[alias]) {
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_CODE");

@@ -198,3 +230,3 @@ throw new TheError({

}
this._errors[code] = this._errors[name] = (Function("BaseError", "metadata", `class ${name} extends BaseError {
_this.errors[code] = _this.errors[name] = (Function("BaseError", "metadata", "hub", `class ${name} extends BaseError {
constructor(opts = {}) {

@@ -206,3 +238,3 @@ super(

{ ...metadata, ...opts.metadata },
${JSON.stringify(this._module)},
${JSON.stringify(_this.module)},
${JSON.stringify(aliasCodes)},

@@ -212,2 +244,8 @@ ${JSON.stringify(aliases)}

};
warn() {
hub.warn(this);
return this;
};
}

@@ -234,3 +272,3 @@

"configurable": false,
"value": ${JSON.stringify(this._module)}
"value": ${JSON.stringify(_this.module)}
},

@@ -254,6 +292,6 @@ "aliases": {

return ${name};`))(this._baseError, metadata);
return ${name};`))(_this.baseError, metadata, this);
if (aliasCodes) {
for (const alias of aliasCodes) {
this._errors[alias] = this._errors[code];
_this.errors[alias] = _this.errors[code];
}

@@ -263,9 +301,9 @@ }

for (const alias of aliases) {
this._errors[alias] = this._errors[code];
_this.errors[alias] = _this.errors[code];
}
}
return this._errors[code];
return _this.errors[code];
}
get(name) {
return this._errors[name];
return HUB_SECRETS.get(this).errors[name];
}

@@ -277,2 +315,8 @@ }

exports.DEFAULT_ERROR_HUB_MODULE = "unknown";
exports.DEFAULT_WARNING_LISTENER_KEY = "litert:errors:hub:default-warning-listener";
exports.DEFAULT_WARNING_LISTENER = function (e) {
// tslint:disable-next-line: no-console
console.warn(`WARNING: A warning was emitted, and it may become an error in the future versions.
${e}`);
};
/**

@@ -282,3 +326,3 @@ * Create a new error hub that has a standalone namespace of error types.

function createErrorHub(moduleName = exports.DEFAULT_ERROR_HUB_MODULE) {
return new ErrorHub(moduleName);
return new ErrorHub(moduleName).addWarningListener(exports.DEFAULT_WARNING_LISTENER_KEY, exports.DEFAULT_WARNING_LISTENER);
}

@@ -291,2 +335,3 @@ exports.createErrorHub = createErrorHub;

DEFAULT_HUB.define(null, "DUPLICATED_ERROR_CODE", `The code of new error already exists.`, {});
DEFAULT_HUB.define(null, "DUPLICATED_WARNING_LISTENER", `The key of warning listener already exists.`, {});
/**

@@ -293,0 +338,0 @@ * Get the default hub of errors.

/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License");

"use strict";
/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -5,0 +5,0 @@ * Licensed under the Apache License, Version 2.0 (the "License");

/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License");

"use strict";
/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -5,0 +5,0 @@ * Licensed under the Apache License, Version 2.0 (the "License");

/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License");

"use strict";
/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -5,0 +5,0 @@ * Licensed under the Apache License, Version 2.0 (the "License");

{
"name": "@litert/core",
"version": "1.0.7",
"version": "1.1.0",
"description": "The core of LiteRT.",

@@ -11,4 +11,4 @@ "main": "lib/index.js",

"rebuild": "npm run clean && npm run lint && npm run build",
"test": "echo See directory src/samples",
"clean": "rm -rf lib samples",
"test": "echo See directory src/examples",
"clean": "rm -rf lib examples",
"lint": "tslint -p . -c tslint.json"

@@ -15,0 +15,0 @@ },

/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -32,2 +32,4 @@ * Licensed under the Apache License, Version 2.0 (the "License");

* exposes the Promise object.
*
* @deprecated Use `@litert/observable` instead. And this class will be removed in v2.0.0 version.
*/

@@ -34,0 +36,0 @@ export class RawPromise<T = void, E = Error> {

/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -34,2 +34,7 @@ * Licensed under the Apache License, Version 2.0 (the "License");

/**
* A promise controllation with timeout.
*
* @deprecated Use `@litert/observable` instead. And this class will be removed in v2.0.0 version.
*/
export class TimeoutPromise<T = any, E = Error>

@@ -57,2 +62,4 @@ extends RawPromise<T, E> {

* @param handleTimeout Inject a handler to receiver the result after timeout.
*
* @deprecated Use `@litert/observable` instead. And this class will be removed in v2.0.0 version.
*/

@@ -59,0 +66,0 @@ public constructor(

/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -138,2 +138,7 @@ * Licensed under the Apache License, Version 2.0 (the "License");

toJSON(withStack: true): IErrorFullData<M>;
/**
* Emit as a warning.
*/
warn(): this;
}

@@ -311,3 +316,3 @@

return `Error ${this.code} (${this.name}): ${this.message}
return `Error #${this.code} (${this.name} from ${this.module}): ${this.message}
Call Stack:

@@ -353,9 +358,2 @@ ${this.getStackAsArray().join("\n ")}`;

/**
* Set the base of error code number while defining an error with an omitted code number.
*
* @param codeBase The new base of error code number.
*/
setCodeBase(codeBase: number): this;
/**
* Get the error constructor by its name or code.

@@ -384,2 +382,31 @@ *

is(e: any, id?: string | number): e is IError<M>;
/**
* Emit a warning.
*
* @param e The details of warning.
*/
warn(e: IError): this;
/**
* Force throwing warnings as errors.
*
* @param enabled Set to true to force throwing warnings as exceptions. [Default: true]
*/
forceWarningAsError(enabled?: boolean): this;
/**
* Add a new listener callback for warnings.
*
* @param key The key of listener.
* @param listener The callback of listener.
*/
addWarningListener(key: string, listener: (e: IError) => void): this;
/**
* Remove an existing listener by key.
*
* @param key The key of listener.
*/
removeWarningListener(key: string): this;
}

@@ -389,44 +416,96 @@

interface IErrorHubPrivateData {
errors: Record<string, IErrorConstructor<any>>;
baseError: IBaseError;
counter: number;
module: string;
warningListeners: Record<string, (e: IError) => void>;
warningAsError: boolean;
}
const HUB_SECRETS = new WeakMap<any, IErrorHubPrivateData>();
class ErrorHub<M extends DefaultMetadataType>
implements IErrorHub<M> {
private _errors: Record<string, IErrorConstructor<any>>;
public constructor(moduleName: string) {
private _baseError: IBaseError;
HUB_SECRETS.set(this, {
warningListeners: {},
warningAsError: false,
counter: 0,
errors: {},
module: moduleName,
baseError: (Function(
"BaseError", `class __ extends BaseError {
constructor(code, name, message, metadata, moduleName, aliasCodes, aliases) {
super(
code,
name,
message,
metadata,
moduleName,
aliasCodes,
aliases
);
};
}
return __;`
))(BaseError, moduleName) as any
});
}
private _counter: number;
public removeWarningListener(key: string): this {
private _module: string;
delete (HUB_SECRETS.get(this) as IErrorHubPrivateData).warningListeners[key];
public setCodeBase(codeBase: number): this {
return this;
}
this._counter = codeBase;
public addWarningListener(key: string, callback: (e: IError) => void): this {
const _this = HUB_SECRETS.get(this) as IErrorHubPrivateData;
if (_this.warningListeners[key]) {
const TheError = DEFAULT_HUB.get("DUPLICATED_WARNING_LISTENER");
throw new TheError({
message: `Duplicated key of warning listener: ${JSON.stringify(name)}.`
});
}
_this.warningListeners[key] = callback;
return this;
}
public constructor(moduleName: string) {
public forceWarningAsError(enabled: boolean = true): this {
this._counter = 0;
(HUB_SECRETS.get(this) as IErrorHubPrivateData).warningAsError = enabled;
this._errors = {};
return this;
}
this._module = moduleName;
public warn(e: IError): this {
this._baseError = (Function(
"BaseError", `class __ extends BaseError {
constructor(code, name, message, metadata, moduleName, aliasCodes, aliases) {
super(
code,
name,
message,
metadata,
moduleName,
aliasCodes,
aliases
);
};
}
return __;`
))(BaseError, this._module) as any;
const _this = HUB_SECRETS.get(this) as IErrorHubPrivateData;
if (_this.warningAsError) {
throw e;
}
for (const k in _this.warningListeners) {
_this.warningListeners[k](e);
}
return this;
}

@@ -436,3 +515,3 @@

return this._module;
return (HUB_SECRETS.get(this) as IErrorHubPrivateData).module;
}

@@ -442,9 +521,11 @@

const _this = HUB_SECRETS.get(this) as IErrorHubPrivateData;
if (typeof id !== "undefined") {
return (!!this._errors[id]) && (e instanceof this._errors[id]);
return (!!_this.errors[id]) && (e instanceof _this.errors[id]);
}
else {
return e instanceof this._baseError;
return e instanceof _this.baseError;
}

@@ -462,2 +543,4 @@ }

const _this = HUB_SECRETS.get(this) as IErrorHubPrivateData;
if (!/^[a-z]\w+$/i.test(name)) {

@@ -474,3 +557,3 @@

code = this._counter++;
code = _this.counter++;
}

@@ -485,19 +568,9 @@ else if (!Number.isSafeInteger(code)) {

}
else {
if (this._errors[code]) {
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_CODE");
throw new TheError({
message: `The code ${JSON.stringify(code)} of new error already exists.`
});
_this.counter = code;
}
if (code > this._counter) {
if (_this.errors[name]) {
this._counter = code + 1;
}
if (this._errors[name]) {
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_NAME");

@@ -514,3 +587,3 @@

if (this._errors[alias]) {
if (_this.errors[alias]) {

@@ -526,2 +599,11 @@ const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_NAME");

if (_this.errors[code]) {
const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_CODE");
throw new TheError({
message: `The code ${JSON.stringify(code)} of new error already exists.`
});
}
if (aliasCodes.length) {

@@ -531,3 +613,3 @@

if (this._errors[alias]) {
if (_this.errors[alias]) {

@@ -543,4 +625,4 @@ const TheError = DEFAULT_HUB.get("DUPLICATED_ERROR_CODE");

this._errors[code] = this._errors[name] = (Function(
"BaseError", "metadata", `class ${name} extends BaseError {
_this.errors[code] = _this.errors[name] = (Function(
"BaseError", "metadata", "hub", `class ${name} extends BaseError {
constructor(opts = {}) {

@@ -552,3 +634,3 @@ super(

{ ...metadata, ...opts.metadata },
${JSON.stringify(this._module)},
${JSON.stringify(_this.module)},
${JSON.stringify(aliasCodes)},

@@ -558,2 +640,8 @@ ${JSON.stringify(aliases)}

};
warn() {
hub.warn(this);
return this;
};
}

@@ -580,3 +668,3 @@

"configurable": false,
"value": ${JSON.stringify(this._module)}
"value": ${JSON.stringify(_this.module)}
},

@@ -601,3 +689,3 @@ "aliases": {

return ${name};`
))(this._baseError, metadata) as any;
))(_this.baseError, metadata, this) as any;

@@ -608,3 +696,3 @@ if (aliasCodes) {

this._errors[alias] = this._errors[code];
_this.errors[alias] = _this.errors[code];
}

@@ -617,7 +705,7 @@ }

this._errors[alias] = this._errors[code];
_this.errors[alias] = _this.errors[code];
}
}
return this._errors[code];
return _this.errors[code];
}

@@ -629,3 +717,3 @@

return this._errors[name];
return (HUB_SECRETS.get(this) as IErrorHubPrivateData).errors[name];
}

@@ -639,2 +727,11 @@ }

export const DEFAULT_WARNING_LISTENER_KEY = "litert:errors:hub:default-warning-listener";
export const DEFAULT_WARNING_LISTENER = function(e: IError): void {
// tslint:disable-next-line: no-console
console.warn(`WARNING: A warning was emitted, and it may become an error in the future versions.
${e}`);
};
/**

@@ -647,3 +744,6 @@ * Create a new error hub that has a standalone namespace of error types.

return new ErrorHub<M>(moduleName);
return new ErrorHub<M>(moduleName).addWarningListener(
DEFAULT_WARNING_LISTENER_KEY,
DEFAULT_WARNING_LISTENER
);
}

@@ -681,2 +781,9 @@

DEFAULT_HUB.define(
null,
"DUPLICATED_WARNING_LISTENER",
`The key of warning listener already exists.`,
{}
);
/**

@@ -683,0 +790,0 @@ * Get the default hub of errors.

/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License");

/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License");

/**
* Copyright 2018 Angus.Fenying
* Copyright 2020 Angus.Fenying <fenying@litert.org>
*

@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License");

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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