@ampproject/toolbox-cors

中文文档
2.10.1发布于 2024/06/14Apache-2.0CommonJS近 30 天下载 2137

An express middleware implementing the AMP CORS protocol

原始使用文档
文档目录9 节
  1. Installation
  2. Usage
  3. Filtering by source origin
  4. Origin verification
  5. Allow Crendentials
  6. Allow AMP-Redirect-To
  7. Logging
  8. Email Mode
  9. Example

AMP CORS Middleware

npm version

The AMP CORS middleware adds CORS and AMP CORS headers to all CORS requests initiated by the AMP runtime. The middleware will only add these headers if the __amp_source_origin query parameter is present. All other requests remain unchanged.

Installation

Install via:

npm install @ampproject/toolbox-cors --save

Usage

Here is an example using Express:

const express = require('express');
const ampCors = require('@ampproject/toolbox-cors');

const app = express();

// That's it!
app.use(ampCors());
...

Please note that AMP CORS does not depend on Express and is based on Node's HTTP Request and Response objects.

Filtering by source origin

You can additionally filter requests by source origin. For example:

app.use(ampCors({
  sourceOriginPattern: /https:\/\/ampbyexample\.com$/
}));

This will only allow requests with https://amp.dev set as the source origin. Requests from all other origins will receive a 403 response,

Origin verification

By default, the AMP CORS middleware will only allow requests from AMP Caches listed on https://cdn.ampproject.org/caches.json. All other origins will receive a 403 response. To allow requests from all origins, disable this via the verifyOrigin option:

app.use(ampCors({
  verifyOrigin: false
}));

Allow Crendentials

By default, the AMP CORS middleware will allow crendentials mode for AMP CORS requests. To disable this, set allowCredentials to false.

app.use(ampCors({
  allowCredentials: false
}));
// => will not set "Access-Control-Allow-Credentials", "true"

Allow AMP-Redirect-To

By default, the AMP CORS middleware will allow redirects via AMP-Redirect-To. To disable this, set enableAmpRedirectTo to false.

app.use(ampCors({
  enableAmpRedirectTo: false
}));
// Access-Control-Expose-Headers: AMP-Redirect-To

Logging

For debugging requests, you can enable the verbose loggin mode via the verbose option:

app.use(ampCors({
  verbose: false
}));

Email Mode

Gmail has specific AMP CORS requirements. You can enable the Email CORS mode via the email option:

app.use(ampCors({
  email: true
}));

Note: the default AMP CORS mode for websites is compatible with email CORS mode (only origin verification is no longer supported). If you want to support both, it's safe to enable email mode by default.

Example

See express.js for a sample implementation. There are two scenarios in which the AMP CORS header will be added:

  1. AMP CORS header will be set if the __amp_source_origin query parameter is set together with the AMP-SAME-ORIGIN header:
$ curl --header "AMP-SAME-ORIGIN: true" -I "http://localhost:3000/items?__amp_source_origin=https://localhost:3000"
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: https://localhost:3000
Content-Type: application/json; charset=utf-8
...
  1. AMP CORS header will be set if the __amp_source_origin query parameter is set together with the Origin header:
$ curl --header "Origin: https://amp-dev.cdn.ampproject.org" -I "http://localhost:3000/items?__amp_source_origin=https://localhost:3000"
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://amp-dev.cdn.ampproject.org
Content-Type: application/json; charset=utf-8
...

In all other cases, no CORS header will be set.

$ curl -I localhost:3000/items
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
...

版本与发布记录

@ampproject/toolbox-cors 共发布 43 个版本,首次发布于 2019/06/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.10.1latest2024/06/142 年前
2.10.02024/06/142 年前
2.9.02022/06/244 年前
3.0.0-canary.2canary2022/01/124 年前
2.8.02021/05/315 年前
2.8.0-canary.152021/03/315 年前

其他发布通道:beta → 1.1.0-beta.0,alpha → 2.7.0-alpha.1,canary → 3.0.0-canary.2。

关于 @ampproject/toolbox-cors 的常见问题

@ampproject/toolbox-cors 是什么?

An express middleware implementing the AMP CORS protocol

如何安装 @ampproject/toolbox-cors?

在项目目录运行 npm install @ampproject/toolbox-cors,或使用 pnpm add / yarn add / bun add。

@ampproject/toolbox-cors 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/ampproject__toolbox-cors 是否存在。

@ampproject/toolbox-cors 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@ampproject/toolbox-cors 可以商用吗?

@ampproject/toolbox-cors 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@ampproject/toolbox-cors 的下载量有多少?

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

@ampproject/toolbox-cors 还在维护吗?

最新版本 2.10.1 发布于 2024/06/14(2 年前);至今共发布 43 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/06/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。