Permalink
Browse files

initial thing

  • Loading branch information...
0 parents commit def1196a58c1e4e79307557f4f7995f1bab63dcf @substack committed Sep 28, 2011
Showing with 89 additions and 0 deletions.
  1. +4 −0 README.markdown
  2. +7 −0 example/getset/index.html
  3. +7 −0 example/getset/main.js
  4. +18 −0 example/getset/server.js
  5. +29 −0 index.js
  6. +24 −0 package.json
@@ -0,0 +1,4 @@
+cookie-cutter
+-------------
+
+Set and get cookies in the browser.
@@ -0,0 +1,7 @@
+<html>
+<head>
+<script type="text/javascript" src="/browserify.js"></script>
+</head>
+<body>
+</body>
+</html>
@@ -0,0 +1,7 @@
+var cookie = require('cookie-cutter');
+var times = parseInt(cookie.get('times'), 10) || 0;
+cookie.set('times', times + 1);
+
+window.onload = function () {
+ document.body.innerHTML += times + ' times';
+};
@@ -0,0 +1,18 @@
+var http = require('http');
+var fs = require('fs');
+var browserify = require('browserify');
+var bundle = browserify({ entry : __dirname + '/main.js', }).bundle();
+
+var server = http.createServer(function (req, res) {
+ if (req.url === '/browserify.js') {
+ res.setHeader('content-type', 'text/javascript');
+ res.end(bundle);
+ }
+ else {
+ res.setHeader('content-type', 'text/html');
+ fs.createReadStream(__dirname + '/index.html').pipe(res);
+ }
+});
+
+server.listen(5000);
+console.log('Listening on :5000');
@@ -0,0 +1,29 @@
+var exports = module.exports = function (doc) {
+ if (doc.cookie === undefined) doc.cookie = '';
+
+ var self = {};
+ self.get = function (key) {
+ var splat = doc.cookie.split(/;\s*/);
+ for (var i = 0; i < splat.length; i++) {
+ var ps = splat[i].split('=');
+ var k = unescape(ps[0]);
+ if (k === key) return unescape(ps[1]);
+ }
+ return undefined;
+ };
+
+ self.set = function (key, value, opts) {
+ if (!opts) opts = {};
+ var s = escape(key) + '=' + escape(value);
+ if (opts.expires) s += '; expires=' + opts.expires;
+ if (opts.path) s += '; path=' + escape(opts.path);
+ doc.cookie = s;
+ };
+ return self;
+};
+
+if (typeof document !== 'undefined') {
+ var cookie = exports(document);
+ exports.get = cookie.get;
+ exports.set = cookie.set;
+}
@@ -0,0 +1,24 @@
+{
+ "name" : "cookie-cutter",
+ "description" : "Browserify-compatible module to get and set cookies in the browser",
+ "version" : "0.0.0",
+ "repository" : {
+ "type" : "git",
+ "url" : "git://github.com/substack/cookie-cutter.git"
+ },
+ "main" : "index.js",
+ "keywords" : [
+ "cookie",
+ "browser",
+ "browserify"
+ ],
+ "directories" : {
+ "lib" : "."
+ },
+ "license" : "MIT",
+ "author" : {
+ "name" : "James Halliday",
+ "email" : "mail@substack.net",
+ "url" : "http://substack.net"
+ }
+}

0 comments on commit def1196

Please sign in to comment.