IPC
As mentioned in the previous chapter on JS Syscalls, ckb-js-vm
supports Spawn. Building on top of Spawn, we implemented an additional abstraction layer called IPC, which also provides a similar set of APIs for use in JavaScript.
Although using JavaScript as a server is technically supported, the overhead of ckb-js-vm
is significant, so it is NOT recommended.
spawnCellServer
Spawns a new IPC server process using the provided code hash and hash type.
export function spawnCellServer(
codeHash: ArrayBuffer,
hashType: number,
argv: string[]
): [Pipe, Pipe];
Parameters
codeHash
: The code hash to search for (must be 32 bytes)hashType
: The type of hash to search for (type or data)argv
: An array of strings representing the arguments to pass to the new process.
Return
[Pipe, Pipe]
: A tuple ofPipe
objects representing the read and write pipes for the parent process.
Error
- Throws on system errors.
spawnServer
Similar to spawnCellServer
, but spawns a process from a specified source.
export function spawnServer(
index: number,
source: SourceType,
argv: string[]
): [Pipe, Pipe] {}
Parameters
index
: The index of the source to spawn fromsource
: The type of source (use SOURCE_* constants)argv
: An array of strings representing the arguments to pass to the new process.
Return
[Pipe, Pipe]
: A tuple ofPipe
objects representing the read and write pipes for the parent process.
Error
- Throws on system errors.
runServer
Runs the server with the provided service implementation. This function listens for incoming requests, processes them using the provided service, and sends back the responses. It uses the inherited file descriptors for communication.
export function runServer(handler: RequestHandler): void;
Parameters
handler
: The service implementation that handles the requests and generates the responses.
Error
- Throws on system errors.
Channel
The Channel
class facilitates IPC communication between a client and a server.
It handles the transmission of requests from the client to the server and the reception of responses from the server to the client.
Channel.constructor
Creates a new Channel
instance
constructor(reader: Read, writer: Write);
Parameters
reader
: Responsible for reading data from the channelwriter
: Responsible for writing data to the channel
Channel.execute
Executes the server loop, processing incoming requests and sending responses.
execute(handler: RequestHandler): void
Parameters
handler
: A service that handles requests and generates responses
Channel.call
Sends a request to the server and waits for the response.
This method handles the complete request-response cycle by sending the request to the server and then waiting for and returning the server's response.
call(req: RequestPacket): ResponsePacket
Parameters
req
: The request packet to send to the server
Return
ResponsePacket
: The response packet received from the server
Channel.sendErrorCode
Sends an error code to the client
sendErrorCode(errorCode: number): void
Parameters
errorCode
- The error code to send
RequestHandler
Interface for a service that can handle requests and generate responses
export interface RequestHandler {
serve(req: RequestPacket): ResponsePacket;
}