1- import { readLines } from ' https://deno.land/[email protected] /io/mod.ts' 1+ import { readLines } from " https://deno.land/[email protected] /io/mod.ts" ; 22
33export class RtCvClient {
4- private proc : Deno . Process
5- private textEncoder = new TextEncoder ( )
6- private firstLineReader : Promise < string >
4+ private proc : Deno . Process ;
5+ private textEncoder = new TextEncoder ( ) ;
6+ private firstLineReader : Promise < string > ;
77 private linePromiseResolveFn : ( value : string ) => void = ( ) => { } ;
88
99 constructor ( ) {
1010 this . proc = Deno . run ( {
11- cmd : [ ' rtcv_scraper_client' ] ,
12- stdin : ' piped' ,
13- stdout : ' piped' ,
14- } )
11+ cmd : [ " rtcv_scraper_client" ] ,
12+ stdin : " piped" ,
13+ stdout : " piped" ,
14+ } ) ;
1515
16- this . panicOnExit ( )
17- this . awaitProcStdOut ( )
18- this . firstLineReader = this . readLine ( )
16+ this . panicOnExit ( ) ;
17+ this . awaitProcStdOut ( ) ;
18+ this . firstLineReader = this . readLine ( ) ;
1919 }
2020
2121 async authenticate ( credentials : {
22- serverLocation : string ,
23- keyId : string
24- key : string
25- mock ?: boolean
26- mockSecrets ?: { [ key : string ] : unknown }
22+ serverLocation ? : string ;
23+ keyId ? : string ;
24+ key ? : string ;
25+ mock ?: boolean ;
26+ mockSecrets ?: { [ key : string ] : unknown } ;
2727 } ) {
2828 // await the ready message
29- await this . firstLineReader
29+ await this . firstLineReader ;
3030
3131 try {
3232 await this . rwLine (
33- ' set_credentials' ,
33+ " set_credentials" ,
3434 credentials . mock
3535 ? {
3636 mock : { secrets : credentials . mockSecrets } ,
@@ -39,64 +39,75 @@ export class RtCvClient {
3939 server_location : credentials . serverLocation ,
4040 api_key_id : credentials . keyId ,
4141 api_key : credentials . key ,
42- }
43- )
42+ } ,
43+ ) ;
4444 } catch ( e ) {
45- console . log ( e )
46- console . log ( 'Hint: did you set the envourment variables using your shell or the .env?' )
47- Deno . exit ( 1 )
45+ console . log ( e ) ;
46+ console . log (
47+ "Hint: did you set the envourment variables using your shell or the .env?" ,
48+ ) ;
49+ Deno . exit ( 1 ) ;
4850 }
4951 }
5052
5153 // This is the main way of communicating with the client
5254 async rwLine < In , Out > ( type : string , content : In ) : Promise < Out > {
53- const lineReader = this . readLine < Out > ( )
54- await this . writeLine ( type , content )
55- return await lineReader
55+ const lineReader = this . readLine < Out > ( ) ;
56+ await this . writeLine ( type , content ) ;
57+ return await lineReader ;
5658 }
5759
5860 private async panicOnExit ( ) {
59- await this . proc . status ( )
60- console . log ( "rtcv scraper client stopped unexpectedly" )
61- Deno . exit ( 1 )
61+ await this . proc . status ( ) ;
62+ console . log ( "rtcv scraper client stopped unexpectedly" ) ;
63+ Deno . exit ( 1 ) ;
6264 }
6365
6466 private async awaitProcStdOut ( ) {
65- for await ( const line of readLines ( this . proc . stdout as ( Deno . Reader & Deno . Closer ) ) ) {
66- this . linePromiseResolveFn ( line )
67+ for await (
68+ const line of readLines ( this . proc . stdout as ( Deno . Reader & Deno . Closer ) )
69+ ) {
70+ this . linePromiseResolveFn ( line ) ;
6771 }
6872 }
6973
7074 private async readLine < T > ( ) : Promise < T > {
71- const line = await new Promise < string > ( res => this . linePromiseResolveFn = res )
72- const parsedLine : { type : string , content : T } = JSON . parse ( line )
75+ const line = await new Promise < string > ( ( res ) =>
76+ this . linePromiseResolveFn = res
77+ ) ;
78+ const parsedLine : { type : string ; content : T } = JSON . parse ( line ) ;
7379 if ( parsedLine . type === "error" ) {
74- throw `request send to rtcv_scraper_client returned an error: ${ parsedLine . content } `
80+ throw `request send to rtcv_scraper_client returned an error: ${ parsedLine . content } ` ;
7581 }
76- return parsedLine . content
82+ return parsedLine . content ;
7783 }
7884
7985 private async writeLine < T > ( type : string , content : T ) : Promise < void > {
80- const input = this . textEncoder . encode ( JSON . stringify ( { type, content } ) + '\n' )
81- await this . proc . stdin ?. write ( input )
86+ const input = this . textEncoder . encode (
87+ JSON . stringify ( { type, content } ) + "\n" ,
88+ ) ;
89+ await this . proc . stdin ?. write ( input ) ;
8290 }
8391}
8492
85- const rtcvClient = new RtCvClient ( )
93+ const rtcvClient = new RtCvClient ( ) ;
8694await rtcvClient . authenticate ( {
87- key : 'abc' ,
88- keyId : '1' ,
89- serverLocation : 'http://localhost:4000' ,
90- mock : false ,
95+ mock : true ,
9196 mockSecrets : { user : { username : "foo" , password : "bar" } } ,
92- } )
93- console . log ( 'authenticated to RTCV' )
97+ } ) ;
98+ // await rtcvClient.authenticate({
99+ // key: 'abc',
100+ // keyId: '1',
101+ // serverLocation: 'http://localhost:4000',
102+ // })
103+ console . log ( "authenticated to RTCV" ) ;
94104
95105const siteLoginCredentials = await rtcvClient . rwLine (
96- ' get_user_secret' ,
106+ " get_user_secret" ,
97107 {
98108 "encryption_key" : "my-very-secret-encryption-key" ,
99109 "key" : "user" ,
100110 } ,
101- )
102- console . log ( siteLoginCredentials )
111+ ) ;
112+ console . log ( siteLoginCredentials ) ;
113+ Deno . exit ( 0 ) ;
0 commit comments