pax_global_header00006660000000000000000000000064142251350470014515gustar00rootroot0000000000000052 comment=663c9aeb85c9e046ff2ad2cdec631afdd40a7965 cookie-0.5.0/000077500000000000000000000000001422513504700127705ustar00rootroot00000000000000cookie-0.5.0/.editorconfig000066400000000000000000000002631422513504700154460ustar00rootroot00000000000000# http://editorconfig.org root = true [*] charset = utf-8 insert_final_newline = true trim_trailing_whitespace = true [{*.js,*.json,*.yml}] indent_size = 2 indent_style = space cookie-0.5.0/.eslintignore000066400000000000000000000000261422513504700154710ustar00rootroot00000000000000coverage node_modules cookie-0.5.0/.eslintrc.yml000066400000000000000000000003431422513504700154140ustar00rootroot00000000000000extends: - plugin:markdown/recommended plugins: - markdown overrides: - files: '**/*.md' processor: 'markdown/markdown' rules: eol-last: error indent: ["error", 2, { "SwitchCase": 1 }] no-trailing-spaces: error cookie-0.5.0/.github/000077500000000000000000000000001422513504700143305ustar00rootroot00000000000000cookie-0.5.0/.github/workflows/000077500000000000000000000000001422513504700163655ustar00rootroot00000000000000cookie-0.5.0/.github/workflows/ci.yml000066400000000000000000000136741422513504700175160ustar00rootroot00000000000000name: ci on: - pull_request - push jobs: test: runs-on: ubuntu-18.04 strategy: matrix: name: - Node.js 0.6 - Node.js 0.8 - Node.js 0.10 - Node.js 0.12 - io.js 1.x - io.js 2.x - io.js 3.x - Node.js 4.x - Node.js 5.x - Node.js 6.x - Node.js 7.x - Node.js 8.x - Node.js 9.x - Node.js 10.x - Node.js 11.x - Node.js 12.x - Node.js 13.x - Node.js 14.x - Node.js 15.x - Node.js 16.x - Node.js 17.x include: - name: Node.js 0.6 node-version: "0.6" npm-i: mocha@1.21.5 npm-rm: beautify-benchmark benchmark nyc top-sites - name: Node.js 0.8 node-version: "0.8" npm-i: mocha@2.5.3 npm-rm: beautify-benchmark benchmark nyc top-sites - name: Node.js 0.10 node-version: "0.10" npm-i: mocha@3.5.3 nyc@10.3.2 npm-rm: beautify-benchmark benchmark top-sites - name: Node.js 0.12 node-version: "0.12" npm-i: mocha@3.5.3 nyc@10.3.2 npm-rm: beautify-benchmark benchmark top-sites - name: io.js 1.x node-version: "1.8" npm-i: mocha@3.5.3 nyc@10.3.2 npm-rm: beautify-benchmark benchmark top-sites - name: io.js 2.x node-version: "2.5" npm-i: mocha@3.5.3 nyc@10.3.2 npm-rm: beautify-benchmark benchmark top-sites - name: io.js 3.x node-version: "3.3" npm-i: mocha@3.5.3 nyc@10.3.2 npm-rm: beautify-benchmark benchmark top-sites - name: Node.js 4.x node-version: "4.9" npm-i: mocha@5.2.0 nyc@11.9.0 npm-rm: beautify-benchmark benchmark top-sites - name: Node.js 5.x node-version: "5.12" npm-i: mocha@5.2.0 nyc@11.9.0 npm-rm: beautify-benchmark benchmark top-sites - name: Node.js 6.x node-version: "6.17" npm-i: mocha@6.2.2 nyc@14.1.1 npm-rm: beautify-benchmark benchmark top-sites - name: Node.js 7.x node-version: "7.10" npm-i: mocha@6.2.2 nyc@14.1.1 npm-rm: beautify-benchmark benchmark top-sites - name: Node.js 8.x node-version: "8.17" npm-i: mocha@7.1.2 npm-rm: beautify-benchmark benchmark top-sites - name: Node.js 9.x node-version: "9.11" npm-i: mocha@7.1.2 npm-rm: beautify-benchmark benchmark top-sites - name: Node.js 10.x node-version: "10.24" npm-i: mocha@8.4.0 - name: Node.js 11.x node-version: "11.15" npm-i: mocha@8.4.0 - name: Node.js 12.x node-version: "12.22" - name: Node.js 13.x node-version: "13.14" - name: Node.js 14.x node-version: "14.19" - name: Node.js 15.x node-version: "15.14" - name: Node.js 16.x node-version: "16.14" - name: Node.js 17.x node-version: "17.9" steps: - uses: actions/checkout@v2 - name: Install Node.js ${{ matrix.node-version }} shell: bash -eo pipefail -l {0} run: | if [[ "${{ matrix.node-version }}" == 0.6* ]]; then sudo apt-get install g++-4.8 gcc-4.8 libssl1.0-dev export CC=/usr/bin/gcc-4.8 export CXX=/usr/bin/g++-4.8 fi nvm install --default ${{ matrix.node-version }} if [[ "${{ matrix.node-version }}" == 0.* && "$(cut -d. -f2 <<< "${{ matrix.node-version }}")" -lt 10 ]]; then nvm install --alias=npm 0.10 nvm use ${{ matrix.node-version }} if [[ "$(npm -v)" == 1.1.* ]]; then nvm exec npm npm install -g npm@1.1 ln -fs "$(which npm)" "$(dirname "$(nvm which npm)")/npm" else sed -i '1s;^.*$;'"$(printf '#!%q' "$(nvm which npm)")"';' "$(readlink -f "$(which npm)")" fi npm config set strict-ssl false fi dirname "$(nvm which ${{ matrix.node-version }})" >> "$GITHUB_PATH" - name: Configure npm run: npm config set shrinkwrap false - name: Remove npm module(s) ${{ matrix.npm-rm }} run: npm rm --silent --save-dev ${{ matrix.npm-rm }} if: matrix.npm-rm != '' - name: Install npm module(s) ${{ matrix.npm-i }} run: npm install --save-dev ${{ matrix.npm-i }} if: matrix.npm-i != '' - name: Setup Node.js version-specific dependencies shell: bash run: | # eslint for linting # - remove on Node.js < 10 if [[ "$(cut -d. -f1 <<< "${{ matrix.node-version }}")" -lt 10 ]]; then node -pe 'Object.keys(require("./package").devDependencies).join("\n")' | \ grep -E '^eslint(-|$)' | \ sort -r | \ xargs -n1 npm rm --silent --save-dev fi - name: Install Node.js dependencies run: npm install - name: List environment id: list_env shell: bash run: | echo "node@$(node -v)" echo "npm@$(npm -v)" npm -s ls ||: (npm -s ls --depth=0 ||:) | awk -F'[ @]' 'NR>1 && $2 { print "::set-output name=" $2 "::" $3 }' - name: Run tests shell: bash run: | if npm -ps ls nyc | grep -q nyc; then npm run test-ci else npm test fi - name: Lint code if: steps.list_env.outputs.eslint != '' run: npm run lint - name: Collect code coverage uses: coverallsapp/github-action@master if: steps.list_env.outputs.nyc != '' with: github-token: ${{ secrets.GITHUB_TOKEN }} flag-name: run-${{ matrix.test_number }} parallel: true coverage: needs: test runs-on: ubuntu-latest steps: - name: Upload code coverage uses: coverallsapp/github-action@master with: github-token: ${{ secrets.github_token }} parallel-finished: true cookie-0.5.0/.gitignore000066400000000000000000000001051422513504700147540ustar00rootroot00000000000000.nyc_output/ coverage/ node_modules/ npm-debug.log package-lock.json cookie-0.5.0/HISTORY.md000066400000000000000000000060601422513504700144550ustar00rootroot000000000000000.5.0 / 2022-04-11 ================== * Add `priority` option * Fix `expires` option to reject invalid dates * pref: improve default decode speed * pref: remove slow string split in parse 0.4.2 / 2022-02-02 ================== * pref: read value only when assigning in parse * pref: remove unnecessary regexp in parse 0.4.1 / 2020-04-21 ================== * Fix `maxAge` option to reject invalid values 0.4.0 / 2019-05-15 ================== * Add `SameSite=None` support 0.3.1 / 2016-05-26 ================== * Fix `sameSite: true` to work with draft-7 clients - `true` now sends `SameSite=Strict` instead of `SameSite` 0.3.0 / 2016-05-26 ================== * Add `sameSite` option - Replaces `firstPartyOnly` option, never implemented by browsers * Improve error message when `encode` is not a function * Improve error message when `expires` is not a `Date` 0.2.4 / 2016-05-20 ================== * perf: enable strict mode * perf: use for loop in parse * perf: use string concatination for serialization 0.2.3 / 2015-10-25 ================== * Fix cookie `Max-Age` to never be a floating point number 0.2.2 / 2015-09-17 ================== * Fix regression when setting empty cookie value - Ease the new restriction, which is just basic header-level validation * Fix typo in invalid value errors 0.2.1 / 2015-09-17 ================== * Throw on invalid values provided to `serialize` - Ensures the resulting string is a valid HTTP header value 0.2.0 / 2015-08-13 ================== * Add `firstPartyOnly` option * Throw better error for invalid argument to parse * perf: hoist regular expression 0.1.5 / 2015-09-17 ================== * Fix regression when setting empty cookie value - Ease the new restriction, which is just basic header-level validation * Fix typo in invalid value errors 0.1.4 / 2015-09-17 ================== * Throw better error for invalid argument to parse * Throw on invalid values provided to `serialize` - Ensures the resulting string is a valid HTTP header value 0.1.3 / 2015-05-19 ================== * Reduce the scope of try-catch deopt * Remove argument reassignments 0.1.2 / 2014-04-16 ================== * Remove unnecessary files from npm package 0.1.1 / 2014-02-23 ================== * Fix bad parse when cookie value contained a comma * Fix support for `maxAge` of `0` 0.1.0 / 2013-05-01 ================== * Add `decode` option * Add `encode` option 0.0.6 / 2013-04-08 ================== * Ignore cookie parts missing `=` 0.0.5 / 2012-10-29 ================== * Return raw cookie value if value unescape errors 0.0.4 / 2012-06-21 ================== * Use encode/decodeURIComponent for cookie encoding/decoding - Improve server/client interoperability 0.0.3 / 2012-06-06 ================== * Only escape special characters per the cookie RFC 0.0.2 / 2012-06-01 ================== * Fix `maxAge` option to not throw error 0.0.1 / 2012-05-28 ================== * Add more tests 0.0.0 / 2012-05-28 ================== * Initial release cookie-0.5.0/LICENSE000066400000000000000000000022271422513504700140000ustar00rootroot00000000000000(The MIT License) Copyright (c) 2012-2014 Roman Shtylman Copyright (c) 2015 Douglas Christopher Wilson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cookie-0.5.0/README.md000066400000000000000000000260421422513504700142530ustar00rootroot00000000000000# cookie [![NPM Version][npm-version-image]][npm-url] [![NPM Downloads][npm-downloads-image]][npm-url] [![Node.js Version][node-version-image]][node-version-url] [![Build Status][github-actions-ci-image]][github-actions-ci-url] [![Test Coverage][coveralls-image]][coveralls-url] 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'); ``` ### cookie.parse(str, options) Parse an HTTP `Cookie` header string and returning an object of all cookie name-value pairs. The `str` argument is the string representing a `Cookie` header value and `options` is an optional object containing additional parsing options. ```js var cookies = cookie.parse('foo=bar; equation=E%3Dmc%5E2'); // { foo: 'bar', equation: 'E=mc^2' } ``` #### Options `cookie.parse` accepts these properties in the options object. ##### decode Specifies a function that will be used to decode a cookie's value. Since the value of a cookie has a limited character set (and must be a simple string), this function can be used to decode a previously-encoded cookie value into a JavaScript string or other object. The default function is the global `decodeURIComponent`, which will decode any URL-encoded 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. ### cookie.serialize(name, value, options) Serialize a cookie name-value pair into a `Set-Cookie` header string. The `name` argument is the name for the cookie, the `value` argument is the value to set the cookie to, and the `options` argument is an optional object containing additional serialization options. ```js var setCookie = cookie.serialize('foo', 'bar'); // foo=bar ``` #### Options `cookie.serialize` accepts these properties in the options object. ##### domain Specifies the value for the [`Domain` `Set-Cookie` attribute][rfc-6265-5.2.3]. By default, no domain is set, and most clients will consider the cookie to apply to only the current domain. ##### encode Specifies a function that will be used to encode a cookie's value. Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode a value into a string suited for a cookie's value. The default function is the global `encodeURIComponent`, which will encode a JavaScript string into UTF-8 byte sequences and then URL-encode any that fall outside of the cookie range. ##### expires Specifies the `Date` object to be the value for the [`Expires` `Set-Cookie` attribute][rfc-6265-5.2.1]. By default, no expiration is set, and most clients will consider this a "non-persistent cookie" and will delete it on a condition like exiting a web browser application. **note** the [cookie storage model specification][rfc-6265-5.3] states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this, so if both are set, they should point to the same date and time. ##### httpOnly Specifies the `boolean` value for the [`HttpOnly` `Set-Cookie` attribute][rfc-6265-5.2.6]. When truthy, the `HttpOnly` attribute is set, otherwise it is not. By default, the `HttpOnly` attribute is not set. **note** be careful when setting this to `true`, as compliant clients will not allow client-side JavaScript to see the cookie in `document.cookie`. ##### maxAge Specifies the `number` (in seconds) to be the value for the [`Max-Age` `Set-Cookie` attribute][rfc-6265-5.2.2]. The given number will be converted to an integer by rounding down. By default, no maximum age is set. **note** the [cookie storage model specification][rfc-6265-5.3] states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this, so if both are set, they should point to the same date and time. ##### path Specifies the value for the [`Path` `Set-Cookie` attribute][rfc-6265-5.2.4]. By default, the path is considered the ["default path"][rfc-6265-5.1.4]. ##### priority Specifies the `string` to be the value for the [`Priority` `Set-Cookie` attribute][rfc-west-cookie-priority-00-4.1]. - `'low'` will set the `Priority` attribute to `Low`. - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set. - `'high'` will set the `Priority` attribute to `High`. More information about the different priority levels can be found in [the specification][rfc-west-cookie-priority-00-4.1]. **note** This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it. ##### sameSite Specifies the `boolean` or `string` to be the value for the [`SameSite` `Set-Cookie` attribute][rfc-6265bis-09-5.4.7]. - `true` will set the `SameSite` attribute to `Strict` for strict same site enforcement. - `false` will not set the `SameSite` attribute. - `'lax'` will set the `SameSite` attribute to `Lax` for lax same site enforcement. - `'none'` will set the `SameSite` attribute to `None` for an explicit cross-site cookie. - `'strict'` will set the `SameSite` attribute to `Strict` for strict same site enforcement. More information about the different enforcement levels can be found in [the specification][rfc-6265bis-09-5.4.7]. **note** This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it. ##### secure Specifies the `boolean` value for the [`Secure` `Set-Cookie` attribute][rfc-6265-5.2.5]. When truthy, the `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set. **note** be careful when setting this to `true`, as compliant clients will not send the cookie back to the server in the future if the browser does not have an HTTPS connection. ## Example The following example uses this module in conjunction with the Node.js core HTTP server to prompt a user for their name and display it back on future visits. ```js var cookie = require('cookie'); var escapeHtml = require('escape-html'); var http = require('http'); var url = require('url'); function onRequest(req, res) { // Parse the query string var query = url.parse(req.url, true, true).query; if (query && query.name) { // Set a new cookie with the name res.setHeader('Set-Cookie', cookie.serialize('name', String(query.name), { httpOnly: true, maxAge: 60 * 60 * 24 * 7 // 1 week })); // Redirect back after setting cookie res.statusCode = 302; res.setHeader('Location', req.headers.referer || '/'); res.end(); return; } // Parse the cookies on the request var cookies = cookie.parse(req.headers.cookie || ''); // Get the visitor name set in the cookie var name = cookies.name; res.setHeader('Content-Type', 'text/html; charset=UTF-8'); if (name) { res.write('

