node deno_Deno简介:安全JavaScript和TypeScript运行时

严升
2023-12-01

node deno

什么是迪诺? (What Is Deno?)

Deno is a JavaScript and TypeScript runtime that lets you write programs in either languages and execute them from the command line

Deno是一个JavaScript和TypeScript运行时 ,可让您 用两种语言编写程序并从命令行执行它们

Deno programs can access resources on the host computer, such as the filesystem and environment variables.

Deno程序可以访问主机上的资源,例如文件系统和环境变量

什么是运行时系统? (What is a runtime system?)

As for Deno, we can say that’s what makes Javascript run outside the browser, adding a series of features that it is not possible to find in the Javascript engine itself.

至于Deno,我们可以说这就是Javascript在浏览器之外运行的原因,它增加了Javascript引擎本身无法找到的一系列功能。

Deno is the brainchild of Node’s creator, Ryan Dahl, who created Deno to address what he sees as the design flaws in Node.

Deno是Node的创建者Ryan Dahl的创意,他创建了Deno来解决他认为Node的设计缺陷

The goal is to provide a secure scripting environment that treats TypeScript as a first-class language, and that is as browser-compatible as possible (where practical).

目标是提供一个安全的脚本环境 ,将TypeScript视为一流的语言,并尽可能与浏览器兼容(在可行的情况下)。

安全功能 (Security Features)

Deno is designed to be secure out of the box

Deno设计为开箱即用

All code is executed in a secure sandbox by default, which means you need to give explicit permission to allow a program to access the filesystem or the network.

默认情况下所有代码都在安全的沙箱中执行 ,这意味着您需要授予显式权限,以允许程序访问文件系统或网络。

Programs can be granted permissions with the following command-line flags:

可以使用以下命令行标志为程序授予权限:

  • -A, –allow-all: allow all permissions (disables all security).

    -A,–allow-all :允许所有权限(禁用所有安全性)。

  • –allow-env: allow getting and setting of environment variables.

    –allow-env :允许获取和设置环境变量。

  • –allow-hrtime: allow high resolution time measurement (can be used in timing attacks and fingerprinting).

    –allow-hrtime :允许高分辨率时间测量(可用于计时攻击和指纹识别)。

  • –allow-net=\: allow network access. Optionally takes a comma-separated whitelist of domains.

    –allow-net = \ :允许网络访问。 (可选)以逗号分隔的域白名单。

  • –allow-plugin: allow loading plugins (unstable feature).

    –allow-plugin :允许加载插件(不稳定的功能)。

  • –allow-read=\: allow file system read access. Optionally takes a comma-separated whitelist of directories or files.

    –allow-read = \ :允许文件系统读取访问。 (可选)以逗号分隔的目录或文件白名单。

  • –allow-run: allow running subprocesses.

    –allow-run :允许运行子进程。

  • –allow-write=\: allow file system write access. Optionally takes a comma-separated whitelist of directories or files.

    –allow-write = \ :允许文件系统写访问。 (可选)采用逗号分隔的目录或文件白名单。

一流的TypeScript支持 (First-class TypeScript Support)

Deno can execute both JavaScript and TypeScript.

Deno可以执行JavaScript TypeScript。

The cool stuff is that Typescript is supported as a first-class language like Javascript and you can load and run your Typescript code without any additional build steps that transpile your code into JavaScript first.

很棒的东西是Typescript是像Javascript这样的一流语言而受支持的, 您可以加载和运行Typescript代码,而无需任何先将代码转换为JavaScript的附加构建步骤

使用外部代码 (Using External Code)

As Ryan mentioned in his talk

正如瑞安在讲话中提到的

One of his goals for Deno was to avoid the need for a package manager

他对Deno的目标之一是避免需要包装经理

Unlike with runtimes/languages such as Node.js and PHP (which use the npm and composer package managers respectively), there’s no package manager for Deno.

与诸如Node.js和PHP(分别使用npm和composer软件包管理器)之类的运行时/语言不同, Deno 没有软件包管理器

Instead, external packages are imported directly via a URL:

相反, 外部软件包是通过URL直接导入的

import { Client } from "https://deno.land/x/mysql@2.2.0/mod.ts";

The first time you run your script, Deno will fetch, compile, and cache all the imports so that subsequent starts are lightning fast.

首次运行脚本时,Deno将获取,编译和缓存所有导入,以便随后的启动很快。

Obviously there are times when you may want to force it to re-fetch the imports, and you can do this with the cache subcommand:

显然,有时您可能要强制它重新获取导入,并且可以使用cache子命令执行此操作:

deno cache --reload my_module.ts

套餐托管 (Package hosting)

While Deno doesn’t provide a package registry as such, there’s a list of third-party modules available.

尽管Deno并未提供软件包注册中心,但仍有可用的第三方模块列表

The service provides a standardized, versioned URL that maps to the module’s GitHub repo.

该服务提供了一个标准化的版本化URL,该URL映射到该模块的GitHub存储库。

You can search for packages by name and see a brief description, and click through to see the package readme.

您可以按名称搜索软件包并查看简要说明,然后单击以查看软件包自述文件。

标准图书馆 (The Standard Library)

Deno provides a standard library — loosely based on Golang’s — which provides a set of high-quality standard modules with no external dependencies

Deno提供了一个标准库 (大致基于Golang的库),提供了一组高质量的标准模块,没有外部依赖

The packages in the standard library are not installed along with Deno.

