Skip to content

Commit

Permalink
Add option to read multiple cookies with same name
Browse files Browse the repository at this point in the history
Fixes jshttp#60
  • Loading branch information
SachinShekhar committed Jun 3, 2020
1 parent b22458d commit 861f045
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
0.5.0 / 2020-06-04
==================

* Add support for reading multiple cookies with same name
- This feature can be turned on by `multiValuedCookies` flag

0.4.1 / 2020-04-21
==================

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ sequences into their byte representations.
**note** if an error is thrown from this function, the original, non-decoded cookie value will
be returned as the cookie's value.

##### multiValuedCookies

Specifies a boolean flag which if set to `true` would allow reading multiple cookies with same name (key). All values will be put into an array.
By default (when `multiValuedCookies` isn't present or set to `false`), only first cookie's value is read which is not in accordance with [RFC 6265](https://tools.ietf.org/html/rfc6265#section-4.2.2) as discussed [here](https://github.com/jshttp/cookie/issues/60) (this behaviour is kept around for backward compatibility reason).

### cookie.serialize(name, value, options)

Serialize a cookie name-value pair into a `Set-Cookie` header string. The `name` argument is the
Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
* Parse a cookie header.
*
* Parse the given cookie header string into an object
* The object has the various cookies as keys(names) => values
* The object has the various cookies as keys(names) => values or array of values
*
* @param {string} str
* @param {object} [options]
Expand Down Expand Up @@ -73,9 +73,15 @@ function parse(str, options) {
val = val.slice(1, -1);
}

// only assign once
// only assign once unless multiValuedCookies is true
if (undefined == obj[key]) {
obj[key] = tryDecode(val, dec);
} else if (opt.multiValuedCookies) {
if (typeof obj[key] === 'string') {
obj[key] = [obj[key], tryDecode(val, dec)];
} else {
obj[key] = obj[key].push(tryDecode(val, dec));
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,18 @@ test('assign only once', function() {
assert.deepEqual({ foo: '', bar: 'bar' },
cookie.parse('foo=;bar=bar;foo=boo'));
});

test('multiValuedCookies flag', function () {
assert.deepEqual(
{ foo: ["%1", "boo"], bar: "bar" },
cookie.parse("foo=%1;bar=bar;foo=boo", {
multiValuedCookies: true
})
);
assert.deepEqual(
{ foo: ["", "boo"], bar: "bar" },
cookie.parse("foo=;bar=bar;foo=boo", {
multiValuedCookies: true,
})
);
});

0 comments on commit 861f045

Please sign in to comment.