ioBuster framework v25.02.4 documentation


utility

utility

Collection of Utility functions

Methods

(static) computeHash(algorithm, value) → {String}

Compute a digest using the specified algorithm on the specified Buffer value.

Parameters:
Name Type Description
algorithm String
value Buffer
Returns:
  • a hex string representing the output digest
Type
String

(static) copyFile(srcPath, destPathopt)

Copies a file into the log directory

Parameters:
Name Type Attributes Description
srcPath String

Absolute path to a source file.

destPath String <optional>

Destination path relative to the log root directory. The filename from the source file will be used if unspecified.

(static) createFile(filename, data)

Creates a new file.

Parameters:
Name Type Description
filename String

Filename. This is relative path to log directory.

data String | Buffer

String data to be written to a file.

(static) createSyncSandbox(func, options)

Creates SyncSandbox for using utility.sync utility.sync requires SyncSandbox

Example
utility.createSyncSandbox(() => { utility.sync('wait', () => {}) })
utility.createSyncSandbox(() => { utility.sync('wait 1', () => {}) }, { timeout: 600 })
// It will wait maximum 600 seconds until all ports join SyncSandbox.
Parameters:
Name Type Description
func function

this function will be executed in a sandbox

options Object
Properties
Name Type Attributes Default Description
period Number <optional>
5

synchronization period (s)

timeout Number <optional>
300

initialization timeout (s)

(static) deleteFile(filename)

Deletes a file.

Parameters:
Name Type Description
filename String

Filename. This is relative path to log directory.

(static) exec(command, optionsopt)

Creates a then executes the command within that shell.

Parameters:
Name Type Attributes Description
command String
options Object <optional>
Properties
Name Type Attributes Description
env Object <optional>
cwd String <optional>

(static) fetchUrl(url, optionsopt) → {HttpResponse}

Makes a request to fetch a URL.

Parameters:
Name Type Attributes Description
url String

The URL to fetch.

options Object <optional>

The optional JavaScript object specifying advanced parameters.

Properties
Name Type Attributes Default Description
method String <optional>
'GET'

The HTTP method used to make the request.

The most common methods are: GET, HEAD, POST, PUT, DELETE.

const options = { method: 'POST' }
const response = utility.fetchUrl('https://httpbin.org/anything', options)
searchParams Object <optional>

URL Search params to be added to the request URL.

// This is the same as requesting https://httpbin.org/anything?hello=world&foo=123
const options = { searchParams: { hello: 'world', foo: 123 } }
const response = utility.fetchUrl('https://httpbin.org/anything', options)
headers Object <optional>
{}

The HTTP headers to be sent.

Headers set to undefined will be omitted.

const options = {
  method: 'POST',
  headers: { contentType: 'application/json'}
}
const response = utility.fetchUrl('https://httpbin.org/anything', options)
body Buffer | String <optional>

The payload to send.

The content-length header is automatically set if the content-length and transfer-encoding headers are missing.

const buffer = utility.readFile('artifact.tgz')
const options = {
  method: 'POST',
  body: buffer
}
const response = utility.fetchUrl('https://httpbin.org/anything', options)
json Object <optional>

JSON body. If set, the content-type header defaults to application/json.

const options = {
  method: 'POST',
  json: {
     hello: 'world',
     foo: 123
  }
}
const response = utility.fetchUrl('https://httpbin.org/anything', options)
form Object <optional>

The form body is converted to a query string.

If set, the content-type header defaults to application/x-www-form-urlencoded

const options = {
  method: 'POST',
  form: {
     hello: 'world',
  }
}
const response = utility.fetchUrl('https://httpbin.org/anything', options)
basicAuth Object <optional>

Sends user credentials using the Basic authentication.

const options = {
  method: 'GET',
  basicAuth: {
     username: 'iobuster',
     password: 'super strong password',
  }
}
const response = utility.fetchUrl('https://httpbin.org/anything', options)
Properties
Name Type Attributes Description
username String <optional>

The username used for the basic authentication.

password String <optional>

The password used for the basic authentication.

https Object <optional>

This option represents the options used to make HTTPS requests.

Properties
Name Type Attributes Default Description
rejectUnauthorized Boolean <optional>
true

If true, it will throw on invalid certificates, such as expired or self-signed ones.

// Allows using a URL with invalid SSL certificate.
const options = {
  method: 'GET',
  https: {
    rejectUnauthorized: false
  }
}
const response = utility.fetchUrl('https://httpbin.org/anything', options)
Returns:
Type
HttpResponse

(static) getTaskInfo() → {utility.TaskInfo}

Returns the current task info.

Example
const taskInfo = utility.getTaskInfo()
Returns:
Type
utility.TaskInfo

(static) readFile(filepath, optionsopt) → {Buffer}

Read a file.

Parameters:
Name Type Attributes Description
filepath String

File path.

  • if baseDir is 'log', this is relative to the log directory.
  • if baseDir is 'workspace', this is relative to the current script.
options Object <optional>
Properties
Name Type Attributes Default Description
baseDir String <optional>
'log'

Selects a base directory

Returns:

file data

Type
Buffer

(static) setOutputContext(context)

Sets an output context that is visible from the coordinator.

Example
utility.setOutputContext({hello:"world"})
// outputContext is visible from the coordinator when set.
const outputContext = utility.getTaskInfo().outputContext
Parameters:
Name Type Description
context Object

(static) sleep(ms)

Adds delay in milliseconds.

Parameters:
Name Type Description
ms Number

Delay in milliseconds.

(static) sync(syncPoint, func, options)

Synchronizes all ports

Example
utility.createSyncSandbox(() => { aFunction(); utility.sync('point1', () => {}); bFunction() })
// Each port waits at "point1" until all ports reaches at "point1".
utility.createSyncSandbox(() => { utility.sync('point2', aFunction, { exclusiveCount = 1 }); bFunction() })
// Only one port can run the function "aFunction".
// Other ports will wait at "point2" until all ports reaches at "point2".
Parameters:
Name Type Description
syncPoint String

synchronization point name

func function

this function will be executed in the sync

options Object
Properties
Name Type Attributes Default Description
period Number <optional>
5

synchronization period (s)

exclusiveCount Number <optional>
0

the number of executed functions in the sync

(static) systemInfo() → {utility.SystemInfo}

Returns system information.

Returns:
Type
utility.SystemInfo

(static) writeFile(filename, data)

Writes strings to a file.

Parameters:
Name Type Description
filename String

Filename. This is relative path to log directory.

data String | Buffer

String data to be written to a file.

Type Definitions

SystemInfo

Properties:
Name Type Description
cpus Array.<Object>

List of CPUs.

hostname String

Hostname.

platform String

OS platform.

type String

OS type.

totalmem Number

Total installed memory.

uptime Number

Uptime since power on.

port Number

IO Buster port number.

totalPorts Number

Total number of IO Buster ports.

version String

IO Buster version.

buildVersion String

IO Buster build commit hash.

Type:
  • Object

TaskInfo

Properties:
Name Type Description
taskId String

Current Task ID.

entryScript String

The entry script for this task.

startTime Number

The task start time in UTC.

inputContext Object

The inputs received when starting a task.

outputContext Object

The outputs set by utility.setOutputContext.

Type:
  • Object