|
| 1 | +import { readLines } from 'https://deno.land/[email protected]/io/mod.ts' |
| 2 | + |
| 3 | +export class RtCvClient { |
| 4 | + private proc: Deno.Process |
| 5 | + private textEncoder = new TextEncoder() |
| 6 | + private firstLineReader: Promise<string> |
| 7 | + private linePromiseResolveFn: (value: string) => void = () => { }; |
| 8 | + |
| 9 | + constructor() { |
| 10 | + this.proc = Deno.run({ |
| 11 | + cmd: ['rtcv_scraper_client'], |
| 12 | + stdin: 'piped', |
| 13 | + stdout: 'piped', |
| 14 | + }) |
| 15 | + |
| 16 | + this.panicOnExit() |
| 17 | + this.awaitProcStdOut() |
| 18 | + this.firstLineReader = this.readLine() |
| 19 | + } |
| 20 | + |
| 21 | + async authenticate(credentials: { |
| 22 | + serverLocation: string, |
| 23 | + keyId: string |
| 24 | + key: string |
| 25 | + mock?: boolean |
| 26 | + mockSecrets?: { [key: string]: unknown } |
| 27 | + }) { |
| 28 | + // await the ready message |
| 29 | + await this.firstLineReader |
| 30 | + |
| 31 | + try { |
| 32 | + await this.rwLine( |
| 33 | + 'set_credentials', |
| 34 | + credentials.mock |
| 35 | + ? { |
| 36 | + mock: { secrets: credentials.mockSecrets }, |
| 37 | + } |
| 38 | + : { |
| 39 | + server_location: credentials.serverLocation, |
| 40 | + api_key_id: credentials.keyId, |
| 41 | + api_key: credentials.key, |
| 42 | + } |
| 43 | + ) |
| 44 | + } 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) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // This is the main way of communicating with the client |
| 52 | + 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 |
| 56 | + } |
| 57 | + |
| 58 | + private async panicOnExit() { |
| 59 | + await this.proc.status() |
| 60 | + console.log("rtcv scraper client stopped unexpectedly") |
| 61 | + Deno.exit(1) |
| 62 | + } |
| 63 | + |
| 64 | + private async awaitProcStdOut() { |
| 65 | + for await (const line of readLines(this.proc.stdout as (Deno.Reader & Deno.Closer))) { |
| 66 | + this.linePromiseResolveFn(line) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + 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) |
| 73 | + if (parsedLine.type === "error") { |
| 74 | + throw `request send to rtcv_scraper_client returned an error: ${parsedLine.content}` |
| 75 | + } |
| 76 | + return parsedLine.content |
| 77 | + } |
| 78 | + |
| 79 | + 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) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +const rtcvClient = new RtCvClient() |
| 86 | +await rtcvClient.authenticate({ |
| 87 | + key: 'abc', |
| 88 | + keyId: '1', |
| 89 | + serverLocation: 'http://localhost:4000', |
| 90 | + mock: false, |
| 91 | + mockSecrets: { user: { username: "foo", password: "bar" } }, |
| 92 | +}) |
| 93 | +console.log('authenticated to RTCV') |
| 94 | + |
| 95 | +const siteLoginCredentials = await rtcvClient.rwLine( |
| 96 | + 'get_user_secret', |
| 97 | + { |
| 98 | + "encryption_key": "my-very-secret-encryption-key", |
| 99 | + "key": "user", |
| 100 | + }, |
| 101 | +) |
| 102 | +console.log(siteLoginCredentials) |
0 commit comments