rage_utils v1.0.7
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 againstrage_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 numbersrandomInt
:() -> (number)
returns a cryptographically secure random numberuuid
:() -> (string)
returns a uuid version 4 string
Hashing
bcrypt
:(value: string, cost: number) -> (Uint8Array)
runs bcrypt on value with given cost and returns the hashscrypt
:(value: string, cost: number, hashLength: number, salt: Uint8Array|null) -> (Object{hash: Uint8Array, salt: Uint8Array})
runs scrypt on value and returns a hash with given hashLengthsha1
:(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 timereset
:(mode: string) -> (number)
resets the clock and returns elapsed time since startelapsed
:(mode: string) -> (number)
returns elapsed time since start