
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
async-protocol
Advanced tools
A nodejs implementation for the asyncProtocol (client+server side and websocket gate for browser client)
Install with npm install async-protocol
or put all files in a folder called "async-protocol", and:
var aP = require("async-protocol")
var net = require("net")
var CC_ADD = aP.registerClientCall(1, "ii", "i")
// Sum server at port 8001: a, b -> a+b
net.createServer(function (conn) {
console.log("Server: new connection")
conn = new aP(conn)
conn.on("call", function (type, data, answer) {
console.log("Server: call received")
if (type == CC_ADD)
answer(new aP.Data().addInt(data[0]+data[1]))
})
conn.on("close", function () {
console.log("Server: connection closed")
})
}).listen(8001)
// Test the server after 1s
setTimeout(function () {
console.log("Client: start test")
var conn = net.connect(8001, function () {
console.log("Client: connected")
conn = new aP(conn, true)
conn.sendCall(CC_ADD, new aP.Data().addInt(12).addInt(13), function (data) {
console.log("Client: received", data)
conn.close()
})
})
}, 1e3)
// WebSocket gate (let browser communicate with the async-server using port 8002)
aP.createGate(function () {
console.log("Gate: new connection from browser")
return net.connect(8001)
}).listen(8002)
The main object, returned by require("async-protocol")
Creates a new asyncProtocol connection, wrapping an already opened net (or tls) socket
.
isClient
is a boolean to indicate if the socket represents the client-side (true) or server-side (false, default).
Registers a valid call that the server can send to clients.
id
is a non-zero integer that identifies the call type and it will be returned, so you can use the syntax var SC_FOO = aP.registerServerCall(7)
to save the id into a constant.
argsFormat
is a string representing the type of arguments sent by the server (default: "").
returnFormat
is a string representing the type of arguments returned by the client (default: "").
exceptions
is an array with the exception types id that this call can throw (default: []).
Examples of format string are:
The types accepted by the protocol are: uint (u), int (i), float (f), token (t), string (s), Buffer (B) and boolean (b). In an array every element has the same format.
Same idea from aP.registerClientCall
, except it register a valid call that clients can send to the server.
Register a valid exception. Every call (server-originated or client-originated) can return this exception.
id
is a non-zero integer that identifies the exception type and it will be returned, so you can save it into a constant.
argsFormat
is the format of the data carried by the exception (default: "")
A boolean to tell if the connection has been closed for any reason (normal, lost connection or protocol error). Any attempt to send a call with a closed connection will throw an exception. Any attempt to answer a call from a closed connection will be silently ignored.
Send a call request to the other side.
type
is the call id (previously registered with aP.registerServerCall
or aP.registerClientCall
).
data
is a aP.Data
object, a aP.DataArray
object, string, boolean or null (default: null).
It must match the registered format for the arguments call.
onreturn
is a callback that will be called when the given call is answered by the other side (default: null).
It will be passed the a data
argument, that contains the data return by the other side.
This data
has already been extracted from the protocol formats, so uint, int and float turn into number.
See examples of accessing the values inside it:
data
: numberdata[0]
: number, data[1]
: numberdata[0]
: string, data[1]
: Array, data[1][n]
: numberdata[0]
: number, data[1]
: Array, data[1][n]
: Array, data[1][n][0]
: number, data[1][n][1]
: Array, data[1][n][1][m]
: numberonexception
is a callback that will be called when the given call is answered with an exception by the other side (default: null).
It will be passed two arguments: type
(the exception id) and data
(same idea from onreturn above).
Aside from the registered exceptions, two other exceptions exists by default (with a null data):
timeout
is the maximum time (in ms) the protocol will wait for a response, 0 means no timeout (default: 60e3).
Close the connection. All pending calls will receive a close-exception (type=-1).
Dispatched when the connection receives a valid call from the other side.
type
is the call type id.
data
contains the data sent by the other side (see the description for onreturn
parameter for sendCall
above).
answer
is a callback that must be called to answer this call. The call doesn't need to be answered right away, since the protocol is asynchronous, so the code is free to read from files, connect to databases, etc.
answer
receives one argument. It call be the return data (same idea from data
argument for sendCall
) or an aP.Exception
object.
Dispatched when the connection is closed, for any reason.
Encode data (write-only) in the protocol format and is used to send data with sendCall
or with answer
callback on "call" event.
Every method return the object itself, so you can use this syntax: var data = new aP.Data().addInt(12).addInt(13)
.
Creates a new data bundle (initialy empty).
Adds an unsigned integer (0 <= u < 2^53).
Adds a signed integer (-2^53 < u < 2^53).
Adds a float (single-presicion) value.
Adds a aP.Token
object.
Adds a string to the bundle.
Adds a Buffer to the bundle.
Adds a boolean to the bundle.
Adds a aP.DataArray
.
Appends a aP.Data
to this one
Appends a array of unsigned integers.
Appends a array of signed integers.
Appends a array of floats.
Appends a array of aP.Token
.
Appends a array of strings.
Appends a array of Buffers.
Appends a array of booleans.
Represents an array (write-only) in the sense of the protocol. Each element is a aP.Data
object.
Creates a new array with the given format
string for every element (see aP.registerServerCall
for more about format strings).
Appends a new element into the array.
Represents an exception, that will be sent as an answer to a call (see "call" event).
Creates a new exception with the given type
id and data
(default: null).
Represents a token of 16 bytes
Creates a new token from the base (another token or 16-byte buffer). Default: random token
Returns if the token is equal to another one
Creates a websocket gate. It is necessary to use the protocol from browsers. The browser connect to this gate, this gate create a connection to the real asyncProtocol server (see example at the top).
options
is an object to be passed to net.createServer() or tls.createServer(), with the additional property "secure" (a boolean)
createPair
is a function to create the connection to the asyncProtocol server and return it. Example: function () {return net.connect(8001)}
Returns the server socket, so you can call .listen()
in it
FAQs
An async, call-return-throw oriented protocol
The npm package async-protocol receives a total of 20 weekly downloads. As such, async-protocol popularity was classified as not popular.
We found that async-protocol demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.