1.4.0 • Published 8 days ago

@maeum/tools v1.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 days ago

@maeum/tools

ts Download Status Github Star Github Issues NPM version @maeum/tools License codecov code style: prettier

The @maeum/tools is a collection of useful functions for writing APT servers.

Table of Contents

Getting Started

installation

npm install @maeum/tools --save

Feature

Encryptioner

Class to help with AES-256-CBC encryption and decryption. You can use it when writing server code to encrypt the location of an error in the response results. Certain countries may prohibit exposing the location of these errors in the response results. For example, in South Korea, sites with more than a certain number of users are not allowed to expose the location of errors to the public internet. EncryptContainer can be used to encrypt the error response location in such cases.

const encryptioner = new Encryptioner(getEncryptionerOptions());

const err = new Error('i am error raised your specific source code');

// You can use the encrypted value in logs or API responses
const encrypted = encryptioner.encrypt(
  JSON.stringify({ message: err.message, stack: err.stack }),
);
const decrypted = encryptioner.encrypt(encrypted);

Encryptioner options overview

PropertyTypeDescription
ivSizenumberSpecify the initialize vector size.
saltnumberSpecifies the maximum size of the salt to make decryption more difficult.
keystringSpecifies the encryption key.

DI

makeEncryptioner(container, { 
  ivSize: 16, 
  key: 'your key', 
  salt: 8,
});

const encryptioner = container.resolve(ENCRYPTIONER_SYMBOL_KEY);
const encrypted = encryptioner.encrypt(
  JSON.stringify({ message: err.message, stack: err.stack }),
);
const decrypted = encryptioner.encrypt(encrypted);

You can implement class container yourself.

class Container implements IClassContainer {
  #container: Record<string | symbol, unknown> = {};

  register<T>(name: string | symbol, registration: T): this {
    this.#container[name] = registration;
    return this;
  }

  resolve<K>(name: string | symbol): K {
    return this.#container[name] as K;
  }
}

Also you can use already DI package like that awilix

class Container implements IClassContainer {
  #container: AwilixContainer = createContainer();

  register<T>(name: string | symbol, registration: T): this {
    this.#container.register(name, asValue(registration));
    return this;
  }

  resolve<K>(name: string | symbol): K {
    return this.#container.resolve(name) as K;
  }
}

Functions

nameDescription
noopnoop function
escapeUse to remove newline characters when logging log record
safeStringifyIf an exception is thrown when using the JSON.stringify function, execution will stop. safeStringify will return the value passed as defaultValue if an exception is thrown.
objectifyReturns only those fields from the input object that conform to the JSON specification
1.4.0

8 days ago

1.3.1

7 months ago

1.3.0

7 months ago

1.2.0

7 months ago

1.1.0

7 months ago

1.0.0

7 months ago