made the content object the target of the proxy, so that natural enumeration of keys and such is naturally proxied
This commit is contained in:
Родитель
e907048f11
Коммит
f21d1348db
|
@ -43,8 +43,16 @@ function Session(req, res, cookies, opts) {
|
|||
}
|
||||
|
||||
Session.prototype = {
|
||||
clearContent: function() {
|
||||
var self = this;
|
||||
Object.keys(this.content).forEach(function(k) {
|
||||
delete self.content[k];
|
||||
});
|
||||
},
|
||||
|
||||
clear: function() {
|
||||
this.content = {};
|
||||
this.clearContent();
|
||||
this.dirty = true;
|
||||
this.updateCookie();
|
||||
},
|
||||
|
||||
|
@ -73,7 +81,7 @@ Session.prototype = {
|
|||
},
|
||||
|
||||
unbox: function(content) {
|
||||
this.content = {};
|
||||
this.clearContent();
|
||||
|
||||
// stop at any time if there's an issue
|
||||
|
||||
|
@ -97,9 +105,13 @@ Session.prototype = {
|
|||
plaintext += cipher.final();
|
||||
|
||||
try {
|
||||
this.content = JSON.parse(plaintext);
|
||||
var new_content = JSON.parse(plaintext);
|
||||
var self = this;
|
||||
Object.keys(new_content).forEach(function(k) {
|
||||
self.content[k] = new_content[k];
|
||||
});
|
||||
} catch (x) {
|
||||
this.content = {};
|
||||
// do nothing
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -113,7 +125,7 @@ Session.prototype = {
|
|||
if (cookie) {
|
||||
this.unbox(cookie);
|
||||
} else {
|
||||
this.content = {};
|
||||
this.clearContent();
|
||||
}
|
||||
|
||||
this.loaded = true;
|
||||
|
@ -122,17 +134,19 @@ Session.prototype = {
|
|||
// called to create a proxy that monitors the session
|
||||
// for new properties being set
|
||||
monitor: function() {
|
||||
// set up proxies
|
||||
var sessionHandler = new Handler(this);
|
||||
// the target for the proxy will be the content
|
||||
// variable, to simplify the proxying
|
||||
var sessionHandler = new Handler(this.content);
|
||||
var raw_session = this;
|
||||
|
||||
// all values from content except special values
|
||||
sessionHandler.get = function(rcvr, name) {
|
||||
if (['clear', 'setExpires'].indexOf(name) > -1) {
|
||||
return this.target[name].bind(this.target);
|
||||
return raw_session[name].bind(raw_session);
|
||||
} else {
|
||||
if (!this.target.loaded)
|
||||
this.target.loadFromCookie();
|
||||
return this.target.content[name];
|
||||
if (!raw_session.loaded)
|
||||
raw_session.loadFromCookie();
|
||||
return this.target[name];
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -140,22 +154,22 @@ Session.prototype = {
|
|||
sessionHandler.set = function(rcvr, name, value) {
|
||||
// we have to load existing content, otherwise it will later override
|
||||
// the content that is written.
|
||||
if (!this.target.loaded)
|
||||
this.target.loadFromCookie();
|
||||
if (!raw_session.loaded)
|
||||
raw_session.loadFromCookie();
|
||||
|
||||
this.target.content[name] = value;
|
||||
this.target.dirty = true;
|
||||
this.target[name] = value;
|
||||
raw_session.dirty = true;
|
||||
};
|
||||
|
||||
// if key is deleted
|
||||
sessionHandler.delete = function(name) {
|
||||
// we have to load existing content, otherwise it will later override
|
||||
// the content that is written.
|
||||
if (!this.target.loaded)
|
||||
this.target.loadFromCookie();
|
||||
if (!raw_session.loaded)
|
||||
raw_session.loadFromCookie();
|
||||
|
||||
delete this.target.content[name];
|
||||
this.target.dirty = true;
|
||||
delete this.target[name];
|
||||
raw_session.dirty = true;
|
||||
};
|
||||
|
||||
var proxySession = Proxy.create(sessionHandler);
|
||||
|
|
Загрузка…
Ссылка в новой задаче