diff --git a/index.js b/index.js index 565204d..8dd42a6 100644 --- a/index.js +++ b/index.js @@ -211,7 +211,7 @@ function fastifySecureSession (fastify, options, next) { return session }) - fastify.decorate('createSecureSession', (data) => new Proxy(new Session(data), sessionProxyHandler)) + fastify.decorate('createSecureSession', (data = {}) => new Proxy(new Session(data), sessionProxyHandler)) fastify.decorate('encodeSecureSession', (session, sessionName = defaultSessionName) => { if (!sessionNames.has(sessionName)) { diff --git a/test/decorators.test.js b/test/decorators.test.js index 7ce34cc..39accf0 100644 --- a/test/decorators.test.js +++ b/test/decorators.test.js @@ -19,6 +19,9 @@ test('it exposes encode and decode decorators for other libraries to use', async t.assert.strictEqual(session.get('foo'), 'bar') t.assert.strictEqual(typeof session.get('somethingElse'), 'undefined') + const emptySession = fastify.createSecureSession() + t.assert.deepStrictEqual(emptySession.data(), {}) + t.assert.ok(fastify.encodeSecureSession(fastify.createSecureSession({ foo: 'bar' }))) t.assert.ok(fastify.encodeSecureSession(fastify.createSecureSession({}))) t.assert.notEqual(fastify.encodeSecureSession(fastify.createSecureSession({ foo: 'bar' })), fastify.encodeSecureSession(fastify.createSecureSession({})))