npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@hudc/cookies

v1.1.1

Published

Operate document.cookie like storage API

Readme

@hudc/cookies

build coverage version license types

使用 Proxy 代理浏览器 cookie 的常用操作,也支持使用类似 Storage API 的方式操作 cookie

安装

npm install @hudc/cookies

引入

import cookies, {
  cookie,
  getItem,
  setItem,
  removeItem,
  hasItem,
  clear,
  keys,
  length,
} from '@hudc/cookies'
  1. 默认导出的 cookies 封装了 getItem, setItem, removeItem, hasItem, clear, keys, length 方法
  2. 按需导出的 cookie 使用 Proxy 代理了 get, set, deleteProperty, has, ownKeys 方法

查询 cookie

// 假设 document.cookie 值为 'abc=123; def-1=456%3AFLAG%3D1'
cookie.abc // 输出 '123'
cookie['def-1'] // 输出 '456:FLAG=1'
cookies.getItem('abc') // 输出 '123'
cookies.getItem('def-1') // 输出 '456:FLAG=1'
getItem('abc') // 输出 '123'
getItem('def-1') // 输出 '456:FLAG=1'

设置 cookie

  • expires 过期时间
    • 值可以是一个 dateString,或者一个 timeStamp
    • 当值为整型,并且小于 1e9(十亿),则认为是一个过期的秒数
    • 默认在会话结束时过期
  • path 路径,默认为当前文档位置的路径
  • domain 子域名,默认为当前文档位置的路径的域名部分(包含子域)
  • secure 是否只能通过 https 协议传递,默认false
  • sameSite 限制跨站传递的策略
    • None 不限制,即跨站点和同站点都会发送 cookie (需要设置 secure)
    • Strict 严格,只有同站点的 cookie 才会被发送
    • Lax 默认
  • 返回当前设置的完整信息
// 假设 document.cookie 为空
cookie.abc = '123' // 'abc=123'
cookie['def-1'] = { value: '456:FLAG=1' } // 'abc=123; def-1=456%3AFLAG%3D1'
cookie['ghi[1]'] = { value: '789', expires: 5 } // 'abc=123; def-1=456%3AFLAG%3D1; ghi[1]=789'
// 假设 document.cookie 为空
cookies.setItem('abc', '123') // 'abc=123'
cookies.setItem('def-1', '456:FLAG=1') // 'abc=123; def-1=456%3AFLAG%3D1'
cookies.setItem('ghi[1]', '789', 'Tue, 19 Jan 2038 03:14:07 GMT') // 'abc=123; def-1=456%3AFLAG%3D1; ghi[1]=789'
// 假设 document.cookie 为空
setItem('abc', '123') // 'abc=123'
setItem('def-1', '456:FLAG=1') // 'abc=123; def-1=456%3AFLAG%3D1'
setItem('ghi[1]', '789', 2147483647000) // 'abc=123; def-1=456%3AFLAG%3D1; ghi[1]=789'

删除 cookie

// 假设 document.cookie 值为 'abc=123; def-1=456%3AFLAG%3D1; ghi[1]=789'
delete cookie.abc // 'def-1=456%3AFLAG%3D1; ghi[1]=789'
cookies.removeItem('def-1') // 'ghi[1]=789'
removeItem('ghi[1]') // ''

清空 cookies

cookies.clear()
clear()

是否存在指定名称的 cookie

// 假设 document.cookie 值为 'abc=123; def-1=456%3AFLAG%3D1; ghi[1]=789'
'abc' in cookie // true
cookies.hasItem('def-1') // true
hasItem('ghi[1]') // true

获取所有 cookie 名称

// 假设 document.cookie 值为 'abc=123; def=456:FLAG=1'
cookie.keys // 输出 ['abc', 'def']
Object.getOwnPropertyNames(cookie) // 输出 ['abc', 'def']
cookies.keys // 输出 ['abc', 'def']
keys() // 输出 ['abc', 'def']

获取 cookies 数量

// 假设 document.cookie 值为 'abc=123; def=456:FLAG=1'
cookie.length // 输出 2
cookies.length // 输出 2
length() // 输出 2

参考