Skip to content

Commit a00990e

Browse files
committed
Add docs: js Spawn and IPC interface
1 parent d18453e commit a00990e

File tree

3 files changed

+340
-0
lines changed

3 files changed

+340
-0
lines changed

website/docs/script/js/js-api-ipc.mdx

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
---
2+
id: js-api-ipc
3+
title: "API (IPC)"
4+
---
5+
6+
import Tabs from "@theme/Tabs";
7+
import TabItem from "@theme/TabItem";
8+
import Link from "@docusaurus/Link";
9+
import TutorialHeader from "@components/TutorialHeader";
10+
import Tooltip from "@components/Tooltip";
11+
import ScriptTools from "./../_ScriptTools.mdx";
12+
13+
# IPC
14+
15+
As mentioned in the previous chapter on [JS Syscalls](./js-api-syscalls), `ckb-js-vm` supports [Spawn](../spawn-cross-script-calling). Building on top of Spawn, we implemented an additional abstraction layer called [IPC](../ckb-ipc), which also provides a similar set of APIs for use in JavaScript.
16+
17+
(While using JavaScript as a server is technically supported, the overhead of `ckb-js-vm` is significant, so we do **not** recommend doing so.)
18+
19+
## `spawnCellServer`
20+
21+
Spawns a new IPC server process using the provided code hash and hash type.
22+
23+
### Syntax
24+
25+
```typescript
26+
export function spawnCellServer(
27+
codeHash: ArrayBuffer,
28+
hashType: number,
29+
argv: string[]
30+
): [Pipe, Pipe];
31+
```
32+
33+
### Parameters
34+
35+
- `codeHash`: The code hash to search for (must be 32 bytes)
36+
- `hashType`: The type of hash to search for (type or data)
37+
- `argv`: An array of strings representing the arguments to pass to the new process.
38+
39+
### Return
40+
41+
A tuple of two `Pipe` objects representing the read and write pipes for the parent process, or throws an `Error` if an error occurs.
42+
43+
## spawnServer
44+
45+
Same to spawnCellServer, but spawn from a specified source
46+
47+
### Syntax
48+
49+
```typescript
50+
export function spawnServer(
51+
index: number,
52+
source: SourceType,
53+
argv: string[],
54+
): [Pipe, Pipe] {
55+
```
56+
57+
### Parameters
58+
59+
- `index` : The index of the source to spawn from
60+
- `source` : The type of source (use SOURCE\_\* constants)
61+
- `argv`: An array of strings representing the arguments to pass to the new process.
62+
63+
### Return
64+
65+
A tuple of two `Pipe` objects representing the read and write pipes for the parent process, or throws an `Error` if an error occurs.
66+
67+
## runServer
68+
69+
Runs the server with the provided service implementation.
70+
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.
71+
72+
### Syntax
73+
74+
```typescript
75+
export function runServer(handler: RequestHandler): void;
76+
```
77+
78+
### Parameters
79+
80+
`handler` : The service implementation that handles the requests and generates the responses.
81+
82+
### Return
83+
84+
Never returns unless an error occurs, in which case it throws an `Error`.
85+
86+
## Channel
87+
88+
The `Channel` class facilitates IPC communication between a client and a server.
89+
It handles the transmission of requests from the client to the server and the reception of responses from the server to the client.
90+
91+
## Channel.constructor
92+
93+
Creates a new Channel instance
94+
95+
### Syntax
96+
97+
```typescript
98+
constructor(reader: Read, writer: Write);
99+
```
100+
101+
### Parameters
102+
103+
- `reader` : Responsible for reading data from the channel
104+
- `writer` : Responsible for writing data to the channel
105+
106+
## Channel.execute
107+
108+
Executes the server loop, processing incoming requests and sending responses.
109+
110+
### Syntax
111+
112+
```typescript
113+
execute(handler: RequestHandler): void
114+
```
115+
116+
### Parameters
117+
118+
`handler` : A service that handles requests and generates responses
119+
120+
## Channel.call
121+
122+
Sends a request to the server and waits for a response.
123+
124+
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.
125+
126+
### Syntax
127+
128+
```typescript
129+
call(req: RequestPacket): ResponsePacket
130+
```
131+
132+
### Parameters
133+
134+
- `req` : The request packet to send to the server
135+
136+
### Return
137+
138+
The response packet received from the server
139+
140+
## Channel.sendErrorCode
141+
142+
Sends an error code to the client
143+
144+
### Syntax
145+
146+
```typescript
147+
sendErrorCode(errorCode: number): void
148+
```
149+
150+
### Parameters
151+
152+
- `errorCode` - The error code to send
153+
154+
## RequestHandler
155+
156+
Interface for a service that can handle requests and generate responses
157+
158+
### Syntax
159+
160+
```typescript
161+
export interface RequestHandler {
162+
serve(req: RequestPacket): ResponsePacket;
163+
}
164+
```

