upperH
upperH is a collection of helper functions to make starting a nodeJS project faster.
Get started
Install
npm install upperh --save
How to use - Server
const H = require('upperh');
(async () => {
// Your program here:
var name = await H.input('What is your name?');
var password = await H.input('Password: ', true); // input is muted (not displayed)
await H.delay(2000); // Wait 2 seconds
var fileContent = await H.readFile('myfile.txt');
await H.writeFile('myfile-copy.txt', fileContent);
})();
How to use - Browser
const H = require('upperh');
H(async () => { // Will execute when DOM is loaded
H('span.classname').removeClass('classname').closest('div').attr('data-value', 'true');
await H.delay(1000); // Some helpers are shared between server/browser
// Yes, you can use the same api for http requests whether it is client-side or server-side
console.log((await H.httpGet('http://api.icndb.com/jokes/random', {}, undefined, undefined, 'form', 'json')).value.joke);
});
Classes
- HObject
-
Collection of DOM Nodes, extends Array
Members
-
HBrowser ⇒
HObject
-
H helper functions available only for Browsers. Chainable and is similar to jQuery's $ constructor.
- H
-
H helper functions available in both Servers and browsers
- HServer
-
H helper functions available only for Servers
HObject
Collection of DOM Nodes, extends Array
Kind: global class
-
HObject
-
.find(selector) ⇒
HObject
-
.closest(selector) ⇒
HObject
-
.contains(selector) ⇒
HObject
-
.parents(selector) ⇒
HObject
-
.parent(selector) ⇒
HObject
-
.children(selector) ⇒
HObject
-
.siblings(selector) ⇒
HObject
-
.first() ⇒
HObject
-
.last() ⇒
HObject
-
.prev(selector) ⇒
HObject
-
.next(selector) ⇒
HObject
-
.prevAll(selector) ⇒
HObject
-
.nextAll(selector) ⇒
HObject
-
.attr(name) ⇒
String
-
.attr(name, value) ⇒
HObject
-
.removeAttr(name) ⇒
HObject
-
.show() ⇒
HObject
-
.hide() ⇒
HObject
-
.prop(key) ⇒
String
-
.prop(key, value) ⇒
HObject
-
.css(name) ⇒
String
-
.css(name, value) ⇒
HObject
-
.css(properties) ⇒
HObject
-
.append(content) ⇒
HObject
-
.appendTo(target) ⇒
HObject
-
.prepend(content) ⇒
HObject
-
.prependTo(target) ⇒
HObject
-
.insertBefore(target) ⇒
HObject
-
.before(content) ⇒
HObject
-
.insertAfter(target) ⇒
HObject
-
.after(content) ⇒
HObject
-
.wrap(element) ⇒
HObject
-
.wrapInner(element) ⇒
HObject
-
.empty() ⇒
HObject
-
.remove() ⇒
void
-
.eq(index) ⇒
HObject
-
.index() ⇒
Number
-
.html() ⇒
String
-
.html(content) ⇒
HObject
-
.text() ⇒
String
-
.text(content) ⇒
HObject
-
.val() ⇒
String
-
.val(value) ⇒
HObject
-
.each(function) ⇒
HObject
-
.get() ⇒
Node
-
.get(index) ⇒
Node
-
.addClass(className) ⇒
HObject
-
.removeClass(className) ⇒
HObject
-
.toggleClass(className) ⇒
HObject
-
.hasClass(className) ⇒
Boolean
-
.filter(selector) ⇒
HObject
-
.filter(fn) ⇒
HObject
-
.is(selector) ⇒
Boolean
-
.offset() ⇒
Object
-
.on(events, selector, callback, options) ⇒
HObject
-
.one(events, selector, callback, options) ⇒
HObject
-
.off(events, callback) ⇒
HObject
-
.trigger(event, extraParams, eventOptions) ⇒
HObject
-
.triggerHandler(event, extraParams, eventOptions) ⇒
Mixed
-
.find(selector) ⇒
HObject
h().find(selector) ⇒ Get the descendants of each element in the current set of matched elements, filtered by a selector.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
selector | String |
DOM selector |
HObject
h().closest(selector) ⇒ For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
selector |
String | Element
|
DOM selector or DOM element |
HObject
h().contains(selector) ⇒ Checks if any of the elements in the set contains the selected element.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
selector |
String | Element
|
DOM selector or DOM element |
HObject
h().parents(selector) ⇒ Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
selector | String |
DOM selector |
HObject
h().parent(selector) ⇒ Get the parent of each element in the current set of matched elements, optionally filtered by a selector.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
selector | String |
DOM selector |
HObject
h().children(selector) ⇒ Get the children of each element in the set of matched elements, optionally filtered by a selector.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
selector | String |
DOM selector |
HObject
h().siblings(selector) ⇒ Get the siblings of each element in the set of matched elements, optionally filtered by a selector.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
selector | String |
DOM selector |
HObject
h().first() ⇒ Reduce the set of matched elements to the first in the set.
Kind: instance method of HObject
HObject
h().last() ⇒ Reduce the set of matched elements to the last in the set.
Kind: instance method of HObject
HObject
h().prev(selector) ⇒ Get the immediately preceding sibling of each element in the set of matched elements. If a selector is provided, it retrieves the previous sibling only if it matches that selector.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
selector | String |
DOM selector |
HObject
h().next(selector) ⇒ Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
selector | String |
DOM selector |
HObject
h().prevAll(selector) ⇒ Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
selector | String |
DOM selector |
HObject
h().nextAll(selector) ⇒ Get all following siblings of each element in the set of matched elements, optionally filtered by a selector.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
selector | String |
DOM selector |
String
h().attr(name) ⇒ Get the value of an attribute for the first element in the set of matched elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
name | String |
Attribute name |
HObject
h().attr(name, value) ⇒ Set an attribute for the set of matched elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
name | String |
Attribute name |
value | String |
Attribute value |
HObject
h().removeAttr(name) ⇒ Remove an attribute from each element in the set of matched elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
name | String |
Attribute name |
HObject
h().show() ⇒ Display the matched elements.
Kind: instance method of HObject
HObject
h().hide() ⇒ Hide the matched elements.
Kind: instance method of HObject
String
h().prop(key) ⇒ Get the value of a property for the first element in the set of matched elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
key | String |
Property name |
HObject
h().prop(key, value) ⇒ Set a property for the set of matched elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
key | String |
Property name |
value | String |
Property value |
String
h().css(name) ⇒ Get the computed style properties for the first element in the set of matched elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
name | String |
Property name |
HObject
h().css(name, value) ⇒ Set a CSS property for the set of matched elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
name | String |
Property name |
value | String |
Property value |
HObject
h().css(properties) ⇒ Set one or more CSS properties for the set of matched elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
properties | Object |
Key-value pair of properties to set. |
HObject
h().append(content) ⇒ Insert content, specified by the parameter, to the end of each element in the set of matched elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
content | Mixed |
Content to insert. (either a selector, HTML content or a DOM Node) |
HObject
h().appendTo(target) ⇒ Insert every element in the set of matched elements to the end of the target(s).
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
target | Mixed |
Target of the content. (either a selector, HTML content or a DOM Node) |
HObject
h().prepend(content) ⇒ Insert content, specified by the parameter, to the beginning of each element in the set of matched elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
content | Mixed |
Content to insert. (either a selector, HTML content or a DOM Node) |
HObject
h().prependTo(target) ⇒ Insert every element in the set of matched elements to the beginning of the target(s).
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
target | Mixed |
Target of the content. (either a selector, HTML content or a DOM Node) |
HObject
h().insertBefore(target) ⇒ Insert every element in the set of matched elements before the target.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
target | String |
DOM selector |
HObject
h().before(content) ⇒ Insert content, specified by the parameter, before each element in the set of matched elements
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
content | String |
HTML code or DOM selector |
HObject
h().insertAfter(target) ⇒ Insert every element in the set of matched elements after the target.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
target | String |
DOM selector |
HObject
h().after(content) ⇒ Insert content, specified by the parameter, after each element in the set of matched elements
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
content | String |
HTML code or DOM selector |
HObject
h().wrap(element) ⇒ Wrap an HTML structure around each element in the set of matched elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
element | String |
Wrapping element. |
HObject
h().wrapInner(element) ⇒ Wrap an HTML structure around the content of each element in the set of matched elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
element | String |
Wrapping element. |
HObject
h().empty() ⇒ Remove all child nodes of the set of matched elements from the DOM.
Kind: instance method of HObject
void
h().remove() ⇒ Remove the set of matched elements from the DOM.
Kind: instance method of HObject
HObject
h().eq(index) ⇒ Reduce the set of matched elements to the one at the specified index.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
index | Number |
Index of the element |
Number
h().index() ⇒ Search for a given element from among the matched elements.
Kind: instance method of HObject
String
h().html() ⇒ Get the HTML contents of the first element in the set of matched elements.
Kind: instance method of HObject
HObject
h().html(content) ⇒ Set the HTML contents of each element in the set of matched elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
content | String |
HTML content |
String
h().text() ⇒ Get the combined text contents of each element in the set of matched elements, including their descendants.
Kind: instance method of HObject
HObject
h().text(content) ⇒ Set the content of each element in the set of matched elements to the specified text.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
content | String |
Text content |
String
h().val() ⇒ Get the current value of the first element in the set of matched elements.
Kind: instance method of HObject
HObject
h().val(value) ⇒ Set the value of each element in the set of matched elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
value | String |
Value |
HObject
h().each(function) ⇒ Iterate over a jQuery object, executing a function for each matched element.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
function | function |
Function to execute |
Node
h().get() ⇒ Retrieve the elements matched as a vanilla Array.
Kind: instance method of HObject
Node
h().get(index) ⇒ Retrieve one of the elements matched.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
index | Number |
Index of the element to return |
HObject
h().addClass(className) ⇒ Adds the specified class(es) to each element in the set of matched elements
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
className | String |
Class name |
HObject
h().removeClass(className) ⇒ Remove a single class, multiple classes, or all classes from each element in the set of matched elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
className | String |
Class name |
HObject
h().toggleClass(className) ⇒ Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the state argument.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
className | String |
Class name |
Boolean
h().hasClass(className) ⇒ Determine whether any of the matched elements are assigned the given class.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
className | String |
Class name |
HObject
h().filter(selector) ⇒ Reduce the set of matched elements to those that match the selector.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
selector | String |
DOM Selector |
HObject
h().filter(fn) ⇒ Reduce the set of matched elements to those that match the function's test.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
fn | function |
Function used as a test for each elkement in the set. |
Boolean
h().is(selector) ⇒ Check the current matched set of elements against a selector and return true if at least one of these elements matches the given arguments.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
selector | String |
DOM Selector |
Object
h().offset() ⇒ Get the current coordinates of the first element in the set of matched elements, relative to the document.
Kind: instance method of HObject
Returns: Object
- {top, left}
HObject
h().on(events, selector, callback, options) ⇒ Attach an event handler function for one or more events to the selected elements.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
events | String |
One or more space-separated event types and optional namespaces. |
selector | String |
A selector string to filter the descendants of the selected elements that trigger the event. |
callback | function |
Event Handler |
options | Object |
Additional options for .addEventListener |
HObject
h().one(events, selector, callback, options) ⇒ Attach a handler to an event for the elements. The handler is executed at most once per element per event type.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
events | String |
One or more space-separated event types and optional namespaces. |
selector | String |
A selector string to filter the descendants of the selected elements that trigger the event. |
callback | function |
Event Handler |
options | Object |
Additional options for .addEventListener |
HObject
h().off(events, callback) ⇒ Remove an event handler.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
events | String |
One or more space-separated event types and optional namespaces. |
callback | function |
Event Handler |
HObject
h().trigger(event, extraParams, eventOptions) ⇒ Execute all handlers and behaviors attached to the matched elements for the given event type.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
event | String |
Event types and optional namespaces. |
extraParams | Array.<Mixed> |
Additional parameters to pass along to the event handler. |
eventOptions | Object |
Additional event options for CustomEvent (e.g. for controlling if event bubbles...) |
Mixed
h().triggerHandler(event, extraParams, eventOptions) ⇒ Execute all handlers attached to an element for an event.
Kind: instance method of HObject
Param | Type | Description |
---|---|---|
event | String |
Event types and optional namespaces. |
extraParams | Array.<Mixed> |
Additional parameters to pass along to the event handler. |
eventOptions | Object |
Additional event options for CustomEvent (e.g. for controlling if event bubbles...) |
HObject
HBrowser ⇒ H helper functions available only for Browsers. Chainable and is similar to jQuery's $ constructor.
Kind: global variable
Param | Type | Description |
---|---|---|
parameter |
String | function
|
DOM selector, HTML code that will be used to create new elements, Function to run when document is ready. Similar to $(function(){ }) or $(document).ready(function(){ }) |
H
H helper functions available in both Servers and browsers
Kind: global variable
-
H
-
.isArray ⇒
Boolean
- .regexp
- .Error
-
.loadScript(url, [reload]) ⇒
Promise
-
.loadStylesheet(url, [reload]) ⇒
Promise
-
.isObject(variable) ⇒
Boolean
-
.delay(time) ⇒
Promise
-
.timestampMs() ⇒
Number
-
.timestamp() ⇒
Number
-
.relativeTime(timestamp) ⇒
String
-
.handlize(str) ⇒
String
- .regexpEscape()
-
.hasOwnProp(obj, key) ⇒
Bool
-
.getVariable(obj, path) ⇒
Mixed
- .setVariable(obj, path, value)
-
.escape(str) ⇒
String
-
.httpRequest(method, url, payload, headers, extras, [inFormat], [outFormat]) ⇒
Promise.<String>
-
.httpGet(url, payload, headers, extras, [inFormat], [outFormat]) ⇒
Promise.<String>
-
.httpPost(url, payload, headers, extras, [inFormat], [outFormat]) ⇒
Promise.<String>
-
.httpPut(url, payload, headers, extras, [inFormat], [outFormat]) ⇒
Promise.<String>
-
.httpDelete(url, payload, headers, extras, [inFormat], [outFormat]) ⇒
Promise.<String>
-
.isArray ⇒
Boolean
H.isArray ⇒ Checks if variable is an array (equivalent to Array.isArray)
Kind: static property of H
Param | Type | Description |
---|---|---|
variable | Mixed |
Variable to check |
H.regexp
Helper regular expressions (RegExp) handle : Valid handle (lowercase letters, numbers, underscores and dashes). email : Valid email address
Kind: static property of H
H.Error
Custom Error constructor
Kind: static property of H
Param | Type | Description |
---|---|---|
message | String |
Error message |
code | Number |
Error code. |
Promise
H.loadScript(url, [reload]) ⇒ Loads a JS script
Kind: static method of H
Returns: Promise
- Resolves when script is loaded
Param | Type | Default | Description |
---|---|---|---|
url | String |
JS script url | |
[reload] | Boolean |
false |
If set to true, the scrippt will be appended regardless if it was previously loaded or not |
Promise
H.loadStylesheet(url, [reload]) ⇒ Loads a CSS stylesheet
Kind: static method of H
Returns: Promise
- Resolves when stylesheet is loaded
Param | Type | Default | Description |
---|---|---|---|
url | String |
CSS stylesheet url | |
[reload] | Boolean |
false |
If set to true, the stylesheet will be reloaded regardless if it was previously loaded or not |
Boolean
H.isObject(variable) ⇒ Checks if variable is an object and not an array
Kind: static method of H
Param | Type | Description |
---|---|---|
variable | Mixed |
Variable to check |
Promise
H.delay(time) ⇒ Wait for a number of miliseconds
Kind: static method of H
Param | Type | Description |
---|---|---|
time | Number |
Time in miliseconds |
Number
H.timestampMs() ⇒ Returns current timestamp in miliseconds
Kind: static method of H
Returns: Number
- timestamp in miliseconds
Number
H.timestamp() ⇒ Returns current timestamp in seconds
Kind: static method of H
Returns: Number
- timestamp in seconds
String
H.relativeTime(timestamp) ⇒ Converts a timestamp into relative time. E.g. about 2 hours ago; less than a minute; in about 5 minutes
Kind: static method of H
Returns: String
- relative representation of timestamp
Param | Type | Description |
---|---|---|
timestamp |
Number | Date
|
Time |
String
H.handlize(str) ⇒ Converts a string into a handlized format (uppercase letters, numbers and dashes)
Kind: static method of H
Returns: String
- Handle
Param | Type | Description |
---|---|---|
str | String |
String to handlize |
H.regexpEscape()
Escapes a regular expression string (RegExp) s : String to escape
Kind: static method of H
Bool
H.hasOwnProp(obj, key) ⇒ Determines if an object has a property. (uses Object.prototype for security)
Kind: static method of H
Returns: Bool
- True if obj has the key property
Param | Type | Description |
---|---|---|
obj | Object |
Object to check |
key | String |
Property to check |
Mixed
H.getVariable(obj, path) ⇒ Goes through an object and returns value of a specific path (using dot notation)
Kind: static method of H
Returns: Mixed
- Value of the path element
Param | Type | Description |
---|---|---|
obj | Object |
Input object |
path | String |
Path to traverse (dot notation). e.g. parent.children.property |
H.setVariable(obj, path, value)
Goes through an object and sets the value of a specific path (using dot notation)
Kind: static method of H
Param | Type | Description |
---|---|---|
obj | Object |
Input object |
path | String |
Path to traverse (dot notation). e.g. parent.children.property |
value | Mixed |
New value to inject |
String
H.escape(str) ⇒ Escapes a string for HTML injection
Kind: static method of H
Returns: String
- Cleaned output
Param | Type | Description |
---|---|---|
str | String |
Input string |
Promise.<String>
H.httpRequest(method, url, payload, headers, extras, [inFormat], [outFormat]) ⇒ Requests an HTTP endpoint
Kind: static method of H
Returns: Promise.<String>
- Response body
Param | Type | Default | Description |
---|---|---|---|
method | String |
Method to use (GET | |
url | String |
HTTP endpoint | |
payload | Object |
Payload to inject (will be converted to query string in case of GET request otherwise, the payload is sent as a JSON body) | |
headers | Object |
Headers to inject | |
extras | Object |
extra options for the request (same as fetch API options) | |
[inFormat] | String |
"json" |
Format of the input request (json, form). |
[outFormat] | String |
"json" |
Format of the output response (json, text, buffer, stream). |
Promise.<String>
H.httpGet(url, payload, headers, extras, [inFormat], [outFormat]) ⇒ Requests a GET HTTP endpoint
Kind: static method of H
Returns: Promise.<String>
- Response body
Param | Type | Default | Description |
---|---|---|---|
url | String |
HTTP endpoint | |
payload | Object |
Payload to inject will be converted to query string | |
headers | Object |
Headers to inject | |
extras | Object |
extra options for request (same as fetch API options) | |
[inFormat] | String |
"json" |
Format of the input request (json, form). |
[outFormat] | String |
"json" |
Format of the output response (json, text, buffer, stream). |
Promise.<String>
H.httpPost(url, payload, headers, extras, [inFormat], [outFormat]) ⇒ Requests a POST HTTP endpoint
Kind: static method of H
Returns: Promise.<String>
- Response body
Param | Type | Default | Description |
---|---|---|---|
url | String |
HTTP endpoint | |
payload | Object |
Payload to inject | |
headers | Object |
Headers to inject | |
extras | Object |
extra options for request (same as fetch API options) | |
[inFormat] | String |
"json" |
Format of the input request (json, form). |
[outFormat] | String |
"json" |
Format of the output response (json, text, buffer, stream). |
Promise.<String>
H.httpPut(url, payload, headers, extras, [inFormat], [outFormat]) ⇒ Requests a PUT HTTP endpoint
Kind: static method of H
Returns: Promise.<String>
- Response body
Param | Type | Default | Description |
---|---|---|---|
url | String |
HTTP endpoint | |
payload | Object |
Payload to inject | |
headers | Object |
Headers to inject | |
extras | Object |
extra options for request (same as fetch API options) | |
[inFormat] | String |
"json" |
Format of the input request (json, form). |
[outFormat] | String |
"json" |
Format of the output response (json, text, buffer, stream). |
Promise.<String>
H.httpDelete(url, payload, headers, extras, [inFormat], [outFormat]) ⇒ Requests a DELETE HTTP endpoint
Kind: static method of H
Returns: Promise.<String>
- Response body
Param | Type | Default | Description |
---|---|---|---|
url | String |
HTTP endpoint | |
payload | Object |
Payload to inject | |
headers | Object |
Headers to inject | |
extras | Object |
extra options for request (same as fetch API options) | |
[inFormat] | String |
"json" |
Format of the input request (json, form). |
[outFormat] | String |
"json" |
Format of the output response (json, text, buffer, stream). |
HServer
H helper functions available only for Servers
Kind: global variable
-
HServer
-
.readFileBuff ⇒
Promise.<Buffer>
-
.readFile ⇒
Promise.<String>
-
.readDir ⇒
Promise.<Array>
-
.mkdir ⇒
Promise
-
.writeFile ⇒
Promise
-
.readFileSync ⇒
String
- .writeFileSync
-
.exec ⇒
Promise.<stdout, stderr>
-
.uniqueToken ⇒
String
-
.md5 ⇒
String
-
.sha1 ⇒
String
-
.sha256 ⇒
String
-
.encrypt ⇒
String
|Buffer
-
.decrypt ⇒
String
|Buffer
-
.render ⇒
Promise.<String>
-
.renderFile ⇒
Promise.<String>
-
.input ⇒
Promise.<String>
-
.waitForKey ⇒
String
- .onKeypress
- .httpServer
-
.readFileBuff ⇒
Promise.<Buffer>
H.readFileBuff ⇒ Reads a local file and returns a buffer
Kind: static property of HServer
Returns: Promise.<Buffer>
- Content of the file as a buffer
Param | Type | Description |
---|---|---|
path | String |
File path |
Promise.<String>
H.readFile ⇒ Reads a local file and returns it content as a string
Kind: static property of HServer
Returns: Promise.<String>
- Content of the file's content as a string
Param | Type | Description |
---|---|---|
path | String |
File path |
Promise.<Array>
H.readDir ⇒ Returns the list of files of a directory
Kind: static property of HServer
Returns: Promise.<Array>
- Directory list
Param | Type | Description |
---|---|---|
path | String |
Path of the directory |
Promise
H.mkdir ⇒ Creates a directory
Kind: static property of HServer
Param | Type | Description |
---|---|---|
path | String |
Path of the directory |
Promise
H.writeFile ⇒ Writes a string into a file
Kind: static property of HServer
Param | Type | Description |
---|---|---|
path | String |
File path |
content | String |
New file content |
String
H.readFileSync ⇒ Reads a local file (synchronously) and returns it content as a string
Kind: static property of HServer
Returns: String
- Content of the file's content as a string
Param | Type | Description |
---|---|---|
path | String |
File path |
H.writeFileSync
Writes a string into a file (synchronously)
Kind: static property of HServer
Param | Type | Description |
---|---|---|
path | String |
File path |
content | String |
New file content |
Promise.<stdout, stderr>
H.exec ⇒ Executes a child process
Kind: static property of HServer
Returns: Promise.<stdout, stderr>
- Returns command output (stdout & stderr)
Param | Type | Description |
---|---|---|
command | String |
Command to execute |
[options] | Object |
Options (see child_process.exec) |
String
H.uniqueToken ⇒ Returns a random unique token
Kind: static property of HServer
Returns: String
- Hexadecimal representation of token
String
H.md5 ⇒ Returns MD5 hash
Kind: static property of HServer
Returns: String
- MD5 hash of input (in HEX format)
Param | Type | Description |
---|---|---|
str |
String | Buffer
|
Input to get hash of |
String
H.sha1 ⇒ Returns SHA1 hash
Kind: static property of HServer
Returns: String
- SHA1 hash of input (in HEX format)
Param | Type | Description |
---|---|---|
str |
String | Buffer
|
Input to get hash of |
String
H.sha256 ⇒ Returns SHA256 hash
Kind: static property of HServer
Returns: String
- SHA256 hash of input (in HEX format)
Param | Type | Description |
---|---|---|
str |
String | Buffer
|
Input to get hash of |
String
| Buffer
H.encrypt ⇒ Encrypts an input string with aes-256-cbc encryption algorithm
Kind: static property of HServer
Returns: String
| Buffer
- Encrypted data
Param | Type | Default | Description |
---|---|---|---|
data |
String | Buffer
|
Input data to encrypt | |
key | String |
Encryption key in HEX format. Must be a 32byte key for a 256bit algorithm. | |
iv | String |
IV to use for the encryption in HEX format. For AES, length must be 16 | |
[algo] | String |
"aes-256-cbc" |
Encryption algorithm |
[format] | String |
"hex" |
Format of output |
Example
const key = crypto.scryptSync('Password here', 'salt', 32).toString('hex'); // 32 = 256/8
const iv = Buffer.from('d65a8b0dcbde0b76cc746faaf0b0beaa', 'hex'); // For AES, length is 16
var encryptedData = H.encrypt('My secret data', key, iv);
String
| Buffer
H.decrypt ⇒ Decrypts an input string with aes-256-cbc encryption algorithm
Kind: static property of HServer
Returns: String
| Buffer
- Encrypted data
Param | Type | Default | Description |
---|---|---|---|
data |
String | Buffer
|
Input data to decrypt | |
key | String |
Decryption key in HEX format. Must be a 32byte key for a 256bit algorithm. | |
iv | String |
IV to use for the decryption in HEX format. For AES, length must be 16 | |
[algo] | String |
"aes-256-cbc" |
Decryption algorithm |
[format] | String |
"hex" |
Format of output |
Example
const key = crypto.scryptSync('Password here', 'salt', 32).toString('hex'); // 32 = 256/8
const iv = Buffer.from('d65a8b0dcbde0b76cc746faaf0b0beaa', 'hex'); // For AES, length is 16
var originalData = H.encrypt(encryptedData, key, iv);
Promise.<String>
H.render ⇒ Renders a nunjucks/jinja template string asynchronously
Kind: static property of HServer
Returns: Promise.<String>
- Rendered template
Param | Type | Description |
---|---|---|
str | String |
Template string (jinja/nunjucks) |
data | Object |
Data to inject in template |
filters | Object |
Filters functions to allow in template |
includeCb | function |
Callback function that is called whenever the template calls the include instruction. |
Promise.<String>
H.renderFile ⇒ Renders a nunjucks/jinja template file asynchronously
Kind: static property of HServer
Returns: Promise.<String>
- Rendered template
Param | Type | Description |
---|---|---|
str | String |
Path of template file to render |
data | Object |
Data to inject in template |
filters | Object |
Filters functions to allow in template |
includeCb | function |
Callback function that is called whenever the template calls the include instruction. |
Promise.<String>
H.input ⇒ Requests input from user in command line interface
Kind: static property of HServer
Returns: Promise.<String>
- the value the user has entered
Param | Type | Default | Description |
---|---|---|---|
q | String |
Question to ask to user during input | |
[muted] | Boolean |
false |
Whether to mute the user input (for passwords) |
String
H.waitForKey ⇒ Shortcut for H.input but without the muted parameter set to true
Kind: static property of HServer
Returns: String
- the value the user has entered
Param | Type | Description |
---|---|---|
q | String |
Question to ask to user during input |
H.onKeypress
Listens to keypresses and calls callback when a key is pressed. Exists when Ctrl+C is typed
Kind: static property of HServer
Param | Type | Description |
---|---|---|
cb | function |
Callback to function that will listen to key presses |
H.httpServer
Starts an HTTP server and calls specific handlers depending on request url
Kind: static property of HServer
Param | Type | Default | Description |
---|---|---|---|
[post] | Number |
80 |
Post to listen to |
handlers |
function | Object
|
If function, it will run function and use returned Object to select handler. If Object, it will use it directly to select the handler. The handler is selected if it matches the request url. If the handler's key starts with ^, it will be considered as a REGEX. | |
options | Object |
Extra options. defaultHandler, onError, beforeHandler, pathFlags, afterHandler, autoEnd, maxPostRequestSize, |