fix(session-query): validate before service registration
This commit is contained in:
@@ -191,8 +191,10 @@ export class SessionQuerySqlite extends SessionQueryService {
|
|||||||
private readonly _optionalPersistenceFiber: Fiber
|
private readonly _optionalPersistenceFiber: Fiber
|
||||||
|
|
||||||
constructor(ctx: Context, config: Config) {
|
constructor(ctx: Context, config: Config) {
|
||||||
super(ctx, config)
|
// The assignment expression resolves before the base constructor can
|
||||||
this.config = resolveConfig(config)
|
// register `ctx.sessionQuery`; keep that same validated value afterward.
|
||||||
|
super(ctx, config = resolveConfig(config))
|
||||||
|
this.config = config as ResolvedConfig
|
||||||
this._ready = this._open()
|
this._ready = this._open()
|
||||||
this._optionalPersistenceFiber = ctx.inject(['sessionPersistence'], (childCtx: Context) => {
|
this._optionalPersistenceFiber = ctx.inject(['sessionPersistence'], (childCtx: Context) => {
|
||||||
const service = childCtx.sessionPersistence
|
const service = childCtx.sessionPersistence
|
||||||
@@ -919,6 +921,9 @@ function resolveConfig(config: Config): ResolvedConfig {
|
|||||||
assertPageLimit('defaultLimit', resolved.defaultLimit)
|
assertPageLimit('defaultLimit', resolved.defaultLimit)
|
||||||
assertPageLimit('maxLimit', resolved.maxLimit)
|
assertPageLimit('maxLimit', resolved.maxLimit)
|
||||||
assertPositiveInteger('snippetChars', resolved.snippetChars)
|
assertPositiveInteger('snippetChars', resolved.snippetChars)
|
||||||
|
if (!Number.isInteger(resolved.readWindowMax) || resolved.readWindowMax < 0) {
|
||||||
|
throw invalidConfig('readWindowMax must be a non-negative integer')
|
||||||
|
}
|
||||||
if (resolved.defaultLimit > resolved.maxLimit) {
|
if (resolved.defaultLimit > resolved.maxLimit) {
|
||||||
throw invalidConfig('defaultLimit must be less than or equal to maxLimit')
|
throw invalidConfig('defaultLimit must be less than or equal to maxLimit')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -482,6 +482,7 @@ describe('SQLite session search', () => {
|
|||||||
{ path: ':memory:', defaultLimit: 1e100 },
|
{ path: ':memory:', defaultLimit: 1e100 },
|
||||||
{ path: ':memory:', maxLimit: 1e100 },
|
{ path: ':memory:', maxLimit: 1e100 },
|
||||||
{ path: ':memory:', snippetChars: 0 },
|
{ path: ':memory:', snippetChars: 0 },
|
||||||
|
{ path: ':memory:', readWindowMax: -1 },
|
||||||
{ path: ':memory:', defaultLimit: 3, maxLimit: 2 },
|
{ path: ':memory:', defaultLimit: 3, maxLimit: 2 },
|
||||||
{ path: ':memory:', journalMode: 'memory' },
|
{ path: ':memory:', journalMode: 'memory' },
|
||||||
]) {
|
]) {
|
||||||
@@ -489,6 +490,7 @@ describe('SQLite session search', () => {
|
|||||||
await direct.plugin(SessionStore)
|
await direct.plugin(SessionStore)
|
||||||
expect(() => new SessionQuerySqlite(direct, config as never))
|
expect(() => new SessionQuerySqlite(direct, config as never))
|
||||||
.toThrow(expectCode('SESSION_QUERY_INVALID_CONFIG'))
|
.toThrow(expectCode('SESSION_QUERY_INVALID_CONFIG'))
|
||||||
|
expect(direct.sessionQuery).toBeUndefined()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user