Deno 1.10 发布:改进内置 test runner、新增 Web Storage API

栏目: 软件资讯 · 发布时间: 5年前

内容简介:Deno 1.10 已正式发布,此版本包含许多新功能、性能优化以及错误修复。 改进内置的 test runner 在 Web Workers 中支持结构化克隆 (structured clone) 新增 Web Storage API 支持远程导入映射 (maps) 如果已经安装...

Deno 1.10 已正式发布,此版本包含许多新功能、性能优化以及错误修复。

如果已经安装了 Deno,运行deno upgrade命令即可升级到 1.10 版本。如果是首次安装,可以参考下面的方法:

# Using Shell (macOS and Linux):
curl -fsSL https://deno.land/x/install/install.sh | sh

# Using PowerShell (Windows):
iwr https://deno.land/x/install/install.ps1 -useb | iex

# Using Homebrew (macOS):
brew install deno

# Using Scoop (Windows):
scoop install deno

# Using Chocolatey (Windows):
choco install deno

改进内置的 test runner

Deno 1.10 对内置的 test runner 进行了重大改进。

  • 支持隔离和并行执行测试
  • 支持为测试用例指定可配置的确切权限
Deno.test({
  name: "write only",
  permissions: { write: true, read: false },
  async fn() {
    await Deno.writeTextFile("./foo.txt", "I can write!");
    console.log(await Deno.readTextFile("./foo.txt"));
  },
});
$ deno test --allow-read --allow-write --unstable test_permissions.ts
Check file:///Users/ry/src/deno/test_permissions.ts
running 1 test from file:///Users/ry/src/deno/test_permissions.ts
test write only ... FAILED (5ms)

failures:

write only
PermissionDenied: Requires read access to "./foo.txt", run again with the --allow-read flag
    at deno:core/core.js:86:46
    at unwrapOpResult (deno:core/core.js:106:13)
    at async open (deno:runtime/js/40_files.js:46:17)
    at async Object.readTextFile (deno:runtime/js/40_read_file.js:40:18)
    at async fn (file:///Users/ry/src/deno/test_permissions.ts:6:17)
    at async asyncOpSanitizer (deno:runtime/js/40_testing.js:21:9)
    at async resourceSanitizer (deno:runtime/js/40_testing.js:58:7)
    at async exitSanitizer (deno:runtime/js/40_testing.js:85:9)
    at async runTest (deno:runtime/js/40_testing.js:199:7)
    at async Object.runTests (deno:runtime/js/40_testing.js:244:7)

failures:

	write only

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out (37ms)
  • 更好地展示 test runner 输出内容
running 4 tests from file:///dev/deno/cli/tests/unit/tty_test.ts
test consoleSizeFile ... ok (11ms)
test consoleSizeError ... ok (4ms)
test isatty ... ok (4ms)
test isattyError ... ok (3ms)
running 6 tests from file:///dev/deno/cli/tests/unit/rename_test.ts
test renameSyncSuccess ... ok (17ms)
test renameSyncReadPerm ... ok (5ms)
test renameSyncWritePerm ... ok (6ms)
test renameSuccess ... ok (13ms)
test renameSyncErrorsUnix ... ok (34ms)
test renameSyncErrorsWin ... ignored (1ms)
...
  • 支持运行测试时监视文件更改
  • 支持在文档中进行类型检测
/**
 * ```
 * import { example } from "./test_docs.ts";
 *
 * console.assert(example() == 42);
 * ```
 */
export function example(): string {
  return "example";
}

示例如下:

$ deno test --doc https://deno.com/v1.10/test_docs.ts

Check file:///dev/test_docs.ts:2-7
error: TS2367 [ERROR]: This condition will always return 'false' since the types 'string' and 'number' have no overlap.
console.assert(example() == 42);
               ~~~~~~~~~~~~~~~
    at file:///dev/test_docs.ts:2-7.ts:3:16

新增 Web Storage API

此版本增加了对 Web Storage API 的支持。该 API 由localStoragesessionStorage组成,可以用于持久存储少量数据,而无需直接访问文件系统。开发者可以不需要申请任何权限来使用localStoragesessionStorage

底层存储层和持久性对于应用程序是不透明的,因此不用担心安全性的问题。

该 API 的工作方式与在浏览器中一样:localStorage可用于在流程重新启动时永久存储多达 5MB 的数据,而 sessionStorage可用于在流程持续时间内存储少量数据。

下面是一个简单的例子:

// kv.ts

const key = Deno.args[0];

if (key === undefined) {
  // if user passes no args, display number of entries
  console.log(localStorage.length);
} else {
  const value = Deno.args[1];

  if (value === undefined) {
    // if no value is specified, return value of the key
    console.log(localStorage.getItem(key));
  } else {
    // if value is specifed, set the value
    localStorage.setItem(key, value);
  }
}
$ deno run --location https://example.com ./kv.ts
0
$ deno run --location https://example.com ./kv.ts foo bar
$ deno run --location https://example.com ./kv.ts foo
bar
$ deno run --location https://example.com ./kv.ts
1

支持远程导入映射 (maps)

Deno 1.8 中导入映射功能已稳定,到了 Deno 1.10,目前已启用远程导入映射功能。这意味着导入映射现在不必存储在本地文件系统上,它们也可以通过 HTTP 进行加载:

$ deno install --import-map=https://example.com/import_map.json -n example https://example.com/mod.ts

详细内容查看发布公告


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

集体智慧编程

集体智慧编程

TOBY SEGARAN / 莫映、王开福 / 电子工业出版社 / 2009-1 / 59.80元

本书以机器学习与计算统计为主题背景,专门讲述如何挖掘和分析Web上的数据和资源,如何分析用户体验、市场营销、个人品味等诸多信息,并得出有用的结论,通过复杂的算法来从Web网站获取、收集并分析用户的数据和反馈信息,以便创造新的用户价值和商业价值。全书内容翔实,包括协作过滤技术(实现关联产品推荐功能)、集群数据分析(在大规模数据集中发掘相似的数据子集)、搜索引擎核心技术(爬虫、索引、查询引擎、Page......一起来看看 《集体智慧编程》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具