Deno 兼容层参考
本文档详细列出 KossJS 的 Deno 兼容层(koss:deno)所有 API。
版本锚定:基于 Deno v2.0.x 实现
Builtin 标志:KOSS_BUILTIN_DENO(1 << 2)
文件位置:src/js_shims/deno_shim.js(210 行)
模块导入
// ES Module (import)
import Deno from 'koss:deno';
import { readTextFile, writeTextFile, serve } from 'koss:deno';
// CommonJS (require)
const Deno = require('koss:deno');API 列表
版本与环境
Deno.version
类型: object
{
deno: '2.0.6',
v8: '12.9',
typescript: '5.6'
}返回 Deno 版本信息对象。
console.log(Deno.version.deno); // '2.0.6'
console.log(Deno.version.typescript); // '5.6'Deno.env
类型: object
环境变量对象(映射到 process.env)。
const PATH = Deno.env.PATH;Deno.args
类型: string[]
命令行参数列表(process.argv 的切片,从第 2 个参数开始)。
console.log(Deno.args); // ['--flag', 'input.txt']Deno.pid
类型: number
当前进程 ID。
console.log(Deno.pid); // 12345Deno.noColor
类型: boolean
是否禁用彩色输出。值为 true。
if (Deno.noColor) {
console.log('No color mode');
}文件系统 API
Deno.readTextFile(path)
类型: function
参数: path: string
返回值: string(同步)
读取文本文件内容(UTF-8)。
const text = Deno.readTextFile('/tmp/hello.txt');
console.log(text);Deno.writeTextFile(path, data)
类型: function
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
path | string | 文件路径 |
data | string | Uint8Array | 要写入的数据 |
写入文本文件(同步)。
Deno.writeTextFile('/tmp/output.txt', 'Hello Deno!');Deno.readFile(path)
类型: function
参数: path: string
返回值: Uint8Array(同步)
读取文件为字节数组。
const bytes = Deno.readFile('/tmp/data.bin');
console.log(bytes.length);Deno.writeFile(path, data)
类型: function
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
path | string | 文件路径 |
data | string | Uint8Array | 要写入的数据 |
写入文件(同步)。
Deno.writeFile('/tmp/output.bin', new Uint8Array([1, 2, 3]));Deno.stat(path)
类型: function
参数: path: string
返回值: object(同步)
获取文件状态信息。
const info = Deno.stat('/tmp/test.txt');
console.log(info); // { size, mtime, ctime, isFile, isDir, isSymlink }Deno.lstat(path)
类型: function
参数: path: string
返回值: object(同步)
获取符号链接状态(与 stat 相同,不支持符号链接区别)。
const info = Deno.lstat('/tmp/link.txt');Deno.mkdir(path, options)
类型: function
参数:
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
path | string | — | 目录路径 |
options.recursive | boolean | false | 是否递归创建 |
创建目录。
Deno.mkdir('/tmp/newdir');
Deno.mkdir('/tmp/a/b/c', { recursive: true });Deno.remove(path)
类型: function
参数: path: string
删除文件或空目录。
Deno.remove('/tmp/file.txt');Deno.rename(oldPath, newPath)
类型: function
参数:
| 参数 | 类型 | 说明 |
|---|---|---|
oldPath | string | 源路径 |
newPath | string | 目标路径 |
重命名或移动文件/目录。
Deno.rename('/tmp/old.txt', '/tmp/new.txt');Deno.realPath(path)
类型: function
参数: path: string
返回值: string(同步)
解析为绝对路径。
const real = Deno.realPath('/tmp/../test.txt');
console.log(real); // '/tmp/test.txt'Deno.cwd()
类型: function
返回值: string
获取当前工作目录。
const current = Deno.cwd();
console.log(current); // 例如 '/home/user'Deno.chdir(path)
类型: function
参数: path: string
切换当前工作目录。
Deno.chdir('/tmp');
console.log(Deno.cwd()); // '/tmp'进程 API
Deno.exit(code)
类型: function
参数:
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
code | number | 0 | 退出码 |
退出当前进程。
Deno.exit(1); // 以退出码 1 退出Deno.memoryUsage()
类型: function
返回值: object
返回内存使用信息。
const mem = Deno.memoryUsage();
console.log(mem); // { rss, heapTotal, heapUsed, external }网络 API
Deno.serve(handler, options)
类型: function
参数:
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
handler | function | — | 请求处理函数(未使用) |
options.port | number | 8000 | 监听端口 |
options.hostname | string | '0.0.0.0' | 监听主机 |
启动 HTTP 服务器。
返回值:
| 属性 | 类型 | 说明 |
|---|---|---|
.port | number | 监听端口 |
.hostname | string | 监听主机 |
.close() | function | 关闭服务器 |
const server = Deno.serve((req) => new Response('Hello'), { port: 8080 });
console.log(`Server running on port ${server.port}`);
server.close();Deno.listen(options)
类型: function
参数:
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
options.port | number | 8000 | 监听端口 |
options.hostname | string | '0.0.0.0' | 监听主机 |
创建 TCP 监听器。
const listener = Deno.listen({ port: 9000 });Deno.connect(options)
类型: function
参数:
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
options.hostname | string | 'localhost' | 目标主机 |
options.port | number | 必需 | 目标端口 |
建立 TCP 连接。
const conn = Deno.connect({ hostname: 'example.com', port: 80 });Deno.resolveDns(host)
类型: function
参数: host: string
返回值: string
DNS 解析。
const ip = Deno.resolveDns('example.com');
console.log(ip); // '93.184.216.34'加密 API
Deno.crypto
类型: object
提供 Web Crypto API 风格的操作。
| API | 类型 | 说明 |
|---|---|---|
crypto.getRandomValues(arr) | function | 填充随机值 |
crypto.randomUUID() | function | 生成 UUID v4 |
crypto.subtle.digest(algorithm, data) | async function | 计算摘要 |
// 随机值
const arr = new Uint8Array(16);
Deno.crypto.getRandomValues(arr);
// UUID
const uuid = Deno.crypto.randomUUID();
// 摘要
const hash = await Deno.crypto.subtle.digest('SHA-256', 'hello');定时器 API
| API | 说明 |
|---|---|
Deno.setTimeout(cb, ms) | 设置超时 |
Deno.clearTimeout(id) | 清除超时 |
Deno.setInterval(cb, ms) | 设置间隔 |
Deno.clearInterval(id) | 清除间隔 |
const id = Deno.setTimeout(() => console.log('timeout'), 1000);
Deno.clearTimeout(id);未实现 API
以下 API 抛出明确的 NotImplementedError:
| API | 错误消息 |
|---|---|
Deno.run() | Deno.run is not implemented in KossJS |
Deno.spawn() | Deno.spawn is not implemented in KossJS |
Deno.permissions() | Deno.permissions is not implemented in KossJS (use Capability bits) |
使用示例
完整示例
import Deno from 'koss:deno';
// 文件操作
Deno.writeTextFile('/tmp/deno-test.txt', 'Hello from Deno!');
const text = Deno.readTextFile('/tmp/deno-test.txt');
console.log(text);
// 文件信息
const info = Deno.stat('/tmp/deno-test.txt');
console.log(`Size: ${info.size}`);
// 当前目录
console.log(`CWD: ${Deno.cwd()}`);
// 环境变量
console.log(`PATH: ${Deno.env.PATH}`);
// 加密
const uuid = Deno.crypto.randomUUID();
console.log(`UUID: ${uuid}`);网络服务器
import { serve } from 'koss:deno';
const server = serve(() => new Response('Hello Deno!'), {
port: 8080,
hostname: '0.0.0.0'
});
console.log(`Server running on ${server.hostname}:${server.port}`);
// 5 秒后关闭
setTimeout(() => {
server.close();
console.log('Server closed');
}, 5000);混合使用
const Deno = require('koss:deno');
const io = require('koss:io');
// Deno 风格
Deno.writeTextFile('/tmp/test.txt', 'Hello');
// Koss 原生风格
const content = io.readText('/tmp/test.txt');
console.log(content);