Permalink
Browse files

JSON.stringify now works

  • Loading branch information...
substack committed Feb 27, 2011
1 parent 504dbdb commit f5d131cfdc18ac24c8e007b7d2f3a2b3e82760d7
Showing with 31 additions and 3 deletions.
  1. +4 −0 examples/memory.js
  2. +8 −2 index.js
  3. +18 −0 test/json.js
  4. +1 −1 test/memory.js
View
@@ -7,6 +7,10 @@ webserver.use(connect.router(function (app) {
req.session.times = (req.session.times || 0) + 1;
res.writeHead(200, { 'Content-Type' : 'text/plain' });
+
+ res.write('req.session = ' + JSON.stringify(req.session) + '\r\n');
+ res.write('req.sessions = ' + JSON.stringify(req.sessions) + '\r\n');
+ res.write('req.sessionID = ' + JSON.stringify(req.sessionID) + '\r\n');
res.end(req.session.times + ' times!');
});
}));
View
@@ -88,7 +88,10 @@ exports.wrap = function (store, sessions) {
return Proxy.create({
get : function (recv, name) {
- return wrap(obj[name])
+ if (name === 'toJSON' && !obj.hasOwnProperty(name)) {
+ return function () { return obj };
+ }
+ else return wrap(obj[name]);
},
set : function (recv, name, value) {
setTaint();
@@ -112,7 +115,10 @@ exports.wrap = function (store, sessions) {
return Proxy.create({
get : function (recv, name) {
- return wrapRoot(name, sessions[name]);
+ if (name === 'toJSON' && !sessions.hasOwnProperty(name)) {
+ return function () { return sessions }
+ }
+ else return wrapRoot(name, sessions[name])
},
set : function (recv, name, value) {
sessions[name] = value;
View
@@ -0,0 +1,18 @@
+var assert = require('assert');
+var sesame = require('sesame');
+
+exports.json = function () {
+ var req = sesame.wrap(null, {
+ zzz : { a : 1, b : 2 }
+ });
+
+ assert.eql(
+ JSON.stringify(req.zzz),
+ JSON.stringify({ a : 1, b : 2 })
+ );
+
+ assert.eql(
+ JSON.stringify(req),
+ JSON.stringify({ zzz : { a : 1, b : 2 } })
+ );
+};
View
@@ -6,7 +6,7 @@ var http = require('http');
var assert = require('assert');
-exports.sesame = function () {
+exports.memory = function () {
var port = 10000 + Math.floor(Math.random() * (65536 - 10000));
var web = server();

1 comment on commit f5d131c

@franck34

This comment has been minimized.

Show comment Hide comment
@franck34

franck34 Feb 27, 2011

Contributor

You are a boss, thx !!

Contributor

franck34 commented on f5d131c Feb 27, 2011

You are a boss, thx !!

Please sign in to comment.