Gathering detailed insights and metrics for cookiesjs
Gathering detailed insights and metrics for cookiesjs
Gathering detailed insights and metrics for cookiesjs
Gathering detailed insights and metrics for cookiesjs
npm install cookiesjs
39.6
Supply Chain
99.5
Quality
75.7
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
43 Stars
49 Commits
3 Forks
3 Watching
1 Branches
3 Contributors
Updated on 07 Feb 2024
JavaScript (88.57%)
HTML (11.43%)
Cumulative downloads
Total Downloads
Last day
-33.8%
49
Compared to previous day
Last week
25.3%
574
Compared to previous week
Last month
-4.6%
2,441
Compared to previous month
Last year
-13.9%
59,413
Compared to previous year
Super simple cookie manipulation on the front-end using javascript:
1cookies({ token: '42' }); // Set it 2var token = cookies('token'); // Get it 3cookies({ token: null }); // Eat it
News: See my latest Cookies and localStorage library
brownies
!
There are few ways to use cookies.js. To install it into your bundle use npm:
npm install cookiesjs
Then import it into your project:
1import cookies from 'cookiesjs'; // New style 2const cookies = require('cookiesjs'); // Old school 3 4cookies(...); // Use it
You can alternatively use the awesome JSDelivr CDN:
1<script src="https://cdn.jsdelivr.net/npm/cookiesjs@3"></script> 2<script type="text/javascript"> 3 cookies(...); 4</script>
Or just download cookies.min.js and use it locally:
1<script src="cookies.min.js"></script> 2<script type="text/javascript"> 3 cookies(...); 4</script>
A cookie is set with a simple object as the first parameter:
1cookies({ token: '42' });
We make the assumption that you want a cookie instead of a session, so cookies.js will set the expiration to 100 days by default. Set
cookies.expires = 0
as seen in the options to use session cookies
You can set as many cookies as you want at the same time:
1cookies({ token: '42', question: 'the Ultimate Question' });
The example above would be the same as this:
1cookies({ question: 'the Ultimate Question' }); 2cookies({ token: '42' });
You can also set complex data and it will be json-encoded saved and retrieved properly in a single cookie:
1var userdata = { email: 'test@test.com', token: '42' }; 2cookies({ user: userdata });
The example above is different from this one. This one sets two cookies:
1cookies({ email: 'test@test.com' }); 2cookies({ token: '42' });
It will also store numbers (instead of strings) because it uses json to encode them:
1cookies({ token: 42 }); 2var token = cookies('token'); 3console.log(token === 42, token === '42'); // true false
Lastly, you can concatenate them as many times as you want:
1cookies({ token: 42 })({ token: '42' });
Note that this library does not accept a two-parameter strings to set cookies like many other libraries to avoid confusion with the options:
1// NOT VALID 2cookies('token', '42');
or continue to read a cookie
When using cookies you can pass a second parameter as options (shown here with the defaults):
1cookies({ token: '42' }, { 2 expires: 100 * 24 * 3600, // The time to expire in seconds 3 domain: false, // The domain for the cookie 4 path: '/', // The path for the cookie 5 secure: https ? true : false // Require the use of https 6});
Or you could set any of the options globally for all the instances after being called:
1cookies.expires = 100 * 24 * 3600; // The time to expire in seconds 2cookies.domain = false; // The domain for the cookie 3cookies.path = '/'; // The path for the cookie 4cookies.secure = https ? true : false; // Require the use of https
An explanation of them all:
expires
: when the cookie will expire and be removed. It can be:
Date()
instance with the date of expiration0
, false
or a falsy value to set a session cookiedomain
: the domain where the cookie is applied.path
: the folder where the cookie will be available. Normally it's better to leave this empty.secure
: force the cookie to be retrieved through https. By default this is set to true when it's set in an https domain to avoid it being stolen if you later visit an http page on a public connection.For instance, to set a cookie that expires in 24h you would do:
1cookies({ shortlife: 42 }, { expires: 24 * 3600 });
To set two cookies with the same domain, you could do:
1cookies({ same: '4', place: '2' }, { domain: '.example.com' });
But you can also set them to different domains:
1cookies({ same: '4' }, { domain: '.example.com' }); 2cookies({ place: '2' }, { domain: 'sub.example.com' });
There are some advanced options that we don't recommend to change as you could have problems down the road, but if you know what you are doing go ahead.
1cookies({ token: '42' }, { 2 nulltoremove: true, // Set the value of a cookie to null to remove it 3 autojson: true, // Encode and decode data structures with JSON 4 autoencode: true, // Encode to make it safe for url (RFC6265) 5 encode: function(str){ return encodeURIComponent(str); }, // Function to encode it 6 decode: function(str){ return decodeURIComponent(str); }, // Function to decode it 7});
Normally you'd want to change these options globally:
1cookies.nulltoremove = true; 2cookies.autojson = true; 3cookies.autoencode = true; 4cookies.encode = function(str){ return encodeURIComponent(str); }; 5cookies.decode = function(str){ return decodeURIComponent(str); };
Few notes and warnings:
null
value in a cookie you'll have to set up cookies.nulltoremove = false
.cookies.autojson
, be aware that objects will be stored literally as [object Object]
, arrays will be joined with ,
and numbers will become strings.cookies.autoencode
or cookies.encode
could contribute to stop making it RFC 6265 compliant.To read a cookie you call the main function with a string, that is the key of the cookie that you want to read. Let's say that you want to read the cookie token
1var token = cookies('token');
cookies.js automatically stores and retrieves the cookies with JSON and URI-encoded. You can disable this with the options
autojson
andautoencode
as seen in the Advanced Options.
Set and read a cookie:
1cookies({ token: '42' }); 2var token = cookies('token'); 3// token === '42'
You can set and read a cookie at the same time taking advantage of the concatenation:
1var token = cookies({ token: '42' })('token'); 2// token === '42'
With the options by default, you can remove a cookie in few different ways:
1cookies({ token: null }); 2cookies({ token: 'a' }, { expires: -10 });
So, let's write, remove and read a cookie:
1cookies({ token: '42' }); 2cookies({ token: null }); 3var token = cookies('token'); 4// token === undefined
Let's do a typical (simplified) authentication flow:
1var token = cookies('token'); 2var user; 3if (token) { 4 $.post('/user/auth', { token: token }, function(data){ 5 user = data; 6 }); 7}
Or let's check if the user accepts cookies (European law):
1if (!cookies('accepted')) { 2 $('.cookie-box').show(); 3 $('.cookie-box .accept').click(function(){ 4 $('.cookie-box').hide(); 5 cookies({ accepted: true }); 6 }); 7}
Created and maintained by Francisco Presencia under the MIT license.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More