Skip to content

Commit 64119e6

Browse files
committed
run deno example in pipeline
1 parent 04875a8 commit 64119e6

File tree

3 files changed

+88
-49
lines changed

3 files changed

+88
-49
lines changed

.github/workflows/examples.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Examples
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
15+
- name: Set up Go
16+
uses: actions/setup-go@v2
17+
with:
18+
go-version: 1.17
19+
20+
- name: Add rtcv_scraper_client to path
21+
run: go install
22+
23+
- name: Set up Deno
24+
uses: denolib/setup-deno@v2
25+
with:
26+
deno-version: 1.16
27+
28+
- name: Run Deno example
29+
run: deno run -A examples/deno/main.ts

.github/workflows/go.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ on:
77
branches: [ main ]
88

99
jobs:
10-
1110
build:
1211
runs-on: ubuntu-latest
1312
steps:
@@ -19,4 +18,4 @@ jobs:
1918
go-version: 1.17
2019

2120
- name: Build
22-
run: go build -v ./...
21+
run: go build

examples/deno/main.ts

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
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

33
export 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();
8694
await 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

95105
const 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

Comments
 (0)