@astrojs/webapi

中文文档
3.0.0发布于 2023/07/28MITTypeScriptES Module近 30 天下载 9.7万

Use Web APIs in Node

此软件包已被标记为弃用

This package is not used by Astro any more and is no longer maintained. In Astro 3.0 polyfills are part of a core module.

原始使用文档
文档目录4 节
  1. Features
  2. Usage
  3. Polyfill Options
  4. License & Attribution

WebAPI

WebAPI lets you use Web APIs not present in Node v16 and later.

npm install @astrojs/webapi
import { polyfill } from '@astrojs/webapi'

polyfill(globalThis)

const t = new EventTarget()
const e = new CustomEvent('hello')

t.addEventListener('hello', console.log)

t.dispatchEvent(e) // logs `e` event from `t`

These APIs are combined from popular open source projects and configured to share implementation details. This allows their behavior to match browser expectations as well as reduce their combined memory footprint.

Features

Usage

You can use WebAPIs as individual exports.

import { Event, EventTarget, File, fetch, Response } from '@astrojs/webapi'

You can apply WebAPIs to an object, like globalThis.

import { polyfill } from '@astrojs/webapi'

polyfill(globalThis)

Polyfill Options

The exclude option receives a list of WebAPIs to exclude from polyfilling.

polyfill(globalThis, {
    // disables polyfills for setTimeout clearTimeout
    exclude: 'setTimeout clearTimeout',
})

The exclude option accepts shorthands to exclude multiple polyfills. These shorthands end with the plus sign (+).

polyfill(globalThis, {
    // disables polyfills for setTimeout clearTimeout
    exclude: 'Timeout+',
})
polyfill(globalThis, {
    // disables polyfills for Node, Window, Document, HTMLElement, etc.
    exclude: 'Node+',
})
polyfill(globalThis, {
    // disables polyfills for Event, EventTarget, Node, Window, Document, HTMLElement, etc.
    exclude: 'Event+',
})
ShorthandExcludes
Document+Document, HTMLDocument
Element+Element, and exclusions from HTMLElement+
Event+Event, CustomEvent, EventTarget, MediaQueryList, Window, and exclusions from Node+
EventTarget+Event, CustomEvent, EventTarget, MediaQueryList, Window, and exclusions from Node+
HTMLElement+CustomElementsRegistry, HTMLElement, HTMLBodyElement, HTMLCanvasElement, HTMLDivElement, HTMLHeadElement, HTMLHtmlElement, HTMLImageElement, HTMLStyleElement, HTMLTemplateElement, HTMLUnknownElement, Image
Node+Node, DocumentFragment, ShadowRoot, Document, HTMLDocument, and exclusions from Element+
StyleSheet+StyleSheet, CSSStyleSheet

License & Attribution

Thank you to Jon Neal for his work on the original webapi project that this package is forked from. Licensed under the CC0-1.0 License.

Code from event-target-shim is licensed under the MIT License (MIT), Copyright Toru Nagashima.

版本与发布记录

@astrojs/webapi 共发布 23 个版本,首次发布于 2022/03/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.0latest2023/07/283 年前
2.2.02023/05/313 年前
2.1.12023/05/013 年前
2.1.02023/03/083 年前
0.0.0-experimental-assets-20230307131344next--experimental-assets2023/03/073 年前
0.0.0-cloudcannon-fix-20230306211609next--cloudcannon-fix2023/03/073 年前

其他发布通道:beta → 2.0.0-beta.1,next--check-watch → 0.0.0-check-watch-20230303211357,next--cloudcannon-fix → 0.0.0-cloudcannon-fix-20230306211609,next--experimental-assets → 0.0.0-experimental-assets-20230307131344。

关于 @astrojs/webapi 的常见问题

@astrojs/webapi 是什么?

Use Web APIs in Node注意:该包已被作者标记为弃用——This package is not used by Astro any more and is no longer maintained. In Astro 3.0 polyfills are part of a core module.

如何安装 @astrojs/webapi?

在项目目录运行 npm install @astrojs/webapi,或使用 pnpm add / yarn add / bun add。

@astrojs/webapi 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@astrojs/webapi 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@astrojs/webapi 可以商用吗?

@astrojs/webapi 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@astrojs/webapi 的下载量有多少?

根据 npm 官方下载统计,@astrojs/webapi 在 2026/10/07 – 2026/11/05 这 30 天内被下载 97,358 次,日均约 3,245 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@astrojs/webapi 还在维护吗?

最新版本 3.0.0 发布于 2023/07/28(3 年前);至今共发布 23 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/03/08。作者已将其标记为弃用,新项目建议寻找替代方案。