Skip to content

Commit

Permalink
docs, refactor: update docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Apr 5, 2023
1 parent 868507e commit 3dddccc
Show file tree
Hide file tree
Showing 95 changed files with 639 additions and 655 deletions.
2 changes: 1 addition & 1 deletion docs
Submodule docs updated 2747 files
6 changes: 3 additions & 3 deletions idl/zh-cn/BlsKey.idl
Expand Up @@ -12,19 +12,19 @@ BlsKey 对象提供了一组灵活而又强大的 API,可以方便地管理 BL
```JavaScript
var crypto = require('crypto');

// 创建一个私钥
// create a private key
var privateKey = new crypto.BlsKey({
'kty': 'EC',
'crv': 'BLS12-381-G1',
'x': 'TPk62yDxSISkoSBRPYkpO%tJmm0tZd4tJeLuCKVFv4UmBPfOQ2aDWrCifANam2wj',
'd': 'zE-pf24p-l0IT_lMcrX0gStTcsx_k1f7DnJmrN8V7ZU',
});

// 签名消息
// sign a message
var message = '这是一条需要签名的消息';
var signature = privateKey.sign(message);

// 验证签名
// verify the signature
var publicKey = privateKey.publicKey;
var verify = publicKey.verify(message, signature);

Expand Down
8 changes: 4 additions & 4 deletions idl/zh-cn/BufferedStream.idl
Expand Up @@ -20,19 +20,19 @@

var filename = "test.txt";

// 打开文件
// open file
var file = fs.openFile(filename);

// 创建 BufferedStream 对象
// create BufferedStream object
var reader = new io.BufferedStream(file);

// 读取文件所有的行
// read file content
var lines = reader.readLines();

for(var i = 0; i < lines.length; i ++)
console.log(lines[i]);

// 关闭文件
// close file
file.close();
```
*/
Expand Down
6 changes: 3 additions & 3 deletions idl/zh-cn/Cipher.idl
Expand Up @@ -10,7 +10,7 @@
首先,我们需要获取一个对称加密的 Cipher 对象,如 AES 加密算法:
```JavaScript
const crypto = require('crypto');
const key = crypto.randomBytes(16); // 生成 16 字节的随机数作为 AES 密钥
const key = crypto.randomBytes(16); // generate a 16-byte random key
const cipher = new crypto.Cipher(crypto.AES, crypto.ECB, key);
```
以上代码中,我们通过 ``randomBytes`` 方法生成了 16 字节的随机数,作为 AES 密钥。然后,我们创建了一个 AES 算法的 Cipher 对象,使用 ECB 分组密码工作模式,并使用生成的随机数作为密钥。
Expand All @@ -19,7 +19,7 @@ const cipher = new crypto.Cipher(crypto.AES, crypto.ECB, key);
```JavaScript
const plaintext = 'Hello, world!';
const encrypted = cipher.encrypt(plaintext);
console.log(encrypted); // 输出加密后的结果
console.log(encrypted); // output encrypted data
```
以上代码中,我们使用 ``encrypt`` 方法对字符串 ``Hello, world!`` 进行加密,并输出加密后的结果。

Expand All @@ -32,7 +32,7 @@ console.log(encrypted); // 输出加密后的结果
```JavaScript
const decipher = new crypto.Cipher(crypto.AES, crypto.ECB, key);
const decrypted = decipher.decrypt(encrypted);
console.log(decrypted.toString()); // 输出解密后的结果
console.log(decrypted.toString()); // output decrypted data
```
在这里,我们创建了一个新的 Cipher 对象,使用相同的 key 和密码工作模式,并且使用 ``decrypt`` 方法对加密数据进行解密。解密的结果是一个 Buffer 对象,需要转换为字符串才能被正确输出。
*/
Expand Down
8 changes: 4 additions & 4 deletions idl/zh-cn/Digest.idl
Expand Up @@ -4,16 +4,16 @@

