Skip to content

Commit

Permalink
build(deps-dev): replace standard with neostandard (#282)
Browse files Browse the repository at this point in the history
* build(deps-dev): replace standard with neostandard

* chore: add eslint.config.js

* fix lint

* test(types): expect error when required `secret` option missing

---------

Co-authored-by: Aras Abbasi <[email protected]>
  • Loading branch information
Fdawgs and Uzlopak authored Dec 12, 2024
1 parent 7283329 commit 4b996a3
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 88 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![CI](https://github.com/fastify/session/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/session/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/@fastify/session.svg?style=flat)](https://www.npmjs.com/package/@fastify/session)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)

A session plugin for [fastify](http://fastify.dev/).
Requires the [@fastify/cookie](https://github.com/fastify/fastify-cookie) plugin.
Expand Down
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict'

module.exports = require('neostandard')({
ignores: require('neostandard').resolveIgnoresFromGitignore(),
ts: true
})
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"test:unit": "c8 --100 node --test",
"test:typescript": "tsd",
"benchmark": "node benchmark/bench.js",
"lint": "standard lib/* test/* benchmark/*.js",
"lint:fix": "standard --fix"
"lint": "eslint",
"lint:fix": "eslint --fix"
},
"keywords": [
"session",
Expand Down Expand Up @@ -40,8 +40,8 @@
"cronometro": "^4.0.0",
"fastify": "^5.0.0",
"ioredis": "^5.3.2",
"neostandard": "^0.11.9",
"session-file-store": "^1.5.0",
"standard": "^17.1.0",
"tsd": "^0.31.0"
},
"types": "types/types.d.ts",
Expand Down
27 changes: 13 additions & 14 deletions types/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// <reference types='node' />

import type * as Fastify from 'fastify';
import { FastifyPluginCallback } from 'fastify';
import { CookieSerializeOptions } from "@fastify/cookie"
import type * as Fastify from 'fastify'
import { FastifyPluginCallback } from 'fastify'
import { CookieSerializeOptions } from '@fastify/cookie'

declare module 'fastify' {
interface FastifyInstance {
Expand All @@ -26,8 +26,8 @@ type FastifySession = FastifyPluginCallback<fastifySession.FastifySessionOptions
MemoryStore: fastifySession.MemoryStore,
}

type Callback = (err?: any) => void;
type CallbackSession = (err: any, result?: Fastify.Session | null) => void;
type Callback = (err?: any) => void
type CallbackSession = (err: any, result?: Fastify.Session | null) => void

interface ExpressSessionData {
/** The cookie properties as defined by express-session */
Expand Down Expand Up @@ -180,25 +180,24 @@ declare namespace fastifySession {
}

export class MemoryStore implements fastifySession.SessionStore {
constructor(map?: Map<string, Fastify.Session>);
set(
constructor (map?: Map<string, Fastify.Session>)
set (
sessionId: string,
session: Fastify.Session,
callback: Callback
): void;
get(
): void
get (
sessionId: string,
callback: CallbackSession
): void;
destroy(sessionId: string, callback: Callback): void;
): void
destroy (sessionId: string, callback: Callback): void
}

export const Store: MemoryStore;
export const Store: MemoryStore

export const fastifySession: FastifySession
export { fastifySession as default }
}

declare function fastifySession(...params: Parameters<FastifySession>): ReturnType<FastifySession>
declare function fastifySession (...params: Parameters<FastifySession>): ReturnType<FastifySession>
export = fastifySession

140 changes: 70 additions & 70 deletions types/types.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import MongoStore from 'connect-mongo';
import RedisStore from 'connect-redis';
import MongoStore from 'connect-mongo'
import RedisStore from 'connect-redis'
import fastify, {
FastifyInstance,
FastifyReply,
FastifyRequest,
Session
} from 'fastify';
import Redis from 'ioredis';
import { expectAssignable, expectNotAssignable, expectDocCommentIncludes, expectError, expectType } from 'tsd';
import { CookieOptions, MemoryStore, SessionStore, default as fastifySession, default as plugin } from '..';
} from 'fastify'
import Redis from 'ioredis'
import { expectAssignable, expectNotAssignable, expectDocCommentIncludes, expectError, expectType } from 'tsd'
import { CookieOptions, MemoryStore, SessionStore, default as fastifySession, default as plugin } from '..'

class EmptyStore {
set(_sessionId: string, _session: any, _callback: Function) {}
set (_sessionId: string, _session: any, _callback: Function) {}

get(_sessionId: string, _callback: Function) {}
get (_sessionId: string, _callback: Function) {}

destroy(_sessionId: string, _callback: Function) {}
destroy (_sessionId: string, _callback: Function) {}
}

declare module 'fastify' {
Expand All @@ -30,104 +30,104 @@ declare module 'fastify' {
expectType<SessionStore>(plugin.Store)
expectType<SessionStore>(plugin.MemoryStore)

const secret = 'ABCDEFGHIJKLNMOPQRSTUVWXYZ012345';
const secret = 'ABCDEFGHIJKLNMOPQRSTUVWXYZ012345'

const app: FastifyInstance = fastify();
app.register(plugin);
app.register(plugin, { secret: 'DizIzSecret' });
app.register(plugin, { secret: 'DizIzSecret', rolling: true });
const app: FastifyInstance = fastify()
app.register(plugin, { secret: 'DizIzSecret' })
app.register(plugin, { secret: 'DizIzSecret', rolling: true })
app.register(plugin, {
secret,
rolling: false,
cookie: {
secure: false
}
});
})
app.register(plugin, {
secret,
cookie: {
maxAge: 1000,
secure: 'auto'
}
});
})

const cookieMaxAge: CookieOptions = {};
expectDocCommentIncludes<"millisecond">(cookieMaxAge.maxAge);
const cookieMaxAge: CookieOptions = {}
expectDocCommentIncludes<'millisecond'>(cookieMaxAge.maxAge)

app.register(plugin, {
secret,
store: new EmptyStore()
});
})
app.register(plugin, {
secret,
store: new RedisStore({ client: new Redis() })
});
})
app.register(plugin, {
secret,
store: MongoStore.create({ mongoUrl: 'mongodb://connection-string'})
});
store: MongoStore.create({ mongoUrl: 'mongodb://connection-string' })
})
app.register(plugin, {
secret,
store: new MemoryStore(new Map<string, Session>())
});
})
app.register(plugin, {
secret,
idGenerator: () => Date.now() + ''
});
})
app.register(plugin, {
secret,
});
})
app.register(plugin, {
secret,
idGenerator: (request) => `${request == undefined ? 'null' : request.ip}-${Date.now()}`
});
idGenerator: (request) => `${request === undefined ? 'null' : request.ip}-${Date.now()}`
})

