Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to read multiple cookies with same name #108

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
unreleased
==================

* 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) (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].push(tryDecode(val, dec));
}
}
}

Expand Down
27 changes: 27 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,30 @@ 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,
})
);
assert.deepEqual(
{ foo: ["%1", "boo", "bar"], bar: "bar" },
cookie.parse("foo=%1;bar=bar;foo=boo;foo=bar", {
multiValuedCookies: true,
})
);
assert.deepEqual(
{ foo: "%1", bar: "bar" },
cookie.parse("foo=%1;bar=bar", {
multiValuedCookies: true,
})
);
});
SachinShekhar marked this conversation as resolved.
Show resolved Hide resolved