```
const crypto = require('crypto');
// 创建 SHA-512 摘要运算对象
// create a SHA-512 digest object
const digest = crypto.createHash('sha512');
// 更新数据
// update digest with data
digest.update('hello');
digest.update('world');
// 获取二进制结果
// get digest result
const result = digest.digest();
console.log(result);

// 生成指定编码的结果
// output result in hex and base64
console.log(result.toString('hex'));
console.log(result.toString('base64'));
```
Expand Down
6 changes: 3 additions & 3 deletions idl/zh-cn/ECKey.idl
Expand Up @@ -4,17 +4,17 @@
```javascript
const crypto = require("crypto");

// 生成密钥对
// generate a secp256k1 key
const key = crypto.generateKey("secp256k1");
console.log("Private key:", key.pem());
console.log("Public key:", key.publicKey.pem());

const message = "Hello, fibjs!";
// 签名消息
// sign a message
const sig = key.sign(message);
console.log("Signature:", sig.hex());

// 验证消息
// very the signature
const verify = key.verify(message, sig);
console.log("Verify result:", verify);
```
Expand Down
8 changes: 4 additions & 4 deletions idl/zh-cn/Ed25519Key.idl
Expand Up @@ -5,19 +5,19 @@
```JavaScript
const crypto = require('crypto');

// 生成 Ed25519 密钥对
// generate a ed25519 key
const key = crypto.generateKey('ed25519');

// 打印公钥、私钥等信息
// output key in PEM format
console.log('PrivateKey:', key.pem());
console.log('PublicKey:', key.publicKey.pem());

// 签名消息
// sign a message
const message = 'Hello, World!';
const sig = key.sign(message);
console.log('Sig:', sig.hex());

// 验证签名
// verify the signature
const verifyResult = key.verify(message, sig);
console.log('Verify Result:', verifyResult);
```
Expand Down
2 changes: 1 addition & 1 deletion idl/zh-cn/FSWatcher.idl
Expand Up @@ -12,7 +12,7 @@

watcher.close();

