11import  {  deserialize  }  from  'bson' ; 
22import  *  as  http  from  'http' ; 
33
4- import  {  errors ,  logger  }  from  '@powersync/lib-services-framework' ; 
4+ import  {  ErrorCode ,   errors ,  logger  }  from  '@powersync/lib-services-framework' ; 
55import  {  ReactiveSocketRouter ,  RSocketRequestMeta  }  from  '@powersync/service-rsocket-router' ; 
66
77import  {  ServiceContext  }  from  '../system/ServiceContext.js' ; 
@@ -22,19 +22,19 @@ export function configureRSocket(router: ReactiveSocketRouter<Context>, options:
2222  const  {  route_generators =  DEFAULT_SOCKET_ROUTES ,  server,  service_context }  =  options ; 
2323
2424  router . applyWebSocketEndpoints ( server ,  { 
25-     contextProvider : async  ( data : Buffer )  =>  { 
25+     contextProvider : async  ( data : Buffer ) :  Promise < Context   &   {   token :  string   } >  =>  { 
2626      const  {  token,  user_agent }  =  RSocketContextMeta . decode ( deserialize ( data )  as  any ) ; 
2727
2828      if  ( ! token )  { 
29-         throw  new  errors . AuthorizationError ( 'No token provided' ) ; 
29+         throw  new  errors . AuthorizationError2 ( ErrorCode . PSYNC_S2115 ,   'No token provided' ) ; 
3030      } 
3131
3232      try  { 
3333        const  extracted_token  =  getTokenFromHeader ( token ) ; 
3434        if  ( extracted_token  !=  null )  { 
35-           const  {  context,  errors :  token_errors  }  =  await  generateContext ( options . service_context ,  extracted_token ) ; 
35+           const  {  context,  tokenError  }  =  await  generateContext ( options . service_context ,  extracted_token ) ; 
3636          if  ( context ?. token_payload  ==  null )  { 
37-             throw  new  errors . AuthorizationError ( token_errors   ??  'Authentication required' ) ; 
37+             throw  new  errors . AuthorizationError2 ( ErrorCode . PSYNC_S2115 ,  'Authentication required' ) ; 
3838          } 
3939
4040          if  ( ! service_context . routerEngine )  { 
@@ -45,11 +45,12 @@ export function configureRSocket(router: ReactiveSocketRouter<Context>, options:
4545            token, 
4646            user_agent, 
4747            ...context , 
48-             token_errors :  token_errors , 
48+             token_error :  tokenError , 
4949            service_context : service_context  as  RouterServiceContext 
5050          } ; 
5151        }  else  { 
52-           throw  new  errors . AuthorizationError ( 'No token provided' ) ; 
52+           // Token field is present, but did not contain a token. 
53+           throw  new  errors . AuthorizationError2 ( ErrorCode . PSYNC_S2115 ,  'No valid token provided' ) ; 
5354        } 
5455      }  catch  ( ex )  { 
5556        logger . error ( ex ) ; 
0 commit comments