Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Oct 4, 2024
1 parent c1a8b8f commit 9f2ca12
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 98 deletions.
101 changes: 32 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ Basic HTTP cookie parser and serializer for HTTP servers.

## Installation

This is a [Node.js](https://nodejs.org/en/) module available through the
[npm registry](https://www.npmjs.com/). Installation is done using the
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):

```sh
$ npm install cookie
```

## API

```js
var cookie = require("cookie");
const cookie = require("cookie");
```

### cookie.parse(str, options)
Expand All @@ -31,7 +27,7 @@ The `str` argument is the string representing a `Cookie` header value and `optio
optional object containing additional parsing options.

```js
var cookies = cookie.parse("foo=bar; equation=E%3Dmc%5E2");
const cookies = cookie.parse("foo=bar; equation=E%3Dmc%5E2");
// { foo: 'bar', equation: 'E=mc^2' }
```

Expand All @@ -58,7 +54,7 @@ name for the cookie, the `value` argument is the value to set the cookie to, and
argument is an optional object containing additional serialization options.

```js
var setCookie = cookie.serialize("foo", "bar");
const setCookie = cookie.serialize("foo", "bar");
// foo=bar
```

Expand Down Expand Up @@ -220,73 +216,40 @@ http.createServer(onRequest).listen(3000);
## Testing

```sh
$ npm test
npm test
```

## Benchmark

```sh
npm run bench
```

```
$ npm run bench
> [email protected] bench
> node benchmark/index.js
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
modules@108
napi@9
[email protected]
[email protected]
[email protected]
[email protected]+quic
[email protected]
tz@2023c
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
> node benchmark/parse-top.js
cookie.parse - top sites
14 tests completed.
parse accounts.google.com x 2,588,913 ops/sec ±0.74% (186 runs sampled)
parse apple.com x 2,370,002 ops/sec ±0.69% (186 runs sampled)
parse cloudflare.com x 2,213,102 ops/sec ±0.88% (188 runs sampled)
parse docs.google.com x 2,194,157 ops/sec ±1.03% (184 runs sampled)
parse drive.google.com x 2,265,084 ops/sec ±0.79% (187 runs sampled)
parse en.wikipedia.org x 457,099 ops/sec ±0.81% (186 runs sampled)
parse linkedin.com x 504,407 ops/sec ±0.89% (186 runs sampled)
parse maps.google.com x 1,230,959 ops/sec ±0.98% (186 runs sampled)
parse microsoft.com x 926,294 ops/sec ±0.88% (184 runs sampled)
parse play.google.com x 2,311,338 ops/sec ±0.83% (185 runs sampled)
parse support.google.com x 1,508,850 ops/sec ±0.86% (186 runs sampled)
parse www.google.com x 1,022,582 ops/sec ±1.32% (182 runs sampled)
parse youtu.be x 332,136 ops/sec ±1.02% (185 runs sampled)
parse youtube.com x 323,833 ops/sec ±0.77% (183 runs sampled)
> node benchmark/parse.js
cookie.parse - generic
6 tests completed.
simple x 3,214,032 ops/sec ±1.61% (183 runs sampled)
decode x 587,237 ops/sec ±1.16% (187 runs sampled)
unquote x 2,954,618 ops/sec ±1.35% (183 runs sampled)
duplicates x 857,008 ops/sec ±0.89% (187 runs sampled)
10 cookies x 292,133 ops/sec ±0.89% (187 runs sampled)
100 cookies x 22,610 ops/sec ±0.68% (187 runs sampled)
name hz min max mean p75 p99 p995 p999 rme samples
· simple 8,566,313.09 0.0000 0.3694 0.0001 0.0001 0.0002 0.0002 0.0003 ±0.64% 4283157 fastest
· decode 3,834,348.85 0.0001 0.2465 0.0003 0.0003 0.0003 0.0004 0.0006 ±0.38% 1917175
· unquote 8,315,355.96 0.0000 0.3824 0.0001 0.0001 0.0002 0.0002 0.0003 ±0.72% 4157880
· duplicates 1,944,765.97 0.0004 0.2959 0.0005 0.0005 0.0006 0.0006 0.0008 ±0.24% 972384
· 10 cookies 675,345.67 0.0012 0.4328 0.0015 0.0015 0.0019 0.0020 0.0058 ±0.75% 337673
· 100 cookies 61,040.71 0.0152 0.4092 0.0164 0.0160 0.0196 0.0228 0.2260 ±0.71% 30521 slowest
✓ parse top-sites (15) 22945ms
name hz min max mean p75 p99 p995 p999 rme samples
· parse accounts.google.com 7,164,349.17 0.0000 0.0929 0.0001 0.0002 0.0002 0.0002 0.0003 ±0.09% 3582184
· parse apple.com 7,817,686.84 0.0000 0.6048 0.0001 0.0001 0.0002 0.0002 0.0003 ±1.05% 3908844
· parse cloudflare.com 7,189,841.70 0.0000 0.0390 0.0001 0.0002 0.0002 0.0002 0.0003 ±0.06% 3594921
· parse docs.google.com 7,051,765.61 0.0000 0.0296 0.0001 0.0002 0.0002 0.0002 0.0003 ±0.06% 3525883
· parse drive.google.com 7,349,104.77 0.0000 0.0368 0.0001 0.0001 0.0002 0.0002 0.0003 ±0.05% 3674553
· parse en.wikipedia.org 1,929,909.49 0.0004 0.3598 0.0005 0.0005 0.0007 0.0007 0.0012 ±0.16% 964955
· parse linkedin.com 2,225,658.01 0.0003 0.0595 0.0004 0.0005 0.0005 0.0005 0.0006 ±0.06% 1112830
· parse maps.google.com 4,423,511.68 0.0001 0.0942 0.0002 0.0003 0.0003 0.0003 0.0005 ±0.08% 2211756
· parse microsoft.com 3,387,601.88 0.0002 0.0725 0.0003 0.0003 0.0004 0.0004 0.0005 ±0.09% 1693801
· parse play.google.com 7,375,980.86 0.0000 0.1994 0.0001 0.0001 0.0002 0.0002 0.0003 ±0.12% 3687991
· parse support.google.com 4,912,267.94 0.0001 2.8958 0.0002 0.0002 0.0003 0.0003 0.0005 ±1.28% 2456134
· parse www.google.com 3,443,035.87 0.0002 0.2783 0.0003 0.0003 0.0004 0.0004 0.0007 ±0.51% 1721518
· parse youtu.be 1,910,492.87 0.0004 0.3490 0.0005 0.0005 0.0007 0.0007 0.0011 ±0.46% 955247
· parse youtube.com 1,895,082.62 0.0004 0.7454 0.0005 0.0005 0.0006 0.0007 0.0013 ±0.64% 947542 slowest
· parse example.com 21,582,835.27 0.0000 0.1095 0.0000 0.0000 0.0001 0.0001 0.0001 ±0.13% 10791418
```

## References
Expand Down
51 changes: 22 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,9 @@ export interface SerializeOptions {
export function serialize(
name: string,
val: string,
opt?: SerializeOptions,
options?: SerializeOptions,
): string {
const enc = opt?.encode || encodeURIComponent;

if (typeof enc !== "function") {
throw new TypeError("option encode is invalid");
}
const enc = options?.encode || encodeURIComponent;

if (!cookieNameRegExp.test(name)) {
throw new TypeError("argument name is invalid");
Expand All @@ -186,57 +182,54 @@ export function serialize(
}

let str = name + "=" + value;
if (!opt) return str;
if (!options) return str;

if (opt.maxAge !== undefined) {
if (!Number.isInteger(opt.maxAge)) {
if (options.maxAge !== undefined) {
if (!Number.isInteger(options.maxAge)) {
throw new TypeError("option maxAge is invalid");
}

str += "; Max-Age=" + opt.maxAge;
str += "; Max-Age=" + options.maxAge;
}

if (opt.domain) {
if (!domainValueRegExp.test(opt.domain)) {
if (options.domain) {
if (!domainValueRegExp.test(options.domain)) {
throw new TypeError("option domain is invalid");
}

str += "; Domain=" + opt.domain;
str += "; Domain=" + options.domain;
}

if (opt.path) {
if (!pathValueRegExp.test(opt.path)) {
if (options.path) {
if (!pathValueRegExp.test(options.path)) {
throw new TypeError("option path is invalid");
}

str += "; Path=" + opt.path;
str += "; Path=" + options.path;
}

if (opt.expires) {
if (
typeof opt.expires.toUTCString !== "function" ||
!Number.isFinite(opt.expires.valueOf())
) {
if (options.expires) {
if (!Number.isFinite(options.expires.valueOf())) {
throw new TypeError("option expires is invalid");
}

str += "; Expires=" + opt.expires.toUTCString();
str += "; Expires=" + options.expires.toUTCString();
}

if (opt.httpOnly) {
if (options.httpOnly) {
str += "; HttpOnly";
}

if (opt.secure) {
if (options.secure) {
str += "; Secure";
}

if (opt.partitioned) {
if (options.partitioned) {
str += "; Partitioned";
}

if (opt.priority) {
switch (opt.priority) {
if (options.priority) {
switch (options.priority) {
case "low":
str += "; Priority=Low";
break;
Expand All @@ -251,8 +244,8 @@ export function serialize(
}
}

if (opt.sameSite) {
switch (opt.sameSite) {
if (options.sameSite) {
switch (options.sameSite) {
case true:
case "strict":
str += "; SameSite=Strict";
Expand Down

0 comments on commit 9f2ca12

Please sign in to comment.