@ampproject/toolbox-script-csp

中文文档
2.10.1发布于 2024/06/14Apache-2.0CommonJS零依赖近 30 天下载 15.9万

A library that calculates the CSP hash for amp-script

原始使用文档
文档目录3 节
  1. Usage
  2. Using the module
  3. Example

AMP-Toolbox Script CSP

npm version

Calculates the Content Security Policy (CSP) hash for the given script in the format expected by amp-script.

CSP is required when using amp-script with inline or cross-origin scripts.

Usage

Install via:

$ npm install @ampproject/toolbox-script-csp

Using the module

The module exposes a single function, calculateHash(src, options?).

src can be either a string (in which case UTF8 encoding is assumed) or a DataView such as a Buffer.

options is used to override the default options.

Options

  • algorithm overrides the hashing algorithm to use. Currently, the only algorithm supported by amp-script is sha384 which is the default value.

Example

Here is an example on how to use the calculateHash function to generate an amp-script CSP header.

Note that both leading and trailing whitespace is included in calculating the hash and must exactly match the whitespace used in the inline script.

const {calculateHash} = require('@ampproject/toolbox-script-csp');

const script = `
    const subject = 'world';
    console.log('Hello, ' + subject);
    `;

const hash = calculateHash(script);

console.log(hash);  // sha384-xRxb5sv13at6tVgZET4JLmf89TSZP10HjCGXVqO9bKWVXB0asV2jLrsDN8v4zX6j

The generated hash can be used in a <meta> tag this way:

<!doctype html>
<html ⚡>
<head>
  ...

  <meta name="amp-script-src" content="sha384-xRxb5sv13at6tVgZET4JLmf89TSZP10HjCGXVqO9bKWVXB0asV2jLrsDN8v4zX6j">
</head>
  <body>
    ...

    <amp-script layout="container" script="example"></amp-script>

    <script id="example" type="text/plain" target="amp-script">
    const subject = 'world';
    console.log('Hello, ' + subject);
    </script>
  </body>
</html>

版本与发布记录

@ampproject/toolbox-script-csp 共发布 15 个版本,首次发布于 2019/08/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.10.1latest2024/06/142 年前
2.10.02024/06/142 年前
2.9.02022/06/244 年前
2.8.02021/05/315 年前
2.8.0-canary.0canary2021/02/135 年前
2.5.42020/06/236 年前

其他发布通道:beta → 1.1.0-beta.0,canary → 2.8.0-canary.0。

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

@ampproject/toolbox-script-csp 是什么?

A library that calculates the CSP hash for amp-script

如何安装 @ampproject/toolbox-script-csp?

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

@ampproject/toolbox-script-csp 支持 TypeScript 吗?

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

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

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

@ampproject/toolbox-script-csp 可以商用吗?

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

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

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

@ampproject/toolbox-script-csp 还在维护吗?

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