// 带回调地调用
// calling fs.watch with callback and options
fs.watch('./tmp', { encoding: 'buffer' }, (eventType, filename) => {
if (filename) {
console.log(filename);
Expand Down
20 changes: 10 additions & 10 deletions idl/zh-cn/HttpClient.idl
Expand Up @@ -103,7 +103,7 @@ interface HttpClient : object
opts 包含请求的附加选项,支持的内容如下:
```JavaScript
{
"method": "GET", //指定 http 请求方法:GET, POST
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
Expand All @@ -116,7 +116,7 @@ interface HttpClient : object
"json": {},
"pack": {},
"headers": {},
"response_body": SeekableStream // 指定接受 resposne 数据的流
"response_body": SeekableStream // specify the response.body stream
}
```
其中 body,json,pack 不得同时出现。缺省为 {},不包含任何附加信息
Expand All @@ -131,7 +131,7 @@ interface HttpClient : object
opts 包含请求的附加选项,支持的内容如下:
```JavaScript
{
"method": "GET", //指定 http 请求方法:GET, POST
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
Expand All @@ -157,7 +157,7 @@ interface HttpClient : object
opts 包含请求的附加选项,支持的内容如下:
```JavaScript
{
"method": "GET", //指定 http 请求方法:GET, POST
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
Expand All @@ -182,7 +182,7 @@ interface HttpClient : object
opts 包含请求的附加选项,支持的内容如下:
```JavaScript
{
"method": "GET", //指定 http 请求方法:GET, POST
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
Expand All @@ -208,7 +208,7 @@ interface HttpClient : object
opts 包含请求的附加选项,支持的内容如下:
```JavaScript
{
"method": "GET", //指定 http 请求方法:GET, POST
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
Expand All @@ -234,7 +234,7 @@ interface HttpClient : object
opts 包含请求的附加选项,支持的内容如下:
```JavaScript
{
"method": "GET", //指定 http 请求方法:GET, POST
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
Expand All @@ -260,7 +260,7 @@ interface HttpClient : object
opts 包含请求的附加选项,支持的内容如下:
```JavaScript
{
"method": "GET", //指定 http 请求方法:GET, POST
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
Expand All @@ -286,7 +286,7 @@ interface HttpClient : object
opts 包含请求的附加选项,支持的内容如下:
```JavaScript
{
"method": "GET", //指定 http 请求方法:GET, POST
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
Expand All @@ -312,7 +312,7 @@ interface HttpClient : object
opts 包含请求的附加选项,支持的内容如下:
```JavaScript
{
"method": "GET", //指定 http 请求方法:GET, POST
"method": "GET", // specify the http request method: GET, POST, etc, default: GET.
"protocol": "http",
"slashes": true,
"username": "",
Expand Down
28 changes: 14 additions & 14 deletions idl/zh-cn/HttpCookie.idl
Expand Up @@ -43,8 +43,8 @@ console.log(cookie);
const http = require('http');

const cookie = new http.Cookie('my_cookie_name', 'my_cookie_value');
delete cookie.server; //移除指定 httponly cookie
cookie.expires = -1; // 将过期时间改为已过期时间,浏览器会自动删除该 cookie
delete cookie.server; // delete server property
cookie.expires = -1; // set expires to -1
```

在上述示例代码中,我们使用了 delete 语句,移除了 cookie 对象的 server 属性(HTTP only cookie)。次外,为了移除最近添加的 cookie,我们将 expires 属性的值设置为 -1。
Expand All @@ -56,13 +56,13 @@ interface HttpCookie : object
opts 可以设置的选项如下:
```JavaScript
{
"name": "", // 指定创建的 cookie 名称
"value": "", // 指定创建的 cookie 值
"expires": Date, // 指定创建的 cookie 过期时间
"domain": "", // 指定创建的 cookie 的域名范围
"path": "", // 指定创建的 cookie 的路径范围
"secure": false, // 指定创建的 cookie 是否仅通过 https 传递
"httpOnly": false, // 指定创建的 cookie 仅允许 http 请求
"name": "", // specify the name of the cookie
"value": "", // specify the value of the cookie
"expires": Date, // specify the expires time of the cookie
"domain": "", // specify the domain of the cookie
"path": "", // specify the path of the cookie
"secure": false, // specify the secure of the cookie
"httpOnly": false, // specify the httpOnly of the cookie
}
```
@param opts 指定创建的 cookie 的属性
Expand All @@ -74,11 +74,11 @@ interface HttpCookie : object
opts 可以设置的选项如下:
```JavaScript
{
"expires": Date, // 指定创建的 cookie 过期时间
"domain": "", // 指定创建的 cookie 的域名范围
"path": "", // 指定创建的 cookie 的路径范围
"secure": false, // 指定创建的 cookie 是否仅通过 https 传递
"httpOnly": false, // 指定创建的 cookie 仅允许 http 请求
"expires": Date, // specify the expires time of the cookie
"domain": "", // specify the domain of the cookie
"path": "", // specify the path of the cookie
"secure": false, // specify the secure of the cookie
"httpOnly": false, // specify the httpOnly of the cookie
}
```
@param name 指定创建的 cookie 名称
Expand Down
8 changes: 4 additions & 4 deletions idl/zh-cn/HttpResponse.idl
Expand Up @@ -5,13 +5,13 @@
const http = require('http');

const server = new http.Server(8080, (request) => {
// 获取响应对象
// retreive the response object
const response = request.response;
// 设置状态码为 200
// set the status code
response.statusCode = 200;
// 设置 Content-Type 响应头
// set the content type to text/plain
response.setHeader('Content-Type', 'text/plain');
// 写入响应内容
// write the response body
response.write('ok');
});

Expand Down
4 changes: 2 additions & 2 deletions idl/zh-cn/HttpsServer.idl
Expand Up @@ -12,11 +12,11 @@
const http = require("http");
const crypto = require("crypto");

// 加载证书和密钥文件
// load cert and key
const cert = crypto.loadCert("server.crt");
const key = crypto.loadPKey("server.key");

//创建 https 服务器并启动
// create https server
const server = new http.HttpsServer(cert, key, 8443, function(req) {
resp.response.write(`Hello, Fibjs!`);
});
Expand Down
12 changes: 6 additions & 6 deletions idl/zh-cn/LruCache.idl
Expand Up @@ -4,7 +4,7 @@

```JavaScript
const util = require('util')
const c = new util.LruCache(10, 100) // 缓存最大尺寸为10,元素失效时间为100ms
const c = new util.LruCache(10, 100) // create a LruCache instance with size 10 and timeout 100ms
```

其中,set() 是设置键值对的接口:
Expand All @@ -17,13 +17,13 @@ name 参数指定要设定的键值,value 参数指定要设定的值。

`LruCache` 的 `get` 方法可以用回调函数的方式更新缓存数据:
```JavaScript
var c = new util.LruCache(10, 1000); // 最多缓存 10 个值,1 秒之后数据过期
var c = new util.LruCache(10, 1000); // create a LruCache instance with size 10 and timeout 1000ms

function get_data(name) {
// 从其他数据源查询数据,如数据库、缓存、文件等
// return data from backend
// ...
var data = {'name': name, 'value': Math.random()};
console.log('update data: ' + JSON.stringify(data)); // 记录缓存更新的信息
console.log('update data: ' + JSON.stringify(data)); // output infomation to console
return data;
}

Expand All @@ -33,8 +33,8 @@ console.log(c.get('a', get_data));
执行结果:
```sh
update data: {"name":"a","value":0.4019124971556616}
{"name":"a","value":0.4019124971556616} // 第一次查询需要缓存数据,会调用 updater 函数
{"name":"a","value":0.4019124971556616} // 第二次查询直接从缓存中获取数据,不需要再调用 updater 函数
{"name":"a","value":0.4019124971556616} // updater will be called to update cache data when cache is empty
{"name":"a","value":0.4019124971556616} // updater will not be called when cache is not empty
```

具体使用 LruCache 时,建议开发人员遵循以下的最佳实践:
Expand Down
9 changes: 2 additions & 7 deletions idl/zh-cn/MySQL.idl
Expand Up @@ -7,23 +7,18 @@ var db = require('db');

var conn = db.openMySQL('mysql://root:password@localhost/test');

// 使用 execute 方法插入数据
// call execute method to insert data
var res = conn.execute("insert into user(username, password) values ('testuser', '123456')");
console.log(res);

// 使用 execute 方法查询数据
// call execute method to query data
res = conn.execute("select * from user where username = 'testuser'");
console.log(res);

// 获取数据库表的结构信息
var fields = conn.getTableFields('user');
console.log(fields);

conn.close();
```
以上示例中,首先我们利用 db.openMySQL 方法创建一个 MySQL 的连接对象并指定连接信息。
然后我们使用 execute 方法向我们提前准备好的 user 数据表中添加一个新的用户,之后我们再调用 execute 方法查询刚刚创建的用户记录。
最后,我们利用 getTableFields 方法获取 user 数据表的列信息。
最终我们调用 close 方法关闭链接对象,并完成了我们的 MySQL 操作。
*/
interface MySQL : DbConnection
Expand Down
2 changes: 1 addition & 1 deletion idl/zh-cn/PKey.idl
Expand Up @@ -36,7 +36,7 @@ var crypto = require('crypto');

var pkey1 = crypto.PKey.from(fs.readFile('rsa.private.pem'));
var pkey2 = crypto.PKey.from(fs.readFile('rsa.public.pem'));
console.log(pkey1.equals(pkey2)); // 输出 true
console.log(pkey1.equals(pkey2)); // output: true
```
*/
interface PKey : object
Expand Down

0 comments on commit 3dddccc

Please sign in to comment.