Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangvvo committed Sep 20, 2021
1 parent 6816ae9 commit c6ef7d8
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 46 deletions.
5 changes: 0 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@ module.exports = {
testMatch: ['**/*.test.ts'],
bail: true,
verbose: false,
globals: {
'ts-jest': {
diagnostics: false,
},
},
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 3 additions & 4 deletions src/compat.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/store/memory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SessionData, SessionStore } from '../types';

export default class MemoryStore implements SessionStore {
private store = new Map<string, string>();
store = new Map<string, string>();

async get(sid: string): Promise<SessionData | null> {
const sess = this.store.get(sid);
Expand Down
29 changes: 19 additions & 10 deletions test/unit/index.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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,
});
Expand All @@ -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');
Expand All @@ -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);
});
Expand Down Expand Up @@ -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();
});
});
36 changes: 13 additions & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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" "*"
Expand Down

0 comments on commit c6ef7d8

Please sign in to comment.