Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ unless the `client` options is provided to override them.
☣️ Legacy reap behaviors use DynamoDB [`scan`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-dynamodb/classes/scancommand.html)
functionality that can incur significant costs. Should instead enable [DynamoDB TTL](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html)
and select the `expires` field. TODO should we just remove it since we're already making a breaking change?
- `expiresIn` Optional set the number of seconds for DynamoDB TTL. Defaults to the cookie's maxAge.

## Usage

Expand All @@ -42,6 +43,8 @@ var options = {
],
// Optional skip throw missing special keys in session, if set true
skipThrowMissingSpecialKeys: true,
// Optional set the number of seconds for DynamoDB TTL
expiresIn: 3600,
};
```

Expand Down
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ declare namespace ConnectDynamoDB {
* Useful if the table already exists or if you want to skip existence checks in a serverless environment such as AWS Lambda.
*/
initialized?: boolean;
/**
* Set the number of seconds added to the item expiry time.
*
* Setting this to 0 will use the `maxAge` value from the session cookie.
* @default 0
*/
expiresIn?: number;
}

interface DynamoDBStoreOptionsSpecialKey {
Expand Down
9 changes: 8 additions & 1 deletion lib/connect-dynamodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ module.exports = function (connect) {
if (this.reapInterval > 0) {
this._reap = setInterval(this.reap.bind(this), this.reapInterval);
}
this.expiresIn = null == options.expiresIn ? 0 : options.expiresIn
}

/*
Expand Down Expand Up @@ -344,11 +345,17 @@ module.exports = function (connect) {
*/
DynamoDBStore.prototype.getExpiresValue = function (sess) {
const now = Math.floor(Date.now() / 1000);
const expires =

if (this.expiresIn != 0) {
return now + this.expiresIn;
}
else {
const expires =
typeof sess.cookie.maxAge === "number"
? now + (sess.cookie.maxAge / 1000)
: now + oneDayInSeconds;
return expires;
}
};

/**
Expand Down
35 changes: 35 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
DynamoDBClient,
CreateTableCommand,
DeleteTableCommand,
GetItemCommand,
ScalarAttributeType,
} = require("@aws-sdk/client-dynamodb");
const ConnectDynamoDB = require(__dirname + "/../lib/connect-dynamodb.js");
Expand Down Expand Up @@ -231,6 +232,40 @@ describe("DynamoDBStore", () => {
});
});
});

it("should set correct expiry when expiresIn option is set", async () => {
const storeWithExpiry = new DynamoDBStore({
client: client,
table: tableName,
expiresIn: 1000
});
const beforeTime = Math.floor(Date.now() / 1000);

await new Promise((resolve, reject) => {
storeWithExpiry.set(
sessionId,
{
cookie: { maxAge: 2000 },
name: "test"
},
(err) => {
if (err) return reject(err);
resolve();
}
);
});

const result = await client.send(
new GetItemCommand({
TableName: tableName,
Key: { id: { S: "sess:" + sessionId } }
})
);

const expiryValue = parseInt(result.Item.expires.N);
const expectedExpiry = beforeTime + 1000;
expiryValue.should.be.approximately(expectedExpiry, 2);
});
});

describe("Getting", () => {
Expand Down