Skip to content

Commit

Permalink
Set cookie properly 🤦
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangvvo committed Sep 2, 2020
1 parent 1763a50 commit b8bb0cd
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,36 @@ const stringify = (sess: SessionData) =>
);

const SESS_PREV = Symbol('session#prev');
const SESS_TOUCHED = Symbol('session#touched')
const SESS_TOUCHED = Symbol('session#touched');

const commitHead = (
req: IncomingMessage & { session?: SessionData | null },
res: ServerResponse,
options: SessionOptions,
options: SessionOptions
) => {
if (res.headersSent || !req.session) return;
if (req.session.isNew || (options.rolling && (req as any)[SESS_TOUCHED])) {
res.setHeader(
'Set-Cookie',
serialize(
options.name,
options.encode ? options.encode(req.session.id) : req.session.id
options.encode ? options.encode(req.session.id) : req.session.id,
{
path: options.cookie.path,
httpOnly: options.cookie.httpOnly,
expires: options.cookie.expires,
domain: options.cookie.domain,
sameSite: options.cookie.sameSite,
secure: options.cookie.secure,
}
)
);
}
};

const save = async (
req: IncomingMessage & { session?: SessionData | null },
options: SessionOptions,
options: SessionOptions
) => {
if (!req.session) return;
const obj: SessionData = {} as any;
Expand All @@ -62,42 +70,49 @@ const save = async (
}
};

function setupStore(store: SessionStore | ExpressStore | NormalizedSessionStore) {
function setupStore(
store: SessionStore | ExpressStore | NormalizedSessionStore
) {
if ('__normalized' in store) return store;
const s = (store as unknown) as NormalizedSessionStore;

s.__destroy = function destroy(sid) {
return new Promise((resolve, reject) => {
const done = (err: any) => err ? reject(err) : resolve()
const done = (err: any) => (err ? reject(err) : resolve());
const result = this.destroy(sid, done);
if (result && typeof result.then === 'function') result.then(resolve, reject);
})
}
if (result && typeof result.then === 'function')
result.then(resolve, reject);
});
};

s.__get = function get(sid) {
return new Promise((resolve, reject) => {
const done = (err: any, val: SessionData) => err ? reject(err) : resolve(val)
const done = (err: any, val: SessionData) =>
err ? reject(err) : resolve(val);
const result = this.get(sid, done);
if (result && typeof result.then === 'function') result.then(resolve, reject);
})
}
if (result && typeof result.then === 'function')
result.then(resolve, reject);
});
};

s.__set = function set(sid, sess) {
return new Promise((resolve, reject) => {
const done = (err: any) => err ? reject(err) : resolve();
const done = (err: any) => (err ? reject(err) : resolve());
const result = this.set(sid, sess, done);
if (result && typeof result.then === 'function') result.then(resolve, reject);
})
}
if (result && typeof result.then === 'function')
result.then(resolve, reject);
});
};

if (store.touch) {
s.__touch = function touch(sid, sess) {
return new Promise((resolve, reject) => {
const done = (err: any) => err ? reject(err) : resolve();
const done = (err: any) => (err ? reject(err) : resolve());
const result = this.touch(sid, sess, done);
if (result && typeof result.then === 'function') result.then(resolve, reject);
})
}
if (result && typeof result.then === 'function')
result.then(resolve, reject);
});
};
}

s.__normalized = true;
Expand Down Expand Up @@ -157,6 +172,7 @@ export async function applySession<T = {}>(
};

if (sess) {
(req as any)[SESS_PREV] = stringify(sess);
const { cookie, ...data } = sess;
if (typeof cookie.expires === 'string')
cookie.expires = new Date(cookie.expires);
Expand All @@ -169,6 +185,7 @@ export async function applySession<T = {}>(
};
for (const key in data) req.session[key] = data[key];
} else {
(req as any)[SESS_PREV] = '{}';
req.session = {
cookie: options.cookie,
commit,
Expand All @@ -180,14 +197,17 @@ export async function applySession<T = {}>(
}

// Extend session expiry
if (((req as any)[SESS_TOUCHED] = shouldTouch(req.session.cookie, options.touchAfter))) {
if (
((req as any)[SESS_TOUCHED] = shouldTouch(
req.session.cookie,
options.touchAfter
))
) {
req.session.cookie.expires = new Date(
Date.now() + req.session.cookie.maxAge! * 1000
);
}

(req as any)[SESS_PREV] = sess ? stringify(sess) : '{}';

// autocommit
if (options.autoCommit) {
const oldWritehead = res.writeHead;
Expand Down

0 comments on commit b8bb0cd

Please sign in to comment.