标准库中的软件包未与Deno一起安装。

Rather, they’re available online and linked to as we saw in the previous section.

相反,它们可以在线使用,并已链接到上一节中所看到的。

The modules are versioned, allowing you to pin your code to the usage of a specific version:

这些模块是版本控制的,允许您将代码固定到特定版本的用法:

import { copy } from "https://deno.land/std@0.50.0/fs/copy.ts";

This means that any code you write that relies on a module from the standard library should continue to work in future versions.

这意味着您编写的任何依赖于标准库中模块的代码都应在以后的版本中继续使用。

The library includes various helpers and utilities you might need for building both command-line and HTTP-based applications:

该库包含构建命令行和基于HTTP的应用程序可能需要的各种帮助程序和实用程序:

  • archive: modules to work with tar files

    存档 :用于tar文件的模块

  • async: async utilities

    异步 :异步实用程序

  • bytes: helpers for working with binary arrays

    字节:使用二进制数组的助手

  • datetime: a helper for parsing date strings into Date objects

    datetime :将日期字符串解析为Date对象的助手

  • encoding: encoders for dealing with base32, binary, CSV, TOML, and YAML formats

    编码 :用于处理base32,二进制,CSV,TOML和YAML格式的编码

  • flags: a command line arguments parser

    标志 :命令行参数解析器

  • fmt: a tool for printing formatted output

    fmt :用于打印格式化输出的工具

  • fs: helpers for working with the filesystem

    fs :用于处理文件系统的助手

  • hash: a module for creating hashes using a variety of algorithms

    hash :使用多种算法创建哈希的模块

  • http: create HTTP and file servers, and manipulate cookies

    http:创建HTTP和文件服务器,并操作cookie

  • io: utilities for string input/output

    io :用于字符串输入/输出的实用程序

  • log: simple logging module

    log :简单的日志记录模块

  • mime: provides support for multipart data

    mime:支持多部分数据

  • node: a (currently in-progress) compatibility layer for Node.js code

    node :Node.js代码的(当前正在进行中)兼容性层

  • path: path manipulation utility

    path :路径操作实用程序

  • permissions: helpers to check and prompt for security permissions

    权限 :帮助人员检查并提示输入安全权限

  • signal: helpers for handling Deno process signals

    signal :处理Deno过程信号的助手

  • testing: test assertions for using with Deno’s built-in test runner

    测试:与Deno的内置测试运行器一起使用的测试断言

  • uuid: utilities for generating and validating UUIDs

    uuid :用于生成和验证UUID的实用程序

  • ws: helpers for creating WebSocket clients and servers

    ws :用于创建WebSocket客户端和服务器的助手

安装Deno (Installing Deno)

Deno ships as a single executable with no dependencies.

Deno作为没有依赖性的单个可执行文件提供。

You can download a binary from the releases page, or install it using the installers below:

您可以从发布页面下载二进制文件,或使用以下安装程序进行安装:

Shell (macOS, Linux)

Shell(macOS,Linux)

curl -fsSL https://deno.land/x/install/install.sh |  sh

PowerShell (Windows)

PowerShell(Windows)

iwr https://deno.land/x/install/install.ps1 -useb | iex

Homebrew (macOS)

自制软件 (macOS)

brew install deno

升级中 (Upgrading)

Deno can be upgraded to the latest release using the following command:

可以使用以下命令将Deno升级到最新版本:

deno upgrade

You can up/downgrade to a specific version:

您可以升级/降级到特定版本:

deno upgrade --version 1.0.1

未来是光明的 (The Future is Bright)

The Deno manual suggests that it

Deno 手册建议

“is a great replacement for utility scripts that may have been historically written with Bash or Python”.

“是历史上用Bash或Python编写的实用程序脚本的绝佳替代品”。

While this is certainly true, I would expect to see it increasingly being used for the same use cases where Node.js is currently popular.

尽管这确实是事实,但我希望看到它越来越多地用于Node.js当前很流行的相同用例。

There are already a number of Express/Koa-like frameworks cropping up, allowing you to build type-safe REST APIs, along with a growing number of third-party modules becoming available.

已经出现了许多类似Express / Koa的框架,使您可以构建类型安全的REST API,以及越来越多的第三方模块可用。

Ryan Dahl said

瑞安·达尔

“Deno is not by any means a rival of Node”.

“ Deno绝不是Node的竞争对手”。

Simply, he was no longer happy with Node and decided to create a new runtime to make up for its “mistakes” and shortages (and adding also a bunch of new features).

简而言之,他对Node不再满意,因此决定创建一个新的运行时来弥补它的“错误”和不足 (并添加了许多新功能)。

So, should you forget Node.js and start learning Deno?

因此,您是否应该忘记Node.js并开始学习Deno?

Current opinion in the industry is that Node.js is not about to disappear anytime soon, but Deno is definitely a technology to watch.

业界目前的观点是Node.js不会很快消失 ,但是Deno绝对是一项值得关注的技术

普通英语JavaScript (JavaScript In Plain English)

Enjoyed this article? If so, get more similar content by subscribing to Decoded, our YouTube channel!

喜欢这篇文章吗? 如果是这样,请订阅我们的YouTube频道解码,以获得更多类似的内容

翻译自: https://medium.com/javascript-in-plain-english/introduction-to-deno-a-secure-javascript-typescript-runtime-a6b3d5c21b2c

node deno

 类似资料: