Deno 标准库使用教程

Deno 标准库使用教程

deno_stdDeno standard library项目地址:https://gitcode.com/gh_mirrors/de/deno_std

1. 项目的目录结构及介绍

Deno 标准库的目录结构如下:

deno_std/
├── archive/
├── bytes/
├── datetime/
├── encoding/
├── flags/
├── fs/
├── http/
├── io/
├── log/
├── mime/
├── node/
├── path/
├── permissions/
├── signal/
├── testing/
├── textproto/
├── uuid/
└── ws/

目录介绍

  • archive/: 提供压缩和解压缩功能。
  • bytes/: 提供字节操作相关的工具函数。
  • datetime/: 提供日期和时间处理功能。
  • encoding/: 提供各种编码和解码功能。
  • flags/: 提供命令行参数解析功能。
  • fs/: 提供文件系统操作功能。
  • http/: 提供HTTP服务器和客户端功能。
  • io/: 提供输入输出流操作功能。
  • log/: 提供日志记录功能。
  • mime/: 提供MIME类型处理功能。
  • node/: 提供Node.js兼容层。
  • path/: 提供路径操作功能。
  • permissions/: 提供权限管理功能。
  • signal/: 提供信号处理功能。
  • testing/: 提供测试框架和断言功能。
  • textproto/: 提供文本协议处理功能。
  • uuid/: 提供UUID生成功能。
  • ws/: 提供WebSocket服务器和客户端功能。

2. 项目的启动文件介绍

Deno 标准库没有统一的启动文件,因为它是模块化的,每个模块都可以独立使用。你可以根据需要导入和使用特定的模块。例如:

import { serve } from "https://deno.land/std@0.106.0/http/server.ts";

const server = serve({ port: 8000 });
console.log("http://localhost:8000/");

for await (const req of server) {
  req.respond({ body: "Hello World\n" });
}

3. 项目的配置文件介绍

Deno 标准库没有统一的配置文件,因为它是一个模块化的库。每个模块可以根据需要进行配置。例如,log 模块可以通过以下方式进行配置:

import { LogLevel, setup } from "https://deno.land/std@0.106.0/log/mod.ts";

await setup({
  handlers: {
    console: new handlers.ConsoleHandler("DEBUG"),
  },
  loggers: {
    default: {
      level: "DEBUG",
      handlers: ["console"],
    },
  },
});

const logger = getLogger();
logger.debug("Hello world!");

通过这种方式,你可以根据需要配置不同的模块。

deno_stdDeno standard library项目地址:https://gitcode.com/gh_mirrors/de/deno_std

使用 Deno 的 crypto 模块来进行 AES 加密和解密需要以下步骤: 1. 导入 crypto 模块和 TextEncoder、TextDecoder: ```typescript import { crypto } from "https://deno.land/std/node/crypto.ts"; const { TextEncoder, TextDecoder } = new TextEncoder(); ``` 2. 定义加密/解密的密钥和初始向量(IV): ```typescript const key = "01234567890123456789012345678901"; const iv = crypto.randomBytes(16); ``` 3. 定义要加密/解密的数据: ```typescript const data = "Hello, world!"; ``` 4. 定义 AES 加密/解密算法的参数: ```typescript const algorithm = "aes-256-cbc"; const inputEncoding = "utf8"; const outputEncoding = "hex"; ``` 5. 进行加密: ```typescript const cipher = crypto.createCipheriv(algorithm, key, iv); let encrypted = cipher.update(data, inputEncoding, outputEncoding); encrypted += cipher.final(outputEncoding); console.log("Encrypted data:", encrypted); ``` 6. 进行解密: ```typescript const decipher = crypto.createDecipheriv(algorithm, key, iv); let decrypted = decipher.update(encrypted, outputEncoding, inputEncoding); decrypted += decipher.final(inputEncoding); console.log("Decrypted data:", decrypted); ``` 完整代码示例: ```typescript import { crypto } from "https://deno.land/std/node/crypto.ts"; const { TextEncoder, TextDecoder } = new TextEncoder(); const key = "01234567890123456789012345678901"; const iv = crypto.randomBytes(16); const data = "Hello, world!"; const algorithm = "aes-256-cbc"; const inputEncoding = "utf8"; const outputEncoding = "hex"; const cipher = crypto.createCipheriv(algorithm, key, iv); let encrypted = cipher.update(data, inputEncoding, outputEncoding); encrypted += cipher.final(outputEncoding); console.log("Encrypted data:", encrypted); const decipher = crypto.createDecipheriv(algorithm, key, iv); let decrypted = decipher.update(encrypted, outputEncoding, inputEncoding); decrypted += decipher.final(inputEncoding); console.log("Decrypted data:", decrypted); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周忻娥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值