expectError(app.register(plugin, {}));
expectError(app.register(plugin))
expectError(app.register(plugin, {}))

expectError(app.decryptSession<string>('sessionId', {}, () => ({})))
app.decryptSession<{hello: 'world'}>('sessionId', { hello: 'world' }, () => ({}))
app.decryptSession<{hello: 'world'}>('sessionId', { hello: 'world' }, { domain: '/' }, () => ({}))
app.decryptSession<{ hello: 'world' }>('sessionId', { hello: 'world' }, () => ({}))
app.decryptSession<{ hello: 'world' }>('sessionId', { hello: 'world' }, { domain: '/' }, () => ({}))
app.decryptSession('sessionId', {}, () => ({}))
app.decryptSession('sessionId', {}, { domain: '/' }, () => ({}))

app.route({
method: 'GET',
url: '/',
preHandler(req, _rep, next) {
expectType<void>(req.session.destroy(next));
expectType<Promise<void>>(req.session.destroy());
preHandler (req, _rep, next) {
expectType<void>(req.session.destroy(next))
expectType<Promise<void>>(req.session.destroy())
},
async handler(request, reply) {
expectType<FastifyRequest>(request);
expectType<FastifyReply>(reply);
expectType<Readonly<SessionStore>>(request.sessionStore);
expectError((request.sessionStore = null));
expectError(request.session.doesNotExist());
expectType<{ id: number } | undefined>(request.session.user);
request.sessionStore.set('session-set-test', request.session, () => {});
async handler (request, reply) {
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
expectType<Readonly<SessionStore>>(request.sessionStore)
expectError((request.sessionStore = null))
expectError(request.session.doesNotExist())
expectType<{ id: number } | undefined>(request.session.user)
request.sessionStore.set('session-set-test', request.session, () => {})
request.sessionStore.get('', (err, session) => {
const store = new MemoryStore();
if (session) store.set('session-set-test', session, () => {});
expectType<any>(err);
expectType<Session | null | undefined>(session);
expectType<{ id: number } | undefined>(session?.user);
});
expectType<void>(request.session.set('foo', 'bar'));
expectType<string | undefined>(request.session.get('foo'));
expectType<void>(request.session.touch());
expectType<boolean>(request.session.isModified());
expectType<void>(request.session.reload(() => {}));
expectType<void>(request.session.destroy(() => {}));
expectType<void>(request.session.regenerate(() => {}));
expectType<void>(request.session.regenerate(['foo'], () => {}));
expectType<void>(request.session.save(() => {}));
expectType<Promise<void>>(request.session.reload());
expectType<Promise<void>>(request.session.destroy());
expectType<Promise<void>>(request.session.regenerate());
expectType<Promise<void>>(request.session.regenerate(['foo']));
expectType<Promise<void>>(request.session.save());
const store = new MemoryStore()
if (session) store.set('session-set-test', session, () => {})
expectType<any>(err)
expectType<Session | null | undefined>(session)
expectType<{ id: number } | undefined>(session?.user)
})
expectType<void>(request.session.set('foo', 'bar'))
expectType<string | undefined>(request.session.get('foo'))
expectType<void>(request.session.touch())
expectType<boolean>(request.session.isModified())
expectType<void>(request.session.reload(() => {}))
expectType<void>(request.session.destroy(() => {}))
expectType<void>(request.session.regenerate(() => {}))
expectType<void>(request.session.regenerate(['foo'], () => {}))
expectType<void>(request.session.save(() => {}))
expectType<Promise<void>>(request.session.reload())
expectType<Promise<void>>(request.session.destroy())
expectType<Promise<void>>(request.session.regenerate())
expectType<Promise<void>>(request.session.regenerate(['foo']))
expectType<Promise<void>>(request.session.save())
}
});
})

const customSigner = {
sign: (value: string) => value,
Expand All @@ -136,19 +136,19 @@ const customSigner = {
renew: false,
value: null
})
};
}

app.register(plugin, { secret: customSigner });
app.register(plugin, { secret: customSigner })

const app2 = fastify()
app2.register(fastifySession)
app2.register(fastifySession, { secret: 'DizIzSecret' })

app2.get('/', async function(request) {
expectAssignable<string | undefined>(request.session.get('foo'));
expectNotAssignable<number | undefined>(request.session.get('foo'));
app2.get('/', async function (request) {
expectAssignable<string | undefined>(request.session.get('foo'))
expectNotAssignable<number | undefined>(request.session.get('foo'))

expectType<void>(request.session.set('foo', 'bar'));
expectError(request.session.set('foo', 2));
expectType<void>(request.session.set('foo', 'bar'))
expectError(request.session.set('foo', 2))

expectType<undefined | { id: number }>(request.session.get('user'))
expectAssignable(request.session.set('user', { id: 2 }))
Expand Down

0 comments on commit 4b996a3

Please sign in to comment.