Tiny, dependency-free helper for reading and writing cookies in browsers or any environment that exposes a document-style interface.
- Features
- Release Highlights (0.3.x)
- Installation
- Quick Start
- API Reference
- Cookie Options
- Examples
- TypeScript
- Testing
- Contributing
- Project Origins & Credits
- License
- 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.
- Adds the
partitionedattribute so cookies can opt into CHIPS while matching Mozilla’s Set-Cookie guidance. - Warns when
SameSite: "None"is paired withoutsecure: trueand providescookie.configure({ suppressInsecureSameSiteNoneWarning: true })to silence the warning in local HTTP setups. - Introduces a top-level
cookie.clearhelper (including TypeScript declarations) and guarantees deletes forceMax-Age=0. - Extends serialization to cover
HttpOnly,Max-Age, andSameSiteattributes so emitted cookies reflect modern browser expectations.
npm install @boiseitguru/cookie-cutter// 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: "/" });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.
Returns the value stored for key, or undefined if the cookie is not set.
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.
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.
Adjusts global defaults for future cookie operations, such as silencing SameSite warnings during local development.
type CookieConfig = {
suppressInsecureSameSiteNoneWarning?: boolean;
};suppressInsecureSameSiteNoneWarning: Whentrue, disables the warning emitted bycookie.setforSameSite: "None"cookies missing thesecureflag. Useful for non-production development over HTTP.
| Option | Type | Description |
|---|---|---|
expires |
Date |
Sets the cookie expiry date. |
path |
string |
Restricts the cookie to a particular path. |
domain |
string |
Restricts the cookie to a particular domain. |
secure |
boolean |
Marks the cookie as secure-only. Required by browsers when SameSite is "None". |
HttpOnly |
boolean |
Adds the HttpOnly attribute. |
MaxAge |
number |
Sets the cookie lifetime (in seconds) via Max-Age. |
SameSite |
"Strict" | "Lax" | "None" |
Controls cross-site cookie behavior. |
partitioned |
boolean |
Appends the Partitioned attribute for CHIPS storage (requires secure: true in supporting browsers). |
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.
var cookie = require("@boiseitguru/cookie-cutter");
cookie.configure({ suppressInsecureSameSiteNoneWarning: true });
var api = cookie({ cookie: "" });
api.set("token", "dev-only", { SameSite: "None" });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 });Run the Tape test suite:
npm test- Keep the ES5 coding style: two-space indentation,
vardeclarations, 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.
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.
MIT. See the LICENSE file for details.