From c6ef7d8282047b208052a6fab320fd3d9e4bbce4 Mon Sep 17 00:00:00 2001 From: Hoang Vo Date: Mon, 20 Sep 2021 19:08:51 +0700 Subject: [PATCH] Cleanup --- jest.config.js | 5 ----- package.json | 6 +++--- src/compat.ts | 7 +++---- src/store/memory.ts | 2 +- test/unit/index.test.ts | 29 +++++++++++++++++++---------- yarn.lock | 36 +++++++++++++----------------------- 6 files changed, 39 insertions(+), 46 deletions(-) diff --git a/jest.config.js b/jest.config.js index c0235cc..2b50ca1 100644 --- a/jest.config.js +++ b/jest.config.js @@ -6,9 +6,4 @@ module.exports = { testMatch: ['**/*.test.ts'], bail: true, verbose: false, - globals: { - 'ts-jest': { - diagnostics: false, - }, - }, }; diff --git a/package.json b/package.json index 56e6c99..0a76154 100644 --- a/package.json +++ b/package.json @@ -33,11 +33,11 @@ "homepage": "https://github.com/hoangvvo/next-session#readme", "devDependencies": { "@types/cookie": "^0.4.1", + "@types/cookie-signature": "^1.0.3", "@types/express-session": "^1.17.4", "@types/jest": "^27.0.1", - "@types/node": "^16.9.3", - "@types/react": "^17.0.21", - "@types/react-dom": "^17.0.9", + "@types/node": "^16.9.4", + "@types/react": "^17.0.22", "@types/supertest": "^2.0.11", "@typescript-eslint/eslint-plugin": "^4.31.1", "@typescript-eslint/parser": "^4.31.1", diff --git a/src/compat.ts b/src/compat.ts index d7d2c78..9046b28 100644 --- a/src/compat.ts +++ b/src/compat.ts @@ -1,19 +1,18 @@ import { EventEmitter } from 'events'; +import { Store as IExpressStore } from 'express-session'; import { callbackify, inherits } from 'util'; import MemoryStore from './store/memory'; // no-op for compat function expressSession(options?: any): any {} -function ExpressStore() { - // @ts-ignore +function ExpressStore(this: IExpressStore) { EventEmitter.call(this); } inherits(ExpressStore, EventEmitter); expressSession.Store = ExpressStore; -function CallbackMemoryStore() { - // @ts-ignore +function CallbackMemoryStore(this: MemoryStore) { this.store = new Map(); } inherits(CallbackMemoryStore, ExpressStore); diff --git a/src/store/memory.ts b/src/store/memory.ts index aec22e9..fd8998d 100644 --- a/src/store/memory.ts +++ b/src/store/memory.ts @@ -1,7 +1,7 @@ import { SessionData, SessionStore } from '../types'; export default class MemoryStore implements SessionStore { - private store = new Map(); + store = new Map(); async get(sid: string): Promise { const sess = this.store.get(sid); diff --git a/test/unit/index.test.ts b/test/unit/index.test.ts index 4c51286..53d0b44 100644 --- a/test/unit/index.test.ts +++ b/test/unit/index.test.ts @@ -1,9 +1,8 @@ -// @ts-nocheck import { parse as parseCookie } from 'cookie'; import signature from 'cookie-signature'; import EventEmitter from 'events'; import { Store as ExpressStore } from 'express-session'; -import { createServer, IncomingMessage, RequestListener } from 'http'; +import { createServer, IncomingMessage, ServerResponse } from 'http'; import { NextApiHandler, NextComponentType, NextPage } from 'next'; import React from 'react'; import request from 'supertest'; @@ -16,10 +15,15 @@ import { withSession, } from '../../src'; import MemoryStore from '../../src/store/memory'; -import { Options } from '../../src/types'; +import { Options, Session } from '../../src/types'; const CbStore = expressSession.MemoryStore; +type RequestListener = ( + req: IncomingMessage & { session: Session }, + res: ServerResponse +) => void; + const defaultHandler: RequestListener = async (req, res) => { if (req.method === 'POST') { req.session.views = req.session.views ? req.session.views + 1 : 1; @@ -35,11 +39,13 @@ function setUpServer( options?: false | Options, prehandler?: RequestListener ) { - const server = createServer(async (req: IncomingMessage, res) => { + const server = createServer(async (req, res) => { + // @ts-ignore if (prehandler) await prehandler(req, res); if (options !== false) { await applySession(req as any, res, options); } + // @ts-ignore await handler(req, res); }); return server; @@ -228,7 +234,7 @@ describe('applySession', () => { const store = new MemoryStore(); const decodeFn = (key: string) => (raw: string) => - signature.unsign(raw.slice(2), key); + signature.unsign(raw.slice(2), key) || null; const encodeFn = (key: string) => (sessId: string) => sessId && `s:${signature.sign(sessId, key)}`; const server = setUpServer( @@ -422,7 +428,8 @@ describe('Store', () => { describe('callback store', () => { it('should work', async () => { const server = setUpServer(defaultHandler, { - store: new CbStore() as unknown as ExpressStore, + // @ts-ignore + store: new CbStore(), }); const agent = request.agent(server); await agent @@ -440,7 +447,8 @@ describe('callback store', () => { }); it('should work (with touch)', async () => { const server = setUpServer(defaultHandler, { - store: new CbStore() as unknown as ExpressStore, + // @ts-ignore + store: new CbStore(), cookie: { maxAge: 100 }, touchAfter: 0, }); @@ -454,7 +462,8 @@ describe('callback store', () => { await agent.get('/').expect('2'); }); it('should work (without touch)', async () => { - const store = new CbStore() as unknown as ExpressStore; + // @ts-ignore + const store = new CbStore(); delete store.touch; const server = setUpServer(defaultHandler, { store }); expect(store).not.toHaveProperty('__touch'); @@ -476,6 +485,7 @@ describe('callback store', () => { describe('promisifyStore', () => { test('should returns the store itself (with console.warn)', async () => { + // @ts-ignore const store = new CbStore(); expect(promisifyStore(store as unknown as ExpressStore)).toBe(store); }); @@ -511,8 +521,7 @@ describe('MemoryStore', () => { expect(await sessionStore.get(sessionId as string)).toBeNull(); // Touch will return undefind expect( - // @ts-ignore - await sessionStore.touch(sessionId as string, sessionInstance) + await sessionStore.touch(sessionId as string, sessionInstance!) ).toBeUndefined(); }); }); diff --git a/yarn.lock b/yarn.lock index 5cd260d..bc881ef 100644 --- a/yarn.lock +++ b/yarn.lock @@ -925,6 +925,11 @@ dependencies: "@types/node" "*" +"@types/cookie-signature@^1.0.3": + version "1.0.3" + resolved "https://registry.yarnpkg.com/@types/cookie-signature/-/cookie-signature-1.0.3.tgz#4761a0c31ad159306564a6b41a3f8b014c47eab2" + integrity sha512-0jpd4oizLaBybWBCew/lx4iAjjNWmCfzbspvnjoppyGEquN51BjShmPCksiH2IXLwtxQ2F/e2PkFy0LL7jpLsw== + "@types/cookie@^0.4.1": version "0.4.1" resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.4.1.tgz#bfd02c1f2224567676c1545199f87c3a861d878d" @@ -1010,10 +1015,10 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.2.tgz#d25295f9e4ca5989a2c610754dc02a9721235eeb" integrity sha512-jeYJU2kl7hL9U5xuI/BhKPZ4vqGM/OmK6whiFAXVhlstzZhVamWhDSmHyGLIp+RVyuF9/d0dqr2P85aFj4BvJg== -"@types/node@^16.9.3": - version "16.9.3" - resolved "https://registry.yarnpkg.com/@types/node/-/node-16.9.3.tgz#92230abb80a66beb10236e78a51cd9e6396f4489" - integrity sha512-5UmMznRvrwKqisJ458JbNoq3AyXHxlAKMkGtNe143W1SkZ1BVgvCHYBzn7wD66J+smE+BolqA1mes5BeXlWY6w== +"@types/node@^16.9.4": + version "16.9.4" + resolved "https://registry.yarnpkg.com/@types/node/-/node-16.9.4.tgz#a12f0ee7847cf17a97f6fdf1093cb7a9af23cca4" + integrity sha512-KDazLNYAGIuJugdbULwFZULF9qQ13yNWEBFnfVpqlpgAAo6H/qnM9RjBgh0A0kmHf3XxAKLdN5mTIng9iUvVLA== "@types/prettier@^2.1.5": version "2.3.2" @@ -1035,25 +1040,10 @@ resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.3.tgz#7ee330ba7caafb98090bece86a5ee44115904c2c" integrity sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA== -"@types/react-dom@^17.0.9": - version "17.0.9" - resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-17.0.9.tgz#441a981da9d7be117042e1a6fd3dac4b30f55add" - integrity sha512-wIvGxLfgpVDSAMH5utdL9Ngm5Owu0VsGmldro3ORLXV8CShrL8awVj06NuEXFQ5xyaYfdca7Sgbk/50Ri1GdPg== - dependencies: - "@types/react" "*" - -"@types/react@*": - version "16.9.53" - resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.53.tgz#40cd4f8b8d6b9528aedd1fff8fcffe7a112a3d23" - integrity sha512-4nW60Sd4L7+WMXH1D6jCdVftuW7j4Za6zdp6tJ33Rqv0nk1ZAmQKML9ZLD4H0dehA3FZxXR/GM8gXplf82oNGw== - dependencies: - "@types/prop-types" "*" - csstype "^3.0.2" - -"@types/react@^17.0.21": - version "17.0.21" - resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.21.tgz#069c43177cd419afaab5ce26bb4e9056549f7ea6" - integrity sha512-GzzXCpOthOjXvrAUFQwU/svyxu658cwu00Q9ugujS4qc1zXgLFaO0kS2SLOaMWLt2Jik781yuHCWB7UcYdGAeQ== +"@types/react@^17.0.22": + version "17.0.22" + resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.22.tgz#c80d1d0e87fe953bae3ab273bef451dea1a6291b" + integrity sha512-kq/BMeaAVLJM6Pynh8C2rnr/drCK+/5ksH0ch9asz+8FW3DscYCIEFtCeYTFeIx/ubvOsMXmRfy7qEJ76gM96A== dependencies: "@types/prop-types" "*" "@types/scheduler" "*"