@@ -97,6 +97,8 @@ export const PAGE_TITLE = 'question.page.title';
9797export const MISSING_POSTGRES = 'question.store is postgres but the pg driver is not installed - npm install pg' ;
9898const DURATION = / ^ ( \d + ) \s * ( m s | s | m | h | d ) ? $ / ;
9999const UNIT_MS = { ms : 1 , s : 1000 , m : 60_000 , h : 3_600_000 , d : 86_400_000 } ;
100+ // the slashes a base URL ends with; the lookbehind starts a match only at a run's first slash, so the scan stays linear
101+ const TRAILING_SLASHES = / (?< ! \/ ) \/ + $ / ;
100102
101103/** A configuration the application cannot start with - the boot fails by name. */
102104export class QuestionError extends Error { }
@@ -158,7 +160,7 @@ export function loadSettings(config) {
158160 if ( ! [ 'memory' , 'postgres' ] . includes ( store ) ) throw new QuestionError ( `${ STORE } must be memory or postgres` ) ;
159161 const auth = text ( config , AUTH , 'none' ) . toLowerCase ( ) ;
160162 if ( ! AUTH_MODES . includes ( auth ) ) throw new QuestionError ( `${ AUTH } must be one of ${ AUTH_MODES . join ( ', ' ) } ` ) ;
161- const issuer = text ( config , OAUTH2_ISSUER ) . replace ( / \/ + $ / , '' ) ;
163+ const issuer = text ( config , OAUTH2_ISSUER ) . replace ( TRAILING_SLASHES , '' ) ;
162164 const jwksUrl = text ( config , OAUTH2_JWKS_URL ) ;
163165 const clientId = text ( config , OAUTH2_CLIENT_ID ) ;
164166 if ( auth === 'oauth2' ) {
@@ -224,6 +226,17 @@ export function publicRow(row) {
224226 } ;
225227}
226228
229+ /** What a wait carries beyond an advice - its turn, its ticket, link and expiry, its calls - or an AppException(400). */
230+ function validateWait ( event , turn ) {
231+ if ( turn === undefined || turn === null ) throw new AppException ( 400 , 'turn is required for an approval or a question' ) ;
232+ for ( const name of [ 'ticket' , 'link' , 'expires_at' ] ) {
233+ if ( ! isText ( event [ name ] ) || ! event [ name ] ) throw new AppException ( 400 , `${ name } is required for an approval or a question` ) ;
234+ }
235+ if ( ! Array . isArray ( event . calls ) || event . calls . length === 0 ) {
236+ throw new AppException ( 400 , 'calls must be a non-empty list for an approval or a question' ) ;
237+ }
238+ }
239+
227240/** The message event the main flow sends, checked for what a channel needs - or an AppException(400). */
228241export function validateEvent ( event ) {
229242 if ( ! isMap ( event ) ) throw new AppException ( 400 , 'the inbox takes the message event as a map' ) ;
@@ -235,15 +248,7 @@ export function validateEvent(event) {
235248 }
236249 const turn = event . turn ;
237250 if ( turn !== undefined && turn !== null && ( ! isWhole ( turn ) || turn < 0 ) ) throw new AppException ( 400 , 'turn must be a whole number >= 0' ) ;
238- if ( WAIT_KINDS . includes ( kind ) ) {
239- if ( turn === undefined || turn === null ) throw new AppException ( 400 , 'turn is required for an approval or a question' ) ;
240- for ( const name of [ 'ticket' , 'link' , 'expires_at' ] ) {
241- if ( ! isText ( event [ name ] ) || ! event [ name ] ) throw new AppException ( 400 , `${ name } is required for an approval or a question` ) ;
242- }
243- if ( ! Array . isArray ( event . calls ) || event . calls . length === 0 ) {
244- throw new AppException ( 400 , 'calls must be a non-empty list for an approval or a question' ) ;
245- }
246- }
251+ if ( WAIT_KINDS . includes ( kind ) ) validateWait ( event , turn ) ;
247252 for ( const name of [ 'details' , 'requested_at' ] ) {
248253 if ( event [ name ] !== undefined && event [ name ] !== null && ! isText ( event [ name ] ) ) throw new AppException ( 400 , `${ name } must be text` ) ;
249254 }
@@ -287,9 +292,13 @@ export class MemoryReader {
287292 this . turnRows = new Map ( ) ;
288293 }
289294
290- async open ( ) { }
295+ async open ( ) {
296+ // nothing to open: the rows are held in this process
297+ }
291298
292- async close ( ) { }
299+ async close ( ) {
300+ // nothing to close: no connection is held
301+ }
293302
294303 put ( row ) {
295304 this . rows . set ( row . id , row ) ;
@@ -562,13 +571,11 @@ export class Wakeups {
562571 * wake-up from the intake or a click, whichever comes first.
563572 */
564573export class Feed {
565- constructor ( ) {
566- this . cursor = 0 ;
567- this . wake = null ;
568- this . pending = false ;
569- this . loop = null ;
570- this . stopped = false ;
571- }
574+ cursor = 0 ;
575+ wake = null ;
576+ pending = false ;
577+ loop = null ;
578+ stopped = false ;
572579
573580 kick ( ) {
574581 if ( this . wake ) {
@@ -610,20 +617,20 @@ export class Feed {
610617 }
611618 this . pending = false ;
612619 if ( this . stopped ) break ;
613- await this . pollOnce ( ) ;
620+ await this . pollOnce ( ) ; // NOSONAR - sequential: each poll reads from the cursor the last one moved
614621 }
615622 }
616623
617624 start ( pollMs ) {
618- if ( this . loop ) return ;
625+ if ( this . loop !== null ) return ;
619626 this . stopped = false ;
620627 this . loop = this . run ( pollMs ) ;
621628 }
622629
623630 async stop ( ) {
624631 this . stopped = true ;
625632 this . kick ( ) ;
626- if ( this . loop ) await this . loop ;
633+ if ( this . loop !== null ) await this . loop ;
627634 this . loop = null ;
628635 }
629636
@@ -633,8 +640,8 @@ export class Feed {
633640}
634641
635642let theReader = null ;
636- export let wakeups = new Wakeups ( ) ;
637- export let feed = new Feed ( ) ;
643+ export let wakeups = new Wakeups ( ) ; // NOSONAR - reset() replaces it, and an importer reads the live binding
644+ export let feed = new Feed ( ) ; // NOSONAR - reset() replaces it, and an importer reads the live binding
638645
639646export function reader ( ) {
640647 theReader ??= new MemoryReader ( ) ;
@@ -850,8 +857,7 @@ export function keepAliveMs() {
850857 if ( [ '0' , '0s' , '0ms' , '0m' ] . includes ( raw ) ) return 0 ;
851858 const match = / ^ ( \d + ) ( m s | s | m ) ? $ / . exec ( raw ) ;
852859 if ( ! match ) return 30_000 ;
853- const unit = match [ 2 ] ?? 's' ;
854- return Number . parseInt ( match [ 1 ] , 10 ) * ( unit === 'ms' ? 1 : unit === 'm' ? 60_000 : 1000 ) ;
860+ return Number . parseInt ( match [ 1 ] , 10 ) * UNIT_MS [ match [ 2 ] ?? 's' ] ;
855861}
856862
857863/** A changed row as the `question` event's data: serialized here, so a null stays a null. */
0 commit comments