Web 平台 APIs
Deno 旨在使用 web 平台的 API（如
fetch），而不是新发明一个有意义的特有 API 。这些 API 通常遵循规范，而且应该与 Chrome
和 Firefox 中的实现相匹配。在某些情况下，因为 Deno 有着不同的安全模式，所以稍微偏离规范是有意义的。
以下是 Deno web 平台 API 的实现列表：
- Channel Messaging API
- Compression Streams API
- Encoding API
- Fetch API
- Location API
- Performance API
- Streams API
- Web Crypto API
- Web Storage API
- Web Workers API
fetch API 可以用来发送 HTTP 请求。 它是按照
fetch 规范 中的规定实现的。
你可以在 MDN 中找到关于
fetch API 的文档。
- The Deno user agent does not have a cookie jar. As such, the
set-cookieheader on a response is not processed, or filtered from the visible response headers.
- Deno does not follow the same-origin policy, because the Deno user agent
currently does not have the concept of origins, and it does not have a cookie
jar. This means Deno does not need to protect against leaking authenticated
data cross origin. Because of this Deno does not implement the following
sections of the WHATWG
3.1. 'Origin' header.
3.2. CORS protocol.
3.6. 'Cross-Origin-Resource-Policy' header.
Atomic HTTP redirect handling.
manualwill return a
basicresponse rather than an
- The specification is vague on how
file:URLs are to be handled . Firefox is the only mainstream browser that implements fetching
file:URLs, and even then it doesn't work by default. As of Deno 1.16, Deno supports fetching local files. See the next section for details.
responseheader guards are implemented, but unlike browsers do not have any constraints on which header names are allowed.
windowproperties and their relevant behaviours in
RequestInitare not implemented. The relevant fields are not present on the
- Request body upload streaming is supported (on HTTP/1.1 and HTTP/2). Unlike the current fetch proposal, the implementation supports duplex streaming.
set-cookieheader is not concatenated when iterated over in the
headersiterator. This behaviour is in the process of being specified .
Fetching local files
As of Deno 1.16, Deno supports fetching
file: URLs. This makes it easier to
write code that uses the same code path on a server as local, as well as easier
to author code that works both with the Deno CLI and Deno Deploy.
Deno only supports absolute file URLs, this means that
will not work. It should be noted though that if
--location is specified, relative URLs use the
--location as the base, but a
file: URL cannot be passed as the
To be able to fetch some resource, relative to the current module, which would
work if the module is local or remote, you would want to use
as the base. For example, something like:
const response = await fetch(new URL("./config.json", import.meta.url)); const config = await response.json();
Notes on fetching local files:
- Permissions are applied to reading resources, so an appropriate
--allow-readpermission is needed to be able to read a local file.
- Fetching locally only supports the
GETmethod, and will reject the promise with any other method.
- A file that does not exists simply rejects the promise with a vague
TypeError. This is to avoid the potential of fingerprinting attacks.
- No headers are set on the response. Therefore it is up to the consumer to determine things like the content type or content length.
- Response bodies are streamed from the Rust side, so large files are available in chunks, and can be cancelled.
DOM 事件 API 可以用来调度和监听应用程序中发生的事件。 它是按照 WHATWG DOM 规范 中的规定实现的。
你可以在 MDN 里找到关于
EventTarget API 的文档。
- 没有冒泡事件，因为 Deno 没有 DOM 层次结构，所以没有树状的事件可以冒泡/捕获。
用 TypeScript 实现定义的 web APIs
关于 workers 的具体定义可以在