website/docs/script/js/js-api-syscalls.mdx

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,178 @@ The tests in `tests/src/type-id.test.ts`.
283283
When minting, You need to create type-id through `inputCell` and `index` and check it in the contract. `check type-id minting success` and `check type-id minting failed`.
284284

285285
When transaction, Will not check again. `check type-id transaction success`.
286+
287+
## spawnCell
288+
289+
Spawn a new process from a cell.
290+
291+
### Syntax
292+
293+
```typescript
294+
export function spawnCell(
295+
codeHash: ArrayBuffer,
296+
hashType: number,
297+
offset: number,
298+
length: number,
299+
args: SpawnArgs
300+
): number;
301+
```
302+
303+
### Parameters
304+
305+
- `codeHash` : The code hash of the cell to spawn
306+
- `hashType` : The hash type of the code (use SCRIPT*HASH_TYPE*\* constants)
307+
- `offset` : The offset in the cell data (defaults to 0)
308+
- `length` : The length of code to execute (defaults to reading until the end)
309+
- `args` : Spawn arguments including argv and inherited file descriptors
310+
311+
### Return
312+
313+
The process ID of the spawned process
314+
315+
## spawn
316+
317+
Spawn a new process from a specified source.
318+
319+
### Syntax
320+
321+
```typescript
322+
export function spawn(
323+
index: number,
324+
source: SourceType,
325+
offset: number,
326+
length: number,
327+
args: SpawnArgs
328+
): number;
329+
```
330+
331+
### Parameters
332+
333+
- `index` : The index of the source to spawn from
334+
- `source` : The type of source (use SOURCE\_\* constants)
335+
- `offset` : The offset in the source data (defaults to 0)
336+
- `length` : The length of code to execute (defaults to reading until the end)
337+
- `args` : Spawn arguments including argv and inherited file descriptors
338+
339+
### Return
340+
341+
The process ID of the spawned process
342+
343+
## pipe
344+
345+
Create a new pipe
346+
347+
### Syntax
348+
349+
```typescript
350+
export function pipe(): [number, number];
351+
```
352+
353+
### Return
354+
355+
A tuple of [read_fd, write_fd] for the pipe.
356+
357+
## inheritedFds
358+
359+
Get inherited file descriptors
360+
361+
### Syntax
362+
363+
```typescript
364+
export function inheritedFds(): number[];
365+
```
366+
367+
### Return
368+
369+
Array of inherited file descriptor numbers.
370+
371+
## read
372+
373+
Read from a file descriptor
374+
375+
### Syntax
376+
377+
```typescript
378+
export function read(fd: number, length: number): ArrayBuffer;
379+
```
380+
381+
### Parameters
382+
383+
- `fd` : The file descriptor to read from
384+
- `length` : Number of bytes to read
385+
386+
### Return
387+
388+
The read data as ArrayBuffer.
389+
390+
### Remarks
391+
392+
The length of returned data might be smaller than `length`.
393+
394+
## write
395+
396+
Write data to a file descriptor
397+
398+
### Syntax
399+
400+
```typescript
401+
export function write(fd: number, data: ArrayBuffer): void;
402+
```
403+
404+
### Parameters
405+
406+
- `fd` : The file descriptor to write to
407+
- `data` : The data to write as ArrayBuffer
408+
409+
### Remarks
410+
411+
All data will be written atomically - no partial writes will occur
412+
413+
- - If the write cannot complete fully, it will throw an error
414+
- - The function blocks until all data is written
415+
416+
## close
417+
418+
Close a file descriptor
419+
420+
### Syntax
421+
422+
```typescript
423+
export function close(fd: number): void;
424+
```
425+
426+
### Parameters
427+
428+
- `fd` : The file descriptor to close
429+
430+
## wait
431+
432+
Wait for a process to exit
433+
434+
### Syntax
435+
436+
```typescript
437+
export function wait(pid: number): number;
438+
```
439+
440+
### Parameters
441+
442+
- `pid` : The process ID to wait for
443+
444+
### Return
445+
446+
The exit status of the process
447+
448+
## processId
449+
450+
Get the current process ID
451+
452+
### Syntax
453+
454+
```typescript
455+
export function processId(): number;
456+
```
457+
458+
### Return
459+
460+
The current process ID

website/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export default {
163163
"script/js/js-api-common-algorithms",
164164
"script/js/js-api-molecule",
165165
"script/js/js-api-other-utilities",
166+
"script/js/js-api-ipc",
166167
],
167168
},
168169
],

0 commit comments

Comments
 (0)