1.0.7 • Published 5 years ago

rage_utils v1.0.7

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Rage Utils

Just a bunch of utility functions for rage (cross environment: server, client, browser) that are not easy to implement in JS specially for client and browser! Also you can use this package for projects other than rage.

If you want a new feature in utilities that you think others might use too, submit an issue.

Install

Either grab a copy of rage_utils.js (which is minified) or use npm:

npm install rage_utils

(I highly recommend to use npm or yarn instead of manually downloading the rage_utils.js file)

Note that you must run this command in both client_packages and your server directory if you want to use Rage Utils on both sides.

Use

Server

// server script
const rutils = require('rage_utils');

Client

For use in client, rage_utils must be imported on server.

// client script
const rutils_init = require('./node_modules/rage_utils/rage_utils.js')

// ONLY INITIAlIZE ONCE
rutils_init().then(rutils => {
    // code that uses rutils
})

All client functions return a promise therefore you must use then or async/await. This is because client runtime doesn't have the required APIs and it uses a hidden browser that the content is served by rage_utils http web server and it calls JS code on browser. Server opens port 33808 for http listening.

CEF

// browser
<script src="package://node_modules/rage_utils/rage_utils.js"></script>
<script>
    RUtils.<function> ...
</script>

Functions

Note: Some functions return an object with an error field that you must check whether it's null to take required action in case of errors.

Note: AES keys must be 32 bytes long and use 2048 or 4096 bits for RSA

Runtime

  • getRuntime: () -> (number) returns current runtime which can be tested against rage_utils.RuntimeServer, rage_utils.RuntimeClient, rage_utils.RuntimeBrowser
  • getRuntimeName: () -> (string) returns current runtime name

Utility

  • uint8ArrayToString: (value: Uint8Array) -> (string)

Random

  • randomBuffer: (length: number) -> (Uint8Array) returns an array with given length filled with cryptographically secure random numbers
  • randomInt: () -> (number) returns a cryptographically secure random number
  • uuid: () -> (string) returns a uuid version 4 string

Hashing

  • bcrypt: (value: string, cost: number) -> (Uint8Array) runs bcrypt on value with given cost and returns the hash
  • scrypt: (value: string, cost: number, hashLength: number, salt: Uint8Array|null) -> (Object{hash: Uint8Array, salt: Uint8Array}) runs scrypt on value and returns a hash with given hashLength
  • sha1: (value: string) -> (Uint8Array)
  • sha256: (value: string) -> (Uint8Array)
  • fnv1a32: (value: string) -> (number)
  • fnv1a64: (value: string) -> (number)
  • base64Encode: (value: Uint8Array|string) -> (string)
  • base64Decode: (value: string) -> (Object{result: Uint8Array, error: string})

AES

  • encryptAES256: (data: string, key: Uint8Array|string) -> (Object{result: Uint8Array, error: string})
  • decryptAES256: (data: Uint8Array, key Uint8Array|string) -> (Object{result: Uint8Array, error: string})

RSA

  • rsaGenerateKeys: (bits: number) -> (Object{public: string, private: string, error: string}) generates RSA public and private keys with given bits size. Keys are PEM encoded and can be saved, loaded to avoid the heavy key generation process.
  • encryptRSA: (value: string, public: string) -> (Object{result: Uint8Array, error: string})
  • decryptRSA: (value: Uint8Array, private: string) -> (Object{result: string, error: string})

Classes

Clock

Server and browser only

This class measures elapsed time from nanoseconds to hours.

Modes that you can select are "ns", "us", "ms", "s", "m", "h"

methods

  • constructor: () -> (Clock) starts measuring time
  • reset: (mode: string) -> (number) resets the clock and returns elapsed time since start
  • elapsed: (mode: string) -> (number) returns elapsed time since start
1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago