Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ module.exports = ({ Store }) => {
intervalMs:
(options.expired && options.expired.intervalMs) ||
clearExpiredInterval,
unrefInterval: (options.expired && options.expired.unrefInterval) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'd have to add any new option property to the documentation. And why do we need "|| false" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will add it to the docs if you accept it to be added. Regarding the "false", that's redundant of course and I'll remove it.

false,
};
this.client = options.client;
this.createDb();
Expand All @@ -40,10 +42,13 @@ module.exports = ({ Store }) => {
}

startInterval() {
setInterval(
const timeout = setInterval(
this.clearExpiredSessions.bind(this),
this.expired.intervalMs
);
if (this.expired.unrefInterval) {
timeout.unref();
}
}

clearExpiredSessions() {
Expand Down
63 changes: 63 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const session = require("express-session");
const { unlinkSync, existsSync } = require("fs");
const differenceInSeconds = require("date-fns/differenceInSeconds");
const add = require("date-fns/add");
const async_hooks = require("node:async_hooks");

const SqliteStore = require("../src/index.js")(session);

Expand Down Expand Up @@ -428,3 +429,65 @@ test("what happens when table is deleted and all methods are invoked (they shoul
s.length((err, res) => t.assert(err));
s.destroy(sid, (err, res) => t.assert(err));
});

function getSymbolValue(obj, symbol) {
const refedSymbol = Object.getOwnPropertySymbols(obj).find(
(s) => s.toString() === symbol
);
return obj[refedSymbol];
}

test("if the expiration interval continues being referenced", async (t) => {
const timeoutMs = 555;
let loggedResource;

function init(asyncId, type, triggerAsyncId, resource) {
if (type === "Timeout" && resource._idleTimeout === timeoutMs) {
loggedResource = resource;
t.assert(getSymbolValue(loggedResource, "Symbol(refed)"));
}
}
const asyncHook = async_hooks.createHook({
init,
});
asyncHook.enable();

const db = new sqlite(dbName, dbOptions);
const s = new SqliteStore({
client: db,
expired: {
clear: true,
intervalMs: timeoutMs,
},
});

t.assert(getSymbolValue(loggedResource, "Symbol(refed)"));
});

test("if the expiration interval is going to be unreferenced", async (t) => {
const timeoutMs = 555;
let loggedResource;

function init(asyncId, type, triggerAsyncId, resource) {
if (type === "Timeout" && resource._idleTimeout === timeoutMs) {
loggedResource = resource;
t.assert(getSymbolValue(loggedResource, "Symbol(refed)"));
}
}
const asyncHook = async_hooks.createHook({
init,
});
asyncHook.enable();

const db = new sqlite(dbName, dbOptions);
const s = new SqliteStore({
client: db,
expired: {
clear: true,
intervalMs: timeoutMs,
unrefInterval: true,
},
});

t.assert(!getSymbolValue(loggedResource, "Symbol(refed)"));
});