11/* eslint-disable unicorn/no-process-exit */
22import type { QueryEngineBase } from '@comunica/actor-init-query' ;
3- import type { Session } from '@rubensworks/solid-client-authn-isomorphic' ;
43import { SparqlMcpServer } from '@comunica/utils-mcp' ;
4+ import type { Session } from '@rubensworks/solid-client-authn-isomorphic' ;
55import yargs from 'yargs' ;
66import { hideBin } from 'yargs/helpers' ;
77
@@ -60,64 +60,63 @@ export function runCliSolid(queryEngine: QueryEngineBase, version: string): void
6060 }
6161
6262 // Add session to query engine context if authenticated
63+ let wrappedQueryEngine = queryEngine ;
6364 if ( session ) {
65+ // Create a wrapper that adds the session to all query contexts
66+ wrappedQueryEngine = Object . create ( queryEngine ) ;
6467 const originalQuery = queryEngine . query . bind ( queryEngine ) ;
65- queryEngine . query = function ( query : string , context ?: any ) : any {
66- const contextWithAuth = {
68+ const originalQueryBindings = queryEngine . queryBindings . bind ( queryEngine ) ;
69+ const originalQueryQuads = queryEngine . queryQuads . bind ( queryEngine ) ;
70+ const originalQueryBoolean = queryEngine . queryBoolean . bind ( queryEngine ) ;
71+ const originalQueryVoid = queryEngine . queryVoid . bind ( queryEngine ) ;
72+
73+ wrappedQueryEngine . query = function ( query : any , context ?: any ) : any {
74+ return originalQuery ( query , {
6775 ...context ,
6876 '@comunica/actor-http-inrupt-solid-client-authn:session' : session ,
69- } ;
70- return originalQuery ( query , contextWithAuth ) ;
77+ } ) ;
7178 } ;
7279
73- const originalQueryBindings = queryEngine . queryBindings . bind ( queryEngine ) ;
74- queryEngine . queryBindings = function ( query : string , context ?: any ) : any {
75- const contextWithAuth = {
80+ wrappedQueryEngine . queryBindings = function ( query : any , context ?: any ) : any {
81+ return originalQueryBindings ( query , {
7682 ...context ,
7783 '@comunica/actor-http-inrupt-solid-client-authn:session' : session ,
78- } ;
79- return originalQueryBindings ( query , contextWithAuth ) ;
84+ } ) ;
8085 } ;
8186
82- const originalQueryQuads = queryEngine . queryQuads . bind ( queryEngine ) ;
83- queryEngine . queryQuads = function ( query : string , context ?: any ) : any {
84- const contextWithAuth = {
87+ wrappedQueryEngine . queryQuads = function ( query : any , context ?: any ) : any {
88+ return originalQueryQuads ( query , {
8589 ...context ,
8690 '@comunica/actor-http-inrupt-solid-client-authn:session' : session ,
87- } ;
88- return originalQueryQuads ( query , contextWithAuth ) ;
91+ } ) ;
8992 } ;
9093
91- const originalQueryBoolean = queryEngine . queryBoolean . bind ( queryEngine ) ;
92- queryEngine . queryBoolean = function ( query : string , context ?: any ) : any {
93- const contextWithAuth = {
94+ wrappedQueryEngine . queryBoolean = function ( query : any , context ?: any ) : any {
95+ return originalQueryBoolean ( query , {
9496 ...context ,
9597 '@comunica/actor-http-inrupt-solid-client-authn:session' : session ,
96- } ;
97- return originalQueryBoolean ( query , contextWithAuth ) ;
98+ } ) ;
9899 } ;
99100
100- const originalQueryVoid = queryEngine . queryVoid . bind ( queryEngine ) ;
101- queryEngine . queryVoid = function ( query : string , context ?: any ) : any {
102- const contextWithAuth = {
101+ wrappedQueryEngine . queryVoid = function ( query : any , context ?: any ) : any {
102+ return originalQueryVoid ( query , {
103103 ...context ,
104104 '@comunica/actor-http-inrupt-solid-client-authn:session' : session ,
105- } ;
106- return originalQueryVoid ( query , contextWithAuth ) ;
105+ } ) ;
107106 } ;
108107 }
109108
110109 const server = new SparqlMcpServer (
111110 'http' ,
112111 argv . port ,
113- queryEngine ,
112+ wrappedQueryEngine ,
114113 version ,
115114 process . stderr ,
116115 defaultSources ,
117116 ) ;
118117
119118 // Handle graceful shutdown
120- const cleanup = async ( ) => {
119+ const cleanup = async ( ) : Promise < void > => {
121120 if ( session ) {
122121 try {
123122 await session . logout ( ) ;
@@ -128,22 +127,20 @@ export function runCliSolid(queryEngine: QueryEngineBase, version: string): void
128127 }
129128 } ;
130129
131- process . on ( 'SIGINT' , async ( ) => {
132- await cleanup ( ) ;
133- process . exit ( 0 ) ;
130+ process . on ( 'SIGINT' , ( ) => {
131+ cleanup ( ) . then ( ( ) => process . exit ( 0 ) ) . catch ( ( ) => process . exit ( 1 ) ) ;
134132 } ) ;
135133
136- process . on ( 'SIGTERM' , async ( ) => {
137- await cleanup ( ) ;
138- process . exit ( 0 ) ;
134+ process . on ( 'SIGTERM' , ( ) => {
135+ cleanup ( ) . then ( ( ) => process . exit ( 0 ) ) . catch ( ( ) => process . exit ( 1 ) ) ;
139136 } ) ;
140137
141- server . start ( ) . catch ( ( error ) => {
138+ await server . start ( ) . catch ( ( error ) => {
142139 process . stderr . write ( `Server error: ${ error . message } \n` ) ;
143140 if ( error . stack ) {
144141 process . stderr . write ( `${ error . stack } \n` ) ;
145142 }
146- cleanup ( ) . finally ( ( ) => process . exit ( 1 ) ) ;
143+ return cleanup ( ) . finally ( ( ) => process . exit ( 1 ) ) ;
147144 } ) ;
148145 } ) ( ) . catch ( ( error ) => {
149146 process . stderr . write ( `Initialization error: ${ error . message } \n` ) ;
0 commit comments