@boiseitguru/cookie-cutter

中文文档
0.3.1发布于 2025/11/04MITTypeScriptCommonJS零依赖近 30 天下载 7522

Browserify-compatible module to get and set cookies in the browser

原始使用文档
文档目录20 节
  1. Table of Contents
  2. Features
  3. Release Highlights (0.3.x)
  4. Installation
  5. Quick Start
  6. API Reference
  7. cookie(document?: Document | string)
  8. cookie.get(key: string): string | undefined
  9. cookie.set(key: string, value: string, options?: CookieOpti…
  10. cookie.clear(key: string, options?: CookieOptions): string
  11. cookie.configure(options: CookieConfig): void
  12. Cookie Options
  13. Examples
  14. Partitioned SameSite=None Cookie
  15. Suppressing Warnings During Local Development
  16. TypeScript
  17. Testing
  18. Contributing
  19. Project Origins & Credits
  20. License

cookie-cutter

npm version bundle size

Tiny, dependency-free helper for reading and writing cookies in browsers or any environment that exposes a document-style interface.

Table of Contents

Features

  • Zero runtime dependencies and ES5-friendly source so it bundles cleanly with legacy toolchains.
  • Works in browsers, Browserify-style bundles, and Node.js when you provide a document stub.
  • Built-in TypeScript declarations with the same API shape as the JavaScript module.
  • Partitioned CHIPS attribute support plus SameSite guardrails to keep you aligned with current browser requirements.

Release Highlights (0.3.x)

  • Adds the partitioned attribute so cookies can opt into CHIPS while matching Mozilla’s Set-Cookie guidance.
  • Warns when SameSite: "None" is paired without secure: true and provides cookie.configure({ suppressInsecureSameSiteNoneWarning: true }) to silence the warning in local HTTP setups.
  • Introduces a top-level cookie.clear helper (including TypeScript declarations) and guarantees deletes force Max-Age=0.
  • Extends serialization to cover HttpOnly, Max-Age, and SameSite attributes so emitted cookies reflect modern browser expectations.

Installation

npm install @boiseitguru/cookie-cutter

Quick Start

// CommonJS
var cookie = require("@boiseitguru/cookie-cutter");

var visits = parseInt(cookie.get("visits"), 10) || 0;
cookie.set("visits", visits + 1, { path: "/" });
// ES Modules (modern bundlers)
import cookie from "@boiseitguru/cookie-cutter";

const visits = Number(cookie.get("visits") || 0) + 1;
cookie.set("visits", String(visits), { path: "/" });

API Reference

cookie(document?: Document | string)

Creates a cookie controller bound to the supplied document or cookie string. If no argument is given, an empty cookie jar is created. When you import/require the module in a browser, the default export is already bound to the global document, so you can call cookie.get, cookie.set, and cookie.clear directly as shown in the Quick Start.

cookie.get(key: string): string | undefined

Returns the value stored for key, or undefined if the cookie is not set.

cookie.set(key: string, value: string, options?: CookieOptions): string

Serializes key=value with any supplied cookie attributes, assigns it to document.cookie, and returns the serialized string. When options.SameSite === "None" and options.secure is not true, a warning is printed to console.warn to anticipate upcoming enforcement. Setting options.partitioned === true appends the Partitioned attribute.

cookie.clear(key: string, options?: CookieOptions): string

Clears the cookie for key by delegating to set with an expiry in the past and Max-Age = 0. Pass the same options you used when setting the cookie (such as path or domain) so the browser targets the correct cookie.

cookie.configure(options: CookieConfig): void

Adjusts global defaults for future cookie operations, such as silencing SameSite warnings during local development.

type CookieConfig = {
  suppressInsecureSameSiteNoneWarning?: boolean;
};
  • suppressInsecureSameSiteNoneWarning: When true, disables the warning emitted by cookie.set for SameSite: "None" cookies missing the secure flag. Useful for non-production development over HTTP.
OptionTypeDescription
expiresDateSets the cookie expiry date.
pathstringRestricts the cookie to a particular path.
domainstringRestricts the cookie to a particular domain.
securebooleanMarks the cookie as secure-only. Required by browsers when SameSite is "None".
HttpOnlybooleanAdds the HttpOnly attribute.
MaxAgenumberSets the cookie lifetime (in seconds) via Max-Age.
SameSite"Strict" | "Lax" | "None"Controls cross-site cookie behavior.
partitionedbooleanAppends the Partitioned attribute for CHIPS storage (requires secure: true in supporting browsers).

Examples

var cookie = require("@boiseitguru/cookie-cutter");
var api = cookie(document);

api.set("session", "abc123", {
  secure: true,
  SameSite: "None",
  partitioned: true
});

This combination satisfies current Mozilla Set-Cookie expectations for partitioned cookies.

Suppressing Warnings During Local Development

var cookie = require("@boiseitguru/cookie-cutter");

cookie.configure({ suppressInsecureSameSiteNoneWarning: true });
var api = cookie({ cookie: "" });

api.set("token", "dev-only", { SameSite: "None" });

TypeScript

Type definitions ship with the package. Import the module using either default or CommonJS syntax and enjoy auto-complete for cookie options, including partitioned and SameSite.

import cookie = require("@boiseitguru/cookie-cutter");

const api = cookie();
api.set("theme", "dark", { SameSite: "Lax", MaxAge: 300 });

Testing

Run the Tape test suite:

npm test

Contributing

  • Keep the ES5 coding style: two-space indentation, var declarations, double-quoted strings, and explicit semicolons.
  • Update TypeScript declarations whenever the runtime API changes.
  • Add focused tests alongside functional changes so behavior stays locked down.

Project Origins & Credits

This repository preserves the original cookie-cutter module by James Halliday (substack) to ensure ongoing availability, with TypeScript support and modern cookie attribute updates maintained here.

License

MIT. See the LICENSE file for details.

版本与发布记录

@boiseitguru/cookie-cutter 共发布 6 个版本,首次发布于 2023/07/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.3.1latest2025/11/041 年前
0.3.02025/11/041 年前
0.2.32024/01/032 年前
0.2.22024/01/032 年前
0.2.12023/07/133 年前
0.2.02023/07/103 年前

关于 @boiseitguru/cookie-cutter 的常见问题

@boiseitguru/cookie-cutter 是什么?

Browserify-compatible module to get and set cookies in the browser

如何安装 @boiseitguru/cookie-cutter?

在项目目录运行 npm install @boiseitguru/cookie-cutter,或使用 pnpm add / yarn add / bun add。

@boiseitguru/cookie-cutter 支持 TypeScript 吗?

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

@boiseitguru/cookie-cutter 支持 ESM 还是 CommonJS?

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

@boiseitguru/cookie-cutter 可以商用吗?

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

@boiseitguru/cookie-cutter 的下载量有多少?

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

@boiseitguru/cookie-cutter 还在维护吗?

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