Functions to generate messages with JSON-RPC 2.0 format.
CAF uses a subset of this spec and, for example, RPC arguments are never passed by name, using instead an array.
CAF always adds a first argument to requests/notifications containing metadata:
{
"token": string, // security token for authentication
"sessionId": string,// logical session name
"to": string, // target CA
"from": string // source CA
}
We also add the same metadata to replies, but the json-rpc
reply
message format complicates things:
-
Application-level errors: Return an array with 3 elements
[meta, error, data]
with theerror
term using a falsy if everything went fine. We never use the JSON-RPC error response object for propagating application errors. -
System-level errors: Use the error response object with
exports.ERROR_CODES
. In that case we use an array in the data field to add the metadata, i.e.,{ "error": {"data": [meta, extraData]}}
.
It is recommended to use the getters and setters in this library to hide this complexity.
- Source:
Members
ACCOUNTS_CA_LENGTH :number
- Source:
Length of prefix in user characters to compute the accounts
CA name.
Type:
- number
DEFAULT_FROM :string
- Source:
Default source of an external request.
Type:
- string
DEFAULT_FROM_ID :string
- Source:
Default ID in requests that come from entities that have no proper id.
Type:
- string
DEFAULT_FROM_USERNAME :string
- Source:
Default username when user is unknown.
Type:
- string
DEFAULT_QUOTA_ID :string
- Source:
Default ID targeting a quota service.
Type:
- string
DEFAULT_REQUEST_ID :string
- Source:
Default id for a response to an invalid request with no id.
Type:
- string
DEFAULT_SESSION :string
- Source:
Default external session.
Type:
- string
DUMMY_TOKEN :string
- Source:
Default token with no authentication.
Type:
- string
ERROR_CODES :Object.<string, number>
- Source:
Enum with error codes.
Type:
- Object.<string, number>
SYSTEM_FROM :string
- Source:
Reserved username_fromid for internal, local sessions.
Type:
- string
SYSTEM_FROM_ID :string
- Source:
Reserved from id for internal, local sessions.
Type:
- string
SYSTEM_SESSION_ID :string
- Source:
Session id for internal sessions. We use the DEFAULT_SESSION.
Type:
- string
SYSTEM_TOKEN :string
- Source:
Reserved token for internal, local sessions.
Type:
- string
SYSTEM_USERNAME :string
- Source:
Reserved username for internal, local sessions.
Type:
- string
Methods
accountFrom(caOwner) → {string}
- Source:
Generate a from
field to access the accounts
service.
Parameters:
Name | Type | Description |
---|---|---|
caOwner |
string | The target CA owner. |
Returns:
A from
field for an accounts service request.
- Type
- string
accountsURL(msg) → {string|null}
- Source:
Extracts the URL of a service for user authenticaton
from a notAuthenticated
error message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A |
Returns:
A url for an authentication service or null if not a valid redirection message.
- Type
- string | null
call(msg, target, cb) → {Promise}
- Source:
Executes an asynchronous method in a target CA using arguments in an RPC request message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
target |
Object | Target CA. |
cb |
cbType | Returns in first argument an optional error of type
|
Returns:
An optional promise to enable async
methods.
- Type
- Promise
getAppReplyData(msg) → {jsonType|undefined}
- Source:
Gets data field from application reply message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
Data in message.
- Type
- jsonType | undefined
getAppReplyError(msg) → {jsonType|undefined}
- Source:
Gets error field from application reply message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
Error in message.
- Type
- jsonType | undefined
getFrom(msg) → {string|undefined}
- Source:
Gets source CA from meta-data in message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
From field in message.
- Type
- string | undefined
getMeta(msg) → {caf.meta}
- Source:
Gets meta-data from message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message |
Throws:
-
when message invalid.
- Type
- Error
Returns:
Metadata in the message.
- Type
- caf.meta
getMethodArgs(msg) → {Array.<jsonType>}
- Source:
Gets original method arguments from message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message |
Throws:
-
when invalid message.
- Type
- Error
Returns:
An array with method arguments.
- Type
- Array.<jsonType>
getMethodName(msg) → {string}
- Source:
Gets the method name from message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Throws:
-
when message is invalid.
- Type
- Error
Returns:
The name of the method.
- Type
- string
getSessionId(msg) → {string|undefined}
- Source:
Gets session id from meta-data in message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
Session id in message.
- Type
- string | undefined
getSystemErrorCode(msg) → {number|undefined}
- Source:
Gets system error code from message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
System error code in message.
- Type
- number | undefined
getSystemErrorData(msg) → {caf.json|undefined}
- Source:
Gets system error data from message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
System error data in message.
- Type
- caf.json | undefined
getSystemErrorMsg(msg) → {string|undefined}
- Source:
Gets system error msg from message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
System error message in message.
- Type
- string | undefined
getTo(msg) → {string|undefined}
- Source:
Gets target CA from meta-data in message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
Target CA in message.
- Type
- string | undefined
getToken(msg) → {string|undefined}
- Source:
Gets token from meta-data in message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
Token in message.
- Type
- string | undefined
isAppReply(msg) → {boolean}
- Source:
Tests if it is an application reply message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
True if it is an application reply message.
- Type
- boolean
isErrorRecoverable(msg) → {boolean}
- Source:
Checks if it there is a recoverable error in message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
True if it is a recoverable error.
- Type
- boolean
isNotAuthenticated(msg) → {boolean}
- Source:
Checks if it is a "principal not authenticated" message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
True if it is a notAuthenticated
message
- Type
- boolean
isNotAuthorized(msg) → {boolean}
- Source:
Checks if it is a not authorized
message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
True if it is a not authorized
message
- Type
- boolean
isNotification(msg) → {boolean}
- Source:
Tests if it is a notification message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
True if it is a notification message.
- Type
- boolean
isQuotaExceeded(msg) → {boolean}
- Source:
Checks if it is a quota exceeded
message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
True if it is a quota exceeded
message
- Type
- boolean
isRedirect(msg) → {boolean}
- Source:
Tests if it is a redirect message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
True if it is a redirect message.
- Type
- boolean
isRequest(msg) → {boolean}
- Source:
Tests if it is a request message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message |
Returns:
True if it is a request message.
- Type
- boolean
isSystemError(msg) → {boolean}
- Source:
Tests if it is a system error message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Returns:
True if it is a system error message.
- Type
- boolean
joinName(…strings) → {string}
- Source:
Joins partial names using the standard separator
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
strings |
string |
<repeatable> |
A var number of strings. |
Returns:
A joined name with the standard separator.
- Type
- string
joinNameArray(args, separatoropt) → {string}
- Source:
Joins partial names using a separator
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
args |
Array.<string> | A var number of strings in an array. |
|
separator |
string |
<optional> |
Optional separator to override '-'. |
Returns:
A joined name with the standard separator.
- Type
- string
metaFreeze(msg)
- Source:
Freezes meta-data in message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
Throws:
-
if msg is not a proper
msgType
type. - Type
- Error
newAppError() → {caf.error}
- Source:
Wraps an Error object of type AppError:
{name: 'AppError', msg: msgType, errorStr: string, error: Error}
Returns:
- Type
- caf.error
newSysError() → {caf.error}
- Source:
Wraps an Error object of type SystemError:
{name: 'SystemError', msg: msgType, code: number, errorStr: string, error: Error}
Returns:
- Type
- caf.error
notification(to, from, sessionId, methodName, …var_args) → {msgType}
- Source:
Creates notification message.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
to |
string | ||
from |
string | ||
sessionId |
string | ||
methodName |
string | ||
var_args |
any |
<repeatable> |
Returns:
- Type
- msgType
patchMeta(msg, data)
- Source:
Patches meta-data in message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
data |
Object | Metadata object to be merged-in. |
Throws:
-
when message invalid.
- Type
- Error
randomId() → {string}
- Source:
Generate a random string.
Returns:
- Type
- string
redirect(request, errMsg, errOrg) → {msgType}
- Source:
Creates a redirect message.
Parameters:
Name | Type | Description |
---|---|---|
request |
msgType | |
errMsg |
string | |
errOrg |
Error |
Returns:
- Type
- msgType
redirectDestination(msg) → {string|null}
- Source:
Extracts the destination address of a redirection message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A redirection message. |
Returns:
A redirection address or null if not a valid redirection message.
- Type
- string | null
reply(error, request, value) → {msgType}
- Source:
Creates a reply message.
Parameters:
Name | Type | Description |
---|---|---|
error |
caf.err | |
request |
msgType | |
value |
caf.json |
Returns:
- Type
- msgType
request(token, to, from, sessionId, methodName, …var_args) → {msgType}
- Source:
Creates a request message.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
token |
string | ||
to |
string | ||
from |
string | ||
sessionId |
string | ||
methodName |
string | ||
var_args |
any |
<repeatable> |
Returns:
- Type
- msgType
setFrom(msg, from)
- Source:
Sets source CA in message meta-data.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
from |
string |
|
setMeta(msg, meta)
- Source:
Sets meta-data in message.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
meta |
caf.meta | Metadata to set. |
Throws:
-
When message invalid.
- Type
- Error
setSessionId(msg, sessionId)
- Source:
Sets session id in message meta-data.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
sessionId |
string | Session id to set. |
setTo(msg, to)
- Source:
Sets target CA in message meta-data.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
to |
string |
|
setToken(msg, token)
- Source:
Sets token in message meta-data.
Parameters:
Name | Type | Description |
---|---|---|
msg |
msgType | A message. |
token |
string | Token to set in message. |
splitName(name, separatoropt) → {Array.<string>}
- Source:
Splits a compound name into namespace root and local name. The convention is to use the character '-' to separate them.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
string | A name to split. |
|
separator |
string |
<optional> |
Optional separator to override '-'. |
Throws:
-
Invalid compound name.
- Type
- Error
Returns:
An array with two elements: namespace root and
local name, or three if it also has a map name, or four if fully
qualified CA name ,i.e., appPublisher-appLocalName-caOwner-caLocalName
.
- Type
- Array.<string>
systemRequest(to, methodName, …var_args) → {msgType}
- Source:
Creates a system request message.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
to |
string | ||
methodName |
string | ||
var_args |
any |
<repeatable> |
Returns:
- Type
- msgType