Welcome back, ' + escapeHtml(name) + '!

'); } else { res.write('

Hello, new visitor!

'); } res.write('
'); res.write(' '); res.end('
'); } http.createServer(onRequest).listen(3000); ``` ## Testing ```sh $ npm test ``` ## Benchmark ``` $ npm run bench > cookie@0.4.2 bench > node benchmark/index.js node@16.14.0 v8@9.4.146.24-node.20 uv@1.43.0 zlib@1.2.11 brotli@1.0.9 ares@1.18.1 modules@93 nghttp2@1.45.1 napi@8 llhttp@6.0.4 openssl@1.1.1m+quic cldr@40.0 icu@70.1 tz@2021a3 unicode@14.0 ngtcp2@0.1.0-DEV nghttp3@0.1.0-DEV > node benchmark/parse-top.js cookie.parse - top sites 15 tests completed. parse accounts.google.com x 2,421,245 ops/sec ±0.80% (188 runs sampled) parse apple.com x 2,684,710 ops/sec ±0.59% (189 runs sampled) parse cloudflare.com x 2,231,418 ops/sec ±0.76% (186 runs sampled) parse docs.google.com x 2,316,357 ops/sec ±1.28% (187 runs sampled) parse drive.google.com x 2,363,543 ops/sec ±0.49% (189 runs sampled) parse en.wikipedia.org x 839,414 ops/sec ±0.53% (189 runs sampled) parse linkedin.com x 553,797 ops/sec ±0.63% (190 runs sampled) parse maps.google.com x 1,314,779 ops/sec ±0.72% (189 runs sampled) parse microsoft.com x 153,783 ops/sec ±0.53% (190 runs sampled) parse play.google.com x 2,249,574 ops/sec ±0.59% (187 runs sampled) parse plus.google.com x 2,258,682 ops/sec ±0.60% (188 runs sampled) parse sites.google.com x 2,247,069 ops/sec ±0.68% (189 runs sampled) parse support.google.com x 1,456,840 ops/sec ±0.70% (187 runs sampled) parse www.google.com x 1,046,028 ops/sec ±0.58% (188 runs sampled) parse youtu.be x 937,428 ops/sec ±1.47% (190 runs sampled) parse youtube.com x 963,878 ops/sec ±0.59% (190 runs sampled) > node benchmark/parse.js cookie.parse - generic 6 tests completed. simple x 2,745,604 ops/sec ±0.77% (185 runs sampled) decode x 557,287 ops/sec ±0.60% (188 runs sampled) unquote x 2,498,475 ops/sec ±0.55% (189 runs sampled) duplicates x 868,591 ops/sec ±0.89% (187 runs sampled) 10 cookies x 306,745 ops/sec ±0.49% (190 runs sampled) 100 cookies x 22,414 ops/sec ±2.38% (182 runs sampled) ``` ## References - [RFC 6265: HTTP State Management Mechanism][rfc-6265] - [Same-site Cookies][rfc-6265bis-09-5.4.7] [rfc-west-cookie-priority-00-4.1]: https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1 [rfc-6265bis-09-5.4.7]: https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7 [rfc-6265]: https://tools.ietf.org/html/rfc6265 [rfc-6265-5.1.4]: https://tools.ietf.org/html/rfc6265#section-5.1.4 [rfc-6265-5.2.1]: https://tools.ietf.org/html/rfc6265#section-5.2.1 [rfc-6265-5.2.2]: https://tools.ietf.org/html/rfc6265#section-5.2.2 [rfc-6265-5.2.3]: https://tools.ietf.org/html/rfc6265#section-5.2.3 [rfc-6265-5.2.4]: https://tools.ietf.org/html/rfc6265#section-5.2.4 [rfc-6265-5.2.5]: https://tools.ietf.org/html/rfc6265#section-5.2.5 [rfc-6265-5.2.6]: https://tools.ietf.org/html/rfc6265#section-5.2.6 [rfc-6265-5.3]: https://tools.ietf.org/html/rfc6265#section-5.3 ## License [MIT](LICENSE) [coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/cookie/master [coveralls-url]: https://coveralls.io/r/jshttp/cookie?branch=master [github-actions-ci-image]: https://img.shields.io/github/workflow/status/jshttp/cookie/ci/master?label=ci [github-actions-ci-url]: https://github.com/jshttp/cookie/actions/workflows/ci.yml [node-version-image]: https://badgen.net/npm/node/cookie [node-version-url]: https://nodejs.org/en/download [npm-downloads-image]: https://badgen.net/npm/dm/cookie [npm-url]: https://npmjs.org/package/cookie [npm-version-image]: https://badgen.net/npm/v/cookie cookie-0.5.0/SECURITY.md000066400000000000000000000022341422513504700145620ustar00rootroot00000000000000# Security Policies and Procedures ## Reporting a Bug The `cookie` team and community take all security bugs seriously. Thank you for improving the security of the project. We appreciate your efforts and responsible disclosure and will make every effort to acknowledge your contributions. Report security bugs by emailing the current owner(s) of `cookie`. This information can be found in the npm registry using the command `npm owner ls cookie`. If unsure or unable to get the information from the above, open an issue in the [project issue tracker](https://github.com/jshttp/cookie/issues) asking for the current contact information. To ensure the timely response to your report, please ensure that the entirety of the report is contained within the email body and not solely behind a web link or an attachment. At least one owner will acknowledge your email within 48 hours, and will send a more detailed response within 48 hours indicating the next steps in handling your report. After the initial reply to your report, the owners will endeavor to keep you informed of the progress towards a fix and full announcement, and may ask for additional information or guidance. cookie-0.5.0/benchmark/000077500000000000000000000000001422513504700147225ustar00rootroot00000000000000cookie-0.5.0/benchmark/index.js000066400000000000000000000013751422513504700163750ustar00rootroot00000000000000var fs = require('fs') var path = require('path') var spawn = require('child_process').spawn var exe = process.argv[0] var cwd = process.cwd() for (var dep in process.versions) { console.log(' %s@%s', dep, process.versions[dep]) } console.log('') runScripts(fs.readdirSync(__dirname)) function runScripts (fileNames) { var fileName = fileNames.shift() if (!fileName) return if (!/\.js$/i.test(fileName)) return runScripts(fileNames) if (fileName.toLowerCase() === 'index.js') return runScripts(fileNames) var fullPath = path.join(__dirname, fileName) console.log('> %s %s', exe, path.relative(cwd, fullPath)) var proc = spawn(exe, [fullPath], { 'stdio': 'inherit' }) proc.on('exit', function () { runScripts(fileNames) }) } cookie-0.5.0/benchmark/parse-top.js000066400000000000000000000015461422513504700172000ustar00rootroot00000000000000/** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') var top = require('./parse-top.json') /** * Globals for benchmark.js */ global.cookie = require('..') var suite = new benchmark.Suite() Object.keys(top).forEach(function (domain) { suite.add({ name: 'parse ' + domain, minSamples: 100, fn: 'var val = cookie.parse(' + JSON.stringify(top[domain]) + ')' }) }) suite.on('start', function onCycle (event) { process.stdout.write(' cookie.parse - top sites\n\n') }) suite.on('cycle', function onCycle (event) { benchmarks.add(event.target) }) suite.on('complete', function onComplete () { benchmarks.log() }) suite.run({async: false}) function gencookies (num) { var str = '' for (var i = 0; i < num; i++) { str += '; foo' + i + '=bar' } return str.slice(2) } cookie-0.5.0/benchmark/parse-top.json000066400000000000000000000055171422513504700175370ustar00rootroot00000000000000{ "accounts.google.com": "__Ulll-UUUU=0:0U0lUlllUUUl0lUl00UUUUUU0lUlll:l0ll0UlUU0l0lUUU", "apple.com": "lll=UU", "cloudflare.com": "__ll_ll=lUllUll.0Ul0Ull0UUUU.ll_U0lllUU0UlUll0lUllU-0000000000-0-UUUl+0Ulll0UUUU0U0Ullll0UUUUUlllU0UUUUlUUUlUUUUUUUlUlUUll0l0UlUlUl0Ul000UUlUU0/0l0l0UU0UllUU0U0lU/lllU0lUllU", "docs.google.com": "__Ulll-UUUU=0:Ulll0UlUUllUlUl0UUlUUlUU0UUUUU:U0U0U0llUU0-0Ul0", "drive.google.com": "__Ulll-UUUU=0:UUll0UllUUll0lUU0llUUllUllUlll:U0UllUlll-lU0lll", "en.wikipedia.org": "UUU-Ulll-Ulllll=00-Ull-0000; UUU-Ulll-Ulllll-Ulllll=00-Ull-0000; UllUU=UU:UU:Ulllllllllll:00.00:-00.00:l0", "linkedin.com": "UUUUUUUUUU=llll:0000000000000000000; llll=l=0&llll=ll-ll; lllllll=\"l=0&ll000l0l-0lll-00l0-0000-0lll0ll0ll00\"; llllllll=\"l=0&0000000000000000l0l000-0l0l-0000-0000-0l0l00l0000lUUUUUl_llU0UlUllUUlU0lUlUl00llUl\"; llll=\"l=UUUU00:l=U:l=U:l=U:l=U:l=0000:l=0:l=0:l=0000000000:l=0000000000:l=0:lll=UUUUUlUlUUUUUUllllllU0UUlUlU0lll\"", "maps.google.com": "0U_UUU=0000-00-00-00; UUU=000=UllUl00U-UU0llllUU0lUUlUl0llUl0U0lllUlUUU0l00UUUllllUlU0l0UllUUlUlU0UllUllU0UUlU00llUUUllUUlUl00lUlUUUlUUU0Ul00lllUUUUllUUl0UUlU0UllUlUUllllUllUllUll0llllllllll0lUUUlU0Ull", "microsoft.com": "llUllllUllllll=0; UUUU=0UUU00U0000000U0000U000000UU00UU; U-UU-UUUUUUUU=lll=00000l0%2200000l%22llllll000l0%22llllllllllll000%22lllllllllll%22llllllllllllllll%22llllllllllllllll%22llllllllllllllllllllll%22llllllllllllllllll%22lll-lll-lllll0l0ll%22llllllllllll%22lllllllllllllllll%2200000l0ll&lll=0ll00ll0-000l-000l-l000-l00ll0l0l00l; U-UU-Ulll=0; lllll_UllUU=0000000000~ll=00~ll=0lll000ll0l0000lll000lll0ll0000l; lllll_UllUU=0000000000~ll=00~ll=0lll000ll0l0000lll000lll0ll0000l", "play.google.com": "UUU=000=Ull0l00UlllUUUlU0UllUl0llU0l-lllUllUl0lllUU0UUU0lUlU-UUll000lUl00U0Ul_UlUU0l0lUUUUlUUl0UlUl0lU0-lll0l0l0UlUllllU0U0UU0U00UllU0l0lU0lll_0lUUU0UllUUUUll0UlUl0l0UUUUU00UUllll", "plus.google.com": "__Ulll-UUUU=0:0Ull0lUU0UUUlll0U-UllUUllllUlU:lll00_lllUUUUUUU", "sites.google.com": "__Ulll-UUUU=0:_UU0lll0UUUUlUU0lUll0UUlllllUl:UUll0lllU0UUlll0", "support.google.com": "UUU=000=llU0lUlllUUlllUUll0U0UUll0UUlUUlU-lUUllU0UllUlUUlUlU0UlUUU_U-UlUU0lUUlUllUlUlUUl00llllUl0lUUUU00Ul0UUlUll0UUUU_UUlUlllUllllU0U0lllllUlU000UllUl--UUU_ll_lllU0UllUUlllU00UlU; UUU=000=llU0lUlllUUlllUUll0U0UUll0UUlUUlU-lUUllU0UllUlUUlUlU0UlUUU_U-UlUU0lUUlUllUlUlUUl00llllUl0lUUUU00Ul0UUlUll0UUUU_UUlUlllUllllU0U0lllllUlU000UllUl--UUU_ll_lllU0UllUUlllU00UlU", "www.google.com": "0U_UUU=0000-00-00-00; UUU=UUUU_UUUlUUUUlUll_lllll0U00UUl0lllUUUUlUU_0_llUl0l0UUlU0_lU; UUU=000=lll0lUlUlllUll00U_lUlUUUUlll0lUlUU0lllUlllUllUUUll0U_U0llUU0UlU0Ull0U_l-_UUl00lUllUl0UUllUlUll0UUllUUUlU0UUlUllUlUUUlUllU-lUUlll0UlU0lUllUUUUlUUUlUlUU0llll_lU0lUlllll_UlUl", "youtu.be": "UUU=0; UUU=UUUUUUUllUU; UUUUUUU_UUUU0_UUUU=00UlUU-UUU0", "youtube.com": "UUU=0; UUU=lU0lUUUlUll; UUUUUUU_UUUU0_UUUU=0UUUlUllU0l" } cookie-0.5.0/benchmark/parse.js000066400000000000000000000025131422513504700163730ustar00rootroot00000000000000/** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') /** * Globals for benchmark.js */ global.cookie = require('..') var suite = new benchmark.Suite() suite.add({ name: 'simple', minSamples: 100, fn: 'var val = cookie.parse("foo=bar")' }) suite.add({ name: 'decode', minSamples: 100, fn: 'var val = cookie.parse("foo=hello%20there!")' }) suite.add({ name: 'unquote', minSamples: 100, fn: 'var val = cookie.parse("foo=\\"foo bar\\"")' }) suite.add({ name: 'duplicates', minSamples: 100, fn: 'var val = cookie.parse(' + JSON.stringify(gencookies(2) + '; ' + gencookies(2)) + ')' }) suite.add({ name: '10 cookies', minSamples: 100, fn: 'var val = cookie.parse(' + JSON.stringify(gencookies(10)) + ')' }) suite.add({ name: '100 cookies', minSamples: 100, fn: 'var val = cookie.parse(' + JSON.stringify(gencookies(100)) + ')' }) suite.on('start', function onCycle (event) { process.stdout.write(' cookie.parse - generic\n\n') }) suite.on('cycle', function onCycle (event) { benchmarks.add(event.target) }) suite.on('complete', function onComplete () { benchmarks.log() }) suite.run({async: false}) function gencookies (num) { var str = '' for (var i = 0; i < num; i++) { str += '; foo' + i + '=bar' } return str.slice(2) } cookie-0.5.0/index.js000066400000000000000000000121501422513504700144340ustar00rootroot00000000000000/*! * cookie * Copyright(c) 2012-2014 Roman Shtylman * Copyright(c) 2015 Douglas Christopher Wilson * MIT Licensed */ 'use strict'; /** * Module exports. * @public */ exports.parse = parse; exports.serialize = serialize; /** * Module variables. * @private */ var __toString = Object.prototype.toString /** * RegExp to match field-content in RFC 7230 sec 3.2 * * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] * field-vchar = VCHAR / obs-text * obs-text = %x80-FF */ 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 * * @param {string} str * @param {object} [options] * @return {object} * @public */ function parse(str, options) { if (typeof str !== 'string') { throw new TypeError('argument str must be a string'); } var obj = {} var opt = options || {}; var dec = opt.decode || decode; var index = 0 while (index < str.length) { var eqIdx = str.indexOf('=', index) // no more cookie pairs if (eqIdx === -1) { break } var endIdx = str.indexOf(';', index) if (endIdx === -1) { endIdx = str.length } else if (endIdx < eqIdx) { // backtrack on prior semicolon index = str.lastIndexOf(';', eqIdx - 1) + 1 continue } var key = str.slice(index, eqIdx).trim() // only assign once if (undefined === obj[key]) { var val = str.slice(eqIdx + 1, endIdx).trim() // quoted values if (val.charCodeAt(0) === 0x22) { val = val.slice(1, -1) } obj[key] = tryDecode(val, dec); } index = endIdx + 1 } return obj; } /** * Serialize data into a cookie header. * * Serialize the a name value pair into a cookie string suitable for * http headers. An optional options object specified cookie parameters. * * serialize('foo', 'bar', { httpOnly: true }) * => "foo=bar; httpOnly" * * @param {string} name * @param {string} val * @param {object} [options] * @return {string} * @public */ function serialize(name, val, options) { var opt = options || {}; var enc = opt.encode || encode; if (typeof enc !== 'function') { throw new TypeError('option encode is invalid'); } if (!fieldContentRegExp.test(name)) { throw new TypeError('argument name is invalid'); } var value = enc(val); if (value && !fieldContentRegExp.test(value)) { throw new TypeError('argument val is invalid'); } var str = name + '=' + value; if (null != opt.maxAge) { var maxAge = opt.maxAge - 0; if (isNaN(maxAge) || !isFinite(maxAge)) { throw new TypeError('option maxAge is invalid') } str += '; Max-Age=' + Math.floor(maxAge); } if (opt.domain) { if (!fieldContentRegExp.test(opt.domain)) { throw new TypeError('option domain is invalid'); } str += '; Domain=' + opt.domain; } if (opt.path) { if (!fieldContentRegExp.test(opt.path)) { throw new TypeError('option path is invalid'); } str += '; Path=' + opt.path; } if (opt.expires) { var expires = opt.expires if (!isDate(expires) || isNaN(expires.valueOf())) { throw new TypeError('option expires is invalid'); } str += '; Expires=' + expires.toUTCString() } if (opt.httpOnly) { str += '; HttpOnly'; } if (opt.secure) { str += '; Secure'; } if (opt.priority) { var priority = typeof opt.priority === 'string' ? opt.priority.toLowerCase() : opt.priority switch (priority) { case 'low': str += '; Priority=Low' break case 'medium': str += '; Priority=Medium' break case 'high': str += '; Priority=High' break default: throw new TypeError('option priority is invalid') } } if (opt.sameSite) { var sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite; switch (sameSite) { case true: str += '; SameSite=Strict'; break; case 'lax': str += '; SameSite=Lax'; break; case 'strict': str += '; SameSite=Strict'; break; case 'none': str += '; SameSite=None'; break; default: throw new TypeError('option sameSite is invalid'); } } return str; } /** * URL-decode string value. Optimized to skip native call when no %. * * @param {string} str * @returns {string} */ function decode (str) { return str.indexOf('%') !== -1 ? decodeURIComponent(str) : str } /** * URL-encode value. * * @param {string} str * @returns {string} */ function encode (val) { return encodeURIComponent(val) } /** * Determine if value is a Date. * * @param {*} val * @private */ function isDate (val) { return __toString.call(val) === '[object Date]' || val instanceof Date } /** * Try decoding a string using a decoding function. * * @param {string} str * @param {function} decode * @private */ function tryDecode(str, decode) { try { return decode(str); } catch (e) { return str; } } cookie-0.5.0/package.json000066400000000000000000000021641422513504700152610ustar00rootroot00000000000000{ "name": "cookie", "description": "HTTP server cookie parsing and serialization", "version": "0.5.0", "author": "Roman Shtylman ", "contributors": [ "Douglas Christopher Wilson " ], "license": "MIT", "keywords": [ "cookie", "cookies" ], "repository": "jshttp/cookie", "devDependencies": { "beautify-benchmark": "0.2.4", "benchmark": "2.1.4", "eslint": "7.32.0", "eslint-plugin-markdown": "2.2.1", "mocha": "9.2.2", "nyc": "15.1.0", "safe-buffer": "5.2.1", "top-sites": "1.1.97" }, "files": [ "HISTORY.md", "LICENSE", "README.md", "SECURITY.md", "index.js" ], "engines": { "node": ">= 0.6" }, "scripts": { "bench": "node benchmark/index.js", "lint": "eslint .", "test": "mocha --reporter spec --bail --check-leaks test/", "test-ci": "nyc --reporter=lcov --reporter=text npm test", "test-cov": "nyc --reporter=html --reporter=text npm test", "update-bench": "node scripts/update-benchmark.js", "version": "node scripts/version-history.js && git add HISTORY.md" } } cookie-0.5.0/scripts/000077500000000000000000000000001422513504700144575ustar00rootroot00000000000000cookie-0.5.0/scripts/update-benchmark.js000066400000000000000000000037121422513504700202320ustar00rootroot00000000000000'use strict' var fs = require('fs') var http = require('http') var https = require('https') var path = require('path') var topSites = require('top-sites') var url = require('url') var BENCH_COOKIES_FILE = path.join(__dirname, '..', 'benchmark', 'parse-top.json') getAllCookies(topSites.slice(0, 20), function (err, cookies) { if (err) throw err var str = '{\n' + Object.keys(cookies).sort().map(function (key) { return ' ' + JSON.stringify(key) + ': ' + JSON.stringify(cookies[key]) }).join(',\n') + '\n}\n' fs.writeFileSync(BENCH_COOKIES_FILE, str) }) function get (href, callback) { var protocol = url.parse(href, false, true).protocol var proto = protocol === 'https:' ? https : http proto.get(href) .on('error', callback) .on('response', function (res) { if (res.headers.location && res.statusCode >= 300 && res.statusCode < 400) { get(url.resolve(href, res.headers.location), callback) } else { callback(null, res) } }) } function getAllCookies (sites, callback) { var all = Object.create(null) var wait = sites.length sites.forEach(function (site) { getCookies(site, function (err, cookies) { if (!err && cookies.length) { all[site.rootDomain] = cookies.map(obfuscate).join('; ') } if (!--wait) { callback(null, all) } }) }) } function getCookies (site, callback) { var href = url.format({ hostname: site.rootDomain, protocol: 'http' }) get(href, function (err, res) { if (err) return callback(err) var cookies = (res.headers['set-cookie'] || []).map(function (c) { return c.split(';')[0] }) callback(null, cookies) }) } function obfuscate (str) { return str .replace(/%[0-9a-f]{2}/gi, function () { return '%__' }) .replace(/[a-z]/g, function () { return 'l' }) .replace(/[A-Z]/g, function () { return 'U' }) .replace(/[0-9]/g, function () { return '0' }) .replace(/%__/g, function () { return '%22' }) } cookie-0.5.0/scripts/version-history.js000066400000000000000000000027771422513504700202160ustar00rootroot00000000000000'use strict' var fs = require('fs') var path = require('path') var HISTORY_FILE_PATH = path.join(__dirname, '..', 'HISTORY.md') var MD_HEADER_REGEXP = /^====*$/ var VERSION = process.env.npm_package_version var VERSION_PLACEHOLDER_REGEXP = /^(?:unreleased|(\d+\.)+x)$/ var historyFileLines = fs.readFileSync(HISTORY_FILE_PATH, 'utf-8').split('\n') if (!MD_HEADER_REGEXP.test(historyFileLines[1])) { console.error('Missing header in HISTORY.md') process.exit(1) } if (!VERSION_PLACEHOLDER_REGEXP.test(historyFileLines[0])) { console.error('Missing placeholder version in HISTORY.md') process.exit(1) } if (historyFileLines[0].indexOf('x') !== -1) { var versionCheckRegExp = new RegExp('^' + historyFileLines[0].replace('x', '.+') + '$') if (!versionCheckRegExp.test(VERSION)) { console.error('Version %s does not match placeholder %s', VERSION, historyFileLines[0]) process.exit(1) } } historyFileLines[0] = VERSION + ' / ' + getLocaleDate() historyFileLines[1] = repeat('=', historyFileLines[0].length) fs.writeFileSync(HISTORY_FILE_PATH, historyFileLines.join('\n')) function getLocaleDate () { var now = new Date() return zeroPad(now.getFullYear(), 4) + '-' + zeroPad(now.getMonth() + 1, 2) + '-' + zeroPad(now.getDate(), 2) } function repeat (str, length) { var out = '' for (var i = 0; i < length; i++) { out += str } return out } function zeroPad (number, length) { var num = number.toString() while (num.length < length) { num = '0' + num } return num } cookie-0.5.0/test/000077500000000000000000000000001422513504700137475ustar00rootroot00000000000000cookie-0.5.0/test/parse.js000066400000000000000000000042061422513504700154210ustar00rootroot00000000000000 var assert = require('assert'); var Buffer = require('safe-buffer').Buffer var cookie = require('..'); describe('cookie.parse(str)', function () { it('should throw with no arguments', function () { assert.throws(cookie.parse.bind(), /argument str must be a string/) }) it('should throw when not a string', function () { assert.throws(cookie.parse.bind(null, 42), /argument str must be a string/) }) it('should parse cookie string to object', function () { assert.deepEqual(cookie.parse('foo=bar'), { foo: 'bar' }) assert.deepEqual(cookie.parse('foo=123'), { foo: '123' }) }) it('should ignore OWS', function () { assert.deepEqual(cookie.parse('FOO = bar; baz = raz'), { FOO: 'bar', baz: 'raz' }) }) it('should parse cookie with empty value', function () { assert.deepEqual(cookie.parse('foo= ; bar='), { foo: '', bar: '' }) }) it('should URL-decode values', function () { assert.deepEqual(cookie.parse('foo="bar=123456789&name=Magic+Mouse"'), { foo: 'bar=123456789&name=Magic+Mouse' }) assert.deepEqual(cookie.parse('email=%20%22%2c%3b%2f'), { email: ' ",;/' }) }) it('should return original value on escape error', function () { assert.deepEqual(cookie.parse('foo=%1;bar=bar'), { foo: '%1', bar: 'bar' }) }) it('should ignore cookies without value', function () { assert.deepEqual(cookie.parse('foo=bar;fizz ; buzz'), { foo: 'bar' }) assert.deepEqual(cookie.parse(' fizz; foo= bar'), { foo: 'bar' }) }) it('should ignore duplicate cookies', function () { assert.deepEqual(cookie.parse('foo=%1;bar=bar;foo=boo'), { foo: '%1', bar: 'bar' }) assert.deepEqual(cookie.parse('foo=false;bar=bar;foo=true'), { foo: 'false', bar: 'bar' }) assert.deepEqual(cookie.parse('foo=;bar=bar;foo=boo'), { foo: '', bar: 'bar' }) }) }) describe('cookie.parse(str, options)', function () { describe('with "decode" option', function () { it('should specify alternative value decoder', function () { assert.deepEqual(cookie.parse('foo="YmFy"', { decode: function (v) { return Buffer.from(v, 'base64').toString() } }), { foo: 'bar' }) }) }) }) cookie-0.5.0/test/serialize.js000066400000000000000000000165701422513504700163050ustar00rootroot00000000000000 var assert = require('assert'); var Buffer = require('safe-buffer').Buffer var cookie = require('..'); describe('cookie.serialize(name, value)', function () { it('should serialize name and value', function () { assert.equal(cookie.serialize('foo', 'bar'), 'foo=bar') }) it('should URL-encode value', function () { assert.equal(cookie.serialize('foo', 'bar +baz'), 'foo=bar%20%2Bbaz') }) it('should serialize empty value', function () { assert.equal(cookie.serialize('foo', ''), 'foo=') }) it('should throw for invalid name', function () { assert.throws(cookie.serialize.bind(cookie, 'foo\n', 'bar'), /argument name is invalid/) assert.throws(cookie.serialize.bind(cookie, 'foo\u280a', 'bar'), /argument name is invalid/) }) }) describe('cookie.serialize(name, value, options)', function () { describe('with "domain" option', function () { it('should serialize domain', function () { assert.equal(cookie.serialize('foo', 'bar', { domain: 'example.com' }), 'foo=bar; Domain=example.com') }) it('should throw for invalid value', function () { assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { domain: 'example.com\n' }), /option domain is invalid/) }) }) describe('with "encode" option', function () { it('should throw on non-function value', function () { assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { encode: 42 }), /option encode is invalid/) }) it('should specify alternative value encoder', function () { assert.deepEqual(cookie.serialize('foo', 'bar', { encode: function (v) { return Buffer.from(v, 'utf8').toString('base64') } }), 'foo=YmFy') }) it('should throw when returned value is invalid', function () { assert.throws(cookie.serialize.bind(cookie, 'foo', '+ \n', { encode: function (v) { return v } }), /argument val is invalid/) }) }) describe('with "expires" option', function () { it('should throw on non-Date value', function () { assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { expires: 42 }), /option expires is invalid/) }) it('should throw on invalid date', function () { assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { expires: new Date(NaN) }), /option expires is invalid/) }) it('should set expires to given date', function () { assert.equal(cookie.serialize('foo', 'bar', { expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)) }), 'foo=bar; Expires=Sun, 24 Dec 2000 10:30:59 GMT') }) }) describe('with "httpOnly" option', function () { it('should include httpOnly flag when true', function () { assert.equal(cookie.serialize('foo', 'bar', { httpOnly: true }), 'foo=bar; HttpOnly') }) it('should not include httpOnly flag when false', function () { assert.equal(cookie.serialize('foo', 'bar', { httpOnly: false }), 'foo=bar') }) }) describe('with "maxAge" option', function () { it('should throw when not a number', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { maxAge: 'buzz' }) }, /option maxAge is invalid/) }) it('should throw when Infinity', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { maxAge: Infinity }) }, /option maxAge is invalid/) }) it('should set max-age to value', function () { assert.equal(cookie.serialize('foo', 'bar', { maxAge: 1000 }), 'foo=bar; Max-Age=1000') assert.equal(cookie.serialize('foo', 'bar', { maxAge: '1000' }), 'foo=bar; Max-Age=1000') assert.equal(cookie.serialize('foo', 'bar', { maxAge: 0 }), 'foo=bar; Max-Age=0') assert.equal(cookie.serialize('foo', 'bar', { maxAge: '0' }), 'foo=bar; Max-Age=0') }) it('should set max-age to integer value', function () { assert.equal(cookie.serialize('foo', 'bar', { maxAge: 3.14 }), 'foo=bar; Max-Age=3') assert.equal(cookie.serialize('foo', 'bar', { maxAge: 3.99 }), 'foo=bar; Max-Age=3') }) it('should not set when null', function () { assert.equal(cookie.serialize('foo', 'bar', { maxAge: null }), 'foo=bar') }) }) describe('with "path" option', function () { it('should serialize path', function () { assert.equal(cookie.serialize('foo', 'bar', { path: '/' }), 'foo=bar; Path=/') }) it('should throw for invalid value', function () { assert.throws(cookie.serialize.bind(cookie, 'foo', 'bar', { path: '/\n' }), /option path is invalid/) }) }) describe('with "priority" option', function () { it('should throw on invalid priority', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { priority: 'foo' }) }, /option priority is invalid/) }) it('should throw on non-string', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { priority: 42 }) }, /option priority is invalid/) }) it('should set priority low', function () { assert.equal(cookie.serialize('foo', 'bar', { priority: 'Low' }), 'foo=bar; Priority=Low') assert.equal(cookie.serialize('foo', 'bar', { priority: 'loW' }), 'foo=bar; Priority=Low') }) it('should set priority medium', function () { assert.equal(cookie.serialize('foo', 'bar', { priority: 'Medium' }), 'foo=bar; Priority=Medium') assert.equal(cookie.serialize('foo', 'bar', { priority: 'medium' }), 'foo=bar; Priority=Medium') }) it('should set priority high', function () { assert.equal(cookie.serialize('foo', 'bar', { priority: 'High' }), 'foo=bar; Priority=High') assert.equal(cookie.serialize('foo', 'bar', { priority: 'HIGH' }), 'foo=bar; Priority=High') }) }) describe('with "sameSite" option', function () { it('should throw on invalid sameSite', function () { assert.throws(function () { cookie.serialize('foo', 'bar', { sameSite: 'foo' }) }, /option sameSite is invalid/) }) it('should set sameSite strict', function () { assert.equal(cookie.serialize('foo', 'bar', { sameSite: 'Strict' }), 'foo=bar; SameSite=Strict') assert.equal(cookie.serialize('foo', 'bar', { sameSite: 'strict' }), 'foo=bar; SameSite=Strict') }) it('should set sameSite lax', function () { assert.equal(cookie.serialize('foo', 'bar', { sameSite: 'Lax' }), 'foo=bar; SameSite=Lax') assert.equal(cookie.serialize('foo', 'bar', { sameSite: 'lax' }), 'foo=bar; SameSite=Lax') }) it('should set sameSite none', function () { assert.equal(cookie.serialize('foo', 'bar', { sameSite: 'None' }), 'foo=bar; SameSite=None') assert.equal(cookie.serialize('foo', 'bar', { sameSite: 'none' }), 'foo=bar; SameSite=None') }) it('should set sameSite strict when true', function () { assert.equal(cookie.serialize('foo', 'bar', { sameSite: true }), 'foo=bar; SameSite=Strict') }) it('should not set sameSite when false', function () { assert.equal(cookie.serialize('foo', 'bar', { sameSite: false }), 'foo=bar') }) }) describe('with "secure" option', function () { it('should include secure flag when true', function () { assert.equal(cookie.serialize('foo', 'bar', { secure: true }), 'foo=bar; Secure') }) it('should not include secure flag when false', function () { assert.equal(cookie.serialize('foo', 'bar', { secure: false }), 'foo=bar') }) }) })